Skip to content

The free and privacy-friendly screen recorder with no limits πŸŽ₯

testing longer recordings...

Alyssa X2cbb959828af
diff --git a/build.zip b/build.zip
new file mode 100644
index 0000000..eb13c2f
Binary files /dev/null and b/build.zip differ
diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsx
index b974d2c..0e2a013 100644
--- a/src/pages/Recorder/Recorder.jsx
+++ b/src/pages/Recorder/Recorder.jsx
@@ -4,7 +4,6 @@ import RecorderUI from "./RecorderUI";
import { createMediaRecorder } from "./mediaRecorderUtils";
import { sendRecordingError, sendStopRecording } from "./messaging";
import { getBitrates, getResolutionForQuality } from "./recorderConfig";
-
import { WebCodecsRecorder } from "./webcodecs/WebCodecsRecorder";
localforage.config({
@@ -13,13 +12,43 @@ localforage.config({
version: 1,
});
-// Get chunks store
const chunksStore = localforage.createInstance({
name: "chunks",
});
document.body.style.willChange = "contents";
+// ============================================================
+// DEBUG FLAG + LOGGER
+// ============================================================
+// Flip this to true locally when you want super detailed logs.
+// You can also override from the devtools console via:
+// window.SCREENITY_DEBUG_RECORDER = true;
+//const DEBUG_RECORDER =
+//typeof window !== "undefined" ? !!window.SCREENITY_DEBUG_RECORDER : false;
+
+const DEBUG_RECORDER = true;
+
+const logPrefix = "[Screenity Recorder]";
+
+function debug(...args) {
+ if (!DEBUG_RECORDER) return;
+ // eslint-disable-next-line no-console
+ console.log(logPrefix, ...args);
+}
+
+function debugWarn(...args) {
+ if (!DEBUG_RECORDER) return;
+ // eslint-disable-next-line no-console
+ console.warn(logPrefix, ...args);
+}
+
+function debugError(...args) {
+ if (!DEBUG_RECORDER) return;
+ // eslint-disable-next-line no-console
+ console.error(logPrefix, ...args);
+}
+
const Recorder = () => {
const isRestarting = useRef(false);
const isFinishing = useRef(false);
@@ -32,14 +61,11 @@ const Recorder = () => {
const [started, setStarted] = useState(false);
- // Main stream (recording)
const liveStream = useRef(null);
- // Helper streams
const helperVideoStream = useRef(null);
const helperAudioStream = useRef(null);
- // Audio controls, with refs to persist across renders
const aCtx = useRef(null);
const destination = useRef(null);
const audioInputSource = useRef(null);
@@ -71,6 +97,8 @@ const Recorder = () => {
const isRecording = useRef(false);
+ debug("Recorder component mounted");
+
async function canFitChunk(byteLength) {
const now = performance.now();
if (now - lastEstimateAt.current < ESTIMATE_INTERVAL_MS) {
@@ -81,8 +109,19 @@ const Recorder = () => {
try {
const { usage = 0, quota = 0 } = await navigator.storage.estimate();
const remaining = quota - usage;
- return remaining > MIN_HEADROOM + (byteLength || 0);
- } catch {
+ const ok = remaining > MIN_HEADROOM + (byteLength || 0);
+ if (DEBUG_RECORDER) {
+ debug("Storage estimate", {
+ usage,
+ quota,
+ remaining,
+ byteLength,
+ ok,
+ });
+ }
+ return ok;
+ } catch (err) {
+ debugWarn("navigator.storage.estimate() failed, assuming OK", err);
return !lowStorageAbort.current;
}
}
@@ -90,16 +129,8 @@ const Recorder = () => {
async function saveChunk(e, i) {
const ts = e.timecode ?? 0;
- // FLAG: disabled duplicate chunk check due to issues with some devices
- // if (
- // savedCount.current > 0 &&
- // ts === lastTimecode.current &&
- // e.data.size === lastSize.current
- // ) {
- // return false;
- // }
-
if (!(await canFitChunk(e.data.size))) {
+ debugWarn("Low storage, aborting recording");
lowStorageAbort.current = true;
chrome.storage.local.set({
recording: false,
@@ -117,7 +148,15 @@ const Recorder = () => {
chunk: e.data,
timestamp: ts,
});
+ if (DEBUG_RECORDER) {
+ debug("Saved chunk to IndexedDB", {
+ key: `chunk_${i}`,
+ size: e.data.size,
+ ts,
+ });
+ }
} catch (err) {
+ debugError("Failed to save chunk, aborting recording", err);
lowStorageAbort.current = true;
chrome.storage.local.set({
recording: false,
@@ -144,8 +183,15 @@ const Recorder = () => {
draining.current = true;
try {
+ if (DEBUG_RECORDER) {
+ debug("Draining queue start", {
+ pending: pending.current.length,
+ pendingBytes: pendingBytes.current,
+ });
+ }
while (pending.current.length) {
if (lowStorageAbort.current) {
+ debugWarn("Low storage while draining, clearing queue");
pending.current.length = 0;
pendingBytes.current = 0;
break;
@@ -155,6 +201,7 @@ const Recorder = () => {
pendingBytes.current -= e.data.size;
if (!(await canFitChunk(e.data.size))) {
+ debugWarn("Low storage during drain, stopping recording");
lowStorageAbort.current = true;
chrome.storage.local.set({
recording: false,
@@ -173,40 +220,55 @@ const Recorder = () => {
if (saved) index.current = i + 1;
}
} finally {
+ if (DEBUG_RECORDER) {
+ debug("Draining queue finished", {
+ pending: pending.current.length,
+ pendingBytes: pendingBytes.current,
+ });
+ }
draining.current = false;
}
}
async function waitForDrain() {
+ if (DEBUG_RECORDER) {
+ debug("waitForDrain() called");
+ }
while (draining.current || pending.current.length) {
await new Promise((r) => setTimeout(r, 10));
}
+ if (DEBUG_RECORDER) {
+ debug("waitForDrain() resolved");
+ }
}
useEffect(() => {
chrome.storage.local.get(["backup"], (result) => {
- if (result.backup) {
- backupRef.current = true;
- } else {
- backupRef.current = false;
- }
+ backupRef.current = !!result.backup;
+ debug("Loaded backup flag from storage", backupRef.current);
});
}, []);
async function startRecording() {
- // Check that a recording is not already in progress
- if (recorder.current !== null) return;
+ if (recorder.current !== null) {
+ debugWarn("startRecording() called but recorder already exists");
+ return;
+ }
+
+ debug("startRecording()");
navigator.storage.persist();
if (
!helperVideoStream.current ||
helperVideoStream.current.getVideoTracks().length === 0
) {
+ debugError("No video tracks available in helperVideoStream");
sendRecordingError("No video tracks available");
return;
}
await chunksStore.clear();
+ debug("Cleared chunksStore");
lastTimecode.current = 0;
lastSize.current = 0;
@@ -223,23 +285,38 @@ const Recorder = () => {
const { audioBitsPerSecond, videoBitsPerSecond } =
getBitrates(qualityValue);
- // Decide if we can use WebCodecs
+ debug("Bitrates resolved", {
+ qualityValue,
+ audioBitsPerSecond,
+ videoBitsPerSecond,
+ });
+
+ // FLAG: FORCE OLD MediaRecorder for testing
+ const FORCE_MEDIARECORDER = false;
+
const canUseWebCodecs =
+ !FORCE_MEDIARECORDER &&
typeof VideoEncoder !== "undefined" &&
typeof AudioEncoder !== "undefined" &&
typeof MediaStreamTrackProcessor !== "undefined";
- // Get basic video info
const videoTrack = liveStream.current?.getVideoTracks()[0] ?? null;
const settings = videoTrack?.getSettings() || {};
const width = settings.width ?? 1920;
const height = settings.height ?? 1080;
- // fps from storage
const { fpsValue } = await chrome.storage.local.get(["fpsValue"]);
let fps = parseInt(fpsValue);
if (Number.isNaN(fps)) fps = 30;
+ debug("Recorder capabilities", {
+ canUseWebCodecs,
+ FORCE_MEDIARECORDER,
+ width,
+ height,
+ fps,
+ });
+
chrome.storage.local.set({
recording: true,
restarting: false,
@@ -249,10 +326,7 @@ const Recorder = () => {
isRestarting.current = false;
index.current = 0;
- // Common "onChunk" handler that uses your existing queue + drain logic
- // Common "onChunk" handler
const handleChunk = async (data, timestampMs) => {
- // πŸ”Ή WebCodecs path: save directly, no queue, no canFitChunk
if (useWebCodecs.current) {
const blob =
data instanceof Blob ? data : new Blob([data], { type: "video/mp4" });
@@ -260,37 +334,35 @@ const Recorder = () => {
const ts = timestampMs ?? 0;
const i = index.current;
- console.log(
- "[RECORDER] WebCodecs handleChunk β†’ index",
- i,
- "size",
- blob.size,
- "ts",
- ts
- );
-
try {
await chunksStore.setItem(`chunk_${i}`, {
index: i,
chunk: blob,
timestamp: ts,
});
- console.log("[RECORDER] WebCodecs saved chunk_", i);
index.current = i + 1;
hasChunks.current = true;
savedCount.current += 1;
+ if (DEBUG_RECORDER) {
+ debug("WebCodecs chunk saved", {
+ i,
+ ts,
+ size: blob.size,
+ savedCount: savedCount.current,
+ });
+ }
+
if (backupRef.current) {
chrome.runtime.sendMessage({ type: "write-file", index: i });
}
} catch (err) {
- console.error("[RECORDER] WebCodecs saveChunk error", err);
+ debugError("Failed to save WebCodecs chunk", err);
}
return;
}
- // πŸ”Ή MediaRecorder path (unchanged)
if (lowStorageAbort.current) return;
const blob =
@@ -310,7 +382,19 @@ const Recorder = () => {
pending.current.push(e);
pendingBytes.current += e.data.size;
+ if (DEBUG_RECORDER) {
+ debug("Queued MediaRecorder chunk", {
+ size: blob.size,
+ timecode: e.timecode,
+ pending: pending.current.length,
+ pendingBytes: pendingBytes.current,
+ });
+ }
+
if (pendingBytes.current > MAX_PENDING_BYTES) {
+ debugWarn(
+ "Pending bytes exceeded threshold, pausing MediaRecorder and draining queue"
+ );
try {
if (
recorder.current instanceof MediaRecorder &&
@@ -325,7 +409,11 @@ const Recorder = () => {
) {
recorder.current.resume();
}
- } catch {
+ } catch (err) {
+ debugError(
+ "Error while draining queue with MediaRecorder paused",
+ err
+ );
await drainQueue();
}
}
@@ -335,20 +423,30 @@ const Recorder = () => {
try {
if (canUseWebCodecs) {
- // βœ… Use WebCodecs backend
useWebCodecs.current = true;
+ const hasAudioTrack =
+ !!liveStream.current &&
+ typeof liveStream.current.getAudioTracks === "function" &&
+ liveStream.current.getAudioTracks().length > 0;
+
+ debug("Initializing WebCodecsRecorder", {
+ hasAudioTrack,
+ videoBitsPerSecond,
+ audioBitsPerSecond,
+ });
+
recorder.current = new WebCodecsRecorder(liveStream.current, {
width,
height,
fps,
videoBitrate: videoBitsPerSecond,
- audioBitrate: audioBitsPerSecond,
+ audioBitrate: hasAudioTrack ? audioBitsPerSecond : undefined,
+ enableAudio: hasAudioTrack,
+ debug: DEBUG_RECORDER,
onFinalized: async () => {
- // Finish writing any remaining chunks (usually none)
+ debug("WebCodecsRecorder onFinalized()");
await waitForDrain();
-
- // Let the editor know the MP4 blobs are ready
if (!sentLast.current) {
sentLast.current = true;
isFinishing.current = false;
@@ -356,39 +454,43 @@ const Recorder = () => {
}
},
onChunk: (chunkData, timestampUs) => {
- // chunkData = Uint8Array from muxer
const blob = new Blob([chunkData], { type: "video/mp4" });
-
- // Convert microseconds β†’ ms (what the rest of your code expects)
const timestampMs = timestampUs
? Math.floor(timestampUs / 1000)
: 0;
-
+ if (DEBUG_RECORDER) {
+ debug("WebCodecsRecorder onChunk", {
+ size: blob.size,
+ timestampUs,
+ timestampMs,
+ });
+ }
handleChunk(blob, timestampMs);
},
onError: (err) => {
- console.error("[WebCodecsRecorder] error", err);
+ debugError("WebCodecsRecorder error", err);
sendRecordingError(String(err));
},
onStop: async () => {
+ debug("WebCodecsRecorder onStop()");
await waitForDrain();
},
});
const ok = await recorder.current.start();
+ debug("WebCodecsRecorder.start() result", ok);
+
if (!ok) {
- console.error(
- "[RECORDER] WebCodecs start failed β€” falling back to MediaRecorder."
+ debugWarn(
+ "Falling back to MediaRecorder because WebCodecsRecorder failed"
);
useWebCodecs.current = false;
recorder.current = null;
-
- // fall back into MediaRecorder path
return await startRecording();
}
} else {
- // πŸ” Fallback to MediaRecorder (current behavior)
+ debug("Using MediaRecorder fallback");
useWebCodecs.current = false;
recorder.current = createMediaRecorder(liveStream.current, {
audioBitsPerSecond,
@@ -397,7 +499,9 @@ const Recorder = () => {
try {
recorder.current.start(1000);
+ debug("MediaRecorder.start(1000) called");
} catch (err) {
+ debugError("Failed to start MediaRecorder", err);
sendRecordingError(
"Failed to start recording: " + JSON.stringify(err)
);
@@ -405,6 +509,7 @@ const Recorder = () => {
}
recorder.current.onerror = (ev) => {
+ debugError("MediaRecorder.onerror", ev);
chrome.runtime.sendMessage({
type: "recording-error",
error: "mediarecorder",
@@ -413,6 +518,7 @@ const Recorder = () => {
};
recorder.current.onstop = async () => {
+ debug("MediaRecorder.onstop");
try {
recorder.current.requestData();
} catch {}
@@ -427,6 +533,7 @@ const Recorder = () => {
recorder.current.ondataavailable = async (e) => {
if (!e || !e.data || !e.data.size) {
+ debugWarn("MediaRecorder.ondataavailable with empty data", e);
if (
recorder.current instanceof MediaRecorder &&
recorder.current.state === "inactive"
@@ -441,19 +548,27 @@ const Recorder = () => {
return;
}
+ if (DEBUG_RECORDER) {
+ debug("MediaRecorder.ondataavailable", {
+ size: e.data.size,
+ timecode: e.timecode ?? 0,
+ });
+ }
+
await handleChunk(e.data, e.timecode ?? 0);
};
}
} catch (err) {
+ debugError("startRecording() top-level error", err);
sendRecordingError(JSON.stringify(err));
return;
}
- // Same audio/video track onended logic
if (helperAudioStream.current) {
const track = helperAudioStream.current.getAudioTracks()[0];
if (track) {
track.onended = () => {
+ debugWarn("helperAudioStream audio track ended");
chrome.storage.local.set({ recording: false });
sendStopRecording();
};
@@ -461,6 +576,7 @@ const Recorder = () => {
}
liveStream.current.getVideoTracks()[0].onended = () => {
+ debugWarn("liveStream video track ended");
chrome.storage.local.set({
recording: false,
restarting: false,
@@ -470,6 +586,7 @@ const Recorder = () => {
};
helperVideoStream.current.getVideoTracks()[0].onended = () => {
+ debugWarn("helperVideoStream video track ended");
chrome.storage.local.set({
recording: false,
restarting: false,
@@ -480,10 +597,14 @@ const Recorder = () => {
}
async function warmUpStream(liveStream) {
+ debug("warmUpStream() start", {
+ hasVideo: liveStream.getVideoTracks().length,
+ hasAudio: liveStream.getAudioTracks().length,
+ });
+
const videoTrack = liveStream.getVideoTracks()[0];
const audioTrack = liveStream.getAudioTracks()[0];
- // 1) Probe video track for a real frame
await new Promise(async (resolve) => {
const proc = new MediaStreamTrackProcessor({ track: videoTrack });
const reader = proc.readable.getReader();
@@ -492,6 +613,10 @@ const Recorder = () => {
const { value: frame } = await reader.read();
if (frame) {
if (frame.codedWidth > 0 && frame.codedHeight > 0) {
+ debug("warmUpStream() video frame OK", {
+ codedWidth: frame.codedWidth,
+ codedHeight: frame.codedHeight,
+ });
frame.close();
reader.releaseLock();
resolve();
@@ -502,7 +627,6 @@ const Recorder = () => {
}
});
- // 2) Probe audio track for real samples
if (audioTrack) {
await new Promise(async (resolve) => {
const proc = new MediaStreamTrackProcessor({ track: audioTrack });
@@ -511,6 +635,9 @@ const Recorder = () => {
while (true) {
const { value: audio } = await reader.read();
if (audio && audio.numberOfFrames > 0) {
+ debug("warmUpStream() audio OK", {
+ numberOfFrames: audio.numberOfFrames,
+ });
audio.close?.();
reader.releaseLock();
resolve();
@@ -521,14 +648,15 @@ const Recorder = () => {
});
}
- console.log("[warmUp] audio + video tracks are now stable");
+ debug("warmUpStream() done");
}
async function stopRecording() {
if (isFinishing.current) {
- console.log("[RECORDER] stopRecording ignored (already running)");
+ debugWarn("stopRecording() called while already finishing");
return;
}
+ debug("stopRecording()");
isFinishing.current = true;
isRecording.current = false;
@@ -537,8 +665,10 @@ const Recorder = () => {
useWebCodecs.current &&
recorder.current instanceof WebCodecsRecorder
) {
+ debug("Stopping WebCodecsRecorder");
await recorder.current.stop();
} else if (recorder.current instanceof MediaRecorder) {
+ debug("Stopping MediaRecorder");
try {
recorder.current.requestData();
} catch {}
@@ -546,14 +676,15 @@ const Recorder = () => {
recorder.current.stop();
}
}
- } catch {}
+ } catch (err) {
+ debugError("stopRecording() error while stopping recorder", err);
+ }
await waitForDrain();
recorder.current = null;
- // ❗ Only fully release tracks if the user is NOT restarting
if (!isRestarting.current) {
- console.log("[RECORDER] stopRecording(): fully stopping all tracks");
+ debug("Stopping tracks and clearing streams");
liveStream.current?.getTracks().forEach((t) => t.stop());
helperVideoStream.current?.getTracks().forEach((t) => t.stop());
helperAudioStream.current?.getTracks().forEach((t) => t.stop());
@@ -561,12 +692,11 @@ const Recorder = () => {
liveStream.current = null;
helperVideoStream.current = null;
helperAudioStream.current = null;
- } else {
- console.log("[RECORDER] stopRecording(): preserving streams for restart");
}
}
const dismissRecording = () => {
+ debug("dismissRecording()");
uiClosing.current = true;
isRecording.current = false;
window.close();
@@ -574,38 +704,39 @@ const Recorder = () => {
const restartRecording = async () => {
if (isRestarting.current) {
- console.warn("[RECORDER] duplicate restart ignored");
+ debugWarn("restartRecording() called while already restarting");
return;
}
- console.log("[RECORDER] restartRecording()");
+
+ debug("restartRecording()");
isRestarting.current = true;
isRecording.current = false;
sentLast.current = false;
isFinishing.current = false;
- // Stop encoders, but keep tracks alive
try {
if (
useWebCodecs.current &&
recorder.current instanceof WebCodecsRecorder
) {
+ debug("Cleaning up WebCodecsRecorder for restart");
recorder.current.running = false;
recorder.current.paused = false;
await recorder.current.cleanup();
} else if (recorder.current instanceof MediaRecorder) {
+ debug("Stopping MediaRecorder for restart");
recorder.current.ondataavailable = null;
recorder.current.onstop = null;
recorder.current.onerror = null;
if (recorder.current.state !== "inactive") recorder.current.stop();
}
} catch (err) {
- console.warn("[RECORDER] restart stop error", err);
+ debugError("Error while restarting recorder", err);
}
recorder.current = null;
- // Reset chunks + timestamps
pending.current = [];
pendingBytes.current = 0;
draining.current = false;
@@ -615,17 +746,14 @@ const Recorder = () => {
lastTimecode.current = 0;
await chunksStore.clear();
- // ❌ DO NOT start recording here
- // UI will decide when to start
- console.log("[RECORDER] ready for restart β€” waiting for startRecording()");
-
- // Ensure next start uses WebCodecs cleanly
useWebCodecs.current = false;
isRestarting.current = false;
+ debug("restartRecording() done, ready to start again");
};
async function startAudioStream(id) {
+ debug("startAudioStream()", { id });
const audioStreamOptions = {
mimeType: "video/webm;codecs=vp8,opus",
audio: {
@@ -638,10 +766,16 @@ const Recorder = () => {
const result = await navigator.mediaDevices
.getUserMedia(audioStreamOptions)
.then((stream) => {
+ debug("startAudioStream() got stream with exact device", {
+ hasAudio: stream.getAudioTracks().length,
+ });
return stream;
})
.catch((err) => {
- // Try again without the device ID
+ debugWarn(
+ "startAudioStream() exact device failed, retrying generic",
+ err
+ );
const audioStreamOptions = {
mimeType: "video/webm;codecs=vp8,opus",
audio: true,
@@ -650,52 +784,69 @@ const Recorder = () => {
return navigator.mediaDevices
.getUserMedia(audioStreamOptions)
.then((stream) => {
+ debug("startAudioStream() got generic stream", {
+ hasAudio: stream.getAudioTracks().length,
+ });
return stream;
})
- .catch((err) => {
+ .catch((err2) => {
+ debugError("startAudioStream() failed completely", err2);
return null;
});
});
return result;
}
- // Set audio input volume
+
function setAudioInputVolume(volume) {
+ if (!audioInputGain.current) {
+ debugWarn("setAudioInputVolume() called but audioInputGain is null");
+ return;
+ }
+ debug("setAudioInputVolume()", volume);
audioInputGain.current.gain.value = volume;
}
- // Set audio output volume
function setAudioOutputVolume(volume) {
+ if (!audioOutputGain.current) {
+ debugWarn("setAudioOutputVolume() called but audioOutputGain is null");
+ return;
+ }
+ debug("setAudioOutputVolume()", volume);
audioOutputGain.current.gain.value = volume;
}
const setMic = async (result) => {
+ debug("setMic()", result);
if (helperAudioStream.current != null) {
if (result.active) {
setAudioInputVolume(1);
} else {
setAudioInputVolume(0);
}
- } else {
- // No microphone available
}
};
async function startStream(data, id, options, permissions, permissions2) {
- // Get quality value
- const { qualityValue } = await chrome.storage.local.get(["qualityValue"]);
+ debug("startStream()", {
+ recordingType: data.recordingType,
+ id,
+ isTab: isTab.current,
+ options,
+ permissions: permissions?.state,
+ micPermissions: permissions2?.state,
+ });
+ const { qualityValue } = await chrome.storage.local.get(["qualityValue"]);
const { width, height } = getResolutionForQuality(qualityValue);
const { fpsValue } = await chrome.storage.local.get(["fpsValue"]);
let fps = parseInt(fpsValue);
- // Check if fps is a number
if (isNaN(fps)) {
fps = 30;
}
- // Check if the user selected a tab in desktopcapture
let userConstraints = {
audio: {
deviceId: data.defaultAudioInput,
@@ -720,16 +871,26 @@ const Recorder = () => {
userConstraints.audio = false;
}
+ debug("User media constraints", {
+ userConstraints,
+ qualityValue,
+ fps,
+ });
+
let userStream;
if (
permissions.state != "denied" &&
permissions2.state != "denied" &&
data.recordingType === "camera"
) {
+ debug("Requesting camera userStream");
userStream = await navigator.mediaDevices.getUserMedia(userConstraints);
+ debug("Camera userStream acquired", {
+ videoTracks: userStream.getVideoTracks().length,
+ audioTracks: userStream.getAudioTracks().length,
+ });
}
- // Save the helper streams
if (data.recordingType === "camera") {
helperVideoStream.current = userStream;
} else {
@@ -744,45 +905,52 @@ const Recorder = () => {
mandatory: {
chromeMediaSource: isTab.current ? "tab" : "desktop",
chromeMediaSourceId: id,
- // FLAG: try ideal + max to enforce exact resolution
- // maxWidth: width,
- // maxHeight: height,
- // width: { ideal: width, max: width },
- // height: { ideal: height, max: height },
+ maxWidth: width,
+ maxHeight: height,
+ width: { ideal: width, max: width },
+ height: { ideal: height, max: height },
maxFrameRate: fps,
},
},
};
+ debug("desktopCapture getUserMedia constraints", constraints);
+
let stream;
try {
stream = await navigator.mediaDevices.getUserMedia(constraints);
- // Check if the stream actually has data in it
if (stream.getVideoTracks().length === 0) {
+ debugError("No video tracks returned from getUserMedia");
sendRecordingError("No video tracks available");
return;
}
} catch (err) {
+ debugError("Failed to get user media for desktop/tab capture", err);
sendRecordingError("Failed to get user media: " + JSON.stringify(err));
return;
}
+ debug("desktop/tab stream acquired", {
+ videoTracks: stream.getVideoTracks().length,
+ audioTracks: stream.getAudioTracks().length,
+ });
+
if (isTab.current) {
- // Continue to play the captured audio to the user.
const output = new AudioContext();
const source = output.createMediaStreamSource(stream);
source.connect(output.destination);
+ debug("Created playback AudioContext for tab preview");
}
helperVideoStream.current = stream;
const surface = stream.getVideoTracks()[0].getSettings().displaySurface;
+ debug("Display surface", surface);
chrome.runtime.sendMessage({ type: "set-surface", surface: surface });
}
- // Create an audio context, destination, and stream
aCtx.current = new AudioContext();
destination.current = aCtx.current.createMediaStreamDestination();
liveStream.current = new MediaStream();
@@ -790,49 +958,45 @@ const Recorder = () => {
const micstream = await startAudioStream(data.defaultAudioInput);
helperAudioStream.current = micstream;
- // Check if micstream has an audio track
- if (
- helperAudioStream.current != null &&
- helperAudioStream.current.getAudioTracks().length > 0
- ) {
- audioInputGain.current = aCtx.current.createGain();
- audioInputSource.current = aCtx.current.createMediaStreamSource(
- helperAudioStream.current
- );
- audioInputSource.current
- .connect(audioInputGain.current)
- .connect(destination.current);
- } else {
- // No microphone available
- }
-
if (helperAudioStream.current != null && !data.micActive) {
setAudioInputVolume(0);
}
- // Check if stream has an audio track
- if (helperVideoStream.current.getAudioTracks().length > 0) {
+ // System/Tab audio
+ const sysTracks = helperVideoStream.current.getAudioTracks();
+ debug("System/tab audio tracks", sysTracks.length);
+ if (sysTracks.length > 0) {
+ const sysSource = aCtx.current.createMediaStreamSource(
+ new MediaStream([sysTracks[0]])
+ );
audioOutputGain.current = aCtx.current.createGain();
- audioOutputSource.current = aCtx.current.createMediaStreamSource(
- helperVideoStream.current
+ sysSource.connect(audioOutputGain.current).connect(destination.current);
+ }
+
+ // Mic audio
+ const micTracks = helperAudioStream.current?.getAudioTracks() ?? [];
+ debug("Mic audio tracks", micTracks.length);
+ if (micTracks.length > 0) {
+ const micSource = aCtx.current.createMediaStreamSource(
+ new MediaStream([micTracks[0]])
);
- audioOutputSource.current
- .connect(audioOutputGain.current)
- .connect(destination.current);
- } else {
- // No system audio available
+ audioInputGain.current = aCtx.current.createGain();
+ micSource.connect(audioInputGain.current).connect(destination.current);
+
+ if (!data.micActive) {
+ audioInputGain.current.gain.value = 0;
+ }
}
- // Add the tracks to the stream
liveStream.current.addTrack(helperVideoStream.current.getVideoTracks()[0]);
const mainVideoTrack = liveStream.current?.getVideoTracks()[0];
if (mainVideoTrack) {
mainVideoTrack.onmute = () => {
- console.warn("[RECORDER] video track muted β€” possible display sleep");
+ debugWarn("mainVideoTrack muted");
};
mainVideoTrack.oninactive = () => {
- console.warn("[RECORDER] video track inactive β€” auto-stop");
+ debugWarn("mainVideoTrack inactive β†’ stopping recording");
stopRecording();
};
}
@@ -846,17 +1010,20 @@ const Recorder = () => {
);
}
- // Send message to go back to the previously active tab
+ debug("liveStream ready", {
+ videoTracks: liveStream.current.getVideoTracks().length,
+ audioTracks: liveStream.current.getAudioTracks().length,
+ });
+
setStarted(true);
- // after helperVideoStream & helperAudioStream are resolved:
await warmUpStream(liveStream.current);
chrome.runtime.sendMessage({ type: "reset-active-tab" });
}
async function startStreaming(data) {
- // Check user permissions for camera and microphone individually
+ debug("startStreaming()", { data });
const permissions = await navigator.permissions.query({
name: "camera",
});
@@ -864,23 +1031,35 @@ const Recorder = () => {
name: "microphone",
});
+ debug("Permissions", {
+ camera: permissions.state,
+ microphone: permissions2.state,
+ });
+
try {
if (data.recordingType === "camera") {
+ debug("Streaming camera recording");
startStream(data, null, null, permissions, permissions2);
} else if (!isTab.current) {
let captureTypes = ["screen", "window", "tab", "audio"];
if (tabPreferred.current) {
captureTypes = ["tab", "screen", "window", "audio"];
}
+ debug("desktopCapture.chooseDesktopMedia", {
+ captureTypes,
+ tabPreferred: tabPreferred.current,
+ });
chrome.desktopCapture.chooseDesktopMedia(
captureTypes,
null,
(streamId, options) => {
+ debug("chooseDesktopMedia callback", { streamId, options });
if (
streamId === undefined ||
streamId === null ||
streamId === ""
) {
+ debugWarn("User cancelled the desktop capture modal");
sendRecordingError("User cancelled the modal", true);
return;
} else {
@@ -889,9 +1068,11 @@ const Recorder = () => {
}
);
} else {
+ debug("Streaming with pre-resolved tabID", tabID.current);
startStream(data, tabID.current, null, permissions, permissions2);
}
} catch (err) {
+ debugError("startStreaming() error", err);
sendRecordingError(
"Failed to start streaming: " + JSON.stringify(err),
true
@@ -899,47 +1080,51 @@ const Recorder = () => {
}
}
- // Check if trying to record from Playground
useEffect(() => {
chrome.storage.local.get(["tabPreferred"], (result) => {
tabPreferred.current = result.tabPreferred;
+ debug("Loaded tabPreferred", tabPreferred.current);
});
}, []);
const getStreamID = async (id) => {
+ debug("getStreamID()", id);
const streamId = await chrome.tabCapture.getMediaStreamId({
targetTabId: id,
});
+ debug("Resolved tabCapture streamId", streamId);
tabID.current = streamId;
};
- // FLAG: add protection on page close
useEffect(() => {
const handleClose = (e) => {
- // OK to close if:
+ debug("beforeunload event", {
+ uiClosing: uiClosing.current,
+ isRecording: isRecording.current,
+ });
if (uiClosing.current || !isRecording.current) return;
- console.warn("[RECORDER] Prevented accidental tab close!");
-
- // Stop everything immediately but prevent loss
stopRecording();
- // Show native "are you sure?" dialog to prevent closing
e.preventDefault();
e.returnValue = "";
};
window.addEventListener("beforeunload", handleClose);
return () => {
+ debug("Removing beforeunload handler");
window.removeEventListener("beforeunload", handleClose);
};
}, []);
const onMessage = useCallback(
(request, sender, sendResponse) => {
+ if (DEBUG_RECORDER) {
+ debug("onMessage()", request.type, { request, sender });
+ }
+
if (request.type === "loaded") {
backupRef.current = request.backup;
- // FLAG: I don't know why this was a false check before...
if (!tabPreferred.current) {
isTab.current = request.isTab;
if (request.isTab) {
@@ -956,8 +1141,6 @@ const Recorder = () => {
} else if (request.type === "restart-recording-tab") {
if (!isRestarting.current) {
restartRecording();
- } else {
- console.warn("[RECORDER] ignoring duplicate restart request");
}
} else if (request.type === "stop-recording-tab") {
stopRecording();
@@ -971,8 +1154,10 @@ const Recorder = () => {
useWebCodecs.current &&
recorder.current instanceof WebCodecsRecorder
) {
+ debug("Pausing WebCodecsRecorder");
recorder.current.pause();
} else if (recorder.current instanceof MediaRecorder) {
+ debug("Pausing MediaRecorder");
recorder.current.pause();
}
} else if (request.type === "resume-recording-tab") {
@@ -981,8 +1166,10 @@ const Recorder = () => {
useWebCodecs.current &&
recorder.current instanceof WebCodecsRecorder
) {
+ debug("Resuming WebCodecsRecorder");
recorder.current.resume();
} else if (recorder.current instanceof MediaRecorder) {
+ debug("Resuming MediaRecorder");
recorder.current.resume();
}
} else if (request.type === "dismiss-recording") {
@@ -993,13 +1180,14 @@ const Recorder = () => {
);
useEffect(() => {
- // Event listener (extension messaging)
+ debug("Adding chrome.runtime.onMessage listener");
chrome.runtime.onMessage.addListener(onMessage);
return () => {
+ debug("Removing chrome.runtime.onMessage listener");
chrome.runtime.onMessage.removeListener(onMessage);
};
- }, []);
+ }, [onMessage]);
return <RecorderUI started={started} isTab={isTab.current} />;
};
diff --git a/src/pages/Recorder/useUnifiedRecorder.js b/src/pages/Recorder/useUnifiedRecorder.js
deleted file mode 100644
index b3f5401..0000000
--- a/src/pages/Recorder/useUnifiedRecorder.js
+++ /dev/null
@@ -1,305 +0,0 @@
-/*!
- * Screenity Unified Recorder Backend (WebCodecs + Fallback)
- * Copyright (c) 2025 Serial Labs Ltd.
- *
- * This file is part of Screenity and is licensed under the GNU GPLv3.
- * See the LICENSE file in the project root for details.
- *
- * This module unifies recorder logic for:
- * - Main recorder (popup/panel)
- * - Region recorder (iframe capture)
- * - Tab capture and full desktop capture
- *
- * WebCodecs primary path:
- * - Better quality
- * - Lower latency + faster finalization
- * - Avoids huge memory spikes during long recordings
- *
- * If reused outside Screenity, GPLv3 obligations apply.
- */
-
-// Imports shared through your monorepo structure
-import localforage from "localforage";
-import { WebCodecsRecorder } from "./webcodecs/WebCodecsRecorder";
-import { sendRecordingError, sendStopRecording } from "../messaging";
-import { getBitrates } from "../recorderConfig";
-import { getResolutionForQuality } from "../recorderConfig";
-
-// === Persistent storage for chunks (indexedDB) ===
-const chunksStore = localforage.createInstance({
- name: "chunks",
-});
-
-// === Constants ===
-const ESTIMATE_INTERVAL_MS = 5000;
-const MIN_HEADROOM = 25 * 1024 * 1024; // ~25MB
-const MAX_PENDING_BYTES = 8 * 1024 * 1024;
-
-// === Unified Recorder Factory ===
-export function useUnifiedRecorder() {
- const recorderRef = {
- recorder: null,
- useWebCodecs: false,
- running: false,
- finishing: false,
- restarting: false,
- savedCount: 0,
- index: 0,
- pendingBytes: 0,
- pendingQueue: [],
- draining: false,
- lastEstimateAt: 0,
- lowStorageAbort: false,
- };
-
- const estimateStorage = async (size = 0) => {
- const now = performance.now();
- if (now - recorderRef.lastEstimateAt < ESTIMATE_INTERVAL_MS) {
- return !recorderRef.lowStorageAbort;
- }
- recorderRef.lastEstimateAt = now;
-
- try {
- const { usage = 0, quota = 0 } = await navigator.storage.estimate();
- const remaining = quota - usage;
- return remaining > MIN_HEADROOM + size;
- } catch {
- return !recorderRef.lowStorageAbort;
- }
- };
-
- const saveChunkDirect = async (blob, ts) => {
- try {
- await chunksStore.setItem(`chunk_${recorderRef.index}`, {
- index: recorderRef.index,
- chunk: blob,
- timestamp: ts,
- });
- recorderRef.index++;
- recorderRef.savedCount++;
- return true;
- } catch (err) {
- console.error("[UnifiedRecorder] saveChunkDirect failed", err);
- return false;
- }
- };
-
- const handleChunk = async (blob, timestampMs) => {
- const allowed = await estimateStorage(blob.size);
- if (!allowed) {
- abortForLowStorage();
- return;
- }
- await saveChunkDirect(blob, timestampMs ?? 0);
- };
-
- function abortForLowStorage() {
- recorderRef.lowStorageAbort = true;
- chrome.storage.local.set({
- recording: false,
- restarting: false,
- memoryError: true,
- });
- chrome.runtime.sendMessage({ type: "stop-recording-tab" });
- }
-
- async function warmUpStream(liveStream) {
- const videoTrack = liveStream.getVideoTracks()[0];
- const audioTrack = liveStream.getAudioTracks()[0];
-
- if (!videoTrack) throw new Error("No video track present for warm-up");
-
- // ensure at least one real frame exists
- await new Promise(async (resolve) => {
- const proc = new MediaStreamTrackProcessor({ track: videoTrack });
- const reader = proc.readable.getReader();
-
- while (true) {
- const { value: frame } = await reader.read();
- if (frame && frame.codedWidth > 0) {
- frame.close();
- reader.releaseLock();
- resolve();
- break;
- }
- frame?.close();
- }
- });
-
- if (audioTrack) {
- await new Promise(async (resolve) => {
- const proc = new MediaStreamTrackProcessor({ track: audioTrack });
- const reader = proc.readable.getReader();
-
- while (true) {
- const { value: audio } = await reader.read();
- if (audio && audio.numberOfFrames > 0) {
- audio.close?.();
- reader.releaseLock();
- resolve();
- break;
- }
- audio?.close?.();
- }
- });
- }
-
- console.log("[UnifiedRecorder] warm-up complete");
- }
-
- async function start(liveStream) {
- if (!liveStream || recorderRef.running) return false;
-
- recorderRef.running = true;
- recorderRef.restart = false;
- recorderRef.index = 0;
- recorderRef.savedCount = 0;
- recorderRef.pendingBytes = 0;
-
- await chunksStore.clear();
-
- const { qualityValue } = await chrome.storage.local.get(["qualityValue"]);
- const { audioBitsPerSecond, videoBitsPerSecond } =
- getBitrates(qualityValue);
-
- const vidTrack = liveStream.getVideoTracks()[0];
- if (!vidTrack) {
- sendRecordingError("No video track");
- return false;
- }
-
- const settings = vidTrack.getSettings();
- const width = settings.width ?? 1920;
- const height = settings.height ?? 1080;
-
- const { fpsValue } = await chrome.storage.local.get(["fpsValue"]);
- let fps = parseInt(fpsValue);
- if (Number.isNaN(fps)) fps = 30;
-
- const useWC =
- typeof VideoEncoder !== "undefined" &&
- typeof AudioEncoder !== "undefined" &&
- typeof MediaStreamTrackProcessor !== "undefined";
-
- // Warm up tracks β€” ensures first frames exist
- await warmUpStream(liveStream);
-
- if (useWC) {
- recorderRef.useWebCodecs = true;
- recorderRef.recorder = new WebCodecsRecorder(liveStream, {
- width,
- height,
- fps,
- videoBitrate: videoBitsPerSecond,
- audioBitrate: audioBitsPerSecond,
- onChunk: async (chunk, timestampUs) => {
- const ts = timestampUs ? Math.floor(timestampUs / 1000) : 0;
- const blob = new Blob([chunk], { type: "video/mp4" });
- await handleChunk(blob, ts);
- },
- onFinalized: () => {
- chrome.runtime.sendMessage({ type: "video-ready" });
- },
- onError: (err) => {
- console.error("[UnifiedRecorder][WC] error", err);
- sendRecordingError(String(err));
- },
- });
-
- const ok = await recorderRef.recorder.start();
- if (!ok) {
- console.warn("[UnifiedRecorder] WC failed β€” fallback");
- recorderRef.useWebCodecs = false;
- recorderRef.recorder = null;
- return await start(liveStream);
- }
- } else {
- recorderRef.useWebCodecs = false;
- const mime = "video/webm;codecs=vp8,opus";
- const mr = new MediaRecorder(liveStream, {
- mimeType: mime,
- audioBitsPerSecond,
- videoBitsPerSecond,
- });
-
- mr.ondataavailable = async (e) => {
- if (!e.data || !e.data.size) return;
- await handleChunk(e.data, e.timecode ?? 0);
- };
-
- mr.onerror = (ev) => {
- sendRecordingError("MediaRecorder error");
- };
-
- mr.onstop = async () => {
- chrome.runtime.sendMessage({ type: "video-ready" });
- };
-
- recorderRef.recorder = mr;
- mr.start(1000);
- }
-
- chrome.storage.local.set({ recording: true, restarting: false });
- return true;
- }
-
- async function stop(preserveStreams = false) {
- if (!recorderRef.running) return;
- recorderRef.running = false;
-
- try {
- if (recorderRef.useWebCodecs) {
- await recorderRef.recorder.stop();
- } else {
- recorderRef.recorder.stop();
- }
- } catch {}
-
- recorderRef.recorder = null;
- chrome.storage.local.set({ recording: false });
- if (!preserveStreams) {
- sendStopRecording();
- }
- }
-
- function pause() {
- if (!recorderRef.running) return;
- try {
- recorderRef.recorder.pause();
- } catch {}
- }
-
- function resume() {
- if (!recorderRef.running) return;
- try {
- recorderRef.recorder.resume();
- } catch {}
- }
-
- async function restart() {
- recorderRef.restarting = true;
- recorderRef.running = false;
- recorderRef.finishing = false;
-
- if (recorderRef.useWebCodecs) {
- await recorderRef.recorder.cleanup();
- } else {
- try {
- recorderRef.recorder.stop();
- } catch {}
- }
-
- recorderRef.recorder = null;
- recorderRef.useWebCodecs = false;
-
- await chunksStore.clear();
- recorderRef.index = 0;
- recorderRef.savedCount = 0;
- recorderRef.pendingQueue = [];
- recorderRef.pendingBytes = 0;
- recorderRef.restarting = false;
- chrome.storage.local.set({ restarting: false });
- }
-
- return { start, stop, pause, resume, restart };
-}
diff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts
index 036124d..b115461 100644
--- a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts
+++ b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts
@@ -21,53 +21,34 @@ import {
EncodedPacket,
} from "mediabunny";
-interface Mp4MuxerWrapperOptions {
- width: number;
- height: number;
- fps: number;
- videoBitrate: number;
- audioBitrate: number;
- videoCodec?: string;
- audioCodec?: string;
- onChunk: (
- chunk: Uint8Array,
- timestampUs: number | null
- ) => void | Promise<void>;
-}
-
export class Mp4MuxerWrapper {
- private options: Mp4MuxerWrapperOptions;
-
- private output: Output;
- private target: BufferTarget;
-
- private videoSource: EncodedVideoPacketSource;
- private audioSource: EncodedAudioPacketSource | null = null;
- private videoCodec: string;
- private audioCodec: string;
-
- private ftypChunk: Uint8Array | null = null;
- private moovChunk: Uint8Array | null = null;
- private headerEmitted = false;
-
- private pendingMoof: Uint8Array | null = null;
- private pendingMoofTimestampSeconds: number | null = null;
- private fragmentTimestampOffsetSeconds: number | null = null;
- private videoTimestampOffsetUs: number | null = null;
- private audioTimestampOffsetUs: number | null = null;
- private lastVideoTimestampUs = 0;
- private lastAudioTimestampUs = 0;
- private pausedOffsetUs: number;
-
- private started = false;
-
- constructor(options: Mp4MuxerWrapperOptions) {
+ constructor(options) {
this.options = options;
+ // πŸ”Ή Unified debug toggle + wrappers
+ this.debug = options.debug ?? false;
+ this.log = (...args) => this.debug && console.log(...args);
+ this.warn = (...args) => this.debug && console.warn(...args);
+ this.err = (...args) => this.debug && console.error(...args);
+
this.target = new BufferTarget();
this.videoCodec = options.videoCodec || "avc";
this.audioCodec = options.audioCodec || "aac";
-this.pausedOffsetUs = 0;
+ this.pausedOffsetUs = 0;
+
+ this.ftypChunk = null;
+ this.moovChunk = null;
+ this.headerEmitted = false;
+
+ this.pendingMoof = null;
+ this.pendingMoofTimestampSeconds = null;
+ this.fragmentTimestampOffsetSeconds = null;
+ this.videoTimestampOffsetUs = null;
+ this.audioTimestampOffsetUs = null;
+ this.lastVideoTimestampUs = 0;
+ this.lastAudioTimestampUs = 0;
+
+ this.started = false;
const minFragmentDuration = Math.max(
0.2,
@@ -88,23 +69,20 @@ this.pausedOffsetUs = 0;
},
onMoof: (data, _pos, timestampSeconds = 0) => {
this.pendingMoof = this.cloneChunk(data);
- this.pendingMoofTimestampSeconds = this.normalizeFragmentTimestamp(
- timestampSeconds
- );
+ this.pendingMoofTimestampSeconds =
+ this.normalizeFragmentTimestamp(timestampSeconds);
},
onMdat: (data) => {
- if (!this.pendingMoof) {
- return;
- }
+ if (!this.pendingMoof) return;
const fragment = this.concatChunks([
this.pendingMoof,
this.cloneChunk(data),
]);
- const timestampUs =
+ const tsUs =
this.pendingMoofTimestampSeconds != null
? Math.round(this.pendingMoofTimestampSeconds * 1e6)
: null;
- this.emitChunk(fragment, timestampUs);
+ this.emitChunk(fragment, tsUs);
this.pendingMoof = null;
this.pendingMoofTimestampSeconds = null;
},
@@ -125,14 +103,14 @@ this.pausedOffsetUs = 0;
}
}
- addVideoChunk(chunk: EncodedVideoChunk, meta: any) {
+ addVideoChunk(chunk, meta) {
const packet = this.buildPacketFromChunk(chunk, meta, "video");
return this.videoSource.add(packet, meta);
}
- addAudioChunk(chunk: EncodedAudioChunk, meta: any) {
+ addAudioChunk(chunk, meta) {
if (!this.audioSource) {
- console.warn("[MUXER] audioSource missing β€” ignoring audio packet");
+ this.warn("[MUXER] audioSource missing β€” ignoring packet");
return;
}
const packet = this.buildPacketFromChunk(chunk, meta, "audio");
@@ -146,131 +124,120 @@ this.pausedOffsetUs = 0;
}
async finalize() {
- await this.output.finalize();
+ this.log?.("[MUXER] finalize() called");
- const data = this.target.buffer;
- if (data && data.byteLength > 0) {
- this.emitChunk(new Uint8Array(data), null);
- }
- }
+ try {
+ await this.output.finalize();
+ } catch (err) {
+ this.err?.("[MUXER] finalize internal error:", err);
+ }
- setPausedOffset(offsetUs: number) {
- this.pausedOffsetUs = offsetUs;
+ // IMPORTANT: Do NOT read this.target.buffer here
+ // It may be gigabytes for long recordings and crash memory.
+ this.log?.("[MUXER] finalize completed (streaming mode, no buffer concat)");
}
- setAudioOffset(offsetUs: number) {
- this.audioTimestampOffsetUs = offsetUs;
- }
+ setPausedOffset(offsetUs) {
+ this.pausedOffsetUs = offsetUs;
+ }
- private emitHeaderIfReady() {
- if (this.headerEmitted || !this.ftypChunk || !this.moovChunk) {
- return;
- }
+ setAudioOffset(offsetUs) {
+ this.audioTimestampOffsetUs = offsetUs;
+ }
+
+ emitHeaderIfReady() {
+ if (this.headerEmitted || !this.ftypChunk || !this.moovChunk) return;
const header = this.concatChunks([this.ftypChunk, this.moovChunk]);
this.headerEmitted = true;
this.emitChunk(header, 0);
}
- private emitChunk(chunk: Uint8Array, timestampUs: number | null) {
+ emitChunk(chunk, timestampUs) {
try {
- const result = this.options.onChunk?.(chunk, timestampUs ?? null);
- if (result && typeof (result as Promise<unknown>).then === "function") {
- Promise.resolve(result).catch((err) => {
- console.error("[MUXER] onChunk rejected", err);
- });
+ const r = this.options.onChunk?.(chunk, timestampUs ?? null);
+ if (r && typeof r.then === "function") {
+ r.catch((err) => this.err("[MUXER] onChunk rejected:", err));
}
} catch (err) {
- console.error("[MUXER] onChunk error", err);
+ this.err("[MUXER] onChunk error:", err);
}
}
- private cloneChunk(data: Uint8Array) {
+ cloneChunk(data) {
return new Uint8Array(data);
}
- private concatChunks(chunks: Uint8Array[]) {
- const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
+ concatChunks(chunks) {
+ const total = chunks.reduce((sum, c) => sum + c.byteLength, 0);
const combined = new Uint8Array(total);
- let offset = 0;
- for (const chunk of chunks) {
- combined.set(chunk, offset);
- offset += chunk.byteLength;
+ let off = 0;
+ for (const c of chunks) {
+ combined.set(c, off);
+ off += c.byteLength;
}
return combined;
}
- private buildPacketFromChunk(
- chunk: EncodedVideoChunk | EncodedAudioChunk,
- meta: any,
- kind: "video" | "audio"
- ) {
+ buildPacketFromChunk(chunk, meta, kind) {
const data = new Uint8Array(chunk.byteLength);
chunk.copyTo(data);
+
const relativeTimestampUs = this.normalizeSampleTimestamp(
kind,
chunk.timestamp,
chunk.duration || 0
);
- const durationSeconds = (chunk.duration || 0) / 1e6;
return new EncodedPacket(
data,
chunk.type,
relativeTimestampUs / 1e6,
- durationSeconds
+ (chunk.duration || 0) / 1e6
);
}
-private normalizeSampleTimestamp(
- kind: "video" | "audio",
- timestampUs?: number,
- durationUs: number = 0
-) {
- const offsetKey =
- kind === "video" ? "videoTimestampOffsetUs" : "audioTimestampOffsetUs";
- const lastKey =
- kind === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";
-
- // Fallback when encoder doesn’t give timestamps
- if (typeof timestampUs !== "number") {
- const prev = (this as any)[lastKey] ?? 0;
- const next = prev + durationUs;
- (this as any)[lastKey] = next;
- return next;
- }
- let baseOffset = (this as any)[offsetKey] as number | null;
+ normalizeSampleTimestamp(kind, timestampUs, durationUs = 0) {
+ const offsetKey =
+ kind === "video" ? "videoTimestampOffsetUs" : "audioTimestampOffsetUs";
+ const lastKey =
+ kind === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";
+
+ if (typeof timestampUs !== "number") {
+ const prev = this[lastKey] ?? 0;
+ const next = prev + durationUs;
+ this[lastKey] = next;
+ return next;
+ }
- // If we explicitly set an audio offset, prefer that
- if (kind === "audio") {
- if (this.audioTimestampOffsetUs == null) {
- this.audioTimestampOffsetUs = timestampUs;
- }
- baseOffset = this.audioTimestampOffsetUs;
- } else if (baseOffset == null) {
- // First sample for this track: establish base offset
- baseOffset = timestampUs;
- (this as any)[offsetKey] = baseOffset;
- }
+ let base = this[offsetKey];
+
+ if (kind === "audio") {
+ if (this.audioTimestampOffsetUs == null) {
+ this.audioTimestampOffsetUs = timestampUs;
+ }
+ base = this.audioTimestampOffsetUs;
+ } else if (base == null) {
+ base = timestampUs;
+ this[offsetKey] = base;
+ }
- const relative = Math.max(
- 0,
- timestampUs - baseOffset - this.pausedOffsetUs
- );
+ const relative = Math.max(
+ 0,
+ timestampUs - base - this.pausedOffsetUs
+ );
- (this as any)[lastKey] = relative;
- return relative;
-}
+ this[lastKey] = relative;
+ return relative;
+ }
- private normalizeFragmentTimestamp(timestampSeconds: number | null | undefined) {
- if (timestampSeconds == null) {
- return null;
- }
+ normalizeFragmentTimestamp(tsSeconds) {
+ if (tsSeconds == null) return null;
if (this.fragmentTimestampOffsetSeconds == null) {
- this.fragmentTimestampOffsetSeconds = timestampSeconds;
+ this.fragmentTimestampOffsetSeconds = tsSeconds;
}
return Math.max(
0,
- timestampSeconds - this.fragmentTimestampOffsetSeconds
+ tsSeconds - this.fragmentTimestampOffsetSeconds
);
}
-}
+}
\ No newline at end of file
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
index b6e129c..cd5b7b2 100644
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
@@ -17,6 +17,11 @@ export class WebCodecsRecorder {
this.stream = stream;
this.options = options;
+ this.debug = options.debug ?? false;
+ this.log = (...args) => this.debug && console.log(...args);
+ this.warn = (...args) => this.debug && console.warn(...args);
+ this.err = (...args) => this.debug && console.error(...args);
+
this.videoTrack = null;
this.audioTrack = null;
@@ -33,11 +38,9 @@ export class WebCodecsRecorder {
this.running = false;
- // Capture resolution (what Chrome is giving us)
this.actualWidth = 0;
this.actualHeight = 0;
- // Output / encoded resolution (after downscale)
this.targetWidth = 0;
this.targetHeight = 0;
@@ -47,7 +50,6 @@ export class WebCodecsRecorder {
this.firstVideoFrame = undefined;
this.frameCount = 0;
- // Resize canvas for downscaling frames
this.resizeCanvas = null;
this.resizeCtx = null;
@@ -61,6 +63,10 @@ export class WebCodecsRecorder {
this._prebufferedAudio = [];
this._audioReady = false;
+
+ this._stopping = false;
+ this._videoLoopPromise = null;
+ this._audioLoopPromise = null;
}
async _probeRealResolution() {
@@ -73,7 +79,7 @@ export class WebCodecsRecorder {
const width = frame.codedWidth;
const height = frame.codedHeight;
- console.log("[WCR] Probed initial resolution:", width, "x", height);
+ this.log("[WCR] Probed resolution:", width, "x", height);
frame.close();
reader.releaseLock();
@@ -81,9 +87,10 @@ export class WebCodecsRecorder {
}
async start() {
- // Reset offsets
this.videoTimestampOffsetUs = null;
this.firstVideoFrame = undefined;
+ this._stopping = false;
+
if (this.running) return this._startPromise;
this._startPromise = new Promise((resolve) => {
@@ -95,13 +102,13 @@ export class WebCodecsRecorder {
this.videoTrack = this.stream.getVideoTracks()[0] || null;
this.audioTrack = this.stream.getAudioTracks()[0] || null;
- console.log(
+ this.log(
"[WCR] videoTrack",
this.videoTrack,
"state=",
this.videoTrack?.readyState
);
- console.log(
+ this.log(
"[WCR] audioTrack",
this.audioTrack,
"state=",
@@ -123,40 +130,24 @@ export class WebCodecsRecorder {
}
try {
- console.log("[WCR] probing real resolution...");
const { width, height } = await this._probeRealResolution();
this.actualWidth = width;
this.actualHeight = height;
- // --- Compute safe target resolution for H.264 ---
- // 1) clamp width to 1920
let targetWidth = Math.min(width, 1920);
let targetHeight = Math.round((height / width) * targetWidth);
-
- // 2) if height > 1080, clamp height and recompute width
if (targetHeight > 1080) {
targetHeight = 1080;
targetWidth = Math.round((width / height) * targetHeight);
}
-
this.targetWidth = targetWidth;
this.targetHeight = targetHeight;
- console.log(
- "[WCR] Capture resolution:",
- width,
- "x",
- height,
- "β†’ Target:",
- this.targetWidth,
- "x",
- this.targetHeight
- );
+ this.log("[WCR] Target:", this.targetWidth, "x", this.targetHeight);
const fps = this.options.fps || 30;
const safeBitrate = this.options.videoBitrate || 10_000_000;
- // Choose encoder config (REAL check, not just isConfigSupported)
const videoConfig = await this.chooseVideoEncoderConfig({
width: this.targetWidth,
height: this.targetHeight,
@@ -168,40 +159,33 @@ export class WebCodecsRecorder {
const audioConfig = await this.prepareAudioEncoderConfig();
this._pendingAudioConfig = audioConfig;
- console.log("[WCR] Audio config:", audioConfig);
+ this.log("[WCR] Audio config:", audioConfig);
if (!audioConfig) {
- console.warn(
- "[WCR] No audio track available β€” disabling audio in muxer"
- );
+ this.warn("[WCR] No audio available");
this.options.audioBitrate = undefined;
this.options.enableAudio = false;
this._audioReady = false;
}
- console.log("[WCR] creating MediaStreamTrackProcessors...");
this.videoProcessor = new MediaStreamTrackProcessor({
track: this.videoTrack,
});
this.videoReader = this.videoProcessor.readable.getReader();
- // --- CREATE AUDIO PROCESSOR ---
if (audioConfig && this.audioTrack) {
this.audioProcessor = new MediaStreamTrackProcessor({
track: this.audioTrack,
});
this.audioReader = this.audioProcessor.readable.getReader();
- } else {
- this.audioProcessor = null;
- this.audioReader = null;
}
if (this.audioReader && this.options.enableAudio) {
- console.log("[WCR] starting early audio buffering loop");
- this.readAudioLoop();
+ this.log("[WCR] start buffering audio");
+ this._audioLoopPromise = this.readAudioLoop();
}
- console.log("[WCR] creating muxer...");
+ this.log("[WCR] creating muxer...");
this.muxer = new Mp4MuxerWrapper({
width: this.targetWidth,
height: this.targetHeight,
@@ -212,25 +196,20 @@ export class WebCodecsRecorder {
audioCodec: this.options.enableAudio ? "aac" : undefined,
onChunk: this.options.onChunk,
});
- console.log("[WCR] muxer created");
if (this.options.enableAudio) {
this.muxer.enableAudio();
}
await this.muxer.start();
- console.log("[WCR] muxer started");
+ this.log("[WCR] muxer started");
- console.log("[WCR] initVideoEncoder()...");
await this.initVideoEncoder(videoConfig.config, videoConfig.codec);
- console.log("[WCR] initVideoEncoder() done");
-
- console.log("[WCR] starting read loops after sync...");
- await Promise.resolve(); // microtasks flushed
+ await Promise.resolve();
setTimeout(() => {
- this.readVideoLoop();
+ this._videoLoopPromise = this.readVideoLoop();
if (this._startResolve) {
this._startResolve(true);
@@ -238,9 +217,9 @@ export class WebCodecsRecorder {
}
}, 30);
- console.log("[WCR] start() completed");
+ this.log("[WCR] start() done");
} catch (err) {
- console.error("[WCR] start() failed:", err);
+ this.err("[WCR] start() failed:", err);
this.options.onError?.(err);
this.running = false;
this.cleanup();
@@ -251,7 +230,7 @@ export class WebCodecsRecorder {
}
}
} catch (err) {
- console.error("[WCR] start() outer error:", err);
+ this.err("[WCR] start() outer error:", err);
this.options.onError?.(err);
this.running = false;
this.cleanup();
@@ -266,41 +245,61 @@ export class WebCodecsRecorder {
}
async stop() {
- console.log("[WCR] stop() called, frames encoded:", this.frameCount);
+ this.log("[WCR] stop()", this.frameCount);
+
+ if (this._stopping) {
+ this.log("[WCR] stop() already in progress");
+ return;
+ }
+ this._stopping = true;
+
if (!this.running) {
- console.log("[WCR] not running, ignoring stop()");
+ this.log("[WCR] stop() called but recorder not running");
+ this._stopping = false;
return;
}
+
+ // Soft stop: let loops finish current work
this.running = false;
+ this.paused = true;
+
+ try {
+ const loopPromises = [];
+ if (this._videoLoopPromise) loopPromises.push(this._videoLoopPromise);
+ if (this._audioLoopPromise) loopPromises.push(this._audioLoopPromise);
+
+ if (loopPromises.length) {
+ this.log("[WCR] Waiting for read loops to finish before flush...");
+ await Promise.race([
+ Promise.allSettled(loopPromises),
+ new Promise((res) => setTimeout(res, 500)), // safety timeout
+ ]);
+ }
+ } catch (err) {
+ this.err("[WCR] Error while waiting for loops:", err);
+ }
try {
if (this.videoEncoder && this.videoEncoder.state !== "closed") {
- console.log("[WCR] flushing video encoder...");
await this.videoEncoder.flush();
}
if (this.audioEncoder && this.audioEncoder.state !== "closed") {
- console.log("[WCR] flushing audio encoder...");
await this.audioEncoder.flush();
}
} catch (err) {
- console.error("[WCR] flush error:", err);
+ this.err("[WCR] flush error:", err);
this.options.onError?.(err);
}
try {
if (this.muxer) {
- console.log("[WCR] finalizing muxer...");
await this.muxer.finalize();
- console.log("[WCR] muxer finalized");
-
if (this.options.onFinalized) {
await this.options.onFinalized();
}
- } else {
- console.log("[WCR] no muxer to finalize");
}
} catch (err) {
- console.error("[WCR] muxer.finalize error:", err);
+ this.err("[WCR] muxer.finalize:", err);
this.options.onError?.(err);
}
@@ -309,12 +308,14 @@ export class WebCodecsRecorder {
if (this.options.onStop) {
await this.options.onStop();
}
+
+ this._stopping = false;
}
pause() {
if (!this.running || this.paused) return;
this.paused = true;
- this.pauseStartUs = performance.now() * 1000; // Β΅s
+ this.pauseStartUs = performance.now() * 1000;
}
resume() {
@@ -325,13 +326,12 @@ export class WebCodecsRecorder {
this.paused = false;
this.justResumed = true;
-
- // 🟒 CRUCIAL LINE
this.muxer?.setPausedOffset(this.totalPausedDurationUs);
}
cleanup() {
- console.log("[WCR] cleanup()");
+ this.log("[WCR] cleanup");
+
try {
this.videoReader?.releaseLock();
} catch {}
@@ -346,18 +346,12 @@ export class WebCodecsRecorder {
this.audioEncoder?.close();
} catch {}
- // ❌ DO NOT stop the shared capture tracks here.
- // The owner (Recorder) decides when to stop/release them.
- // try { this.videoTrack?.stop(); } catch {}
- // try { this.audioTrack?.stop(); } catch {}
-
this.videoProcessor = null;
this.audioProcessor = null;
this.videoReader = null;
this.audioReader = null;
this.videoEncoder = null;
this.audioEncoder = null;
-
this.videoTrack = null;
this.audioTrack = null;
@@ -368,29 +362,26 @@ export class WebCodecsRecorder {
this.resizeCanvas = null;
this.resizeCtx = null;
-
this._startPromise = null;
this._startResolve = null;
+
+ this._videoLoopPromise = null;
+ this._audioLoopPromise = null;
}
async initVideoEncoder(config, codecLabel) {
- console.log("[WCR] initVideoEncoder()", codecLabel, config);
+ this.log("[WCR] initVideoEncoder()", codecLabel);
this.videoEncoder = new VideoEncoder({
output: (chunk, meta = {}) => {
- console.log("[WCR] VIDEO CHUNK ENCODED", {
- type: chunk.type,
- timestamp: chunk.timestamp,
- duration: chunk.duration,
- byteLength: chunk.byteLength,
- });
-
+ if (this.debug) {
+ this.log("[WCR] VIDEO", chunk.type, chunk.timestamp, chunk.duration);
+ }
const normalizedMeta = { ...meta };
const decoderConfig = normalizedMeta.decoderConfig
? { ...normalizedMeta.decoderConfig }
: {};
- // Use encoded resolution for decoder metadata
decoderConfig.codec = decoderConfig.codec || codecLabel;
decoderConfig.codedWidth = decoderConfig.codedWidth || this.targetWidth;
decoderConfig.codedHeight =
@@ -400,13 +391,13 @@ export class WebCodecsRecorder {
this.muxer.addVideoChunk(chunk, normalizedMeta);
},
error: (err) => {
- console.error("[WCR] VideoEncoder error:", err);
+ this.err("[WCR] VideoEncoder error:", err);
this.options.onError?.(err);
},
});
this.videoEncoder.configure(config);
- console.log("[WCR] VideoEncoder configured successfully");
+ this.log("[WCR] VideoEncoder configured");
}
async initAudioEncoder(config) {
@@ -414,24 +405,20 @@ export class WebCodecsRecorder {
this.audioEncoder = new AudioEncoder({
output: (chunk, meta) => {
- console.log("[WCR] AUDIO CHUNK", chunk, meta);
-
+ if (this.debug) this.log("[WCR] AUDIO");
this.muxer.addAudioChunk(chunk, meta);
},
error: (err) => {
- console.error("[WCR] AudioEncoder error:", err);
+ this.err("[WCR] AudioEncoder error:", err);
this.options.onError?.(err);
},
});
this.audioEncoder.configure(config);
- // πŸ”₯ align audio base timestamp to video base timestamp
if (this.muxer && this.videoTimestampOffsetUs != null) {
this.muxer.setAudioOffset(this.videoTimestampOffsetUs);
}
-
- console.log("[WCR] audio encoder OK");
}
async prepareAudioEncoderConfig() {
@@ -451,12 +438,12 @@ export class WebCodecsRecorder {
try {
const support = await AudioEncoder.isConfigSupported(candidateConfig);
if (!support.supported) {
- console.warn("[WCR] AAC encoder unsupported, skipping audio track");
+ this.warn("[WCR] AAC unsupported");
return null;
}
return support.config || candidateConfig;
- } catch (err) {
- console.warn("[WCR] Audio encoder capability probe failed", err);
+ } catch {
+ this.warn("[WCR] AAC probe failed");
return null;
}
}
@@ -472,45 +459,32 @@ export class WebCodecsRecorder {
};
const candidates = [
- { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" }, // High
- { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" }, // Main
- { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" }, // Baseline
+ { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" },
+ { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" },
{ codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-software" },
{ codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-software" },
{ codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-software" },
];
for (const c of candidates) {
- const config = {
- ...base,
- codec: c.codec,
- hardwareAcceleration: c.hw,
- };
-
+ const config = { ...base, codec: c.codec, hardwareAcceleration: c.hw };
try {
- // REAL capability check: actually configure an encoder, not just ask.
const test = new VideoEncoder({
output() {},
- error(err) {
- console.warn("[WCR] test encoder error for", c.codec, c.hw, err);
- },
+ error() {},
});
-
test.configure(config);
test.close();
-
- console.log("[WCR] Selected encoder:", c.codec, c.hw);
+ this.log("[WCR] Selected encoder:", c.codec, c.hw);
return {
config,
codec: c.codec,
containerCodec: c.containerCodec,
};
- } catch (err) {
- console.warn("[WCR] Unsupported encoder:", c.codec, c.hw, err);
- }
+ } catch (e) {}
}
-
- throw new Error("WebCodecsRecorder: No supported H.264 encoder available");
+ throw new Error("WebCodecsRecorder: No supported H.264 encoder");
}
getVideoTimestampUs(frame) {
@@ -524,20 +498,17 @@ export class WebCodecsRecorder {
Math.round(frame.timestamp - this.videoTimestampOffsetUs)
);
- // 🟒 FIX: subtract paused duration
return Math.max(0, baseUs - this.totalPausedDurationUs);
}
- // Fallback for browsers without timestamps
const now = performance.now();
if (!this._lastFrameTime) this._lastFrameTime = now;
const delta = now - this._lastFrameTime;
this._lastFrameTime = now;
- // If encoder falls behind (e.g. >150ms per frame, ~7 FPS)
if (delta > 160 && !this._warnedBackpressure) {
this._warnedBackpressure = true;
- console.warn("[WCR] encoding overload detected:", delta, "ms/frame");
+ this.warn("[WCR] overload", delta);
this.options.onError?.({ type: "encoder-overload", delta });
}
if (this.videoFallbackStartMs === null) {
@@ -553,25 +524,16 @@ export class WebCodecsRecorder {
}
async readVideoLoop() {
- console.log("[WCR] video loop starting");
- if (!this.videoReader || !this.videoEncoder) {
- console.error("[WCR] Missing videoReader or videoEncoder!");
- return;
- }
+ this.log("[WCR] video loop start");
+ if (!this.videoReader || !this.videoEncoder) return;
- // lazily init canvas
const ensureResizeCanvas = () => {
if (!this.resizeCanvas) {
this.resizeCanvas = document.createElement("canvas");
this.resizeCanvas.width = this.targetWidth;
this.resizeCanvas.height = this.targetHeight;
this.resizeCtx = this.resizeCanvas.getContext("2d");
- console.log(
- "[WCR] resize canvas created:",
- this.targetWidth,
- "x",
- this.targetHeight
- );
+ this.log("[WCR] resize canvas created");
}
};
@@ -586,15 +548,10 @@ export class WebCodecsRecorder {
.read()
.catch(() => ({ done: true }));
- if (done) {
- console.warn("[WCR] video track ended β€” stopping");
- this.options.onError?.({ type: "video-ended" });
- break;
- }
- if (!frame) break;
+ if (done || !frame) break;
if (frame.codedWidth === 0 || frame.codedHeight === 0) {
- console.warn("[WCR] zero-size video frame β€” possible track loss");
+ this.warn("[WCR] zero-size frame");
this.options.onError?.({ type: "video-lost" });
}
@@ -614,27 +571,17 @@ export class WebCodecsRecorder {
const timestampUs = this.getVideoTimestampUs(frame);
- // πŸ”₯ FIRST VIDEO FRAME β†’ establish baseline and init audio
if (this.firstVideoFrame === undefined) {
this.firstVideoFrame = true;
-
- // define baseline
this.videoTimestampOffsetUs = frame.timestamp;
- console.log(
- "[WCR] Video baseline set to",
- this.videoTimestampOffsetUs
- );
+ this.log("[WCR] baseline:", this.videoTimestampOffsetUs);
- // apply offset BEFORE any audio arrives
if (this.muxer)
this.muxer.setAudioOffset(this.videoTimestampOffsetUs);
- // initialize audio encoder now that baseline exists
if (!this.audioEncoder && this._pendingAudioConfig) {
- console.log("[WCR] initAudioEncoder AFTER first video frame");
await this.initAudioEncoder(this._pendingAudioConfig);
- await Promise.resolve(); // flush any encoder microtasks
- // flush prebuffered audio
+ await Promise.resolve();
for (const buf of this._prebufferedAudio) {
this.audioEncoder.encode(buf);
buf.close?.();
@@ -644,7 +591,6 @@ export class WebCodecsRecorder {
}
}
- // Force first-frame keyframe
const needsKey =
this.frameCount === 0 ||
this.justResumed ||
@@ -652,61 +598,57 @@ export class WebCodecsRecorder {
this.justResumed = false;
this.videoEncoder.encode(resized, { keyFrame: needsKey });
-
this.frameCount++;
resized.close();
frame.close();
}
} catch (err) {
- console.error("[WCR] video loop error:", err);
+ this.err("[WCR] video loop error:", err);
}
- console.log("[WCR] video loop exited, total frames:", this.frameCount);
+ this.log("[WCR] video loop exit", this.frameCount);
}
async readAudioLoop() {
while (this.paused && this.running) {
await new Promise((r) => setTimeout(r, 10));
}
+ if (!this.audioReader) return;
+ this.log("[WCR] audio loop start");
- if (!this.audioReader) return; // ❗ remove audioEncoder requirement
-
- console.log("[WCR] audio loop starting");
-
- while (this.running) {
- const { value: audioData, done } = await this.audioReader
- .read()
- .catch(() => ({ done: true }));
+ try {
+ while (this.running) {
+ const { value: audioData, done } = await this.audioReader
+ .read()
+ .catch(() => ({ done: true }));
- if (done || !audioData) {
- console.log("[WCR] audio loop done or no audio", done, audioData);
- break;
- }
+ if (done || !audioData) break;
+ if (!this.audioTrack || this.audioTrack.readyState === "ended") {
+ this.warn("[WCR] audio lost");
+ this.options.onError?.({ type: "audio-lost" });
+ break;
+ }
- if (!this.audioTrack || this.audioTrack.readyState === "ended") {
- console.warn("[WCR] audio track ended β€” mic lost");
- this.options.onError?.({ type: "audio-lost" });
- break;
- }
+ if (!this._audioReady) {
+ this._prebufferedAudio.push(audioData);
+ continue;
+ }
- if (!this._audioReady) {
- // buffer raw audio frames until video baseline is known
- this._prebufferedAudio.push(audioData);
- continue;
- }
+ try {
+ this.audioEncoder.encode(audioData);
+ } catch (err) {
+ audioData.close?.();
+ this.options.onError?.(err);
+ break;
+ }
- try {
- this.audioEncoder.encode(audioData);
- } catch (err) {
audioData.close?.();
- this.options.onError?.(err);
- break;
}
-
- audioData.close?.();
+ } catch (err) {
+ this.err("[WCR] audio loop error:", err);
}
- console.log("[WCR] audio loop exited");
+ this.log("[WCR] audio loop exit");
}
}
diff --git a/src/pages/Region/Recorder.jsx b/src/pages/Region/Recorder.jsx
index 69691ea..6c944d9 100644
--- a/src/pages/Region/Recorder.jsx
+++ b/src/pages/Region/Recorder.jsx
@@ -1,10 +1,4 @@
-import React, {
- useEffect,
- useState,
- useRef,
- useCallback,
- useContext,
-} from "react";
+import React, { useEffect, useRef, useCallback } from "react";
import localforage from "localforage";