added acquireId guard to prevent stale getUserMedia from repopulating webcamStream

This commit is contained in:
dheerajmr01
2026-04-04 14:25:48 -05:00
parent 5ff613922f
commit 210baee0da
+4 -1
View File
@@ -104,6 +104,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
const discardRecordingId = useRef<number | null>(null);
const restarting = useRef(false);
const webcamReady = useRef(false);
const webcamAcquireId = useRef(0);
const selectMimeType = () => {
const preferred = [
@@ -183,6 +184,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
let cancelled = false;
let acquiredStream: MediaStream | null = null;
const thisAcquireId = ++webcamAcquireId.current;
webcamReady.current = false;
const acquire = async () => {
@@ -203,7 +205,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
},
});
if (cancelled) {
if (cancelled || thisAcquireId !== webcamAcquireId.current) {
stream.getTracks().forEach((track) => {
track.onended = null;
track.stop();
@@ -499,6 +501,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
});
}
if (!webcamStream.current) {
webcamAcquireId.current++;
setWebcamEnabledState(false);
}
}