2021-03-23 15:08:03 -04:00
|
|
|
-- WirePlumber
|
|
|
|
|
--
|
|
|
|
|
-- Copyright © 2021 Collabora Ltd.
|
|
|
|
|
-- @author Julian Bouzas <julian.bouzas@collabora.com>
|
|
|
|
|
--
|
|
|
|
|
-- SPDX-License-Identifier: MIT
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
-- create-item.lua script takes pipewire nodes and creates session items (a.k.a
|
2022-06-20 18:15:04 +05:30
|
|
|
-- linkable) objects out of them.
|
|
|
|
|
|
2021-03-23 15:08:03 -04:00
|
|
|
items = {}
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
function configProperties (node)
|
2021-10-08 00:09:42 +03:00
|
|
|
local np = node.properties
|
|
|
|
|
local properties = {
|
|
|
|
|
["item.node"] = node,
|
2022-07-13 06:25:51 +05:30
|
|
|
["item.plugged.usec"] = GLib.get_monotonic_time (),
|
2022-05-19 10:12:38 +05:30
|
|
|
["item.features.no-dsp"] =
|
2022-08-18 13:11:34 -04:00
|
|
|
Settings.get ("default-policy-audio.no-dsp"):parse(),
|
2021-10-08 00:09:42 +03:00
|
|
|
["item.features.monitor"] = true,
|
|
|
|
|
["item.features.control-port"] = false,
|
2022-07-13 06:25:51 +05:30
|
|
|
["node.id"] = node ["bound-id"],
|
|
|
|
|
["client.id"] = np ["client.id"],
|
|
|
|
|
["object.path"] = np ["object.path"],
|
|
|
|
|
["object.serial"] = np ["object.serial"],
|
|
|
|
|
["target.object"] = np ["target.object"],
|
|
|
|
|
["priority.session"] = np ["priority.session"],
|
|
|
|
|
["device.id"] = np ["device.id"],
|
|
|
|
|
["card.profile.device"] = np ["card.profile.device"],
|
2021-10-08 00:09:42 +03:00
|
|
|
}
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
for k, v in pairs (np) do
|
|
|
|
|
if k:find ("^node") or k:find ("^stream") or k:find ("^media") then
|
|
|
|
|
properties [k] = v
|
2021-10-08 00:09:42 +03:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
local media_class = properties ["media.class"] or ""
|
2021-10-08 00:09:42 +03:00
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
if not properties ["media.type"] then
|
|
|
|
|
for _, i in ipairs ({ "Audio", "Video", "Midi" }) do
|
|
|
|
|
if media_class:find (i) then
|
|
|
|
|
properties ["media.type"] = i
|
2021-10-08 00:09:42 +03:00
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
properties ["item.node.type"] =
|
|
|
|
|
media_class:find ("^Stream/") and "stream" or "device"
|
2021-10-08 00:09:42 +03:00
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
if media_class:find ("Sink") or
|
|
|
|
|
media_class:find ("Input") or
|
|
|
|
|
media_class:find ("Duplex") then
|
|
|
|
|
properties ["item.node.direction"] = "input"
|
|
|
|
|
elseif media_class:find ("Source") or media_class:find ("Output") then
|
|
|
|
|
properties ["item.node.direction"] = "output"
|
2021-10-08 00:09:42 +03:00
|
|
|
end
|
|
|
|
|
return properties
|
|
|
|
|
end
|
|
|
|
|
|
2022-05-20 17:43:43 +03:00
|
|
|
AsyncEventHook {
|
2022-07-15 09:57:01 +05:30
|
|
|
name = "node-added@create-item",
|
2022-07-06 05:17:43 +05:30
|
|
|
type = "on-event",
|
|
|
|
|
priority = "node-added-create-item",
|
2022-05-20 17:43:43 +03:00
|
|
|
interests = {
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-added" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Stream/*", type = "pw-global" },
|
|
|
|
|
},
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-added" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Video/*", type = "pw-global" },
|
|
|
|
|
},
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-added" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Audio/*", type = "pw-global" },
|
|
|
|
|
Constraint { "wireplumber.is-endpoint", "-", type = "pw" },
|
|
|
|
|
},
|
2021-06-10 16:38:34 +03:00
|
|
|
},
|
2022-05-20 17:43:43 +03:00
|
|
|
steps = {
|
|
|
|
|
start = {
|
|
|
|
|
next = "register",
|
|
|
|
|
execute = function (event, transition)
|
2022-07-13 06:25:51 +05:30
|
|
|
local node = event:get_subject ()
|
|
|
|
|
local id = node ["bound-id"]
|
2022-05-20 17:43:43 +03:00
|
|
|
local item
|
|
|
|
|
local item_type
|
|
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
local media_class = node.properties ['media.class']
|
2022-05-20 17:43:43 +03:00
|
|
|
if string.find (media_class, "Audio") then
|
|
|
|
|
item_type = "si-audio-adapter"
|
|
|
|
|
else
|
|
|
|
|
item_type = "si-node"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- create item
|
|
|
|
|
item = SessionItem (item_type)
|
2022-07-13 06:25:51 +05:30
|
|
|
items [id] = item
|
2022-05-20 17:43:43 +03:00
|
|
|
|
|
|
|
|
-- configure item
|
2022-07-13 06:25:51 +05:30
|
|
|
if not item:configure (configProperties (node)) then
|
|
|
|
|
transition:return_error ("failed to configure item for node "
|
|
|
|
|
.. tostring (id))
|
2022-05-20 17:43:43 +03:00
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- activate item
|
|
|
|
|
item:activate (Features.ALL, function (_, e)
|
|
|
|
|
if e then
|
2022-07-13 06:25:51 +05:30
|
|
|
transition:return_error ("failed to activate item: "
|
|
|
|
|
.. tostring (e));
|
2022-05-20 17:43:43 +03:00
|
|
|
else
|
2022-07-13 06:25:51 +05:30
|
|
|
transition:advance ()
|
2022-05-20 17:43:43 +03:00
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end,
|
|
|
|
|
},
|
|
|
|
|
register = {
|
|
|
|
|
next = "none",
|
|
|
|
|
execute = function (event, transition)
|
2022-07-13 06:25:51 +05:30
|
|
|
local node = event:get_subject ()
|
|
|
|
|
local id = node ["bound-id"]
|
|
|
|
|
local item = items [id]
|
2022-05-20 17:43:43 +03:00
|
|
|
|
2022-07-13 06:25:51 +05:30
|
|
|
Log.info (item, "activated item for node " .. tostring (id))
|
2022-05-20 17:43:43 +03:00
|
|
|
item:register ()
|
2022-07-13 06:25:51 +05:30
|
|
|
transition:advance ()
|
2022-05-20 17:43:43 +03:00
|
|
|
|
|
|
|
|
props = {}
|
2022-07-13 06:25:51 +05:30
|
|
|
props ["event.subject.type"] = "linkable"
|
2022-05-20 17:43:43 +03:00
|
|
|
EventDispatcher.push_event { type = "object-added",
|
2022-08-10 11:32:31 +05:30
|
|
|
priority = 50, properties = props, subject = item }
|
2022-05-20 17:43:43 +03:00
|
|
|
end,
|
|
|
|
|
},
|
2021-06-10 16:38:34 +03:00
|
|
|
},
|
2022-07-13 06:25:51 +05:30
|
|
|
}:register ()
|
2022-05-20 17:43:43 +03:00
|
|
|
|
|
|
|
|
SimpleEventHook {
|
2022-07-15 09:57:01 +05:30
|
|
|
name = "node-removed@create-item",
|
2022-07-06 05:17:43 +05:30
|
|
|
type = "on-event",
|
|
|
|
|
priority = "node-removed-create-item",
|
2022-05-20 17:43:43 +03:00
|
|
|
interests = {
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-removed" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Stream/*", type = "pw-global" },
|
|
|
|
|
},
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-removed" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Video/*", type = "pw-global" },
|
|
|
|
|
},
|
|
|
|
|
EventInterest {
|
|
|
|
|
Constraint { "event.type", "=", "object-removed" },
|
|
|
|
|
Constraint { "event.subject.type", "=", "node" },
|
|
|
|
|
Constraint { "media.class", "#", "Audio/*", type = "pw-global" },
|
|
|
|
|
Constraint { "wireplumber.is-endpoint", "-", type = "pw" },
|
|
|
|
|
},
|
2021-06-10 16:38:34 +03:00
|
|
|
},
|
2022-07-13 06:27:08 +05:30
|
|
|
execute = function (event)
|
2022-07-13 06:25:51 +05:30
|
|
|
local node = event:get_subject ()
|
|
|
|
|
local id = node ["bound-id"]
|
|
|
|
|
if items [id] then
|
|
|
|
|
items [id]:remove ()
|
|
|
|
|
items [id] = nil
|
2022-05-20 17:43:43 +03:00
|
|
|
end
|
2022-05-20 17:43:43 +03:00
|
|
|
|
|
|
|
|
props = {}
|
2022-07-13 06:25:51 +05:30
|
|
|
props ["event.subject.type"] = "linkable"
|
2022-05-20 17:43:43 +03:00
|
|
|
EventDispatcher.push_event { type = "object-removed",
|
2022-07-13 06:25:51 +05:30
|
|
|
priority = 50, properties = props, subject = item }
|
2021-03-23 15:08:03 -04:00
|
|
|
end
|
2022-07-13 06:25:51 +05:30
|
|
|
}:register ()
|