| 1 |
-- WirePlumber |
| 2 |
|
| 3 |
-- Copyright © 2023 Collabora Ltd. |
| 4 |
-- @author Julian Bouzas <[email protected]> |
| 5 |
|
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
-- Script is a Lua Module of filter Lua utility functions |
| 9 |
|
| 10 |
local cutils = require ("common-utils") |
| 11 |
|
| 12 |
local module = { |
| 13 |
metadata = nil, |
| 14 |
filters = {}, |
| 15 |
} |
| 16 |
|
| 17 |
local function getFilterSmart (metadata, node) |
| 18 |
-- Check metadata |
| 19 |
if metadata ~= nil then |
| 20 |
local id = node["bound-id"] |
| 21 |
local value_str = metadata:find (id, "filter.smart") |
| 22 |
if value_str ~= nil then |
| 23 |
local json = Json.Raw (value_str) |
| 24 |
if json:is_boolean() then |
| 25 |
return json:parse() |
| 26 |
end |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
-- Check node properties |
| 31 |
local prop_str = node.properties ["filter.smart"] |
| 32 |
if prop_str ~= nil then |
| 33 |
return cutils.parseBool (prop_str) |
| 34 |
end |
| 35 |
|
| 36 |
-- Otherwise consider the filter not smart by default |
| 37 |
return false |
| 38 |
end |
| 39 |
|
| 40 |
local function getFilterSmartName (metadata, node) |
| 41 |
-- Check metadata |
| 42 |
if metadata ~= nil then |
| 43 |
local id = node["bound-id"] |
| 44 |
local value_str = metadata:find (id, "filter.smart.name") |
| 45 |
if value_str ~= nil then |
| 46 |
local json = Json.Raw (value_str) |
| 47 |
if json:is_string() then |
| 48 |
return json:parse() |
| 49 |
end |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
-- Check node properties |
| 54 |
local prop_str = node.properties ["filter.smart.name"] |
| 55 |
if prop_str ~= nil then |
| 56 |
return prop_str |
| 57 |
end |
| 58 |
|
| 59 |
-- Otherwise use link group as name |
| 60 |
return node.properties ["node.link-group"] |
| 61 |
end |
| 62 |
|
| 63 |
local function getFilterSmartDisabled (metadata, node) |
| 64 |
-- Check metadata |
| 65 |
if metadata ~= nil then |
| 66 |
local id = node["bound-id"] |
| 67 |
local value_str = metadata:find (id, "filter.smart.disabled") |
| 68 |
if value_str ~= nil then |
| 69 |
local json = Json.Raw (value_str) |
| 70 |
if json:is_boolean() then |
| 71 |
return json:parse() |
| 72 |
end |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
-- Check node properties |
| 77 |
local prop_str = node.properties ["filter.smart.disabled"] |
| 78 |
if prop_str ~= nil then |
| 79 |
return cutils.parseBool (prop_str) |
| 80 |
end |
| 81 |
|
| 82 |
-- Otherwise consider the filter not disabled by default |
| 83 |
return false |
| 84 |
end |
| 85 |
|
| 86 |
local function getFilterSmartTargetable (metadata, node) |
| 87 |
-- Check metadata |
| 88 |
if metadata ~= nil then |
| 89 |
local id = node["bound-id"] |
| 90 |
local value_str = metadata:find (id, "filter.smart.targetable") |
| 91 |
if value_str ~= nil then |
| 92 |
local json = Json.Raw (value_str) |
| 93 |
if json:is_boolean() then |
| 94 |
return json:parse() |
| 95 |
end |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
-- Check node properties |
| 100 |
local prop_str = node.properties ["filter.smart.targetable"] |
| 101 |
if prop_str ~= nil then |
| 102 |
return cutils.parseBool (prop_str) |
| 103 |
end |
| 104 |
|
| 105 |
-- Otherwise consider the filter not targetable by default |
| 106 |
return false |
| 107 |
end |
| 108 |
|
| 109 |
local function getFilterSmartTarget (metadata, node, om) |
| 110 |
-- Check metadata and fallback to properties |
| 111 |
local id = node["bound-id"] |
| 112 |
local value_str = nil |
| 113 |
if metadata ~= nil then |
| 114 |
value_str = metadata:find (id, "filter.smart.target") |
| 115 |
end |
| 116 |
if value_str == nil then |
| 117 |
value_str = node.properties ["filter.smart.target"] |
| 118 |
if value_str == nil then |
| 119 |
return nil |
| 120 |
end |
| 121 |
end |
| 122 |
|
| 123 |
-- Parse match rules |
| 124 |
local match_rules_json = Json.Raw (value_str) |
| 125 |
if not match_rules_json:is_object () then |
| 126 |
return nil |
| 127 |
end |
| 128 |
local match_rules = match_rules_json:parse () |
| 129 |
|
| 130 |
-- Find target |
| 131 |
local target = nil |
| 132 |
for si_target in om:iterate { type = "SiLinkable" } do |
| 133 |
local n_target = si_target:get_associated_proxy ("node") |
| 134 |
if n_target == nil then |
| 135 |
goto skip_target |
| 136 |
end |
| 137 |
|
| 138 |
-- Target nodes cannot be smart filters |
| 139 |
if n_target.properties ["node.link-group"] ~= nil and |
| 140 |
getFilterSmart (metadata, n_target) then |
| 141 |
goto skip_target |
| 142 |
end |
| 143 |
|
| 144 |
-- Make sure the target node properties match all rules |
| 145 |
for key, val in pairs(match_rules) do |
| 146 |
if n_target.properties[key] ~= tostring (val) then |
| 147 |
goto skip_target |
| 148 |
end |
| 149 |
end |
| 150 |
|
| 151 |
-- Target found |
| 152 |
target = si_target |
| 153 |
break; |
| 154 |
|
| 155 |
::skip_target:: |
| 156 |
end |
| 157 |
|
| 158 |
return target |
| 159 |
end |
| 160 |
|
| 161 |
local function getFilterSmartTargetless (metadata, node) |
| 162 |
local id = node["bound-id"] |
| 163 |
local value_str = nil |
| 164 |
if metadata ~= nil then |
| 165 |
value_str = metadata:find (id, "filter.smart.target") |
| 166 |
end |
| 167 |
if value_str == nil then |
| 168 |
value_str = node.properties ["filter.smart.target"] |
| 169 |
end |
| 170 |
|
| 171 |
return value_str == nil |
| 172 |
end |
| 173 |
|
| 174 |
local function getFilterSmartBefore (metadata, node) |
| 175 |
-- Check metadata and fallback to properties |
| 176 |
local id = node["bound-id"] |
| 177 |
local value_str = nil |
| 178 |
if metadata ~= nil then |
| 179 |
value_str = metadata:find (id, "filter.smart.before") |
| 180 |
end |
| 181 |
if value_str == nil then |
| 182 |
value_str = node.properties ["filter.smart.before"] |
| 183 |
if value_str == nil then |
| 184 |
return nil |
| 185 |
end |
| 186 |
end |
| 187 |
|
| 188 |
-- Parse |
| 189 |
local before_json = Json.Raw (value_str) |
| 190 |
if not before_json:is_array() then |
| 191 |
return nil |
| 192 |
end |
| 193 |
return before_json:parse () |
| 194 |
end |
| 195 |
|
| 196 |
local function getFilterSmartAfter (metadata, node) |
| 197 |
-- Check metadata and fallback to properties |
| 198 |
local id = node["bound-id"] |
| 199 |
local value_str = nil |
| 200 |
if metadata ~= nil then |
| 201 |
value_str = metadata:find (id, "filter.smart.after") |
| 202 |
end |
| 203 |
if value_str == nil then |
| 204 |
value_str = node.properties ["filter.smart.after"] |
| 205 |
if value_str == nil then |
| 206 |
return nil |
| 207 |
end |
| 208 |
end |
| 209 |
|
| 210 |
-- Parse |
| 211 |
local after_json = Json.Raw (value_str) |
| 212 |
if not after_json:is_array() then |
| 213 |
return nil |
| 214 |
end |
| 215 |
return after_json:parse () |
| 216 |
end |
| 217 |
|
| 218 |
local function insertFilterSorted (curr_filters, filter) |
| 219 |
local before_filters = {} |
| 220 |
local after_filters = {} |
| 221 |
local new_filters = {} |
| 222 |
|
| 223 |
-- Check if the current filters need to be inserted before or after |
| 224 |
for i, v in ipairs(curr_filters) do |
| 225 |
local insert_before = true |
| 226 |
local insert_after = false |
| 227 |
|
| 228 |
if v.before ~= nil then |
| 229 |
for j, b in ipairs(v.before) do |
| 230 |
if filter.name == b then |
| 231 |
insert_after = false |
| 232 |
break |
| 233 |
end |
| 234 |
end |
| 235 |
end |
| 236 |
|
| 237 |
if v.after ~= nil then |
| 238 |
for j, b in ipairs(v.after) do |
| 239 |
if filter.name == b then |
| 240 |
insert_before = false |
| 241 |
break |
| 242 |
end |
| 243 |
end |
| 244 |
end |
| 245 |
|
| 246 |
if filter.before ~= nil then |
| 247 |
for j, b in ipairs(filter.before) do |
| 248 |
if v.name == b then |
| 249 |
insert_after = true |
| 250 |
end |
| 251 |
end |
| 252 |
end |
| 253 |
|
| 254 |
if filter.after ~= nil then |
| 255 |
for j, b in ipairs(filter.after) do |
| 256 |
if v.name == b then |
| 257 |
insert_before = true |
| 258 |
end |
| 259 |
end |
| 260 |
end |
| 261 |
|
| 262 |
if insert_before then |
| 263 |
if insert_after then |
| 264 |
Log.warning ("cyclic before/after found in filters " .. v.name .. " and " .. filter.name) |
| 265 |
end |
| 266 |
table.insert (before_filters, v) |
| 267 |
else |
| 268 |
table.insert (after_filters, v) |
| 269 |
end |
| 270 |
|
| 271 |
end |
| 272 |
|
| 273 |
-- Add the filters to the new table stored |
| 274 |
for i, v in ipairs(before_filters) do |
| 275 |
table.insert (new_filters, v) |
| 276 |
end |
| 277 |
table.insert (new_filters, filter) |
| 278 |
for i, v in ipairs(after_filters) do |
| 279 |
table.insert (new_filters, v) |
| 280 |
end |
| 281 |
|
| 282 |
return new_filters |
| 283 |
end |
| 284 |
|
| 285 |
local function rescanFilters (om, metadata_om) |
| 286 |
local metadata = |
| 287 |
metadata_om:lookup { Constraint { "metadata.name", "=", "filters" } } |
| 288 |
|
| 289 |
-- Always clear all filters data on rescan |
| 290 |
module.filters = {} |
| 291 |
|
| 292 |
Log.info ("rescanning filters...") |
| 293 |
|
| 294 |
for si in om:iterate { type = "SiLinkable" } do |
| 295 |
local filter = {} |
| 296 |
|
| 297 |
local n = si:get_associated_proxy ("node") |
| 298 |
if n == nil then |
| 299 |
goto skip_linkable |
| 300 |
end |
| 301 |
|
| 302 |
-- Only handle nodes with link group (filters) |
| 303 |
filter.link_group = n.properties ["node.link-group"] |
| 304 |
if filter.link_group == nil then |
| 305 |
goto skip_linkable |
| 306 |
end |
| 307 |
|
| 308 |
-- Only handle the main filter nodes |
| 309 |
filter.media_class = n.properties ["media.class"] |
| 310 |
if string.find (filter.media_class, "Stream") then |
| 311 |
goto skip_linkable |
| 312 |
end |
| 313 |
|
| 314 |
-- Filter direction |
| 315 |
if string.find (filter.media_class, "Audio/Sink") or |
| 316 |
string.find (filter.media_class, "Video/Sink") then |
| 317 |
filter.direction = "input" |
| 318 |
else |
| 319 |
filter.direction = "output" |
| 320 |
end |
| 321 |
|
| 322 |
-- Filter media type |
| 323 |
filter.media_type = si.properties["media.type"] |
| 324 |
|
| 325 |
-- Get filter properties |
| 326 |
filter.smart = getFilterSmart (metadata, n) |
| 327 |
filter.name = getFilterSmartName (metadata, n) |
| 328 |
filter.disabled = getFilterSmartDisabled (metadata, n) |
| 329 |
filter.targetable = getFilterSmartTargetable (metadata, n) |
| 330 |
filter.target = getFilterSmartTarget (metadata, n, om) |
| 331 |
filter.targetless = getFilterSmartTargetless (metadata, n) |
| 332 |
filter.before = getFilterSmartBefore (metadata, n) |
| 333 |
filter.after = getFilterSmartAfter (metadata, n) |
| 334 |
|
| 335 |
-- Add the main and stream session items |
| 336 |
filter.main_si = si |
| 337 |
filter.stream_si = om:lookup { |
| 338 |
type = "SiLinkable", |
| 339 |
Constraint { "node.link-group", "=", filter.link_group }, |
| 340 |
Constraint { "media.class", "#", "Stream/*", type = "pw-global" } |
| 341 |
} |
| 342 |
|
| 343 |
-- Add the filter to the list sorted by before and after |
| 344 |
module.filters = insertFilterSorted (module.filters, filter) |
| 345 |
|
| 346 |
::skip_linkable:: |
| 347 |
end |
| 348 |
|
| 349 |
end |
| 350 |
|
| 351 |
SimpleEventHook { |
| 352 |
name = "lib/filter-utils/rescan", |
| 353 |
before = "linking/rescan", |
| 354 |
interests = { |
| 355 |
EventInterest { |
| 356 |
Constraint { "event.type", "=", "rescan-for-linking" }, |
| 357 |
}, |
| 358 |
}, |
| 359 |
execute = function (event) |
| 360 |
local source = event:get_source () |
| 361 |
local om = source:call ("get-object-manager", "session-item") |
| 362 |
local metadata_om = source:call ("get-object-manager", "metadata") |
| 363 |
|
| 364 |
rescanFilters (om, metadata_om) |
| 365 |
end |
| 366 |
}:register () |
| 367 |
|
| 368 |
function module.is_filter_smart (direction, link_group) |
| 369 |
-- Make sure direction and link_group is valid |
| 370 |
if direction == nil or link_group == nil then |
| 371 |
return false |
| 372 |
end |
| 373 |
|
| 374 |
for i, v in ipairs(module.filters) do |
| 375 |
if v.direction == direction and v.link_group == link_group then |
| 376 |
return v.smart |
| 377 |
end |
| 378 |
end |
| 379 |
|
| 380 |
return false |
| 381 |
end |
| 382 |
|
| 383 |
function module.is_filter_disabled (direction, link_group) |
| 384 |
-- Make sure direction and link_group is valid |
| 385 |
if direction == nil or link_group == nil then |
| 386 |
return false |
| 387 |
end |
| 388 |
|
| 389 |
for i, v in ipairs(module.filters) do |
| 390 |
if v.direction == direction and v.link_group == link_group then |
| 391 |
return v.disabled |
| 392 |
end |
| 393 |
end |
| 394 |
|
| 395 |
return false |
| 396 |
end |
| 397 |
|
| 398 |
function module.is_filter_targetable (direction, link_group) |
| 399 |
-- Make sure direction and link_group is valid |
| 400 |
if direction == nil or link_group == nil then |
| 401 |
return false |
| 402 |
end |
| 403 |
|
| 404 |
for i, v in ipairs(module.filters) do |
| 405 |
if v.direction == direction and v.link_group == link_group then |
| 406 |
return v.targetable |
| 407 |
end |
| 408 |
end |
| 409 |
|
| 410 |
return false |
| 411 |
end |
| 412 |
|
| 413 |
function module.get_filter_target (direction, link_group) |
| 414 |
-- Make sure direction and link_group are valid |
| 415 |
if direction == nil or link_group == nil then |
| 416 |
return nil |
| 417 |
end |
| 418 |
|
| 419 |
-- Find the current filter |
| 420 |
local filter = nil |
| 421 |
local index = nil |
| 422 |
for i, v in ipairs(module.filters) do |
| 423 |
if v.direction == direction and |
| 424 |
v.link_group == link_group and |
| 425 |
not v.disabled and |
| 426 |
v.smart then |
| 427 |
filter = v |
| 428 |
index = i |
| 429 |
break |
| 430 |
end |
| 431 |
end |
| 432 |
if filter == nil then |
| 433 |
return nil |
| 434 |
end |
| 435 |
|
| 436 |
-- Return the next filter with matching target |
| 437 |
for i, v in ipairs(module.filters) do |
| 438 |
if v.direction == direction and |
| 439 |
v.media_type == filter.media_type and |
| 440 |
v.name ~= filter.name and |
| 441 |
v.link_group ~= link_group and |
| 442 |
not v.disabled and |
| 443 |
v.smart and |
| 444 |
((v.target == nil and filter.target == nil) or |
| 445 |
(v.target ~= nil and filter.target ~= nil and v.target.id == filter.target.id)) and |
| 446 |
i > index then |
| 447 |
return v.main_si |
| 448 |
end |
| 449 |
end |
| 450 |
|
| 451 |
-- Otherwise return the filter destination target |
| 452 |
return filter.target |
| 453 |
end |
| 454 |
|
| 455 |
function module.get_filter_from_target (direction, media_type, si_target) |
| 456 |
local target = si_target |
| 457 |
|
| 458 |
-- Make sure direction and media_type are valid |
| 459 |
if direction == nil or media_type == nil then |
| 460 |
return nil |
| 461 |
end |
| 462 |
|
| 463 |
-- If si_target is a filter, find it and use its target |
| 464 |
if si_target then |
| 465 |
local target_node = si_target:get_associated_proxy ("node") |
| 466 |
local target_link_group = target_node.properties ["node.link-group"] |
| 467 |
if target_link_group ~= nil then |
| 468 |
local filter = nil |
| 469 |
for i, v in ipairs(module.filters) do |
| 470 |
if v.direction == direction and |
| 471 |
v.media_type == media_type and |
| 472 |
v.link_group == target_link_group and |
| 473 |
not v.disabled and |
| 474 |
v.smart then |
| 475 |
filter = v |
| 476 |
break |
| 477 |
end |
| 478 |
end |
| 479 |
if filter == nil then |
| 480 |
return nil |
| 481 |
end |
| 482 |
target = filter.target |
| 483 |
end |
| 484 |
end |
| 485 |
|
| 486 |
-- Find the first filter matching target |
| 487 |
for i, v in ipairs(module.filters) do |
| 488 |
if v.direction == direction and |
| 489 |
v.media_type == media_type and |
| 490 |
not v.disabled and |
| 491 |
v.smart and |
| 492 |
((v.target ~= nil and target ~= nil and v.target.id == target.id) or |
| 493 |
(target == nil and v.targetless)) then |
| 494 |
return v.main_si |
| 495 |
end |
| 496 |
end |
| 497 |
|
| 498 |
return nil |
| 499 |
end |
| 500 |
|
| 501 |
return module |