+ pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o->source), PA_SOURCE_MESSAGE_REMOVE_OUTPUT, o, 0, NULL) == 0);
+
+ pa_source_update_status(o->source);
+ o->source = NULL;
+
+ pa_source_output_unref(o);
+
+ return 0;
+}
+
+/* Called from main context. If it has an origin source that uses volume sharing,
+ * then also the origin source and all streams connected to it need to update
+ * their volume - this function does all that by using recursion. */
+static void update_volume_due_to_moving(pa_source_output *o, pa_source *dest) {
+ pa_cvolume old_volume;
+
+ pa_assert(o);
+ pa_assert(dest);
+ pa_assert(o->source); /* The destination source should already be set. */
+
+ if (o->destination_source && (o->destination_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
+ pa_source *root_source = o->source;
+ pa_source_output *destination_source_output;
+ uint32_t idx;
+
+ while (root_source->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)
+ root_source = root_source->output_from_master->source;
+
+ if (pa_source_flat_volume_enabled(o->source)) {
+ /* Ok, so the origin source uses volume sharing, and flat volume is
+ * enabled. The volume will have to be updated as follows:
+ *
+ * o->volume := o->source->real_volume
+ * (handled later by pa_source_set_volume)
+ * o->reference_ratio := o->volume / o->source->reference_volume
+ * (handled later by pa_source_set_volume)
+ * o->real_ratio stays unchanged
+ * (streams whose origin source uses volume sharing should
+ * always have real_ratio of 0 dB)
+ * o->soft_volume stays unchanged
+ * (streams whose origin source uses volume sharing should
+ * always have volume_factor as soft_volume, so no change
+ * should be needed) */
+
+ pa_assert(pa_cvolume_is_norm(&o->real_ratio));
+ pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
+
+ /* Notifications will be sent by pa_source_set_volume(). */
+
+ } else {
+ /* Ok, so the origin source uses volume sharing, and flat volume is
+ * disabled. The volume will have to be updated as follows:
+ *
+ * o->volume := 0 dB
+ * o->reference_ratio := 0 dB
+ * o->real_ratio stays unchanged
+ * (streams whose origin source uses volume sharing should
+ * always have real_ratio of 0 dB)
+ * o->soft_volume stays unchanged
+ * (streams whose origin source uses volume sharing should
+ * always have volume_factor as soft_volume, so no change
+ * should be needed) */
+
+ old_volume = o->volume;
+ pa_cvolume_reset(&o->volume, o->volume.channels);
+ pa_cvolume_reset(&o->reference_ratio, o->reference_ratio.channels);
+ pa_assert(pa_cvolume_is_norm(&o->real_ratio));
+ pa_assert(pa_cvolume_equal(&o->soft_volume, &o->volume_factor));
+
+ /* Notify others about the changed source output volume. */
+ if (!pa_cvolume_equal(&o->volume, &old_volume)) {
+ if (o->volume_changed)
+ o->volume_changed(o);
+
+ pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
+ }
+ }
+
+ /* Additionally, the origin source volume needs updating:
+ *
+ * o->destination_source->reference_volume := root_source->reference_volume
+ * o->destination_source->real_volume := root_source->real_volume
+ * o->destination_source->soft_volume stays unchanged
+ * (sources that use volume sharing should always have
+ * soft_volume of 0 dB) */
+
+ old_volume = o->destination_source->reference_volume;
+
+ o->destination_source->reference_volume = root_source->reference_volume;
+ pa_cvolume_remap(&o->destination_source->reference_volume, &root_source->channel_map, &o->destination_source->channel_map);
+
+ o->destination_source->real_volume = root_source->real_volume;
+ pa_cvolume_remap(&o->destination_source->real_volume, &root_source->channel_map, &o->destination_source->channel_map);
+
+ pa_assert(pa_cvolume_is_norm(&o->destination_source->soft_volume));
+
+ /* Notify others about the changed source volume. If you wonder whether
+ * o->destination_source->set_volume() should be called somewhere, that's not
+ * the case, because sources that use volume sharing shouldn't have any
+ * internal volume that set_volume() would update. If you wonder
+ * whether the thread_info variables should be synced, yes, they
+ * should, and it's done by the PA_SOURCE_MESSAGE_FINISH_MOVE message
+ * handler. */
+ if (!pa_cvolume_equal(&o->destination_source->reference_volume, &old_volume))
+ pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, o->destination_source->index);
+
+ /* Recursively update origin source outputs. */
+ PA_IDXSET_FOREACH(destination_source_output, o->destination_source->outputs, idx)
+ update_volume_due_to_moving(destination_source_output, dest);
+
+ } else {
+ old_volume = o->volume;
+
+ if (pa_source_flat_volume_enabled(o->source)) {
+ /* Ok, so this is a regular stream, and flat volume is enabled. The
+ * volume will have to be updated as follows:
+ *
+ * o->volume := o->reference_ratio * o->source->reference_volume
+ * o->reference_ratio stays unchanged
+ * o->real_ratio := o->volume / o->source->real_volume
+ * (handled later by pa_source_set_volume)
+ * o->soft_volume := o->real_ratio * o->volume_factor
+ * (handled later by pa_source_set_volume) */
+
+ o->volume = o->source->reference_volume;
+ pa_cvolume_remap(&o->volume, &o->source->channel_map, &o->channel_map);
+ pa_sw_cvolume_multiply(&o->volume, &o->volume, &o->reference_ratio);
+
+ } else {
+ /* Ok, so this is a regular stream, and flat volume is disabled.
+ * The volume will have to be updated as follows:
+ *
+ * o->volume := o->reference_ratio
+ * o->reference_ratio stays unchanged
+ * o->real_ratio := o->reference_ratio
+ * o->soft_volume := o->real_ratio * o->volume_factor */
+
+ o->volume = o->reference_ratio;
+ o->real_ratio = o->reference_ratio;
+ pa_sw_cvolume_multiply(&o->soft_volume, &o->real_ratio, &o->volume_factor);
+ }
+
+ /* Notify others about the changed source output volume. */
+ if (!pa_cvolume_equal(&o->volume, &old_volume)) {
+ /* XXX: In case o->source has flat volume enabled, then real_ratio
+ * and soft_volume are not updated yet. Let's hope that the
+ * callback implementation doesn't care about those variables... */
+ if (o->volume_changed)
+ o->volume_changed(o);
+
+ pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
+ }
+ }
+
+ /* If o->source == dest, then recursion has finished, and we can finally call
+ * pa_source_set_volume(), which will do the rest of the updates. */
+ if ((o->source == dest) && pa_source_flat_volume_enabled(o->source))
+ pa_source_set_volume(o->source, NULL, FALSE, o->save_volume);
+}
+
+/* Called from main context */
+int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, pa_bool_t save) {
+ pa_resampler *new_resampler;
+
+ pa_source_output_assert_ref(o);
+ pa_assert_ctl_context();
+ pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
+ pa_assert(!o->source);
+ pa_source_assert_ref(dest);
+
+ if (!pa_source_output_may_move_to(o, dest))
+ return -PA_ERR_NOTSUPPORTED;
+
+ if (pa_source_output_is_passthrough(o) && !pa_source_check_format(dest, o->format)) {
+ pa_proplist *p = pa_proplist_new();
+ pa_log_debug("New source doesn't support stream format, sending format-changed and killing");
+ /* Tell the client what device we want to be on if it is going to
+ * reconnect */
+ pa_proplist_sets(p, "device", dest->name);
+ pa_source_output_send_event(o, PA_STREAM_EVENT_FORMAT_LOST, p);
+ pa_proplist_free(p);
+ return -PA_ERR_NOTSUPPORTED;