3 * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Luca Barbato <lu_zero@gentoo.org>
27 * This avdevice decoder allows to capture audio from a Pulseaudio device using
31 #include <pulse/simple.h>
32 #include <pulse/rtclock.h>
33 #include <pulse/error.h>
35 #include "libavformat/avformat.h"
36 #include "libavutil/opt.h"
38 #define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE)
40 typedef struct PulseData {
53 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
55 case CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
56 case CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
57 case CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
58 case CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
59 case CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
60 case CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
61 case CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
62 case CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
63 case CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
64 case CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
65 case CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
66 default: return PA_SAMPLE_INVALID;
70 static av_cold int pulse_read_header(AVFormatContext *s,
71 AVFormatParameters *ap)
73 PulseData *pd = s->priv_data;
77 enum CodecID codec_id =
78 s->audio_codec_id == CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
79 const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
83 pa_buffer_attr attr = { -1 };
85 st = avformat_new_stream(s, NULL);
88 av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
89 return AVERROR(ENOMEM);
92 attr.fragsize = pd->fragment_size;
94 if (strcmp(s->filename, "default"))
97 pd->s = pa_simple_new(pd->server, pd->name,
99 device, pd->stream_name, &ss,
103 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
107 /* take real parameters */
108 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
109 st->codec->codec_id = codec_id;
110 st->codec->sample_rate = pd->sample_rate;
111 st->codec->channels = pd->channels;
112 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
114 pd->pts = AV_NOPTS_VALUE;
119 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
121 PulseData *pd = s->priv_data;
124 uint64_t frame_duration =
125 (pd->frame_size*1000000LL)/(pd->sample_rate * pd->channels);
127 if (av_new_packet(pkt, pd->frame_size) < 0) {
128 return AVERROR(ENOMEM);
131 if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
132 av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
138 if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
139 av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
144 if (pd->pts == AV_NOPTS_VALUE) {
148 pd->pts += frame_duration;
155 static av_cold int pulse_close(AVFormatContext *s)
157 PulseData *pd = s->priv_data;
158 pa_simple_free(pd->s);
162 #define OFFSET(a) offsetof(PulseData, a)
163 #define D AV_OPT_FLAG_DECODING_PARAM
165 static const AVOption options[] = {
166 { "server", "pulse server name",
167 OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
168 { "name", "application name",
169 OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
170 { "stream_name", "stream description",
171 OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
173 OFFSET(sample_rate), AV_OPT_TYPE_INT, {.dbl = 48000}, 1, INT_MAX, D },
175 OFFSET(channels), AV_OPT_TYPE_INT, {.dbl = 2}, 1, INT_MAX, D },
177 OFFSET(frame_size), AV_OPT_TYPE_INT, {.dbl = 1024}, 1, INT_MAX, D },
178 { "fragment_size", "buffering size, affects latency and cpu usage",
179 OFFSET(fragment_size), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX, D },
183 static const AVClass pulse_demuxer_class = {
184 .class_name = "Pulse demuxer",
185 .item_name = av_default_item_name,
187 .version = LIBAVUTIL_VERSION_INT,
190 AVInputFormat ff_pulse_demuxer = {
192 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
193 .priv_data_size = sizeof(PulseData),
194 .read_header = pulse_read_header,
195 .read_packet = pulse_read_packet,
196 .read_close = pulse_close,
197 .flags = AVFMT_NOFILE,
198 .priv_class = &pulse_demuxer_class,