Statistics
| Branch: | Revision:

ffmpeg / libavformat / audio.c @ 8509fc7b

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