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.w...@canonical.com>
---
 src/modules/module-stream-restore.c | 10 ++--------
 src/pulsecore/sink-input.c          | 14 ++++++++++++++
 src/pulsecore/sink-input.h          |  2 ++
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/modules/module-stream-restore.c 
b/src/modules/module-stream-restore.c
index a8c6a42c5..366f1a5a8 100644
--- a/src/modules/module-stream-restore.c
+++ b/src/modules/module-stream-restore.c
@@ -1953,18 +1953,12 @@ static void entry_apply(struct userdata *u, const char 
*name, struct entry *e) {
                        preferred_sink is cleared as the user may have 
specifically
                        removed the sink element from the rule. */
                     pa_xfree(si->preferred_sink);
-                    si->preferred_sink = NULL;
-                    /* 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_xfree(si->preferred_sink);
-                si->preferred_sink = pa_xstrdup(s->name);
+                pa_sink_input_set_preferred_sink(si, s);
             }
         }
     }
diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c
index f41eacf07..4dbc0cdef 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -2416,3 +2416,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);
+
+    if (s) {
+        i->preferred_sink = pa_xstrdup(s->name);
+        pa_sink_input_move_to(i, s, true);
+    } else {
+        i->preferred_sink = NULL;
+        pa_sink_input_move_to(i, i->core->default_sink, true);
+    }
+    pa_subscription_post(i->core, 
PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+}
diff --git a/src/pulsecore/sink-input.h b/src/pulsecore/sink-input.h
index 76140a2db..b67d3c1c3 100644
--- a/src/pulsecore/sink-input.h
+++ b/src/pulsecore/sink-input.h
@@ -460,6 +460,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))
 
-- 
2.17.1

_______________________________________________
pulseaudio-discuss mailing list
pulseaudio-discuss@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/pulseaudio-discuss

Reply via email to