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 #if 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
44
45 #define AUDIO_BLOCK_SIZE 4096
46
54 unsigned int flip_left : 1;
58
60 {
62 int audio_fd;
63 int tmp, err;
64 char *
flip = getenv(
"AUDIO_FLIP_LEFT");
65
66 if (is_output)
68 else
70 if (audio_fd < 0) {
73 }
74
75 if (flip && *flip == '1') {
77 }
78
79 /* non blocking mode */
80 if (!is_output) {
81 if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
82 av_log(s1,
AV_LOG_WARNING,
"%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
83 }
84 }
85
87
88 /* select format : favour native format */
89 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
90
91 #if HAVE_BIGENDIAN
92 if (tmp & AFMT_S16_BE) {
93 tmp = AFMT_S16_BE;
94 } else if (tmp & AFMT_S16_LE) {
95 tmp = AFMT_S16_LE;
96 } else {
97 tmp = 0;
98 }
99 #else
100 if (tmp & AFMT_S16_LE) {
101 tmp = AFMT_S16_LE;
102 } else if (tmp & AFMT_S16_BE) {
103 tmp = AFMT_S16_BE;
104 } else {
105 tmp = 0;
106 }
107 #endif
108
109 switch(tmp) {
110 case AFMT_S16_LE:
112 break;
113 case AFMT_S16_BE:
115 break;
116 default:
120 }
121 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
122 if (err < 0) {
124 goto fail;
125 }
126
128 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129 if (err < 0) {
131 goto fail;
132 }
133
135 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
136 if (err < 0) {
138 goto fail;
139 }
142
143 return 0;
144 fail:
147 }
148
150 {
152 return 0;
153 }
154
155 /* sound output support */
157 {
161
166 if (ret < 0) {
168 } else {
169 return 0;
170 }
171 }
172
174 {
179
180 while (size > 0) {
185 for(;;) {
187 if (ret > 0)
188 break;
189 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
191 }
193 }
196 }
197 return 0;
198 }
199
201 {
203
205 return 0;
206 }
207
208 /* grab support */
209
211 {
215
217 if (!st) {
219 }
220
222 if (ret < 0) {
224 }
225
226 /* take real parameters */
231
233 return 0;
234 }
235
237 {
241 struct audio_buf_info abufi;
242
245
247 if (ret <= 0){
250 if (ret<0)
return AVERROR(errno);
252 }
254
255 /* compute pts of the start of the packet */
258 if (ioctl(s->
fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
259 bdelay += abufi.bytes;
260 }
261 /* subtract time represented by the number of bytes in the audio fifo */
263
264 /* convert to wanted units */
266
268 int i;
269 short *p = (
short *) pkt->
data;
270
271 for (i = 0; i <
ret; i += 4) {
272 *p = ~*p;
273 p += 2;
274 }
275 }
276 return 0;
277 }
278
280 {
282
284 return 0;
285 }
286
287 #if CONFIG_OSS_INDEV
291 { NULL },
292 };
293
294 static const AVClass oss_demuxer_class = {
299 };
300
309 .priv_class = &oss_demuxer_class,
310 };
311 #endif
312
313 #if CONFIG_OSS_OUTDEV
318 /* XXX: we make the assumption that the soundcard accepts this format */
319 /* XXX: find better solution with "preinit" method, needed also in
320 other formats */
327 };
328 #endif