22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
55 int eol,
void *userdata)
63 pa_threaded_mainloop_signal(
s->mainloop, 0);
65 if (dev->flags & PA_SINK_FLAT_VOLUME)
66 s->base_volume = dev->base_volume;
68 s->base_volume = PA_VOLUME_NORM;
78 if (!(
op = pa_context_get_sink_info_by_name(
s->ctx,
s->device,
83 while (pa_operation_get_state(
op) == PA_OPERATION_RUNNING)
84 pa_threaded_mainloop_wait(
s->mainloop);
85 pa_operation_unref(
op);
90 int eol,
void *userdata)
100 pa_volume_t vol = pa_cvolume_avg(&
i->volume);
101 if (
s->mute < 0 || (
s->mute && !
i->mute) || (!
s->mute &&
i->mute)) {
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;
121 enum pa_operation_state op_state;
122 pa_mainloop *ml =
NULL;
129 if (!(
op = pa_context_get_sink_input_info(
ctx, pa_stream_get_index(
s->stream),
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) {
151 uint32_t idx,
void *userdata)
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)
170 int64_t
val = nbytes;
172 if (stream !=
s->stream)
176 pa_threaded_mainloop_signal(
s->mainloop, 0);
195 if (stream !=
s->stream)
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);
210 pa_stream_state_t
state;
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);
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);
239 pa_context_state_t
state;
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);
253 if (stream !=
s->stream)
257 pa_threaded_mainloop_signal(
s->mainloop, 0);
263 pa_threaded_mainloop_unlock(
s->mainloop);
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;
280 pa_threaded_mainloop_lock(
s->mainloop);
288 pa_threaded_mainloop_lock(
s->mainloop);
301 pa_threaded_mainloop_signal(
s->mainloop, 0);
307 pa_threaded_mainloop_unlock(
s->mainloop);
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;
324 pa_threaded_mainloop_lock(
s->mainloop);
325 op = pa_context_set_sink_input_mute(
s->ctx, pa_stream_get_index(
s->stream),
335 const pa_sample_spec *
ss = pa_stream_get_sample_spec(
s->stream);
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),
350 pa_threaded_mainloop_lock(
s->mainloop);
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;
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);
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);
432 pa_threaded_mainloop_unlock(
s->mainloop);
433 pa_threaded_mainloop_stop(
s->mainloop);
434 pa_threaded_mainloop_free(
s->mainloop);
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;
463 stream_name =
h->url;
465 stream_name =
"Playback";
469 if (
s->buffer_duration) {
470 int64_t bytes =
s->buffer_duration;
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;
489 if (!pa_sample_spec_valid(&sample_spec)) {
494 if (sample_spec.channels == 1) {
495 channel_map.channels = 1;
496 channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
502 if (channel_map.channels != sample_spec.channels) {
504 channel_map.channels = 0;
507 channel_map.channels = 0;
509 if (!channel_map.channels)
511 else if (!pa_channel_map_valid(&channel_map)) {
517 s->mainloop = pa_threaded_mainloop_new();
522 if ((
ret = pa_threaded_mainloop_start(
s->mainloop)) < 0) {
524 pa_threaded_mainloop_free(
s->mainloop);
529 pa_threaded_mainloop_lock(
s->mainloop);
531 mainloop_api = pa_threaded_mainloop_get_api(
s->mainloop);
538 s->ctx = pa_context_new(mainloop_api,
s->name);
547 if ((
ret = pa_context_connect(
s->ctx,
s->server, 0,
NULL)) < 0) {
558 s->stream = pa_stream_new(
s->ctx, stream_name, &sample_spec,
559 channel_map.channels ? &channel_map :
NULL);
576 if ((
ret = pa_stream_connect_playback(
s->stream,
s->device, &buffer_attributes,
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);
596 pa_threaded_mainloop_unlock(
s->mainloop);
601 pa_threaded_mainloop_lock(
s->mainloop);
607 s->last_volume = PA_VOLUME_INVALID;
608 pa_threaded_mainloop_lock(
s->mainloop);
613 pa_threaded_mainloop_unlock(
s->mainloop);
619 pa_threaded_mainloop_unlock(
s->mainloop);
628 int64_t writable_size;
645 pa_threaded_mainloop_lock(
s->mainloop);
646 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(
s->stream))) {
650 while (pa_stream_writable_size(
s->stream) <
s->minreq) {
651 if (
s->nonblocking) {
652 pa_threaded_mainloop_unlock(
s->mainloop);
655 pa_threaded_mainloop_wait(
s->mainloop);
662 if ((writable_size = pa_stream_writable_size(
s->stream)) >=
s->minreq)
665 pa_threaded_mainloop_unlock(
s->mainloop);
669 pa_threaded_mainloop_unlock(
s->mainloop);
685 pkt.
dts = (*frame)->pkt_dts;
696 pa_threaded_mainloop_lock(
s->mainloop);
697 pa_stream_get_latency(
s->stream, &latency, &neg);
698 pa_threaded_mainloop_unlock(
s->mainloop);
702 *dts =
s->timestamp - (neg ? -latency : latency);
712 void *
data,
size_t data_size)
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);
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 },