2001-07-22 17:18:56 +03:00
|
|
|
/*
|
|
|
|
* Linux audio play and grab interface
|
2002-05-26 01:34:32 +03:00
|
|
|
* Copyright (c) 2000, 2001 Fabrice Bellard.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:34:32 +03: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 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:34:32 +03:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2001-07-22 17:18:56 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:34:32 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:34:32 +03: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 17:18:56 +03:00
|
|
|
*/
|
2001-08-14 00:37:10 +03:00
|
|
|
#include "avformat.h"
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2002-05-26 18:09:58 +03:00
|
|
|
#include <sys/soundcard.h>
|
2001-07-22 17:18:56 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
const char *audio_device = "/dev/dsp";
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
#define AUDIO_BLOCK_SIZE 4096
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
2001-09-25 02:27:06 +03:00
|
|
|
int sample_rate;
|
2001-07-22 17:18:56 +03:00
|
|
|
int channels;
|
2001-09-25 02:27:06 +03:00
|
|
|
int frame_size; /* in bytes ! */
|
|
|
|
int codec_id;
|
2002-05-09 04:15:21 +03:00
|
|
|
int flip_left : 1;
|
2001-09-25 02:27:06 +03:00
|
|
|
UINT8 buffer[AUDIO_BLOCK_SIZE];
|
|
|
|
int buffer_ptr;
|
2001-07-22 17:18:56 +03:00
|
|
|
} AudioData;
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
static int audio_open(AudioData *s, int is_output)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2001-09-25 02:27:06 +03:00
|
|
|
int audio_fd;
|
2001-07-22 17:18:56 +03:00
|
|
|
int tmp, err;
|
2002-05-09 04:15:21 +03:00
|
|
|
char *flip = getenv("AUDIO_FLIP_LEFT");
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
/* open linux audio device */
|
2001-09-25 02:27:06 +03:00
|
|
|
if (is_output)
|
|
|
|
audio_fd = open(audio_device, O_WRONLY);
|
2001-07-22 17:18:56 +03:00
|
|
|
else
|
2001-09-25 02:27:06 +03:00
|
|
|
audio_fd = open(audio_device, O_RDONLY);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (audio_fd < 0) {
|
|
|
|
perror(audio_device);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2002-05-09 04:15:21 +03:00
|
|
|
if (flip && *flip == '1') {
|
|
|
|
s->flip_left = 1;
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* non blocking mode */
|
|
|
|
fcntl(audio_fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
s->frame_size = AUDIO_BLOCK_SIZE;
|
2001-07-22 17:18:56 +03:00
|
|
|
#if 0
|
2001-09-25 02:27:06 +03:00
|
|
|
tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SETFRAGMENT");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-09-25 02:27:06 +03: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 17:18:56 +03:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SETFMT");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
tmp = (s->channels == 2);
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_STEREO");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-05-09 04:15:21 +03:00
|
|
|
if (tmp)
|
|
|
|
s->channels = 2;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
tmp = s->sample_rate;
|
|
|
|
err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (err < 0) {
|
|
|
|
perror("SNDCTL_DSP_SPEED");
|
|
|
|
goto fail;
|
|
|
|
}
|
2001-09-25 02:27:06 +03:00
|
|
|
s->sample_rate = tmp; /* store real sample rate */
|
2001-07-22 17:18:56 +03:00
|
|
|
s->fd = audio_fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
close(audio_fd);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
static int audio_close(AudioData *s)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
close(s->fd);
|
2001-09-25 02:27:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sound output support */
|
|
|
|
static int audio_write_header(AVFormatContext *s1)
|
|
|
|
{
|
2002-05-20 19:31:13 +03:00
|
|
|
AudioData *s = s1->priv_data;
|
2001-09-25 02:27:06 +03:00
|
|
|
AVStream *st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
st = s1->streams[0];
|
|
|
|
s->sample_rate = st->codec.sample_rate;
|
|
|
|
s->channels = st->codec.channels;
|
|
|
|
ret = audio_open(s, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_write_packet(AVFormatContext *s1, int stream_index,
|
2002-04-08 00:44:29 +03:00
|
|
|
UINT8 *buf, int size, int force_pts)
|
2001-09-25 02:27:06 +03: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);
|
|
|
|
if (ret != 0)
|
|
|
|
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 17:18:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-25 02:27:06 +03:00
|
|
|
/* grab support */
|
|
|
|
|
|
|
|
static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
|
|
|
|
{
|
2002-05-20 19:31:13 +03:00
|
|
|
AudioData *s = s1->priv_data;
|
2001-09-25 02:27:06 +03:00
|
|
|
AVStream *st;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-05-20 19:31:13 +03:00
|
|
|
st = av_new_stream(s1, 0);
|
2001-09-25 02:27:06 +03:00
|
|
|
if (!st) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
s->sample_rate = ap->sample_rate;
|
|
|
|
s->channels = ap->channels;
|
|
|
|
|
|
|
|
ret = audio_open(s, 0);
|
|
|
|
if (ret < 0) {
|
2002-05-19 02:11:09 +03:00
|
|
|
av_free(st);
|
2001-09-25 02:27:06 +03:00
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
/* 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;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AudioData *s = s1->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, s->frame_size) < 0)
|
|
|
|
return -EIO;
|
|
|
|
for(;;) {
|
|
|
|
ret = read(s->fd, pkt->data, pkt->size);
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
2002-04-08 23:58:56 +03:00
|
|
|
if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
pkt->size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-25 02:27:06 +03:00
|
|
|
if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pkt->size = ret;
|
2002-05-09 04:15:21 +03: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-25 02:27:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_read_close(AVFormatContext *s1)
|
|
|
|
{
|
|
|
|
AudioData *s = s1->priv_data;
|
|
|
|
|
|
|
|
audio_close(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-20 19:31:13 +03:00
|
|
|
AVInputFormat audio_in_format = {
|
|
|
|
"audio_device",
|
|
|
|
"audio grab and output",
|
|
|
|
sizeof(AudioData),
|
|
|
|
NULL,
|
|
|
|
audio_read_header,
|
|
|
|
audio_read_packet,
|
|
|
|
audio_read_close,
|
2002-10-07 02:06:06 +03:00
|
|
|
.flags = AVFMT_NOFILE,
|
2002-05-20 19:31:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVOutputFormat audio_out_format = {
|
2001-09-25 02:27:06 +03:00
|
|
|
"audio_device",
|
|
|
|
"audio grab and output",
|
|
|
|
"",
|
|
|
|
"",
|
2002-05-20 19:31:13 +03:00
|
|
|
sizeof(AudioData),
|
2001-09-25 02:27:06 +03: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-07 02:06:06 +03:00
|
|
|
.flags = AVFMT_NOFILE,
|
2001-07-22 17:18:56 +03:00
|
|
|
};
|
2002-05-20 19:31:13 +03:00
|
|
|
|
|
|
|
int audio_init(void)
|
|
|
|
{
|
|
|
|
av_register_input_format(&audio_in_format);
|
|
|
|
av_register_output_format(&audio_out_format);
|
|
|
|
return 0;
|
|
|
|
}
|