Use enum typers instead of int.
[libav.git] / libavdevice / audio.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux audio play and grab interface
19720f15 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
8be1c656 21
4f786086 22#include "config.h"
de6d9b64
FB
23#include <stdlib.h>
24#include <stdio.h>
4f786086 25#include <stdint.h>
de6d9b64 26#include <string.h>
4f786086 27#include <errno.h>
74476a3a 28#ifdef HAVE_SOUNDCARD_H
8c802695
JM
29#include <soundcard.h>
30#else
b13788c5 31#include <sys/soundcard.h>
8c802695 32#endif
de6d9b64
FB
33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/ioctl.h>
de6d9b64
FB
36#include <sys/time.h>
37
245976da
DB
38#include "libavutil/log.h"
39#include "libavcodec/avcodec.h"
40#include "libavformat/avformat.h"
4f786086 41
4972b26f
FB
42#define AUDIO_BLOCK_SIZE 4096
43
de6d9b64
FB
44typedef struct {
45 int fd;
4972b26f 46 int sample_rate;
de6d9b64 47 int channels;
4972b26f 48 int frame_size; /* in bytes ! */
fb65d2ca 49 enum CodecID codec_id;
72e043dd 50 unsigned int flip_left : 1;
0c1a9eda 51 uint8_t buffer[AUDIO_BLOCK_SIZE];
4972b26f 52 int buffer_ptr;
de6d9b64
FB
53} AudioData;
54
7f172339 55static int audio_open(AudioData *s, int is_output, const char *audio_device)
de6d9b64 56{
4972b26f 57 int audio_fd;
de6d9b64 58 int tmp, err;
1de1cce2 59 char *flip = getenv("AUDIO_FLIP_LEFT");
de6d9b64 60
4972b26f
FB
61 if (is_output)
62 audio_fd = open(audio_device, O_WRONLY);
de6d9b64 63 else
4972b26f 64 audio_fd = open(audio_device, O_RDONLY);
de6d9b64 65 if (audio_fd < 0) {
9f74582c 66 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
6f3e0b21 67 return AVERROR(EIO);
de6d9b64
FB
68 }
69
1de1cce2
PG
70 if (flip && *flip == '1') {
71 s->flip_left = 1;
72 }
73
de6d9b64 74 /* non blocking mode */
4364a3e0
FB
75 if (!is_output)
76 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
de6d9b64 77
4972b26f 78 s->frame_size = AUDIO_BLOCK_SIZE;
de6d9b64 79#if 0
4972b26f
FB
80 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
81 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
de6d9b64
FB
82 if (err < 0) {
83 perror("SNDCTL_DSP_SETFRAGMENT");
84 }
85#endif
86
4972b26f
FB
87 /* select format : favour native format */
88 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
115329f1 89
4972b26f
FB
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:
bc874dae 116 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
4972b26f 117 close(audio_fd);
6f3e0b21 118 return AVERROR(EIO);
4972b26f
FB
119 }
120 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
de6d9b64 121 if (err < 0) {
9f74582c 122 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
de6d9b64
FB
123 goto fail;
124 }
115329f1 125
4972b26f
FB
126 tmp = (s->channels == 2);
127 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
de6d9b64 128 if (err < 0) {
9f74582c 129 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
de6d9b64
FB
130 goto fail;
131 }
115329f1 132
4972b26f
FB
133 tmp = s->sample_rate;
134 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
de6d9b64 135 if (err < 0) {
9f74582c 136 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
de6d9b64
FB
137 goto fail;
138 }
4972b26f 139 s->sample_rate = tmp; /* store real sample rate */
de6d9b64
FB
140 s->fd = audio_fd;
141
142 return 0;
143 fail:
144 close(audio_fd);
6f3e0b21 145 return AVERROR(EIO);
de6d9b64
FB
146}
147
4972b26f 148static int audio_close(AudioData *s)
de6d9b64 149{
de6d9b64 150 close(s->fd);
4972b26f
FB
151 return 0;
152}
153
154/* sound output support */
155static int audio_write_header(AVFormatContext *s1)
156{
c9a65ca8 157 AudioData *s = s1->priv_data;
4972b26f
FB
158 AVStream *st;
159 int ret;
160
4972b26f 161 st = s1->streams[0];
01f4895c
MN
162 s->sample_rate = st->codec->sample_rate;
163 s->channels = st->codec->channels;
7bb7ad0e 164 ret = audio_open(s, 1, s1->filename);
4972b26f 165 if (ret < 0) {
6f3e0b21 166 return AVERROR(EIO);
4972b26f
FB
167 } else {
168 return 0;
169 }
170}
171
e928649b 172static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
4972b26f
FB
173{
174 AudioData *s = s1->priv_data;
175 int len, ret;
e928649b
MN
176 int size= pkt->size;
177 uint8_t *buf= pkt->data;
4972b26f
FB
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);
4364a3e0 188 if (ret > 0)
4972b26f
FB
189 break;
190 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
6f3e0b21 191 return AVERROR(EIO);
4972b26f
FB
192 }
193 s->buffer_ptr = 0;
194 }
195 buf += len;
196 size -= len;
197 }
198 return 0;
199}
200
201static int audio_write_trailer(AVFormatContext *s1)
202{
203 AudioData *s = s1->priv_data;
204
205 audio_close(s);
de6d9b64
FB
206 return 0;
207}
208
4972b26f
FB
209/* grab support */
210
211static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
212{
c9a65ca8 213 AudioData *s = s1->priv_data;
4972b26f
FB
214 AVStream *st;
215 int ret;
216
c04c3282 217 if (ap->sample_rate <= 0 || ap->channels <= 0)
4972b26f
FB
218 return -1;
219
c9a65ca8 220 st = av_new_stream(s1, 0);
4972b26f 221 if (!st) {
8fa36ae0 222 return AVERROR(ENOMEM);
4972b26f 223 }
4972b26f
FB
224 s->sample_rate = ap->sample_rate;
225 s->channels = ap->channels;
226
cc58300e 227 ret = audio_open(s, 0, s1->filename);
4972b26f 228 if (ret < 0) {
1ea4f593 229 av_free(st);
6f3e0b21 230 return AVERROR(EIO);
4972b26f 231 }
45dd5c69
FB
232
233 /* take real parameters */
01f4895c
MN
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;
45dd5c69 238
0a7b514f 239 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
45dd5c69 240 return 0;
4972b26f
FB
241}
242
243static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
244{
245 AudioData *s = s1->priv_data;
45dd5c69
FB
246 int ret, bdelay;
247 int64_t cur_time;
248 struct audio_buf_info abufi;
115329f1 249
4972b26f 250 if (av_new_packet(pkt, s->frame_size) < 0)
6f3e0b21 251 return AVERROR(EIO);
4972b26f 252 for(;;) {
79134973
PG
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
4972b26f
FB
265 ret = read(s->fd, pkt->data, pkt->size);
266 if (ret > 0)
267 break;
4606ac8d
ZK
268 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
269 av_free_packet(pkt);
270 pkt->size = 0;
0a7b514f 271 pkt->pts = av_gettime();
4606ac8d
ZK
272 return 0;
273 }
4972b26f
FB
274 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
275 av_free_packet(pkt);
6f3e0b21 276 return AVERROR(EIO);
4972b26f
FB
277 }
278 }
279 pkt->size = ret;
45dd5c69
FB
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 }
52b541ad 287 /* subtract time represented by the number of bytes in the audio fifo */
45dd5c69
FB
288 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
289
290 /* convert to wanted units */
0a7b514f 291 pkt->pts = cur_time;
45dd5c69 292
1de1cce2
PG
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 }
4972b26f
FB
302 return 0;
303}
304
305static int audio_read_close(AVFormatContext *s1)
306{
307 AudioData *s = s1->priv_data;
308
309 audio_close(s);
4972b26f
FB
310 return 0;
311}
312
50383e88
RP
313#ifdef CONFIG_OSS_DEMUXER
314AVInputFormat oss_demuxer = {
1156c6b0 315 "oss",
c57c4897 316 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
c9a65ca8
FB
317 sizeof(AudioData),
318 NULL,
319 audio_read_header,
320 audio_read_packet,
321 audio_read_close,
bb76a117 322 .flags = AVFMT_NOFILE,
c9a65ca8 323};
ff70e601 324#endif
c9a65ca8 325
50383e88
RP
326#ifdef CONFIG_OSS_MUXER
327AVOutputFormat oss_muxer = {
1156c6b0 328 "oss",
c57c4897 329 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
4972b26f
FB
330 "",
331 "",
c9a65ca8 332 sizeof(AudioData),
4972b26f
FB
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,
bb76a117 345 .flags = AVFMT_NOFILE,
de6d9b64 346};
ff70e601 347#endif