| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
-- |
| 7 |
-- Links a session item to the target that has been previously selected. |
| 8 |
-- This is meant to be the last hook in the select-target chain. |
| 9 |
|
| 10 |
lutils = require ("linking-utils") |
| 11 |
cutils = require ("common-utils") |
| 12 |
log = Log.open_topic ("s-linking") |
| 13 |
|
| 14 |
AsyncEventHook { |
| 15 |
name = "linking/link-target", |
| 16 |
after = "linking/prepare-link", |
| 17 |
interests = { |
| 18 |
EventInterest { |
| 19 |
Constraint { "event.type", "=", "select-target" }, |
| 20 |
}, |
| 21 |
}, |
| 22 |
steps = { |
| 23 |
start = { |
| 24 |
next = "none", |
| 25 |
execute = function (event, transition) |
| 26 |
local source, om, si, si_props, si_flags, target = |
| 27 |
lutils:unwrap_select_target_event (event) |
| 28 |
|
| 29 |
if not target then |
| 30 |
-- bypass the hook, nothing to link to. |
| 31 |
transition:advance () |
| 32 |
return |
| 33 |
end |
| 34 |
|
| 35 |
local target_props = target.properties |
| 36 |
local out_item = nil |
| 37 |
local in_item = nil |
| 38 |
local si_link = nil |
| 39 |
local passthrough = si_flags.can_passthrough |
| 40 |
|
| 41 |
log:info (si, string.format ("handling item %d: %s (%s)", si.id, |
| 42 |
tostring (si_props ["node.name"]), tostring (si_props ["node.id"]))) |
| 43 |
|
| 44 |
local exclusive = cutils.parseBool (si_props ["node.exclusive"]) |
| 45 |
|
| 46 |
-- break rescan if tried more than 5 times with same target |
| 47 |
if si_flags.failed_peer_id ~= nil and |
| 48 |
si_flags.failed_peer_id == target.id and |
| 49 |
si_flags.failed_count ~= nil and |
| 50 |
si_flags.failed_count > 5 then |
| 51 |
transition:return_error ("tried to link on last rescan, not retrying " |
| 52 |
.. tostring (si_link)) |
| 53 |
return |
| 54 |
end |
| 55 |
|
| 56 |
if si_props["item.node.direction"] == "output" then |
| 57 |
-- playback |
| 58 |
out_item = si |
| 59 |
in_item = target |
| 60 |
else |
| 61 |
-- capture |
| 62 |
in_item = si |
| 63 |
out_item = target |
| 64 |
end |
| 65 |
|
| 66 |
local is_role_policy_link = lutils.is_role_policy_target (si_props, target_props) |
| 67 |
|
| 68 |
log:info (si, |
| 69 |
string.format ("link %s <-> %s passthrough:%s, exclusive:%s, media role link:%s", |
| 70 |
tostring (si_props ["node.name"]), |
| 71 |
tostring (target_props ["node.name"]), |
| 72 |
tostring (passthrough), |
| 73 |
tostring (exclusive), |
| 74 |
tostring (is_role_policy_link))) |
| 75 |
|
| 76 |
-- create and configure link |
| 77 |
si_link = SessionItem ("si-standard-link") |
| 78 |
if not si_link:configure { |
| 79 |
["out.item"] = out_item, |
| 80 |
["in.item"] = in_item, |
| 81 |
["passthrough"] = passthrough, |
| 82 |
["exclusive"] = exclusive, |
| 83 |
["out.item.port.context"] = "output", |
| 84 |
["in.item.port.context"] = "input", |
| 85 |
["media.role"] = si_props["media.role"], |
| 86 |
["target.media.class"] = target_props["media.class"], |
| 87 |
["policy.role-based.priority"] = target_props["policy.role-based.priority"], |
| 88 |
["policy.role-based.action.same-priority"] = target_props["policy.role-based.action.same-priority"], |
| 89 |
["policy.role-based.action.lower-priority"] = target_props["policy.role-based.action.lower-priority"], |
| 90 |
["is.role.policy.link"] = is_role_policy_link, |
| 91 |
["main.item.id"] = si.id, |
| 92 |
["target.item.id"] = target.id, |
| 93 |
} then |
| 94 |
transition:return_error ("failed to configure si-standard-link " |
| 95 |
.. tostring (si_link)) |
| 96 |
return |
| 97 |
end |
| 98 |
|
| 99 |
local ids = {si.id, target.id} |
| 100 |
si_link:connect("link-error", function (_, error_msg) |
| 101 |
for _, id in ipairs (ids) do |
| 102 |
local si = om:lookup { |
| 103 |
Constraint { "id", "=", id, type = "gobject" }, |
| 104 |
} |
| 105 |
if si then |
| 106 |
local node = si:get_associated_proxy ("node") |
| 107 |
lutils.sendClientError(event, node, -32, error_msg) |
| 108 |
end |
| 109 |
end |
| 110 |
end) |
| 111 |
|
| 112 |
-- register |
| 113 |
si_flags.was_handled = true |
| 114 |
si_flags.peer_id = target.id |
| 115 |
si_flags.failed_peer_id = target.id |
| 116 |
if si_flags.failed_count ~= nil then |
| 117 |
si_flags.failed_count = si_flags.failed_count + 1 |
| 118 |
else |
| 119 |
si_flags.failed_count = 1 |
| 120 |
end |
| 121 |
si_link:register () |
| 122 |
|
| 123 |
log:debug (si_link, "registered link between " |
| 124 |
.. tostring (si) .. " and " .. tostring (target)) |
| 125 |
|
| 126 |
-- only activate non role-based policy links because their activation is |
| 127 |
-- handled by rescan-media-role-links.lua |
| 128 |
if not is_role_policy_link then |
| 129 |
si_link:activate (Feature.SessionItem.ACTIVE, function (l, e) |
| 130 |
if e then |
| 131 |
transition:return_error (tostring (l) .. " link failed: " |
| 132 |
.. tostring (e)) |
| 133 |
if si_flags ~= nil then |
| 134 |
si_flags.peer_id = nil |
| 135 |
end |
| 136 |
l:remove () |
| 137 |
else |
| 138 |
si_flags.failed_peer_id = nil |
| 139 |
if si_flags.peer_id == nil then |
| 140 |
si_flags.peer_id = target.id |
| 141 |
end |
| 142 |
si_flags.failed_count = 0 |
| 143 |
|
| 144 |
log:debug (l, "activated link between " |
| 145 |
.. tostring (si) .. " and " .. tostring (target)) |
| 146 |
|
| 147 |
transition:advance () |
| 148 |
end |
| 149 |
end) |
| 150 |
else |
| 151 |
lutils.updatePriorityMediaRoleLink(si_link) |
| 152 |
transition:advance () |
| 153 |
end |
| 154 |
end, |
| 155 |
}, |
| 156 |
}, |
| 157 |
}:register () |