Mercurial > libavformat.hg
annotate audio.c @ 1461:23f88db38eb5 libavformat
allow spaces in source and build directory names
out of tree builds from a source dir with spaces is impossible
due to how make handles vpath
author | mru |
---|---|
date | Wed, 08 Nov 2006 00:02:15 +0000 |
parents | 0899bfe4105c |
children | 2f59a73884af |
rev | line source |
---|---|
0 | 1 /* |
2 * Linux audio play and grab interface | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
22 | |
23 #include <stdlib.h> | |
24 #include <stdio.h> | |
25 #include <string.h> | |
754
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
26 #ifdef __OpenBSD__ |
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
27 #include <soundcard.h> |
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
28 #else |
0 | 29 #include <sys/soundcard.h> |
754
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
30 #endif |
0 | 31 #include <unistd.h> |
32 #include <fcntl.h> | |
33 #include <sys/ioctl.h> | |
34 #include <sys/mman.h> | |
35 #include <sys/time.h> | |
36 | |
37 #define AUDIO_BLOCK_SIZE 4096 | |
38 | |
39 typedef struct { | |
40 int fd; | |
41 int sample_rate; | |
42 int channels; | |
43 int frame_size; /* in bytes ! */ | |
44 int codec_id; | |
45 int flip_left : 1; | |
65 | 46 uint8_t buffer[AUDIO_BLOCK_SIZE]; |
0 | 47 int buffer_ptr; |
48 } AudioData; | |
49 | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
50 static int audio_open(AudioData *s, int is_output, const char *audio_device) |
0 | 51 { |
52 int audio_fd; | |
53 int tmp, err; | |
54 char *flip = getenv("AUDIO_FLIP_LEFT"); | |
55 | |
56 /* open linux audio device */ | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
57 if (!audio_device) |
754
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
58 #ifdef __OpenBSD__ |
887 | 59 audio_device = "/dev/sound"; |
754
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
60 #else |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
61 audio_device = "/dev/dsp"; |
754
aa52767bb802
OpenBSD support patch by (Jacob Meuser // jakemsr jakemsr com)
michael
parents:
482
diff
changeset
|
62 #endif |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
63 |
0 | 64 if (is_output) |
65 audio_fd = open(audio_device, O_WRONLY); | |
66 else | |
67 audio_fd = open(audio_device, O_RDONLY); | |
68 if (audio_fd < 0) { | |
69 perror(audio_device); | |
482 | 70 return AVERROR_IO; |
0 | 71 } |
72 | |
73 if (flip && *flip == '1') { | |
74 s->flip_left = 1; | |
75 } | |
76 | |
77 /* non blocking mode */ | |
78 if (!is_output) | |
79 fcntl(audio_fd, F_SETFL, O_NONBLOCK); | |
80 | |
81 s->frame_size = AUDIO_BLOCK_SIZE; | |
82 #if 0 | |
83 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS; | |
84 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp); | |
85 if (err < 0) { | |
86 perror("SNDCTL_DSP_SETFRAGMENT"); | |
87 } | |
88 #endif | |
89 | |
90 /* select format : favour native format */ | |
91 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp); | |
885 | 92 |
0 | 93 #ifdef WORDS_BIGENDIAN |
94 if (tmp & AFMT_S16_BE) { | |
95 tmp = AFMT_S16_BE; | |
96 } else if (tmp & AFMT_S16_LE) { | |
97 tmp = AFMT_S16_LE; | |
98 } else { | |
99 tmp = 0; | |
100 } | |
101 #else | |
102 if (tmp & AFMT_S16_LE) { | |
103 tmp = AFMT_S16_LE; | |
104 } else if (tmp & AFMT_S16_BE) { | |
105 tmp = AFMT_S16_BE; | |
106 } else { | |
107 tmp = 0; | |
108 } | |
109 #endif | |
110 | |
111 switch(tmp) { | |
112 case AFMT_S16_LE: | |
113 s->codec_id = CODEC_ID_PCM_S16LE; | |
114 break; | |
115 case AFMT_S16_BE: | |
116 s->codec_id = CODEC_ID_PCM_S16BE; | |
117 break; | |
118 default: | |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
241
diff
changeset
|
119 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n"); |
0 | 120 close(audio_fd); |
482 | 121 return AVERROR_IO; |
0 | 122 } |
123 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp); | |
124 if (err < 0) { | |
125 perror("SNDCTL_DSP_SETFMT"); | |
126 goto fail; | |
127 } | |
885 | 128 |
0 | 129 tmp = (s->channels == 2); |
130 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp); | |
131 if (err < 0) { | |
132 perror("SNDCTL_DSP_STEREO"); | |
133 goto fail; | |
134 } | |
135 if (tmp) | |
136 s->channels = 2; | |
885 | 137 |
0 | 138 tmp = s->sample_rate; |
139 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp); | |
140 if (err < 0) { | |
141 perror("SNDCTL_DSP_SPEED"); | |
142 goto fail; | |
143 } | |
144 s->sample_rate = tmp; /* store real sample rate */ | |
145 s->fd = audio_fd; | |
146 | |
147 return 0; | |
148 fail: | |
149 close(audio_fd); | |
482 | 150 return AVERROR_IO; |
0 | 151 } |
152 | |
153 static int audio_close(AudioData *s) | |
154 { | |
155 close(s->fd); | |
156 return 0; | |
157 } | |
158 | |
159 /* sound output support */ | |
160 static int audio_write_header(AVFormatContext *s1) | |
161 { | |
162 AudioData *s = s1->priv_data; | |
163 AVStream *st; | |
164 int ret; | |
165 | |
166 st = s1->streams[0]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
167 s->sample_rate = st->codec->sample_rate; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
168 s->channels = st->codec->channels; |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
169 ret = audio_open(s, 1, NULL); |
0 | 170 if (ret < 0) { |
482 | 171 return AVERROR_IO; |
0 | 172 } else { |
173 return 0; | |
174 } | |
175 } | |
176 | |
468 | 177 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) |
0 | 178 { |
179 AudioData *s = s1->priv_data; | |
180 int len, ret; | |
468 | 181 int size= pkt->size; |
182 uint8_t *buf= pkt->data; | |
0 | 183 |
184 while (size > 0) { | |
185 len = AUDIO_BLOCK_SIZE - s->buffer_ptr; | |
186 if (len > size) | |
187 len = size; | |
188 memcpy(s->buffer + s->buffer_ptr, buf, len); | |
189 s->buffer_ptr += len; | |
190 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) { | |
191 for(;;) { | |
192 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE); | |
193 if (ret > 0) | |
194 break; | |
195 if (ret < 0 && (errno != EAGAIN && errno != EINTR)) | |
482 | 196 return AVERROR_IO; |
0 | 197 } |
198 s->buffer_ptr = 0; | |
199 } | |
200 buf += len; | |
201 size -= len; | |
202 } | |
203 return 0; | |
204 } | |
205 | |
206 static int audio_write_trailer(AVFormatContext *s1) | |
207 { | |
208 AudioData *s = s1->priv_data; | |
209 | |
210 audio_close(s); | |
211 return 0; | |
212 } | |
213 | |
214 /* grab support */ | |
215 | |
216 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap) | |
217 { | |
218 AudioData *s = s1->priv_data; | |
219 AVStream *st; | |
220 int ret; | |
221 | |
1003 | 222 if (ap->sample_rate <= 0 || ap->channels <= 0) |
0 | 223 return -1; |
224 | |
225 st = av_new_stream(s1, 0); | |
226 if (!st) { | |
227 return -ENOMEM; | |
228 } | |
229 s->sample_rate = ap->sample_rate; | |
230 s->channels = ap->channels; | |
231 | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
0
diff
changeset
|
232 ret = audio_open(s, 0, ap->device); |
0 | 233 if (ret < 0) { |
234 av_free(st); | |
482 | 235 return AVERROR_IO; |
0 | 236 } |
237 | |
238 /* take real parameters */ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
239 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
240 st->codec->codec_id = s->codec_id; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
241 st->codec->sample_rate = s->sample_rate; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
754
diff
changeset
|
242 st->codec->channels = s->channels; |
0 | 243 |
921 | 244 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ |
0 | 245 return 0; |
246 } | |
247 | |
248 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
249 { | |
250 AudioData *s = s1->priv_data; | |
251 int ret, bdelay; | |
252 int64_t cur_time; | |
253 struct audio_buf_info abufi; | |
885 | 254 |
0 | 255 if (av_new_packet(pkt, s->frame_size) < 0) |
482 | 256 return AVERROR_IO; |
0 | 257 for(;;) { |
56
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
258 struct timeval tv; |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
259 fd_set fds; |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
260 |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
261 tv.tv_sec = 0; |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
262 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */ |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
263 |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
264 FD_ZERO(&fds); |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
265 FD_SET(s->fd, &fds); |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
266 |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
267 /* This will block until data is available or we get a timeout */ |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
268 (void) select(s->fd + 1, &fds, 0, 0, &tv); |
01d48dc59dab
Fix the 'hard cpu loop' problem when capturing audio from /dev/dsp. This
philipjsg
parents:
30
diff
changeset
|
269 |
0 | 270 ret = read(s->fd, pkt->data, pkt->size); |
271 if (ret > 0) | |
272 break; | |
273 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) { | |
274 av_free_packet(pkt); | |
275 pkt->size = 0; | |
921 | 276 pkt->pts = av_gettime(); |
0 | 277 return 0; |
278 } | |
279 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) { | |
280 av_free_packet(pkt); | |
482 | 281 return AVERROR_IO; |
0 | 282 } |
283 } | |
284 pkt->size = ret; | |
285 | |
286 /* compute pts of the start of the packet */ | |
287 cur_time = av_gettime(); | |
288 bdelay = ret; | |
289 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) { | |
290 bdelay += abufi.bytes; | |
291 } | |
292 /* substract time represented by the number of bytes in the audio fifo */ | |
293 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels); | |
294 | |
295 /* convert to wanted units */ | |
921 | 296 pkt->pts = cur_time; |
0 | 297 |
298 if (s->flip_left && s->channels == 2) { | |
299 int i; | |
300 short *p = (short *) pkt->data; | |
301 | |
302 for (i = 0; i < ret; i += 4) { | |
303 *p = ~*p; | |
304 p += 2; | |
305 } | |
306 } | |
307 return 0; | |
308 } | |
309 | |
310 static int audio_read_close(AVFormatContext *s1) | |
311 { | |
312 AudioData *s = s1->priv_data; | |
313 | |
314 audio_close(s); | |
315 return 0; | |
316 } | |
317 | |
1169 | 318 #ifdef CONFIG_AUDIO_DEMUXER |
319 AVInputFormat audio_demuxer = { | |
0 | 320 "audio_device", |
321 "audio grab and output", | |
322 sizeof(AudioData), | |
323 NULL, | |
324 audio_read_header, | |
325 audio_read_packet, | |
326 audio_read_close, | |
327 .flags = AVFMT_NOFILE, | |
328 }; | |
1169 | 329 #endif |
0 | 330 |
1169 | 331 #ifdef CONFIG_AUDIO_MUXER |
332 AVOutputFormat audio_muxer = { | |
0 | 333 "audio_device", |
334 "audio grab and output", | |
335 "", | |
336 "", | |
337 sizeof(AudioData), | |
338 /* XXX: we make the assumption that the soundcard accepts this format */ | |
339 /* XXX: find better solution with "preinit" method, needed also in | |
340 other formats */ | |
341 #ifdef WORDS_BIGENDIAN | |
342 CODEC_ID_PCM_S16BE, | |
343 #else | |
344 CODEC_ID_PCM_S16LE, | |
345 #endif | |
346 CODEC_ID_NONE, | |
347 audio_write_header, | |
348 audio_write_packet, | |
349 audio_write_trailer, | |
350 .flags = AVFMT_NOFILE, | |
351 }; | |
1169 | 352 #endif |