| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2020-2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
-- |
| 7 |
-- Handle new linkables and trigger rescanning of the graph. |
| 8 |
-- Rescan the graph by pushing new select-target events for |
| 9 |
-- all linkables that need to be linked |
| 10 |
-- Cleanup links when the linkables they are associated with are removed. |
| 11 |
-- Also, cleanup flags attached to linkables. |
| 12 |
|
| 13 |
lutils = require ("linking-utils") |
| 14 |
cutils = require ("common-utils") |
| 15 |
futils = require ("filter-utils") |
| 16 |
log = Log.open_topic ("s-linking") |
| 17 |
handles = {} |
| 18 |
handles.rescan_enabled = true |
| 19 |
handles.timeout_source = nil |
| 20 |
|
| 21 |
function checkFilter (si, om, handle_nonstreams) |
| 22 |
-- always handle filters if handle_nonstreams is true, even if it is disabled |
| 23 |
if handle_nonstreams then |
| 24 |
return true |
| 25 |
end |
| 26 |
|
| 27 |
-- always return true if this is not a filter |
| 28 |
local node = si:get_associated_proxy ("node") |
| 29 |
local link_group = node:get_property ("node.link-group") |
| 30 |
if link_group == nil then |
| 31 |
return true |
| 32 |
end |
| 33 |
|
| 34 |
local direction = cutils.getTargetDirection (si.properties) |
| 35 |
|
| 36 |
-- always handle filters that are not smart |
| 37 |
if not futils.is_filter_smart (direction, link_group) then |
| 38 |
return true |
| 39 |
end |
| 40 |
|
| 41 |
-- dont handle smart filters that are disabled |
| 42 |
return not futils.is_filter_disabled (direction, link_group) |
| 43 |
end |
| 44 |
|
| 45 |
function checkLinkable (si, om, handle_nonstreams) |
| 46 |
-- For the rest of them, only handle stream session items |
| 47 |
if si:get_property ("item.node.type") ~= "stream" and |
| 48 |
not handle_nonstreams then |
| 49 |
return false |
| 50 |
end |
| 51 |
|
| 52 |
-- check filters |
| 53 |
if not checkFilter (si, om, handle_nonstreams) then |
| 54 |
return false |
| 55 |
end |
| 56 |
|
| 57 |
return true |
| 58 |
end |
| 59 |
|
| 60 |
function unhandleLinkable (si, om) |
| 61 |
if not checkLinkable (si, om, true) then |
| 62 |
return |
| 63 |
end |
| 64 |
|
| 65 |
local si_id = si.id |
| 66 |
log:info (si, string.format ("unhandling item %d", si_id)) |
| 67 |
|
| 68 |
-- iterate over all the links in the graph and |
| 69 |
-- remove any links associated with this item |
| 70 |
for silink in om:iterate { type = "SiLink" } do |
| 71 |
local silink_props = silink.properties |
| 72 |
local out_id = silink_props:get_int ("out.item.id") |
| 73 |
local in_id = silink_props:get_int ("in.item.id") |
| 74 |
|
| 75 |
if out_id == si_id or in_id == si_id then |
| 76 |
local in_flags = lutils:get_flags (in_id) |
| 77 |
local out_flags = lutils:get_flags (out_id) |
| 78 |
|
| 79 |
if out_id == si_id and in_flags.peer_id == out_id then |
| 80 |
in_flags.peer_id = nil |
| 81 |
elseif in_id == si_id and out_flags.peer_id == in_id then |
| 82 |
out_flags.peer_id = nil |
| 83 |
end |
| 84 |
|
| 85 |
if silink_props:get_boolean ("is.role.policy.link") then |
| 86 |
lutils.clearPriorityMediaRoleLink(silink) |
| 87 |
end |
| 88 |
|
| 89 |
silink:remove () |
| 90 |
log:info (silink, "... link removed") |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
lutils:clear_flags (si_id) |
| 95 |
end |
| 96 |
|
| 97 |
SimpleEventHook { |
| 98 |
name = "linking/linkable-removed", |
| 99 |
interests = { |
| 100 |
EventInterest { |
| 101 |
Constraint { "event.type", "=", "session-item-removed" }, |
| 102 |
Constraint { "event.session-item.interface", "=", "linkable" }, |
| 103 |
}, |
| 104 |
}, |
| 105 |
execute = function (event) |
| 106 |
local si = event:get_subject () |
| 107 |
local source = event:get_source () |
| 108 |
local om = source:call ("get-object-manager", "session-item") |
| 109 |
|
| 110 |
unhandleLinkable (si, om) |
| 111 |
end |
| 112 |
}:register () |
| 113 |
|
| 114 |
-- Handle newly added linkable immediately without waiting for full rescan |
| 115 |
-- Only for simple cases where we know it won't affect other parts of the graph |
| 116 |
SimpleEventHook { |
| 117 |
name = "linking/linkable-added-immediate", |
| 118 |
before = "linking/rescan-trigger", |
| 119 |
interests = { |
| 120 |
EventInterest { |
| 121 |
Constraint { "event.type", "=", "session-item-added" }, |
| 122 |
Constraint { "event.session-item.interface", "=", "linkable" }, |
| 123 |
}, |
| 124 |
}, |
| 125 |
execute = function (event) |
| 126 |
local si = event:get_subject () |
| 127 |
local source = event:get_source () |
| 128 |
local om = source:call ("get-object-manager", "session-item") |
| 129 |
|
| 130 |
if not checkLinkable (si, om, false) then |
| 131 |
return |
| 132 |
end |
| 133 |
|
| 134 |
-- Don't handle immediately if this is a smart filter that could affect other nodes |
| 135 |
local node = si:get_associated_proxy ("node") |
| 136 |
local link_group = node:get_property ("node.link-group") |
| 137 |
if link_group then |
| 138 |
local direction = cutils.getTargetDirection (si.properties) |
| 139 |
if futils.is_filter_smart (direction, link_group) then |
| 140 |
-- Smart filters need full rescan to handle cascading effects |
| 141 |
return |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
-- Only handle if autoconnect is enabled |
| 146 |
local autoconnect = si:get_property ("node.autoconnect") |
| 147 |
if autoconnect ~= "true" then |
| 148 |
return |
| 149 |
end |
| 150 |
|
| 151 |
-- Check if this is a simple stream (most common case) |
| 152 |
-- Don't handle device nodes or special nodes that might become default targets |
| 153 |
if si:get_property ("item.node.type") ~= "stream" then |
| 154 |
return |
| 155 |
end |
| 156 |
|
| 157 |
-- Push select-target event immediately for simple stream case |
| 158 |
source:call ("push-event", "select-target", si, nil) |
| 159 |
end |
| 160 |
}:register () |
| 161 |
|
| 162 |
function handleLinkables (source) |
| 163 |
local om = source:call ("get-object-manager", "session-item") |
| 164 |
|
| 165 |
for si in om:iterate { type = "SiLinkable" } do |
| 166 |
if not checkLinkable (si, om) then |
| 167 |
goto skip_linkable |
| 168 |
end |
| 169 |
|
| 170 |
-- Get properties |
| 171 |
local si_props = si.properties |
| 172 |
|
| 173 |
-- check if we need to link this node at all |
| 174 |
local autoconnect = si_props:get_boolean ("node.autoconnect") |
| 175 |
if not autoconnect then |
| 176 |
log:debug (si, tostring (si_props ["node.name"]) .. " does not need to be autoconnected") |
| 177 |
goto skip_linkable |
| 178 |
end |
| 179 |
|
| 180 |
-- push event to find target and link |
| 181 |
source:call ("push-event", "select-target", si, nil) |
| 182 |
|
| 183 |
::skip_linkable:: |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
SimpleEventHook { |
| 188 |
name = "linking/rescan", |
| 189 |
interests = { |
| 190 |
EventInterest { |
| 191 |
Constraint { "event.type", "=", "rescan-for-linking" }, |
| 192 |
}, |
| 193 |
}, |
| 194 |
execute = function (event) |
| 195 |
local source = event:get_source () |
| 196 |
local om = source:call ("get-object-manager", "session-item") |
| 197 |
|
| 198 |
log:info ("rescanning...") |
| 199 |
|
| 200 |
-- always unlink all filters that are smart and disabled |
| 201 |
for si in om:iterate { |
| 202 |
type = "SiLinkable", |
| 203 |
Constraint { "node.link-group", "+" }, |
| 204 |
} do |
| 205 |
local node = si:get_associated_proxy ("node") |
| 206 |
local link_group = node:get_property ("node.link-group") |
| 207 |
local direction = cutils.getTargetDirection (si.properties) |
| 208 |
if futils.is_filter_smart (direction, link_group) and |
| 209 |
futils.is_filter_disabled (direction, link_group) then |
| 210 |
unhandleLinkable (si, om) |
| 211 |
end |
| 212 |
end |
| 213 |
|
| 214 |
handleLinkables (source) |
| 215 |
end |
| 216 |
}:register () |
| 217 |
|
| 218 |
SimpleEventHook { |
| 219 |
name = "linking/rescan-trigger", |
| 220 |
interests = { |
| 221 |
-- on device Routes changed |
| 222 |
EventInterest { |
| 223 |
Constraint { "event.type", "=", "device-params-changed" }, |
| 224 |
Constraint { "event.subject.param-id", "c", "Route", "EnumRoute" }, |
| 225 |
}, |
| 226 |
-- on any "default" target changed |
| 227 |
EventInterest { |
| 228 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 229 |
Constraint { "metadata.name", "=", "default" }, |
| 230 |
Constraint { "event.subject.key", "c", "default.audio.source", |
| 231 |
"default.audio.sink", "default.video.source" }, |
| 232 |
}, |
| 233 |
-- on any "filters" metadata changed |
| 234 |
EventInterest { |
| 235 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 236 |
Constraint { "metadata.name", "=", "filters" }, |
| 237 |
}, |
| 238 |
}, |
| 239 |
execute = function (event) |
| 240 |
if handles.rescan_enabled then |
| 241 |
local source = event:get_source () |
| 242 |
source:call ("schedule-rescan", "linking") |
| 243 |
end |
| 244 |
end |
| 245 |
}:register () |
| 246 |
|
| 247 |
SimpleEventHook { |
| 248 |
name = "linking/session-item-added", |
| 249 |
before = "linking/rescan-trigger", |
| 250 |
interests = { |
| 251 |
EventInterest { |
| 252 |
Constraint { "event.type", "=", "session-item-added" }, |
| 253 |
}, |
| 254 |
}, |
| 255 |
execute = function (event) |
| 256 |
-- clear timeout source, if any |
| 257 |
if handles.timeout_source ~= nil then |
| 258 |
handles.timeout_source:destroy () |
| 259 |
handles.timeout_source = nil |
| 260 |
end |
| 261 |
|
| 262 |
-- Always enable rescan when any node is added |
| 263 |
handles.rescan_enabled = true |
| 264 |
end |
| 265 |
}:register () |
| 266 |
|
| 267 |
-- Stop rescan for 2 seconds if BT item was removed. This avoids audio |
| 268 |
-- being played on internal nodes for a few seconds while the BT device is |
| 269 |
-- switching profiles. |
| 270 |
SimpleEventHook { |
| 271 |
name = "linking/bluez-session-item-removed", |
| 272 |
before = "linking/rescan-trigger", |
| 273 |
interests = { |
| 274 |
EventInterest { |
| 275 |
Constraint { "event.type", "=", "session-item-removed" }, |
| 276 |
Constraint { "device.api", "=", "bluez5" }, |
| 277 |
}, |
| 278 |
}, |
| 279 |
execute = function (event) |
| 280 |
local si = event:get_subject () |
| 281 |
local source = event:get_source () |
| 282 |
|
| 283 |
-- clear timeout source, if any |
| 284 |
if handles.timeout_source ~= nil then |
| 285 |
handles.timeout_source:destroy () |
| 286 |
handles.timeout_source = nil |
| 287 |
end |
| 288 |
|
| 289 |
-- disable rescan |
| 290 |
handles.rescan_enabled = false |
| 291 |
|
| 292 |
-- re-enable rescan after 2 seconds |
| 293 |
handles.timeout_source = Core.timeout_add (2000, function() |
| 294 |
handles.timeout_source = nil |
| 295 |
handles.rescan_enabled = true |
| 296 |
source:call ("schedule-rescan", "linking") |
| 297 |
end) |
| 298 |
end |
| 299 |
}:register () |
| 300 |
|
| 301 |
function handleMoveSetting (enable) |
| 302 |
if (not handles.move_hook) and (enable == true) then |
| 303 |
handles.move_hook = SimpleEventHook { |
| 304 |
name = "linking/rescan-trigger-on-target-metadata-changed", |
| 305 |
interests = { |
| 306 |
EventInterest { |
| 307 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 308 |
Constraint { "metadata.name", "=", "default" }, |
| 309 |
Constraint { "event.subject.key", "c", "target.object", "target.node" }, |
| 310 |
}, |
| 311 |
}, |
| 312 |
execute = function (event) |
| 313 |
local source = event:get_source () |
| 314 |
source:call ("schedule-rescan", "linking") |
| 315 |
end |
| 316 |
} |
| 317 |
handles.move_hook:register() |
| 318 |
elseif (handles.move_hook) and (enable == false) then |
| 319 |
handles.move_hook:remove () |
| 320 |
handles.move_hook = nil |
| 321 |
end |
| 322 |
end |
| 323 |
|
| 324 |
Settings.subscribe ("linking.allow-moving-streams", function () |
| 325 |
handleMoveSetting (Settings.get_boolean ("linking.allow-moving-streams")) |
| 326 |
end) |
| 327 |
handleMoveSetting (Settings.get_boolean ("linking.allow-moving-streams")) |