From f8ced47a1a9595b32c59ece24b864bafbdcc74fe Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Tue, 19 Oct 2021 13:04:58 +0300 Subject: [PATCH] policy-node: enforce the direction of the target when linking by node name When node.target is set to contain a node's name, it is possible to run into the situation where you have a sink and a source with the same name (typically the case with module-loopback) and the sink has monitor ports, so wireplumber may link the stream to the monitor of the sink instead of the capture ports of the source With this policy, if a stream really wants to link to the monitor of the sink, it has to have the "stream.capture.sink" property set or specify the target by id. --- src/scripts/policy-node.lua | 76 +++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/src/scripts/policy-node.lua b/src/scripts/policy-node.lua index 4c5c359b..e982a09d 100644 --- a/src/scripts/policy-node.lua +++ b/src/scripts/policy-node.lua @@ -187,47 +187,6 @@ function canLink (properties, si_target) return true end --- Try to locate a valid target node that was explicitly defined by the user --- Use the target.node metadata, if config.move is enabled, --- then use the node.target property that was set on the node --- `properties` must be the properties dictionary of the session item --- that is currently being handled -function findDefinedTarget (properties) - local function findTargetByTargetNodeMetadata (properties) - local node_id = properties["node.id"] - local metadata = metadata_om:lookup() - local target_id = metadata and metadata:find(node_id, "target.node") or nil - if target_id and tonumber(target_id) > 0 then - local si_target = linkables_om:lookup { - Constraint { "node.id", "=", target_id }, - } - if si_target and canLink (properties, si_target) then - return si_target - end - end - return nil - end - - local function findTargetByNodeTargetProperty (properties) - local target_id = properties["node.target"] - if target_id then - for si_target in linkables_om:iterate() do - local target_props = si_target.properties - if (target_props["node.id"] == target_id or - target_props["node.name"] == target_id or - target_props["object.path"] == target_id) and - canLink (properties, si_target) then - return si_target - end - end - end - return nil - end - - return (config.move and findTargetByTargetNodeMetadata (properties) or nil) - or findTargetByNodeTargetProperty (properties) -end - function getTargetDirection(properties) local target_direction = nil if properties["item.node.direction"] == "output" or @@ -247,6 +206,41 @@ function getDefaultNode(properties, target_direction) return default_nodes:call("get-default-node", target_media_class) end +-- Try to locate a valid target node that was explicitly defined by the user +-- Use the target.node metadata, if config.move is enabled, +-- then use the node.target property that was set on the node +-- `properties` must be the properties dictionary of the session item +-- that is currently being handled +function findDefinedTarget (properties) + local metadata = config.move and metadata_om:lookup() + local target_id = metadata + and metadata:find(properties["node.id"], "target.node") + or properties["node.target"] + local target_direction = getTargetDirection(properties) + + if target_id and tonumber(target_id) then + local si_target = linkables_om:lookup { + Constraint { "node.id", "=", target_id }, + } + if si_target and canLink (properties, si_target) then + return si_target + end + end + + if target_id then + for si_target in linkables_om:iterate() do + local target_props = si_target.properties + if (target_props["node.name"] == target_id or + target_props["object.path"] == target_id) and + target_props["item.node.direction"] == target_direction and + canLink (properties, si_target) then + return si_target + end + end + end + return nil +end + -- Try to locate a valid target node that was NOT explicitly defined by the user -- `properties` must be the properties dictionary of the session item -- that is currently being handled -- GitLab