fix(cctv): bound HLS sessions and isolate consumer playback leases

This commit is contained in:
Bilawal Sidhu committed 2026-09-15 10:52:30 -05:00
1 parent 48666cb9a4
commit 292cd610f3
22 files changed
+665 -730

No files matched your search

+7
View File
@@ -38,6 +38,11 @@ export class CctvControls {
this._calibrationEdit = null;
this._actionGeneration = 0;
this._initCctvPanel();
if (this._cctvVideo && typeof MutationObserver !== 'undefined') {
this._videoVisibilityObserver = new MutationObserver(() => this._renderCctvState(this._cctvState));
if (this._cctvPanel) this._videoVisibilityObserver.observe(this._cctvPanel, { attributes: true, attributeFilter: ['class', 'hidden'] });
this.listen(document, 'visibilitychange', () => this._renderCctvState(this._cctvState));
}
}
listen(target, type, handler, options = {}) {
target?.addEventListener(type, handler, {
@@ -99,6 +104,8 @@ export class CctvControls {
destroy() {
if (this.destroyed) return;
this.destroyed = true;
this._cctvVideoSurface?.stop();
this._videoVisibilityObserver?.disconnect();
this._actionGeneration++;
this.listeners.abort();
this._cctvUnsubscribe?.();
+9 -32
View File
@@ -1,3 +1,4 @@
import { createCctvVideoSurface } from './cctvVideo.js';
export function _calBadgeLabel(badge) {
switch (badge) {
case 'calibrated':
@@ -164,42 +165,18 @@ export function _renderCctvState(state) {
}
}
// 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);
const visible = liveIntent && !document.hidden && !this._cctvPanel?.classList.contains('collapsed');
if (!visible || this._cctvVideoCameraId !== activeId) {
this._cctvVideoSurface?.stop();
this._cctvVideoSurface = null;
}
this._cctvVideoCameraId = activeId;
if (visible && !this._cctvVideoSurface) {
this._cctvVideoSurface = createCctvVideoSurface(this._cctvVideo, () => this.cctv.getActiveVideoElement?.());
}
}
+31
View File
@@ -0,0 +1,31 @@
/** Paint a second surface from the projection decoder, bounded to 640px/15 fps. */
export function createCctvVideoSurface(canvas, getVideo, { requestFrame = requestAnimationFrame, cancelFrame = cancelAnimationFrame } = {}) {
const ctx = canvas.getContext('2d');
let handle = 0;
let stopped = false;
let previous = null;
let previousTime = -1;
let paintedAt = -Infinity;
const paint = (now) => {
if (stopped) return;
const video = getVideo();
if (video !== previous) {
ctx?.clearRect(0, 0, canvas.width, canvas.height);
previous = video;
previousTime = -1;
}
if (ctx && video?.readyState >= 2 && video.videoWidth > 0 && video.videoHeight > 0 && now - paintedAt >= 1000 / 15 && video.currentTime !== previousTime) {
const width = Math.min(640, video.videoWidth);
const height = Math.max(1, Math.round(width * video.videoHeight / video.videoWidth));
if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; }
try {
ctx.drawImage(video, 0, 0, width, height);
previousTime = video.currentTime;
paintedAt = now;
} catch { /* A resolution/decode transition retries on the next frame. */ }
}
handle = requestFrame(paint);
};
handle = requestFrame(paint);
return { stop() { stopped = true; cancelFrame(handle); ctx?.clearRect(0, 0, canvas.width, canvas.height); } };
}
+21
View File
@@ -0,0 +1,21 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createCctvVideoSurface } from './cctvVideo.js';
test('second surface uses shared decoder, caps draws, clears switch and cancels teardown', () => {
let callback;
let draws = 0;
let clears = 0;
let cancelled = 0;
const canvas = { width: 1, height: 1, getContext: () => ({ drawImage: () => draws++, clearRect: () => clears++ }) };
let v = { readyState: 2, videoWidth: 1920, videoHeight: 1080, currentTime: 1 };
const surface = createCctvVideoSurface(canvas, () => v, { requestFrame: (fn) => { callback = fn; return 1; }, cancelFrame: () => cancelled++ });
callback(0); callback(20); callback(80);
assert.equal(draws, 1);
assert.equal(canvas.width, 640);
v = { ...v }; callback(160);
assert.equal(draws, 2);
assert.equal(clears, 2);
surface.stop(); callback(200);
assert.equal(cancelled, 1);
assert.equal(draws, 2);
});
-2
View File
@@ -234,8 +234,6 @@
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 */