gstreamer.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { spawn, execSync } from "node:child_process";
  2. import { createJpegFramer } from "./jpeg-framer.mjs";
  3. const HW_ENCODERS = ["vaapijpegenc"];
  4. let _jpegEncoder;
  5. export function detectJpegEncoder() {
  6. if (_jpegEncoder !== undefined) return _jpegEncoder;
  7. for (const enc of HW_ENCODERS) {
  8. try {
  9. execSync(`gst-inspect-1.0 ${enc}`, { stdio: "ignore" });
  10. _jpegEncoder = enc;
  11. return enc;
  12. } catch { /* not available */ }
  13. }
  14. _jpegEncoder = "jpegenc";
  15. return _jpegEncoder;
  16. }
  17. /** @param {{ fps: number; quality: number; width?: number; height?: number }} opts */
  18. export function buildPipeline(source, { fps, quality, width, height }) {
  19. let pipeline = `${source} ! videoconvert`;
  20. if (width && height) {
  21. pipeline += ` ! videoscale ! video/x-raw,width=${width},height=${height}`;
  22. }
  23. pipeline += ` ! videorate drop-only=true max-rate=${fps}`;
  24. pipeline += ` ! queue max-size-buffers=1 max-size-bytes=0 max-size-time=0 leaky=downstream`;
  25. const encoder = detectJpegEncoder();
  26. pipeline += ` ! ${encoder} quality=${quality}`;
  27. pipeline += ` ! fdsink fd=1 sync=false`;
  28. return pipeline;
  29. }
  30. /**
  31. * @param {object} opts
  32. * @param {string} opts.pipeline
  33. * @param {number | undefined} opts.pipeWireFd - if set, duplicated to child as fd 3
  34. * @param {(frame: Buffer) => void} opts.onFrame
  35. * @param {(err: Error) => void} [opts.onSpawnError]
  36. * @param {(code: number | null, signal?: string) => void} [opts.onExit]
  37. */
  38. export function startGstreamer(opts) {
  39. const { pipeline, pipeWireFd, onFrame, onSpawnError, onExit } = opts;
  40. const stdio =
  41. pipeWireFd !== undefined && pipeWireFd !== null
  42. ? ["ignore", "pipe", "pipe", pipeWireFd]
  43. : ["ignore", "pipe", "pipe"];
  44. const argv = ["-q", ...pipeline.trim().split(/\s+/)];
  45. const child = spawn("gst-launch-1.0", argv, {
  46. stdio,
  47. });
  48. if (!child.stdout) {
  49. onSpawnError?.(new Error("gstreamer: no stdout"));
  50. return { child: null, stop: () => {} };
  51. }
  52. const framer = createJpegFramer(onFrame);
  53. child.stdout.on("data", (chunk) => framer.push(chunk));
  54. child.stderr?.on("data", (d) => {
  55. const s = d.toString().trim();
  56. if (s) console.error("[gstreamer]", s);
  57. });
  58. child.on("error", (err) => onSpawnError?.(err));
  59. child.on("exit", (code, signal) => onExit?.(code, signal));
  60. function stop() {
  61. child.kill("SIGTERM");
  62. }
  63. return { child, stop };
  64. }
  65. export function defaultSourceForSession() {
  66. const session = process.env.XDG_SESSION_TYPE || "";
  67. if (session === "x11") {
  68. return "ximagesrc use-damage=0 show-pointer=true";
  69. }
  70. return "pipewiresrc do-timestamp=true keepalive-time=1000";
  71. }
  72. /** PipeWire fd is passed to the child as stdio fd 3 (see spawn stdio array). */
  73. export function pipewireSourceForChild(nodeId) {
  74. return `pipewiresrc fd=3 path=${nodeId} do-timestamp=true keepalive-time=1000`;
  75. }