From e529db75ec9406fb1e52ba60f025e92c0595398c Mon Sep 17 00:00:00 2001 From: Hui Wang Date: Sat, 7 Dec 2019 10:13:28 +0800 Subject: [PATCH] source-output: add a new API pa_source_output_set_preferred_source If the source here is NULL, that means users want to clear the preferred_source and move the source-output to the default_source, otherwise set the preferred_source to the source->name and move the source-output to the source. After that fire the source_output_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 --- src/modules/module-stream-restore.c | 10 ++-------- src/pulsecore/source-output.c | 14 ++++++++++++++ src/pulsecore/source-output.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 b0cce88c3..644571b43 100644 --- a/src/modules/module-stream-restore.c +++ b/src/modules/module-stream-restore.c @@ -1905,17 +1905,11 @@ static void entry_apply(struct userdata *u, const char *name, struct entry *e) { /* If the device is not valid we should make sure the preferred_source is cleared as the user may have specifically removed the source element from the rule. */ - pa_xfree(so->preferred_source); - so->preferred_source = NULL; - /* This is cheating a bit. The source output 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(so->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, so->index); + pa_source_output_set_preferred_source(so, NULL); } } else if ((s = pa_namereg_get(u->core, e->device, PA_NAMEREG_SOURCE))) { pa_log_info("Restoring device for stream %s.", name); - pa_source_output_move_to(so, s, true); + pa_source_output_set_preferred_source(so, s); } } } diff --git a/src/pulsecore/source-output.c b/src/pulsecore/source-output.c index 3c539515f..2e6df822a 100644 --- a/src/pulsecore/source-output.c +++ b/src/pulsecore/source-output.c @@ -1886,3 +1886,17 @@ void pa_source_output_set_reference_ratio(pa_source_output *o, const pa_cvolume pa_cvolume_snprint_verbose(old_ratio_str, sizeof(old_ratio_str), &old_ratio, &o->channel_map, true), pa_cvolume_snprint_verbose(new_ratio_str, sizeof(new_ratio_str), ratio, &o->channel_map, true)); } + +/* Called from the main thread. */ +void pa_source_output_set_preferred_source(pa_source_output *o, pa_source *s) { + pa_assert(o); + + pa_xfree(o->preferred_source); + if (s) { + o->preferred_source = pa_xstrdup(s->name); + pa_source_output_move_to(o, s, false); + } else { + o->preferred_source = NULL; + pa_source_output_move_to(o, o->core->default_source, false); + } +} diff --git a/src/pulsecore/source-output.h b/src/pulsecore/source-output.h index d46b37446..2bf56820b 100644 --- a/src/pulsecore/source-output.h +++ b/src/pulsecore/source-output.h @@ -402,6 +402,8 @@ void pa_source_output_set_volume_direct(pa_source_output *o, const pa_cvolume *v * o->reference_ratio and logs a message if the value changes. */ void pa_source_output_set_reference_ratio(pa_source_output *o, const pa_cvolume *ratio); +void pa_source_output_set_preferred_source(pa_source_output *o, pa_source *s); + #define pa_source_output_assert_io_context(s) \ pa_assert(pa_thread_mq_get() || !PA_SOURCE_OUTPUT_IS_LINKED((s)->state))