WIP: port create-item.lua to use event hooks

This commit is contained in:
George Kiagiadakis
2022-05-20 17:43:43 +03:00
committed by Julian Bouzas
parent d72f31e803
commit fd91d8a35b

View File

@@ -56,72 +56,105 @@ function configProperties(node)
return properties return properties
end end
function addItem (node, item_type) AsyncEventHook {
local id = node["bound-id"] priority = 10,
local item type = "on-event",
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" },
},
},
steps = {
start = {
next = "register",
execute = function (event, transition)
local node = event:get_subject()
local id = node["bound-id"]
local item
local item_type
-- create item local media_class = node.properties['media.class']
item = SessionItem ( item_type ) if string.find (media_class, "Audio") then
items[id] = item item_type = "si-audio-adapter"
else
item_type = "si-node"
end
-- configure item -- create item
if not item:configure(configProperties(node)) then item = SessionItem (item_type)
Log.warning(item, "failed to configure item for node " .. tostring(id)) items[id] = item
return
end
item:register () -- configure item
if not item:configure(configProperties(node)) then
transition:return_error("failed to configure item for node " .. tostring(id))
return
end
-- activate item -- activate item
items[id]:activate (Features.ALL, function (item, e) item:activate (Features.ALL, function (_, e)
if e then if e then
Log.message(item, "failed to activate item: " .. tostring(e)); transition:return_error("failed to activate item: " .. tostring(e));
if item then else
item:remove () transition:advance()
end end
else end)
Log.info(item, "activated item for node " .. tostring(id)) end,
},
register = {
next = "none",
execute = function (event, transition)
local node = event:get_subject()
local id = node["bound-id"]
local item = items[id]
-- Trigger object managers to update status Log.info(item, "activated item for node " .. tostring(id))
item:remove ()
if item["active-features"] ~= 0 then
item:register () item:register ()
end transition:advance()
end,
},
},
}:register()
SimpleEventHook {
priority = 10,
type = "on-event",
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" },
},
},
execute = function (_, event)
local node = event:get_subject()
local id = node["bound-id"]
if items[id] then
items[id]:remove ()
items[id] = nil
end end
end)
end
nodes_om = ObjectManager {
Interest {
type = "node",
Constraint { "media.class", "#", "Stream/*", type = "pw-global" },
},
Interest {
type = "node",
Constraint { "media.class", "#", "Video/*", type = "pw-global" },
},
Interest {
type = "node",
Constraint { "media.class", "#", "Audio/*", type = "pw-global" },
Constraint { "wireplumber.is-endpoint", "-", type = "pw" },
},
}
nodes_om:connect("object-added", function (om, node)
local media_class = node.properties['media.class']
if string.find (media_class, "Audio") then
addItem (node, "si-audio-adapter")
else
addItem (node, "si-node")
end end
end) }:register()
nodes_om:connect("object-removed", function (om, node)
local id = node["bound-id"]
if items[id] then
items[id]:remove ()
items[id] = nil
end
end)
nodes_om:activate()