Statistics
| Branch: | Revision:

ffmpeg / libavformat / audio.c @ 8c653280

History | View | Annotate | Download (8.15 KB)

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