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, see <http://www.gnu.org/licenses/>.
32 #include <pulse/introspect.h>
33 #include <pulse/format.h>
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/timeval.h>
37 #include <pulse/util.h>
38 #include <pulse/rtclock.h>
39 #include <pulse/internal.h>
41 #include <pulsecore/i18n.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/sample-util.h>
46 #include <pulsecore/mix.h>
47 #include <pulsecore/core-subscribe.h>
48 #include <pulsecore/log.h>
49 #include <pulsecore/macro.h>
50 #include <pulsecore/play-memblockq.h>
51 #include <pulsecore/flist.h>
55 #define MAX_MIX_CHANNELS 32
56 #define MIX_BUFFER_LENGTH (pa_page_size())
57 #define ABSOLUTE_MIN_LATENCY (500)
58 #define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
59 #define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
61 PA_DEFINE_PUBLIC_CLASS(pa_sink, pa_msgobject);
63 struct pa_sink_volume_change {
67 PA_LLIST_FIELDS(pa_sink_volume_change);
70 struct sink_message_set_port {
75 static void sink_free(pa_object *s);
77 static void pa_sink_volume_change_push(pa_sink *s);
78 static void pa_sink_volume_change_flush(pa_sink *s);
79 static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes);
82 static void pa_sink_write_pcm_dump(pa_sink *s, pa_memchunk *chunk)
84 char *dump_time = NULL, *dump_path_surfix = NULL;
85 const char *s_device_api_str, *card_name_str, *device_idx_str;
90 /* open file for dump pcm */
91 if (s->core->pcm_dump & PA_PCM_DUMP_PA_SINK && !s->pcm_dump_fp && s->state == PA_SINK_RUNNING) {
92 pa_gettimeofday(&now);
93 localtime_r(&now.tv_sec, &tm);
94 memset(&datetime[0], 0x00, sizeof(datetime));
95 strftime(&datetime[0], sizeof(datetime), "%H%M%S", &tm);
96 dump_time = pa_sprintf_malloc("%s.%03ld", &datetime[0], now.tv_usec / 1000);
98 if ((s_device_api_str = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_API))) {
99 if (pa_streq(s_device_api_str, "alsa")) {
100 card_name_str = pa_proplist_gets(s->proplist, "alsa.card_name");
101 device_idx_str = pa_proplist_gets(s->proplist, "alsa.device");
102 dump_path_surfix = pa_sprintf_malloc("%s.%s", pa_strnull(card_name_str), pa_strnull(device_idx_str));
104 dump_path_surfix = pa_sprintf_malloc("%s", s_device_api_str);
107 dump_path_surfix = pa_sprintf_malloc("%s", s->name);
110 s->dump_path = pa_sprintf_malloc("%s_%s_pa-sink%d-%s_%dch_%d.raw", PA_PCM_DUMP_PATH_PREFIX, pa_strempty(dump_time),
111 s->index, pa_strempty(dump_path_surfix), s->sample_spec.channels, s->sample_spec.rate);
113 s->pcm_dump_fp = fopen(s->dump_path, "w");
115 pa_log_warn("%s open failed", s->dump_path);
117 pa_log_info("%s opened", s->dump_path);
120 pa_xfree(dump_path_surfix);
121 /* close file for dump pcm when config is changed */
122 } else if (~s->core->pcm_dump & PA_PCM_DUMP_PA_SINK && s->pcm_dump_fp) {
123 fclose(s->pcm_dump_fp);
124 pa_log_info("%s closed", s->dump_path);
125 pa_xfree(s->dump_path);
126 s->pcm_dump_fp = NULL;
130 if (s->pcm_dump_fp) {
133 ptr = pa_memblock_acquire(chunk->memblock);
135 fwrite((uint8_t *)ptr + chunk->index, 1, chunk->length, s->pcm_dump_fp);
137 pa_log_warn("pa_memblock_acquire is failed. ptr is NULL");
139 pa_memblock_release(chunk->memblock);
144 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
148 data->proplist = pa_proplist_new();
149 data->ports = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) pa_device_port_unref);
154 void pa_sink_new_data_set_name(pa_sink_new_data *data, const char *name) {
157 pa_xfree(data->name);
158 data->name = pa_xstrdup(name);
161 void pa_sink_new_data_set_sample_spec(pa_sink_new_data *data, const pa_sample_spec *spec) {
164 if ((data->sample_spec_is_set = !!spec))
165 data->sample_spec = *spec;
168 void pa_sink_new_data_set_channel_map(pa_sink_new_data *data, const pa_channel_map *map) {
171 if ((data->channel_map_is_set = !!map))
172 data->channel_map = *map;
175 void pa_sink_new_data_set_alternate_sample_rate(pa_sink_new_data *data, const uint32_t alternate_sample_rate) {
178 data->alternate_sample_rate_is_set = true;
179 data->alternate_sample_rate = alternate_sample_rate;
182 void pa_sink_new_data_set_volume(pa_sink_new_data *data, const pa_cvolume *volume) {
185 if ((data->volume_is_set = !!volume))
186 data->volume = *volume;
189 void pa_sink_new_data_set_muted(pa_sink_new_data *data, bool mute) {
192 data->muted_is_set = true;
196 void pa_sink_new_data_set_port(pa_sink_new_data *data, const char *port) {
199 pa_xfree(data->active_port);
200 data->active_port = pa_xstrdup(port);
203 void pa_sink_new_data_done(pa_sink_new_data *data) {
206 pa_proplist_free(data->proplist);
209 pa_hashmap_free(data->ports);
211 pa_xfree(data->name);
212 pa_xfree(data->active_port);
215 /* Called from main context */
216 static void reset_callbacks(pa_sink *s) {
220 s->get_volume = NULL;
221 s->set_volume = NULL;
222 s->write_volume = NULL;
225 s->request_rewind = NULL;
226 s->update_requested_latency = NULL;
228 s->get_formats = NULL;
229 s->set_formats = NULL;
230 s->reconfigure = NULL;
233 /* Called from main context */
234 pa_sink* pa_sink_new(
236 pa_sink_new_data *data,
237 pa_sink_flags_t flags) {
241 char st[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX];
242 pa_source_new_data source_data;
248 pa_assert(data->name);
249 pa_assert_ctl_context();
251 s = pa_msgobject_new(pa_sink);
253 if (!(name = pa_namereg_register(core, data->name, PA_NAMEREG_SINK, s, data->namereg_fail))) {
254 pa_log_debug("Failed to register name %s.", data->name);
259 pa_sink_new_data_set_name(data, name);
261 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_NEW], data) < 0) {
263 pa_namereg_unregister(core, name);
267 /* FIXME, need to free s here on failure */
269 pa_return_null_if_fail(!data->driver || pa_utf8_valid(data->driver));
270 pa_return_null_if_fail(data->name && pa_utf8_valid(data->name) && data->name[0]);
272 pa_return_null_if_fail(data->sample_spec_is_set && pa_sample_spec_valid(&data->sample_spec));
274 if (!data->channel_map_is_set)
275 pa_return_null_if_fail(pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT));
277 pa_return_null_if_fail(pa_channel_map_valid(&data->channel_map));
278 pa_return_null_if_fail(data->channel_map.channels == data->sample_spec.channels);
280 /* FIXME: There should probably be a general function for checking whether
281 * the sink volume is allowed to be set, like there is for sink inputs. */
282 pa_assert(!data->volume_is_set || !(flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
284 if (!data->volume_is_set) {
285 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
286 data->save_volume = false;
289 pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
290 pa_return_null_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec));
292 if (!data->muted_is_set)
296 pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->card->proplist);
298 pa_device_init_description(data->proplist, data->card);
299 pa_device_init_icon(data->proplist, true);
300 pa_device_init_intended_roles(data->proplist);
302 if (!data->active_port) {
303 pa_device_port *p = pa_device_port_find_best(data->ports);
305 pa_sink_new_data_set_port(data, p->name);
308 if (pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_FIXATE], data) < 0) {
310 pa_namereg_unregister(core, name);
314 s->parent.parent.free = sink_free;
315 s->parent.process_msg = pa_sink_process_msg;
318 s->state = PA_SINK_INIT;
321 s->suspend_cause = data->suspend_cause;
322 pa_sink_set_mixer_dirty(s, false);
323 s->name = pa_xstrdup(name);
324 s->proplist = pa_proplist_copy(data->proplist);
325 s->driver = pa_xstrdup(pa_path_get_filename(data->driver));
326 s->module = data->module;
327 s->card = data->card;
329 s->priority = pa_device_init_priority(s->proplist);
331 s->sample_spec = data->sample_spec;
332 s->channel_map = data->channel_map;
333 s->default_sample_rate = s->sample_spec.rate;
335 if (data->alternate_sample_rate_is_set)
336 s->alternate_sample_rate = data->alternate_sample_rate;
338 s->alternate_sample_rate = s->core->alternate_sample_rate;
340 s->avoid_resampling = data->avoid_resampling;
342 s->origin_avoid_resampling = data->avoid_resampling;
343 s->selected_sample_format = s->sample_spec.format;
344 s->selected_sample_rate = s->sample_spec.rate;
347 s->inputs = pa_idxset_new(NULL, NULL);
349 s->input_to_master = NULL;
351 s->reference_volume = s->real_volume = data->volume;
352 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
353 s->base_volume = PA_VOLUME_NORM;
354 s->n_volume_steps = PA_VOLUME_NORM+1;
355 s->muted = data->muted;
356 s->refresh_volume = s->refresh_muted = false;
363 /* As a minor optimization we just steal the list instead of
365 s->ports = data->ports;
368 s->active_port = NULL;
369 s->save_port = false;
371 if (data->active_port)
372 if ((s->active_port = pa_hashmap_get(s->ports, data->active_port)))
373 s->save_port = data->save_port;
375 /* Hopefully the active port has already been assigned in the previous call
376 to pa_device_port_find_best, but better safe than sorry */
378 s->active_port = pa_device_port_find_best(s->ports);
381 s->port_latency_offset = s->active_port->latency_offset;
383 s->port_latency_offset = 0;
385 s->save_volume = data->save_volume;
386 s->save_muted = data->save_muted;
387 #ifdef TIZEN_PCM_DUMP
388 s->pcm_dump_fp = NULL;
392 pa_silence_memchunk_get(
393 &core->silence_cache,
399 s->thread_info.rtpoll = NULL;
400 s->thread_info.inputs = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL,
401 (pa_free_cb_t) pa_sink_input_unref);
402 s->thread_info.soft_volume = s->soft_volume;
403 s->thread_info.soft_muted = s->muted;
404 s->thread_info.state = s->state;
405 s->thread_info.rewind_nbytes = 0;
406 s->thread_info.rewind_requested = false;
407 s->thread_info.max_rewind = 0;
408 s->thread_info.max_request = 0;
409 s->thread_info.requested_latency_valid = false;
410 s->thread_info.requested_latency = 0;
411 s->thread_info.min_latency = ABSOLUTE_MIN_LATENCY;
412 s->thread_info.max_latency = ABSOLUTE_MAX_LATENCY;
413 s->thread_info.fixed_latency = flags & PA_SINK_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
415 PA_LLIST_HEAD_INIT(pa_sink_volume_change, s->thread_info.volume_changes);
416 s->thread_info.volume_changes_tail = NULL;
417 pa_sw_cvolume_divide(&s->thread_info.current_hw_volume, &s->real_volume, &s->soft_volume);
418 s->thread_info.volume_change_safety_margin = core->deferred_volume_safety_margin_usec;
419 s->thread_info.volume_change_extra_delay = core->deferred_volume_extra_delay_usec;
420 s->thread_info.port_latency_offset = s->port_latency_offset;
422 /* FIXME: This should probably be moved to pa_sink_put() */
423 pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
426 pa_assert_se(pa_idxset_put(s->card->sinks, s, NULL) >= 0);
428 pt = pa_proplist_to_string_sep(s->proplist, "\n ");
429 pa_log_info("Created sink %u \"%s\" with sample spec %s and channel map %s\n %s",
432 pa_sample_spec_snprint(st, sizeof(st), &s->sample_spec),
433 pa_channel_map_snprint(cm, sizeof(cm), &s->channel_map),
437 pa_source_new_data_init(&source_data);
438 pa_source_new_data_set_sample_spec(&source_data, &s->sample_spec);
439 pa_source_new_data_set_channel_map(&source_data, &s->channel_map);
440 pa_source_new_data_set_alternate_sample_rate(&source_data, s->alternate_sample_rate);
441 source_data.name = pa_sprintf_malloc("%s.monitor", name);
442 source_data.driver = data->driver;
443 source_data.module = data->module;
444 source_data.card = data->card;
446 dn = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
447 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Monitor of %s", dn ? dn : s->name);
448 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "monitor");
450 s->monitor_source = pa_source_new(core, &source_data,
451 ((flags & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
452 ((flags & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0));
454 pa_source_new_data_done(&source_data);
456 if (!s->monitor_source) {
462 s->monitor_source->monitor_of = s;
464 pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
465 pa_source_set_fixed_latency(s->monitor_source, s->thread_info.fixed_latency);
466 pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
471 /* Called from main context */
472 static int sink_set_state(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause) {
475 bool suspend_cause_changed;
480 pa_assert_ctl_context();
482 state_changed = state != s->state;
483 suspend_cause_changed = suspend_cause != s->suspend_cause;
485 if (!state_changed && !suspend_cause_changed)
488 suspending = PA_SINK_IS_OPENED(s->state) && state == PA_SINK_SUSPENDED;
489 resuming = s->state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(state);
491 /* If we are resuming, suspend_cause must be 0. */
492 pa_assert(!resuming || !suspend_cause);
494 /* Here's something to think about: what to do with the suspend cause if
495 * resuming the sink fails? The old suspend cause will be incorrect, so we
496 * can't use that. On the other hand, if we set no suspend cause (as is the
497 * case currently), then it looks strange to have a sink suspended without
498 * any cause. It might be a good idea to add a new "resume failed" suspend
499 * cause, or it might just add unnecessary complexity, given that the
500 * current approach of not setting any suspend cause works well enough. */
502 if (s->set_state && state_changed) {
503 ret = s->set_state(s, state);
504 /* set_state() is allowed to fail only when resuming. */
505 pa_assert(ret >= 0 || resuming);
508 if (ret >= 0 && s->asyncmsgq && state_changed)
509 if ((ret = pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_STATE, PA_UINT_TO_PTR(state), 0, NULL)) < 0) {
510 /* SET_STATE is allowed to fail only when resuming. */
514 s->set_state(s, PA_SINK_SUSPENDED);
517 #ifdef TIZEN_PCM_DUMP
518 /* close file for dump pcm */
519 if (s->pcm_dump_fp && (s->core->pcm_dump_option & PA_PCM_DUMP_OPTION_SEPARATED) && suspending) {
520 fclose(s->pcm_dump_fp);
521 pa_log_info("%s closed", s->dump_path);
522 pa_xfree(s->dump_path);
523 s->pcm_dump_fp = NULL;
526 if (suspend_cause_changed) {
527 char old_cause_buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE];
528 char new_cause_buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE];
530 pa_log_debug("%s: suspend_cause: %s -> %s", s->name, pa_suspend_cause_to_string(s->suspend_cause, old_cause_buf),
531 pa_suspend_cause_to_string(suspend_cause, new_cause_buf));
532 s->suspend_cause = suspend_cause;
539 pa_log_debug("%s: state: %s -> %s", s->name, pa_sink_state_to_string(s->state), pa_sink_state_to_string(state));
542 /* If we enter UNLINKED state, then we don't send change notifications.
543 * pa_sink_unlink() will send unlink notifications instead. */
544 if (state != PA_SINK_UNLINKED) {
545 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], s);
546 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
550 if (suspending || resuming) {
554 /* We're suspending or resuming, tell everyone about it */
556 PA_IDXSET_FOREACH(i, s->inputs, idx)
557 if (s->state == PA_SINK_SUSPENDED &&
558 (i->flags & PA_SINK_INPUT_KILL_ON_SUSPEND))
559 pa_sink_input_kill(i);
561 i->suspend(i, state == PA_SINK_SUSPENDED);
565 if ((suspending || resuming || suspend_cause_changed) && s->monitor_source && state != PA_SINK_UNLINKED)
566 pa_source_sync_suspend(s->monitor_source);
571 void pa_sink_set_get_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
577 void pa_sink_set_set_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
578 pa_sink_flags_t flags;
581 pa_assert(!s->write_volume || cb);
585 /* Save the current flags so we can tell if they've changed */
589 /* The sink implementor is responsible for setting decibel volume support */
590 s->flags |= PA_SINK_HW_VOLUME_CTRL;
592 s->flags &= ~PA_SINK_HW_VOLUME_CTRL;
593 /* See note below in pa_sink_put() about volume sharing and decibel volumes */
594 pa_sink_enable_decibel_volume(s, !(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
597 /* If the flags have changed after init, let any clients know via a change event */
598 if (s->state != PA_SINK_INIT && flags != s->flags)
599 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
602 void pa_sink_set_write_volume_callback(pa_sink *s, pa_sink_cb_t cb) {
603 pa_sink_flags_t flags;
606 pa_assert(!cb || s->set_volume);
608 s->write_volume = cb;
610 /* Save the current flags so we can tell if they've changed */
614 s->flags |= PA_SINK_DEFERRED_VOLUME;
616 s->flags &= ~PA_SINK_DEFERRED_VOLUME;
618 /* If the flags have changed after init, let any clients know via a change event */
619 if (s->state != PA_SINK_INIT && flags != s->flags)
620 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
623 void pa_sink_set_get_mute_callback(pa_sink *s, pa_sink_get_mute_cb_t cb) {
629 void pa_sink_set_set_mute_callback(pa_sink *s, pa_sink_cb_t cb) {
630 pa_sink_flags_t flags;
636 /* Save the current flags so we can tell if they've changed */
640 s->flags |= PA_SINK_HW_MUTE_CTRL;
642 s->flags &= ~PA_SINK_HW_MUTE_CTRL;
644 /* If the flags have changed after init, let any clients know via a change event */
645 if (s->state != PA_SINK_INIT && flags != s->flags)
646 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
649 static void enable_flat_volume(pa_sink *s, bool enable) {
650 pa_sink_flags_t flags;
654 /* Always follow the overall user preference here */
655 enable = enable && s->core->flat_volumes;
657 /* Save the current flags so we can tell if they've changed */
661 s->flags |= PA_SINK_FLAT_VOLUME;
663 s->flags &= ~PA_SINK_FLAT_VOLUME;
665 /* If the flags have changed after init, let any clients know via a change event */
666 if (s->state != PA_SINK_INIT && flags != s->flags)
667 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
670 void pa_sink_enable_decibel_volume(pa_sink *s, bool enable) {
671 pa_sink_flags_t flags;
675 /* Save the current flags so we can tell if they've changed */
679 s->flags |= PA_SINK_DECIBEL_VOLUME;
680 enable_flat_volume(s, true);
682 s->flags &= ~PA_SINK_DECIBEL_VOLUME;
683 enable_flat_volume(s, false);
686 /* If the flags have changed after init, let any clients know via a change event */
687 if (s->state != PA_SINK_INIT && flags != s->flags)
688 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
691 /* Called from main context */
692 void pa_sink_put(pa_sink* s) {
693 pa_sink_assert_ref(s);
694 pa_assert_ctl_context();
696 pa_assert(s->state == PA_SINK_INIT);
697 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) || pa_sink_is_filter(s));
699 /* The following fields must be initialized properly when calling _put() */
700 pa_assert(s->asyncmsgq);
701 pa_assert(s->thread_info.min_latency <= s->thread_info.max_latency);
703 /* Generally, flags should be initialized via pa_sink_new(). As a
704 * special exception we allow some volume related flags to be set
705 * between _new() and _put() by the callback setter functions above.
707 * Thus we implement a couple safeguards here which ensure the above
708 * setters were used (or at least the implementor made manual changes
709 * in a compatible way).
711 * Note: All of these flags set here can change over the life time
713 pa_assert(!(s->flags & PA_SINK_HW_VOLUME_CTRL) || s->set_volume);
714 pa_assert(!(s->flags & PA_SINK_DEFERRED_VOLUME) || s->write_volume);
715 pa_assert(!(s->flags & PA_SINK_HW_MUTE_CTRL) || s->set_mute);
717 /* XXX: Currently decibel volume is disabled for all sinks that use volume
718 * sharing. When the master sink supports decibel volume, it would be good
719 * to have the flag also in the filter sink, but currently we don't do that
720 * so that the flags of the filter sink never change when it's moved from
721 * a master sink to another. One solution for this problem would be to
722 * remove user-visible volume altogether from filter sinks when volume
723 * sharing is used, but the current approach was easier to implement... */
724 /* We always support decibel volumes in software, otherwise we leave it to
725 * the sink implementor to set this flag as needed.
727 * Note: This flag can also change over the life time of the sink. */
728 if (!(s->flags & PA_SINK_HW_VOLUME_CTRL) && !(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
729 pa_sink_enable_decibel_volume(s, true);
730 s->soft_volume = s->reference_volume;
733 /* If the sink implementor support DB volumes by itself, we should always
734 * try and enable flat volumes too */
735 if ((s->flags & PA_SINK_DECIBEL_VOLUME))
736 enable_flat_volume(s, true);
738 if (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) {
739 pa_sink *root_sink = pa_sink_get_master(s);
741 pa_assert(root_sink);
743 s->reference_volume = root_sink->reference_volume;
744 pa_cvolume_remap(&s->reference_volume, &root_sink->channel_map, &s->channel_map);
746 s->real_volume = root_sink->real_volume;
747 pa_cvolume_remap(&s->real_volume, &root_sink->channel_map, &s->channel_map);
749 /* We assume that if the sink implementor changed the default
750 * volume he did so in real_volume, because that is the usual
751 * place where he is supposed to place his changes. */
752 s->reference_volume = s->real_volume;
754 s->thread_info.soft_volume = s->soft_volume;
755 s->thread_info.soft_muted = s->muted;
756 pa_sw_cvolume_divide(&s->thread_info.current_hw_volume, &s->real_volume, &s->soft_volume);
758 pa_assert((s->flags & PA_SINK_HW_VOLUME_CTRL)
759 || (s->base_volume == PA_VOLUME_NORM
760 && ((s->flags & PA_SINK_DECIBEL_VOLUME || (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)))));
761 pa_assert(!(s->flags & PA_SINK_DECIBEL_VOLUME) || s->n_volume_steps == PA_VOLUME_NORM+1);
762 pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == !(s->thread_info.fixed_latency == 0));
763 pa_assert(!(s->flags & PA_SINK_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_LATENCY));
764 pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_DYNAMIC_LATENCY));
766 pa_assert(s->monitor_source->thread_info.fixed_latency == s->thread_info.fixed_latency);
767 pa_assert(s->monitor_source->thread_info.min_latency == s->thread_info.min_latency);
768 pa_assert(s->monitor_source->thread_info.max_latency == s->thread_info.max_latency);
770 if (s->suspend_cause)
771 pa_assert_se(sink_set_state(s, PA_SINK_SUSPENDED, s->suspend_cause) == 0);
773 pa_assert_se(sink_set_state(s, PA_SINK_IDLE, 0) == 0);
775 pa_source_put(s->monitor_source);
777 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
778 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
780 /* This function must be called after the PA_CORE_HOOK_SINK_PUT hook,
781 * because module-switch-on-connect needs to know the old default sink */
782 pa_core_update_default_sink(s->core);
785 /* Called from main context */
786 void pa_sink_unlink(pa_sink* s) {
788 pa_sink_input *i, PA_UNUSED *j = NULL;
790 pa_sink_assert_ref(s);
791 pa_assert_ctl_context();
793 /* Please note that pa_sink_unlink() does more than simply
794 * reversing pa_sink_put(). It also undoes the registrations
795 * already done in pa_sink_new()! */
797 if (s->unlink_requested)
800 s->unlink_requested = true;
802 linked = PA_SINK_IS_LINKED(s->state);
805 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK], s);
807 if (s->state != PA_SINK_UNLINKED)
808 pa_namereg_unregister(s->core, s->name);
809 pa_idxset_remove_by_data(s->core->sinks, s, NULL);
811 pa_core_update_default_sink(s->core);
814 pa_idxset_remove_by_data(s->card->sinks, s, NULL);
816 while ((i = pa_idxset_first(s->inputs, NULL))) {
818 pa_sink_input_kill(i);
823 sink_set_state(s, PA_SINK_UNLINKED, 0);
825 s->state = PA_SINK_UNLINKED;
829 if (s->monitor_source)
830 pa_source_unlink(s->monitor_source);
833 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
834 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], s);
838 /* Called from main context */
839 static void sink_free(pa_object *o) {
840 pa_sink *s = PA_SINK(o);
843 pa_assert_ctl_context();
844 pa_assert(pa_sink_refcnt(s) == 0);
845 pa_assert(!PA_SINK_IS_LINKED(s->state));
847 pa_log_info("Freeing sink %u \"%s\"", s->index, s->name);
849 pa_sink_volume_change_flush(s);
851 if (s->monitor_source) {
852 pa_source_unref(s->monitor_source);
853 s->monitor_source = NULL;
856 pa_idxset_free(s->inputs, NULL);
857 pa_hashmap_free(s->thread_info.inputs);
859 if (s->silence.memblock)
860 pa_memblock_unref(s->silence.memblock);
866 pa_proplist_free(s->proplist);
869 pa_hashmap_free(s->ports);
871 #ifdef TIZEN_PCM_DUMP
872 /* close file for dump pcm */
873 if (s->pcm_dump_fp) {
874 fclose(s->pcm_dump_fp);
875 pa_log_info("%s closed", s->dump_path);
876 pa_xfree(s->dump_path);
877 s->pcm_dump_fp = NULL;
883 /* Called from main context, and not while the IO thread is active, please */
884 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
885 pa_sink_assert_ref(s);
886 pa_assert_ctl_context();
890 if (s->monitor_source)
891 pa_source_set_asyncmsgq(s->monitor_source, q);
894 /* Called from main context, and not while the IO thread is active, please */
895 void pa_sink_update_flags(pa_sink *s, pa_sink_flags_t mask, pa_sink_flags_t value) {
896 pa_sink_flags_t old_flags;
897 pa_sink_input *input;
900 pa_sink_assert_ref(s);
901 pa_assert_ctl_context();
903 /* For now, allow only a minimal set of flags to be changed. */
904 pa_assert((mask & ~(PA_SINK_DYNAMIC_LATENCY|PA_SINK_LATENCY)) == 0);
906 old_flags = s->flags;
907 s->flags = (s->flags & ~mask) | (value & mask);
909 if (s->flags == old_flags)
912 if ((s->flags & PA_SINK_LATENCY) != (old_flags & PA_SINK_LATENCY))
913 pa_log_debug("Sink %s: LATENCY flag %s.", s->name, (s->flags & PA_SINK_LATENCY) ? "enabled" : "disabled");
915 if ((s->flags & PA_SINK_DYNAMIC_LATENCY) != (old_flags & PA_SINK_DYNAMIC_LATENCY))
916 pa_log_debug("Sink %s: DYNAMIC_LATENCY flag %s.",
917 s->name, (s->flags & PA_SINK_DYNAMIC_LATENCY) ? "enabled" : "disabled");
919 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
920 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_FLAGS_CHANGED], s);
922 if (s->monitor_source)
923 pa_source_update_flags(s->monitor_source,
924 ((mask & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
925 ((mask & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0),
926 ((value & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
927 ((value & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0));
929 PA_IDXSET_FOREACH(input, s->inputs, idx) {
930 if (input->origin_sink)
931 pa_sink_update_flags(input->origin_sink, mask, value);
935 /* Called from IO context, or before _put() from main context */
936 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
937 pa_sink_assert_ref(s);
938 pa_sink_assert_io_context(s);
940 s->thread_info.rtpoll = p;
942 if (s->monitor_source)
943 pa_source_set_rtpoll(s->monitor_source, p);
946 /* Called from main context */
947 int pa_sink_update_status(pa_sink*s) {
948 pa_sink_assert_ref(s);
949 pa_assert_ctl_context();
950 pa_assert(PA_SINK_IS_LINKED(s->state));
952 if (s->state == PA_SINK_SUSPENDED)
955 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE, 0);
958 /* Called from any context - must be threadsafe */
959 void pa_sink_set_mixer_dirty(pa_sink *s, bool is_dirty) {
960 pa_atomic_store(&s->mixer_dirty, is_dirty ? 1 : 0);
963 /* Called from main context */
964 int pa_sink_suspend(pa_sink *s, bool suspend, pa_suspend_cause_t cause) {
965 pa_suspend_cause_t merged_cause;
967 pa_sink_assert_ref(s);
968 pa_assert_ctl_context();
969 pa_assert(PA_SINK_IS_LINKED(s->state));
970 pa_assert(cause != 0);
973 merged_cause = s->suspend_cause | cause;
975 merged_cause = s->suspend_cause & ~cause;
977 if (!(merged_cause & PA_SUSPEND_SESSION) && (pa_atomic_load(&s->mixer_dirty) != 0)) {
978 /* This might look racy but isn't: If somebody sets mixer_dirty exactly here,
979 it'll be handled just fine. */
980 pa_sink_set_mixer_dirty(s, false);
981 pa_log_debug("Mixer is now accessible. Updating alsa mixer settings.");
982 if (s->active_port && s->set_port) {
983 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
984 struct sink_message_set_port msg = { .port = s->active_port, .ret = 0 };
985 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_PORT, &msg, 0, NULL) == 0);
988 s->set_port(s, s->active_port);
999 return sink_set_state(s, PA_SINK_SUSPENDED, merged_cause);
1001 return sink_set_state(s, pa_sink_used_by(s) ? PA_SINK_RUNNING : PA_SINK_IDLE, 0);
1004 /* Called from main context */
1005 pa_queue *pa_sink_move_all_start(pa_sink *s, pa_queue *q) {
1006 pa_sink_input *i, *n;
1009 pa_sink_assert_ref(s);
1010 pa_assert_ctl_context();
1011 pa_assert(PA_SINK_IS_LINKED(s->state));
1016 for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = n) {
1017 n = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx));
1019 pa_sink_input_ref(i);
1021 if (pa_sink_input_start_move(i) >= 0)
1022 pa_queue_push(q, i);
1024 pa_sink_input_unref(i);
1030 /* Called from main context */
1031 void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, bool save) {
1034 pa_sink_assert_ref(s);
1035 pa_assert_ctl_context();
1036 pa_assert(PA_SINK_IS_LINKED(s->state));
1039 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
1040 if (PA_SINK_INPUT_IS_LINKED(i->state)) {
1041 if (pa_sink_input_finish_move(i, s, save) < 0)
1042 pa_sink_input_fail_move(i);
1045 pa_sink_input_unref(i);
1048 pa_queue_free(q, NULL);
1051 /* Called from main context */
1052 void pa_sink_move_all_fail(pa_queue *q) {
1055 pa_assert_ctl_context();
1058 while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
1059 pa_sink_input_fail_move(i);
1060 pa_sink_input_unref(i);
1063 pa_queue_free(q, NULL);
1066 /* Called from IO thread context */
1067 size_t pa_sink_process_input_underruns(pa_sink *s, size_t left_to_play) {
1072 pa_sink_assert_ref(s);
1073 pa_sink_assert_io_context(s);
1075 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
1076 size_t uf = i->thread_info.underrun_for_sink;
1078 /* Propagate down the filter tree */
1079 if (i->origin_sink) {
1080 size_t filter_result, left_to_play_origin;
1082 /* The recursive call works in the origin sink domain ... */
1083 left_to_play_origin = pa_convert_size(left_to_play, &i->sink->sample_spec, &i->origin_sink->sample_spec);
1085 /* .. and returns the time to sleep before waking up. We need the
1086 * underrun duration for comparisons, so we undo the subtraction on
1087 * the return value... */
1088 filter_result = left_to_play_origin - pa_sink_process_input_underruns(i->origin_sink, left_to_play_origin);
1090 /* ... and convert it back to the master sink domain */
1091 filter_result = pa_convert_size(filter_result, &i->origin_sink->sample_spec, &i->sink->sample_spec);
1093 /* Remember the longest underrun so far */
1094 if (filter_result > result)
1095 result = filter_result;
1099 /* No underrun here, move on */
1101 } else if (uf >= left_to_play) {
1102 /* The sink has possibly consumed all the data the sink input provided */
1103 pa_sink_input_process_underrun(i);
1104 } else if (uf > result) {
1105 /* Remember the longest underrun so far */
1111 pa_log_debug("%s: Found underrun %ld bytes ago (%ld bytes ahead in playback buffer)", s->name,
1112 (long) result, (long) left_to_play - result);
1113 return left_to_play - result;
1116 /* Called from IO thread context */
1117 void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
1121 pa_sink_assert_ref(s);
1122 pa_sink_assert_io_context(s);
1123 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1125 /* If nobody requested this and this is actually no real rewind
1126 * then we can short cut this. Please note that this means that
1127 * not all rewind requests triggered upstream will always be
1128 * translated in actual requests! */
1129 if (!s->thread_info.rewind_requested && nbytes <= 0)
1132 s->thread_info.rewind_nbytes = 0;
1133 s->thread_info.rewind_requested = false;
1136 pa_log_debug("Processing rewind...");
1137 if (s->flags & PA_SINK_DEFERRED_VOLUME)
1138 pa_sink_volume_change_rewind(s, nbytes);
1139 #ifdef TIZEN_PCM_DUMP
1142 fseeko(s->pcm_dump_fp, (off_t)nbytes * (-1), SEEK_CUR);
1146 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
1147 pa_sink_input_assert_ref(i);
1148 pa_sink_input_process_rewind(i, nbytes);
1152 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
1153 pa_source_process_rewind(s->monitor_source, nbytes);
1157 /* Called from IO thread context */
1158 static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, unsigned maxinfo) {
1162 size_t mixlength = *length;
1164 pa_sink_assert_ref(s);
1165 pa_sink_assert_io_context(s);
1168 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
1169 pa_sink_input_assert_ref(i);
1171 pa_sink_input_peek(i, *length, &info->chunk, &info->volume);
1173 if (mixlength == 0 || info->chunk.length < mixlength)
1174 mixlength = info->chunk.length;
1176 if (pa_memblock_is_silence(info->chunk.memblock)) {
1177 pa_memblock_unref(info->chunk.memblock);
1181 info->userdata = pa_sink_input_ref(i);
1183 pa_assert(info->chunk.memblock);
1184 pa_assert(info->chunk.length > 0);
1192 *length = mixlength;
1197 /* Called from IO thread context */
1198 static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) {
1202 unsigned n_unreffed = 0;
1204 pa_sink_assert_ref(s);
1205 pa_sink_assert_io_context(s);
1207 pa_assert(result->memblock);
1208 pa_assert(result->length > 0);
1210 /* We optimize for the case where the order of the inputs has not changed */
1212 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
1214 pa_mix_info* m = NULL;
1216 pa_sink_input_assert_ref(i);
1218 /* Let's try to find the matching entry info the pa_mix_info array */
1219 for (j = 0; j < n; j ++) {
1221 if (info[p].userdata == i) {
1231 /* Drop read data */
1232 pa_sink_input_drop(i, result->length);
1234 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state)) {
1236 if (pa_hashmap_size(i->thread_info.direct_outputs) > 0) {
1237 void *ostate = NULL;
1238 pa_source_output *o;
1241 if (m && m->chunk.memblock) {
1243 pa_memblock_ref(c.memblock);
1244 pa_assert(result->length <= c.length);
1245 c.length = result->length;
1247 pa_memchunk_make_writable(&c, 0);
1248 pa_volume_memchunk(&c, &s->sample_spec, &m->volume);
1251 pa_memblock_ref(c.memblock);
1252 pa_assert(result->length <= c.length);
1253 c.length = result->length;
1256 while ((o = pa_hashmap_iterate(i->thread_info.direct_outputs, &ostate, NULL))) {
1257 pa_source_output_assert_ref(o);
1258 pa_assert(o->direct_on_input == i);
1259 pa_source_post_direct(s->monitor_source, o, &c);
1262 pa_memblock_unref(c.memblock);
1267 if (m->chunk.memblock) {
1268 pa_memblock_unref(m->chunk.memblock);
1269 pa_memchunk_reset(&m->chunk);
1272 pa_sink_input_unref(m->userdata);
1279 /* Now drop references to entries that are included in the
1280 * pa_mix_info array but don't exist anymore */
1282 if (n_unreffed < n) {
1283 for (; n > 0; info++, n--) {
1285 pa_sink_input_unref(info->userdata);
1286 if (info->chunk.memblock)
1287 pa_memblock_unref(info->chunk.memblock);
1291 if (s->monitor_source && PA_SOURCE_IS_LINKED(s->monitor_source->thread_info.state))
1292 pa_source_post(s->monitor_source, result);
1295 /* Called from IO thread context */
1296 void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
1297 pa_mix_info info[MAX_MIX_CHANNELS];
1299 size_t block_size_max;
1301 pa_sink_assert_ref(s);
1302 pa_sink_assert_io_context(s);
1303 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1304 pa_assert(pa_frame_aligned(length, &s->sample_spec));
1307 pa_assert(!s->thread_info.rewind_requested);
1308 pa_assert(s->thread_info.rewind_nbytes == 0);
1310 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1311 result->memblock = pa_memblock_ref(s->silence.memblock);
1312 result->index = s->silence.index;
1313 result->length = PA_MIN(s->silence.length, length);
1320 length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec);
1322 block_size_max = pa_mempool_block_size_max(s->core->mempool);
1323 if (length > block_size_max)
1324 length = pa_frame_align(block_size_max, &s->sample_spec);
1326 pa_assert(length > 0);
1328 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
1332 *result = s->silence;
1333 pa_memblock_ref(result->memblock);
1335 if (result->length > length)
1336 result->length = length;
1338 } else if (n == 1) {
1341 *result = info[0].chunk;
1342 pa_memblock_ref(result->memblock);
1344 if (result->length > length)
1345 result->length = length;
1347 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
1349 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume)) {
1350 pa_memblock_unref(result->memblock);
1351 pa_silence_memchunk_get(&s->core->silence_cache,
1356 } else if (!pa_cvolume_is_norm(&volume)) {
1357 pa_memchunk_make_writable(result, 0);
1358 pa_volume_memchunk(result, &s->sample_spec, &volume);
1362 result->memblock = pa_memblock_new(s->core->mempool, length);
1364 ptr = pa_memblock_acquire(result->memblock);
1365 result->length = pa_mix(info, n,
1368 &s->thread_info.soft_volume,
1369 s->thread_info.soft_muted);
1370 pa_memblock_release(result->memblock);
1375 inputs_drop(s, info, n, result);
1377 #ifdef TIZEN_PCM_DUMP
1378 pa_sink_write_pcm_dump(s, result);
1383 /* Called from IO thread context */
1384 void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
1385 pa_mix_info info[MAX_MIX_CHANNELS];
1387 size_t length, block_size_max;
1389 pa_sink_assert_ref(s);
1390 pa_sink_assert_io_context(s);
1391 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1393 pa_assert(target->memblock);
1394 pa_assert(target->length > 0);
1395 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
1397 pa_assert(!s->thread_info.rewind_requested);
1398 pa_assert(s->thread_info.rewind_nbytes == 0);
1400 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1401 pa_silence_memchunk(target, &s->sample_spec);
1407 length = target->length;
1408 block_size_max = pa_mempool_block_size_max(s->core->mempool);
1409 if (length > block_size_max)
1410 length = pa_frame_align(block_size_max, &s->sample_spec);
1412 pa_assert(length > 0);
1414 n = fill_mix_info(s, &length, info, MAX_MIX_CHANNELS);
1417 if (target->length > length)
1418 target->length = length;
1420 pa_silence_memchunk(target, &s->sample_spec);
1421 } else if (n == 1) {
1424 if (target->length > length)
1425 target->length = length;
1427 pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume);
1429 if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume))
1430 pa_silence_memchunk(target, &s->sample_spec);
1434 vchunk = info[0].chunk;
1435 pa_memblock_ref(vchunk.memblock);
1437 if (vchunk.length > length)
1438 vchunk.length = length;
1440 if (!pa_cvolume_is_norm(&volume)) {
1441 pa_memchunk_make_writable(&vchunk, 0);
1442 pa_volume_memchunk(&vchunk, &s->sample_spec, &volume);
1445 pa_memchunk_memcpy(target, &vchunk);
1446 pa_memblock_unref(vchunk.memblock);
1452 ptr = pa_memblock_acquire(target->memblock);
1454 target->length = pa_mix(info, n,
1455 (uint8_t*) ptr + target->index, length,
1457 &s->thread_info.soft_volume,
1458 s->thread_info.soft_muted);
1460 pa_memblock_release(target->memblock);
1463 inputs_drop(s, info, n, target);
1465 #ifdef TIZEN_PCM_DUMP
1466 pa_sink_write_pcm_dump(s, target);
1471 /* Called from IO thread context */
1472 void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
1476 pa_sink_assert_ref(s);
1477 pa_sink_assert_io_context(s);
1478 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1480 pa_assert(target->memblock);
1481 pa_assert(target->length > 0);
1482 pa_assert(pa_frame_aligned(target->length, &s->sample_spec));
1484 pa_assert(!s->thread_info.rewind_requested);
1485 pa_assert(s->thread_info.rewind_nbytes == 0);
1487 if (s->thread_info.state == PA_SINK_SUSPENDED) {
1488 pa_silence_memchunk(target, &s->sample_spec);
1501 pa_sink_render_into(s, &chunk);
1510 /* Called from IO thread context */
1511 void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
1512 pa_sink_assert_ref(s);
1513 pa_sink_assert_io_context(s);
1514 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1515 pa_assert(length > 0);
1516 pa_assert(pa_frame_aligned(length, &s->sample_spec));
1519 pa_assert(!s->thread_info.rewind_requested);
1520 pa_assert(s->thread_info.rewind_nbytes == 0);
1524 pa_sink_render(s, length, result);
1526 if (result->length < length) {
1529 pa_memchunk_make_writable(result, length);
1531 chunk.memblock = result->memblock;
1532 chunk.index = result->index + result->length;
1533 chunk.length = length - result->length;
1535 pa_sink_render_into_full(s, &chunk);
1537 result->length = length;
1543 /* Called from main thread */
1544 void pa_sink_reconfigure(pa_sink *s, pa_sample_spec *spec, bool passthrough) {
1545 pa_sample_spec desired_spec;
1546 uint32_t default_rate = s->default_sample_rate;
1547 uint32_t alternate_rate = s->alternate_sample_rate;
1550 bool default_rate_is_usable = false;
1551 bool alternate_rate_is_usable = false;
1552 bool avoid_resampling = s->avoid_resampling;
1555 if (pa_sample_spec_equal(spec, &s->sample_spec))
1559 if (!s->reconfigure)
1563 if (PA_UNLIKELY(default_rate == alternate_rate && !passthrough && !avoid_resampling)) {
1564 pa_log_debug("Default and alternate sample rates are the same, so there is no point in switching.");
1569 if (PA_SINK_IS_RUNNING(s->state)) {
1570 pa_log_info("Cannot update sample spec, SINK_IS_RUNNING, will keep using %s and %u Hz",
1571 pa_sample_format_to_string(s->sample_spec.format), s->sample_spec.rate);
1575 if (s->monitor_source) {
1576 if (PA_SOURCE_IS_RUNNING(s->monitor_source->state) == true) {
1577 pa_log_info("Cannot update sample spec, monitor source is RUNNING");
1582 if (PA_UNLIKELY(!pa_sample_spec_valid(spec)))
1585 desired_spec = s->sample_spec;
1588 if (!avoid_resampling) {
1589 default_rate = alternate_rate = s->selected_sample_rate;
1590 desired_spec.format = s->selected_sample_format;
1594 /* We have to try to use the sink input format and rate */
1595 desired_spec.format = spec->format;
1596 desired_spec.rate = spec->rate;
1598 } else if (avoid_resampling) {
1599 /* We just try to set the sink input's sample rate if it's not too low */
1600 if (spec->rate >= default_rate || spec->rate >= alternate_rate)
1601 desired_spec.rate = spec->rate;
1602 desired_spec.format = spec->format;
1604 } else if (default_rate == spec->rate || alternate_rate == spec->rate) {
1605 /* We can directly try to use this rate */
1606 desired_spec.rate = spec->rate;
1610 if (desired_spec.rate != spec->rate) {
1611 /* See if we can pick a rate that results in less resampling effort */
1612 if (default_rate % 11025 == 0 && spec->rate % 11025 == 0)
1613 default_rate_is_usable = true;
1614 if (default_rate % 4000 == 0 && spec->rate % 4000 == 0)
1615 default_rate_is_usable = true;
1616 if (alternate_rate % 11025 == 0 && spec->rate % 11025 == 0)
1617 alternate_rate_is_usable = true;
1618 if (alternate_rate % 4000 == 0 && spec->rate % 4000 == 0)
1619 alternate_rate_is_usable = true;
1621 if (alternate_rate_is_usable && !default_rate_is_usable)
1622 desired_spec.rate = alternate_rate;
1624 desired_spec.rate = default_rate;
1627 if (pa_sample_spec_equal(&desired_spec, &s->sample_spec) && passthrough == pa_sink_is_passthrough(s))
1630 pa_log_info("desired spec is same as sink->sample_spec");
1637 if (!passthrough && pa_sink_used_by(s) > 0)
1640 pa_log_debug("Suspending sink %s due to changing format, desired format = %s rate = %u",
1641 s->name, pa_sample_format_to_string(desired_spec.format), desired_spec.rate);
1642 pa_sink_suspend(s, true, PA_SUSPEND_INTERNAL);
1644 s->reconfigure(s, &desired_spec, passthrough);
1646 /* update monitor source as well */
1647 if (s->monitor_source && !passthrough)
1648 pa_source_reconfigure(s->monitor_source, &s->sample_spec, false);
1649 pa_log_info("Reconfigured successfully");
1651 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1652 if (i->state == PA_SINK_INPUT_CORKED)
1653 pa_sink_input_update_resampler(i);
1656 pa_sink_suspend(s, false, PA_SUSPEND_INTERNAL);
1659 /* Called from main thread */
1660 pa_usec_t pa_sink_get_latency(pa_sink *s) {
1663 pa_sink_assert_ref(s);
1664 pa_assert_ctl_context();
1665 pa_assert(PA_SINK_IS_LINKED(s->state));
1667 /* The returned value is supposed to be in the time domain of the sound card! */
1669 if (s->state == PA_SINK_SUSPENDED)
1672 if (!(s->flags & PA_SINK_LATENCY))
1675 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
1677 /* the return value is unsigned, so check that the offset can be added to usec without
1679 if (-s->port_latency_offset <= usec)
1680 usec += s->port_latency_offset;
1684 return (pa_usec_t)usec;
1687 /* Called from IO thread */
1688 int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative) {
1692 pa_sink_assert_ref(s);
1693 pa_sink_assert_io_context(s);
1694 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
1696 /* The returned value is supposed to be in the time domain of the sound card! */
1698 if (s->thread_info.state == PA_SINK_SUSPENDED)
1701 if (!(s->flags & PA_SINK_LATENCY))
1704 o = PA_MSGOBJECT(s);
1706 /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
1708 o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL);
1710 /* If allow_negative is false, the call should only return positive values, */
1711 usec += s->thread_info.port_latency_offset;
1712 if (!allow_negative && usec < 0)
1718 /* Called from the main thread (and also from the IO thread while the main
1719 * thread is waiting).
1721 * When a sink uses volume sharing, it never has the PA_SINK_FLAT_VOLUME flag
1722 * set. Instead, flat volume mode is detected by checking whether the root sink
1723 * has the flag set. */
1724 bool pa_sink_flat_volume_enabled(pa_sink *s) {
1725 pa_sink_assert_ref(s);
1727 s = pa_sink_get_master(s);
1730 return (s->flags & PA_SINK_FLAT_VOLUME);
1735 /* Called from the main thread (and also from the IO thread while the main
1736 * thread is waiting). */
1737 pa_sink *pa_sink_get_master(pa_sink *s) {
1738 pa_sink_assert_ref(s);
1740 while (s && (s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1741 if (PA_UNLIKELY(!s->input_to_master))
1744 s = s->input_to_master->sink;
1750 /* Called from main context */
1751 bool pa_sink_is_filter(pa_sink *s) {
1752 pa_sink_assert_ref(s);
1754 return (s->input_to_master != NULL);
1757 /* Called from main context */
1758 bool pa_sink_is_passthrough(pa_sink *s) {
1759 pa_sink_input *alt_i;
1762 pa_sink_assert_ref(s);
1764 /* one and only one PASSTHROUGH input can possibly be connected */
1765 if (pa_idxset_size(s->inputs) == 1) {
1766 alt_i = pa_idxset_first(s->inputs, &idx);
1768 if (pa_sink_input_is_passthrough(alt_i))
1775 /* Called from main context */
1776 void pa_sink_enter_passthrough(pa_sink *s) {
1779 /* The sink implementation is reconfigured for passthrough in
1780 * pa_sink_reconfigure(). This function sets the PA core objects to
1781 * passthrough mode. */
1783 /* disable the monitor in passthrough mode */
1784 if (s->monitor_source) {
1785 pa_log_debug("Suspending monitor source %s, because the sink is entering the passthrough mode.", s->monitor_source->name);
1786 pa_source_suspend(s->monitor_source, true, PA_SUSPEND_PASSTHROUGH);
1789 /* set the volume to NORM */
1790 s->saved_volume = *pa_sink_get_volume(s, true);
1791 s->saved_save_volume = s->save_volume;
1793 pa_cvolume_set(&volume, s->sample_spec.channels, PA_MIN(s->base_volume, PA_VOLUME_NORM));
1794 pa_sink_set_volume(s, &volume, true, false);
1796 pa_log_debug("Suspending/Restarting sink %s to enter passthrough mode", s->name);
1799 /* Called from main context */
1800 void pa_sink_leave_passthrough(pa_sink *s) {
1801 /* Unsuspend monitor */
1802 if (s->monitor_source) {
1803 pa_log_debug("Resuming monitor source %s, because the sink is leaving the passthrough mode.", s->monitor_source->name);
1804 pa_source_suspend(s->monitor_source, false, PA_SUSPEND_PASSTHROUGH);
1807 /* Restore sink volume to what it was before we entered passthrough mode */
1808 pa_sink_set_volume(s, &s->saved_volume, true, s->saved_save_volume);
1810 pa_cvolume_init(&s->saved_volume);
1811 s->saved_save_volume = false;
1815 /* Called from main context. */
1816 static void compute_reference_ratio(pa_sink_input *i) {
1818 pa_cvolume remapped;
1822 pa_assert(pa_sink_flat_volume_enabled(i->sink));
1825 * Calculates the reference ratio from the sink's reference
1826 * volume. This basically calculates:
1828 * i->reference_ratio = i->volume / i->sink->reference_volume
1831 remapped = i->sink->reference_volume;
1832 pa_cvolume_remap(&remapped, &i->sink->channel_map, &i->channel_map);
1834 ratio = i->reference_ratio;
1836 for (c = 0; c < i->sample_spec.channels; c++) {
1838 /* We don't update when the sink volume is 0 anyway */
1839 if (remapped.values[c] <= PA_VOLUME_MUTED)
1842 /* Don't update the reference ratio unless necessary */
1843 if (pa_sw_volume_multiply(
1845 remapped.values[c]) == i->volume.values[c])
1848 ratio.values[c] = pa_sw_volume_divide(
1849 i->volume.values[c],
1850 remapped.values[c]);
1853 pa_sink_input_set_reference_ratio(i, &ratio);
1856 /* Called from main context. Only called for the root sink in volume sharing
1857 * cases, except for internal recursive calls. */
1858 static void compute_reference_ratios(pa_sink *s) {
1862 pa_sink_assert_ref(s);
1863 pa_assert_ctl_context();
1864 pa_assert(PA_SINK_IS_LINKED(s->state));
1865 pa_assert(pa_sink_flat_volume_enabled(s));
1867 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1868 compute_reference_ratio(i);
1870 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)
1871 && PA_SINK_IS_LINKED(i->origin_sink->state))
1872 compute_reference_ratios(i->origin_sink);
1876 /* Called from main context. Only called for the root sink in volume sharing
1877 * cases, except for internal recursive calls. */
1878 static void compute_real_ratios(pa_sink *s) {
1882 pa_sink_assert_ref(s);
1883 pa_assert_ctl_context();
1884 pa_assert(PA_SINK_IS_LINKED(s->state));
1885 pa_assert(pa_sink_flat_volume_enabled(s));
1887 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1889 pa_cvolume remapped;
1891 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1892 /* The origin sink uses volume sharing, so this input's real ratio
1893 * is handled as a special case - the real ratio must be 0 dB, and
1894 * as a result i->soft_volume must equal i->volume_factor. */
1895 pa_cvolume_reset(&i->real_ratio, i->real_ratio.channels);
1896 i->soft_volume = i->volume_factor;
1898 if (PA_SINK_IS_LINKED(i->origin_sink->state))
1899 compute_real_ratios(i->origin_sink);
1905 * This basically calculates:
1907 * i->real_ratio := i->volume / s->real_volume
1908 * i->soft_volume := i->real_ratio * i->volume_factor
1911 remapped = s->real_volume;
1912 pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
1914 i->real_ratio.channels = i->sample_spec.channels;
1915 i->soft_volume.channels = i->sample_spec.channels;
1917 for (c = 0; c < i->sample_spec.channels; c++) {
1919 if (remapped.values[c] <= PA_VOLUME_MUTED) {
1920 /* We leave i->real_ratio untouched */
1921 i->soft_volume.values[c] = PA_VOLUME_MUTED;
1925 /* Don't lose accuracy unless necessary */
1926 if (pa_sw_volume_multiply(
1927 i->real_ratio.values[c],
1928 remapped.values[c]) != i->volume.values[c])
1930 i->real_ratio.values[c] = pa_sw_volume_divide(
1931 i->volume.values[c],
1932 remapped.values[c]);
1934 i->soft_volume.values[c] = pa_sw_volume_multiply(
1935 i->real_ratio.values[c],
1936 i->volume_factor.values[c]);
1939 /* We don't copy the soft_volume to the thread_info data
1940 * here. That must be done by the caller */
1944 static pa_cvolume *cvolume_remap_minimal_impact(
1946 const pa_cvolume *template,
1947 const pa_channel_map *from,
1948 const pa_channel_map *to) {
1953 pa_assert(template);
1956 pa_assert(pa_cvolume_compatible_with_channel_map(v, from));
1957 pa_assert(pa_cvolume_compatible_with_channel_map(template, to));
1959 /* Much like pa_cvolume_remap(), but tries to minimize impact when
1960 * mapping from sink input to sink volumes:
1962 * If template is a possible remapping from v it is used instead
1963 * of remapping anew.
1965 * If the channel maps don't match we set an all-channel volume on
1966 * the sink to ensure that changing a volume on one stream has no
1967 * effect that cannot be compensated for in another stream that
1968 * does not have the same channel map as the sink. */
1970 if (pa_channel_map_equal(from, to))
1974 if (pa_cvolume_equal(pa_cvolume_remap(&t, to, from), v)) {
1979 pa_cvolume_set(v, to->channels, pa_cvolume_max(v));
1983 /* Called from main thread. Only called for the root sink in volume sharing
1984 * cases, except for internal recursive calls. */
1985 static void get_maximum_input_volume(pa_sink *s, pa_cvolume *max_volume, const pa_channel_map *channel_map) {
1989 pa_sink_assert_ref(s);
1990 pa_assert(max_volume);
1991 pa_assert(channel_map);
1992 pa_assert(pa_sink_flat_volume_enabled(s));
1994 PA_IDXSET_FOREACH(i, s->inputs, idx) {
1995 pa_cvolume remapped;
1997 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
1998 if (PA_SINK_IS_LINKED(i->origin_sink->state))
1999 get_maximum_input_volume(i->origin_sink, max_volume, channel_map);
2001 /* Ignore this input. The origin sink uses volume sharing, so this
2002 * input's volume will be set to be equal to the root sink's real
2003 * volume. Obviously this input's current volume must not then
2004 * affect what the root sink's real volume will be. */
2008 remapped = i->volume;
2009 cvolume_remap_minimal_impact(&remapped, max_volume, &i->channel_map, channel_map);
2010 pa_cvolume_merge(max_volume, max_volume, &remapped);
2014 /* Called from main thread. Only called for the root sink in volume sharing
2015 * cases, except for internal recursive calls. */
2016 static bool has_inputs(pa_sink *s) {
2020 pa_sink_assert_ref(s);
2022 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2023 if (!i->origin_sink || !(i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER) || has_inputs(i->origin_sink))
2030 /* Called from main thread. Only called for the root sink in volume sharing
2031 * cases, except for internal recursive calls. */
2032 static void update_real_volume(pa_sink *s, const pa_cvolume *new_volume, pa_channel_map *channel_map) {
2036 pa_sink_assert_ref(s);
2037 pa_assert(new_volume);
2038 pa_assert(channel_map);
2040 s->real_volume = *new_volume;
2041 pa_cvolume_remap(&s->real_volume, channel_map, &s->channel_map);
2043 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2044 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
2045 if (pa_sink_flat_volume_enabled(s)) {
2046 pa_cvolume new_input_volume;
2048 /* Follow the root sink's real volume. */
2049 new_input_volume = *new_volume;
2050 pa_cvolume_remap(&new_input_volume, channel_map, &i->channel_map);
2051 pa_sink_input_set_volume_direct(i, &new_input_volume);
2052 compute_reference_ratio(i);
2055 if (PA_SINK_IS_LINKED(i->origin_sink->state))
2056 update_real_volume(i->origin_sink, new_volume, channel_map);
2061 /* Called from main thread. Only called for the root sink in shared volume
2063 static void compute_real_volume(pa_sink *s) {
2064 pa_sink_assert_ref(s);
2065 pa_assert_ctl_context();
2066 pa_assert(PA_SINK_IS_LINKED(s->state));
2067 pa_assert(pa_sink_flat_volume_enabled(s));
2068 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2070 /* This determines the maximum volume of all streams and sets
2071 * s->real_volume accordingly. */
2073 if (!has_inputs(s)) {
2074 /* In the special case that we have no sink inputs we leave the
2075 * volume unmodified. */
2076 update_real_volume(s, &s->reference_volume, &s->channel_map);
2080 pa_cvolume_mute(&s->real_volume, s->channel_map.channels);
2082 /* First let's determine the new maximum volume of all inputs
2083 * connected to this sink */
2084 get_maximum_input_volume(s, &s->real_volume, &s->channel_map);
2085 update_real_volume(s, &s->real_volume, &s->channel_map);
2087 /* Then, let's update the real ratios/soft volumes of all inputs
2088 * connected to this sink */
2089 compute_real_ratios(s);
2092 /* Called from main thread. Only called for the root sink in shared volume
2093 * cases, except for internal recursive calls. */
2094 static void propagate_reference_volume(pa_sink *s) {
2098 pa_sink_assert_ref(s);
2099 pa_assert_ctl_context();
2100 pa_assert(PA_SINK_IS_LINKED(s->state));
2101 pa_assert(pa_sink_flat_volume_enabled(s));
2103 /* This is called whenever the sink volume changes that is not
2104 * caused by a sink input volume change. We need to fix up the
2105 * sink input volumes accordingly */
2107 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2108 pa_cvolume new_volume;
2110 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
2111 if (PA_SINK_IS_LINKED(i->origin_sink->state))
2112 propagate_reference_volume(i->origin_sink);
2114 /* Since the origin sink uses volume sharing, this input's volume
2115 * needs to be updated to match the root sink's real volume, but
2116 * that will be done later in update_real_volume(). */
2120 /* This basically calculates:
2122 * i->volume := s->reference_volume * i->reference_ratio */
2124 new_volume = s->reference_volume;
2125 pa_cvolume_remap(&new_volume, &s->channel_map, &i->channel_map);
2126 pa_sw_cvolume_multiply(&new_volume, &new_volume, &i->reference_ratio);
2127 pa_sink_input_set_volume_direct(i, &new_volume);
2131 /* Called from main thread. Only called for the root sink in volume sharing
2132 * cases, except for internal recursive calls. The return value indicates
2133 * whether any reference volume actually changed. */
2134 static bool update_reference_volume(pa_sink *s, const pa_cvolume *v, const pa_channel_map *channel_map, bool save) {
2136 bool reference_volume_changed;
2140 pa_sink_assert_ref(s);
2141 pa_assert(PA_SINK_IS_LINKED(s->state));
2143 pa_assert(channel_map);
2144 pa_assert(pa_cvolume_valid(v));
2147 pa_cvolume_remap(&volume, channel_map, &s->channel_map);
2149 reference_volume_changed = !pa_cvolume_equal(&volume, &s->reference_volume);
2150 pa_sink_set_reference_volume_direct(s, &volume);
2152 s->save_volume = (!reference_volume_changed && s->save_volume) || save;
2154 if (!reference_volume_changed && !(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2155 /* If the root sink's volume doesn't change, then there can't be any
2156 * changes in the other sinks in the sink tree either.
2158 * It's probably theoretically possible that even if the root sink's
2159 * volume changes slightly, some filter sink doesn't change its volume
2160 * due to rounding errors. If that happens, we still want to propagate
2161 * the changed root sink volume to the sinks connected to the
2162 * intermediate sink that didn't change its volume. This theoretical
2163 * possibility is the reason why we have that !(s->flags &
2164 * PA_SINK_SHARE_VOLUME_WITH_MASTER) condition. Probably nobody would
2165 * notice even if we returned here false always if
2166 * reference_volume_changed is false. */
2169 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2170 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)
2171 && PA_SINK_IS_LINKED(i->origin_sink->state))
2172 update_reference_volume(i->origin_sink, v, channel_map, false);
2178 /* Called from main thread */
2179 void pa_sink_set_volume(
2181 const pa_cvolume *volume,
2185 pa_cvolume new_reference_volume;
2188 pa_sink_assert_ref(s);
2189 pa_assert_ctl_context();
2190 pa_assert(PA_SINK_IS_LINKED(s->state));
2191 pa_assert(!volume || pa_cvolume_valid(volume));
2192 pa_assert(volume || pa_sink_flat_volume_enabled(s));
2193 pa_assert(!volume || volume->channels == 1 || pa_cvolume_compatible(volume, &s->sample_spec));
2195 /* make sure we don't change the volume when a PASSTHROUGH input is connected ...
2196 * ... *except* if we're being invoked to reset the volume to ensure 0 dB gain */
2197 if (pa_sink_is_passthrough(s) && (!volume || !pa_cvolume_is_norm(volume))) {
2198 pa_log_warn("Cannot change volume, Sink is connected to PASSTHROUGH input");
2202 /* In case of volume sharing, the volume is set for the root sink first,
2203 * from which it's then propagated to the sharing sinks. */
2204 root_sink = pa_sink_get_master(s);
2206 if (PA_UNLIKELY(!root_sink))
2209 /* As a special exception we accept mono volumes on all sinks --
2210 * even on those with more complex channel maps */
2213 if (pa_cvolume_compatible(volume, &s->sample_spec))
2214 new_reference_volume = *volume;
2216 new_reference_volume = s->reference_volume;
2217 pa_cvolume_scale(&new_reference_volume, pa_cvolume_max(volume));
2220 pa_cvolume_remap(&new_reference_volume, &s->channel_map, &root_sink->channel_map);
2222 if (update_reference_volume(root_sink, &new_reference_volume, &root_sink->channel_map, save)) {
2223 if (pa_sink_flat_volume_enabled(root_sink)) {
2224 /* OK, propagate this volume change back to the inputs */
2225 propagate_reference_volume(root_sink);
2227 /* And now recalculate the real volume */
2228 compute_real_volume(root_sink);
2230 update_real_volume(root_sink, &root_sink->reference_volume, &root_sink->channel_map);
2234 /* If volume is NULL we synchronize the sink's real and
2235 * reference volumes with the stream volumes. */
2237 pa_assert(pa_sink_flat_volume_enabled(root_sink));
2239 /* Ok, let's determine the new real volume */
2240 compute_real_volume(root_sink);
2242 /* Let's 'push' the reference volume if necessary */
2243 pa_cvolume_merge(&new_reference_volume, &s->reference_volume, &root_sink->real_volume);
2244 /* If the sink and its root don't have the same number of channels, we need to remap */
2245 if (s != root_sink && !pa_channel_map_equal(&s->channel_map, &root_sink->channel_map))
2246 pa_cvolume_remap(&new_reference_volume, &s->channel_map, &root_sink->channel_map);
2247 update_reference_volume(root_sink, &new_reference_volume, &root_sink->channel_map, save);
2249 /* Now that the reference volume is updated, we can update the streams'
2250 * reference ratios. */
2251 compute_reference_ratios(root_sink);
2254 if (root_sink->set_volume) {
2255 /* If we have a function set_volume(), then we do not apply a
2256 * soft volume by default. However, set_volume() is free to
2257 * apply one to root_sink->soft_volume */
2259 pa_cvolume_reset(&root_sink->soft_volume, root_sink->sample_spec.channels);
2260 if (!(root_sink->flags & PA_SINK_DEFERRED_VOLUME))
2261 root_sink->set_volume(root_sink);
2264 /* If we have no function set_volume(), then the soft volume
2265 * becomes the real volume */
2266 root_sink->soft_volume = root_sink->real_volume;
2268 /* This tells the sink that soft volume and/or real volume changed */
2270 pa_assert_se(pa_asyncmsgq_send(root_sink->asyncmsgq, PA_MSGOBJECT(root_sink), PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL) == 0);
2273 /* Called from the io thread if sync volume is used, otherwise from the main thread.
2274 * Only to be called by sink implementor */
2275 void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume) {
2277 pa_sink_assert_ref(s);
2278 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2280 if (s->flags & PA_SINK_DEFERRED_VOLUME)
2281 pa_sink_assert_io_context(s);
2283 pa_assert_ctl_context();
2286 pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
2288 s->soft_volume = *volume;
2290 if (PA_SINK_IS_LINKED(s->state) && !(s->flags & PA_SINK_DEFERRED_VOLUME))
2291 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
2293 s->thread_info.soft_volume = s->soft_volume;
2296 /* Called from the main thread. Only called for the root sink in volume sharing
2297 * cases, except for internal recursive calls. */
2298 static void propagate_real_volume(pa_sink *s, const pa_cvolume *old_real_volume) {
2302 pa_sink_assert_ref(s);
2303 pa_assert(old_real_volume);
2304 pa_assert_ctl_context();
2305 pa_assert(PA_SINK_IS_LINKED(s->state));
2307 /* This is called when the hardware's real volume changes due to
2308 * some external event. We copy the real volume into our
2309 * reference volume and then rebuild the stream volumes based on
2310 * i->real_ratio which should stay fixed. */
2312 if (!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)) {
2313 if (pa_cvolume_equal(old_real_volume, &s->real_volume))
2316 /* 1. Make the real volume the reference volume */
2317 update_reference_volume(s, &s->real_volume, &s->channel_map, true);
2320 if (pa_sink_flat_volume_enabled(s)) {
2322 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2323 pa_cvolume new_volume;
2325 /* 2. Since the sink's reference and real volumes are equal
2326 * now our ratios should be too. */
2327 pa_sink_input_set_reference_ratio(i, &i->real_ratio);
2329 /* 3. Recalculate the new stream reference volume based on the
2330 * reference ratio and the sink's reference volume.
2332 * This basically calculates:
2334 * i->volume = s->reference_volume * i->reference_ratio
2336 * This is identical to propagate_reference_volume() */
2337 new_volume = s->reference_volume;
2338 pa_cvolume_remap(&new_volume, &s->channel_map, &i->channel_map);
2339 pa_sw_cvolume_multiply(&new_volume, &new_volume, &i->reference_ratio);
2340 pa_sink_input_set_volume_direct(i, &new_volume);
2342 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER)
2343 && PA_SINK_IS_LINKED(i->origin_sink->state))
2344 propagate_real_volume(i->origin_sink, old_real_volume);
2348 /* Something got changed in the hardware. It probably makes sense
2349 * to save changed hw settings given that hw volume changes not
2350 * triggered by PA are almost certainly done by the user. */
2351 if (!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2352 s->save_volume = true;
2355 /* Called from io thread */
2356 void pa_sink_update_volume_and_mute(pa_sink *s) {
2358 pa_sink_assert_io_context(s);
2360 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE, NULL, 0, NULL, NULL);
2363 /* Called from main thread */
2364 const pa_cvolume *pa_sink_get_volume(pa_sink *s, bool force_refresh) {
2365 pa_sink_assert_ref(s);
2366 pa_assert_ctl_context();
2367 pa_assert(PA_SINK_IS_LINKED(s->state));
2369 if (s->refresh_volume || force_refresh) {
2370 struct pa_cvolume old_real_volume;
2372 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2374 old_real_volume = s->real_volume;
2376 if (!(s->flags & PA_SINK_DEFERRED_VOLUME) && s->get_volume)
2379 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
2381 update_real_volume(s, &s->real_volume, &s->channel_map);
2382 propagate_real_volume(s, &old_real_volume);
2385 return &s->reference_volume;
2388 /* Called from main thread. In volume sharing cases, only the root sink may
2390 void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_real_volume) {
2391 pa_cvolume old_real_volume;
2393 pa_sink_assert_ref(s);
2394 pa_assert_ctl_context();
2395 pa_assert(PA_SINK_IS_LINKED(s->state));
2396 pa_assert(!(s->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER));
2398 /* The sink implementor may call this if the volume changed to make sure everyone is notified */
2400 old_real_volume = s->real_volume;
2401 update_real_volume(s, new_real_volume, &s->channel_map);
2402 propagate_real_volume(s, &old_real_volume);
2405 /* Called from main thread */
2406 void pa_sink_set_mute(pa_sink *s, bool mute, bool save) {
2409 pa_sink_assert_ref(s);
2410 pa_assert_ctl_context();
2412 old_muted = s->muted;
2414 if (mute == old_muted) {
2415 s->save_muted |= save;
2420 s->save_muted = save;
2422 if (!(s->flags & PA_SINK_DEFERRED_VOLUME) && s->set_mute) {
2423 s->set_mute_in_progress = true;
2425 s->set_mute_in_progress = false;
2428 if (!PA_SINK_IS_LINKED(s->state))
2431 pa_log_debug("The mute of sink %s changed from %s to %s.", s->name, pa_yes_no(old_muted), pa_yes_no(mute));
2432 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MUTE, NULL, 0, NULL) == 0);
2433 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2434 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_MUTE_CHANGED], s);
2437 /* Called from main thread */
2438 bool pa_sink_get_mute(pa_sink *s, bool force_refresh) {
2440 pa_sink_assert_ref(s);
2441 pa_assert_ctl_context();
2442 pa_assert(PA_SINK_IS_LINKED(s->state));
2444 if ((s->refresh_muted || force_refresh) && s->get_mute) {
2447 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
2448 if (pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MUTE, &mute, 0, NULL) >= 0)
2449 pa_sink_mute_changed(s, mute);
2451 if (s->get_mute(s, &mute) >= 0)
2452 pa_sink_mute_changed(s, mute);
2459 /* Called from main thread */
2460 void pa_sink_mute_changed(pa_sink *s, bool new_muted) {
2461 pa_sink_assert_ref(s);
2462 pa_assert_ctl_context();
2463 pa_assert(PA_SINK_IS_LINKED(s->state));
2465 if (s->set_mute_in_progress)
2468 /* pa_sink_set_mute() does this same check, so this may appear redundant,
2469 * but we must have this here also, because the save parameter of
2470 * pa_sink_set_mute() would otherwise have unintended side effects (saving
2471 * the mute state when it shouldn't be saved). */
2472 if (new_muted == s->muted)
2475 pa_sink_set_mute(s, new_muted, true);
2478 /* Called from main thread */
2479 bool pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p) {
2480 pa_sink_assert_ref(s);
2481 pa_assert_ctl_context();
2484 pa_proplist_update(s->proplist, mode, p);
2486 if (PA_SINK_IS_LINKED(s->state)) {
2487 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
2488 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2494 /* Called from main thread */
2495 /* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
2496 void pa_sink_set_description(pa_sink *s, const char *description) {
2498 pa_sink_assert_ref(s);
2499 pa_assert_ctl_context();
2501 if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
2504 old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
2506 if (old && description && pa_streq(old, description))
2510 pa_proplist_sets(s->proplist, PA_PROP_DEVICE_DESCRIPTION, description);
2512 pa_proplist_unset(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
2514 if (s->monitor_source) {
2517 n = pa_sprintf_malloc("Monitor Source of %s", description ? description : s->name);
2518 pa_source_set_description(s->monitor_source, n);
2522 if (PA_SINK_IS_LINKED(s->state)) {
2523 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
2524 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], s);
2528 /* Called from main thread */
2529 unsigned pa_sink_linked_by(pa_sink *s) {
2532 pa_sink_assert_ref(s);
2533 pa_assert_ctl_context();
2534 pa_assert(PA_SINK_IS_LINKED(s->state));
2536 ret = pa_idxset_size(s->inputs);
2538 /* We add in the number of streams connected to us here. Please
2539 * note the asymmetry to pa_sink_used_by()! */
2541 if (s->monitor_source)
2542 ret += pa_source_linked_by(s->monitor_source);
2547 /* Called from main thread */
2548 unsigned pa_sink_used_by(pa_sink *s) {
2551 pa_sink_assert_ref(s);
2552 pa_assert_ctl_context();
2553 pa_assert(PA_SINK_IS_LINKED(s->state));
2555 ret = pa_idxset_size(s->inputs);
2556 pa_assert(ret >= s->n_corked);
2558 /* Streams connected to our monitor source do not matter for
2559 * pa_sink_used_by()!.*/
2561 return ret - s->n_corked;
2564 /* Called from main thread */
2565 unsigned pa_sink_check_suspend(pa_sink *s, pa_sink_input *ignore_input, pa_source_output *ignore_output) {
2570 pa_sink_assert_ref(s);
2571 pa_assert_ctl_context();
2573 if (!PA_SINK_IS_LINKED(s->state))
2578 PA_IDXSET_FOREACH(i, s->inputs, idx) {
2579 pa_sink_input_state_t st;
2581 if (i == ignore_input)
2584 st = pa_sink_input_get_state(i);
2586 /* We do not assert here. It is perfectly valid for a sink input to
2587 * be in the INIT state (i.e. created, marked done but not yet put)
2588 * and we should not care if it's unlinked as it won't contribute
2589 * towards our busy status.
2591 if (!PA_SINK_INPUT_IS_LINKED(st))
2594 if (st == PA_SINK_INPUT_CORKED)
2597 if (i->flags & PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND)
2603 if (s->monitor_source)
2604 ret += pa_source_check_suspend(s->monitor_source, ignore_output);
2609 const char *pa_sink_state_to_string(pa_sink_state_t state) {
2611 case PA_SINK_INIT: return "INIT";
2612 case PA_SINK_IDLE: return "IDLE";
2613 case PA_SINK_RUNNING: return "RUNNING";
2614 case PA_SINK_SUSPENDED: return "SUSPENDED";
2615 case PA_SINK_UNLINKED: return "UNLINKED";
2616 case PA_SINK_INVALID_STATE: return "INVALID_STATE";
2619 pa_assert_not_reached();
2622 /* Called from the IO thread */
2623 static void sync_input_volumes_within_thread(pa_sink *s) {
2627 pa_sink_assert_ref(s);
2628 pa_sink_assert_io_context(s);
2630 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
2631 if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
2634 i->thread_info.soft_volume = i->soft_volume;
2635 pa_sink_input_request_rewind(i, 0, true, false, false);
2639 /* Called from the IO thread. Only called for the root sink in volume sharing
2640 * cases, except for internal recursive calls. */
2641 static void set_shared_volume_within_thread(pa_sink *s) {
2642 pa_sink_input *i = NULL;
2645 pa_sink_assert_ref(s);
2647 PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME_SYNCED, NULL, 0, NULL);
2649 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
2650 if (i->origin_sink && (i->origin_sink->flags & PA_SINK_SHARE_VOLUME_WITH_MASTER))
2651 set_shared_volume_within_thread(i->origin_sink);
2655 /* Called from IO thread, except when it is not */
2656 int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offset, pa_memchunk *chunk) {
2657 pa_sink *s = PA_SINK(o);
2658 pa_sink_assert_ref(s);
2660 switch ((pa_sink_message_t) code) {
2662 case PA_SINK_MESSAGE_ADD_INPUT: {
2663 pa_sink_input *i = PA_SINK_INPUT(userdata);
2665 /* If you change anything here, make sure to change the
2666 * sink input handling a few lines down at
2667 * PA_SINK_MESSAGE_FINISH_MOVE, too. */
2669 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
2671 /* Since the caller sleeps in pa_sink_input_put(), we can
2672 * safely access data outside of thread_info even though
2675 if ((i->thread_info.sync_prev = i->sync_prev)) {
2676 pa_assert(i->sink == i->thread_info.sync_prev->sink);
2677 pa_assert(i->sync_prev->sync_next == i);
2678 i->thread_info.sync_prev->thread_info.sync_next = i;
2681 if ((i->thread_info.sync_next = i->sync_next)) {
2682 pa_assert(i->sink == i->thread_info.sync_next->sink);
2683 pa_assert(i->sync_next->sync_prev == i);
2684 i->thread_info.sync_next->thread_info.sync_prev = i;
2687 pa_sink_input_attach(i);
2689 pa_sink_input_set_state_within_thread(i, i->state);
2691 /* The requested latency of the sink input needs to be fixed up and
2692 * then configured on the sink. If this causes the sink latency to
2693 * go down, the sink implementor is responsible for doing a rewind
2694 * in the update_requested_latency() callback to ensure that the
2695 * sink buffer doesn't contain more data than what the new latency
2698 * XXX: Does it really make sense to push this responsibility to
2699 * the sink implementors? Wouldn't it be better to do it once in
2700 * the core than many times in the modules? */
2702 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
2703 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
2705 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
2706 pa_sink_input_update_max_request(i, s->thread_info.max_request);
2708 /* We don't rewind here automatically. This is left to the
2709 * sink input implementor because some sink inputs need a
2710 * slow start, i.e. need some time to buffer client
2711 * samples before beginning streaming.
2713 * XXX: Does it really make sense to push this functionality to
2714 * the sink implementors? Wouldn't it be better to do it once in
2715 * the core than many times in the modules? */
2717 /* In flat volume mode we need to update the volume as
2719 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2722 case PA_SINK_MESSAGE_REMOVE_INPUT: {
2723 pa_sink_input *i = PA_SINK_INPUT(userdata);
2725 /* If you change anything here, make sure to change the
2726 * sink input handling a few lines down at
2727 * PA_SINK_MESSAGE_START_MOVE, too. */
2729 pa_sink_input_detach(i);
2731 pa_sink_input_set_state_within_thread(i, i->state);
2733 /* Since the caller sleeps in pa_sink_input_unlink(),
2734 * we can safely access data outside of thread_info even
2735 * though it is mutable */
2737 pa_assert(!i->sync_prev);
2738 pa_assert(!i->sync_next);
2740 if (i->thread_info.sync_prev) {
2741 i->thread_info.sync_prev->thread_info.sync_next = i->thread_info.sync_prev->sync_next;
2742 i->thread_info.sync_prev = NULL;
2745 if (i->thread_info.sync_next) {
2746 i->thread_info.sync_next->thread_info.sync_prev = i->thread_info.sync_next->sync_prev;
2747 i->thread_info.sync_next = NULL;
2750 pa_hashmap_remove_and_free(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
2751 pa_sink_invalidate_requested_latency(s, true);
2752 pa_sink_request_rewind(s, (size_t) -1);
2754 /* In flat volume mode we need to update the volume as
2756 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2759 case PA_SINK_MESSAGE_START_MOVE: {
2760 pa_sink_input *i = PA_SINK_INPUT(userdata);
2762 /* We don't support moving synchronized streams. */
2763 pa_assert(!i->sync_prev);
2764 pa_assert(!i->sync_next);
2765 pa_assert(!i->thread_info.sync_next);
2766 pa_assert(!i->thread_info.sync_prev);
2768 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
2770 size_t sink_nbytes, total_nbytes;
2772 /* The old sink probably has some audio from this
2773 * stream in its buffer. We want to "take it back" as
2774 * much as possible and play it to the new sink. We
2775 * don't know at this point how much the old sink can
2776 * rewind. We have to pick something, and that
2777 * something is the full latency of the old sink here.
2778 * So we rewind the stream buffer by the sink latency
2779 * amount, which may be more than what we should
2780 * rewind. This can result in a chunk of audio being
2781 * played both to the old sink and the new sink.
2783 * FIXME: Fix this code so that we don't have to make
2784 * guesses about how much the sink will actually be
2785 * able to rewind. If someone comes up with a solution
2786 * for this, something to note is that the part of the
2787 * latency that the old sink couldn't rewind should
2788 * ideally be compensated after the stream has moved
2789 * to the new sink by adding silence. The new sink
2790 * most likely can't start playing the moved stream
2791 * immediately, and that gap should be removed from
2792 * the "compensation silence" (at least at the time of
2793 * writing this, the move finish code will actually
2794 * already take care of dropping the new sink's
2795 * unrewindable latency, so taking into account the
2796 * unrewindable latency of the old sink is the only
2799 * The render_memblockq contents are discarded,
2800 * because when the sink changes, the format of the
2801 * audio stored in the render_memblockq may change
2802 * too, making the stored audio invalid. FIXME:
2803 * However, the read and write indices are moved back
2804 * the same amount, so if they are not the same now,
2805 * they won't be the same after the rewind either. If
2806 * the write index of the render_memblockq is ahead of
2807 * the read index, then the render_memblockq will feed
2808 * the new sink some silence first, which it shouldn't
2809 * do. The write index should be flushed to be the
2810 * same as the read index. */
2812 /* Get the latency of the sink */
2813 usec = pa_sink_get_latency_within_thread(s, false);
2814 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
2815 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
2817 if (total_nbytes > 0) {
2818 i->thread_info.rewrite_nbytes = i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, total_nbytes) : total_nbytes;
2819 i->thread_info.rewrite_flush = true;
2820 pa_sink_input_process_rewind(i, sink_nbytes);
2824 pa_sink_input_detach(i);
2826 /* Let's remove the sink input ...*/
2827 pa_hashmap_remove_and_free(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index));
2829 pa_sink_invalidate_requested_latency(s, true);
2831 pa_log_debug("Requesting rewind due to started move");
2832 pa_sink_request_rewind(s, (size_t) -1);
2834 /* In flat volume mode we need to update the volume as
2836 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2839 case PA_SINK_MESSAGE_FINISH_MOVE: {
2840 pa_sink_input *i = PA_SINK_INPUT(userdata);
2842 /* We don't support moving synchronized streams. */
2843 pa_assert(!i->sync_prev);
2844 pa_assert(!i->sync_next);
2845 pa_assert(!i->thread_info.sync_next);
2846 pa_assert(!i->thread_info.sync_prev);
2848 pa_hashmap_put(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index), pa_sink_input_ref(i));
2850 pa_sink_input_attach(i);
2852 if (i->thread_info.state != PA_SINK_INPUT_CORKED) {
2856 /* In the ideal case the new sink would start playing
2857 * the stream immediately. That requires the sink to
2858 * be able to rewind all of its latency, which usually
2859 * isn't possible, so there will probably be some gap
2860 * before the moved stream becomes audible. We then
2861 * have two possibilities: 1) start playing the stream
2862 * from where it is now, or 2) drop the unrewindable
2863 * latency of the sink from the stream. With option 1
2864 * we won't lose any audio but the stream will have a
2865 * pause. With option 2 we may lose some audio but the
2866 * stream time will be somewhat in sync with the wall
2867 * clock. Lennart seems to have chosen option 2 (one
2868 * of the reasons might have been that option 1 is
2869 * actually much harder to implement), so we drop the
2870 * latency of the new sink from the moved stream and
2871 * hope that the sink will undo most of that in the
2874 /* Get the latency of the sink */
2875 usec = pa_sink_get_latency_within_thread(s, false);
2876 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
2879 pa_sink_input_drop(i, nbytes);
2881 pa_log_debug("Requesting rewind due to finished move");
2882 pa_sink_request_rewind(s, nbytes);
2885 /* Updating the requested sink latency has to be done
2886 * after the sink rewind request, not before, because
2887 * otherwise the sink may limit the rewind amount
2890 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1)
2891 pa_sink_input_set_requested_latency_within_thread(i, i->thread_info.requested_sink_latency);
2893 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
2894 pa_sink_input_update_max_request(i, s->thread_info.max_request);
2896 return o->process_msg(o, PA_SINK_MESSAGE_SET_SHARED_VOLUME, NULL, 0, NULL);
2899 case PA_SINK_MESSAGE_SET_SHARED_VOLUME: {
2900 pa_sink *root_sink = pa_sink_get_master(s);
2902 if (PA_LIKELY(root_sink))
2903 set_shared_volume_within_thread(root_sink);
2908 case PA_SINK_MESSAGE_SET_VOLUME_SYNCED:
2910 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
2912 pa_sink_volume_change_push(s);
2914 /* Fall through ... */
2916 case PA_SINK_MESSAGE_SET_VOLUME:
2918 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
2919 s->thread_info.soft_volume = s->soft_volume;
2920 pa_sink_request_rewind(s, (size_t) -1);
2923 /* Fall through ... */
2925 case PA_SINK_MESSAGE_SYNC_VOLUMES:
2926 sync_input_volumes_within_thread(s);
2929 case PA_SINK_MESSAGE_GET_VOLUME:
2931 if ((s->flags & PA_SINK_DEFERRED_VOLUME) && s->get_volume) {
2933 pa_sink_volume_change_flush(s);
2934 pa_sw_cvolume_divide(&s->thread_info.current_hw_volume, &s->real_volume, &s->soft_volume);
2937 /* In case sink implementor reset SW volume. */
2938 if (!pa_cvolume_equal(&s->thread_info.soft_volume, &s->soft_volume)) {
2939 s->thread_info.soft_volume = s->soft_volume;
2940 pa_sink_request_rewind(s, (size_t) -1);
2945 case PA_SINK_MESSAGE_SET_MUTE:
2947 if (s->thread_info.soft_muted != s->muted) {
2948 s->thread_info.soft_muted = s->muted;
2949 pa_sink_request_rewind(s, (size_t) -1);
2952 if (s->flags & PA_SINK_DEFERRED_VOLUME && s->set_mute)
2957 case PA_SINK_MESSAGE_GET_MUTE:
2959 if (s->flags & PA_SINK_DEFERRED_VOLUME && s->get_mute)
2960 return s->get_mute(s, userdata);
2964 case PA_SINK_MESSAGE_SET_STATE: {
2966 bool suspend_change =
2967 (s->thread_info.state == PA_SINK_SUSPENDED && PA_SINK_IS_OPENED(PA_PTR_TO_UINT(userdata))) ||
2968 (PA_SINK_IS_OPENED(s->thread_info.state) && PA_PTR_TO_UINT(userdata) == PA_SINK_SUSPENDED);
2970 s->thread_info.state = PA_PTR_TO_UINT(userdata);
2972 if (s->thread_info.state == PA_SINK_SUSPENDED) {
2973 s->thread_info.rewind_nbytes = 0;
2974 s->thread_info.rewind_requested = false;
2977 if (suspend_change) {
2981 while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
2982 if (i->suspend_within_thread)
2983 i->suspend_within_thread(i, s->thread_info.state == PA_SINK_SUSPENDED);
2989 case PA_SINK_MESSAGE_GET_REQUESTED_LATENCY: {
2991 pa_usec_t *usec = userdata;
2992 *usec = pa_sink_get_requested_latency_within_thread(s);
2994 /* Yes, that's right, the IO thread will see -1 when no
2995 * explicit requested latency is configured, the main
2996 * thread will see max_latency */
2997 if (*usec == (pa_usec_t) -1)
2998 *usec = s->thread_info.max_latency;
3003 case PA_SINK_MESSAGE_SET_LATENCY_RANGE: {
3004 pa_usec_t *r = userdata;
3006 pa_sink_set_latency_range_within_thread(s, r[0], r[1]);
3011 case PA_SINK_MESSAGE_GET_LATENCY_RANGE: {
3012 pa_usec_t *r = userdata;
3014 r[0] = s->thread_info.min_latency;
3015 r[1] = s->thread_info.max_latency;
3020 case PA_SINK_MESSAGE_GET_FIXED_LATENCY:
3022 *((pa_usec_t*) userdata) = s->thread_info.fixed_latency;
3025 case PA_SINK_MESSAGE_SET_FIXED_LATENCY:
3027 pa_sink_set_fixed_latency_within_thread(s, (pa_usec_t) offset);
3030 case PA_SINK_MESSAGE_GET_MAX_REWIND:
3032 *((size_t*) userdata) = s->thread_info.max_rewind;
3035 case PA_SINK_MESSAGE_GET_MAX_REQUEST:
3037 *((size_t*) userdata) = s->thread_info.max_request;
3040 case PA_SINK_MESSAGE_SET_MAX_REWIND:
3042 pa_sink_set_max_rewind_within_thread(s, (size_t) offset);
3045 case PA_SINK_MESSAGE_SET_MAX_REQUEST:
3047 pa_sink_set_max_request_within_thread(s, (size_t) offset);
3050 case PA_SINK_MESSAGE_SET_PORT:
3052 pa_assert(userdata);
3054 struct sink_message_set_port *msg_data = userdata;
3055 msg_data->ret = s->set_port(s, msg_data->port);
3059 case PA_SINK_MESSAGE_UPDATE_VOLUME_AND_MUTE:
3060 /* This message is sent from IO-thread and handled in main thread. */
3061 pa_assert_ctl_context();
3063 /* Make sure we're not messing with main thread when no longer linked */
3064 if (!PA_SINK_IS_LINKED(s->state))
3067 pa_sink_get_volume(s, true);
3068 pa_sink_get_mute(s, true);
3071 case PA_SINK_MESSAGE_SET_PORT_LATENCY_OFFSET:
3072 s->thread_info.port_latency_offset = offset;
3075 case PA_SINK_MESSAGE_GET_LATENCY:
3076 case PA_SINK_MESSAGE_MAX:
3083 /* Called from main thread */
3084 int pa_sink_suspend_all(pa_core *c, bool suspend, pa_suspend_cause_t cause) {
3089 pa_core_assert_ref(c);
3090 pa_assert_ctl_context();
3091 pa_assert(cause != 0);
3093 PA_IDXSET_FOREACH(sink, c->sinks, idx) {
3096 if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
3103 /* Called from IO thread */
3104 void pa_sink_detach_within_thread(pa_sink *s) {
3108 pa_sink_assert_ref(s);
3109 pa_sink_assert_io_context(s);
3110 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
3112 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3113 pa_sink_input_detach(i);
3115 if (s->monitor_source)
3116 pa_source_detach_within_thread(s->monitor_source);
3119 /* Called from IO thread */
3120 void pa_sink_attach_within_thread(pa_sink *s) {
3124 pa_sink_assert_ref(s);
3125 pa_sink_assert_io_context(s);
3126 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
3128 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3129 pa_sink_input_attach(i);
3131 if (s->monitor_source)
3132 pa_source_attach_within_thread(s->monitor_source);
3135 /* Called from IO thread */
3136 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
3137 pa_sink_assert_ref(s);
3138 pa_sink_assert_io_context(s);
3139 pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
3141 if (nbytes == (size_t) -1)
3142 nbytes = s->thread_info.max_rewind;
3144 nbytes = PA_MIN(nbytes, s->thread_info.max_rewind);
3146 if (s->thread_info.rewind_requested &&
3147 nbytes <= s->thread_info.rewind_nbytes)
3150 s->thread_info.rewind_nbytes = nbytes;
3151 s->thread_info.rewind_requested = true;
3153 if (s->request_rewind)
3154 s->request_rewind(s);
3157 /* Called from IO thread */
3158 pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
3159 pa_usec_t result = (pa_usec_t) -1;
3162 pa_usec_t monitor_latency;
3164 pa_sink_assert_ref(s);
3165 pa_sink_assert_io_context(s);
3167 if (!(s->flags & PA_SINK_DYNAMIC_LATENCY))
3168 return PA_CLAMP(s->thread_info.fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
3170 if (s->thread_info.requested_latency_valid)
3171 return s->thread_info.requested_latency;
3173 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3174 if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
3175 (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
3176 result = i->thread_info.requested_sink_latency;
3178 monitor_latency = pa_source_get_requested_latency_within_thread(s->monitor_source);
3180 if (monitor_latency != (pa_usec_t) -1 &&
3181 (result == (pa_usec_t) -1 || result > monitor_latency))
3182 result = monitor_latency;
3184 if (result != (pa_usec_t) -1)
3185 result = PA_CLAMP(result, s->thread_info.min_latency, s->thread_info.max_latency);
3187 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3188 /* Only cache if properly initialized */
3189 s->thread_info.requested_latency = result;
3190 s->thread_info.requested_latency_valid = true;
3196 /* Called from main thread */
3197 pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
3200 pa_sink_assert_ref(s);
3201 pa_assert_ctl_context();
3202 pa_assert(PA_SINK_IS_LINKED(s->state));
3204 if (s->state == PA_SINK_SUSPENDED)
3207 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
3212 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
3213 void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
3217 pa_sink_assert_ref(s);
3218 pa_sink_assert_io_context(s);
3220 if (max_rewind == s->thread_info.max_rewind)
3223 s->thread_info.max_rewind = max_rewind;
3225 if (PA_SINK_IS_LINKED(s->thread_info.state))
3226 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3227 pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
3229 if (s->monitor_source)
3230 pa_source_set_max_rewind_within_thread(s->monitor_source, s->thread_info.max_rewind);
3233 /* Called from main thread */
3234 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
3235 pa_sink_assert_ref(s);
3236 pa_assert_ctl_context();
3238 if (PA_SINK_IS_LINKED(s->state))
3239 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
3241 pa_sink_set_max_rewind_within_thread(s, max_rewind);
3244 /* Called from IO as well as the main thread -- the latter only before the IO thread started up */
3245 void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
3248 pa_sink_assert_ref(s);
3249 pa_sink_assert_io_context(s);
3251 if (max_request == s->thread_info.max_request)
3254 s->thread_info.max_request = max_request;
3256 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3259 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3260 pa_sink_input_update_max_request(i, s->thread_info.max_request);
3264 /* Called from main thread */
3265 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
3266 pa_sink_assert_ref(s);
3267 pa_assert_ctl_context();
3269 if (PA_SINK_IS_LINKED(s->state))
3270 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REQUEST, NULL, max_request, NULL) == 0);
3272 pa_sink_set_max_request_within_thread(s, max_request);
3275 /* Called from IO thread */
3276 void pa_sink_invalidate_requested_latency(pa_sink *s, bool dynamic) {
3280 pa_sink_assert_ref(s);
3281 pa_sink_assert_io_context(s);
3283 if ((s->flags & PA_SINK_DYNAMIC_LATENCY))
3284 s->thread_info.requested_latency_valid = false;
3288 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3290 if (s->update_requested_latency)
3291 s->update_requested_latency(s);
3293 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3294 if (i->update_sink_requested_latency)
3295 i->update_sink_requested_latency(i);
3299 /* Called from main thread */
3300 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
3301 pa_sink_assert_ref(s);
3302 pa_assert_ctl_context();
3304 /* min_latency == 0: no limit
3305 * min_latency anything else: specified limit
3307 * Similar for max_latency */
3309 if (min_latency < ABSOLUTE_MIN_LATENCY)
3310 min_latency = ABSOLUTE_MIN_LATENCY;
3312 if (max_latency <= 0 ||
3313 max_latency > ABSOLUTE_MAX_LATENCY)
3314 max_latency = ABSOLUTE_MAX_LATENCY;
3316 pa_assert(min_latency <= max_latency);
3318 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3319 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
3320 max_latency == ABSOLUTE_MAX_LATENCY) ||
3321 (s->flags & PA_SINK_DYNAMIC_LATENCY));
3323 if (PA_SINK_IS_LINKED(s->state)) {
3329 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_LATENCY_RANGE, r, 0, NULL) == 0);
3331 pa_sink_set_latency_range_within_thread(s, min_latency, max_latency);
3334 /* Called from main thread */
3335 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
3336 pa_sink_assert_ref(s);
3337 pa_assert_ctl_context();
3338 pa_assert(min_latency);
3339 pa_assert(max_latency);
3341 if (PA_SINK_IS_LINKED(s->state)) {
3342 pa_usec_t r[2] = { 0, 0 };
3344 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY_RANGE, r, 0, NULL) == 0);
3346 *min_latency = r[0];
3347 *max_latency = r[1];
3349 *min_latency = s->thread_info.min_latency;
3350 *max_latency = s->thread_info.max_latency;
3354 /* Called from IO thread */
3355 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
3356 pa_sink_assert_ref(s);
3357 pa_sink_assert_io_context(s);
3359 pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
3360 pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
3361 pa_assert(min_latency <= max_latency);
3363 /* Hmm, let's see if someone forgot to set PA_SINK_DYNAMIC_LATENCY here... */
3364 pa_assert((min_latency == ABSOLUTE_MIN_LATENCY &&
3365 max_latency == ABSOLUTE_MAX_LATENCY) ||
3366 (s->flags & PA_SINK_DYNAMIC_LATENCY));
3368 if (s->thread_info.min_latency == min_latency &&
3369 s->thread_info.max_latency == max_latency)
3372 s->thread_info.min_latency = min_latency;
3373 s->thread_info.max_latency = max_latency;
3375 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3379 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3380 if (i->update_sink_latency_range)
3381 i->update_sink_latency_range(i);
3384 pa_sink_invalidate_requested_latency(s, false);
3386 pa_source_set_latency_range_within_thread(s->monitor_source, min_latency, max_latency);
3389 /* Called from main thread */
3390 void pa_sink_set_fixed_latency(pa_sink *s, pa_usec_t latency) {
3391 pa_sink_assert_ref(s);
3392 pa_assert_ctl_context();
3394 if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
3395 pa_assert(latency == 0);
3399 if (latency < ABSOLUTE_MIN_LATENCY)
3400 latency = ABSOLUTE_MIN_LATENCY;
3402 if (latency > ABSOLUTE_MAX_LATENCY)
3403 latency = ABSOLUTE_MAX_LATENCY;
3405 if (PA_SINK_IS_LINKED(s->state))
3406 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_FIXED_LATENCY, NULL, (int64_t) latency, NULL) == 0);
3408 s->thread_info.fixed_latency = latency;
3410 pa_source_set_fixed_latency(s->monitor_source, latency);
3413 /* Called from main thread */
3414 pa_usec_t pa_sink_get_fixed_latency(pa_sink *s) {
3417 pa_sink_assert_ref(s);
3418 pa_assert_ctl_context();
3420 if (s->flags & PA_SINK_DYNAMIC_LATENCY)
3423 if (PA_SINK_IS_LINKED(s->state))
3424 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_FIXED_LATENCY, &latency, 0, NULL) == 0);
3426 latency = s->thread_info.fixed_latency;
3431 /* Called from IO thread */
3432 void pa_sink_set_fixed_latency_within_thread(pa_sink *s, pa_usec_t latency) {
3433 pa_sink_assert_ref(s);
3434 pa_sink_assert_io_context(s);
3436 if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
3437 pa_assert(latency == 0);
3438 s->thread_info.fixed_latency = 0;
3440 if (s->monitor_source)
3441 pa_source_set_fixed_latency_within_thread(s->monitor_source, 0);
3446 pa_assert(latency >= ABSOLUTE_MIN_LATENCY);
3447 pa_assert(latency <= ABSOLUTE_MAX_LATENCY);
3449 if (s->thread_info.fixed_latency == latency)
3452 s->thread_info.fixed_latency = latency;
3454 if (PA_SINK_IS_LINKED(s->thread_info.state)) {
3458 PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
3459 if (i->update_sink_fixed_latency)
3460 i->update_sink_fixed_latency(i);
3463 pa_sink_invalidate_requested_latency(s, false);
3465 pa_source_set_fixed_latency_within_thread(s->monitor_source, latency);
3468 /* Called from main context */
3469 void pa_sink_set_port_latency_offset(pa_sink *s, int64_t offset) {
3470 pa_sink_assert_ref(s);
3472 s->port_latency_offset = offset;
3474 if (PA_SINK_IS_LINKED(s->state))
3475 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_PORT_LATENCY_OFFSET, NULL, offset, NULL) == 0);
3477 s->thread_info.port_latency_offset = offset;
3479 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PORT_LATENCY_OFFSET_CHANGED], s);
3482 /* Called from main context */
3483 size_t pa_sink_get_max_rewind(pa_sink *s) {
3485 pa_assert_ctl_context();
3486 pa_sink_assert_ref(s);
3488 if (!PA_SINK_IS_LINKED(s->state))
3489 return s->thread_info.max_rewind;
3491 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REWIND, &r, 0, NULL) == 0);
3496 /* Called from main context */
3497 size_t pa_sink_get_max_request(pa_sink *s) {
3499 pa_sink_assert_ref(s);
3500 pa_assert_ctl_context();
3502 if (!PA_SINK_IS_LINKED(s->state))
3503 return s->thread_info.max_request;
3505 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_MAX_REQUEST, &r, 0, NULL) == 0);
3510 /* Called from main context */
3511 int pa_sink_set_port(pa_sink *s, const char *name, bool save) {
3512 pa_device_port *port;
3515 pa_sink_assert_ref(s);
3516 pa_assert_ctl_context();
3519 pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s->index, s->name);
3520 return -PA_ERR_NOTIMPLEMENTED;
3524 return -PA_ERR_NOENTITY;
3526 if (!(port = pa_hashmap_get(s->ports, name)))
3527 return -PA_ERR_NOENTITY;
3529 if (s->active_port == port) {
3530 s->save_port = s->save_port || save;
3534 if (s->flags & PA_SINK_DEFERRED_VOLUME) {
3535 struct sink_message_set_port msg = { .port = port, .ret = 0 };
3536 pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_PORT, &msg, 0, NULL) == 0);
3540 ret = s->set_port(s, port);
3543 return -PA_ERR_NOENTITY;
3545 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
3547 pa_log_info("Changed port of sink %u \"%s\" to %s", s->index, s->name, port->name);
3549 s->active_port = port;
3550 s->save_port = save;
3552 pa_sink_set_port_latency_offset(s, s->active_port->latency_offset);
3554 /* The active port affects the default sink selection. */
3555 pa_core_update_default_sink(s->core);
3557 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PORT_CHANGED], s);
3562 bool pa_device_init_icon(pa_proplist *p, bool is_sink) {
3563 const char *ff, *c, *t = NULL, *s = "", *profile, *bus;
3567 if (pa_proplist_contains(p, PA_PROP_DEVICE_ICON_NAME))
3570 if ((ff = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
3572 if (pa_streq(ff, "microphone"))
3573 t = "audio-input-microphone";
3574 else if (pa_streq(ff, "webcam"))
3576 else if (pa_streq(ff, "computer"))
3578 else if (pa_streq(ff, "handset"))
3580 else if (pa_streq(ff, "portable"))
3581 t = "multimedia-player";
3582 else if (pa_streq(ff, "tv"))
3583 t = "video-display";
3586 * The following icons are not part of the icon naming spec,
3587 * because Rodney Dawes sucks as the maintainer of that spec.
3589 * http://lists.freedesktop.org/archives/xdg/2009-May/010397.html
3591 else if (pa_streq(ff, "headset"))
3592 t = "audio-headset";
3593 else if (pa_streq(ff, "headphone"))
3594 t = "audio-headphones";
3595 else if (pa_streq(ff, "speaker"))
3596 t = "audio-speakers";
3597 else if (pa_streq(ff, "hands-free"))
3598 t = "audio-handsfree";
3602 if ((c = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
3603 if (pa_streq(c, "modem"))
3610 t = "audio-input-microphone";
3613 if ((profile = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
3614 if (strstr(profile, "analog"))
3616 else if (strstr(profile, "iec958"))
3618 else if (strstr(profile, "hdmi"))
3622 bus = pa_proplist_gets(p, PA_PROP_DEVICE_BUS);
3624 pa_proplist_setf(p, PA_PROP_DEVICE_ICON_NAME, "%s%s%s%s", t, pa_strempty(s), bus ? "-" : "", pa_strempty(bus));
3629 bool pa_device_init_description(pa_proplist *p, pa_card *card) {
3630 const char *s, *d = NULL, *k;
3633 if (pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
3637 if ((s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_DESCRIPTION)))
3641 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
3642 if (pa_streq(s, "internal"))
3643 d = _("Built-in Audio");
3646 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS)))
3647 if (pa_streq(s, "modem"))
3651 d = pa_proplist_gets(p, PA_PROP_DEVICE_PRODUCT_NAME);
3656 k = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_DESCRIPTION);
3659 pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s %s", d, k);
3661 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, d);
3666 bool pa_device_init_intended_roles(pa_proplist *p) {
3670 if (pa_proplist_contains(p, PA_PROP_DEVICE_INTENDED_ROLES))
3673 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR)))
3674 if (pa_streq(s, "handset") || pa_streq(s, "hands-free")
3675 || pa_streq(s, "headset")) {
3676 pa_proplist_sets(p, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
3683 unsigned pa_device_init_priority(pa_proplist *p) {
3685 unsigned priority = 0;
3689 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS))) {
3691 if (pa_streq(s, "sound"))
3693 else if (!pa_streq(s, "modem"))
3697 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) {
3699 if (pa_streq(s, "headphone"))
3701 else if (pa_streq(s, "hifi"))
3703 else if (pa_streq(s, "speaker"))
3705 else if (pa_streq(s, "portable"))
3709 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_BUS))) {
3711 if (pa_streq(s, "bluetooth"))
3713 else if (pa_streq(s, "usb"))
3715 else if (pa_streq(s, "pci"))
3719 if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) {
3721 if (pa_startswith(s, "analog-"))
3723 else if (pa_startswith(s, "iec958-"))
3730 PA_STATIC_FLIST_DECLARE(pa_sink_volume_change, 0, pa_xfree);
3732 /* Called from the IO thread. */
3733 static pa_sink_volume_change *pa_sink_volume_change_new(pa_sink *s) {
3734 pa_sink_volume_change *c;
3735 if (!(c = pa_flist_pop(PA_STATIC_FLIST_GET(pa_sink_volume_change))))
3736 c = pa_xnew(pa_sink_volume_change, 1);
3738 PA_LLIST_INIT(pa_sink_volume_change, c);
3740 pa_cvolume_reset(&c->hw_volume, s->sample_spec.channels);
3744 /* Called from the IO thread. */
3745 static void pa_sink_volume_change_free(pa_sink_volume_change *c) {
3747 if (pa_flist_push(PA_STATIC_FLIST_GET(pa_sink_volume_change), c) < 0)
3751 /* Called from the IO thread. */
3752 void pa_sink_volume_change_push(pa_sink *s) {
3753 pa_sink_volume_change *c = NULL;
3754 pa_sink_volume_change *nc = NULL;
3755 pa_sink_volume_change *pc = NULL;
3756 uint32_t safety_margin = s->thread_info.volume_change_safety_margin;
3758 const char *direction = NULL;
3761 nc = pa_sink_volume_change_new(s);
3763 /* NOTE: There is already more different volumes in pa_sink that I can remember.
3764 * Adding one more volume for HW would get us rid of this, but I am trying
3765 * to survive with the ones we already have. */
3766 pa_sw_cvolume_divide(&nc->hw_volume, &s->real_volume, &s->soft_volume);
3768 if (!s->thread_info.volume_changes && pa_cvolume_equal(&nc->hw_volume, &s->thread_info.current_hw_volume)) {
3769 pa_log_debug("Volume not changing");
3770 pa_sink_volume_change_free(nc);
3774 nc->at = pa_sink_get_latency_within_thread(s, false);
3775 nc->at += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
3777 if (s->thread_info.volume_changes_tail) {
3778 for (c = s->thread_info.volume_changes_tail; c; c = c->prev) {
3779 /* If volume is going up let's do it a bit late. If it is going
3780 * down let's do it a bit early. */
3781 if (pa_cvolume_avg(&nc->hw_volume) > pa_cvolume_avg(&c->hw_volume)) {
3782 if (nc->at + safety_margin > c->at) {
3783 nc->at += safety_margin;
3788 else if (nc->at - safety_margin > c->at) {
3789 nc->at -= safety_margin;
3797 if (pa_cvolume_avg(&nc->hw_volume) > pa_cvolume_avg(&s->thread_info.current_hw_volume)) {
3798 nc->at += safety_margin;
3801 nc->at -= safety_margin;
3804 PA_LLIST_PREPEND(pa_sink_volume_change, s->thread_info.volume_changes, nc);
3807 PA_LLIST_INSERT_AFTER(pa_sink_volume_change, s->thread_info.volume_changes, c, nc);
3810 pa_log_debug("Volume going %s to %d at %llu", direction, pa_cvolume_avg(&nc->hw_volume), (long long unsigned) nc->at);
3812 /* We can ignore volume events that came earlier but should happen later than this. */
3813 PA_LLIST_FOREACH_SAFE(c, pc, nc->next) {
3814 pa_log_debug("Volume change to %d at %llu was dropped", pa_cvolume_avg(&c->hw_volume), (long long unsigned) c->at);
3815 pa_sink_volume_change_free(c);
3818 s->thread_info.volume_changes_tail = nc;
3821 /* Called from the IO thread. */
3822 static void pa_sink_volume_change_flush(pa_sink *s) {
3823 pa_sink_volume_change *c = s->thread_info.volume_changes;
3825 s->thread_info.volume_changes = NULL;
3826 s->thread_info.volume_changes_tail = NULL;
3828 pa_sink_volume_change *next = c->next;
3829 pa_sink_volume_change_free(c);
3834 /* Called from the IO thread. */
3835 bool pa_sink_volume_change_apply(pa_sink *s, pa_usec_t *usec_to_next) {
3841 if (!s->thread_info.volume_changes || !PA_SINK_IS_LINKED(s->state)) {
3847 pa_assert(s->write_volume);
3849 now = pa_rtclock_now();
3851 while (s->thread_info.volume_changes && now >= s->thread_info.volume_changes->at) {
3852 pa_sink_volume_change *c = s->thread_info.volume_changes;
3853 PA_LLIST_REMOVE(pa_sink_volume_change, s->thread_info.volume_changes, c);
3854 pa_log_debug("Volume change to %d at %llu was written %llu usec late",
3855 pa_cvolume_avg(&c->hw_volume), (long long unsigned) c->at, (long long unsigned) (now - c->at));
3857 s->thread_info.current_hw_volume = c->hw_volume;
3858 pa_sink_volume_change_free(c);
3864 if (s->thread_info.volume_changes) {
3866 *usec_to_next = s->thread_info.volume_changes->at - now;
3867 if (pa_log_ratelimit(PA_LOG_DEBUG))
3868 pa_log_debug("Next volume change in %lld usec", (long long) (s->thread_info.volume_changes->at - now));
3873 s->thread_info.volume_changes_tail = NULL;
3878 /* Called from the IO thread. */
3879 static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes) {
3880 /* All the queued volume events later than current latency are shifted to happen earlier. */
3881 pa_sink_volume_change *c;
3882 pa_volume_t prev_vol = pa_cvolume_avg(&s->thread_info.current_hw_volume);
3883 pa_usec_t rewound = pa_bytes_to_usec(nbytes, &s->sample_spec);
3884 pa_usec_t limit = pa_sink_get_latency_within_thread(s, false);
3886 pa_log_debug("latency = %lld", (long long) limit);
3887 limit += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
3889 PA_LLIST_FOREACH(c, s->thread_info.volume_changes) {
3890 pa_usec_t modified_limit = limit;
3891 if (prev_vol > pa_cvolume_avg(&c->hw_volume))
3892 modified_limit -= s->thread_info.volume_change_safety_margin;
3894 modified_limit += s->thread_info.volume_change_safety_margin;
3895 if (c->at > modified_limit) {
3897 if (c->at < modified_limit)
3898 c->at = modified_limit;
3900 prev_vol = pa_cvolume_avg(&c->hw_volume);
3902 pa_sink_volume_change_apply(s, NULL);
3905 /* Called from the main thread */
3906 /* Gets the list of formats supported by the sink. The members and idxset must
3907 * be freed by the caller. */
3908 pa_idxset* pa_sink_get_formats(pa_sink *s) {
3913 if (s->get_formats) {
3914 /* Sink supports format query, all is good */
3915 ret = s->get_formats(s);
3917 /* Sink doesn't support format query, so assume it does PCM */
3918 pa_format_info *f = pa_format_info_new();
3919 f->encoding = PA_ENCODING_PCM;
3921 ret = pa_idxset_new(NULL, NULL);
3922 pa_idxset_put(ret, f, NULL);
3928 /* Called from the main thread */
3929 /* Allows an external source to set what formats a sink supports if the sink
3930 * permits this. The function makes a copy of the formats on success. */
3931 bool pa_sink_set_formats(pa_sink *s, pa_idxset *formats) {
3936 /* Sink supports setting formats -- let's give it a shot */
3937 return s->set_formats(s, formats);
3939 /* Sink doesn't support setting this -- bail out */
3943 /* Called from the main thread */
3944 /* Checks if the sink can accept this format */
3945 bool pa_sink_check_format(pa_sink *s, pa_format_info *f) {
3946 pa_idxset *formats = NULL;
3952 formats = pa_sink_get_formats(s);
3955 pa_format_info *finfo_device;
3958 PA_IDXSET_FOREACH(finfo_device, formats, i) {
3959 if (pa_format_info_is_compatible(finfo_device, f)) {
3965 pa_idxset_free(formats, (pa_free_cb_t) pa_format_info_free);
3971 /* Called from the main thread */
3972 /* Calculates the intersection between formats supported by the sink and
3973 * in_formats, and returns these, in the order of the sink's formats. */
3974 pa_idxset* pa_sink_check_formats(pa_sink *s, pa_idxset *in_formats) {
3975 pa_idxset *out_formats = pa_idxset_new(NULL, NULL), *sink_formats = NULL;
3976 pa_format_info *f_sink, *f_in;
3981 if (!in_formats || pa_idxset_isempty(in_formats))
3984 sink_formats = pa_sink_get_formats(s);
3986 PA_IDXSET_FOREACH(f_sink, sink_formats, i) {
3987 PA_IDXSET_FOREACH(f_in, in_formats, j) {
3988 if (pa_format_info_is_compatible(f_sink, f_in))
3989 pa_idxset_put(out_formats, pa_format_info_copy(f_in), NULL);
3995 pa_idxset_free(sink_formats, (pa_free_cb_t) pa_format_info_free);
4000 /* Called from the main thread */
4001 void pa_sink_set_sample_format(pa_sink *s, pa_sample_format_t format) {
4002 pa_sample_format_t old_format;
4005 pa_assert(pa_sample_format_valid(format));
4007 old_format = s->sample_spec.format;
4008 if (old_format == format)
4011 pa_log_info("%s: format: %s -> %s",
4012 s->name, pa_sample_format_to_string(old_format), pa_sample_format_to_string(format));
4014 s->sample_spec.format = format;
4016 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
4019 /* Called from the main thread */
4020 void pa_sink_set_sample_rate(pa_sink *s, uint32_t rate) {
4024 pa_assert(pa_sample_rate_valid(rate));
4026 old_rate = s->sample_spec.rate;
4027 if (old_rate == rate)
4030 pa_log_info("%s: rate: %u -> %u", s->name, old_rate, rate);
4032 s->sample_spec.rate = rate;
4034 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
4037 /* Called from the main thread. */
4038 void pa_sink_set_reference_volume_direct(pa_sink *s, const pa_cvolume *volume) {
4039 pa_cvolume old_volume;
4040 char old_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
4041 char new_volume_str[PA_CVOLUME_SNPRINT_VERBOSE_MAX];
4046 old_volume = s->reference_volume;
4048 if (pa_cvolume_equal(volume, &old_volume))
4051 s->reference_volume = *volume;
4052 pa_log_debug("The reference volume of sink %s changed from %s to %s.", s->name,
4053 pa_cvolume_snprint_verbose(old_volume_str, sizeof(old_volume_str), &old_volume, &s->channel_map,
4054 s->flags & PA_SINK_DECIBEL_VOLUME),
4055 pa_cvolume_snprint_verbose(new_volume_str, sizeof(new_volume_str), volume, &s->channel_map,
4056 s->flags & PA_SINK_DECIBEL_VOLUME));
4058 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
4059 pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_VOLUME_CHANGED], s);