de6d9b64 |
/*
* Linux audio play and grab interface |
406792e7 |
* Copyright (c) 2000, 2001 Fabrice Bellard |
de6d9b64 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
19720f15 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
de6d9b64 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. |
de6d9b64 |
* |
19720f15 |
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 |
*/ |
8be1c656 |
|
4f786086 |
#include "config.h" |
de6d9b64 |
#include <stdlib.h>
#include <stdio.h> |
4f786086 |
#include <stdint.h> |
de6d9b64 |
#include <string.h> |
4f786086 |
#include <errno.h> |
b250f9c6 |
#if HAVE_SOUNDCARD_H |
8c802695 |
#include <soundcard.h>
#else |
b13788c5 |
#include <sys/soundcard.h> |
8c802695 |
#endif |
de6d9b64 |
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
|
71bf6b41 |
#include "libavutil/internal.h" |
245976da |
#include "libavutil/log.h" |
003e63b6 |
#include "libavutil/opt.h" |
c4ef6a3e |
#include "libavutil/time.h" |
245976da |
#include "libavcodec/avcodec.h" |
6b899e16 |
#include "avdevice.h" |
c3f9ebf7 |
#include "libavformat/internal.h" |
4f786086 |
|
4972b26f |
#define AUDIO_BLOCK_SIZE 4096
|
de6d9b64 |
typedef struct { |
003e63b6 |
AVClass *class; |
de6d9b64 |
int fd; |
4972b26f |
int sample_rate; |
de6d9b64 |
int channels; |
4972b26f |
int frame_size; /* in bytes ! */ |
36ef5369 |
enum AVCodecID codec_id; |
72e043dd |
unsigned int flip_left : 1; |
0c1a9eda |
uint8_t buffer[AUDIO_BLOCK_SIZE]; |
4972b26f |
int buffer_ptr; |
de6d9b64 |
} AudioData;
|
d99b8166 |
static int audio_open(AVFormatContext *s1, int is_output, const char *audio_device) |
de6d9b64 |
{ |
d99b8166 |
AudioData *s = s1->priv_data; |
4972b26f |
int audio_fd; |
de6d9b64 |
int tmp, err; |
1de1cce2 |
char *flip = getenv("AUDIO_FLIP_LEFT"); |
de6d9b64 |
|
4972b26f |
if (is_output) |
71bf6b41 |
audio_fd = avpriv_open(audio_device, O_WRONLY); |
de6d9b64 |
else |
71bf6b41 |
audio_fd = avpriv_open(audio_device, O_RDONLY); |
de6d9b64 |
if (audio_fd < 0) { |
0c26e964 |
av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno)); |
6f3e0b21 |
return AVERROR(EIO); |
de6d9b64 |
}
|
1de1cce2 |
if (flip && *flip == '1') {
s->flip_left = 1;
}
|
de6d9b64 |
/* non blocking mode */ |
1ba0d9b5 |
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));
}
} |
de6d9b64 |
|
4972b26f |
s->frame_size = AUDIO_BLOCK_SIZE; |
de6d9b64 |
|
4972b26f |
/* select format : favour native format */
err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp); |
115329f1 |
|
63613fe6 |
#if HAVE_BIGENDIAN |
4972b26f |
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: |
36ef5369 |
s->codec_id = AV_CODEC_ID_PCM_S16LE; |
4972b26f |
break;
case AFMT_S16_BE: |
36ef5369 |
s->codec_id = AV_CODEC_ID_PCM_S16BE; |
4972b26f |
break;
default: |
0c26e964 |
av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n"); |
4972b26f |
close(audio_fd); |
6f3e0b21 |
return AVERROR(EIO); |
4972b26f |
}
err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp); |
de6d9b64 |
if (err < 0) { |
0c26e964 |
av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno)); |
de6d9b64 |
goto fail;
} |
115329f1 |
|
4972b26f |
tmp = (s->channels == 2);
err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp); |
de6d9b64 |
if (err < 0) { |
0c26e964 |
av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno)); |
de6d9b64 |
goto fail;
} |
115329f1 |
|
4972b26f |
tmp = s->sample_rate;
err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp); |
de6d9b64 |
if (err < 0) { |
0c26e964 |
av_log(s1, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno)); |
de6d9b64 |
goto fail;
} |
4972b26f |
s->sample_rate = tmp; /* store real sample rate */ |
de6d9b64 |
s->fd = audio_fd;
return 0;
fail:
close(audio_fd); |
6f3e0b21 |
return AVERROR(EIO); |
de6d9b64 |
}
|
4972b26f |
static int audio_close(AudioData *s) |
de6d9b64 |
{
close(s->fd); |
4972b26f |
return 0;
}
/* sound output support */
static int audio_write_header(AVFormatContext *s1)
{ |
c9a65ca8 |
AudioData *s = s1->priv_data; |
4972b26f |
AVStream *st;
int ret;
st = s1->streams[0]; |
01f4895c |
s->sample_rate = st->codec->sample_rate;
s->channels = st->codec->channels; |
d99b8166 |
ret = audio_open(s1, 1, s1->filename); |
4972b26f |
if (ret < 0) { |
6f3e0b21 |
return AVERROR(EIO); |
4972b26f |
} else {
return 0;
}
}
|
e928649b |
static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) |
4972b26f |
{
AudioData *s = s1->priv_data;
int len, ret; |
e928649b |
int size= pkt->size;
uint8_t *buf= pkt->data; |
4972b26f |
while (size > 0) { |
e76c0c6a |
len = FFMIN(AUDIO_BLOCK_SIZE - s->buffer_ptr, size); |
4972b26f |
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); |
4364a3e0 |
if (ret > 0) |
4972b26f |
break;
if (ret < 0 && (errno != EAGAIN && errno != EINTR)) |
6f3e0b21 |
return AVERROR(EIO); |
4972b26f |
}
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); |
de6d9b64 |
return 0;
}
|
4972b26f |
/* grab support */
|
6e9651d1 |
static int audio_read_header(AVFormatContext *s1) |
4972b26f |
{ |
c9a65ca8 |
AudioData *s = s1->priv_data; |
4972b26f |
AVStream *st;
int ret;
|
3b3bbdd3 |
st = avformat_new_stream(s1, NULL); |
4972b26f |
if (!st) { |
8fa36ae0 |
return AVERROR(ENOMEM); |
4972b26f |
}
|
d99b8166 |
ret = audio_open(s1, 0, s1->filename); |
4972b26f |
if (ret < 0) { |
6f3e0b21 |
return AVERROR(EIO); |
4972b26f |
} |
45dd5c69 |
/* take real parameters */ |
72415b2a |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
01f4895c |
st->codec->codec_id = s->codec_id;
st->codec->sample_rate = s->sample_rate;
st->codec->channels = s->channels; |
45dd5c69 |
|
c3f9ebf7 |
avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ |
45dd5c69 |
return 0; |
4972b26f |
}
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
AudioData *s = s1->priv_data; |
45dd5c69 |
int ret, bdelay;
int64_t cur_time;
struct audio_buf_info abufi; |
115329f1 |
|
d3298350 |
if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
return ret; |
79134973 |
|
802da0b4 |
ret = read(s->fd, pkt->data, pkt->size); |
fed0c2fb |
if (ret <= 0){
av_free_packet(pkt);
pkt->size = 0;
if (ret<0) return AVERROR(errno); |
bd01c393 |
else return AVERROR_EOF; |
4972b26f |
}
pkt->size = ret; |
45dd5c69 |
/* 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;
} |
52b541ad |
/* subtract time represented by the number of bytes in the audio fifo */ |
45dd5c69 |
cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
/* convert to wanted units */ |
0a7b514f |
pkt->pts = cur_time; |
45dd5c69 |
|
1de1cce2 |
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;
}
} |
4972b26f |
return 0;
}
static int audio_read_close(AVFormatContext *s1)
{
AudioData *s = s1->priv_data;
audio_close(s);
return 0;
}
|
38e54a75 |
#if CONFIG_OSS_INDEV |
003e63b6 |
static const AVOption options[] = { |
e6153f17 |
{ "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 }, |
003e63b6 |
{ NULL },
};
static const AVClass oss_demuxer_class = {
.class_name = "OSS demuxer",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
|
66355be3 |
AVInputFormat ff_oss_demuxer = { |
30b4ee79 |
.name = "oss", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"), |
30b4ee79 |
.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, |
c9a65ca8 |
}; |
ff70e601 |
#endif |
c9a65ca8 |
|
38e54a75 |
#if CONFIG_OSS_OUTDEV |
66355be3 |
AVOutputFormat ff_oss_muxer = { |
30b4ee79 |
.name = "oss", |
0177b7d2 |
.long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) playback"), |
30b4ee79 |
.priv_data_size = sizeof(AudioData), |
4972b26f |
/* XXX: we make the assumption that the soundcard accepts this format */
/* XXX: find better solution with "preinit" method, needed also in
other formats */ |
36ef5369 |
.audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
.video_codec = AV_CODEC_ID_NONE, |
30b4ee79 |
.write_header = audio_write_header,
.write_packet = audio_write_packet,
.write_trailer = audio_write_trailer,
.flags = AVFMT_NOFILE, |
de6d9b64 |
}; |
ff70e601 |
#endif |