Consistency: make the output buffer the first argument of functions
[FFMpeg-mirror/DVCPRO-HD.git] / libavdevice / audio.c
blob4fcd3afea0d2f5150abab9919b150244374ef5d6
1 /*
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
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28 #ifdef HAVE_SOUNDCARD_H
29 #include <soundcard.h>
30 #else
31 #include <sys/soundcard.h>
32 #endif
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/time.h>
38 #include "libavutil/log.h"
39 #include "libavcodec/avcodec.h"
40 #include "libavformat/avformat.h"
42 #define AUDIO_BLOCK_SIZE 4096
44 typedef struct {
45 int fd;
46 int sample_rate;
47 int channels;
48 int frame_size; /* in bytes ! */
49 int codec_id;
50 int flip_left : 1;
51 uint8_t buffer[AUDIO_BLOCK_SIZE];
52 int buffer_ptr;
53 } AudioData;
55 static int audio_open(AudioData *s, int is_output, const char *audio_device)
57 int audio_fd;
58 int tmp, err;
59 char *flip = getenv("AUDIO_FLIP_LEFT");
61 if (is_output)
62 audio_fd = open(audio_device, O_WRONLY);
63 else
64 audio_fd = open(audio_device, O_RDONLY);
65 if (audio_fd < 0) {
66 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
67 return AVERROR(EIO);
70 if (flip && *flip == '1') {
71 s->flip_left = 1;
74 /* non blocking mode */
75 if (!is_output)
76 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
78 s->frame_size = AUDIO_BLOCK_SIZE;
79 #if 0
80 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
81 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
82 if (err < 0) {
83 perror("SNDCTL_DSP_SETFRAGMENT");
85 #endif
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) {
92 tmp = AFMT_S16_BE;
93 } else if (tmp & AFMT_S16_LE) {
94 tmp = AFMT_S16_LE;
95 } else {
96 tmp = 0;
98 #else
99 if (tmp & AFMT_S16_LE) {
100 tmp = AFMT_S16_LE;
101 } else if (tmp & AFMT_S16_BE) {
102 tmp = AFMT_S16_BE;
103 } else {
104 tmp = 0;
106 #endif
108 switch(tmp) {
109 case AFMT_S16_LE:
110 s->codec_id = CODEC_ID_PCM_S16LE;
111 break;
112 case AFMT_S16_BE:
113 s->codec_id = CODEC_ID_PCM_S16BE;
114 break;
115 default:
116 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
117 close(audio_fd);
118 return AVERROR(EIO);
120 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
121 if (err < 0) {
122 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
123 goto fail;
126 tmp = (s->channels == 2);
127 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
128 if (err < 0) {
129 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
130 goto fail;
132 if (tmp)
133 s->channels = 2;
135 tmp = s->sample_rate;
136 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
137 if (err < 0) {
138 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
139 goto fail;
141 s->sample_rate = tmp; /* store real sample rate */
142 s->fd = audio_fd;
144 return 0;
145 fail:
146 close(audio_fd);
147 return AVERROR(EIO);
150 static int audio_close(AudioData *s)
152 close(s->fd);
153 return 0;
156 /* sound output support */
157 static int audio_write_header(AVFormatContext *s1)
159 AudioData *s = s1->priv_data;
160 AVStream *st;
161 int ret;
163 st = s1->streams[0];
164 s->sample_rate = st->codec->sample_rate;
165 s->channels = st->codec->channels;
166 ret = audio_open(s, 1, s1->filename);
167 if (ret < 0) {
168 return AVERROR(EIO);
169 } else {
170 return 0;
174 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
176 AudioData *s = s1->priv_data;
177 int len, ret;
178 int size= pkt->size;
179 uint8_t *buf= pkt->data;
181 while (size > 0) {
182 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
183 if (len > size)
184 len = size;
185 memcpy(s->buffer + s->buffer_ptr, buf, len);
186 s->buffer_ptr += len;
187 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
188 for(;;) {
189 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
190 if (ret > 0)
191 break;
192 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
193 return AVERROR(EIO);
195 s->buffer_ptr = 0;
197 buf += len;
198 size -= len;
200 return 0;
203 static int audio_write_trailer(AVFormatContext *s1)
205 AudioData *s = s1->priv_data;
207 audio_close(s);
208 return 0;
211 /* grab support */
213 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
215 AudioData *s = s1->priv_data;
216 AVStream *st;
217 int ret;
219 if (ap->sample_rate <= 0 || ap->channels <= 0)
220 return -1;
222 st = av_new_stream(s1, 0);
223 if (!st) {
224 return AVERROR(ENOMEM);
226 s->sample_rate = ap->sample_rate;
227 s->channels = ap->channels;
229 ret = audio_open(s, 0, s1->filename);
230 if (ret < 0) {
231 av_free(st);
232 return AVERROR(EIO);
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 */
242 return 0;
245 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
247 AudioData *s = s1->priv_data;
248 int ret, bdelay;
249 int64_t cur_time;
250 struct audio_buf_info abufi;
252 if (av_new_packet(pkt, s->frame_size) < 0)
253 return AVERROR(EIO);
254 for(;;) {
255 struct timeval tv;
256 fd_set fds;
258 tv.tv_sec = 0;
259 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
261 FD_ZERO(&fds);
262 FD_SET(s->fd, &fds);
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);
268 if (ret > 0)
269 break;
270 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
271 av_free_packet(pkt);
272 pkt->size = 0;
273 pkt->pts = av_gettime();
274 return 0;
276 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
277 av_free_packet(pkt);
278 return AVERROR(EIO);
281 pkt->size = ret;
283 /* compute pts of the start of the packet */
284 cur_time = av_gettime();
285 bdelay = ret;
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 */
293 pkt->pts = cur_time;
295 if (s->flip_left && s->channels == 2) {
296 int i;
297 short *p = (short *) pkt->data;
299 for (i = 0; i < ret; i += 4) {
300 *p = ~*p;
301 p += 2;
304 return 0;
307 static int audio_read_close(AVFormatContext *s1)
309 AudioData *s = s1->priv_data;
311 audio_close(s);
312 return 0;
315 #ifdef CONFIG_OSS_DEMUXER
316 AVInputFormat oss_demuxer = {
317 "oss",
318 NULL_IF_CONFIG_SMALL("audio grab and output"),
319 sizeof(AudioData),
320 NULL,
321 audio_read_header,
322 audio_read_packet,
323 audio_read_close,
324 .flags = AVFMT_NOFILE,
326 #endif
328 #ifdef CONFIG_OSS_MUXER
329 AVOutputFormat oss_muxer = {
330 "oss",
331 NULL_IF_CONFIG_SMALL("audio grab and output"),
334 sizeof(AudioData),
335 /* XXX: we make the assumption that the soundcard accepts this format */
336 /* XXX: find better solution with "preinit" method, needed also in
337 other formats */
338 #ifdef WORDS_BIGENDIAN
339 CODEC_ID_PCM_S16BE,
340 #else
341 CODEC_ID_PCM_S16LE,
342 #endif
343 CODEC_ID_NONE,
344 audio_write_header,
345 audio_write_packet,
346 audio_write_trailer,
347 .flags = AVFMT_NOFILE,
349 #endif