2001-07-22 14:18:56 +00:00
|
|
|
/*
|
|
|
|
* Linux audio play and grab interface
|
2002-05-25 22:34:32 +00:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-22 14:18:56 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-25 22:34:32 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-07-22 14:18:56 +00:00
|
|
|
*/
|
2001-08-13 21:37:10 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2002-05-26 15:09:58 +00:00
|
|
|
#include <sys/soundcard.h>
|
2001-07-22 14:18:56 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
#define AUDIO_BLOCK_SIZE 4096
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
2001-09-24 23:27:06 +00:00
|
|
|
int sample_rate;
|
2001-07-22 14:18:56 +00:00
|
|
|
int channels;
|
2001-09-24 23:27:06 +00:00
|
|
|
int frame_size; /* in bytes ! */
|
|
|
|
int codec_id;
|
2002-05-09 01:15:21 +00:00
|
|
|
int flip_left : 1;
|
2003-02-11 16:35:48 +00:00
|
|
|
uint8_t buffer[AUDIO_BLOCK_SIZE];
|
2001-09-24 23:27:06 +00:00
|
|
|
int buffer_ptr;
|
2001-07-22 14:18:56 +00:00
|
|
|
} AudioData;
|
|
|
|
|
2003-01-23 10:33:16 +00:00
|
|
|
static int audio_open(AudioData *s, int is_output, const char *audio_device)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2001-09-24 23:27:06 +00:00
|
|
|
int audio_fd;
|
2001-07-22 14:18:56 +00:00
|
|
|
int tmp, err;
|
2002-05-09 01:15:21 +00:00
|
|
|
char *flip = getenv("AUDIO_FLIP_LEFT");
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
/* open linux audio device */
|
2003-01-23 10:33:16 +00:00
|
|
|
if (!audio_device)
|
|
|
|
audio_device = "/dev/dsp";
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
if (is_output)
|
|
|
|
audio_fd = open(audio_device, O_WRONLY);
|
2001-07-22 14:18:56 +00:00
|
|
|
else
|
2001-09-24 23:27:06 +00:00
|
|
|
audio_fd = open(audio_device, O_RDONLY);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (audio_fd < 0) {
|
|
|
|
perror(audio_device);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2002-05-09 01:15:21 +00:00
|
|
|
if (flip && *flip == '1') {
|
|
|
|
s->flip_left = 1;
|
|
|
|
}
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
/* non blocking mode */
|
2002-11-19 18:21:18 +00:00
|
|
|
if (!is_output)
|
|
|
|
fcntl(audio_fd, F_SETFL, O_NONBLOCK);
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
s->frame_size = AUDIO_BLOCK_SIZE;
|
2001-07-22 14:18:56 +00:00
|
|
|
#if 0
|
2001-09-24 23:27:06 +00:00
|
|
|
tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SETFRAGMENT");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
/* select format : favour native format */
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
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:
|
|
|
|
fprintf(stderr, "Soundcard does not support 16 bit sample format\n");
|
|
|
|
close(audio_fd);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SETFMT");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
tmp = (s->channels == 2);
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_STEREO");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-05-09 01:15:21 +00:00
|
|
|
if (tmp)
|
|
|
|
s->channels = 2;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
tmp = s->sample_rate;
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SPEED");
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-09-24 23:27:06 +00:00
|
|
|
s->sample_rate = tmp; /* store real sample rate */
|
2001-07-22 14:18:56 +00:00
|
|
|
s->fd = audio_fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
close(audio_fd);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
static int audio_close(AudioData *s)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
|
|
|
close(s->fd);
|
2001-09-24 23:27:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sound output support */
|
|
|
|
static int audio_write_header(AVFormatContext *s1)
|
|
|
|
{
|
2002-05-20 16:31:13 +00:00
|
|
|
AudioData *s = s1->priv_data;
|
2001-09-24 23:27:06 +00:00
|
|
|
AVStream *st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
st = s1->streams[0];
|
|
|
|
s->sample_rate = st->codec.sample_rate;
|
|
|
|
s->channels = st->codec.channels;
|
2003-01-23 10:33:16 +00:00
|
|
|
ret = audio_open(s, 1, NULL);
|
2001-09-24 23:27:06 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_write_packet(AVFormatContext *s1, int stream_index,
|
2003-09-10 22:37:33 +00:00
|
|
|
const uint8_t *buf, int size, int64_t pts)
|
2001-09-24 23:27:06 +00:00
|
|
|
{
|
|
|
|
AudioData *s = s1->priv_data;
|
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
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);
|
2002-11-19 18:21:18 +00:00
|
|
|
if (ret > 0)
|
2001-09-24 23:27:06 +00:00
|
|
|
break;
|
|
|
|
if (ret < 0 && (errno != EAGAIN && errno != EINTR))
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
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);
|
2001-07-22 14:18:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
/* grab support */
|
|
|
|
|
|
|
|
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
|
|
{
|
2002-05-20 16:31:13 +00:00
|
|
|
AudioData *s = s1->priv_data;
|
2001-09-24 23:27:06 +00:00
|
|
|
AVStream *st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-05-20 16:31:13 +00:00
|
|
|
st = av_new_stream(s1, 0);
|
2001-09-24 23:27:06 +00:00
|
|
|
if (!st) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
s->sample_rate = ap->sample_rate;
|
|
|
|
s->channels = ap->channels;
|
|
|
|
|
2003-01-23 10:33:16 +00:00
|
|
|
ret = audio_open(s, 0, ap->device);
|
2001-09-24 23:27:06 +00:00
|
|
|
if (ret < 0) {
|
2002-05-18 23:11:09 +00:00
|
|
|
av_free(st);
|
2001-09-24 23:27:06 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
2002-11-19 15:52:29 +00:00
|
|
|
|
|
|
|
/* take real parameters */
|
|
|
|
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;
|
|
|
|
|
|
|
|
av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
|
|
|
|
return 0;
|
2001-09-24 23:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AudioData *s = s1->priv_data;
|
2002-11-19 15:52:29 +00:00
|
|
|
int ret, bdelay;
|
|
|
|
int64_t cur_time;
|
|
|
|
struct audio_buf_info abufi;
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
if (av_new_packet(pkt, s->frame_size) < 0)
|
|
|
|
return -EIO;
|
|
|
|
for(;;) {
|
2003-02-05 01:59:52 +00:00
|
|
|
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);
|
|
|
|
|
2001-09-24 23:27:06 +00:00
|
|
|
ret = read(s->fd, pkt->data, pkt->size);
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
2002-04-08 20:58:56 +00:00
|
|
|
if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
pkt->size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-24 23:27:06 +00:00
|
|
|
if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pkt->size = ret;
|
2002-11-19 15:52:29 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
/* substract time represented by the number of bytes in the audio fifo */
|
|
|
|
cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
|
|
|
|
|
|
|
|
/* convert to wanted units */
|
|
|
|
pkt->pts = cur_time & ((1LL << 48) - 1);
|
|
|
|
|
2002-05-09 01:15:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2001-09-24 23:27:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_read_close(AVFormatContext *s1)
|
|
|
|
{
|
|
|
|
AudioData *s = s1->priv_data;
|
|
|
|
|
|
|
|
audio_close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-11 09:07:32 +00:00
|
|
|
static AVInputFormat audio_in_format = {
|
2002-05-20 16:31:13 +00:00
|
|
|
"audio_device",
|
|
|
|
"audio grab and output",
|
|
|
|
sizeof(AudioData),
|
|
|
|
NULL,
|
|
|
|
audio_read_header,
|
|
|
|
audio_read_packet,
|
|
|
|
audio_read_close,
|
2002-10-06 23:06:06 +00:00
|
|
|
.flags = AVFMT_NOFILE,
|
2002-05-20 16:31:13 +00:00
|
|
|
};
|
|
|
|
|
2002-11-11 09:07:32 +00:00
|
|
|
static AVOutputFormat audio_out_format = {
|
2001-09-24 23:27:06 +00:00
|
|
|
"audio_device",
|
|
|
|
"audio grab and output",
|
|
|
|
"",
|
|
|
|
"",
|
2002-05-20 16:31:13 +00:00
|
|
|
sizeof(AudioData),
|
2001-09-24 23:27:06 +00:00
|
|
|
/* XXX: we make the assumption that the soundcard accepts this format */
|
|
|
|
/* XXX: find better solution with "preinit" method, needed also in
|
|
|
|
other formats */
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
CODEC_ID_PCM_S16BE,
|
|
|
|
#else
|
|
|
|
CODEC_ID_PCM_S16LE,
|
|
|
|
#endif
|
|
|
|
CODEC_ID_NONE,
|
|
|
|
audio_write_header,
|
|
|
|
audio_write_packet,
|
|
|
|
audio_write_trailer,
|
2002-10-06 23:06:06 +00:00
|
|
|
.flags = AVFMT_NOFILE,
|
2001-07-22 14:18:56 +00:00
|
|
|
};
|
2002-05-20 16:31:13 +00:00
|
|
|
|
|
|
|
int audio_init(void)
|
|
|
|
{
|
|
|
|
av_register_input_format(&audio_in_format);
|
|
|
|
av_register_output_format(&audio_out_format);
|
|
|
|
return 0;
|
|
|
|
}
|