Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(recording): answer the review on the camera fixes

EtienneLescotbe144d8a4c14
diff --git a/electron/recording/nativeWindowsCaptureStop.test.ts b/electron/recording/nativeWindowsCaptureStop.test.ts
index 77663ba3b..acbee862e 100644
--- a/electron/recording/nativeWindowsCaptureStop.test.ts
+++ b/electron/recording/nativeWindowsCaptureStop.test.ts
@@ -124,6 +124,28 @@ describe("readWebcamFormat", () => {
});
});
+ // A friendly name is free text from the driver, so it can contain the very
+ // character that used to end the slice. Cutting the object there dropped a
+ // camera that was working perfectly.
+ it("reads a device name that contains a closing brace", () => {
+ const output =
+ '{"event":"webcam-format","schemaVersion":2,"width":1280,"height":720,"fps":30,"deviceName":"Camera } Studio"}\n';
+ expect(readWebcamFormat(output)).toMatchObject({
+ width: 1280,
+ deviceName: "Camera } Studio",
+ });
+ });
+
+ it("reads a device name whose escaped quote precedes a brace", () => {
+ const output =
+ '{"event":"webcam-format","schemaVersion":2,"width":640,"height":480,"fps":30,"deviceName":"Cam \\"X\\" } 2"}\n';
+ expect(readWebcamFormat(output)).toMatchObject({ deviceName: 'Cam "X" } 2' });
+ });
+
+ it("is null when the object is cut off mid-way", () => {
+ expect(readWebcamFormat('{"event":"webcam-format","width":1280')).toBe(null);
+ });
+
it("is null when the helper never announced a camera", () => {
expect(readWebcamFormat("Recording started\n")).toBe(null);
});
diff --git a/electron/recording/nativeWindowsCaptureStop.ts b/electron/recording/nativeWindowsCaptureStop.ts
index b545d50fa..24f047f3b 100644
--- a/electron/recording/nativeWindowsCaptureStop.ts
+++ b/electron/recording/nativeWindowsCaptureStop.ts
@@ -109,6 +109,44 @@ export function readWebcamUnavailable(output: string) {
return output.includes('"code":"webcam-unavailable"');
}
+/**
+ * Index of the `}` that closes the object starting at `start`, or -1.
+ *
+ * Stopping at the first `}` is wrong for a value that contains one, and a
+ * camera's friendly name is free text straight from the driver — "Camera }
+ * Studio" is unusual but nothing forbids it, and cutting the object there turns
+ * a working camera into one that reported no format at all. So brace depth is
+ * counted, and braces inside a JSON string are skipped along with anything an
+ * escape protects.
+ */
+function findObjectEnd(output: string, start: number) {
+ let depth = 0;
+ let inString = false;
+ let escaped = false;
+ for (let index = start; index < output.length; index += 1) {
+ const ch = output[index];
+ if (escaped) {
+ escaped = false;
+ continue;
+ }
+ if (ch === "\\" && inString) {
+ escaped = true;
+ continue;
+ }
+ if (ch === '"') {
+ inString = !inString;
+ continue;
+ }
+ if (inString) continue;
+ if (ch === "{") depth += 1;
+ else if (ch === "}") {
+ depth -= 1;
+ if (depth === 0) return index;
+ }
+ }
+ return -1;
+}
+
/**
* The format the helper negotiated with the camera, or null if it never said.
*
@@ -125,7 +163,7 @@ export function readWebcamFormat(output: string) {
if (start === -1) {
return null;
}
- const end = output.indexOf("}", start);
+ const end = findObjectEnd(output, start);
if (end === -1) {
return null;
}
diff --git a/src/hooks/useCameraDevices.ts b/src/hooks/useCameraDevices.ts
index 6a2b45f5f..0466cc40c 100644
--- a/src/hooks/useCameraDevices.ts
+++ b/src/hooks/useCameraDevices.ts
@@ -20,10 +20,21 @@ export function useCameraDevices(enabled: boolean = false, preferredDeviceId?: s
const [selectedDeviceId, setSelectedDeviceId] = useState<string>("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
+ // `loadDevices` runs long after the render that scheduled it — on a
+ // `devicechange` that may arrive at any moment — so it reads these two
+ // through refs rather than closing over them.
+ //
+ // Synchronised in an effect and not during render: React may discard a render
+ // without committing it, and a ref written there keeps the value anyway. The
+ // selection would then be resolved against a device the committed tree never
+ // agreed on. Declared above the loading effect so the refs are current before
+ // the first enumeration reads them.
const selectedDeviceIdRef = useRef(selectedDeviceId);
- selectedDeviceIdRef.current = selectedDeviceId;
const preferredDeviceIdRef = useRef(preferredDeviceId);
- preferredDeviceIdRef.current = preferredDeviceId;
+ useEffect(() => {
+ selectedDeviceIdRef.current = selectedDeviceId;
+ preferredDeviceIdRef.current = preferredDeviceId;
+ }, [selectedDeviceId, preferredDeviceId]);
useEffect(() => {
if (!enabled) return;
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 2e669be4a..5668b0f92 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -199,7 +199,12 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
* recording is starting from — the take never begins, with nothing logged.
*/
const tRef = useRef(t);
- tRef.current = t;
+ // In an effect, not during render: a render React discards still leaves a ref
+ // written there, and a later recording error would then be worded by a UI that
+ // never reached the screen.
+ useEffect(() => {
+ tRef.current = t;
+ }, [t]);
const [recording, setRecording] = useState(false);
const [paused, setPaused] = useState(false);
const [saving, setSaving] = useState(false);
diff --git a/src/i18n/locales/it/editor.json b/src/i18n/locales/it/editor.json
index 229c74028..ba0afe4db 100644
--- a/src/i18n/locales/it/editor.json
+++ b/src/i18n/locales/it/editor.json
@@ -42,7 +42,7 @@
"cameraDenied": "Accesso alla fotocamera negato. La registrazione continuerà senza webcam.",
"cameraDisconnected": "Webcam disconnessa.",
"cameraNotFound": "Fotocamera non trovata.",
- "cameraCaptureUnavailable": "Impossibile aprire la fotocamera. Registrazione senza.",
+ "cameraCaptureUnavailable": "Impossibile aprire la fotocamera. Registrazione senza di essa.",
"permissionDenied": "Autorizzazione di registrazione negata. Consenti la registrazione dello schermo.",
"accessibilityAllowAndRetry": "Consenti l'accesso all'accessibilità per OpenScreen, poi premi di nuovo registra per avviare il conto alla rovescia.",
"selectSource": "Seleziona una sorgente da registrare"