• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavdevice/alsa-audio-dec.c

Go to the documentation of this file.
00001 /*
00002  * ALSA input and output
00003  * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
00004  * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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                                      AVFormatParameters *ap)
00058 {
00059     AlsaData *s = s1->priv_data;
00060     AVStream *st;
00061     int ret;
00062     enum CodecID codec_id;
00063     double o;
00064 
00065     st = avformat_new_stream(s1, NULL);
00066     if (!st) {
00067         av_log(s1, AV_LOG_ERROR, "Cannot add stream\n");
00068 
00069         return AVERROR(ENOMEM);
00070     }
00071     codec_id    = s1->audio_codec_id;
00072 
00073     ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels,
00074         &codec_id);
00075     if (ret < 0) {
00076         return AVERROR(EIO);
00077     }
00078 
00079     /* take real parameters */
00080     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00081     st->codec->codec_id    = codec_id;
00082     st->codec->sample_rate = s->sample_rate;
00083     st->codec->channels    = s->channels;
00084     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
00085     o = 2 * M_PI * s->period_size / s->sample_rate * 1.5; // bandwidth: 1.5Hz
00086     s->timefilter = ff_timefilter_new(1000000.0 / s->sample_rate,
00087                                       sqrt(2 * o), o * o);
00088     if (!s->timefilter)
00089         goto fail;
00090 
00091     return 0;
00092 
00093 fail:
00094     snd_pcm_close(s->h);
00095     return AVERROR(EIO);
00096 }
00097 
00098 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
00099 {
00100     AlsaData *s  = s1->priv_data;
00101     int res;
00102     int64_t dts;
00103     snd_pcm_sframes_t delay = 0;
00104 
00105     if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) {
00106         return AVERROR(EIO);
00107     }
00108 
00109     while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) {
00110         if (res == -EAGAIN) {
00111             av_free_packet(pkt);
00112 
00113             return AVERROR(EAGAIN);
00114         }
00115         if (ff_alsa_xrun_recover(s1, res) < 0) {
00116             av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n",
00117                    snd_strerror(res));
00118             av_free_packet(pkt);
00119 
00120             return AVERROR(EIO);
00121         }
00122         ff_timefilter_reset(s->timefilter);
00123     }
00124 
00125     dts = av_gettime();
00126     snd_pcm_delay(s->h, &delay);
00127     dts -= av_rescale(delay + res, 1000000, s->sample_rate);
00128     pkt->pts = ff_timefilter_update(s->timefilter, dts, res);
00129 
00130     pkt->size = res * s->frame_size;
00131 
00132     return 0;
00133 }
00134 
00135 static const AVOption options[] = {
00136     { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00137     { "channels",    "", offsetof(AlsaData, channels),    AV_OPT_TYPE_INT, {.dbl = 2},     1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00138     { NULL },
00139 };
00140 
00141 static const AVClass alsa_demuxer_class = {
00142     .class_name     = "ALSA demuxer",
00143     .item_name      = av_default_item_name,
00144     .option         = options,
00145     .version        = LIBAVUTIL_VERSION_INT,
00146 };
00147 
00148 AVInputFormat ff_alsa_demuxer = {
00149     .name           = "alsa",
00150     .long_name      = NULL_IF_CONFIG_SMALL("ALSA audio input"),
00151     .priv_data_size = sizeof(AlsaData),
00152     .read_header    = audio_read_header,
00153     .read_packet    = audio_read_packet,
00154     .read_close     = ff_alsa_close,
00155     .flags          = AVFMT_NOFILE,
00156     .priv_class     = &alsa_demuxer_class,
00157 };
Generated on Fri Feb 1 2013 14:34:47 for FFmpeg by doxygen 1.7.1