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
24 * PulseAudio input using the simple API.
25 * @author Luca Barbato <lu_zero@gentoo.org>
28 #include <pulse/simple.h>
29 #include <pulse/rtclock.h>
30 #include <pulse/error.h>
32 #include "libavutil/internal.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/time.h"
36 #include "libavformat/avformat.h"
37 #include "libavformat/internal.h"
39 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
41 typedef struct PulseData {
52 int64_t frame_duration;
56 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
58 case AV_CODEC_ID_PCM_U8: return PA_SAMPLE_U8;
59 case AV_CODEC_ID_PCM_ALAW: return PA_SAMPLE_ALAW;
60 case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
61 case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
62 case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
63 case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
64 case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
65 case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
66 case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
67 case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
68 case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
69 default: return PA_SAMPLE_INVALID;
73 static av_cold int pulse_read_header(AVFormatContext *s)
75 PulseData *pd = s->priv_data;
79 enum AVCodecID codec_id =
80 s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
81 const pa_sample_spec ss = { codec_id_to_pulse_format(codec_id),
85 pa_buffer_attr attr = { -1 };
87 st = avformat_new_stream(s, NULL);
90 av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
91 return AVERROR(ENOMEM);
94 attr.fragsize = pd->fragment_size;
96 if (strcmp(s->filename, "default"))
99 pd->s = pa_simple_new(pd->server, pd->name,
101 device, pd->stream_name, &ss,
105 av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
109 /* take real parameters */
110 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
111 st->codec->codec_id = codec_id;
112 st->codec->sample_rate = pd->sample_rate;
113 st->codec->channels = pd->channels;
114 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
116 pd->pts = AV_NOPTS_VALUE;
117 pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
118 (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
123 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
125 PulseData *pd = s->priv_data;
129 if (av_new_packet(pkt, pd->frame_size) < 0) {
130 return AVERROR(ENOMEM);
133 if ((pa_simple_read(pd->s, pkt->data, pkt->size, &res)) < 0) {
134 av_log(s, AV_LOG_ERROR, "pa_simple_read failed: %s\n",
136 av_packet_unref(pkt);
140 if ((latency = pa_simple_get_latency(pd->s, &res)) == (pa_usec_t) -1) {
141 av_log(s, AV_LOG_ERROR, "pa_simple_get_latency() failed: %s\n",
146 if (pd->pts == AV_NOPTS_VALUE) {
149 pd->pts += av_gettime();
154 pd->pts += pd->frame_duration;
159 static av_cold int pulse_close(AVFormatContext *s)
161 PulseData *pd = s->priv_data;
162 pa_simple_free(pd->s);
166 #define OFFSET(a) offsetof(PulseData, a)
167 #define D AV_OPT_FLAG_DECODING_PARAM
169 static const AVOption options[] = {
170 { "server", "pulse server name", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
171 { "name", "application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = "libav"}, 0, 0, D },
172 { "stream_name", "stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
173 { "sample_rate", "sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
174 { "channels", "number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
175 { "frame_size", "number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
176 { "fragment_size", "buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
177 { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
181 static const AVClass pulse_demuxer_class = {
182 .class_name = "Pulse demuxer",
183 .item_name = av_default_item_name,
185 .version = LIBAVUTIL_VERSION_INT,
188 AVInputFormat ff_pulse_demuxer = {
190 .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
191 .priv_data_size = sizeof(PulseData),
192 .read_header = pulse_read_header,
193 .read_packet = pulse_read_packet,
194 .read_close = pulse_close,
195 .flags = AVFMT_NOFILE,
196 .priv_class = &pulse_demuxer_class,