Statistics
| Branch: | Revision:

ffmpeg / libavformat / audio.c @ 71e445fc

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