Statistics
| Branch: | Revision:

ffmpeg / libav / audio.c @ 9eb82647

History | View | Annotate | Download (7.21 KB)

1
/*
2
 * Linux audio play and grab interface
3
 * Copyright (c) 2000, 2001 Fabrice Bellard.
4
 *
5
 * 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
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * 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
 */
19
#include "avformat.h"
20

    
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <sys/soundcard.h>
25
#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
#define AUDIO_BLOCK_SIZE 4096
34

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

    
46
static int audio_open(AudioData *s, int is_output)
47
{
48
    int audio_fd;
49
    int tmp, err;
50
    char *flip = getenv("AUDIO_FLIP_LEFT");
51

    
52
    /* open linux audio device */
53
    if (is_output)
54
        audio_fd = open(audio_device, O_WRONLY);
55
    else
56
        audio_fd = open(audio_device, O_RDONLY);
57
    if (audio_fd < 0) {
58
        perror(audio_device);
59
        return -EIO;
60
    }
61

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

    
66
    /* non blocking mode */
67
    fcntl(audio_fd, F_SETFL, O_NONBLOCK);
68

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

    
78
    /* 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
    if (err < 0) {
113
        perror("SNDCTL_DSP_SETFMT");
114
        goto fail;
115
    }
116
    
117
    tmp = (s->channels == 2);
118
    err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
119
    if (err < 0) {
120
        perror("SNDCTL_DSP_STEREO");
121
        goto fail;
122
    }
123
    if (tmp)
124
        s->channels = 2;
125
    
126
    tmp = s->sample_rate;
127
    err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
128
    if (err < 0) {
129
        perror("SNDCTL_DSP_SPEED");
130
        goto fail;
131
    }
132
    s->sample_rate = tmp; /* store real sample rate */
133
    s->fd = audio_fd;
134

    
135
    return 0;
136
 fail:
137
    close(audio_fd);
138
    return -EIO;
139
}
140

    
141
static int audio_close(AudioData *s)
142
{
143
    close(s->fd);
144
    return 0;
145
}
146

    
147
/* sound output support */
148
static int audio_write_header(AVFormatContext *s1)
149
{
150
    AudioData *s = s1->priv_data;
151
    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
                              UINT8 *buf, int size, int force_pts)
167
{
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
    return 0;
199
}
200

    
201
/* grab support */
202

    
203
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
204
{
205
    AudioData *s = s1->priv_data;
206
    AVStream *st;
207
    int ret;
208

    
209
    if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
210
        return -1;
211

    
212
    st = av_new_stream(s1, 0);
213
    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
        av_free(st);
222
        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
        if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
245
            av_free_packet(pkt);
246
            pkt->size = 0;
247
            return 0;
248
        }
249
        if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
250
            av_free_packet(pkt);
251
            return -EIO;
252
        }
253
    }
254
    pkt->size = ret;
255
    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
    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
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
    .flags = AVFMT_NOFILE,
284
};
285

    
286
AVOutputFormat audio_out_format = {
287
    "audio_device",
288
    "audio grab and output",
289
    "",
290
    "",
291
    sizeof(AudioData),
292
    /* 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
    .flags = AVFMT_NOFILE,
305
};
306

    
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
}