| 45 | const zoomWide = () => tx(0x0100, [0x81, 0x01, 0x04, 0x47, 0, 0, 0, 0, 0xff]); |
| 46 | |
| 47 | function captureOnce(secs: number): Promise<Uint8Array[]> { |
| 48 | return new Promise((resolve) => { |
| 49 | const ff = spawn("ffmpeg", [ |
| 50 | // NOTE: no -rw_timeout — this ffmpeg build rejects it for RTSP |
| 51 | // ("Option rw_timeout not found") and exits instantly with 0 frames. |
| 52 | // The SIGKILL watchdog below is the hang protection. |
| 53 | "-hide_banner", "-loglevel", "error", "-rtsp_transport", "tcp", "-i", RTSP, |
| 54 | "-vf", `scale=${W}:${H},format=gray`, "-t", String(secs), "-f", "rawvideo", "pipe:1", |
| 55 | ]); |
| 56 | // Belt & braces: the camera's RTSP server can wedge and feed nothing. |
| 57 | const watchdog = setTimeout(() => ff.kill("SIGKILL"), (secs + 12) * 1000); |
| 58 | const fb = W * H; let buf = Buffer.alloc(0); const frames: Uint8Array[] = []; |
| 59 | ff.stdout.on("data", (c: Buffer) => { |
| 60 | buf = Buffer.concat([buf, c]); |
| 61 | while (buf.length >= fb) { frames.push(new Uint8Array(buf.subarray(0, fb))); buf = buf.subarray(fb); } |
| 62 | }); |
| 63 | ff.on("close", () => { clearTimeout(watchdog); resolve(frames); }); |
| 64 | }); |
| 65 | } |
| 66 | const med = (a: number[]): number => a.slice().sort((x, y) => x - y)[a.length >> 1] ?? 0; |
| 67 | const ax = (s: { dx: number; dy: number }, axis: "pan" | "tilt") => Math.abs(axis === "pan" ? s.dx : s.dy); |
| 68 | |