[pulseaudio-discuss] [PATCH v3 5/8] sink: move streams to new appeared sinks if they prefer these sinks

Hui Wang hui.wang at canonical.com
Sun Aug 18 05:32:38 UTC 2019


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>
---
 src/modules/module-stream-restore.c | 53 -----------------------------
 src/pulsecore/core.c                | 28 +++++++++++++++
 src/pulsecore/core.h                |  2 ++
 src/pulsecore/sink.c                |  2 ++
 4 files changed, 32 insertions(+), 53 deletions(-)

diff --git a/src/modules/module-stream-restore.c b/src/modules/module-stream-restore.c
index 0d3f9d8ac..aa7bd38dc 100644
--- a/src/modules/module-stream-restore.c
+++ b/src/modules/module-stream-restore.c
@@ -95,7 +95,6 @@ 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,
@@ -1643,57 +1642,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 (pa_safe_streq(si->sink->name, si->preferred_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;
@@ -2471,7 +2419,6 @@ 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);
     }
 
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index a112ff753..e761a9934 100644
--- a/src/pulsecore/core.c
+++ b/src/pulsecore/core.c
@@ -523,6 +523,34 @@ 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;
+
+        /* 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
diff --git a/src/pulsecore/core.h b/src/pulsecore/core.h
index 04f11f8e9..255fae4ae 100644
--- a/src/pulsecore/core.h
+++ b/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
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index aca116c7f..274d5c673 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -727,6 +727,8 @@ void pa_sink_put(pa_sink* 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 */
     pa_core_update_default_sink(s->core, false);
+
+    pa_core_move_streams_to_newly_available_preferred_sink(s->core, s);
 }
 
 /* Called from main context */
-- 
2.17.1



More information about the pulseaudio-discuss mailing list