Debian Codegraph @

tests/examples/bt-profile-switch.lua

raw
1 #!/usr/bin/wpexec
2 --
3 -- WirePlumber
4 --
5 -- Copyright © 2021 Collabora Ltd.
6 -- @author George Kiagiadakis <[email protected]>
7 --
8 -- SPDX-License-Identifier: MIT
9 --
10 -- This is an example of a standalone policy making script. It can be executed
11 -- either on top of another instance of wireplumber or pipewire-media-session,
12 -- as a standalone executable, or it can be placed in WirePlumber's scripts
13 -- directory and loaded together with other scripts.
14 --
15 -- The script basically watches for a client application called
16 -- "ZOOM VoiceEngine", and when it appears (i.e. Zoom starts), it switches
17 -- the profile of all connected bluetooth devices to the "headset-head-unit"
18 -- (a.k.a HSP Headset Audio) profile. When Zoom exits, it switches again the
19 -- profile of all bluetooth devices to A2DP Sink.
20 --
21 -- The script can be customized further to look for other clients and/or
22 -- change the profile of a specific device, by customizing the constraints.
23 -----------------------------------------------------------------------------
24
25 devices_om = ObjectManager {
26 Interest { type = "device",
27 Constraint { "device.api", "=", "bluez5" },
28 }
29 }
30
31 clients_om = ObjectManager {
32 Interest { type = "client",
33 Constraint { "application.name", "=", "ZOOM VoiceEngine" },
34 }
35 }
36
37 function set_profile(profile_name)
38 for device in devices_om:iterate() do
39 local index = nil
40 local desc = nil
41
42 for profile in device:iterate_params("EnumProfile") do
43 local p = profile:parse()
44 if p.properties.name == profile_name then
45 index = p.properties.index
46 desc = p.properties.description
47 break
48 end
49 end
50
51 if index then
52 local pod = Pod.Object {
53 "Spa:Pod:Object:Param:Profile", "Profile",
54 index = index
55 }
56
57 print("Setting profile of '"
58 .. device.properties["device.description"]
59 .. "' to: " .. desc)
60 device:set_params("Profile", pod)
61 end
62 end
63 end
64
65 clients_om:connect("object-added", function (om, client)
66 print("Client '" .. client.properties["application.name"] .. "' connected")
67 set_profile("headset-head-unit")
68 end)
69
70 clients_om:connect("object-removed", function (om, client)
71 print("Client '" .. client.properties["application.name"] .. "' disconnected")
72 set_profile("a2dp-sink")
73 end)
74
75 devices_om:activate()
76 clients_om:activate()