| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2021-2022 Collabora Ltd. |
| 4 |
-- @author George Kiagiadakis <[email protected]> |
| 5 |
-- |
| 6 |
-- Based on restore-stream.c from pipewire-media-session |
| 7 |
-- Copyright © 2020 Wim Taymans |
| 8 |
-- |
| 9 |
-- SPDX-License-Identifier: MIT |
| 10 |
|
| 11 |
cutils = require ("common-utils") |
| 12 |
log = Log.open_topic ("s-node") |
| 13 |
|
| 14 |
config = {} |
| 15 |
config.rules = Conf.get_section_as_json ("stream.rules", Json.Array {}) |
| 16 |
|
| 17 |
-- the state storage |
| 18 |
state = nil |
| 19 |
state_table = nil |
| 20 |
|
| 21 |
-- Support for the "System Sounds" volume control in pavucontrol |
| 22 |
rs_metadata = nil |
| 23 |
|
| 24 |
-- hook to restore stream properties & target |
| 25 |
restore_stream_hook = SimpleEventHook { |
| 26 |
name = "node/restore-stream", |
| 27 |
interests = { |
| 28 |
-- match stream nodes |
| 29 |
EventInterest { |
| 30 |
Constraint { "event.type", "=", "node-added" }, |
| 31 |
Constraint { "media.class", "matches", "Stream/*" }, |
| 32 |
}, |
| 33 |
-- and device nodes that are not associated with any routes |
| 34 |
EventInterest { |
| 35 |
Constraint { "event.type", "=", "node-added" }, |
| 36 |
Constraint { "media.class", "matches", "Audio/*" }, |
| 37 |
Constraint { "device.routes", "is-absent" }, |
| 38 |
}, |
| 39 |
EventInterest { |
| 40 |
Constraint { "event.type", "=", "node-added" }, |
| 41 |
Constraint { "media.class", "matches", "Audio/*" }, |
| 42 |
Constraint { "device.routes", "equals", "0" }, |
| 43 |
}, |
| 44 |
}, |
| 45 |
execute = function (event) |
| 46 |
local node = event:get_subject () |
| 47 |
local stream_props = node.properties |
| 48 |
stream_props = JsonUtils.match_rules_update_properties (config.rules, stream_props) |
| 49 |
|
| 50 |
local key = formKey (stream_props) |
| 51 |
if not key then |
| 52 |
return |
| 53 |
end |
| 54 |
|
| 55 |
local stored_values = getStoredStreamProps (key) or {} |
| 56 |
|
| 57 |
-- restore node Props (volumes, channelMap, etc...) |
| 58 |
if Settings.get_boolean ("node.stream.restore-props") and stream_props ["state.restore-props"] ~= "false" |
| 59 |
then |
| 60 |
local props = { |
| 61 |
"Spa:Pod:Object:Param:Props", "Props", |
| 62 |
volume = stored_values.volume, |
| 63 |
mute = stored_values.mute, |
| 64 |
channelVolumes = stored_values.channelVolumes ~= nil and |
| 65 |
stored_values.channelVolumes or buildDefaultChannelVolumes (node), |
| 66 |
channelMap = stored_values.channelMap, |
| 67 |
} |
| 68 |
-- convert arrays to Spa Pod |
| 69 |
if props.channelVolumes then |
| 70 |
table.insert (props.channelVolumes, 1, "Spa:Float") |
| 71 |
props.channelVolumes = Pod.Array (props.channelVolumes) |
| 72 |
end |
| 73 |
if props.channelMap then |
| 74 |
table.insert (props.channelMap, 1, "Spa:Enum:AudioChannel") |
| 75 |
props.channelMap = Pod.Array (props.channelMap) |
| 76 |
end |
| 77 |
|
| 78 |
if props.volume or (props.mute ~= nil) or props.channelVolumes or props.channelMap |
| 79 |
then |
| 80 |
log:info (node, "restore values from " .. key) |
| 81 |
|
| 82 |
local param = Pod.Object (props) |
| 83 |
log:debug (param, "setting props on " .. tostring (stream_props ["node.name"])) |
| 84 |
node:set_param ("Props", param) |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
-- restore the node's link target on metadata |
| 89 |
if Settings.get_boolean ("node.stream.restore-target") and stream_props ["state.restore-target"] ~= "false" |
| 90 |
then |
| 91 |
if stored_values.target then |
| 92 |
-- check first if there is a defined target in the node's properties |
| 93 |
-- and skip restoring if this is the case (#335) |
| 94 |
local target_in_props = |
| 95 |
stream_props ["target.object"] or stream_props ["node.target"] |
| 96 |
|
| 97 |
if not target_in_props then |
| 98 |
local source = event:get_source () |
| 99 |
local nodes_om = source:call ("get-object-manager", "node") |
| 100 |
local metadata_om = source:call ("get-object-manager", "metadata") |
| 101 |
|
| 102 |
local target_node = nodes_om:lookup { |
| 103 |
Constraint { "node.name", "=", stored_values.target, type = "pw" } |
| 104 |
} |
| 105 |
local metadata = metadata_om:lookup { |
| 106 |
Constraint { "metadata.name", "=", "default" } |
| 107 |
} |
| 108 |
|
| 109 |
if target_node and metadata then |
| 110 |
metadata:set (node ["bound-id"], "target.object", "Spa:Id", |
| 111 |
target_node.properties ["object.serial"]) |
| 112 |
end |
| 113 |
else |
| 114 |
log:debug (node, |
| 115 |
"Not restoring the target for " .. |
| 116 |
tostring (stream_props ["node.name"]) .. |
| 117 |
" because it is already set to " .. target_in_props) |
| 118 |
end |
| 119 |
end |
| 120 |
end |
| 121 |
|
| 122 |
end |
| 123 |
} |
| 124 |
|
| 125 |
-- store stream properties on the state file |
| 126 |
store_stream_props_hook = SimpleEventHook { |
| 127 |
name = "node/store-stream-props", |
| 128 |
interests = { |
| 129 |
-- match stream nodes |
| 130 |
EventInterest { |
| 131 |
Constraint { "event.type", "=", "node-params-changed" }, |
| 132 |
Constraint { "event.subject.param-id", "=", "Props" }, |
| 133 |
Constraint { "media.class", "matches", "Stream/*" }, |
| 134 |
}, |
| 135 |
-- and device nodes that are not associated with any routes |
| 136 |
EventInterest { |
| 137 |
Constraint { "event.type", "=", "node-params-changed" }, |
| 138 |
Constraint { "event.subject.param-id", "=", "Props" }, |
| 139 |
Constraint { "media.class", "matches", "Audio/*" }, |
| 140 |
Constraint { "device.routes", "is-absent" }, |
| 141 |
}, |
| 142 |
EventInterest { |
| 143 |
Constraint { "event.type", "=", "node-params-changed" }, |
| 144 |
Constraint { "event.subject.param-id", "=", "Props" }, |
| 145 |
Constraint { "media.class", "matches", "Audio/*" }, |
| 146 |
Constraint { "device.routes", "equals", "0" }, |
| 147 |
}, |
| 148 |
}, |
| 149 |
execute = function (event) |
| 150 |
local node = event:get_subject () |
| 151 |
local stream_props = node.properties |
| 152 |
stream_props = JsonUtils.match_rules_update_properties (config.rules, stream_props) |
| 153 |
|
| 154 |
if Settings.get_boolean ("node.stream.restore-props") and stream_props ["state.restore-props"] ~= "false" |
| 155 |
then |
| 156 |
local key = formKey (stream_props) |
| 157 |
if not key then |
| 158 |
return |
| 159 |
end |
| 160 |
|
| 161 |
local stored_values = getStoredStreamProps (key) or {} |
| 162 |
local hasChanges = false |
| 163 |
|
| 164 |
log:info (node, "saving stream props for " .. |
| 165 |
tostring (stream_props ["node.name"])) |
| 166 |
|
| 167 |
for p in node:iterate_params ("Props") do |
| 168 |
local props = cutils.parseParam (p, "Props") |
| 169 |
if not props then |
| 170 |
goto skip_prop |
| 171 |
end |
| 172 |
|
| 173 |
if props.volume ~= stored_values.volume then |
| 174 |
stored_values.volume = props.volume |
| 175 |
hasChanges = true |
| 176 |
end |
| 177 |
if props.mute ~= stored_values.mute then |
| 178 |
stored_values.mute = props.mute |
| 179 |
hasChanges = true |
| 180 |
end |
| 181 |
if props.channelVolumes then |
| 182 |
stored_values.channelVolumes = props.channelVolumes |
| 183 |
hasChanges = true |
| 184 |
end |
| 185 |
if props.channelMap then |
| 186 |
stored_values.channelMap = props.channelMap |
| 187 |
hasChanges = true |
| 188 |
end |
| 189 |
|
| 190 |
::skip_prop:: |
| 191 |
end |
| 192 |
|
| 193 |
if hasChanges then |
| 194 |
saveStreamProps (key, stored_values) |
| 195 |
end |
| 196 |
end |
| 197 |
end |
| 198 |
} |
| 199 |
|
| 200 |
-- save "target.node"/"target.object" on metadata changes |
| 201 |
store_stream_target_hook = SimpleEventHook { |
| 202 |
name = "node/store-stream-target-metadata-changed", |
| 203 |
interests = { |
| 204 |
EventInterest { |
| 205 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 206 |
Constraint { "metadata.name", "=", "default" }, |
| 207 |
Constraint { "event.subject.key", "c", "target.object", "target.node" }, |
| 208 |
}, |
| 209 |
}, |
| 210 |
execute = function (event) |
| 211 |
local source = event:get_source () |
| 212 |
local nodes_om = source:call ("get-object-manager", "node") |
| 213 |
local props = event:get_properties () |
| 214 |
local subject_id = props ["event.subject.id"] |
| 215 |
local target_key = props ["event.subject.key"] |
| 216 |
local target_value = props ["event.subject.value"] |
| 217 |
|
| 218 |
local node = nodes_om:lookup { |
| 219 |
Constraint { "bound-id", "=", subject_id, type = "gobject" } |
| 220 |
} |
| 221 |
if not node then |
| 222 |
return |
| 223 |
end |
| 224 |
|
| 225 |
local stream_props = node.properties |
| 226 |
stream_props = JsonUtils.match_rules_update_properties (config.rules, stream_props) |
| 227 |
|
| 228 |
if stream_props ["state.restore-target"] == "false" then |
| 229 |
return |
| 230 |
end |
| 231 |
|
| 232 |
local key = formKey (stream_props) |
| 233 |
if not key then |
| 234 |
return |
| 235 |
end |
| 236 |
|
| 237 |
local target_name = nil |
| 238 |
|
| 239 |
if target_value and target_value ~= "-1" then |
| 240 |
local target_node |
| 241 |
if target_key == "target.object" then |
| 242 |
target_node = nodes_om:lookup { |
| 243 |
Constraint { "object.serial", "=", target_value, type = "pw-global" } |
| 244 |
} |
| 245 |
else |
| 246 |
target_node = nodes_om:lookup { |
| 247 |
Constraint { "bound-id", "=", target_value, type = "gobject" } |
| 248 |
} |
| 249 |
end |
| 250 |
if target_node then |
| 251 |
target_name = target_node.properties ["node.name"] |
| 252 |
end |
| 253 |
end |
| 254 |
|
| 255 |
log:info (node, "saving stream target for " .. |
| 256 |
tostring (stream_props ["node.name"]) .. " -> " .. tostring (target_name)) |
| 257 |
|
| 258 |
local stored_values = getStoredStreamProps (key) or {} |
| 259 |
stored_values.target = target_name |
| 260 |
saveStreamProps (key, stored_values) |
| 261 |
end |
| 262 |
} |
| 263 |
|
| 264 |
-- populate route-settings metadata |
| 265 |
function populateMetadata (metadata) |
| 266 |
-- copy state into the metadata |
| 267 |
local key = "Output/Audio:media.role:Notification" |
| 268 |
local p = getStoredStreamProps (key) |
| 269 |
if p then |
| 270 |
p.channels = p.channelMap and Json.Array (p.channelMap) |
| 271 |
p.volumes = p.channelVolumes and Json.Array (p.channelVolumes) |
| 272 |
p.channelMap = nil |
| 273 |
p.channelVolumes = nil |
| 274 |
p.target = nil |
| 275 |
|
| 276 |
-- pipewire-pulse expects the key to be |
| 277 |
-- "restore.stream.Output/Audio.media.role:Notification" |
| 278 |
key = string.gsub (key, ":", ".", 1); |
| 279 |
metadata:set (0, "restore.stream." .. key, "Spa:String:JSON", |
| 280 |
Json.Object (p):to_string ()) |
| 281 |
end |
| 282 |
end |
| 283 |
|
| 284 |
-- track route-settings metadata changes |
| 285 |
route_settings_metadata_changed_hook = SimpleEventHook { |
| 286 |
name = "node/route-settings-metadata-changed", |
| 287 |
interests = { |
| 288 |
EventInterest { |
| 289 |
Constraint { "event.type", "=", "metadata-changed" }, |
| 290 |
Constraint { "metadata.name", "=", "route-settings" }, |
| 291 |
Constraint { "event.subject.key", "=", |
| 292 |
"restore.stream.Output/Audio.media.role:Notification" }, |
| 293 |
Constraint { "event.subject.spa_type", "=", "Spa:String:JSON" }, |
| 294 |
Constraint { "event.subject.value", "is-present" }, |
| 295 |
}, |
| 296 |
}, |
| 297 |
execute = function (event) |
| 298 |
local props = event:get_properties () |
| 299 |
local subject_id = props ["event.subject.id"] |
| 300 |
local key = props ["event.subject.key"] |
| 301 |
local value = props ["event.subject.value"] |
| 302 |
|
| 303 |
local json = Json.Raw (value) |
| 304 |
if json == nil or not json:is_object () then |
| 305 |
return |
| 306 |
end |
| 307 |
|
| 308 |
local vparsed = json:parse () |
| 309 |
|
| 310 |
-- we store the key as "Output/Audio:media.role:Notification" |
| 311 |
local key = string.sub (key, string.len ("restore.stream.") + 1) |
| 312 |
key = string.gsub (key, "%.", ":", 1); |
| 313 |
|
| 314 |
local stored_values = getStoredStreamProps (key) or {} |
| 315 |
|
| 316 |
if vparsed.volume ~= nil then |
| 317 |
stored_values.volume = vparsed.volume |
| 318 |
end |
| 319 |
if vparsed.mute ~= nil then |
| 320 |
stored_values.mute = vparsed.mute |
| 321 |
end |
| 322 |
if vparsed.channels ~= nil then |
| 323 |
stored_values.channelMap = vparsed.channels |
| 324 |
end |
| 325 |
if vparsed.volumes ~= nil then |
| 326 |
stored_values.channelVolumes = vparsed.volumes |
| 327 |
end |
| 328 |
saveStreamProps (key, stored_values) |
| 329 |
end |
| 330 |
} |
| 331 |
|
| 332 |
function buildDefaultChannelVolumes (node) |
| 333 |
local node_props = node.properties |
| 334 |
local direction = cutils.mediaClassToDirection (node_props ["media.class"] or "") |
| 335 |
local def_vol = 1.0 |
| 336 |
local channels = 2 |
| 337 |
local res = {} |
| 338 |
|
| 339 |
local str = node.properties["state.default-volume"] |
| 340 |
if str ~= nil then |
| 341 |
def_vol = tonumber (str) |
| 342 |
elseif direction == "input" then |
| 343 |
def_vol = Settings.get_float ("node.stream.default-capture-volume") |
| 344 |
elseif direction == "output" then |
| 345 |
def_vol = Settings.get_float ("node.stream.default-playback-volume") |
| 346 |
end |
| 347 |
|
| 348 |
for pod in node:iterate_params("Format") do |
| 349 |
local pod_parsed = pod:parse() |
| 350 |
if pod_parsed ~= nil then |
| 351 |
local t = type(pod_parsed.properties.channels) |
| 352 |
if t == "number" then |
| 353 |
channels = pod_parsed.properties.channels |
| 354 |
break |
| 355 |
elseif t == "table" and #pod_parsed.properties.channels > 0 then |
| 356 |
-- in some misbehaving clients a non-fixed Format may appear here, which means the number of |
| 357 |
-- channels will be some kind of choice. If this is the case, pick the first number in the |
| 358 |
-- choice (which is either the default in an enum or range, or may just happen to be the |
| 359 |
-- right number in other cases) |
| 360 |
channels = pod_parsed.properties.channels[1] |
| 361 |
break |
| 362 |
end |
| 363 |
end |
| 364 |
end |
| 365 |
|
| 366 |
log:info (node, "using default volume: " .. tostring(def_vol) .. |
| 367 |
", channels: " .. tostring(channels)) |
| 368 |
|
| 369 |
while (#res < channels) do |
| 370 |
table.insert(res, def_vol) |
| 371 |
end |
| 372 |
|
| 373 |
return res |
| 374 |
end |
| 375 |
|
| 376 |
function getStoredStreamProps (key) |
| 377 |
local value = state_table [key] |
| 378 |
if not value then |
| 379 |
return nil |
| 380 |
end |
| 381 |
|
| 382 |
local json = Json.Raw (value) |
| 383 |
if not json or not json:is_object () then |
| 384 |
return nil |
| 385 |
end |
| 386 |
|
| 387 |
return json:parse () |
| 388 |
end |
| 389 |
|
| 390 |
function saveStreamProps (key, p) |
| 391 |
assert (type (p) == "table") |
| 392 |
|
| 393 |
p.channelMap = p.channelMap and Json.Array (p.channelMap) |
| 394 |
p.channelVolumes = p.channelVolumes and Json.Array (p.channelVolumes) |
| 395 |
|
| 396 |
state_table [key] = Json.Object (p):to_string () |
| 397 |
state:save_after_timeout (state_table) |
| 398 |
end |
| 399 |
|
| 400 |
function formKey (properties) |
| 401 |
local keys = { |
| 402 |
"media.role", |
| 403 |
"application.id", |
| 404 |
"application.name", |
| 405 |
"media.name", |
| 406 |
"node.name", |
| 407 |
} |
| 408 |
local key_base = nil |
| 409 |
|
| 410 |
for _, k in ipairs (keys) do |
| 411 |
local p = properties [k] |
| 412 |
if p then |
| 413 |
key_base = string.format ("%s:%s:%s", |
| 414 |
properties ["media.class"]:gsub ("^Stream/", ""), k, p) |
| 415 |
break |
| 416 |
end |
| 417 |
end |
| 418 |
return key_base |
| 419 |
end |
| 420 |
|
| 421 |
function toggleState (enable) |
| 422 |
if enable and not state then |
| 423 |
state = State ("stream-properties") |
| 424 |
state_table = state:load () |
| 425 |
|
| 426 |
restore_stream_hook:register () |
| 427 |
store_stream_props_hook:register () |
| 428 |
store_stream_target_hook:register () |
| 429 |
route_settings_metadata_changed_hook:register () |
| 430 |
|
| 431 |
rs_metadata = ImplMetadata ("route-settings") |
| 432 |
rs_metadata:activate (Features.ALL, function (m, e) |
| 433 |
if e then |
| 434 |
log:warning ("failed to activate route-settings metadata: " .. tostring (e)) |
| 435 |
else |
| 436 |
populateMetadata (m) |
| 437 |
end |
| 438 |
end) |
| 439 |
|
| 440 |
elseif not enable and state then |
| 441 |
state = nil |
| 442 |
state_table = nil |
| 443 |
restore_stream_hook:remove () |
| 444 |
store_stream_props_hook:remove () |
| 445 |
store_stream_target_hook:remove () |
| 446 |
route_settings_metadata_changed_hook:remove () |
| 447 |
rs_metadata = nil |
| 448 |
end |
| 449 |
end |
| 450 |
|
| 451 |
Settings.subscribe ("node.stream.restore-props", function () |
| 452 |
toggleState (Settings.get_boolean ("node.stream.restore-props") or |
| 453 |
Settings.get_boolean ("node.stream.restore-target")) |
| 454 |
end) |
| 455 |
|
| 456 |
Settings.subscribe ("node.stream.restore-target", function () |
| 457 |
toggleState (Settings.get_boolean ("node.stream.restore-props") or |
| 458 |
Settings.get_boolean ("node.stream.restore-target")) |
| 459 |
end) |
| 460 |
|
| 461 |
toggleState (Settings.get_boolean ("node.stream.restore-props") or |
| 462 |
Settings.get_boolean ("node.stream.restore-target")) |