oss_audio.c 8.39 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
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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>

37
#include "libavutil/internal.h"
38
#include "libavutil/log.h"
39
#include "libavutil/opt.h"
40
#include "libavutil/time.h"
41 42
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
43
#include "libavformat/internal.h"
44

45 46
#define AUDIO_BLOCK_SIZE 4096

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

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

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

75 76 77 78
    if (flip && *flip == '1') {
        s->flip_left = 1;
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
79
    /* non blocking mode */
80 81
    if (!is_output)
        fcntl(audio_fd, F_SETFL, O_NONBLOCK);
Fabrice Bellard's avatar
Fabrice Bellard committed
82

83
    s->frame_size = AUDIO_BLOCK_SIZE;
Fabrice Bellard's avatar
Fabrice Bellard committed
84

85 86
    /* select format : favour native format */
    err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
87

88
#if HAVE_BIGENDIAN
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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:
108
        s->codec_id = AV_CODEC_ID_PCM_S16LE;
109 110
        break;
    case AFMT_S16_BE:
111
        s->codec_id = AV_CODEC_ID_PCM_S16BE;
112 113
        break;
    default:
114
        av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
115
        close(audio_fd);
116
        return AVERROR(EIO);
117 118
    }
    err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
Fabrice Bellard's avatar
Fabrice Bellard committed
119
    if (err < 0) {
120
        av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
Fabrice Bellard's avatar
Fabrice Bellard committed
121 122
        goto fail;
    }
123

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

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

    return 0;
 fail:
    close(audio_fd);
143
    return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
144 145
}

146
static int audio_close(AudioData *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
147 148
{
    close(s->fd);
149 150 151 152 153 154
    return 0;
}

/* sound output support */
static int audio_write_header(AVFormatContext *s1)
{
Fabrice Bellard's avatar
Fabrice Bellard committed
155
    AudioData *s = s1->priv_data;
156 157 158 159
    AVStream *st;
    int ret;

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

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

    while (size > 0) {
178
        len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size);
179 180 181 182 183
        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);
184
                if (ret > 0)
185 186
                    break;
                if (ret < 0 && (errno != EAGAIN && errno != EINTR))
187
                    return AVERROR(EIO);
188 189 190 191 192 193 194 195 196 197 198 199 200 201
            }
            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
202 203 204
    return 0;
}

205 206
/* grab support */

207
static int audio_read_header(AVFormatContext *s1)
208
{
Fabrice Bellard's avatar
Fabrice Bellard committed
209
    AudioData *s = s1->priv_data;
210 211 212
    AVStream *st;
    int ret;

213
    st = avformat_new_stream(s1, NULL);
214
    if (!st) {
215
        return AVERROR(ENOMEM);
216 217
    }

218
    ret = audio_open(s1, 0, s1->filename);
219
    if (ret < 0) {
220
        return AVERROR(EIO);
221
    }
222 223

    /* take real parameters */
224
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
225 226 227
    st->codec->codec_id = s->codec_id;
    st->codec->sample_rate = s->sample_rate;
    st->codec->channels = s->channels;
228

229
    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
230
    return 0;
231 232 233 234 235
}

static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
    AudioData *s = s1->priv_data;
236 237 238
    int ret, bdelay;
    int64_t cur_time;
    struct audio_buf_info abufi;
239

240 241
    if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
        return ret;
242

Ramiro Polla's avatar
Ramiro Polla committed
243
    ret = read(s->fd, pkt->data, pkt->size);
244 245 246 247
    if (ret <= 0){
        av_free_packet(pkt);
        pkt->size = 0;
        if (ret<0)  return AVERROR(errno);
248
        else        return AVERROR_EOF;
249 250
    }
    pkt->size = ret;
251 252 253 254 255 256 257

    /* 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
258
    /* subtract time represented by the number of bytes in the audio fifo */
259 260 261
    cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);

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

264 265 266 267 268 269 270 271 272
    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;
        }
    }
273 274 275 276 277 278 279 280 281 282 283
    return 0;
}

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

    audio_close(s);
    return 0;
}

284
#if CONFIG_OSS_INDEV
285
static const AVOption options[] = {
286 287
    { "sample_rate", "", offsetof(AudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
    { "channels",    "", offsetof(AudioData, channels),    AV_OPT_TYPE_INT, {.i64 = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
288 289 290 291 292 293 294 295 296 297
    { NULL },
};

static const AVClass oss_demuxer_class = {
    .class_name     = "OSS demuxer",
    .item_name      = av_default_item_name,
    .option         = options,
    .version        = LIBAVUTIL_VERSION_INT,
};

298
AVInputFormat ff_oss_demuxer = {
299
    .name           = "oss",
300
    .long_name      = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
301 302 303 304 305 306
    .priv_data_size = sizeof(AudioData),
    .read_header    = audio_read_header,
    .read_packet    = audio_read_packet,
    .read_close     = audio_read_close,
    .flags          = AVFMT_NOFILE,
    .priv_class     = &oss_demuxer_class,
Fabrice Bellard's avatar
Fabrice Bellard committed
307
};
308
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
309

310
#if CONFIG_OSS_OUTDEV
311
AVOutputFormat ff_oss_muxer = {
312
    .name           = "oss",
313
    .long_name      = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
314
    .priv_data_size = sizeof(AudioData),
315 316 317
    /* XXX: we make the assumption that the soundcard accepts this format */
    /* XXX: find better solution with "preinit" method, needed also in
       other formats */
318 319
    .audio_codec    = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
    .video_codec    = AV_CODEC_ID_NONE,
320 321 322 323
    .write_header   = audio_write_header,
    .write_packet   = audio_write_packet,
    .write_trailer  = audio_write_trailer,
    .flags          = AVFMT_NOFILE,
Fabrice Bellard's avatar
Fabrice Bellard committed
324
};
325
#endif