Debian Codegraph @

src/scripts/device/apply-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 -- Set the Route param as part of the "select-routes" event run
12
13 devinfo = require ("device-info-cache")
14 log = Log.open_topic ("s-device")
15
16 AsyncEventHook {
17 name = "device/apply-routes",
18 after = { "device/find-stored-routes",
19 "device/find-best-routes",
20 "device/apply-route-props" },
21 interests = {
22 EventInterest {
23 Constraint { "event.type", "=", "select-routes" },
24 },
25 },
26 steps = {
27 start = {
28 next = "none",
29 execute = function (event, transition)
30 local device = event:get_subject ()
31 local selected_routes = event:get_data ("selected-routes")
32
33 local dev_info = devinfo:get_device_info (device)
34 assert (dev_info)
35
36 if not selected_routes then
37 log:info (device, "No routes selected to set on " .. dev_info.name)
38 transition:advance ()
39 return
40 end
41
42 for device_id, route in pairs (selected_routes) do
43 -- JSON to lua table
44 route = Json.Raw (route):parse ()
45
46 -- steal the props
47 local props = route.props or {}
48
49 -- replace with the full route info
50 local route_info = devinfo.find_route_info (dev_info, route)
51 if not route_info then
52 goto skip_route
53 end
54
55 -- ensure default values
56 local is_input = (route_info.direction == "Input")
57 props.mute = props.mute or false
58 props.channelVolumes = props.channelVolumes or {
59 -- See if we have a per-device override
60 (is_input and tonumber(device.properties["device.routes.default-source-volume"]))
61 or tonumber(device.properties["device.routes.default-sink-volume"])
62 -- Otherwise we use the global default
63 or (is_input and Settings.get_float ("device.routes.default-source-volume"))
64 or Settings.get_float ("device.routes.default-sink-volume")
65 }
66
67 -- prefix the props with correct IDs to create a Pod.Object
68 table.insert (props, 1, "Spa:Pod:Object:Param:Props")
69 table.insert (props, 2, "Route")
70
71 -- convert arrays to Spa Pod
72 if props.channelVolumes then
73 table.insert (props.channelVolumes, 1, "Spa:Float")
74 props.channelVolumes = Pod.Array (props.channelVolumes)
75 end
76 if props.channelMap then
77 table.insert (props.channelMap, 1, "Spa:Enum:AudioChannel")
78 props.channelMap = Pod.Array (props.channelMap)
79 end
80 if props.iec958Codecs then
81 table.insert (props.iec958Codecs, 1, "Spa:Enum:AudioIEC958Codec")
82 props.iec958Codecs = Pod.Array (props.iec958Codecs)
83 end
84
85 -- construct Route param
86 local param = Pod.Object {
87 "Spa:Pod:Object:Param:Route", "Route",
88 index = route_info.index,
89 device = device_id,
90 props = Pod.Object (props),
91 save = false,
92 }
93
94 log:debug (param,
95 string.format ("setting route(%s) on for device(%s)(%s)",
96 route_info.name, dev_info.name, tostring (device)))
97
98 device:set_param ("Route", param)
99
100 ::skip_route::
101 end
102
103 -- FIXME: add cancellability
104 -- sync on the pipewire connection to ensure that the params
105 -- have been configured on the remote device object
106 Core.sync (function ()
107 transition:advance ()
108 end)
109 end
110 },
111 }
112 }:register()