Statistics
| Branch: | Revision:

ffmpeg / libavdevice / audio.c @ 362b3bf7

History | View | Annotate | Download (8.25 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
#include <sys/time.h>
35

    
36
#define AUDIO_BLOCK_SIZE 4096
37

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

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

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

    
64
    if (flip && *flip == '1') {
65
        s->flip_left = 1;
66
    }
67

    
68
    /* non blocking mode */
69
    if (!is_output)
70
        fcntl(audio_fd, F_SETFL, O_NONBLOCK);
71

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

    
81
    /* select format : favour native format */
82
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
83

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

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

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

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

    
138
    return 0;
139
 fail:
140
    close(audio_fd);
141
    return AVERROR(EIO);
142
}
143

    
144
static int audio_close(AudioData *s)
145
{
146
    close(s->fd);
147
    return 0;
148
}
149

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

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

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

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

    
197
static int audio_write_trailer(AVFormatContext *s1)
198
{
199
    AudioData *s = s1->priv_data;
200

    
201
    audio_close(s);
202
    return 0;
203
}
204

    
205
/* grab support */
206

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

    
213
    if (ap->sample_rate <= 0 || ap->channels <= 0)
214
        return -1;
215

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

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

    
229
    /* take real parameters */
230
    st->codec->codec_type = CODEC_TYPE_AUDIO;
231
    st->codec->codec_id = s->codec_id;
232
    st->codec->sample_rate = s->sample_rate;
233
    st->codec->channels = s->channels;
234

    
235
    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
236
    return 0;
237
}
238

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

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

    
252
        tv.tv_sec = 0;
253
        tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
254

    
255
        FD_ZERO(&fds);
256
        FD_SET(s->fd, &fds);
257

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

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

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

    
286
    /* convert to wanted units */
287
    pkt->pts = cur_time;
288

    
289
    if (s->flip_left && s->channels == 2) {
290
        int i;
291
        short *p = (short *) pkt->data;
292

    
293
        for (i = 0; i < ret; i += 4) {
294
            *p = ~*p;
295
            p += 2;
296
        }
297
    }
298
    return 0;
299
}
300

    
301
static int audio_read_close(AVFormatContext *s1)
302
{
303
    AudioData *s = s1->priv_data;
304

    
305
    audio_close(s);
306
    return 0;
307
}
308

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

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