22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
58 int eol,
void *userdata)
66 pa_threaded_mainloop_signal(
s->mainloop, 0);
68 if (dev->flags & PA_SINK_FLAT_VOLUME)
69 s->base_volume = dev->base_volume;
71 s->base_volume = PA_VOLUME_NORM;
81 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
86 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
87 pa_threaded_mainloop_wait(
s->mainloop);
88 pa_operation_unref(
op);
93 int eol,
void *userdata)
103 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
104 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
109 vol = pa_sw_volume_divide(vol,
s->base_volume);
110 if (
s->last_volume != vol) {
113 s->last_volume = vol;
124 enum pa_operation_state op_state;
125 pa_mainloop *ml =
NULL;
132 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
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) {
154 uint32_t idx,
void *userdata)
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)
173 int64_t
val = nbytes;
175 if (stream !=
s->stream)
179 pa_threaded_mainloop_signal(
s->mainloop, 0);
198 if (stream !=
s->stream)
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);
213 pa_stream_state_t
state;
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);
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);
242 pa_context_state_t
state;
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);
256 if (stream !=
s->stream)
260 pa_threaded_mainloop_signal(
s->mainloop, 0);
266 pa_threaded_mainloop_unlock(
s->mainloop);
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;
283 pa_threaded_mainloop_lock(
s->mainloop);
291 pa_threaded_mainloop_lock(
s->mainloop);
304 pa_threaded_mainloop_signal(
s->mainloop, 0);
310 pa_threaded_mainloop_unlock(
s->mainloop);
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;
327 pa_threaded_mainloop_lock(
s->mainloop);
328 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
338 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
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),
353 pa_threaded_mainloop_lock(
s->mainloop);
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);
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);
435 pa_threaded_mainloop_unlock(
s->mainloop);
436 pa_threaded_mainloop_stop(
s->mainloop);
437 pa_threaded_mainloop_free(
s->mainloop);
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;
466 stream_name =
h->url;
468 stream_name =
"Playback";
472 if (
s->buffer_duration) {
473 int64_t bytes =
s->buffer_duration;
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;
492 if (!pa_sample_spec_valid(&sample_spec)) {
497 if (sample_spec.channels == 1) {
505 if (
channel_map.channels != sample_spec.channels) {
520 s->mainloop = pa_threaded_mainloop_new();
525 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
527 pa_threaded_mainloop_free(
s->mainloop);
532 pa_threaded_mainloop_lock(
s->mainloop);
534 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
541 s->ctx = pa_context_new(mainloop_api,
s->name);
550 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
561 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
579 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
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);
599 pa_threaded_mainloop_unlock(
s->mainloop);
604 pa_threaded_mainloop_lock(
s->mainloop);
610 s->last_volume = PA_VOLUME_INVALID;
611 pa_threaded_mainloop_lock(
s->mainloop);
616 pa_threaded_mainloop_unlock(
s->mainloop);
622 pa_threaded_mainloop_unlock(
s->mainloop);
631 int64_t writable_size;
648 pa_threaded_mainloop_lock(
s->mainloop);
649 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
653 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
654 if (
s->nonblocking) {
655 pa_threaded_mainloop_unlock(
s->mainloop);
658 pa_threaded_mainloop_wait(
s->mainloop);
665 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
668 pa_threaded_mainloop_unlock(
s->mainloop);
672 pa_threaded_mainloop_unlock(
s->mainloop);
688 pkt.
dts = (*frame)->pkt_dts;
689 #if FF_API_PKT_DURATION
691 if ((*frame)->pkt_duration)
706 pa_threaded_mainloop_lock(
s->mainloop);
707 pa_stream_get_latency(
s->stream, &latency, &neg);
708 pa_threaded_mainloop_unlock(
s->mainloop);
712 *dts =
s->timestamp - (neg ? -latency : latency);
722 void *
data,
size_t data_size)
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);
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 },