Debian Codegraph @

src/scripts/lib/monitor-utils.lua

raw
1 -- WirePlumber
2
3 -- Copyright © 2023 Collabora Ltd.
4 -- @author Ashok Sidipotu <[email protected]>
5
6 -- SPDX-License-Identifier: MIT
7
8 -- Script is a Lua Module of monitor Lua utility functions
9
10 log = Log.open_topic ("s-monitors-utils")
11
12 local mutils = {
13 cam_data = {},
14 cam_source = nil
15 }
16
17 -- finds out if any of the managed objects(nodes of a device or devices of
18 -- device enumerator) has duplicate values
19 function mutils.find_duplicate (parent, id, property, value)
20 for i = 0, id - 1, 1 do
21 local obj = parent:get_managed_object (i)
22 if obj and obj.properties[property] == value then
23 return true
24 end
25 end
26 return false
27 end
28
29 function create_cam_node(cam_data)
30 local api = cam_data.properties["device.api"]
31
32 log:info ("create " .. api .. " node for device: " .. cam_data.obj_path)
33
34 source = source or Plugin.find ("standard-event-source")
35 local e = source:call ("create-event", "create-" .. api .. "-device-node",
36 cam_data.parent, nil)
37 e:set_data ("factory", cam_data.factory)
38 e:set_data ("node-properties", cam_data.properties)
39 e:set_data ("node-sub-id", cam_data.id)
40
41 EventDispatcher.push_event (e)
42 end
43
44 function table_contains(table, element)
45 for _, value in pairs(table) do
46 if value == element then
47 return true
48 end
49 end
50 return false
51 end
52
53 function mutils.create_cam_nodes(self)
54 log:debug("create_cam_nodes for " .. #self.cam_data .. " devices")
55
56 local libcamera_cameras = {}
57 local v4l2_uvc_cameras = {}
58 local v4l2_other_cameras = {}
59
60 for _, data in ipairs(self.cam_data) do
61 local api = data.properties["device.api"]
62
63 local dev_ids = data.properties["device.devids"]
64 local dev_ids_json = Json.Raw(dev_ids)
65
66 if dev_ids_json:is_array() then
67 data.dev_ids = dev_ids_json:parse()
68 else
69 data.dev_ids = {}
70 -- to maintain the backward compatibility with earlier pipewire versions.
71 for dev_id_str in dev_ids:gmatch("%S+") do
72 local dev_id = tonumber(dev_id_str)
73 if dev_id then
74 table.insert(data.dev_ids, dev_id)
75 end
76 end
77 end
78
79 if api == 'libcamera' then
80 table.insert(libcamera_cameras, data)
81 elseif api == 'v4l2' then
82 if data.properties["api.v4l2.cap.driver"] == "uvcvideo" then
83 table.insert(v4l2_uvc_cameras, data)
84 else
85 table.insert(v4l2_other_cameras, data)
86 end
87 else
88 log:warning("Got camera with unknown API, ignoring")
89 end
90 end
91
92 local device_ids_libcamera = {}
93 local device_ids_v4l2 = {}
94
95 for _, data in ipairs(v4l2_uvc_cameras) do
96 create_cam_node (data)
97 table.insert(device_ids_v4l2, data.dev_ids[1])
98 end
99
100 for _, data in ipairs(libcamera_cameras) do
101 local should_create = true
102
103 for _, dev_id in ipairs(data.dev_ids) do
104 if table_contains(device_ids_v4l2, dev_id) then
105 should_create = false
106 break
107 end
108 end
109
110 if not should_create then
111 log:debug ("skipping device " .. data.obj_path)
112 else
113 create_cam_node (data)
114 for _, dev_id in ipairs(data.dev_ids) do
115 table.insert(device_ids_libcamera, dev_id)
116 end
117 end
118 end
119
120 for _, data in ipairs(v4l2_other_cameras) do
121 if table_contains(device_ids_libcamera, data.dev_ids[1]) then
122 log:debug ("skipping device " .. data.obj_path)
123 else
124 create_cam_node (data)
125 end
126 end
127
128 self.cam_data = {}
129 end
130
131
132 -- arbitrates between v4l2 and libcamera on who gets to create the device node
133 -- for a device, logic is based on the device number of the device given by both
134 -- the parties.
135 function mutils.register_cam_node (self, parent, id, factory, properties)
136 local obj_path = properties["object.path"]
137 local api = properties["device.api"]
138
139 -- cache info, it comes handy when creating node
140 local cam_data = {}
141 cam_data.id = id
142 cam_data.parent = parent
143 cam_data.obj_path = obj_path
144 cam_data.factory = factory
145 cam_data.properties = properties
146
147 local dev_ids = properties["device.devids"]
148 if not dev_ids then
149 log:debug(api .. " reported device " .. obj_path .. " without device ids")
150 create_cam_node(cam_data)
151 return
152 end
153
154 log:debug(api .. " reported device " .. obj_path .. " with device ids " .. dev_ids)
155 table.insert(self.cam_data, cam_data)
156
157 if self.cam_source then
158 self.cam_source:destroy ()
159 end
160
161 self.cam_source = Core.timeout_add (
162 Settings.get_int ("monitor.camera-discovery-timeout"), function()
163 self:create_cam_nodes ()
164 self.cam_source = nil
165 end)
166 end
167
168 return mutils