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>
32
53
55 int eol, void *userdata)
56 {
58
60 return;
61
62 if (eol) {
63 pa_threaded_mainloop_signal(
s->mainloop, 0);
64 } else {
65 if (dev->flags & PA_SINK_FLAT_VOLUME)
66 s->base_volume = dev->base_volume;
67 else
68 s->base_volume = PA_VOLUME_NORM;
70 }
71 }
72
73 /* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
75 {
78 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
82 }
83 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
84 pa_threaded_mainloop_wait(
s->mainloop);
85 pa_operation_unref(
op);
86 return 0;
87 }
88
90 int eol, void *userdata)
91 {
94
96 return;
97
98 if (!eol) {
100 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
101 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
104 }
105
106 vol = pa_sw_volume_divide(vol,
s->base_volume);
107 if (
s->last_volume != vol) {
108 val = (double)vol / PA_VOLUME_NORM;
110 s->last_volume = vol;
111 }
112 }
113 }
114
115 /* This function creates new loop so may be called from PA callbacks.
116 Mainloop must be locked before calling this function as it operates on streams. */
118 {
121 enum pa_operation_state op_state;
122 pa_mainloop *ml =
NULL;
125
128
129 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
133 }
134
135 while ((op_state = pa_operation_get_state(
op)) == PA_OPERATION_RUNNING)
136 pa_mainloop_iterate(ml, 1,
NULL);
137 pa_operation_unref(
op);
138 if (op_state != PA_OPERATION_DONE) {
141 }
142
148 }
149
151 uint32_t idx, void *userdata)
152 {
155
157 return;
158
159 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
160 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
161 // Calling from mainloop callback. No need to lock mainloop.
163 }
164 }
165
167 {
170 int64_t
val = nbytes;
171
172 if (stream !=
s->stream)
173 return;
174
176 pa_threaded_mainloop_signal(
s->mainloop, 0);
177 }
178
180 {
183 }
184
186 {
189 }
190
192 {
194
195 if (stream !=
s->stream)
196 return;
197
198 switch (pa_stream_get_state(
s->stream)) {
199 case PA_STREAM_READY:
200 case PA_STREAM_FAILED:
201 case PA_STREAM_TERMINATED:
202 pa_threaded_mainloop_signal(
s->mainloop, 0);
203 default:
204 break;
205 }
206 }
207
209 {
210 pa_stream_state_t
state;
211
212 while ((
state = pa_stream_get_state(
s->stream)) != PA_STREAM_READY) {
213 if (
state == PA_STREAM_FAILED ||
state == PA_STREAM_TERMINATED)
215 pa_threaded_mainloop_wait(
s->mainloop);
216 }
217 return 0;
218 }
219
221 {
223
225 return;
226
227 switch (pa_context_get_state(
ctx)) {
228 case PA_CONTEXT_READY:
229 case PA_CONTEXT_FAILED:
230 case PA_CONTEXT_TERMINATED:
231 pa_threaded_mainloop_signal(
s->mainloop, 0);
232 default:
233 break;
234 }
235 }
236
238 {
239 pa_context_state_t
state;
240
241 while ((
state = pa_context_get_state(
s->ctx)) != PA_CONTEXT_READY) {
242 if (
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED)
244 pa_threaded_mainloop_wait(
s->mainloop);
245 }
246 return 0;
247 }
248
250 {
252
253 if (stream !=
s->stream)
254 return;
255
257 pa_threaded_mainloop_signal(
s->mainloop, 0);
258 }
259
261 {
263 pa_threaded_mainloop_unlock(
s->mainloop);
266 }
268 while (
s->last_result == 2)
269 pa_threaded_mainloop_wait(
s->mainloop);
270 pa_operation_unref(
op);
271 pa_threaded_mainloop_unlock(
s->mainloop);
272 if (
s->last_result != 0)
274 return s->last_result;
275 }
276
278 {
280 pa_threaded_mainloop_lock(
s->mainloop);
283 }
284
286 {
288 pa_threaded_mainloop_lock(
s->mainloop);
291 }
292
294 {
296
298 return;
299
301 pa_threaded_mainloop_signal(
s->mainloop, 0);
302 }
303
305 {
307 pa_threaded_mainloop_unlock(
s->mainloop);
310 }
312 while (
s->last_result == 2)
313 pa_threaded_mainloop_wait(
s->mainloop);
314 pa_operation_unref(
op);
315 pa_threaded_mainloop_unlock(
s->mainloop);
316 if (
s->last_result != 0)
318 return s->last_result;
319 }
320
322 {
324 pa_threaded_mainloop_lock(
s->mainloop);
325 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
328 }
329
331 {
333 pa_cvolume cvol;
334 pa_volume_t vol;
335 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
336
337 vol = pa_sw_volume_multiply(
lrint(volume * PA_VOLUME_NORM),
s->base_volume);
338 pa_cvolume_set(&cvol,
ss->channels, PA_VOLUME_NORM);
339 pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
340 pa_threaded_mainloop_lock(
s->mainloop);
341 op = pa_context_set_sink_input_volume(
s->ctx, pa_stream_get_index(
s->stream),
344 }
345
347 {
349
350 pa_threaded_mainloop_lock(
s->mainloop);
353 }
354
356 {
357 channel_map->channels = 0;
359 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
361 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
363 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
365 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
367 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
369 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
371 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
373 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
375 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
377 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
379 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
381 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
383 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
385 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
387 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
389 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
391 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
393 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
395 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
397 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
399 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0;
401 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1;
403 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2;
405 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3;
407 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
408 }
409
411 {
413
415 pa_threaded_mainloop_lock(
s->mainloop);
417 pa_stream_disconnect(
s->stream);
418 pa_stream_set_state_callback(
s->stream,
NULL,
NULL);
419 pa_stream_set_write_callback(
s->stream,
NULL,
NULL);
420 pa_stream_set_overflow_callback(
s->stream,
NULL,
NULL);
421 pa_stream_set_underflow_callback(
s->stream,
NULL,
NULL);
422 pa_stream_unref(
s->stream);
424 }
426 pa_context_disconnect(
s->ctx);
427 pa_context_set_state_callback(
s->ctx,
NULL,
NULL);
428 pa_context_set_subscribe_callback(
s->ctx,
NULL,
NULL);
429 pa_context_unref(
s->ctx);
431 }
432 pa_threaded_mainloop_unlock(
s->mainloop);
433 pa_threaded_mainloop_stop(
s->mainloop);
434 pa_threaded_mainloop_free(
s->mainloop);
436 }
437
438 return 0;
439 }
440
442 {
446 pa_sample_spec sample_spec;
447 pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
448 pa_channel_map channel_map;
449 pa_mainloop_api *mainloop_api;
450 const char *stream_name =
s->stream_name;
451 static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
452 PA_STREAM_AUTO_TIMING_UPDATE |
453 PA_STREAM_NOT_MONOTONIC;
454
458 }
460
461 if (!stream_name) {
463 stream_name =
h->url;
464 else
465 stream_name = "Playback";
466 }
468
469 if (
s->buffer_duration) {
470 int64_t bytes =
s->buffer_duration;
473 bytes /= 1000;
474 buffer_attributes.tlength =
FFMAX(
s->buffer_size,
av_clip64(bytes, 0, UINT32_MAX - 1));
476 "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
477 s->buffer_duration, bytes);
479 }
else if (
s->buffer_size)
480 buffer_attributes.tlength =
s->buffer_size;
482 buffer_attributes.prebuf =
s->prebuf;
484 buffer_attributes.minreq =
s->minreq;
485
489 if (!pa_sample_spec_valid(&sample_spec)) {
492 }
493
494 if (sample_spec.channels == 1) {
495 channel_map.channels = 1;
496 channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
501 /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
502 if (channel_map.channels != sample_spec.channels) {
504 channel_map.channels = 0;
505 }
506 } else
507 channel_map.channels = 0;
508
509 if (!channel_map.channels)
511 else if (!pa_channel_map_valid(&channel_map)) {
514 }
515
516 /* start main loop */
517 s->mainloop = pa_threaded_mainloop_new();
521 }
522 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
524 pa_threaded_mainloop_free(
s->mainloop);
527 }
528
529 pa_threaded_mainloop_lock(
s->mainloop);
530
531 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
532 if (!mainloop_api) {
536 }
537
538 s->ctx = pa_context_new(mainloop_api,
s->name);
543 }
546
547 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
551 }
552
556 }
557
558 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
559 channel_map.channels ? &channel_map :
NULL);
560
564 }
565
570 }
575
576 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
581 }
582
586 }
587
588 /* read back buffer attributes for future use */
589 buffer_attributes = *pa_stream_get_buffer_attr(
s->stream);
590 s->buffer_size = buffer_attributes.tlength;
591 s->prebuf = buffer_attributes.prebuf;
592 s->minreq = buffer_attributes.minreq;
594 s->buffer_size,
s->prebuf,
s->minreq);
595
596 pa_threaded_mainloop_unlock(
s->mainloop);
597
600 /* a bit ugly but the simplest to lock here*/
601 pa_threaded_mainloop_lock(
s->mainloop);
603 }
604
605 /* force control messages */
607 s->last_volume = PA_VOLUME_INVALID;
608 pa_threaded_mainloop_lock(
s->mainloop);
612 }
613 pa_threaded_mainloop_unlock(
s->mainloop);
614
616
617 return 0;
619 pa_threaded_mainloop_unlock(
s->mainloop);
622 }
623
625 {
628 int64_t writable_size;
629
632
635
638 } else {
643 }
644
645 pa_threaded_mainloop_lock(
s->mainloop);
646 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
649 }
650 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
651 if (
s->nonblocking) {
652 pa_threaded_mainloop_unlock(
s->mainloop);
654 } else
655 pa_threaded_mainloop_wait(
s->mainloop);
656 }
657
661 }
662 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
664
665 pa_threaded_mainloop_unlock(
s->mainloop);
666
667 return 0;
669 pa_threaded_mainloop_unlock(
s->mainloop);
671 }
672
675 {
677
678 /* Planar formats are not supported yet. */
682
685 pkt.
dts = (*frame)->pkt_dts;
688 }
689
690
692 {
694 pa_usec_t latency;
695 int neg;
696 pa_threaded_mainloop_lock(
s->mainloop);
697 pa_stream_get_latency(
s->stream, &latency, &neg);
698 pa_threaded_mainloop_unlock(
s->mainloop);
699 if (wall)
701 if (dts)
702 *dts =
s->timestamp - (neg ? -latency : latency);
703 }
704
706 {
709 }
710
712 void *
data,
size_t data_size)
713 {
716
728 }
729 return 0;
734 }
735 return 0;
742 s->last_volume = PA_VOLUME_INVALID;
743 pa_threaded_mainloop_lock(
s->mainloop);
745 pa_threaded_mainloop_unlock(
s->mainloop);
749 pa_threaded_mainloop_lock(
s->mainloop);
751 pa_threaded_mainloop_unlock(
s->mainloop);
753 default:
754 break;
755 }
757 }
758
759 #define OFFSET(a) offsetof(PulseData, a)
760 #define E AV_OPT_FLAG_ENCODING_PARAM
766 {
"buffer_size",
"set buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
767 {
"buffer_duration",
"set buffer duration in millisecs",
OFFSET(buffer_duration),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
771 };
772
779 };
780
796 };