Debian Codegraph @

src/scripts/lib/linking-utils.lua

raw
1 -- WirePlumber
2
3 -- Copyright © 2022 Collabora Ltd.
4 -- @author Ashok Sidipotu <[email protected]>
5
6 -- SPDX-License-Identifier: MIT
7
8 -- Script is a Lua Module of linking Lua utility functions
9
10 local cutils = require ("common-utils")
11
12 local lutils = {
13 si_flags = {},
14 priority_media_role_link = {},
15 }
16
17 function lutils.get_flags (self, si_id)
18 if not self.si_flags [si_id] then
19 self.si_flags [si_id] = {}
20 end
21
22 return self.si_flags [si_id]
23 end
24
25 function lutils.clear_flags (self, si_id)
26 self.si_flags [si_id] = nil
27 end
28
29 function getprio (link)
30 return tonumber (link.properties ["policy.role-based.priority"]) or 0
31 end
32
33 function getplugged (link)
34 return tonumber (link.properties ["item.plugged.usec"]) or 0
35 end
36
37 function lutils.getAction (pmrl, link)
38 local props = pmrl.properties
39
40 if getprio (pmrl) == getprio (link) then
41 return props ["policy.role-based.action.same-priority"] or "mix"
42 else
43 return props ["policy.role-based.action.lower-priority"] or "mix"
44 end
45 end
46
47 -- if the link happens to be priority one, clear it and find the next
48 -- priority.
49 function lutils.clearPriorityMediaRoleLink (link)
50 local lprops = link.properties
51 local lmc = lprops ["target.media.class"]
52
53 pmrl = lutils.getPriorityMediaRoleLink (lmc)
54
55 -- only proceed if the link happens to be priority one.
56 if pmrl ~= link then
57 return
58 end
59
60 local prio_link = nil
61 local prio = 0
62 local plugged = 0
63 for l in cutils.get_object_manager ("session-item"):iterate {
64 type = "SiLink",
65 Constraint { "item.factory.name", "=", "si-standard-link", type = "pw-global" },
66 Constraint { "is.role.policy.link", "=", true },
67 Constraint { "target.media.class", "=", lmc },
68 } do
69 local props = l.properties
70
71 -- dont consider this link as it is about to be removed.
72 if pmrl == l then
73 goto continue
74 end
75
76 if getprio (link) > prio or
77 (getprio (link) == prio and getplugged (link) > plugged) then
78 prio = getprio (l)
79 plugged = getplugged (l)
80 prio_link = l
81 end
82 ::continue::
83 end
84
85 if prio_link then
86 setPriorityMediaRoleLink (lmc, prio_link)
87 else
88 setPriorityMediaRoleLink (lmc, nil)
89 end
90 end
91
92 -- record priority media role link
93 function lutils.updatePriorityMediaRoleLink (link)
94 local lprops = link.properties
95 local mc = lprops ["target.media.class"]
96
97 if not lutils.priority_media_role_link [mc] then
98 setPriorityMediaRoleLink (mc, link)
99 return
100 end
101
102 pmrl = lutils.getPriorityMediaRoleLink (mc)
103
104 if getprio (link) > getprio (pmrl) or
105 (getprio (link) == getprio (pmrl) and getplugged (link) >= getplugged (pmrl)) then
106 setPriorityMediaRoleLink (mc, link)
107 end
108 end
109
110 function lutils.getPriorityMediaRoleLink (lmc)
111 return lutils.priority_media_role_link [lmc]
112 end
113
114 function setPriorityMediaRoleLink (lmc, link)
115 lutils.priority_media_role_link [lmc] = link
116 if link then
117 Log.debug (
118 string.format ("update priority link(%d) media role(\"%s\") priority(%d)",
119 link.id, link.properties ["media.role"], getprio (link)))
120 else
121 Log.debug ("clear priority media role")
122 end
123 end
124
125 function lutils.is_role_policy_target (si_props, target_props)
126 -- role-based policy links are those that link to targets with
127 -- policy.role-based.target = true, unless the stream is a monitor
128 -- (usually pavucontrol) or the stream is linking to the monitor ports
129 -- of a sink (both are "input")
130 return Core.test_feature ("hooks.linking.role-based.rescan")
131 and cutils.parseBool (target_props["policy.role-based.target"])
132 and not cutils.parseBool (si_props ["stream.monitor"])
133 and si_props["item.node.direction"] ~= target_props["item.node.direction"]
134 end
135
136 function lutils.unwrap_select_target_event (self, event)
137 local source = event:get_source ()
138 local si = event:get_subject ()
139 local target = event:get_data ("target")
140 local om = source:call ("get-object-manager", "session-item")
141 local si_id = si.id
142
143 return source, om, si, si.properties, self:get_flags (si_id), target
144 end
145
146 function lutils.canPassthrough (si, si_target)
147 local props = si.properties
148 local tprops = si_target.properties
149 -- both nodes must support encoded formats
150 if not cutils.parseBool (props ["item.node.supports-encoded-fmts"])
151 or not cutils.parseBool (tprops ["item.node.supports-encoded-fmts"]) then
152 return false
153 end
154
155 -- make sure that the nodes have at least one common non-raw format
156 local n1 = si:get_associated_proxy ("node")
157 local n2 = si_target:get_associated_proxy ("node")
158 for p1 in n1:iterate_params ("EnumFormat") do
159 local p1p = p1:parse ()
160 if p1p.properties.mediaSubtype ~= "raw" then
161 for p2 in n2:iterate_params ("EnumFormat") do
162 if p1:filter (p2) then
163 return true
164 end
165 end
166 end
167 end
168 return false
169 end
170
171 function lutils.checkFollowDefault (si, si_target)
172 -- If it got linked to the default target that is defined by node
173 -- props but not metadata, start ignoring the node prop from now on.
174 -- This is what Pulseaudio does.
175 --
176 -- Pulseaudio skips here filter streams (i->origin_sink and
177 -- o->destination_source set in PA). Pipewire does not have a flag
178 -- explicitly for this, but we can use presence of node.link-group.
179 local si_props = si.properties
180 local target_props = si_target.properties
181 local reconnect = not cutils.parseBool (si_props ["node.dont-reconnect"])
182 local is_filter = (si_props ["node.link-group"] ~= nil)
183
184 if reconnect and not is_filter then
185 local def_id = cutils.getDefaultNode (si_props,
186 cutils.getTargetDirection (si_props))
187
188 if target_props ["node.id"] == tostring (def_id) then
189 local metadata = cutils.get_default_metadata_object ()
190 -- Set target.node, for backward compatibility
191 metadata:set (tonumber
192 (si_props ["node.id"]), "target.node", "Spa:Id", "-1")
193 Log.info (si, "... set metadata to follow default")
194 end
195 end
196 end
197
198 function lutils.lookupLink (si_id, si_target_id)
199 local link = cutils.get_object_manager ("session-item"):lookup {
200 type = "SiLink",
201 Constraint { "out.item.id", "=", si_id },
202 Constraint { "in.item.id", "=", si_target_id }
203 }
204 if not link then
205 link = cutils.get_object_manager ("session-item"):lookup {
206 type = "SiLink",
207 Constraint { "in.item.id", "=", si_id },
208 Constraint { "out.item.id", "=", si_target_id }
209 }
210 end
211 return link
212 end
213
214 function lutils.isLinked (si_target)
215 local target_id = si_target.id
216 local linked = false
217 local exclusive = false
218
219 for l in cutils.get_object_manager ("session-item"):iterate {
220 type = "SiLink",
221 } do
222 local p = l.properties
223 local out_id = tonumber (p ["out.item.id"])
224 local in_id = tonumber (p ["in.item.id"])
225 linked = (out_id == target_id) or (in_id == target_id)
226 if linked then
227 exclusive = cutils.parseBool (p ["exclusive"]) or cutils.parseBool (p ["passthrough"])
228 break
229 end
230 end
231 return linked, exclusive
232 end
233
234 function lutils.getNodePeerId (node_id)
235 for l in cutils.get_object_manager ("link"):iterate() do
236 local p = l.properties
237 local in_id = tonumber(p["link.input.node"])
238 local out_id = tonumber(p["link.output.node"])
239 if in_id == node_id then
240 return out_id
241 elseif out_id == node_id then
242 return in_id
243 end
244 end
245 return nil
246 end
247
248 function lutils.canLink (properties, si_target)
249 local target_props = si_target.properties
250
251 -- nodes must have the same media type
252 if properties ["media.type"] ~= target_props ["media.type"] then
253 return false
254 end
255
256 local function isMonitor(properties)
257 return properties ["item.node.direction"] == "input" and
258 cutils.parseBool (properties ["item.features.monitor"]) and
259 not cutils.parseBool (properties ["item.features.no-dsp"]) and
260 properties ["item.factory.name"] == "si-audio-adapter"
261 end
262
263 -- nodes must have opposite direction, or otherwise they must be both input
264 -- and the target must have a monitor (so the target will be used as a source)
265 if properties ["item.node.direction"] == target_props ["item.node.direction"]
266 and not isMonitor (target_props) then
267 return false
268 end
269
270 -- check link group
271 local function canLinkGroupCheck(link_group, si_target, hops)
272 local target_props = si_target.properties
273 local target_link_group = target_props ["node.link-group"]
274
275 if hops == 8 then
276 return false
277 end
278
279 -- allow linking if target has no link-group property
280 if not target_link_group then
281 return true
282 end
283
284 -- do not allow linking if target has the same link-group
285 if link_group == target_link_group then
286 return false
287 end
288
289 -- make sure target is not linked with another node with same link group
290 -- start by locating other nodes in the target's link-group, in opposite direction
291 for n in cutils.get_object_manager ("session-item"):iterate {
292 type = "SiLinkable",
293 Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" },
294 Constraint { "id", "!", si_target.id, type = "gobject" },
295 Constraint { "item.node.direction", "!", target_props ["item.node.direction"] },
296 Constraint { "node.link-group", "=", target_link_group },
297 } do
298 -- iterate their peers and return false if one of them cannot link
299 for silink in cutils.get_object_manager ("session-item"):iterate {
300 type = "SiLink",
301 } do
302 local out_id = tonumber (silink.properties ["out.item.id"])
303 local in_id = tonumber (silink.properties ["in.item.id"])
304 if out_id == n.id or in_id == n.id then
305 local peer_id = (out_id == n.id) and in_id or out_id
306 local peer = cutils.get_object_manager ("session-item"):lookup {
307 type = "SiLinkable",
308 Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" },
309 Constraint { "id", "=", peer_id, type = "gobject" },
310 }
311 if peer and not canLinkGroupCheck (link_group, peer, hops + 1) then
312 return false
313 end
314 end
315 end
316 end
317 return true
318 end
319
320 local link_group = properties ["node.link-group"]
321 if link_group then
322 return canLinkGroupCheck (link_group, si_target, 0)
323 end
324 return true
325 end
326
327 function lutils.findDefaultLinkable (si)
328 local si_props = si.properties
329 local target_direction = cutils.getTargetDirection (si_props)
330 local def_node_id = cutils.getDefaultNode (si_props, target_direction)
331 return cutils.get_object_manager ("session-item"):lookup {
332 type = "SiLinkable",
333 Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" },
334 Constraint { "node.id", "=", tostring (def_node_id) }
335 }
336 end
337
338 function lutils.checkPassthroughCompatibility (si, si_target)
339 local si_must_passthrough =
340 cutils.parseBool (si.properties ["item.node.encoded-only"])
341 local si_target_must_passthrough =
342 cutils.parseBool (si_target.properties ["item.node.encoded-only"])
343 local can_passthrough = lutils.canPassthrough (si, si_target)
344 if (si_must_passthrough or si_target_must_passthrough)
345 and not can_passthrough then
346 return false, can_passthrough
347 end
348 return true, can_passthrough
349 end
350
351 -- If the node has an associated device, does it have any active/available
352 -- paths/routes to the physical device?
353 -- Some UCM profiles expose all paths (headphones, HDMI, etc) as nodes,
354 -- even though they may not be connected... See #145
355 function lutils.haveAvailableRoutes (si_props, devices_om)
356 local card_profile_device = si_props ["card.profile.device"]
357 local device_id = si_props ["device.id"]
358
359 -- if the node does not have an associated device, it's all good
360 if not card_profile_device or not device_id then
361 return true
362 end
363
364 -- Get the device
365 devices_om = devices_om or cutils.get_object_manager ("device")
366 local device = devices_om:lookup {
367 Constraint { "bound-id", "=", device_id, type = "gobject" },
368 }
369
370 if not device then
371 return true
372 end
373
374 -- Check if at least one of the current device routes supports
375 -- the node's card.profile.device and is available
376 for p in device:iterate_params ("Route") do
377 local route = cutils.parseParam (p, "Route")
378 if route and (route.device == tonumber (card_profile_device)) then
379 if (route.available == "no") then
380 return false
381 else
382 return true
383 end
384 end
385 end
386
387 -- Check if available routes support the node's card.profile.device
388 local found = 0
389 for r in device:iterate_params ("EnumRoute") do
390 local route = cutils.parseParam (r, "EnumRoute")
391 if route and type (route.devices) == "table" then
392 for _, i in ipairs (route.devices) do
393 if i == tonumber (card_profile_device) then
394 found = found + 1
395 if route.available ~= "no" then
396 return true
397 end
398 end
399 end
400 end
401 end
402
403 -- The node is part of a profile without routes so we assume it
404 -- is available. This can happen for Pro Audio profiles
405 if found == 0 then
406 return true
407 end
408
409 return false
410 end
411
412 function lutils.sendClientError (event, node, code, message)
413 local source = event:get_source ()
414 local client_id = node.properties ["client.id"]
415 if client_id then
416 local clients_om = source:call ("get-object-manager", "client")
417 local client = clients_om:lookup {
418 Constraint { "bound-id", "=", client_id, type = "gobject" }
419 }
420 if client then
421 client:send_error (node ["bound-id"], code, message)
422 end
423 end
424 end
425
426 return lutils