| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { spawn } from "node:child_process";
- import { createJpegFramer } from "./jpeg-framer.mjs";
- /** @param {{ fps: number; quality: number }} opts */
- export function buildPipeline(source, { fps, quality }) {
- return (
- `${source} ! videoconvert ! videoscale ! video/x-raw,width=1280,height=720 ! ` +
- `videorate ! video/x-raw,framerate=${fps}/1 ! jpegenc quality=${quality} ! ` +
- `fdsink fd=1 sync=false`
- );
- }
- /**
- * @param {object} opts
- * @param {string} opts.pipeline
- * @param {number | undefined} opts.pipeWireFd - if set, duplicated to child as fd 3
- * @param {(frame: Buffer) => void} opts.onFrame
- * @param {(err: Error) => void} [opts.onSpawnError]
- * @param {(code: number | null, signal?: string) => void} [opts.onExit]
- */
- export function startGstreamer(opts) {
- const { pipeline, pipeWireFd, onFrame, onSpawnError, onExit } = opts;
- const stdio =
- pipeWireFd !== undefined && pipeWireFd !== null
- ? ["ignore", "pipe", "pipe", pipeWireFd]
- : ["ignore", "pipe", "pipe"];
- const argv = ["-q", ...pipeline.trim().split(/\s+/)];
- const child = spawn("gst-launch-1.0", argv, {
- stdio,
- });
- if (!child.stdout) {
- onSpawnError?.(new Error("gstreamer: no stdout"));
- return { child: null, stop: () => {} };
- }
- const framer = createJpegFramer(onFrame);
- child.stdout.on("data", (chunk) => framer.push(chunk));
- child.stderr?.on("data", (d) => {
- const s = d.toString().trim();
- if (s) console.error("[gstreamer]", s);
- });
- child.on("error", (err) => onSpawnError?.(err));
- child.on("exit", (code, signal) => onExit?.(code, signal));
- function stop() {
- child.kill("SIGTERM");
- }
- return { child, stop };
- }
- export function defaultSourceForSession() {
- const session = process.env.XDG_SESSION_TYPE || "";
- if (session === "x11") {
- return "ximagesrc use-damage=0 show-pointer=true";
- }
- return "pipewiresrc do-timestamp=true keepalive-time=1000";
- }
- /** PipeWire fd is passed to the child as stdio fd 3 (see spawn stdio array). */
- export function pipewireSourceForChild(nodeId) {
- return `pipewiresrc fd=3 path=${nodeId} do-timestamp=true keepalive-time=1000`;
- }
|