diff --git a/web/js/components/preact/HLSVideoCell.tsx b/web/js/components/preact/HLSVideoCell.tsx index b1a83007c..0cbaea3c0 100644 --- a/web/js/components/preact/HLSVideoCell.tsx +++ b/web/js/components/preact/HLSVideoCell.tsx @@ -4,7 +4,7 @@ import { refreshStream } from '../../api/mediaLifecycle.ts'; * A self-contained component for displaying an HLS video stream */ -import { useState, useEffect, useRef } from 'preact/hooks'; +import { useState, useEffect, useLayoutEffect, useRef } from 'preact/hooks'; import { DetectionOverlay, drawDetectionsOnCanvas } from './DetectionOverlay.tsx'; import { SnapshotButton } from './SnapshotManager.tsx'; import { ManualRecordingButton } from './ManualRecordingButton.tsx'; @@ -738,6 +738,17 @@ export function HLSVideoCell({ // Auto-retry while the error overlay is visible — see WebRTCVideoCell for rationale. const autoRetryCountdown = useAutoRetry(privacyActive ? null : error, handleRetry); + const privacyRequestGenerationRef = useRef(0); + const streamIdentityRef = useRef(stream.name); + + useLayoutEffect(() => { + streamIdentityRef.current = stream.name; + privacyRequestGenerationRef.current += 1; + setIsTogglingEnabled(false); + setShowPrivacyConfirm(false); + return () => { privacyRequestGenerationRef.current += 1; }; + }, [stream.name]); + useEffect(() => { if (error && onTransportFailure) onTransportFailure(error); }, [error, onTransportFailure]); @@ -747,22 +758,41 @@ export function HLSVideoCell({ * The stream stays in the Live View grid under a privacy overlay. */ const handlePauseForPrivacy = async () => { + const requestStreamIdentity = stream.name; + const requestGeneration = ++privacyRequestGenerationRef.current; setIsTogglingEnabled(true); try { - const res = await fetch(`/api/streams/${encodeURIComponent(stream.name)}`, { + const res = await fetch(`/api/streams/${encodeURIComponent(requestStreamIdentity)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ set_privacy_mode: true }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } setPrivacyActive(true); setShowPrivacyConfirm(false); queryClient.invalidateQueries({ queryKey: ['streams'] }); } catch (err) { + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } showStatusMessage(`${t('live.pauseForPrivacy')}: ${(err as Error).message}`, 'error', 5000); setShowPrivacyConfirm(false); } finally { - setIsTogglingEnabled(false); + if ( + privacyRequestGenerationRef.current === requestGeneration && + streamIdentityRef.current === requestStreamIdentity + ) { + setIsTogglingEnabled(false); + } } }; @@ -770,20 +800,39 @@ export function HLSVideoCell({ * Resume stream from privacy mode — sets privacy_mode=false and re-starts the stream. */ const handleResumeFromPrivacy = async () => { + const requestStreamIdentity = stream.name; + const requestGeneration = ++privacyRequestGenerationRef.current; setIsTogglingEnabled(true); try { - const res = await fetch(`/api/streams/${encodeURIComponent(stream.name)}`, { + const res = await fetch(`/api/streams/${encodeURIComponent(requestStreamIdentity)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ set_privacy_mode: false }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } setPrivacyActive(false); queryClient.invalidateQueries({ queryKey: ['streams'] }); } catch (err) { + if ( + privacyRequestGenerationRef.current !== requestGeneration || + streamIdentityRef.current !== requestStreamIdentity + ) { + return; + } showStatusMessage(`${t('live.resumeStream')}: ${(err as Error).message}`, 'error', 5000); } finally { - setIsTogglingEnabled(false); + if ( + privacyRequestGenerationRef.current === requestGeneration && + streamIdentityRef.current === requestStreamIdentity + ) { + setIsTogglingEnabled(false); + } } }; diff --git a/web/tests/e2e/hlsRecovery.browser.spec.js b/web/tests/e2e/hlsRecovery.browser.spec.js index e3b29d866..055125dcf 100644 --- a/web/tests/e2e/hlsRecovery.browser.spec.js +++ b/web/tests/e2e/hlsRecovery.browser.spec.js @@ -36,6 +36,9 @@ beforeAll(async () => { HTMLMediaElement.prototype.load=()=>{}; window.fetch=async(input,options={})=>{ window.requests.push({url:String(input),signal:options.signal}); + if(options.method==='PUT'&&window.config.deferPrivacy) return new Promise(resolve=>{ + window.resolvePrivacyRequest=()=>{window.privacyResolved=true;resolve(new Response('',{status:200}));}; + }); if(window.config.deferManifest) return new Promise((resolve,reject)=>{ options.signal.addEventListener('abort',()=>{window.fetchAborted=true;reject(new DOMException('Aborted','AbortError'));},{once:true}); }); @@ -164,6 +167,25 @@ it('focuses the resume control when privacy mode starts active',async()=>{ } finally {await browser.close();} }); +it('ignores an old privacy response after the tile changes streams',async()=>{ + const {browser,page}=await openPlayer({privacyMode:true,deferPrivacy:true}); + try { + await page.locator('button[aria-label="live.resumeStream"]').click(); + await page.waitForFunction(()=>!!window.resolvePrivacyRequest); + await page.evaluate(()=>{ + window.config.streamName='second camera'; + window.config.streamId='second'; + window.mount(); + window.resolvePrivacyRequest(); + }); + await page.waitForFunction(()=>window.privacyResolved&& + document.querySelector('.video-cell')?.dataset.streamName==='second camera'); + expect(await page.locator('button[aria-label="live.resumeStream"]').isVisible()).toBe(true); + expect(await page.locator('button[aria-label="live.resumeStream"]').isEnabled()).toBe(true); + expect(await page.evaluate(()=>window.errors)).toEqual([]); + } finally {await browser.close();} +}); + it('resets fallback transport when the tile receives a different stream',async()=>{ const {browser,page}=await openPlayer({enabled:false}); try {