[pulseaudio-commits] [Git][pulseaudio/pulseaudio][master] 8 commits: sink-input: change bool save_sink to char *preferred_sink

Georg Chini gitlab at gitlab.freedesktop.org
Wed Nov 27 08:04:48 UTC 2019



Georg Chini pushed to branch master at PulseAudio / pulseaudio


Commits:
fbf87166 by Hui Wang at 2019-11-25T11:43:53Z
sink-input: change bool save_sink to char *preferred_sink

The finial objective is to store the preferred sink name in the
sink-input struct, and use module-stream-restore to save and restore
it.

This patch just replaces the save_sink with preferred_sink, and tries
to keep the original logic.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
24d5d180 by Hui Wang at 2019-11-25T11:43:53Z
sink-input: add a new API pa_sink_input_set_preferred_sink

If the sink here is NULL, that means users want to clear the
preferred_sink and move the sink-input to the default_sink, otherwise
set the preferred_sink to the sink->name and move the sink-input to
the sink. After that fire the sink_input_change event.

After adding this API, we can use this API to simplify the entry_apply
in the module-stream-restore.c.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
bc0e7283 by Hui Wang at 2019-11-26T11:38:29Z
sink-input: clear the preferred_sink if it is default_sink

When the user moves a stream to the current default sink, the
preferred_sink should be set to NULL and module-stream-restore
should clear the routing for that stream in the stream database. From
that point on the stream will be always routed to the default sink.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
40d92e9b by Hui Wang at 2019-11-27T04:09:21Z
core: move sink-inputs conditionally when update default_sink

When the default sink changes, the streams from the old default sink
should be moved to the new default sink, unless the preferred_sink
string is set to the old default sink and the active port of the old
default sink is not unavailable

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
b8868366 by Hui Wang at 2019-11-27T04:11:28Z
sink: move streams to new appeared sinks if they prefer these sinks

When a new sink appears, all streams that have their preferred_sink
set to the new sink should be moved to the new sink.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
60d94861 by Hui Wang at 2019-11-27T04:23:17Z
device-port: moving streams due to changing the status of active_port

When the active port of a sink becomes unavailable, all streams from
that sink should be moved to the default sink.

When the active port of a sink changes state from unavailable, all
streams that have their preferred_sink set to this sink should be moved
to this sink.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
43e3a7f3 by Hui Wang at 2019-11-27T04:26:34Z
sink: move the streams to the default_sink when the sink is unlinked

When a sink is unlinked, all streams of this sink are moved to
default_sink, this action is implemented in the core rather than
modules now.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -
f899d5f4 by Hui Wang at 2019-11-27T04:26:40Z
stream-restore: skip entries setting action from gnome-control-center

When users select an output device from gnome-control-center UI, the
sink of this output device will be set to the configured_default_sink
and the default_sink, these actions are expected, but after these
actions, the gnome-control-center will call extension_cb() to modify
the entries in the database, let all stream entries to bind the sink
users select, this is not correct since the sink is default_sink now.

This is a temp fix for this issue, after gnome-control-center fixes
this problem, this patch should be reverted.

Signed-off-by: Hui Wang <hui.wang at canonical.com>

- - - - -


13 changed files:

- src/modules/module-device-manager.c
- src/modules/module-intended-roles.c
- src/modules/module-rescue-streams.c
- src/modules/module-stream-restore.c
- src/modules/module-switch-on-connect.c
- src/pulsecore/core.c
- src/pulsecore/core.h
- src/pulsecore/device-port.c
- src/pulsecore/device-port.h
- src/pulsecore/sink-input.c
- src/pulsecore/sink-input.h
- src/pulsecore/sink.c
- src/pulsecore/sink.h


Changes:

=====================================
src/modules/module-device-manager.c
=====================================
@@ -656,14 +656,14 @@ static void route_sink_input(struct userdata *u, pa_sink_input *si) {
     pa_assert(u);
     pa_assert(u->do_routing);
 
-    /* Don't override user or application routing requests. */
-    if (si->save_sink || si->sink_requested_by_application)
-        return;
-
     /* Skip this if it is already in the process of being moved anyway */
     if (!si->sink)
         return;
 
+    /* Don't override user or application routing requests. */
+    if (pa_safe_streq(si->sink->name, si->preferred_sink) || si->sink_requested_by_application)
+        return;
+
     auto_filtered_prop = pa_proplist_gets(si->proplist, "module-device-manager.auto_filtered");
     if (auto_filtered_prop)
         auto_filtered = (pa_parse_boolean(auto_filtered_prop) == 1);


=====================================
src/modules/module-intended-roles.c
=====================================
@@ -175,14 +175,14 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct
         if (si->sink == sink)
             continue;
 
-        if (si->save_sink)
-            continue;
-
         /* Skip this if it is already in the process of being moved
          * anyway */
         if (!si->sink)
             continue;
 
+        if (pa_safe_streq(si->sink->name, si->preferred_sink))
+            continue;
+
         /* It might happen that a stream and a sink are set up at the
            same time, in which case we want to make sure we don't
            interfere with that */


=====================================
src/modules/module-rescue-streams.c
=====================================
@@ -24,7 +24,6 @@
 #include <pulse/xmalloc.h>
 
 #include <pulsecore/core.h>
-#include <pulsecore/sink-input.h>
 #include <pulsecore/source-output.h>
 #include <pulsecore/modargs.h>
 #include <pulsecore/log.h>
@@ -32,7 +31,7 @@
 #include <pulsecore/core-util.h>
 
 PA_MODULE_AUTHOR("Lennart Poettering");
-PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move its streams to the default sink/source");
+PA_MODULE_DESCRIPTION("When a source is removed, try to move its streams to the default source");
 PA_MODULE_VERSION(PACKAGE_VERSION);
 PA_MODULE_LOAD_ONCE(true);
 
@@ -42,9 +41,7 @@ static const char* const valid_modargs[] = {
 
 struct userdata {
     pa_hook_slot
-        *sink_unlink_slot,
         *source_unlink_slot,
-        *sink_input_move_fail_slot,
         *source_output_move_fail_slot;
 };
 
@@ -65,23 +62,6 @@ static pa_source* find_source_from_port(pa_core *c, pa_device_port *port) {
     return NULL;
 }
 
-static pa_sink* find_sink_from_port(pa_core *c, pa_device_port *port) {
-    pa_sink *target;
-    uint32_t idx;
-    void *state;
-    pa_device_port *p;
-
-    if (!port)
-        return NULL;
-
-    PA_IDXSET_FOREACH(target, c->sinks, idx)
-        PA_HASHMAP_FOREACH(p, target->ports, state)
-            if (port == p)
-                return target;
-
-    return NULL;
-}
-
 static void build_group_ports(pa_hashmap *g_ports, pa_hashmap *s_ports) {
     void *state;
     pa_device_port *p;
@@ -93,112 +73,6 @@ static void build_group_ports(pa_hashmap *g_ports, pa_hashmap *s_ports) {
         pa_hashmap_put(g_ports, p, p);
 }
 
-static pa_sink* find_evacuation_sink(pa_core *c, pa_sink_input *i, pa_sink *skip) {
-    pa_sink *target, *fb_sink = NULL;
-    uint32_t idx;
-    pa_hashmap *all_ports;
-    pa_device_port *best_port;
-
-    pa_assert(c);
-    pa_assert(i);
-
-    if (c->default_sink && c->default_sink != skip && pa_sink_input_may_move_to(i, c->default_sink))
-        return c->default_sink;
-
-    all_ports = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
-
-    PA_IDXSET_FOREACH(target, c->sinks, idx) {
-        if (target == c->default_sink)
-            continue;
-
-        if (target == skip)
-            continue;
-
-        if (!PA_SINK_IS_LINKED(target->state))
-            continue;
-
-        if (!pa_sink_input_may_move_to(i, target))
-            continue;
-
-        if (!fb_sink)
-            fb_sink = target;
-
-        build_group_ports(all_ports, target->ports);
-    }
-
-    best_port = pa_device_port_find_best(all_ports);
-
-    pa_hashmap_free(all_ports);
-
-    if (best_port)
-        target = find_sink_from_port(c, best_port);
-    else
-        target = fb_sink;
-
-    if (!target)
-        pa_log_debug("No evacuation sink found.");
-
-    return target;
-}
-
-static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
-    pa_sink_input *i;
-    uint32_t idx;
-
-    pa_assert(c);
-    pa_assert(sink);
-
-    /* There's no point in doing anything if the core is shut down anyway */
-    if (c->state == PA_CORE_SHUTDOWN)
-        return PA_HOOK_OK;
-
-    if (pa_idxset_size(sink->inputs) <= 0) {
-        pa_log_debug("No sink inputs to move away.");
-        return PA_HOOK_OK;
-    }
-
-    PA_IDXSET_FOREACH(i, sink->inputs, idx) {
-        pa_sink *target;
-
-        if (!(target = find_evacuation_sink(c, i, sink)))
-            continue;
-
-        if (pa_sink_input_move_to(i, target, false) < 0)
-            pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
-                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
-        else
-            pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
-                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
-    }
-
-    return PA_HOOK_OK;
-}
-
-static pa_hook_result_t sink_input_move_fail_hook_callback(pa_core *c, pa_sink_input *i, void *userdata) {
-    pa_sink *target;
-
-    pa_assert(c);
-    pa_assert(i);
-
-    /* There's no point in doing anything if the core is shut down anyway */
-    if (c->state == PA_CORE_SHUTDOWN)
-        return PA_HOOK_OK;
-
-    if (!(target = find_evacuation_sink(c, i, NULL)))
-        return PA_HOOK_OK;
-
-    if (pa_sink_input_finish_move(i, target, false) < 0) {
-        pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
-                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
-        return PA_HOOK_OK;
-
-    } else {
-        pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
-                    pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
-        return PA_HOOK_STOP;
-    }
-}
-
 static pa_source* find_evacuation_source(pa_core *c, pa_source_output *o, pa_source *skip) {
     pa_source *target, *fb_source = NULL;
     uint32_t idx;
@@ -323,10 +197,8 @@ int pa__init(pa_module*m) {
     m->userdata = u = pa_xnew(struct userdata, 1);
 
     /* A little bit later than module-stream-restore, module-intended-roles... */
-    u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_unlink_hook_callback, u);
     u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_unlink_hook_callback, u);
 
-    u->sink_input_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_input_move_fail_hook_callback, u);
     u->source_output_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) source_output_move_fail_hook_callback, u);
 
     pa_modargs_free(ma);
@@ -341,13 +213,9 @@ void pa__done(pa_module*m) {
     if (!(u = m->userdata))
         return;
 
-    if (u->sink_unlink_slot)
-        pa_hook_slot_free(u->sink_unlink_slot);
     if (u->source_unlink_slot)
         pa_hook_slot_free(u->source_unlink_slot);
 
-    if (u->sink_input_move_fail_slot)
-        pa_hook_slot_free(u->sink_input_move_fail_slot);
     if (u->source_output_move_fail_slot)
         pa_hook_slot_free(u->source_output_move_fail_slot);
 


=====================================
src/modules/module-stream-restore.c
=====================================
@@ -95,9 +95,7 @@ struct userdata {
         *sink_input_fixate_hook_slot,
         *source_output_new_hook_slot,
         *source_output_fixate_hook_slot,
-        *sink_put_hook_slot,
         *source_put_hook_slot,
-        *sink_unlink_hook_slot,
         *source_unlink_hook_slot,
         *connection_unlink_hook_slot;
     pa_time_event *save_time_event;
@@ -1311,19 +1309,24 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
             mute_updated = !created_new_entry && (!old->muted_valid || entry->muted != old->muted);
         }
 
-        if (sink_input->save_sink) {
+        if (sink_input->preferred_sink != NULL || !created_new_entry) {
+            pa_sink *s = NULL;
+
             pa_xfree(entry->device);
-            entry->device = pa_xstrdup(sink_input->sink->name);
+            entry->device = pa_xstrdup(sink_input->preferred_sink);
             entry->device_valid = true;
-
-            device_updated = !created_new_entry && (!old->device_valid || !pa_streq(entry->device, old->device));
-            if (sink_input->sink->card) {
-                pa_xfree(entry->card);
-                entry->card = pa_xstrdup(sink_input->sink->card->name);
+            if (!entry->device)
+                entry->device_valid = false;
+
+            device_updated = !created_new_entry && !pa_safe_streq(entry->device, old->device);
+            pa_xfree(entry->card);
+            entry->card = NULL;
+            entry->card_valid = false;
+            if (entry->device_valid && (s = pa_namereg_get(c, entry->device, PA_NAMEREG_SINK)) && s->card) {
+                entry->card = pa_xstrdup(s->card->name);
                 entry->card_valid = true;
             }
         }
-
     } else {
         pa_source_output *source_output;
 
@@ -1634,57 +1637,6 @@ static pa_hook_result_t source_output_fixate_hook_callback(pa_core *c, pa_source
     return PA_HOOK_OK;
 }
 
-static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
-    pa_sink_input *si;
-    uint32_t idx;
-
-    pa_assert(c);
-    pa_assert(sink);
-    pa_assert(u);
-    pa_assert(u->on_hotplug && u->restore_device);
-
-    PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
-        char *name;
-        struct entry *e;
-
-        if (si->sink == sink)
-            continue;
-
-        if (si->save_sink)
-            continue;
-
-        /* Skip this if it is already in the process of being moved
-         * anyway */
-        if (!si->sink)
-            continue;
-
-        /* Skip this sink input if it is connecting a filter sink to
-         * the master */
-        if (si->origin_sink)
-            continue;
-
-        /* It might happen that a stream and a sink are set up at the
-           same time, in which case we want to make sure we don't
-           interfere with that */
-        if (!PA_SINK_INPUT_IS_LINKED(si->state))
-            continue;
-
-        if (!(name = pa_proplist_get_stream_group(si->proplist, "sink-input", IDENTIFICATION_PROPERTY)))
-            continue;
-
-        if ((e = entry_read(u, name))) {
-            if (e->device_valid && pa_streq(e->device, sink->name))
-                pa_sink_input_move_to(si, sink, true);
-
-            entry_free(e);
-        }
-
-        pa_xfree(name);
-    }
-
-    return PA_HOOK_OK;
-}
-
 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
     pa_source_output *so;
     uint32_t idx;
@@ -1738,54 +1690,6 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
     return PA_HOOK_OK;
 }
 
-static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
-    pa_sink_input *si;
-    uint32_t idx;
-
-    pa_assert(c);
-    pa_assert(sink);
-    pa_assert(u);
-    pa_assert(u->on_rescue && u->restore_device);
-
-    /* There's no point in doing anything if the core is shut down anyway */
-    if (c->state == PA_CORE_SHUTDOWN)
-        return PA_HOOK_OK;
-
-    PA_IDXSET_FOREACH(si, sink->inputs, idx) {
-        char *name;
-        struct entry *e;
-
-        if (!si->sink)
-            continue;
-
-        /* Skip this sink input if it is connecting a filter sink to
-         * the master */
-        if (si->origin_sink)
-            continue;
-
-        if (!(name = pa_proplist_get_stream_group(si->proplist, "sink-input", IDENTIFICATION_PROPERTY)))
-            continue;
-
-        if ((e = entry_read(u, name))) {
-
-            if (e->device_valid) {
-                pa_sink *d;
-
-                if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) &&
-                    d != sink &&
-                    PA_SINK_IS_LINKED(d->state))
-                    pa_sink_input_move_to(si, d, true);
-            }
-
-            entry_free(e);
-        }
-
-        pa_xfree(name);
-    }
-
-    return PA_HOOK_OK;
-}
-
 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
     pa_source_output *so;
     uint32_t idx;
@@ -1951,21 +1855,16 @@ static void entry_apply(struct userdata *u, const char *name, struct entry *e) {
 
         if (u->restore_device) {
             if (!e->device_valid) {
-                if (si->save_sink) {
+                if (si->preferred_sink != NULL) {
                     pa_log_info("Ensuring device is not saved for stream %s.", name);
                     /* If the device is not valid we should make sure the
-                       save flag is cleared as the user may have specifically
+                       preferred_sink is cleared as the user may have specifically
                        removed the sink element from the rule. */
-                    si->save_sink = false;
-                    /* This is cheating a bit. The sink input itself has not changed
-                       but the rules governing its routing have, so we fire this event
-                       such that other routing modules (e.g. module-device-manager)
-                       will pick up the change and reapply their routing */
-                    pa_subscription_post(si->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, si->index);
+                    pa_sink_input_set_preferred_sink(si, NULL);
                 }
             } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SINK))) {
                 pa_log_info("Restoring device for stream %s.", name);
-                pa_sink_input_move_to(si, s, true);
+                pa_sink_input_set_preferred_sink(si, s);
             }
         }
     }
@@ -2152,7 +2051,7 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
             }
 
             while (!pa_tagstruct_eof(t)) {
-                const char *name, *device;
+                const char *name, *device, *client_name;
                 bool muted;
                 struct entry *entry;
 #ifdef HAVE_DBUS
@@ -2193,7 +2092,18 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
                     entry_free(entry);
                     goto fail;
                 }
-
+                /* When users select an output device from gnome-control-center, the gnome-control-center will change all entries
+                 * in the database to bind the sink of this output device, this is not correct since at this moment, the sink is
+                 * default_sink and we shouldn't bind a stream to default_sink via preferred_sink or database.
+                 * After gnome-control-center fix the issue, let us remove this code */
+                client_name = pa_strnull(pa_proplist_gets(pa_native_connection_get_client(c)->proplist, PA_PROP_APPLICATION_PROCESS_BINARY));
+                if (pa_safe_streq(client_name, "gnome-control-center")) {
+                    if (entry->device_valid && m->core->default_sink && pa_safe_streq(device, m->core->default_sink->name)) {
+                        entry_free(entry);
+                        pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c), reply);
+                        return 0;
+                    }
+                }
 #ifdef HAVE_DBUS
                 old = entry_read(u, name);
 #endif
@@ -2465,13 +2375,11 @@ int pa__init(pa_module*m) {
 
     if (restore_device && on_hotplug) {
         /* A little bit earlier than module-intended-roles ... */
-        pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_callback, u);
         pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_put_hook_callback, u);
     }
 
     if (restore_device && on_rescue) {
         /* A little bit earlier than module-intended-roles, module-rescue-streams, ... */
-        pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_unlink_hook_callback, u);
         pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_hook_callback, u);
     }
 


=====================================
src/modules/module-switch-on-connect.c
=====================================
@@ -57,9 +57,6 @@ struct userdata {
 };
 
 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
-    pa_sink_input *i;
-    uint32_t idx;
-    pa_sink *old_default_sink;
     const char *s;
     struct userdata *u = userdata;
 
@@ -112,29 +109,9 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void*
             return PA_HOOK_OK;
         }
 
-    old_default_sink = c->default_sink;
-
     /* Actually do the switch to the new sink */
     pa_core_set_configured_default_sink(c, sink->name);
 
-    /* Now move all old inputs over */
-    if (pa_idxset_size(old_default_sink->inputs) <= 0) {
-        pa_log_debug("No sink inputs to move away.");
-        return PA_HOOK_OK;
-    }
-
-    PA_IDXSET_FOREACH(i, old_default_sink->inputs, idx) {
-        if (i->save_sink || !PA_SINK_INPUT_IS_LINKED(i->state))
-            continue;
-
-        if (pa_sink_input_move_to(i, sink, false) < 0)
-            pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
-                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
-        else
-            pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
-                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), sink->name);
-    }
-
     return PA_HOOK_OK;
 }
 


=====================================
src/pulsecore/core.c
=====================================
@@ -349,6 +349,10 @@ void pa_core_update_default_sink(pa_core *core) {
 
     pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
     pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SINK_CHANGED], core->default_sink);
+
+    /* try to move the streams from old_default_sink to the new default_sink conditionally */
+    if (old_default_sink)
+        pa_sink_move_streams_to_default_sink(core, old_default_sink, true);
 }
 
 /* a  < b  ->  return -1
@@ -519,6 +523,37 @@ void pa_core_rttime_restart(pa_core *c, pa_time_event *e, pa_usec_t usec) {
     c->mainloop->time_restart(e, pa_timeval_rtstore(&tv, usec, true));
 }
 
+void pa_core_move_streams_to_newly_available_preferred_sink(pa_core *c, pa_sink *s) {
+    pa_sink_input *si;
+    uint32_t idx;
+
+    pa_assert(c);
+    pa_assert(s);
+
+    PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
+        if (si->sink == s)
+            continue;
+
+        if (!si->sink)
+            continue;
+
+        /* Skip this sink input if it is connecting a filter sink to
+         * the master */
+        if (si->origin_sink)
+            continue;
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SINK_INPUT_IS_LINKED(si->state))
+            continue;
+
+        if (pa_safe_streq(si->preferred_sink, s->name))
+            pa_sink_input_move_to(si, s, false);
+    }
+
+}
+
 /* Helper macro to reduce repetition in pa_suspend_cause_to_string().
  * Parameters:
  *   char *p: the current position in the write buffer


=====================================
src/pulsecore/core.h
=====================================
@@ -278,4 +278,6 @@ static const size_t PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE =
  * provided buffer. The same buffer is the return value of this function. */
 const char *pa_suspend_cause_to_string(pa_suspend_cause_t cause, char buf[PA_SUSPEND_CAUSE_TO_STRING_BUF_SIZE]);
 
+void pa_core_move_streams_to_newly_available_preferred_sink(pa_core *c, pa_sink *s);
+
 #endif


=====================================
src/pulsecore/device-port.c
=====================================
@@ -100,6 +100,18 @@ void pa_device_port_set_available(pa_device_port *p, pa_available_t status) {
         else
             pa_core_update_default_source(p->core);
 
+        if (p->direction == PA_DIRECTION_OUTPUT) {
+            pa_sink *sink;
+
+            sink = pa_device_port_get_sink(p);
+            if (sink && p == sink->active_port) {
+                if (sink->active_port->available == PA_AVAILABLE_NO)
+                    pa_sink_move_streams_to_default_sink(p->core, sink, false);
+                else
+                    pa_core_move_streams_to_newly_available_preferred_sink(p->core, sink);
+            }
+        }
+
         pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
         pa_hook_fire(&p->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], p);
     }
@@ -224,3 +236,16 @@ pa_device_port *pa_device_port_find_best(pa_hashmap *ports)
 
     return best;
 }
+
+pa_sink *pa_device_port_get_sink(pa_device_port *p) {
+    pa_sink *rs = NULL;
+    pa_sink *sink;
+    uint32_t state;
+
+    PA_IDXSET_FOREACH(sink, p->card->sinks, state)
+        if (p == pa_hashmap_get(sink->ports, p->name)) {
+            rs = sink;
+            break;
+        }
+    return rs;
+}


=====================================
src/pulsecore/device-port.h
=====================================
@@ -87,4 +87,6 @@ void pa_device_port_set_preferred_profile(pa_device_port *p, const char *new_pp)
 
 pa_device_port *pa_device_port_find_best(pa_hashmap *ports);
 
+pa_sink *pa_device_port_get_sink(pa_device_port *p);
+
 #endif


=====================================
src/pulsecore/sink-input.c
=====================================
@@ -190,7 +190,10 @@ bool pa_sink_input_new_data_set_sink(pa_sink_input_new_data *data, pa_sink *s, b
     if (!data->req_formats) {
         /* We're not working with the extended API */
         data->sink = s;
-        data->save_sink = save;
+        if (save) {
+            pa_xfree(data->preferred_sink);
+            data->preferred_sink = pa_xstrdup(s->name);
+	}
         data->sink_requested_by_application = requested_by_application;
     } else {
         /* Extended API: let's see if this sink supports the formats the client can provide */
@@ -199,7 +202,10 @@ bool pa_sink_input_new_data_set_sink(pa_sink_input_new_data *data, pa_sink *s, b
         if (formats && !pa_idxset_isempty(formats)) {
             /* Sink supports at least one of the requested formats */
             data->sink = s;
-            data->save_sink = save;
+	    if (save) {
+		pa_xfree(data->preferred_sink);
+		data->preferred_sink = pa_xstrdup(s->name);
+	    }
             data->sink_requested_by_application = requested_by_application;
             if (data->nego_formats)
                 pa_idxset_free(data->nego_formats, (pa_free_cb_t) pa_format_info_free);
@@ -226,7 +232,7 @@ bool pa_sink_input_new_data_set_formats(pa_sink_input_new_data *data, pa_idxset
 
     if (data->sink) {
         /* Trigger format negotiation */
-        return pa_sink_input_new_data_set_sink(data, data->sink, data->save_sink, data->sink_requested_by_application);
+        return pa_sink_input_new_data_set_sink(data, data->sink, (data->preferred_sink != NULL), data->sink_requested_by_application);
     }
 
     return true;
@@ -250,6 +256,9 @@ void pa_sink_input_new_data_done(pa_sink_input_new_data *data) {
     if (data->volume_factor_sink_items)
         pa_hashmap_free(data->volume_factor_sink_items);
 
+    if (data->preferred_sink)
+        pa_xfree(data->preferred_sink);
+
     pa_proplist_free(data->proplist);
 }
 
@@ -518,7 +527,7 @@ int pa_sink_input_new(
     pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
     i->volume_writable = data->volume_writable;
     i->save_volume = data->save_volume;
-    i->save_sink = data->save_sink;
+    i->preferred_sink = pa_xstrdup(data->preferred_sink);
     i->save_muted = data->save_muted;
 
     i->muted = data->muted;
@@ -776,6 +785,9 @@ static void sink_input_free(pa_object *o) {
     if (i->volume_factor_sink_items)
         pa_hashmap_free(i->volume_factor_sink_items);
 
+    if (i->preferred_sink)
+        pa_xfree(i->preferred_sink);
+
     pa_xfree(i->driver);
     pa_xfree(i);
 }
@@ -1914,7 +1926,16 @@ int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, bool save) {
         i->moving(i, dest);
 
     i->sink = dest;
-    i->save_sink = save;
+    /* save == true, means user is calling the move_to() and want to
+       save the preferred_sink */
+    if (save) {
+        pa_xfree(i->preferred_sink);
+        if (dest == dest->core->default_sink)
+            i->preferred_sink = NULL;
+        else
+            i->preferred_sink = pa_xstrdup(dest->name);
+    }
+
     pa_idxset_put(dest->inputs, pa_sink_input_ref(i), NULL);
 
     PA_HASHMAP_FOREACH(v, i->volume_factor_sink_items, state)
@@ -2404,3 +2425,17 @@ void pa_sink_input_set_reference_ratio(pa_sink_input *i, const pa_cvolume *ratio
                  pa_cvolume_snprint_verbose(old_ratio_str, sizeof(old_ratio_str), &old_ratio, &i->channel_map, true),
                  pa_cvolume_snprint_verbose(new_ratio_str, sizeof(new_ratio_str), ratio, &i->channel_map, true));
 }
+
+/* Called from the main thread. */
+void pa_sink_input_set_preferred_sink(pa_sink_input *i, pa_sink *s) {
+    pa_assert(i);
+
+    pa_xfree(i->preferred_sink);
+    if (s) {
+        i->preferred_sink = pa_xstrdup(s->name);
+        pa_sink_input_move_to(i, s, false);
+    } else {
+        i->preferred_sink = NULL;
+        pa_sink_input_move_to(i, i->core->default_sink, false);
+    }
+}


=====================================
src/pulsecore/sink-input.h
=====================================
@@ -119,11 +119,16 @@ struct pa_sink_input {
 
     bool muted:1;
 
-    /* if true then the sink we are connected to and/or the volume
-     * set is worth remembering, i.e. was explicitly chosen by the
-     * user and not automatically. module-stream-restore looks for
+    /* if true then the volume and the mute state of this sink-input
+     * are worth remembering, module-stream-restore looks for
      * this.*/
-    bool save_sink:1, save_volume:1, save_muted:1;
+    bool save_volume:1, save_muted:1;
+
+    /* if users move the sink-input to a sink, and the sink is not default_sink,
+     * the sink->name will be saved in preferred_sink. And later if sink-input
+     * is moved to other sinks for some reason, it still can be restored to the
+     * preferred_sink at an appropriate time */
+    char *preferred_sink;
 
     pa_resample_method_t requested_resample_method, actual_resample_method;
 
@@ -315,7 +320,9 @@ typedef struct pa_sink_input_new_data {
 
     bool volume_writable:1;
 
-    bool save_sink:1, save_volume:1, save_muted:1;
+    bool save_volume:1, save_muted:1;
+
+    char *preferred_sink;
 } pa_sink_input_new_data;
 
 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data);
@@ -454,6 +461,8 @@ void pa_sink_input_set_volume_direct(pa_sink_input *i, const pa_cvolume *volume)
  * i->reference_ratio and logs a message if the value changes. */
 void pa_sink_input_set_reference_ratio(pa_sink_input *i, const pa_cvolume *ratio);
 
+void pa_sink_input_set_preferred_sink(pa_sink_input *i, pa_sink *s);
+
 #define pa_sink_input_assert_io_context(s) \
     pa_assert(pa_thread_mq_get() || !PA_SINK_INPUT_IS_LINKED((s)->state))
 


=====================================
src/pulsecore/sink.c
=====================================
@@ -724,9 +724,14 @@ void pa_sink_put(pa_sink* s) {
     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
     pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
 
-    /* This function must be called after the PA_CORE_HOOK_SINK_PUT hook,
-     * because module-switch-on-connect needs to know the old default sink */
+    /* It's good to fire the SINK_PUT hook before updating the default sink,
+     * because module-switch-on-connect will set the new sink as the default
+     * sink, and if we were to call pa_core_update_default_sink() before that,
+     * the default sink might change twice, causing unnecessary stream moving. */
+
     pa_core_update_default_sink(s->core);
+
+    pa_core_move_streams_to_newly_available_preferred_sink(s->core, s);
 }
 
 /* Called from main context */
@@ -757,6 +762,9 @@ void pa_sink_unlink(pa_sink* s) {
 
     pa_core_update_default_sink(s->core);
 
+    if (linked)
+	pa_sink_move_streams_to_default_sink(s->core, s, false);
+
     if (s->card)
         pa_idxset_remove_by_data(s->card->sinks, s, NULL);
 
@@ -3931,3 +3939,47 @@ void pa_sink_set_reference_volume_direct(pa_sink *s, const pa_cvolume *volume) {
     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
     pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_VOLUME_CHANGED], s);
 }
+
+void pa_sink_move_streams_to_default_sink(pa_core *core, pa_sink *old_sink, bool default_sink_changed) {
+    pa_sink_input *i;
+    uint32_t idx;
+    bool old_sink_is_unavailable = false;
+
+    pa_assert(core);
+    pa_assert(old_sink);
+
+    if (core->state == PA_CORE_SHUTDOWN)
+        return;
+
+    if (core->default_sink == NULL || core->default_sink->unlink_requested)
+        return;
+
+    if (old_sink == core->default_sink)
+        return;
+
+    if (old_sink->active_port && old_sink->active_port->available == PA_AVAILABLE_NO)
+        old_sink_is_unavailable = true;
+
+    PA_IDXSET_FOREACH(i, old_sink->inputs, idx) {
+        if (!PA_SINK_INPUT_IS_LINKED(i->state))
+            continue;
+
+        if (!i->sink)
+            continue;
+
+        if (pa_safe_streq(old_sink->name, i->preferred_sink) && !old_sink_is_unavailable)
+            continue;
+
+        if (!pa_sink_input_may_move_to(i, core->default_sink))
+            continue;
+
+        if (default_sink_changed)
+            pa_log_info("The sink input %u \"%s\" is moving to %s due to change of the default sink.",
+                        i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), core->default_sink->name);
+        else
+            pa_log_info("The sink input %u \"%s\" is moving to %s due to unlink of a sink.",
+                        i->index, pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), core->default_sink->name);
+
+        pa_sink_input_move_to(i, core->default_sink, false);
+    }
+}


=====================================
src/pulsecore/sink.h
=====================================
@@ -558,6 +558,12 @@ int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative);
  * s->reference_volume and fires change notifications. */
 void pa_sink_set_reference_volume_direct(pa_sink *s, const pa_cvolume *volume);
 
+/* When the default_sink is changed or the active_port of a sink is changed to
+ * PA_AVAILABLE_NO, this function is called to move the streams of the old
+ * default_sink or the sink with active_port equals PA_AVAILABLE_NO to the
+ * current default_sink conditionally*/
+void pa_sink_move_streams_to_default_sink(pa_core *core, pa_sink *old_sink, bool default_sink_changed);
+
 /* Verify that we called in IO context (aka 'thread context), or that
  * the sink is not yet set up, i.e. the thread not set up yet. See
  * pa_assert_io_context() in thread-mq.h for more information. */



View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/compare/26a66d103fd4bb6b93f5ef025cfe20b0e08ff63c...f899d5f4669dcd536cc142cee99fe359dd8af3d6

-- 
View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/compare/26a66d103fd4bb6b93f5ef025cfe20b0e08ff63c...f899d5f4669dcd536cc142cee99fe359dd8af3d6
You're receiving this email because of your account on gitlab.freedesktop.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/pulseaudio-commits/attachments/20191127/d05808d0/attachment-0001.html>


More information about the pulseaudio-commits mailing list