• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Pulseaudio input
3  * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
4  * Copyright 2004-2006 Lennart Poettering
5  * Copyright (c) 2014 Michael Niedermayer <michaelni@gmx.at>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <pulse/rtclock.h>
25 #include <pulse/error.h>
26 
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/time.h"
30 
31 #include "libavformat/avformat.h"
32 #include "libavformat/internal.h"
33 #include "libavformat/version.h"
34 #include "pulse_audio_common.h"
35 #include "timefilter.h"
36 
37 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
38 
39 typedef struct PulseData {
40     AVClass *class;
41     char *server;
42     char *name;
43     char *stream_name;
44     int  sample_rate;
45     int  channels;
46     int  frame_size;
47     int  fragment_size;
48 
49     pa_threaded_mainloop *mainloop;
50     pa_context *context;
51     pa_stream *stream;
52     size_t pa_frame_size;
53 
54     TimeFilter *timefilter;
55     int last_period;
56     int wallclock;
57 } PulseData;
58 
59 
60 #define CHECK_SUCCESS_GOTO(rerror, expression, label)        \
61     do {                                                        \
62         if (!(expression)) {                                    \
63             rerror = AVERROR_EXTERNAL;                          \
64             goto label;                                         \
65         }                                                       \
66     } while (0)
67 
68 #define CHECK_DEAD_GOTO(p, rerror, label)                               \
69     do {                                                                \
70         if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
71             !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
72             rerror = AVERROR_EXTERNAL;                                  \
73             goto label;                                                 \
74         }                                                               \
75     } while (0)
76 
context_state_cb(pa_context * c,void * userdata)77 static void context_state_cb(pa_context *c, void *userdata) {
78     PulseData *p = userdata;
79 
80     switch (pa_context_get_state(c)) {
81         case PA_CONTEXT_READY:
82         case PA_CONTEXT_TERMINATED:
83         case PA_CONTEXT_FAILED:
84             pa_threaded_mainloop_signal(p->mainloop, 0);
85             break;
86     }
87 }
88 
stream_state_cb(pa_stream * s,void * userdata)89 static void stream_state_cb(pa_stream *s, void * userdata) {
90     PulseData *p = userdata;
91 
92     switch (pa_stream_get_state(s)) {
93         case PA_STREAM_READY:
94         case PA_STREAM_FAILED:
95         case PA_STREAM_TERMINATED:
96             pa_threaded_mainloop_signal(p->mainloop, 0);
97             break;
98     }
99 }
100 
stream_request_cb(pa_stream * s,size_t length,void * userdata)101 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
102     PulseData *p = userdata;
103 
104     pa_threaded_mainloop_signal(p->mainloop, 0);
105 }
106 
stream_latency_update_cb(pa_stream * s,void * userdata)107 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
108     PulseData *p = userdata;
109 
110     pa_threaded_mainloop_signal(p->mainloop, 0);
111 }
112 
pulse_close(AVFormatContext * s)113 static av_cold int pulse_close(AVFormatContext *s)
114 {
115     PulseData *pd = s->priv_data;
116 
117     if (pd->mainloop)
118         pa_threaded_mainloop_stop(pd->mainloop);
119 
120     if (pd->stream)
121         pa_stream_unref(pd->stream);
122     pd->stream = NULL;
123 
124     if (pd->context) {
125         pa_context_disconnect(pd->context);
126         pa_context_unref(pd->context);
127     }
128     pd->context = NULL;
129 
130     if (pd->mainloop)
131         pa_threaded_mainloop_free(pd->mainloop);
132     pd->mainloop = NULL;
133 
134     ff_timefilter_destroy(pd->timefilter);
135     pd->timefilter = NULL;
136 
137     return 0;
138 }
139 
pulse_read_header(AVFormatContext * s)140 static av_cold int pulse_read_header(AVFormatContext *s)
141 {
142     PulseData *pd = s->priv_data;
143     AVStream *st;
144     char *device = NULL;
145     int ret;
146     enum AVCodecID codec_id =
147         s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
148     const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
149                                 pd->sample_rate,
150                                 pd->channels };
151 
152     pa_buffer_attr attr = { -1 };
153     pa_channel_map cmap;
154     const pa_buffer_attr *queried_attr;
155 
156     pa_channel_map_init_extend(&cmap, pd->channels, PA_CHANNEL_MAP_WAVEEX);
157 
158     st = avformat_new_stream(s, NULL);
159 
160     if (!st) {
161         av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
162         return AVERROR(ENOMEM);
163     }
164 
165     if (pd->fragment_size == -1) {
166         // 50 ms fragments/latency by default seem good enough
167         attr.fragsize = pa_frame_size(&ss) * (pd->sample_rate / 20);
168     } else {
169         attr.fragsize = pd->fragment_size;
170     }
171 
172     if (s->url[0] != '\0' && strcmp(s->url, "default"))
173         device = s->url;
174 
175     if (!(pd->mainloop = pa_threaded_mainloop_new())) {
176         pulse_close(s);
177         return AVERROR_EXTERNAL;
178     }
179 
180     if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
181         pulse_close(s);
182         return AVERROR_EXTERNAL;
183     }
184 
185     pa_context_set_state_callback(pd->context, context_state_cb, pd);
186 
187     if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
188         pulse_close(s);
189         return AVERROR(pa_context_errno(pd->context));
190     }
191 
192     pa_threaded_mainloop_lock(pd->mainloop);
193 
194     if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
195         ret = -1;
196         goto unlock_and_fail;
197     }
198 
199     for (;;) {
200         pa_context_state_t state;
201 
202         state = pa_context_get_state(pd->context);
203 
204         if (state == PA_CONTEXT_READY)
205             break;
206 
207         if (!PA_CONTEXT_IS_GOOD(state)) {
208             ret = AVERROR(pa_context_errno(pd->context));
209             goto unlock_and_fail;
210         }
211 
212         /* Wait until the context is ready */
213         pa_threaded_mainloop_wait(pd->mainloop);
214     }
215 
216     if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, &cmap))) {
217         ret = AVERROR(pa_context_errno(pd->context));
218         goto unlock_and_fail;
219     }
220 
221     pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
222     pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
223     pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
224     pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
225 
226     ret = pa_stream_connect_record(pd->stream, device, &attr,
227                                     PA_STREAM_INTERPOLATE_TIMING
228                                     |PA_STREAM_ADJUST_LATENCY
229                                     |PA_STREAM_AUTO_TIMING_UPDATE);
230 
231     if (ret < 0) {
232         ret = AVERROR(pa_context_errno(pd->context));
233         goto unlock_and_fail;
234     }
235 
236     for (;;) {
237         pa_stream_state_t state;
238 
239         state = pa_stream_get_state(pd->stream);
240 
241         if (state == PA_STREAM_READY)
242             break;
243 
244         if (!PA_STREAM_IS_GOOD(state)) {
245             ret = AVERROR(pa_context_errno(pd->context));
246             goto unlock_and_fail;
247         }
248 
249         /* Wait until the stream is ready */
250         pa_threaded_mainloop_wait(pd->mainloop);
251     }
252 
253     /* Query actual fragment size */
254     queried_attr = pa_stream_get_buffer_attr(pd->stream);
255     if (!queried_attr || queried_attr->fragsize > INT_MAX/100) {
256         ret = AVERROR_EXTERNAL;
257         goto unlock_and_fail;
258     }
259     pd->fragment_size = queried_attr->fragsize;
260     pd->pa_frame_size = pa_frame_size(&ss);
261 
262     pa_threaded_mainloop_unlock(pd->mainloop);
263 
264     /* take real parameters */
265     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
266     st->codecpar->codec_id    = codec_id;
267     st->codecpar->sample_rate = pd->sample_rate;
268     st->codecpar->ch_layout.nb_channels = pd->channels;
269     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
270 
271     pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
272                                        pd->fragment_size / pd->pa_frame_size, 1.5E-6);
273 
274     if (!pd->timefilter) {
275         pulse_close(s);
276         return AVERROR(ENOMEM);
277     }
278 
279     return 0;
280 
281 unlock_and_fail:
282     pa_threaded_mainloop_unlock(pd->mainloop);
283 
284     pulse_close(s);
285     return ret;
286 }
287 
pulse_read_packet(AVFormatContext * s,AVPacket * pkt)288 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
289 {
290     PulseData *pd  = s->priv_data;
291     int ret;
292     size_t read_length;
293     const void *read_data = NULL;
294     int64_t dts;
295     pa_usec_t latency;
296     int negative;
297     ptrdiff_t pos = 0;
298 
299     pa_threaded_mainloop_lock(pd->mainloop);
300 
301     CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
302 
303     while (pos < pd->fragment_size) {
304         int r;
305 
306         r = pa_stream_peek(pd->stream, &read_data, &read_length);
307         CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
308 
309         if (read_length <= 0) {
310             pa_threaded_mainloop_wait(pd->mainloop);
311             CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
312         } else if (!read_data) {
313             /* There's a hole in the stream, skip it. We could generate
314                 * silence, but that wouldn't work for compressed streams. */
315             r = pa_stream_drop(pd->stream);
316             CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
317         } else {
318             if (!pos) {
319                 if (av_new_packet(pkt, pd->fragment_size) < 0) {
320                     ret = AVERROR(ENOMEM);
321                     goto unlock_and_fail;
322                 }
323 
324                 dts = av_gettime();
325                 pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
326 
327                 if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
328                     if (negative) {
329                         dts += latency;
330                     } else
331                         dts -= latency;
332                 } else {
333                     av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
334                 }
335             }
336             if (pkt->size - pos < read_length) {
337                 if (pos)
338                     break;
339                 pa_stream_drop(pd->stream);
340                 /* Oversized fragment??? */
341                 ret = AVERROR_EXTERNAL;
342                 goto unlock_and_fail;
343             }
344             memcpy(pkt->data + pos, read_data, read_length);
345             pos += read_length;
346             pa_stream_drop(pd->stream);
347         }
348     }
349 
350     pa_threaded_mainloop_unlock(pd->mainloop);
351 
352     av_shrink_packet(pkt, pos);
353 
354     if (pd->wallclock)
355         pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
356     pd->last_period = pkt->size / pd->pa_frame_size;
357 
358     return 0;
359 
360 unlock_and_fail:
361     av_packet_unref(pkt);
362     pa_threaded_mainloop_unlock(pd->mainloop);
363     return ret;
364 }
365 
pulse_get_device_list(AVFormatContext * h,AVDeviceInfoList * device_list)366 static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
367 {
368     PulseData *s = h->priv_data;
369     return ff_pulse_audio_get_devices(device_list, s->server, 0);
370 }
371 
372 #define OFFSET(a) offsetof(PulseData, a)
373 #define D AV_OPT_FLAG_DECODING_PARAM
374 #define DEPR AV_OPT_FLAG_DEPRECATED
375 
376 static const AVOption options[] = {
377     { "server",        "set PulseAudio server",                             OFFSET(server),        AV_OPT_TYPE_STRING, {.str = NULL},     0, 0, D },
378     { "name",          "set application name",                              OFFSET(name),          AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, D },
379     { "stream_name",   "set stream description",                            OFFSET(stream_name),   AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
380     { "sample_rate",   "set sample rate in Hz",                             OFFSET(sample_rate),   AV_OPT_TYPE_INT,    {.i64 = 48000},    1, INT_MAX, D },
381     { "channels",      "set number of audio channels",                      OFFSET(channels),      AV_OPT_TYPE_INT,    {.i64 = 2},        1, INT_MAX, D },
382     { "frame_size",    "set number of bytes per frame",                     OFFSET(frame_size),    AV_OPT_TYPE_INT,    {.i64 = 1024},     1, INT_MAX, D | DEPR },
383     { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT,    {.i64 = -1},      -1, INT_MAX, D },
384     { "wallclock",     "set the initial pts using the current time",     OFFSET(wallclock),     AV_OPT_TYPE_INT,    {.i64 = 1},       -1, 1, D },
385     { NULL },
386 };
387 
388 static const AVClass pulse_demuxer_class = {
389     .class_name     = "Pulse indev",
390     .item_name      = av_default_item_name,
391     .option         = options,
392     .version        = LIBAVUTIL_VERSION_INT,
393     .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
394 };
395 
396 const AVInputFormat ff_pulse_demuxer = {
397     .name           = "pulse",
398     .long_name      = NULL_IF_CONFIG_SMALL("Pulse audio input"),
399     .priv_data_size = sizeof(PulseData),
400     .read_header    = pulse_read_header,
401     .read_packet    = pulse_read_packet,
402     .read_close     = pulse_close,
403     .get_device_list = pulse_get_device_list,
404     .flags          = AVFMT_NOFILE,
405     .priv_class     = &pulse_demuxer_class,
406 };
407