From 2353a0991bcb72af6187120f1ba124d5f0948eee Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Wed, 12 Jun 2024 16:21:27 -0400 Subject: [PATCH] autoswitch-bluetooth-profile: Always destroy the restore timeout source before switching We always have to destroy the restore timeout source when we want to switch to HSP/HFP profile, even if the device is already set to HSP/HFP or has already an input route. Apart from this, we also want to make sure there is no pending timeout source when we are creating a new one. This should avoid an infinite loop about switching BT profiles when capture nodes are created and destroyed quickly. --- .../device/autoswitch-bluetooth-profile.lua | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/scripts/device/autoswitch-bluetooth-profile.lua b/src/scripts/device/autoswitch-bluetooth-profile.lua index f1aba4e3..9f63b4a5 100644 --- a/src/scripts/device/autoswitch-bluetooth-profile.lua +++ b/src/scripts/device/autoswitch-bluetooth-profile.lua @@ -212,9 +212,10 @@ local function switchDeviceToHeadsetProfile (dev_id) return end - if isSwitchedToHeadsetProfile (device) then - Log.info ("Device with id " .. tostring(dev_id).. " is already switched to HSP/HFP") - return + -- clear restore callback, if any + if restore_timeout_source[dev_id] ~= nil then + restore_timeout_source[dev_id]:destroy () + restore_timeout_source[dev_id] = nil end local cur_profile_name = getCurrentProfile (device) @@ -224,10 +225,9 @@ local function switchDeviceToHeadsetProfile (dev_id) return end - -- clear restore callback, if any - if restore_timeout_source[dev_id] ~= nil then - restore_timeout_source[dev_id]:destroy () - restore_timeout_source[dev_id] = nil + if isSwitchedToHeadsetProfile (device) then + Log.info ("Device with id " .. tostring(dev_id).. " is already switched to HSP/HFP") + return end local saved_headset_profile = getSavedHeadsetProfile (device) @@ -324,7 +324,13 @@ local function triggerRestoreProfile (dev_id) end end - restore_timeout_source[dev_id] = nil + -- clear restore callback, if any + if restore_timeout_source[dev_id] ~= nil then + restore_timeout_source[dev_id]:destroy () + restore_timeout_source[dev_id] = nil + end + + -- create new restore callback restore_timeout_source[dev_id] = Core.timeout_add (profile_restore_timeout_msec, function () restore_timeout_source[dev_id] = nil restoreProfile (dev_id) @@ -435,7 +441,6 @@ SimpleEventHook { local p = link.properties for stream in streams_om:iterate () do local in_id = tonumber(p["link.input.node"]) - local out_id = tonumber(p["link.output.node"]) local stream_id = tonumber(stream["bound-id"]) if in_id == stream_id then handleStream (stream)