Skip to content

Commit 210baee

Browse files
committed
added acquireId guard to prevent stale getUserMedia from repopulating webcamStream
1 parent 5ff6139 commit 210baee

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

src/hooks/useScreenRecorder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
104104
const discardRecordingId = useRef<number | null>(null);
105105
const restarting = useRef(false);
106106
const webcamReady = useRef(false);
107+
const webcamAcquireId = useRef(0);
107108

108109
const selectMimeType = () => {
109110
const preferred = [
@@ -183,6 +184,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
183184

184185
let cancelled = false;
185186
let acquiredStream: MediaStream | null = null;
187+
const thisAcquireId = ++webcamAcquireId.current;
186188
webcamReady.current = false;
187189

188190
const acquire = async () => {
@@ -203,7 +205,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
203205
},
204206
});
205207

206-
if (cancelled) {
208+
if (cancelled || thisAcquireId !== webcamAcquireId.current) {
207209
stream.getTracks().forEach((track) => {
208210
track.onended = null;
209211
track.stop();
@@ -499,6 +501,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
499501
});
500502
}
501503
if (!webcamStream.current) {
504+
webcamAcquireId.current++;
502505
setWebcamEnabledState(false);
503506
}
504507
}

0 commit comments

Comments
 (0)