With this patch module-bluetooth-policy automatically switch from a2dp profile to hsp profile if some VOIP application with media.role=phone wants to start recording audio. By default a2dp profile is used for listening music, but for VOIP calls is needed profile with microphone support (hsp). So this patch will switch to hsp profile if some application want to use microphone (and specify it in media.role as "phone). After recording is stopped profile is switched back to a2dp. So this patch allows to use bluetooth microphone for VOIP applications with media.role=phone automatically without need of user interaction. Signed-off-by: Pali Rohár <pali.rohar at gmail.com> --- src/modules/bluetooth/module-bluetooth-policy.c | 203 ++++++++++++++++++++++- 1 file changed, 201 insertions(+), 2 deletions(-) diff --git a/src/modules/bluetooth/module-bluetooth-policy.c b/src/modules/bluetooth/module-bluetooth-policy.c index 860868f..c7cb7ef 100644 --- a/src/modules/bluetooth/module-bluetooth-policy.c +++ b/src/modules/bluetooth/module-bluetooth-policy.c @@ -35,16 +35,18 @@ #include "module-bluetooth-policy-symdef.h" -PA_MODULE_AUTHOR("Frédéric Dalleau"); -PA_MODULE_DESCRIPTION("When a bluetooth sink or source is added, load module-loopback"); +PA_MODULE_AUTHOR("Frédéric Dalleau, Pali Rohár"); +PA_MODULE_DESCRIPTION("Policy module to make using bluetooth devices out-of-the-box easier"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(true); PA_MODULE_USAGE( + "auto_switch=<Switch between hsp and a2dp profile?> " "a2dp_source=<Handle a2dp_source card profile (sink role)?> " "ag=<Handle headset_audio_gateway card profile (headset role)?> " "hfgw=<Handle hfgw card profile (headset role)?> DEPRECATED"); static const char* const valid_modargs[] = { + "auto_switch", "a2dp_source", "ag", "hfgw", @@ -56,7 +58,12 @@ struct userdata { bool enable_ag; pa_hook_slot *source_put_slot; pa_hook_slot *sink_put_slot; + pa_hook_slot *source_output_put_slot; + pa_hook_slot *source_output_unlink_slot; + pa_hook_slot *card_new_slot; + pa_hook_slot *card_unlink_slot; pa_hook_slot *profile_available_changed_slot; + pa_hashmap *will_need_revert_card_map; }; /* When a source is created, loopback it to default sink */ @@ -139,6 +146,161 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, void * return PA_HOOK_OK; } +/* Switch profile for one card */ +static void switch_profile(pa_card *card, bool revert_to_a2dp, void *userdata) { + struct userdata *u = userdata; + const char *from_profile; + const char *to_profile; + pa_card_profile *profile; + const char *s; + void *state; + + if (revert_to_a2dp) { + from_profile = "hsp"; + to_profile = "a2dp"; + } else { + from_profile = "a2dp"; + to_profile = "hsp"; + } + + /* Only consider bluetooth cards */ + s = pa_proplist_gets(card->proplist, PA_PROP_DEVICE_BUS); + if (!s || !pa_streq(s, "bluetooth")) + return; + + if (revert_to_a2dp) { + /* In revert_to_a2dp phase only consider cards with will_need_revert flag and remove it */ + if (!pa_hashmap_remove(u->will_need_revert_card_map, card)) + return; + } + + /* Skip card if does not have active from_profile */ + if (!pa_streq(card->active_profile->name, from_profile)) + return; + + /* Skip card if already has active profile to_profile */ + if (pa_streq(card->active_profile->name, to_profile)) + return; + + /* Find available to_profile and activate it */ + PA_HASHMAP_FOREACH(profile, card->profiles, state) { + if (!pa_streq(profile->name, to_profile)) + continue; + + if (profile->available == PA_AVAILABLE_NO) + continue; + + pa_log_debug("Setting card '%s' to profile '%s'", card->name, to_profile); + + if (pa_card_set_profile(card, profile, false) != 0) { + pa_log_warn("Could not set profile '%s'", to_profile); + continue; + } + + /* When we are not in revert_to_a2dp phase flag this card for will_need_revert */ + if (!revert_to_a2dp) + pa_hashmap_put(u->will_need_revert_card_map, card, (void *)1); + + break; + } +} + +/* Return true if we should ignore this source output */ +static bool ignore_output(pa_source_output *source_output) { + const char *s; + + /* New applications could set media.role for identifying streams */ + /* We are interested only in media.role=phone */ + s = pa_proplist_gets(source_output->proplist, PA_PROP_MEDIA_ROLE); + if (s) + return !pa_streq(s, "phone"); + + return true; +} + +static unsigned source_output_count(pa_core *c) { + pa_source_output *source_output; + uint32_t idx; + unsigned count = 0; + + PA_IDXSET_FOREACH(source_output, c->source_outputs, idx) + if (!ignore_output(source_output)) + ++count; + + return count; +} + +/* Switch profile for all cards */ +static void switch_profile_all(pa_idxset *cards, bool revert_to_a2dp, void *userdata) { + pa_card *card; + uint32_t idx; + + PA_IDXSET_FOREACH(card, cards, idx) + switch_profile(card, revert_to_a2dp, userdata); +} + +/* When a source output is created, switch profile a2dp to profile hsp */ +static pa_hook_result_t source_output_put_hook_callback(pa_core *c, pa_source_output *source_output, void *userdata) { + pa_assert(c); + pa_assert(source_output); + + if (ignore_output(source_output)) + return PA_HOOK_OK; + + switch_profile_all(c->cards, false, userdata); + return PA_HOOK_OK; +} + +/* When all source outputs are unlinked, switch profile hsp back back to profile a2dp */ +static pa_hook_result_t source_output_unlink_hook_callback(pa_core *c, pa_source_output *source_output, void *userdata) { + pa_assert(c); + pa_assert(source_output); + + if (ignore_output(source_output)) + return PA_HOOK_OK; + + /* If there are still some source outputs do nothing (count is with *this* source_output, so +1) */ + if (source_output_count(c) > 1) + return PA_HOOK_OK; + + switch_profile_all(c->cards, true, userdata); + return PA_HOOK_OK; +} + +static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new_data, void *userdata) { + struct userdata *u = userdata; + const char *s; + + pa_assert(c); + pa_assert(new_data); + + if (source_output_count(c) == 0) + return PA_HOOK_OK; + + /* Only consider bluetooth cards */ + s = pa_proplist_gets(new_data->proplist, PA_PROP_DEVICE_BUS); + if (!s || !pa_streq(s, "bluetooth")) + return PA_HOOK_OK; + + /* Ignore card if has already set other initial profile than a2dp */ + if (new_data->active_profile && !pa_streq(new_data->active_profile, "a2dp")) + return PA_HOOK_OK; + + /* Set initial profile to hsp */ + pa_card_new_data_set_profile(new_data, "hsp"); + + /* Flag this card for will_need_revert */ + pa_hashmap_put(u->will_need_revert_card_map, new_data, (void *)1); + return PA_HOOK_OK; +} + +static pa_hook_result_t card_unlink_hook_callback(pa_core *c, pa_card *card, void *userdata) { + pa_assert(c); + pa_assert(card); + switch_profile(card, true, userdata); + return PA_HOOK_OK; +} + static pa_card_profile *find_best_profile(pa_card *card) { void *state; pa_card_profile *profile; @@ -222,6 +384,7 @@ static void handle_all_profiles(pa_core *core) { int pa__init(pa_module *m) { pa_modargs *ma; struct userdata *u; + bool auto_switch; pa_assert(m); @@ -232,6 +395,12 @@ int pa__init(pa_module *m) { m->userdata = u = pa_xnew0(struct userdata, 1); + auto_switch = true; + if (pa_modargs_get_value_boolean(ma, "auto_switch", &auto_switch) < 0) { + pa_log("Failed to parse auto_switch argument."); + goto fail; + } + u->enable_a2dp_source = true; if (pa_modargs_get_value_boolean(ma, "a2dp_source", &u->enable_a2dp_source) < 0) { pa_log("Failed to parse a2dp_source argument."); @@ -248,12 +417,28 @@ int pa__init(pa_module *m) { goto fail; } + u->will_need_revert_card_map = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + u->source_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) source_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); + if (auto_switch) { + u->source_output_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_NORMAL, + (pa_hook_cb_t) source_output_put_hook_callback, u); + + u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_NORMAL, + (pa_hook_cb_t) source_output_unlink_hook_callback, u); + + u->card_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_NORMAL, + (pa_hook_cb_t) card_new_hook_callback, u); + + u->card_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_UNLINK], PA_HOOK_NORMAL, + (pa_hook_cb_t) card_unlink_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); @@ -281,8 +466,22 @@ void pa__done(pa_module *m) { if (u->sink_put_slot) pa_hook_slot_free(u->sink_put_slot); + if (u->source_output_put_slot) + pa_hook_slot_free(u->source_output_put_slot); + + if (u->source_output_unlink_slot) + pa_hook_slot_free(u->source_output_unlink_slot); + + if (u->card_new_slot) + pa_hook_slot_free(u->card_new_slot); + + if (u->card_unlink_slot) + pa_hook_slot_free(u->card_unlink_slot); + if (u->profile_available_changed_slot) pa_hook_slot_free(u->profile_available_changed_slot); + pa_hashmap_free(u->will_need_revert_card_map); + pa_xfree(u); } -- 1.7.9.5