2 Commits

Author SHA1 Message Date
julian 2c3bdb7f34 wiki: camera live view — large view, Live/Snapshot switch, when the stream stops
Build & push images / images (push) Successful in 3m31s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
2026-11-17 14:14:20 +01:00
julian 301aefc186 feat(cameras): green pulsing LIVE badge; large view with live-on-demand and a Live/Snapshot switch
Clicking a tile's picture opens a large view that streams live on demand (vehicle event
or not), with a Live / Snapshot switch — a camera-started stream can be stopped to see the
last snapshot, and a snapshot can be switched to live. Closing the view returns the tile to
automatic: a user-started stream stops, a camera-started one carries on in the tile. The
tile and the large view share one live socket. Live is marked by a pulsing green badge.

Verified in headless Firefox against the park-lab cameras.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RZgP5oMncSaVmKRKk1UvN4
2026-11-17 14:14:20 +01:00
4 changed files with 232 additions and 47 deletions
+3
View File
@@ -270,6 +270,9 @@ export const en: Catalog = {
checkedAt: "checked {{time}}",
},
camera: {
expand: "Show larger",
snapshot: "Snapshot",
liveOnDemand: "Live on request",
live: "Live",
showTile: "Show this camera",
hideTile: "Hide this camera",
+3
View File
@@ -272,6 +272,9 @@ export const sq = {
checkedAt: "kontrolluar {{time}}",
},
camera: {
expand: "Shfaq më të madhe",
snapshot: "Foto",
liveOnDemand: "Live me kërkesë",
live: "Live",
showTile: "Shfaq këtë kamerë",
hideTile: "Fshih këtë kamerë",
+208 -46
View File
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { useTranslation } from "react-i18next";
import { useQuery } from "@tanstack/react-query";
import { fetchLatestCameraSnapshot, fetchSnapshotObjectUrl, type DeviceStatus } from "../api.js";
@@ -11,9 +12,12 @@ import { createPlatformSocket } from "../lib/platform-ws.js";
// chips in the device footer; docked bottom-right, the first opened rightmost and each
// next one to its left. A tile rests on the camera's newest entry/exit snapshot and
// switches to live video while that camera is seeing a vehicle (the server's per-camera
// activity, pushed over /api/ws). Live frames come over their own socket as JPEG
// binaries; only the newest frame is shown. Advisory viewing — nothing here gates
// anything.
// activity, pushed over /api/ws). Clicking the picture opens a large view that streams
// LIVE ON DEMAND (car or no car) with a Live / Snapshot switch; closing it returns the
// tile to automatic — a stream the user started stops, one the camera started carries
// on. Live frames come over their own socket as JPEG binaries; only the newest frame is
// shown, and the tile and the large view share that one socket. Advisory viewing —
// nothing here gates anything.
type LiveState =
| { state: "connecting" }
@@ -69,20 +73,42 @@ function CameraTile({
onClose: () => void;
}) {
const { t } = useTranslation();
/** The camera is reporting a vehicle (server-side activity, 30 s after its last push). */
const active = useLiveStore((s) => s.activeCameras[deviceId] === true);
const live = useLiveFrames(deviceId, active);
/** Large view open, and what the user picked in it (opens on "live"). */
const [expanded, setExpanded] = useState(false);
const [modalMode, setModalMode] = useState<"live" | "snapshot">("live");
// Modal open → the user's choice rules; closed → automatic (live while a vehicle is in view).
const wantLive = expanded ? modalMode === "live" : active;
const live = useLiveFrames(deviceId, wantLive);
const snapshot = useLatestSnapshot(deviceId, canSeeSnapshots);
const showLive = active && live.hasFrame;
const showLive = wantLive && live.hasFrame;
const caption = wantLive
? showLive
? active
? t("camera.vehicleInView")
: t("camera.liveOnDemand")
: live.status.state === "unavailable" || live.status.state === "retrying"
? `${t("camera.liveUnavailable")}: ${live.status.reason}`
: t("camera.connecting")
: snapshot.meta
? `${snapshot.meta.direction === "entry" ? t("pay.snapEntry") : t("pay.snapExit")} · ${formatClock(snapshot.meta.capturedAt)}`
: "";
const view = (slot: "tile" | "modal") => (
<CameraView
imgRef={live.imgRefs[slot]}
showLive={showLive}
snapshotUrl={snapshot.url}
placeholder={canSeeSnapshots ? t("camera.noSnapshot") : t("camera.idle")}
caption={caption}
large={slot === "modal"}
/>
);
return (
<div className="pointer-events-auto flex w-[15vw] min-w-[200px] flex-col overflow-hidden rounded-term border border-term-border bg-term-panel shadow-2xl">
<div className="flex items-center gap-2 border-b border-term-border bg-term-panel-2 px-2 py-1 text-[0.6875rem]">
{showLive ? (
<span className="flex items-center gap-1 font-semibold uppercase tracking-wider text-term-red">
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-term-red" />
{t("camera.live")}
</span>
) : null}
<span className="min-w-0 flex-1 truncate font-semibold text-term-text">{title}</span>
<button
type="button"
@@ -94,37 +120,158 @@ function CameraTile({
✕
</button>
</div>
<div className="relative aspect-video bg-black">
{/* Live frames are painted straight into this <img> (no re-render per frame). */}
<img
ref={live.imgRef}
alt=""
className={`absolute inset-0 h-full w-full object-contain ${showLive ? "" : "hidden"}`}
/>
{!showLive && snapshot.url && (
<img src={snapshot.url} alt="" className="absolute inset-0 h-full w-full object-contain" />
)}
{!showLive && !snapshot.url && (
<div className="absolute inset-0 flex items-center justify-center px-2 text-center text-[0.6875rem] text-term-muted">
{canSeeSnapshots ? t("camera.noSnapshot") : t("camera.idle")}
</div>
)}
<div className="absolute bottom-0 left-0 right-0 truncate bg-black/60 px-1.5 py-0.5 text-[0.625rem] text-term-muted">
{active && !showLive
? live.status.state === "unavailable" || live.status.state === "retrying"
? `${t("camera.liveUnavailable")}: ${live.status.reason}`
: t("camera.connecting")
: !showLive && snapshot.meta
? `${snapshot.meta.direction === "entry" ? t("pay.snapEntry") : t("pay.snapExit")} · ${formatClock(snapshot.meta.capturedAt)}`
: showLive
? t("camera.vehicleInView")
: ""}
</div>
</div>
{/* The picture is the expand control: a click opens the large view, live. */}
<button
type="button"
onClick={() => {
setModalMode("live");
setExpanded(true);
}}
className="block w-full cursor-zoom-in"
title={t("camera.expand")}
aria-label={t("camera.expand")}
>
{view("tile")}
</button>
{expanded && (
<CameraModal title={title} mode={modalMode} onMode={setModalMode} onClose={() => setExpanded(false)}>
{view("modal")}
</CameraModal>
)}
</div>
);
}
/** The picture area shared by the tile and the large view: live frames (painted
* straight into the <img>, no re-render per frame) or the resting snapshot. */
function CameraView({
imgRef,
showLive,
snapshotUrl,
placeholder,
caption,
large,
}: {
imgRef: (el: HTMLImageElement | null) => void;
showLive: boolean;
snapshotUrl: string | null;
placeholder: string;
caption: string;
large: boolean;
}) {
return (
<div className="relative aspect-video w-full bg-black">
<img ref={imgRef} alt="" className={`absolute inset-0 h-full w-full object-contain ${showLive ? "" : "hidden"}`} />
{!showLive && snapshotUrl && (
<img src={snapshotUrl} alt="" className="absolute inset-0 h-full w-full object-contain" />
)}
{!showLive && !snapshotUrl && (
<div className="absolute inset-0 flex items-center justify-center px-2 text-center text-[0.6875rem] text-term-muted">
{placeholder}
</div>
)}
{showLive && <LiveBadge large={large} />}
{caption && (
<div
className={`absolute bottom-0 left-0 right-0 truncate bg-black/60 text-left text-term-muted ${
large ? "px-3 py-1 text-[0.8125rem]" : "px-1.5 py-0.5 text-[0.625rem]"
}`}
>
{caption}
</div>
)}
</div>
);
}
/** "● LIVE" with a pulsing green dot, over the video's top-left corner. */
function LiveBadge({ large }: { large: boolean }) {
const { t } = useTranslation();
return (
<span
className={`absolute left-1.5 top-1.5 flex items-center gap-1.5 rounded-full bg-black/70 font-semibold uppercase tracking-wider text-term-green ${
large ? "px-2.5 py-1 text-[0.8125rem]" : "px-1.5 py-0.5 text-[0.625rem]"
}`}
>
<span className="relative flex h-2 w-2">
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-term-green opacity-75" />
<span className="relative inline-flex h-2 w-2 rounded-full bg-term-green" />
</span>
{t("camera.live")}
</span>
);
}
/** Large view of one camera with a Live / Snapshot switch. Closes on ✕, Escape or a
* backdrop click. */
function CameraModal({
title,
mode,
onMode,
onClose,
children,
}: {
title: string;
mode: "live" | "snapshot";
onMode: (m: "live" | "snapshot") => void;
onClose: () => void;
children: ReactNode;
}) {
const { t } = useTranslation();
useEffect(() => {
const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose();
document.addEventListener("keydown", onKey);
return () => document.removeEventListener("keydown", onKey);
}, [onClose]);
const seg = (m: "live" | "snapshot", label: string) => (
<button
type="button"
onClick={() => onMode(m)}
aria-pressed={mode === m}
className={`px-3 py-0.5 text-[0.75rem] font-semibold uppercase tracking-wider ${
mode === m ? "bg-term-amber text-term-bg" : "text-term-muted hover:text-term-text"
}`}
>
{label}
</button>
);
// Portalled to <body>: the dock is a fixed, z-indexed, pointer-events-none layer that
// would otherwise trap the modal inside its stacking context.
return createPortal(
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-6"
onClick={onClose}
role="dialog"
aria-modal="true"
aria-label={title}
>
<div
className="flex w-[min(90vw,calc((100vh-7rem)*16/9))] flex-col overflow-hidden rounded-term border border-term-border bg-term-panel shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center gap-3 border-b border-term-border bg-term-panel-2 px-3 py-1.5 text-[0.8125rem]">
<span className="min-w-0 flex-1 truncate font-semibold text-term-text">{title}</span>
<div className="flex shrink-0 overflow-hidden rounded-term border border-term-border" role="group">
{seg("live", t("camera.live"))}
{seg("snapshot", t("camera.snapshot"))}
</div>
<button
type="button"
onClick={onClose}
className="shrink-0 text-term-muted hover:text-term-text"
aria-label={t("common.close")}
title={t("common.close")}
>
✕
</button>
</div>
{children}
</div>
</div>,
document.body,
);
}
/** Newest snapshot for the tile's resting picture, as an object URL (revoked on change). */
function useLatestSnapshot(deviceId: string, enabled: boolean) {
const meta = useQuery({
@@ -160,13 +307,27 @@ function useLatestSnapshot(deviceId: string, enabled: boolean) {
}
/**
* While `active`, hold a live socket to the camera and paint each JPEG into `imgRef`.
* While `active` (wanted: camera activity, or the user's choice in the large view), hold a live socket to the camera and paint each JPEG into `imgRef`.
* Closed (and the last frame dropped) as soon as the camera goes idle or the tile
* unmounts; reconnects after a drop while still active. The server shares one camera
* session between every viewer and skips frames for a slow one.
*/
function useLiveFrames(deviceId: string, active: boolean) {
const imgRef = useRef<HTMLImageElement>(null);
// Every <img> showing this camera (the tile, and the large view while open) gets each
// frame; a view that mounts between frames is given the newest one at once.
const imgs = useRef(new Map<string, HTMLImageElement>());
const latest = useRef<string | null>(null);
const imgRefs = useMemo(() => {
const slot = (name: string) => (el: HTMLImageElement | null) => {
if (el) {
imgs.current.set(name, el);
if (latest.current) el.src = latest.current;
} else {
imgs.current.delete(name);
}
};
return { tile: slot("tile"), modal: slot("modal") };
}, []);
const [status, setStatus] = useState<LiveState>({ state: "connecting" });
const [hasFrame, setHasFrame] = useState(false);
@@ -182,12 +343,12 @@ function useLiveFrames(deviceId: string, active: boolean) {
let sock: ReturnType<typeof createPlatformSocket> | null = null;
const paint = (buf: ArrayBuffer) => {
const img = imgRef.current;
if (closed || !img) return; // a frame landing after cleanup must not resurrect the view
if (closed) return; // a frame landing after cleanup must not resurrect the view
const next = URL.createObjectURL(new Blob([buf], { type: "image/jpeg" }));
const prev = currentUrl;
currentUrl = next;
img.src = next;
latest.current = next;
for (const img of imgs.current.values()) img.src = next;
if (prev) URL.revokeObjectURL(prev);
setHasFrame(true);
};
@@ -222,9 +383,10 @@ function useLiveFrames(deviceId: string, active: boolean) {
if (retry) window.clearTimeout(retry);
sock?.close();
if (currentUrl) URL.revokeObjectURL(currentUrl);
if (imgRef.current) imgRef.current.removeAttribute("src");
latest.current = null;
for (const img of imgs.current.values()) img.removeAttribute("src");
};
}, [deviceId, active]);
return { imgRef, status, hasFrame };
return { imgRefs, status, hasFrame };
}
+18 -1
View File
@@ -20,6 +20,21 @@ verified end to end against the two park-lab cameras through the real server and
- 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** that streams **live on demand** — car or no car — with a **Live / Snapshot
switch**, 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.
### 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 640×360 sub-stream scaled up; the Snapshot side shows the stored
evidence still (full resolution when the camera's snapshot stream is Main).
## Decision
@@ -94,7 +109,9 @@ Headless Firefox against the server with both park-lab cameras and a simulated v
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.
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.
## Not yet verified