|
808
|
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 #include "avformat.h"
|
|
|
22
|
|
|
23 #include <stdlib.h>
|
|
|
24 #include <stdio.h>
|
|
|
25 #include <string.h>
|
|
|
26 #ifdef __OpenBSD__
|
|
|
27 #include <soundcard.h>
|
|
|
28 #else
|
|
|
29 #include <sys/soundcard.h>
|
|
|
30 #endif
|
|
|
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;
|
|
|
46 uint8_t buffer[AUDIO_BLOCK_SIZE];
|
|
|
47 int buffer_ptr;
|
|
|
48 } AudioData;
|
|
|
49
|
|
|
50 static int audio_open(AudioData *s, int is_output, const char *audio_device)
|
|
|
51 {
|
|
|
52 int audio_fd;
|
|
|
53 int tmp, err;
|
|
|
54 char *flip = getenv("AUDIO_FLIP_LEFT");
|
|
|
55
|
|
|
56 /* open linux audio device */
|
|
|
57 if (!audio_device)
|
|
|
58 #ifdef __OpenBSD__
|
|
|
59 audio_device = "/dev/sound";
|
|
|
60 #else
|
|
|
61 audio_device = "/dev/dsp";
|
|
|
62 #endif
|
|
|
63
|
|
|
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);
|
|
|
70 return AVERROR_IO;
|
|
|
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);
|
|
|
92
|
|
|
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:
|
|
|
119 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
|
|
|
120 close(audio_fd);
|
|
|
121 return AVERROR_IO;
|
|
|
122 }
|
|
|
123 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
|
|
|
124 if (err < 0) {
|
|
|
125 perror("SNDCTL_DSP_SETFMT");
|
|
|
126 goto fail;
|
|
|
127 }
|
|
|
128
|
|
|
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;
|
|
|
137
|
|
|
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);
|
|
|
150 return AVERROR_IO;
|
|
|
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];
|
|
|
167 s->sample_rate = st->codec->sample_rate;
|
|
|
168 s->channels = st->codec->channels;
|
|
|
169 ret = audio_open(s, 1, NULL);
|
|
|
170 if (ret < 0) {
|
|
|
171 return AVERROR_IO;
|
|
|
172 } else {
|
|
|
173 return 0;
|
|
|
174 }
|
|
|
175 }
|
|
|
176
|
|
|
177 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
|
|
|
178 {
|
|
|
179 AudioData *s = s1->priv_data;
|
|
|
180 int len, ret;
|
|
|
181 int size= pkt->size;
|
|
|
182 uint8_t *buf= pkt->data;
|
|
|
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))
|
|
|
196 return AVERROR_IO;
|
|
|
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
|
|
|
222 if (ap->sample_rate <= 0 || ap->channels <= 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
|
|
|
232 ret = audio_open(s, 0, ap->device);
|
|
|
233 if (ret < 0) {
|
|
|
234 av_free(st);
|
|
|
235 return AVERROR_IO;
|
|
|
236 }
|
|
|
237
|
|
|
238 /* take real parameters */
|
|
|
239 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
|
240 st->codec->codec_id = s->codec_id;
|
|
|
241 st->codec->sample_rate = s->sample_rate;
|
|
|
242 st->codec->channels = s->channels;
|
|
|
243
|
|
|
244 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
|
|
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;
|
|
|
254
|
|
|
255 if (av_new_packet(pkt, s->frame_size) < 0)
|
|
|
256 return AVERROR_IO;
|
|
|
257 for(;;) {
|
|
|
258 struct timeval tv;
|
|
|
259 fd_set fds;
|
|
|
260
|
|
|
261 tv.tv_sec = 0;
|
|
|
262 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
|
|
|
263
|
|
|
264 FD_ZERO(&fds);
|
|
|
265 FD_SET(s->fd, &fds);
|
|
|
266
|
|
|
267 /* This will block until data is available or we get a timeout */
|
|
|
268 (void) select(s->fd + 1, &fds, 0, 0, &tv);
|
|
|
269
|
|
|
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;
|
|
|
276 pkt->pts = av_gettime();
|
|
|
277 return 0;
|
|
|
278 }
|
|
|
279 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
|
|
|
280 av_free_packet(pkt);
|
|
|
281 return AVERROR_IO;
|
|
|
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 */
|
|
|
296 pkt->pts = cur_time;
|
|
|
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
|
|
|
318 #ifdef CONFIG_AUDIO_DEMUXER
|
|
|
319 AVInputFormat audio_demuxer = {
|
|
|
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 };
|
|
|
329 #endif
|
|
|
330
|
|
|
331 #ifdef CONFIG_AUDIO_MUXER
|
|
|
332 AVOutputFormat audio_muxer = {
|
|
|
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 };
|
|
|
352 #endif
|