suspend-node.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- WirePlumber
  2. --
  3. -- Copyright © 2021 Collabora Ltd.
  4. -- @author George Kiagiadakis <george.kiagiadakis@collabora.com>
  5. --
  6. -- SPDX-License-Identifier: MIT
  7. om = ObjectManager {
  8. Interest { type = "node",
  9. Constraint { "media.class", "matches", "Audio/*" }
  10. },
  11. Interest { type = "node",
  12. Constraint { "media.class", "matches", "Video/*" }
  13. },
  14. }
  15. sources = {}
  16. om:connect("object-added", function (om, node)
  17. node:connect("state-changed", function (node, old_state, cur_state)
  18. -- Always clear the current source if any
  19. local id = node["bound-id"]
  20. if sources[id] then
  21. sources[id]:destroy()
  22. sources[id] = nil
  23. end
  24. -- Add a timeout source if idle for at least 5 seconds
  25. if cur_state == "idle" or cur_state == "error" then
  26. -- honor "session.suspend-timeout-seconds" if specified
  27. local timeout =
  28. tonumber(node.properties["session.suspend-timeout-seconds"]) or 5
  29. if timeout == 0 then
  30. return
  31. end
  32. -- add idle timeout; multiply by 1000, timeout_add() expects ms
  33. sources[id] = Core.timeout_add(timeout * 1000, function()
  34. -- Suspend the node
  35. Log.info(node, "was idle for a while; suspending ...")
  36. node:send_command("Suspend")
  37. -- Unref the source
  38. sources[id] = nil
  39. -- false (== G_SOURCE_REMOVE) destroys the source so that this
  40. -- function does not get fired again after 5 seconds
  41. return false
  42. end)
  43. end
  44. end)
  45. end)
  46. om:activate()