Files
parking_solution/wiki/decisions/camera-live-view.md
T

150 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: decision
tags: [parking, camera, ui, booth, video, hikvision]
sources: []
updated: 2026-11-17
status: settled
---
# Camera Live View — Booth Camera Tiles
The booth operator can open a small tile per camera from the [[device-status-monitoring|device
footer]] and watch that camera live while a vehicle is in front of it. **BUILT 2026-11-17**;
verified end to end against the two park-lab cameras through the real server and in a browser
(not yet in the [[desktop-shell-tauri|desktop shell]] — see *Not yet verified*).
## What the user asked for (2026-11-17)
- The camera chips in the footer are clickable; a click opens that camera's tile in the bottom
right, about 15 % of the screen wide. A second camera's tile stacks to the **left** of the first.
- A tile closes with ✕; which tiles are open is a **user preference** (restored on login).
- A tile rests on the **latest entry/exit snapshot** of that camera; when the camera's vehicle
events reach the booth, it shows **live video**.
- (2026-11-17, second round) Live shows a **pulsing green LIVE badge**. A click on the picture opens a
**large view** with a **Live / Snapshot switch** whose Live streams **on demand** — car or no
car — so a camera-started stream can be stopped to look at the last snapshot and a
snapshot can be switched to live. Closing the large view returns the tile to automatic: a
stream the user started stops; a stream the camera started carries on in the tile.
- (2026-11-17, third round) The large view **opens in the tile's state**: on Live when the camera
is active (the same stream carries on, no reconnect), else on the Snapshot. The choice then
holds while the view is open — a car leaving does not drop it to the snapshot, a car arriving
does not switch it to live.
### When the stream stops
A tile streams while its camera is active — **30 s after the camera's last vehicle push**
(`LANE_BUSY_TTL_MS`, shared with the lane lights; the camera re-pushes every 1–3 s while a car
moves and every 15–25 s while it stands, so a waiting car keeps it live) — or while the large view
is open on Live. It also stops on ✕, logout, or a dropped booth feed (resumes on reconnect). The
camera session closes 5 s after the last viewer anywhere stops (RTSP TEARDOWN). No upper bound.
The large live view is the 1280×720 sub-stream (the same stream as the tile); the Snapshot side shows the stored
evidence still (full resolution when the camera's snapshot stream is Main).
## Decision
| Question | Choice | Why |
|---|---|---|
| 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. |
| 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. |
**Advisory viewing only.** Nothing here writes to the ledger, gates a ticket or opens a barrier.
### Not a reversal of the "livestream" rejection
[[lane-presence-and-anpr-entry]] rejected *vision monitoring the RTSP livestream continuously for
presence* — inference on every frame, 24/7. This is an operator **watching** video, only while a
vehicle is present, with no inference at all.
## Measured on park-lab (2026-11-17)
Cameras (read from `ISAPI/System/deviceInfo` the same day): `10.0.10.6` entry =
**DS-2CD1083G2-LIU** (8MP, V5.8.4), `10.0.10.7` exit = **DS-2CD1043G2-LIU** (4MP, V5.8.10).
Both offer `H.264, H.265, MJPEG` on channel 102 and only two streams (101, 102). Channel 102 on
both was switched from H.264 to **MJPEG** (640×360; other settings unchanged) with the user's
agreement.
- **RTSP:** first frame 110–155 ms after PLAY; `.6` 12.5 fps ≈ 310 KB/s, `.7` 20 fps ≈ 270 KB/s;
RFC 2435 type 1 (4:2:0), Q 63 / 48 (no in-band tables), no restart markers, padding on each
frame's last packet. `Session: …;timeout=60`; a 150 s session ran without drops with and
without keepalive (a `GET_PARAMETER` is sent every timeout/2 anyway).
- **Interference with ANPR stills** (what was measured — one camera at a time, one stream):
- main-stream stills (`101/picture`, back-to-back, ~15–20 s) with an RTSP stream starting
mid-run: `.6` 0/87 → 0/110 HTTP 503; `.7` 4/55 → 4/71 (the `.7` 503s occur without the
stream too — the transient `deviceBusy` the driver retries). Starting a stream added none.
- sub-stream stills (`102/picture`, back-to-back) while 102 streams MJPEG over RTSP for 180 s:
`.6` 2121/2121 and `.7` 3187/3187 HTTP 200 (baselines without the stream, 60 s: 672/672 and
594/594); the stream held 12.0 / 20.0 fps throughout.
- Not measured: several live sessions on one camera at once (the hub opens one), a stream
alongside a browser left on the camera's web UI, and the G3H model.
- `102/picture` returns a 640×360 JPEG after the codec switch.
- **Raised to 1280×720 MJPEG** (both cameras, same day, user's approval, for the large view; only
width/height changed). RTSP: `.6` 12.2 fps ≈ 1030 KB/s, frames ≈ 83 KB; `.7` 20 fps ≈ 990 KB/s,
frames ≈ 49 KB; first frame 120–190 ms; RFC 2435 header still type 1, Q 63 / 48. Over a
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.
- **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,
keyframe wait, unverified in the desktop shell).
- **End to end** (server + scratch DB + both cameras, 2026-11-17): a simulated vehicle push →
`camera-activity` on `/api/ws`; two viewers on the entry camera and one on the exit camera went
`connecting → live` in 151 / 181 ms, 8–10 fps each, zero malformed JPEGs.
## Install step (new)
Set the camera's **sub-stream (102) video encoding to MJPEG** (web UI: Configuration → Video/Audio
→ Video → Sub Stream, or `PUT /ISAPI/Streaming/channels/102` with `videoCodecType=MJPEG`). With
H.264 the tile says *"camera stream is H264, not MJPEG"* and stays on the snapshot. Added to
[[site-device-installation]] §3. On a camera whose snapshot stream is set to **Sub** (the G3H
workaround), the sub-stream resolution is both the ANPR/evidence still and the live view — MJPEG on
the G2 offers 640×360, 640×480 or 1280×720 on channel 102; park-lab runs 1280×720 (~1 MB/s per camera
while live, ~300 KB/s at 640×360). Dahua cameras have no live view yet (tile shows the reason).
## Consequences elsewhere
- [[passage-confirmation]] planned to decode the **H.264** sub-stream. On a camera set up for live
view that stream is now MJPEG: OpenCV decodes it too, every frame is a keyframe (the "≤ 1 s
I-frame" requirement disappears), at ~5–10× the bandwidth (~300 KB/s per camera on the device
VLAN). The two consumers should share one RTSP session per camera when that is built.
- **Unverified, probably broken today:** the existing snapshot thumbnails
(`SnapshotStrip`, `WashDesk`) use `<img src="/api/snapshots/:id">` — a relative, cookie-authed
URL that the desktop shell's webview cannot authenticate. The camera tile fetches its snapshot
through `platformFetch` instead. Check on a desktop install.
## Verified in a browser (2026-11-17)
Headless Firefox against the server with both park-lab cameras and a simulated vehicle push:
footer chips open tiles bottom-right (≈ 15 % wide, first opened rightmost, next to its left), each
resting on its camera's latest snapshot with direction and time; after the entry camera's push only
the entry tile went **LIVE** (live OSD clock); a reload restored both tiles; ✕ closed one and the
saved preference followed. Second round: the large view on an idle camera streamed live on
demand; Snapshot/Live switched both ways; closing it stopped the user's stream; on a camera-started
stream, Snapshot in the large view stopped it and closing the view resumed it in the tile. Third round (no camera credentials
in that run, so no frames — the socket count and the switch state were checked): an idle tile opens
the large view on Snapshot with no socket; an active tile opens it on Live on the same socket (no
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:`
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).
- A real vehicle push from the cameras (park-lab's alarm server is not set yet — see
[[passage-confirmation]] park-lab rig).
## Where the code is
`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).