2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 "libavutil/time.h"
40 #include "libavcodec/avcodec.h"
41 #include "libavformat/avformat.h"
42 #include "libavformat/internal.h"
44 #define AUDIO_BLOCK_SIZE 4096
51 int frame_size; /* in bytes ! */
52 enum AVCodecID 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 /* select format : favour native format */
85 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
88 if (tmp & AFMT_S16_BE) {
90 } else if (tmp & AFMT_S16_LE) {
96 if (tmp & AFMT_S16_LE) {
98 } else if (tmp & AFMT_S16_BE) {
107 s->codec_id = AV_CODEC_ID_PCM_S16LE;
110 s->codec_id = AV_CODEC_ID_PCM_S16BE;
113 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
117 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
119 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
123 tmp = (s->channels == 2);
124 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
126 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
130 tmp = s->sample_rate;
131 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
133 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
136 s->sample_rate = tmp; /* store real sample rate */
145 static int audio_close(AudioData *s)
151 /* sound output support */
152 static int audio_write_header(AVFormatContext *s1)
154 AudioData *s = s1->priv_data;
159 s->sample_rate = st->codec->sample_rate;
160 s->channels = st->codec->channels;
161 ret = audio_open(s1, 1, s1->filename);
169 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
171 AudioData *s = s1->priv_data;
174 uint8_t *buf= pkt->data;
177 len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
178 memcpy(s->buffer + s->buffer_ptr, buf, len);
179 s->buffer_ptr += len;
180 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
182 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
185 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
196 static int audio_write_trailer(AVFormatContext *s1)
198 AudioData *s = s1->priv_data;
206 static int audio_read_header(AVFormatContext *s1)
208 AudioData *s = s1->priv_data;
212 st = avformat_new_stream(s1, NULL);
214 return AVERROR(ENOMEM);
217 ret = audio_open(s1, 0, s1->filename);
222 /* take real parameters */
223 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
224 st->codec->codec_id = s->codec_id;
225 st->codec->sample_rate = s->sample_rate;
226 st->codec->channels = s->channels;
228 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
232 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
234 AudioData *s = s1->priv_data;
237 struct audio_buf_info abufi;
239 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
242 ret = read(s->fd, pkt->data, pkt->size);
246 if (ret<0) return AVERROR(errno);
247 else return AVERROR_EOF;
251 /* compute pts of the start of the packet */
252 cur_time = av_gettime();
254 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
255 bdelay += abufi.bytes;
257 /* subtract time represented by the number of bytes in the audio fifo */
258 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
260 /* convert to wanted units */
263 if (s->flip_left && s->channels == 2) {
265 short *p = (short *) pkt->data;
267 for (i = 0; i < ret; i += 4) {
275 static int audio_read_close(AVFormatContext *s1)
277 AudioData *s = s1->priv_data;
284 static const AVOption options[] = {
285 { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
286 { "channels", "", offsetof(AudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
290 static const AVClass oss_demuxer_class = {
291 .class_name = "OSS demuxer",
292 .item_name = av_default_item_name,
294 .version = LIBAVUTIL_VERSION_INT,
297 AVInputFormat ff_oss_demuxer = {
299 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
300 .priv_data_size = sizeof(AudioData),
301 .read_header = audio_read_header,
302 .read_packet = audio_read_packet,
303 .read_close = audio_read_close,
304 .flags = AVFMT_NOFILE,
305 .priv_class = &oss_demuxer_class,
309 #if CONFIG_OSS_OUTDEV
310 AVOutputFormat ff_oss_muxer = {
312 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
313 .priv_data_size = sizeof(AudioData),
314 /* XXX: we make the assumption that the soundcard accepts this format */
315 /* XXX: find better solution with "preinit" method, needed also in
317 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
318 .video_codec = AV_CODEC_ID_NONE,
319 .write_header = audio_write_header,
320 .write_packet = audio_write_packet,
321 .write_trailer = audio_write_trailer,
322 .flags = AVFMT_NOFILE,