22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
59 int eol,
void *userdata)
67 pa_threaded_mainloop_signal(
s->mainloop, 0);
69 if (dev->flags & PA_SINK_FLAT_VOLUME)
70 s->base_volume = dev->base_volume;
72 s->base_volume = PA_VOLUME_NORM;
82 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
87 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
88 pa_threaded_mainloop_wait(
s->mainloop);
89 pa_operation_unref(
op);
94 int eol,
void *userdata)
104 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
105 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
110 vol = pa_sw_volume_divide(vol,
s->base_volume);
111 if (
s->last_volume != vol) {
114 s->last_volume = vol;
125 enum pa_operation_state op_state;
126 pa_mainloop *ml =
NULL;
133 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
139 while ((op_state = pa_operation_get_state(
op)) == PA_OPERATION_RUNNING)
140 pa_mainloop_iterate(ml, 1,
NULL);
141 pa_operation_unref(
op);
142 if (op_state != PA_OPERATION_DONE) {
155 uint32_t idx,
void *userdata)
163 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
164 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
176 if (stream !=
s->stream)
180 pa_threaded_mainloop_signal(
s->mainloop, 0);
199 if (stream !=
s->stream)
202 switch (pa_stream_get_state(
s->stream)) {
203 case PA_STREAM_READY:
204 case PA_STREAM_FAILED:
205 case PA_STREAM_TERMINATED:
206 pa_threaded_mainloop_signal(
s->mainloop, 0);
214 pa_stream_state_t
state;
216 while ((
state = pa_stream_get_state(
s->stream)) != PA_STREAM_READY) {
217 if (
state == PA_STREAM_FAILED ||
state == PA_STREAM_TERMINATED)
219 pa_threaded_mainloop_wait(
s->mainloop);
231 switch (pa_context_get_state(
ctx)) {
232 case PA_CONTEXT_READY:
233 case PA_CONTEXT_FAILED:
234 case PA_CONTEXT_TERMINATED:
235 pa_threaded_mainloop_signal(
s->mainloop, 0);
243 pa_context_state_t
state;
245 while ((
state = pa_context_get_state(
s->ctx)) != PA_CONTEXT_READY) {
246 if (
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED)
248 pa_threaded_mainloop_wait(
s->mainloop);
257 if (stream !=
s->stream)
261 pa_threaded_mainloop_signal(
s->mainloop, 0);
267 pa_threaded_mainloop_unlock(
s->mainloop);
272 while (
s->last_result == 2)
273 pa_threaded_mainloop_wait(
s->mainloop);
274 pa_operation_unref(
op);
275 pa_threaded_mainloop_unlock(
s->mainloop);
276 if (
s->last_result != 0)
278 return s->last_result;
284 pa_threaded_mainloop_lock(
s->mainloop);
292 pa_threaded_mainloop_lock(
s->mainloop);
305 pa_threaded_mainloop_signal(
s->mainloop, 0);
311 pa_threaded_mainloop_unlock(
s->mainloop);
316 while (
s->last_result == 2)
317 pa_threaded_mainloop_wait(
s->mainloop);
318 pa_operation_unref(
op);
319 pa_threaded_mainloop_unlock(
s->mainloop);
320 if (
s->last_result != 0)
322 return s->last_result;
328 pa_threaded_mainloop_lock(
s->mainloop);
329 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
339 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
341 vol = pa_sw_volume_multiply(
lrint(volume * PA_VOLUME_NORM),
s->base_volume);
342 pa_cvolume_set(&cvol,
ss->channels, PA_VOLUME_NORM);
343 pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
344 pa_threaded_mainloop_lock(
s->mainloop);
345 op = pa_context_set_sink_input_volume(
s->ctx, pa_stream_get_index(
s->stream),
354 pa_threaded_mainloop_lock(
s->mainloop);
419 pa_threaded_mainloop_lock(
s->mainloop);
421 pa_stream_disconnect(
s->stream);
422 pa_stream_set_state_callback(
s->stream,
NULL,
NULL);
423 pa_stream_set_write_callback(
s->stream,
NULL,
NULL);
424 pa_stream_set_overflow_callback(
s->stream,
NULL,
NULL);
425 pa_stream_set_underflow_callback(
s->stream,
NULL,
NULL);
426 pa_stream_unref(
s->stream);
430 pa_context_disconnect(
s->ctx);
431 pa_context_set_state_callback(
s->ctx,
NULL,
NULL);
432 pa_context_set_subscribe_callback(
s->ctx,
NULL,
NULL);
433 pa_context_unref(
s->ctx);
436 pa_threaded_mainloop_unlock(
s->mainloop);
437 pa_threaded_mainloop_stop(
s->mainloop);
438 pa_threaded_mainloop_free(
s->mainloop);
450 pa_sample_spec sample_spec;
451 pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
453 pa_mainloop_api *mainloop_api;
454 const char *stream_name =
s->stream_name;
455 static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
456 PA_STREAM_AUTO_TIMING_UPDATE |
457 PA_STREAM_NOT_MONOTONIC;
467 stream_name =
h->url;
469 stream_name =
"Playback";
473 if (
s->buffer_duration) {
479 buffer_attributes.tlength =
FFMAX(
s->buffer_size,
av_clip64(bytes, 0, UINT32_MAX - 1));
481 "Buffer duration: %ums recalculated into %"PRId64
" bytes buffer.\n",
482 s->buffer_duration, bytes);
484 }
else if (
s->buffer_size)
485 buffer_attributes.tlength =
s->buffer_size;
487 buffer_attributes.prebuf =
s->prebuf;
489 buffer_attributes.minreq =
s->minreq;
494 if (!pa_sample_spec_valid(&sample_spec)) {
499 if (sample_spec.channels == 1) {
507 if (
channel_map.channels != sample_spec.channels) {
522 s->mainloop = pa_threaded_mainloop_new();
527 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
529 pa_threaded_mainloop_free(
s->mainloop);
534 pa_threaded_mainloop_lock(
s->mainloop);
536 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
543 s->ctx = pa_context_new(mainloop_api,
s->name);
552 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
563 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
581 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
594 buffer_attributes = *pa_stream_get_buffer_attr(
s->stream);
595 s->buffer_size = buffer_attributes.tlength;
596 s->prebuf = buffer_attributes.prebuf;
597 s->minreq = buffer_attributes.minreq;
599 s->buffer_size,
s->prebuf,
s->minreq);
601 pa_threaded_mainloop_unlock(
s->mainloop);
606 pa_threaded_mainloop_lock(
s->mainloop);
612 s->last_volume = PA_VOLUME_INVALID;
613 pa_threaded_mainloop_lock(
s->mainloop);
618 pa_threaded_mainloop_unlock(
s->mainloop);
624 pa_threaded_mainloop_unlock(
s->mainloop);
650 pa_threaded_mainloop_lock(
s->mainloop);
651 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
655 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
656 if (
s->nonblocking) {
657 pa_threaded_mainloop_unlock(
s->mainloop);
660 pa_threaded_mainloop_wait(
s->mainloop);
667 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
670 pa_threaded_mainloop_unlock(
s->mainloop);
674 pa_threaded_mainloop_unlock(
s->mainloop);
690 pkt.
dts = (*frame)->pkt_dts;
701 pa_threaded_mainloop_lock(
s->mainloop);
702 pa_stream_get_latency(
s->stream, &latency, &neg);
703 pa_threaded_mainloop_unlock(
s->mainloop);
707 *dts =
s->timestamp - (neg ? -latency : latency);
717 void *
data,
size_t data_size)
747 s->last_volume = PA_VOLUME_INVALID;
748 pa_threaded_mainloop_lock(
s->mainloop);
750 pa_threaded_mainloop_unlock(
s->mainloop);
754 pa_threaded_mainloop_lock(
s->mainloop);
756 pa_threaded_mainloop_unlock(
s->mainloop);
764 #define OFFSET(a) offsetof(PulseData, a)
765 #define E AV_OPT_FLAG_ENCODING_PARAM
771 {
"buffer_size",
"set buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
772 {
"buffer_duration",
"set buffer duration in millisecs",
OFFSET(buffer_duration),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
799 #if FF_API_ALLOW_FLUSH