Use enum typers instead of int.
[libav.git] / libavdevice / audio.c
1 /*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
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>
37
38 #include "libavutil/log.h"
39 #include "libavcodec/avcodec.h"
40 #include "libavformat/avformat.h"
41
42 #define AUDIO_BLOCK_SIZE 4096
43
44 typedef struct {
45 int fd;
46 int sample_rate;
47 int channels;
48 int frame_size; /* in bytes ! */
49 enum CodecID codec_id;
50 unsigned int flip_left : 1;
51 uint8_t buffer[AUDIO_BLOCK_SIZE];
52 int buffer_ptr;
53 } AudioData;
54
55 static int audio_open(AudioData *s, int is_output, const char *audio_device)
56 {
57 int audio_fd;
58 int tmp, err;
59 char *flip = getenv("AUDIO_FLIP_LEFT");
60
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);
68 }
69
70 if (flip && *flip == '1') {
71 s->flip_left = 1;
72 }
73
74 /* non blocking mode */
75 if (!is_output)
76 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
77
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");
84 }
85 #endif
86
87 /* select format : favour native format */
88 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
89
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;
97 }
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;
105 }
106 #endif
107
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);
119 }
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;
124 }
125
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;
131 }
132
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;
138 }
139 s->sample_rate = tmp; /* store real sample rate */
140 s->fd = audio_fd;
141
142 return 0;
143 fail:
144 close(audio_fd);
145 return AVERROR(EIO);
146 }
147
148 static int audio_close(AudioData *s)
149 {
150 close(s->fd);
151 return 0;
152 }
153
154 /* sound output support */
155 static int audio_write_header(AVFormatContext *s1)
156 {
157 AudioData *s = s1->priv_data;
158 AVStream *st;
159 int ret;
160
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;
169 }
170 }
171
172 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
173 {
174 AudioData *s = s1->priv_data;
175 int len, ret;
176 int size= pkt->size;
177 uint8_t *buf= pkt->data;
178
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);
192 }
193 s->buffer_ptr = 0;
194 }
195 buf += len;
196 size -= len;
197 }
198 return 0;
199 }
200
201 static int audio_write_trailer(AVFormatContext *s1)
202 {
203 AudioData *s = s1->priv_data;
204
205 audio_close(s);
206 return 0;
207 }
208
209 /* grab support */
210
211 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
212 {
213 AudioData *s = s1->priv_data;
214 AVStream *st;
215 int ret;
216
217 if (ap->sample_rate <= 0 || ap->channels <= 0)
218 return -1;
219
220 st = av_new_stream(s1, 0);
221 if (!st) {
222 return AVERROR(ENOMEM);
223 }
224 s->sample_rate = ap->sample_rate;
225 s->channels = ap->channels;
226
227 ret = audio_open(s, 0, s1->filename);
228 if (ret < 0) {
229 av_free(st);
230 return AVERROR(EIO);
231 }
232
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;
238
239 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
240 return 0;
241 }
242
243 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
244 {
245 AudioData *s = s1->priv_data;
246 int ret, bdelay;
247 int64_t cur_time;
248 struct audio_buf_info abufi;
249
250 if (av_new_packet(pkt, s->frame_size) < 0)
251 return AVERROR(EIO);
252 for(;;) {
253 struct timeval tv;
254 fd_set fds;
255
256 tv.tv_sec = 0;
257 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
258
259 FD_ZERO(&fds);
260 FD_SET(s->fd, &fds);
261
262 /* This will block until data is available or we get a timeout */
263 (void) select(s->fd + 1, &fds, 0, 0, &tv);
264
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;
273 }
274 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
275 av_free_packet(pkt);
276 return AVERROR(EIO);
277 }
278 }
279 pkt->size = ret;
280
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;
286 }
287 /* subtract time represented by the number of bytes in the audio fifo */
288 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
289
290 /* convert to wanted units */
291 pkt->pts = cur_time;
292
293 if (s->flip_left && s->channels == 2) {
294 int i;
295 short *p = (short *) pkt->data;
296
297 for (i = 0; i < ret; i += 4) {
298 *p = ~*p;
299 p += 2;
300 }
301 }
302 return 0;
303 }
304
305 static int audio_read_close(AVFormatContext *s1)
306 {
307 AudioData *s = s1->priv_data;
308
309 audio_close(s);
310 return 0;
311 }
312
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,
323 };
324 #endif
325
326 #ifdef CONFIG_OSS_MUXER
327 AVOutputFormat oss_muxer = {
328 "oss",
329 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
330 "",
331 "",
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,
346 };
347 #endif