From beaec3d0032d7a986c6319a269cbb81cbbc66064 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Wed, 10 Feb 2021 21:30:15 +0200 Subject: [PATCH] bluez5: set initial profile based on what's connected at startup Set initial device profile according to what's connected at startup, rather than having media-session try to set it to A2DP (and fail, if the profile was not connected, resulting to startup in null profile). This avoids making a codec switch at device startup (we'll stay with what BlueZ autoconnected us to, usually the previously used codec). --- spa/plugins/bluez5/bluez5-device.c | 37 ++++++++++++++++++++++ src/examples/media-session/bluez-monitor.c | 1 - 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/spa/plugins/bluez5/bluez5-device.c b/spa/plugins/bluez5/bluez5-device.c index 6499bed42..030402826 100644 --- a/spa/plugins/bluez5/bluez5-device.c +++ b/spa/plugins/bluez5/bluez5-device.c @@ -502,6 +502,41 @@ static uint32_t get_index_from_profile(struct impl *this, uint32_t profile, cons return SPA_ID_INVALID; } +static void set_initial_profile(struct impl *this) +{ + struct spa_bt_transport *t; + int i; + + /* Prefer A2DP, then HFP, then null */ + + for (i = SPA_BT_PROFILE_A2DP_SINK; i <= SPA_BT_PROFILE_A2DP_SOURCE; i <<= 1) { + if (!(this->bt_dev->connected_profiles & i)) + continue; + + t = find_transport(this, i, NULL); + if (t) { + this->profile = 1; + this->selected_a2dp_codec = t->a2dp_codec; + return; + } + } + + for (i = SPA_BT_PROFILE_HSP_HS; i <= SPA_BT_PROFILE_HFP_AG; i <<= 1) { + if (!(this->bt_dev->connected_profiles & i)) + continue; + + t = find_transport(this, i, NULL); + if (t) { + this->profile = 2; + this->selected_a2dp_codec = NULL; + return; + } + } + + this->profile = 0; + this->selected_a2dp_codec = NULL; +} + static struct spa_pod *build_profile(struct impl *this, struct spa_pod_builder *b, uint32_t id, uint32_t index, uint32_t profile_index, const struct a2dp_codec *codec) { @@ -1133,6 +1168,8 @@ impl_init(const struct spa_handle_factory *factory, spa_bt_device_add_listener(this->bt_dev, &this->bt_dev_listener, &bt_dev_events, this); + set_initial_profile(this); + return 0; } diff --git a/src/examples/media-session/bluez-monitor.c b/src/examples/media-session/bluez-monitor.c index 28ed94b3d..e8ca56f98 100644 --- a/src/examples/media-session/bluez-monitor.c +++ b/src/examples/media-session/bluez-monitor.c @@ -377,7 +377,6 @@ static void device_update(void *data) &device->device_listener, &bluez5_device_events, device); - set_profile(device, 1); sm_object_sync_update(&device->sdevice->obj); }