| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2023 Collabora Ltd. |
| 4 |
-- @author George Kiagiadakis <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
-- |
| 8 |
-- The script exposes a metadata object named "sm-objects" that clients can |
| 9 |
-- use to load objects into the WirePlumber daemon process. The objects are |
| 10 |
-- loaded as soon as the metadata is set and are destroyed when the metadata |
| 11 |
-- is cleared. |
| 12 |
-- |
| 13 |
-- To load an object, a client needs to set a metadata entry with: |
| 14 |
-- |
| 15 |
-- * subject: |
| 16 |
-- The ID of the owner of the object; you can use 0 here, but the |
| 17 |
-- idea is to be able to restrict which clients can change and/or |
| 18 |
-- delete these objects by using IDs of other objects appropriately |
| 19 |
-- |
| 20 |
-- * key: "<UNIQUE-OBJECT-NAME>" |
| 21 |
-- This is the name that will be used to identify the object. |
| 22 |
-- If an object with the same name already exists, it will be destroyed. |
| 23 |
-- Note that the keys are unique per subject, so you can have multiple |
| 24 |
-- objects with the same name as long as they are owned by different subjects. |
| 25 |
-- |
| 26 |
-- * type: "Spa:String:JSON" |
| 27 |
-- |
| 28 |
-- * value: "{ type = <object-type>, |
| 29 |
-- name = <object-name>, |
| 30 |
-- args = { ...object arguments... } }" |
| 31 |
-- The object type can be one of the following: |
| 32 |
-- - "pw-module": loads a pipewire module: `name` and `args` are interpreted |
| 33 |
-- just like a module entry in pipewire.conf |
| 34 |
-- - "metadata": loads a metadata object with `metadata.name` = `name` |
| 35 |
-- and any additional properties provided in `args` |
| 36 |
-- |
| 37 |
|
| 38 |
on_demand_objects = {} |
| 39 |
|
| 40 |
object_constructors = { |
| 41 |
["pw-module"] = LocalModule, |
| 42 |
["metadata"] = function (name, args) |
| 43 |
local m = ImplMetadata (name, args) |
| 44 |
m:activate (Features.ALL, function (m, e) |
| 45 |
if e then |
| 46 |
Log.warning ("failed to activate on-demand metadata `" .. name .. "`: " .. tostring (e)) |
| 47 |
end |
| 48 |
end) |
| 49 |
return m |
| 50 |
end |
| 51 |
} |
| 52 |
|
| 53 |
function handle_metadata_changed (m, subject, key, type, value) |
| 54 |
-- destroy all objects when metadata is cleared |
| 55 |
if not key then |
| 56 |
on_demand_objects = {} |
| 57 |
return |
| 58 |
end |
| 59 |
|
| 60 |
local object_id = key .. "@" .. tostring(subject) |
| 61 |
|
| 62 |
-- destroy existing object instance, if needed |
| 63 |
if on_demand_objects[object_id] then |
| 64 |
Log.debug("destroy on-demand object: " .. object_id) |
| 65 |
on_demand_objects[object_id] = nil |
| 66 |
end |
| 67 |
|
| 68 |
if value then |
| 69 |
local json = Json.Raw(value) |
| 70 |
if not json:is_object() then |
| 71 |
Log.warning("loading '".. object_id .. "' failed: expected JSON object, got: '" .. value .. "'") |
| 72 |
return |
| 73 |
end |
| 74 |
|
| 75 |
local obj = json:parse(1) |
| 76 |
if not obj.type then |
| 77 |
Log.warning("loading '".. object_id .. "' failed: no object type specified") |
| 78 |
return |
| 79 |
end |
| 80 |
if not obj.name then |
| 81 |
Log.warning("loading '".. object_id .. "' failed: no object name specified") |
| 82 |
return |
| 83 |
end |
| 84 |
|
| 85 |
local constructor = object_constructors[obj.type] |
| 86 |
if not constructor then |
| 87 |
Log.warning("loading '".. object_id .. "' failed: unknown object type: " .. obj.type) |
| 88 |
return |
| 89 |
end |
| 90 |
|
| 91 |
Log.info("load on-demand object: " .. object_id .. " -> " .. obj.name) |
| 92 |
on_demand_objects[object_id] = constructor(obj.name, obj.args) |
| 93 |
end |
| 94 |
end |
| 95 |
|
| 96 |
objects_metadata = ImplMetadata ("sm-objects") |
| 97 |
objects_metadata:activate (Features.ALL, function (m, e) |
| 98 |
if e then |
| 99 |
Log.warning ("failed to activate the sm-objects metadata: " .. tostring (e)) |
| 100 |
else |
| 101 |
m:connect("changed", handle_metadata_changed) |
| 102 |
end |
| 103 |
end) |