mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-02 09:01:46 -05:00 
			
		
		
		
	bluetooth: Use profile availability to auto-switch profiles
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.
This commit is contained in:
		
							parent
							
								
									966a827d77
								
							
						
					
					
						commit
						d776416709
					
				
					 1 changed files with 49 additions and 55 deletions
				
			
		| 
						 | 
					@ -54,7 +54,7 @@ struct userdata {
 | 
				
			||||||
    bool enable_hfgw;
 | 
					    bool enable_hfgw;
 | 
				
			||||||
    pa_hook_slot *source_put_slot;
 | 
					    pa_hook_slot *source_put_slot;
 | 
				
			||||||
    pa_hook_slot *sink_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 */
 | 
					/* When a source is created, loopback it to default sink */
 | 
				
			||||||
| 
						 | 
					@ -133,54 +133,36 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void *
 | 
				
			||||||
    return PA_HOOK_OK;
 | 
					    return PA_HOOK_OK;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static pa_device_port* find_best_port(pa_hashmap *ports) {
 | 
					static pa_card_profile *find_best_profile(pa_card *card) {
 | 
				
			||||||
    void *state;
 | 
					 | 
				
			||||||
    pa_device_port *port;
 | 
					 | 
				
			||||||
    pa_device_port *result = NULL;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    PA_HASHMAP_FOREACH(port, ports, state) {
 | 
					 | 
				
			||||||
        if (port->available != PA_AVAILABLE_YES)
 | 
					 | 
				
			||||||
            continue;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (result == NULL || port->priority > result->priority)
 | 
					 | 
				
			||||||
            result = port;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return result;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void set_port_profile(pa_card *card, pa_device_port *port) {
 | 
					 | 
				
			||||||
    void *state;
 | 
					    void *state;
 | 
				
			||||||
    pa_card_profile *profile;
 | 
					    pa_card_profile *profile;
 | 
				
			||||||
 | 
					    pa_card_profile *result = card->active_profile;
 | 
				
			||||||
 | 
					    pa_card_profile *off;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PA_HASHMAP_FOREACH(profile, port->profiles, state) {
 | 
					    pa_assert_se(off = pa_hashmap_get(card->profiles, "off"));
 | 
				
			||||||
        if (card->active_profile == profile)
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        pa_log_debug("Setting card '%s' to profile '%s'", card->name, profile->name);
 | 
					    PA_HASHMAP_FOREACH(profile, card->profiles, state) {
 | 
				
			||||||
 | 
					        if (profile->available == PA_AVAILABLE_NO || profile == off)
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (pa_card_set_profile(card, profile->name, false) != 0)
 | 
					        if (result == NULL ||
 | 
				
			||||||
            pa_log_warn("Could not set profile '%s'", profile->name);
 | 
					            (profile->available == PA_AVAILABLE_YES && result->available == PA_AVAILABLE_UNKNOWN) ||
 | 
				
			||||||
 | 
					            (profile->available == result->available && profile->priority > profile->priority))
 | 
				
			||||||
        return;
 | 
					            result = profile;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return result ? result : off;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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;
 | 
					    pa_card *card;
 | 
				
			||||||
    const char *s;
 | 
					    const char *s;
 | 
				
			||||||
    uint32_t state;
 | 
					 | 
				
			||||||
    bool is_active_profile;
 | 
					    bool is_active_profile;
 | 
				
			||||||
    pa_device_port *port2;
 | 
					    pa_card_profile *selected_profile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PA_IDXSET_FOREACH(card, c->cards, state)
 | 
					    pa_assert(c);
 | 
				
			||||||
        if (port == pa_hashmap_get(card->ports, port->name))
 | 
					    pa_assert(profile);
 | 
				
			||||||
            break;
 | 
					    pa_assert_se((card = profile->card));
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!card) {
 | 
					 | 
				
			||||||
        pa_log_warn("Did not find port %s in array of cards", port->name);
 | 
					 | 
				
			||||||
        return PA_HOOK_OK;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Only consider bluetooth cards */
 | 
					    /* Only consider bluetooth cards */
 | 
				
			||||||
    s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS);
 | 
					    s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS);
 | 
				
			||||||
| 
						 | 
					@ -188,35 +170,47 @@ static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port
 | 
				
			||||||
        return PA_HOOK_OK;
 | 
					        return PA_HOOK_OK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Do not automatically switch profiles for headsets, just in case */
 | 
					    /* 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;
 | 
					        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_AVAILABLE_YES)
 | 
					    if (profile->available == PA_AVAILABLE_YES) {
 | 
				
			||||||
        return PA_HOOK_OK;
 | 
					        if (is_active_profile)
 | 
				
			||||||
 | 
					            return PA_HOOK_OK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!is_active_profile && port->available != PA_AVAILABLE_YES)
 | 
					        if (card->active_profile->available == PA_AVAILABLE_YES && card->active_profile->priority >= profile->priority)
 | 
				
			||||||
        return PA_HOOK_OK;
 | 
					            return PA_HOOK_OK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ((port2 = find_best_port(card->ports)) == NULL)
 | 
					        selected_profile = profile;
 | 
				
			||||||
        return PA_HOOK_OK;
 | 
					    } else {
 | 
				
			||||||
 | 
					        if (!is_active_profile)
 | 
				
			||||||
 | 
					            return PA_HOOK_OK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set_port_profile(card, port2);
 | 
					        pa_assert_se((selected_profile = find_best_profile(card)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (selected_profile == card->active_profile)
 | 
				
			||||||
 | 
					            return PA_HOOK_OK;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pa_log_debug("Setting card '%s' to profile '%s'", card->name, selected_profile->name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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;
 | 
					    return PA_HOOK_OK;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void handle_all_ports(pa_core *core) {
 | 
					static void handle_all_profiles(pa_core *core) {
 | 
				
			||||||
    pa_card *card;
 | 
					    pa_card *card;
 | 
				
			||||||
    uint32_t state;
 | 
					    uint32_t state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    PA_IDXSET_FOREACH(card, core->cards, state) {
 | 
					    PA_IDXSET_FOREACH(card, core->cards, state) {
 | 
				
			||||||
        pa_device_port *port;
 | 
					        pa_card_profile *profile;
 | 
				
			||||||
        void *state2;
 | 
					        void *state2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        PA_HASHMAP_FOREACH(port, card->ports, state2)
 | 
					        PA_HASHMAP_FOREACH(profile, card->profiles, state2)
 | 
				
			||||||
            port_available_hook_callback(core, port, NULL);
 | 
					            profile_available_hook_callback(core, profile, NULL);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -249,10 +243,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->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],
 | 
					    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) port_available_hook_callback, u);
 | 
					                                                        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);
 | 
					    pa_modargs_free(ma);
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
| 
						 | 
					@ -276,8 +270,8 @@ void pa__done(pa_module *m) {
 | 
				
			||||||
    if (u->sink_put_slot)
 | 
					    if (u->sink_put_slot)
 | 
				
			||||||
        pa_hook_slot_free(u->sink_put_slot);
 | 
					        pa_hook_slot_free(u->sink_put_slot);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (u->port_available_changed_slot)
 | 
					    if (u->profile_available_changed_slot)
 | 
				
			||||||
        pa_hook_slot_free(u->port_available_changed_slot);
 | 
					        pa_hook_slot_free(u->profile_available_changed_slot);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pa_xfree(u);
 | 
					    pa_xfree(u);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue