| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2025 Pauli Virtanen |
| 4 |
-- @author Pauli Virtanen <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
-- |
| 8 |
-- Pause media player applications when (one of) their output |
| 9 |
-- target(s) goes away. |
| 10 |
|
| 11 |
cutils = require ("common-utils") |
| 12 |
lutils = require ("linking-utils") |
| 13 |
|
| 14 |
log = Log.open_topic ("s-linking.mpris") |
| 15 |
|
| 16 |
mpris = Plugin.find ("mpris") |
| 17 |
|
| 18 |
RESCAN_DELAY_MSEC = 1000 |
| 19 |
|
| 20 |
function initializeState () |
| 21 |
-- Delaying rescan while pausing players |
| 22 |
pending_ops = 0 |
| 23 |
need_rescan = false |
| 24 |
|
| 25 |
-- Links between nodes: links_in [in_node_id] = { out_node_id, ... } |
| 26 |
links_in = {} |
| 27 |
links_initialized = false |
| 28 |
|
| 29 |
-- Link nodes: link_nodes [link.id] = { in_node_id, out_node_id } |
| 30 |
link_nodes = {} |
| 31 |
|
| 32 |
-- Status |
| 33 |
script_active = false |
| 34 |
end |
| 35 |
|
| 36 |
-- Get nodes that are (indirectly) linked to `si` by link group |
| 37 |
-- or links with input direction. Returns table { [node_id] = node, ... } |
| 38 |
function getLinkedNodes (start_id) |
| 39 |
local node_om = cutils.get_object_manager ("node") |
| 40 |
local groups = {} |
| 41 |
local link_groups = {} |
| 42 |
|
| 43 |
log:trace(string.format("start %d", start_id)) |
| 44 |
|
| 45 |
-- construct groups based on node.link-group |
| 46 |
for node in node_om:iterate () do |
| 47 |
local id = node ["bound-id"] |
| 48 |
|
| 49 |
if groups [id] ~= nil then |
| 50 |
return |
| 51 |
end |
| 52 |
|
| 53 |
local link_group = node.properties ["node.link-group"] |
| 54 |
local group = nil |
| 55 |
|
| 56 |
-- join via link groups |
| 57 |
if link_group ~= nil then |
| 58 |
group = groups [link_groups [link_group]] |
| 59 |
link_groups [link_group] = id |
| 60 |
end |
| 61 |
if group == nil then |
| 62 |
group = {} |
| 63 |
end |
| 64 |
|
| 65 |
group [id] = node |
| 66 |
groups [id] = group |
| 67 |
end |
| 68 |
|
| 69 |
-- follow links |
| 70 |
local group = {} |
| 71 |
local active = { [start_id] = true } |
| 72 |
|
| 73 |
while true do |
| 74 |
local a_id = next(active) |
| 75 |
if a_id == nil then |
| 76 |
break |
| 77 |
end |
| 78 |
|
| 79 |
active [a_id] = nil |
| 80 |
|
| 81 |
local b_ids = links_in [a_id] |
| 82 |
if b_ids ~= nil then |
| 83 |
for b_id in pairs (b_ids) do |
| 84 |
if group [b_id] == nil and groups [b_id] ~= nil then |
| 85 |
for k, v in pairs (groups [b_id]) do |
| 86 |
if group [k] == nil then |
| 87 |
active [k] = true |
| 88 |
group [k] = v |
| 89 |
log:trace(string.format("node %d", k)) |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
return group |
| 98 |
end |
| 99 |
|
| 100 |
-- Track link status. |
| 101 |
-- |
| 102 |
-- We need to know what state links were in just before a sink node is |
| 103 |
-- removed. We rely here on the following facts: |
| 104 |
-- |
| 105 |
-- * When node is removed existing links to it are removed by server synchronously |
| 106 |
-- * Although it appears server notifies link removal *after* the node, use Core sync |
| 107 |
-- to delay handling link removal after node removal, to be sure |
| 108 |
-- * event_priority(node-removed, session-item-removed) > event_priority(link-removed) |
| 109 |
-- then ensures we first handle item removal, then update links |
| 110 |
|
| 111 |
function updateLink (in_id, out_id, remove) |
| 112 |
if links_in [in_id] == nil then |
| 113 |
links_in [in_id] = {} |
| 114 |
end |
| 115 |
if remove then |
| 116 |
links_in [in_id] [out_id] = nil |
| 117 |
else |
| 118 |
links_in [in_id] [out_id] = true |
| 119 |
end |
| 120 |
end |
| 121 |
|
| 122 |
function updateLinks (links_om) |
| 123 |
log:debug ("update links") |
| 124 |
for link in links_om:iterate () do |
| 125 |
local lprops = link.properties |
| 126 |
local in_id = tonumber (lprops ["link.input.node"]) |
| 127 |
local out_id = tonumber (lprops ["link.output.node"]) |
| 128 |
updateLink (in_id, out_id, false) |
| 129 |
link_nodes [link.id] = { in_id, out_id } |
| 130 |
end |
| 131 |
end |
| 132 |
|
| 133 |
function initializeLinks (source) |
| 134 |
if links_initialized then |
| 135 |
return |
| 136 |
end |
| 137 |
if source == nil then |
| 138 |
-- postpone to later |
| 139 |
return |
| 140 |
end |
| 141 |
|
| 142 |
local links_om = source:call ("get-object-manager", "link") |
| 143 |
updateLinks(links_om) |
| 144 |
links_initialized = true |
| 145 |
end |
| 146 |
|
| 147 |
link_hook = SimpleEventHook { |
| 148 |
name = "linking/mpris-pause@track-links", |
| 149 |
interests = { |
| 150 |
EventInterest { |
| 151 |
Constraint { "event.type", "c", "link-added", "link-removed" }, |
| 152 |
}, |
| 153 |
}, |
| 154 |
execute = function (event) |
| 155 |
local link = event:get_subject () |
| 156 |
local eprops = event:get_properties () |
| 157 |
local source = event:get_source () |
| 158 |
|
| 159 |
initializeLinks (source) |
| 160 |
|
| 161 |
local in_id |
| 162 |
local out_id |
| 163 |
local remove = false |
| 164 |
|
| 165 |
if eprops ["event.type"] == "link-added" then |
| 166 |
local lprops = link.properties |
| 167 |
in_id = tonumber (lprops ["link.input.node"]) |
| 168 |
out_id = tonumber (lprops ["link.output.node"]) |
| 169 |
link_nodes [link.id] = { in_id, out_id } |
| 170 |
elseif link_nodes [link.id] ~= nil then |
| 171 |
in_id = link_nodes [link.id] [1] |
| 172 |
out_id = link_nodes [link.id] [2] |
| 173 |
link_nodes [link.id] = nil |
| 174 |
remove = true |
| 175 |
end |
| 176 |
|
| 177 |
if in_id == nil or out_id == nil then |
| 178 |
return |
| 179 |
end |
| 180 |
|
| 181 |
Core.sync(function() |
| 182 |
updateLink (in_id, out_id, remove) |
| 183 |
end) |
| 184 |
end |
| 185 |
} |
| 186 |
|
| 187 |
-- Pause media applications associated with the streams linked to a sink to be removed |
| 188 |
pause_hook = SimpleEventHook { |
| 189 |
name = "linking/mpris-pause", |
| 190 |
before = "linking/linkable-removed", |
| 191 |
interests = { |
| 192 |
EventInterest { |
| 193 |
Constraint { "event.type", "=", "session-item-removed" }, |
| 194 |
Constraint { "item.node.type", "=", "device" }, |
| 195 |
Constraint { "item.node.direction", "=", "input" }, |
| 196 |
}, |
| 197 |
}, |
| 198 |
execute = function (event) |
| 199 |
local players = mpris:call ("get-players") |
| 200 |
if next(players) == nil then |
| 201 |
return |
| 202 |
end |
| 203 |
|
| 204 |
-- find clients to handle |
| 205 |
local si = event:get_subject () |
| 206 |
local source = event:get_source () |
| 207 |
local client_om = source:call ("get-object-manager", "client") |
| 208 |
local node = si:get_associated_proxy ("node") |
| 209 |
local node_group = getLinkedNodes (tonumber (si.properties ["node.id"])) |
| 210 |
local client_ids = {} |
| 211 |
|
| 212 |
for id, node in pairs (node_group) do |
| 213 |
local media_class = tostring(node.properties ["media.class"]) |
| 214 |
if media_class:find ("^Stream/Output") then |
| 215 |
client_ids [node.properties ["client.id"]] = true |
| 216 |
end |
| 217 |
end |
| 218 |
|
| 219 |
-- find players to handle |
| 220 |
local pause_players = {} |
| 221 |
|
| 222 |
for client_id in pairs (client_ids) do |
| 223 |
local client = client_om:lookup { |
| 224 |
Constraint { "bound-id", "=", client_id, type = "gobject" } |
| 225 |
} |
| 226 |
if not client then |
| 227 |
goto next |
| 228 |
end |
| 229 |
|
| 230 |
log:debug (si, string.format ("node %s removed: was linked to client %s (%s pid %s)", |
| 231 |
tostring(si.properties ["node.id"]), |
| 232 |
tostring(client.properties ["application.name"]), |
| 233 |
tostring(client.properties ["application.id"]), |
| 234 |
tostring(client.properties ["application.process.id"]))) |
| 235 |
|
| 236 |
for _, player in ipairs(players) do |
| 237 |
local match = false |
| 238 |
|
| 239 |
if client.properties ["pipewire.access.portal.app_id"] == nil and player ["flatpak-app-id"] == nil then |
| 240 |
-- assume non-flatpak apps serve audio from same or child process as MPRIS |
| 241 |
if player ["pid"] ~= nil then |
| 242 |
local keys = { "pipewire.sec.pid", "application.process.id" } |
| 243 |
for _, key in ipairs(keys) do |
| 244 |
local pid = tonumber (client.properties [key]) |
| 245 |
if pid ~= nil and mpris:call ("match-pid", player ["pid"], pid) then |
| 246 |
log:debug (si, string.format(".. match %s %u ~ %u", key, pid, player ["pid"])) |
| 247 |
match = true |
| 248 |
end |
| 249 |
end |
| 250 |
end |
| 251 |
elseif client.properties ["pipewire.access.portal.app_id"] == player ["flatpak-app-id"] then |
| 252 |
local instance_id = client.properties ["pipewire.access.portal.instance_id"] |
| 253 |
if instance_id == nil then |
| 254 |
-- Only new Pipewire versions have instance_id |
| 255 |
log:debug (si, string.format(".. match pipewire.access.portal.app_id = %s", player ["flatpak-app-id"])) |
| 256 |
match = true |
| 257 |
elseif instance_id == player ["flatpak-instance-id"] then |
| 258 |
log:debug (si, string.format(".. match pipewire.access.portal.app_id = %s instance_id = %s", |
| 259 |
player ["flatpak-app-id"], player ["flatpak-instance-id"])) |
| 260 |
match = true |
| 261 |
end |
| 262 |
end |
| 263 |
|
| 264 |
if match then |
| 265 |
pause_players [player ["name"]] = true |
| 266 |
end |
| 267 |
end |
| 268 |
|
| 269 |
::next:: |
| 270 |
end |
| 271 |
|
| 272 |
-- handle players |
| 273 |
for bus_name in pairs(pause_players) do |
| 274 |
log:info (si, string.format("node %s removed: pausing linked media player %s", |
| 275 |
tostring(si.properties ["node.id"]), |
| 276 |
bus_name)) |
| 277 |
|
| 278 |
local op = mpris:call ("pause", bus_name) |
| 279 |
if op ["result"] == 0 then |
| 280 |
log:debug ("pause pending") |
| 281 |
pending_ops = pending_ops + 1 |
| 282 |
op:connect("notify::result", function (op, pspec) |
| 283 |
if pending_ops > 0 then |
| 284 |
pending_ops = pending_ops - 1 |
| 285 |
end |
| 286 |
log:debug (string.format("pause completed, res = %d, %d remaining", |
| 287 |
op ["result"], pending_ops)) |
| 288 |
if pending_ops == 0 and need_rescan then |
| 289 |
-- Some players respond to DBus before actually pausing output, |
| 290 |
-- so add also a small delay |
| 291 |
Core.timeout_add(RESCAN_DELAY_MSEC, function() |
| 292 |
log:debug ("continue rescan") |
| 293 |
source:call ("push-event", "rescan-for-linking", nil, nil) |
| 294 |
end) |
| 295 |
end |
| 296 |
end) |
| 297 |
end |
| 298 |
end |
| 299 |
end |
| 300 |
} |
| 301 |
|
| 302 |
-- Do not perform rescans while we are pausing media players |
| 303 |
rescan_hook = SimpleEventHook { |
| 304 |
name = "linking/mpris-pause-disable-rescan", |
| 305 |
before = "linking/rescan", |
| 306 |
interests = { |
| 307 |
EventInterest { |
| 308 |
Constraint { "event.type", "=", "rescan-for-linking" }, |
| 309 |
}, |
| 310 |
}, |
| 311 |
execute = function (event) |
| 312 |
if pending_ops > 0 then |
| 313 |
log:info ("rescan disabled, wait for pending operations") |
| 314 |
need_rescan = true |
| 315 |
event:stop_processing () |
| 316 |
else |
| 317 |
need_rescan = false |
| 318 |
end |
| 319 |
end |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
function updateEnabled () |
| 324 |
local enable = Settings.get_boolean ("linking.pause-playback") |
| 325 |
|
| 326 |
log:debug (string.format ("enabled: %s", tostring(enable))) |
| 327 |
|
| 328 |
if enable and not script_active then |
| 329 |
local source = Plugin.find ("standard-event-source") |
| 330 |
initializeState () |
| 331 |
initializeLinks (source) |
| 332 |
link_hook:register () |
| 333 |
pause_hook:register () |
| 334 |
rescan_hook:register () |
| 335 |
script_active = true |
| 336 |
elseif not enable and script_active then |
| 337 |
link_hook:remove () |
| 338 |
pause_hook:remove () |
| 339 |
rescan_hook:remove () |
| 340 |
if need_rescan then |
| 341 |
local source = Plugin.find ("standard-event-source") |
| 342 |
source:call ("push-event", "rescan-for-linking", nil, nil) |
| 343 |
end |
| 344 |
initializeState () |
| 345 |
end |
| 346 |
end |
| 347 |
|
| 348 |
Settings.subscribe ("linking.pause-playback", updateEnabled) |
| 349 |
updateEnabled () |