The free and privacy-friendly screen recorder with no limits π₯
JavaScript annotationannotation-toolaudiocamerachrome-extensiondesigneditorffmpegjavascriptproductivityrecorderscreen-capturescreen-recorderscreencasttensorflowvideo
WIP: Webcodecs recorder
Alyssa X498d90209f9d
diff --git a/src/pages/Background/recording/recordingHelpers.js b/src/pages/Background/recording/recordingHelpers.jsindex 03170ab..4bd1ed4 100644--- a/src/pages/Background/recording/recordingHelpers.js+++ b/src/pages/Background/recording/recordingHelpers.js@@ -71,7 +71,7 @@ export const handleRecordingComplete = async () => {tab.url.includes("recorder.html")) {// FLAG: For testing purposes -> comment to debug- removeTab(recordingTab);+ //removeTab(recordingTab);}}});@@ -95,7 +95,7 @@ export const handleRecordingError = async (request) => {const { region } = await chrome.storage.local.get(["region"]);if (recordingTab && !region) {// FLAG: For testing purposes -> comment to debug- removeTab(recordingTab);+ //removeTab(recordingTab);}chrome.storage.local.set({ recordingTab: null });discardOffscreenDocuments();diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsxindex ef68eb4..ef3b414 100644--- a/src/pages/Recorder/Recorder.jsx+++ b/src/pages/Recorder/Recorder.jsx@@ -5,6 +5,8 @@ import { createMediaRecorder } from "./mediaRecorderUtils";import { sendRecordingError, sendStopRecording } from "./messaging";import { getBitrates, getResolutionForQuality } from "./recorderConfig";+import { WebCodecsRecorder } from "./webcodecs/WebCodecsRecorder";+localforage.config({driver: localforage.INDEXEDDB,name: "screenity",@@ -16,6 +18,8 @@ const chunksStore = localforage.createInstance({name: "chunks",});+document.body.style.willChange = "contents";+const Recorder = () => {const isRestarting = useRef(false);const isFinishing = useRef(false);@@ -44,6 +48,7 @@ const Recorder = () => {const audioOutputGain = useRef(null);const recorder = useRef(null);+ const useWebCodecs = useRef(false);const isTab = useRef(false);const tabID = useRef(null);@@ -189,8 +194,10 @@ const Recorder = () => {if (recorder.current !== null) return;navigator.storage.persist();- // Check if the stream actually has data in it- if (helperVideoStream.current.getVideoTracks().length === 0) {+ if (+ !helperVideoStream.current ||+ helperVideoStream.current.getVideoTracks().length === 0+ ) {sendRecordingError("No video tracks available");return;}@@ -205,21 +212,29 @@ const Recorder = () => {draining.current = false;lowStorageAbort.current = false;pendingBytes.current = 0;+ sentLast.current = false;+ isFinishing.current = false;- try {- const { qualityValue } = await chrome.storage.local.get(["qualityValue"]);-- const { audioBitsPerSecond, videoBitsPerSecond } =- getBitrates(qualityValue);-- recorder.current = createMediaRecorder(liveStream.current, {- audioBitsPerSecond,- videoBitsPerSecond,- });- } catch (err) {- sendRecordingError(JSON.stringify(err));- return;- }+ const { qualityValue } = await chrome.storage.local.get(["qualityValue"]);+ const { audioBitsPerSecond, videoBitsPerSecond } =+ getBitrates(qualityValue);++ // Decide if we can use WebCodecs+ const canUseWebCodecs =+ 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;chrome.storage.local.set({recording: true,@@ -229,102 +244,83 @@ const Recorder = () => {isRestarting.current = false;index.current = 0;- try {- recorder.current.start(1000);- } catch (err) {- sendRecordingError("Failed to start recording: " + JSON.stringify(err));- return;- }+ // 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" });- recorder.current.onerror = (ev) => {- chrome.runtime.sendMessage({- type: "recording-error",- error: "mediarecorder",- why: String(ev?.error || "unknown"),- });- };-- recorder.current.onstop = async () => {- try {- recorder.current?.requestData();- } catch {}- if (isRestarting.current) return;- await waitForDrain();- if (!sentLast.current) {- sentLast.current = true;- isFinishing.current = false;- chrome.runtime.sendMessage({ type: "video-ready" });- }- };-- if (helperAudioStream.current) {- const track = helperAudioStream.current.getAudioTracks()[0];- if (track) {- track.onended = () => {- chrome.storage.local.set({ recording: false });- sendStopRecording();- };- }- }+ const ts = timestampMs ?? 0;+ const i = index.current;- const checkMaxMemory = () => {- try {- navigator.storage.estimate().then(({ usage = 0, quota = 0 }) => {- const remaining = quota - usage;- const minHeadroom = 25 * 1024 * 1024;- if (remaining < minHeadroom) {- chrome.storage.local.set({- recording: false,- restarting: false,- tabRecordedID: null,- memoryError: true,- });- sendStopRecording();- }- });- } catch (err) {- sendRecordingError(- "Failed to check available memory: " + JSON.stringify(err)+ console.log(+ "[RECORDER] WebCodecs handleChunk β index",+ i,+ "size",+ blob.size,+ "ts",+ ts);- }- };- recorder.current.ondataavailable = async (e) => {- if (!e || !e.data || !e.data.size) {- if (recorder.current && recorder.current.state === "inactive") {- chrome.storage.local.set({- recording: false,- restarting: false,- tabRecordedID: null,+ try {+ await chunksStore.setItem(`chunk_${i}`, {+ index: i,+ chunk: blob,+ timestamp: ts,});- sendStopRecording();+ console.log("[RECORDER] WebCodecs saved chunk_", i);+ index.current = i + 1;+ hasChunks.current = true;+ savedCount.current += 1;++ if (backupRef.current) {+ chrome.runtime.sendMessage({ type: "write-file", index: i });+ }+ } catch (err) {+ console.error("[RECORDER] WebCodecs saveChunk error", err);}+return;}+ // πΉ MediaRecorder path (unchanged)if (lowStorageAbort.current) return;+ const blob =+ data instanceof Blob ? data : new Blob([data], { type: "video/mp4" });++ const e = {+ data: blob,+ timecode: timestampMs ?? 0,+ };+if (!hasChunks.current) {hasChunks.current = true;- lastTimecode.current = e.timecode ?? 0;+ lastTimecode.current = e.timecode;lastSize.current = e.data.size;}- // push chunkpending.current.push(e);pendingBytes.current += e.data.size;if (pendingBytes.current > MAX_PENDING_BYTES) {try {- if (recorder.current.state !== "paused") {+ if (+ recorder.current instanceof MediaRecorder &&+ recorder.current.state !== "paused"+ ) {recorder.current.pause();}await drainQueue();- if (recorder.current.state === "paused") {+ if (+ recorder.current instanceof MediaRecorder &&+ recorder.current.state === "paused"+ ) {recorder.current.resume();}} catch {- // fallback: force drain without pausingawait drainQueue();}}@@ -332,14 +328,119 @@ const Recorder = () => {void drainQueue();};- recorder.current.onpause = () => {- lastTimecode.current = 0;- lastSize.current = 0;- };- recorder.current.onresume = () => {- lastTimecode.current = 0;- lastSize.current = 0;- };+ try {+ if (canUseWebCodecs) {+ // β Use WebCodecs backend+ useWebCodecs.current = true;++ recorder.current = new WebCodecsRecorder(liveStream.current, {+ width,+ height,+ fps,+ videoBitrate: videoBitsPerSecond,+ audioBitrate: audioBitsPerSecond,+ onFinalized: async () => {+ console.log("[RECORDER] WebCodecs finalize event β assembling MP4");+ await assembleAndDownload(); // β only downloaded here+ },+ 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;++ handleChunk(blob, timestampMs);+ },+ onError: (err) => {+ console.error("[WebCodecsRecorder] error", err);+ sendRecordingError(String(err));+ },+ onStop: async () => {+ await waitForDrain();+ if (!sentLast.current) {+ sentLast.current = true;+ isFinishing.current = false;+ chrome.runtime.sendMessage({ type: "video-ready" });+ }+ },+ });++ await recorder.current.start();+ } else {+ // π Fallback to MediaRecorder (current behavior)+ useWebCodecs.current = false;+ recorder.current = createMediaRecorder(liveStream.current, {+ audioBitsPerSecond,+ videoBitsPerSecond,+ });++ try {+ recorder.current.start(1000);+ } catch (err) {+ sendRecordingError(+ "Failed to start recording: " + JSON.stringify(err)+ );+ return;+ }++ recorder.current.onerror = (ev) => {+ chrome.runtime.sendMessage({+ type: "recording-error",+ error: "mediarecorder",+ why: String(ev?.error || "unknown"),+ });+ };++ recorder.current.onstop = async () => {+ try {+ recorder.current.requestData();+ } catch {}+ if (isRestarting.current) return;+ await waitForDrain();+ if (!sentLast.current) {+ sentLast.current = true;+ isFinishing.current = false;+ chrome.runtime.sendMessage({ type: "video-ready" });+ }+ };++ recorder.current.ondataavailable = async (e) => {+ if (!e || !e.data || !e.data.size) {+ if (+ recorder.current instanceof MediaRecorder &&+ recorder.current.state === "inactive"+ ) {+ chrome.storage.local.set({+ recording: false,+ restarting: false,+ tabRecordedID: null,+ });+ sendStopRecording();+ }+ return;+ }++ await handleChunk(e.data, e.timecode ?? 0);+ };+ }+ } catch (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 = () => {+ chrome.storage.local.set({ recording: false });+ sendStopRecording();+ };+ }+ }liveStream.current.getVideoTracks()[0].onended = () => {chrome.storage.local.set({@@ -360,16 +461,53 @@ const Recorder = () => {};}+ async function assembleAndDownload() {+ const keys = await chunksStore.keys();+ const items = await Promise.all(keys.map((k) => chunksStore.getItem(k)));+ const ordered = items.sort((a, b) => a.index - b.index);++ const blob = new Blob(+ ordered.map((o) => o.chunk),+ {+ type: "video/mp4",+ }+ );++ const url = URL.createObjectURL(blob);+ const a = document.createElement("a");+ a.href = url;+ a.download = "recording.mp4";+ a.click();++ setTimeout(() => URL.revokeObjectURL(url), 2000);++ console.log(+ "[RECORDER] Auto-downloaded recording.mp4",+ ordered.length,+ "chunks"+ );+ }+async function stopRecording() {+ if (isFinishing.current) {+ console.log("[RECORDER] stopRecording ignored (already running)");+ return;+ }isFinishing.current = true;try {- recorder.current?.requestData();- } catch {}-- try {- if (recorder.current && recorder.current.state !== "inactive") {- recorder.current.stop();+ if (+ useWebCodecs.current &&+ recorder.current instanceof WebCodecsRecorder+ ) {+ await recorder.current.stop();+ } else if (recorder.current instanceof MediaRecorder) {+ try {+ recorder.current.requestData();+ } catch {}+ if (recorder.current.state !== "inactive") {+ recorder.current.stop();+ }}} catch {}@@ -529,8 +667,11 @@ const Recorder = () => {mandatory: {chromeMediaSource: isTab.current ? "tab" : "desktop",chromeMediaSourceId: id,- maxWidth: width,- maxHeight: height,+ // FLAG: try ideal + max to enforce exact resolution+ // maxWidth: width,+ // maxHeight: height,+ // width: { ideal: width, max: width },+ // height: { ideal: height, max: height },maxFrameRate: fps,},},@@ -726,10 +867,20 @@ const Recorder = () => {setAudioOutputVolume(request.volume);} else if (request.type === "pause-recording-tab") {if (!recorder.current) return;- recorder.current.pause();+ if (+ !useWebCodecs.current &&+ recorder.current instanceof MediaRecorder+ ) {+ recorder.current.pause();+ }} else if (request.type === "resume-recording-tab") {if (!recorder.current) return;- recorder.current.resume();+ if (+ !useWebCodecs.current &&+ recorder.current instanceof MediaRecorder+ ) {+ recorder.current.resume();+ }} else if (request.type === "dismiss-recording") {dismissRecording();}diff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.tsnew file mode 100644index 0000000..49651a8--- /dev/null+++ b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts@@ -0,0 +1,240 @@+// webcodecs/Mp4MuxerWrapper.ts+// @ts-nocheck++import {+ Output,+ Mp4OutputFormat,+ BufferTarget,+ EncodedVideoPacketSource,+ EncodedAudioPacketSource,+ 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 started = false;++ constructor(options: Mp4MuxerWrapperOptions) {+ this.options = options;++ this.target = new BufferTarget();+ this.videoCodec = options.videoCodec || "avc";+ this.audioCodec = options.audioCodec || "aac";++ const minFragmentDuration = Math.max(+ 0.2,+ (this.options.fps ? 1 / this.options.fps : 1 / 30) * 6+ );++ this.output = new Output({+ format: new Mp4OutputFormat({+ fastStart: "in-memory",+ minimumFragmentDuration: minFragmentDuration,+ onFtyp: (data) => {+ this.ftypChunk = this.cloneChunk(data);+ this.emitHeaderIfReady();+ },+ onMoov: (data) => {+ this.moovChunk = this.cloneChunk(data);+ this.emitHeaderIfReady();+ },+ onMoof: (data, _pos, timestampSeconds = 0) => {+ this.pendingMoof = this.cloneChunk(data);+ this.pendingMoofTimestampSeconds = this.normalizeFragmentTimestamp(+ timestampSeconds+ );+ },+ onMdat: (data) => {+ if (!this.pendingMoof) {+ return;+ }+ const fragment = this.concatChunks([+ this.pendingMoof,+ this.cloneChunk(data),+ ]);+ const timestampUs =+ this.pendingMoofTimestampSeconds != null+ ? Math.round(this.pendingMoofTimestampSeconds * 1e6)+ : null;+ this.emitChunk(fragment, timestampUs);+ this.pendingMoof = null;+ this.pendingMoofTimestampSeconds = null;+ },+ }),+ target: this.target,+ });++ this.videoSource = new EncodedVideoPacketSource(this.videoCodec);+ this.output.addVideoTrack(this.videoSource, {+ frameRate: this.options.fps,+ });+ }++ enableAudio() {+ if (!this.audioSource) {+ this.audioSource = new EncodedAudioPacketSource(this.audioCodec);+ this.output.addAudioTrack(this.audioSource);+ }+ }++ addVideoChunk(chunk: EncodedVideoChunk, meta: any) {+ const packet = this.buildPacketFromChunk(chunk, meta, "video");+ return this.videoSource.add(packet, meta);+ }++ addAudioChunk(chunk: EncodedAudioChunk, meta: any) {+ if (!this.audioSource) {+ console.warn("[MUXER] audioSource missing β ignoring audio packet");+ return;+ }+ const packet = this.buildPacketFromChunk(chunk, meta, "audio");+ return this.audioSource.add(packet, meta);+ }++ async start() {+ if (this.started) return;+ await this.output.start();+ this.started = true;+ }++ async finalize() {+ await this.output.finalize();++ const data = this.target.buffer;+ if (data && data.byteLength > 0) {+ this.emitChunk(new Uint8Array(data), null);+ }+ }++ private 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) {+ 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);+ });+ }+ } catch (err) {+ console.error("[MUXER] onChunk error", err);+ }+ }++ private cloneChunk(data: Uint8Array) {+ return new Uint8Array(data);+ }++ private concatChunks(chunks: Uint8Array[]) {+ const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);+ const combined = new Uint8Array(total);+ let offset = 0;+ for (const chunk of chunks) {+ combined.set(chunk, offset);+ offset += chunk.byteLength;+ }+ return combined;+ }++ private buildPacketFromChunk(+ chunk: EncodedVideoChunk | EncodedAudioChunk,+ meta: any,+ kind: "video" | "audio"+ ) {+ 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+ );+ }++ private normalizeSampleTimestamp(+ kind: "video" | "audio",+ timestampUs?: number,+ durationUs: number = 0+ ) {+ const offsetKey =+ kind === "video" ? "videoTimestampOffsetUs" : "audioTimestampOffsetUs";+ const lastKey =+ kind === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";++ if (typeof timestampUs !== "number") {+ const next = (this[lastKey] || 0) + durationUs;+ this[lastKey] = next;+ return next;+ }++ if (this[offsetKey] === null) {+ this[offsetKey] = timestampUs;+ }++ const relative = Math.max(0, timestampUs - (this[offsetKey] || 0));+ this[lastKey] = relative;+ return relative;+ }++ private normalizeFragmentTimestamp(timestampSeconds: number | null | undefined) {+ if (timestampSeconds == null) {+ return null;+ }+ if (this.fragmentTimestampOffsetSeconds == null) {+ this.fragmentTimestampOffsetSeconds = timestampSeconds;+ }+ return Math.max(+ 0,+ timestampSeconds - this.fragmentTimestampOffsetSeconds+ );+ }+}diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.jsnew file mode 100644index 0000000..c6cb346--- /dev/null+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js@@ -0,0 +1,574 @@+// extension/webcodecs/WebCodecsRecorder.js+import { Mp4MuxerWrapper } from "./Mp4MuxerWrapper";++export class WebCodecsRecorder {+ constructor(stream, options) {+ this.stream = stream;+ this.options = options;++ this.videoTrack = null;+ this.audioTrack = null;++ this.videoProcessor = null;+ this.audioProcessor = null;++ this.videoReader = null;+ this.audioReader = null;++ this.videoEncoder = null;+ this.audioEncoder = null;++ this.muxer = null;++ 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;++ this.videoTimestampOffsetUs = null;+ this.videoFallbackStartMs = null;+ this.selectedVideoCodec = null;+ this.firstVideoFrame = undefined;+ this.frameCount = 0;++ // Resize canvas for downscaling frames+ this.resizeCanvas = null;+ this.resizeCtx = null;+ }++ async _probeRealResolution() {+ const processor = new MediaStreamTrackProcessor({ track: this.videoTrack });+ const reader = processor.readable.getReader();++ const { value: frame } = await reader.read();+ if (!frame) throw new Error("Cannot probe frame resolution");++ const width = frame.codedWidth;+ const height = frame.codedHeight;++ console.log("[WCR] Probed initial resolution:", width, "x", height);+ frame.close();+ reader.releaseLock();++ return { width, height };+ }++ async start() {+ console.log("[WCR] start() called");+ if (this.running) {+ console.log("[WCR] already running, ignoring start()");+ return;+ }++ this.running = true;++ this.videoTrack = this.stream.getVideoTracks()[0] || null;+ this.audioTrack = this.stream.getAudioTracks()[0] || null;++ console.log(+ "[WCR] videoTrack",+ this.videoTrack,+ "state=",+ this.videoTrack?.readyState+ );+ console.log(+ "[WCR] audioTrack",+ this.audioTrack,+ "state=",+ this.audioTrack?.readyState+ );++ if (!this.videoTrack) {+ const err = new Error("WebCodecsRecorder: No video track");+ console.error("[WCR] error:", err);+ this.options.onError?.(err);+ this.running = false;+ return;+ }++ 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+ );++ 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,+ fps,+ bitrate: safeBitrate,+ });+ this.selectedVideoCodec = videoConfig.codec;++ const audioConfig = await this.prepareAudioEncoderConfig();++ console.log("[WCR] Video config chosen:", videoConfig);+ console.log("[WCR] Audio config:", audioConfig);++ console.log("[WCR] creating MediaStreamTrackProcessors...");+ this.videoProcessor = new MediaStreamTrackProcessor({+ track: this.videoTrack,+ });+ this.videoReader = this.videoProcessor.readable.getReader();++ if (audioConfig && this.audioTrack) {+ this.audioProcessor = new MediaStreamTrackProcessor({+ track: this.audioTrack,+ });+ this.audioReader = this.audioProcessor.readable.getReader();+ } else {+ this.audioProcessor = null;+ this.audioReader = null;+ }++ console.log("[WCR] creating muxer...");+ this.muxer = new Mp4MuxerWrapper({+ width: this.targetWidth,+ height: this.targetHeight,+ fps,+ videoBitrate: this.options.videoBitrate,+ audioBitrate: this.options.audioBitrate,+ videoCodec: videoConfig.containerCodec,+ audioCodec: audioConfig ? "aac" : undefined,+ onChunk: this.options.onChunk,+ });+ console.log("[WCR] muxer created");++ if (audioConfig) {+ this.muxer.enableAudio();+ }++ await this.muxer.start();+ console.log("[WCR] muxer started");++ console.log("[WCR] initVideoEncoder()...");+ await this.initVideoEncoder(videoConfig.config, videoConfig.codec);+ console.log("[WCR] initVideoEncoder() done");++ if (audioConfig) {+ console.log("[WCR] initAudioEncoder()...");+ await this.initAudioEncoder(audioConfig);+ console.log("[WCR] initAudioEncoder() done");+ } else {+ console.log("[WCR] audio encoder skipped (unsupported or no track)");+ }++ console.log("[WCR] starting read loops...");+ this.readVideoLoop();+ if (this.audioReader && this.audioEncoder) {+ this.readAudioLoop();+ }+ console.log("[WCR] start() completed");+ } catch (err) {+ console.error("[WCR] start() failed:", err);+ this.options.onError?.(err);+ this.running = false;+ this.cleanup();+ }+ }++ async stop() {+ console.log("[WCR] stop() called, frames encoded:", this.frameCount);+ if (!this.running) {+ console.log("[WCR] not running, ignoring stop()");+ return;+ }+ this.running = false;++ 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.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.options.onError?.(err);+ }++ this.cleanup();++ if (this.options.onStop) {+ await this.options.onStop();+ }+ }++ cleanup() {+ console.log("[WCR] cleanup()");+ try {+ this.videoReader?.releaseLock();+ } catch {}+ try {+ this.audioReader?.releaseLock();+ } catch {}++ try {+ this.videoEncoder?.close();+ } catch {}+ try {+ this.audioEncoder?.close();+ } catch {}++ 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.videoTimestampOffsetUs = null;+ this.videoFallbackStartMs = null;+ this.firstVideoFrame = undefined;+ this.selectedVideoCodec = null;++ this.resizeCanvas = null;+ this.resizeCtx = null;+ }++ async initVideoEncoder(config, codecLabel) {+ console.log("[WCR] initVideoEncoder()", codecLabel, config);++ 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,+ });++ 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 =+ decoderConfig.codedHeight || this.targetHeight;+ normalizedMeta.decoderConfig = decoderConfig;++ this.muxer.addVideoChunk(chunk, normalizedMeta);+ },+ error: (err) => {+ console.error("[WCR] VideoEncoder error:", err);+ this.options.onError?.(err);+ },+ });++ this.videoEncoder.configure(config);+ console.log("[WCR] VideoEncoder configured successfully");+ }++ async initAudioEncoder(config) {+ if (!config) return;++ this.audioEncoder = new AudioEncoder({+ output: (chunk, meta) => {+ console.log("[WCR] AUDIO CHUNK", chunk, meta);+ this.muxer.addAudioChunk(chunk, meta);+ },+ error: (err) => {+ console.error("[WCR] AudioEncoder error:", err);+ this.options.onError?.(err);+ },+ });++ this.audioEncoder.configure(config);++ console.log("[WCR] audio encoder OK");+ }++ async prepareAudioEncoderConfig() {+ if (!this.audioTrack) return null;++ const settings = this.audioTrack.getSettings();+ const sampleRate = settings.sampleRate || 48000;+ const numberOfChannels = settings.channelCount || 2;++ const candidateConfig = {+ codec: "mp4a.40.2",+ sampleRate,+ numberOfChannels,+ bitrate: this.options.audioBitrate || 128000,+ };++ try {+ const support = await AudioEncoder.isConfigSupported(candidateConfig);+ if (!support.supported) {+ console.warn("[WCR] AAC encoder unsupported, skipping audio track");+ return null;+ }+ return support.config || candidateConfig;+ } catch (err) {+ console.warn("[WCR] Audio encoder capability probe failed", err);+ return null;+ }+ }++ async chooseVideoEncoderConfig({ width, height, fps, bitrate }) {+ const base = {+ width,+ height,+ framerate: fps,+ bitrate,+ bitrateMode: "constant",+ latencyMode: "realtime",+ };++ 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-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,+ };++ 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);+ },+ });++ test.configure(config);+ test.close();++ console.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);+ }+ }++ throw new Error("WebCodecsRecorder: No supported H.264 encoder available");+ }++ getVideoTimestampUs(frame) {+ if (typeof frame.timestamp === "number") {+ if (this.videoTimestampOffsetUs === null) {+ this.videoTimestampOffsetUs = frame.timestamp;+ }+ return Math.max(+ 0,+ Math.round(frame.timestamp - this.videoTimestampOffsetUs)+ );+ }++ const now = performance.now();+ if (this.videoFallbackStartMs === null) {+ this.videoFallbackStartMs = now;+ }+ return Math.max(0, Math.round((now - this.videoFallbackStartMs) * 1000));+ }++ async readVideoLoop() {+ console.log("[WCR] video loop starting");+ if (!this.videoReader || !this.videoEncoder) {+ console.error("[WCR] Missing videoReader or videoEncoder!");+ return;+ }++ // Lazily init resize canvas on first frame+ 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+ );+ }+ };++ try {+ while (this.running) {+ let readResult;+ try {+ readResult = await this.videoReader.read();+ } catch (err) {+ console.error("[WCR] videoReader.read() error:", err);+ break;+ }++ const { value: frame, done } = readResult;++ if (done || !frame) {+ console.log("[WCR] video loop done or no frame", done, !!frame);+ break;+ }++ console.log("[WCR] got VIDEO FRAME", {+ frameNumber: this.frameCount,+ ts: frame.timestamp,+ codedWidth: frame.codedWidth,+ codedHeight: frame.codedHeight,+ format: frame.format,+ });++ ensureResizeCanvas();++ // Draw original frame to canvas β downscale to target+ this.resizeCtx.drawImage(+ frame,+ 0,+ 0,+ this.targetWidth,+ this.targetHeight+ );++ // Create new VideoFrame from the resized canvas+ const resized = new VideoFrame(this.resizeCanvas, {+ timestamp: frame.timestamp,+ });++ const timestampUs = this.getVideoTimestampUs(frame);++ try {+ const isFirstFrame = this.firstVideoFrame === undefined;+ if (isFirstFrame) this.firstVideoFrame = true;++ // Force keyframe every 2 seconds or on first frame+ const framesSinceKey = this.frameCount % (this.options.fps * 2 || 60);+ const needsKey = isFirstFrame || framesSinceKey === 0;++ console.log("[WCR] Encoding frame", this.frameCount, {+ timestampUs,+ keyFrame: needsKey,+ encoderQueueSize: this.videoEncoder.encodeQueueSize,+ encoderState: this.videoEncoder.state,+ });++ // encode resized frame; timestamp comes from frame.timestamp+ this.videoEncoder.encode(resized, {+ keyFrame: needsKey,+ });++ this.frameCount++;+ } catch (err) {+ console.error("[WCR] video encode error", err);+ this.options.onError?.(err);+ resized.close();+ frame.close();+ break;+ }++ resized.close();+ frame.close();+ }+ } catch (err) {+ console.error("[WCR] video loop error:", err);+ }++ console.log("[WCR] video loop exited, total frames:", this.frameCount);+ }++ async readAudioLoop() {+ if (!this.audioReader || !this.audioEncoder) return;++ console.log("[WCR] audio loop starting");++ 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;+ }++ console.log("[WCR] got AUDIO FRAME", audioData);++ try {+ this.audioEncoder.encode(audioData);+ } catch (err) {+ audioData.close?.();+ this.options.onError?.(err);+ break;+ }++ audioData.close?.();+ }++ console.log("[WCR] audio loop exited");+ }+}