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>
37 #include <sys/ioctl.h>
39 #include "libavutil/internal.h"
40 #include "libavutil/log.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/time.h"
43 #include "libavcodec/avcodec.h"
45 #include "libavformat/internal.h"
47 #define AUDIO_BLOCK_SIZE 4096
54 int frame_size; /* in bytes ! */
55 enum AVCodecID codec_id;
56 unsigned int flip_left : 1;
57 uint8_t buffer[AUDIO_BLOCK_SIZE];
61 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
63 AudioData *s = s1->priv_data;
66 char *flip = getenv("AUDIO_FLIP_LEFT");
69 audio_fd = avpriv_open(audio_device, O_WRONLY);
71 audio_fd = avpriv_open(audio_device, O_RDONLY);
73 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
77 if (flip && *flip == '1') {
81 /* non blocking mode */
83 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
84 av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
88 s->frame_size = AUDIO_BLOCK_SIZE;
90 /* select format : favour native format */
91 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
94 if (tmp & AFMT_S16_BE) {
96 } else if (tmp & AFMT_S16_LE) {
102 if (tmp & AFMT_S16_LE) {
104 } else if (tmp & AFMT_S16_BE) {
113 s->codec_id = AV_CODEC_ID_PCM_S16LE;
116 s->codec_id = AV_CODEC_ID_PCM_S16BE;
119 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
123 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
125 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
129 tmp = (s->channels == 2);
130 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
132 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
136 tmp = s->sample_rate;
137 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
139 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
142 s->sample_rate = tmp; /* store real sample rate */
151 static int audio_close(AudioData *s)
157 /* sound output support */
158 static int audio_write_header(AVFormatContext *s1)
160 AudioData *s = s1->priv_data;
165 s->sample_rate = st->codec->sample_rate;
166 s->channels = st->codec->channels;
167 ret = audio_open(s1, 1, s1->filename);
175 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
177 AudioData *s = s1->priv_data;
180 uint8_t *buf= pkt->data;
183 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
184 memcpy(s->buffer + s->buffer_ptr, buf, len);
185 s->buffer_ptr += len;
186 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
188 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
191 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
202 static int audio_write_trailer(AVFormatContext *s1)
204 AudioData *s = s1->priv_data;
212 static int audio_read_header(AVFormatContext *s1)
214 AudioData *s = s1->priv_data;
218 st = avformat_new_stream(s1, NULL);
220 return AVERROR(ENOMEM);
223 ret = audio_open(s1, 0, s1->filename);
228 /* take real parameters */
229 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
230 st->codec->codec_id = s->codec_id;
231 st->codec->sample_rate = s->sample_rate;
232 st->codec->channels = s->channels;
234 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
238 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
240 AudioData *s = s1->priv_data;
243 struct audio_buf_info abufi;
245 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
248 ret = read(s->fd, pkt->data, pkt->size);
252 if (ret<0) return AVERROR(errno);
253 else return AVERROR_EOF;
257 /* compute pts of the start of the packet */
258 cur_time = av_gettime();
260 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
261 bdelay += abufi.bytes;
263 /* subtract time represented by the number of bytes in the audio fifo */
264 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
266 /* convert to wanted units */
269 if (s->flip_left && s->channels == 2) {
271 short *p = (short *) pkt->data;
273 for (i = 0; i < ret; i += 4) {
281 static int audio_read_close(AVFormatContext *s1)
283 AudioData *s = s1->priv_data;
290 static const AVOption options[] = {
291 { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
292 { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
296 static const AVClass oss_demuxer_class = {
297 .class_name = "OSS demuxer",
298 .item_name = av_default_item_name,
300 .version = LIBAVUTIL_VERSION_INT,
301 .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
304 AVInputFormat ff_oss_demuxer = {
306 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
307 .priv_data_size = sizeof(AudioData),
308 .read_header = audio_read_header,
309 .read_packet = audio_read_packet,
310 .read_close = audio_read_close,
311 .flags = AVFMT_NOFILE,
312 .priv_class = &oss_demuxer_class,
316 #if CONFIG_OSS_OUTDEV
317 static const AVClass oss_muxer_class = {
318 .class_name = "OSS muxer",
319 .item_name = av_default_item_name,
320 .version = LIBAVUTIL_VERSION_INT,
321 .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
324 AVOutputFormat ff_oss_muxer = {
326 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
327 .priv_data_size = sizeof(AudioData),
328 /* XXX: we make the assumption that the soundcard accepts this format */
329 /* XXX: find better solution with "preinit" method, needed also in
331 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
332 .video_codec = AV_CODEC_ID_NONE,
333 .write_header = audio_write_header,
334 .write_packet = audio_write_packet,
335 .write_trailer = audio_write_trailer,
336 .flags = AVFMT_NOFILE,
337 .priv_class = &oss_muxer_class,