diff --git a/changelog.d/1267-cctv-hls-capacity-health.md b/changelog.d/1267-cctv-hls-capacity-health.md index a86c90e82a..e40c0762e1 100644 --- a/changelog.d/1267-cctv-hls-capacity-health.md +++ b/changelog.d/1267-cctv-hls-capacity-health.md @@ -1,4 +1,5 @@ - Stop reporting a live HLS camera as degraded when a viewer is refused because every viewer or session slot is taken: the refusal answers 503 with - `Retry-After`, and the frame shown in its place leaves the camera's health - as it was (asturksever, gekh, #1267). + `Retry-After`, the frame shown in its place leaves the camera's health + as it was, and that viewer's panel labels the frame `FALLBACK · DEGRADED` + rather than live (asturksever, gekh, #1267). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index a05ed3c4ae..ee9c7e6d95 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -9658,7 +9658,9 @@ coexist with at most eight independent consumer leases each; no-access lease expiry is 15 seconds. A viewer refused because every lease or session slot is taken gets 503 with `Retry-After: 2` and leaves the camera's health unchanged; so does its frame fallback, since an HLS camera without a snapshot has no frame -of its own. Closing one consumer leaves other consumers running. Lease +of its own. That viewer's panel badge reads FALLBACK · DEGRADED and its meta +line drops the live health message, since the shared live health does not +describe the frame it shows. Closing one consumer leaves other consumers running. Lease ids use `crypto.randomUUID` where the page is a secure context and an equivalent v4 id from `crypto.getRandomValues` on plain-HTTP LAN pages. Encrypted/fMP4/byte-range playlists fail closed. Performance and live agency reliability require recorded validation. diff --git a/src/data/cctvRefusedViewerBadge.test.mjs b/src/data/cctvRefusedViewerBadge.test.mjs new file mode 100644 index 0000000000..8231eb0e83 --- /dev/null +++ b/src/data/cctvRefusedViewerBadge.test.mjs @@ -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/); +}); diff --git a/src/ui/cctvFrames.js b/src/ui/cctvFrames.js index 7617111748..171a67fb34 100644 --- a/src/ui/cctvFrames.js +++ b/src/ui/cctvFrames.js @@ -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', ); diff --git a/src/ui/cctvPresentation.js b/src/ui/cctvPresentation.js index 2f4cb8584e..92653e0ad7 100644 --- a/src/ui/cctvPresentation.js +++ b/src/ui/cctvPresentation.js @@ -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'; @@ -96,9 +100,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