Blame SOURCES/0001-alsa-card-add-dynamic-priority-bonus-base-for-alsa-p.patch

011702
From 832d7ac1144416306de1b6990d70115079bd1935 Mon Sep 17 00:00:00 2001
011702
From: Hui Wang <hui.wang@canonical.com>
011702
Date: Fri, 21 Aug 2020 17:34:25 +0800
011702
Subject: [PATCH] alsa-card: add dynamic priority bonus base for alsa profiles
011702
011702
After applying the commit 0d50e787 ("alsa-card: improve the profile
011702
availability logic"), we met an new issue. when system selects the
011702
initial profile, the profile off is selected instead of a profile with
011702
a valid output device on it. That is the issue we met:
011702
011702
Profiles:
011702
  HiFi: Default (sinks: 2, sources: 2, priority: 8000, available: no)
011702
  off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
011702
Active Profile: off
011702
Ports:
011702
  [Out] Headphones: Headphones (priority: 300, latency offset: 0 usec, not available)
011702
   Part of profile(s): HiFi
011702
  [Out] Speaker: Speaker (priority: 100, latency offset: 0 usec)
011702
   Part of profile(s): HiFi
011702
...
011702
011702
I know the commit 0d50e787 really fixed something, but we still need
011702
to fix the new issue, to do so, this patch introduces a priority bonus
011702
for alsa profiles and separate the alsa profiles to 3 groups:
011702
group a (will be granted priority bonus dynamically):
011702
a profile has only output ports and at least one port is not unavailable
011702
a profile has only input ports and at least one port is not unavailable
011702
a profile has both input and output ports, and at least one output and
011702
one input ports are not unavailable
011702
011702
group b (will be marked unavailable)
011702
a profile has only output ports and all ports are unavailable
011702
a profile has only input ports and all ports are unavailable
011702
a profile has both output and input ports, and all ports are unavailable
011702
011702
group c
011702
the rest profiles, their priority and availability is not changed.
011702
011702
With this change, the profile HiFi will become avaialbe:yes, and will
011702
not be granted priority bonus if no input port is plugged.
011702
011702
The priority bonus provides a higher priority base to profiles, this
011702
guarantees this patch doesn't break the fix of 0d50e787.
011702
011702
https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/issues/927
011702
011702
Signed-off-by: Hui Wang <hui.wang@canonical.com>
011702
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/355>
011702
---
011702
 src/modules/alsa/module-alsa-card.c | 35 ++++++++++++++++++++++++-----
011702
 1 file changed, 30 insertions(+), 5 deletions(-)
011702
011702
diff --git a/src/modules/alsa/module-alsa-card.c b/src/modules/alsa/module-alsa-card.c
011702
index de2fe9cc4..5349314b5 100644
011702
--- a/src/modules/alsa/module-alsa-card.c
011702
+++ b/src/modules/alsa/module-alsa-card.c
011702
@@ -104,6 +104,13 @@ static const char* const valid_modargs[] = {
011702
 
011702
 #define DEFAULT_DEVICE_ID "0"
011702
 
011702
+/* dynamic profile priority bonus, for all alsa profiles, the original priority
011702
+   needs to be less than 0x7fff (32767), then could apply the rule of priority
011702
+   bonus. So far there are 2 kinds of alsa profiles, one is from alsa ucm, the
011702
+   other is from mixer profile-sets, their priorities are all far less than 0x7fff
011702
+*/
011702
+#define PROFILE_PRIO_BONUS 0x8000
011702
+
011702
 struct userdata {
011702
     pa_core *core;
011702
     pa_module *module;
011702
@@ -459,9 +466,19 @@ static int report_jack_state(snd_mixer_elem_t *melem, unsigned int mask) {
011702
      * as available (well, "unknown" to be precise, but there's little
011702
      * practical difference).
011702
      *
011702
-     * When all output ports are unavailable, we know that all sinks are
011702
-     * unavailable, and therefore the profile is marked unavailable as well.
011702
-     * The same applies to input ports as well, of course.
011702
+     * A profile will be marked unavailable:
011702
+     * only contains output ports and all ports are unavailable
011702
+     * only contains input ports and all ports are unavailable
011702
+     * contains both input and output ports and all ports are unavailable
011702
+     *
011702
+     * A profile will be awarded priority bonus:
011702
+     * only contains output ports and at least one port is available
011702
+     * only contains input ports and at least one port is available
011702
+     * contains both output and input ports and at least one output port
011702
+     * and one input port are available
011702
+     *
011702
+     * The rest profiles will not be marked unavailable and will not be
011702
+     * awarded priority bonus
011702
      *
011702
      * If there are no output ports at all, but the profile contains at least
011702
      * one sink, then the output is considered to be available. */
011702
@@ -476,6 +493,7 @@ static int report_jack_state(snd_mixer_elem_t *melem, unsigned int mask) {
011702
         bool found_available_output_port = false;
011702
         pa_available_t available = PA_AVAILABLE_UNKNOWN;
011702
 
011702
+        profile->priority &= ~PROFILE_PRIO_BONUS;
011702
         PA_HASHMAP_FOREACH(port, u->card->ports, state2) {
011702
             if (!pa_hashmap_get(port->profiles, profile->name))
011702
                 continue;
011702
@@ -493,8 +511,15 @@ static int report_jack_state(snd_mixer_elem_t *melem, unsigned int mask) {
011702
             }
011702
         }
011702
 
011702
-        if ((has_input_port && !found_available_input_port) || (has_output_port && !found_available_output_port))
011702
-            available = PA_AVAILABLE_NO;
011702
+        if ((has_input_port && found_available_input_port && !has_output_port) ||
011702
+            (has_output_port && found_available_output_port && !has_input_port) ||
011702
+            (has_input_port && found_available_input_port && has_output_port && found_available_output_port))
011702
+                profile->priority |= PROFILE_PRIO_BONUS;
011702
+
011702
+        if ((has_input_port && !found_available_input_port && has_output_port && !found_available_output_port) ||
011702
+            (has_input_port && !found_available_input_port && !has_output_port) ||
011702
+            (has_output_port && !found_available_output_port && !has_input_port))
011702
+                available = PA_AVAILABLE_NO;
011702
 
011702
         /* We want to update the active profile's status last, so logic that
011702
          * may change the active profile based on profile availability status
011702
-- 
011702
2.36.1
011702