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 "libavutil/log.h"
38 #include "libavutil/opt.h"
39 #include "libavcodec/avcodec.h"
41 #include "libavformat/internal.h"
43 #define AUDIO_BLOCK_SIZE 4096
50 int frame_size; /* in bytes ! */
51 enum CodecID codec_id;
52 unsigned int flip_left : 1;
53 uint8_t buffer[AUDIO_BLOCK_SIZE];
57 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
59 AudioData *s = s1->priv_data;
62 char *flip = getenv("AUDIO_FLIP_LEFT");
65 audio_fd = open(audio_device, O_WRONLY);
67 audio_fd = open(audio_device, O_RDONLY);
69 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
73 if (flip && *flip == '1') {
77 /* non blocking mode */
79 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
81 s->frame_size = AUDIO_BLOCK_SIZE;
83 /* select format : favour native format */
84 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
87 if (tmp & AFMT_S16_BE) {
89 } else if (tmp & AFMT_S16_LE) {
95 if (tmp & AFMT_S16_LE) {
97 } else if (tmp & AFMT_S16_BE) {
106 s->codec_id = CODEC_ID_PCM_S16LE;
109 s->codec_id = CODEC_ID_PCM_S16BE;
112 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
116 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
118 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
122 tmp = (s->channels == 2);
123 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
125 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
129 tmp = s->sample_rate;
130 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
132 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
135 s->sample_rate = tmp; /* store real sample rate */
144 static int audio_close(AudioData *s)
150 /* sound output support */
151 static int audio_write_header(AVFormatContext *s1)
153 AudioData *s = s1->priv_data;
158 s->sample_rate = st->codec->sample_rate;
159 s->channels = st->codec->channels;
160 ret = audio_open(s1, 1, s1->filename);
168 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
170 AudioData *s = s1->priv_data;
173 uint8_t *buf= pkt->data;
176 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
177 memcpy(s->buffer + s->buffer_ptr, buf, len);
178 s->buffer_ptr += len;
179 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
181 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
184 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
195 static int audio_write_trailer(AVFormatContext *s1)
197 AudioData *s = s1->priv_data;
205 static int audio_read_header(AVFormatContext *s1)
207 AudioData *s = s1->priv_data;
211 st = avformat_new_stream(s1, NULL);
213 return AVERROR(ENOMEM);
216 ret = audio_open(s1, 0, s1->filename);
221 /* take real parameters */
222 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
223 st->codec->codec_id = s->codec_id;
224 st->codec->sample_rate = s->sample_rate;
225 st->codec->channels = s->channels;
227 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
231 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
233 AudioData *s = s1->priv_data;
236 struct audio_buf_info abufi;
238 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
241 ret = read(s->fd, pkt->data, pkt->size);
245 if (ret<0) return AVERROR(errno);
246 else return AVERROR_EOF;
250 /* compute pts of the start of the packet */
251 cur_time = av_gettime();
253 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
254 bdelay += abufi.bytes;
256 /* subtract time represented by the number of bytes in the audio fifo */
257 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
259 /* convert to wanted units */
262 if (s->flip_left && s->channels == 2) {
264 short *p = (short *) pkt->data;
266 for (i = 0; i < ret; i += 4) {
274 static int audio_read_close(AVFormatContext *s1)
276 AudioData *s = s1->priv_data;
283 static const AVOption options[] = {
284 { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
285 { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
289 static const AVClass oss_demuxer_class = {
290 .class_name = "OSS demuxer",
291 .item_name = av_default_item_name,
293 .version = LIBAVUTIL_VERSION_INT,
296 AVInputFormat ff_oss_demuxer = {
298 .long_name = NULL_IF_CONFIG_SMALL("Open Sound System capture"),
299 .priv_data_size = sizeof(AudioData),
300 .read_header = audio_read_header,
301 .read_packet = audio_read_packet,
302 .read_close = audio_read_close,
303 .flags = AVFMT_NOFILE,
304 .priv_class = &oss_demuxer_class,
308 #if CONFIG_OSS_OUTDEV
309 AVOutputFormat ff_oss_muxer = {
311 .long_name = NULL_IF_CONFIG_SMALL("Open Sound System playback"),
312 .priv_data_size = sizeof(AudioData),
313 /* XXX: we make the assumption that the soundcard accepts this format */
314 /* XXX: find better solution with "preinit" method, needed also in
316 .audio_codec = AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
317 .video_codec = CODEC_ID_NONE,
318 .write_header = audio_write_header,
319 .write_packet = audio_write_packet,
320 .write_trailer = audio_write_trailer,
321 .flags = AVFMT_NOFILE,