4fa155c85de4c54df410d3a69ea50b6bd0650f67
[libav.git] / libavformat / audio.c
1 /*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/soundcard.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/time.h>
30
31 const char *audio_device = "/dev/dsp";
32
33 #define AUDIO_BLOCK_SIZE 4096
34
35 typedef struct {
36 int fd;
37 int sample_rate;
38 int channels;
39 int frame_size; /* in bytes ! */
40 int codec_id;
41 int flip_left : 1;
42 UINT8 buffer[AUDIO_BLOCK_SIZE];
43 int buffer_ptr;
44 } AudioData;
45
46 static int audio_open(AudioData *s, int is_output)
47 {
48 int audio_fd;
49 int tmp, err;
50 char *flip = getenv("AUDIO_FLIP_LEFT");
51
52 /* open linux audio device */
53 if (is_output)
54 audio_fd = open(audio_device, O_WRONLY);
55 else
56 audio_fd = open(audio_device, O_RDONLY);
57 if (audio_fd < 0) {
58 perror(audio_device);
59 return -EIO;
60 }
61
62 if (flip && *flip == '1') {
63 s->flip_left = 1;
64 }
65
66 /* non blocking mode */
67 if (!is_output)
68 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
69
70 s->frame_size = AUDIO_BLOCK_SIZE;
71 #if 0
72 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
73 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
74 if (err < 0) {
75 perror("SNDCTL_DSP_SETFRAGMENT");
76 }
77 #endif
78
79 /* select format : favour native format */
80 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
81
82 #ifdef WORDS_BIGENDIAN
83 if (tmp & AFMT_S16_BE) {
84 tmp = AFMT_S16_BE;
85 } else if (tmp & AFMT_S16_LE) {
86 tmp = AFMT_S16_LE;
87 } else {
88 tmp = 0;
89 }
90 #else
91 if (tmp & AFMT_S16_LE) {
92 tmp = AFMT_S16_LE;
93 } else if (tmp & AFMT_S16_BE) {
94 tmp = AFMT_S16_BE;
95 } else {
96 tmp = 0;
97 }
98 #endif
99
100 switch(tmp) {
101 case AFMT_S16_LE:
102 s->codec_id = CODEC_ID_PCM_S16LE;
103 break;
104 case AFMT_S16_BE:
105 s->codec_id = CODEC_ID_PCM_S16BE;
106 break;
107 default:
108 fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
109 close(audio_fd);
110 return -EIO;
111 }
112 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
113 if (err < 0) {
114 perror("SNDCTL_DSP_SETFMT");
115 goto fail;
116 }
117
118 tmp = (s->channels == 2);
119 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
120 if (err < 0) {
121 perror("SNDCTL_DSP_STEREO");
122 goto fail;
123 }
124 if (tmp)
125 s->channels = 2;
126
127 tmp = s->sample_rate;
128 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
129 if (err < 0) {
130 perror("SNDCTL_DSP_SPEED");
131 goto fail;
132 }
133 s->sample_rate = tmp; /* store real sample rate */
134 s->fd = audio_fd;
135
136 return 0;
137 fail:
138 close(audio_fd);
139 return -EIO;
140 }
141
142 static int audio_close(AudioData *s)
143 {
144 close(s->fd);
145 return 0;
146 }
147
148 /* sound output support */
149 static int audio_write_header(AVFormatContext *s1)
150 {
151 AudioData *s = s1->priv_data;
152 AVStream *st;
153 int ret;
154
155 st = s1->streams[0];
156 s->sample_rate = st->codec.sample_rate;
157 s->channels = st->codec.channels;
158 ret = audio_open(s, 1);
159 if (ret < 0) {
160 return -EIO;
161 } else {
162 return 0;
163 }
164 }
165
166 static int audio_write_packet(AVFormatContext *s1, int stream_index,
167 UINT8 *buf, int size, int force_pts)
168 {
169 AudioData *s = s1->priv_data;
170 int len, ret;
171
172 while (size > 0) {
173 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
174 if (len > size)
175 len = size;
176 memcpy(s->buffer + s->buffer_ptr, buf, len);
177 s->buffer_ptr += len;
178 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
179 for(;;) {
180 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
181 if (ret > 0)
182 break;
183 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
184 return -EIO;
185 }
186 s->buffer_ptr = 0;
187 }
188 buf += len;
189 size -= len;
190 }
191 return 0;
192 }
193
194 static int audio_write_trailer(AVFormatContext *s1)
195 {
196 AudioData *s = s1->priv_data;
197
198 audio_close(s);
199 return 0;
200 }
201
202 /* grab support */
203
204 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
205 {
206 AudioData *s = s1->priv_data;
207 AVStream *st;
208 int ret;
209
210 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
211 return -1;
212
213 st = av_new_stream(s1, 0);
214 if (!st) {
215 return -ENOMEM;
216 }
217 s->sample_rate = ap->sample_rate;
218 s->channels = ap->channels;
219
220 ret = audio_open(s, 0);
221 if (ret < 0) {
222 av_free(st);
223 return -EIO;
224 }
225
226 /* take real parameters */
227 st->codec.codec_type = CODEC_TYPE_AUDIO;
228 st->codec.codec_id = s->codec_id;
229 st->codec.sample_rate = s->sample_rate;
230 st->codec.channels = s->channels;
231
232 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
233 return 0;
234 }
235
236 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
237 {
238 AudioData *s = s1->priv_data;
239 int ret, bdelay;
240 int64_t cur_time;
241 struct audio_buf_info abufi;
242
243 if (av_new_packet(pkt, s->frame_size) < 0)
244 return -EIO;
245 for(;;) {
246 ret = read(s->fd, pkt->data, pkt->size);
247 if (ret > 0)
248 break;
249 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
250 av_free_packet(pkt);
251 pkt->size = 0;
252 return 0;
253 }
254 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
255 av_free_packet(pkt);
256 return -EIO;
257 }
258 }
259 pkt->size = ret;
260
261 /* compute pts of the start of the packet */
262 cur_time = av_gettime();
263 bdelay = ret;
264 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
265 bdelay += abufi.bytes;
266 }
267 /* substract time represented by the number of bytes in the audio fifo */
268 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
269
270 /* convert to wanted units */
271 pkt->pts = cur_time & ((1LL << 48) - 1);
272
273 if (s->flip_left && s->channels == 2) {
274 int i;
275 short *p = (short *) pkt->data;
276
277 for (i = 0; i < ret; i += 4) {
278 *p = ~*p;
279 p += 2;
280 }
281 }
282 return 0;
283 }
284
285 static int audio_read_close(AVFormatContext *s1)
286 {
287 AudioData *s = s1->priv_data;
288
289 audio_close(s);
290 return 0;
291 }
292
293 static AVInputFormat audio_in_format = {
294 "audio_device",
295 "audio grab and output",
296 sizeof(AudioData),
297 NULL,
298 audio_read_header,
299 audio_read_packet,
300 audio_read_close,
301 .flags = AVFMT_NOFILE,
302 };
303
304 static AVOutputFormat audio_out_format = {
305 "audio_device",
306 "audio grab and output",
307 "",
308 "",
309 sizeof(AudioData),
310 /* XXX: we make the assumption that the soundcard accepts this format */
311 /* XXX: find better solution with "preinit" method, needed also in
312 other formats */
313 #ifdef WORDS_BIGENDIAN
314 CODEC_ID_PCM_S16BE,
315 #else
316 CODEC_ID_PCM_S16LE,
317 #endif
318 CODEC_ID_NONE,
319 audio_write_header,
320 audio_write_packet,
321 audio_write_trailer,
322 .flags = AVFMT_NOFILE,
323 };
324
325 int audio_init(void)
326 {
327 av_register_input_format(&audio_in_format);
328 av_register_output_format(&audio_out_format);
329 return 0;
330 }