Debian Codegraph @

src/scripts/linking/find-media-role-sink-target.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2025 Phosh.mobi e.V.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- Pick up a preferred target node for the output stream of role-based loopbacks
8
9 lutils = require ("linking-utils")
10 cutils = require ("common-utils")
11
12 log = Log.open_topic ("s-linking")
13
14 SimpleEventHook {
15 name = "linking/find-media-role-sink-target",
16 after = { "linking/find-defined-target",
17 "linking/find-media-role-target" },
18 before = "linking/prepare-link",
19 interests = {
20 EventInterest {
21 Constraint { "event.type", "=", "select-target" },
22 },
23 },
24 execute = function (event)
25 local _, om, si, si_props, _, target =
26 lutils:unwrap_select_target_event (event)
27
28 local node_name = si_props["node.name"]
29 local target_direction = cutils.getTargetDirection (si_props)
30 local media_class = si_props["media.class"]
31 local link_group = si_props["node.link-group"]
32 local is_virtual = si_props["node.virtual"]
33
34 log:info (si, string.format ("Lookup for '%s' (%s) / '%s' / '%s'",
35 node_name, tostring (si_props ["node.id"]), media_class, link_group))
36
37 --- bypass the hook if the target is already set or there's no link group
38 if target or media_class ~= "Stream/Output/Audio" or not is_virtual or link_group == nil then
39 return
40 end
41
42 --- We link the output node but the relevant properties are on the input node
43 --- of the link group
44 local input_node = om:lookup {
45 type = "SiLinkable",
46 Constraint { "media.class", "=", "Audio/Sink" },
47 Constraint { "node.link-group", "=", link_group },
48 }
49
50 if input_node == nil then
51 log:warning (si, string.format("No input node for %s found", link_group))
52 return
53 end
54
55 local target_name = input_node.properties["policy.role-based.preferred-target"]
56 --- no preferred target
57 if target_name == nil then
58 return
59 end
60
61 local si_target = om:lookup {
62 type = "SiLinkable",
63 Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" },
64 Constraint { "node.name", "=", target_name },
65 }
66 if si_target == nil then
67 si_target = om:lookup {
68 type = "SiLinkable",
69 Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" },
70 Constraint { "node.nick", "=", target_name },
71 }
72 end
73 if si_target then
74 log:info (si,
75 string.format ("... role based sink target picked: %s (%s)",
76 tostring (si_target.properties ["node.name"]),
77 tostring (si_target.properties ["node.id"])))
78 event:set_data ("target", si_target)
79 end
80 end
81 }:register ()