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 "libavcodec/avcodec.h"
41 #include "libavformat/avformat.h"
43 #define AUDIO_BLOCK_SIZE 4096
49 int frame_size; /* in bytes ! */
50 enum CodecID codec_id;
51 unsigned int flip_left : 1;
52 uint8_t buffer[AUDIO_BLOCK_SIZE];
56 static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
58 AudioData *s = s1->priv_data;
61 char *flip = getenv("AUDIO_FLIP_LEFT");
64 audio_fd = open(audio_device, O_WRONLY);
66 audio_fd = open(audio_device, O_RDONLY);
68 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
72 if (flip && *flip == '1') {
76 /* non blocking mode */
78 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
80 s->frame_size = AUDIO_BLOCK_SIZE;
82 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
83 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
85 perror("SNDCTL_DSP_SETFRAGMENT");
89 /* select format : favour native format */
90 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
92 #ifdef WORDS_BIGENDIAN
93 if (tmp & AFMT_S16_BE) {
95 } else if (tmp & AFMT_S16_LE) {
101 if (tmp & AFMT_S16_LE) {
103 } else if (tmp & AFMT_S16_BE) {
112 s->codec_id = CODEC_ID_PCM_S16LE;
115 s->codec_id = CODEC_ID_PCM_S16BE;
118 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
122 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
124 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
128 tmp = (s->channels == 2);
129 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
131 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
135 tmp = s->sample_rate;
136 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
138 av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
141 s->sample_rate = tmp; /* store real sample rate */
150 static int audio_close(AudioData *s)
156 /* sound output support */
157 static int audio_write_header(AVFormatContext *s1)
159 AudioData *s = s1->priv_data;
164 s->sample_rate = st->codec->sample_rate;
165 s->channels = st->codec->channels;
166 ret = audio_open(s1, 1, s1->filename);
174 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
176 AudioData *s = s1->priv_data;
179 uint8_t *buf= pkt->data;
182 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
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 (ap->sample_rate <= 0 || ap->channels <= 0)
222 st = av_new_stream(s1, 0);
224 return AVERROR(ENOMEM);
226 s->sample_rate = ap->sample_rate;
227 s->channels = ap->channels;
229 ret = audio_open(s1, 0, s1->filename);
235 /* take real parameters */
236 st->codec->codec_type = CODEC_TYPE_AUDIO;
237 st->codec->codec_id = s->codec_id;
238 st->codec->sample_rate = s->sample_rate;
239 st->codec->channels = s->channels;
241 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
245 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
247 AudioData *s = s1->priv_data;
250 struct audio_buf_info abufi;
252 if (av_new_packet(pkt, s->frame_size) < 0)
259 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
264 /* This will block until data is available or we get a timeout */
265 (void) select(s->fd + 1, &fds, 0, 0, &tv);
267 ret = read(s->fd, pkt->data, pkt->size);
270 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
273 pkt->pts = av_gettime();
276 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
283 /* compute pts of the start of the packet */
284 cur_time = av_gettime();
286 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
287 bdelay += abufi.bytes;
289 /* subtract time represented by the number of bytes in the audio fifo */
290 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
292 /* convert to wanted units */
295 if (s->flip_left && s->channels == 2) {
297 short *p = (short *) pkt->data;
299 for (i = 0; i < ret; i += 4) {
307 static int audio_read_close(AVFormatContext *s1)
309 AudioData *s = s1->priv_data;
315 #if CONFIG_OSS_DEMUXER
316 AVInputFormat oss_demuxer = {
318 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
324 .flags = AVFMT_NOFILE,
329 AVOutputFormat oss_muxer = {
331 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
335 /* XXX: we make the assumption that the soundcard accepts this format */
336 /* XXX: find better solution with "preinit" method, needed also in
338 #ifdef WORDS_BIGENDIAN
347 .flags = AVFMT_NOFILE,