Simplify if() in copy_and_dup()
[ffmpeg-lucabe.git] / libavdevice / audio.c
blob1cb3d69409018be7cf72cc108893aa726a8facdb
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 unsigned 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;
133 tmp = s->sample_rate;
134 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
135 if (err < 0) {
136 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
137 goto fail;
139 s->sample_rate = tmp; /* store real sample rate */
140 s->fd = audio_fd;
142 return 0;
143 fail:
144 close(audio_fd);
145 return AVERROR(EIO);
148 static int audio_close(AudioData *s)
150 close(s->fd);
151 return 0;
154 /* sound output support */
155 static int audio_write_header(AVFormatContext *s1)
157 AudioData *s = s1->priv_data;
158 AVStream *st;
159 int ret;
161 st = s1->streams[0];
162 s->sample_rate = st->codec->sample_rate;
163 s->channels = st->codec->channels;
164 ret = audio_open(s, 1, s1->filename);
165 if (ret < 0) {
166 return AVERROR(EIO);
167 } else {
168 return 0;
172 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
174 AudioData *s = s1->priv_data;
175 int len, ret;
176 int size= pkt->size;
177 uint8_t *buf= pkt->data;
179 while (size > 0) {
180 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
181 if (len > size)
182 len = size;
183 memcpy(s->buffer + s->buffer_ptr, buf, len);
184 s->buffer_ptr += len;
185 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
186 for(;;) {
187 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
188 if (ret > 0)
189 break;
190 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
191 return AVERROR(EIO);
193 s->buffer_ptr = 0;
195 buf += len;
196 size -= len;
198 return 0;
201 static int audio_write_trailer(AVFormatContext *s1)
203 AudioData *s = s1->priv_data;
205 audio_close(s);
206 return 0;
209 /* grab support */
211 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
213 AudioData *s = s1->priv_data;
214 AVStream *st;
215 int ret;
217 if (ap->sample_rate <= 0 || ap->channels <= 0)
218 return -1;
220 st = av_new_stream(s1, 0);
221 if (!st) {
222 return AVERROR(ENOMEM);
224 s->sample_rate = ap->sample_rate;
225 s->channels = ap->channels;
227 ret = audio_open(s, 0, s1->filename);
228 if (ret < 0) {
229 av_free(st);
230 return AVERROR(EIO);
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 */
240 return 0;
243 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
245 AudioData *s = s1->priv_data;
246 int ret, bdelay;
247 int64_t cur_time;
248 struct audio_buf_info abufi;
250 if (av_new_packet(pkt, s->frame_size) < 0)
251 return AVERROR(EIO);
252 for(;;) {
253 struct timeval tv;
254 fd_set fds;
256 tv.tv_sec = 0;
257 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
259 FD_ZERO(&fds);
260 FD_SET(s->fd, &fds);
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);
266 if (ret > 0)
267 break;
268 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
269 av_free_packet(pkt);
270 pkt->size = 0;
271 pkt->pts = av_gettime();
272 return 0;
274 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
275 av_free_packet(pkt);
276 return AVERROR(EIO);
279 pkt->size = ret;
281 /* compute pts of the start of the packet */
282 cur_time = av_gettime();
283 bdelay = ret;
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 */
291 pkt->pts = cur_time;
293 if (s->flip_left && s->channels == 2) {
294 int i;
295 short *p = (short *) pkt->data;
297 for (i = 0; i < ret; i += 4) {
298 *p = ~*p;
299 p += 2;
302 return 0;
305 static int audio_read_close(AVFormatContext *s1)
307 AudioData *s = s1->priv_data;
309 audio_close(s);
310 return 0;
313 #ifdef CONFIG_OSS_DEMUXER
314 AVInputFormat oss_demuxer = {
315 "oss",
316 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
317 sizeof(AudioData),
318 NULL,
319 audio_read_header,
320 audio_read_packet,
321 audio_read_close,
322 .flags = AVFMT_NOFILE,
324 #endif
326 #ifdef CONFIG_OSS_MUXER
327 AVOutputFormat oss_muxer = {
328 "oss",
329 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
332 sizeof(AudioData),
333 /* XXX: we make the assumption that the soundcard accepts this format */
334 /* XXX: find better solution with "preinit" method, needed also in
335 other formats */
336 #ifdef WORDS_BIGENDIAN
337 CODEC_ID_PCM_S16BE,
338 #else
339 CODEC_ID_PCM_S16LE,
340 #endif
341 CODEC_ID_NONE,
342 audio_write_header,
343 audio_write_packet,
344 audio_write_trailer,
345 .flags = AVFMT_NOFILE,
347 #endif