bluez5: create device handle before profile negotiation started so that profile handler can retrieve per-device settings

This commit is contained in:
Huang-Huang Bao 2021-03-14 14:06:50 +08:00 committed by Wim Taymans
parent e7cecaaea6
commit af8272fe08
5 changed files with 140 additions and 80 deletions

View file

@ -344,18 +344,6 @@ static int update_device_props(struct device *device)
return 0;
}
static void bluez5_update_device(struct impl *impl, struct device *dev,
const struct spa_device_object_info *info)
{
pw_log_debug("update device %u", dev->id);
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
pw_properties_update(dev->props, info->props);
update_device_props(dev);
}
static void device_destroy(void *data)
{
struct device *device = data;
@ -363,27 +351,15 @@ static void device_destroy(void *data)
pw_log_debug("device %p destroy", device);
if (device->appeared) {
device->appeared = false;
spa_hook_remove(&device->device_listener);
}
spa_list_consume(node, &device->node_list, link)
bluez5_remove_node(device, node);
}
static void device_free(void *data)
{
struct device *device = data;
pw_log_debug("remove device %u", device->id);
spa_list_remove(&device->link);
spa_hook_remove(&device->listener);
if (device->appeared)
spa_hook_remove(&device->device_listener);
sm_object_discard(&device->sdevice->obj);
pw_unload_spa_handle(device->handle);
pw_properties_free(device->props);
free(device);
}
static void device_update(void *data)
{
struct device *device = data;
@ -406,7 +382,6 @@ static void device_update(void *data)
static const struct sm_object_events device_events = {
SM_VERSION_OBJECT_EVENTS,
.destroy = device_destroy,
.free = device_free,
.update = device_update,
};
@ -460,16 +435,6 @@ static struct device *bluez5_create_device(struct impl *impl, uint32_t id,
device->handle = handle;
device->device = iface;
device->sdevice = sm_media_session_export_device(impl->session,
&device->props->dict, device->device);
if (device->sdevice == NULL) {
res = -errno;
goto unload_handle;
}
sm_object_add_listener(&device->sdevice->obj,
&device->listener,
&device_events, device);
spa_list_append(&impl->device_list, &device->link);
@ -487,7 +452,53 @@ exit:
static void bluez5_remove_device(struct impl *impl, struct device *device)
{
sm_object_destroy(&device->sdevice->obj);
pw_log_debug("remove device %u", device->id);
if (device->sdevice) {
sm_object_destroy(&device->sdevice->obj);
device->sdevice = NULL;
}
}
static void bluez5_update_device(struct impl *impl, struct device *device,
const struct spa_device_object_info *info)
{
bool connected;
const char *str;
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
pw_log_debug("update device %u", device->id);
pw_properties_update(device->props, info->props);
update_device_props(device);
str = spa_dict_lookup(info->props, SPA_KEY_API_BLUEZ5_CONNECTION);
connected = str != NULL && strcmp(str, "connected") == 0;
/* Export device after bluez profiles get connected */
if (device->sdevice == NULL && connected) {
device->sdevice = sm_media_session_export_device(impl->session,
&device->props->dict, device->device);
if (device->sdevice == NULL) {
bluez5_remove_device(impl, device);
return;
}
sm_object_add_listener(&device->sdevice->obj,
&device->listener,
&device_events, device);
}
}
static void bluez5_device_free(struct impl *impl, struct device *device)
{
bluez5_remove_device(impl, device);
spa_list_remove(&device->link);
pw_unload_spa_handle(device->handle);
pw_properties_free(device->props);
free(device);
}
static void bluez5_enum_object_info(void *data, uint32_t id,
@ -519,6 +530,10 @@ static const struct spa_device_events bluez5_enum_callbacks =
static void session_destroy(void *data)
{
struct impl *impl = data;
struct device *device;
spa_list_consume(device, &impl->device_list, link)
bluez5_device_free(impl, device);
spa_hook_remove(&impl->session_listener);
spa_hook_remove(&impl->listener);
pw_unload_spa_handle(impl->handle);