Debian Codegraph @

tests/examples/bt-pinephone.lua

raw
1 #!/usr/bin/wpexec
2 --
3 -- WirePlumber
4 --
5 -- Copyright © 2022 Collabora Ltd.
6 -- @author Frédéric Danis <[email protected]>
7 --
8 -- SPDX-License-Identifier: MIT
9 --
10 -- This is an example of the offload SCO nodes platform specific management,
11 -- in this case for the PinePhone.
12 --
13 -- The PinePhone provides specific ALSA ports to route audio to the Bluetooth
14 -- chipset. This script selects these ports when the offload SCO nodes state
15 -- change to 'running'.
16 --
17 -- This scriptcan be executed as a standalone executable, or it can be placed
18 -- in WirePlumber's scripts directory and loaded together with other scripts.
19 -----------------------------------------------------------------------------
20
21 devices_om = ObjectManager {
22 Interest {
23 type = "device",
24 }
25 }
26
27 nodes_om = ObjectManager {
28 Interest {
29 type = "node",
30 Constraint { "node.name", "#", "*.bluez_*put*"},
31 Constraint { "device.id", "+" },
32 }
33 }
34
35 function parseParam(param, id)
36 local route = param:parse()
37 if route.pod_type == "Object" and route.object_id == id then
38 return route.properties
39 else
40 return nil
41 end
42 end
43
44 function setPlatformRoute (route_name, direction)
45 local platform_sound = nil
46 local device_id = nil
47 local route_save = nil
48
49 local interest = Interest {
50 type = "device",
51 Constraint { "device.name", "=", "alsa_card.platform-sound"}
52 }
53 for d in devices_om:iterate (interest) do
54 platform_sound = d
55
56 for p in platform_sound:iterate_params("Route") do
57 route = parseParam(p, "Route")
58 if route.direction == direction then
59 device_id = route.device
60 route_save = route.save
61 end
62 end
63
64 for p in platform_sound:iterate_params("EnumRoute") do
65 enum_route = parseParam(p, "EnumRoute")
66
67 if enum_route.name == route_name then
68 route_index = enum_route.index
69 end
70 end
71 end
72
73 if route.index == route_index then
74 return
75 end
76
77 -- default props
78 local props = {
79 "Spa:Pod:Object:Param:Props", "Route",
80 mute = false,
81 }
82
83 -- construct Route param
84 local param = Pod.Object {
85 "Spa:Pod:Object:Param:Route", "Route",
86 index = route_index,
87 device = device_id,
88 props = Pod.Object(props),
89 save = route_save,
90 }
91
92 Log.info(param, "setting route on " .. tostring(platform_sound))
93 platform_sound:set_param("Route", param)
94 end
95
96 nodes_om:connect("object-added", function(_, node)
97 node:connect("state-changed", function(node, old_state, cur_state)
98 local interest = Interest {
99 type = "device",
100 Constraint { "object.id", "=", node.properties["device.id"]}
101 }
102 for d in devices_om:iterate (interest) do
103 if cur_state == "running" then
104 -- Both direction should be set to allow audio streaming
105 setPlatformRoute("[Out] BluetoothHeadset", "Output")
106 setPlatformRoute("[In] BluetoothHeadset", "Input")
107 else
108 setPlatformRoute("[Out] Earpiece", "Output")
109 setPlatformRoute("[In] DigitalMic", "Input")
110 end
111 end
112 end)
113 end)
114
115 nodes_om:activate()
116 devices_om:activate()