]> git.sesse.net Git - ffmpeg/blob - libavdevice/pulse_audio_dec.c
Merge commit 'eac77fcd56fc2a3391f0d86faf54302afb368ff7'
[ffmpeg] / libavdevice / pulse_audio_dec.c
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 #include "libavformat/avformat.h"
27 #include "libavformat/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/time.h"
30 #include "pulse_audio_common.h"
31 #include "timefilter.h"
32
33 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
34
35 typedef struct PulseData {
36     AVClass *class;
37     char *server;
38     char *name;
39     char *stream_name;
40     int  sample_rate;
41     int  channels;
42     int  frame_size;
43     int  fragment_size;
44
45     pa_threaded_mainloop *mainloop;
46     pa_context *context;
47     pa_stream *stream;
48
49     TimeFilter *timefilter;
50     int last_period;
51 } PulseData;
52
53
54 #define CHECK_SUCCESS_GOTO(rerror, expression, label)        \
55     do {                                                        \
56         if (!(expression)) {                                    \
57             rerror = AVERROR_EXTERNAL;                          \
58             goto label;                                         \
59         }                                                       \
60     } while(0);
61
62 #define CHECK_DEAD_GOTO(p, rerror, label)                               \
63     do {                                                                \
64         if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
65             !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
66             rerror = AVERROR_EXTERNAL;                                  \
67             goto label;                                                 \
68         }                                                               \
69     } while(0);
70
71 static void context_state_cb(pa_context *c, void *userdata) {
72     PulseData *p = userdata;
73
74     switch (pa_context_get_state(c)) {
75         case PA_CONTEXT_READY:
76         case PA_CONTEXT_TERMINATED:
77         case PA_CONTEXT_FAILED:
78             pa_threaded_mainloop_signal(p->mainloop, 0);
79             break;
80     }
81 }
82
83 static void stream_state_cb(pa_stream *s, void * userdata) {
84     PulseData *p = userdata;
85
86     switch (pa_stream_get_state(s)) {
87         case PA_STREAM_READY:
88         case PA_STREAM_FAILED:
89         case PA_STREAM_TERMINATED:
90             pa_threaded_mainloop_signal(p->mainloop, 0);
91             break;
92     }
93 }
94
95 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
96     PulseData *p = userdata;
97
98     pa_threaded_mainloop_signal(p->mainloop, 0);
99 }
100
101 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
102     PulseData *p = userdata;
103
104     pa_threaded_mainloop_signal(p->mainloop, 0);
105 }
106
107 static av_cold int pulse_close(AVFormatContext *s)
108 {
109     PulseData *pd = s->priv_data;
110
111     if (pd->mainloop)
112         pa_threaded_mainloop_stop(pd->mainloop);
113
114     if (pd->stream)
115         pa_stream_unref(pd->stream);
116     pd->stream = NULL;
117
118     if (pd->context) {
119         pa_context_disconnect(pd->context);
120         pa_context_unref(pd->context);
121     }
122     pd->context = NULL;
123
124     if (pd->mainloop)
125         pa_threaded_mainloop_free(pd->mainloop);
126     pd->mainloop = NULL;
127
128     ff_timefilter_destroy(pd->timefilter);
129     pd->timefilter = NULL;
130
131     return 0;
132 }
133
134 static av_cold int pulse_read_header(AVFormatContext *s)
135 {
136     PulseData *pd = s->priv_data;
137     AVStream *st;
138     char *device = NULL;
139     int ret;
140     enum AVCodecID codec_id =
141         s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
142     const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
143                                 pd->sample_rate,
144                                 pd->channels };
145
146     pa_buffer_attr attr = { -1 };
147
148     st = avformat_new_stream(s, NULL);
149
150     if (!st) {
151         av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
152         return AVERROR(ENOMEM);
153     }
154
155     attr.fragsize = pd->fragment_size;
156
157     if (strcmp(s->filename, "default"))
158         device = s->filename;
159
160     if (!(pd->mainloop = pa_threaded_mainloop_new())) {
161         pulse_close(s);
162         return AVERROR_EXTERNAL;
163     }
164
165     if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
166         pulse_close(s);
167         return AVERROR_EXTERNAL;
168     }
169
170     pa_context_set_state_callback(pd->context, context_state_cb, pd);
171
172     if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
173         pulse_close(s);
174         return AVERROR(pa_context_errno(pd->context));
175     }
176
177     pa_threaded_mainloop_lock(pd->mainloop);
178
179     if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
180         ret = -1;
181         goto unlock_and_fail;
182     }
183
184     for (;;) {
185         pa_context_state_t state;
186
187         state = pa_context_get_state(pd->context);
188
189         if (state == PA_CONTEXT_READY)
190             break;
191
192         if (!PA_CONTEXT_IS_GOOD(state)) {
193             ret = AVERROR(pa_context_errno(pd->context));
194             goto unlock_and_fail;
195         }
196
197         /* Wait until the context is ready */
198         pa_threaded_mainloop_wait(pd->mainloop);
199     }
200
201     if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, NULL))) {
202         ret = AVERROR(pa_context_errno(pd->context));
203         goto unlock_and_fail;
204     }
205
206     pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
207     pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
208     pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
209     pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
210
211     ret = pa_stream_connect_record(pd->stream, device, &attr,
212                                     PA_STREAM_INTERPOLATE_TIMING
213                                     |PA_STREAM_ADJUST_LATENCY
214                                     |PA_STREAM_AUTO_TIMING_UPDATE);
215
216     if (ret < 0) {
217         ret = AVERROR(pa_context_errno(pd->context));
218         goto unlock_and_fail;
219     }
220
221     for (;;) {
222         pa_stream_state_t state;
223
224         state = pa_stream_get_state(pd->stream);
225
226         if (state == PA_STREAM_READY)
227             break;
228
229         if (!PA_STREAM_IS_GOOD(state)) {
230             ret = AVERROR(pa_context_errno(pd->context));
231             goto unlock_and_fail;
232         }
233
234         /* Wait until the stream is ready */
235         pa_threaded_mainloop_wait(pd->mainloop);
236     }
237
238     pa_threaded_mainloop_unlock(pd->mainloop);
239
240     /* take real parameters */
241     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
242     st->codec->codec_id    = codec_id;
243     st->codec->sample_rate = pd->sample_rate;
244     st->codec->channels    = pd->channels;
245     avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
246
247     pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
248                                        1000, 1.5E-6);
249
250     if (!pd->timefilter) {
251         pulse_close(s);
252         return AVERROR(ENOMEM);
253     }
254
255     return 0;
256
257 unlock_and_fail:
258     pa_threaded_mainloop_unlock(pd->mainloop);
259
260     pulse_close(s);
261     return ret;
262 }
263
264 static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
265 {
266     PulseData *pd  = s->priv_data;
267     int ret;
268     size_t read_length;
269     const void *read_data = NULL;
270     int64_t dts;
271     pa_usec_t latency;
272     int negative;
273
274     pa_threaded_mainloop_lock(pd->mainloop);
275
276     CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
277
278     while (!read_data) {
279         int r;
280
281         r = pa_stream_peek(pd->stream, &read_data, &read_length);
282         CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
283
284         if (read_length <= 0) {
285             pa_threaded_mainloop_wait(pd->mainloop);
286             CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
287         } else if (!read_data) {
288             /* There's a hole in the stream, skip it. We could generate
289                 * silence, but that wouldn't work for compressed streams. */
290             r = pa_stream_drop(pd->stream);
291             CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
292         }
293     }
294
295     if (av_new_packet(pkt, read_length) < 0) {
296         ret = AVERROR(ENOMEM);
297         goto unlock_and_fail;
298     }
299
300     dts = av_gettime();
301     pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
302
303     if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
304         enum AVCodecID codec_id =
305             s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
306         int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels);
307         int frame_duration = read_length / frame_size;
308
309
310         if (negative) {
311             dts += latency;
312         } else
313             dts -= latency;
314         pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
315
316         pd->last_period = frame_duration;
317     } else {
318         av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
319     }
320
321     memcpy(pkt->data, read_data, read_length);
322     pa_stream_drop(pd->stream);
323
324     pa_threaded_mainloop_unlock(pd->mainloop);
325     return 0;
326
327 unlock_and_fail:
328     pa_threaded_mainloop_unlock(pd->mainloop);
329     return ret;
330 }
331
332 static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
333 {
334     PulseData *s = h->priv_data;
335     return ff_pulse_audio_get_devices(device_list, s->server, 0);
336 }
337
338 #define OFFSET(a) offsetof(PulseData, a)
339 #define D AV_OPT_FLAG_DECODING_PARAM
340
341 static const AVOption options[] = {
342     { "server",        "set PulseAudio server",                             OFFSET(server),        AV_OPT_TYPE_STRING, {.str = NULL},     0, 0, D },
343     { "name",          "set application name",                              OFFSET(name),          AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, D },
344     { "stream_name",   "set stream description",                            OFFSET(stream_name),   AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
345     { "sample_rate",   "set sample rate in Hz",                             OFFSET(sample_rate),   AV_OPT_TYPE_INT,    {.i64 = 48000},    1, INT_MAX, D },
346     { "channels",      "set number of audio channels",                      OFFSET(channels),      AV_OPT_TYPE_INT,    {.i64 = 2},        1, INT_MAX, D },
347     { "frame_size",    "set number of bytes per frame",                     OFFSET(frame_size),    AV_OPT_TYPE_INT,    {.i64 = 1024},     1, INT_MAX, D },
348     { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT,    {.i64 = -1},      -1, INT_MAX, D },
349     { NULL },
350 };
351
352 static const AVClass pulse_demuxer_class = {
353     .class_name     = "Pulse demuxer",
354     .item_name      = av_default_item_name,
355     .option         = options,
356     .version        = LIBAVUTIL_VERSION_INT,
357     .category       = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
358 };
359
360 AVInputFormat ff_pulse_demuxer = {
361     .name           = "pulse",
362     .long_name      = NULL_IF_CONFIG_SMALL("Pulse audio input"),
363     .priv_data_size = sizeof(PulseData),
364     .read_header    = pulse_read_header,
365     .read_packet    = pulse_read_packet,
366     .read_close     = pulse_close,
367     .get_device_list = pulse_get_device_list,
368     .flags          = AVFMT_NOFILE,
369     .priv_class     = &pulse_demuxer_class,
370 };