| 1 |
-- WirePlumber |
| 2 |
|
| 3 |
-- Copyright © 2022 Collabora Ltd. |
| 4 |
-- @author Ashok Sidipotu <[email protected]> |
| 5 |
|
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
-- Script is a Lua Module of common Lua utility functions |
| 9 |
|
| 10 |
local cutils = {} |
| 11 |
|
| 12 |
function cutils.parseBool (var) |
| 13 |
return var and (var:lower () == "true" or var == "1") |
| 14 |
end |
| 15 |
|
| 16 |
function cutils.parseParam (param, id) |
| 17 |
local props = param:parse () |
| 18 |
if props.pod_type == "Object" and props.object_id == id then |
| 19 |
return props.properties |
| 20 |
else |
| 21 |
return nil |
| 22 |
end |
| 23 |
end |
| 24 |
|
| 25 |
function cutils.mediaClassToDirection (media_class) |
| 26 |
if media_class:find ("Sink") or |
| 27 |
media_class:find ("Input") or |
| 28 |
media_class:find ("Duplex") then |
| 29 |
return "input" |
| 30 |
elseif media_class:find ("Source") or media_class:find ("Output") then |
| 31 |
return "output" |
| 32 |
else |
| 33 |
return nil |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
function cutils.getTargetDirection (properties) |
| 38 |
local target_direction = nil |
| 39 |
|
| 40 |
if properties ["item.node.direction"] == "output" or |
| 41 |
(properties ["item.node.direction"] == "input" and |
| 42 |
cutils.parseBool (properties ["stream.capture.sink"])) then |
| 43 |
target_direction = "input" |
| 44 |
else |
| 45 |
target_direction = "output" |
| 46 |
end |
| 47 |
return target_direction |
| 48 |
end |
| 49 |
|
| 50 |
local default_nodes = Plugin.find ("default-nodes-api") |
| 51 |
|
| 52 |
function cutils.getDefaultNode (properties, target_direction) |
| 53 |
local target_media_class = |
| 54 |
properties ["media.type"] .. |
| 55 |
(target_direction == "input" and "/Sink" or "/Source") |
| 56 |
|
| 57 |
if not default_nodes then |
| 58 |
default_nodes = Plugin.find ("default-nodes-api") |
| 59 |
end |
| 60 |
|
| 61 |
return default_nodes:call ("get-default-node", target_media_class) |
| 62 |
end |
| 63 |
|
| 64 |
cutils.source_plugin = nil |
| 65 |
cutils.object_managers = {} |
| 66 |
|
| 67 |
function cutils.get_object_manager (name) |
| 68 |
cutils.source_plugin = cutils.source_plugin or |
| 69 |
Plugin.find ("standard-event-source") |
| 70 |
cutils.object_managers [name] = cutils.object_managers [name] or |
| 71 |
cutils.source_plugin:call ("get-object-manager", name) |
| 72 |
return cutils.object_managers [name] |
| 73 |
end |
| 74 |
|
| 75 |
function cutils.get_default_metadata_object () |
| 76 |
return cutils.get_object_manager ("metadata"):lookup { |
| 77 |
Constraint { "metadata.name", "=", "default" }, |
| 78 |
} |
| 79 |
end |
| 80 |
|
| 81 |
function cutils.arrayContains (a, value) |
| 82 |
for _, v in ipairs (a) do |
| 83 |
if v == value then |
| 84 |
return true |
| 85 |
end |
| 86 |
end |
| 87 |
return false |
| 88 |
end |
| 89 |
|
| 90 |
function cutils.get_application_name () |
| 91 |
return Core.get_properties()["application.name"] or "WirePlumber" |
| 92 |
end |
| 93 |
|
| 94 |
return cutils |