Statistics
| Branch: | Revision:

ffmpeg / libavdevice / oss_audio.c @ 63613fe6

History | View | Annotate | Download (8.45 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Linux audio play and grab interface
3 406792e7 Diego Biurrun
 * 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
22 4f786086 Diego Biurrun
#include "config.h"
23 de6d9b64 Fabrice Bellard
#include <stdlib.h>
24
#include <stdio.h>
25 4f786086 Diego Biurrun
#include <stdint.h>
26 de6d9b64 Fabrice Bellard
#include <string.h>
27 4f786086 Diego Biurrun
#include <errno.h>
28 b250f9c6 Aurelien Jacobs
#if HAVE_SOUNDCARD_H
29 8c802695 Jacob Meuser
#include <soundcard.h>
30
#else
31 b13788c5 Fabrice Bellard
#include <sys/soundcard.h>
32 8c802695 Jacob Meuser
#endif
33 de6d9b64 Fabrice Bellard
#include <unistd.h>
34
#include <fcntl.h>
35
#include <sys/ioctl.h>
36
#include <sys/time.h>
37 d84b1cb3 Alexis Ballier
#include <sys/select.h>
38 de6d9b64 Fabrice Bellard
39 245976da Diego Biurrun
#include "libavutil/log.h"
40
#include "libavcodec/avcodec.h"
41
#include "libavformat/avformat.h"
42 4f786086 Diego Biurrun
43 4972b26f Fabrice Bellard
#define AUDIO_BLOCK_SIZE 4096
44
45 de6d9b64 Fabrice Bellard
typedef struct {
46
    int fd;
47 4972b26f Fabrice Bellard
    int sample_rate;
48 de6d9b64 Fabrice Bellard
    int channels;
49 4972b26f Fabrice Bellard
    int frame_size; /* in bytes ! */
50 fb65d2ca Diego Pettenò
    enum CodecID codec_id;
51 72e043dd Erik Hovland
    unsigned int flip_left : 1;
52 0c1a9eda Zdenek Kabelac
    uint8_t buffer[AUDIO_BLOCK_SIZE];
53 4972b26f Fabrice Bellard
    int buffer_ptr;
54 de6d9b64 Fabrice Bellard
} AudioData;
55
56 d99b8166 Benoit Fouet
static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
57 de6d9b64 Fabrice Bellard
{
58 d99b8166 Benoit Fouet
    AudioData *s = s1->priv_data;
59 4972b26f Fabrice Bellard
    int audio_fd;
60 de6d9b64 Fabrice Bellard
    int tmp, err;
61 1de1cce2 Philip Gladstone
    char *flip = getenv("AUDIO_FLIP_LEFT");
62 de6d9b64 Fabrice Bellard
63 4972b26f Fabrice Bellard
    if (is_output)
64
        audio_fd = open(audio_device, O_WRONLY);
65 de6d9b64 Fabrice Bellard
    else
66 4972b26f Fabrice Bellard
        audio_fd = open(audio_device, O_RDONLY);
67 de6d9b64 Fabrice Bellard
    if (audio_fd < 0) {
68 0c26e964 Benoit Fouet
        av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
69 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
70 de6d9b64 Fabrice Bellard
    }
71
72 1de1cce2 Philip Gladstone
    if (flip && *flip == '1') {
73
        s->flip_left = 1;
74
    }
75
76 de6d9b64 Fabrice Bellard
    /* non blocking mode */
77 4364a3e0 Fabrice Bellard
    if (!is_output)
78
        fcntl(audio_fd, F_SETFL, O_NONBLOCK);
79 de6d9b64 Fabrice Bellard
80 4972b26f Fabrice Bellard
    s->frame_size = AUDIO_BLOCK_SIZE;
81 de6d9b64 Fabrice Bellard
#if 0
82 4972b26f Fabrice Bellard
    tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
83
    err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
84 de6d9b64 Fabrice Bellard
    if (err < 0) {
85
        perror("SNDCTL_DSP_SETFRAGMENT");
86
    }
87
#endif
88
89 4972b26f Fabrice Bellard
    /* select format : favour native format */
90
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
91 115329f1 Diego Biurrun
92 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
93 4972b26f Fabrice Bellard
    if (tmp & AFMT_S16_BE) {
94
        tmp = AFMT_S16_BE;
95
    } else if (tmp & AFMT_S16_LE) {
96
        tmp = AFMT_S16_LE;
97
    } else {
98
        tmp = 0;
99
    }
100
#else
101
    if (tmp & AFMT_S16_LE) {
102
        tmp = AFMT_S16_LE;
103
    } else if (tmp & AFMT_S16_BE) {
104
        tmp = AFMT_S16_BE;
105
    } else {
106
        tmp = 0;
107
    }
108
#endif
109
110
    switch(tmp) {
111
    case AFMT_S16_LE:
112
        s->codec_id = CODEC_ID_PCM_S16LE;
113
        break;
114
    case AFMT_S16_BE:
115
        s->codec_id = CODEC_ID_PCM_S16BE;
116
        break;
117
    default:
118 0c26e964 Benoit Fouet
        av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
119 4972b26f Fabrice Bellard
        close(audio_fd);
120 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
121 4972b26f Fabrice Bellard
    }
122
    err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
123 de6d9b64 Fabrice Bellard
    if (err < 0) {
124 0c26e964 Benoit Fouet
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
125 de6d9b64 Fabrice Bellard
        goto fail;
126
    }
127 115329f1 Diego Biurrun
128 4972b26f Fabrice Bellard
    tmp = (s->channels == 2);
129
    err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
130 de6d9b64 Fabrice Bellard
    if (err < 0) {
131 0c26e964 Benoit Fouet
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
132 de6d9b64 Fabrice Bellard
        goto fail;
133
    }
134 115329f1 Diego Biurrun
135 4972b26f Fabrice Bellard
    tmp = s->sample_rate;
136
    err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
137 de6d9b64 Fabrice Bellard
    if (err < 0) {
138 0c26e964 Benoit Fouet
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
139 de6d9b64 Fabrice Bellard
        goto fail;
140
    }
141 4972b26f Fabrice Bellard
    s->sample_rate = tmp; /* store real sample rate */
142 de6d9b64 Fabrice Bellard
    s->fd = audio_fd;
143
144
    return 0;
145
 fail:
146
    close(audio_fd);
147 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
148 de6d9b64 Fabrice Bellard
}
149
150 4972b26f Fabrice Bellard
static int audio_close(AudioData *s)
151 de6d9b64 Fabrice Bellard
{
152
    close(s->fd);
153 4972b26f Fabrice Bellard
    return 0;
154
}
155
156
/* sound output support */
157
static int audio_write_header(AVFormatContext *s1)
158
{
159 c9a65ca8 Fabrice Bellard
    AudioData *s = s1->priv_data;
160 4972b26f Fabrice Bellard
    AVStream *st;
161
    int ret;
162
163
    st = s1->streams[0];
164 01f4895c Michael Niedermayer
    s->sample_rate = st->codec->sample_rate;
165
    s->channels = st->codec->channels;
166 d99b8166 Benoit Fouet
    ret = audio_open(s1, 1, s1->filename);
167 4972b26f Fabrice Bellard
    if (ret < 0) {
168 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
169 4972b26f Fabrice Bellard
    } else {
170
        return 0;
171
    }
172
}
173
174 e928649b Michael Niedermayer
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
175 4972b26f Fabrice Bellard
{
176
    AudioData *s = s1->priv_data;
177
    int len, ret;
178 e928649b Michael Niedermayer
    int size= pkt->size;
179
    uint8_t *buf= pkt->data;
180 4972b26f Fabrice Bellard
181
    while (size > 0) {
182
        len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
183
        if (len > size)
184
            len = size;
185
        memcpy(s->buffer + s->buffer_ptr, buf, len);
186
        s->buffer_ptr += len;
187
        if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
188
            for(;;) {
189
                ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
190 4364a3e0 Fabrice Bellard
                if (ret > 0)
191 4972b26f Fabrice Bellard
                    break;
192
                if (ret < 0 && (errno != EAGAIN && errno != EINTR))
193 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
194 4972b26f Fabrice Bellard
            }
195
            s->buffer_ptr = 0;
196
        }
197
        buf += len;
198
        size -= len;
199
    }
200
    return 0;
201
}
202
203
static int audio_write_trailer(AVFormatContext *s1)
204
{
205
    AudioData *s = s1->priv_data;
206
207
    audio_close(s);
208 de6d9b64 Fabrice Bellard
    return 0;
209
}
210
211 4972b26f Fabrice Bellard
/* grab support */
212
213
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
214
{
215 c9a65ca8 Fabrice Bellard
    AudioData *s = s1->priv_data;
216 4972b26f Fabrice Bellard
    AVStream *st;
217
    int ret;
218
219 c04c3282 Michael Niedermayer
    if (ap->sample_rate <= 0 || ap->channels <= 0)
220 4972b26f Fabrice Bellard
        return -1;
221
222 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s1, 0);
223 4972b26f Fabrice Bellard
    if (!st) {
224 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
225 4972b26f Fabrice Bellard
    }
226
    s->sample_rate = ap->sample_rate;
227
    s->channels = ap->channels;
228
229 d99b8166 Benoit Fouet
    ret = audio_open(s1, 0, s1->filename);
230 4972b26f Fabrice Bellard
    if (ret < 0) {
231 1ea4f593 Fabrice Bellard
        av_free(st);
232 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
233 4972b26f Fabrice Bellard
    }
234 45dd5c69 Fabrice Bellard
235
    /* take real parameters */
236 01f4895c Michael Niedermayer
    st->codec->codec_type = CODEC_TYPE_AUDIO;
237
    st->codec->codec_id = s->codec_id;
238
    st->codec->sample_rate = s->sample_rate;
239
    st->codec->channels = s->channels;
240 45dd5c69 Fabrice Bellard
241 0a7b514f Luca Abeni
    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
242 45dd5c69 Fabrice Bellard
    return 0;
243 4972b26f Fabrice Bellard
}
244
245
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
246
{
247
    AudioData *s = s1->priv_data;
248 45dd5c69 Fabrice Bellard
    int ret, bdelay;
249
    int64_t cur_time;
250
    struct audio_buf_info abufi;
251 115329f1 Diego Biurrun
252 4972b26f Fabrice Bellard
    if (av_new_packet(pkt, s->frame_size) < 0)
253 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
254 4972b26f Fabrice Bellard
    for(;;) {
255 79134973 Philip Gladstone
        struct timeval tv;
256
        fd_set fds;
257
258
        tv.tv_sec = 0;
259
        tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
260
261
        FD_ZERO(&fds);
262
        FD_SET(s->fd, &fds);
263
264
        /* This will block until data is available or we get a timeout */
265
        (void) select(s->fd + 1, &fds, 0, 0, &tv);
266
267 4972b26f Fabrice Bellard
        ret = read(s->fd, pkt->data, pkt->size);
268
        if (ret > 0)
269
            break;
270 4606ac8d Zdenek Kabelac
        if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
271
            av_free_packet(pkt);
272
            pkt->size = 0;
273 0a7b514f Luca Abeni
            pkt->pts = av_gettime();
274 4606ac8d Zdenek Kabelac
            return 0;
275
        }
276 4972b26f Fabrice Bellard
        if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
277
            av_free_packet(pkt);
278 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
279 4972b26f Fabrice Bellard
        }
280
    }
281
    pkt->size = ret;
282 45dd5c69 Fabrice Bellard
283
    /* compute pts of the start of the packet */
284
    cur_time = av_gettime();
285
    bdelay = ret;
286
    if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
287
        bdelay += abufi.bytes;
288
    }
289 52b541ad Vitor Sessak
    /* subtract time represented by the number of bytes in the audio fifo */
290 45dd5c69 Fabrice Bellard
    cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
291
292
    /* convert to wanted units */
293 0a7b514f Luca Abeni
    pkt->pts = cur_time;
294 45dd5c69 Fabrice Bellard
295 1de1cce2 Philip Gladstone
    if (s->flip_left && s->channels == 2) {
296
        int i;
297
        short *p = (short *) pkt->data;
298
299
        for (i = 0; i < ret; i += 4) {
300
            *p = ~*p;
301
            p += 2;
302
        }
303
    }
304 4972b26f Fabrice Bellard
    return 0;
305
}
306
307
static int audio_read_close(AVFormatContext *s1)
308
{
309
    AudioData *s = s1->priv_data;
310
311
    audio_close(s);
312
    return 0;
313
}
314
315 38e54a75 Stefano Sabatini
#if CONFIG_OSS_INDEV
316 50383e88 Ramiro Polla
AVInputFormat oss_demuxer = {
317 1156c6b0 Ramiro Polla
    "oss",
318 c57c4897 Benoit Fouet
    NULL_IF_CONFIG_SMALL("Open Sound System capture"),
319 c9a65ca8 Fabrice Bellard
    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 ff70e601 Måns Rullgård
#endif
327 c9a65ca8 Fabrice Bellard
328 38e54a75 Stefano Sabatini
#if CONFIG_OSS_OUTDEV
329 50383e88 Ramiro Polla
AVOutputFormat oss_muxer = {
330 1156c6b0 Ramiro Polla
    "oss",
331 c57c4897 Benoit Fouet
    NULL_IF_CONFIG_SMALL("Open Sound System playback"),
332 4972b26f Fabrice Bellard
    "",
333
    "",
334 c9a65ca8 Fabrice Bellard
    sizeof(AudioData),
335 4972b26f Fabrice Bellard
    /* XXX: we make the assumption that the soundcard accepts this format */
336
    /* XXX: find better solution with "preinit" method, needed also in
337
       other formats */
338 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
339 4972b26f Fabrice Bellard
    CODEC_ID_PCM_S16BE,
340
#else
341
    CODEC_ID_PCM_S16LE,
342
#endif
343
    CODEC_ID_NONE,
344
    audio_write_header,
345
    audio_write_packet,
346
    audio_write_trailer,
347 bb76a117 Måns Rullgård
    .flags = AVFMT_NOFILE,
348 de6d9b64 Fabrice Bellard
};
349 ff70e601 Måns Rullgård
#endif