yading@10: /* yading@10: * ALSA input and output yading@10: * Copyright (c) 2007 Luca Abeni ( lucabe72 email it ) yading@10: * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr ) yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * ALSA input and output: input yading@10: * @author Luca Abeni ( lucabe72 email it ) yading@10: * @author Benoit Fouet ( benoit fouet free fr ) yading@10: * @author Nicolas George ( nicolas george normalesup org ) yading@10: * yading@10: * This avdevice decoder allows to capture audio from an ALSA (Advanced yading@10: * Linux Sound Architecture) device. yading@10: * yading@10: * The filename parameter is the name of an ALSA PCM device capable of yading@10: * capture, for example "default" or "plughw:1"; see the ALSA documentation yading@10: * for naming conventions. The empty string is equivalent to "default". yading@10: * yading@10: * The capture period is set to the lower value available for the device, yading@10: * which gives a low latency suitable for real-time capture. yading@10: * yading@10: * The PTS are an Unix time in microsecond. yading@10: * yading@10: * Due to a bug in the ALSA library yading@10: * (https://bugtrack.alsa-project.org/alsa-bug/view.php?id=4308), this yading@10: * decoder does not work with certain ALSA plugins, especially the dsnoop yading@10: * plugin. yading@10: */ yading@10: yading@10: #include yading@10: #include "libavformat/internal.h" yading@10: #include "libavutil/opt.h" yading@10: #include "libavutil/mathematics.h" yading@10: #include "libavutil/time.h" yading@10: yading@10: #include "avdevice.h" yading@10: #include "alsa-audio.h" yading@10: yading@10: static av_cold int audio_read_header(AVFormatContext *s1) yading@10: { yading@10: AlsaData *s = s1->priv_data; yading@10: AVStream *st; yading@10: int ret; yading@10: enum AVCodecID codec_id; yading@10: yading@10: st = avformat_new_stream(s1, NULL); yading@10: if (!st) { yading@10: av_log(s1, AV_LOG_ERROR, "Cannot add stream\n"); yading@10: yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: codec_id = s1->audio_codec_id; yading@10: yading@10: ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels, yading@10: &codec_id); yading@10: if (ret < 0) { yading@10: return AVERROR(EIO); yading@10: } yading@10: yading@10: /* take real parameters */ yading@10: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@10: st->codec->codec_id = codec_id; yading@10: st->codec->sample_rate = s->sample_rate; yading@10: st->codec->channels = s->channels; yading@10: avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */ yading@10: /* microseconds instead of seconds, MHz instead of Hz */ yading@10: s->timefilter = ff_timefilter_new(1000000.0 / s->sample_rate, yading@10: s->period_size, 1.5E-6); yading@10: if (!s->timefilter) yading@10: goto fail; yading@10: yading@10: return 0; yading@10: yading@10: fail: yading@10: snd_pcm_close(s->h); yading@10: return AVERROR(EIO); yading@10: } yading@10: yading@10: static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt) yading@10: { yading@10: AlsaData *s = s1->priv_data; yading@10: int res; yading@10: int64_t dts; yading@10: snd_pcm_sframes_t delay = 0; yading@10: yading@10: if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) { yading@10: return AVERROR(EIO); yading@10: } yading@10: yading@10: while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) { yading@10: if (res == -EAGAIN) { yading@10: av_free_packet(pkt); yading@10: yading@10: return AVERROR(EAGAIN); yading@10: } yading@10: if (ff_alsa_xrun_recover(s1, res) < 0) { yading@10: av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n", yading@10: snd_strerror(res)); yading@10: av_free_packet(pkt); yading@10: yading@10: return AVERROR(EIO); yading@10: } yading@10: ff_timefilter_reset(s->timefilter); yading@10: } yading@10: yading@10: dts = av_gettime(); yading@10: snd_pcm_delay(s->h, &delay); yading@10: dts -= av_rescale(delay + res, 1000000, s->sample_rate); yading@10: pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period); yading@10: s->last_period = res; yading@10: yading@10: pkt->size = res * s->frame_size; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static const AVOption options[] = { yading@10: { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, yading@10: { "channels", "", offsetof(AlsaData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, yading@10: { NULL }, yading@10: }; yading@10: yading@10: static const AVClass alsa_demuxer_class = { yading@10: .class_name = "ALSA demuxer", yading@10: .item_name = av_default_item_name, yading@10: .option = options, yading@10: .version = LIBAVUTIL_VERSION_INT, yading@10: }; yading@10: yading@10: AVInputFormat ff_alsa_demuxer = { yading@10: .name = "alsa", yading@10: .long_name = NULL_IF_CONFIG_SMALL("ALSA audio input"), yading@10: .priv_data_size = sizeof(AlsaData), yading@10: .read_header = audio_read_header, yading@10: .read_packet = audio_read_packet, yading@10: .read_close = ff_alsa_close, yading@10: .flags = AVFMT_NOFILE, yading@10: .priv_class = &alsa_demuxer_class, yading@10: };