feat(cameras): no frame-rate cap on live view by default (viewers get the camera's 12–20 fps)
Build & push images / images (push) Successful in 3m46s

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
This commit is contained in:
2026-11-17 16:49:52 +01:00
parent 91ebdb2bbf
commit 1c1db5c5d0
3 changed files with 29 additions and 10 deletions
+8
View File
@@ -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();
+9 -6
View File
@@ -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<typeof setTimeout> | 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;
}
+12 -4
View File
@@ -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 `<img>`/`<video>` request (the WS already solves that with a ticket). A viewer whose socket still holds > 256 KB unsent skips frames — server memory stays bounded. |
| Delivery to the page | A **WebSocket per open tile** (`/api/cameras/:id/live`), one JPEG per binary message, **latest frame only**, at the camera's own rate (no cap; `CAMERA_LIVE_MAX_FPS` can set one) | Works in the desktop shell, whose webview cannot carry the session cookie on an `<img>`/`<video>` request (the WS already solves that with a ticket). A viewer whose socket still holds > 256 KB unsent skips frames — server memory stays bounded. |
| When live | **Per camera**, while that camera pushes vehicle events (same 30 s TTL as lane busy/free) | On a shared lane (park-lab) only the camera that saw the car goes live; lane state would light both. |
| Auth | Same handshake as `/api/ws` (Origin allowlist, then desktop ticket or cookie), then `device:read` | One helper (`authenticateWsHandshake`) for every socket route, so they cannot drift. The resting snapshot needs `session:read` (it is evidence). |
| Preference | `users.open_cameras` (JSON id list, migration 0033), `PUT /api/auth/open-cameras` | Same shape as language / theme / font scale. |
@@ -88,7 +88,15 @@ agreement.
135 s stream: main-stream stills `.6` 0/169 → 0/251 HTTP 503 (avg 0.16 → 0.23 s per still —
slower, not failing); `.7` 3/159 → 4/330 (the known transient `deviceBusy`); sub-stream stills
638/638 and 757/757 HTTP 200. Server end to end: three viewers `connecting → live` in
95–148 ms, 7–9 fps each, zero malformed JPEGs. Per viewer at the 10 fps cap that is ≈ 0.5–0.8 MB/s.
95–148 ms, 7–9 fps each, zero malformed JPEGs. (That run had the then-default 10 fps cap.) Uncapped, a viewer takes ≈ 1 MB/s per camera.
- **Delay** (2026-11-17; the camera's printed clock vs arrival time on a host on the camera LAN,
camera clocks bracketed to ±2 ms over ISAPI): camera → host ≈ 0.09–0.10 s; through the server
the same (the server adds nothing measurable); the 10 fps cap then in place added ≈ 40 ms and
cut `.6` to 7 fps, so it was **removed** (user's decision); browser message → picture 1–2 ms.
A viewer on a ~300 KB/s link climbed to **13–14 s** behind within 30 s and stayed there —
frames queue in TCP buffers the server's 256 KB check cannot see. The user judged the delay a
non-issue for now; the fix, if it becomes one, is a per-frame ack from the page (send the next
frame only after the last was shown).
- **Main stream in the large view: not possible on this transport.** RTP/JPEG carries width and
height as one byte × 8 (ceiling 2040×2040), and 101 is the 4MP/8MP H.264 ANPR/evidence stream.
A true main-stream view would be a separate H.264 pipeline (depacketizer, in-browser decode,
@@ -134,7 +142,7 @@ reconnect); the close rules held.
## Not yet verified
- The Tauri/WebKitGTK shell (binary WS frames through `@tauri-apps/plugin-websocket` arrive as a
number array over IPC — at 1280×720 that is up to 10 fps × 83 KB, to be measured). Its CSP already allows `blob:`
number array over IPC — at 1280×720 uncapped that is 12–20 fps × 50–83 KB ≈ 1 MB/s per camera, to be measured). Its CSP already allows `blob:`
images.
- Through Caddy (`reverse_proxy` upgrades WebSockets; not exercised).
- The live → snapshot return 30 s after the last push in the browser (covered by unit tests only).
@@ -146,4 +154,4 @@ reconnect); the close rules held.
`packages/devices/src/drivers/rtsp-mjpeg.ts` (RTSP client), `rfc2435.ts` (frame reassembly,
fixture-tested against frames verified with a JPEG decoder), `camera.ts` (`openLiveStream` on
Hikvision); `apps/server/src/camera-live.ts` (hub), `camera-activity.ts`, `routes/cameras.ts`;
`apps/web/src/ui/CameraDock.tsx`, `ui/DeviceFooter.tsx`. Knob: `CAMERA_LIVE_MAX_FPS` (10).
`apps/web/src/ui/CameraDock.tsx`, `ui/DeviceFooter.tsx`. Knob: `CAMERA_LIVE_MAX_FPS` (unset = no cap).