rigforge_pipewire.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_port,
  7. playback_port,
  8. resolve_host_port,
  9. ):
  10. """Translate mod-ui graph boundary cables to concrete PipeWire ports."""
  11. if port_from == "/graph/capture_1" and capture_port:
  12. return capture_port, resolve_host_port(port_to)
  13. if port_to == "/graph/playback_1" and playback_port:
  14. return resolve_host_port(port_from), playback_port
  15. return None
  16. def _link_status(source, destination, runner):
  17. try:
  18. result = runner(
  19. ["pw-link", "-l"],
  20. check=False,
  21. capture_output=True,
  22. text=True,
  23. )
  24. except OSError:
  25. return None
  26. if result.returncode != 0:
  27. return None
  28. current_port = None
  29. for line in result.stdout.splitlines():
  30. if line.startswith(" |-> "):
  31. if current_port == source and line.removeprefix(" |-> ") == destination:
  32. return True
  33. elif line.startswith(" |<- "):
  34. if current_port == destination and line.removeprefix(" |<- ") == source:
  35. return True
  36. else:
  37. current_port = line.strip()
  38. return False
  39. def connect_link(source, destination, runner=subprocess.run):
  40. try:
  41. result = runner(
  42. ["pw-link", source, destination],
  43. check=False,
  44. capture_output=True,
  45. text=True,
  46. )
  47. except OSError:
  48. return False
  49. return result.returncode == 0 or _link_status(source, destination, runner) is True
  50. def disconnect_link(source, destination, runner=subprocess.run):
  51. try:
  52. result = runner(
  53. ["pw-link", "-d", source, destination],
  54. check=False,
  55. capture_output=True,
  56. text=True,
  57. )
  58. except OSError:
  59. return False
  60. return result.returncode == 0 or _link_status(source, destination, runner) is False