Statistics
| Branch: | Revision:

ffmpeg / libavdevice / audio.c @ b0067549

History | View | Annotate | Download (8.3 KB)

1
/*
2
 * Linux audio play and grab interface
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22

    
23
#include <stdlib.h>
24
#include <stdio.h>
25
#include <string.h>
26
#ifdef HAVE_SOUNDCARD_H
27
#include <soundcard.h>
28
#else
29
#include <sys/soundcard.h>
30
#endif
31
#include <unistd.h>
32
#include <fcntl.h>
33
#include <sys/ioctl.h>
34
#ifdef HAVE_SYS_MMAN_H
35
#include <sys/mman.h>
36
#endif
37
#include <sys/time.h>
38

    
39
#define AUDIO_BLOCK_SIZE 4096
40

    
41
typedef struct {
42
    int fd;
43
    int sample_rate;
44
    int channels;
45
    int frame_size; /* in bytes ! */
46
    int codec_id;
47
    int flip_left : 1;
48
    uint8_t buffer[AUDIO_BLOCK_SIZE];
49
    int buffer_ptr;
50
} AudioData;
51

    
52
static int audio_open(AudioData *s, int is_output, const char *audio_device)
53
{
54
    int audio_fd;
55
    int tmp, err;
56
    char *flip = getenv("AUDIO_FLIP_LEFT");
57

    
58
    if (is_output)
59
        audio_fd = open(audio_device, O_WRONLY);
60
    else
61
        audio_fd = open(audio_device, O_RDONLY);
62
    if (audio_fd < 0) {
63
        av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
64
        return AVERROR(EIO);
65
    }
66

    
67
    if (flip && *flip == '1') {
68
        s->flip_left = 1;
69
    }
70

    
71
    /* non blocking mode */
72
    if (!is_output)
73
        fcntl(audio_fd, F_SETFL, O_NONBLOCK);
74

    
75
    s->frame_size = AUDIO_BLOCK_SIZE;
76
#if 0
77
    tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
78
    err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
79
    if (err < 0) {
80
        perror("SNDCTL_DSP_SETFRAGMENT");
81
    }
82
#endif
83

    
84
    /* select format : favour native format */
85
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
86

    
87
#ifdef WORDS_BIGENDIAN
88
    if (tmp & AFMT_S16_BE) {
89
        tmp = AFMT_S16_BE;
90
    } else if (tmp & AFMT_S16_LE) {
91
        tmp = AFMT_S16_LE;
92
    } else {
93
        tmp = 0;
94
    }
95
#else
96
    if (tmp & AFMT_S16_LE) {
97
        tmp = AFMT_S16_LE;
98
    } else if (tmp & AFMT_S16_BE) {
99
        tmp = AFMT_S16_BE;
100
    } else {
101
        tmp = 0;
102
    }
103
#endif
104

    
105
    switch(tmp) {
106
    case AFMT_S16_LE:
107
        s->codec_id = CODEC_ID_PCM_S16LE;
108
        break;
109
    case AFMT_S16_BE:
110
        s->codec_id = CODEC_ID_PCM_S16BE;
111
        break;
112
    default:
113
        av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
114
        close(audio_fd);
115
        return AVERROR(EIO);
116
    }
117
    err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
118
    if (err < 0) {
119
        av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
120
        goto fail;
121
    }
122

    
123
    tmp = (s->channels == 2);
124
    err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
125
    if (err < 0) {
126
        av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
127
        goto fail;
128
    }
129
    if (tmp)
130
        s->channels = 2;
131

    
132
    tmp = s->sample_rate;
133
    err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
134
    if (err < 0) {
135
        av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
136
        goto fail;
137
    }
138
    s->sample_rate = tmp; /* store real sample rate */
139
    s->fd = audio_fd;
140

    
141
    return 0;
142
 fail:
143
    close(audio_fd);
144
    return AVERROR(EIO);
145
}
146

    
147
static int audio_close(AudioData *s)
148
{
149
    close(s->fd);
150
    return 0;
151
}
152

    
153
/* sound output support */
154
static int audio_write_header(AVFormatContext *s1)
155
{
156
    AudioData *s = s1->priv_data;
157
    AVStream *st;
158
    int ret;
159

    
160
    st = s1->streams[0];
161
    s->sample_rate = st->codec->sample_rate;
162
    s->channels = st->codec->channels;
163
    ret = audio_open(s, 1, s1->filename);
164
    if (ret < 0) {
165
        return AVERROR(EIO);
166
    } else {
167
        return 0;
168
    }
169
}
170

    
171
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
172
{
173
    AudioData *s = s1->priv_data;
174
    int len, ret;
175
    int size= pkt->size;
176
    uint8_t *buf= pkt->data;
177

    
178
    while (size > 0) {
179
        len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
180
        if (len > size)
181
            len = size;
182
        memcpy(s->buffer + s->buffer_ptr, buf, len);
183
        s->buffer_ptr += len;
184
        if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
185
            for(;;) {
186
                ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
187
                if (ret > 0)
188
                    break;
189
                if (ret < 0 && (errno != EAGAIN && errno != EINTR))
190
                    return AVERROR(EIO);
191
            }
192
            s->buffer_ptr = 0;
193
        }
194
        buf += len;
195
        size -= len;
196
    }
197
    return 0;
198
}
199

    
200
static int audio_write_trailer(AVFormatContext *s1)
201
{
202
    AudioData *s = s1->priv_data;
203

    
204
    audio_close(s);
205
    return 0;
206
}
207

    
208
/* grab support */
209

    
210
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
211
{
212
    AudioData *s = s1->priv_data;
213
    AVStream *st;
214
    int ret;
215

    
216
    if (ap->sample_rate <= 0 || ap->channels <= 0)
217
        return -1;
218

    
219
    st = av_new_stream(s1, 0);
220
    if (!st) {
221
        return AVERROR(ENOMEM);
222
    }
223
    s->sample_rate = ap->sample_rate;
224
    s->channels = ap->channels;
225

    
226
    ret = audio_open(s, 0, s1->filename);
227
    if (ret < 0) {
228
        av_free(st);
229
        return AVERROR(EIO);
230
    }
231

    
232
    /* take real parameters */
233
    st->codec->codec_type = CODEC_TYPE_AUDIO;
234
    st->codec->codec_id = s->codec_id;
235
    st->codec->sample_rate = s->sample_rate;
236
    st->codec->channels = s->channels;
237

    
238
    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
239
    return 0;
240
}
241

    
242
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
243
{
244
    AudioData *s = s1->priv_data;
245
    int ret, bdelay;
246
    int64_t cur_time;
247
    struct audio_buf_info abufi;
248

    
249
    if (av_new_packet(pkt, s->frame_size) < 0)
250
        return AVERROR(EIO);
251
    for(;;) {
252
        struct timeval tv;
253
        fd_set fds;
254

    
255
        tv.tv_sec = 0;
256
        tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
257

    
258
        FD_ZERO(&fds);
259
        FD_SET(s->fd, &fds);
260

    
261
        /* This will block until data is available or we get a timeout */
262
        (void) select(s->fd + 1, &fds, 0, 0, &tv);
263

    
264
        ret = read(s->fd, pkt->data, pkt->size);
265
        if (ret > 0)
266
            break;
267
        if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
268
            av_free_packet(pkt);
269
            pkt->size = 0;
270
            pkt->pts = av_gettime();
271
            return 0;
272
        }
273
        if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
274
            av_free_packet(pkt);
275
            return AVERROR(EIO);
276
        }
277
    }
278
    pkt->size = ret;
279

    
280
    /* compute pts of the start of the packet */
281
    cur_time = av_gettime();
282
    bdelay = ret;
283
    if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
284
        bdelay += abufi.bytes;
285
    }
286
    /* subtract time represented by the number of bytes in the audio fifo */
287
    cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
288

    
289
    /* convert to wanted units */
290
    pkt->pts = cur_time;
291

    
292
    if (s->flip_left && s->channels == 2) {
293
        int i;
294
        short *p = (short *) pkt->data;
295

    
296
        for (i = 0; i < ret; i += 4) {
297
            *p = ~*p;
298
            p += 2;
299
        }
300
    }
301
    return 0;
302
}
303

    
304
static int audio_read_close(AVFormatContext *s1)
305
{
306
    AudioData *s = s1->priv_data;
307

    
308
    audio_close(s);
309
    return 0;
310
}
311

    
312
#ifdef CONFIG_OSS_DEMUXER
313
AVInputFormat oss_demuxer = {
314
    "oss",
315
    "audio grab and output",
316
    sizeof(AudioData),
317
    NULL,
318
    audio_read_header,
319
    audio_read_packet,
320
    audio_read_close,
321
    .flags = AVFMT_NOFILE,
322
};
323
#endif
324

    
325
#ifdef CONFIG_OSS_MUXER
326
AVOutputFormat oss_muxer = {
327
    "oss",
328
    "audio grab and output",
329
    "",
330
    "",
331
    sizeof(AudioData),
332
    /* XXX: we make the assumption that the soundcard accepts this format */
333
    /* XXX: find better solution with "preinit" method, needed also in
334
       other formats */
335
#ifdef WORDS_BIGENDIAN
336
    CODEC_ID_PCM_S16BE,
337
#else
338
    CODEC_ID_PCM_S16LE,
339
#endif
340
    CODEC_ID_NONE,
341
    audio_write_header,
342
    audio_write_packet,
343
    audio_write_trailer,
344
    .flags = AVFMT_NOFILE,
345
};
346
#endif