wayland-python-bridge.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { spawn } from "node:child_process";
  2. import { fileURLToPath } from "node:url";
  3. import { dirname, join } from "node:path";
  4. import { createJpegFramer } from "./jpeg-framer.mjs";
  5. const __dirname = dirname(fileURLToPath(import.meta.url));
  6. const SCRIPT = join(__dirname, "wayland-capture-bridge.py");
  7. /**
  8. * Portal + GStreamer in Python (OpenPipeWireRemote works with dbus-python).
  9. * Emits the same JPEG stream as startGstreamer on stdout.
  10. */
  11. export function startPythonWaylandBridge(opts) {
  12. const { fps, quality, onFrame, onSpawnError, onExit } = opts;
  13. const child = spawn("python3", [SCRIPT, "--fps", String(fps), "--quality", String(quality)], {
  14. stdio: ["ignore", "pipe", "pipe"],
  15. });
  16. if (!child.stdout) {
  17. onSpawnError?.(new Error("python bridge: no stdout"));
  18. return { child: null, stop: () => {} };
  19. }
  20. const framer = createJpegFramer(onFrame);
  21. child.stdout.on("data", (chunk) => framer.push(chunk));
  22. child.stderr?.on("data", (d) => {
  23. process.stderr.write(d);
  24. });
  25. child.on("error", (err) => onSpawnError?.(err));
  26. child.on("exit", (code, signal) => onExit?.(code, signal));
  27. function stop() {
  28. child.kill("SIGTERM");
  29. }
  30. return { child, stop };
  31. }