yading@10
|
1 /*
|
yading@10
|
2 * ALSA input and output
|
yading@10
|
3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
|
yading@10
|
4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 /**
|
yading@10
|
24 * @file
|
yading@10
|
25 * ALSA input and output: input
|
yading@10
|
26 * @author Luca Abeni ( lucabe72 email it )
|
yading@10
|
27 * @author Benoit Fouet ( benoit fouet free fr )
|
yading@10
|
28 * @author Nicolas George ( nicolas george normalesup org )
|
yading@10
|
29 *
|
yading@10
|
30 * This avdevice decoder allows to capture audio from an ALSA (Advanced
|
yading@10
|
31 * Linux Sound Architecture) device.
|
yading@10
|
32 *
|
yading@10
|
33 * The filename parameter is the name of an ALSA PCM device capable of
|
yading@10
|
34 * capture, for example "default" or "plughw:1"; see the ALSA documentation
|
yading@10
|
35 * for naming conventions. The empty string is equivalent to "default".
|
yading@10
|
36 *
|
yading@10
|
37 * The capture period is set to the lower value available for the device,
|
yading@10
|
38 * which gives a low latency suitable for real-time capture.
|
yading@10
|
39 *
|
yading@10
|
40 * The PTS are an Unix time in microsecond.
|
yading@10
|
41 *
|
yading@10
|
42 * Due to a bug in the ALSA library
|
yading@10
|
43 * (https://bugtrack.alsa-project.org/alsa-bug/view.php?id=4308), this
|
yading@10
|
44 * decoder does not work with certain ALSA plugins, especially the dsnoop
|
yading@10
|
45 * plugin.
|
yading@10
|
46 */
|
yading@10
|
47
|
yading@10
|
48 #include <alsa/asoundlib.h>
|
yading@10
|
49 #include "libavformat/internal.h"
|
yading@10
|
50 #include "libavutil/opt.h"
|
yading@10
|
51 #include "libavutil/mathematics.h"
|
yading@10
|
52 #include "libavutil/time.h"
|
yading@10
|
53
|
yading@10
|
54 #include "avdevice.h"
|
yading@10
|
55 #include "alsa-audio.h"
|
yading@10
|
56
|
yading@10
|
57 static av_cold int audio_read_header(AVFormatContext *s1)
|
yading@10
|
58 {
|
yading@10
|
59 AlsaData *s = s1->priv_data;
|
yading@10
|
60 AVStream *st;
|
yading@10
|
61 int ret;
|
yading@10
|
62 enum AVCodecID codec_id;
|
yading@10
|
63
|
yading@10
|
64 st = avformat_new_stream(s1, NULL);
|
yading@10
|
65 if (!st) {
|
yading@10
|
66 av_log(s1, AV_LOG_ERROR, "Cannot add stream\n");
|
yading@10
|
67
|
yading@10
|
68 return AVERROR(ENOMEM);
|
yading@10
|
69 }
|
yading@10
|
70 codec_id = s1->audio_codec_id;
|
yading@10
|
71
|
yading@10
|
72 ret = ff_alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels,
|
yading@10
|
73 &codec_id);
|
yading@10
|
74 if (ret < 0) {
|
yading@10
|
75 return AVERROR(EIO);
|
yading@10
|
76 }
|
yading@10
|
77
|
yading@10
|
78 /* take real parameters */
|
yading@10
|
79 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
yading@10
|
80 st->codec->codec_id = codec_id;
|
yading@10
|
81 st->codec->sample_rate = s->sample_rate;
|
yading@10
|
82 st->codec->channels = s->channels;
|
yading@10
|
83 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
|
yading@10
|
84 /* microseconds instead of seconds, MHz instead of Hz */
|
yading@10
|
85 s->timefilter = ff_timefilter_new(1000000.0 / s->sample_rate,
|
yading@10
|
86 s->period_size, 1.5E-6);
|
yading@10
|
87 if (!s->timefilter)
|
yading@10
|
88 goto fail;
|
yading@10
|
89
|
yading@10
|
90 return 0;
|
yading@10
|
91
|
yading@10
|
92 fail:
|
yading@10
|
93 snd_pcm_close(s->h);
|
yading@10
|
94 return AVERROR(EIO);
|
yading@10
|
95 }
|
yading@10
|
96
|
yading@10
|
97 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
|
yading@10
|
98 {
|
yading@10
|
99 AlsaData *s = s1->priv_data;
|
yading@10
|
100 int res;
|
yading@10
|
101 int64_t dts;
|
yading@10
|
102 snd_pcm_sframes_t delay = 0;
|
yading@10
|
103
|
yading@10
|
104 if (av_new_packet(pkt, s->period_size * s->frame_size) < 0) {
|
yading@10
|
105 return AVERROR(EIO);
|
yading@10
|
106 }
|
yading@10
|
107
|
yading@10
|
108 while ((res = snd_pcm_readi(s->h, pkt->data, s->period_size)) < 0) {
|
yading@10
|
109 if (res == -EAGAIN) {
|
yading@10
|
110 av_free_packet(pkt);
|
yading@10
|
111
|
yading@10
|
112 return AVERROR(EAGAIN);
|
yading@10
|
113 }
|
yading@10
|
114 if (ff_alsa_xrun_recover(s1, res) < 0) {
|
yading@10
|
115 av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n",
|
yading@10
|
116 snd_strerror(res));
|
yading@10
|
117 av_free_packet(pkt);
|
yading@10
|
118
|
yading@10
|
119 return AVERROR(EIO);
|
yading@10
|
120 }
|
yading@10
|
121 ff_timefilter_reset(s->timefilter);
|
yading@10
|
122 }
|
yading@10
|
123
|
yading@10
|
124 dts = av_gettime();
|
yading@10
|
125 snd_pcm_delay(s->h, &delay);
|
yading@10
|
126 dts -= av_rescale(delay + res, 1000000, s->sample_rate);
|
yading@10
|
127 pkt->pts = ff_timefilter_update(s->timefilter, dts, s->last_period);
|
yading@10
|
128 s->last_period = res;
|
yading@10
|
129
|
yading@10
|
130 pkt->size = res * s->frame_size;
|
yading@10
|
131
|
yading@10
|
132 return 0;
|
yading@10
|
133 }
|
yading@10
|
134
|
yading@10
|
135 static const AVOption options[] = {
|
yading@10
|
136 { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
|
yading@10
|
137 { "channels", "", offsetof(AlsaData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
|
yading@10
|
138 { NULL },
|
yading@10
|
139 };
|
yading@10
|
140
|
yading@10
|
141 static const AVClass alsa_demuxer_class = {
|
yading@10
|
142 .class_name = "ALSA demuxer",
|
yading@10
|
143 .item_name = av_default_item_name,
|
yading@10
|
144 .option = options,
|
yading@10
|
145 .version = LIBAVUTIL_VERSION_INT,
|
yading@10
|
146 };
|
yading@10
|
147
|
yading@10
|
148 AVInputFormat ff_alsa_demuxer = {
|
yading@10
|
149 .name = "alsa",
|
yading@10
|
150 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio input"),
|
yading@10
|
151 .priv_data_size = sizeof(AlsaData),
|
yading@10
|
152 .read_header = audio_read_header,
|
yading@10
|
153 .read_packet = audio_read_packet,
|
yading@10
|
154 .read_close = ff_alsa_close,
|
yading@10
|
155 .flags = AVFMT_NOFILE,
|
yading@10
|
156 .priv_class = &alsa_demuxer_class,
|
yading@10
|
157 };
|