Merge PR #489 live CCTV onto current architecture

This commit is contained in:
Bilawal Sidhu committed 2026-09-15 02:16:11 -05:00
commit 48666cb9a4
19 files changed
+941 -10

No files matched your search

+1
View File
@@ -63,6 +63,7 @@ export function createCctvLayer({ services, source }) {
_setCctvOverlayHostForTest: parts.testing._setCctvOverlayHostForTest,
createCctvProjectionOverlayEntry:
parts.projection.createCctvProjectionOverlayEntry,
getActiveVideoElement: parts.projection.getActiveVideoElement,
setCctvCardPresentationOptions:
parts.cards.setCctvCardPresentationOptions,
surfaceRegimeKey: parts.ground.surfaceRegimeKey,
+1
View File
@@ -83,6 +83,7 @@ export function createPresentation({
mountHeightM: camera.mountHeightM,
active: isActive,
feedType: camera.feedType,
isVideo: parts.model.isVideoFeedType(camera.feedType),
sourceKind:
health?.sourceKind ||
camera.sourceKind ||
+113 -4
View File
@@ -168,6 +168,79 @@ export function createProjection({
* @returns {Object|null} Projection runtime, or null if no viewer.
*/
async function attachVideoSource(runtime, url, feedType) {
const video = runtime?.video;
if (!video) return;
if (feedType !== 'hls') {
video.src = url;
return;
}
try {
const { default: Hls } = await import('hls.js');
if (runtime.disposed || runtime.video !== video) return;
if (!Hls.isSupported()) {
video.src = url;
return;
}
const hls = new Hls({
lowLatencyMode: false,
enableWorker: true,
liveSyncDurationCount: 5,
liveMaxLatencyDurationCount: 6,
maxBufferLength: 60,
backBufferLength: 30,
});
hls.on(Hls.Events.ERROR, (_event, data) => {
if (!data?.fatal) return;
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) {
// reread master for a fresh session
console.warn(
'[Data:CCTV] hls network error, reloading source:',
data.details,
);
setTimeout(() => {
if (runtime.hls === hls) hls.loadSource(url);
}, 2000);
return;
}
if (data.type === Hls.ErrorTypes.MEDIA_ERROR) {
hls.recoverMediaError();
return;
}
console.warn('[Data:CCTV] hls fatal:', data.type, data.details);
hls.destroy();
if (runtime.hls === hls) runtime.hls = null;
});
hls.loadSource(url);
hls.attachMedia(video);
runtime.hls = hls;
// Rate governor: DelDOT cameras deliver ~52 s of content per 60 s wall
// time. Hold playbackRate by buffer depth so the stream never starves;
// cameras with honest clocks sit at 1.0 and never leave it.
const governor = setInterval(() => {
if (runtime.video !== video || !runtime.hls) {
clearInterval(governor);
return;
}
const b = video.buffered;
if (!b.length) return;
const ahead = b.end(b.length - 1) - video.currentTime;
let rate = 1.0;
if (ahead < 15) rate = 0.75;
else if (ahead < 20) rate = 0.85;
else if (ahead < 30) rate = 0.93;
else if (ahead > 40) rate = 1.05;
if (Math.abs(video.playbackRate - rate) > 0.01)
video.playbackRate = rate;
}, 1000);
runtime.rateGovernor = governor;
} catch (error) {
console.warn('[Data:CCTV] hls.js unavailable:', error?.message || error);
video.src = url;
}
}
function createProjectionRuntime(record) {
if (!layerState._viewer) return null;
const canvas = document.createElement('canvas');
@@ -207,6 +280,7 @@ export function createProjection({
// only re-uploads the plane texture when there is genuinely new content.
canvasStamp: 1,
lastSwappedCanvasStamp: 0,
disposed: false,
};
parts.frames.paintProjectionPlaceholder(ctx, record.camera);
@@ -214,16 +288,32 @@ export function createProjection({
if (mode === 'video') {
const video = document.createElement('video');
video.muted = true;
video.loop = true;
video.autoplay = true;
video.playsInline = true;
video.crossOrigin = 'anonymous';
video.preload = 'auto';
video.src = parts.frames.mediaUrlFor(record.camera);
video.addEventListener('canplay', () => {
video.play().catch(() => {});
});
// Cesium sizes the video texture from the element's width/height
// attributes at first upload. Set them from the real stream dimensions
// and rebind on any resolution change (camera switch, adaptive source).
const bindVideoTexture = () => {
if (runtime.video !== video || !runtime.planeMaterial) return;
if (!(video.videoWidth > 0 && video.videoHeight > 0)) return;
video.width = video.videoWidth;
video.height = video.videoHeight;
runtime.planeMaterial.image = runtime.canvas;
runtime.planeMaterial.image = video;
};
video.addEventListener('loadedmetadata', bindVideoTexture);
video.addEventListener('resize', bindVideoTexture);
runtime.video = video;
runtime.hls = null;
attachVideoSource(
runtime,
parts.frames.mediaUrlFor(record.camera),
feedType,
);
} else {
const img = new Image();
img.decoding = 'async';
@@ -254,7 +344,7 @@ export function createProjection({
const positions =
record.frustumPositions || parts.geometry.frustumCartesians(geometry);
runtime.planeMaterial = new Cesium.ImageMaterialProperty({
image: mode === 'video' && runtime.video ? runtime.video : canvas,
image: canvas,
transparent: true,
color: Cesium.Color.WHITE.withAlpha(0.95),
});
@@ -288,6 +378,15 @@ export function createProjection({
function destroyProjectionRuntime(runtime) {
if (!runtime) return;
runtime.disposed = true;
if (runtime.hls) {
runtime.hls.destroy();
runtime.hls = null;
}
if (runtime.rateGovernor) {
clearInterval(runtime.rateGovernor);
runtime.rateGovernor = null;
}
if (runtime.video) {
runtime.video.pause();
runtime.video.removeAttribute('src');
@@ -355,6 +454,15 @@ export function createProjection({
* @param {string|null} activeId - ID of the currently active camera.
*/
/**
* The active camera's decoded <video>, for a second surface (the panel card)
* to paint from. Null when the active feed is a still or not yet attached.
* @returns {HTMLVideoElement|null}
*/
function getActiveVideoElement() {
return parts.selection.getActiveRecord()?.projection?.video || null;
}
function pauseInactiveProjectionFeeds(activeId) {
for (const record of layerState._records) {
if (!record.projection?.video) continue;
@@ -371,6 +479,7 @@ export function createProjection({
}
return {
createCctvProjectionOverlayEntry,
getActiveVideoElement,
updatePlanePlacement,
clearProjectionOverlay,
setPlaneVisible,
+1
View File
@@ -1886,6 +1886,7 @@ export class StyleManager {
_cctvFocusBtn: this._cctvFocusBtn,
_cctvFrame: this._cctvFrame,
_cctvFrameWrap: this._cctvFrameWrap,
_cctvVideo: this._cctvVideo,
_cctvMeta: this._cctvMeta,
_cctvNearestBtn: this._cctvNearestBtn,
_cctvNextBtn: this._cctvNextBtn,
+42 -1
View File
@@ -164,7 +164,46 @@ export function _renderCctvState(state) {
}
}
if (this._cctvFrame) {
// Live video: for HLS cameras, paint the active camera's decoded <video>
// element onto a canvas each frame (one decoder, two surfaces — the plane
// reads the same element). The loop rides through hls.js pipeline resets
// (a null element or unchanged currentTime is a skipped frame, not an exit)
// and self-stops when the card hides or the feed goes away. The still-image
// path below is suppressed while a live feed is showing.
const liveIntent = enabled && !!activeCamera?.isVideo;
if (this._cctvVideo) {
this._cctvVideo.hidden = !liveIntent;
if (this._cctvFrame) this._cctvFrame.hidden = liveIntent;
this._cctvVideoActive = liveIntent;
if (liveIntent && !this._cctvVideoRaf) {
const canvas = this._cctvVideo;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
let lastTime = -1;
const paint = () => {
if (this.destroyed || !this._cctvVideoActive) {
this._cctvVideoRaf = 0;
return;
}
const v = this.cctv.getActiveVideoElement();
if (v && v.currentTime !== lastTime) {
lastTime = v.currentTime;
if (
canvas.width !== v.videoWidth ||
canvas.height !== v.videoHeight
) {
canvas.width = v.videoWidth;
canvas.height = v.videoHeight;
}
ctx.drawImage(v, 0, 0);
}
this._cctvVideoRaf = requestAnimationFrame(paint);
};
this._cctvVideoRaf = requestAnimationFrame(paint);
}
}
if (this._cctvFrame && !liveIntent) {
const nextSrc = enabled ? activeCamera?.frameUrl : null;
const nextCameraId = enabled ? activeCamera?.id || '' : '';
const cameraChanged = this._cctvFrame.dataset.cameraId !== nextCameraId;
@@ -183,6 +222,8 @@ export function _renderCctvState(state) {
if (!nextSrc) {
this._clearCctvFrame();
}
} else if (liveIntent) {
this._clearCctvFrame();
}
this._syncCctvSourceBadge(activeCamera, enabled);
+1
View File
@@ -142,6 +142,7 @@ export function readShellElements(document = globalThis.document) {
_cctvCalibResetBtn: document.getElementById('cctv-calib-reset-btn'),
_cctvFrame: document.getElementById('cctv-frame'),
_cctvFrameWrap: document.getElementById('cctv-frame-wrap'),
_cctvVideo: document.getElementById('cctv-video'),
_cctvSourceBadge: document.getElementById('cctv-source-badge'),
_cctvMeta: document.getElementById('cctv-meta'),
_cctvSummary: document.getElementById('cctv-summary'),
+18
View File
@@ -234,6 +234,8 @@
image inside is taller — and the wrap is overflow:hidden, which clipped
the bottom of the frame right back off. Never shrink the preview. */
flex-shrink: 0;
aspect-ratio: 16/9;
min-height: 0;
}
/* Lightweight loading shimmer while the active frame is fetching */
@@ -298,10 +300,26 @@
transition: opacity 180ms ease;
}
#cctv-video {
width: 100%;
height: 100%;
display: block;
object-fit: contain;
}
#cctv-frame.active {
opacity: 1;
}
#cctv-video:not([hidden]) {
opacity: 1;
}
#cctv-frame[hidden],
#cctv-video[hidden] {
display: none;
}
#cctv-meta {
font-family: var(--font-mono);
font-size: 9px;
+1
View File
@@ -23,6 +23,7 @@
</div>
<div id="cctv-frame-wrap" class="cctv-frame-wrap">
<img id="cctv-frame" alt="CCTV feed frame" />
<canvas id="cctv-video" class="cctv-frame" hidden></canvas>
</div>
<div id="cctv-source-badge" role="status" aria-live="polite">SOURCE · UNKNOWN</div>
<div id="cctv-meta">Enable CCTV to load camera intersections</div>