mod_ui_pipewire.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """PipeWire link helpers used by the Rigforge mod-ui host integration."""
  2. import subprocess
  3. def boundary_connection(
  4. port_from,
  5. port_to,
  6. capture_ports,
  7. playback_ports,
  8. resolve_host_port,
  9. ):
  10. """Translate mod-ui graph boundary cables to concrete PipeWire ports."""
  11. capture_symbol = port_from.rsplit("/", 1)[-1]
  12. capture_port = capture_ports.get(capture_symbol)
  13. if capture_port:
  14. return capture_port, resolve_host_port(port_to)
  15. playback_symbol = port_to.rsplit("/", 1)[-1]
  16. playback_port = playback_ports.get(playback_symbol)
  17. if playback_port:
  18. return resolve_host_port(port_from), playback_port
  19. return None
  20. def _link_status(source, destination, runner):
  21. try:
  22. result = runner(
  23. ["pw-link", "-l"],
  24. check=False,
  25. capture_output=True,
  26. text=True,
  27. )
  28. except OSError:
  29. return None
  30. if result.returncode != 0:
  31. return None
  32. current_port = None
  33. for line in result.stdout.splitlines():
  34. if line.startswith(" |-> "):
  35. if current_port == source and line.removeprefix(" |-> ") == destination:
  36. return True
  37. elif line.startswith(" |<- "):
  38. if current_port == destination and line.removeprefix(" |<- ") == source:
  39. return True
  40. else:
  41. current_port = line.strip()
  42. return False
  43. def connect_link(source, destination, runner=subprocess.run):
  44. try:
  45. result = runner(
  46. ["pw-link", source, destination],
  47. check=False,
  48. capture_output=True,
  49. text=True,
  50. )
  51. except OSError:
  52. return False
  53. return result.returncode == 0 or _link_status(source, destination, runner) is True
  54. def disconnect_link(source, destination, runner=subprocess.run):
  55. try:
  56. result = runner(
  57. ["pw-link", "-d", source, destination],
  58. check=False,
  59. capture_output=True,
  60. text=True,
  61. )
  62. except OSError:
  63. return False
  64. return result.returncode == 0 or _link_status(source, destination, runner) is False