| 1 |
-- WirePlumber |
| 2 |
-- |
| 3 |
-- Copyright © 2021 Collabora Ltd. |
| 4 |
-- @author George Kiagiadakis <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
log = Log.open_topic ("s-node") |
| 9 |
|
| 10 |
sources = {} |
| 11 |
|
| 12 |
SimpleEventHook { |
| 13 |
name = "node/suspend-node", |
| 14 |
interests = { |
| 15 |
EventInterest { |
| 16 |
Constraint { "event.type", "=", "node-state-changed" }, |
| 17 |
Constraint { "media.class", "matches", "Audio/*" }, |
| 18 |
}, |
| 19 |
EventInterest { |
| 20 |
Constraint { "event.type", "=", "node-state-changed" }, |
| 21 |
Constraint { "media.class", "matches", "Video/*" }, |
| 22 |
}, |
| 23 |
}, |
| 24 |
execute = function (event) |
| 25 |
local node = event:get_subject () |
| 26 |
local new_state = event:get_properties ()["event.subject.new-state"] |
| 27 |
|
| 28 |
log:debug (node, "changed state to " .. new_state) |
| 29 |
|
| 30 |
-- Always clear the current source if any |
| 31 |
local id = node["bound-id"] |
| 32 |
if sources[id] then |
| 33 |
sources[id]:destroy() |
| 34 |
sources[id] = nil |
| 35 |
end |
| 36 |
|
| 37 |
-- Add a timeout source if idle for at least 5 seconds |
| 38 |
if new_state == "idle" or new_state == "error" then |
| 39 |
-- honor "session.suspend-timeout-seconds" if specified |
| 40 |
local timeout = |
| 41 |
tonumber(node.properties["session.suspend-timeout-seconds"]) or 5 |
| 42 |
|
| 43 |
if timeout == 0 then |
| 44 |
return |
| 45 |
end |
| 46 |
|
| 47 |
-- add idle timeout; multiply by 1000, timeout_add() expects ms |
| 48 |
sources[id] = Core.timeout_add(timeout * 1000, function() |
| 49 |
-- Suspend the node |
| 50 |
-- but check first if the node still exists |
| 51 |
if (node:get_active_features() & Feature.Proxy.BOUND) ~= 0 then |
| 52 |
log:info(node, "was idle for a while; suspending ...") |
| 53 |
node:send_command("Suspend") |
| 54 |
end |
| 55 |
|
| 56 |
-- Unref the source |
| 57 |
sources[id] = nil |
| 58 |
|
| 59 |
-- false (== G_SOURCE_REMOVE) destroys the source so that this |
| 60 |
-- function does not get fired again after 5 seconds |
| 61 |
return false |
| 62 |
end) |
| 63 |
end |
| 64 |
end |
| 65 |
}:register () |