[pulseaudio-discuss] [RFC v0 4/5] bluetooth: Use profile availability to auto-switch profiles

Mikel Astiz mikel.astiz.oss at gmail.com
Wed Feb 13 05:48:15 PST 2013


From: Mikel Astiz <mikel.astiz at bmw-carit.de>

Use the card profile availability flag instead of port availability in
order to automatically switch profiles, for example when a paired phone
starts streaming A2DP audio.
---
 src/modules/bluetooth/module-bluetooth-policy.c | 98 +++++++++++--------------
 1 file changed, 44 insertions(+), 54 deletions(-)

diff --git a/src/modules/bluetooth/module-bluetooth-policy.c b/src/modules/bluetooth/module-bluetooth-policy.c
index fc605b3..538cb7c 100644
--- a/src/modules/bluetooth/module-bluetooth-policy.c
+++ b/src/modules/bluetooth/module-bluetooth-policy.c
@@ -54,7 +54,7 @@ struct userdata {
     bool enable_hfgw;
     pa_hook_slot *source_put_slot;
     pa_hook_slot *sink_put_slot;
-    pa_hook_slot *port_available_changed_slot;
+    pa_hook_slot *profile_available_changed_slot;
 };
 
 /* When a source is created, loopback it to default sink */
@@ -133,54 +133,33 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void *
     return PA_HOOK_OK;
 }
 
-static pa_device_port* find_best_port(pa_hashmap *ports) {
+static pa_card_profile *find_best_profile(pa_hashmap *profiles) {
     void *state;
-    pa_device_port *port;
-    pa_device_port *result = NULL;
+    pa_card_profile *profile;
+    pa_card_profile *result = NULL;
 
-    PA_HASHMAP_FOREACH(port, ports, state) {
-        if (port->available != PA_PORT_AVAILABLE_YES)
+    PA_HASHMAP_FOREACH(profile, profiles, state) {
+        if (profile->available == PA_PROFILE_AVAILABLE_NO)
             continue;
 
-        if (result == NULL || port->priority > result->priority)
-            result = port;
+        if (result == NULL ||
+            (profile->available == PA_PROFILE_AVAILABLE_YES && result->available == PA_PROFILE_AVAILABLE_UNKNOWN) ||
+            (profile->available == result->available && profile->priority > profile->priority))
+            result = profile;
     }
 
     return result;
 }
 
-static void set_port_profile(pa_card *card, pa_device_port *port) {
-    void *state;
-    pa_card_profile *profile;
-
-    PA_HASHMAP_FOREACH(profile, port->profiles, state) {
-        if (card->active_profile == profile)
-            return;
-
-        pa_log_debug("Setting card '%s' to profile '%s'", card->name, profile->name);
-
-        if (pa_card_set_profile(card, profile->name, false) != 0)
-            pa_log_warn("Could not set profile '%s'", profile->name);
-
-        return;
-    }
-}
-
-static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port *port, void *userdata) {
+static pa_hook_result_t profile_available_hook_callback(pa_core *c, pa_card_profile *profile, void *userdata) {
     pa_card *card;
     const char *s;
-    uint32_t state;
     bool is_active_profile;
-    pa_device_port *port2;
-
-    PA_IDXSET_FOREACH(card, c->cards, state)
-        if (port == pa_hashmap_get(card->ports, port->name))
-            break;
+    pa_card_profile *selected_profile;
 
-    if (!card) {
-        pa_log_warn("Did not find port %s in array of cards", port->name);
-        return PA_HOOK_OK;
-    }
+    pa_assert(c);
+    pa_assert(profile);
+    pa_assert_se((card = profile->card));
 
     /* Only consider bluetooth cards */
     s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS);
@@ -188,35 +167,46 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
         return PA_HOOK_OK;
 
     /* Do not automatically switch profiles for headsets, just in case */
-    if (pa_hashmap_get(port->profiles, "hsp") || pa_hashmap_get(port->profiles, "a2dp"))
+    if (pa_streq(profile->name, "hsp") || pa_streq(profile->name, "a2dp"))
         return PA_HOOK_OK;
 
-    is_active_profile = card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name);
+    is_active_profile = card->active_profile == profile;
 
-    if (is_active_profile && port->available == PA_PORT_AVAILABLE_YES)
-        return PA_HOOK_OK;
+    if (profile->available == PA_PROFILE_AVAILABLE_YES) {
+        if (is_active_profile)
+            return PA_HOOK_OK;
 
-    if (!is_active_profile && port->available != PA_PORT_AVAILABLE_YES)
-        return PA_HOOK_OK;
+        if (card->active_profile && card->active_profile->available == PA_PROFILE_AVAILABLE_YES &&
+            card->active_profile->priority >= profile->priority)
+            return PA_HOOK_OK;
 
-    if ((port2 = find_best_port(card->ports)) == NULL)
-        return PA_HOOK_OK;
+        selected_profile = profile;
+    } else {
+        if (!is_active_profile)
+            return PA_HOOK_OK;
+
+        if (!(selected_profile = find_best_profile(card->profiles)))
+            return PA_HOOK_OK;
+    }
+
+    pa_log_debug("Setting card '%s' to profile '%s'", card->name, selected_profile->name);
 
-    set_port_profile(card, port2);
+    if (pa_card_set_profile(card, selected_profile->name, false) != 0)
+        pa_log_warn("Could not set profile '%s'", selected_profile->name);
 
     return PA_HOOK_OK;
 }
 
-static void handle_all_ports(pa_core *core) {
+static void handle_all_profiles(pa_core *core) {
     pa_card *card;
     uint32_t state;
 
     PA_IDXSET_FOREACH(card, core->cards, state) {
-        pa_device_port *port;
+        pa_card_profile *profile;
         void *state2;
 
-        PA_HASHMAP_FOREACH(port, card->ports, state2)
-            port_available_hook_callback(core, port, NULL);
+        PA_HASHMAP_FOREACH(profile, card->profiles, state2)
+            profile_available_hook_callback(core, profile, NULL);
     }
 }
 
@@ -249,10 +239,10 @@ int pa__init(pa_module *m) {
 
     u->sink_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_put_hook_callback, u);
 
-    u->port_available_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED],
-                                       PA_HOOK_NORMAL, (pa_hook_cb_t) port_available_hook_callback, u);
+    u->profile_available_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_PROFILE_AVAILABLE_CHANGED],
+                                                        PA_HOOK_NORMAL, (pa_hook_cb_t) profile_available_hook_callback, u);
 
-    handle_all_ports(m->core);
+    handle_all_profiles(m->core);
 
     pa_modargs_free(ma);
     return 0;
@@ -276,8 +266,8 @@ void pa__done(pa_module *m) {
     if (u->sink_put_slot)
         pa_hook_slot_free(u->sink_put_slot);
 
-    if (u->port_available_changed_slot)
-        pa_hook_slot_free(u->port_available_changed_slot);
+    if (u->profile_available_changed_slot)
+        pa_hook_slot_free(u->profile_available_changed_slot);
 
     pa_xfree(u);
 }
-- 
1.8.1



More information about the pulseaudio-discuss mailing list