| 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 |
-- Update the device info cache with the latest information from EnumRoute(all |
| 12 |
-- the device routes) and trigger a "select-routes" event to select new routes |
| 13 |
-- for the given device configuration, if it has changed |
| 14 |
|
| 15 |
cutils = require ("common-utils") |
| 16 |
devinfo = require ("device-info-cache") |
| 17 |
log = Log.open_topic ("s-device") |
| 18 |
|
| 19 |
SimpleEventHook { |
| 20 |
name = "device/select-route", |
| 21 |
after = "device/select-profile", |
| 22 |
interests = { |
| 23 |
EventInterest { |
| 24 |
Constraint { "event.type", "=", "device-added" }, |
| 25 |
}, |
| 26 |
EventInterest { |
| 27 |
Constraint { "event.type", "=", "device-params-changed" }, |
| 28 |
Constraint { "event.subject.param-id", "c", "EnumRoute" }, |
| 29 |
}, |
| 30 |
}, |
| 31 |
execute = function (event) |
| 32 |
local source = event:get_source () |
| 33 |
local device = event:get_subject () |
| 34 |
|
| 35 |
local dev_info = devinfo:get_device_info (device) |
| 36 |
if not dev_info then |
| 37 |
return |
| 38 |
end |
| 39 |
|
| 40 |
local new_route_infos = {} |
| 41 |
local avail_routes_changed = false |
| 42 |
local profile = nil |
| 43 |
|
| 44 |
-- get current profile |
| 45 |
for p in device:iterate_params ("Profile") do |
| 46 |
profile = cutils.parseParam (p, "Profile") |
| 47 |
end |
| 48 |
|
| 49 |
-- look at all the routes and update/reset cached information |
| 50 |
for p in device:iterate_params ("EnumRoute") do |
| 51 |
-- parse pod |
| 52 |
local route = cutils.parseParam (p, "EnumRoute") |
| 53 |
if not route then |
| 54 |
goto skip_enum_route |
| 55 |
end |
| 56 |
|
| 57 |
-- find cached route information |
| 58 |
local route_info = devinfo.find_route_info (dev_info, route, true) |
| 59 |
|
| 60 |
-- update properties |
| 61 |
route_info.prev_available = route_info.available |
| 62 |
if route_info.available ~= route.available then |
| 63 |
log:info (device, "route " .. route.name .. " available changed " .. |
| 64 |
route_info.available .. " -> " .. route.available) |
| 65 |
route_info.available = route.available |
| 66 |
if profile and cutils.arrayContains (route.profiles, profile.index) then |
| 67 |
avail_routes_changed = true |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
-- store |
| 72 |
new_route_infos [route.index] = route_info |
| 73 |
|
| 74 |
::skip_enum_route:: |
| 75 |
end |
| 76 |
|
| 77 |
-- replace old route_infos to lose old routes |
| 78 |
-- that no longer exist on the device |
| 79 |
dev_info.route_infos = new_route_infos |
| 80 |
new_route_infos = nil |
| 81 |
|
| 82 |
-- restore routes for profile |
| 83 |
if profile then |
| 84 |
local profile_changed = (dev_info.active_profile ~= profile.index) |
| 85 |
dev_info.active_profile = profile.index |
| 86 |
|
| 87 |
-- if the profile changed, restore routes for that profile |
| 88 |
-- if any of the routes of the current profile changed in availability, |
| 89 |
-- then try to select a new "best" route for each device and ignore |
| 90 |
-- what was stored |
| 91 |
if profile_changed or avail_routes_changed then |
| 92 |
log:info (device, |
| 93 |
string.format ("restore routes for profile(%s) of device(%s)", |
| 94 |
profile.name, dev_info.name)) |
| 95 |
|
| 96 |
-- find the active device IDs for which to select routes |
| 97 |
local active_ids = findActiveDeviceIDs (profile) |
| 98 |
active_ids = Json.Array (active_ids):to_string () |
| 99 |
|
| 100 |
-- push select-routes event and let the hooks select the appropriate routes |
| 101 |
local props = { |
| 102 |
["profile.changed"] = profile_changed, |
| 103 |
["profile.name"] = profile.name, |
| 104 |
["profile.active-device-ids"] = active_ids, |
| 105 |
} |
| 106 |
source:call ("push-event", "select-routes", device, props) |
| 107 |
end |
| 108 |
end |
| 109 |
end |
| 110 |
}:register() |
| 111 |
|
| 112 |
-- These device ids are like routes(speaker, mic, headset etc) or sub-devices or |
| 113 |
-- paths with in the pipewire devices/soundcards. |
| 114 |
function findActiveDeviceIDs (profile) |
| 115 |
-- parses the classes from the profile and returns the device IDs |
| 116 |
----- sample structure, should return { 0, 8 } ----- |
| 117 |
-- classes: |
| 118 |
-- 1: 2 |
| 119 |
-- 2: |
| 120 |
-- 1: Audio/Source |
| 121 |
-- 2: 1 |
| 122 |
-- 3: card.profile.devices |
| 123 |
-- 4: |
| 124 |
-- 1: 0 |
| 125 |
-- pod_type: Array |
| 126 |
-- value_type: Spa:Int |
| 127 |
-- pod_type: Struct |
| 128 |
-- 3: |
| 129 |
-- 1: Audio/Sink |
| 130 |
-- 2: 1 |
| 131 |
-- 3: card.profile.devices |
| 132 |
-- 4: |
| 133 |
-- 1: 8 |
| 134 |
-- pod_type: Array |
| 135 |
-- value_type: Spa:Int |
| 136 |
-- pod_type: Struct |
| 137 |
-- pod_type: Struct |
| 138 |
local active_ids = {} |
| 139 |
if type (profile.classes) == "table" and profile.classes.pod_type == "Struct" then |
| 140 |
for _, p in ipairs (profile.classes) do |
| 141 |
if type (p) == "table" and p.pod_type == "Struct" then |
| 142 |
local i = 1 |
| 143 |
while true do |
| 144 |
local k, v = p [i], p [i+1] |
| 145 |
i = i + 2 |
| 146 |
if not k or not v then |
| 147 |
break |
| 148 |
end |
| 149 |
if k == "card.profile.devices" and |
| 150 |
type (v) == "table" and v.pod_type == "Array" then |
| 151 |
for _, dev_id in ipairs (v) do |
| 152 |
table.insert (active_ids, dev_id) |
| 153 |
end |
| 154 |
end |
| 155 |
end |
| 156 |
end |
| 157 |
end |
| 158 |
end |
| 159 |
return active_ids |
| 160 |
end |