oss_audio.c 8.85 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>

37
#include "libavutil/internal.h"
38
#include "libavutil/log.h"
39
#include "libavutil/opt.h"
40
#include "libavutil/time.h"
41
#include "libavcodec/avcodec.h"
42
#include "avdevice.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 82 83 84
    if (!is_output) {
        if (fcntl(audio_fd, F_SETFL, O_NONBLOCK) < 0) {
            av_log(s1, AV_LOG_WARNING, "%s: Could not enable non block mode (%s)\n", audio_device, strerror(errno));
        }
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
85

86
    s->frame_size = AUDIO_BLOCK_SIZE;
Fabrice Bellard's avatar
Fabrice Bellard committed
87

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

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

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

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

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

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

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

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

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

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

208 209
/* grab support */

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

216
    st = avformat_new_stream(s1, NULL);
217
    if (!st) {
218
        return AVERROR(ENOMEM);
219 220
    }

221
    ret = audio_open(s1, 0, s1->filename);
222
    if (ret < 0) {
223
        return AVERROR(EIO);
224
    }
225 226

    /* take real parameters */
227
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
228 229 230
    st->codec->codec_id = s->codec_id;
    st->codec->sample_rate = s->sample_rate;
    st->codec->channels = s->channels;
231

232
    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
233
    return 0;
234 235 236 237 238
}

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

243 244
    if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
        return ret;
245

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

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

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

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

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

    audio_close(s);
    return 0;
}

287
#if CONFIG_OSS_INDEV
288
static const AVOption options[] = {
289 290
    { "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 },
291 292 293 294 295 296 297 298
    { NULL },
};

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

302
AVInputFormat ff_oss_demuxer = {
303
    .name           = "oss",
304
    .long_name      = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
305 306 307 308 309 310
    .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
311
};
312
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
313

314
#if CONFIG_OSS_OUTDEV
315 316 317 318 319 320 321
static const AVClass oss_muxer_class = {
    .class_name     = "OSS muxer",
    .item_name      = av_default_item_name,
    .version        = LIBAVUTIL_VERSION_INT,
    .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
};

322
AVOutputFormat ff_oss_muxer = {
323
    .name           = "oss",
324
    .long_name      = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"),
325
    .priv_data_size = sizeof(AudioData),
326 327 328
    /* XXX: we make the assumption that the soundcard accepts this format */
    /* XXX: find better solution with "preinit" method, needed also in
       other formats */
329 330
    .audio_codec    = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
    .video_codec    = AV_CODEC_ID_NONE,
331 332 333 334
    .write_header   = audio_write_header,
    .write_packet   = audio_write_packet,
    .write_trailer  = audio_write_trailer,
    .flags          = AVFMT_NOFILE,
335
    .priv_class     = &oss_muxer_class,
Fabrice Bellard's avatar
Fabrice Bellard committed
336
};
337
#endif