| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Linux audio play interface | ||
| 3 | * Copyright (c) 2000, 2001 Fabrice Bellard | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "config.h" | ||
| 23 | |||
| 24 | #include <stdint.h> | ||
| 25 | |||
| 26 | #if HAVE_UNISTD_H | ||
| 27 | #include <unistd.h> | ||
| 28 | #endif | ||
| 29 | #include <fcntl.h> | ||
| 30 | #include <sys/ioctl.h> | ||
| 31 | #include <sys/soundcard.h> | ||
| 32 | |||
| 33 | #include "libavutil/internal.h" | ||
| 34 | #include "libavutil/opt.h" | ||
| 35 | #include "libavutil/time.h" | ||
| 36 | |||
| 37 | #include "avdevice.h" | ||
| 38 | #include "libavformat/demux.h" | ||
| 39 | #include "libavformat/internal.h" | ||
| 40 | |||
| 41 | #include "oss.h" | ||
| 42 | |||
| 43 | ✗ | static int audio_read_header(AVFormatContext *s1) | |
| 44 | { | ||
| 45 | ✗ | OSSAudioData *s = s1->priv_data; | |
| 46 | AVStream *st; | ||
| 47 | int ret; | ||
| 48 | |||
| 49 | ✗ | st = avformat_new_stream(s1, NULL); | |
| 50 | ✗ | if (!st) { | |
| 51 | ✗ | return AVERROR(ENOMEM); | |
| 52 | } | ||
| 53 | |||
| 54 | ✗ | ret = ff_oss_audio_open(s1, 0, s1->url); | |
| 55 | ✗ | if (ret < 0) { | |
| 56 | ✗ | return AVERROR(EIO); | |
| 57 | } | ||
| 58 | |||
| 59 | /* take real parameters */ | ||
| 60 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 61 | ✗ | st->codecpar->codec_id = s->codec_id; | |
| 62 | ✗ | st->codecpar->sample_rate = s->sample_rate; | |
| 63 | ✗ | st->codecpar->ch_layout.nb_channels = s->channels; | |
| 64 | |||
| 65 | ✗ | avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ | |
| 66 | ✗ | return 0; | |
| 67 | } | ||
| 68 | |||
| 69 | ✗ | static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
| 70 | { | ||
| 71 | ✗ | OSSAudioData *s = s1->priv_data; | |
| 72 | int ret, bdelay; | ||
| 73 | int64_t cur_time; | ||
| 74 | struct audio_buf_info abufi; | ||
| 75 | |||
| 76 | ✗ | if ((ret=av_new_packet(pkt, s->frame_size)) < 0) | |
| 77 | ✗ | return ret; | |
| 78 | |||
| 79 | ✗ | ret = read(s->fd, pkt->data, pkt->size); | |
| 80 | ✗ | if (ret <= 0){ | |
| 81 | ✗ | av_packet_unref(pkt); | |
| 82 | ✗ | pkt->size = 0; | |
| 83 | ✗ | if (ret<0) return AVERROR(errno); | |
| 84 | ✗ | else return AVERROR_EOF; | |
| 85 | } | ||
| 86 | ✗ | pkt->size = ret; | |
| 87 | |||
| 88 | /* compute pts of the start of the packet */ | ||
| 89 | ✗ | cur_time = av_gettime(); | |
| 90 | ✗ | bdelay = ret; | |
| 91 | ✗ | if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) { | |
| 92 | ✗ | bdelay += abufi.bytes; | |
| 93 | } | ||
| 94 | /* subtract time represented by the number of bytes in the audio fifo */ | ||
| 95 | ✗ | cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->sample_size * s->channels); | |
| 96 | |||
| 97 | /* convert to wanted units */ | ||
| 98 | ✗ | pkt->pts = cur_time; | |
| 99 | |||
| 100 | ✗ | if (s->flip_left && s->channels == 2) { | |
| 101 | int i; | ||
| 102 | ✗ | short *p = (short *) pkt->data; | |
| 103 | |||
| 104 | ✗ | for (i = 0; i < ret; i += 4) { | |
| 105 | ✗ | *p = ~*p; | |
| 106 | ✗ | p += 2; | |
| 107 | } | ||
| 108 | } | ||
| 109 | ✗ | return 0; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | static int audio_read_close(AVFormatContext *s1) | |
| 113 | { | ||
| 114 | ✗ | OSSAudioData *s = s1->priv_data; | |
| 115 | |||
| 116 | ✗ | ff_oss_audio_close(s); | |
| 117 | ✗ | return 0; | |
| 118 | } | ||
| 119 | |||
| 120 | static const AVOption options[] = { | ||
| 121 | { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, | ||
| 122 | { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, | ||
| 123 | { NULL }, | ||
| 124 | }; | ||
| 125 | |||
| 126 | static const AVClass oss_demuxer_class = { | ||
| 127 | .class_name = "OSS indev", | ||
| 128 | .item_name = av_default_item_name, | ||
| 129 | .option = options, | ||
| 130 | .version = LIBAVUTIL_VERSION_INT, | ||
| 131 | .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT, | ||
| 132 | }; | ||
| 133 | |||
| 134 | const FFInputFormat ff_oss_demuxer = { | ||
| 135 | .p.name = "oss", | ||
| 136 | .p.long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"), | ||
| 137 | .p.flags = AVFMT_NOFILE, | ||
| 138 | .p.priv_class = &oss_demuxer_class, | ||
| 139 | .priv_data_size = sizeof(OSSAudioData), | ||
| 140 | .read_header = audio_read_header, | ||
| 141 | .read_packet = audio_read_packet, | ||
| 142 | .read_close = audio_read_close, | ||
| 143 | }; | ||
| 144 |