[pulseaudio-discuss] [PATCH v2 5/5] bluetooth: Unload the device module when there are no audio profiles connected.

Tanu Kaskinen tanuk at iki.fi
Tue Nov 20 06:48:13 PST 2012


Without this patch, device modules will be left around after the
device has been disconnected and when they are reconnected, the
discovery module will load duplicate device module instances.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=57239
---
 src/modules/bluetooth/bluetooth-util.c          |    2 ++
 src/modules/bluetooth/bluetooth-util.h          |    1 +
 src/modules/bluetooth/module-bluetooth-device.c |   22 +++++++++++++++++++---
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 6b3085b..1f40e81 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -517,6 +517,8 @@ static void device_update_any_audio_profile_connected(pa_bluetooth_device *d) {
     else
         pa_log_debug("Device %s doesn't have any audio profiles connected anymore.", d->path);
 
+    pa_hook_fire(&d->hooks[PA_BLUETOOTH_DEVICE_HOOK_ANY_AUDIO_PROFILE_CONNECTED_CHANGED], NULL);
+
     run_callback(d, false);
 }
 
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
index c865b93..bcc7be3 100644
--- a/src/modules/bluetooth/bluetooth-util.h
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -95,6 +95,7 @@ typedef enum pa_bt_audio_state {
 typedef enum pa_bluetooth_device_hook {
     PA_BLUETOOTH_DEVICE_HOOK_REMOVED, /* Call data: NULL. */
     PA_BLUETOOTH_DEVICE_HOOK_UUID_ADDED, /* Call data: const char *uuid. */
+    PA_BLUETOOTH_DEVICE_HOOK_ANY_AUDIO_PROFILE_CONNECTED_CHANGED, /* Call data: NULL. */
     PA_BLUETOOTH_DEVICE_HOOK_MAX
 } pa_bluetooth_device_hook_t;
 
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
index 8a9d39f..4335911 100644
--- a/src/modules/bluetooth/module-bluetooth-device.c
+++ b/src/modules/bluetooth/module-bluetooth-device.c
@@ -146,6 +146,7 @@ struct userdata {
     char *accesstype;
     pa_hook_slot *transport_removed_slot;
     pa_hook_slot *device_removed_slot;
+    pa_hook_slot *device_any_audio_profile_connected_changed_slot;
 
     pa_bluetooth_discovery *discovery;
     pa_bool_t auto_connect;
@@ -2522,6 +2523,17 @@ static pa_hook_result_t device_removed_cb(pa_bluetooth_device *d, void *call_dat
     return PA_HOOK_OK;
 }
 
+/* Run from main thread */
+static pa_hook_result_t device_any_audio_profile_connected_changed_cb(pa_bluetooth_device *d, void *call_data, struct userdata *u) {
+    pa_assert(d);
+    pa_assert(u);
+
+    pa_log_debug("Unloading module, because device %s doesn't have any audio profiles connected anymore.", d->path);
+    pa_module_unload(u->core, u->module, true);
+
+    return PA_HOOK_OK;
+}
+
 int pa__init(pa_module* m) {
     pa_modargs *ma;
     uint32_t channels;
@@ -2594,6 +2606,9 @@ int pa__init(pa_module* m) {
 
     u->device_removed_slot = pa_hook_connect(&device->hooks[PA_BLUETOOTH_DEVICE_HOOK_REMOVED], PA_HOOK_NORMAL,
                                              (pa_hook_cb_t) device_removed_cb, u);
+    u->device_any_audio_profile_connected_changed_slot =
+            pa_hook_connect(&device->hooks[PA_BLUETOOTH_DEVICE_HOOK_ANY_AUDIO_PROFILE_CONNECTED_CHANGED],
+                            PA_HOOK_NORMAL, (pa_hook_cb_t) device_any_audio_profile_connected_changed_cb, u);
 
     u->device = device;
 
@@ -2684,10 +2699,11 @@ void pa__done(pa_module *m) {
 
     stop_thread(u);
 
-    if (u->device_removed_slot) {
+    if (u->device_any_audio_profile_connected_changed_slot)
+        pa_hook_slot_free(u->device_any_audio_profile_connected_changed_slot);
+
+    if (u->device_removed_slot)
         pa_hook_slot_free(u->device_removed_slot);
-        u->device_removed_slot = NULL;
-    }
 
     if (USE_SCO_OVER_PCM(u))
         restore_sco_volume_callbacks(u);
-- 
1.7.10.4



More information about the pulseaudio-discuss mailing list