| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import { spawn, execSync } from "node:child_process";
- import { createJpegFramer } from "./jpeg-framer.mjs";
- const HW_ENCODERS = ["vaapijpegenc"];
- let _jpegEncoder;
- export function detectJpegEncoder() {
- if (_jpegEncoder !== undefined) return _jpegEncoder;
- for (const enc of HW_ENCODERS) {
- try {
- execSync(`gst-inspect-1.0 ${enc}`, { stdio: "ignore" });
- _jpegEncoder = enc;
- return enc;
- } catch { /* not available */ }
- }
- _jpegEncoder = "jpegenc";
- return _jpegEncoder;
- }
- /** @param {{ fps: number; quality: number; width?: number; height?: number }} opts */
- export function buildPipeline(source, { fps, quality, width, height }) {
- let pipeline = `${source} ! videoconvert`;
- if (width && height) {
- pipeline += ` ! videoscale ! video/x-raw,width=${width},height=${height}`;
- }
- pipeline += ` ! videorate drop-only=true max-rate=${fps}`;
- pipeline += ` ! queue max-size-buffers=1 max-size-bytes=0 max-size-time=0 leaky=downstream`;
- const encoder = detectJpegEncoder();
- pipeline += ` ! ${encoder} quality=${quality}`;
- pipeline += ` ! fdsink fd=1 sync=false`;
- return pipeline;
- }
- /**
- * @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`;
- }
|