Statistics
| Branch: | Revision:

ffmpeg / libavformat / audio.c @ 3840147e

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