| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- |
| 5 |
-- SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
-- looks for changes in user-preferences and devices added/removed and schedules |
| 8 |
-- rescan and pushes "select-default-node" event for each of the media_classes |
| 9 |
|
| 10 |
lutils = require ("linking-utils") |
| 11 |
|
| 12 |
log = Log.open_topic ("s-default-nodes") |
| 13 |
|
| 14 |
-- looks for changes in user-preferences and devices added/removed and schedules |
| 15 |
-- rescan |
| 16 |
SimpleEventHook { |
| 17 |
name = "default-nodes/rescan-trigger", |
| 18 |
interests = { |
| 19 |
EventInterest { |
| 20 |
Constraint { "event.type", "c", "session-item-added", "session-item-removed" }, |
| 21 |
Constraint { "event.session-item.interface", "=", "linkable" }, |
| 22 |
Constraint { "media.class", "#", "Audio/*" }, |
| 23 |
}, |
| 24 |
EventInterest { |
| 25 |
Constraint { "event.type", "c", "session-item-added", "session-item-removed" }, |
| 26 |
Constraint { "event.session-item.interface", "=", "linkable" }, |
| 27 |
Constraint { "media.class", "#", "Video/*" }, |
| 28 |
}, |
| 29 |
EventInterest { |
| 30 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 31 |
Constraint { "metadata.name", "=", "default" }, |
| 32 |
Constraint { "event.subject.key", "c", "default.configured.audio.sink", |
| 33 |
"default.configured.audio.source", "default.configured.video.source" |
| 34 |
}, |
| 35 |
}, |
| 36 |
EventInterest { |
| 37 |
Constraint { "event.type", "=", "device-params-changed"}, |
| 38 |
Constraint { "event.subject.param-id", "c", "Route", "EnumRoute"}, |
| 39 |
}, |
| 40 |
}, |
| 41 |
execute = function (event) |
| 42 |
local source = event:get_source () |
| 43 |
source:call ("schedule-rescan", "default-nodes") |
| 44 |
end |
| 45 |
}:register () |
| 46 |
|
| 47 |
-- pushes "select-default-node" event for each of the media_classes |
| 48 |
SimpleEventHook { |
| 49 |
name = "default-nodes/rescan", |
| 50 |
interests = { |
| 51 |
EventInterest { |
| 52 |
Constraint { "event.type", "=", "rescan-for-default-nodes" }, |
| 53 |
}, |
| 54 |
}, |
| 55 |
execute = function (event) |
| 56 |
local source = event:get_source () |
| 57 |
local si_om = source:call ("get-object-manager", "session-item") |
| 58 |
local devices_om = source:call ("get-object-manager", "device") |
| 59 |
|
| 60 |
log:trace ("re-evaluating default nodes") |
| 61 |
|
| 62 |
-- Audio Sink |
| 63 |
pushSelectDefaultNodeEvent (source, si_om, devices_om, "audio.sink", "in", { |
| 64 |
"Audio/Sink", "Audio/Duplex" |
| 65 |
}) |
| 66 |
|
| 67 |
-- Audio Source |
| 68 |
pushSelectDefaultNodeEvent (source, si_om, devices_om, "audio.source", "out", { |
| 69 |
"Audio/Source", "Audio/Source/Virtual", "Audio/Duplex", "Audio/Sink" |
| 70 |
}) |
| 71 |
|
| 72 |
-- Video Source |
| 73 |
pushSelectDefaultNodeEvent (source, si_om, devices_om, "video.source", "out", { |
| 74 |
"Video/Source", "Video/Source/Virtual" |
| 75 |
}) |
| 76 |
end |
| 77 |
}:register () |
| 78 |
|
| 79 |
function pushSelectDefaultNodeEvent (source, si_om, devices_om, def_node_type, |
| 80 |
port_direction, media_classes) |
| 81 |
local nodes = |
| 82 |
collectAvailableNodes (si_om, devices_om, port_direction, media_classes) |
| 83 |
local event = source:call ("create-event", "select-default-node", nil, { |
| 84 |
["default-node.type"] = def_node_type, |
| 85 |
}) |
| 86 |
event:set_data ("available-nodes", Json.Array (nodes)) |
| 87 |
EventDispatcher.push_event (event) |
| 88 |
end |
| 89 |
|
| 90 |
-- Return an array table where each element is another table containing all the |
| 91 |
-- node properties of all the nodes that can be selected for a given media class |
| 92 |
-- set and direction |
| 93 |
function collectAvailableNodes (si_om, devices_om, port_direction, media_classes) |
| 94 |
local collected = {} |
| 95 |
|
| 96 |
for linkable in si_om:iterate { |
| 97 |
type = "SiLinkable", |
| 98 |
Constraint { "media.class", "c", table.unpack (media_classes) }, |
| 99 |
} do |
| 100 |
local node = linkable:get_associated_proxy ("node") |
| 101 |
local node_props = node.properties |
| 102 |
|
| 103 |
-- check that the node has ports in the requested direction |
| 104 |
if not node:lookup_port { |
| 105 |
Constraint { "port.direction", "=", port_direction } |
| 106 |
} then |
| 107 |
goto next_linkable |
| 108 |
end |
| 109 |
|
| 110 |
-- check that the node has available routes, |
| 111 |
-- if it is associated to a real device |
| 112 |
if not lutils.haveAvailableRoutes (node_props, devices_om) then |
| 113 |
goto next_linkable |
| 114 |
end |
| 115 |
|
| 116 |
table.insert (collected, Json.Object (node_props)) |
| 117 |
|
| 118 |
::next_linkable:: |
| 119 |
end |
| 120 |
|
| 121 |
return collected |
| 122 |
end |