| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
-- |
| 7 |
-- Logic to "forward" the format set on special filter nodes to their |
| 8 |
-- virtual device peer node. This is for things like the "loopback" module, |
| 9 |
-- which always comes in pairs of 2 nodes, one stream and one virtual device. |
| 10 |
-- |
| 11 |
-- FIXME: this script can be further improved |
| 12 |
|
| 13 |
lutils = require ("linking-utils") |
| 14 |
log = Log.open_topic ("s-node") |
| 15 |
|
| 16 |
function findAssociatedLinkGroupNode (si) |
| 17 |
local si_props = si.properties |
| 18 |
local link_group = si_props ["node.link-group"] |
| 19 |
if link_group == nil then |
| 20 |
return nil |
| 21 |
end |
| 22 |
|
| 23 |
local std_event_source = Plugin.find ("standard-event-source") |
| 24 |
local om = std_event_source:call ("get-object-manager", "session-item") |
| 25 |
|
| 26 |
-- get the associated media class |
| 27 |
local assoc_direction = cutils.getTargetDirection (si_props) |
| 28 |
local assoc_media_class = si_props ["media.type"] .. |
| 29 |
(assoc_direction == "input" and "/Sink" or "/Source") |
| 30 |
|
| 31 |
-- find the linkable with same link group and matching assoc media class |
| 32 |
for assoc_si in om:iterate { type = "SiLinkable" } do |
| 33 |
local assoc_props = assoc_si.properties |
| 34 |
local assoc_link_group = assoc_props ["node.link-group"] |
| 35 |
if assoc_link_group == link_group and |
| 36 |
assoc_media_class == assoc_props ["media.class"] then |
| 37 |
return assoc_si |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
return nil |
| 42 |
end |
| 43 |
|
| 44 |
function onLinkGroupPortsStateChanged (si, old_state, new_state) |
| 45 |
local si_props = si.properties |
| 46 |
|
| 47 |
-- only handle items with configured ports state |
| 48 |
if new_state ~= "configured" then |
| 49 |
return |
| 50 |
end |
| 51 |
|
| 52 |
log:info (si, "ports format changed on " .. si_props ["node.name"]) |
| 53 |
|
| 54 |
-- find associated device |
| 55 |
local si_device = findAssociatedLinkGroupNode (si) |
| 56 |
if si_device ~= nil then |
| 57 |
local device_node_name = si_device.properties ["node.name"] |
| 58 |
|
| 59 |
-- get the stream format |
| 60 |
local f, m = si:get_ports_format () |
| 61 |
|
| 62 |
-- unregister the device |
| 63 |
log:info (si_device, "unregistering " .. device_node_name) |
| 64 |
si_device:remove () |
| 65 |
|
| 66 |
-- set new format in the device |
| 67 |
log:info (si_device, "setting new format in " .. device_node_name) |
| 68 |
si_device:set_ports_format (f, m, function (item, e) |
| 69 |
if e ~= nil then |
| 70 |
log:warning (item, "failed to configure ports in " .. |
| 71 |
device_node_name .. ": " .. e) |
| 72 |
end |
| 73 |
|
| 74 |
-- register back the device |
| 75 |
log:info (item, "registering " .. device_node_name) |
| 76 |
item:register () |
| 77 |
end) |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
SimpleEventHook { |
| 82 |
name = "node/filter-forward-format", |
| 83 |
interests = { |
| 84 |
EventInterest { |
| 85 |
Constraint { "event.type", "=", "session-item-added" }, |
| 86 |
Constraint { "event.session-item.interface", "=", "linkable" }, |
| 87 |
Constraint { "item.factory.name", "c", "si-audio-adapter", "si-node" }, |
| 88 |
Constraint { "media.class", "#", "Stream/*", type = "pw-global" }, |
| 89 |
}, |
| 90 |
}, |
| 91 |
execute = function (event) |
| 92 |
local si = event:get_subject () |
| 93 |
|
| 94 |
-- Forward filters ports format to associated virtual devices if enabled |
| 95 |
if Settings.get_boolean ("node.filter.forward-format") then |
| 96 |
local si_props = si.properties |
| 97 |
local link_group = si_props ["node.link-group"] |
| 98 |
local si_flags = lutils:get_flags (si.id) |
| 99 |
|
| 100 |
-- only listen for ports state changed on audio filter streams |
| 101 |
if si_flags.ports_state_signal ~= true and |
| 102 |
si_props ["item.factory.name"] == "si-audio-adapter" and |
| 103 |
si_props ["item.node.type"] == "stream" and |
| 104 |
link_group ~= nil then |
| 105 |
si:connect ("adapter-ports-state-changed", onLinkGroupPortsStateChanged) |
| 106 |
si_flags.ports_state_signal = true |
| 107 |
log:info (si, "listening ports state changed on " .. si_props ["node.name"]) |
| 108 |
end |
| 109 |
end |
| 110 |
end |
| 111 |
}:register () |