Debian Codegraph @

src/scripts/device/find-best-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 -- find the best route for a given device_id, based on availability and priority
12
13 cutils = require ("common-utils")
14 devinfo = require ("device-info-cache")
15 log = Log.open_topic ("s-device")
16
17 SimpleEventHook {
18 name = "device/find-best-routes",
19 after = "device/find-stored-routes",
20 interests = {
21 EventInterest {
22 Constraint { "event.type", "=", "select-routes" },
23 Constraint { "profile.active-device-ids", "is-present" },
24 },
25 },
26 execute = function (event)
27 local device = event:get_subject ()
28 local event_properties = event:get_properties ()
29 local active_ids = event_properties ["profile.active-device-ids"]
30 local selected_routes = event:get_data ("selected-routes") or Properties()
31
32 local dev_info = devinfo:get_device_info (device)
33 assert (dev_info)
34
35 -- active IDs are exchanged in JSON format
36 active_ids = Json.Raw (active_ids):parse ()
37
38 for _, device_id in ipairs (active_ids) do
39 -- if a previous hook already selected a route for this device_id, skip it
40 if selected_routes [tostring (device_id)] then
41 goto next_device_id
42 end
43
44 local best_avail = nil
45 local best_unk = nil
46 for _, ri in pairs (dev_info.route_infos) do
47 if cutils.arrayContains (ri.devices, device_id) and
48 (ri.profiles == nil or cutils.arrayContains (ri.profiles, dev_info.active_profile)) then
49 if ri.available == "yes" or ri.available == "unknown" then
50 if ri.direction == "Output" and ri.available ~= ri.prev_available then
51 best_avail = ri
52 ri.save = true
53 break
54 elseif ri.available == "yes" then
55 if (best_avail == nil or ri.priority > best_avail.priority) then
56 best_avail = ri
57 end
58 elseif best_unk == nil or ri.priority > best_unk.priority then
59 best_unk = ri
60 end
61 end
62 end
63 end
64
65 local route = best_avail or best_unk
66 if route then
67 selected_routes [tostring (device_id)] =
68 Json.Object { index = route.index }:to_string ()
69 end
70
71 ::next_device_id::
72 end
73
74 -- save the selected routes for the apply-routes hook
75 event:set_data ("selected-routes", selected_routes)
76 end
77 }:register ()