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
30
37
38 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
39
49
54
59
60
61 #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
62 do { \
63 if (!(expression)) { \
64 rerror = AVERROR_EXTERNAL; \
65 goto label; \
66 } \
67 } while (0)
68
69 #define CHECK_DEAD_GOTO(p, rerror, label) \
70 do { \
71 if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
72 !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
73 rerror = AVERROR_EXTERNAL; \
74 goto label; \
75 } \
76 } while (0)
77
80
81 switch (pa_context_get_state(
c)) {
82 case PA_CONTEXT_READY:
83 case PA_CONTEXT_TERMINATED:
84 case PA_CONTEXT_FAILED:
85 pa_threaded_mainloop_signal(p->
mainloop, 0);
86 break;
87 }
88 }
89
92
93 switch (pa_stream_get_state(
s)) {
94 case PA_STREAM_READY:
95 case PA_STREAM_FAILED:
96 case PA_STREAM_TERMINATED:
97 pa_threaded_mainloop_signal(p->
mainloop, 0);
98 break;
99 }
100 }
101
104
105 pa_threaded_mainloop_signal(p->
mainloop, 0);
106 }
107
110
111 pa_threaded_mainloop_signal(p->
mainloop, 0);
112 }
113
115 {
117
119 pa_threaded_mainloop_stop(pd->
mainloop);
120
122 pa_stream_unref(pd->
stream);
124
126 pa_context_disconnect(pd->
context);
128 }
130
132 pa_threaded_mainloop_free(pd->
mainloop);
134
137
138 return 0;
139 }
140
142 {
152
153 pa_buffer_attr attr = { -1 };
154 pa_channel_map cmap;
155 const pa_buffer_attr *queried_attr;
156
157 pa_channel_map_init_extend(&cmap, pd->
channels, PA_CHANNEL_MAP_WAVEEX);
158
160
161 if (!st) {
164 }
165
167 // 50 ms fragments/latency by default seem good enough
169 } else {
171 }
172
173 if (
s->url[0] !=
'0円' && strcmp(
s->url,
"default"))
175
176 if (!(pd->
mainloop = pa_threaded_mainloop_new())) {
179 }
180
181 if (!(pd->
context = pa_context_new(pa_threaded_mainloop_get_api(pd->
mainloop), pd->
name))) {
184 }
185
187
191 }
192
193 pa_threaded_mainloop_lock(pd->
mainloop);
194
195 if (pa_threaded_mainloop_start(pd->
mainloop) < 0) {
197 goto unlock_and_fail;
198 }
199
200 for (;;) {
201 pa_context_state_t
state;
202
204
205 if (
state == PA_CONTEXT_READY)
206 break;
207
208 if (!PA_CONTEXT_IS_GOOD(
state)) {
210 goto unlock_and_fail;
211 }
212
213 /* Wait until the context is ready */
214 pa_threaded_mainloop_wait(pd->
mainloop);
215 }
216
219 goto unlock_and_fail;
220 }
221
226
227 ret = pa_stream_connect_record(pd->
stream, device, &attr,
228 PA_STREAM_INTERPOLATE_TIMING
229 |PA_STREAM_ADJUST_LATENCY
230 |PA_STREAM_AUTO_TIMING_UPDATE);
231
234 goto unlock_and_fail;
235 }
236
237 for (;;) {
238 pa_stream_state_t
state;
239
241
242 if (
state == PA_STREAM_READY)
243 break;
244
245 if (!PA_STREAM_IS_GOOD(
state)) {
247 goto unlock_and_fail;
248 }
249
250 /* Wait until the stream is ready */
251 pa_threaded_mainloop_wait(pd->
mainloop);
252 }
253
254 /* Query actual fragment size */
255 queried_attr = pa_stream_get_buffer_attr(pd->
stream);
256 if (!queried_attr || queried_attr->fragsize > INT_MAX/100) {
258 goto unlock_and_fail;
259 }
262
263 pa_threaded_mainloop_unlock(pd->
mainloop);
264
265 /* take real parameters */
271
274
278 }
279
280 return 0;
281
282 unlock_and_fail:
283 pa_threaded_mainloop_unlock(pd->
mainloop);
284
287 }
288
290 {
293 size_t read_length;
296 pa_usec_t latency;
297 int negative;
299
300 pa_threaded_mainloop_lock(pd->
mainloop);
301
303
304 while (pos < pd->fragment_size) {
306
309
310 if (read_length <= 0) {
311 pa_threaded_mainloop_wait(pd->
mainloop);
314 /* There's a hole in the stream, skip it. We could generate
315 * silence, but that wouldn't work for compressed streams. */
316 r = pa_stream_drop(pd->
stream);
318 } else {
322 goto unlock_and_fail;
323 }
324
326 pa_operation_unref(pa_stream_update_timing_info(pd->
stream,
NULL,
NULL));
327
328 if (pa_stream_get_latency(pd->
stream, &latency, &negative) >= 0) {
329 if (negative) {
330 dts += latency;
331 } else
332 dts -= latency;
333 } else {
335 }
336 }
339 break;
340 pa_stream_drop(pd->
stream);
341 /* Oversized fragment??? */
343 goto unlock_and_fail;
344 }
347 pa_stream_drop(pd->
stream);
348 }
349 }
350
351 pa_threaded_mainloop_unlock(pd->
mainloop);
352
354
358
359 return 0;
360
361 unlock_and_fail:
363 pa_threaded_mainloop_unlock(pd->
mainloop);
365 }
366
368 {
371 }
372
373 #define OFFSET(a) offsetof(PulseData, a)
374 #define D AV_OPT_FLAG_DECODING_PARAM
375 #define DEPR AV_OPT_FLAG_DEPRECATED
376
384 {
"fragment_size",
"set buffering size, affects latency and cpu usage",
OFFSET(fragment_size),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D },
385 {
"wallclock",
"set the initial pts using the current time",
OFFSET(wallclock),
AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1,
D },
387 };
388
395 };
396
407 };