+void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) {
+ pa_sink_input *i;
+ uint32_t idx;
+
+ /* This is called whenever a sink input volume changes and we
+ * might need to fix up the sink volume accordingly. Please note
+ * that we don't actually update the sinks volume here, we only
+ * return how it needs to be updated. The caller should then call
+ * pa_sink_set_flat_volume().*/
+
+ pa_cvolume_mute(new_volume, s->channel_map.channels);
+
+ /* First let's determine the new maximum volume of all inputs
+ * connected to this sink */
+ for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
+ unsigned c;
+ pa_cvolume remapped_volume;
+
+ remapped_volume = i->virtual_volume;
+ pa_cvolume_remap(&remapped_volume, &i->channel_map, &s->channel_map);
+
+ for (c = 0; c < new_volume->channels; c++)
+ if (remapped_volume.values[c] > new_volume->values[c])
+ new_volume->values[c] = remapped_volume.values[c];
+ }
+
+ /* Then, let's update the soft volumes of all inputs connected
+ * to this sink */
+ for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
+ pa_cvolume remapped_new_volume;
+
+ remapped_new_volume = *new_volume;
+ pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
+ pa_sw_cvolume_divide(&i->soft_volume, &i->virtual_volume, &remapped_new_volume);
+
+ /* Hooks have the ability to play games with i->soft_volume */
+ pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i);
+
+ /* We don't issue PA_SINK_INPUT_MESSAGE_SET_VOLUME because
+ * we want the update to have atomically with the sink
+ * volume update, hence we do it within the
+ * pa_sink_set_flat_volume() call below*/
+ }
+}
+
+/* Called from main thread */
+void pa_sink_propagate_flat_volume(pa_sink *s, const pa_cvolume *old_volume) {
+ pa_sink_input *i;
+ uint32_t idx;
+
+ pa_sink_assert_ref(s);
+ pa_assert(PA_SINK_IS_LINKED(s->state));
+ pa_assert(old_volume);
+ pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
+
+ /* This is called whenever the sink volume changes that is not
+ * caused by a sink input volume change. We need to fix up the
+ * sink input volumes accordingly */
+
+ for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
+ pa_cvolume remapped_old_volume, remapped_new_volume, fixed_volume;
+ unsigned c;
+
+ remapped_new_volume = s->virtual_volume;
+ pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
+
+ remapped_old_volume = *old_volume;
+ pa_cvolume_remap(&remapped_old_volume, &s->channel_map, &i->channel_map);
+
+ for (c = 0; c < i->sample_spec.channels; c++)
+
+ if (remapped_old_volume.values[c] == PA_VOLUME_MUTED)
+ fixed_volume.values[c] = PA_VOLUME_MUTED;
+ else
+ fixed_volume.values[c] = (pa_volume_t)
+ ((uint64_t) i->virtual_volume.values[c] *
+ (uint64_t) remapped_new_volume.values[c] /
+ (uint64_t) remapped_old_volume.values[c]);
+
+ fixed_volume.channels = i->virtual_volume.channels;
+
+ if (!pa_cvolume_equal(&fixed_volume, &i->virtual_volume)) {
+ i->virtual_volume = fixed_volume;
+
+ /* The virtual volume changed, let's tell people so */
+ pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+ }
+ }
+}
+
+/* Called from main thread */
+void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg) {
+ pa_cvolume old_virtual_volume;
+ pa_bool_t virtual_volume_changed;