| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2024 Collabora Ltd. |
| 4 |
-- @author Ashok Sidipotu <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
lutils = require("linking-utils") |
| 9 |
cutils = require("common-utils") |
| 10 |
log = Log.open_topic("s-linking") |
| 11 |
|
| 12 |
function restoreVolume (om, link) |
| 13 |
setVolume(om, link, 1.0) |
| 14 |
end |
| 15 |
|
| 16 |
function duckVolume (om, link) |
| 17 |
setVolume(om, link, Settings.get_float("linking.role-based.duck-level")) |
| 18 |
end |
| 19 |
|
| 20 |
function setVolume (om, link, level) |
| 21 |
local lprops = link.properties |
| 22 |
local media_role_si_id = nil |
| 23 |
local dir = lprops ["item.node.direction"] |
| 24 |
|
| 25 |
if dir == "output" then |
| 26 |
media_role_si_id = lprops ["out.item.id"] |
| 27 |
else |
| 28 |
media_role_si_id = lprops ["in.item.id"] |
| 29 |
end |
| 30 |
|
| 31 |
local media_role_lnkbl = om:lookup { |
| 32 |
type = "SiLinkable", |
| 33 |
Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" }, |
| 34 |
Constraint { "id", "=", media_role_si_id, type = "gobject" }, |
| 35 |
} |
| 36 |
|
| 37 |
-- apply volume control on the stream node of the loopback module, instead of |
| 38 |
-- the sink/source node as it simplifies the volume ducking and |
| 39 |
-- restoration. |
| 40 |
local media_role_other_lnkbl = om:lookup { |
| 41 |
type = "SiLinkable", |
| 42 |
Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" }, |
| 43 |
Constraint { "node.link-group", "=", media_role_lnkbl.properties ["node.link-group"] }, |
| 44 |
Constraint { "id", "!", media_role_lnkbl.id, type = "gobject" }, |
| 45 |
} |
| 46 |
|
| 47 |
if media_role_other_lnkbl then |
| 48 |
local n = media_role_other_lnkbl:get_associated_proxy("node") |
| 49 |
if n then |
| 50 |
log:info(string.format(".. %s volume of media role node \"%s(%d)\" to %f", |
| 51 |
level < 1.0 and "duck" or "restore", n.properties ["node.name"], |
| 52 |
n ["bound-id"], level)) |
| 53 |
|
| 54 |
local props = { |
| 55 |
"Spa:Pod:Object:Param:Props", |
| 56 |
"Props", |
| 57 |
volume = level, |
| 58 |
} |
| 59 |
|
| 60 |
local param = Pod.Object(props) |
| 61 |
n:set_param("Props", param) |
| 62 |
end |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
function getSuspendPlaybackFromMetadata (om) |
| 67 |
local suspend = false |
| 68 |
local metadata = om:lookup { |
| 69 |
type = "metadata", |
| 70 |
Constraint { "metadata.name", "=", "default" }, |
| 71 |
} |
| 72 |
if metadata then |
| 73 |
local value = metadata:find(0, "suspend.playback") |
| 74 |
if value then |
| 75 |
suspend = value == "1" and true or false |
| 76 |
end |
| 77 |
end |
| 78 |
return suspend |
| 79 |
end |
| 80 |
|
| 81 |
AsyncEventHook { |
| 82 |
name = "linking/rescan-media-role-links", |
| 83 |
interests = { |
| 84 |
EventInterest { |
| 85 |
-- on media client link added and removed |
| 86 |
Constraint { "event.type", "c", "session-item-added", "session-item-removed" }, |
| 87 |
Constraint { "event.session-item.interface", "=", "link" }, |
| 88 |
Constraint { "is.role.policy.link", "=", true }, |
| 89 |
}, |
| 90 |
EventInterest { |
| 91 |
-- on default metadata suspend.playback changed |
| 92 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 93 |
Constraint { "metadata.name", "=", "default" }, |
| 94 |
Constraint { "event.subject.key", "=", "suspend.playback" }, |
| 95 |
} |
| 96 |
}, |
| 97 |
steps = { |
| 98 |
start = { |
| 99 |
next = "none", |
| 100 |
execute = function(event, transition) |
| 101 |
local source, om, _, si_props, _, _ = |
| 102 |
lutils:unwrap_select_target_event(event) |
| 103 |
|
| 104 |
local metadata_om = source:call("get-object-manager", "metadata") |
| 105 |
local suspend = getSuspendPlaybackFromMetadata(metadata_om) |
| 106 |
local pending_activations = 0 |
| 107 |
local mc = si_props ["target.media.class"] |
| 108 |
local pmrl_active = nil |
| 109 |
pmrl = lutils.getPriorityMediaRoleLink(mc) |
| 110 |
|
| 111 |
log:debug("Rescanning media role links...") |
| 112 |
|
| 113 |
local function onMediaRoleLinkActivated (l, e) |
| 114 |
local si_id = tonumber(l.properties ["main.item.id"]) |
| 115 |
local target_id = tonumber(l.properties ["target.item.id"]) |
| 116 |
local si_flags = lutils:get_flags(si_id) |
| 117 |
|
| 118 |
if e then |
| 119 |
log:warning(l, "failed to activate media role link: " .. e) |
| 120 |
if si_flags ~= nil then |
| 121 |
si_flags.peer_id = nil |
| 122 |
end |
| 123 |
l:remove() |
| 124 |
else |
| 125 |
log:info(l, "media role link activated successfully") |
| 126 |
si_flags.failed_peer_id = nil |
| 127 |
if si_flags.peer_id == nil then |
| 128 |
si_flags.peer_id = target_id |
| 129 |
end |
| 130 |
si_flags.failed_count = 0 |
| 131 |
end |
| 132 |
|
| 133 |
-- advance only when all pending activations are completed |
| 134 |
pending_activations = pending_activations - 1 |
| 135 |
if pending_activations <= 0 then |
| 136 |
log:info("All media role links activated") |
| 137 |
transition:advance() |
| 138 |
end |
| 139 |
end |
| 140 |
|
| 141 |
for link in om:iterate { |
| 142 |
type = "SiLink", |
| 143 |
Constraint { "is.role.policy.link", "=", true }, |
| 144 |
Constraint { "target.media.class", "=", mc }, |
| 145 |
} do |
| 146 |
-- deactivate all links if suspend playback metadata is present |
| 147 |
if suspend then |
| 148 |
link:deactivate(Feature.SessionItem.ACTIVE) |
| 149 |
end |
| 150 |
|
| 151 |
local active = ((link:get_active_features() & Feature.SessionItem.ACTIVE) ~= 0) |
| 152 |
|
| 153 |
log:debug(string.format(" .. looking at link(%d) active %s pmrl %s", link.id, tostring(active), |
| 154 |
tostring(link == pmrl))) |
| 155 |
|
| 156 |
if link == pmrl then |
| 157 |
pmrl_active = active |
| 158 |
restoreVolume(om, pmrl) |
| 159 |
goto continue |
| 160 |
end |
| 161 |
|
| 162 |
local action = lutils.getAction(pmrl, link) |
| 163 |
|
| 164 |
log:debug(string.format(" .. apply action(%s) on link(%d)", action, link.id, tostring(active))) |
| 165 |
|
| 166 |
if action == "cork" then |
| 167 |
if active then |
| 168 |
link:deactivate(Feature.SessionItem.ACTIVE) |
| 169 |
end |
| 170 |
elseif action == "mix" then |
| 171 |
if not active and not suspend then |
| 172 |
pending_activations = pending_activations + 1 |
| 173 |
link:activate(Feature.SessionItem.ACTIVE, onMediaRoleLinkActivated) |
| 174 |
end |
| 175 |
restoreVolume(om, link) |
| 176 |
elseif action == "duck" then |
| 177 |
if not active and not suspend then |
| 178 |
pending_activations = pending_activations + 1 |
| 179 |
link:activate(Feature.SessionItem.ACTIVE, onMediaRoleLinkActivated) |
| 180 |
end |
| 181 |
duckVolume(om, link) |
| 182 |
else |
| 183 |
log:warning("Unknown action: " .. action) |
| 184 |
end |
| 185 |
|
| 186 |
::continue:: |
| 187 |
end |
| 188 |
|
| 189 |
if pmrl and not pmrl_active then |
| 190 |
pending_activations = pending_activations + 1 |
| 191 |
pmrl:activate(Feature.SessionItem.ACTIVE, onMediaRoleLinkActivated) |
| 192 |
restoreVolume(om, pmrl) |
| 193 |
end |
| 194 |
|
| 195 |
-- just advance transition if no pending activations are needed |
| 196 |
if pending_activations <= 0 then |
| 197 |
log:debug("All media role links rescanned") |
| 198 |
transition:advance() |
| 199 |
end |
| 200 |
end, |
| 201 |
}, |
| 202 |
}, |
| 203 |
}:register() |