fallback-sink.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. -- WirePlumber
  2. --
  3. -- Copyright © 2021 Collabora Ltd.
  4. -- @author Frédéric Danis <frederic.danis@collabora.com>
  5. --
  6. -- SPDX-License-Identifier: MIT
  7. local sink_ids = {}
  8. local fallback_node = nil
  9. node_om = ObjectManager {
  10. Interest {
  11. type = "node",
  12. Constraint { "media.class", "matches", "Audio/Sink", type = "pw-global" },
  13. -- Do not consider endpoints created by WirePlumber
  14. Constraint { "wireplumber.is-endpoint", "!", true, type = "pw" },
  15. -- or the fallback sink itself
  16. Constraint { "wireplumber.is-fallback", "!", true, type = "pw" },
  17. }
  18. }
  19. function createFallbackSink()
  20. if fallback_node then
  21. return
  22. end
  23. Log.info("Create fallback sink")
  24. local properties = {}
  25. properties["node.name"] = "auto_null"
  26. properties["node.description"] = "Dummy Output"
  27. properties["audio.rate"] = 48000
  28. properties["audio.channels"] = 2
  29. properties["audio.position"] = "FL,FR"
  30. properties["media.class"] = "Audio/Sink"
  31. properties["factory.name"] = "support.null-audio-sink"
  32. properties["node.virtual"] = "true"
  33. properties["monitor.channel-volumes"] = "true"
  34. properties["wireplumber.is-fallback"] = "true"
  35. properties["priority.session"] = 500
  36. fallback_node = LocalNode("adapter", properties)
  37. fallback_node:activate(Feature.Proxy.BOUND)
  38. end
  39. function checkSinks()
  40. local sink_ids_items = 0
  41. for _ in pairs(sink_ids) do sink_ids_items = sink_ids_items + 1 end
  42. if sink_ids_items > 0 then
  43. if fallback_node then
  44. Log.info("Remove fallback sink")
  45. fallback_node = nil
  46. end
  47. elseif not fallback_node then
  48. createFallbackSink()
  49. end
  50. end
  51. function checkSinksAfterTimeout()
  52. if timeout_source then
  53. timeout_source:destroy()
  54. end
  55. timeout_source = Core.timeout_add(1000, function ()
  56. checkSinks()
  57. timeout_source = nil
  58. end)
  59. end
  60. node_om:connect("object-added", function (_, node)
  61. Log.debug("object added: " .. node.properties["object.id"] .. " " ..
  62. tostring(node.properties["node.name"]))
  63. sink_ids[node.properties["object.id"]] = node.properties["node.name"]
  64. checkSinksAfterTimeout()
  65. end)
  66. node_om:connect("object-removed", function (_, node)
  67. Log.debug("object removed: " .. node.properties["object.id"] .. " " ..
  68. tostring(node.properties["node.name"]))
  69. sink_ids[node.properties["object.id"]] = nil
  70. checkSinksAfterTimeout()
  71. end)
  72. node_om:activate()
  73. checkSinksAfterTimeout()