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)
174 int64_t
val = nbytes;
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) {
474 int64_t bytes =
s->buffer_duration;
478 buffer_attributes.tlength =
FFMAX(
s->buffer_size,
av_clip64(bytes, 0, UINT32_MAX - 1));
480 "Buffer duration: %ums recalculated into %"PRId64
" bytes buffer.\n",
481 s->buffer_duration, bytes);
483 }
else if (
s->buffer_size)
484 buffer_attributes.tlength =
s->buffer_size;
486 buffer_attributes.prebuf =
s->prebuf;
488 buffer_attributes.minreq =
s->minreq;
493 if (!pa_sample_spec_valid(&sample_spec)) {
498 if (sample_spec.channels == 1) {
506 if (
channel_map.channels != sample_spec.channels) {
521 s->mainloop = pa_threaded_mainloop_new();
526 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
528 pa_threaded_mainloop_free(
s->mainloop);
533 pa_threaded_mainloop_lock(
s->mainloop);
535 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
542 s->ctx = pa_context_new(mainloop_api,
s->name);
551 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
562 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
580 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
593 buffer_attributes = *pa_stream_get_buffer_attr(
s->stream);
594 s->buffer_size = buffer_attributes.tlength;
595 s->prebuf = buffer_attributes.prebuf;
596 s->minreq = buffer_attributes.minreq;
598 s->buffer_size,
s->prebuf,
s->minreq);
600 pa_threaded_mainloop_unlock(
s->mainloop);
605 pa_threaded_mainloop_lock(
s->mainloop);
611 s->last_volume = PA_VOLUME_INVALID;
612 pa_threaded_mainloop_lock(
s->mainloop);
617 pa_threaded_mainloop_unlock(
s->mainloop);
623 pa_threaded_mainloop_unlock(
s->mainloop);
632 int64_t writable_size;
649 pa_threaded_mainloop_lock(
s->mainloop);
650 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
654 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
655 if (
s->nonblocking) {
656 pa_threaded_mainloop_unlock(
s->mainloop);
659 pa_threaded_mainloop_wait(
s->mainloop);
666 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
669 pa_threaded_mainloop_unlock(
s->mainloop);
673 pa_threaded_mainloop_unlock(
s->mainloop);
689 pkt.
dts = (*frame)->pkt_dts;
690 #if FF_API_PKT_DURATION
692 if ((*frame)->pkt_duration)
707 pa_threaded_mainloop_lock(
s->mainloop);
708 pa_stream_get_latency(
s->stream, &latency, &neg);
709 pa_threaded_mainloop_unlock(
s->mainloop);
713 *dts =
s->timestamp - (neg ? -latency : latency);
723 void *
data,
size_t data_size)
753 s->last_volume = PA_VOLUME_INVALID;
754 pa_threaded_mainloop_lock(
s->mainloop);
756 pa_threaded_mainloop_unlock(
s->mainloop);
760 pa_threaded_mainloop_lock(
s->mainloop);
762 pa_threaded_mainloop_unlock(
s->mainloop);
770 #define OFFSET(a) offsetof(PulseData, a)
771 #define E AV_OPT_FLAG_ENCODING_PARAM
777 {
"buffer_size",
"set buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
778 {
"buffer_duration",
"set buffer duration in millisecs",
OFFSET(buffer_duration),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
805 #if FF_API_ALLOW_FLUSH