1 /*
2 * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <math.h>
22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
35
56
58 int eol, void *userdata)
59 {
61
63 return;
64
65 if (eol) {
66 pa_threaded_mainloop_signal(
s->mainloop, 0);
67 } else {
68 if (dev->flags & PA_SINK_FLAT_VOLUME)
69 s->base_volume = dev->base_volume;
70 else
71 s->base_volume = PA_VOLUME_NORM;
73 }
74 }
75
76 /* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
78 {
81 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
85 }
86 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
87 pa_threaded_mainloop_wait(
s->mainloop);
88 pa_operation_unref(
op);
89 return 0;
90 }
91
93 int eol, void *userdata)
94 {
97
99 return;
100
101 if (!eol) {
103 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
104 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
107 }
108
109 vol = pa_sw_volume_divide(vol,
s->base_volume);
110 if (
s->last_volume != vol) {
113 s->last_volume = vol;
114 }
115 }
116 }
117
118 /* This function creates new loop so may be called from PA callbacks.
119 Mainloop must be locked before calling this function as it operates on streams. */
121 {
124 enum pa_operation_state op_state;
125 pa_mainloop *ml =
NULL;
128
131
132 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
136 }
137
138 while ((op_state = pa_operation_get_state(
op)) == PA_OPERATION_RUNNING)
139 pa_mainloop_iterate(ml, 1,
NULL);
140 pa_operation_unref(
op);
141 if (op_state != PA_OPERATION_DONE) {
144 }
145
151 }
152
154 uint32_t idx, void *userdata)
155 {
158
160 return;
161
162 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
163 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
164 // Calling from mainloop callback. No need to lock mainloop.
166 }
167 }
168
170 {
173 int64_t
val = nbytes;
174
175 if (stream !=
s->stream)
176 return;
177
179 pa_threaded_mainloop_signal(
s->mainloop, 0);
180 }
181
183 {
186 }
187
189 {
192 }
193
195 {
197
198 if (stream !=
s->stream)
199 return;
200
201 switch (pa_stream_get_state(
s->stream)) {
202 case PA_STREAM_READY:
203 case PA_STREAM_FAILED:
204 case PA_STREAM_TERMINATED:
205 pa_threaded_mainloop_signal(
s->mainloop, 0);
206 default:
207 break;
208 }
209 }
210
212 {
213 pa_stream_state_t
state;
214
215 while ((
state = pa_stream_get_state(
s->stream)) != PA_STREAM_READY) {
216 if (
state == PA_STREAM_FAILED ||
state == PA_STREAM_TERMINATED)
218 pa_threaded_mainloop_wait(
s->mainloop);
219 }
220 return 0;
221 }
222
224 {
226
228 return;
229
230 switch (pa_context_get_state(
ctx)) {
231 case PA_CONTEXT_READY:
232 case PA_CONTEXT_FAILED:
233 case PA_CONTEXT_TERMINATED:
234 pa_threaded_mainloop_signal(
s->mainloop, 0);
235 default:
236 break;
237 }
238 }
239
241 {
242 pa_context_state_t
state;
243
244 while ((
state = pa_context_get_state(
s->ctx)) != PA_CONTEXT_READY) {
245 if (
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED)
247 pa_threaded_mainloop_wait(
s->mainloop);
248 }
249 return 0;
250 }
251
253 {
255
256 if (stream !=
s->stream)
257 return;
258
260 pa_threaded_mainloop_signal(
s->mainloop, 0);
261 }
262
264 {
266 pa_threaded_mainloop_unlock(
s->mainloop);
269 }
271 while (
s->last_result == 2)
272 pa_threaded_mainloop_wait(
s->mainloop);
273 pa_operation_unref(
op);
274 pa_threaded_mainloop_unlock(
s->mainloop);
275 if (
s->last_result != 0)
277 return s->last_result;
278 }
279
281 {
283 pa_threaded_mainloop_lock(
s->mainloop);
286 }
287
289 {
291 pa_threaded_mainloop_lock(
s->mainloop);
294 }
295
297 {
299
301 return;
302
304 pa_threaded_mainloop_signal(
s->mainloop, 0);
305 }
306
308 {
310 pa_threaded_mainloop_unlock(
s->mainloop);
313 }
315 while (
s->last_result == 2)
316 pa_threaded_mainloop_wait(
s->mainloop);
317 pa_operation_unref(
op);
318 pa_threaded_mainloop_unlock(
s->mainloop);
319 if (
s->last_result != 0)
321 return s->last_result;
322 }
323
325 {
327 pa_threaded_mainloop_lock(
s->mainloop);
328 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
331 }
332
334 {
336 pa_cvolume cvol;
337 pa_volume_t vol;
338 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
339
340 vol = pa_sw_volume_multiply(
lrint(volume * PA_VOLUME_NORM),
s->base_volume);
341 pa_cvolume_set(&cvol,
ss->channels, PA_VOLUME_NORM);
342 pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
343 pa_threaded_mainloop_lock(
s->mainloop);
344 op = pa_context_set_sink_input_volume(
s->ctx, pa_stream_get_index(
s->stream),
347 }
348
350 {
352
353 pa_threaded_mainloop_lock(
s->mainloop);
356 }
357
359 {
411 }
412
414 {
416
418 pa_threaded_mainloop_lock(
s->mainloop);
420 pa_stream_disconnect(
s->stream);
421 pa_stream_set_state_callback(
s->stream,
NULL,
NULL);
422 pa_stream_set_write_callback(
s->stream,
NULL,
NULL);
423 pa_stream_set_overflow_callback(
s->stream,
NULL,
NULL);
424 pa_stream_set_underflow_callback(
s->stream,
NULL,
NULL);
425 pa_stream_unref(
s->stream);
427 }
429 pa_context_disconnect(
s->ctx);
430 pa_context_set_state_callback(
s->ctx,
NULL,
NULL);
431 pa_context_set_subscribe_callback(
s->ctx,
NULL,
NULL);
432 pa_context_unref(
s->ctx);
434 }
435 pa_threaded_mainloop_unlock(
s->mainloop);
436 pa_threaded_mainloop_stop(
s->mainloop);
437 pa_threaded_mainloop_free(
s->mainloop);
439 }
440
441 return 0;
442 }
443
445 {
449 pa_sample_spec sample_spec;
450 pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
452 pa_mainloop_api *mainloop_api;
453 const char *stream_name =
s->stream_name;
454 static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
455 PA_STREAM_AUTO_TIMING_UPDATE |
456 PA_STREAM_NOT_MONOTONIC;
457
461 }
463
464 if (!stream_name) {
466 stream_name =
h->url;
467 else
468 stream_name = "Playback";
469 }
471
472 if (
s->buffer_duration) {
473 int64_t bytes =
s->buffer_duration;
476 bytes /= 1000;
477 buffer_attributes.tlength =
FFMAX(
s->buffer_size,
av_clip64(bytes, 0, UINT32_MAX - 1));
479 "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
480 s->buffer_duration, bytes);
482 }
else if (
s->buffer_size)
483 buffer_attributes.tlength =
s->buffer_size;
485 buffer_attributes.prebuf =
s->prebuf;
487 buffer_attributes.minreq =
s->minreq;
488
492 if (!pa_sample_spec_valid(&sample_spec)) {
495 }
496
497 if (sample_spec.channels == 1) {
504 /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
505 if (
channel_map.channels != sample_spec.channels) {
508 }
509 } else
511
517 }
518
519 /* start main loop */
520 s->mainloop = pa_threaded_mainloop_new();
524 }
525 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
527 pa_threaded_mainloop_free(
s->mainloop);
530 }
531
532 pa_threaded_mainloop_lock(
s->mainloop);
533
534 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
535 if (!mainloop_api) {
539 }
540
541 s->ctx = pa_context_new(mainloop_api,
s->name);
546 }
549
550 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
554 }
555
559 }
560
561 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
563
567 }
568
573 }
578
579 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
584 }
585
589 }
590
591 /* read back buffer attributes for future use */
592 buffer_attributes = *pa_stream_get_buffer_attr(
s->stream);
593 s->buffer_size = buffer_attributes.tlength;
594 s->prebuf = buffer_attributes.prebuf;
595 s->minreq = buffer_attributes.minreq;
597 s->buffer_size,
s->prebuf,
s->minreq);
598
599 pa_threaded_mainloop_unlock(
s->mainloop);
600
603 /* a bit ugly but the simplest to lock here*/
604 pa_threaded_mainloop_lock(
s->mainloop);
606 }
607
608 /* force control messages */
610 s->last_volume = PA_VOLUME_INVALID;
611 pa_threaded_mainloop_lock(
s->mainloop);
615 }
616 pa_threaded_mainloop_unlock(
s->mainloop);
617
619
620 return 0;
622 pa_threaded_mainloop_unlock(
s->mainloop);
625 }
626
628 {
631 int64_t writable_size;
632
635
638
641 } else {
646 }
647
648 pa_threaded_mainloop_lock(
s->mainloop);
649 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
652 }
653 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
654 if (
s->nonblocking) {
655 pa_threaded_mainloop_unlock(
s->mainloop);
657 } else
658 pa_threaded_mainloop_wait(
s->mainloop);
659 }
660
664 }
665 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
667
668 pa_threaded_mainloop_unlock(
s->mainloop);
669
670 return 0;
672 pa_threaded_mainloop_unlock(
s->mainloop);
674 }
675
678 {
680
681 /* Planar formats are not supported yet. */
685
688 pkt.
dts = (*frame)->pkt_dts;
689 #if FF_API_PKT_DURATION
691 if ((*frame)->pkt_duration)
693 else
695 #endif
698 }
699
700
702 {
704 pa_usec_t latency;
705 int neg;
706 pa_threaded_mainloop_lock(
s->mainloop);
707 pa_stream_get_latency(
s->stream, &latency, &neg);
708 pa_threaded_mainloop_unlock(
s->mainloop);
709 if (wall)
711 if (dts)
712 *dts =
s->timestamp - (neg ? -latency : latency);
713 }
714
716 {
719 }
720
722 void *
data,
size_t data_size)
723 {
726
738 }
739 return 0;
744 }
745 return 0;
752 s->last_volume = PA_VOLUME_INVALID;
753 pa_threaded_mainloop_lock(
s->mainloop);
755 pa_threaded_mainloop_unlock(
s->mainloop);
759 pa_threaded_mainloop_lock(
s->mainloop);
761 pa_threaded_mainloop_unlock(
s->mainloop);
763 default:
764 break;
765 }
767 }
768
769 #define OFFSET(a) offsetof(PulseData, a)
770 #define E AV_OPT_FLAG_ENCODING_PARAM
776 {
"buffer_size",
"set buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
777 {
"buffer_duration",
"set buffer duration in millisecs",
OFFSET(buffer_duration),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
781 };
782
789 };
790
806 };