| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2021 Collabora Ltd. |
| 4 |
-- @author Julian Bouzas <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
-- create-item.lua script takes pipewire nodes and creates session items (a.k.a |
| 9 |
-- linkable) objects out of them. |
| 10 |
|
| 11 |
cutils = require ("common-utils") |
| 12 |
log = Log.open_topic ("s-node") |
| 13 |
|
| 14 |
items = {} |
| 15 |
|
| 16 |
function configProperties (node) |
| 17 |
local properties = node.properties |
| 18 |
local media_class = properties ["media.class"] or "" |
| 19 |
local factory_name = properties ["factory.name"] or "" |
| 20 |
|
| 21 |
-- ensure a media.type is set |
| 22 |
if not properties ["media.type"] then |
| 23 |
for _, i in ipairs ({ "Audio", "Video", "Midi" }) do |
| 24 |
if media_class:find (i) then |
| 25 |
properties ["media.type"] = i |
| 26 |
break |
| 27 |
end |
| 28 |
end |
| 29 |
end |
| 30 |
|
| 31 |
properties ["item.node"] = node |
| 32 |
properties ["item.node.direction"] = |
| 33 |
cutils.mediaClassToDirection (media_class) |
| 34 |
properties ["item.node.type"] = |
| 35 |
media_class:find ("^Stream/") and "stream" or "device" |
| 36 |
properties ["item.plugged.usec"] = GLib.get_monotonic_time () |
| 37 |
properties ["item.features.no-dsp"] = |
| 38 |
Settings.get_boolean ("node.features.audio.no-dsp") |
| 39 |
properties ["item.features.monitor"] = |
| 40 |
Settings.get_boolean ("node.features.audio.monitor-ports") |
| 41 |
properties ["item.features.control-port"] = |
| 42 |
Settings.get_boolean ("node.features.audio.control-port") |
| 43 |
properties ["item.features.mono"] = |
| 44 |
(factory_name == "api.alsa.pcm.sink" or factory_name == "api.bluez5.a2dp.sink") and |
| 45 |
Settings.get_boolean ("node.features.audio.mono") |
| 46 |
properties ["node.id"] = node ["bound-id"] |
| 47 |
|
| 48 |
-- set the default media.role, if configured |
| 49 |
-- avoid Settings.get_string(), as it will parse the default "null" value |
| 50 |
-- as a string instead of returning nil |
| 51 |
local default_role = Settings.get ("node.stream.default-media-role") |
| 52 |
if default_role then |
| 53 |
default_role = default_role:parse() |
| 54 |
properties ["media.role"] = properties ["media.role"] or default_role |
| 55 |
end |
| 56 |
|
| 57 |
return properties |
| 58 |
end |
| 59 |
|
| 60 |
AsyncEventHook { |
| 61 |
name = "node/create-item", |
| 62 |
interests = { |
| 63 |
EventInterest { |
| 64 |
Constraint { "event.type", "=", "node-added" }, |
| 65 |
Constraint { "media.class", "#", "Stream/*", type = "pw-global" }, |
| 66 |
}, |
| 67 |
EventInterest { |
| 68 |
Constraint { "event.type", "=", "node-added" }, |
| 69 |
Constraint { "media.class", "#", "Video/*", type = "pw-global" }, |
| 70 |
}, |
| 71 |
EventInterest { |
| 72 |
Constraint { "event.type", "=", "node-added" }, |
| 73 |
Constraint { "media.class", "#", "Audio/*", type = "pw-global" }, |
| 74 |
Constraint { "wireplumber.is-virtual", "-", type = "pw" }, |
| 75 |
}, |
| 76 |
}, |
| 77 |
steps = { |
| 78 |
start = { |
| 79 |
next = "register", |
| 80 |
execute = function (event, transition) |
| 81 |
local node = event:get_subject () |
| 82 |
local id = node.id |
| 83 |
local item |
| 84 |
local item_type |
| 85 |
|
| 86 |
local media_class = node.properties ['media.class'] |
| 87 |
if string.find (media_class, "Audio") then |
| 88 |
item_type = "si-audio-adapter" |
| 89 |
else |
| 90 |
item_type = "si-node" |
| 91 |
end |
| 92 |
|
| 93 |
log:info (node, "creating item for node -> " .. item_type) |
| 94 |
|
| 95 |
-- create item |
| 96 |
item = SessionItem (item_type) |
| 97 |
items [id] = item |
| 98 |
|
| 99 |
-- configure item |
| 100 |
if not item:configure (configProperties (node)) then |
| 101 |
transition:return_error ("failed to configure item for node " |
| 102 |
.. tostring (id)) |
| 103 |
return |
| 104 |
end |
| 105 |
|
| 106 |
-- activate item |
| 107 |
item:activate (Features.ALL, function (_, e) |
| 108 |
if e then |
| 109 |
transition:return_error ("failed to activate item: " |
| 110 |
.. tostring (e)); |
| 111 |
else |
| 112 |
transition:advance () |
| 113 |
end |
| 114 |
end) |
| 115 |
end, |
| 116 |
}, |
| 117 |
register = { |
| 118 |
next = "none", |
| 119 |
execute = function (event, transition) |
| 120 |
local node = event:get_subject () |
| 121 |
local bound_id = node ["bound-id"] |
| 122 |
local item = items [node.id] |
| 123 |
|
| 124 |
log:info (item, "activated item for node " .. tostring (bound_id)) |
| 125 |
item:register () |
| 126 |
transition:advance () |
| 127 |
end, |
| 128 |
}, |
| 129 |
}, |
| 130 |
}:register () |
| 131 |
|
| 132 |
SimpleEventHook { |
| 133 |
name = "node/destroy-item", |
| 134 |
interests = { |
| 135 |
EventInterest { |
| 136 |
Constraint { "event.type", "=", "node-removed" }, |
| 137 |
Constraint { "media.class", "#", "Stream/*", type = "pw-global" }, |
| 138 |
}, |
| 139 |
EventInterest { |
| 140 |
Constraint { "event.type", "=", "node-removed" }, |
| 141 |
Constraint { "media.class", "#", "Video/*", type = "pw-global" }, |
| 142 |
}, |
| 143 |
EventInterest { |
| 144 |
Constraint { "event.type", "=", "node-removed" }, |
| 145 |
Constraint { "media.class", "#", "Audio/*", type = "pw-global" }, |
| 146 |
Constraint { "wireplumber.is-virtual", "-", type = "pw" }, |
| 147 |
}, |
| 148 |
}, |
| 149 |
execute = function (event) |
| 150 |
local node = event:get_subject () |
| 151 |
local id = node.id |
| 152 |
if items [id] then |
| 153 |
items [id]:remove () |
| 154 |
items [id] = nil |
| 155 |
end |
| 156 |
|
| 157 |
end |
| 158 |
}:register () |
| 159 |
|
| 160 |
function reconfigureAudioAdapters () |
| 161 |
local ids = {} |
| 162 |
|
| 163 |
-- Get the Id of all session items that are audio adapters |
| 164 |
for id, item in pairs(items) do |
| 165 |
local si_props = item.properties |
| 166 |
if si_props ["item.factory.name"] == "si-audio-adapter" then |
| 167 |
table.insert (ids, id) |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
-- Re-configure all audio adapters |
| 172 |
for _, id in pairs (ids) do |
| 173 |
local item = items[id] |
| 174 |
local node = item:get_associated_proxy ("node") |
| 175 |
|
| 176 |
log:info (item, "Started re-configuring audio adapter") |
| 177 |
|
| 178 |
-- Remove the session item so that it is unlinked |
| 179 |
items[id] = nil |
| 180 |
item:remove() |
| 181 |
|
| 182 |
-- Configure the session item |
| 183 |
if not item:configure (configProperties (node)) then |
| 184 |
log:warning (item, "Could not re-configure audio adapter") |
| 185 |
goto skip_item |
| 186 |
end |
| 187 |
|
| 188 |
-- Activate the session item so that it is linked again |
| 189 |
items[id] = item |
| 190 |
item:activate (Features.ALL, function (si, e) |
| 191 |
if e then |
| 192 |
log:warning (si, "Could not re-activate audio adapter") |
| 193 |
else |
| 194 |
log:info (si, "Successfully re-activated audio adapter") |
| 195 |
si:register () |
| 196 |
end |
| 197 |
end) |
| 198 |
|
| 199 |
::skip_item:: |
| 200 |
end |
| 201 |
end |
| 202 |
|
| 203 |
Settings.subscribe ("node.features.audio.*", function () |
| 204 |
reconfigureAudioAdapters () |
| 205 |
end) |