Debian Codegraph @

src/scripts/device/state-routes.lua

raw
1 -- WirePlumber
2 --
3 -- Copyright © 2021-2022 Collabora Ltd.
4 -- @author George Kiagiadakis <[email protected]>
5 --
6 -- Based on default-routes.c from pipewire-media-session
7 -- Copyright © 2020 Wim Taymans
8 --
9 -- SPDX-License-Identifier: MIT
10 --
11 -- This file contains all the logic related to saving device routes and their
12 -- properties to a state file and restoring both the routes selection and
13 -- the properties of routes later on.
14 --
15
16 cutils = require ("common-utils")
17 devinfo = require ("device-info-cache")
18 log = Log.open_topic ("s-device")
19
20 -- the state storage
21 state = nil
22 state_table = nil
23
24 -- hook to restore routes selection for a newly selected profile
25 find_stored_routes_hook = SimpleEventHook {
26 name = "device/find-stored-routes",
27 interests = {
28 EventInterest {
29 Constraint { "event.type", "=", "select-routes" },
30 Constraint { "profile.changed", "=", "true" },
31 Constraint { "profile.active-device-ids", "is-present" },
32 },
33 },
34 execute = function (event)
35 local device = event:get_subject ()
36 local event_properties = event:get_properties ()
37 local profile_name = event_properties ["profile.name"]
38 local active_ids = event_properties ["profile.active-device-ids"]
39 local selected_routes = event:get_data ("selected-routes") or Properties()
40
41 local dev_info = devinfo:get_device_info (device)
42 assert (dev_info)
43
44 -- get the stored routes for this profile
45 -- skip the hook if there are no stored routes, there is no point
46 local spr = getStoredProfileRoutes (dev_info, profile_name)
47 if #spr == 0 then
48 return
49 end
50
51 -- active IDs are exchanged in JSON format
52 active_ids = Json.Raw (active_ids):parse ()
53
54 for _, device_id in ipairs (active_ids) do
55 -- if a previous hook already selected a route for this device_id, skip it
56 if selected_routes [tostring (device_id)] then
57 goto next_device_id
58 end
59
60 log:info (device, "restoring route for device ID " .. tostring (device_id));
61
62 local route_info = nil
63
64 -- find a route that was previously stored for a device_id
65 for _, ri in pairs (dev_info.route_infos) do
66 if cutils.arrayContains (ri.devices, tonumber (device_id)) and
67 (ri.profiles == nil or cutils.arrayContains (ri.profiles, dev_info.active_profile)) and
68 cutils.arrayContains (spr, ri.name) then
69 route_info = ri
70 break
71 end
72 end
73
74 if route_info then
75 -- we found a stored route
76 if route_info.available == "no" then
77 log:info (device, "stored route '" .. route_info.name .. "' not available")
78 -- not available, try to find next best
79 route_info = nil
80 else
81 log:info (device, "found stored route: " .. route_info.name)
82 end
83 end
84
85 if route_info then
86 selected_routes [tostring (device_id)] =
87 Json.Object { index = route_info.index }:to_string ()
88 end
89
90 ::next_device_id::
91 end
92
93 -- save the selected routes for the apply-routes hook
94 event:set_data ("selected-routes", selected_routes)
95 end
96 }
97
98 -- extract the "selected-routes" event data and augment it to include
99 -- the route properties, as they were stored in the state file;
100 -- this is the last step before applying the routes
101 apply_route_props_hook = SimpleEventHook {
102 name = "device/apply-route-props",
103 after = { "device/find-stored-routes", "device/find-best-routes" },
104 interests = {
105 EventInterest {
106 Constraint { "event.type", "=", "select-routes" },
107 },
108 },
109 execute = function (event)
110 local device = event:get_subject ()
111 local selected_routes = event:get_data ("selected-routes") or Properties()
112 local new_selected_routes = {}
113
114 local dev_info = devinfo:get_device_info (device)
115 assert (dev_info)
116
117 if selected_routes:get_count () == 0 then
118 log:info (device, "No routes selected to set on " .. dev_info.name)
119 return
120 end
121
122 for device_id, route in pairs (selected_routes) do
123 -- JSON to lua table
124 route = Json.Raw (route):parse ()
125
126 local route_info = devinfo.find_route_info (dev_info, route, false)
127 local props = getStoredRouteProps (dev_info, route_info)
128
129 -- convert arrays to Json
130 if props.channelVolumes then
131 props.channelVolumes = Json.Array (props.channelVolumes)
132 end
133 if props.channelMap then
134 props.channelMap = Json.Array (props.channelMap)
135 end
136 if props.iec958Codecs then
137 props.iec958Codecs = Json.Array (props.iec958Codecs)
138 end
139
140 local json = Json.Object {
141 index = route_info.index,
142 props = Json.Object (props),
143 }
144 new_selected_routes [device_id] = json:to_string ()
145 end
146
147 -- save the selected routes for the apply-routes hook
148 event:set_data ("selected-routes", new_selected_routes)
149 end
150 }
151
152 store_or_restore_routes_hook = AsyncEventHook {
153 name = "device/store-or-restore-routes",
154 interests = {
155 EventInterest {
156 Constraint { "event.type", "=", "device-params-changed" },
157 Constraint { "event.subject.param-id", "=", "Route" },
158 },
159 },
160 steps = {
161 start = {
162 next = "none",
163 execute = function (event, transition)
164 local source = event:get_source ()
165 local device = event:get_subject ()
166
167 -- Make sure the routes are always updated before evaluating them.
168 -- https://gitlab.freedesktop.org/pipewire/wireplumber/-/issues/762
169 device:enum_params ("EnumRoute", function (enum_route_it, e)
170 local selected_routes = {}
171 local push_select_routes = false
172
173 -- check for error
174 if e then
175 transition:return_error ("failed to enum routes: "
176 .. tostring (e));
177 return
178 end
179
180 -- Make sure the device is still valid
181 if (device:get_active_features() & Feature.Proxy.BOUND) == 0 then
182 transition:advance ()
183 return
184 end
185
186 local dev_info = devinfo:get_device_info (device)
187 if not dev_info then
188 transition:advance ()
189 return
190 end
191
192 local new_route_infos = {}
193
194 -- look at all the routes and update/reset cached information
195 for p in enum_route_it:iterate() do
196 -- parse pod
197 local route = cutils.parseParam (p, "EnumRoute")
198 if not route then
199 goto skip_enum_route
200 end
201
202 -- find cached route information
203 local route_info = devinfo.find_route_info (dev_info, route, true)
204 if not route_info then
205 goto skip_enum_route
206 end
207
208 -- update properties
209 route_info.prev_active = route_info.active
210 route_info.active = false
211 route_info.save = false
212
213 -- store
214 new_route_infos [route.index] = route_info
215
216 ::skip_enum_route::
217 end
218
219 -- update route_infos with new prev_active, active and save changes
220 dev_info.route_infos = new_route_infos
221 new_route_infos = nil
222
223 -- check for changes in the active routes
224 for p in device:iterate_params ("Route") do
225 local route = cutils.parseParam (p, "Route")
226 if not route then
227 goto skip_route
228 end
229
230 -- get cached route info and at the same time
231 -- ensure that the route is also in EnumRoute
232 local route_info = devinfo.find_route_info (dev_info, route, false)
233 if not route_info then
234 goto skip_route
235 end
236
237 -- update route_info state
238 route_info.active = true
239 route_info.save = route.save
240
241 if not route_info.prev_active then
242 -- a new route is now active, restore the volume and
243 -- make sure we save this as a preferred route
244 log:info (device,
245 string.format ("new active route(%s) found of device(%s)",
246 route.name, dev_info.name))
247 route_info.prev_active = true
248 route_info.active = true
249
250 selected_routes [tostring (route.device)] =
251 Json.Object { index = route_info.index }:to_string ()
252 push_select_routes = true
253
254 elseif route.available ~= "no" and route.save and route.props then
255 -- just save route properties
256 log:info (device,
257 string.format ("storing route(%s) props of device(%s)",
258 route.name, dev_info.name))
259
260 saveRouteProps (dev_info, route)
261 end
262
263 ::skip_route::
264 end
265
266 -- save selected routes for the active profile
267 for p in device:iterate_params ("Profile") do
268 local profile = cutils.parseParam (p, "Profile")
269 saveProfileRoutes (dev_info, profile.name)
270 end
271
272 -- push a select-routes event to re-apply the routes with new properties
273 if push_select_routes then
274 local e = source:call ("create-event", "select-routes", device, nil)
275 e:set_data ("selected-routes", selected_routes)
276 EventDispatcher.push_event (e)
277 end
278
279 transition:advance ()
280 end)
281 end
282 }
283 }
284 }
285
286 function saveRouteProps (dev_info, route)
287 local props = route.props.properties
288 local key = dev_info.name .. ":" ..
289 route.direction:lower () .. ":" ..
290 route.name
291
292 state_table [key] = Json.Object {
293 volume = props.volume,
294 mute = props.mute,
295 channelVolumes = props.channelVolumes and Json.Array (props.channelVolumes),
296 channelMap = props.channelMap and Json.Array (props.channelMap),
297 latencyOffsetNsec = props.latencyOffsetNsec,
298 iec958Codecs = props.iec958Codecs and Json.Array (props.iec958Codecs),
299 }:to_string ()
300
301 state:save_after_timeout (state_table)
302 end
303
304 function getStoredRouteProps (dev_info, route)
305 local key = dev_info.name .. ":" ..
306 route.direction:lower () .. ":" ..
307 route.name
308 local value = state_table [key]
309 if value then
310 local json = Json.Raw (value)
311 if json and json:is_object () then
312 return json:parse ()
313 end
314 end
315 return {}
316 end
317
318 -- stores an array with the route names that are selected
319 -- for the given device and profile
320 function saveProfileRoutes (dev_info, profile_name)
321 -- select only routes with save == true
322 local routes = {}
323 for idx, ri in pairs (dev_info.route_infos) do
324 if ri.save then
325 table.insert (routes, ri.name)
326 end
327 end
328
329 if #routes > 0 then
330 local key = dev_info.name .. ":profile:" .. profile_name
331 state_table [key] = Json.Array (routes):to_string()
332 state:save_after_timeout (state_table)
333 end
334 end
335
336 -- returns an array of the route names that were previously selected
337 -- for the given device and profile
338 function getStoredProfileRoutes (dev_info, profile_name)
339 local key = dev_info.name .. ":profile:" .. profile_name
340 local value = state_table [key]
341 if value then
342 local json = Json.Raw (value)
343 if json and json:is_array () then
344 return json:parse ()
345 end
346 end
347 return {}
348 end
349
350 function toggleState (enable)
351 if enable and not state then
352 state = State ("default-routes")
353 state_table = state:load ()
354 find_stored_routes_hook:register ()
355 apply_route_props_hook:register ()
356 store_or_restore_routes_hook:register ()
357 elseif not enable and state then
358 state = nil
359 state_table = nil
360 find_stored_routes_hook:remove ()
361 apply_route_props_hook:remove ()
362 store_or_restore_routes_hook:remove ()
363 end
364 end
365
366 Settings.subscribe ("device.restore-routes", function ()
367 toggleState (Settings.get_boolean ("device.restore-routes"))
368 end)
369 toggleState (Settings.get_boolean ("device.restore-routes"))