Merge branch 'main' into byrdit/cctv-visible-only-fetch

This commit is contained in:
German Khokhlov authored and GitHub committed 2026-11-20 19:26:40 +04:00
commit b94e556693
18 files changed
+1209 -300

No files matched your search

+131 -32
View File
@@ -10,7 +10,8 @@ import { useScreenAnnotationsStore } from '../ui/stores/screenAnnotations.js';
import { applicationPinia } from '../ui/vueRuntime.js';
import { heightsOf } from '../queries/heights.js';
import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js';
import { distance } from '../geo/vector.js';
import { viewPlanes } from '../geo/frustum.js';
import { distance, dot } from '../geo/vector.js';
import {
canvasOf as sceneCanvasOf,
findCamera,
@@ -87,11 +88,12 @@ export function createScreenAnnotationRenderer(
const arrowMarkers = new Map();
let viewBox = null;
// The eye this frame is projected from (the camera port's pose), and the
// query port and canvas it is projected through, read once per pass.
// Read the camera pose, query port, canvas and view-frustum planes once
// per projection pass.
let pose = null;
let passQuery = null;
let passCanvas = null;
let cullingPlanes = null;
// The renderer's canvas; a viewer with no renderer bound, its own.
const canvasOf = () => sceneCanvasOf(viewer);
const records = new Map(); // anno.id -> { key, anno, group, parts }
@@ -109,6 +111,18 @@ export function createScreenAnnotationRenderer(
const heightCache = new Map();
const HEIGHT_CACHE_SOFT = 600; // target size; over this we evict keys not used this frame
const HEIGHT_CACHE_HARD = 8000; // absolute ceiling (one big ring + others) — never exceeded
// Accepted surface-height range; samples outside it are treated as misses.
const MIN_SURFACE_HEIGHT = -430;
const MAX_SURFACE_HEIGHT = 9000;
const HEIGHT_SAMPLE_BUDGET = 32; // clampNow calls per projection pass
const HEIGHT_RETRY_MS = 1000; // minimum interval between misses per anchor
// Fair sampling: keys refused by an exhausted budget queue in arrival order,
// and each pass reserves budget for the oldest queued keys first, so a fixed
// iteration order cannot starve anchors beyond the first budget's worth.
const heightQueue = new Set();
const heightReserved = new Set(); // queued keys this pass serves first
const heightRefused = new Set(); // keys refused during this pass
let heightBudget = HEIGHT_SAMPLE_BUDGET; // unreserved slots left this pass
let projGen = 0; // bumped each projection frame; entries used this frame are "hot"
function trimHeightCache() {
if (heightCache.size <= HEIGHT_CACHE_SOFT) return;
@@ -136,12 +150,20 @@ export function createScreenAnnotationRenderer(
return PALETTE[anno.color] || PALETTE.primary;
}
// Best-effort surface height under a coordinate (clamps onto the 3D tiles).
// Returns 0 until the tile loads, then caches the validated height.
function groundHeight(lon, lat) {
const key = `${lon.toFixed(5)},${lat.toFixed(5)}`;
const cached = heightCache.get(key);
if (cached && cached.settled) {
function clearHeights() {
heightCache.clear();
heightQueue.clear();
}
function heightKey(lon, lat) {
return `${lon.toFixed(5)},${lat.toFixed(5)}`;
}
// Best-effort surface height under a coordinate (clamps onto the 3D tiles):
// the validated height once the tile loads, null until then.
// `key` and `cached` are the caller's cache lookup for this coordinate.
function groundHeight(lon, lat, key, cached) {
if (cached?.settled) {
// Mark hot for this frame + bump to most-recently-used so heights for live
// marks survive eviction while places we've navigated away from age out.
cached.gen = projGen;
@@ -149,6 +171,19 @@ export function createScreenAnnotationRenderer(
heightCache.set(key, cached);
return cached.h;
}
const now = Date.now();
if (cached && now < cached.nextAttempt) {
cached.gen = projGen;
return null;
}
if (heightReserved.has(key)) {
heightReserved.delete(key);
} else if (heightBudget > 0) {
heightBudget--;
} else {
heightRefused.add(key);
return null;
}
try {
const heights = heightsOf(viewer);
if (heights.supported().clamp) {
@@ -156,7 +191,11 @@ export function createScreenAnnotationRenderer(
const c = heights.clampNow(fromDegrees(lon, lat, 0));
if (c) {
const h = toLonLatH(c)?.height;
if (Number.isFinite(h) && h > -430 && h < 9000) {
if (
Number.isFinite(h) &&
h > MIN_SURFACE_HEIGHT &&
h < MAX_SURFACE_HEIGHT
) {
heightCache.set(key, { h, settled: true, gen: projGen });
trimHeightCache();
return h;
@@ -166,10 +205,13 @@ export function createScreenAnnotationRenderer(
} catch {
/* tiles not ready */
}
const fallback = cached ? cached.h : 0;
heightCache.set(key, { h: fallback, settled: false, gen: projGen });
heightCache.set(key, {
settled: false,
gen: projGen,
nextAttempt: now + HEIGHT_RETRY_MS,
});
trimHeightCache();
return fallback;
return null;
}
function add(anno) {
@@ -313,7 +355,7 @@ export function createScreenAnnotationRenderer(
} catch {
/* the board is unreachable */
}
if (records.size === 0) heightCache.clear();
if (records.size === 0) clearHeights();
throw error;
}
}
@@ -365,22 +407,32 @@ export function createScreenAnnotationRenderer(
// Area-type updates route through the hybrid proxy only.
}
function project(lon, lat, allowOffscreen = false) {
// The window position of a point at its surface height (0 m until that is
// sampled), or null when it is not in view. A point of an arrow, route or
// area (`connector`) is sampled even outside the view, so the part of the
// mark that is in view runs to the real height of the part that is not.
function project(lon, lat, allowOffscreen = false, connector = false) {
if (!pose) return null;
// Anchor at the real surface height (cached + validated), so marks sit on
// the ground/building instead of at sea level. The settle-once cache keeps
// it stable (no per-frame jitter that earlier broke nearby projections).
const world = fromDegrees(lon, lat, groundHeight(lon, lat));
if (!pose) return null;
const key = heightKey(lon, lat);
const cached = heightCache.get(key);
let sample = true;
if (!cached?.settled) {
// Unsettled: before sampling, cull only if the whole height range is hidden.
const low = fromDegrees(lon, lat, MIN_SURFACE_HEIGHT);
const high = fromDegrees(lon, lat, MAX_SURFACE_HEIGHT);
if (heightRangeHidden(low, high)) return null;
// A lone point outside the view is not sampled; the entry keeps its
// retry time.
sample = connector || !rangeOutsideView(low, high);
if (!sample && cached) cached.gen = projGen;
}
const height = sample ? groundHeight(lon, lat, key, cached) : null;
const world = fromDegrees(lon, lat, height ?? 0);
// Reject points behind the camera (they produce wrapped/extreme coords).
const eye = pose.position;
const look = pose.direction;
if (
(world.x - eye.x) * look.x +
(world.y - eye.y) * look.y +
(world.z - eye.z) * look.z <=
0
)
return null;
if (behindEye(world)) return null;
// Reject points beyond the globe horizon.
const query = passQuery;
if (!query || query.isOccluded(world)) return null;
@@ -398,6 +450,36 @@ export function createScreenAnnotationRenderer(
return { x: win.x, y: win.y };
}
// Points along one surface normal are linear in height, and each rejection
// region (behind the camera plane; the occluder's cone beyond the horizon
// plane) is convex. So when both height-range endpoints fail the SAME test,
// every intermediate height fails it too. Mixed failures prove nothing.
function heightRangeHidden(low, high) {
if (behindEye(low) && behindEye(high)) return true;
return Boolean(passQuery?.isOccluded(low) && passQuery.isOccluded(high));
}
// Whether the height range lies wholly outside one view-frustum plane, so
// no accepted height under the point is in view.
function rangeOutsideView(low, high) {
if (!cullingPlanes) return false;
for (const { normal, w } of cullingPlanes)
if (dot(normal, low) + w < 0 && dot(normal, high) + w < 0) return true;
return false;
}
// Whether `point` is on or behind the camera plane (needs a pose).
function behindEye(point) {
const eye = pose.position;
const look = pose.direction;
return (
(point.x - eye.x) * look.x +
(point.y - eye.y) * look.y +
(point.z - eye.z) * look.z <=
0
);
}
// Tracked-entity z-order: the tracked aircraft is a Cesium billboard in the
// CANVAS, which sits BELOW this SVG overlay (z-90, HTML over canvas). So a screen mark
// could hide it. After layout, any mark whose ACTUAL projected bounding box (rings,
@@ -477,8 +559,16 @@ export function createScreenAnnotationRenderer(
publish();
return;
}
heightReserved.clear();
for (const key of heightQueue) {
if (heightReserved.size >= HEIGHT_SAMPLE_BUDGET) break;
heightReserved.add(key);
}
heightBudget = HEIGHT_SAMPLE_BUDGET - heightReserved.size;
heightRefused.clear();
projGen += 1; // new frame: entries touched below are "hot" and survive trimming
pose = findCamera(viewer)?.pose() ?? null;
cullingPlanes = viewPlanes(pose);
passQuery = findQuery(viewer);
passCanvas = canvasOf();
const trackedRect = trackedEntityRect();
@@ -515,7 +605,9 @@ export function createScreenAnnotationRenderer(
const polygons = anno.polygons?.length ? anno.polygons : [[anno.ring]];
for (const polygon of polygons) {
const projectedRings = polygon.map((ring) =>
ring.map(([lon, lat]) => project(lon, lat, polygon.length > 1)),
ring.map(([lon, lat]) =>
project(lon, lat, polygon.length > 1, true),
),
);
// Keep the historical visible-outer projection, but never fill over
// an unprojectable hole or hide another independently visible part.
@@ -549,8 +641,8 @@ export function createScreenAnnotationRenderer(
group.display = 'none';
}
} else if (anno.type === 'arrow' && parts.path) {
const a = project(anno.anchor.lon, anno.anchor.lat);
const b = project(anno.to.lon, anno.to.lat);
const a = project(anno.anchor.lon, anno.anchor.lat, false, true);
const b = project(anno.to.lon, anno.to.lat, false, true);
if (a && b) {
// gentle arc so the connector reads as a drawn gesture, not a ruler line
const mx = (a.x + b.x) / 2;
@@ -575,7 +667,9 @@ export function createScreenAnnotationRenderer(
group.display = 'none';
}
} else if (anno.type === 'route' && parts.poly) {
const projected = anno.path.map((pt) => project(pt.lon, pt.lat));
const projected = anno.path.map((pt) =>
project(pt.lon, pt.lat, false, true),
);
const pts = [];
for (const p of projected) {
if (p) pts.push(`${p.x.toFixed(1)},${p.y.toFixed(1)}`);
@@ -637,6 +731,11 @@ export function createScreenAnnotationRenderer(
}
}
}
// Still-refused queued keys keep their place; new refusals join the back.
// Keys no longer requesting a sample (settled, culled, removed) drop out.
for (const key of heightQueue)
if (!heightRefused.has(key)) heightQueue.delete(key);
for (const key of heightRefused) heightQueue.add(key);
// Keep clustered callouts readable.
const placed = [];
@@ -824,7 +923,7 @@ export function createScreenAnnotationRenderer(
records.delete(anno.id);
publish();
// Board emptied → drop accumulated height samples (natural reset point).
if (records.size === 0) heightCache.clear();
if (records.size === 0) clearHeights();
}
function sync() {
@@ -847,7 +946,7 @@ export function createScreenAnnotationRenderer(
records.clear();
leaving.clear();
byKey.clear();
heightCache.clear();
clearHeights();
}
// Claim the board; from here on only this renderer's own board changes.
@@ -178,10 +178,17 @@ test('an arrow defines its arrowhead and drops the dash once drawn on', async (t
test('outline upgrade preserves the existing SVG group identity', async (t) => {
const { store, svg, groups } = board(t);
const camera = {
positionWC: Cesium.Cartesian3.ZERO,
directionWC: Cesium.Cartesian3.ZERO,
positionWC: Cesium.Cartesian3.fromDegrees(-97.5, 31.2, 1000000),
directionWC: Cesium.Cartesian3.normalize(
Cesium.Cartesian3.negate(
Cesium.Cartesian3.fromDegrees(-97.5, 31.2),
new Cesium.Cartesian3(),
),
new Cesium.Cartesian3(),
),
positionCartographic: { height: 1000 },
};
projectTo(t, { x: 100, y: 100 });
const sampledAnchors = [];
const scene = sceneFor(camera, {
canvas: { clientWidth: 1280, clientHeight: 720, width: 1280, height: 720 },
@@ -586,3 +593,537 @@ test('a mark hidden from Draw is not drawn or placed, and comes back when shown'
await settle();
assert.equal(drawn(), true, 'shown again, it is drawn');
});
test('hidden and back-facing annotations do not sample; visible unresolved heights have bounded retries', (t) => {
const store = samplingBoard(t);
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
projectTo(t, { x: 100, y: 100 });
const camera = {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 100000),
directionWC: new Cesium.Cartesian3(-1, 0, 0),
upWC: new Cesium.Cartesian3(0, 0, 1),
positionCartographic: { height: 100000 },
};
let samples = 0;
const postRender = new Cesium.Event();
const scene = {
camera,
canvas: { width: 800, height: 600 },
postRender,
sampleHeightSupported: true,
sampleHeight() {
samples++;
},
};
const renderer = samplingRenderer(store, scene);
const mark = (id, lon) => ({
id,
type: 'label',
anchor: { lon, lat: 0 },
label: id,
});
renderer.add(mark('far-side', 180));
for (let i = 0; i < 100; i++) postRender.raiseEvent();
assert.equal(samples, 0);
renderer.add(mark('near-side', 0));
for (let i = 0; i < 100; i++) postRender.raiseEvent();
assert.equal(samples, 1);
t.mock.timers.tick(999);
postRender.raiseEvent();
assert.equal(samples, 1, 'no retry within a second of the miss');
t.mock.timers.tick(1);
postRender.raiseEvent();
assert.equal(samples, 2, 'one retry once a second has passed');
for (let i = 0; i < 9; i++) {
t.mock.timers.tick(1000);
postRender.raiseEvent();
}
assert.equal(samples, 11);
renderer.update(mark('near-side', 0), { empty: true });
t.mock.timers.tick(1000);
postRender.raiseEvent();
assert.equal(samples, 11, 'hidden records do not sample');
// With no perspective frustum (no view planes), only the range's
// behind-camera test keeps this anchor from sampling.
camera.directionWC = new Cesium.Cartesian3(1, 0, 0);
camera.frustum = new Cesium.OrthographicOffCenterFrustum();
renderer.update(mark('near-side', 0));
postRender.raiseEvent();
assert.equal(samples, 11, 'behind-camera anchors do not sample');
delete camera.frustum;
// Turned 45 degrees east: the anchor is in front and above the horizon,
// but outside the 60-degree frustum at every accepted height.
camera.directionWC = new Cesium.Cartesian3(-Math.SQRT1_2, Math.SQRT1_2, 0);
postRender.raiseEvent();
assert.equal(samples, 11, 'outside-frustum anchors do not sample');
camera.directionWC = new Cesium.Cartesian3(-1, 0, 0);
const before = samples;
renderer.add({
id: 'large-area',
type: 'area',
label: 'Area',
ring: Array.from({ length: 100 }, (_, i) => [0.001 + i / 10000, 0]),
anchor: { lon: 0, lat: 0 },
});
assert.equal(
samples - before,
32,
'a projection pass caps synchronous surface work',
);
renderer.destroy();
assert.equal(postRender.numberOfListeners, 0);
});
function cartographicLon(carto) {
return Number(Cesium.Math.toDegrees(carto.longitude).toFixed(5));
}
/** A board with no view mounted: tests read the store's marks. Animation frames run at once. */
function samplingBoard(t) {
const previousRaf = globalThis.requestAnimationFrame;
globalThis.requestAnimationFrame = (fn) => {
fn();
return 1;
};
t.after(() => {
globalThis.requestAnimationFrame = previousRaf;
});
return useScreenAnnotationsStore(createPinia());
}
/** The screen renderer over a viewer double around `scene`. */
function samplingRenderer(store, scene) {
return createScreenAnnotationRenderer(
{ scene, camera: scene.camera, trackedEntity: null },
{ store, activeTrackedReadoutId: () => null },
);
}
function squareFrustum() {
const frustum = new Cesium.PerspectiveFrustum();
frustum.fov = Cesium.Math.toRadians(60);
frustum.aspectRatio = 1;
frustum.near = 1;
frustum.far = 1e8;
return frustum;
}
const metresEast = (m) => Number((m / 111319.49).toFixed(5)); // degrees of longitude at the equator
const metresNorth = (m) => Number((m / 110574).toFixed(5)); // degrees of latitude at the equator
const at = (p, h) => Cesium.Cartesian3.fromDegrees(p.lon, p.lat, h);
/**
* A camera double `height` m above (0, 0) looking due east (+Y), pitched
* `pitchDeg` up (+X is up there).
*/
function eastwardCamera(height, pitchDeg = 0) {
const p = Cesium.Math.toRadians(pitchDeg);
return {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, height),
directionWC: new Cesium.Cartesian3(Math.sin(p), Math.cos(p), 0),
upWC: new Cesium.Cartesian3(Math.cos(p), -Math.sin(p), 0),
positionCartographic: { height },
frustum: squareFrustum(),
};
}
/** The drawn end point of the board's only arrow, or null when it is hidden. */
function arrowEnd(store) {
const [mark] = store.board.marks;
const path = mark.children.find((child) => child?.attrs?.class?.includes('gev-anno-arrow'));
const end = path?.attrs.d?.match(/(-?[\d.]+) (-?[\d.]+)$/);
return mark.display === '' && end ? { x: Number(end[1]), y: Number(end[2]) } : null;
}
/**
* Project through `camera` (position, direction, up, square frustum) onto an
* 800 x 800 canvas for the test's length; returns that projection.
*/
function projectThrough(t, camera, size = 800) {
const tan = Math.tan(camera.frustum.fov / 2);
const toWindow = (world) => {
const right = Cesium.Cartesian3.cross(camera.directionWC, camera.upWC, new Cesium.Cartesian3());
const v = Cesium.Cartesian3.subtract(world, camera.positionWC, new Cesium.Cartesian3());
const z = Cesium.Cartesian3.dot(v, camera.directionWC);
return {
x: ((Cesium.Cartesian3.dot(v, right) / z / tan + 1) / 2) * size,
y: ((1 - Cesium.Cartesian3.dot(v, camera.upWC) / z / tan) / 2) * size,
};
};
projectTo(t, (_scene, world) => toWindow(world));
return toWindow;
}
/** Whether `point`'s accepted heights (a 9.5 km sphere holds them) are all outside `camera`'s frustum. */
function outsideFrustum(camera, point) {
const volume = camera.frustum.computeCullingVolume(camera.positionWC, camera.directionWC, camera.upWC);
return volume.computeVisibility(new Cesium.BoundingSphere(point, 9500)) === Cesium.Intersect.OUTSIDE;
}
/**
* An 800 x 800 scene over `camera` whose surface under a longitude is
* `surface(lon)`; `sampled` lists the sampled longitudes.
*/
function surfaceScene(camera, surface) {
const sampled = [];
const postRender = new Cesium.Event();
const scene = {
camera,
canvas: { width: 800, height: 800 },
postRender,
sampleHeightSupported: true,
sampleHeight(cartographic) {
const lon = cartographicLon(cartographic);
sampled.push(lon);
return surface(lon);
},
};
return { scene, postRender, sampled };
}
test('an arrow with one endpoint outside a real frustum keeps its visible connector', (t) => {
const store = samplingBoard(t);
// 30 km straight down over (0, 0): east is +Y, north is +Z in ECEF here.
const camera = {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 30000),
directionWC: new Cesium.Cartesian3(-1, 0, 0),
upWC: new Cesium.Cartesian3(0, 0, 1),
positionCartographic: { height: 30000 },
frustum: squareFrustum(),
};
const toWindow = projectThrough(t, camera);
// 35 km east: in front of the camera and the horizon, but outside the
// frustum at every accepted height.
const far = { lon: metresEast(35000), lat: 0 };
assert.ok(outsideFrustum(camera, at(far, 0)));
const farWin = toWindow(at(far, 0));
assert.ok(farWin.x > 800 && farWin.x < 1600, 'far end off-screen right, inside the 2w limit');
const { scene, postRender, sampled } = surfaceScene(camera, () => 0);
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({
id: 'partial-arrow',
type: 'arrow',
label: 'Ridge',
anchor: { lon: 0, lat: 0 },
to: far,
});
postRender.raiseEvent();
const [mark] = store.board.marks;
assert.equal(mark.display, '', 'the arrow stays drawn');
const path = mark.children.find((child) => child.attrs.class?.includes('gev-anno-arrow'));
assert.ok(path.attrs.d.startsWith('M 400.0 400.0 Q '));
assert.ok(path.attrs.d.endsWith(` ${farWin.x.toFixed(1)} ${farWin.y.toFixed(1)}`));
assert.deepEqual([...new Set(sampled)], [0, far.lon], 'the off-view end samples too');
});
// The far end lies outside the frustum and beyond the horizon at 0 m, but
// above it at its real 100 m height: it is sampled, so the arrow keeps its
// connector and label.
test('an arrow whose far end is past the horizon only at 0 m keeps its connector', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(100);
const toWindow = projectThrough(t, camera);
const near = { lon: metresEast(2000), lat: 0 };
const bearing = Cesium.Math.toRadians(50);
const far = {
lon: metresEast(56000 * Math.cos(bearing)),
lat: metresNorth(56000 * Math.sin(bearing)),
};
const surface = { [near.lon]: 10, [far.lon]: 100 };
const { scene, postRender } = surfaceScene(camera, (lon) => surface[lon]);
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC);
assert.equal(occluder.isPointVisible(at(far, 100)), true, 'far end above the horizon at 100 m');
assert.equal(occluder.isPointVisible(at(far, 0)), false, 'far end past the horizon at 0 m');
assert.ok(outsideFrustum(camera, at(far, 0)), 'far end outside the frustum at every accepted height');
const farWin = toWindow(at(far, 100));
assert.ok(farWin.x > -800 && farWin.x < 0, 'far end off-screen left, inside [-w, 2w]');
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({
id: 'long-arrow',
type: 'arrow',
label: 'Ridge',
anchor: near,
to: far,
});
for (let i = 0; i < 3; i++) postRender.raiseEvent();
const [mark] = store.board.marks;
assert.equal(mark.display, '', 'the arrow stays drawn');
const path = mark.children.find((child) => child.attrs.class?.includes('gev-anno-arrow'));
assert.match(path.attrs.d, /^M 400\.0 4\d\d\.\d Q /, 'the connector starts at the on-screen end');
});
test('an arrow whose end crosses the 2h screen limit only at 0 m keeps its connector', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(18000);
const toWindow = projectThrough(t, camera);
const start = { lon: metresEast(40000), lat: 0 };
const end = { lon: metresEast(10000), lat: 0 };
const { scene, postRender, sampled } = surfaceScene(camera, (lon) => (lon === end.lon ? 1500 : 0));
assert.ok(toWindow(at(start, 0)).y < 800, 'start on screen');
assert.ok(outsideFrustum(camera, at(end, 0)), 'end sphere outside the frustum');
assert.ok(toWindow(at(end, 1500)).y < 1600, 'end inside the limit at its real height');
assert.ok(toWindow(at(end, 0)).y > 1600, 'end past the limit at 0 m');
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'a', type: 'arrow', label: 'Ridge', anchor: start, to: end });
for (let i = 0; i < 3; i++) postRender.raiseEvent();
assert.equal(store.board.marks[0].display, '', 'the partly visible arrow stays drawn');
assert.deepEqual([...new Set(sampled)], [start.lon, end.lon], 'the off-view end samples too');
});
test('an upward-looking arrow keeps a far end that is behind the eye only at 0 m', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(100, 59);
projectThrough(t, camera);
// Roofs: 132 m 19 m east (in view), 145 m 12 m west (above the view).
const start = { lon: metresEast(19), lat: 0 };
const end = { lon: metresEast(-12), lat: 0 };
const { scene, postRender } = surfaceScene(camera, (lon) => (lon === start.lon ? 132 : 145));
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'up', type: 'arrow', label: 'Tower', anchor: start, to: end });
for (let i = 0; i < 4; i++) postRender.raiseEvent();
const drawn = arrowEnd(store);
assert.ok(drawn, 'the arrow from the on-screen roof is drawn');
assert.ok(drawn.y < 0 && drawn.y > -800, `the connector leaves through the top edge (end y ${drawn.y})`);
});
test('an arrow crossing the view between two off-view ends on a plateau is drawn', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(4000);
projectThrough(t, camera);
// Both ends 2 km ahead and 2 km to either side on a 4000 m plateau: at 0 m
// both fall past the 2h limit, at 4000 m the arc crosses the screen.
const a = { lon: metresEast(2000), lat: metresNorth(2000) };
const b = { lon: metresEast(2000), lat: metresNorth(-2000) };
const { scene, postRender } = surfaceScene(camera, () => 4000);
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'plateau', type: 'arrow', label: 'Pass', anchor: a, to: b });
for (let i = 0; i < 4; i++) postRender.raiseEvent();
const drawn = arrowEnd(store);
assert.ok(drawn, 'the arrow is drawn');
assert.ok(Math.abs(drawn.y - 400) < 5, `the connector crosses at eye level (end y ${drawn.y})`);
});
test('an arrow to an off-view peak points at the peak, not at 0 m below it', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(4000);
projectThrough(t, camera);
// From a 4000 m plateau 2 km ahead to a 4500 m peak just left of the view.
const start = { lon: metresEast(2000), lat: 0 };
const end = { lon: metresEast(1000), lat: metresNorth(900) };
const { scene, postRender } = surfaceScene(camera, (lon) => (lon === start.lon ? 4000 : 4500));
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'peak', type: 'arrow', label: 'Peak', anchor: start, to: end });
for (let i = 0; i < 4; i++) postRender.raiseEvent();
const drawn = arrowEnd(store);
assert.ok(drawn, 'the arrow is drawn');
assert.ok(
Math.abs(drawn.x + 223.8) < 2 && Math.abs(drawn.y - 53.8) < 2,
`end at the 4500 m projection (-223.8, 53.8), got (${drawn.x}, ${drawn.y})`,
);
});
test('a lone label beyond the horizon just outside the view stays hidden', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(100);
const toWindow = projectThrough(t, camera);
const bearing = Cesium.Math.toRadians(34);
const anchor = {
lon: metresEast(200000 * Math.cos(bearing)),
lat: metresNorth(200000 * Math.sin(bearing)),
};
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC);
assert.equal(occluder.isPointVisible(at(anchor, 0)), false, 'beyond the horizon at 0 m');
assert.ok(outsideFrustum(camera, at(anchor, 0)), 'sphere outside the frustum');
assert.ok(toWindow(at(anchor, 0)).x > -800, 'projection inside [-w, 2w]');
const { scene, postRender } = surfaceScene(camera, () => 0);
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'l', type: 'label', label: 'Far city', anchor });
for (let i = 0; i < 3; i++) postRender.raiseEvent();
assert.equal(store.board.marks[0].display, 'none', 'its callout does not peek in at the left edge');
});
test('an arrow wholly beyond the horizon is not drawn across the screen', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(100);
projectThrough(t, camera);
const a = { lon: metresEast(80000), lat: metresNorth(67000) };
const b = { lon: metresEast(80000), lat: metresNorth(-67000) };
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC);
for (const point of [a, b]) {
assert.equal(occluder.isPointVisible(at(point, 0)), false, 'end beyond the horizon at 0 m');
assert.ok(outsideFrustum(camera, at(point, 0)), 'end sphere outside the frustum');
}
const { scene, postRender } = surfaceScene(camera, () => 0);
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({ id: 'x', type: 'arrow', label: 'Ghost', anchor: a, to: b });
for (let i = 0; i < 3; i++) postRender.raiseEvent();
assert.equal(store.board.marks[0].display, 'none', 'no ghost connector');
});
test('a visible area ring keeps a vertex past the horizon only at 0 m', (t) => {
const store = samplingBoard(t);
const camera = eastwardCamera(100);
const toWindow = projectThrough(t, camera);
const bearing = Cesium.Math.toRadians(50);
const far = { lon: metresEast(56000 * Math.cos(bearing)), lat: metresNorth(56000 * Math.sin(bearing)) };
const nearLon = metresEast(2000);
const vertices = [
{ lon: nearLon, lat: metresNorth(300) },
{ lon: nearLon, lat: metresNorth(-300) },
far,
];
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC);
assert.ok(outsideFrustum(camera, at(far, 0)));
assert.equal(occluder.isPointVisible(at(far, 0)), false);
assert.equal(occluder.isPointVisible(at(far, 100)), true);
const { scene, postRender, sampled } = surfaceScene(camera, (lon) => (lon === far.lon ? 100 : 10));
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
renderer.add({
id: 'triangle', type: 'area', label: 'Ridge', anchor: vertices[0],
ring: vertices.map(({ lon, lat }) => [lon, lat]),
});
postRender.raiseEvent();
const [mark] = store.board.marks;
assert.equal(mark.display, '', 'the triangle stays drawn');
const path = mark.children.find((child) => child.attrs.class?.includes('gev-anno-area'));
const coords = vertices.map((p) => {
const { x, y } = toWindow(at(p, p === far ? 100 : 10));
return `${x.toFixed(1)},${y.toFixed(1)}`;
});
assert.equal(path.attrs.d, `M ${coords.join(' L ')} Z`, 'the far vertex sits at its real height');
assert.deepEqual(sampled, [nearLon, nearLon, far.lon], 'the off-view vertex samples too');
});
test('a low-angle cached roof stays visible when range endpoints fail different tests', (t) => {
const store = samplingBoard(t);
projectTo(t, { x: 400, y: 380 });
// Camera 1 m above the ellipsoid, heading east, pitched 25 degrees down.
const pitch = Cesium.Math.toRadians(25);
const camera = {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 1),
directionWC: new Cesium.Cartesian3(-Math.sin(pitch), Math.cos(pitch), 0),
upWC: new Cesium.Cartesian3(Math.cos(pitch), Math.sin(pitch), 0),
positionCartographic: { height: 1 },
frustum: squareFrustum(),
};
const roofLon = 4000 / 111319.49;
let samples = 0;
const postRender = new Cesium.Event();
const scene = {
camera,
canvas: { width: 800, height: 800 },
postRender,
sampleHeightSupported: true,
sampleHeight() {
samples++;
return 20;
},
};
// Sanity: height 0 is beyond the horizon and 9000 m is behind the camera,
// while the 20 m roof itself is in front and above the horizon.
const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC);
const ahead = (h) => {
const p = Cesium.Cartesian3.fromDegrees(roofLon, 0, h);
const v = Cesium.Cartesian3.subtract(p, camera.positionWC, new Cesium.Cartesian3());
return Cesium.Cartesian3.dot(v, camera.directionWC) > 0;
};
assert.equal(occluder.isPointVisible(Cesium.Cartesian3.fromDegrees(roofLon, 0, 0)), false);
assert.equal(ahead(9000), false);
assert.equal(ahead(20), true);
assert.equal(occluder.isPointVisible(Cesium.Cartesian3.fromDegrees(roofLon, 0, 20)), true);
const renderer = samplingRenderer(store, scene);
renderer.add({ id: 'roof', type: 'label', label: 'Roof', anchor: { lon: roofLon, lat: 0 } });
assert.equal(samples, 1, 'the unknown height is sampled, not culled');
assert.equal(store.board.marks[0].display, '');
for (let i = 0; i < 5; i++) postRender.raiseEvent();
assert.equal(samples, 1, 'the settled height is reused');
assert.equal(store.board.marks[0].display, '', 'the cached roof point is tested and shown');
renderer.destroy();
});
test('height retries beyond the first budget are served under sparse passes', (t) => {
const store = samplingBoard(t);
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
projectTo(t, { x: 100, y: 100 });
const camera = {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 100000),
directionWC: new Cesium.Cartesian3(-1, 0, 0),
positionCartographic: { height: 100000 },
};
const lons = Array.from({ length: 33 }, (_, i) => Number(((i + 1) / 1000).toFixed(3)));
const last = lons.at(-1);
let tilesReady = false;
let pass = [];
const postRender = new Cesium.Event();
const scene = {
camera,
canvas: { width: 800, height: 600 },
postRender,
sampleHeightSupported: true,
sampleHeight(cartographic) {
const lon = cartographicLon(cartographic);
pass.push(lon);
return tilesReady && lon === last ? 12 : undefined;
},
};
const renderer = samplingRenderer(store, scene);
for (const lon of lons) {
renderer.add({ id: `p${lon}`, type: 'label', label: 'P', anchor: { lon, lat: 0 } });
}
assert.ok(pass.includes(last), 'every point gets its initial miss');
tilesReady = true; // tiles under the final point arrive after its miss
const lastSamples = [];
for (let i = 0; i < 4; i++) {
t.mock.timers.tick(1000);
pass = [];
postRender.raiseEvent();
assert.ok(pass.length <= 32, `pass ${i} keeps the 32-sample budget`);
lastSamples.push(pass.filter((lon) => lon === last).length);
}
assert.ok(lastSamples.slice(0, 2).includes(1), 'the 33rd point is served within two passes');
assert.equal(lastSamples.at(-1), 0, 'and then stays settled');
renderer.destroy();
});
test('points refused longest are sampled before newer refusals', (t) => {
const store = samplingBoard(t);
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: 1000 });
projectTo(t, { x: 100, y: 100 });
const camera = {
positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 100000),
directionWC: new Cesium.Cartesian3(-1, 0, 0),
positionCartographic: { height: 100000 },
};
const lons = Array.from({ length: 65 }, (_, i) => Number(((i + 1) / 1000).toFixed(3)));
let pass = [];
const { scene, postRender } = surfaceScene(camera, (lon) => {
pass.push(lon);
});
const renderer = samplingRenderer(store, scene);
t.after(() => renderer.destroy());
for (const lon of lons) {
renderer.add({ id: `p${lon}`, type: 'label', label: 'P', anchor: { lon, lat: 0 } });
}
assert.deepEqual(pass, lons, 'every point gets its first miss');
const passes = [];
for (let i = 0; i < 3; i++) {
t.mock.timers.tick(1000);
pass = [];
postRender.raiseEvent();
passes.push(pass);
}
assert.deepEqual(passes[0], lons.slice(0, 32), 'a pass serves the first 32 in board order');
assert.deepEqual(passes[1], lons.slice(32, 64), 'the next serves the 32 refused longest');
assert.deepEqual(passes[2], [...lons.slice(0, 31), lons[64]], 'then the rest of the queue, before newer refusals');
});
+187
View File
@@ -0,0 +1,187 @@
// Live HLS through the real /api/cctv middleware. Leases are random
// per-playback ids, not bound to the socket address, and a viewer refused for
// capacity leaves the camera's health as it was.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { HLS_LIMITS } from '../../server/providers/cctv/stream.js';
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
const perSession = HLS_LIMITS.leasesPerSession;
const SAME_ORIGIN = { host: 'localhost:4173', 'sec-fetch-site': 'same-origin' };
const camera = (id) => ({
id,
name: id,
lat: 30.27,
lon: -97.74,
feedType: 'hls',
url: `https://cams.upstream.invalid/${id}/list.m3u8`,
});
const PLAYLIST =
'#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
const lease = (index) =>
`00000000-0000-4000-8000-${String(index).padStart(12, '0')}`;
/** Mount the provider over three HLS cameras; `failing` upstreams answer 500. */
function mount(t, failing = []) {
const upstream = [];
const app = mountCctvProvider(t, {
cameras: ['cam-a', 'cam-b', 'cam-c'].map(camera),
fetch: async (url) => {
upstream.push(String(url));
if (failing.some((id) => String(url).includes(`/${id}/`)))
return new Response('', { status: 500 });
return new Response(String(url).endsWith('.m3u8') ? PLAYLIST : 'abc');
},
});
return { ...app, upstream };
}
/** Open a playlist and return the first segment path it lists. */
async function openPlaylist(call, cameraId, leaseId, remoteAddress) {
const res = await call(
`/media/${cameraId}?lease=${leaseId}`,
SAME_ORIGIN,
'GET',
remoteAddress,
);
assert.equal(res.statusCode, 200, `playlist ${cameraId} ${leaseId}`);
const segment = res.body
.split('\n')
.find((line) => line.startsWith('/api/cctv/media/'));
assert.ok(segment, 'the playlist lists a segment');
return segment.replace('/api/cctv', '');
}
test('viewers behind one shared address get every slot, and no more', async (t) => {
assert.equal(HLS_LIMITS.sessions, 2, 'the test fills both sessions');
const { call, health } = mount(t);
const shared = '::ffff:172.17.0.1';
let segment;
for (let index = 0; index < 2 * perSession; index++)
segment = await openPlaylist(
call,
index < perSession ? 'cam-a' : 'cam-b',
lease(index),
shared,
);
assert.equal(
(await call(segment, SAME_ORIGIN, 'GET', shared)).statusCode,
200,
);
const before = await health('cam-a');
// A ninth viewer of a full session and a third session are both refused,
// without reporting a camera whose upstream is fine as degraded.
for (const cameraId of ['cam-a', 'cam-c']) {
const refused = await call(
`/media/${cameraId}?lease=${lease(2 * perSession)}`,
SAME_ORIGIN,
'GET',
shared,
);
assert.equal(refused.statusCode, 503, cameraId);
assert.equal(refused.headers['Retry-After'], '2');
}
assert.deepEqual(await health('cam-a'), before);
assert.equal(await health('cam-c'), undefined);
// A released slot is free for the next viewer.
await call(`/media/cam-a?lease=${lease(0)}`, SAME_ORIGIN, 'DELETE', shared);
await openPlaylist(call, 'cam-a', lease(2 * perSession), shared);
});
test('a lease keeps working after its viewer reconnects from another address', async (t) => {
const { call } = mount(t);
await openPlaylist(call, 'cam-a', lease(0), '192.168.1.20');
const segment = await openPlaylist(call, 'cam-a', lease(0), '192.168.1.21');
assert.equal(
(await call(segment, SAME_ORIGIN, 'GET', '192.168.1.21')).statusCode,
200,
);
// A second viewer keeps the session alive, so the 404 below is the released
// lease and not a stopped session.
const other = await openPlaylist(call, 'cam-a', lease(1), '192.168.1.30');
const release = await call(
`/media/cam-a?lease=${lease(0)}`,
SAME_ORIGIN,
'DELETE',
'192.168.1.21',
);
assert.equal(release.statusCode, 204);
assert.equal(
(await call(segment, SAME_ORIGIN, 'GET', '192.168.1.21')).statusCode,
404,
);
assert.equal(
(await call(other, SAME_ORIGIN, 'GET', '192.168.1.30')).statusCode,
200,
);
});
test('ninth viewer frame fallback preserves active camera health', async (t) => {
const { call, health } = mount(t);
let segment;
for (let index = 0; index < perSession; index++)
segment = await openPlaylist(call, 'cam-a', lease(index));
const before = await health('cam-a');
assert.equal(before.status, 'ok');
const refused = await call(
`/media/cam-a?lease=${lease(perSession)}`,
SAME_ORIGIN,
);
assert.equal(refused.statusCode, 503);
const frame = await call(
'/frame/cam-a?label=cam-a&lat=30.27&lon=-97.74',
SAME_ORIGIN,
);
assert.equal(frame.statusCode, 200);
assert.equal(
(await call(segment, SAME_ORIGIN)).statusCode,
200,
'admitted viewer still plays',
);
assert.deepEqual(await health('cam-a'), before);
});
test('third camera frame fallback keeps absent health', async (t) => {
const { call, health, upstream } = mount(t);
await openPlaylist(call, 'cam-a', lease(0));
await openPlaylist(call, 'cam-b', lease(1));
const refused = await call(`/media/cam-c?lease=${lease(2)}`, SAME_ORIGIN);
assert.equal(refused.statusCode, 503);
assert.equal(await health('cam-c'), undefined);
const frame = await call(
'/frame/cam-c?label=cam-c&lat=30.27&lon=-97.74',
SAME_ORIGIN,
);
assert.equal(frame.statusCode, 200);
assert.equal(upstream.filter((url) => url.includes('/cam-c/')).length, 0);
assert.equal(
await health('cam-c'),
undefined,
'the fallback frame wrote cam-c health',
);
});
test('a failing HLS upstream with free slots is still reported degraded', async (t) => {
const { call, health } = mount(t, ['cam-c']);
const res = await call(`/media/cam-c?lease=${lease(0)}`, SAME_ORIGIN);
assert.equal(res.statusCode, 503);
assert.equal(res.headers['Retry-After'], '2');
const row = await health('cam-c');
assert.deepEqual(
{ status: row.status, message: row.message },
{ status: 'degraded', message: 'Live HLS unavailable' },
);
});
test('a cross-site request can neither read nor release a live HLS lease', async (t) => {
const { call } = mount(t);
const segment = await openPlaylist(call, 'cam-a', lease(0));
const crossSite = { host: 'localhost:4173', 'sec-fetch-site': 'cross-site' };
assert.equal((await call(segment, crossSite)).statusCode, 403);
assert.equal(
(await call(`/media/cam-a?lease=${lease(0)}`, crossSite, 'DELETE'))
.statusCode,
403,
);
assert.equal((await call(segment, SAME_ORIGIN)).statusCode, 200);
});
+6 -2
View File
@@ -5,6 +5,7 @@ import {
fetchHlsBytes,
parseHlsMedia,
HLS_LIMITS,
HlsCapacityError,
} from '../../server/providers/cctv/stream.js';
const playlist =
'#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
@@ -144,7 +145,7 @@ test('one session is reserved before await; disk-free cache and capacity remain
manager.ensure('a', base),
]);
assert.equal(a, b);
await assert.rejects(manager.ensure('b', base));
await assert.rejects(manager.ensure('b', base), HlsCapacityError);
assert.equal(await manager.waitReady(a), true);
assert.deepEqual(manager.stats(), { sessions: 1, bytes: 6 });
const text = await manager.buildPlaylist(a, 'a');
@@ -261,7 +262,10 @@ test('two consumers share downloads but release and abandoned expiry are indepen
await manager.waitReady(a);
assert.equal(downloads, 4); // One manifest and three segments, not per consumer.
assert.equal(a.leases.size, 2);
await assert.rejects(manager.ensure('camera', base, 'viewer-c'));
await assert.rejects(
manager.ensure('camera', base, 'viewer-c'),
HlsCapacityError,
);
manager.release('camera', 'viewer-a');
assert.equal(manager.stats().sessions, 1);
assert.equal(manager.getSegment('camera', a.token, 0, 'viewer-a'), null);
+39 -122
View File
@@ -8,7 +8,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Readable } from 'node:stream';
import { cctvProxy } from '../../server/providers/cctv.js';
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
const STILL = {
id: 'still-fixture',
@@ -63,113 +63,17 @@ function upstreamResponse({ status = 200, headers = {}, body = 'payload' }) {
};
}
/** Collects what a handler wrote, the way a Node ServerResponse would. */
function recordingResponse() {
const chunks = [];
let finished;
const done = new Promise((resolve) => {
finished = resolve;
});
const res = {
statusCode: 0,
headers: {},
writableEnded: false,
body: '',
writeHead(status, headers) {
res.statusCode = status;
res.headers = { ...res.headers, ...headers };
},
setHeader(name, value) {
res.headers[name] = value;
},
write(chunk) {
chunks.push(Buffer.from(chunk));
return true;
},
end(chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.writableEnded = true;
res.body = Buffer.concat(chunks).toString('utf8');
finished();
},
on() {},
once() {},
emit() {},
destroy() {},
};
return { res, done };
}
/**
* Mount the real provider over the fixture cameras and answer every upstream
* request from the supplied function.
*/
/** Mount the real provider over the fixture cameras, recording upstream requests. */
function mount(t, respond) {
const before = {
json: process.env.CCTV_SOURCES_JSON,
file: process.env.CCTV_SOURCES_FILE,
austin: process.env.CCTV_FORCE_AUSTIN,
serverKey: process.env.GOOGLE_MAPS_SERVER_API_KEY,
browserKey: process.env.GOOGLE_MAPS_API_KEY,
rateLimit: process.env.GEV_RATELIMIT_GOOGLE_PER_MIN,
};
process.env.CCTV_SOURCES_JSON = JSON.stringify([
STILL,
STILL_ALIAS,
VIDEO,
WEBM,
MJPEG,
]);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
// No Street View key: a /frame miss lands on the synthetic billboard rather
// than a network call this file does not model.
delete process.env.GOOGLE_MAPS_SERVER_API_KEY;
delete process.env.GOOGLE_MAPS_API_KEY;
// The default Google budget, whatever the shell sets.
delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
const nativeFetch = globalThis.fetch;
const requests = [];
globalThis.fetch = async (url, init = {}) => {
requests.push({ url: String(url), redirect: init.redirect });
return respond({ url: String(url), init });
};
t.after(() => {
globalThis.fetch = nativeFetch;
for (const [name, value] of [
['CCTV_SOURCES_JSON', before.json],
['CCTV_SOURCES_FILE', before.file],
['CCTV_FORCE_AUSTIN', before.austin],
['GOOGLE_MAPS_SERVER_API_KEY', before.serverKey],
['GOOGLE_MAPS_API_KEY', before.browserKey],
['GEV_RATELIMIT_GOOGLE_PER_MIN', before.rateLimit],
]) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});
let handler = null;
cctvProxy().configureServer({
middlewares: {
use: (_route, fn) => {
handler = fn;
},
const app = mountCctvProvider(t, {
cameras: [STILL, STILL_ALIAS, VIDEO, WEBM, MJPEG],
fetch: async (url, init = {}) => {
requests.push({ url: String(url), redirect: init.redirect });
return respond({ url: String(url), init });
},
});
assert.ok(handler, 'the CCTV provider must mount a middleware');
const call = async (path, headers = {}, socket) => {
const { res, done } = recordingResponse();
await handler({ url: path, headers, method: 'GET', on() {}, socket }, res);
await done;
return res;
};
const health = async (id) => {
const res = await call('/health');
return JSON.parse(res.body).cameras.find((entry) => entry.id === id);
};
return { call, health, requests };
return { ...app, requests };
}
test('a still camera is refused by /media without any upstream request', async (t) => {
@@ -405,24 +309,35 @@ test('every CCTV route refuses cross-site requests and admits the app itself', a
}),
);
const host = 'localhost:5173';
const paths = [
'/sources',
'/health',
`/stream/${STILL.id}`,
`/frame/${STILL.id}`,
`/media/${VIDEO.id}`,
const lease = '00000000-0000-4000-8000-000000000000';
const routes = [
['GET', '/sources'],
['GET', '/health'],
['GET', `/stream/${STILL.id}`],
['GET', `/frame/${STILL.id}`],
['GET', `/media/${VIDEO.id}`],
['GET', `/media/${VIDEO.id}/seg_0.ts?lease=${lease}`],
['DELETE', `/media/${VIDEO.id}?lease=${lease}`],
];
// A catalog refresh would fetch the live packs: a refused request must not
// start one.
process.env.CCTV_FORCE_AUSTIN = '1';
for (const headers of [
{ host, 'sec-fetch-site': 'cross-site' },
{ host, 'sec-fetch-site': 'same-site' },
{ host, origin: 'https://elsewhere.example' },
{ host, origin: 'null' },
])
for (const path of paths) {
const res = await app.call(path, headers);
assert.equal(res.statusCode, 403, `${path} ${JSON.stringify(headers)}`);
for (const [method, path] of routes) {
const res = await app.call(path, headers, method);
assert.equal(
res.statusCode,
403,
`${method} ${path} ${JSON.stringify(headers)}`,
);
}
assert.deepEqual(app.requests, [], 'a refused request reached an upstream');
process.env.CCTV_FORCE_AUSTIN = '0';
for (const headers of [
{ host, 'sec-fetch-site': 'same-origin' },
{ host, origin: `http://${host}` },
@@ -578,9 +493,11 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
app.requests.filter(({ url }) =>
url.startsWith('https://maps.googleapis.com/'),
).length;
const first = { remoteAddress: '203.0.113.7' };
const frameSource = async (socket) =>
(await app.call(`/frame/${STILL.id}`, {}, socket)).headers['X-CCTV-Source'];
const first = '203.0.113.7';
const frameSource = async (address) =>
(await app.call(`/frame/${STILL.id}`, {}, 'GET', address)).headers[
'X-CCTV-Source'
];
// Without a key nothing is spent.
for (let i = 0; i < 3; i++)
@@ -590,7 +507,9 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
// Neither an unknown id nor a forwarded request spends the window.
assert.equal(
(await app.call('/frame/not-a-camera', {}, first)).headers['X-CCTV-Source'],
(await app.call('/frame/not-a-camera', {}, 'GET', first)).headers[
'X-CCTV-Source'
],
'synthetic',
);
assert.equal(
@@ -598,6 +517,7 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
await app.call(
`/frame/${STILL.id}`,
{ 'x-forwarded-for': '203.0.113.9' },
'GET',
first,
)
).headers['X-CCTV-Source'],
@@ -614,14 +534,11 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
assert.equal(streetViewCalls(), 2, 'no Google call over the budget');
// Another address has a budget of its own.
assert.equal(
await frameSource({ remoteAddress: '203.0.113.8' }),
'streetview',
);
assert.equal(await frameSource('203.0.113.8'), 'streetview');
assert.equal(streetViewCalls(), 3);
// Addresses within one IPv6 /64 share a budget.
const v6 = (remoteAddress) => frameSource({ remoteAddress });
const v6 = frameSource;
assert.deepEqual(
[await v6('2001:db8::1'), await v6('2001:db8::2'), await v6('2001:db8::3')],
['streetview', 'streetview', 'synthetic'],
+12 -90
View File
@@ -9,7 +9,7 @@ import assert from 'node:assert/strict';
import http from 'node:http';
import net from 'node:net';
import { Readable, Writable } from 'node:stream';
import { cctvProxy } from '../../server/providers/cctv.js';
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
import { proxyMediaResponse } from '../../server/providers/cctv/media.js';
import { sanitizeCctvRangeHeader } from '../../server/providers/cctv/range.js';
import { CCTV_MEDIA_MAX_BODY_BYTES as CAP } from '../../server/providers/cctv/constants.js';
@@ -131,99 +131,21 @@ function upstreamResponse({ status = 206, headers = {}, body = 'payload' }) {
};
}
/** Collects what a handler wrote, the way a Node ServerResponse would. */
function recordingResponse() {
const chunks = [];
let finished;
const done = new Promise((resolve) => {
finished = resolve;
});
const res = {
statusCode: 0,
headers: {},
writableEnded: false,
body: '',
writeHead(status, headers) {
res.statusCode = status;
res.headers = headers || {};
},
write(chunk) {
chunks.push(Buffer.from(chunk));
return true;
},
end(chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.writableEnded = true;
res.body = Buffer.concat(chunks).toString('utf8');
finished();
},
on() {},
once() {},
emit() {},
destroy() {},
};
return { res, done };
}
/**
* Mount the real provider, serve one camera, and answer its upstream from the
* supplied function.
*/
/** Mount the real provider over the fixture cameras, recording upstream requests. */
function mount(t, respond) {
const before = {
json: process.env.CCTV_SOURCES_JSON,
file: process.env.CCTV_SOURCES_FILE,
austin: process.env.CCTV_FORCE_AUSTIN,
};
process.env.CCTV_SOURCES_JSON = JSON.stringify([CAMERA]);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
const nativeFetch = globalThis.fetch;
const requests = [];
globalThis.fetch = async (url, init = {}) => {
requests.push({
url: String(url),
headers: init.headers || {},
signal: init.signal || null,
});
return respond({ url: String(url), init });
};
t.after(() => {
globalThis.fetch = nativeFetch;
for (const [name, value] of [
['CCTV_SOURCES_JSON', before.json],
['CCTV_SOURCES_FILE', before.file],
['CCTV_FORCE_AUSTIN', before.austin],
]) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});
let handler = null;
const plugin = cctvProxy();
plugin.configureServer({
middlewares: {
use: (_route, fn) => {
handler = fn;
},
const app = mountCctvProvider(t, {
cameras: [CAMERA],
fetch: async (url, init = {}) => {
requests.push({
url: String(url),
headers: init.headers || {},
signal: init.signal || null,
});
return respond({ url: String(url), init });
},
});
assert.ok(handler, 'the CCTV provider must mount a middleware');
/** Drive one request through the mounted handler. */
const call = async (path, headers = {}) => {
const { res, done } = recordingResponse();
await handler({ url: path, headers, method: 'GET', on() {} }, res);
await done;
return res;
};
/** Read the camera's current health entry. */
const health = async () => {
const res = await call('/health');
return JSON.parse(res.body).cameras.find(({ id }) => id === CAMERA.id);
};
return { call, health, requests, handler: (req, res) => handler(req, res) };
return { ...app, requests, health: () => app.health(CAMERA.id) };
}
test('a canonical range reaches the upstream and its 206 is passed through', async (t) => {
+85
View File
@@ -0,0 +1,85 @@
// Reproduction: a viewer refused for HLS capacity is shown its fallback frame
// under a badge that still reads the shared live health ("LIVE · OK").
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { HLS_LIMITS } from '../../server/providers/cctv/stream.js';
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
import { presentCctvSourceBadge } from '../ui/cctvFrames.js';
import { presentCctvPanel } from '../ui/cctvPresentation.js';
const SAME_ORIGIN = { host: 'localhost:4173', 'sec-fetch-site': 'same-origin' };
const PLAYLIST =
'#EXTM3U\n#EXT-X-TARGETDURATION:2\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
const lease = (index) =>
`00000000-0000-4000-8000-${String(index).padStart(12, '0')}`;
const camera = (id) => ({
id,
name: id,
lat: 30.27,
lon: -97.74,
feedType: 'hls',
url: `https://cams.upstream.invalid/${id}/list.m3u8`,
});
test('a capacity-refused viewer is not shown its fallback frame as live and ok', async (t) => {
assert.equal(HLS_LIMITS.sessions, 2);
const { call, health } = mountCctvProvider(t, {
cameras: ['cam-a', 'cam-b', 'cam-c'].map(camera),
fetch: async (url) =>
new Response(String(url).endsWith('.m3u8') ? PLAYLIST : 'abc'),
});
const open = (id, index) =>
call(`/media/${id}?lease=${lease(index)}`, SAME_ORIGIN);
// cam-c played earlier and was released: its row is live/ok.
assert.equal((await open('cam-c', 0)).statusCode, 200);
assert.equal(
(await call(`/media/cam-c?lease=${lease(0)}`, SAME_ORIGIN, 'DELETE'))
.statusCode,
204,
);
// Two other viewers now hold both camera sessions.
assert.equal((await open('cam-a', 1)).statusCode, 200);
assert.equal((await open('cam-b', 2)).statusCode, 200);
// The cam-c viewer is refused and falls back to the frame.
const refused = await open('cam-c', 3);
assert.equal(refused.statusCode, 503);
const frame = await call('/frame/cam-c', SAME_ORIGIN);
assert.equal(frame.statusCode, 200);
assert.equal(frame.headers['X-CCTV-Source'], 'synthetic');
assert.match(frame.body, /UPSTREAM UNAVAILABLE/);
const row = await health('cam-c');
// The badge the panel derives for this viewer, in image (fallback) mode.
const shown = {
feedType: 'hls',
isVideo: false,
sourceKind: row?.sourceKind || 'configured',
sourceStatus: row?.status || 'unknown',
sourceMessage: row?.message || '',
};
const badge = presentCctvSourceBadge(shown, true, {
loading: false,
error: false,
hasFrame: true,
});
assert.notEqual(
badge.label,
'LIVE · OK',
`fallback frame (${frame.headers['X-CCTV-Source']}) labelled ${badge.label}; health message "${row?.message}"`,
);
const { meta } = presentCctvPanel(
{
enabled: true,
activeCameraId: 'cam-c',
activeCamera: {
...shown,
id: 'cam-c',
city: 'Austin',
headingDeg: 0,
fovDeg: 60,
rangeM: 100,
},
},
true,
);
assert.doesNotMatch(meta, /Live HLS connected/);
});
+20
View File
@@ -47,6 +47,26 @@ export function frustumPlanes(pose) {
return [left, rightPlane, bottom, top, nearPlane, farPlane];
}
/**
* `frustumPlanes(pose)`, or null when the pose does not describe a
* perspective frustum: no position, direction or up, or no finite `fovy`,
* `aspectRatio` or `near` (an orthographic camera, no camera).
* @param {object|null|undefined} pose
* @returns {ReturnType<typeof frustumPlanes>|null}
*/
export function viewPlanes(pose) {
if (
!pose?.position ||
!pose.direction ||
!pose.up ||
!Number.isFinite(pose.fovy) ||
!Number.isFinite(pose.aspectRatio) ||
!Number.isFinite(pose.near)
)
return null;
return frustumPlanes(pose);
}
/**
* Whether a sphere is wholly outside `planes` (from `frustumPlanes`), as
* the default renderer's `CullingVolume.computeVisibility` reports
+1 -18
View File
@@ -9,7 +9,7 @@ import { poseSignature } from '../../render/camera/poseSignature.js';
import { heightsOf } from '../../queries/heights.js';
import { fromDegreesInto } from '../../geo/ellipsoid.js';
import { Vec3 } from '../../geo/vector.js';
import { frustumPlanes } from '../../geo/frustum.js';
import { viewPlanes } from '../../geo/frustum.js';
import { perspectiveCourseRotation } from '../../data/screenRotation.js';
import {
PLACED_WAKE_MS,
@@ -1074,23 +1074,6 @@ export function createRendering({ state, services, parts }) {
);
}
/**
* The planes of the frustum the camera looks through, as the default
* renderer culls with them, or null when the pose does not describe one.
*/
function viewPlanes(pose) {
if (
!pose?.position ||
!pose.direction ||
!pose.up ||
!Number.isFinite(pose.fovy) ||
!Number.isFinite(pose.aspectRatio) ||
!Number.isFinite(pose.near)
)
return null;
return frustumPlanes(pose);
}
function refreshVisibility() {
if (!state._enabled) return 0;
const now = Date.now();
+94
View File
@@ -0,0 +1,94 @@
import { cctvProxy } from '../../server/providers/cctv.js';
/** Collects what a handler wrote, the way a Node ServerResponse would. */
export function recordingResponse() {
const chunks = [];
let finished;
const done = new Promise((resolve) => {
finished = resolve;
});
const res = {
statusCode: 0,
headers: {},
writableEnded: false,
body: '',
writeHead(status, headers) {
res.statusCode = status;
res.headers = { ...res.headers, ...headers };
},
setHeader(name, value) {
res.headers[name] = value;
},
write(chunk) {
chunks.push(Buffer.from(chunk));
return true;
},
end(chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.writableEnded = true;
res.body = Buffer.concat(chunks).toString('utf8');
finished();
},
on() {},
once() {},
emit() {},
destroy() {},
};
return { res, done };
}
export function mountCctvProvider(t, { cameras, fetch }) {
const saved = {};
for (const name of [
'CCTV_SOURCES_JSON',
'CCTV_SOURCES_FILE',
'CCTV_FORCE_AUSTIN',
'GOOGLE_MAPS_SERVER_API_KEY',
'GOOGLE_MAPS_API_KEY',
'GEV_RATELIMIT_GOOGLE_PER_MIN',
])
saved[name] = process.env[name];
const nativeFetch = globalThis.fetch;
const closers = [];
t.after(() => {
for (const close of closers) close();
globalThis.fetch = nativeFetch;
for (const [name, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});
process.env.CCTV_SOURCES_JSON = JSON.stringify(cameras);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
// No Street View key: a /frame miss lands on the synthetic billboard.
delete process.env.GOOGLE_MAPS_SERVER_API_KEY;
delete process.env.GOOGLE_MAPS_API_KEY;
// The default Google budget, whatever the shell sets.
delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
// The HLS puller binds fetch when cctvProxy() creates it, so mock first.
globalThis.fetch = fetch;
let handler;
cctvProxy().configureServer({
httpServer: { on: (_event, fn) => closers.push(fn) },
middlewares: { use: (_route, fn) => (handler = fn) },
});
if (!handler) throw new Error('the CCTV provider must mount a middleware');
const call = async (path, headers = {}, method = 'GET', remoteAddress) => {
const { res, done } = recordingResponse();
await handler(
{ url: path, headers, method, socket: { remoteAddress }, on() {} },
res,
);
await done;
return res;
};
const health = async (id) => {
const res = await call('/health');
return JSON.parse(res.body).cameras.find((entry) => entry.id === id);
};
return { call, health, handler };
}
+1 -1
View File
@@ -234,7 +234,7 @@ export function bindFakeCesiumScene(
direction: c?.directionWC
? plain(c.directionWC)
: { x: 0, y: 0, z: -1 },
up: { x: 0, y: 1, z: 0 },
up: c?.upWC ? plain(c.upWC) : { x: 0, y: 1, z: 0 },
right: { x: 1, y: 0, z: 0 },
// An orthographic frustum has none, as the Cesium port reports it.
fovy:
+13
View File
@@ -186,6 +186,14 @@ export function _settleCctvFrame(token, src, ok) {
);
}
/** Shared live health describes the stream even when this viewer shows a frame. */
export function isCctvLiveFrameFallback(activeCamera) {
return (
activeCamera?.isVideo === false &&
String(activeCamera.sourceKind || '').toLowerCase() === 'live'
);
}
/** The source badge's label and frame state for the camera and preview. */
export function presentCctvSourceBadge(activeCamera, enabled, frame) {
if (!enabled || !activeCamera)
@@ -194,6 +202,11 @@ export function presentCctvSourceBadge(activeCamera, enabled, frame) {
return { label: t('FRAME · LOADING'), state: 'loading' };
if (frame.error && !frame.hasFrame)
return { label: t('FRAME · UNAVAILABLE'), state: 'error' };
if (isCctvLiveFrameFallback(activeCamera))
return {
label: `${cctvSourceKindLabel('fallback')} · ${cctvSourceStatusLabel('degraded')}`,
state: 'ready',
};
const kind = cctvSourceKindLabel(
activeCamera.sourceKind || activeCamera.feedType || 'unknown',
);
+9 -4
View File
@@ -1,5 +1,9 @@
import { getLocale, keepCodesWithValues, msg, t, tn } from '../i18n/index.js';
import { cctvSourceLabelText, cctvSourceMessageText } from './cctvFrames.js';
import {
cctvSourceLabelText,
cctvSourceMessageText,
isCctvLiveFrameFallback,
} from './cctvFrames.js';
import { createCctvVideoSurface } from './cctvVideo.js';
import { presentCctvCalibration } from './cctvCalibration.js';
@@ -105,9 +109,10 @@ function metaText(state, enabled, activeCamera, catalogCount) {
const provider = cctvSourceLabelText(
activeCamera.sourceLabel || activeCamera.provider || 'Configured Source',
);
const statusMsg = activeCamera.sourceMessage
? ` · ${cctvSourceMessageText(activeCamera.sourceMessage)}`
: '';
const statusMsg =
activeCamera.sourceMessage && !isCctvLiveFrameFallback(activeCamera)
? ` · ${cctvSourceMessageText(activeCamera.sourceMessage)}`
: '';
// A partner-supplied feed inside a pack names its owner here.
const credit = activeCamera.credit ? ` · ${activeCamera.credit}` : '';
const calBadge = activeCamera.calBadge