Statistics
| Branch: | Revision:

ffmpeg / libav / audio.c @ 9eb82647

History | View | Annotate | Download (7.21 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 19720f15 Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 de6d9b64 Fabrice Bellard
 */
19 8be1c656 Fabrice Bellard
#include "avformat.h"
20
21 de6d9b64 Fabrice Bellard
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24 b13788c5 Fabrice Bellard
#include <sys/soundcard.h>
25 de6d9b64 Fabrice Bellard
#include <unistd.h>
26
#include <fcntl.h>
27
#include <sys/ioctl.h>
28
#include <sys/mman.h>
29
#include <sys/time.h>
30
31
const char *audio_device = "/dev/dsp";
32
33 4972b26f Fabrice Bellard
#define AUDIO_BLOCK_SIZE 4096
34
35 de6d9b64 Fabrice Bellard
typedef struct {
36
    int fd;
37 4972b26f Fabrice Bellard
    int sample_rate;
38 de6d9b64 Fabrice Bellard
    int channels;
39 4972b26f Fabrice Bellard
    int frame_size; /* in bytes ! */
40
    int codec_id;
41 1de1cce2 Philip Gladstone
    int flip_left : 1;
42 4972b26f Fabrice Bellard
    UINT8 buffer[AUDIO_BLOCK_SIZE];
43
    int buffer_ptr;
44 de6d9b64 Fabrice Bellard
} AudioData;
45
46 4972b26f Fabrice Bellard
static int audio_open(AudioData *s, int is_output)
47 de6d9b64 Fabrice Bellard
{
48 4972b26f Fabrice Bellard
    int audio_fd;
49 de6d9b64 Fabrice Bellard
    int tmp, err;
50 1de1cce2 Philip Gladstone
    char *flip = getenv("AUDIO_FLIP_LEFT");
51 de6d9b64 Fabrice Bellard
52
    /* open linux audio device */
53 4972b26f Fabrice Bellard
    if (is_output)
54
        audio_fd = open(audio_device, O_WRONLY);
55 de6d9b64 Fabrice Bellard
    else
56 4972b26f Fabrice Bellard
        audio_fd = open(audio_device, O_RDONLY);
57 de6d9b64 Fabrice Bellard
    if (audio_fd < 0) {
58
        perror(audio_device);
59
        return -EIO;
60
    }
61
62 1de1cce2 Philip Gladstone
    if (flip && *flip == '1') {
63
        s->flip_left = 1;
64
    }
65
66 de6d9b64 Fabrice Bellard
    /* non blocking mode */
67
    fcntl(audio_fd, F_SETFL, O_NONBLOCK);
68
69 4972b26f Fabrice Bellard
    s->frame_size = AUDIO_BLOCK_SIZE;
70 de6d9b64 Fabrice Bellard
#if 0
71 4972b26f Fabrice Bellard
    tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
72
    err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
73 de6d9b64 Fabrice Bellard
    if (err < 0) {
74
        perror("SNDCTL_DSP_SETFRAGMENT");
75
    }
76
#endif
77
78 4972b26f Fabrice Bellard
    /* select format : favour native format */
79
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
80
    
81
#ifdef WORDS_BIGENDIAN
82
    if (tmp & AFMT_S16_BE) {
83
        tmp = AFMT_S16_BE;
84
    } else if (tmp & AFMT_S16_LE) {
85
        tmp = AFMT_S16_LE;
86
    } else {
87
        tmp = 0;
88
    }
89
#else
90
    if (tmp & AFMT_S16_LE) {
91
        tmp = AFMT_S16_LE;
92
    } else if (tmp & AFMT_S16_BE) {
93
        tmp = AFMT_S16_BE;
94
    } else {
95
        tmp = 0;
96
    }
97
#endif
98
99
    switch(tmp) {
100
    case AFMT_S16_LE:
101
        s->codec_id = CODEC_ID_PCM_S16LE;
102
        break;
103
    case AFMT_S16_BE:
104
        s->codec_id = CODEC_ID_PCM_S16BE;
105
        break;
106
    default:
107
        fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
108
        close(audio_fd);
109
        return -EIO;
110
    }
111
    err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
112 de6d9b64 Fabrice Bellard
    if (err < 0) {
113
        perror("SNDCTL_DSP_SETFMT");
114
        goto fail;
115
    }
116
    
117 4972b26f Fabrice Bellard
    tmp = (s->channels == 2);
118
    err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
119 de6d9b64 Fabrice Bellard
    if (err < 0) {
120
        perror("SNDCTL_DSP_STEREO");
121
        goto fail;
122
    }
123 1de1cce2 Philip Gladstone
    if (tmp)
124
        s->channels = 2;
125 de6d9b64 Fabrice Bellard
    
126 4972b26f Fabrice Bellard
    tmp = s->sample_rate;
127
    err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
128 de6d9b64 Fabrice Bellard
    if (err < 0) {
129
        perror("SNDCTL_DSP_SPEED");
130
        goto fail;
131
    }
132 4972b26f Fabrice Bellard
    s->sample_rate = tmp; /* store real sample rate */
133 de6d9b64 Fabrice Bellard
    s->fd = audio_fd;
134
135
    return 0;
136
 fail:
137
    close(audio_fd);
138
    return -EIO;
139
}
140
141 4972b26f Fabrice Bellard
static int audio_close(AudioData *s)
142 de6d9b64 Fabrice Bellard
{
143
    close(s->fd);
144 4972b26f Fabrice Bellard
    return 0;
145
}
146
147
/* sound output support */
148
static int audio_write_header(AVFormatContext *s1)
149
{
150 c9a65ca8 Fabrice Bellard
    AudioData *s = s1->priv_data;
151 4972b26f Fabrice Bellard
    AVStream *st;
152
    int ret;
153
154
    st = s1->streams[0];
155
    s->sample_rate = st->codec.sample_rate;
156
    s->channels = st->codec.channels;
157
    ret = audio_open(s, 1);
158
    if (ret < 0) {
159
        return -EIO;
160
    } else {
161
        return 0;
162
    }
163
}
164
165
static int audio_write_packet(AVFormatContext *s1, int stream_index,
166 10bb7023 Juanjo
                              UINT8 *buf, int size, int force_pts)
167 4972b26f Fabrice Bellard
{
168
    AudioData *s = s1->priv_data;
169
    int len, ret;
170
171
    while (size > 0) {
172
        len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
173
        if (len > size)
174
            len = size;
175
        memcpy(s->buffer + s->buffer_ptr, buf, len);
176
        s->buffer_ptr += len;
177
        if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
178
            for(;;) {
179
                ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
180
                if (ret != 0)
181
                    break;
182
                if (ret < 0 && (errno != EAGAIN && errno != EINTR))
183
                    return -EIO;
184
            }
185
            s->buffer_ptr = 0;
186
        }
187
        buf += len;
188
        size -= len;
189
    }
190
    return 0;
191
}
192
193
static int audio_write_trailer(AVFormatContext *s1)
194
{
195
    AudioData *s = s1->priv_data;
196
197
    audio_close(s);
198 de6d9b64 Fabrice Bellard
    return 0;
199
}
200
201 4972b26f Fabrice Bellard
/* grab support */
202
203
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
204
{
205 c9a65ca8 Fabrice Bellard
    AudioData *s = s1->priv_data;
206 4972b26f Fabrice Bellard
    AVStream *st;
207
    int ret;
208
209
    if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
210
        return -1;
211
212 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s1, 0);
213 4972b26f Fabrice Bellard
    if (!st) {
214
        return -ENOMEM;
215
    }
216
    s->sample_rate = ap->sample_rate;
217
    s->channels = ap->channels;
218
219
    ret = audio_open(s, 0);
220
    if (ret < 0) {
221 1ea4f593 Fabrice Bellard
        av_free(st);
222 4972b26f Fabrice Bellard
        return -EIO;
223
    } else {
224
        /* take real parameters */
225
        st->codec.codec_type = CODEC_TYPE_AUDIO;
226
        st->codec.codec_id = s->codec_id;
227
        st->codec.sample_rate = s->sample_rate;
228
        st->codec.channels = s->channels;
229
        return 0;
230
    }
231
}
232
233
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
234
{
235
    AudioData *s = s1->priv_data;
236
    int ret;
237
238
    if (av_new_packet(pkt, s->frame_size) < 0)
239
        return -EIO;
240
    for(;;) {
241
        ret = read(s->fd, pkt->data, pkt->size);
242
        if (ret > 0)
243
            break;
244 4606ac8d Zdenek Kabelac
        if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
245
            av_free_packet(pkt);
246
            pkt->size = 0;
247
            return 0;
248
        }
249 4972b26f Fabrice Bellard
        if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
250
            av_free_packet(pkt);
251
            return -EIO;
252
        }
253
    }
254
    pkt->size = ret;
255 1de1cce2 Philip Gladstone
    if (s->flip_left && s->channels == 2) {
256
        int i;
257
        short *p = (short *) pkt->data;
258
259
        for (i = 0; i < ret; i += 4) {
260
            *p = ~*p;
261
            p += 2;
262
        }
263
    }
264 4972b26f Fabrice Bellard
    return 0;
265
}
266
267
static int audio_read_close(AVFormatContext *s1)
268
{
269
    AudioData *s = s1->priv_data;
270
271
    audio_close(s);
272
    return 0;
273
}
274
275 c9a65ca8 Fabrice Bellard
AVInputFormat audio_in_format = {
276
    "audio_device",
277
    "audio grab and output",
278
    sizeof(AudioData),
279
    NULL,
280
    audio_read_header,
281
    audio_read_packet,
282
    audio_read_close,
283 bb76a117 Måns Rullgård
    .flags = AVFMT_NOFILE,
284 c9a65ca8 Fabrice Bellard
};
285
286
AVOutputFormat audio_out_format = {
287 4972b26f Fabrice Bellard
    "audio_device",
288
    "audio grab and output",
289
    "",
290
    "",
291 c9a65ca8 Fabrice Bellard
    sizeof(AudioData),
292 4972b26f Fabrice Bellard
    /* XXX: we make the assumption that the soundcard accepts this format */
293
    /* XXX: find better solution with "preinit" method, needed also in
294
       other formats */
295
#ifdef WORDS_BIGENDIAN
296
    CODEC_ID_PCM_S16BE,
297
#else
298
    CODEC_ID_PCM_S16LE,
299
#endif
300
    CODEC_ID_NONE,
301
    audio_write_header,
302
    audio_write_packet,
303
    audio_write_trailer,
304 bb76a117 Måns Rullgård
    .flags = AVFMT_NOFILE,
305 de6d9b64 Fabrice Bellard
};
306 c9a65ca8 Fabrice Bellard
307
int audio_init(void)
308
{
309
    av_register_input_format(&audio_in_format);
310
    av_register_output_format(&audio_out_format);
311
    return 0;
312
}