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/format.h>
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
35 #include <pulse/timeval.h>
36 #include <pulse/util.h>
37 #include <pulse/rtclock.h>
38 #include <pulse/internal.h>
40 #include <pulsecore/i18n.h>
41 #include <pulsecore/sink-input.h>
42 #include <pulsecore/namereg.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/sample-util.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
48 #include <pulsecore/play-memblockq.h>
49 #include <pulsecore/flist.h>
53 #define MAX_MIX_CHANNELS 32
54 #define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
55 #define ABSOLUTE_MIN_LATENCY (500)
56 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
57 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
59 PA_DEFINE_PUBLIC_CLASS(pa_sink
, pa_msgobject
);
61 struct pa_sink_volume_change
{
65 PA_LLIST_FIELDS(pa_sink_volume_change
);
68 struct sink_message_set_port
{
73 static void sink_free(pa_object
*s
);
75 static void pa_sink_volume_change_push(pa_sink
*s
);
76 static void pa_sink_volume_change_flush(pa_sink
*s
);
77 static void pa_sink_volume_change_rewind(pa_sink
*s
, size_t nbytes
);
79 pa_sink_new_data
* pa_sink_new_data_init(pa_sink_new_data
*data
) {
83 data
->proplist
= pa_proplist_new();
88 void pa_sink_new_data_set_name(pa_sink_new_data
*data
, const char *name
) {
92 data
->name
= pa_xstrdup(name
);
95 void pa_sink_new_data_set_sample_spec(pa_sink_new_data
*data
, const pa_sample_spec
*spec
) {
98 if ((data
->sample_spec_is_set
= !!spec
))
99 data
->sample_spec
= *spec
;
102 void pa_sink_new_data_set_channel_map(pa_sink_new_data
*data
, const pa_channel_map
*map
) {
105 if ((data
->channel_map_is_set
= !!map
))
106 data
->channel_map
= *map
;
109 void pa_sink_new_data_set_alternate_sample_rate(pa_sink_new_data
*data
, const uint32_t alternate_sample_rate
) {
112 data
->alternate_sample_rate_is_set
= TRUE
;
113 data
->alternate_sample_rate
= alternate_sample_rate
;
116 void pa_sink_new_data_set_volume(pa_sink_new_data
*data
, const pa_cvolume
*volume
) {
119 if ((data
->volume_is_set
= !!volume
))
120 data
->volume
= *volume
;
123 void pa_sink_new_data_set_muted(pa_sink_new_data
*data
, pa_bool_t mute
) {
126 data
->muted_is_set
= TRUE
;
127 data
->muted
= !!mute
;
130 void pa_sink_new_data_set_port(pa_sink_new_data
*data
, const char *port
) {
133 pa_xfree(data
->active_port
);
134 data
->active_port
= pa_xstrdup(port
);
137 void pa_sink_new_data_done(pa_sink_new_data
*data
) {
140 pa_proplist_free(data
->proplist
);
145 while ((p
= pa_hashmap_steal_first(data
->ports
)))
146 pa_device_port_free(p
);
148 pa_hashmap_free(data
->ports
, NULL
, NULL
);
151 pa_xfree(data
->name
);
152 pa_xfree(data
->active_port
);
155 pa_device_port
*pa_device_port_new(const char *name
, const char *description
, size_t extra
) {
160 p
= pa_xmalloc(PA_ALIGN(sizeof(pa_device_port
)) + extra
);
161 p
->name
= pa_xstrdup(name
);
162 p
->description
= pa_xstrdup(description
);
169 void pa_device_port_free(pa_device_port
*p
) {
173 pa_xfree(p
->description
);
177 /* Called from main context */
178 static void reset_callbacks(pa_sink
*s
) {
182 s
->get_volume
= NULL
;
183 s
->set_volume
= NULL
;
184 s
->write_volume
= NULL
;
187 s
->request_rewind
= NULL
;
188 s
->update_requested_latency
= NULL
;
190 s
->get_formats
= NULL
;
191 s
->set_formats
= NULL
;
192 s
->update_rate
= NULL
;
195 /* Called from main context */
196 pa_sink
* pa_sink_new(
198 pa_sink_new_data
*data
,
199 pa_sink_flags_t flags
) {
203 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
], cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
204 pa_source_new_data source_data
;
210 pa_assert(data
->name
);
211 pa_assert_ctl_context();
213 s
= pa_msgobject_new(pa_sink
);
215 if (!(name
= pa_namereg_register(core
, data
->name
, PA_NAMEREG_SINK
, s
, data
->namereg_fail
))) {
216 pa_log_debug("Failed to register name %s.", data
->name
);
221 pa_sink_new_data_set_name(data
, name
);
223 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_NEW
], data
) < 0) {
225 pa_namereg_unregister(core
, name
);
229 /* FIXME, need to free s here on failure */
231 pa_return_null_if_fail(!data
->driver
|| pa_utf8_valid(data
->driver
));
232 pa_return_null_if_fail(data
->name
&& pa_utf8_valid(data
->name
) && data
->name
[0]);
234 pa_return_null_if_fail(data
->sample_spec_is_set
&& pa_sample_spec_valid(&data
->sample_spec
));
236 if (!data
->channel_map_is_set
)
237 pa_return_null_if_fail(pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
));
239 pa_return_null_if_fail(pa_channel_map_valid(&data
->channel_map
));
240 pa_return_null_if_fail(data
->channel_map
.channels
== data
->sample_spec
.channels
);
242 /* FIXME: There should probably be a general function for checking whether
243 * the sink volume is allowed to be set, like there is for sink inputs. */
244 pa_assert(!data
->volume_is_set
|| !(flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
246 if (!data
->volume_is_set
) {
247 pa_cvolume_reset(&data
->volume
, data
->sample_spec
.channels
);
248 data
->save_volume
= FALSE
;
251 pa_return_null_if_fail(pa_cvolume_valid(&data
->volume
));
252 pa_return_null_if_fail(pa_cvolume_compatible(&data
->volume
, &data
->sample_spec
));
254 if (!data
->muted_is_set
)
258 pa_proplist_update(data
->proplist
, PA_UPDATE_MERGE
, data
->card
->proplist
);
260 pa_device_init_description(data
->proplist
);
261 pa_device_init_icon(data
->proplist
, TRUE
);
262 pa_device_init_intended_roles(data
->proplist
);
264 if (pa_hook_fire(&core
->hooks
[PA_CORE_HOOK_SINK_FIXATE
], data
) < 0) {
266 pa_namereg_unregister(core
, name
);
270 s
->parent
.parent
.free
= sink_free
;
271 s
->parent
.process_msg
= pa_sink_process_msg
;
274 s
->state
= PA_SINK_INIT
;
277 s
->suspend_cause
= 0;
278 s
->name
= pa_xstrdup(name
);
279 s
->proplist
= pa_proplist_copy(data
->proplist
);
280 s
->driver
= pa_xstrdup(pa_path_get_filename(data
->driver
));
281 s
->module
= data
->module
;
282 s
->card
= data
->card
;
284 s
->priority
= pa_device_init_priority(s
->proplist
);
286 s
->sample_spec
= data
->sample_spec
;
287 s
->channel_map
= data
->channel_map
;
288 s
->default_sample_rate
= s
->sample_spec
.rate
;
290 if (data
->alternate_sample_rate_is_set
)
291 s
->alternate_sample_rate
= data
->alternate_sample_rate
;
293 s
->alternate_sample_rate
= s
->core
->alternate_sample_rate
;
295 if (s
->sample_spec
.rate
== s
->alternate_sample_rate
) {
296 pa_log_warn("Default and alternate sample rates are the same.");
297 s
->alternate_sample_rate
= 0;
300 s
->inputs
= pa_idxset_new(NULL
, NULL
);
302 s
->input_to_master
= NULL
;
304 s
->reference_volume
= s
->real_volume
= data
->volume
;
305 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
306 s
->base_volume
= PA_VOLUME_NORM
;
307 s
->n_volume_steps
= PA_VOLUME_NORM
+1;
308 s
->muted
= data
->muted
;
309 s
->refresh_volume
= s
->refresh_muted
= FALSE
;
316 /* As a minor optimization we just steal the list instead of
318 s
->ports
= data
->ports
;
321 s
->active_port
= NULL
;
322 s
->save_port
= FALSE
;
324 if (data
->active_port
&& s
->ports
)
325 if ((s
->active_port
= pa_hashmap_get(s
->ports
, data
->active_port
)))
326 s
->save_port
= data
->save_port
;
328 if (!s
->active_port
&& s
->ports
) {
332 PA_HASHMAP_FOREACH(p
, s
->ports
, state
)
333 if (!s
->active_port
|| p
->priority
> s
->active_port
->priority
)
337 s
->save_volume
= data
->save_volume
;
338 s
->save_muted
= data
->save_muted
;
340 pa_silence_memchunk_get(
341 &core
->silence_cache
,
347 s
->thread_info
.rtpoll
= NULL
;
348 s
->thread_info
.inputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
349 s
->thread_info
.soft_volume
= s
->soft_volume
;
350 s
->thread_info
.soft_muted
= s
->muted
;
351 s
->thread_info
.state
= s
->state
;
352 s
->thread_info
.rewind_nbytes
= 0;
353 s
->thread_info
.rewind_requested
= FALSE
;
354 s
->thread_info
.max_rewind
= 0;
355 s
->thread_info
.max_request
= 0;
356 s
->thread_info
.requested_latency_valid
= FALSE
;
357 s
->thread_info
.requested_latency
= 0;
358 s
->thread_info
.min_latency
= ABSOLUTE_MIN_LATENCY
;
359 s
->thread_info
.max_latency
= ABSOLUTE_MAX_LATENCY
;
360 s
->thread_info
.fixed_latency
= flags
& PA_SINK_DYNAMIC_LATENCY
? 0 : DEFAULT_FIXED_LATENCY
;
362 PA_LLIST_HEAD_INIT(pa_sink_volume_change
, s
->thread_info
.volume_changes
);
363 s
->thread_info
.volume_changes_tail
= NULL
;
364 pa_sw_cvolume_multiply(&s
->thread_info
.current_hw_volume
, &s
->soft_volume
, &s
->real_volume
);
365 s
->thread_info
.volume_change_safety_margin
= core
->deferred_volume_safety_margin_usec
;
366 s
->thread_info
.volume_change_extra_delay
= core
->deferred_volume_extra_delay_usec
;
368 /* FIXME: This should probably be moved to pa_sink_put() */
369 pa_assert_se(pa_idxset_put(core
->sinks
, s
, &s
->index
) >= 0);
372 pa_assert_se(pa_idxset_put(s
->card
->sinks
, s
, NULL
) >= 0);
374 pt
= pa_proplist_to_string_sep(s
->proplist
, "\n ");
375 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
378 pa_sample_spec_snprint(st
, sizeof(st
), &s
->sample_spec
),
379 pa_channel_map_snprint(cm
, sizeof(cm
), &s
->channel_map
),
383 pa_source_new_data_init(&source_data
);
384 pa_source_new_data_set_sample_spec(&source_data
, &s
->sample_spec
);
385 pa_source_new_data_set_channel_map(&source_data
, &s
->channel_map
);
386 pa_source_new_data_set_alternate_sample_rate(&source_data
, s
->alternate_sample_rate
);
387 source_data
.name
= pa_sprintf_malloc("%s.monitor", name
);
388 source_data
.driver
= data
->driver
;
389 source_data
.module
= data
->module
;
390 source_data
.card
= data
->card
;
392 dn
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
393 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Monitor of %s", dn
? dn
: s
->name
);
394 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "monitor");
396 s
->monitor_source
= pa_source_new(core
, &source_data
,
397 ((flags
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
398 ((flags
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0));
400 pa_source_new_data_done(&source_data
);
402 if (!s
->monitor_source
) {
408 s
->monitor_source
->monitor_of
= s
;
410 pa_source_set_latency_range(s
->monitor_source
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
411 pa_source_set_fixed_latency(s
->monitor_source
, s
->thread_info
.fixed_latency
);
412 pa_source_set_max_rewind(s
->monitor_source
, s
->thread_info
.max_rewind
);
417 /* Called from main context */
418 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
420 pa_bool_t suspend_change
;
421 pa_sink_state_t original_state
;
424 pa_assert_ctl_context();
426 if (s
->state
== state
)
429 original_state
= s
->state
;
432 (original_state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(state
)) ||
433 (PA_SINK_IS_OPENED(original_state
) && state
== PA_SINK_SUSPENDED
);
436 if ((ret
= s
->set_state(s
, state
)) < 0)
440 if ((ret
= pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), 0, NULL
)) < 0) {
443 s
->set_state(s
, original_state
);
450 if (state
!= PA_SINK_UNLINKED
) { /* if we enter UNLINKED state pa_sink_unlink() will fire the appropriate events */
451 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], s
);
452 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
455 if (suspend_change
) {
459 /* We're suspending or resuming, tell everyone about it */
461 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
)
462 if (s
->state
== PA_SINK_SUSPENDED
&&
463 (i
->flags
& PA_SINK_INPUT_KILL_ON_SUSPEND
))
464 pa_sink_input_kill(i
);
466 i
->suspend(i
, state
== PA_SINK_SUSPENDED
);
468 if (s
->monitor_source
)
469 pa_source_sync_suspend(s
->monitor_source
);
475 void pa_sink_set_get_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
481 void pa_sink_set_set_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
482 pa_sink_flags_t flags
;
485 pa_assert(!s
->write_volume
|| cb
);
489 /* Save the current flags so we can tell if they've changed */
493 /* The sink implementor is responsible for setting decibel volume support */
494 s
->flags
|= PA_SINK_HW_VOLUME_CTRL
;
496 s
->flags
&= ~PA_SINK_HW_VOLUME_CTRL
;
497 /* See note below in pa_sink_put() about volume sharing and decibel volumes */
498 pa_sink_enable_decibel_volume(s
, !(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
501 /* If the flags have changed after init, let any clients know via a change event */
502 if (s
->state
!= PA_SINK_INIT
&& flags
!= s
->flags
)
503 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
506 void pa_sink_set_write_volume_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
507 pa_sink_flags_t flags
;
510 pa_assert(!cb
|| s
->set_volume
);
512 s
->write_volume
= cb
;
514 /* Save the current flags so we can tell if they've changed */
518 s
->flags
|= PA_SINK_DEFERRED_VOLUME
;
520 s
->flags
&= ~PA_SINK_DEFERRED_VOLUME
;
522 /* If the flags have changed after init, let any clients know via a change event */
523 if (s
->state
!= PA_SINK_INIT
&& flags
!= s
->flags
)
524 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
527 void pa_sink_set_get_mute_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
533 void pa_sink_set_set_mute_callback(pa_sink
*s
, pa_sink_cb_t cb
) {
534 pa_sink_flags_t flags
;
540 /* Save the current flags so we can tell if they've changed */
544 s
->flags
|= PA_SINK_HW_MUTE_CTRL
;
546 s
->flags
&= ~PA_SINK_HW_MUTE_CTRL
;
548 /* If the flags have changed after init, let any clients know via a change event */
549 if (s
->state
!= PA_SINK_INIT
&& flags
!= s
->flags
)
550 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
553 static void enable_flat_volume(pa_sink
*s
, pa_bool_t enable
) {
554 pa_sink_flags_t flags
;
558 /* Always follow the overall user preference here */
559 enable
= enable
&& s
->core
->flat_volumes
;
561 /* Save the current flags so we can tell if they've changed */
565 s
->flags
|= PA_SINK_FLAT_VOLUME
;
567 s
->flags
&= ~PA_SINK_FLAT_VOLUME
;
569 /* If the flags have changed after init, let any clients know via a change event */
570 if (s
->state
!= PA_SINK_INIT
&& flags
!= s
->flags
)
571 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
574 void pa_sink_enable_decibel_volume(pa_sink
*s
, pa_bool_t enable
) {
575 pa_sink_flags_t flags
;
579 /* Save the current flags so we can tell if they've changed */
583 s
->flags
|= PA_SINK_DECIBEL_VOLUME
;
584 enable_flat_volume(s
, TRUE
);
586 s
->flags
&= ~PA_SINK_DECIBEL_VOLUME
;
587 enable_flat_volume(s
, FALSE
);
590 /* If the flags have changed after init, let any clients know via a change event */
591 if (s
->state
!= PA_SINK_INIT
&& flags
!= s
->flags
)
592 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
595 /* Called from main context */
596 void pa_sink_put(pa_sink
* s
) {
597 pa_sink_assert_ref(s
);
598 pa_assert_ctl_context();
600 pa_assert(s
->state
== PA_SINK_INIT
);
601 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) || s
->input_to_master
);
603 /* The following fields must be initialized properly when calling _put() */
604 pa_assert(s
->asyncmsgq
);
605 pa_assert(s
->thread_info
.min_latency
<= s
->thread_info
.max_latency
);
607 /* Generally, flags should be initialized via pa_sink_new(). As a
608 * special exception we allow some volume related flags to be set
609 * between _new() and _put() by the callback setter functions above.
611 * Thus we implement a couple safeguards here which ensure the above
612 * setters were used (or at least the implementor made manual changes
613 * in a compatible way).
615 * Note: All of these flags set here can change over the life time
617 pa_assert(!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
) || s
->set_volume
);
618 pa_assert(!(s
->flags
& PA_SINK_DEFERRED_VOLUME
) || s
->write_volume
);
619 pa_assert(!(s
->flags
& PA_SINK_HW_MUTE_CTRL
) || s
->set_mute
);
621 /* XXX: Currently decibel volume is disabled for all sinks that use volume
622 * sharing. When the master sink supports decibel volume, it would be good
623 * to have the flag also in the filter sink, but currently we don't do that
624 * so that the flags of the filter sink never change when it's moved from
625 * a master sink to another. One solution for this problem would be to
626 * remove user-visible volume altogether from filter sinks when volume
627 * sharing is used, but the current approach was easier to implement... */
628 /* We always support decibel volumes in software, otherwise we leave it to
629 * the sink implementor to set this flag as needed.
631 * Note: This flag can also change over the life time of the sink. */
632 if (!(s
->flags
& PA_SINK_HW_VOLUME_CTRL
) && !(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
633 pa_sink_enable_decibel_volume(s
, TRUE
);
635 /* If the sink implementor support DB volumes by itself, we should always
636 * try and enable flat volumes too */
637 if ((s
->flags
& PA_SINK_DECIBEL_VOLUME
))
638 enable_flat_volume(s
, TRUE
);
640 if (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) {
641 pa_sink
*root_sink
= pa_sink_get_master(s
);
643 pa_assert(root_sink
);
645 s
->reference_volume
= root_sink
->reference_volume
;
646 pa_cvolume_remap(&s
->reference_volume
, &root_sink
->channel_map
, &s
->channel_map
);
648 s
->real_volume
= root_sink
->real_volume
;
649 pa_cvolume_remap(&s
->real_volume
, &root_sink
->channel_map
, &s
->channel_map
);
651 /* We assume that if the sink implementor changed the default
652 * volume he did so in real_volume, because that is the usual
653 * place where he is supposed to place his changes. */
654 s
->reference_volume
= s
->real_volume
;
656 s
->thread_info
.soft_volume
= s
->soft_volume
;
657 s
->thread_info
.soft_muted
= s
->muted
;
658 pa_sw_cvolume_multiply(&s
->thread_info
.current_hw_volume
, &s
->soft_volume
, &s
->real_volume
);
660 pa_assert((s
->flags
& PA_SINK_HW_VOLUME_CTRL
)
661 || (s
->base_volume
== PA_VOLUME_NORM
662 && ((s
->flags
& PA_SINK_DECIBEL_VOLUME
|| (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)))));
663 pa_assert(!(s
->flags
& PA_SINK_DECIBEL_VOLUME
) || s
->n_volume_steps
== PA_VOLUME_NORM
+1);
664 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == (s
->thread_info
.fixed_latency
!= 0));
665 pa_assert(!(s
->flags
& PA_SINK_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_LATENCY
));
666 pa_assert(!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
) == !(s
->monitor_source
->flags
& PA_SOURCE_DYNAMIC_LATENCY
));
668 pa_assert(s
->monitor_source
->thread_info
.fixed_latency
== s
->thread_info
.fixed_latency
);
669 pa_assert(s
->monitor_source
->thread_info
.min_latency
== s
->thread_info
.min_latency
);
670 pa_assert(s
->monitor_source
->thread_info
.max_latency
== s
->thread_info
.max_latency
);
672 pa_assert_se(sink_set_state(s
, PA_SINK_IDLE
) == 0);
674 pa_source_put(s
->monitor_source
);
676 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
677 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], s
);
680 /* Called from main context */
681 void pa_sink_unlink(pa_sink
* s
) {
683 pa_sink_input
*i
, *j
= NULL
;
686 pa_assert_ctl_context();
688 /* Please note that pa_sink_unlink() does more than simply
689 * reversing pa_sink_put(). It also undoes the registrations
690 * already done in pa_sink_new()! */
692 /* All operations here shall be idempotent, i.e. pa_sink_unlink()
693 * may be called multiple times on the same sink without bad
696 linked
= PA_SINK_IS_LINKED(s
->state
);
699 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], s
);
701 if (s
->state
!= PA_SINK_UNLINKED
)
702 pa_namereg_unregister(s
->core
, s
->name
);
703 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
706 pa_idxset_remove_by_data(s
->card
->sinks
, s
, NULL
);
708 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
710 pa_sink_input_kill(i
);
715 sink_set_state(s
, PA_SINK_UNLINKED
);
717 s
->state
= PA_SINK_UNLINKED
;
721 if (s
->monitor_source
)
722 pa_source_unlink(s
->monitor_source
);
725 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
726 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], s
);
730 /* Called from main context */
731 static void sink_free(pa_object
*o
) {
732 pa_sink
*s
= PA_SINK(o
);
736 pa_assert_ctl_context();
737 pa_assert(pa_sink_refcnt(s
) == 0);
739 if (PA_SINK_IS_LINKED(s
->state
))
742 pa_log_info("Freeing sink %u \"%s\"", s
->index
, s
->name
);
744 if (s
->monitor_source
) {
745 pa_source_unref(s
->monitor_source
);
746 s
->monitor_source
= NULL
;
749 pa_idxset_free(s
->inputs
, NULL
, NULL
);
751 while ((i
= pa_hashmap_steal_first(s
->thread_info
.inputs
)))
752 pa_sink_input_unref(i
);
754 pa_hashmap_free(s
->thread_info
.inputs
, NULL
, NULL
);
756 if (s
->silence
.memblock
)
757 pa_memblock_unref(s
->silence
.memblock
);
763 pa_proplist_free(s
->proplist
);
768 while ((p
= pa_hashmap_steal_first(s
->ports
)))
769 pa_device_port_free(p
);
771 pa_hashmap_free(s
->ports
, NULL
, NULL
);
777 /* Called from main context, and not while the IO thread is active, please */
778 void pa_sink_set_asyncmsgq(pa_sink
*s
, pa_asyncmsgq
*q
) {
779 pa_sink_assert_ref(s
);
780 pa_assert_ctl_context();
784 if (s
->monitor_source
)
785 pa_source_set_asyncmsgq(s
->monitor_source
, q
);
788 /* Called from main context, and not while the IO thread is active, please */
789 void pa_sink_update_flags(pa_sink
*s
, pa_sink_flags_t mask
, pa_sink_flags_t value
) {
790 pa_sink_assert_ref(s
);
791 pa_assert_ctl_context();
796 /* For now, allow only a minimal set of flags to be changed. */
797 pa_assert((mask
& ~(PA_SINK_DYNAMIC_LATENCY
|PA_SINK_LATENCY
)) == 0);
799 s
->flags
= (s
->flags
& ~mask
) | (value
& mask
);
801 pa_source_update_flags(s
->monitor_source
,
802 ((mask
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
803 ((mask
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SOURCE_DYNAMIC_LATENCY
: 0),
804 ((value
& PA_SINK_LATENCY
) ? PA_SOURCE_LATENCY
: 0) |
805 ((value
& PA_SINK_DYNAMIC_LATENCY
) ? PA_SINK_DYNAMIC_LATENCY
: 0));
808 /* Called from IO context, or before _put() from main context */
809 void pa_sink_set_rtpoll(pa_sink
*s
, pa_rtpoll
*p
) {
810 pa_sink_assert_ref(s
);
811 pa_sink_assert_io_context(s
);
813 s
->thread_info
.rtpoll
= p
;
815 if (s
->monitor_source
)
816 pa_source_set_rtpoll(s
->monitor_source
, p
);
819 /* Called from main context */
820 int pa_sink_update_status(pa_sink
*s
) {
821 pa_sink_assert_ref(s
);
822 pa_assert_ctl_context();
823 pa_assert(PA_SINK_IS_LINKED(s
->state
));
825 if (s
->state
== PA_SINK_SUSPENDED
)
828 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
831 /* Called from main context */
832 int pa_sink_suspend(pa_sink
*s
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
833 pa_sink_assert_ref(s
);
834 pa_assert_ctl_context();
835 pa_assert(PA_SINK_IS_LINKED(s
->state
));
836 pa_assert(cause
!= 0);
839 s
->suspend_cause
|= cause
;
840 s
->monitor_source
->suspend_cause
|= cause
;
842 s
->suspend_cause
&= ~cause
;
843 s
->monitor_source
->suspend_cause
&= ~cause
;
846 if ((pa_sink_get_state(s
) == PA_SINK_SUSPENDED
) == !!s
->suspend_cause
)
849 pa_log_debug("Suspend cause of sink %s is 0x%04x, %s", s
->name
, s
->suspend_cause
, s
->suspend_cause
? "suspending" : "resuming");
851 if (s
->suspend_cause
)
852 return sink_set_state(s
, PA_SINK_SUSPENDED
);
854 return sink_set_state(s
, pa_sink_used_by(s
) ? PA_SINK_RUNNING
: PA_SINK_IDLE
);
857 /* Called from main context */
858 pa_queue
*pa_sink_move_all_start(pa_sink
*s
, pa_queue
*q
) {
859 pa_sink_input
*i
, *n
;
862 pa_sink_assert_ref(s
);
863 pa_assert_ctl_context();
864 pa_assert(PA_SINK_IS_LINKED(s
->state
));
869 for (i
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); i
; i
= n
) {
870 n
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
));
872 pa_sink_input_ref(i
);
874 if (pa_sink_input_start_move(i
) >= 0)
877 pa_sink_input_unref(i
);
883 /* Called from main context */
884 void pa_sink_move_all_finish(pa_sink
*s
, pa_queue
*q
, pa_bool_t save
) {
887 pa_sink_assert_ref(s
);
888 pa_assert_ctl_context();
889 pa_assert(PA_SINK_IS_LINKED(s
->state
));
892 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
893 if (pa_sink_input_finish_move(i
, s
, save
) < 0)
894 pa_sink_input_fail_move(i
);
896 pa_sink_input_unref(i
);
899 pa_queue_free(q
, NULL
, NULL
);
902 /* Called from main context */
903 void pa_sink_move_all_fail(pa_queue
*q
) {
906 pa_assert_ctl_context();
909 while ((i
= PA_SINK_INPUT(pa_queue_pop(q
)))) {
910 pa_sink_input_fail_move(i
);
911 pa_sink_input_unref(i
);
914 pa_queue_free(q
, NULL
, NULL
);
917 /* Called from IO thread context */
918 void pa_sink_process_rewind(pa_sink
*s
, size_t nbytes
) {
922 pa_sink_assert_ref(s
);
923 pa_sink_assert_io_context(s
);
924 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
926 /* If nobody requested this and this is actually no real rewind
927 * then we can short cut this. Please note that this means that
928 * not all rewind requests triggered upstream will always be
929 * translated in actual requests! */
930 if (!s
->thread_info
.rewind_requested
&& nbytes
<= 0)
933 s
->thread_info
.rewind_nbytes
= 0;
934 s
->thread_info
.rewind_requested
= FALSE
;
936 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
940 pa_log_debug("Processing rewind...");
941 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
)
942 pa_sink_volume_change_rewind(s
, nbytes
);
945 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
946 pa_sink_input_assert_ref(i
);
947 pa_sink_input_process_rewind(i
, nbytes
);
951 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
952 pa_source_process_rewind(s
->monitor_source
, nbytes
);
956 /* Called from IO thread context */
957 static unsigned fill_mix_info(pa_sink
*s
, size_t *length
, pa_mix_info
*info
, unsigned maxinfo
) {
961 size_t mixlength
= *length
;
963 pa_sink_assert_ref(s
);
964 pa_sink_assert_io_context(s
);
967 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)) && maxinfo
> 0) {
968 pa_sink_input_assert_ref(i
);
970 pa_sink_input_peek(i
, *length
, &info
->chunk
, &info
->volume
);
972 if (mixlength
== 0 || info
->chunk
.length
< mixlength
)
973 mixlength
= info
->chunk
.length
;
975 if (pa_memblock_is_silence(info
->chunk
.memblock
)) {
976 pa_memblock_unref(info
->chunk
.memblock
);
980 info
->userdata
= pa_sink_input_ref(i
);
982 pa_assert(info
->chunk
.memblock
);
983 pa_assert(info
->chunk
.length
> 0);
996 /* Called from IO thread context */
997 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned n
, pa_memchunk
*result
) {
1001 unsigned n_unreffed
= 0;
1003 pa_sink_assert_ref(s
);
1004 pa_sink_assert_io_context(s
);
1006 pa_assert(result
->memblock
);
1007 pa_assert(result
->length
> 0);
1009 /* We optimize for the case where the order of the inputs has not changed */
1011 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
1013 pa_mix_info
* m
= NULL
;
1015 pa_sink_input_assert_ref(i
);
1017 /* Let's try to find the matching entry info the pa_mix_info array */
1018 for (j
= 0; j
< n
; j
++) {
1020 if (info
[p
].userdata
== i
) {
1030 /* Drop read data */
1031 pa_sink_input_drop(i
, result
->length
);
1033 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
)) {
1035 if (pa_hashmap_size(i
->thread_info
.direct_outputs
) > 0) {
1036 void *ostate
= NULL
;
1037 pa_source_output
*o
;
1040 if (m
&& m
->chunk
.memblock
) {
1042 pa_memblock_ref(c
.memblock
);
1043 pa_assert(result
->length
<= c
.length
);
1044 c
.length
= result
->length
;
1046 pa_memchunk_make_writable(&c
, 0);
1047 pa_volume_memchunk(&c
, &s
->sample_spec
, &m
->volume
);
1050 pa_memblock_ref(c
.memblock
);
1051 pa_assert(result
->length
<= c
.length
);
1052 c
.length
= result
->length
;
1055 while ((o
= pa_hashmap_iterate(i
->thread_info
.direct_outputs
, &ostate
, NULL
))) {
1056 pa_source_output_assert_ref(o
);
1057 pa_assert(o
->direct_on_input
== i
);
1058 pa_source_post_direct(s
->monitor_source
, o
, &c
);
1061 pa_memblock_unref(c
.memblock
);
1066 if (m
->chunk
.memblock
)
1067 pa_memblock_unref(m
->chunk
.memblock
);
1068 pa_memchunk_reset(&m
->chunk
);
1070 pa_sink_input_unref(m
->userdata
);
1077 /* Now drop references to entries that are included in the
1078 * pa_mix_info array but don't exist anymore */
1080 if (n_unreffed
< n
) {
1081 for (; n
> 0; info
++, n
--) {
1083 pa_sink_input_unref(info
->userdata
);
1084 if (info
->chunk
.memblock
)
1085 pa_memblock_unref(info
->chunk
.memblock
);
1089 if (s
->monitor_source
&& PA_SOURCE_IS_LINKED(s
->monitor_source
->thread_info
.state
))
1090 pa_source_post(s
->monitor_source
, result
);
1093 /* Called from IO thread context */
1094 void pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
1095 pa_mix_info info
[MAX_MIX_CHANNELS
];
1097 size_t block_size_max
;
1099 pa_sink_assert_ref(s
);
1100 pa_sink_assert_io_context(s
);
1101 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1102 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
1105 pa_assert(!s
->thread_info
.rewind_requested
);
1106 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1108 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1109 result
->memblock
= pa_memblock_ref(s
->silence
.memblock
);
1110 result
->index
= s
->silence
.index
;
1111 result
->length
= PA_MIN(s
->silence
.length
, length
);
1118 length
= pa_frame_align(MIX_BUFFER_LENGTH
, &s
->sample_spec
);
1120 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
1121 if (length
> block_size_max
)
1122 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
1124 pa_assert(length
> 0);
1126 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
1130 *result
= s
->silence
;
1131 pa_memblock_ref(result
->memblock
);
1133 if (result
->length
> length
)
1134 result
->length
= length
;
1136 } else if (n
== 1) {
1139 *result
= info
[0].chunk
;
1140 pa_memblock_ref(result
->memblock
);
1142 if (result
->length
> length
)
1143 result
->length
= length
;
1145 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1147 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
)) {
1148 pa_memblock_unref(result
->memblock
);
1149 pa_silence_memchunk_get(&s
->core
->silence_cache
,
1154 } else if (!pa_cvolume_is_norm(&volume
)) {
1155 pa_memchunk_make_writable(result
, 0);
1156 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
1160 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
1162 ptr
= pa_memblock_acquire(result
->memblock
);
1163 result
->length
= pa_mix(info
, n
,
1166 &s
->thread_info
.soft_volume
,
1167 s
->thread_info
.soft_muted
);
1168 pa_memblock_release(result
->memblock
);
1173 inputs_drop(s
, info
, n
, result
);
1178 /* Called from IO thread context */
1179 void pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
1180 pa_mix_info info
[MAX_MIX_CHANNELS
];
1182 size_t length
, block_size_max
;
1184 pa_sink_assert_ref(s
);
1185 pa_sink_assert_io_context(s
);
1186 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1188 pa_assert(target
->memblock
);
1189 pa_assert(target
->length
> 0);
1190 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
1192 pa_assert(!s
->thread_info
.rewind_requested
);
1193 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1195 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1196 pa_silence_memchunk(target
, &s
->sample_spec
);
1202 length
= target
->length
;
1203 block_size_max
= pa_mempool_block_size_max(s
->core
->mempool
);
1204 if (length
> block_size_max
)
1205 length
= pa_frame_align(block_size_max
, &s
->sample_spec
);
1207 pa_assert(length
> 0);
1209 n
= fill_mix_info(s
, &length
, info
, MAX_MIX_CHANNELS
);
1212 if (target
->length
> length
)
1213 target
->length
= length
;
1215 pa_silence_memchunk(target
, &s
->sample_spec
);
1216 } else if (n
== 1) {
1219 if (target
->length
> length
)
1220 target
->length
= length
;
1222 pa_sw_cvolume_multiply(&volume
, &s
->thread_info
.soft_volume
, &info
[0].volume
);
1224 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&volume
))
1225 pa_silence_memchunk(target
, &s
->sample_spec
);
1229 vchunk
= info
[0].chunk
;
1230 pa_memblock_ref(vchunk
.memblock
);
1232 if (vchunk
.length
> length
)
1233 vchunk
.length
= length
;
1235 if (!pa_cvolume_is_norm(&volume
)) {
1236 pa_memchunk_make_writable(&vchunk
, 0);
1237 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &volume
);
1240 pa_memchunk_memcpy(target
, &vchunk
);
1241 pa_memblock_unref(vchunk
.memblock
);
1247 ptr
= pa_memblock_acquire(target
->memblock
);
1249 target
->length
= pa_mix(info
, n
,
1250 (uint8_t*) ptr
+ target
->index
, length
,
1252 &s
->thread_info
.soft_volume
,
1253 s
->thread_info
.soft_muted
);
1255 pa_memblock_release(target
->memblock
);
1258 inputs_drop(s
, info
, n
, target
);
1263 /* Called from IO thread context */
1264 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
1268 pa_sink_assert_ref(s
);
1269 pa_sink_assert_io_context(s
);
1270 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1272 pa_assert(target
->memblock
);
1273 pa_assert(target
->length
> 0);
1274 pa_assert(pa_frame_aligned(target
->length
, &s
->sample_spec
));
1276 pa_assert(!s
->thread_info
.rewind_requested
);
1277 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1279 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
1280 pa_silence_memchunk(target
, &s
->sample_spec
);
1293 pa_sink_render_into(s
, &chunk
);
1302 /* Called from IO thread context */
1303 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
1304 pa_sink_assert_ref(s
);
1305 pa_sink_assert_io_context(s
);
1306 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1307 pa_assert(length
> 0);
1308 pa_assert(pa_frame_aligned(length
, &s
->sample_spec
));
1311 pa_assert(!s
->thread_info
.rewind_requested
);
1312 pa_assert(s
->thread_info
.rewind_nbytes
== 0);
1316 pa_sink_render(s
, length
, result
);
1318 if (result
->length
< length
) {
1321 pa_memchunk_make_writable(result
, length
);
1323 chunk
.memblock
= result
->memblock
;
1324 chunk
.index
= result
->index
+ result
->length
;
1325 chunk
.length
= length
- result
->length
;
1327 pa_sink_render_into_full(s
, &chunk
);
1329 result
->length
= length
;
1335 /* Called from main thread */
1336 pa_bool_t
pa_sink_update_rate(pa_sink
*s
, uint32_t rate
, pa_bool_t passthrough
)
1338 if (s
->update_rate
) {
1339 uint32_t desired_rate
= rate
;
1340 uint32_t default_rate
= s
->default_sample_rate
;
1341 uint32_t alternate_rate
= s
->alternate_sample_rate
;
1342 pa_bool_t use_alternate
= FALSE
;
1344 if (PA_UNLIKELY(default_rate
== alternate_rate
)) {
1345 pa_log_warn("Default and alternate sample rates are the same.");
1349 if (PA_SINK_IS_RUNNING(s
->state
)) {
1350 pa_log_info("Cannot update rate, SINK_IS_RUNNING, will keep using %u kHz",
1351 s
->sample_spec
.rate
);
1355 if (s
->monitor_source
) {
1356 if (PA_SOURCE_IS_RUNNING(s
->monitor_source
->state
) == TRUE
) {
1357 pa_log_info("Cannot update rate, monitor source is RUNNING");
1362 if (PA_UNLIKELY (desired_rate
< 8000 ||
1363 desired_rate
> PA_RATE_MAX
))
1367 pa_assert(default_rate
% 4000 || default_rate
% 11025);
1368 pa_assert(alternate_rate
% 4000 || alternate_rate
% 11025);
1370 if (default_rate
% 4000) {
1371 /* default is a 11025 multiple */
1372 if ((alternate_rate
% 4000 == 0) && (desired_rate
% 4000 == 0))
1375 /* default is 4000 multiple */
1376 if ((alternate_rate
% 11025 == 0) && (desired_rate
% 11025 == 0))
1381 desired_rate
= alternate_rate
;
1383 desired_rate
= default_rate
;
1385 desired_rate
= rate
; /* use stream sampling rate, discard default/alternate settings */
1388 if (passthrough
|| pa_sink_linked_by(s
) == 0) {
1389 pa_sink_suspend(s
, TRUE
, PA_SUSPEND_IDLE
); /* needed before rate update, will be resumed automatically */
1392 if (s
->update_rate(s
, desired_rate
) == TRUE
) {
1393 /* update monitor source as well */
1394 if (s
->monitor_source
&& !passthrough
)
1395 pa_source_update_rate(s
->monitor_source
, desired_rate
, FALSE
);
1396 pa_log_info("Changed sampling rate successfully");
1403 /* Called from main thread */
1404 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
1407 pa_sink_assert_ref(s
);
1408 pa_assert_ctl_context();
1409 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1411 /* The returned value is supposed to be in the time domain of the sound card! */
1413 if (s
->state
== PA_SINK_SUSPENDED
)
1416 if (!(s
->flags
& PA_SINK_LATENCY
))
1419 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) == 0);
1424 /* Called from IO thread */
1425 pa_usec_t
pa_sink_get_latency_within_thread(pa_sink
*s
) {
1429 pa_sink_assert_ref(s
);
1430 pa_sink_assert_io_context(s
);
1431 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
1433 /* The returned value is supposed to be in the time domain of the sound card! */
1435 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
1438 if (!(s
->flags
& PA_SINK_LATENCY
))
1441 o
= PA_MSGOBJECT(s
);
1443 /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
1445 if (o
->process_msg(o
, PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
1451 /* Called from the main thread (and also from the IO thread while the main
1452 * thread is waiting).
1454 * When a sink uses volume sharing, it never has the PA_SINK_FLAT_VOLUME flag
1455 * set. Instead, flat volume mode is detected by checking whether the root sink
1456 * has the flag set. */
1457 pa_bool_t
pa_sink_flat_volume_enabled(pa_sink
*s
) {
1458 pa_sink_assert_ref(s
);
1460 s
= pa_sink_get_master(s
);
1463 return (s
->flags
& PA_SINK_FLAT_VOLUME
);
1468 /* Called from the main thread (and also from the IO thread while the main
1469 * thread is waiting). */
1470 pa_sink
*pa_sink_get_master(pa_sink
*s
) {
1471 pa_sink_assert_ref(s
);
1473 while (s
&& (s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1474 if (PA_UNLIKELY(!s
->input_to_master
))
1477 s
= s
->input_to_master
->sink
;
1483 /* Called from main context */
1484 pa_bool_t
pa_sink_is_passthrough(pa_sink
*s
) {
1485 pa_sink_input
*alt_i
;
1488 pa_sink_assert_ref(s
);
1490 /* one and only one PASSTHROUGH input can possibly be connected */
1491 if (pa_idxset_size(s
->inputs
) == 1) {
1492 alt_i
= pa_idxset_first(s
->inputs
, &idx
);
1494 if (pa_sink_input_is_passthrough(alt_i
))
1501 /* Called from main context */
1502 void pa_sink_enter_passthrough(pa_sink
*s
) {
1505 /* disable the monitor in passthrough mode */
1506 if (s
->monitor_source
)
1507 pa_source_suspend(s
->monitor_source
, TRUE
, PA_SUSPEND_PASSTHROUGH
);
1509 /* set the volume to NORM */
1510 s
->saved_volume
= *pa_sink_get_volume(s
, TRUE
);
1511 s
->saved_save_volume
= s
->save_volume
;
1513 pa_cvolume_set(&volume
, s
->sample_spec
.channels
, PA_MIN(s
->base_volume
, PA_VOLUME_NORM
));
1514 pa_sink_set_volume(s
, &volume
, TRUE
, FALSE
);
1517 /* Called from main context */
1518 void pa_sink_leave_passthrough(pa_sink
*s
) {
1519 /* Unsuspend monitor */
1520 if (s
->monitor_source
)
1521 pa_source_suspend(s
->monitor_source
, FALSE
, PA_SUSPEND_PASSTHROUGH
);
1523 /* Restore sink volume to what it was before we entered passthrough mode */
1524 pa_sink_set_volume(s
, &s
->saved_volume
, TRUE
, s
->saved_save_volume
);
1526 pa_cvolume_init(&s
->saved_volume
);
1527 s
->saved_save_volume
= FALSE
;
1530 /* Called from main context. */
1531 static void compute_reference_ratio(pa_sink_input
*i
) {
1533 pa_cvolume remapped
;
1536 pa_assert(pa_sink_flat_volume_enabled(i
->sink
));
1539 * Calculates the reference ratio from the sink's reference
1540 * volume. This basically calculates:
1542 * i->reference_ratio = i->volume / i->sink->reference_volume
1545 remapped
= i
->sink
->reference_volume
;
1546 pa_cvolume_remap(&remapped
, &i
->sink
->channel_map
, &i
->channel_map
);
1548 i
->reference_ratio
.channels
= i
->sample_spec
.channels
;
1550 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1552 /* We don't update when the sink volume is 0 anyway */
1553 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
)
1556 /* Don't update the reference ratio unless necessary */
1557 if (pa_sw_volume_multiply(
1558 i
->reference_ratio
.values
[c
],
1559 remapped
.values
[c
]) == i
->volume
.values
[c
])
1562 i
->reference_ratio
.values
[c
] = pa_sw_volume_divide(
1563 i
->volume
.values
[c
],
1564 remapped
.values
[c
]);
1568 /* Called from main context. Only called for the root sink in volume sharing
1569 * cases, except for internal recursive calls. */
1570 static void compute_reference_ratios(pa_sink
*s
) {
1574 pa_sink_assert_ref(s
);
1575 pa_assert_ctl_context();
1576 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1577 pa_assert(pa_sink_flat_volume_enabled(s
));
1579 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1580 compute_reference_ratio(i
);
1582 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1583 compute_reference_ratios(i
->origin_sink
);
1587 /* Called from main context. Only called for the root sink in volume sharing
1588 * cases, except for internal recursive calls. */
1589 static void compute_real_ratios(pa_sink
*s
) {
1593 pa_sink_assert_ref(s
);
1594 pa_assert_ctl_context();
1595 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1596 pa_assert(pa_sink_flat_volume_enabled(s
));
1598 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1600 pa_cvolume remapped
;
1602 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1603 /* The origin sink uses volume sharing, so this input's real ratio
1604 * is handled as a special case - the real ratio must be 0 dB, and
1605 * as a result i->soft_volume must equal i->volume_factor. */
1606 pa_cvolume_reset(&i
->real_ratio
, i
->real_ratio
.channels
);
1607 i
->soft_volume
= i
->volume_factor
;
1609 compute_real_ratios(i
->origin_sink
);
1615 * This basically calculates:
1617 * i->real_ratio := i->volume / s->real_volume
1618 * i->soft_volume := i->real_ratio * i->volume_factor
1621 remapped
= s
->real_volume
;
1622 pa_cvolume_remap(&remapped
, &s
->channel_map
, &i
->channel_map
);
1624 i
->real_ratio
.channels
= i
->sample_spec
.channels
;
1625 i
->soft_volume
.channels
= i
->sample_spec
.channels
;
1627 for (c
= 0; c
< i
->sample_spec
.channels
; c
++) {
1629 if (remapped
.values
[c
] <= PA_VOLUME_MUTED
) {
1630 /* We leave i->real_ratio untouched */
1631 i
->soft_volume
.values
[c
] = PA_VOLUME_MUTED
;
1635 /* Don't lose accuracy unless necessary */
1636 if (pa_sw_volume_multiply(
1637 i
->real_ratio
.values
[c
],
1638 remapped
.values
[c
]) != i
->volume
.values
[c
])
1640 i
->real_ratio
.values
[c
] = pa_sw_volume_divide(
1641 i
->volume
.values
[c
],
1642 remapped
.values
[c
]);
1644 i
->soft_volume
.values
[c
] = pa_sw_volume_multiply(
1645 i
->real_ratio
.values
[c
],
1646 i
->volume_factor
.values
[c
]);
1649 /* We don't copy the soft_volume to the thread_info data
1650 * here. That must be done by the caller */
1654 static pa_cvolume
*cvolume_remap_minimal_impact(
1656 const pa_cvolume
*template,
1657 const pa_channel_map
*from
,
1658 const pa_channel_map
*to
) {
1663 pa_assert(template);
1666 pa_assert(pa_cvolume_compatible_with_channel_map(v
, from
));
1667 pa_assert(pa_cvolume_compatible_with_channel_map(template, to
));
1669 /* Much like pa_cvolume_remap(), but tries to minimize impact when
1670 * mapping from sink input to sink volumes:
1672 * If template is a possible remapping from v it is used instead
1673 * of remapping anew.
1675 * If the channel maps don't match we set an all-channel volume on
1676 * the sink to ensure that changing a volume on one stream has no
1677 * effect that cannot be compensated for in another stream that
1678 * does not have the same channel map as the sink. */
1680 if (pa_channel_map_equal(from
, to
))
1684 if (pa_cvolume_equal(pa_cvolume_remap(&t
, to
, from
), v
)) {
1689 pa_cvolume_set(v
, to
->channels
, pa_cvolume_max(v
));
1693 /* Called from main thread. Only called for the root sink in volume sharing
1694 * cases, except for internal recursive calls. */
1695 static void get_maximum_input_volume(pa_sink
*s
, pa_cvolume
*max_volume
, const pa_channel_map
*channel_map
) {
1699 pa_sink_assert_ref(s
);
1700 pa_assert(max_volume
);
1701 pa_assert(channel_map
);
1702 pa_assert(pa_sink_flat_volume_enabled(s
));
1704 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1705 pa_cvolume remapped
;
1707 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1708 get_maximum_input_volume(i
->origin_sink
, max_volume
, channel_map
);
1710 /* Ignore this input. The origin sink uses volume sharing, so this
1711 * input's volume will be set to be equal to the root sink's real
1712 * volume. Obviously this input's current volume must not then
1713 * affect what the root sink's real volume will be. */
1717 remapped
= i
->volume
;
1718 cvolume_remap_minimal_impact(&remapped
, max_volume
, &i
->channel_map
, channel_map
);
1719 pa_cvolume_merge(max_volume
, max_volume
, &remapped
);
1723 /* Called from main thread. Only called for the root sink in volume sharing
1724 * cases, except for internal recursive calls. */
1725 static pa_bool_t
has_inputs(pa_sink
*s
) {
1729 pa_sink_assert_ref(s
);
1731 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1732 if (!i
->origin_sink
|| !(i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
) || has_inputs(i
->origin_sink
))
1739 /* Called from main thread. Only called for the root sink in volume sharing
1740 * cases, except for internal recursive calls. */
1741 static void update_real_volume(pa_sink
*s
, const pa_cvolume
*new_volume
, pa_channel_map
*channel_map
) {
1745 pa_sink_assert_ref(s
);
1746 pa_assert(new_volume
);
1747 pa_assert(channel_map
);
1749 s
->real_volume
= *new_volume
;
1750 pa_cvolume_remap(&s
->real_volume
, channel_map
, &s
->channel_map
);
1752 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1753 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1754 if (pa_sink_flat_volume_enabled(s
)) {
1755 pa_cvolume old_volume
= i
->volume
;
1757 /* Follow the root sink's real volume. */
1758 i
->volume
= *new_volume
;
1759 pa_cvolume_remap(&i
->volume
, channel_map
, &i
->channel_map
);
1760 compute_reference_ratio(i
);
1762 /* The volume changed, let's tell people so */
1763 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1764 if (i
->volume_changed
)
1765 i
->volume_changed(i
);
1767 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1771 update_real_volume(i
->origin_sink
, new_volume
, channel_map
);
1776 /* Called from main thread. Only called for the root sink in shared volume
1778 static void compute_real_volume(pa_sink
*s
) {
1779 pa_sink_assert_ref(s
);
1780 pa_assert_ctl_context();
1781 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1782 pa_assert(pa_sink_flat_volume_enabled(s
));
1783 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
1785 /* This determines the maximum volume of all streams and sets
1786 * s->real_volume accordingly. */
1788 if (!has_inputs(s
)) {
1789 /* In the special case that we have no sink inputs we leave the
1790 * volume unmodified. */
1791 update_real_volume(s
, &s
->reference_volume
, &s
->channel_map
);
1795 pa_cvolume_mute(&s
->real_volume
, s
->channel_map
.channels
);
1797 /* First let's determine the new maximum volume of all inputs
1798 * connected to this sink */
1799 get_maximum_input_volume(s
, &s
->real_volume
, &s
->channel_map
);
1800 update_real_volume(s
, &s
->real_volume
, &s
->channel_map
);
1802 /* Then, let's update the real ratios/soft volumes of all inputs
1803 * connected to this sink */
1804 compute_real_ratios(s
);
1807 /* Called from main thread. Only called for the root sink in shared volume
1808 * cases, except for internal recursive calls. */
1809 static void propagate_reference_volume(pa_sink
*s
) {
1813 pa_sink_assert_ref(s
);
1814 pa_assert_ctl_context();
1815 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1816 pa_assert(pa_sink_flat_volume_enabled(s
));
1818 /* This is called whenever the sink volume changes that is not
1819 * caused by a sink input volume change. We need to fix up the
1820 * sink input volumes accordingly */
1822 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1823 pa_cvolume old_volume
;
1825 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
1826 propagate_reference_volume(i
->origin_sink
);
1828 /* Since the origin sink uses volume sharing, this input's volume
1829 * needs to be updated to match the root sink's real volume, but
1830 * that will be done later in update_shared_real_volume(). */
1834 old_volume
= i
->volume
;
1836 /* This basically calculates:
1838 * i->volume := s->reference_volume * i->reference_ratio */
1840 i
->volume
= s
->reference_volume
;
1841 pa_cvolume_remap(&i
->volume
, &s
->channel_map
, &i
->channel_map
);
1842 pa_sw_cvolume_multiply(&i
->volume
, &i
->volume
, &i
->reference_ratio
);
1844 /* The volume changed, let's tell people so */
1845 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
1847 if (i
->volume_changed
)
1848 i
->volume_changed(i
);
1850 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
1855 /* Called from main thread. Only called for the root sink in volume sharing
1856 * cases, except for internal recursive calls. The return value indicates
1857 * whether any reference volume actually changed. */
1858 static pa_bool_t
update_reference_volume(pa_sink
*s
, const pa_cvolume
*v
, const pa_channel_map
*channel_map
, pa_bool_t save
) {
1860 pa_bool_t reference_volume_changed
;
1864 pa_sink_assert_ref(s
);
1865 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1867 pa_assert(channel_map
);
1868 pa_assert(pa_cvolume_valid(v
));
1871 pa_cvolume_remap(&volume
, channel_map
, &s
->channel_map
);
1873 reference_volume_changed
= !pa_cvolume_equal(&volume
, &s
->reference_volume
);
1874 s
->reference_volume
= volume
;
1876 s
->save_volume
= (!reference_volume_changed
&& s
->save_volume
) || save
;
1878 if (reference_volume_changed
)
1879 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
1880 else if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1881 /* If the root sink's volume doesn't change, then there can't be any
1882 * changes in the other sinks in the sink tree either.
1884 * It's probably theoretically possible that even if the root sink's
1885 * volume changes slightly, some filter sink doesn't change its volume
1886 * due to rounding errors. If that happens, we still want to propagate
1887 * the changed root sink volume to the sinks connected to the
1888 * intermediate sink that didn't change its volume. This theoretical
1889 * possibility is the reason why we have that !(s->flags &
1890 * PA_SINK_SHARE_VOLUME_WITH_MASTER) condition. Probably nobody would
1891 * notice even if we returned here FALSE always if
1892 * reference_volume_changed is FALSE. */
1895 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
1896 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
1897 update_reference_volume(i
->origin_sink
, v
, channel_map
, FALSE
);
1903 /* Called from main thread */
1904 void pa_sink_set_volume(
1906 const pa_cvolume
*volume
,
1910 pa_cvolume new_reference_volume
;
1913 pa_sink_assert_ref(s
);
1914 pa_assert_ctl_context();
1915 pa_assert(PA_SINK_IS_LINKED(s
->state
));
1916 pa_assert(!volume
|| pa_cvolume_valid(volume
));
1917 pa_assert(volume
|| pa_sink_flat_volume_enabled(s
));
1918 pa_assert(!volume
|| volume
->channels
== 1 || pa_cvolume_compatible(volume
, &s
->sample_spec
));
1920 /* make sure we don't change the volume when a PASSTHROUGH input is connected ...
1921 * ... *except* if we're being invoked to reset the volume to ensure 0 dB gain */
1922 if (pa_sink_is_passthrough(s
) && (!volume
|| !pa_cvolume_is_norm(volume
))) {
1923 pa_log_warn("Cannot change volume, Sink is connected to PASSTHROUGH input");
1927 /* In case of volume sharing, the volume is set for the root sink first,
1928 * from which it's then propagated to the sharing sinks. */
1929 root_sink
= pa_sink_get_master(s
);
1931 if (PA_UNLIKELY(!root_sink
))
1934 /* As a special exception we accept mono volumes on all sinks --
1935 * even on those with more complex channel maps */
1938 if (pa_cvolume_compatible(volume
, &s
->sample_spec
))
1939 new_reference_volume
= *volume
;
1941 new_reference_volume
= s
->reference_volume
;
1942 pa_cvolume_scale(&new_reference_volume
, pa_cvolume_max(volume
));
1945 pa_cvolume_remap(&new_reference_volume
, &s
->channel_map
, &root_sink
->channel_map
);
1947 if (update_reference_volume(root_sink
, &new_reference_volume
, &root_sink
->channel_map
, save
)) {
1948 if (pa_sink_flat_volume_enabled(root_sink
)) {
1949 /* OK, propagate this volume change back to the inputs */
1950 propagate_reference_volume(root_sink
);
1952 /* And now recalculate the real volume */
1953 compute_real_volume(root_sink
);
1955 update_real_volume(root_sink
, &root_sink
->reference_volume
, &root_sink
->channel_map
);
1959 /* If volume is NULL we synchronize the sink's real and
1960 * reference volumes with the stream volumes. */
1962 pa_assert(pa_sink_flat_volume_enabled(root_sink
));
1964 /* Ok, let's determine the new real volume */
1965 compute_real_volume(root_sink
);
1967 /* Let's 'push' the reference volume if necessary */
1968 pa_cvolume_merge(&new_reference_volume
, &s
->reference_volume
, &root_sink
->real_volume
);
1969 /* If the sink and it's root don't have the same number of channels, we need to remap */
1970 if (s
!= root_sink
&& !pa_channel_map_equal(&s
->channel_map
, &root_sink
->channel_map
))
1971 pa_cvolume_remap(&new_reference_volume
, &s
->channel_map
, &root_sink
->channel_map
);
1972 update_reference_volume(root_sink
, &new_reference_volume
, &root_sink
->channel_map
, save
);
1974 /* Now that the reference volume is updated, we can update the streams'
1975 * reference ratios. */
1976 compute_reference_ratios(root_sink
);
1979 if (root_sink
->set_volume
) {
1980 /* If we have a function set_volume(), then we do not apply a
1981 * soft volume by default. However, set_volume() is free to
1982 * apply one to root_sink->soft_volume */
1984 pa_cvolume_reset(&root_sink
->soft_volume
, root_sink
->sample_spec
.channels
);
1985 if (!(root_sink
->flags
& PA_SINK_DEFERRED_VOLUME
))
1986 root_sink
->set_volume(root_sink
);
1989 /* If we have no function set_volume(), then the soft volume
1990 * becomes the real volume */
1991 root_sink
->soft_volume
= root_sink
->real_volume
;
1993 /* This tells the sink that soft volume and/or real volume changed */
1995 pa_assert_se(pa_asyncmsgq_send(root_sink
->asyncmsgq
, PA_MSGOBJECT(root_sink
), PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
) == 0);
1998 /* Called from the io thread if sync volume is used, otherwise from the main thread.
1999 * Only to be called by sink implementor */
2000 void pa_sink_set_soft_volume(pa_sink
*s
, const pa_cvolume
*volume
) {
2002 pa_sink_assert_ref(s
);
2003 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
2005 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
)
2006 pa_sink_assert_io_context(s
);
2008 pa_assert_ctl_context();
2011 pa_cvolume_reset(&s
->soft_volume
, s
->sample_spec
.channels
);
2013 s
->soft_volume
= *volume
;
2015 if (PA_SINK_IS_LINKED(s
->state
) && !(s
->flags
& PA_SINK_DEFERRED_VOLUME
))
2016 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME
, NULL
, 0, NULL
) == 0);
2018 s
->thread_info
.soft_volume
= s
->soft_volume
;
2021 /* Called from the main thread. Only called for the root sink in volume sharing
2022 * cases, except for internal recursive calls. */
2023 static void propagate_real_volume(pa_sink
*s
, const pa_cvolume
*old_real_volume
) {
2027 pa_sink_assert_ref(s
);
2028 pa_assert(old_real_volume
);
2029 pa_assert_ctl_context();
2030 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2032 /* This is called when the hardware's real volume changes due to
2033 * some external event. We copy the real volume into our
2034 * reference volume and then rebuild the stream volumes based on
2035 * i->real_ratio which should stay fixed. */
2037 if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
)) {
2038 if (pa_cvolume_equal(old_real_volume
, &s
->real_volume
))
2041 /* 1. Make the real volume the reference volume */
2042 update_reference_volume(s
, &s
->real_volume
, &s
->channel_map
, TRUE
);
2045 if (pa_sink_flat_volume_enabled(s
)) {
2047 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
2048 pa_cvolume old_volume
= i
->volume
;
2050 /* 2. Since the sink's reference and real volumes are equal
2051 * now our ratios should be too. */
2052 i
->reference_ratio
= i
->real_ratio
;
2054 /* 3. Recalculate the new stream reference volume based on the
2055 * reference ratio and the sink's reference volume.
2057 * This basically calculates:
2059 * i->volume = s->reference_volume * i->reference_ratio
2061 * This is identical to propagate_reference_volume() */
2062 i
->volume
= s
->reference_volume
;
2063 pa_cvolume_remap(&i
->volume
, &s
->channel_map
, &i
->channel_map
);
2064 pa_sw_cvolume_multiply(&i
->volume
, &i
->volume
, &i
->reference_ratio
);
2066 /* Notify if something changed */
2067 if (!pa_cvolume_equal(&old_volume
, &i
->volume
)) {
2069 if (i
->volume_changed
)
2070 i
->volume_changed(i
);
2072 pa_subscription_post(i
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
2075 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
2076 propagate_real_volume(i
->origin_sink
, old_real_volume
);
2080 /* Something got changed in the hardware. It probably makes sense
2081 * to save changed hw settings given that hw volume changes not
2082 * triggered by PA are almost certainly done by the user. */
2083 if (!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
2084 s
->save_volume
= TRUE
;
2087 /* Called from io thread */
2088 void pa_sink_update_volume_and_mute(pa_sink
*s
) {
2090 pa_sink_assert_io_context(s
);
2092 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE
, NULL
, 0, NULL
, NULL
);
2095 /* Called from main thread */
2096 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_bool_t force_refresh
) {
2097 pa_sink_assert_ref(s
);
2098 pa_assert_ctl_context();
2099 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2101 if (s
->refresh_volume
|| force_refresh
) {
2102 struct pa_cvolume old_real_volume
;
2104 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
2106 old_real_volume
= s
->real_volume
;
2108 if (!(s
->flags
& PA_SINK_DEFERRED_VOLUME
) && s
->get_volume
)
2111 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_VOLUME
, NULL
, 0, NULL
) == 0);
2113 update_real_volume(s
, &s
->real_volume
, &s
->channel_map
);
2114 propagate_real_volume(s
, &old_real_volume
);
2117 return &s
->reference_volume
;
2120 /* Called from main thread. In volume sharing cases, only the root sink may
2122 void pa_sink_volume_changed(pa_sink
*s
, const pa_cvolume
*new_real_volume
) {
2123 pa_cvolume old_real_volume
;
2125 pa_sink_assert_ref(s
);
2126 pa_assert_ctl_context();
2127 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2128 pa_assert(!(s
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
));
2130 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
2132 old_real_volume
= s
->real_volume
;
2133 update_real_volume(s
, new_real_volume
, &s
->channel_map
);
2134 propagate_real_volume(s
, &old_real_volume
);
2137 /* Called from main thread */
2138 void pa_sink_set_mute(pa_sink
*s
, pa_bool_t mute
, pa_bool_t save
) {
2139 pa_bool_t old_muted
;
2141 pa_sink_assert_ref(s
);
2142 pa_assert_ctl_context();
2143 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2145 old_muted
= s
->muted
;
2147 s
->save_muted
= (old_muted
== s
->muted
&& s
->save_muted
) || save
;
2149 if (!(s
->flags
& PA_SINK_DEFERRED_VOLUME
) && s
->set_mute
)
2152 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
2154 if (old_muted
!= s
->muted
)
2155 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2158 /* Called from main thread */
2159 pa_bool_t
pa_sink_get_mute(pa_sink
*s
, pa_bool_t force_refresh
) {
2161 pa_sink_assert_ref(s
);
2162 pa_assert_ctl_context();
2163 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2165 if (s
->refresh_muted
|| force_refresh
) {
2166 pa_bool_t old_muted
= s
->muted
;
2168 if (!(s
->flags
& PA_SINK_DEFERRED_VOLUME
) && s
->get_mute
)
2171 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MUTE
, NULL
, 0, NULL
) == 0);
2173 if (old_muted
!= s
->muted
) {
2174 s
->save_muted
= TRUE
;
2176 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2178 /* Make sure the soft mute status stays in sync */
2179 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MUTE
, NULL
, 0, NULL
) == 0);
2186 /* Called from main thread */
2187 void pa_sink_mute_changed(pa_sink
*s
, pa_bool_t new_muted
) {
2188 pa_sink_assert_ref(s
);
2189 pa_assert_ctl_context();
2190 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2192 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
2194 if (s
->muted
== new_muted
)
2197 s
->muted
= new_muted
;
2198 s
->save_muted
= TRUE
;
2200 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2203 /* Called from main thread */
2204 pa_bool_t
pa_sink_update_proplist(pa_sink
*s
, pa_update_mode_t mode
, pa_proplist
*p
) {
2205 pa_sink_assert_ref(s
);
2206 pa_assert_ctl_context();
2209 pa_proplist_update(s
->proplist
, mode
, p
);
2211 if (PA_SINK_IS_LINKED(s
->state
)) {
2212 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
2213 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2219 /* Called from main thread */
2220 /* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
2221 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
2223 pa_sink_assert_ref(s
);
2224 pa_assert_ctl_context();
2226 if (!description
&& !pa_proplist_contains(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
))
2229 old
= pa_proplist_gets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
2231 if (old
&& description
&& pa_streq(old
, description
))
2235 pa_proplist_sets(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, description
);
2237 pa_proplist_unset(s
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
2239 if (s
->monitor_source
) {
2242 n
= pa_sprintf_malloc("Monitor Source of %s", description
? description
: s
->name
);
2243 pa_source_set_description(s
->monitor_source
, n
);
2247 if (PA_SINK_IS_LINKED(s
->state
)) {
2248 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
2249 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PROPLIST_CHANGED
], s
);
2253 /* Called from main thread */
2254 unsigned pa_sink_linked_by(pa_sink
*s
) {
2257 pa_sink_assert_ref(s
);
2258 pa_assert_ctl_context();
2259 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2261 ret
= pa_idxset_size(s
->inputs
);
2263 /* We add in the number of streams connected to us here. Please
2264 * note the asymmetry to pa_sink_used_by()! */
2266 if (s
->monitor_source
)
2267 ret
+= pa_source_linked_by(s
->monitor_source
);
2272 /* Called from main thread */
2273 unsigned pa_sink_used_by(pa_sink
*s
) {
2276 pa_sink_assert_ref(s
);
2277 pa_assert_ctl_context();
2278 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2280 ret
= pa_idxset_size(s
->inputs
);
2281 pa_assert(ret
>= s
->n_corked
);
2283 /* Streams connected to our monitor source do not matter for
2284 * pa_sink_used_by()!.*/
2286 return ret
- s
->n_corked
;
2289 /* Called from main thread */
2290 unsigned pa_sink_check_suspend(pa_sink
*s
) {
2295 pa_sink_assert_ref(s
);
2296 pa_assert_ctl_context();
2298 if (!PA_SINK_IS_LINKED(s
->state
))
2303 PA_IDXSET_FOREACH(i
, s
->inputs
, idx
) {
2304 pa_sink_input_state_t st
;
2306 st
= pa_sink_input_get_state(i
);
2308 /* We do not assert here. It is perfectly valid for a sink input to
2309 * be in the INIT state (i.e. created, marked done but not yet put)
2310 * and we should not care if it's unlinked as it won't contribute
2311 * towards our busy status.
2313 if (!PA_SINK_INPUT_IS_LINKED(st
))
2316 if (st
== PA_SINK_INPUT_CORKED
)
2319 if (i
->flags
& PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND
)
2325 if (s
->monitor_source
)
2326 ret
+= pa_source_check_suspend(s
->monitor_source
);
2331 /* Called from the IO thread */
2332 static void sync_input_volumes_within_thread(pa_sink
*s
) {
2336 pa_sink_assert_ref(s
);
2337 pa_sink_assert_io_context(s
);
2339 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
2340 if (pa_cvolume_equal(&i
->thread_info
.soft_volume
, &i
->soft_volume
))
2343 i
->thread_info
.soft_volume
= i
->soft_volume
;
2344 pa_sink_input_request_rewind(i
, 0, TRUE
, FALSE
, FALSE
);
2348 /* Called from the IO thread. Only called for the root sink in volume sharing
2349 * cases, except for internal recursive calls. */
2350 static void set_shared_volume_within_thread(pa_sink
*s
) {
2351 pa_sink_input
*i
= NULL
;
2354 pa_sink_assert_ref(s
);
2356 PA_MSGOBJECT(s
)->process_msg(PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_VOLUME_SYNCED
, NULL
, 0, NULL
);
2358 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
) {
2359 if (i
->origin_sink
&& (i
->origin_sink
->flags
& PA_SINK_SHARE_VOLUME_WITH_MASTER
))
2360 set_shared_volume_within_thread(i
->origin_sink
);
2364 /* Called from IO thread, except when it is not */
2365 int pa_sink_process_msg(pa_msgobject
*o
, int code
, void *userdata
, int64_t offset
, pa_memchunk
*chunk
) {
2366 pa_sink
*s
= PA_SINK(o
);
2367 pa_sink_assert_ref(s
);
2369 switch ((pa_sink_message_t
) code
) {
2371 case PA_SINK_MESSAGE_ADD_INPUT
: {
2372 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2374 /* If you change anything here, make sure to change the
2375 * sink input handling a few lines down at
2376 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
2378 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
2380 /* Since the caller sleeps in pa_sink_input_put(), we can
2381 * safely access data outside of thread_info even though
2384 if ((i
->thread_info
.sync_prev
= i
->sync_prev
)) {
2385 pa_assert(i
->sink
== i
->thread_info
.sync_prev
->sink
);
2386 pa_assert(i
->sync_prev
->sync_next
== i
);
2387 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
;
2390 if ((i
->thread_info
.sync_next
= i
->sync_next
)) {
2391 pa_assert(i
->sink
== i
->thread_info
.sync_next
->sink
);
2392 pa_assert(i
->sync_next
->sync_prev
== i
);
2393 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
;
2396 pa_assert(!i
->thread_info
.attached
);
2397 i
->thread_info
.attached
= TRUE
;
2402 pa_sink_input_set_state_within_thread(i
, i
->state
);
2404 /* The requested latency of the sink input needs to be
2405 * fixed up and then configured on the sink */
2407 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
2408 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
2410 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2411 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2413 /* We don't rewind here automatically. This is left to the
2414 * sink input implementor because some sink inputs need a
2415 * slow start, i.e. need some time to buffer client
2416 * samples before beginning streaming. */
2418 /* FIXME: Actually rewinding should be requested before
2419 * updating the sink requested latency, because updating
2420 * the requested latency updates also max_rewind of the
2421 * sink. Now consider this: a sink has a 10 s buffer and
2422 * nobody has requested anything less. Then a new stream
2423 * appears while the sink buffer is full. The new stream
2424 * requests e.g. 100 ms latency. That request is forwarded
2425 * to the sink, so now max_rewind is 100 ms. When a rewind
2426 * is requested, the sink will only rewind 100 ms, and the
2427 * new stream will have to wait about 10 seconds before it
2428 * becomes audible. */
2430 /* In flat volume mode we need to update the volume as
2432 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2435 case PA_SINK_MESSAGE_REMOVE_INPUT
: {
2436 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2438 /* If you change anything here, make sure to change the
2439 * sink input handling a few lines down at
2440 * PA_SINK_MESSAGE_START_MOVE, too. */
2445 pa_sink_input_set_state_within_thread(i
, i
->state
);
2447 pa_assert(i
->thread_info
.attached
);
2448 i
->thread_info
.attached
= FALSE
;
2450 /* Since the caller sleeps in pa_sink_input_unlink(),
2451 * we can safely access data outside of thread_info even
2452 * though it is mutable */
2454 pa_assert(!i
->sync_prev
);
2455 pa_assert(!i
->sync_next
);
2457 if (i
->thread_info
.sync_prev
) {
2458 i
->thread_info
.sync_prev
->thread_info
.sync_next
= i
->thread_info
.sync_prev
->sync_next
;
2459 i
->thread_info
.sync_prev
= NULL
;
2462 if (i
->thread_info
.sync_next
) {
2463 i
->thread_info
.sync_next
->thread_info
.sync_prev
= i
->thread_info
.sync_next
->sync_prev
;
2464 i
->thread_info
.sync_next
= NULL
;
2467 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
2468 pa_sink_input_unref(i
);
2470 pa_sink_invalidate_requested_latency(s
, TRUE
);
2471 pa_sink_request_rewind(s
, (size_t) -1);
2473 /* In flat volume mode we need to update the volume as
2475 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2478 case PA_SINK_MESSAGE_START_MOVE
: {
2479 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2481 /* We don't support moving synchronized streams. */
2482 pa_assert(!i
->sync_prev
);
2483 pa_assert(!i
->sync_next
);
2484 pa_assert(!i
->thread_info
.sync_next
);
2485 pa_assert(!i
->thread_info
.sync_prev
);
2487 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
2489 size_t sink_nbytes
, total_nbytes
;
2491 /* The old sink probably has some audio from this
2492 * stream in its buffer. We want to "take it back" as
2493 * much as possible and play it to the new sink. We
2494 * don't know at this point how much the old sink can
2495 * rewind. We have to pick something, and that
2496 * something is the full latency of the old sink here.
2497 * So we rewind the stream buffer by the sink latency
2498 * amount, which may be more than what we should
2499 * rewind. This can result in a chunk of audio being
2500 * played both to the old sink and the new sink.
2502 * FIXME: Fix this code so that we don't have to make
2503 * guesses about how much the sink will actually be
2504 * able to rewind. If someone comes up with a solution
2505 * for this, something to note is that the part of the
2506 * latency that the old sink couldn't rewind should
2507 * ideally be compensated after the stream has moved
2508 * to the new sink by adding silence. The new sink
2509 * most likely can't start playing the moved stream
2510 * immediately, and that gap should be removed from
2511 * the "compensation silence" (at least at the time of
2512 * writing this, the move finish code will actually
2513 * already take care of dropping the new sink's
2514 * unrewindable latency, so taking into account the
2515 * unrewindable latency of the old sink is the only
2518 * The render_memblockq contents are discarded,
2519 * because when the sink changes, the format of the
2520 * audio stored in the render_memblockq may change
2521 * too, making the stored audio invalid. FIXME:
2522 * However, the read and write indices are moved back
2523 * the same amount, so if they are not the same now,
2524 * they won't be the same after the rewind either. If
2525 * the write index of the render_memblockq is ahead of
2526 * the read index, then the render_memblockq will feed
2527 * the new sink some silence first, which it shouldn't
2528 * do. The write index should be flushed to be the
2529 * same as the read index. */
2531 /* Get the latency of the sink */
2532 usec
= pa_sink_get_latency_within_thread(s
);
2533 sink_nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
2534 total_nbytes
= sink_nbytes
+ pa_memblockq_get_length(i
->thread_info
.render_memblockq
);
2536 if (total_nbytes
> 0) {
2537 i
->thread_info
.rewrite_nbytes
= i
->thread_info
.resampler
? pa_resampler_request(i
->thread_info
.resampler
, total_nbytes
) : total_nbytes
;
2538 i
->thread_info
.rewrite_flush
= TRUE
;
2539 pa_sink_input_process_rewind(i
, sink_nbytes
);
2546 pa_assert(i
->thread_info
.attached
);
2547 i
->thread_info
.attached
= FALSE
;
2549 /* Let's remove the sink input ...*/
2550 if (pa_hashmap_remove(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
)))
2551 pa_sink_input_unref(i
);
2553 pa_sink_invalidate_requested_latency(s
, TRUE
);
2555 pa_log_debug("Requesting rewind due to started move");
2556 pa_sink_request_rewind(s
, (size_t) -1);
2558 /* In flat volume mode we need to update the volume as
2560 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2563 case PA_SINK_MESSAGE_FINISH_MOVE
: {
2564 pa_sink_input
*i
= PA_SINK_INPUT(userdata
);
2566 /* We don't support moving synchronized streams. */
2567 pa_assert(!i
->sync_prev
);
2568 pa_assert(!i
->sync_next
);
2569 pa_assert(!i
->thread_info
.sync_next
);
2570 pa_assert(!i
->thread_info
.sync_prev
);
2572 pa_hashmap_put(s
->thread_info
.inputs
, PA_UINT32_TO_PTR(i
->index
), pa_sink_input_ref(i
));
2574 pa_assert(!i
->thread_info
.attached
);
2575 i
->thread_info
.attached
= TRUE
;
2580 if (i
->thread_info
.state
!= PA_SINK_INPUT_CORKED
) {
2584 /* In the ideal case the new sink would start playing
2585 * the stream immediately. That requires the sink to
2586 * be able to rewind all of its latency, which usually
2587 * isn't possible, so there will probably be some gap
2588 * before the moved stream becomes audible. We then
2589 * have two possibilities: 1) start playing the stream
2590 * from where it is now, or 2) drop the unrewindable
2591 * latency of the sink from the stream. With option 1
2592 * we won't lose any audio but the stream will have a
2593 * pause. With option 2 we may lose some audio but the
2594 * stream time will be somewhat in sync with the wall
2595 * clock. Lennart seems to have chosen option 2 (one
2596 * of the reasons might have been that option 1 is
2597 * actually much harder to implement), so we drop the
2598 * latency of the new sink from the moved stream and
2599 * hope that the sink will undo most of that in the
2602 /* Get the latency of the sink */
2603 usec
= pa_sink_get_latency_within_thread(s
);
2604 nbytes
= pa_usec_to_bytes(usec
, &s
->sample_spec
);
2607 pa_sink_input_drop(i
, nbytes
);
2609 pa_log_debug("Requesting rewind due to finished move");
2610 pa_sink_request_rewind(s
, nbytes
);
2613 /* Updating the requested sink latency has to be done
2614 * after the sink rewind request, not before, because
2615 * otherwise the sink may limit the rewind amount
2618 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1)
2619 pa_sink_input_set_requested_latency_within_thread(i
, i
->thread_info
.requested_sink_latency
);
2621 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2622 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
2624 return o
->process_msg(o
, PA_SINK_MESSAGE_SET_SHARED_VOLUME
, NULL
, 0, NULL
);
2627 case PA_SINK_MESSAGE_SET_SHARED_VOLUME
: {
2628 pa_sink
*root_sink
= pa_sink_get_master(s
);
2630 if (PA_LIKELY(root_sink
))
2631 set_shared_volume_within_thread(root_sink
);
2636 case PA_SINK_MESSAGE_SET_VOLUME_SYNCED
:
2638 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
) {
2640 pa_sink_volume_change_push(s
);
2642 /* Fall through ... */
2644 case PA_SINK_MESSAGE_SET_VOLUME
:
2646 if (!pa_cvolume_equal(&s
->thread_info
.soft_volume
, &s
->soft_volume
)) {
2647 s
->thread_info
.soft_volume
= s
->soft_volume
;
2648 pa_sink_request_rewind(s
, (size_t) -1);
2651 /* Fall through ... */
2653 case PA_SINK_MESSAGE_SYNC_VOLUMES
:
2654 sync_input_volumes_within_thread(s
);
2657 case PA_SINK_MESSAGE_GET_VOLUME
:
2659 if ((s
->flags
& PA_SINK_DEFERRED_VOLUME
) && s
->get_volume
) {
2661 pa_sink_volume_change_flush(s
);
2662 pa_sw_cvolume_divide(&s
->thread_info
.current_hw_volume
, &s
->real_volume
, &s
->soft_volume
);
2665 /* In case sink implementor reset SW volume. */
2666 if (!pa_cvolume_equal(&s
->thread_info
.soft_volume
, &s
->soft_volume
)) {
2667 s
->thread_info
.soft_volume
= s
->soft_volume
;
2668 pa_sink_request_rewind(s
, (size_t) -1);
2673 case PA_SINK_MESSAGE_SET_MUTE
:
2675 if (s
->thread_info
.soft_muted
!= s
->muted
) {
2676 s
->thread_info
.soft_muted
= s
->muted
;
2677 pa_sink_request_rewind(s
, (size_t) -1);
2680 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
&& s
->set_mute
)
2685 case PA_SINK_MESSAGE_GET_MUTE
:
2687 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
&& s
->get_mute
)
2692 case PA_SINK_MESSAGE_SET_STATE
: {
2694 pa_bool_t suspend_change
=
2695 (s
->thread_info
.state
== PA_SINK_SUSPENDED
&& PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata
))) ||
2696 (PA_SINK_IS_OPENED(s
->thread_info
.state
) && PA_PTR_TO_UINT(userdata
) == PA_SINK_SUSPENDED
);
2698 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
2700 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
) {
2701 s
->thread_info
.rewind_nbytes
= 0;
2702 s
->thread_info
.rewind_requested
= FALSE
;
2705 if (suspend_change
) {
2709 while ((i
= pa_hashmap_iterate(s
->thread_info
.inputs
, &state
, NULL
)))
2710 if (i
->suspend_within_thread
)
2711 i
->suspend_within_thread(i
, s
->thread_info
.state
== PA_SINK_SUSPENDED
);
2717 case PA_SINK_MESSAGE_DETACH
:
2719 /* Detach all streams */
2720 pa_sink_detach_within_thread(s
);
2723 case PA_SINK_MESSAGE_ATTACH
:
2725 /* Reattach all streams */
2726 pa_sink_attach_within_thread(s
);
2729 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
: {
2731 pa_usec_t
*usec
= userdata
;
2732 *usec
= pa_sink_get_requested_latency_within_thread(s
);
2734 /* Yes, that's right, the IO thread will see -1 when no
2735 * explicit requested latency is configured, the main
2736 * thread will see max_latency */
2737 if (*usec
== (pa_usec_t
) -1)
2738 *usec
= s
->thread_info
.max_latency
;
2743 case PA_SINK_MESSAGE_SET_LATENCY_RANGE
: {
2744 pa_usec_t
*r
= userdata
;
2746 pa_sink_set_latency_range_within_thread(s
, r
[0], r
[1]);
2751 case PA_SINK_MESSAGE_GET_LATENCY_RANGE
: {
2752 pa_usec_t
*r
= userdata
;
2754 r
[0] = s
->thread_info
.min_latency
;
2755 r
[1] = s
->thread_info
.max_latency
;
2760 case PA_SINK_MESSAGE_GET_FIXED_LATENCY
:
2762 *((pa_usec_t
*) userdata
) = s
->thread_info
.fixed_latency
;
2765 case PA_SINK_MESSAGE_SET_FIXED_LATENCY
:
2767 pa_sink_set_fixed_latency_within_thread(s
, (pa_usec_t
) offset
);
2770 case PA_SINK_MESSAGE_GET_MAX_REWIND
:
2772 *((size_t*) userdata
) = s
->thread_info
.max_rewind
;
2775 case PA_SINK_MESSAGE_GET_MAX_REQUEST
:
2777 *((size_t*) userdata
) = s
->thread_info
.max_request
;
2780 case PA_SINK_MESSAGE_SET_MAX_REWIND
:
2782 pa_sink_set_max_rewind_within_thread(s
, (size_t) offset
);
2785 case PA_SINK_MESSAGE_SET_MAX_REQUEST
:
2787 pa_sink_set_max_request_within_thread(s
, (size_t) offset
);
2790 case PA_SINK_MESSAGE_SET_PORT
:
2792 pa_assert(userdata
);
2794 struct sink_message_set_port
*msg_data
= userdata
;
2795 msg_data
->ret
= s
->set_port(s
, msg_data
->port
);
2799 case PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE
:
2800 /* This message is sent from IO-thread and handled in main thread. */
2801 pa_assert_ctl_context();
2803 /* Make sure we're not messing with main thread when no longer linked */
2804 if (!PA_SINK_IS_LINKED(s
->state
))
2807 pa_sink_get_volume(s
, TRUE
);
2808 pa_sink_get_mute(s
, TRUE
);
2811 case PA_SINK_MESSAGE_GET_LATENCY
:
2812 case PA_SINK_MESSAGE_MAX
:
2819 /* Called from main thread */
2820 int pa_sink_suspend_all(pa_core
*c
, pa_bool_t suspend
, pa_suspend_cause_t cause
) {
2825 pa_core_assert_ref(c
);
2826 pa_assert_ctl_context();
2827 pa_assert(cause
!= 0);
2829 PA_IDXSET_FOREACH(sink
, c
->sinks
, idx
) {
2832 if ((r
= pa_sink_suspend(sink
, suspend
, cause
)) < 0)
2839 /* Called from main thread */
2840 void pa_sink_detach(pa_sink
*s
) {
2841 pa_sink_assert_ref(s
);
2842 pa_assert_ctl_context();
2843 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2845 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_DETACH
, NULL
, 0, NULL
) == 0);
2848 /* Called from main thread */
2849 void pa_sink_attach(pa_sink
*s
) {
2850 pa_sink_assert_ref(s
);
2851 pa_assert_ctl_context();
2852 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2854 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_ATTACH
, NULL
, 0, NULL
) == 0);
2857 /* Called from IO thread */
2858 void pa_sink_detach_within_thread(pa_sink
*s
) {
2862 pa_sink_assert_ref(s
);
2863 pa_sink_assert_io_context(s
);
2864 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2866 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2870 if (s
->monitor_source
)
2871 pa_source_detach_within_thread(s
->monitor_source
);
2874 /* Called from IO thread */
2875 void pa_sink_attach_within_thread(pa_sink
*s
) {
2879 pa_sink_assert_ref(s
);
2880 pa_sink_assert_io_context(s
);
2881 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2883 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2887 if (s
->monitor_source
)
2888 pa_source_attach_within_thread(s
->monitor_source
);
2891 /* Called from IO thread */
2892 void pa_sink_request_rewind(pa_sink
*s
, size_t nbytes
) {
2893 pa_sink_assert_ref(s
);
2894 pa_sink_assert_io_context(s
);
2895 pa_assert(PA_SINK_IS_LINKED(s
->thread_info
.state
));
2897 if (s
->thread_info
.state
== PA_SINK_SUSPENDED
)
2900 if (nbytes
== (size_t) -1)
2901 nbytes
= s
->thread_info
.max_rewind
;
2903 nbytes
= PA_MIN(nbytes
, s
->thread_info
.max_rewind
);
2905 if (s
->thread_info
.rewind_requested
&&
2906 nbytes
<= s
->thread_info
.rewind_nbytes
)
2909 s
->thread_info
.rewind_nbytes
= nbytes
;
2910 s
->thread_info
.rewind_requested
= TRUE
;
2912 if (s
->request_rewind
)
2913 s
->request_rewind(s
);
2916 /* Called from IO thread */
2917 pa_usec_t
pa_sink_get_requested_latency_within_thread(pa_sink
*s
) {
2918 pa_usec_t result
= (pa_usec_t
) -1;
2921 pa_usec_t monitor_latency
;
2923 pa_sink_assert_ref(s
);
2924 pa_sink_assert_io_context(s
);
2926 if (!(s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
2927 return PA_CLAMP(s
->thread_info
.fixed_latency
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2929 if (s
->thread_info
.requested_latency_valid
)
2930 return s
->thread_info
.requested_latency
;
2932 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2933 if (i
->thread_info
.requested_sink_latency
!= (pa_usec_t
) -1 &&
2934 (result
== (pa_usec_t
) -1 || result
> i
->thread_info
.requested_sink_latency
))
2935 result
= i
->thread_info
.requested_sink_latency
;
2937 monitor_latency
= pa_source_get_requested_latency_within_thread(s
->monitor_source
);
2939 if (monitor_latency
!= (pa_usec_t
) -1 &&
2940 (result
== (pa_usec_t
) -1 || result
> monitor_latency
))
2941 result
= monitor_latency
;
2943 if (result
!= (pa_usec_t
) -1)
2944 result
= PA_CLAMP(result
, s
->thread_info
.min_latency
, s
->thread_info
.max_latency
);
2946 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
2947 /* Only cache if properly initialized */
2948 s
->thread_info
.requested_latency
= result
;
2949 s
->thread_info
.requested_latency_valid
= TRUE
;
2955 /* Called from main thread */
2956 pa_usec_t
pa_sink_get_requested_latency(pa_sink
*s
) {
2959 pa_sink_assert_ref(s
);
2960 pa_assert_ctl_context();
2961 pa_assert(PA_SINK_IS_LINKED(s
->state
));
2963 if (s
->state
== PA_SINK_SUSPENDED
)
2966 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY
, &usec
, 0, NULL
) == 0);
2971 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
2972 void pa_sink_set_max_rewind_within_thread(pa_sink
*s
, size_t max_rewind
) {
2976 pa_sink_assert_ref(s
);
2977 pa_sink_assert_io_context(s
);
2979 if (max_rewind
== s
->thread_info
.max_rewind
)
2982 s
->thread_info
.max_rewind
= max_rewind
;
2984 if (PA_SINK_IS_LINKED(s
->thread_info
.state
))
2985 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
2986 pa_sink_input_update_max_rewind(i
, s
->thread_info
.max_rewind
);
2988 if (s
->monitor_source
)
2989 pa_source_set_max_rewind_within_thread(s
->monitor_source
, s
->thread_info
.max_rewind
);
2992 /* Called from main thread */
2993 void pa_sink_set_max_rewind(pa_sink
*s
, size_t max_rewind
) {
2994 pa_sink_assert_ref(s
);
2995 pa_assert_ctl_context();
2997 if (PA_SINK_IS_LINKED(s
->state
))
2998 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REWIND
, NULL
, max_rewind
, NULL
) == 0);
3000 pa_sink_set_max_rewind_within_thread(s
, max_rewind
);
3003 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
3004 void pa_sink_set_max_request_within_thread(pa_sink
*s
, size_t max_request
) {
3007 pa_sink_assert_ref(s
);
3008 pa_sink_assert_io_context(s
);
3010 if (max_request
== s
->thread_info
.max_request
)
3013 s
->thread_info
.max_request
= max_request
;
3015 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
3018 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
3019 pa_sink_input_update_max_request(i
, s
->thread_info
.max_request
);
3023 /* Called from main thread */
3024 void pa_sink_set_max_request(pa_sink
*s
, size_t max_request
) {
3025 pa_sink_assert_ref(s
);
3026 pa_assert_ctl_context();
3028 if (PA_SINK_IS_LINKED(s
->state
))
3029 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_MAX_REQUEST
, NULL
, max_request
, NULL
) == 0);
3031 pa_sink_set_max_request_within_thread(s
, max_request
);
3034 /* Called from IO thread */
3035 void pa_sink_invalidate_requested_latency(pa_sink
*s
, pa_bool_t dynamic
) {
3039 pa_sink_assert_ref(s
);
3040 pa_sink_assert_io_context(s
);
3042 if ((s
->flags
& PA_SINK_DYNAMIC_LATENCY
))
3043 s
->thread_info
.requested_latency_valid
= FALSE
;
3047 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
3049 if (s
->update_requested_latency
)
3050 s
->update_requested_latency(s
);
3052 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
3053 if (i
->update_sink_requested_latency
)
3054 i
->update_sink_requested_latency(i
);
3058 /* Called from main thread */
3059 void pa_sink_set_latency_range(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
3060 pa_sink_assert_ref(s
);
3061 pa_assert_ctl_context();
3063 /* min_latency == 0: no limit
3064 * min_latency anything else: specified limit
3066 * Similar for max_latency */
3068 if (min_latency
< ABSOLUTE_MIN_LATENCY
)
3069 min_latency
= ABSOLUTE_MIN_LATENCY
;
3071 if (max_latency
<= 0 ||
3072 max_latency
> ABSOLUTE_MAX_LATENCY
)
3073 max_latency
= ABSOLUTE_MAX_LATENCY
;
3075 pa_assert(min_latency
<= max_latency
);
3077 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3078 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
3079 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
3080 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
3082 if (PA_SINK_IS_LINKED(s
->state
)) {
3088 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
3090 pa_sink_set_latency_range_within_thread(s
, min_latency
, max_latency
);
3093 /* Called from main thread */
3094 void pa_sink_get_latency_range(pa_sink
*s
, pa_usec_t
*min_latency
, pa_usec_t
*max_latency
) {
3095 pa_sink_assert_ref(s
);
3096 pa_assert_ctl_context();
3097 pa_assert(min_latency
);
3098 pa_assert(max_latency
);
3100 if (PA_SINK_IS_LINKED(s
->state
)) {
3101 pa_usec_t r
[2] = { 0, 0 };
3103 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_LATENCY_RANGE
, r
, 0, NULL
) == 0);
3105 *min_latency
= r
[0];
3106 *max_latency
= r
[1];
3108 *min_latency
= s
->thread_info
.min_latency
;
3109 *max_latency
= s
->thread_info
.max_latency
;
3113 /* Called from IO thread */
3114 void pa_sink_set_latency_range_within_thread(pa_sink
*s
, pa_usec_t min_latency
, pa_usec_t max_latency
) {
3115 pa_sink_assert_ref(s
);
3116 pa_sink_assert_io_context(s
);
3118 pa_assert(min_latency
>= ABSOLUTE_MIN_LATENCY
);
3119 pa_assert(max_latency
<= ABSOLUTE_MAX_LATENCY
);
3120 pa_assert(min_latency
<= max_latency
);
3122 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3123 pa_assert((min_latency
== ABSOLUTE_MIN_LATENCY
&&
3124 max_latency
== ABSOLUTE_MAX_LATENCY
) ||
3125 (s
->flags
& PA_SINK_DYNAMIC_LATENCY
));
3127 if (s
->thread_info
.min_latency
== min_latency
&&
3128 s
->thread_info
.max_latency
== max_latency
)
3131 s
->thread_info
.min_latency
= min_latency
;
3132 s
->thread_info
.max_latency
= max_latency
;
3134 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
3138 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
3139 if (i
->update_sink_latency_range
)
3140 i
->update_sink_latency_range(i
);
3143 pa_sink_invalidate_requested_latency(s
, FALSE
);
3145 pa_source_set_latency_range_within_thread(s
->monitor_source
, min_latency
, max_latency
);
3148 /* Called from main thread */
3149 void pa_sink_set_fixed_latency(pa_sink
*s
, pa_usec_t latency
) {
3150 pa_sink_assert_ref(s
);
3151 pa_assert_ctl_context();
3153 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
3154 pa_assert(latency
== 0);
3158 if (latency
< ABSOLUTE_MIN_LATENCY
)
3159 latency
= ABSOLUTE_MIN_LATENCY
;
3161 if (latency
> ABSOLUTE_MAX_LATENCY
)
3162 latency
= ABSOLUTE_MAX_LATENCY
;
3164 if (PA_SINK_IS_LINKED(s
->state
))
3165 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_FIXED_LATENCY
, NULL
, (int64_t) latency
, NULL
) == 0);
3167 s
->thread_info
.fixed_latency
= latency
;
3169 pa_source_set_fixed_latency(s
->monitor_source
, latency
);
3172 /* Called from main thread */
3173 pa_usec_t
pa_sink_get_fixed_latency(pa_sink
*s
) {
3176 pa_sink_assert_ref(s
);
3177 pa_assert_ctl_context();
3179 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
)
3182 if (PA_SINK_IS_LINKED(s
->state
))
3183 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_FIXED_LATENCY
, &latency
, 0, NULL
) == 0);
3185 latency
= s
->thread_info
.fixed_latency
;
3190 /* Called from IO thread */
3191 void pa_sink_set_fixed_latency_within_thread(pa_sink
*s
, pa_usec_t latency
) {
3192 pa_sink_assert_ref(s
);
3193 pa_sink_assert_io_context(s
);
3195 if (s
->flags
& PA_SINK_DYNAMIC_LATENCY
) {
3196 pa_assert(latency
== 0);
3200 pa_assert(latency
>= ABSOLUTE_MIN_LATENCY
);
3201 pa_assert(latency
<= ABSOLUTE_MAX_LATENCY
);
3203 if (s
->thread_info
.fixed_latency
== latency
)
3206 s
->thread_info
.fixed_latency
= latency
;
3208 if (PA_SINK_IS_LINKED(s
->thread_info
.state
)) {
3212 PA_HASHMAP_FOREACH(i
, s
->thread_info
.inputs
, state
)
3213 if (i
->update_sink_fixed_latency
)
3214 i
->update_sink_fixed_latency(i
);
3217 pa_sink_invalidate_requested_latency(s
, FALSE
);
3219 pa_source_set_fixed_latency_within_thread(s
->monitor_source
, latency
);
3222 /* Called from main context */
3223 size_t pa_sink_get_max_rewind(pa_sink
*s
) {
3225 pa_assert_ctl_context();
3226 pa_sink_assert_ref(s
);
3228 if (!PA_SINK_IS_LINKED(s
->state
))
3229 return s
->thread_info
.max_rewind
;
3231 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REWIND
, &r
, 0, NULL
) == 0);
3236 /* Called from main context */
3237 size_t pa_sink_get_max_request(pa_sink
*s
) {
3239 pa_sink_assert_ref(s
);
3240 pa_assert_ctl_context();
3242 if (!PA_SINK_IS_LINKED(s
->state
))
3243 return s
->thread_info
.max_request
;
3245 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_GET_MAX_REQUEST
, &r
, 0, NULL
) == 0);
3250 /* Called from main context */
3251 int pa_sink_set_port(pa_sink
*s
, const char *name
, pa_bool_t save
) {
3252 pa_device_port
*port
;
3255 pa_sink_assert_ref(s
);
3256 pa_assert_ctl_context();
3259 pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s
->index
, s
->name
);
3260 return -PA_ERR_NOTIMPLEMENTED
;
3264 return -PA_ERR_NOENTITY
;
3266 if (!(port
= pa_hashmap_get(s
->ports
, name
)))
3267 return -PA_ERR_NOENTITY
;
3269 if (s
->active_port
== port
) {
3270 s
->save_port
= s
->save_port
|| save
;
3274 if (s
->flags
& PA_SINK_DEFERRED_VOLUME
) {
3275 struct sink_message_set_port msg
= { .port
= port
, .ret
= 0 };
3276 pa_assert_se(pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SINK_MESSAGE_SET_PORT
, &msg
, 0, NULL
) == 0);
3280 ret
= s
->set_port(s
, port
);
3283 return -PA_ERR_NOENTITY
;
3285 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
3287 pa_log_info("Changed port of sink %u \"%s\" to %s", s
->index
, s
->name
, port
->name
);
3289 s
->active_port
= port
;
3290 s
->save_port
= save
;
3292 pa_hook_fire(&s
->core
->hooks
[PA_CORE_HOOK_SINK_PORT_CHANGED
], s
);
3297 pa_bool_t
pa_device_init_icon(pa_proplist
*p
, pa_bool_t is_sink
) {
3298 const char *ff
, *c
, *t
= NULL
, *s
= "", *profile
, *bus
;
3302 if (pa_proplist_contains(p
, PA_PROP_DEVICE_ICON_NAME
))
3305 if ((ff
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
3307 if (pa_streq(ff
, "microphone"))
3308 t
= "audio-input-microphone";
3309 else if (pa_streq(ff
, "webcam"))
3311 else if (pa_streq(ff
, "computer"))
3313 else if (pa_streq(ff
, "handset"))
3315 else if (pa_streq(ff
, "portable"))
3316 t
= "multimedia-player";
3317 else if (pa_streq(ff
, "tv"))
3318 t
= "video-display";
3321 * The following icons are not part of the icon naming spec,
3322 * because Rodney Dawes sucks as the maintainer of that spec.
3324 * http://lists.freedesktop.org/archives/xdg/2009-May/010397.html
3326 else if (pa_streq(ff
, "headset"))
3327 t
= "audio-headset";
3328 else if (pa_streq(ff
, "headphone"))
3329 t
= "audio-headphones";
3330 else if (pa_streq(ff
, "speaker"))
3331 t
= "audio-speakers";
3332 else if (pa_streq(ff
, "hands-free"))
3333 t
= "audio-handsfree";
3337 if ((c
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
3338 if (pa_streq(c
, "modem"))
3345 t
= "audio-input-microphone";
3348 if ((profile
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
3349 if (strstr(profile
, "analog"))
3351 else if (strstr(profile
, "iec958"))
3353 else if (strstr(profile
, "hdmi"))
3357 bus
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
);
3359 pa_proplist_setf(p
, PA_PROP_DEVICE_ICON_NAME
, "%s%s%s%s", t
, pa_strempty(s
), bus
? "-" : "", pa_strempty(bus
));
3364 pa_bool_t
pa_device_init_description(pa_proplist
*p
) {
3365 const char *s
, *d
= NULL
, *k
;
3368 if (pa_proplist_contains(p
, PA_PROP_DEVICE_DESCRIPTION
))
3371 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
3372 if (pa_streq(s
, "internal"))
3373 d
= _("Internal Audio");
3376 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
)))
3377 if (pa_streq(s
, "modem"))
3381 d
= pa_proplist_gets(p
, PA_PROP_DEVICE_PRODUCT_NAME
);
3386 k
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_DESCRIPTION
);
3389 pa_proplist_setf(p
, PA_PROP_DEVICE_DESCRIPTION
, _("%s %s"), d
, k
);
3391 pa_proplist_sets(p
, PA_PROP_DEVICE_DESCRIPTION
, d
);
3396 pa_bool_t
pa_device_init_intended_roles(pa_proplist
*p
) {
3400 if (pa_proplist_contains(p
, PA_PROP_DEVICE_INTENDED_ROLES
))
3403 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
)))
3404 if (pa_streq(s
, "handset") || pa_streq(s
, "hands-free")
3405 || pa_streq(s
, "headset")) {
3406 pa_proplist_sets(p
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
3413 unsigned pa_device_init_priority(pa_proplist
*p
) {
3415 unsigned priority
= 0;
3419 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_CLASS
))) {
3421 if (pa_streq(s
, "sound"))
3423 else if (!pa_streq(s
, "modem"))
3427 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_FORM_FACTOR
))) {
3429 if (pa_streq(s
, "internal"))
3431 else if (pa_streq(s
, "speaker"))
3433 else if (pa_streq(s
, "headphone"))
3437 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_BUS
))) {
3439 if (pa_streq(s
, "pci"))
3441 else if (pa_streq(s
, "usb"))
3443 else if (pa_streq(s
, "bluetooth"))
3447 if ((s
= pa_proplist_gets(p
, PA_PROP_DEVICE_PROFILE_NAME
))) {
3449 if (pa_startswith(s
, "analog-"))
3451 else if (pa_startswith(s
, "iec958-"))
3458 PA_STATIC_FLIST_DECLARE(pa_sink_volume_change
, 0, pa_xfree
);
3460 /* Called from the IO thread. */
3461 static pa_sink_volume_change
*pa_sink_volume_change_new(pa_sink
*s
) {
3462 pa_sink_volume_change
*c
;
3463 if (!(c
= pa_flist_pop(PA_STATIC_FLIST_GET(pa_sink_volume_change
))))
3464 c
= pa_xnew(pa_sink_volume_change
, 1);
3466 PA_LLIST_INIT(pa_sink_volume_change
, c
);
3468 pa_cvolume_reset(&c
->hw_volume
, s
->sample_spec
.channels
);
3472 /* Called from the IO thread. */
3473 static void pa_sink_volume_change_free(pa_sink_volume_change
*c
) {
3475 if (pa_flist_push(PA_STATIC_FLIST_GET(pa_sink_volume_change
), c
) < 0)
3479 /* Called from the IO thread. */
3480 void pa_sink_volume_change_push(pa_sink
*s
) {
3481 pa_sink_volume_change
*c
= NULL
;
3482 pa_sink_volume_change
*nc
= NULL
;
3483 uint32_t safety_margin
= s
->thread_info
.volume_change_safety_margin
;
3485 const char *direction
= NULL
;
3488 nc
= pa_sink_volume_change_new(s
);
3490 /* NOTE: There is already more different volumes in pa_sink that I can remember.
3491 * Adding one more volume for HW would get us rid of this, but I am trying
3492 * to survive with the ones we already have. */
3493 pa_sw_cvolume_divide(&nc
->hw_volume
, &s
->real_volume
, &s
->soft_volume
);
3495 if (!s
->thread_info
.volume_changes
&& pa_cvolume_equal(&nc
->hw_volume
, &s
->thread_info
.current_hw_volume
)) {
3496 pa_log_debug("Volume not changing");
3497 pa_sink_volume_change_free(nc
);
3501 nc
->at
= pa_sink_get_latency_within_thread(s
);
3502 nc
->at
+= pa_rtclock_now() + s
->thread_info
.volume_change_extra_delay
;
3504 if (s
->thread_info
.volume_changes_tail
) {
3505 for (c
= s
->thread_info
.volume_changes_tail
; c
; c
= c
->prev
) {
3506 /* If volume is going up let's do it a bit late. If it is going
3507 * down let's do it a bit early. */
3508 if (pa_cvolume_avg(&nc
->hw_volume
) > pa_cvolume_avg(&c
->hw_volume
)) {
3509 if (nc
->at
+ safety_margin
> c
->at
) {
3510 nc
->at
+= safety_margin
;
3515 else if (nc
->at
- safety_margin
> c
->at
) {
3516 nc
->at
-= safety_margin
;
3524 if (pa_cvolume_avg(&nc
->hw_volume
) > pa_cvolume_avg(&s
->thread_info
.current_hw_volume
)) {
3525 nc
->at
+= safety_margin
;
3528 nc
->at
-= safety_margin
;
3531 PA_LLIST_PREPEND(pa_sink_volume_change
, s
->thread_info
.volume_changes
, nc
);
3534 PA_LLIST_INSERT_AFTER(pa_sink_volume_change
, s
->thread_info
.volume_changes
, c
, nc
);
3537 pa_log_debug("Volume going %s to %d at %llu", direction
, pa_cvolume_avg(&nc
->hw_volume
), (long long unsigned) nc
->at
);
3539 /* We can ignore volume events that came earlier but should happen later than this. */
3540 PA_LLIST_FOREACH(c
, nc
->next
) {
3541 pa_log_debug("Volume change to %d at %llu was dropped", pa_cvolume_avg(&c
->hw_volume
), (long long unsigned) c
->at
);
3542 pa_sink_volume_change_free(c
);
3545 s
->thread_info
.volume_changes_tail
= nc
;
3548 /* Called from the IO thread. */
3549 static void pa_sink_volume_change_flush(pa_sink
*s
) {
3550 pa_sink_volume_change
*c
= s
->thread_info
.volume_changes
;
3552 s
->thread_info
.volume_changes
= NULL
;
3553 s
->thread_info
.volume_changes_tail
= NULL
;
3555 pa_sink_volume_change
*next
= c
->next
;
3556 pa_sink_volume_change_free(c
);
3561 /* Called from the IO thread. */
3562 pa_bool_t
pa_sink_volume_change_apply(pa_sink
*s
, pa_usec_t
*usec_to_next
) {
3564 pa_bool_t ret
= FALSE
;
3568 if (!s
->thread_info
.volume_changes
|| !PA_SINK_IS_LINKED(s
->state
)) {
3574 pa_assert(s
->write_volume
);
3576 now
= pa_rtclock_now();
3578 while (s
->thread_info
.volume_changes
&& now
>= s
->thread_info
.volume_changes
->at
) {
3579 pa_sink_volume_change
*c
= s
->thread_info
.volume_changes
;
3580 PA_LLIST_REMOVE(pa_sink_volume_change
, s
->thread_info
.volume_changes
, c
);
3581 pa_log_debug("Volume change to %d at %llu was written %llu usec late",
3582 pa_cvolume_avg(&c
->hw_volume
), (long long unsigned) c
->at
, (long long unsigned) (now
- c
->at
));
3584 s
->thread_info
.current_hw_volume
= c
->hw_volume
;
3585 pa_sink_volume_change_free(c
);
3591 if (s
->thread_info
.volume_changes
) {
3593 *usec_to_next
= s
->thread_info
.volume_changes
->at
- now
;
3594 if (pa_log_ratelimit(PA_LOG_DEBUG
))
3595 pa_log_debug("Next volume change in %lld usec", (long long) (s
->thread_info
.volume_changes
->at
- now
));
3600 s
->thread_info
.volume_changes_tail
= NULL
;
3605 /* Called from the IO thread. */
3606 static void pa_sink_volume_change_rewind(pa_sink
*s
, size_t nbytes
) {
3607 /* All the queued volume events later than current latency are shifted to happen earlier. */
3608 pa_sink_volume_change
*c
;
3609 pa_volume_t prev_vol
= pa_cvolume_avg(&s
->thread_info
.current_hw_volume
);
3610 pa_usec_t rewound
= pa_bytes_to_usec(nbytes
, &s
->sample_spec
);
3611 pa_usec_t limit
= pa_sink_get_latency_within_thread(s
);
3613 pa_log_debug("latency = %lld", (long long) limit
);
3614 limit
+= pa_rtclock_now() + s
->thread_info
.volume_change_extra_delay
;
3616 PA_LLIST_FOREACH(c
, s
->thread_info
.volume_changes
) {
3617 pa_usec_t modified_limit
= limit
;
3618 if (prev_vol
> pa_cvolume_avg(&c
->hw_volume
))
3619 modified_limit
-= s
->thread_info
.volume_change_safety_margin
;
3621 modified_limit
+= s
->thread_info
.volume_change_safety_margin
;
3622 if (c
->at
> modified_limit
) {
3624 if (c
->at
< modified_limit
)
3625 c
->at
= modified_limit
;
3627 prev_vol
= pa_cvolume_avg(&c
->hw_volume
);
3629 pa_sink_volume_change_apply(s
, NULL
);
3632 /* Called from the main thread */
3633 /* Gets the list of formats supported by the sink. The members and idxset must
3634 * be freed by the caller. */
3635 pa_idxset
* pa_sink_get_formats(pa_sink
*s
) {
3640 if (s
->get_formats
) {
3641 /* Sink supports format query, all is good */
3642 ret
= s
->get_formats(s
);
3644 /* Sink doesn't support format query, so assume it does PCM */
3645 pa_format_info
*f
= pa_format_info_new();
3646 f
->encoding
= PA_ENCODING_PCM
;
3648 ret
= pa_idxset_new(NULL
, NULL
);
3649 pa_idxset_put(ret
, f
, NULL
);
3655 /* Called from the main thread */
3656 /* Allows an external source to set what formats a sink supports if the sink
3657 * permits this. The function makes a copy of the formats on success. */
3658 pa_bool_t
pa_sink_set_formats(pa_sink
*s
, pa_idxset
*formats
) {
3663 /* Sink supports setting formats -- let's give it a shot */
3664 return s
->set_formats(s
, formats
);
3666 /* Sink doesn't support setting this -- bail out */
3670 /* Called from the main thread */
3671 /* Checks if the sink can accept this format */
3672 pa_bool_t
pa_sink_check_format(pa_sink
*s
, pa_format_info
*f
)
3674 pa_idxset
*formats
= NULL
;
3675 pa_bool_t ret
= FALSE
;
3680 formats
= pa_sink_get_formats(s
);
3683 pa_format_info
*finfo_device
;
3686 PA_IDXSET_FOREACH(finfo_device
, formats
, i
) {
3687 if (pa_format_info_is_compatible(finfo_device
, f
)) {
3693 pa_idxset_free(formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);
3699 /* Called from the main thread */
3700 /* Calculates the intersection between formats supported by the sink and
3701 * in_formats, and returns these, in the order of the sink's formats. */
3702 pa_idxset
* pa_sink_check_formats(pa_sink
*s
, pa_idxset
*in_formats
) {
3703 pa_idxset
*out_formats
= pa_idxset_new(NULL
, NULL
), *sink_formats
= NULL
;
3704 pa_format_info
*f_sink
, *f_in
;
3709 if (!in_formats
|| pa_idxset_isempty(in_formats
))
3712 sink_formats
= pa_sink_get_formats(s
);
3714 PA_IDXSET_FOREACH(f_sink
, sink_formats
, i
) {
3715 PA_IDXSET_FOREACH(f_in
, in_formats
, j
) {
3716 if (pa_format_info_is_compatible(f_sink
, f_in
))
3717 pa_idxset_put(out_formats
, pa_format_info_copy(f_in
), NULL
);
3723 pa_idxset_free(sink_formats
, (pa_free2_cb_t
) pa_format_info_free2
, NULL
);