The free and privacy-friendly screen recorder with no limits 🎥
JavaScript annotationannotation-toolaudiocamerachrome-extensiondesigneditorffmpegjavascriptproductivityrecorderscreen-capturescreen-recorderscreencasttensorflowvideo
Stable webcodecs recorder
Alyssa Xf999733bad14
diff --git a/build.zip b/build.zipindex eb13c2f..5a836e2 100644Binary files a/build.zip and b/build.zip differdiff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsxindex 0e2a013..9928d4e 100644--- a/src/pages/Recorder/Recorder.jsx+++ b/src/pages/Recorder/Recorder.jsx@@ -25,8 +25,7 @@ document.body.style.willChange = "contents";// 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;-+// typeof window !== "undefined" ? !!window.SCREENITY_DEBUG_RECORDER : false;const DEBUG_RECORDER = true;const logPrefix = "[Screenity Recorder]";diff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.tsnew file mode 100644index 0000000..1f62198--- /dev/null+++ b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.ts@@ -0,0 +1,276 @@+/*!+ * Screenity WebCodecs Recorder+ * 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 implements complex WebCodecs-based recording logic+ * for video/audio sync, muxing, and safe timestamp management.+ * If you reuse or modify this file in another project, GPLv3+ * obligations (including copyleft and attribution) apply.+ */+// @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 pausedOffsetUs: number;++ private started = false;++ constructor(options: Mp4MuxerWrapperOptions) {+ this.options = options;++ this.target = new BufferTarget();+ this.videoCodec = options.videoCodec || "avc";+ this.audioCodec = options.audioCodec || "aac";+ this.pausedOffsetUs = 0;++ 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);+ }+ }++ setPausedOffset(offsetUs: number) {+ this.pausedOffsetUs = offsetUs;+ }++ setAudioOffset(offsetUs: number) {+ this.audioTimestampOffsetUs = offsetUs;+ }++ 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";++ // 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;++ // 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;+ }++ const relative = Math.max(+ 0,+ timestampUs - baseOffset - this.pausedOffsetUs+ );++ (this as any)[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/Mp4MuxerWrapper-old2.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old2.tsnew file mode 100644index 0000000..bbecbc3--- /dev/null+++ b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old2.ts@@ -0,0 +1,252 @@+/*!+ * Screenity WebCodecs Recorder+ * 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 implements complex WebCodecs-based recording logic+ * for video/audio sync, muxing, and safe timestamp management.+ * If you reuse or modify this file in another project, GPLv3+ * obligations (including copyleft and attribution) apply.+ */+// @ts-nocheck++import {+ Output,+ Mp4OutputFormat,+ StreamTarget,+ StreamTargetChunk,+ 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: StreamTarget;++ private videoSource: EncodedVideoPacketSource;+ private audioSource: EncodedAudioPacketSource | null = null;+ private videoCodec: string;+ private audioCodec: string;++ // We no longer manually track ftyp/moov/moof/mdat here. StreamTarget+ // will give us the final MP4 in order, chunk by chunk.+ private headerEmitted = false;++ 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) {+ this.options = options;++ this.videoCodec = options.videoCodec || "avc";+ this.audioCodec = options.audioCodec || "aac";+ this.pausedOffsetUs = 0;++ const minFragmentDuration = Math.max(+ 0.2,+ (this.options.fps ? 1 / this.options.fps : 1 / 30) * 6+ );++ // StreamTarget: lets us stream bytes out incrementally instead of+ // ever materializing a single huge ArrayBuffer in memory.+ const writable = new WritableStream<StreamTargetChunk>({+ write: (chunk) => {+ // In fragmented fast-start mode, Mp4OutputFormat writes in+ // append-only order, so simply appending all Uint8Arrays in+ // arrival order yields a valid fMP4 file.+ //+ // Your existing IndexedDB “chunksStore” can keep doing:+ // chunks.push(chunk.data)+ // and later:+ // new Blob(chunks, { type: "video/mp4" })+ //+ // We don’t have an exact fragment timestamp here, so we just+ // pass null. If you really want, you can plumb something like+ // this.lastVideoTimestampUs instead.+ this.emitChunk(chunk.data, null);+ },+ });++ this.target = new StreamTarget(writable, {+ // Chunked mode: accumulate into reasonably sized chunks before+ // emitting, to keep `onChunk` frequency sane.+ chunked: true,+ chunkSize: 2 ** 20, // 1 MiB; tweak if you want bigger/smaller IDB chunks+ });++ this.output = new Output({+ target: this.target,+ format: new Mp4OutputFormat({+ // Key part: fragmented fast-start → append-only writes,+ // suitable for streaming / simple concatenation.+ fastStart: "fragmented",+ minimumFragmentDuration: minFragmentDuration,+ }),+ });++ 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() {+ // Important: with StreamTarget this *no longer* allocates a giant+ // ArrayBuffer. It just finishes writing and closes the stream.+ await this.output.finalize();+ // Nothing else to emit here: all data (including header + tail)+ // has already gone through the WritableStream → onChunk path.+ }++ setPausedOffset(offsetUs: number) {+ this.pausedOffsetUs = offsetUs;+ }++ setAudioOffset(offsetUs: number) {+ this.audioTimestampOffsetUs = offsetUs;+ }++ 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 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";++ // 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;++ // 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;+ }++ const relative = Math.max(+ 0,+ timestampUs - baseOffset - this.pausedOffsetUs+ );++ (this as any)[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+ );+ }+}\ No newline at end of filediff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.tsindex b115461..b16d990 100644--- a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts+++ b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper.ts@@ -1,14 +1,6 @@/*!- * Screenity WebCodecs Recorder- * 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 implements complex WebCodecs-based recording logic- * for video/audio sync, muxing, and safe timestamp management.- * If you reuse or modify this file in another project, GPLv3- * obligations (including copyleft and attribution) apply.+ * Screenity WebCodecs Recorder - MP4 Muxer Wrapper+ * Licensed under the GNU GPLv3.*/// @ts-nocheck@@ -21,71 +13,56 @@ 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>;+ debug?: boolean;+}+export class Mp4MuxerWrapper {- constructor(options) {- this.options = options;+ private output: Output;+ private target: BufferTarget;++ private videoSource: EncodedVideoPacketSource;+ private audioSource: EncodedAudioPacketSource | null = null;++ private videoCodec: string;+ private audioCodec: string;+ private pausedOffsetUs = 0;+ private started = false;++ private videoTimestampOffsetUs: number | null = null;+ private audioTimestampOffsetUs: number | null = null;+ private lastVideoTimestampUs = 0;+ private lastAudioTimestampUs = 0;++ private _lastDecoderConfig: any = null;++ private debug: boolean;+ private log: (...args: any[]) => void;+ private warn: (...args: any[]) => void;+ private err: (...args: any[]) => void;- // 🔹 Unified debug toggle + wrappers+ constructor(private options: Mp4MuxerWrapperOptions) {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.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,- (this.options.fps ? 1 / this.options.fps : 1 / 30) * 6- );+ this.target = new BufferTarget();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 tsUs =- this.pendingMoofTimestampSeconds != null- ? Math.round(this.pendingMoofTimestampSeconds * 1e6)- : null;- this.emitChunk(fragment, tsUs);- this.pendingMoof = null;- this.pendingMoofTimestampSeconds = null;- },+ fastStart: false, // metadata at end, more compatible}),target: this.target,});@@ -98,146 +75,115 @@ export class Mp4MuxerWrapper {enableAudio() {if (!this.audioSource) {+ this.log("[MUXER] enableAudio");this.audioSource = new EncodedAudioPacketSource(this.audioCodec);this.output.addAudioTrack(this.audioSource);}}- addVideoChunk(chunk, meta) {- const packet = this.buildPacketFromChunk(chunk, meta, "video");+ addVideoChunk(chunk: EncodedVideoChunk, meta: any) {+ if (meta?.decoderConfig) {+ this._lastDecoderConfig = meta.decoderConfig;+ } else if (!meta.decoderConfig && this._lastDecoderConfig) {+ meta.decoderConfig = this._lastDecoderConfig;+ }++ const packet = this.buildPacket(chunk, meta, "video");return this.videoSource.add(packet, meta);}- addAudioChunk(chunk, meta) {- if (!this.audioSource) {- this.warn("[MUXER] audioSource missing — ignoring packet");- return;- }- const packet = this.buildPacketFromChunk(chunk, meta, "audio");+ addAudioChunk(chunk: EncodedAudioChunk, meta: any) {+ if (!this.audioSource) return;++ const packet = this.buildPacket(chunk, meta, "audio");return this.audioSource.add(packet, meta);}async start() {if (this.started) return;+ this.log("[MUXER] start");await this.output.start();this.started = true;}async finalize() {- this.log?.("[MUXER] finalize() called");+ this.log("[MUXER] finalize");+ await this.output.finalize();- try {- await this.output.finalize();- } catch (err) {- this.err?.("[MUXER] finalize internal error:", err);- }-- // 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)");- }+ const data = this.target.buffer;+ if (data?.byteLength) {+ this.log("[MUXER] emitting final file", data.byteLength, "bytes");+ this.emitChunk(new Uint8Array(data), null);+ }+ }- setPausedOffset(offsetUs) {+ setPausedOffset(offsetUs: number) {this.pausedOffsetUs = offsetUs;}- setAudioOffset(offsetUs) {+ setAudioOffset(offsetUs: number) {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);- }-- emitChunk(chunk, timestampUs) {+ private emitChunk(chunk: Uint8Array, timestampUs: number | null) {try {- const r = this.options.onChunk?.(chunk, timestampUs ?? null);- if (r && typeof r.then === "function") {- r.catch((err) => this.err("[MUXER] onChunk rejected:", err));+ const res = this.options.onChunk?.(chunk, timestampUs ?? null);+ if (res instanceof Promise) {+ res.catch((err) => this.err("[MUXER] onChunk rejected", err));}} catch (err) {- this.err("[MUXER] onChunk error:", err);+ this.err("[MUXER] onChunk error", err);}}- cloneChunk(data) {- return new Uint8Array(data);- }-- concatChunks(chunks) {- const total = chunks.reduce((sum, c) => sum + c.byteLength, 0);- const combined = new Uint8Array(total);- let off = 0;- for (const c of chunks) {- combined.set(c, off);- off += c.byteLength;- }- return combined;- }-- buildPacketFromChunk(chunk, meta, kind) {+ private buildPacket(+ chunk: EncodedVideoChunk | EncodedAudioChunk,+ meta: any,+ type: "video" | "audio"+ ) {const data = new Uint8Array(chunk.byteLength);chunk.copyTo(data);- const relativeTimestampUs = this.normalizeSampleTimestamp(- kind,- chunk.timestamp,- chunk.duration || 0- );+ const tsUs = this.normalizeTimestamp(type, chunk.timestamp, chunk.duration || 0);+ const durSec = (chunk.duration || 0) / 1e6;- return new EncodedPacket(- data,- chunk.type,- relativeTimestampUs / 1e6,- (chunk.duration || 0) / 1e6- );+ return new EncodedPacket(data, chunk.type, tsUs / 1e6, durSec);}- 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;+ private normalizeTimestamp(+ type: "video" | "audio",+ timestampUs?: number,+ durationUs = 0+ ) {+ const key = type === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";+ const last = (this as any)[key] ?? 0;++ if (type === "audio") {+ const next = last + durationUs;+ (this as any)[key] = next;+ this.log(`[MUX] audio ts +${durationUs} => ${next}`);return next;}- let base = this[offsetKey];+ if (typeof timestampUs !== "number") {+ const fallback = last + durationUs;+ (this as any)[key] = fallback;+ this.warn("[MUX] video missing ts, fallback:", fallback);+ return fallback;+ }- if (kind === "audio") {- if (this.audioTimestampOffsetUs == null) {- this.audioTimestampOffsetUs = timestampUs;- }- base = this.audioTimestampOffsetUs;- } else if (base == null) {- base = timestampUs;- this[offsetKey] = base;+ if (this.videoTimestampOffsetUs == null) {+ this.videoTimestampOffsetUs = timestampUs;}const relative = Math.max(0,- timestampUs - base - this.pausedOffsetUs+ timestampUs - this.videoTimestampOffsetUs - this.pausedOffsetUs);- this[lastKey] = relative;- return relative;- }+ (this as any)[key] = relative;+ this.log(`[MUX] video ts => ${relative}`);- normalizeFragmentTimestamp(tsSeconds) {- if (tsSeconds == null) return null;- if (this.fragmentTimestampOffsetSeconds == null) {- this.fragmentTimestampOffsetSeconds = tsSeconds;- }- return Math.max(- 0,- tsSeconds - this.fragmentTimestampOffsetSeconds- );+ return relative;}}\ No newline at end of filediff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.jsnew file mode 100644index 0000000..cd5b7b2--- /dev/null+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.js@@ -0,0 +1,654 @@+/*!+ * Screenity WebCodecs Recorder+ * 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 implements complex WebCodecs-based recording logic+ * for video/audio sync, muxing, and safe timestamp management.+ * If you reuse or modify this file in another project, GPLv3+ * obligations (including copyleft and attribution) apply.+ */+import { Mp4MuxerWrapper } from "./Mp4MuxerWrapper";++export class WebCodecsRecorder {+ constructor(stream, options) {+ 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;++ this.videoProcessor = null;+ this.audioProcessor = null;++ this.videoReader = null;+ this.audioReader = null;++ this.videoEncoder = null;+ this.audioEncoder = null;++ this.muxer = null;++ this.running = false;++ this.actualWidth = 0;+ this.actualHeight = 0;++ this.targetWidth = 0;+ this.targetHeight = 0;++ this.videoTimestampOffsetUs = null;+ this.videoFallbackStartMs = null;+ this.selectedVideoCodec = null;+ this.firstVideoFrame = undefined;+ this.frameCount = 0;++ this.resizeCanvas = null;+ this.resizeCtx = null;++ this.justResumed = false;+ this.paused = false;+ this.pauseStartUs = null;+ this.totalPausedDurationUs = 0;++ this._startPromise = null;+ this._startResolve = null;++ this._prebufferedAudio = [];+ this._audioReady = false;++ this._stopping = false;+ this._videoLoopPromise = null;+ this._audioLoopPromise = 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;++ this.log("[WCR] Probed resolution:", width, "x", height);+ frame.close();+ reader.releaseLock();++ return { width, height };+ }++ async start() {+ this.videoTimestampOffsetUs = null;+ this.firstVideoFrame = undefined;+ this._stopping = false;++ if (this.running) return this._startPromise;++ this._startPromise = new Promise((resolve) => {+ this._startResolve = resolve;+ });++ this.running = true;+ try {+ this.videoTrack = this.stream.getVideoTracks()[0] || null;+ this.audioTrack = this.stream.getAudioTracks()[0] || null;++ this.log(+ "[WCR] videoTrack",+ this.videoTrack,+ "state=",+ this.videoTrack?.readyState+ );+ this.log(+ "[WCR] audioTrack",+ this.audioTrack,+ "state=",+ this.audioTrack?.readyState+ );++ if (!this.videoTrack) {+ const err = new Error("WebCodecsRecorder: No video track");+ this.options.onError?.(err);+ this.running = false;++ if (this._startResolve) {+ this._startResolve(false);+ this._startResolve = null;+ }++ this._startPromise = null;+ return false;+ }++ try {+ const { width, height } = await this._probeRealResolution();+ this.actualWidth = width;+ this.actualHeight = height;++ let targetWidth = Math.min(width, 1920);+ let targetHeight = Math.round((height / width) * targetWidth);+ if (targetHeight > 1080) {+ targetHeight = 1080;+ targetWidth = Math.round((width / height) * targetHeight);+ }+ this.targetWidth = targetWidth;+ this.targetHeight = targetHeight;++ this.log("[WCR] Target:", this.targetWidth, "x", this.targetHeight);++ const fps = this.options.fps || 30;+ const safeBitrate = this.options.videoBitrate || 10_000_000;++ const videoConfig = await this.chooseVideoEncoderConfig({+ width: this.targetWidth,+ height: this.targetHeight,+ fps,+ bitrate: safeBitrate,+ });+ this.selectedVideoCodec = videoConfig.codec;++ const audioConfig = await this.prepareAudioEncoderConfig();+ this._pendingAudioConfig = audioConfig;++ this.log("[WCR] Audio config:", audioConfig);++ if (!audioConfig) {+ this.warn("[WCR] No audio available");+ this.options.audioBitrate = undefined;+ this.options.enableAudio = false;+ this._audioReady = false;+ }++ 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();+ }++ if (this.audioReader && this.options.enableAudio) {+ this.log("[WCR] start buffering audio");+ this._audioLoopPromise = this.readAudioLoop();+ }++ this.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: this.options.enableAudio ? "aac" : undefined,+ onChunk: this.options.onChunk,+ });++ if (this.options.enableAudio) {+ this.muxer.enableAudio();+ }++ await this.muxer.start();+ this.log("[WCR] muxer started");++ await this.initVideoEncoder(videoConfig.config, videoConfig.codec);++ await Promise.resolve();++ setTimeout(() => {+ this._videoLoopPromise = this.readVideoLoop();++ if (this._startResolve) {+ this._startResolve(true);+ this._startResolve = null;+ }+ }, 30);++ this.log("[WCR] start() done");+ } catch (err) {+ this.err("[WCR] start() failed:", err);+ this.options.onError?.(err);+ this.running = false;+ this.cleanup();++ if (this._startResolve) {+ this._startResolve(false);+ this._startResolve = null;+ }+ }+ } catch (err) {+ this.err("[WCR] start() outer error:", err);+ this.options.onError?.(err);+ this.running = false;+ this.cleanup();++ if (this._startResolve) {+ this._startResolve(false);+ this._startResolve = null;+ }+ }++ return this._startPromise;+ }++ async stop() {+ this.log("[WCR] stop()", this.frameCount);++ if (this._stopping) {+ this.log("[WCR] stop() already in progress");+ return;+ }+ this._stopping = true;++ if (!this.running) {+ 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") {+ await this.videoEncoder.flush();+ }+ if (this.audioEncoder && this.audioEncoder.state !== "closed") {+ await this.audioEncoder.flush();+ }+ } catch (err) {+ this.err("[WCR] flush error:", err);+ this.options.onError?.(err);+ }++ try {+ if (this.muxer) {+ await this.muxer.finalize();+ if (this.options.onFinalized) {+ await this.options.onFinalized();+ }+ }+ } catch (err) {+ this.err("[WCR] muxer.finalize:", err);+ this.options.onError?.(err);+ }++ this.cleanup();++ 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;+ }++ resume() {+ if (!this.running || !this.paused) return;++ const nowUs = performance.now() * 1000;+ this.totalPausedDurationUs += nowUs - this.pauseStartUs;++ this.paused = false;+ this.justResumed = true;+ this.muxer?.setPausedOffset(this.totalPausedDurationUs);+ }++ cleanup() {+ this.log("[WCR] cleanup");++ try {+ this.videoReader?.releaseLock();+ } catch {}+ try {+ this.audioReader?.releaseLock();+ } catch {}++ try {+ this.videoEncoder?.close();+ } catch {}+ try {+ this.audioEncoder?.close();+ } 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;++ this.videoTimestampOffsetUs = null;+ this.videoFallbackStartMs = null;+ this.firstVideoFrame = undefined;+ this.selectedVideoCodec = null;++ this.resizeCanvas = null;+ this.resizeCtx = null;+ this._startPromise = null;+ this._startResolve = null;++ this._videoLoopPromise = null;+ this._audioLoopPromise = null;+ }++ async initVideoEncoder(config, codecLabel) {+ this.log("[WCR] initVideoEncoder()", codecLabel);++ this.videoEncoder = new VideoEncoder({+ output: (chunk, meta = {}) => {+ if (this.debug) {+ this.log("[WCR] VIDEO", chunk.type, chunk.timestamp, chunk.duration);+ }+ const normalizedMeta = { ...meta };+ const decoderConfig = normalizedMeta.decoderConfig+ ? { ...normalizedMeta.decoderConfig }+ : {};++ 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) => {+ this.err("[WCR] VideoEncoder error:", err);+ this.options.onError?.(err);+ },+ });++ this.videoEncoder.configure(config);+ this.log("[WCR] VideoEncoder configured");+ }++ async initAudioEncoder(config) {+ if (!config) return;++ this.audioEncoder = new AudioEncoder({+ output: (chunk, meta) => {+ if (this.debug) this.log("[WCR] AUDIO");+ this.muxer.addAudioChunk(chunk, meta);+ },+ error: (err) => {+ this.err("[WCR] AudioEncoder error:", err);+ this.options.onError?.(err);+ },+ });++ this.audioEncoder.configure(config);++ if (this.muxer && this.videoTimestampOffsetUs != null) {+ this.muxer.setAudioOffset(this.videoTimestampOffsetUs);+ }+ }++ 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) {+ this.warn("[WCR] AAC unsupported");+ return null;+ }+ return support.config || candidateConfig;+ } catch {+ this.warn("[WCR] AAC probe failed");+ 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" },+ { 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 };+ try {+ const test = new VideoEncoder({+ output() {},+ error() {},+ });+ test.configure(config);+ test.close();+ this.log("[WCR] Selected encoder:", c.codec, c.hw);+ return {+ config,+ codec: c.codec,+ containerCodec: c.containerCodec,+ };+ } catch (e) {}+ }+ throw new Error("WebCodecsRecorder: No supported H.264 encoder");+ }++ getVideoTimestampUs(frame) {+ if (typeof frame.timestamp === "number") {+ if (this.videoTimestampOffsetUs === null) {+ this.videoTimestampOffsetUs = frame.timestamp;+ }++ const baseUs = Math.max(+ 0,+ Math.round(frame.timestamp - this.videoTimestampOffsetUs)+ );++ return Math.max(0, baseUs - this.totalPausedDurationUs);+ }++ const now = performance.now();+ if (!this._lastFrameTime) this._lastFrameTime = now;+ const delta = now - this._lastFrameTime;+ this._lastFrameTime = now;++ if (delta > 160 && !this._warnedBackpressure) {+ this._warnedBackpressure = true;+ this.warn("[WCR] overload", delta);+ this.options.onError?.({ type: "encoder-overload", delta });+ }+ if (this.videoFallbackStartMs === null) {+ this.videoFallbackStartMs = now;+ }++ const baseUs = Math.max(+ 0,+ Math.round((now - this.videoFallbackStartMs) * 1000)+ );++ return Math.max(0, baseUs - this.totalPausedDurationUs);+ }++ async readVideoLoop() {+ this.log("[WCR] video loop start");+ if (!this.videoReader || !this.videoEncoder) return;++ 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");+ this.log("[WCR] resize canvas created");+ }+ };++ try {+ while (this.running) {+ if (this.paused) {+ await new Promise((r) => setTimeout(r, 10));+ continue;+ }++ const { value: frame, done } = await this.videoReader+ .read()+ .catch(() => ({ done: true }));++ if (done || !frame) break;++ if (frame.codedWidth === 0 || frame.codedHeight === 0) {+ this.warn("[WCR] zero-size frame");+ this.options.onError?.({ type: "video-lost" });+ }++ ensureResizeCanvas();++ this.resizeCtx.drawImage(+ frame,+ 0,+ 0,+ this.targetWidth,+ this.targetHeight+ );++ const resized = new VideoFrame(this.resizeCanvas, {+ timestamp: frame.timestamp,+ });++ const timestampUs = this.getVideoTimestampUs(frame);++ if (this.firstVideoFrame === undefined) {+ this.firstVideoFrame = true;+ this.videoTimestampOffsetUs = frame.timestamp;+ this.log("[WCR] baseline:", this.videoTimestampOffsetUs);++ if (this.muxer)+ this.muxer.setAudioOffset(this.videoTimestampOffsetUs);++ if (!this.audioEncoder && this._pendingAudioConfig) {+ await this.initAudioEncoder(this._pendingAudioConfig);+ await Promise.resolve();+ for (const buf of this._prebufferedAudio) {+ this.audioEncoder.encode(buf);+ buf.close?.();+ }+ this._prebufferedAudio.length = 0;+ this._audioReady = true;+ }+ }++ const needsKey =+ this.frameCount === 0 ||+ this.justResumed ||+ this.firstVideoFrame === true;+ this.justResumed = false;++ this.videoEncoder.encode(resized, { keyFrame: needsKey });+ this.frameCount++;++ resized.close();+ frame.close();+ }+ } catch (err) {+ this.err("[WCR] video loop error:", err);+ }++ 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");++ try {+ while (this.running) {+ const { value: audioData, done } = await this.audioReader+ .read()+ .catch(() => ({ done: true }));++ 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._audioReady) {+ this._prebufferedAudio.push(audioData);+ continue;+ }++ 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);+ }++ this.log("[WCR] audio loop exit");+ }+}diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.jsindex cd5b7b2..4e69ceb 100644--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js@@ -2,13 +2,12 @@* Screenity WebCodecs Recorder* 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.+ * Licensed under the GNU GPLv3.+ * See the LICENSE file for full details.*- * This module implements complex WebCodecs-based recording logic- * for video/audio sync, muxing, and safe timestamp management.- * If you reuse or modify this file in another project, GPLv3- * obligations (including copyleft and attribution) apply.+ * MP4 recording using WebCodecs + MediaBunny.+ * Handles H.264 video, AAC audio, timestamp safety,+ * canvas resizing, pause/resume, and chunked output.*/import { Mp4MuxerWrapper } from "./Mp4MuxerWrapper";@@ -49,6 +48,8 @@ export class WebCodecsRecorder {this.selectedVideoCodec = null;this.firstVideoFrame = undefined;this.frameCount = 0;+ // Monotonic video frame index for safe timestamps+ this._videoFrameIndex = 0;this.resizeCanvas = null;this.resizeCtx = null;@@ -90,6 +91,7 @@ export class WebCodecsRecorder {this.videoTimestampOffsetUs = null;this.firstVideoFrame = undefined;this._stopping = false;+ this._videoFrameIndex = 0;if (this.running) return this._startPromise;@@ -168,11 +170,6 @@ export class WebCodecsRecorder {this._audioReady = false;}- this.videoProcessor = new MediaStreamTrackProcessor({- track: this.videoTrack,- });- this.videoReader = this.videoProcessor.readable.getReader();-if (audioConfig && this.audioTrack) {this.audioProcessor = new MediaStreamTrackProcessor({track: this.audioTrack,@@ -185,6 +182,17 @@ export class WebCodecsRecorder {this._audioLoopPromise = this.readAudioLoop();}+ // Make sure audio encoder is ready before first video chunk+ if (this._pendingAudioConfig) {+ await this.initAudioEncoder(this._pendingAudioConfig);+ this._audioReady = true;+ }++ this.videoProcessor = new MediaStreamTrackProcessor({+ track: this.videoTrack,+ });+ this.videoReader = this.videoProcessor.readable.getReader();+this.log("[WCR] creating muxer...");this.muxer = new Mp4MuxerWrapper({width: this.targetWidth,@@ -359,6 +367,7 @@ export class WebCodecsRecorder {this.videoFallbackStartMs = null;this.firstVideoFrame = undefined;this.selectedVideoCodec = null;+ this._videoFrameIndex = 0;this.resizeCanvas = null;this.resizeCtx = null;@@ -373,22 +382,8 @@ export class WebCodecsRecorder {this.log("[WCR] initVideoEncoder()", codecLabel);this.videoEncoder = new VideoEncoder({- output: (chunk, meta = {}) => {- if (this.debug) {- this.log("[WCR] VIDEO", chunk.type, chunk.timestamp, chunk.duration);- }- const normalizedMeta = { ...meta };- const decoderConfig = normalizedMeta.decoderConfig- ? { ...normalizedMeta.decoderConfig }- : {};-- 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);+ output: (chunk, meta) => {+ this.muxer.addVideoChunk(chunk, meta);},error: (err) => {this.err("[WCR] VideoEncoder error:", err);@@ -415,10 +410,6 @@ export class WebCodecsRecorder {});this.audioEncoder.configure(config);-- if (this.muxer && this.videoTimestampOffsetUs != null) {- this.muxer.setAudioOffset(this.videoTimestampOffsetUs);- }}async prepareAudioEncoderConfig() {@@ -487,42 +478,6 @@ export class WebCodecsRecorder {throw new Error("WebCodecsRecorder: No supported H.264 encoder");}- getVideoTimestampUs(frame) {- if (typeof frame.timestamp === "number") {- if (this.videoTimestampOffsetUs === null) {- this.videoTimestampOffsetUs = frame.timestamp;- }-- const baseUs = Math.max(- 0,- Math.round(frame.timestamp - this.videoTimestampOffsetUs)- );-- return Math.max(0, baseUs - this.totalPausedDurationUs);- }-- const now = performance.now();- if (!this._lastFrameTime) this._lastFrameTime = now;- const delta = now - this._lastFrameTime;- this._lastFrameTime = now;-- if (delta > 160 && !this._warnedBackpressure) {- this._warnedBackpressure = true;- this.warn("[WCR] overload", delta);- this.options.onError?.({ type: "encoder-overload", delta });- }- if (this.videoFallbackStartMs === null) {- this.videoFallbackStartMs = now;- }-- const baseUs = Math.max(- 0,- Math.round((now - this.videoFallbackStartMs) * 1000)- );-- return Math.max(0, baseUs - this.totalPausedDurationUs);- }-async readVideoLoop() {this.log("[WCR] video loop start");if (!this.videoReader || !this.videoEncoder) return;@@ -565,23 +520,28 @@ export class WebCodecsRecorder {this.targetHeight);+ const fps = this.options.fps || 30;+ const durUs = Math.round(1e6 / fps);++ // Monotonic, frame-index-based timestamp to avoid drift/WebCodecs bugs+ const tsUs = this._videoFrameIndex * durUs;+ this._videoFrameIndex++;+const resized = new VideoFrame(this.resizeCanvas, {- timestamp: frame.timestamp,+ timestamp: tsUs,+ duration: durUs, // <-- ADD THIS});- const timestampUs = this.getVideoTimestampUs(frame);-if (this.firstVideoFrame === undefined) {this.firstVideoFrame = true;- this.videoTimestampOffsetUs = frame.timestamp;- this.log("[WCR] baseline:", this.videoTimestampOffsetUs);+ this.frameCount = 0;- if (this.muxer)- this.muxer.setAudioOffset(this.videoTimestampOffsetUs);+ if (this.muxer) {+ this.muxer.setAudioOffset(0);+ }if (!this.audioEncoder && this._pendingAudioConfig) {await this.initAudioEncoder(this._pendingAudioConfig);- await Promise.resolve();for (const buf of this._prebufferedAudio) {this.audioEncoder.encode(buf);buf.close?.();@@ -591,13 +551,16 @@ export class WebCodecsRecorder {}}- const needsKey =- this.frameCount === 0 ||- this.justResumed ||- this.firstVideoFrame === true;+ // Keyframe only first frame and after resume+ const keyFrame = this.frameCount === 0 || this.justResumed;this.justResumed = false;- this.videoEncoder.encode(resized, { keyFrame: needsKey });+ this.videoEncoder.encode(resized, {+ timestamp: tsUs,+ duration: durUs,+ keyFrame,+ });+this.frameCount++;resized.close();@@ -635,8 +598,14 @@ export class WebCodecsRecorder {continue;}+ if (this.firstAudioTs === null) {+ this.firstAudioTs = audioData.timestamp;+ }+try {- this.audioEncoder.encode(audioData);+ this.audioEncoder.encode(audioData, {+ timestamp: audioData.timestamp - this.firstAudioTs,+ });} catch (err) {audioData.close?.();this.options.onError?.(err);