From 9a3fc84935047dc393c1ec0f1e266041f017f40a Mon Sep 17 00:00:00 2001 From: Byrd-IT <189950726+Byrd-IT@users.noreply.github.com> Date: Thu, 5 Nov 2026 00:22:19 -0600 Subject: [PATCH] fix(cctv): address review of #1543 (horizon test, resume guard, no per-frame allocation, mobile sheet) - frames.test: viewerAt uses Cesium's default far (5e8) so the below-horizon test fails without occluder.isPointVisible - cctvControls: install the resume observer for a still-only panel too - cctvPresentation: panelShown also requires the Cameras sheet on phones; the observer watches body class/data-mobile-sheet - frames.projectionInView: shared horizonOccluder + scratch sphere/array --- changelog.d/1543-cctv-visible-only-fetch.md | 4 +- src/layers/cctv/frames.js | 23 ++++-- src/layers/cctv/frames.test.mjs | 4 +- src/ui/cctvControls.js | 11 ++- src/ui/cctvPanel.test.mjs | 84 ++++++++++++++++++--- src/ui/cctvPresentation.js | 9 ++- 6 files changed, 110 insertions(+), 25 deletions(-) diff --git a/changelog.d/1543-cctv-visible-only-fetch.md b/changelog.d/1543-cctv-visible-only-fetch.md index 4db3c8bc7b..80421a35c2 100644 --- a/changelog.d/1543-cctv-visible-only-fetch.md +++ b/changelog.d/1543-cctv-visible-only-fetch.md @@ -1,4 +1,4 @@ - Stop fetching the active CCTV camera's pictures when nobody can see them. The monitor plane skips its 10 s still while it is off screen or below the - horizon, and the panel's still preview waits for a visible tab and an open - panel (Byrd-IT, #1543). + horizon, and the panel's still preview waits for a visible tab, an open + panel and, on phones, the open Cameras sheet (Byrd-IT, #1543). diff --git a/src/layers/cctv/frames.js b/src/layers/cctv/frames.js index ba0177f225..8048b0e31a 100644 --- a/src/layers/cctv/frames.js +++ b/src/layers/cctv/frames.js @@ -1,4 +1,5 @@ import * as Cesium from 'cesium'; +import { horizonOccluder } from '../../data/iconOrientation.js'; import { FRAME_SIGNATURE_W, FRAME_SIGNATURE_H, @@ -42,6 +43,9 @@ export function fitToProjection(sourceW, sourceH) { }; } +const _scratchPoints = []; +const _scratchSphere = new Cesium.BoundingSphere(); + export function createFrames({ state: layerState, services, parts, source }) { /** * FNV-1a over the RGB channels of a downsampled frame. Pure (takes the raw @@ -254,7 +258,10 @@ export function createFrames({ state: layerState, services, parts, source }) { if (!camera || !scene || scene.mode !== Cesium.SceneMode.SCENE3D) return true; const placed = record?.frustumPositions; - const points = [ + // Reused buffers: this runs on every projection tick while out of view. + const points = _scratchPoints; + points.length = 0; + for (const point of [ record?.billboard?.position, placed?.mount, placed?.capCenter, @@ -262,16 +269,15 @@ export function createFrames({ state: layerState, services, parts, source }) { placed?.tr, placed?.br, placed?.bl, - ].filter(Boolean); + ]) { + if (point) points.push(point); + } if ( !points.length || typeof camera.frustum?.computeCullingVolume !== 'function' ) return true; - const occluder = new Cesium.EllipsoidalOccluder( - Cesium.Ellipsoid.WGS84, - camera.positionWC, - ); + const occluder = horizonOccluder(camera); if (!points.some((point) => occluder.isPointVisible(point))) return false; const culling = camera.frustum.computeCullingVolume( camera.positionWC, @@ -279,8 +285,9 @@ export function createFrames({ state: layerState, services, parts, source }) { camera.upWC, ); return ( - culling.computeVisibility(Cesium.BoundingSphere.fromPoints(points)) !== - Cesium.Intersect.OUTSIDE + culling.computeVisibility( + Cesium.BoundingSphere.fromPoints(points, _scratchSphere), + ) !== Cesium.Intersect.OUTSIDE ); } diff --git a/src/layers/cctv/frames.test.mjs b/src/layers/cctv/frames.test.mjs index 3253074ba0..7beea0c03a 100644 --- a/src/layers/cctv/frames.test.mjs +++ b/src/layers/cctv/frames.test.mjs @@ -185,7 +185,9 @@ function viewerAt(position, direction, up) { fov: Cesium.Math.toRadians(60), aspectRatio: 16 / 9, near: 1, - far: 5e6, + // Cesium's default far plane, as the app uses: a smaller one rejects + // the far-side mount before the horizon check is ever reached. + far: 5e8, }), }, }; diff --git a/src/ui/cctvControls.js b/src/ui/cctvControls.js index 1e227c92e2..2d5ad447a6 100644 --- a/src/ui/cctvControls.js +++ b/src/ui/cctvControls.js @@ -73,7 +73,10 @@ export class CctvControls { if (state) this._renderCctvState(state); if (this._cctvChip) this._publishCctvChip({}); }); - if (this._cctvVideo && typeof MutationObserver !== 'undefined') { + if ( + (this._cctvVideo || this._cctvFrame) && + typeof MutationObserver !== 'undefined' + ) { this._videoVisibilityObserver = new MutationObserver(() => this._renderCctvState(this._cctvState), ); @@ -82,6 +85,12 @@ export class CctvControls { attributes: true, attributeFilter: ['class', 'hidden'], }); + // On phones the panel is on screen only while the Cameras sheet is open. + if (typeof document !== 'undefined' && document.body) + this._videoVisibilityObserver.observe(document.body, { + attributes: true, + attributeFilter: ['class', 'data-mobile-sheet'], + }); this.listen(document, 'visibilitychange', () => this._renderCctvState(this._cctvState), ); diff --git a/src/ui/cctvPanel.test.mjs b/src/ui/cctvPanel.test.mjs index 18d18152c3..7612eacc3d 100644 --- a/src/ui/cctvPanel.test.mjs +++ b/src/ui/cctvPanel.test.mjs @@ -609,6 +609,15 @@ test('the meta line of a disabled layer does not describe its last camera', () = assert.match(presentCctvPanel(activeState, true).meta, /^Austin ยท HDG /); }); +const settle = () => new Promise((resolve) => setTimeout(resolve, 0)); +const stillState = (frameUrl) => ({ + enabled: true, + count: 1, + cameraIndex: [camera], + activeCameraId: 'a', + activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' }, +}); + test('the still preview is fetched only while the tab and the panel are shown', async (t) => { const prior = globalThis.Image; const requests = []; @@ -630,13 +639,7 @@ test('the still preview is fetched only while the tab and the panel are shown', }); const { controls, byId } = panel(t); const panelElement = byId('cctv-panel'); - const state = (frameUrl) => ({ - enabled: true, - count: 1, - cameraIndex: [camera], - activeCameraId: 'a', - activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' }, - }); + const state = stillState; // Collapsed panel: nothing is requested, however often the state arrives. assert.equal(panelElement.classList.contains('collapsed'), true); @@ -644,22 +647,79 @@ test('the still preview is fetched only while the tab and the panel are shown', controls._renderCctvState(state('a2.jpg')); assert.equal(requests.length, 0, 'collapsed panel'); - // Opening it renders again (the real panel does so from a class observer) - // and asks for the current picture once. + // Opening it resumes through the class observer, with no manual render. panelElement.classList.remove('collapsed'); - controls._renderCctvState(state('a2.jpg')); + await settle(); assert.equal(requests.length, 1, 'expanded panel'); assert.equal(requests[0].src, 'a2.jpg'); requests[0].onload(); // A background tab: refreshed URLs are not requested. tabHidden = true; + document.dispatchEvent(new Event('visibilitychange')); controls._renderCctvState(state('a3.jpg')); assert.equal(requests.length, 1, 'hidden tab'); - // Back in the tab: the next render fetches the current URL. + // Back in the tab: visibilitychange alone fetches the current URL. tabHidden = false; - controls._renderCctvState(state('a3.jpg')); + document.dispatchEvent(new Event('visibilitychange')); assert.equal(requests.length, 2, 'visible tab'); assert.equal(requests[1].src, 'a3.jpg'); }); + +test('the still preview waits for the Cameras sheet on phones', async (t) => { + const prior = globalThis.Image; + const requests = []; + globalThis.Image = class { + constructor() { + requests.push(this); + } + }; + t.after(() => { + globalThis.Image = prior; + document.body.classList.remove('mobile-shell'); + document.body.removeAttribute('data-mobile-sheet'); + }); + const { controls, byId } = panel(t); + byId('cctv-panel').classList.remove('collapsed'); + document.body.classList.add('mobile-shell'); + document.body.setAttribute('data-mobile-sheet', 'layers'); + controls._renderCctvState(stillState('a1.jpg')); + assert.equal(requests.length, 0, 'another sheet is open'); + + document.body.removeAttribute('data-mobile-sheet'); + controls._renderCctvState(stillState('a1.jpg')); + assert.equal(requests.length, 0, 'no sheet is open'); + + document.body.setAttribute('data-mobile-sheet', 'cameras'); + await settle(); + assert.equal(requests.length, 1, 'Cameras sheet opened'); + assert.equal(requests[0].src, 'a1.jpg'); + + // Off a phone the sheet attribute means nothing. + requests[0].onload(); + document.body.classList.remove('mobile-shell'); + document.body.setAttribute('data-mobile-sheet', 'layers'); + controls._renderCctvState(stillState('a2.jpg')); + assert.equal(requests.length, 2, 'desktop ignores the sheet'); +}); + +test('a still-only panel (no video element) resumes from the class change', async (t) => { + const prior = globalThis.Image; + const requests = []; + globalThis.Image = class { + constructor() { + requests.push(this); + } + }; + t.after(() => { + globalThis.Image = prior; + }); + const { controls, byId } = panel(t); + assert.equal(controls._cctvVideo, undefined); + controls._renderCctvState(stillState('a1.jpg')); + assert.equal(requests.length, 0); + byId('cctv-panel').classList.remove('collapsed'); + await settle(); + assert.equal(requests.length, 1); +}); diff --git a/src/ui/cctvPresentation.js b/src/ui/cctvPresentation.js index 8b2003d0f3..7e4b576f62 100644 --- a/src/ui/cctvPresentation.js +++ b/src/ui/cctvPresentation.js @@ -277,9 +277,16 @@ export function _renderCctvState(state) { // A background tab or a collapsed panel shows nothing, so neither plays the // video nor fetches the still; the observer and the visibilitychange listener // render again when the panel is shown. + // On phones the panel also belongs to one sheet: the shell hides it unless + // data-mobile-sheet is 'cameras'. + const body = typeof document !== 'undefined' ? document.body : null; + const sheetClosed = + !!body?.classList?.contains('mobile-shell') && + body.getAttribute('data-mobile-sheet') !== 'cameras'; const panelShown = !(typeof document !== 'undefined' && document.hidden) && - !this._cctvPanel?.classList?.contains('collapsed'); + !this._cctvPanel?.classList?.contains('collapsed') && + !sheetClosed; if (this._cctvVideo) { const visible = liveIntent && panelShown; if (!visible || this._cctvVideoCameraId !== activeId) {