| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2023 Collabora Ltd. |
| 4 |
-- @author Ashok Sidipotu <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
mutils = require ("monitor-utils") |
| 9 |
|
| 10 |
log = Log.open_topic ("s-monitors-v4l2") |
| 11 |
|
| 12 |
SimpleEventHook { |
| 13 |
name = "monitor/v4l2/name-node", |
| 14 |
interests = { |
| 15 |
EventInterest { |
| 16 |
Constraint { "event.type", "=", "create-v4l2-device-node" }, |
| 17 |
}, |
| 18 |
}, |
| 19 |
execute = function(event) |
| 20 |
local properties = event:get_data ("node-properties") |
| 21 |
local parent = event:get_subject () |
| 22 |
local dev_props = parent.properties |
| 23 |
local factory = event:get_data ("factory") |
| 24 |
local id = event:get_data ("node-sub-id") |
| 25 |
|
| 26 |
-- set the device id and spa factory name; REQUIRED, do not change |
| 27 |
properties["device.id"] = parent["bound-id"] |
| 28 |
properties["factory.name"] = factory |
| 29 |
|
| 30 |
-- set the default pause-on-idle setting |
| 31 |
properties["node.pause-on-idle"] = false |
| 32 |
|
| 33 |
-- set the node name |
| 34 |
local name = |
| 35 |
(factory:find ("sink") and "v4l2_output") or |
| 36 |
(factory:find ("source") and "v4l2_input" or factory) |
| 37 |
.. "." .. |
| 38 |
(dev_props["device.name"]:gsub ("^v4l2_device%.(.+)", "%1") or |
| 39 |
dev_props["device.name"] or |
| 40 |
dev_props["device.nick"] or |
| 41 |
dev_props["device.alias"] or |
| 42 |
"v4l2-device") |
| 43 |
-- sanitize name |
| 44 |
name = name:gsub ("([^%w_%-%.])", "_") |
| 45 |
|
| 46 |
properties["node.name"] = name |
| 47 |
|
| 48 |
-- deduplicate nodes with the same name |
| 49 |
for counter = 2, 99, 1 do |
| 50 |
if mutils.find_duplicate (parent, id, "node.name", properties["node.name"]) then |
| 51 |
properties["node.name"] = name .. "." .. counter |
| 52 |
else |
| 53 |
break |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
-- set the node description |
| 58 |
local desc = dev_props["device.description"] or "v4l2-device" |
| 59 |
desc = desc .. " (V4L2)" |
| 60 |
-- sanitize description, replace ':' with ' ' |
| 61 |
properties["node.description"] = desc:gsub ("(:)", " ") |
| 62 |
|
| 63 |
-- set the node nick |
| 64 |
local nick = properties["node.nick"] or |
| 65 |
dev_props["device.product.name"] or |
| 66 |
dev_props["api.v4l2.cap.card"] or |
| 67 |
dev_props["device.description"] or |
| 68 |
dev_props["device.nick"] |
| 69 |
properties["node.nick"] = nick:gsub ("(:)", " ") |
| 70 |
|
| 71 |
-- set priority |
| 72 |
if not properties["priority.session"] then |
| 73 |
local path = properties["api.v4l2.path"] or "/dev/video100" |
| 74 |
local dev = path:gsub ("/dev/video(%d+)", "%1") |
| 75 |
properties["priority.session"] = 1000 - (tonumber (dev) * 10) |
| 76 |
end |
| 77 |
|
| 78 |
event:set_data ("node-properties", properties) |
| 79 |
end |
| 80 |
}:register () |