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
28 #ifdef HAVE_SOUNDCARD_H
29 #include <soundcard.h>
31 #include <sys/soundcard.h>
35 #include <sys/ioctl.h>
38 #include "libavutil/log.h"
39 #include "libavcodec/avcodec.h"
40 #include "libavformat/avformat.h"
42 #define AUDIO_BLOCK_SIZE 4096
48 int frame_size; /* in bytes ! */
50 unsigned int flip_left : 1;
51 uint8_t buffer[AUDIO_BLOCK_SIZE];
55 static int audio_open(AudioData *s, int is_output, const char *audio_device)
59 char *flip = getenv("AUDIO_FLIP_LEFT");
62 audio_fd = open(audio_device, O_WRONLY);
64 audio_fd = open(audio_device, O_RDONLY);
66 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
70 if (flip && *flip == '1') {
74 /* non blocking mode */
76 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
78 s->frame_size = AUDIO_BLOCK_SIZE;
80 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
81 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
83 perror("SNDCTL_DSP_SETFRAGMENT");
87 /* select format : favour native format */
88 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
90 #ifdef WORDS_BIGENDIAN
91 if (tmp & AFMT_S16_BE) {
93 } else if (tmp & AFMT_S16_LE) {
99 if (tmp & AFMT_S16_LE) {
101 } else if (tmp & AFMT_S16_BE) {
110 s->codec_id = CODEC_ID_PCM_S16LE;
113 s->codec_id = CODEC_ID_PCM_S16BE;
116 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
120 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
122 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
126 tmp = (s->channels == 2);
127 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
133 tmp = s->sample_rate;
134 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
136 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
139 s->sample_rate = tmp; /* store real sample rate */
148 static int audio_close(AudioData *s)
154 /* sound output support */
155 static int audio_write_header(AVFormatContext *s1)
157 AudioData *s = s1->priv_data;
162 s->sample_rate = st->codec->sample_rate;
163 s->channels = st->codec->channels;
164 ret = audio_open(s, 1, s1->filename);
172 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
174 AudioData *s = s1->priv_data;
177 uint8_t *buf= pkt->data;
180 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
183 memcpy(s->buffer + s->buffer_ptr, buf, len);
184 s->buffer_ptr += len;
185 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
187 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
190 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
201 static int audio_write_trailer(AVFormatContext *s1)
203 AudioData *s = s1->priv_data;
211 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
213 AudioData *s = s1->priv_data;
217 if (ap->sample_rate <= 0 || ap->channels <= 0)
220 st = av_new_stream(s1, 0);
222 return AVERROR(ENOMEM);
224 s->sample_rate = ap->sample_rate;
225 s->channels = ap->channels;
227 ret = audio_open(s, 0, s1->filename);
233 /* take real parameters */
234 st->codec->codec_type = CODEC_TYPE_AUDIO;
235 st->codec->codec_id = s->codec_id;
236 st->codec->sample_rate = s->sample_rate;
237 st->codec->channels = s->channels;
239 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
243 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
245 AudioData *s = s1->priv_data;
248 struct audio_buf_info abufi;
250 if (av_new_packet(pkt, s->frame_size) < 0)
257 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
262 /* This will block until data is available or we get a timeout */
263 (void) select(s->fd + 1, &fds, 0, 0, &tv);
265 ret = read(s->fd, pkt->data, pkt->size);
268 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
271 pkt->pts = av_gettime();
274 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
281 /* compute pts of the start of the packet */
282 cur_time = av_gettime();
284 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
285 bdelay += abufi.bytes;
287 /* subtract time represented by the number of bytes in the audio fifo */
288 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
290 /* convert to wanted units */
293 if (s->flip_left && s->channels == 2) {
295 short *p = (short *) pkt->data;
297 for (i = 0; i < ret; i += 4) {
305 static int audio_read_close(AVFormatContext *s1)
307 AudioData *s = s1->priv_data;
313 #ifdef CONFIG_OSS_DEMUXER
314 AVInputFormat oss_demuxer = {
316 NULL_IF_CONFIG_SMALL("audio grab and output"),
322 .flags = AVFMT_NOFILE,
326 #ifdef CONFIG_OSS_MUXER
327 AVOutputFormat oss_muxer = {
329 NULL_IF_CONFIG_SMALL("audio grab and output"),
333 /* XXX: we make the assumption that the soundcard accepts this format */
334 /* XXX: find better solution with "preinit" method, needed also in
336 #ifdef WORDS_BIGENDIAN
345 .flags = AVFMT_NOFILE,