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