WIP: port create-item.lua to use event hooks
This commit is contained in:

committed by
Julian Bouzas

parent
d72f31e803
commit
fd91d8a35b
@@ -56,9 +56,42 @@ function configProperties(node)
|
|||||||
return properties
|
return properties
|
||||||
end
|
end
|
||||||
|
|
||||||
function addItem (node, item_type)
|
AsyncEventHook {
|
||||||
|
priority = 10,
|
||||||
|
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 id = node["bound-id"]
|
||||||
local item
|
local item
|
||||||
|
local item_type
|
||||||
|
|
||||||
|
local media_class = node.properties['media.class']
|
||||||
|
if string.find (media_class, "Audio") then
|
||||||
|
item_type = "si-audio-adapter"
|
||||||
|
else
|
||||||
|
item_type = "si-node"
|
||||||
|
end
|
||||||
|
|
||||||
-- create item
|
-- create item
|
||||||
item = SessionItem (item_type)
|
item = SessionItem (item_type)
|
||||||
@@ -66,62 +99,62 @@ function addItem (node, item_type)
|
|||||||
|
|
||||||
-- configure item
|
-- configure item
|
||||||
if not item:configure(configProperties(node)) then
|
if not item:configure(configProperties(node)) then
|
||||||
Log.warning(item, "failed to configure item for node " .. tostring(id))
|
transition:return_error("failed to configure item for node " .. tostring(id))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
item:register ()
|
|
||||||
|
|
||||||
-- 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
|
|
||||||
item:remove ()
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
Log.info(item, "activated item for node " .. tostring(id))
|
transition:advance()
|
||||||
|
|
||||||
-- Trigger object managers to update status
|
|
||||||
item:remove ()
|
|
||||||
if item["active-features"] ~= 0 then
|
|
||||||
item:register ()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end,
|
||||||
|
},
|
||||||
|
register = {
|
||||||
|
next = "none",
|
||||||
|
execute = function (event, transition)
|
||||||
|
local node = event:get_subject()
|
||||||
|
local id = node["bound-id"]
|
||||||
|
local item = items[id]
|
||||||
|
|
||||||
nodes_om = ObjectManager {
|
Log.info(item, "activated item for node " .. tostring(id))
|
||||||
Interest {
|
item:register ()
|
||||||
type = "node",
|
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" },
|
Constraint { "media.class", "#", "Stream/*", type = "pw-global" },
|
||||||
},
|
},
|
||||||
Interest {
|
EventInterest {
|
||||||
type = "node",
|
Constraint { "event.type", "=", "object-removed" },
|
||||||
|
Constraint { "event.subject.type", "=", "node" },
|
||||||
Constraint { "media.class", "#", "Video/*", type = "pw-global" },
|
Constraint { "media.class", "#", "Video/*", type = "pw-global" },
|
||||||
},
|
},
|
||||||
Interest {
|
EventInterest {
|
||||||
type = "node",
|
Constraint { "event.type", "=", "object-removed" },
|
||||||
|
Constraint { "event.subject.type", "=", "node" },
|
||||||
Constraint { "media.class", "#", "Audio/*", type = "pw-global" },
|
Constraint { "media.class", "#", "Audio/*", type = "pw-global" },
|
||||||
Constraint { "wireplumber.is-endpoint", "-", type = "pw" },
|
Constraint { "wireplumber.is-endpoint", "-", type = "pw" },
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
|
execute = function (_, event)
|
||||||
nodes_om:connect("object-added", function (om, node)
|
local node = event:get_subject()
|
||||||
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)
|
|
||||||
|
|
||||||
nodes_om:connect("object-removed", function (om, node)
|
|
||||||
local id = node["bound-id"]
|
local id = node["bound-id"]
|
||||||
if items[id] then
|
if items[id] then
|
||||||
items[id]:remove ()
|
items[id]:remove ()
|
||||||
items[id] = nil
|
items[id] = nil
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
}:register()
|
||||||
nodes_om:activate()
|
|
||||||
|
Reference in New Issue
Block a user