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
27 #include <soundcard.h>
29 #include <sys/soundcard.h>
33 #include <sys/ioctl.h>
37 #define AUDIO_BLOCK_SIZE 4096
43 int frame_size; /* in bytes ! */
46 uint8_t buffer[AUDIO_BLOCK_SIZE];
50 static int audio_open(AudioData *s, int is_output, const char *audio_device)
54 char *flip = getenv("AUDIO_FLIP_LEFT");
56 /* open linux audio device */
59 audio_device = "/dev/sound";
61 audio_device = "/dev/dsp";
65 audio_fd = open(audio_device, O_WRONLY);
67 audio_fd = open(audio_device, O_RDONLY);
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 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
84 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
86 perror("SNDCTL_DSP_SETFRAGMENT");
90 /* select format : favour native format */
91 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
93 #ifdef WORDS_BIGENDIAN
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 = CODEC_ID_PCM_S16LE;
116 s->codec_id = CODEC_ID_PCM_S16BE;
119 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
123 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
125 perror("SNDCTL_DSP_SETFMT");
129 tmp = (s->channels == 2);
130 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
132 perror("SNDCTL_DSP_STEREO");
138 tmp = s->sample_rate;
139 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
141 perror("SNDCTL_DSP_SPEED");
144 s->sample_rate = tmp; /* store real sample rate */
153 static int audio_close(AudioData *s)
159 /* sound output support */
160 static int audio_write_header(AVFormatContext *s1)
162 AudioData *s = s1->priv_data;
167 s->sample_rate = st->codec->sample_rate;
168 s->channels = st->codec->channels;
169 ret = audio_open(s, 1, NULL);
177 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
179 AudioData *s = s1->priv_data;
182 uint8_t *buf= pkt->data;
185 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
188 memcpy(s->buffer + s->buffer_ptr, buf, len);
189 s->buffer_ptr += len;
190 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
192 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
195 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
206 static int audio_write_trailer(AVFormatContext *s1)
208 AudioData *s = s1->priv_data;
216 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
218 AudioData *s = s1->priv_data;
222 if (ap->sample_rate <= 0 || ap->channels <= 0)
225 st = av_new_stream(s1, 0);
229 s->sample_rate = ap->sample_rate;
230 s->channels = ap->channels;
232 ret = audio_open(s, 0, ap->device);
238 /* take real parameters */
239 st->codec->codec_type = CODEC_TYPE_AUDIO;
240 st->codec->codec_id = s->codec_id;
241 st->codec->sample_rate = s->sample_rate;
242 st->codec->channels = s->channels;
244 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
248 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
250 AudioData *s = s1->priv_data;
253 struct audio_buf_info abufi;
255 if (av_new_packet(pkt, s->frame_size) < 0)
262 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
267 /* This will block until data is available or we get a timeout */
268 (void) select(s->fd + 1, &fds, 0, 0, &tv);
270 ret = read(s->fd, pkt->data, pkt->size);
273 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
276 pkt->pts = av_gettime();
279 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
286 /* compute pts of the start of the packet */
287 cur_time = av_gettime();
289 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
290 bdelay += abufi.bytes;
292 /* substract time represented by the number of bytes in the audio fifo */
293 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
295 /* convert to wanted units */
298 if (s->flip_left && s->channels == 2) {
300 short *p = (short *) pkt->data;
302 for (i = 0; i < ret; i += 4) {
310 static int audio_read_close(AVFormatContext *s1)
312 AudioData *s = s1->priv_data;
318 #ifdef CONFIG_AUDIO_DEMUXER
319 AVInputFormat audio_demuxer = {
321 "audio grab and output",
327 .flags = AVFMT_NOFILE,
331 #ifdef CONFIG_AUDIO_MUXER
332 AVOutputFormat audio_muxer = {
334 "audio grab and output",
338 /* XXX: we make the assumption that the soundcard accepts this format */
339 /* XXX: find better solution with "preinit" method, needed also in
341 #ifdef WORDS_BIGENDIAN
350 .flags = AVFMT_NOFILE,