00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00048 #include <alsa/asoundlib.h>
00049 #include "libavformat/internal.h"
00050 #include "libavutil/opt.h"
00051 #include "libavutil/mathematics.h"
00052 
00053 #include "avdevice.h"
00054 #include "alsa-audio.h"
00055 
00056 static av_cold int audio_read_header(AVFormatContext *s1)
00057 {
00058     AlsaData *s = s1->priv_data;
00059     AVStream *st;
00060     int ret;
00061     enum AVCodecID codec_id;
00062 
00063     st = avformat_new_stream(s1, NULL);
00064     if (!st) {
00065         av_log(s1, AV_LOG_ERROR, "Cannot add stream\n");
00066 
00067         return AVERROR(ENOMEM);
00068     }
00069     codec_id    = s1->audio_codec_id;
00070 
00071     ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels,
00072         &codec_id);
00073     if (ret < 0) {
00074         return AVERROR(EIO);
00075     }
00076 
00077     
00078     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00079     st->codec->codec_id    = codec_id;
00080     st->codec->sample_rate = s->sample_rate;
00081     st->codec->channels    = s->channels;
00082     avpriv_set_pts_info(st, 64, 1, 1000000);  
00083     
00084     s->timefilter = ff_timefilter_new(1000000.0 / s->sample_rate,
00085                                       s->period_size, 1.5E-6);
00086     if (!s->timefilter)
00087         goto fail;
00088 
00089     return 0;
00090 
00091 fail:
00092     snd_pcm_close(s->h);
00093     return AVERROR(EIO);
00094 }
00095 
00096 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
00097 {
00098     AlsaData *s  = s1->priv_data;
00099     int res;
00100     int64_t dts;
00101     snd_pcm_sframes_t delay = 0;
00102 
00103     if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) {
00104         return AVERROR(EIO);
00105     }
00106 
00107     while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) {
00108         if (res == -EAGAIN) {
00109             av_free_packet(pkt);
00110 
00111             return AVERROR(EAGAIN);
00112         }
00113         if (ff_alsa_xrun_recover(s1, res) < 0) {
00114             av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n",
00115                    snd_strerror(res));
00116             av_free_packet(pkt);
00117 
00118             return AVERROR(EIO);
00119         }
00120         ff_timefilter_reset(s->timefilter);
00121     }
00122 
00123     dts = av_gettime();
00124     snd_pcm_delay(s->h, &delay);
00125     dts -= av_rescale(delay + res, 1000000, s->sample_rate);
00126     pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period);
00127     s->last_period = res;
00128 
00129     pkt->size = res * s->frame_size;
00130 
00131     return 0;
00132 }
00133 
00134 static const AVOption options[] = {
00135     { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00136     { "channels",    "", offsetof(AlsaData, channels),    AV_OPT_TYPE_INT, {.i64 = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00137     { NULL },
00138 };
00139 
00140 static const AVClass alsa_demuxer_class = {
00141     .class_name     = "ALSA demuxer",
00142     .item_name      = av_default_item_name,
00143     .option         = options,
00144     .version        = LIBAVUTIL_VERSION_INT,
00145 };
00146 
00147 AVInputFormat ff_alsa_demuxer = {
00148     .name           = "alsa",
00149     .long_name      = NULL_IF_CONFIG_SMALL("ALSA audio input"),
00150     .priv_data_size = sizeof(AlsaData),
00151     .read_header    = audio_read_header,
00152     .read_packet    = audio_read_packet,
00153     .read_close     = ff_alsa_close,
00154     .flags          = AVFMT_NOFILE,
00155     .priv_class     = &alsa_demuxer_class,
00156 };