2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
29 #include <soundcard.h>
31 #include <sys/soundcard.h>
35 #include <sys/ioctl.h>
37 #include <sys/select.h>
39 #include "libavutil/log.h"
40 #include "libavutil/opt.h"
41 #include "libavcodec/avcodec.h"
44 #define AUDIO_BLOCK_SIZE 4096
51 int frame_size; /* in bytes ! */
52 enum CodecID codec_id;
53 unsigned int flip_left : 1;
54 uint8_t buffer[AUDIO_BLOCK_SIZE];
58 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
60 AudioData *s = s1->priv_data;
63 char *flip = getenv("AUDIO_FLIP_LEFT");
66 audio_fd = open(audio_device, O_WRONLY);
68 audio_fd = open(audio_device, O_RDONLY);
70 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
74 if (flip && *flip == '1') {
78 /* non blocking mode */
80 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
82 s->frame_size = AUDIO_BLOCK_SIZE;
84 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
85 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
87 perror("SNDCTL_DSP_SETFRAGMENT");
91 /* select format : favour native format */
92 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
95 if (tmp & AFMT_S16_BE) {
97 } else if (tmp & AFMT_S16_LE) {
103 if (tmp & AFMT_S16_LE) {
105 } else if (tmp & AFMT_S16_BE) {
114 s->codec_id = CODEC_ID_PCM_S16LE;
117 s->codec_id = CODEC_ID_PCM_S16BE;
120 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
124 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
126 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
130 tmp = (s->channels == 2);
131 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
133 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
137 tmp = s->sample_rate;
138 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
140 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
143 s->sample_rate = tmp; /* store real sample rate */
152 static int audio_close(AudioData *s)
158 /* sound output support */
159 static int audio_write_header(AVFormatContext *s1)
161 AudioData *s = s1->priv_data;
166 s->sample_rate = st->codec->sample_rate;
167 s->channels = st->codec->channels;
168 ret = audio_open(s1, 1, s1->filename);
176 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
178 AudioData *s = s1->priv_data;
181 uint8_t *buf= pkt->data;
184 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
185 memcpy(s->buffer + s->buffer_ptr, buf, len);
186 s->buffer_ptr += len;
187 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
189 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
192 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
203 static int audio_write_trailer(AVFormatContext *s1)
205 AudioData *s = s1->priv_data;
213 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
215 AudioData *s = s1->priv_data;
219 #if FF_API_FORMAT_PARAMETERS
220 if (ap->sample_rate > 0)
221 s->sample_rate = ap->sample_rate;
222 if (ap->channels > 0)
223 s->channels = ap->channels;
226 st = av_new_stream(s1, 0);
228 return AVERROR(ENOMEM);
231 ret = audio_open(s1, 0, s1->filename);
236 /* take real parameters */
237 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
238 st->codec->codec_id = s->codec_id;
239 st->codec->sample_rate = s->sample_rate;
240 st->codec->channels = s->channels;
242 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
246 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
248 AudioData *s = s1->priv_data;
251 struct audio_buf_info abufi;
253 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
256 ret = read(s->fd, pkt->data, pkt->size);
260 if (ret<0) return AVERROR(errno);
261 else return AVERROR_EOF;
265 /* compute pts of the start of the packet */
266 cur_time = av_gettime();
268 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
269 bdelay += abufi.bytes;
271 /* subtract time represented by the number of bytes in the audio fifo */
272 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
274 /* convert to wanted units */
277 if (s->flip_left && s->channels == 2) {
279 short *p = (short *) pkt->data;
281 for (i = 0; i < ret; i += 4) {
289 static int audio_read_close(AVFormatContext *s1)
291 AudioData *s = s1->priv_data;
298 static const AVOption options[] = {
299 { "sample_rate", "", offsetof(AudioData, sample_rate), FF_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
300 { "channels", "", offsetof(AudioData, channels), FF_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
304 static const AVClass oss_demuxer_class = {
305 .class_name = "OSS demuxer",
306 .item_name = av_default_item_name,
308 .version = LIBAVUTIL_VERSION_INT,
311 AVInputFormat ff_oss_demuxer = {
313 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
319 .flags = AVFMT_NOFILE,
320 .priv_class = &oss_demuxer_class,
324 #if CONFIG_OSS_OUTDEV
325 AVOutputFormat ff_oss_muxer = {
327 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
331 /* XXX: we make the assumption that the soundcard accepts this format */
332 /* XXX: find better solution with "preinit" method, needed also in
334 AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
339 .flags = AVFMT_NOFILE,