intended-roles.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. -- WirePlumber
  2. --
  3. -- Copyright © 2021 Asymptotic
  4. -- @author Arun Raghavan <arun@asymptotic.io>
  5. --
  6. -- SPDX-License-Identifier: MIT
  7. --
  8. -- Route streams of a given role (media.role property) to devices that are
  9. -- intended for that role (device.intended-roles property)
  10. metadata_om = ObjectManager {
  11. Interest {
  12. type = "metadata",
  13. Constraint { "metadata.name", "=", "default" },
  14. }
  15. }
  16. devices_om = ObjectManager {
  17. Interest {
  18. type = "node",
  19. Constraint { "media.class", "matches", "Audio/*", type = "pw-global" },
  20. Constraint { "device.intended-roles", "is-present", type = "pw" },
  21. }
  22. }
  23. streams_om = ObjectManager {
  24. Interest {
  25. type = "node",
  26. Constraint { "media.class", "matches", "Stream/*/Audio", type = "pw-global" },
  27. Constraint { "media.role", "is-present", type = "pw-global" }
  28. }
  29. }
  30. local function routeUsingIntendedRole(stream, dev)
  31. local stream_role = stream.properties["media.role"]
  32. local is_input = stream.properties["media.class"]:find("Input") ~= nil
  33. local is_source = dev.properties["media.class"]:find("Source") ~= nil
  34. local dev_roles = dev.properties["device.intended-roles"]
  35. -- Make sure the stream and device direction match
  36. if is_input ~= is_source then
  37. return
  38. end
  39. for role in dev_roles:gmatch("(%a+)") do
  40. if role == stream_role then
  41. Log.info(stream,
  42. string.format("Routing stream '%s' (%d) with role '%s' to '%s' (%d)",
  43. stream.properties["node.name"], stream["bound-id"], stream_role,
  44. dev.properties["node.name"], dev["bound-id"])
  45. )
  46. local metadata = metadata_om:lookup()
  47. metadata:set(stream["bound-id"], "target.node", "Spa:Id", dev["bound-id"])
  48. end
  49. end
  50. end
  51. streams_om:connect("object-added", function (streams_om, stream)
  52. for dev in devices_om:iterate() do
  53. routeUsingIntendedRole(stream, dev)
  54. end
  55. end)
  56. devices_om:connect("object-added", function (devices_om, dev)
  57. for stream in streams_om:iterate() do
  58. routeUsingIntendedRole(stream, dev)
  59. end
  60. end)
  61. metadata_om:activate()
  62. devices_om:activate()
  63. streams_om:activate()