mirror of
https://gitlab.freedesktop.org/pipewire/wireplumber.git
synced 2026-05-05 12:28:02 +02:00
monitor-v4l2: copy properties and rules logic from media-session
This commit is contained in:
parent
989708a880
commit
100e6dc360
1 changed files with 96 additions and 13 deletions
|
|
@ -5,21 +5,86 @@
|
|||
--
|
||||
-- SPDX-License-Identifier: MIT
|
||||
|
||||
local config = ... or {}
|
||||
|
||||
-- preprocess rules and create Interest objects
|
||||
for _, r in ipairs(config.rules or {}) do
|
||||
r.interests = {}
|
||||
for _, i in ipairs(r.matches) do
|
||||
local interest_desc = { type = "properties" }
|
||||
for _, c in ipairs(i) do
|
||||
c.type = "pw"
|
||||
table.insert(interest_desc, Constraint(c))
|
||||
end
|
||||
local interest = Interest(interest_desc)
|
||||
table.insert(r.interests, interest)
|
||||
end
|
||||
r.matches = nil
|
||||
end
|
||||
|
||||
-- applies properties from config.rules when asked to
|
||||
function rulesApplyProperties(properties)
|
||||
for _, r in ipairs(config.rules or {}) do
|
||||
if r.apply_properties then
|
||||
for _, interest in ipairs(r.interests) do
|
||||
if interest:matches(properties) then
|
||||
for k, v in pairs(r.apply_properties) do
|
||||
properties[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function findDuplicate(parent, id, property, value)
|
||||
for i = 0, id - 1, 1 do
|
||||
local obj = parent:get_managed_object(i)
|
||||
if obj and obj.properties[property] == value then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function createNode(parent, id, type, factory, properties)
|
||||
local dev_props = parent.properties
|
||||
|
||||
-- ensure the node has a name and description
|
||||
local devname = dev_props["device.name"]
|
||||
or dev_props["device.nick"]
|
||||
or dev_props["device.alias"]
|
||||
or "v4l2-device"
|
||||
properties["node.name"] = factory .. "." .. devname
|
||||
properties["node.description"] = dev_props["device.description"] or devname
|
||||
|
||||
-- set the device id and spa factory name; REQUIRED, do not change
|
||||
properties["device.id"] = parent["bound-id"]
|
||||
properties["factory.name"] = factory
|
||||
|
||||
-- set the default pause-on-idle setting
|
||||
properties["node.pause-on-idle"] = false
|
||||
|
||||
-- set the node name
|
||||
local name =
|
||||
(factory:find("sink") and "v4l2_output") or
|
||||
(factory:find("source") and "v4l2_input" or factory)
|
||||
.. "." ..
|
||||
(dev_props["device.name"]:gsub("^v4l2_device%.(.+)", "%1") or
|
||||
dev_props["device.name"] or
|
||||
dev_props["device.nick"] or
|
||||
dev_props["device.alias"] or
|
||||
"v4l2-device")
|
||||
|
||||
properties["node.name"] = name
|
||||
|
||||
-- deduplicate nodes with the same name
|
||||
for counter = 2, 99, 1 do
|
||||
if findDuplicate(parent, id, "node.name", properties["node.name"]) then
|
||||
properties["node.name"] = name .. "." .. counter
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- set the node description
|
||||
properties["node.description"] = dev_props["device.description"] or "v4l2-device"
|
||||
|
||||
-- apply properties from config.rules
|
||||
rulesApplyProperties(properties)
|
||||
|
||||
-- create the node
|
||||
local node = Node("spa-node-factory", properties)
|
||||
node:activate(Feature.Proxy.BOUND)
|
||||
|
|
@ -27,15 +92,33 @@ function createNode(parent, id, type, factory, properties)
|
|||
end
|
||||
|
||||
function createDevice(parent, id, type, factory, properties)
|
||||
-- ensure the device has a name
|
||||
properties["device.name"] = properties["device.name"]
|
||||
or "v4l2_device." .. (properties["device.bus-id"] or properties["device.bus-path"] or "unknown")
|
||||
-- ensure the device has an appropriate name
|
||||
local name = "v4l2_device." ..
|
||||
(properties["device.name"] or
|
||||
properties["device.bus-id"] or
|
||||
properties["device.bus-path"] or
|
||||
tostring(id))
|
||||
|
||||
properties["device.name"] = name
|
||||
|
||||
-- deduplicate devices with the same name
|
||||
for counter = 2, 99, 1 do
|
||||
if findDuplicate(parent, id, "device.name", properties["device.name"]) then
|
||||
properties["device.name"] = name .. "." .. counter
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- ensure the device has a description
|
||||
properties["device.description"] = properties["device.description"]
|
||||
properties["device.description"] =
|
||||
properties["device.description"]
|
||||
or properties["device.product.name"]
|
||||
or "Unknown device"
|
||||
|
||||
-- apply properties from config.rules
|
||||
rulesApplyProperties(properties)
|
||||
|
||||
-- create the device
|
||||
local device = SpaDevice(factory, properties)
|
||||
device:connect("create-object", createNode)
|
||||
|
|
@ -43,6 +126,6 @@ function createDevice(parent, id, type, factory, properties)
|
|||
parent:store_managed_object(id, device)
|
||||
end
|
||||
|
||||
monitor = SpaDevice("api.v4l2.enum.udev")
|
||||
monitor = SpaDevice("api.v4l2.enum.udev", config.properties or {})
|
||||
monitor:connect("create-object", createDevice)
|
||||
monitor:activate(Feature.SpaDevice.ENABLED)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue