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"
43 #include "libavformat/internal.h"
45 #define AUDIO_BLOCK_SIZE 4096
52 int frame_size; /* in bytes ! */
53 enum CodecID codec_id;
54 unsigned int flip_left : 1;
55 uint8_t buffer[AUDIO_BLOCK_SIZE];
59 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
61 AudioData *s = s1->priv_data;
64 char *flip = getenv("AUDIO_FLIP_LEFT");
67 audio_fd = open(audio_device, O_WRONLY);
69 audio_fd = open(audio_device, O_RDONLY);
71 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
75 if (flip && *flip == '1') {
79 /* non blocking mode */
81 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
83 s->frame_size = AUDIO_BLOCK_SIZE;
85 /* select format : favour native format */
86 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
89 if (tmp & AFMT_S16_BE) {
91 } else if (tmp & AFMT_S16_LE) {
97 if (tmp & AFMT_S16_LE) {
99 } else if (tmp & AFMT_S16_BE) {
108 s->codec_id = CODEC_ID_PCM_S16LE;
111 s->codec_id = CODEC_ID_PCM_S16BE;
114 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
118 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
120 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
124 tmp = (s->channels == 2);
125 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
127 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
131 tmp = s->sample_rate;
132 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
134 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
137 s->sample_rate = tmp; /* store real sample rate */
146 static int audio_close(AudioData *s)
152 /* sound output support */
153 static int audio_write_header(AVFormatContext *s1)
155 AudioData *s = s1->priv_data;
160 s->sample_rate = st->codec->sample_rate;
161 s->channels = st->codec->channels;
162 ret = audio_open(s1, 1, s1->filename);
170 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
172 AudioData *s = s1->priv_data;
175 uint8_t *buf= pkt->data;
178 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
179 memcpy(s->buffer + s->buffer_ptr, buf, len);
180 s->buffer_ptr += len;
181 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
183 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
186 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
197 static int audio_write_trailer(AVFormatContext *s1)
199 AudioData *s = s1->priv_data;
207 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
209 AudioData *s = s1->priv_data;
213 st = avformat_new_stream(s1, NULL);
215 return AVERROR(ENOMEM);
218 ret = audio_open(s1, 0, s1->filename);
223 /* take real parameters */
224 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
225 st->codec->codec_id = s->codec_id;
226 st->codec->sample_rate = s->sample_rate;
227 st->codec->channels = s->channels;
229 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
233 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
235 AudioData *s = s1->priv_data;
238 struct audio_buf_info abufi;
240 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
243 ret = read(s->fd, pkt->data, pkt->size);
247 if (ret<0) return AVERROR(errno);
248 else return AVERROR_EOF;
252 /* compute pts of the start of the packet */
253 cur_time = av_gettime();
255 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
256 bdelay += abufi.bytes;
258 /* subtract time represented by the number of bytes in the audio fifo */
259 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
261 /* convert to wanted units */
264 if (s->flip_left && s->channels == 2) {
266 short *p = (short *) pkt->data;
268 for (i = 0; i < ret; i += 4) {
276 static int audio_read_close(AVFormatContext *s1)
278 AudioData *s = s1->priv_data;
285 static const AVOption options[] = {
286 { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
287 { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
291 static const AVClass oss_demuxer_class = {
292 .class_name = "OSS demuxer",
293 .item_name = av_default_item_name,
295 .version = LIBAVUTIL_VERSION_INT,
298 AVInputFormat ff_oss_demuxer = {
300 .long_name = NULL_IF_CONFIG_SMALL("Open Sound System capture"),
301 .priv_data_size = sizeof(AudioData),
302 .read_header = audio_read_header,
303 .read_packet = audio_read_packet,
304 .read_close = audio_read_close,
305 .flags = AVFMT_NOFILE,
306 .priv_class = &oss_demuxer_class,
310 #if CONFIG_OSS_OUTDEV
311 AVOutputFormat ff_oss_muxer = {
313 .long_name = NULL_IF_CONFIG_SMALL("Open Sound System playback"),
314 .priv_data_size = sizeof(AudioData),
315 /* XXX: we make the assumption that the soundcard accepts this format */
316 /* XXX: find better solution with "preinit" method, needed also in
318 .audio_codec = AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
319 .video_codec = CODEC_ID_NONE,
320 .write_header = audio_write_header,
321 .write_packet = audio_write_packet,
322 .write_trailer = audio_write_trailer,
323 .flags = AVFMT_NOFILE,