live ˆÃ ƒqÇßÁÀ+-- WirePlumber -- Copyright © 2024 Collabora Ltd. -- @author Julian Bouzas -- SPDX-License-Identifier: MIT -- audio-group.lua script takes pipewire audio stream nodes and groups them -- into a single unit by creating a loopback filter per group. The grouping -- is done by a common ancestor 'audio-group-namespace' process name. agutils = require ("audio-group-utils") PW_AUDIO_NAMESPACE = "pw-audio-namespace" node_directions = {} group_loopback_modules = {} group_loopback_modules["input"] = {} group_loopback_modules["output"] = {} function GetNodeDirection (id, props) if string.find (props["media.class"], "Stream/Input/Audio") then return "input" elseif string.find (props["media.class"], "Stream/Output/Audio") then return "output" end return nil end function GetNodeAudioGroup (pid) local group = nil local target_object = nil -- We group a processes by PW_AUDIO_NAMESPACE. ancestor local curr_pid = pid while curr_pid ~= 0 do local pid_info = ProcUtils.get_proc_info (curr_pid) local arg0 = pid_info:get_arg (0) -- Check if ancestor process name is PW_AUDIO_NAMESPACE if arg0 ~= nil and string.find (arg0, PW_AUDIO_NAMESPACE, 1, true) then -- Check if the PW_AUDIO_NAMESPACE has a defined target for i = 0, pid_info:get_n_args () - 1, 1 do local argn = pid_info:get_arg (i) -- Ignore any args after '--' if argn == "--" then break end -- Get target node id value if any if (argn == "--target-object") or (argn == "-t") then target_object = pid_info:get_arg (i + 1) break end end -- We name the audio group as PW_AUDIO_NAMESPACE. group = PW_AUDIO_NAMESPACE .. "." .. tostring(curr_pid) break end curr_pid = pid_info:get_parent_pid () end return group, target_object end function CreateStreamLoopback (props, group, target_object, direction) local is_input = direction == "input" and true or false -- Set stream properties local stream_props = {} stream_props["node.name"] = "stream.audio_group:" .. group stream_props["node.description"] = "Stream Audio Group for " .. group stream_props["media.class"] = is_input and "Stream/Input/Audio" or "Stream/Output/Audio" stream_props["node.passive"] = true stream_props["session.audio-group"] = group if target_object ~= nil then stream_props["target.object"] = tostring (target_object) end -- Set device properties local device_props = {} device_props["node.name"] = "device.audio_group:" .. group device_props["node.description"] = "Device Audio Group for " .. group device_props["media.class"] = is_input and "Audio/Source" or "Audio/Sink" device_props["session.audio-group"] = group -- Set loopback module args local args = Json.Object { ["capture.props"] = Json.Object (is_input and stream_props or device_props), ["playback.props"] = Json.Object (is_input and device_props or stream_props) } -- Create module return LocalModule("libpipewire-module-loopback", args:get_data(), {}) end SimpleEventHook { name = "lib/audio-group-utils/create-audio-group-loopback", interests = { -- on linkable added or removed, where linkable is adapter or plain node EventInterest { Constraint { "event.type", "=", "node-added" }, Constraint { "media.class", "#", "Stream/*Audio*", type = "pw-global" }, Constraint { "stream.monitor", "!", "true", type = "pw" }, Constraint { "node.link-group", "-" }, }, }, execute = function (event) local node = event:get_subject () local source = event:get_source () local client_om = source:call ("get-object-manager", "client") local id = node.id local bound_id = node["bound-id"] local stream_props = node.properties local stream_name = stream_props["node.name"] -- Get client local client = client_om:lookup { Constraint { "bound-id", "=", stream_props["client.id"], type = "gobject"} } i