oss_audio.c 8.43 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Linux audio play and grab interface
3
 * Copyright (c) 2000, 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
Fabrice Bellard's avatar
Fabrice Bellard committed
21

22
#include "config.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
23 24
#include <stdlib.h>
#include <stdio.h>
25
#include <stdint.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
26
#include <string.h>
27
#include <errno.h>
28
#if HAVE_SOUNDCARD_H
29 30
#include <soundcard.h>
#else
31
#include <sys/soundcard.h>
32
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
33 34 35 36
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
37
#include <sys/select.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
38

39 40 41
#include "libavutil/log.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
42

43 44
#define AUDIO_BLOCK_SIZE 4096

Fabrice Bellard's avatar
Fabrice Bellard committed
45 46
typedef struct {
    int fd;
47
    int sample_rate;
Fabrice Bellard's avatar
Fabrice Bellard committed
48
    int channels;
49
    int frame_size; /* in bytes ! */
50
    enum CodecID codec_id;
51
    unsigned int flip_left : 1;
52
    uint8_t buffer[AUDIO_BLOCK_SIZE];
53
    int buffer_ptr;
Fabrice Bellard's avatar
Fabrice Bellard committed
54 55
} AudioData;

56
static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device)
Fabrice Bellard's avatar
Fabrice Bellard committed
57
{
58
    AudioData *s = s1->priv_data;
59
    int audio_fd;
Fabrice Bellard's avatar
Fabrice Bellard committed
60
    int tmp, err;
61
    char *flip = getenv("AUDIO_FLIP_LEFT");
Fabrice Bellard's avatar
Fabrice Bellard committed
62

63 64
    if (is_output)
        audio_fd = open(audio_device, O_WRONLY);
Fabrice Bellard's avatar
Fabrice Bellard committed
65
    else
66
        audio_fd = open(audio_device, O_RDONLY);
Fabrice Bellard's avatar
Fabrice Bellard committed
67
    if (audio_fd < 0) {
68
        av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
69
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
70 71
    }

72 73 74 75
    if (flip && *flip == '1') {
        s->flip_left = 1;
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
76
    /* non blocking mode */
77 78
    if (!is_output)
        fcntl(audio_fd, F_SETFL, O_NONBLOCK);
Fabrice Bellard's avatar
Fabrice Bellard committed
79

80
    s->frame_size = AUDIO_BLOCK_SIZE;
Fabrice Bellard's avatar
Fabrice Bellard committed
81
#if 0
82 83
    tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
    err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
Fabrice Bellard's avatar
Fabrice Bellard committed
84 85 86 87 88
    if (err < 0) {
        perror("SNDCTL_DSP_SETFRAGMENT");
    }
#endif

89 90
    /* select format : favour native format */
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
91

92
#if HAVE_BIGENDIAN
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    if (tmp & AFMT_S16_BE) {
        tmp = AFMT_S16_BE;
    } else if (tmp & AFMT_S16_LE) {
        tmp = AFMT_S16_LE;
    } else {
        tmp = 0;
    }
#else
    if (tmp & AFMT_S16_LE) {
        tmp = AFMT_S16_LE;
    } else if (tmp & AFMT_S16_BE) {
        tmp = AFMT_S16_BE;
    } else {
        tmp = 0;
    }
#endif

    switch(tmp) {
    case AFMT_S16_LE:
        s->codec_id = CODEC_ID_PCM_S16LE;
        break;
    case AFMT_S16_BE:
        s->codec_id = CODEC_ID_PCM_S16BE;
        break;
    default:
118
        av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
119
        close(audio_fd);
120
        return AVERROR(EIO);
121 122
    }
    err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
Fabrice Bellard's avatar
Fabrice Bellard committed
123
    if (err < 0) {
124
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
Fabrice Bellard's avatar
Fabrice Bellard committed
125 126
        goto fail;
    }
127

128 129
    tmp = (s->channels == 2);
    err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
Fabrice Bellard's avatar
Fabrice Bellard committed
130
    if (err < 0) {
131
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
Fabrice Bellard's avatar
Fabrice Bellard committed
132 133
        goto fail;
    }
134

135 136
    tmp = s->sample_rate;
    err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
Fabrice Bellard's avatar
Fabrice Bellard committed
137
    if (err < 0) {
138
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
Fabrice Bellard's avatar
Fabrice Bellard committed
139 140
        goto fail;
    }
141
    s->sample_rate = tmp; /* store real sample rate */
Fabrice Bellard's avatar
Fabrice Bellard committed
142 143 144 145 146
    s->fd = audio_fd;

    return 0;
 fail:
    close(audio_fd);
147
    return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
148 149
}

150
static int audio_close(AudioData *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
151 152
{
    close(s->fd);
153 154 155 156 157 158
    return 0;
}

/* sound output support */
static int audio_write_header(AVFormatContext *s1)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
159
    AudioData *s = s1->priv_data;
160 161 162 163
    AVStream *st;
    int ret;

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

174
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
175 176 177
{
    AudioData *s = s1->priv_data;
    int len, ret;
178 179
    int size= pkt->size;
    uint8_t *buf= pkt->data;
180 181 182 183 184 185 186 187 188 189

    while (size > 0) {
        len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
        if (len > size)
            len = size;
        memcpy(s->buffer + s->buffer_ptr, buf, len);
        s->buffer_ptr += len;
        if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
            for(;;) {
                ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
190
                if (ret > 0)
191 192
                    break;
                if (ret < 0 && (errno != EAGAIN && errno != EINTR))
193
                    return AVERROR(EIO);
194 195 196 197 198 199 200 201 202 203 204 205 206 207
            }
            s->buffer_ptr = 0;
        }
        buf += len;
        size -= len;
    }
    return 0;
}

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

    audio_close(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
208 209 210
    return 0;
}

211 212 213 214
/* grab support */

static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
215
    AudioData *s = s1->priv_data;
216 217 218
    AVStream *st;
    int ret;

219
    if (ap->sample_rate <= 0 || ap->channels <= 0)
220 221
        return -1;

Fabrice Bellard's avatar
Fabrice Bellard committed
222
    st = av_new_stream(s1, 0);
223
    if (!st) {
224
        return AVERROR(ENOMEM);
225 226 227 228
    }
    s->sample_rate = ap->sample_rate;
    s->channels = ap->channels;

229
    ret = audio_open(s1, 0, s1->filename);
230
    if (ret < 0) {
231
        return AVERROR(EIO);
232
    }
233 234

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

Luca Abeni's avatar
Luca Abeni committed
240
    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
241
    return 0;
242 243 244 245 246
}

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

251
    if (av_new_packet(pkt, s->frame_size) < 0)
252
        return AVERROR(EIO);
253
    for(;;) {
254 255 256 257 258 259 260 261 262 263 264 265
        struct timeval tv;
        fd_set fds;

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

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

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

266 267 268
        ret = read(s->fd, pkt->data, pkt->size);
        if (ret > 0)
            break;
269 270 271
        if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
            av_free_packet(pkt);
            pkt->size = 0;
Luca Abeni's avatar
Luca Abeni committed
272
            pkt->pts = av_gettime();
273 274
            return 0;
        }
275 276
        if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
            av_free_packet(pkt);
277
            return AVERROR(EIO);
278 279 280
        }
    }
    pkt->size = ret;
281 282 283 284 285 286 287

    /* compute pts of the start of the packet */
    cur_time = av_gettime();
    bdelay = ret;
    if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
        bdelay += abufi.bytes;
    }
Vitor Sessak's avatar
Vitor Sessak committed
288
    /* subtract time represented by the number of bytes in the audio fifo */
289 290 291
    cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);

    /* convert to wanted units */
Luca Abeni's avatar
Luca Abeni committed
292
    pkt->pts = cur_time;
293

294 295 296 297 298 299 300 301 302
    if (s->flip_left && s->channels == 2) {
        int i;
        short *p = (short *) pkt->data;

        for (i = 0; i < ret; i += 4) {
            *p = ~*p;
            p += 2;
        }
    }
303 304 305 306 307 308 309 310 311 312 313
    return 0;
}

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

    audio_close(s);
    return 0;
}

314
#if CONFIG_OSS_INDEV
315
AVInputFormat oss_demuxer = {
316
    "oss",
Benoit Fouet's avatar
Benoit Fouet committed
317
    NULL_IF_CONFIG_SMALL("Open Sound System capture"),
Fabrice Bellard's avatar
Fabrice Bellard committed
318 319 320 321 322
    sizeof(AudioData),
    NULL,
    audio_read_header,
    audio_read_packet,
    audio_read_close,
323
    .flags = AVFMT_NOFILE,
Fabrice Bellard's avatar
Fabrice Bellard committed
324
};
325
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
326

327
#if CONFIG_OSS_OUTDEV
328
AVOutputFormat oss_muxer = {
329
    "oss",
Benoit Fouet's avatar
Benoit Fouet committed
330
    NULL_IF_CONFIG_SMALL("Open Sound System playback"),
331 332
    "",
    "",
Fabrice Bellard's avatar
Fabrice Bellard committed
333
    sizeof(AudioData),
334 335 336
    /* XXX: we make the assumption that the soundcard accepts this format */
    /* XXX: find better solution with "preinit" method, needed also in
       other formats */
337
#if HAVE_BIGENDIAN
338 339 340 341 342 343 344 345
    CODEC_ID_PCM_S16BE,
#else
    CODEC_ID_PCM_S16LE,
#endif
    CODEC_ID_NONE,
    audio_write_header,
    audio_write_packet,
    audio_write_trailer,
346
    .flags = AVFMT_NOFILE,
Fabrice Bellard's avatar
Fabrice Bellard committed
347
};
348
#endif