Debian Codegraph @

src/scripts/fallback-sink.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2021 Collabora Ltd.
4 -- @author Frédéric Danis <[email protected]>
5 --
6 -- SPDX-License-Identifier: MIT
7
8 sink_ids = {}
9 fallback_node = nil
10
11 node_om = ObjectManager {
12 Interest {
13 type = "node",
14 Constraint { "media.class", "matches", "Audio/Sink", type = "pw-global" },
15 -- Do not consider virtual items created by WirePlumber
16 Constraint { "wireplumber.is-virtual", "!", true, type = "pw" },
17 -- or the fallback sink itself
18 Constraint { "wireplumber.is-fallback", "!", true, type = "pw" },
19 }
20 }
21
22 function createFallbackSink()
23 if fallback_node then
24 return
25 end
26
27 Log.info("Create fallback sink")
28
29 local properties = {}
30
31 properties["node.name"] = "auto_null"
32 properties["node.description"] = "Dummy Output"
33
34 properties["audio.rate"] = 48000
35 properties["audio.channels"] = 2
36 properties["audio.position"] = "FL,FR"
37
38 properties["media.class"] = "Audio/Sink"
39 properties["factory.name"] = "support.null-audio-sink"
40 properties["node.virtual"] = "true"
41 properties["monitor.channel-volumes"] = "true"
42
43 properties["wireplumber.is-fallback"] = "true"
44 properties["priority.session"] = 500
45
46 fallback_node = LocalNode("adapter", properties)
47 fallback_node:activate(Feature.Proxy.BOUND)
48 end
49
50 function checkSinks()
51 local sink_ids_items = 0
52 for _ in pairs(sink_ids) do sink_ids_items = sink_ids_items + 1 end
53
54 if sink_ids_items > 0 then
55 if fallback_node then
56 Log.info("Remove fallback sink")
57 fallback_node = nil
58 end
59 elseif not fallback_node then
60 createFallbackSink()
61 end
62 end
63
64 function checkSinksAfterTimeout()
65 if timeout_source then
66 timeout_source:destroy()
67 end
68 timeout_source = Core.timeout_add(1000, function ()
69 checkSinks()
70 timeout_source = nil
71 end)
72 end
73
74 node_om:connect("object-added", function (_, node)
75 Log.debug("object added: " .. node.properties["object.id"] .. " " ..
76 tostring(node.properties["node.name"]))
77
78 sink_ids[node.properties["object.id"]] = node.properties["node.name"]
79
80 checkSinksAfterTimeout()
81 end)
82
83 node_om:connect("object-removed", function (_, node)
84 Log.debug("object removed: " .. node.properties["object.id"] .. " " ..
85 tostring(node.properties["node.name"]))
86
87 sink_ids[node.properties["object.id"]] = nil
88 checkSinksAfterTimeout()
89 end)
90
91 node_om:activate()
92
93 checkSinksAfterTimeout()