suppressed mpeg demux mess - use now dynamic stream creation api
[libav.git] / libav / audio.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
8be1c656
FB
19#include "avformat.h"
20
de6d9b64
FB
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <linux/soundcard.h>
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
31const char *audio_device = "/dev/dsp";
32
4972b26f
FB
33#define AUDIO_BLOCK_SIZE 4096
34
de6d9b64
FB
35typedef 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 46static 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 141static int audio_close(AudioData *s)
de6d9b64 142{
de6d9b64 143 close(s->fd);
4972b26f
FB
144 return 0;
145}
146
147/* sound output support */
148static int audio_write_header(AVFormatContext *s1)
149{
150 AudioData *s;
151 AVStream *st;
152 int ret;
153
154 s = av_mallocz(sizeof(AudioData));
155 if (!s)
156 return -ENOMEM;
157 s1->priv_data = s;
158
159 st = s1->streams[0];
160 s->sample_rate = st->codec.sample_rate;
161 s->channels = st->codec.channels;
162 ret = audio_open(s, 1);
163 if (ret < 0) {
1ea4f593 164 av_free(s);
4972b26f
FB
165 return -EIO;
166 } else {
167 return 0;
168 }
169}
170
171static int audio_write_packet(AVFormatContext *s1, int stream_index,
10bb7023 172 UINT8 *buf, int size, int force_pts)
4972b26f
FB
173{
174 AudioData *s = s1->priv_data;
175 int len, ret;
176
177 while (size > 0) {
178 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
179 if (len > size)
180 len = size;
181 memcpy(s->buffer + s->buffer_ptr, buf, len);
182 s->buffer_ptr += len;
183 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
184 for(;;) {
185 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
186 if (ret != 0)
187 break;
188 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
189 return -EIO;
190 }
191 s->buffer_ptr = 0;
192 }
193 buf += len;
194 size -= len;
195 }
196 return 0;
197}
198
199static int audio_write_trailer(AVFormatContext *s1)
200{
201 AudioData *s = s1->priv_data;
202
203 audio_close(s);
1ea4f593 204 av_free(s);
de6d9b64
FB
205 return 0;
206}
207
4972b26f
FB
208/* grab support */
209
210static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
211{
212 AudioData *s;
213 AVStream *st;
214 int ret;
215
216 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
217 return -1;
218
219 s = av_mallocz(sizeof(AudioData));
220 if (!s)
221 return -ENOMEM;
222 st = av_mallocz(sizeof(AVStream));
223 if (!st) {
1ea4f593 224 av_free(s);
4972b26f
FB
225 return -ENOMEM;
226 }
227 s1->priv_data = s;
228 s1->nb_streams = 1;
229 s1->streams[0] = st;
230 s->sample_rate = ap->sample_rate;
231 s->channels = ap->channels;
232
233 ret = audio_open(s, 0);
234 if (ret < 0) {
1ea4f593
FB
235 av_free(st);
236 av_free(s);
4972b26f
FB
237 return -EIO;
238 } else {
239 /* take real parameters */
240 st->codec.codec_type = CODEC_TYPE_AUDIO;
241 st->codec.codec_id = s->codec_id;
242 st->codec.sample_rate = s->sample_rate;
243 st->codec.channels = s->channels;
244 return 0;
245 }
246}
247
248static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
249{
250 AudioData *s = s1->priv_data;
251 int ret;
252
253 if (av_new_packet(pkt, s->frame_size) < 0)
254 return -EIO;
255 for(;;) {
256 ret = read(s->fd, pkt->data, pkt->size);
257 if (ret > 0)
258 break;
4606ac8d
ZK
259 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
260 av_free_packet(pkt);
261 pkt->size = 0;
262 return 0;
263 }
4972b26f
FB
264 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
265 av_free_packet(pkt);
266 return -EIO;
267 }
268 }
269 pkt->size = ret;
1de1cce2
PG
270 if (s->flip_left && s->channels == 2) {
271 int i;
272 short *p = (short *) pkt->data;
273
274 for (i = 0; i < ret; i += 4) {
275 *p = ~*p;
276 p += 2;
277 }
278 }
4972b26f
FB
279 return 0;
280}
281
282static int audio_read_close(AVFormatContext *s1)
283{
284 AudioData *s = s1->priv_data;
285
286 audio_close(s);
1ea4f593 287 av_free(s);
4972b26f
FB
288 return 0;
289}
290
291AVFormat audio_device_format = {
292 "audio_device",
293 "audio grab and output",
294 "",
295 "",
296 /* XXX: we make the assumption that the soundcard accepts this format */
297 /* XXX: find better solution with "preinit" method, needed also in
298 other formats */
299#ifdef WORDS_BIGENDIAN
300 CODEC_ID_PCM_S16BE,
301#else
302 CODEC_ID_PCM_S16LE,
303#endif
304 CODEC_ID_NONE,
305 audio_write_header,
306 audio_write_packet,
307 audio_write_trailer,
308
309 audio_read_header,
310 audio_read_packet,
311 audio_read_close,
312 NULL,
313 AVFMT_NOFILE,
de6d9b64 314};