Debian Codegraph @

src/scripts/linking/prepare-link.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2022 Collabora Ltd.
4 --
5 -- SPDX-License-Identifier: MIT
6 --
7 -- remove the existing link if needed, check the properties of target, which
8 -- indicate it is not available for linking. If no target is available, send
9 -- down an error to the corresponding client.
10
11 lutils = require ("linking-utils")
12 cutils = require ("common-utils")
13 log = Log.open_topic ("s-linking")
14
15 SimpleEventHook {
16 name = "linking/prepare-link",
17 interests = {
18 EventInterest {
19 Constraint { "event.type", "=", "select-target" },
20 },
21 },
22 execute = function (event)
23 local source, _, si, si_props, si_flags, target =
24 lutils:unwrap_select_target_event (event)
25
26 local si_id = si.id
27 local reconnect = not cutils.parseBool (si_props ["node.dont-reconnect"])
28 local exclusive = cutils.parseBool (si_props ["node.exclusive"])
29 local si_must_passthrough = cutils.parseBool (si_props ["item.node.encoded-only"])
30
31 log:info (si, string.format ("handling item %d: %s (%s)", si_id,
32 tostring (si_props ["node.name"]), tostring (si_props ["node.id"])))
33
34 -- Check if item is linked to proper target, otherwise re-link
35 if si_flags.peer_id then
36 if target and si_flags.peer_id == target.id then
37 log:info (si, "... already linked to proper target")
38
39 -- Check this also here, in case in default targets changed
40 if Settings.get_boolean ("linking.follow-default-target") and
41 si_flags.has_node_defined_target then
42 lutils.checkFollowDefault (si, target)
43 end
44
45 target = nil
46 goto done
47 end
48
49 local link = lutils.lookupLink (si_id, si_flags.peer_id)
50 if reconnect then
51 if link ~= nil then
52 -- remove old link
53 if ((link:get_active_features () & Feature.SessionItem.ACTIVE) == 0)
54 then
55 -- remove also not yet activated links: they might never become
56 -- active, and we need not wait for it to become active
57 log:warning (link, "Link was not activated before removing")
58 end
59 si_flags.peer_id = nil
60 link:remove ()
61 log:info (si, "... moving to new target")
62 end
63 else
64 if link ~= nil then
65 log:info (si, "... dont-reconnect, not moving")
66 goto done
67 end
68 end
69 end
70
71 -- if the stream has dont-reconnect and was already linked before,
72 -- don't link it to a new target
73 if not reconnect and si_flags.was_handled then
74 target = nil
75 goto done
76 end
77
78 -- check target's availability
79 if target then
80 local target_is_linked, target_is_exclusive = lutils.isLinked (target)
81 if target_is_exclusive then
82 log:info (si, "... target is linked exclusively")
83 target = nil
84 end
85
86 if target_is_linked then
87 if exclusive or si_must_passthrough then
88 log:info (si, "... target is already linked, cannot link exclusively")
89 target = nil
90 else
91 -- disable passthrough, we can live without it
92 si_flags.can_passthrough = false
93 end
94 end
95 end
96
97 if not target then
98 log:info (si, "... target not found, reconnect:" .. tostring (reconnect))
99
100 local node = si:get_associated_proxy ("node")
101 if reconnect and si_flags.was_handled then
102 log:info (si, "... waiting reconnect")
103 return
104 end
105
106 local linger = cutils.parseBool (si_props ["node.linger"])
107
108 if linger then
109 log:info (si, "... node linger")
110 return
111 end
112
113 lutils.sendClientError (event, node, -2,
114 reconnect and "no target node available" or "target not found")
115
116 if not reconnect then
117 log:info (si, "... destroy node")
118 node:request_destroy ()
119 end
120 end
121
122 ::done::
123 event:set_data ("target", target)
124 end
125 }:register ()