| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2025 Collabora Ltd. |
| 4 |
-- @author Julian Bouzas <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
cutils = require ("common-utils") |
| 9 |
log = Log.open_topic ("s-automute-alsa-routes") |
| 10 |
hooks_registered = false |
| 11 |
|
| 12 |
function setRoute (device, route, mute) |
| 13 |
local param = Pod.Object { |
| 14 |
"Spa:Pod:Object:Param:Route", "Route", |
| 15 |
index = route.index, |
| 16 |
device = route.device, |
| 17 |
props = Pod.Object { |
| 18 |
"Spa:Pod:Object:Param:Props", "Route", |
| 19 |
mute = mute |
| 20 |
}, |
| 21 |
save = false, |
| 22 |
} |
| 23 |
|
| 24 |
log:info (device, "Setting mute to " .. tostring(mute) .. |
| 25 |
" on route " .. route.name) |
| 26 |
device:set_param("Route", param) |
| 27 |
end |
| 28 |
|
| 29 |
function findLowestPriorityAvailableOutputRoute (device) |
| 30 |
local lowest_prio_r = nil |
| 31 |
for p in device:iterate_params("Route") do |
| 32 |
local route = cutils.parseParam (p, "Route") |
| 33 |
if route and route.direction == "Output" and route.available ~= "no" then |
| 34 |
if lowest_prio_r == nil or lowest_prio_r.priority > route.priority then |
| 35 |
lowest_prio_r = route |
| 36 |
end |
| 37 |
end |
| 38 |
end |
| 39 |
return lowest_prio_r |
| 40 |
end |
| 41 |
|
| 42 |
function evaluateNode (node, source) |
| 43 |
if nodes_info [node.id] == nil then |
| 44 |
return |
| 45 |
end |
| 46 |
|
| 47 |
-- Get node info |
| 48 |
local node_state = nodes_info [node.id].state |
| 49 |
local node_api = nodes_info [node.id].api |
| 50 |
local node_dev_id = nodes_info [node.id].dev_id |
| 51 |
local node_cpd = nodes_info [node.id].cpd |
| 52 |
|
| 53 |
-- Don't do anything if node was not running |
| 54 |
if node_state ~= "running" then |
| 55 |
return |
| 56 |
end |
| 57 |
|
| 58 |
-- Emite event if setting is enabled for this API |
| 59 |
local mute_alsa = Settings.get_boolean ("device.routes.mute-on-alsa-playback-removed") |
| 60 |
local mute_bluez = Settings.get_boolean ("device.routes.mute-on-bluetooth-playback-removed") |
| 61 |
if (mute_alsa and node_api == "alsa") or |
| 62 |
(mute_bluez and node_api == "bluez5") then |
| 63 |
local e = source:call ("create-event", "mute-alsa-devices", nil, nil) |
| 64 |
e:set_data ("device-id", node_dev_id) |
| 65 |
e:set_data ("card-profile-device", node_cpd) |
| 66 |
EventDispatcher.push_event (e) |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
mute_alsa_devices_hook = SimpleEventHook { |
| 71 |
name = "device/mute-alsa-devices", |
| 72 |
interests = { |
| 73 |
EventInterest { |
| 74 |
Constraint { "event.type", "=", "mute-alsa-devices" }, |
| 75 |
}, |
| 76 |
}, |
| 77 |
execute = function (event) |
| 78 |
local source = event:get_source () |
| 79 |
local dev_id = tonumber (event:get_data ("device-id")) |
| 80 |
local cpd = tonumber (event:get_data ("card-profile-device")) |
| 81 |
local device_om = source:call ("get-object-manager", "device") |
| 82 |
local send_notification = false |
| 83 |
|
| 84 |
-- We mute all available output ALSA routes but the one associated with |
| 85 |
-- the running node. |
| 86 |
-- |
| 87 |
-- We also don't mute any routes if the running node is associated with |
| 88 |
-- the lowest priority route as this is most likely to be the Speakers. |
| 89 |
-- |
| 90 |
-- For instance, we want to mute all routes except the Headphones one |
| 91 |
-- when unplugging a headset while playing audio, but we don't |
| 92 |
-- want to mute the Headphones route when a headset is plugged in while |
| 93 |
-- playing audio on the Speakers. |
| 94 |
for device in device_om:iterate() do |
| 95 |
local dev_bound_id = device["bound-id"] |
| 96 |
if device.properties["device.api"] == "alsa" then |
| 97 |
local lpr = findLowestPriorityAvailableOutputRoute (device) |
| 98 |
if lpr == nil or lpr.device ~= cpd or dev_bound_id ~= dev_id then |
| 99 |
for p in device:iterate_params("Route") do |
| 100 |
local route = cutils.parseParam (p, "Route") |
| 101 |
if route and |
| 102 |
route.direction == "Output" and |
| 103 |
route.available ~= "no" and |
| 104 |
(route.device ~= cpd or dev_bound_id ~= dev_id) then |
| 105 |
setRoute (device, route, true) |
| 106 |
send_notification = true |
| 107 |
end |
| 108 |
end |
| 109 |
end |
| 110 |
end |
| 111 |
end |
| 112 |
|
| 113 |
-- Send notification if devices were muted |
| 114 |
notifications = notifications or Plugin.find("notifications-api") |
| 115 |
if notifications ~= nil and send_notification then |
| 116 |
notifications:call ("send", I18n.gettext("Audio was auto-muted"), |
| 117 |
I18n.gettext("Active playback device was disconnected")) |
| 118 |
end |
| 119 |
end |
| 120 |
} |
| 121 |
|
| 122 |
update_nodes_info_hook = SimpleEventHook { |
| 123 |
name = "device/update-nodes-info", |
| 124 |
interests = { |
| 125 |
EventInterest { |
| 126 |
Constraint { "event.type", "=", "node-state-changed" }, |
| 127 |
Constraint { "media.class", "matches", "Audio/Sink" }, |
| 128 |
Constraint { "device.api", "+" }, |
| 129 |
Constraint { "device.id", "+" }, |
| 130 |
Constraint { "card.profile.device", "+", type = "pw" }, |
| 131 |
}, |
| 132 |
}, |
| 133 |
execute = function (event) |
| 134 |
local node = event:get_subject () |
| 135 |
local node_id = node["bound-id"] |
| 136 |
local device_api = node.properties ["device.api"] |
| 137 |
local device_id = node.properties ["device.id"] |
| 138 |
local cpd = node.properties ["card.profile.device"] |
| 139 |
local new_state = event:get_properties ()["event.subject.new-state"] |
| 140 |
|
| 141 |
-- Update node info |
| 142 |
if nodes_info [node.id] == nil then |
| 143 |
nodes_info [node.id] = {} |
| 144 |
end |
| 145 |
nodes_info [node.id].api = device_api |
| 146 |
nodes_info [node.id].state = new_state |
| 147 |
nodes_info [node.id].dev_id = device_id |
| 148 |
nodes_info [node.id].cpd = cpd |
| 149 |
end |
| 150 |
} |
| 151 |
|
| 152 |
evaluate_mute_on_device_route_changed_hook = SimpleEventHook { |
| 153 |
name = "device/evaluate-mute-on-device-route-changed", |
| 154 |
interests = { |
| 155 |
EventInterest { |
| 156 |
Constraint { "event.type", "=", "device-params-changed" }, |
| 157 |
Constraint { "event.subject.param-id", "=", "EnumRoute" } |
| 158 |
}, |
| 159 |
}, |
| 160 |
execute = function (event) |
| 161 |
local source = event:get_source () |
| 162 |
local device = event:get_subject () |
| 163 |
local node_om = source:call ("get-object-manager", "node") |
| 164 |
|
| 165 |
-- Evaluate all nodes for this device when the EnumRoute param changed |
| 166 |
for node in node_om:iterate { |
| 167 |
Constraint { "media.class", "matches", "Audio/Sink", type = "pw-global" }, |
| 168 |
Constraint { "device.id", "=", device["bound-id"], type = "pw-global" }, |
| 169 |
} do |
| 170 |
evaluateNode (node, source) |
| 171 |
end |
| 172 |
end |
| 173 |
} |
| 174 |
|
| 175 |
evaluate_mute_on_node_removed_hook = SimpleEventHook { |
| 176 |
name = "device/evaluate-mute-on-node-removed", |
| 177 |
interests = { |
| 178 |
EventInterest { |
| 179 |
Constraint { "event.type", "=", "node-removed" }, |
| 180 |
Constraint { "media.class", "matches", "Audio/Sink" }, |
| 181 |
}, |
| 182 |
}, |
| 183 |
execute = function (event) |
| 184 |
local source = event:get_source () |
| 185 |
local node = event:get_subject () |
| 186 |
|
| 187 |
-- Evaluate removed node |
| 188 |
evaluateNode (node, source) |
| 189 |
|
| 190 |
-- Clear removed node info |
| 191 |
nodes_info [node.id] = nil |
| 192 |
end |
| 193 |
} |
| 194 |
|
| 195 |
function toggleState () |
| 196 |
local mute_alsa = Settings.get_boolean ("device.routes.mute-on-alsa-playback-removed") |
| 197 |
local mute_bluez = Settings.get_boolean ("device.routes.mute-on-bluetooth-playback-removed") |
| 198 |
if (mute_alsa or mute_bluez) and not hooks_registered then |
| 199 |
nodes_info = {} |
| 200 |
mute_alsa_devices_hook:register () |
| 201 |
update_nodes_info_hook:register () |
| 202 |
evaluate_mute_on_device_route_changed_hook:register () |
| 203 |
evaluate_mute_on_node_removed_hook:register () |
| 204 |
hooks_registered = true |
| 205 |
elseif not mute_alsa and not mute_bluez and hooks_registered then |
| 206 |
mute_alsa_devices_hook:remove () |
| 207 |
update_nodes_info_hook:remove () |
| 208 |
evaluate_mute_on_device_route_changed_hook:remove () |
| 209 |
evaluate_mute_on_node_removed_hook:remove () |
| 210 |
hooks_registered = false |
| 211 |
end |
| 212 |
end |
| 213 |
|
| 214 |
Settings.subscribe ("device.routes.mute-on-alsa-playback-removed", function () |
| 215 |
toggleState () |
| 216 |
end) |
| 217 |
Settings.subscribe ("device.routes.mute-on-bluetooth-playback-removed", function () |
| 218 |
toggleState () |
| 219 |
end) |
| 220 |
toggleState () |