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
26 #ifdef HAVE_SOUNDCARD_H
27 #include <soundcard.h>
29 #include <sys/soundcard.h>
33 #include <sys/ioctl.h>
36 #define AUDIO_BLOCK_SIZE 4096
42 int frame_size; /* in bytes ! */
45 uint8_t buffer[AUDIO_BLOCK_SIZE];
49 static int audio_open(AudioData *s, int is_output, const char *audio_device)
53 char *flip = getenv("AUDIO_FLIP_LEFT");
56 audio_fd = open(audio_device, O_WRONLY);
58 audio_fd = open(audio_device, O_RDONLY);
60 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
64 if (flip && *flip == '1') {
68 /* non blocking mode */
70 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
72 s->frame_size = AUDIO_BLOCK_SIZE;
74 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
75 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
77 perror("SNDCTL_DSP_SETFRAGMENT");
81 /* select format : favour native format */
82 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
84 #ifdef WORDS_BIGENDIAN
85 if (tmp & AFMT_S16_BE) {
87 } else if (tmp & AFMT_S16_LE) {
93 if (tmp & AFMT_S16_LE) {
95 } else if (tmp & AFMT_S16_BE) {
104 s->codec_id = CODEC_ID_PCM_S16LE;
107 s->codec_id = CODEC_ID_PCM_S16BE;
110 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
114 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
116 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
120 tmp = (s->channels == 2);
121 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
123 av_log(NULL, 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(NULL, 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(s, 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 = AUDIO_BLOCK_SIZE - s->buffer_ptr;
179 memcpy(s->buffer + s->buffer_ptr, buf, len);
180 s->buffer_ptr += len;
181 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
183 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
186 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
197 static int audio_write_trailer(AVFormatContext *s1)
199 AudioData *s = s1->priv_data;
207 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
209 AudioData *s = s1->priv_data;
213 if (ap->sample_rate <= 0 || ap->channels <= 0)
216 st = av_new_stream(s1, 0);
218 return AVERROR(ENOMEM);
220 s->sample_rate = ap->sample_rate;
221 s->channels = ap->channels;
223 ret = audio_open(s, 0, s1->filename);
229 /* take real parameters */
230 st->codec->codec_type = CODEC_TYPE_AUDIO;
231 st->codec->codec_id = s->codec_id;
232 st->codec->sample_rate = s->sample_rate;
233 st->codec->channels = s->channels;
235 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
239 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
241 AudioData *s = s1->priv_data;
244 struct audio_buf_info abufi;
246 if (av_new_packet(pkt, s->frame_size) < 0)
253 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
258 /* This will block until data is available or we get a timeout */
259 (void) select(s->fd + 1, &fds, 0, 0, &tv);
261 ret = read(s->fd, pkt->data, pkt->size);
264 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
267 pkt->pts = av_gettime();
270 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
277 /* compute pts of the start of the packet */
278 cur_time = av_gettime();
280 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
281 bdelay += abufi.bytes;
283 /* subtract time represented by the number of bytes in the audio fifo */
284 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
286 /* convert to wanted units */
289 if (s->flip_left && s->channels == 2) {
291 short *p = (short *) pkt->data;
293 for (i = 0; i < ret; i += 4) {
301 static int audio_read_close(AVFormatContext *s1)
303 AudioData *s = s1->priv_data;
309 #ifdef CONFIG_OSS_DEMUXER
310 AVInputFormat oss_demuxer = {
312 "audio grab and output",
318 .flags = AVFMT_NOFILE,
322 #ifdef CONFIG_OSS_MUXER
323 AVOutputFormat oss_muxer = {
325 "audio grab and output",
329 /* XXX: we make the assumption that the soundcard accepts this format */
330 /* XXX: find better solution with "preinit" method, needed also in
332 #ifdef WORDS_BIGENDIAN
341 .flags = AVFMT_NOFILE,