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
35
36 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
37
47
52
57
58
59 #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
60 do { \
61 if (!(expression)) { \
62 rerror = AVERROR_EXTERNAL; \
63 goto label; \
64 } \
65 } while (0)
66
67 #define CHECK_DEAD_GOTO(p, rerror, label) \
68 do { \
69 if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
70 !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
71 rerror = AVERROR_EXTERNAL; \
72 goto label; \
73 } \
74 } while (0)
75
78
79 switch (pa_context_get_state(
c)) {
80 case PA_CONTEXT_READY:
81 case PA_CONTEXT_TERMINATED:
82 case PA_CONTEXT_FAILED:
83 pa_threaded_mainloop_signal(p->
mainloop, 0);
84 break;
85 }
86 }
87
90
91 switch (pa_stream_get_state(
s)) {
92 case PA_STREAM_READY:
93 case PA_STREAM_FAILED:
94 case PA_STREAM_TERMINATED:
95 pa_threaded_mainloop_signal(p->
mainloop, 0);
96 break;
97 }
98 }
99
102
103 pa_threaded_mainloop_signal(p->
mainloop, 0);
104 }
105
108
109 pa_threaded_mainloop_signal(p->
mainloop, 0);
110 }
111
113 {
115
117 pa_threaded_mainloop_stop(pd->
mainloop);
118
120 pa_stream_unref(pd->
stream);
122
124 pa_context_disconnect(pd->
context);
126 }
128
130 pa_threaded_mainloop_free(pd->
mainloop);
132
135
136 return 0;
137 }
138
140 {
150
151 pa_buffer_attr attr = { -1 };
152 pa_channel_map cmap;
153 const pa_buffer_attr *queried_attr;
154
155 pa_channel_map_init_extend(&cmap, pd->
channels, PA_CHANNEL_MAP_WAVEEX);
156
158
159 if (!st) {
162 }
163
165
166 if (
s->url[0] !=
'0円' && strcmp(
s->url,
"default"))
168
169 if (!(pd->
mainloop = pa_threaded_mainloop_new())) {
172 }
173
174 if (!(pd->
context = pa_context_new(pa_threaded_mainloop_get_api(pd->
mainloop), pd->
name))) {
177 }
178
180
184 }
185
186 pa_threaded_mainloop_lock(pd->
mainloop);
187
188 if (pa_threaded_mainloop_start(pd->
mainloop) < 0) {
190 goto unlock_and_fail;
191 }
192
193 for (;;) {
194 pa_context_state_t
state;
195
197
198 if (
state == PA_CONTEXT_READY)
199 break;
200
201 if (!PA_CONTEXT_IS_GOOD(
state)) {
203 goto unlock_and_fail;
204 }
205
206 /* Wait until the context is ready */
207 pa_threaded_mainloop_wait(pd->
mainloop);
208 }
209
212 goto unlock_and_fail;
213 }
214
219
220 ret = pa_stream_connect_record(pd->
stream, device, &attr,
221 PA_STREAM_INTERPOLATE_TIMING
223 |PA_STREAM_AUTO_TIMING_UPDATE);
224
227 goto unlock_and_fail;
228 }
229
230 for (;;) {
231 pa_stream_state_t
state;
232
234
235 if (
state == PA_STREAM_READY)
236 break;
237
238 if (!PA_STREAM_IS_GOOD(
state)) {
240 goto unlock_and_fail;
241 }
242
243 /* Wait until the stream is ready */
244 pa_threaded_mainloop_wait(pd->
mainloop);
245 }
246
247 /* Query actual fragment size */
248 queried_attr = pa_stream_get_buffer_attr(pd->
stream);
249 if (!queried_attr || queried_attr->fragsize > INT_MAX/100) {
251 goto unlock_and_fail;
252 }
255
256 pa_threaded_mainloop_unlock(pd->
mainloop);
257
258 /* take real parameters */
264
267
271 }
272
273 return 0;
274
275 unlock_and_fail:
276 pa_threaded_mainloop_unlock(pd->
mainloop);
277
280 }
281
283 {
286 size_t read_length;
288 int64_t dts;
289 pa_usec_t latency;
290 int negative;
292
293 pa_threaded_mainloop_lock(pd->
mainloop);
294
296
297 while (pos < pd->fragment_size) {
299
302
303 if (read_length <= 0) {
304 pa_threaded_mainloop_wait(pd->
mainloop);
307 /* There's a hole in the stream, skip it. We could generate
308 * silence, but that wouldn't work for compressed streams. */
309 r = pa_stream_drop(pd->
stream);
311 } else {
315 goto unlock_and_fail;
316 }
317
319 pa_operation_unref(pa_stream_update_timing_info(pd->
stream,
NULL,
NULL));
320
321 if (pa_stream_get_latency(pd->
stream, &latency, &negative) >= 0) {
322 if (negative) {
323 dts += latency;
324 } else
325 dts -= latency;
326 } else {
328 }
329 }
332 break;
333 pa_stream_drop(pd->
stream);
334 /* Oversized fragment??? */
336 goto unlock_and_fail;
337 }
340 pa_stream_drop(pd->
stream);
341 }
342 }
343
344 pa_threaded_mainloop_unlock(pd->
mainloop);
345
347
351
352 return 0;
353
354 unlock_and_fail:
356 pa_threaded_mainloop_unlock(pd->
mainloop);
358 }
359
361 {
364 }
365
366 #define OFFSET(a) offsetof(PulseData, a)
367 #define D AV_OPT_FLAG_DECODING_PARAM
368
376 {
"fragment_size",
"set buffering size, affects latency and cpu usage",
OFFSET(fragment_size),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D },
377 {
"wallclock",
"set the initial pts using the current time",
OFFSET(wallclock),
AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1,
D },
379 };
380
387 };
388
399 };