2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
34 #include <pulse/timeval.h>
35 #include <pulse/util.h>
36 #include <pulse/i18n.h>
37 #include <pulse/rtclock.h>
38 #include <pulse/internal.h>
40 #include <pulsecore/sink-input.h>
41 #include <pulsecore/namereg.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/sample-util.h>
44 #include <pulsecore/core-subscribe.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/play-memblockq.h>
48 #include <pulsecore/flist.h>
52 #define MAX_MIX_CHANNELS 32
53 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
54 #define ABSOLUTE_MIN_LATENCY (500)
55 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
56 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
58 PA_DEFINE_PUBLIC_CLASS(pa_sink
, pa_msgobject
);
60 struct pa_sink_volume_change
{
64 PA_LLIST_FIELDS(pa_sink_volume_change
);
67 struct sink_message_set_port
{
72 static void sink_free(pa_object
*s
);
74 static void pa_sink_volume_change_push(pa_sink
*s
);
75 static void pa_sink_volume_change_flush(pa_sink
*s
);
76 static void pa_sink_volume_change_rewind(pa_sink
*s
, size_t nbytes
);
78 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
82 data
->proplist
= pa_proplist_new();
87 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
91 data
->name
= pa_xstrdup(name
);
94 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
97 if ((data
->sample_spec_is_set
= !!spec
))
98 data
->sample_spec
= *spec
;
101 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
104 if ((data
->channel_map_is_set
= !!map
))
105 data
->channel_map
= *map
;
108 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
111 if ((data
->volume_is_set
= !!volume
))
112 data
->volume
= *volume
;
115 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
118 data
->muted_is_set
= TRUE
;
119 data
->muted
= !!mute
;
122 void pa_sink_new_data_set_port(pa_sink_new_data
*data
, const char *port
) {
125 pa_xfree(data
->active_port
);
126 data
->active_port
= pa_xstrdup(port
);
129 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
132 pa_proplist_free(data
->proplist
);
137 while ((p
= pa_hashmap_steal_first(data
->ports
)))
138 pa_device_port_free(p
);
140 pa_hashmap_free(data
->ports
, NULL
, NULL
);
143 pa_xfree(data
->name
);
144 pa_xfree(data
->active_port
);
147 pa_device_port
*pa_device_port_new(const char *name
, const char *description
, size_t extra
) {
152 p
= pa_xmalloc(PA_ALIGN(sizeof(pa_device_port
)) + extra
);
153 p
->name
= pa_xstrdup(name
);
154 p
->description
= pa_xstrdup(description
);
161 void pa_device_port_free(pa_device_port
*p
) {
165 pa_xfree(p
->description
);
169 /* Called from main context */
170 static void reset_callbacks(pa_sink
*s
) {
174 s
->get_volume
= NULL
;
175 s
->set_volume
= NULL
;
178 s
->request_rewind
= NULL
;
179 s
->update_requested_latency
= NULL
;
181 s
->get_formats
= NULL
;
184 /* Called from main context */
185 pa_sink
* pa_sink_new(
187 pa_sink_new_data
*data
,
188 pa_sink_flags_t flags
) {
192 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
193 pa_source_new_data source_data
;
199 pa_assert(data
->name
);
200 pa_assert_ctl_context();
202 s
= pa_msgobject_new(pa_sink
);
204 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
205 pa_log_debug("Failed to register name %s.", data
->name
);
210 pa_sink_new_data_set_name(data
, name
);
212 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
214 pa_namereg_unregister(core
, name
);
218 /* FIXME, need to free s here on failure */
220 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
221 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
223 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
225 if (!data
->channel_map_is_set
)
226 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
228 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
229 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
231 /* FIXME: There should probably be a general function for checking whether
232 * the sink volume is allowed to be set, like there is for sink inputs. */
233 pa_assert(!data
->volume_is_set
|| !(flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
235 if (!data
->volume_is_set
) {
236 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
237 data
->save_volume
= FALSE
;
240 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
241 pa_return_null_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
));
243 if (!data
->muted_is_set
)
247 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
249 pa_device_init_description(data
->proplist
);
250 pa_device_init_icon(data
->proplist
, TRUE
);
251 pa_device_init_intended_roles(data
->proplist
);
253 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
255 pa_namereg_unregister(core
, name
);
259 s
->parent
.parent
.free
= sink_free
;
260 s
->parent
.process_msg
= pa_sink_process_msg
;
263 s
->state
= PA_SINK_INIT
;
266 s
->suspend_cause
= 0;
267 s
->name
= pa_xstrdup(name
);
268 s
->proplist
= pa_proplist_copy(data
->proplist
);
269 s
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
270 s
->module
= data
->module
;
271 s
->card
= data
->card
;
273 s
->priority
= pa_device_init_priority(s
->proplist
);
275 s
->sample_spec
= data
->sample_spec
;
276 s
->channel_map
= data
->channel_map
;
278 s
->inputs
= pa_idxset_new(NULL
, NULL
);
280 s
->input_to_master
= NULL
;
282 s
->reference_volume
= s
->real_volume
= data
->volume
;
283 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
284 s
->base_volume
= PA_VOLUME_NORM
;
285 s
->n_volume_steps
= PA_VOLUME_NORM
+1;
286 s
->muted
= data
->muted
;
287 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
294 /* As a minor optimization we just steal the list instead of
296 s
->ports
= data
->ports
;
299 s
->active_port
= NULL
;
300 s
->save_port
= FALSE
;
302 if (data
->active_port
&& s
->ports
)
303 if ((s
->active_port
= pa_hashmap_get(s
->ports
, data
->active_port
)))
304 s
->save_port
= data
->save_port
;
306 if (!s
->active_port
&& s
->ports
) {
310 PA_HASHMAP_FOREACH(p
, s
->ports
, state
)
311 if (!s
->active_port
|| p
->priority
> s
->active_port
->priority
)
315 s
->save_volume
= data
->save_volume
;
316 s
->save_muted
= data
->save_muted
;
318 pa_silence_memchunk_get(
319 &core
->silence_cache
,
325 s
->thread_info
.rtpoll
= NULL
;
326 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
327 s
->thread_info
.soft_volume
= s
->soft_volume
;
328 s
->thread_info
.soft_muted
= s
->muted
;
329 s
->thread_info
.state
= s
->state
;
330 s
->thread_info
.rewind_nbytes
= 0;
331 s
->thread_info
.rewind_requested
= FALSE
;
332 s
->thread_info
.max_rewind
= 0;
333 s
->thread_info
.max_request
= 0;
334 s
->thread_info
.requested_latency_valid
= FALSE
;
335 s
->thread_info
.requested_latency
= 0;
336 s
->thread_info
.min_latency
= ABSOLUTE_MIN_LATENCY
;
337 s
->thread_info
.max_latency
= ABSOLUTE_MAX_LATENCY
;
338 s
->thread_info
.fixed_latency
= flags
& PA_SINK_DYNAMIC_LATENCY
? 0 : DEFAULT_FIXED_LATENCY
;
340 PA_LLIST_HEAD_INIT(pa_sink_volume_change
, s
->thread_info
.volume_changes
);
341 s
->thread_info
.volume_changes_tail
= NULL
;
342 pa_sw_cvolume_multiply(&s
->thread_info
.current_hw_volume
, &s
->soft_volume
, &s
->real_volume
);
343 s
->thread_info
.volume_change_safety_margin
= core
->sync_volume_safety_margin_usec
;
344 s
->thread_info
.volume_change_extra_delay
= core
->sync_volume_extra_delay_usec
;
346 /* FIXME: This should probably be moved to pa_sink_put() */
347 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
350 pa_assert_se(pa_idxset_put(s
->card
->sinks
, s
, NULL
) >= 0);
352 pt
= pa_proplist_to_string_sep(s
->proplist
, "\n ");
353 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
356 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
357 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
),
361 pa_source_new_data_init(&source_data
);
362 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
363 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
364 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
365 source_data
.driver
= data
->driver
;
366 source_data
.module
= data
->module
;
367 source_data
.card
= data
->card
;
369 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
370 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
371 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
373 s
->monitor_source
= pa_source_new(core
, &source_data
,
374 ((flags
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
375 ((flags
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0));
377 pa_source_new_data_done(&source_data
);
379 if (!s
->monitor_source
) {
385 s
->monitor_source
->monitor_of
= s
;
387 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
388 pa_source_set_fixed_latency(s
->monitor_source
, s
->thread_info
.fixed_latency
);
389 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
394 /* Called from main context */
395 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
397 pa_bool_t suspend_change
;
398 pa_sink_state_t original_state
;
401 pa_assert_ctl_context();
403 if (s
->state
== state
)
406 original_state
= s
->state
;
409 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
410 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
413 if ((ret
= s
->set_state(s
, state
)) < 0)
417 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
420 s
->set_state(s
, original_state
);
427 if (state
!= PA_SINK_UNLINKED
) { /* if we enter UNLINKED state pa_sink_unlink() will fire the apropriate events */
428 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
429 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
432 if (suspend_change
) {
436 /* We're suspending or resuming, tell everyone about it */
438 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
)
439 if (s
->state
== PA_SINK_SUSPENDED
&&
440 (i
->flags
& PA_SINK_INPUT_KILL_ON_SUSPEND
))
441 pa_sink_input_kill(i
);
443 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
445 if (s
->monitor_source
)
446 pa_source_sync_suspend(s
->monitor_source
);
452 void pa_sink_set_get_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
458 void pa_sink_set_set_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
461 pa_assert(!s
->write_volume
|| cb
);
466 s
->flags
|= PA_SINK_HW_VOLUME_CTRL
;
468 s
->flags
&= ~PA_SINK_HW_VOLUME_CTRL
;
471 void pa_sink_set_write_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
474 pa_assert(!cb
|| s
->set_volume
);
476 s
->write_volume
= cb
;
479 s
->flags
|= PA_SINK_SYNC_VOLUME
;
481 s
->flags
&= ~PA_SINK_SYNC_VOLUME
;
484 void pa_sink_set_get_mute_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
490 void pa_sink_set_set_mute_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
496 s
->flags
|= PA_SINK_HW_MUTE_CTRL
;
498 s
->flags
&= ~PA_SINK_HW_MUTE_CTRL
;
501 /* Called from main context */
502 void pa_sink_put(pa_sink
* s
) {
503 pa_sink_assert_ref(s
);
504 pa_assert_ctl_context();
506 pa_assert(s
->state
== PA_SINK_INIT
);
507 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) || s
->input_to_master
);
509 /* The following fields must be initialized properly when calling _put() */
510 pa_assert(s
->asyncmsgq
);
511 pa_assert(s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
513 /* Generally, flags should be initialized via pa_sink_new(). As a
514 * special exception we allow some volume related flags to be set
515 * between _new() and _put() by the callback setter functions above.
517 * Thus we implement a couple safeguards here which ensure the above
518 * setters were used (or at least the implementor made manual changes
519 * in a compatible way).
521 * Note: All of these flags set here can change over the life time
523 pa_assert(!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
) || s
->set_volume
);
524 pa_assert(!(s
->flags
& PA_SINK_SYNC_VOLUME
) || s
->write_volume
);
525 pa_assert(!(s
->flags
& PA_SINK_HW_MUTE_CTRL
) || s
->set_mute
);
527 /* XXX: Currently decibel volume is disabled for all sinks that use volume
528 * sharing. When the master sink supports decibel volume, it would be good
529 * to have the flag also in the filter sink, but currently we don't do that
530 * so that the flags of the filter sink never change when it's moved from
531 * a master sink to another. One solution for this problem would be to
532 * remove user-visible volume altogether from filter sinks when volume
533 * sharing is used, but the current approach was easier to implement... */
534 /* We always support decibel volumes in software, otherwise we leave it to
535 * the sink implementor to set this flag as needed.
537 * Note: This flag can also change over the life time of the sink. */
538 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
) && !(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
539 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
541 if ((s
->flags
& PA_SINK_DECIBEL_VOLUME
) && s
->core
->flat_volumes
)
542 s
->flags
|= PA_SINK_FLAT_VOLUME
;
544 if (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) {
545 pa_sink
*root_sink
= s
->input_to_master
->sink
;
547 while (root_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)
548 root_sink
= root_sink
->input_to_master
->sink
;
550 s
->reference_volume
= root_sink
->reference_volume
;
551 pa_cvolume_remap(&s
->reference_volume
, &root_sink
->channel_map
, &s
->channel_map
);
553 s
->real_volume
= root_sink
->real_volume
;
554 pa_cvolume_remap(&s
->real_volume
, &root_sink
->channel_map
, &s
->channel_map
);
556 /* We assume that if the sink implementor changed the default
557 * volume he did so in real_volume, because that is the usual
558 * place where he is supposed to place his changes. */
559 s
->reference_volume
= s
->real_volume
;
561 s
->thread_info
.soft_volume
= s
->soft_volume
;
562 s
->thread_info
.soft_muted
= s
->muted
;
563 pa_sw_cvolume_multiply(&s
->thread_info
.current_hw_volume
, &s
->soft_volume
, &s
->real_volume
);
565 pa_assert((s
->flags
& PA_SINK_HW_VOLUME_CTRL
)
566 || (s
->base_volume
== PA_VOLUME_NORM
567 && ((s
->flags
& PA_SINK_DECIBEL_VOLUME
|| (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)))));
568 pa_assert(!(s
->flags
& PA_SINK_DECIBEL_VOLUME
) || s
->n_volume_steps
== PA_VOLUME_NORM
+1);
569 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == (s
->thread_info
.fixed_latency
!= 0));
570 pa_assert(!(s
->flags
& PA_SINK_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_LATENCY
));
571 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
));
573 pa_assert(s
->monitor_source
->thread_info
.fixed_latency
== s
->thread_info
.fixed_latency
);
574 pa_assert(s
->monitor_source
->thread_info
.min_latency
== s
->thread_info
.min_latency
);
575 pa_assert(s
->monitor_source
->thread_info
.max_latency
== s
->thread_info
.max_latency
);
577 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
579 pa_source_put(s
->monitor_source
);
581 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
582 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
585 /* Called from main context */
586 void pa_sink_unlink(pa_sink
* s
) {
588 pa_sink_input
*i
, *j
= NULL
;
591 pa_assert_ctl_context();
593 /* Please note that pa_sink_unlink() does more than simply
594 * reversing pa_sink_put(). It also undoes the registrations
595 * already done in pa_sink_new()! */
597 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
598 * may be called multiple times on the same sink without bad
601 linked
= PA_SINK_IS_LINKED(s
->state
);
604 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
606 if (s
->state
!= PA_SINK_UNLINKED
)
607 pa_namereg_unregister(s
->core
, s
->name
);
608 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
611 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
613 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
615 pa_sink_input_kill(i
);
620 sink_set_state(s
, PA_SINK_UNLINKED
);
622 s
->state
= PA_SINK_UNLINKED
;
626 if (s
->monitor_source
)
627 pa_source_unlink(s
->monitor_source
);
630 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
631 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
635 /* Called from main context */
636 static void sink_free(pa_object
*o
) {
637 pa_sink
*s
= PA_SINK(o
);
641 pa_assert_ctl_context();
642 pa_assert(pa_sink_refcnt(s
) == 0);
644 if (PA_SINK_IS_LINKED(s
->state
))
647 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
649 if (s
->monitor_source
) {
650 pa_source_unref(s
->monitor_source
);
651 s
->monitor_source
= NULL
;
654 pa_idxset_free(s
->inputs
, NULL
, NULL
);
656 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
657 pa_sink_input_unref(i
);
659 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
661 if (s
->silence
.memblock
)
662 pa_memblock_unref(s
->silence
.memblock
);
668 pa_proplist_free(s
->proplist
);
673 while ((p
= pa_hashmap_steal_first(s
->ports
)))
674 pa_device_port_free(p
);
676 pa_hashmap_free(s
->ports
, NULL
, NULL
);
682 /* Called from main context, and not while the IO thread is active, please */
683 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
684 pa_sink_assert_ref(s
);
685 pa_assert_ctl_context();
689 if (s
->monitor_source
)
690 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
693 /* Called from main context, and not while the IO thread is active, please */
694 void pa_sink_update_flags(pa_sink
*s
, pa_sink_flags_t mask
, pa_sink_flags_t value
) {
695 pa_sink_assert_ref(s
);
696 pa_assert_ctl_context();
701 /* For now, allow only a minimal set of flags to be changed. */
702 pa_assert((mask
& ~(PA_SINK_DYNAMIC_LATENCY
|PA_SINK_LATENCY
)) == 0);
704 s
->flags
= (s
->flags
& ~mask
) | (value
& mask
);
706 pa_source_update_flags(s
->monitor_source
,
707 ((mask
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
708 ((mask
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0),
709 ((value
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
710 ((value
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SINK_DYNAMIC_LATENCY
: 0));
713 /* Called from IO context, or before _put() from main context */
714 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
715 pa_sink_assert_ref(s
);
716 pa_sink_assert_io_context(s
);
718 s
->thread_info
.rtpoll
= p
;
720 if (s
->monitor_source
)
721 pa_source_set_rtpoll(s
->monitor_source
, p
);
724 /* Called from main context */
725 int pa_sink_update_status(pa_sink
*s
) {
726 pa_sink_assert_ref(s
);
727 pa_assert_ctl_context();
728 pa_assert(PA_SINK_IS_LINKED(s
->state
));
730 if (s
->state
== PA_SINK_SUSPENDED
)
733 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
736 /* Called from main context */
737 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
738 pa_sink_assert_ref(s
);
739 pa_assert_ctl_context();
740 pa_assert(PA_SINK_IS_LINKED(s
->state
));
741 pa_assert(cause
!= 0);
744 s
->suspend_cause
|= cause
;
745 s
->monitor_source
->suspend_cause
|= cause
;
747 s
->suspend_cause
&= ~cause
;
748 s
->monitor_source
->suspend_cause
&= ~cause
;
751 if ((pa_sink_get_state(s
) == PA_SINK_SUSPENDED
) == !!s
->suspend_cause
)
754 pa_log_debug("Suspend cause of sink %s is 0x%04x, %s", s
->name
, s
->suspend_cause
, s
->suspend_cause
? "suspending" : "resuming");
756 if (s
->suspend_cause
)
757 return sink_set_state(s
, PA_SINK_SUSPENDED
);
759 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
762 /* Called from main context */
763 pa_queue
*pa_sink_move_all_start(pa_sink
*s
, pa_queue
*q
) {
764 pa_sink_input
*i
, *n
;
767 pa_sink_assert_ref(s
);
768 pa_assert_ctl_context();
769 pa_assert(PA_SINK_IS_LINKED(s
->state
));
774 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= n
) {
775 n
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
));
777 pa_sink_input_ref(i
);
779 if (pa_sink_input_start_move(i
) >= 0)
782 pa_sink_input_unref(i
);
788 /* Called from main context */
789 void pa_sink_move_all_finish(pa_sink
*s
, pa_queue
*q
, pa_bool_t save
) {
792 pa_sink_assert_ref(s
);
793 pa_assert_ctl_context();
794 pa_assert(PA_SINK_IS_LINKED(s
->state
));
797 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
798 if (pa_sink_input_finish_move(i
, s
, save
) < 0)
799 pa_sink_input_fail_move(i
);
801 pa_sink_input_unref(i
);
804 pa_queue_free(q
, NULL
, NULL
);
807 /* Called from main context */
808 void pa_sink_move_all_fail(pa_queue
*q
) {
811 pa_assert_ctl_context();
814 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
815 pa_sink_input_fail_move(i
);
816 pa_sink_input_unref(i
);
819 pa_queue_free(q
, NULL
, NULL
);
822 /* Called from IO thread context */
823 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
827 pa_sink_assert_ref(s
);
828 pa_sink_assert_io_context(s
);
829 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
831 /* If nobody requested this and this is actually no real rewind
832 * then we can short cut this. Please note that this means that
833 * not all rewind requests triggered upstream will always be
834 * translated in actual requests! */
835 if (!s
->thread_info
.rewind_requested
&& nbytes
<= 0)
838 s
->thread_info
.rewind_nbytes
= 0;
839 s
->thread_info
.rewind_requested
= FALSE
;
841 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
845 pa_log_debug("Processing rewind...");
846 if (s
->flags
& PA_SINK_SYNC_VOLUME
)
847 pa_sink_volume_change_rewind(s
, nbytes
);
850 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
851 pa_sink_input_assert_ref(i
);
852 pa_sink_input_process_rewind(i
, nbytes
);
856 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
857 pa_source_process_rewind(s
->monitor_source
, nbytes
);
861 /* Called from IO thread context */
862 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
866 size_t mixlength
= *length
;
868 pa_sink_assert_ref(s
);
869 pa_sink_assert_io_context(s
);
872 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
873 pa_sink_input_assert_ref(i
);
875 pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
);
877 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
878 mixlength
= info
->chunk
.length
;
880 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
881 pa_memblock_unref(info
->chunk
.memblock
);
885 info
->userdata
= pa_sink_input_ref(i
);
887 pa_assert(info
->chunk
.memblock
);
888 pa_assert(info
->chunk
.length
> 0);
901 /* Called from IO thread context */
902 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
906 unsigned n_unreffed
= 0;
908 pa_sink_assert_ref(s
);
909 pa_sink_assert_io_context(s
);
911 pa_assert(result
->memblock
);
912 pa_assert(result
->length
> 0);
914 /* We optimize for the case where the order of the inputs has not changed */
916 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
918 pa_mix_info
* m
= NULL
;
920 pa_sink_input_assert_ref(i
);
922 /* Let's try to find the matching entry info the pa_mix_info array */
923 for (j
= 0; j
< n
; j
++) {
925 if (info
[p
].userdata
== i
) {
936 pa_sink_input_drop(i
, result
->length
);
938 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
)) {
940 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
945 if (m
&& m
->chunk
.memblock
) {
947 pa_memblock_ref(c
.memblock
);
948 pa_assert(result
->length
<= c
.length
);
949 c
.length
= result
->length
;
951 pa_memchunk_make_writable(&c
, 0);
952 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
955 pa_memblock_ref(c
.memblock
);
956 pa_assert(result
->length
<= c
.length
);
957 c
.length
= result
->length
;
960 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
961 pa_source_output_assert_ref(o
);
962 pa_assert(o
->direct_on_input
== i
);
963 pa_source_post_direct(s
->monitor_source
, o
, &c
);
966 pa_memblock_unref(c
.memblock
);
971 if (m
->chunk
.memblock
)
972 pa_memblock_unref(m
->chunk
.memblock
);
973 pa_memchunk_reset(&m
->chunk
);
975 pa_sink_input_unref(m
->userdata
);
982 /* Now drop references to entries that are included in the
983 * pa_mix_info array but don't exist anymore */
985 if (n_unreffed
< n
) {
986 for (; n
> 0; info
++, n
--) {
988 pa_sink_input_unref(info
->userdata
);
989 if (info
->chunk
.memblock
)
990 pa_memblock_unref(info
->chunk
.memblock
);
994 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
995 pa_source_post(s
->monitor_source
, result
);
998 /* Called from IO thread context */
999 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
1000 pa_mix_info info
[MAX_MIX_CHANNELS
];
1002 size_t block_size_max
;
1004 pa_sink_assert_ref(s
);
1005 pa_sink_assert_io_context(s
);
1006 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1007 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
1010 pa_assert(!s
->thread_info
.rewind_requested
);
1011 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1013 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1014 result
->memblock
= pa_memblock_ref(s
->silence
.memblock
);
1015 result
->index
= s
->silence
.index
;
1016 result
->length
= PA_MIN(s
->silence
.length
, length
);
1023 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
1025 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
1026 if (length
> block_size_max
)
1027 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
1029 pa_assert(length
> 0);
1031 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
1035 *result
= s
->silence
;
1036 pa_memblock_ref(result
->memblock
);
1038 if (result
->length
> length
)
1039 result
->length
= length
;
1041 } else if (n
== 1) {
1044 *result
= info
[0].chunk
;
1045 pa_memblock_ref(result
->memblock
);
1047 if (result
->length
> length
)
1048 result
->length
= length
;
1050 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1052 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
)) {
1053 pa_memblock_unref(result
->memblock
);
1054 pa_silence_memchunk_get(&s
->core
->silence_cache
,
1059 } else if (!pa_cvolume_is_norm(&volume
)) {
1060 pa_memchunk_make_writable(result
, 0);
1061 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
1065 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
1067 ptr
= pa_memblock_acquire(result
->memblock
);
1068 result
->length
= pa_mix(info
, n
,
1071 &s
->thread_info
.soft_volume
,
1072 s
->thread_info
.soft_muted
);
1073 pa_memblock_release(result
->memblock
);
1078 inputs_drop(s
, info
, n
, result
);
1083 /* Called from IO thread context */
1084 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
1085 pa_mix_info info
[MAX_MIX_CHANNELS
];
1087 size_t length
, block_size_max
;
1089 pa_sink_assert_ref(s
);
1090 pa_sink_assert_io_context(s
);
1091 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1093 pa_assert(target
->memblock
);
1094 pa_assert(target
->length
> 0);
1095 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
1097 pa_assert(!s
->thread_info
.rewind_requested
);
1098 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1100 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1101 pa_silence_memchunk(target
, &s
->sample_spec
);
1107 length
= target
->length
;
1108 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
1109 if (length
> block_size_max
)
1110 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
1112 pa_assert(length
> 0);
1114 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
1117 if (target
->length
> length
)
1118 target
->length
= length
;
1120 pa_silence_memchunk(target
, &s
->sample_spec
);
1121 } else if (n
== 1) {
1124 if (target
->length
> length
)
1125 target
->length
= length
;
1127 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1129 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
1130 pa_silence_memchunk(target
, &s
->sample_spec
);
1134 vchunk
= info
[0].chunk
;
1135 pa_memblock_ref(vchunk
.memblock
);
1137 if (vchunk
.length
> length
)
1138 vchunk
.length
= length
;
1140 if (!pa_cvolume_is_norm(&volume
)) {
1141 pa_memchunk_make_writable(&vchunk
, 0);
1142 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
1145 pa_memchunk_memcpy(target
, &vchunk
);
1146 pa_memblock_unref(vchunk
.memblock
);
1152 ptr
= pa_memblock_acquire(target
->memblock
);
1154 target
->length
= pa_mix(info
, n
,
1155 (uint8_t*) ptr
+ target
->index
, length
,
1157 &s
->thread_info
.soft_volume
,
1158 s
->thread_info
.soft_muted
);
1160 pa_memblock_release(target
->memblock
);
1163 inputs_drop(s
, info
, n
, target
);
1168 /* Called from IO thread context */
1169 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
1173 pa_sink_assert_ref(s
);
1174 pa_sink_assert_io_context(s
);
1175 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1177 pa_assert(target
->memblock
);
1178 pa_assert(target
->length
> 0);
1179 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
1181 pa_assert(!s
->thread_info
.rewind_requested
);
1182 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1184 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1185 pa_silence_memchunk(target
, &s
->sample_spec
);
1198 pa_sink_render_into(s
, &chunk
);
1207 /* Called from IO thread context */
1208 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
1209 pa_sink_assert_ref(s
);
1210 pa_sink_assert_io_context(s
);
1211 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1212 pa_assert(length
> 0);
1213 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
1216 pa_assert(!s
->thread_info
.rewind_requested
);
1217 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1221 pa_sink_render(s
, length
, result
);
1223 if (result
->length
< length
) {
1226 pa_memchunk_make_writable(result
, length
);
1228 chunk
.memblock
= result
->memblock
;
1229 chunk
.index
= result
->index
+ result
->length
;
1230 chunk
.length
= length
- result
->length
;
1232 pa_sink_render_into_full(s
, &chunk
);
1234 result
->length
= length
;
1240 /* Called from main thread */
1241 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
1244 pa_sink_assert_ref(s
);
1245 pa_assert_ctl_context();
1246 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1248 /* The returned value is supposed to be in the time domain of the sound card! */
1250 if (s
->state
== PA_SINK_SUSPENDED
)
1253 if (!(s
->flags
& PA_SINK_LATENCY
))
1256 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
1261 /* Called from IO thread */
1262 pa_usec_t
pa_sink_get_latency_within_thread(pa_sink
*s
) {
1266 pa_sink_assert_ref(s
);
1267 pa_sink_assert_io_context(s
);
1268 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1270 /* The returned value is supposed to be in the time domain of the sound card! */
1272 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
1275 if (!(s
->flags
& PA_SINK_LATENCY
))
1278 o
= PA_MSGOBJECT(s
);
1280 /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
1282 if (o
->process_msg(o
, PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1288 /* Called from the main thread (and also from the IO thread while the main
1289 * thread is waiting).
1291 * When a sink uses volume sharing, it never has the PA_SINK_FLAT_VOLUME flag
1292 * set. Instead, flat volume mode is detected by checking whether the root sink
1293 * has the flag set. */
1294 pa_bool_t
pa_sink_flat_volume_enabled(pa_sink
*s
) {
1295 pa_sink_assert_ref(s
);
1297 while (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)
1298 s
= s
->input_to_master
->sink
;
1300 return (s
->flags
& PA_SINK_FLAT_VOLUME
);
1303 /* Called from main context */
1304 pa_bool_t
pa_sink_is_passthrough(pa_sink
*s
) {
1305 pa_sink_input
*alt_i
;
1308 pa_sink_assert_ref(s
);
1310 /* one and only one PASSTHROUGH input can possibly be connected */
1311 if (pa_idxset_size(s
->inputs
) == 1) {
1312 alt_i
= pa_idxset_first(s
->inputs
, &idx
);
1314 if (pa_sink_input_is_passthrough(alt_i
))
1321 /* Called from main context. */
1322 static void compute_reference_ratio(pa_sink_input
*i
) {
1324 pa_cvolume remapped
;
1327 pa_assert(pa_sink_flat_volume_enabled(i
->sink
));
1330 * Calculates the reference ratio from the sink's reference
1331 * volume. This basically calculates:
1333 * i->reference_ratio = i->volume / i->sink->reference_volume
1336 remapped
= i
->sink
->reference_volume
;
1337 pa_cvolume_remap(&remapped
, &i
->sink
->channel_map
, &i
->channel_map
);
1339 i
->reference_ratio
.channels
= i
->sample_spec
.channels
;
1341 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1343 /* We don't update when the sink volume is 0 anyway */
1344 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
)
1347 /* Don't update the reference ratio unless necessary */
1348 if (pa_sw_volume_multiply(
1349 i
->reference_ratio
.values
[c
],
1350 remapped
.values
[c
]) == i
->volume
.values
[c
])
1353 i
->reference_ratio
.values
[c
] = pa_sw_volume_divide(
1354 i
->volume
.values
[c
],
1355 remapped
.values
[c
]);
1359 /* Called from main context. Only called for the root sink in volume sharing
1360 * cases, except for internal recursive calls. */
1361 static void compute_reference_ratios(pa_sink
*s
) {
1365 pa_sink_assert_ref(s
);
1366 pa_assert_ctl_context();
1367 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1368 pa_assert(pa_sink_flat_volume_enabled(s
));
1370 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1371 compute_reference_ratio(i
);
1373 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1374 compute_reference_ratios(i
->origin_sink
);
1378 /* Called from main context. Only called for the root sink in volume sharing
1379 * cases, except for internal recursive calls. */
1380 static void compute_real_ratios(pa_sink
*s
) {
1384 pa_sink_assert_ref(s
);
1385 pa_assert_ctl_context();
1386 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1387 pa_assert(pa_sink_flat_volume_enabled(s
));
1389 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1391 pa_cvolume remapped
;
1393 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1394 /* The origin sink uses volume sharing, so this input's real ratio
1395 * is handled as a special case - the real ratio must be 0 dB, and
1396 * as a result i->soft_volume must equal i->volume_factor. */
1397 pa_cvolume_reset(&i
->real_ratio
, i
->real_ratio
.channels
);
1398 i
->soft_volume
= i
->volume_factor
;
1400 compute_real_ratios(i
->origin_sink
);
1406 * This basically calculates:
1408 * i->real_ratio := i->volume / s->real_volume
1409 * i->soft_volume := i->real_ratio * i->volume_factor
1412 remapped
= s
->real_volume
;
1413 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1415 i
->real_ratio
.channels
= i
->sample_spec
.channels
;
1416 i
->soft_volume
.channels
= i
->sample_spec
.channels
;
1418 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1420 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
) {
1421 /* We leave i->real_ratio untouched */
1422 i
->soft_volume
.values
[c
] = PA_VOLUME_MUTED
;
1426 /* Don't lose accuracy unless necessary */
1427 if (pa_sw_volume_multiply(
1428 i
->real_ratio
.values
[c
],
1429 remapped
.values
[c
]) != i
->volume
.values
[c
])
1431 i
->real_ratio
.values
[c
] = pa_sw_volume_divide(
1432 i
->volume
.values
[c
],
1433 remapped
.values
[c
]);
1435 i
->soft_volume
.values
[c
] = pa_sw_volume_multiply(
1436 i
->real_ratio
.values
[c
],
1437 i
->volume_factor
.values
[c
]);
1440 /* We don't copy the soft_volume to the thread_info data
1441 * here. That must be done by the caller */
1445 static pa_cvolume
*cvolume_remap_minimal_impact(
1447 const pa_cvolume
*template,
1448 const pa_channel_map
*from
,
1449 const pa_channel_map
*to
) {
1454 pa_assert(template);
1457 pa_assert(pa_cvolume_compatible_with_channel_map(v
, from
));
1458 pa_assert(pa_cvolume_compatible_with_channel_map(template, to
));
1460 /* Much like pa_cvolume_remap(), but tries to minimize impact when
1461 * mapping from sink input to sink volumes:
1463 * If template is a possible remapping from v it is used instead
1464 * of remapping anew.
1466 * If the channel maps don't match we set an all-channel volume on
1467 * the sink to ensure that changing a volume on one stream has no
1468 * effect that cannot be compensated for in another stream that
1469 * does not have the same channel map as the sink. */
1471 if (pa_channel_map_equal(from
, to
))
1475 if (pa_cvolume_equal(pa_cvolume_remap(&t
, to
, from
), v
)) {
1480 pa_cvolume_set(v
, to
->channels
, pa_cvolume_max(v
));
1484 /* Called from main thread. Only called for the root sink in volume sharing
1485 * cases, except for internal recursive calls. */
1486 static void get_maximum_input_volume(pa_sink
*s
, pa_cvolume
*max_volume
, const pa_channel_map
*channel_map
) {
1490 pa_sink_assert_ref(s
);
1491 pa_assert(max_volume
);
1492 pa_assert(channel_map
);
1493 pa_assert(pa_sink_flat_volume_enabled(s
));
1495 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1496 pa_cvolume remapped
;
1498 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1499 get_maximum_input_volume(i
->origin_sink
, max_volume
, channel_map
);
1501 /* Ignore this input. The origin sink uses volume sharing, so this
1502 * input's volume will be set to be equal to the root sink's real
1503 * volume. Obviously this input's current volume must not then
1504 * affect what the root sink's real volume will be. */
1508 remapped
= i
->volume
;
1509 cvolume_remap_minimal_impact(&remapped
, max_volume
, &i
->channel_map
, channel_map
);
1510 pa_cvolume_merge(max_volume
, max_volume
, &remapped
);
1514 /* Called from main thread. Only called for the root sink in volume sharing
1515 * cases, except for internal recursive calls. */
1516 static pa_bool_t
has_inputs(pa_sink
*s
) {
1520 pa_sink_assert_ref(s
);
1522 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1523 if (!i
->origin_sink
|| !(i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) || has_inputs(i
->origin_sink
))
1530 /* Called from main thread. Only called for the root sink in volume sharing
1531 * cases, except for internal recursive calls. */
1532 static void update_real_volume(pa_sink
*s
, const pa_cvolume
*new_volume
, pa_channel_map
*channel_map
) {
1536 pa_sink_assert_ref(s
);
1537 pa_assert(new_volume
);
1538 pa_assert(channel_map
);
1540 s
->real_volume
= *new_volume
;
1541 pa_cvolume_remap(&s
->real_volume
, channel_map
, &s
->channel_map
);
1543 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1544 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1545 if (pa_sink_flat_volume_enabled(s
)) {
1546 pa_cvolume old_volume
= i
->volume
;
1548 /* Follow the root sink's real volume. */
1549 i
->volume
= *new_volume
;
1550 pa_cvolume_remap(&i
->volume
, channel_map
, &i
->channel_map
);
1551 compute_reference_ratio(i
);
1553 /* The volume changed, let's tell people so */
1554 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1555 if (i
->volume_changed
)
1556 i
->volume_changed(i
);
1558 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1562 update_real_volume(i
->origin_sink
, new_volume
, channel_map
);
1567 /* Called from main thread. Only called for the root sink in shared volume
1569 static void compute_real_volume(pa_sink
*s
) {
1570 pa_sink_assert_ref(s
);
1571 pa_assert_ctl_context();
1572 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1573 pa_assert(pa_sink_flat_volume_enabled(s
));
1574 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
1576 /* This determines the maximum volume of all streams and sets
1577 * s->real_volume accordingly. */
1579 if (!has_inputs(s
)) {
1580 /* In the special case that we have no sink inputs we leave the
1581 * volume unmodified. */
1582 update_real_volume(s
, &s
->reference_volume
, &s
->channel_map
);
1586 pa_cvolume_mute(&s
->real_volume
, s
->channel_map
.channels
);
1588 /* First let's determine the new maximum volume of all inputs
1589 * connected to this sink */
1590 get_maximum_input_volume(s
, &s
->real_volume
, &s
->channel_map
);
1591 update_real_volume(s
, &s
->real_volume
, &s
->channel_map
);
1593 /* Then, let's update the real ratios/soft volumes of all inputs
1594 * connected to this sink */
1595 compute_real_ratios(s
);
1598 /* Called from main thread. Only called for the root sink in shared volume
1599 * cases, except for internal recursive calls. */
1600 static void propagate_reference_volume(pa_sink
*s
) {
1604 pa_sink_assert_ref(s
);
1605 pa_assert_ctl_context();
1606 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1607 pa_assert(pa_sink_flat_volume_enabled(s
));
1609 /* This is called whenever the sink volume changes that is not
1610 * caused by a sink input volume change. We need to fix up the
1611 * sink input volumes accordingly */
1613 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1614 pa_cvolume old_volume
;
1616 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1617 propagate_reference_volume(i
->origin_sink
);
1619 /* Since the origin sink uses volume sharing, this input's volume
1620 * needs to be updated to match the root sink's real volume, but
1621 * that will be done later in update_shared_real_volume(). */
1625 old_volume
= i
->volume
;
1627 /* This basically calculates:
1629 * i->volume := s->reference_volume * i->reference_ratio */
1631 i
->volume
= s
->reference_volume
;
1632 pa_cvolume_remap(&i
->volume
, &s
->channel_map
, &i
->channel_map
);
1633 pa_sw_cvolume_multiply(&i
->volume
, &i
->volume
, &i
->reference_ratio
);
1635 /* The volume changed, let's tell people so */
1636 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1638 if (i
->volume_changed
)
1639 i
->volume_changed(i
);
1641 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1646 /* Called from main thread. Only called for the root sink in volume sharing
1647 * cases, except for internal recursive calls. The return value indicates
1648 * whether any reference volume actually changed. */
1649 static pa_bool_t
update_reference_volume(pa_sink
*s
, const pa_cvolume
*v
, const pa_channel_map
*channel_map
, pa_bool_t save
) {
1651 pa_bool_t reference_volume_changed
;
1655 pa_sink_assert_ref(s
);
1656 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1658 pa_assert(channel_map
);
1659 pa_assert(pa_cvolume_valid(v
));
1662 pa_cvolume_remap(&volume
, channel_map
, &s
->channel_map
);
1664 reference_volume_changed
= !pa_cvolume_equal(&volume
, &s
->reference_volume
);
1665 s
->reference_volume
= volume
;
1667 s
->save_volume
= (!reference_volume_changed
&& s
->save_volume
) || save
;
1669 if (reference_volume_changed
)
1670 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1671 else if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1672 /* If the root sink's volume doesn't change, then there can't be any
1673 * changes in the other sinks in the sink tree either.
1675 * It's probably theoretically possible that even if the root sink's
1676 * volume changes slightly, some filter sink doesn't change its volume
1677 * due to rounding errors. If that happens, we still want to propagate
1678 * the changed root sink volume to the sinks connected to the
1679 * intermediate sink that didn't change its volume. This theoretical
1680 * possiblity is the reason why we have that !(s->flags &
1681 * PA_SINK_SHARE_VOLUME_WITH_MASTER) condition. Probably nobody would
1682 * notice even if we returned here FALSE always if
1683 * reference_volume_changed is FALSE. */
1686 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1687 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1688 update_reference_volume(i
->origin_sink
, v
, channel_map
, FALSE
);
1694 /* Called from main thread */
1695 void pa_sink_set_volume(
1697 const pa_cvolume
*volume
,
1701 pa_cvolume new_reference_volume
;
1702 pa_sink
*root_sink
= s
;
1704 pa_sink_assert_ref(s
);
1705 pa_assert_ctl_context();
1706 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1707 pa_assert(!volume
|| pa_cvolume_valid(volume
));
1708 pa_assert(volume
|| pa_sink_flat_volume_enabled(s
));
1709 pa_assert(!volume
|| volume
->channels
== 1 || pa_cvolume_compatible(volume
, &s
->sample_spec
));
1711 /* make sure we don't change the volume when a PASSTHROUGH input is connected */
1712 if (pa_sink_is_passthrough(s
)) {
1713 /* FIXME: Need to notify client that volume control is disabled */
1714 pa_log_warn("Cannot change volume, Sink is connected to PASSTHROUGH input");
1718 /* In case of volume sharing, the volume is set for the root sink first,
1719 * from which it's then propagated to the sharing sinks. */
1720 while (root_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)
1721 root_sink
= root_sink
->input_to_master
->sink
;
1723 /* As a special exception we accept mono volumes on all sinks --
1724 * even on those with more complex channel maps */
1727 if (pa_cvolume_compatible(volume
, &s
->sample_spec
))
1728 new_reference_volume
= *volume
;
1730 new_reference_volume
= s
->reference_volume
;
1731 pa_cvolume_scale(&new_reference_volume
, pa_cvolume_max(volume
));
1734 pa_cvolume_remap(&new_reference_volume
, &s
->channel_map
, &root_sink
->channel_map
);
1737 /* If volume is NULL we synchronize the sink's real and reference
1738 * volumes with the stream volumes. If it is not NULL we update
1739 * the reference_volume with it. */
1742 if (update_reference_volume(root_sink
, &new_reference_volume
, &root_sink
->channel_map
, save
)) {
1743 if (pa_sink_flat_volume_enabled(root_sink
)) {
1744 /* OK, propagate this volume change back to the inputs */
1745 propagate_reference_volume(root_sink
);
1747 /* And now recalculate the real volume */
1748 compute_real_volume(root_sink
);
1750 update_real_volume(root_sink
, &root_sink
->reference_volume
, &root_sink
->channel_map
);
1754 pa_assert(pa_sink_flat_volume_enabled(root_sink
));
1756 /* Ok, let's determine the new real volume */
1757 compute_real_volume(root_sink
);
1759 /* Let's 'push' the reference volume if necessary */
1760 pa_cvolume_merge(&new_reference_volume
, &s
->reference_volume
, &root_sink
->real_volume
);
1761 update_reference_volume(root_sink
, &new_reference_volume
, &root_sink
->channel_map
, save
);
1763 /* Now that the reference volume is updated, we can update the streams'
1764 * reference ratios. */
1765 compute_reference_ratios(root_sink
);
1768 if (root_sink
->set_volume
) {
1769 /* If we have a function set_volume(), then we do not apply a
1770 * soft volume by default. However, set_volume() is free to
1771 * apply one to root_sink->soft_volume */
1773 pa_cvolume_reset(&root_sink
->soft_volume
, root_sink
->sample_spec
.channels
);
1774 if (!(root_sink
->flags
& PA_SINK_SYNC_VOLUME
))
1775 root_sink
->set_volume(root_sink
);
1778 /* If we have no function set_volume(), then the soft volume
1779 * becomes the real volume */
1780 root_sink
->soft_volume
= root_sink
->real_volume
;
1782 /* This tells the sink that soft volume and/or real volume changed */
1784 pa_assert_se(pa_asyncmsgq_send(root_sink
->asyncmsgq
, PA_MSGOBJECT(root_sink
), PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
) == 0);
1787 /* Called from the io thread if sync volume is used, otherwise from the main thread.
1788 * Only to be called by sink implementor */
1789 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
1791 pa_sink_assert_ref(s
);
1792 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
1794 if (s
->flags
& PA_SINK_SYNC_VOLUME
)
1795 pa_sink_assert_io_context(s
);
1797 pa_assert_ctl_context();
1800 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
1802 s
->soft_volume
= *volume
;
1804 if (PA_SINK_IS_LINKED(s
->state
) && !(s
->flags
& PA_SINK_SYNC_VOLUME
))
1805 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
) == 0);
1807 s
->thread_info
.soft_volume
= s
->soft_volume
;
1810 /* Called from the main thread. Only called for the root sink in volume sharing
1811 * cases, except for internal recursive calls. */
1812 static void propagate_real_volume(pa_sink
*s
, const pa_cvolume
*old_real_volume
) {
1816 pa_sink_assert_ref(s
);
1817 pa_assert(old_real_volume
);
1818 pa_assert_ctl_context();
1819 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1821 /* This is called when the hardware's real volume changes due to
1822 * some external event. We copy the real volume into our
1823 * reference volume and then rebuild the stream volumes based on
1824 * i->real_ratio which should stay fixed. */
1826 if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1827 if (pa_cvolume_equal(old_real_volume
, &s
->real_volume
))
1830 /* 1. Make the real volume the reference volume */
1831 update_reference_volume(s
, &s
->real_volume
, &s
->channel_map
, TRUE
);
1834 if (pa_sink_flat_volume_enabled(s
)) {
1836 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1837 pa_cvolume old_volume
= i
->volume
;
1839 /* 2. Since the sink's reference and real volumes are equal
1840 * now our ratios should be too. */
1841 i
->reference_ratio
= i
->real_ratio
;
1843 /* 3. Recalculate the new stream reference volume based on the
1844 * reference ratio and the sink's reference volume.
1846 * This basically calculates:
1848 * i->volume = s->reference_volume * i->reference_ratio
1850 * This is identical to propagate_reference_volume() */
1851 i
->volume
= s
->reference_volume
;
1852 pa_cvolume_remap(&i
->volume
, &s
->channel_map
, &i
->channel_map
);
1853 pa_sw_cvolume_multiply(&i
->volume
, &i
->volume
, &i
->reference_ratio
);
1855 /* Notify if something changed */
1856 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1858 if (i
->volume_changed
)
1859 i
->volume_changed(i
);
1861 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1864 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1865 propagate_real_volume(i
->origin_sink
, old_real_volume
);
1869 /* Something got changed in the hardware. It probably makes sense
1870 * to save changed hw settings given that hw volume changes not
1871 * triggered by PA are almost certainly done by the user. */
1872 if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1873 s
->save_volume
= TRUE
;
1876 /* Called from io thread */
1877 void pa_sink_update_volume_and_mute(pa_sink
*s
) {
1879 pa_sink_assert_io_context(s
);
1881 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE
, NULL
, 0, NULL
, NULL
);
1884 /* Called from main thread */
1885 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
1886 pa_sink_assert_ref(s
);
1887 pa_assert_ctl_context();
1888 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1890 if (s
->refresh_volume
|| force_refresh
) {
1891 struct pa_cvolume old_real_volume
;
1893 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
1895 old_real_volume
= s
->real_volume
;
1897 if (!(s
->flags
& PA_SINK_SYNC_VOLUME
) && s
->get_volume
)
1900 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, NULL
, 0, NULL
) == 0);
1902 update_real_volume(s
, &s
->real_volume
, &s
->channel_map
);
1903 propagate_real_volume(s
, &old_real_volume
);
1906 return &s
->reference_volume
;
1909 /* Called from main thread. In volume sharing cases, only the root sink may
1911 void pa_sink_volume_changed(pa_sink
*s
, const pa_cvolume
*new_real_volume
) {
1912 pa_cvolume old_real_volume
;
1914 pa_sink_assert_ref(s
);
1915 pa_assert_ctl_context();
1916 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1917 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
1919 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1921 old_real_volume
= s
->real_volume
;
1922 update_real_volume(s
, new_real_volume
, &s
->channel_map
);
1923 propagate_real_volume(s
, &old_real_volume
);
1926 /* Called from main thread */
1927 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
, pa_bool_t save
) {
1928 pa_bool_t old_muted
;
1930 pa_sink_assert_ref(s
);
1931 pa_assert_ctl_context();
1932 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1934 old_muted
= s
->muted
;
1936 s
->save_muted
= (old_muted
== s
->muted
&& s
->save_muted
) || save
;
1938 if (!(s
->flags
& PA_SINK_SYNC_VOLUME
) && s
->set_mute
)
1941 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
1943 if (old_muted
!= s
->muted
)
1944 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1947 /* Called from main thread */
1948 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
1950 pa_sink_assert_ref(s
);
1951 pa_assert_ctl_context();
1952 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1954 if (s
->refresh_muted
|| force_refresh
) {
1955 pa_bool_t old_muted
= s
->muted
;
1957 if (!(s
->flags
& PA_SINK_SYNC_VOLUME
) && s
->get_mute
)
1960 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, NULL
, 0, NULL
) == 0);
1962 if (old_muted
!= s
->muted
) {
1963 s
->save_muted
= TRUE
;
1965 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1967 /* Make sure the soft mute status stays in sync */
1968 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
1975 /* Called from main thread */
1976 void pa_sink_mute_changed(pa_sink
*s
, pa_bool_t new_muted
) {
1977 pa_sink_assert_ref(s
);
1978 pa_assert_ctl_context();
1979 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1981 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
1983 if (s
->muted
== new_muted
)
1986 s
->muted
= new_muted
;
1987 s
->save_muted
= TRUE
;
1989 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1992 /* Called from main thread */
1993 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
1994 pa_sink_assert_ref(s
);
1995 pa_assert_ctl_context();
1998 pa_proplist_update(s
->proplist
, mode
, p
);
2000 if (PA_SINK_IS_LINKED(s
->state
)) {
2001 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
2002 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2008 /* Called from main thread */
2009 /* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
2010 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
2012 pa_sink_assert_ref(s
);
2013 pa_assert_ctl_context();
2015 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
2018 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
2020 if (old
&& description
&& pa_streq(old
, description
))
2024 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
2026 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
2028 if (s
->monitor_source
) {
2031 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
2032 pa_source_set_description(s
->monitor_source
, n
);
2036 if (PA_SINK_IS_LINKED(s
->state
)) {
2037 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2038 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
2042 /* Called from main thread */
2043 unsigned pa_sink_linked_by(pa_sink
*s
) {
2046 pa_sink_assert_ref(s
);
2047 pa_assert_ctl_context();
2048 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2050 ret
= pa_idxset_size(s
->inputs
);
2052 /* We add in the number of streams connected to us here. Please
2053 * note the asymmmetry to pa_sink_used_by()! */
2055 if (s
->monitor_source
)
2056 ret
+= pa_source_linked_by(s
->monitor_source
);
2061 /* Called from main thread */
2062 unsigned pa_sink_used_by(pa_sink
*s
) {
2065 pa_sink_assert_ref(s
);
2066 pa_assert_ctl_context();
2067 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2069 ret
= pa_idxset_size(s
->inputs
);
2070 pa_assert(ret
>= s
->n_corked
);
2072 /* Streams connected to our monitor source do not matter for
2073 * pa_sink_used_by()!.*/
2075 return ret
- s
->n_corked
;
2078 /* Called from main thread */
2079 unsigned pa_sink_check_suspend(pa_sink
*s
) {
2084 pa_sink_assert_ref(s
);
2085 pa_assert_ctl_context();
2087 if (!PA_SINK_IS_LINKED(s
->state
))
2092 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
2093 pa_sink_input_state_t st
;
2095 st
= pa_sink_input_get_state(i
);
2097 /* We do not assert here. It is perfectly valid for a sink input to
2098 * be in the INIT state (i.e. created, marked done but not yet put)
2099 * and we should not care if it's unlinked as it won't contribute
2100 * towarards our busy status.
2102 if (!PA_SINK_INPUT_IS_LINKED(st
))
2105 if (st
== PA_SINK_INPUT_CORKED
)
2108 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
2114 if (s
->monitor_source
)
2115 ret
+= pa_source_check_suspend(s
->monitor_source
);
2120 /* Called from the IO thread */
2121 static void sync_input_volumes_within_thread(pa_sink
*s
) {
2125 pa_sink_assert_ref(s
);
2126 pa_sink_assert_io_context(s
);
2128 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
2129 if (pa_cvolume_equal(&i
->thread_info
.soft_volume
, &i
->soft_volume
))
2132 i
->thread_info
.soft_volume
= i
->soft_volume
;
2133 pa_sink_input_request_rewind(i
, 0, TRUE
, FALSE
, FALSE
);
2137 /* Called from the IO thread. Only called for the root sink in volume sharing
2138 * cases, except for internal recursive calls. */
2139 static void set_shared_volume_within_thread(pa_sink
*s
) {
2140 pa_sink_input
*i
= NULL
;
2143 pa_sink_assert_ref(s
);
2145 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME_SYNCED
, NULL
, 0, NULL
);
2147 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
2148 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
2149 set_shared_volume_within_thread(i
->origin_sink
);
2153 /* Called from IO thread, except when it is not */
2154 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
2155 pa_sink
*s
= PA_SINK(o
);
2156 pa_sink_assert_ref(s
);
2158 switch ((pa_sink_message_t
) code
) {
2160 case PA_SINK_MESSAGE_ADD_INPUT
: {
2161 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2163 /* If you change anything here, make sure to change the
2164 * sink input handling a few lines down at
2165 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
2167 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
2169 /* Since the caller sleeps in pa_sink_input_put(), we can
2170 * safely access data outside of thread_info even though
2173 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
2174 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
2175 pa_assert(i
->sync_prev
->sync_next
== i
);
2176 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
2179 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
2180 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
2181 pa_assert(i
->sync_next
->sync_prev
== i
);
2182 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
2185 pa_assert(!i
->thread_info
.attached
);
2186 i
->thread_info
.attached
= TRUE
;
2191 pa_sink_input_set_state_within_thread(i
, i
->state
);
2193 /* The requested latency of the sink input needs to be
2194 * fixed up and then configured on the sink */
2196 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
2197 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
2199 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2200 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2202 /* We don't rewind here automatically. This is left to the
2203 * sink input implementor because some sink inputs need a
2204 * slow start, i.e. need some time to buffer client
2205 * samples before beginning streaming. */
2207 /* In flat volume mode we need to update the volume as
2209 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2212 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
2213 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2215 /* If you change anything here, make sure to change the
2216 * sink input handling a few lines down at
2217 * PA_SINK_MESSAGE_START_MOVE, too. */
2222 pa_sink_input_set_state_within_thread(i
, i
->state
);
2224 pa_assert(i
->thread_info
.attached
);
2225 i
->thread_info
.attached
= FALSE
;
2227 /* Since the caller sleeps in pa_sink_input_unlink(),
2228 * we can safely access data outside of thread_info even
2229 * though it is mutable */
2231 pa_assert(!i
->sync_prev
);
2232 pa_assert(!i
->sync_next
);
2234 if (i
->thread_info
.sync_prev
) {
2235 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
2236 i
->thread_info
.sync_prev
= NULL
;
2239 if (i
->thread_info
.sync_next
) {
2240 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
2241 i
->thread_info
.sync_next
= NULL
;
2244 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
2245 pa_sink_input_unref(i
);
2247 pa_sink_invalidate_requested_latency(s
, TRUE
);
2248 pa_sink_request_rewind(s
, (size_t) -1);
2250 /* In flat volume mode we need to update the volume as
2252 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2255 case PA_SINK_MESSAGE_START_MOVE
: {
2256 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2258 /* We don't support moving synchronized streams. */
2259 pa_assert(!i
->sync_prev
);
2260 pa_assert(!i
->sync_next
);
2261 pa_assert(!i
->thread_info
.sync_next
);
2262 pa_assert(!i
->thread_info
.sync_prev
);
2264 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
2266 size_t sink_nbytes
, total_nbytes
;
2268 /* Get the latency of the sink */
2269 usec
= pa_sink_get_latency_within_thread(s
);
2270 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
2271 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
2273 if (total_nbytes
> 0) {
2274 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
2275 i
->thread_info
.rewrite_flush
= TRUE
;
2276 pa_sink_input_process_rewind(i
, sink_nbytes
);
2283 pa_assert(i
->thread_info
.attached
);
2284 i
->thread_info
.attached
= FALSE
;
2286 /* Let's remove the sink input ...*/
2287 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
2288 pa_sink_input_unref(i
);
2290 pa_sink_invalidate_requested_latency(s
, TRUE
);
2292 pa_log_debug("Requesting rewind due to started move");
2293 pa_sink_request_rewind(s
, (size_t) -1);
2295 /* In flat volume mode we need to update the volume as
2297 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2300 case PA_SINK_MESSAGE_FINISH_MOVE
: {
2301 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2303 /* We don't support moving synchronized streams. */
2304 pa_assert(!i
->sync_prev
);
2305 pa_assert(!i
->sync_next
);
2306 pa_assert(!i
->thread_info
.sync_next
);
2307 pa_assert(!i
->thread_info
.sync_prev
);
2309 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
2311 pa_assert(!i
->thread_info
.attached
);
2312 i
->thread_info
.attached
= TRUE
;
2317 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
2318 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
2320 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2321 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2323 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
2327 /* Get the latency of the sink */
2328 usec
= pa_sink_get_latency_within_thread(s
);
2329 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
2332 pa_sink_input_drop(i
, nbytes
);
2334 pa_log_debug("Requesting rewind due to finished move");
2335 pa_sink_request_rewind(s
, nbytes
);
2338 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2341 case PA_SINK_MESSAGE_SET_SHARED_VOLUME
: {
2342 pa_sink
*root_sink
= s
;
2344 while (root_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)
2345 root_sink
= root_sink
->input_to_master
->sink
;
2347 set_shared_volume_within_thread(root_sink
);
2351 case PA_SINK_MESSAGE_SET_VOLUME_SYNCED
:
2353 if (s
->flags
& PA_SINK_SYNC_VOLUME
) {
2355 pa_sink_volume_change_push(s
);
2357 /* Fall through ... */
2359 case PA_SINK_MESSAGE_SET_VOLUME
:
2361 if (!pa_cvolume_equal(&s
->thread_info
.soft_volume
, &s
->soft_volume
)) {
2362 s
->thread_info
.soft_volume
= s
->soft_volume
;
2363 pa_sink_request_rewind(s
, (size_t) -1);
2366 /* Fall through ... */
2368 case PA_SINK_MESSAGE_SYNC_VOLUMES
:
2369 sync_input_volumes_within_thread(s
);
2372 case PA_SINK_MESSAGE_GET_VOLUME
:
2374 if ((s
->flags
& PA_SINK_SYNC_VOLUME
) && s
->get_volume
) {
2376 pa_sink_volume_change_flush(s
);
2377 pa_sw_cvolume_divide(&s
->thread_info
.current_hw_volume
, &s
->real_volume
, &s
->soft_volume
);
2380 /* In case sink implementor reset SW volume. */
2381 if (!pa_cvolume_equal(&s
->thread_info
.soft_volume
, &s
->soft_volume
)) {
2382 s
->thread_info
.soft_volume
= s
->soft_volume
;
2383 pa_sink_request_rewind(s
, (size_t) -1);
2388 case PA_SINK_MESSAGE_SET_MUTE
:
2390 if (s
->thread_info
.soft_muted
!= s
->muted
) {
2391 s
->thread_info
.soft_muted
= s
->muted
;
2392 pa_sink_request_rewind(s
, (size_t) -1);
2395 if (s
->flags
& PA_SINK_SYNC_VOLUME
&& s
->set_mute
)
2400 case PA_SINK_MESSAGE_GET_MUTE
:
2402 if (s
->flags
& PA_SINK_SYNC_VOLUME
&& s
->get_mute
)
2407 case PA_SINK_MESSAGE_SET_STATE
: {
2409 pa_bool_t suspend_change
=
2410 (s
->thread_info
.state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata
))) ||
2411 (PA_SINK_IS_OPENED(s
->thread_info
.state
) && PA_PTR_TO_UINT(userdata
) == PA_SINK_SUSPENDED
);
2413 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
2415 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
2416 s
->thread_info
.rewind_nbytes
= 0;
2417 s
->thread_info
.rewind_requested
= FALSE
;
2420 if (suspend_change
) {
2424 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
2425 if (i
->suspend_within_thread
)
2426 i
->suspend_within_thread(i
, s
->thread_info
.state
== PA_SINK_SUSPENDED
);
2432 case PA_SINK_MESSAGE_DETACH
:
2434 /* Detach all streams */
2435 pa_sink_detach_within_thread(s
);
2438 case PA_SINK_MESSAGE_ATTACH
:
2440 /* Reattach all streams */
2441 pa_sink_attach_within_thread(s
);
2444 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
2446 pa_usec_t
*usec
= userdata
;
2447 *usec
= pa_sink_get_requested_latency_within_thread(s
);
2449 /* Yes, that's right, the IO thread will see -1 when no
2450 * explicit requested latency is configured, the main
2451 * thread will see max_latency */
2452 if (*usec
== (pa_usec_t
) -1)
2453 *usec
= s
->thread_info
.max_latency
;
2458 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
2459 pa_usec_t
*r
= userdata
;
2461 pa_sink_set_latency_range_within_thread(s
, r
[0], r
[1]);
2466 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
2467 pa_usec_t
*r
= userdata
;
2469 r
[0] = s
->thread_info
.min_latency
;
2470 r
[1] = s
->thread_info
.max_latency
;
2475 case PA_SINK_MESSAGE_GET_FIXED_LATENCY
:
2477 *((pa_usec_t
*) userdata
) = s
->thread_info
.fixed_latency
;
2480 case PA_SINK_MESSAGE_SET_FIXED_LATENCY
:
2482 pa_sink_set_fixed_latency_within_thread(s
, (pa_usec_t
) offset
);
2485 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
2487 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
2490 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
2492 *((size_t*) userdata
) = s
->thread_info
.max_request
;
2495 case PA_SINK_MESSAGE_SET_MAX_REWIND
:
2497 pa_sink_set_max_rewind_within_thread(s
, (size_t) offset
);
2500 case PA_SINK_MESSAGE_SET_MAX_REQUEST
:
2502 pa_sink_set_max_request_within_thread(s
, (size_t) offset
);
2505 case PA_SINK_MESSAGE_SET_PORT
:
2507 pa_assert(userdata
);
2509 struct sink_message_set_port
*msg_data
= userdata
;
2510 msg_data
->ret
= s
->set_port(s
, msg_data
->port
);
2514 case PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE
:
2515 /* This message is sent from IO-thread and handled in main thread. */
2516 pa_assert_ctl_context();
2518 pa_sink_get_volume(s
, TRUE
);
2519 pa_sink_get_mute(s
, TRUE
);
2522 case PA_SINK_MESSAGE_GET_LATENCY
:
2523 case PA_SINK_MESSAGE_MAX
:
2530 /* Called from main thread */
2531 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
2536 pa_core_assert_ref(c
);
2537 pa_assert_ctl_context();
2538 pa_assert(cause
!= 0);
2540 PA_IDXSET_FOREACH(sink
, c
->sinks
, idx
) {
2543 if ((r
= pa_sink_suspend(sink
, suspend
, cause
)) < 0)
2550 /* Called from main thread */
2551 void pa_sink_detach(pa_sink
*s
) {
2552 pa_sink_assert_ref(s
);
2553 pa_assert_ctl_context();
2554 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2556 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
2559 /* Called from main thread */
2560 void pa_sink_attach(pa_sink
*s
) {
2561 pa_sink_assert_ref(s
);
2562 pa_assert_ctl_context();
2563 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2565 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
2568 /* Called from IO thread */
2569 void pa_sink_detach_within_thread(pa_sink
*s
) {
2573 pa_sink_assert_ref(s
);
2574 pa_sink_assert_io_context(s
);
2575 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2577 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2581 if (s
->monitor_source
)
2582 pa_source_detach_within_thread(s
->monitor_source
);
2585 /* Called from IO thread */
2586 void pa_sink_attach_within_thread(pa_sink
*s
) {
2590 pa_sink_assert_ref(s
);
2591 pa_sink_assert_io_context(s
);
2592 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2594 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2598 if (s
->monitor_source
)
2599 pa_source_attach_within_thread(s
->monitor_source
);
2602 /* Called from IO thread */
2603 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
2604 pa_sink_assert_ref(s
);
2605 pa_sink_assert_io_context(s
);
2606 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2608 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
2611 if (nbytes
== (size_t) -1)
2612 nbytes
= s
->thread_info
.max_rewind
;
2614 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
2616 if (s
->thread_info
.rewind_requested
&&
2617 nbytes
<= s
->thread_info
.rewind_nbytes
)
2620 s
->thread_info
.rewind_nbytes
= nbytes
;
2621 s
->thread_info
.rewind_requested
= TRUE
;
2623 if (s
->request_rewind
)
2624 s
->request_rewind(s
);
2627 /* Called from IO thread */
2628 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
2629 pa_usec_t result
= (pa_usec_t
) -1;
2632 pa_usec_t monitor_latency
;
2634 pa_sink_assert_ref(s
);
2635 pa_sink_assert_io_context(s
);
2637 if (!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
2638 return PA_CLAMP(s
->thread_info
.fixed_latency
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2640 if (s
->thread_info
.requested_latency_valid
)
2641 return s
->thread_info
.requested_latency
;
2643 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2644 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
2645 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
2646 result
= i
->thread_info
.requested_sink_latency
;
2648 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
2650 if (monitor_latency
!= (pa_usec_t
) -1 &&
2651 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
2652 result
= monitor_latency
;
2654 if (result
!= (pa_usec_t
) -1)
2655 result
= PA_CLAMP(result
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2657 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2658 /* Only cache if properly initialized */
2659 s
->thread_info
.requested_latency
= result
;
2660 s
->thread_info
.requested_latency_valid
= TRUE
;
2666 /* Called from main thread */
2667 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
2670 pa_sink_assert_ref(s
);
2671 pa_assert_ctl_context();
2672 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2674 if (s
->state
== PA_SINK_SUSPENDED
)
2677 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
2682 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2683 void pa_sink_set_max_rewind_within_thread(pa_sink
*s
, size_t max_rewind
) {
2687 pa_sink_assert_ref(s
);
2688 pa_sink_assert_io_context(s
);
2690 if (max_rewind
== s
->thread_info
.max_rewind
)
2693 s
->thread_info
.max_rewind
= max_rewind
;
2695 if (PA_SINK_IS_LINKED(s
->thread_info
.state
))
2696 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2697 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2699 if (s
->monitor_source
)
2700 pa_source_set_max_rewind_within_thread(s
->monitor_source
, s
->thread_info
.max_rewind
);
2703 /* Called from main thread */
2704 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
2705 pa_sink_assert_ref(s
);
2706 pa_assert_ctl_context();
2708 if (PA_SINK_IS_LINKED(s
->state
))
2709 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REWIND
, NULL
, max_rewind
, NULL
) == 0);
2711 pa_sink_set_max_rewind_within_thread(s
, max_rewind
);
2714 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2715 void pa_sink_set_max_request_within_thread(pa_sink
*s
, size_t max_request
) {
2718 pa_sink_assert_ref(s
);
2719 pa_sink_assert_io_context(s
);
2721 if (max_request
== s
->thread_info
.max_request
)
2724 s
->thread_info
.max_request
= max_request
;
2726 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2729 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2730 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2734 /* Called from main thread */
2735 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
2736 pa_sink_assert_ref(s
);
2737 pa_assert_ctl_context();
2739 if (PA_SINK_IS_LINKED(s
->state
))
2740 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REQUEST
, NULL
, max_request
, NULL
) == 0);
2742 pa_sink_set_max_request_within_thread(s
, max_request
);
2745 /* Called from IO thread */
2746 void pa_sink_invalidate_requested_latency(pa_sink
*s
, pa_bool_t dynamic
) {
2750 pa_sink_assert_ref(s
);
2751 pa_sink_assert_io_context(s
);
2753 if ((s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
2754 s
->thread_info
.requested_latency_valid
= FALSE
;
2758 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2760 if (s
->update_requested_latency
)
2761 s
->update_requested_latency(s
);
2763 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2764 if (i
->update_sink_requested_latency
)
2765 i
->update_sink_requested_latency(i
);
2769 /* Called from main thread */
2770 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
2771 pa_sink_assert_ref(s
);
2772 pa_assert_ctl_context();
2774 /* min_latency == 0: no limit
2775 * min_latency anything else: specified limit
2777 * Similar for max_latency */
2779 if (min_latency
< ABSOLUTE_MIN_LATENCY
)
2780 min_latency
= ABSOLUTE_MIN_LATENCY
;
2782 if (max_latency
<= 0 ||
2783 max_latency
> ABSOLUTE_MAX_LATENCY
)
2784 max_latency
= ABSOLUTE_MAX_LATENCY
;
2786 pa_assert(min_latency
<= max_latency
);
2788 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
2789 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
2790 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
2791 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
2793 if (PA_SINK_IS_LINKED(s
->state
)) {
2799 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
2801 pa_sink_set_latency_range_within_thread(s
, min_latency
, max_latency
);
2804 /* Called from main thread */
2805 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
2806 pa_sink_assert_ref(s
);
2807 pa_assert_ctl_context();
2808 pa_assert(min_latency
);
2809 pa_assert(max_latency
);
2811 if (PA_SINK_IS_LINKED(s
->state
)) {
2812 pa_usec_t r
[2] = { 0, 0 };
2814 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
2816 *min_latency
= r
[0];
2817 *max_latency
= r
[1];
2819 *min_latency
= s
->thread_info
.min_latency
;
2820 *max_latency
= s
->thread_info
.max_latency
;
2824 /* Called from IO thread */
2825 void pa_sink_set_latency_range_within_thread(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
2826 pa_sink_assert_ref(s
);
2827 pa_sink_assert_io_context(s
);
2829 pa_assert(min_latency
>= ABSOLUTE_MIN_LATENCY
);
2830 pa_assert(max_latency
<= ABSOLUTE_MAX_LATENCY
);
2831 pa_assert(min_latency
<= max_latency
);
2833 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
2834 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
2835 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
2836 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
2838 if (s
->thread_info
.min_latency
== min_latency
&&
2839 s
->thread_info
.max_latency
== max_latency
)
2842 s
->thread_info
.min_latency
= min_latency
;
2843 s
->thread_info
.max_latency
= max_latency
;
2845 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2849 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2850 if (i
->update_sink_latency_range
)
2851 i
->update_sink_latency_range(i
);
2854 pa_sink_invalidate_requested_latency(s
, FALSE
);
2856 pa_source_set_latency_range_within_thread(s
->monitor_source
, min_latency
, max_latency
);
2859 /* Called from main thread */
2860 void pa_sink_set_fixed_latency(pa_sink
*s
, pa_usec_t latency
) {
2861 pa_sink_assert_ref(s
);
2862 pa_assert_ctl_context();
2864 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
2865 pa_assert(latency
== 0);
2869 if (latency
< ABSOLUTE_MIN_LATENCY
)
2870 latency
= ABSOLUTE_MIN_LATENCY
;
2872 if (latency
> ABSOLUTE_MAX_LATENCY
)
2873 latency
= ABSOLUTE_MAX_LATENCY
;
2875 if (PA_SINK_IS_LINKED(s
->state
))
2876 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_FIXED_LATENCY
, NULL
, (int64_t) latency
, NULL
) == 0);
2878 s
->thread_info
.fixed_latency
= latency
;
2880 pa_source_set_fixed_latency(s
->monitor_source
, latency
);
2883 /* Called from main thread */
2884 pa_usec_t
pa_sink_get_fixed_latency(pa_sink
*s
) {
2887 pa_sink_assert_ref(s
);
2888 pa_assert_ctl_context();
2890 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
)
2893 if (PA_SINK_IS_LINKED(s
->state
))
2894 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_FIXED_LATENCY
, &latency
, 0, NULL
) == 0);
2896 latency
= s
->thread_info
.fixed_latency
;
2901 /* Called from IO thread */
2902 void pa_sink_set_fixed_latency_within_thread(pa_sink
*s
, pa_usec_t latency
) {
2903 pa_sink_assert_ref(s
);
2904 pa_sink_assert_io_context(s
);
2906 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
2907 pa_assert(latency
== 0);
2911 pa_assert(latency
>= ABSOLUTE_MIN_LATENCY
);
2912 pa_assert(latency
<= ABSOLUTE_MAX_LATENCY
);
2914 if (s
->thread_info
.fixed_latency
== latency
)
2917 s
->thread_info
.fixed_latency
= latency
;
2919 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2923 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2924 if (i
->update_sink_fixed_latency
)
2925 i
->update_sink_fixed_latency(i
);
2928 pa_sink_invalidate_requested_latency(s
, FALSE
);
2930 pa_source_set_fixed_latency_within_thread(s
->monitor_source
, latency
);
2933 /* Called from main context */
2934 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
2936 pa_assert_ctl_context();
2937 pa_sink_assert_ref(s
);
2939 if (!PA_SINK_IS_LINKED(s
->state
))
2940 return s
->thread_info
.max_rewind
;
2942 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
2947 /* Called from main context */
2948 size_t pa_sink_get_max_request(pa_sink
*s
) {
2950 pa_sink_assert_ref(s
);
2951 pa_assert_ctl_context();
2953 if (!PA_SINK_IS_LINKED(s
->state
))
2954 return s
->thread_info
.max_request
;
2956 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);
2961 /* Called from main context */
2962 int pa_sink_set_port(pa_sink
*s
, const char *name
, pa_bool_t save
) {
2963 pa_device_port
*port
;
2966 pa_sink_assert_ref(s
);
2967 pa_assert_ctl_context();
2970 pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s
->index
, s
->name
);
2971 return -PA_ERR_NOTIMPLEMENTED
;
2975 return -PA_ERR_NOENTITY
;
2977 if (!(port
= pa_hashmap_get(s
->ports
, name
)))
2978 return -PA_ERR_NOENTITY
;
2980 if (s
->active_port
== port
) {
2981 s
->save_port
= s
->save_port
|| save
;
2985 if (s
->flags
& PA_SINK_SYNC_VOLUME
) {
2986 struct sink_message_set_port msg
= { .port
= port
, .ret
= 0 };
2987 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_PORT
, &msg
, 0, NULL
) == 0);
2991 ret
= s
->set_port(s
, port
);
2994 return -PA_ERR_NOENTITY
;
2996 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2998 pa_log_info("Changed port of sink %u \"%s\" to %s", s
->index
, s
->name
, port
->name
);
3000 s
->active_port
= port
;
3001 s
->save_port
= save
;
3003 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PORT_CHANGED
], s
);
3008 pa_bool_t
pa_device_init_icon(pa_proplist
*p
, pa_bool_t is_sink
) {
3009 const char *ff
, *c
, *t
= NULL
, *s
= "", *profile
, *bus
;
3013 if (pa_proplist_contains(p
, PA_PROP_DEVICE_ICON_NAME
))
3016 if ((ff
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
3018 if (pa_streq(ff
, "microphone"))
3019 t
= "audio-input-microphone";
3020 else if (pa_streq(ff
, "webcam"))
3022 else if (pa_streq(ff
, "computer"))
3024 else if (pa_streq(ff
, "handset"))
3026 else if (pa_streq(ff
, "portable"))
3027 t
= "multimedia-player";
3028 else if (pa_streq(ff
, "tv"))
3029 t
= "video-display";
3032 * The following icons are not part of the icon naming spec,
3033 * because Rodney Dawes sucks as the maintainer of that spec.
3035 * http://lists.freedesktop.org/archives/xdg/2009-May/010397.html
3037 else if (pa_streq(ff
, "headset"))
3038 t
= "audio-headset";
3039 else if (pa_streq(ff
, "headphone"))
3040 t
= "audio-headphones";
3041 else if (pa_streq(ff
, "speaker"))
3042 t
= "audio-speakers";
3043 else if (pa_streq(ff
, "hands-free"))
3044 t
= "audio-handsfree";
3048 if ((c
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
3049 if (pa_streq(c
, "modem"))
3056 t
= "audio-input-microphone";
3059 if ((profile
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
3060 if (strstr(profile
, "analog"))
3062 else if (strstr(profile
, "iec958"))
3064 else if (strstr(profile
, "hdmi"))
3068 bus
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
);
3070 pa_proplist_setf(p
, PA_PROP_DEVICE_ICON_NAME
, "%s%s%s%s", t
, pa_strempty(s
), bus
? "-" : "", pa_strempty(bus
));
3075 pa_bool_t
pa_device_init_description(pa_proplist
*p
) {
3076 const char *s
, *d
= NULL
, *k
;
3079 if (pa_proplist_contains(p
, PA_PROP_DEVICE_DESCRIPTION
))
3082 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
3083 if (pa_streq(s
, "internal"))
3084 d
= _("Internal Audio");
3087 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
3088 if (pa_streq(s
, "modem"))
3092 d
= pa_proplist_gets(p
, PA_PROP_DEVICE_PRODUCT_NAME
);
3097 k
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_DESCRIPTION
);
3100 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, _("%s %s"), d
, k
);
3102 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, d
);
3107 pa_bool_t
pa_device_init_intended_roles(pa_proplist
*p
) {
3111 if (pa_proplist_contains(p
, PA_PROP_DEVICE_INTENDED_ROLES
))
3114 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
3115 if (pa_streq(s
, "handset") || pa_streq(s
, "hands-free")
3116 || pa_streq(s
, "headset")) {
3117 pa_proplist_sets(p
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
3124 unsigned pa_device_init_priority(pa_proplist
*p
) {
3126 unsigned priority
= 0;
3130 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
))) {
3132 if (pa_streq(s
, "sound"))
3134 else if (!pa_streq(s
, "modem"))
3138 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
3140 if (pa_streq(s
, "internal"))
3142 else if (pa_streq(s
, "speaker"))
3144 else if (pa_streq(s
, "headphone"))
3148 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
))) {
3150 if (pa_streq(s
, "pci"))
3152 else if (pa_streq(s
, "usb"))
3154 else if (pa_streq(s
, "bluetooth"))
3158 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
3160 if (pa_startswith(s
, "analog-"))
3162 else if (pa_startswith(s
, "iec958-"))
3169 PA_STATIC_FLIST_DECLARE(pa_sink_volume_change
, 0, pa_xfree
);
3171 /* Called from the IO thread. */
3172 static pa_sink_volume_change
*pa_sink_volume_change_new(pa_sink
*s
) {
3173 pa_sink_volume_change
*c
;
3174 if (!(c
= pa_flist_pop(PA_STATIC_FLIST_GET(pa_sink_volume_change
))))
3175 c
= pa_xnew(pa_sink_volume_change
, 1);
3177 PA_LLIST_INIT(pa_sink_volume_change
, c
);
3179 pa_cvolume_reset(&c
->hw_volume
, s
->sample_spec
.channels
);
3183 /* Called from the IO thread. */
3184 static void pa_sink_volume_change_free(pa_sink_volume_change
*c
) {
3186 if (pa_flist_push(PA_STATIC_FLIST_GET(pa_sink_volume_change
), c
) < 0)
3190 /* Called from the IO thread. */
3191 void pa_sink_volume_change_push(pa_sink
*s
) {
3192 pa_sink_volume_change
*c
= NULL
;
3193 pa_sink_volume_change
*nc
= NULL
;
3194 uint32_t safety_margin
= s
->thread_info
.volume_change_safety_margin
;
3196 const char *direction
= NULL
;
3199 nc
= pa_sink_volume_change_new(s
);
3201 /* NOTE: There is already more different volumes in pa_sink that I can remember.
3202 * Adding one more volume for HW would get us rid of this, but I am trying
3203 * to survive with the ones we already have. */
3204 pa_sw_cvolume_divide(&nc
->hw_volume
, &s
->real_volume
, &s
->soft_volume
);
3206 if (!s
->thread_info
.volume_changes
&& pa_cvolume_equal(&nc
->hw_volume
, &s
->thread_info
.current_hw_volume
)) {
3207 pa_log_debug("Volume not changing");
3208 pa_sink_volume_change_free(nc
);
3212 nc
->at
= pa_sink_get_latency_within_thread(s
);
3213 nc
->at
+= pa_rtclock_now() + s
->thread_info
.volume_change_extra_delay
;
3215 if (s
->thread_info
.volume_changes_tail
) {
3216 for (c
= s
->thread_info
.volume_changes_tail
; c
; c
= c
->prev
) {
3217 /* If volume is going up let's do it a bit late. If it is going
3218 * down let's do it a bit early. */
3219 if (pa_cvolume_avg(&nc
->hw_volume
) > pa_cvolume_avg(&c
->hw_volume
)) {
3220 if (nc
->at
+ safety_margin
> c
->at
) {
3221 nc
->at
+= safety_margin
;
3226 else if (nc
->at
- safety_margin
> c
->at
) {
3227 nc
->at
-= safety_margin
;
3235 if (pa_cvolume_avg(&nc
->hw_volume
) > pa_cvolume_avg(&s
->thread_info
.current_hw_volume
)) {
3236 nc
->at
+= safety_margin
;
3239 nc
->at
-= safety_margin
;
3242 PA_LLIST_PREPEND(pa_sink_volume_change
, s
->thread_info
.volume_changes
, nc
);
3245 PA_LLIST_INSERT_AFTER(pa_sink_volume_change
, s
->thread_info
.volume_changes
, c
, nc
);
3248 pa_log_debug("Volume going %s to %d at %llu", direction
, pa_cvolume_avg(&nc
->hw_volume
), (long long unsigned) nc
->at
);
3250 /* We can ignore volume events that came earlier but should happen later than this. */
3251 PA_LLIST_FOREACH(c
, nc
->next
) {
3252 pa_log_debug("Volume change to %d at %llu was dropped", pa_cvolume_avg(&c
->hw_volume
), (long long unsigned) c
->at
);
3253 pa_sink_volume_change_free(c
);
3256 s
->thread_info
.volume_changes_tail
= nc
;
3259 /* Called from the IO thread. */
3260 static void pa_sink_volume_change_flush(pa_sink
*s
) {
3261 pa_sink_volume_change
*c
= s
->thread_info
.volume_changes
;
3263 s
->thread_info
.volume_changes
= NULL
;
3264 s
->thread_info
.volume_changes_tail
= NULL
;
3266 pa_sink_volume_change
*next
= c
->next
;
3267 pa_sink_volume_change_free(c
);
3272 /* Called from the IO thread. */
3273 pa_bool_t
pa_sink_volume_change_apply(pa_sink
*s
, pa_usec_t
*usec_to_next
) {
3274 pa_usec_t now
= pa_rtclock_now();
3275 pa_bool_t ret
= FALSE
;
3278 pa_assert(s
->write_volume
);
3280 while (s
->thread_info
.volume_changes
&& now
>= s
->thread_info
.volume_changes
->at
) {
3281 pa_sink_volume_change
*c
= s
->thread_info
.volume_changes
;
3282 PA_LLIST_REMOVE(pa_sink_volume_change
, s
->thread_info
.volume_changes
, c
);
3283 pa_log_debug("Volume change to %d at %llu was written %llu usec late",
3284 pa_cvolume_avg(&c
->hw_volume
), (long long unsigned) c
->at
, (long long unsigned) (now
- c
->at
));
3286 s
->thread_info
.current_hw_volume
= c
->hw_volume
;
3287 pa_sink_volume_change_free(c
);
3290 if (s
->write_volume
&& ret
)
3293 if (s
->thread_info
.volume_changes
) {
3295 *usec_to_next
= s
->thread_info
.volume_changes
->at
- now
;
3296 if (pa_log_ratelimit(PA_LOG_DEBUG
))
3297 pa_log_debug("Next volume change in %lld usec", (long long) (s
->thread_info
.volume_changes
->at
- now
));
3302 s
->thread_info
.volume_changes_tail
= NULL
;
3307 /* Called from the IO thread. */
3308 static void pa_sink_volume_change_rewind(pa_sink
*s
, size_t nbytes
) {
3309 /* All the queued volume events later than current latency are shifted to happen earlier. */
3310 pa_sink_volume_change
*c
;
3311 pa_volume_t prev_vol
= pa_cvolume_avg(&s
->thread_info
.current_hw_volume
);
3312 pa_usec_t rewound
= pa_bytes_to_usec(nbytes
, &s
->sample_spec
);
3313 pa_usec_t limit
= pa_sink_get_latency_within_thread(s
);
3315 pa_log_debug("latency = %lld", (long long) limit
);
3316 limit
+= pa_rtclock_now() + s
->thread_info
.volume_change_extra_delay
;
3318 PA_LLIST_FOREACH(c
, s
->thread_info
.volume_changes
) {
3319 pa_usec_t modified_limit
= limit
;
3320 if (prev_vol
> pa_cvolume_avg(&c
->hw_volume
))
3321 modified_limit
-= s
->thread_info
.volume_change_safety_margin
;
3323 modified_limit
+= s
->thread_info
.volume_change_safety_margin
;
3324 if (c
->at
> modified_limit
) {
3326 if (c
->at
< modified_limit
)
3327 c
->at
= modified_limit
;
3329 prev_vol
= pa_cvolume_avg(&c
->hw_volume
);
3331 pa_sink_volume_change_apply(s
, NULL
);
3334 /* Called from the main thread */
3335 /* Gets the list of formats supported by the sink. The members and idxset must
3336 * be freed by the caller. */
3337 pa_idxset
* pa_sink_get_formats(pa_sink
*s
) {
3342 if (s
->get_formats
) {
3343 /* Sink supports format query, all is good */
3344 ret
= s
->get_formats(s
);
3346 /* Sink doesn't support format query, so assume it does PCM */
3347 pa_format_info
*f
= pa_format_info_new();
3348 f
->encoding
= PA_ENCODING_PCM
;
3350 ret
= pa_idxset_new(NULL
, NULL
);
3351 pa_idxset_put(ret
, f
, NULL
);
3357 /* Called from the main thread */
3358 /* Checks if the sink can accept this format */
3359 pa_bool_t
pa_sink_check_format(pa_sink
*s
, pa_format_info
*f
)
3361 pa_idxset
*formats
= NULL
;
3362 pa_bool_t ret
= FALSE
;
3367 formats
= pa_sink_get_formats(s
);
3370 pa_format_info
*finfo_device
;
3373 PA_IDXSET_FOREACH(finfo_device
, formats
, i
) {
3374 if (pa_format_info_is_compatible(finfo_device
, f
)) {
3380 pa_idxset_free(formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);
3386 /* Called from the main thread */
3387 /* Calculates the intersection between formats supported by the sink and
3388 * in_formats, and returns these, in the order of the sink's formats. */
3389 pa_idxset
* pa_sink_check_formats(pa_sink
*s
, pa_idxset
*in_formats
) {
3390 pa_idxset
*out_formats
= pa_idxset_new(NULL
, NULL
), *sink_formats
= NULL
;
3391 pa_format_info
*f_sink
, *f_in
;
3396 if (!in_formats
|| pa_idxset_isempty(in_formats
))
3399 sink_formats
= pa_sink_get_formats(s
);
3401 PA_IDXSET_FOREACH(f_sink
, sink_formats
, i
) {
3402 PA_IDXSET_FOREACH(f_in
, in_formats
, j
) {
3403 if (pa_format_info_is_compatible(f_sink
, f_in
))
3404 pa_idxset_put(out_formats
, pa_format_info_copy(f_in
), NULL
);
3410 pa_idxset_free(sink_formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);