gstreamer.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ! video/x-raw,framerate=${fps}/1`;
  24. const encoder = detectJpegEncoder();
  25. pipeline += ` ! ${encoder} quality=${quality}`;
  26. pipeline += ` ! fdsink fd=1 sync=false`;
  27. return pipeline;
  28. }
  29. /**
  30. * @param {object} opts
  31. * @param {string} opts.pipeline
  32. * @param {number | undefined} opts.pipeWireFd - if set, duplicated to child as fd 3
  33. * @param {(frame: Buffer) => void} opts.onFrame
  34. * @param {(err: Error) => void} [opts.onSpawnError]
  35. * @param {(code: number | null, signal?: string) => void} [opts.onExit]
  36. */
  37. export function startGstreamer(opts) {
  38. const { pipeline, pipeWireFd, onFrame, onSpawnError, onExit } = opts;
  39. const stdio =
  40. pipeWireFd !== undefined && pipeWireFd !== null
  41. ? ["ignore", "pipe", "pipe", pipeWireFd]
  42. : ["ignore", "pipe", "pipe"];
  43. const argv = ["-q", ...pipeline.trim().split(/\s+/)];
  44. const child = spawn("gst-launch-1.0", argv, {
  45. stdio,
  46. });
  47. if (!child.stdout) {
  48. onSpawnError?.(new Error("gstreamer: no stdout"));
  49. return { child: null, stop: () => {} };
  50. }
  51. const framer = createJpegFramer(onFrame);
  52. child.stdout.on("data", (chunk) => framer.push(chunk));
  53. child.stderr?.on("data", (d) => {
  54. const s = d.toString().trim();
  55. if (s) console.error("[gstreamer]", s);
  56. });
  57. child.on("error", (err) => onSpawnError?.(err));
  58. child.on("exit", (code, signal) => onExit?.(code, signal));
  59. function stop() {
  60. child.kill("SIGTERM");
  61. }
  62. return { child, stop };
  63. }
  64. export function defaultSourceForSession() {
  65. const session = process.env.XDG_SESSION_TYPE || "";
  66. if (session === "x11") {
  67. return "ximagesrc use-damage=0 show-pointer=true";
  68. }
  69. return "pipewiresrc do-timestamp=true keepalive-time=1000";
  70. }
  71. /** PipeWire fd is passed to the child as stdio fd 3 (see spawn stdio array). */
  72. export function pipewireSourceForChild(nodeId) {
  73. return `pipewiresrc fd=3 path=${nodeId} do-timestamp=true keepalive-time=1000`;
  74. }