From 1c1db5c5d0b90a5ef9bb80a8029388d6f94b962f Mon Sep 17 00:00:00 2001 From: Julian Cuni Date: Tue, 17 Nov 2026 16:49:52 +0100 Subject: [PATCH] =?UTF-8?q?feat(cameras):=20no=20frame-rate=20cap=20on=20l?= =?UTF-8?q?ive=20view=20by=20default=20(viewers=20get=20the=20camera's=201?= =?UTF-8?q?2=E2=80=9320=20fps)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Measured: the 10 fps cap cut the 12.5 fps entry camera to ~7 fps and added ~40 ms; the server otherwise adds no measurable delay. CAMERA_LIVE_MAX_FPS still sets a cap when wanted. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4 --- apps/server/src/camera-live.test.ts | 8 ++++++++ apps/server/src/camera-live.ts | 15 +++++++++------ wiki/decisions/camera-live-view.md | 16 ++++++++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/apps/server/src/camera-live.test.ts b/apps/server/src/camera-live.test.ts index 5cb88cb..e01dd4d 100644 --- a/apps/server/src/camera-live.test.ts +++ b/apps/server/src/camera-live.test.ts @@ -118,6 +118,14 @@ describe("CameraLiveHub", () => { expect(slow.frames).toHaveLength(1); }); + it("sends every frame when no cap is set (the default)", () => { + const hub = new CameraLiveHub(() => device, logger); + const v = viewer(); + hub.subscribe("cam1", v); + for (let i = 0; i < 5; i++) sources[0]!.frame(); // same instant: all pass + expect(v.frames).toHaveLength(5); + }); + it("reconnects with backoff while viewers remain", async () => { const hub = new CameraLiveHub(() => device, logger); const v = viewer(); diff --git a/apps/server/src/camera-live.ts b/apps/server/src/camera-live.ts index 3ae744a..4c183d1 100644 --- a/apps/server/src/camera-live.ts +++ b/apps/server/src/camera-live.ts @@ -8,8 +8,9 @@ import type { LiveStreamDevice, LiveVideoSource } from "@parking/devices"; // // Frames are complete JPEGs (the driver reassembles RTP/JPEG; nothing is decoded here). // Delivery is LATEST-FRAME-ONLY: a viewer whose socket still holds unsent bytes simply -// misses frames, so a slow client can never grow server memory, and a per-viewer frame -// cap keeps the desktop shell's IPC light. Advisory viewing only: nothing here touches +// misses frames, so a slow client can never grow server memory. No frame-rate cap by +// default — viewers get the camera's own rate (measured: a cap only dropped frames and +// added ~40 ms); CAMERA_LIVE_MAX_FPS sets one if a client ever needs it. Advisory viewing only: nothing here touches // the ledger, a gate or a barrier. See wiki/decisions/camera-live-view.md. export type LiveStatus = @@ -31,7 +32,7 @@ export type LiveResolver = (deviceId: string) => LiveStreamDevice | string; export interface CameraLiveOptions { /** Keep the upstream this long after the last viewer leaves (re-render churn). */ lingerMs?: number; - /** Per-viewer frame cap. The cameras send 12.5–20 fps; 10 is plenty to watch a car. */ + /** Per-viewer frame cap; 0 / unset = none (the camera's own rate, 12.5–20 fps). */ maxFps?: number; /** Backoff ceiling for reconnects while viewers are present. */ maxRetryMs?: number; @@ -49,9 +50,10 @@ interface Upstream { retry: ReturnType | null; } +/** Optional per-viewer cap from CAMERA_LIVE_MAX_FPS; 0 (the default) means no cap. */ export function liveMaxFps(): number { - const raw = Number(process.env.CAMERA_LIVE_MAX_FPS ?? 10); - return Number.isFinite(raw) && raw > 0 ? raw : 10; + const raw = Number(process.env.CAMERA_LIVE_MAX_FPS ?? 0); + return Number.isFinite(raw) && raw > 0 ? raw : 0; } export class CameraLiveHub { @@ -67,7 +69,8 @@ export class CameraLiveHub { this.#resolve = resolve; this.#logger = logger; this.#lingerMs = opts.lingerMs ?? 5000; - this.#minFrameGapMs = 1000 / (opts.maxFps ?? liveMaxFps()); + const fps = opts.maxFps ?? liveMaxFps(); + this.#minFrameGapMs = fps > 0 ? 1000 / fps : 0; this.#maxRetryMs = opts.maxRetryMs ?? 30_000; this.#unavailableRetryMs = opts.unavailableRetryMs ?? 60_000; } diff --git a/wiki/decisions/camera-live-view.md b/wiki/decisions/camera-live-view.md index 8ca1f20..c6984be 100644 --- a/wiki/decisions/camera-live-view.md +++ b/wiki/decisions/camera-live-view.md @@ -47,7 +47,7 @@ evidence still (full resolution when the camera's snapshot stream is Main). | Video source | The camera's **sub-stream (x02) set to MJPEG**, pulled over **RTSP** | The G2 firmware answers the HTTP MJPEG preview (`/ISAPI/Streaming/channels/102/httpPreview`) with `notSupport`; RTSP offers MJPEG as RTP/JPEG (RFC 2435). Every MJPEG frame is a whole picture, so a view is live ~150 ms after connect — H.264 would wait for a keyframe (every 2.5–4 s on these cameras) and need a decoder. | | Where it runs | **In the server process**, as a driver capability (`LiveStreamDevice`) in `packages/devices` | No sidecar container (camera config + `localAddress` already live in the DB; the [[device-adapter-pattern]] keeps the protocol in the driver). The server reassembles JPEG frames from RTP packets — about 450 small packets/s for two cameras; it never decodes or re-encodes. | | Fan-out | **One RTSP session per camera**, shared by every viewer, torn down 5 s after the last leaves | A camera has few stream slots and the [[lane-presence-and-anpr-entry|ANPR loop]] pulls stills from the same unit. | -| Delivery to the page | A **WebSocket per open tile** (`/api/cameras/:id/live`), one JPEG per binary message, **latest frame only**, capped at 10 fps per viewer | Works in the desktop shell, whose webview cannot carry the session cookie on an ``/`