2009-01-26 10:16:09 +01:00
|
|
|
/*
|
|
|
|
* ALSA input and output
|
|
|
|
* Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
|
|
|
|
* Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2009-01-26 10:16:09 +01:00
|
|
|
* ALSA input and output: common code
|
|
|
|
* @author Luca Abeni ( lucabe72 email it )
|
|
|
|
* @author Benoit Fouet ( benoit fouet free fr )
|
|
|
|
* @author Nicolas George ( nicolas george normalesup org )
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
2009-11-30 00:30:46 +01:00
|
|
|
#include "libavformat/avformat.h"
|
2009-01-26 10:16:09 +01:00
|
|
|
|
|
|
|
#include "alsa-audio.h"
|
|
|
|
|
|
|
|
static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
|
|
|
|
{
|
|
|
|
switch(codec_id) {
|
|
|
|
case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
|
|
|
|
case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
|
|
|
|
case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
|
|
|
|
default: return SND_PCM_FORMAT_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-15 00:30:11 +02:00
|
|
|
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
|
2009-01-26 10:16:09 +01:00
|
|
|
unsigned int *sample_rate,
|
2009-04-15 00:30:11 +02:00
|
|
|
int channels, enum CodecID *codec_id)
|
2009-01-26 10:16:09 +01:00
|
|
|
{
|
|
|
|
AlsaData *s = ctx->priv_data;
|
|
|
|
const char *audio_device;
|
|
|
|
int res, flags = 0;
|
|
|
|
snd_pcm_format_t format;
|
|
|
|
snd_pcm_t *h;
|
|
|
|
snd_pcm_hw_params_t *hw_params;
|
|
|
|
snd_pcm_uframes_t buffer_size, period_size;
|
|
|
|
|
|
|
|
if (ctx->filename[0] == 0) audio_device = "default";
|
|
|
|
else audio_device = ctx->filename;
|
|
|
|
|
|
|
|
if (*codec_id == CODEC_ID_NONE)
|
|
|
|
*codec_id = DEFAULT_CODEC_ID;
|
|
|
|
format = codec_id_to_pcm_format(*codec_id);
|
|
|
|
if (format == SND_PCM_FORMAT_UNKNOWN) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
|
|
|
|
|
|
|
|
if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
|
2009-03-07 19:17:13 +01:00
|
|
|
flags = SND_PCM_NONBLOCK;
|
2009-01-26 10:16:09 +01:00
|
|
|
}
|
|
|
|
res = snd_pcm_open(&h, audio_device, mode, flags);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
|
|
|
|
audio_device, snd_strerror(res));
|
2010-03-13 20:19:44 +01:00
|
|
|
return AVERROR(EIO);
|
2009-01-26 10:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_malloc(&hw_params);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_any(h, hw_params);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_set_format(h, hw_params, format);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
|
|
|
|
*codec_id, format, snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
|
|
|
|
channels, snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
|
|
|
|
/* TODO: maybe use ctx->max_picture_buffer somehow */
|
|
|
|
res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
|
|
|
|
res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->period_size = period_size;
|
|
|
|
|
|
|
|
res = snd_pcm_hw_params(h, hw_params);
|
|
|
|
if (res < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
|
|
|
|
snd_strerror(res));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(hw_params);
|
|
|
|
|
|
|
|
s->h = h;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
snd_pcm_hw_params_free(hw_params);
|
|
|
|
fail1:
|
|
|
|
snd_pcm_close(h);
|
2010-03-13 20:19:44 +01:00
|
|
|
return AVERROR(EIO);
|
2009-01-26 10:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
av_cold int ff_alsa_close(AVFormatContext *s1)
|
|
|
|
{
|
|
|
|
AlsaData *s = s1->priv_data;
|
|
|
|
|
|
|
|
snd_pcm_close(s->h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
|
|
|
|
{
|
|
|
|
AlsaData *s = s1->priv_data;
|
|
|
|
snd_pcm_t *handle = s->h;
|
|
|
|
|
|
|
|
av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
|
|
|
|
if (err == -EPIPE) {
|
|
|
|
err = snd_pcm_prepare(handle);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
|
|
|
|
|
2010-03-13 20:19:44 +01:00
|
|
|
return AVERROR(EIO);
|
2009-01-26 10:16:09 +01:00
|
|
|
}
|
|
|
|
} else if (err == -ESTRPIPE) {
|
|
|
|
av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|