Skip to content

The free and privacy-friendly screen recorder with no limits 🎥

WebCodecs path for Linux + fix download clicks and editor routing race

Alyssa X06f3ad8fe40e
diff --git a/package.json b/package.json
index 4d7510c..e1eed01 100755
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "screenity",
- "version": "4.4.3",
+ "version": "4.4.4",
"description": "The free and privacy-friendly screen recorder with no sign in needed.",
"scripts": {
"build": "cross-env NODE_ENV=development node utils/build.js",
diff --git a/src/manifest.json b/src/manifest.json
index bd42371..5059fe5 100755
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -3,7 +3,7 @@
"name": "__MSG_extName__",
"description": "__MSG_extDesc__",
"default_locale": "en",
- "version": "4.4.3",
+ "version": "4.4.4",
"background": {
"service_worker": "background.bundle.js"
},
diff --git a/src/media/fastRecorderGate.ts b/src/media/fastRecorderGate.ts
index d4f09e4..fd9dc0d 100644
--- a/src/media/fastRecorderGate.ts
+++ b/src/media/fastRecorderGate.ts
@@ -326,6 +326,74 @@ export const probeFastRecorderSupport = async (): Promise<FastRecorderProbeResul
reasons.push("linux-missing-codecs");
}
+ let containerKind: "mp4" | "webm" = "mp4";
+ const mp4Reasons = [...reasons];
+ const mp4Ok = mp4Reasons.length === 0;
+
+ if (!mp4Ok && hasVideoEncoder && hasAudioEncoder) {
+ const webmAudioConfig = {
+ codec: "opus",
+ sampleRate: 48000,
+ numberOfChannels: 2,
+ bitrate: 128000,
+ } as AudioEncoderConfig;
+ const webmVideoBase = {
+ codec: "vp09.00.10.08",
+ width: 1280,
+ height: 720,
+ bitrate: 4_000_000,
+ framerate: 30,
+ bitrateMode: "constant",
+ latencyMode: "realtime",
+ } as VideoEncoderConfig;
+
+ let webmAudioSupport: any = null;
+ try {
+ webmAudioSupport = await AudioEncoder.isConfigSupported(webmAudioConfig);
+ } catch (err) {
+ details.webmAudioError = String(err);
+ }
+
+ let selectedWebmVideo: VideoEncoderConfig | null = null;
+ const webmHwOptions: Array<VideoEncoderConfig["hardwareAcceleration"]> = [
+ "prefer-hardware",
+ "prefer-software",
+ ];
+ for (const hw of webmHwOptions) {
+ try {
+ const candidate: any = { ...webmVideoBase, hardwareAcceleration: hw };
+ const support = await VideoEncoder.isConfigSupported(candidate);
+ if (support?.supported) {
+ selectedWebmVideo = support.config || candidate;
+ break;
+ }
+ } catch {}
+ }
+
+ if (selectedWebmVideo && webmAudioSupport?.supported) {
+ containerKind = "webm";
+ details.containerKind = "webm";
+ details.webmSelectedVideoConfig = selectedWebmVideo;
+ details.webmAudioConfig = webmAudioSupport.config || webmAudioConfig;
+ details.selectedVideoConfig = selectedWebmVideo;
+ details.audioConfig = webmAudioSupport.config || webmAudioConfig;
+ details.videoConfigSupported = true;
+ details.audioConfigSupported = true;
+ details.mp4FallbackReasons = mp4Reasons;
+ const carryOver = new Set([
+ "no-video-encoder",
+ "no-audio-encoder",
+ "no-track-processor",
+ "probe_exception",
+ ]);
+ for (let i = reasons.length - 1; i >= 0; i--) {
+ if (!carryOver.has(reasons[i])) reasons.splice(i, 1);
+ }
+ }
+ }
+
+ if (containerKind === "mp4") details.containerKind = "mp4";
+
const ok = reasons.length === 0;
const at = Date.now();
debugLog("probe result", { ok, reasons, details, at });
@@ -394,7 +462,7 @@ export const validateFastRecorderOutputBlob = async (
reasons.push("blob-too-small");
}
- if (!blob.type || !blob.type.includes("mp4")) {
+ if (!blob.type || !(blob.type.includes("mp4") || blob.type.includes("webm"))) {
reasons.push("unexpected-mime");
}
@@ -436,15 +504,22 @@ export const validateFastRecorderOutputBlob = async (
reasons.push("demuxer-error");
}
- const videoCodec = opts.videoCodec || "avc1.42E01E";
+ const containerKind = blob.type.includes("webm") ? "webm" : "mp4";
+ const defaultVideoCodec = containerKind === "webm" ? "vp9" : "avc1.42E01E";
+ const defaultAudioCodec = containerKind === "webm" ? "opus" : "mp4a.40.2";
+ const videoCodec = opts.videoCodec || defaultVideoCodec;
const audioCodec =
- opts.audioCodec === undefined ? "mp4a.40.2" : opts.audioCodec;
+ opts.audioCodec === undefined ? defaultAudioCodec : opts.audioCodec;
const codecSuffix = audioCodec ? `, ${audioCodec}` : "";
- const mp4Mime = `video/mp4; codecs="${videoCodec}${codecSuffix}"`;
+ const playMime =
+ containerKind === "webm"
+ ? `video/webm; codecs="${videoCodec}${codecSuffix}"`
+ : `video/mp4; codecs="${videoCodec}${codecSuffix}"`;
+ details.containerKind = containerKind;
details.expectedVideoCodec = videoCodec;
details.expectedAudioCodec = audioCodec;
- details.canPlayType = safeCanPlayType(mp4Mime);
- details.mediaSourceSupported = safeMseSupport(mp4Mime);
+ details.canPlayType = safeCanPlayType(playMime);
+ details.mediaSourceSupported = safeMseSupport(playMime);
const hardFail =
reasons.includes("no-blob") ||
diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsx
index b5ee53a..538bff7 100644
--- a/src/pages/Recorder/Recorder.jsx
+++ b/src/pages/Recorder/Recorder.jsx
@@ -322,6 +322,7 @@ const Recorder = () => {
const isTab = useRef(false);
const tabID = useRef(null);
+ const tabIDError = useRef(null);
// Numeric chrome tab id (tabID holds the chromeMediaSourceId from
// tabCapture). Used to query the tab's viewport for aspect-correct
// capture constraints.
@@ -1195,6 +1196,8 @@ const Recorder = () => {
probeResult,
stickyState,
);
+ const recordingExtension =
+ probeResult?.details?.containerKind === "webm" ? "webm" : "mp4";
chunkWriter.current = null;
chunkWriterBackend.current = null;
@@ -1210,7 +1213,10 @@ const Recorder = () => {
let openResult = null;
const wantBackend = shouldUseFast ? "opfs" : "idb";
const prewarmedMatches =
- prewarmedWriter && prewarmedWriter.selection.backend === wantBackend;
+ prewarmedWriter &&
+ prewarmedWriter.selection.backend === wantBackend &&
+ (prewarmedWriter.selection.backend !== "opfs" ||
+ (prewarmedWriter.extension || "mp4") === recordingExtension);
if (prewarmedMatches) {
selection = prewarmedWriter.selection;
openResult = prewarmedWriter.openResult;
@@ -1230,7 +1236,9 @@ const Recorder = () => {
const endOpen = perfSpan("Recorder.preflight writer.open");
try {
if (!openResult) {
- openResult = await selection.writer.open(recordingId);
+ openResult = await selection.writer.open(recordingId, {
+ extension: recordingExtension,
+ });
}
endOpen({ backend: selection.backend, prewarmed: prewarmedMatches });
} catch (openErr) {
@@ -1247,7 +1255,9 @@ const Recorder = () => {
await selection.writer.abort();
} catch {}
selection = await chooseWriter({ preferOpfs: false });
- openResult = await selection.writer.open(recordingId);
+ openResult = await selection.writer.open(recordingId, {
+ extension: recordingExtension,
+ });
} else {
throw openErr;
}
@@ -1288,6 +1298,7 @@ const Recorder = () => {
}
const selectedVideoConfig =
probeResult?.details?.selectedVideoConfig || null;
+ const containerKind = recordingExtension;
await chrome.storage.local.set({
fastRecorderDecision: {
@@ -1639,6 +1650,7 @@ const Recorder = () => {
audioBitrate: hasAudioTrack ? audioBitsPerSecond : undefined,
enableAudio: hasAudioTrack,
videoEncoderConfig: selectedVideoConfig,
+ containerKind,
debug: DEBUG_RECORDER,
onFinalized: async () => {
debug("WebCodecsRecorder onFinalized()");
@@ -2506,8 +2518,17 @@ const Recorder = () => {
prewarmedWriterRef.current = (async () => {
try {
const selection = await chooseWriter({ preferOpfs: true });
- const openResult = await selection.writer.open(recordingId);
- return { selection, openResult, recordingId };
+ let prewarmExt = "mp4";
+ try {
+ const stored = await chrome.storage.local.get(["fastRecorderProbe"]);
+ if (stored?.fastRecorderProbe?.details?.containerKind === "webm") {
+ prewarmExt = "webm";
+ }
+ } catch {}
+ const openResult = await selection.writer.open(recordingId, {
+ extension: prewarmExt,
+ });
+ return { selection, openResult, recordingId, extension: prewarmExt };
} catch {
return null;
}
@@ -3578,7 +3599,13 @@ const Recorder = () => {
debug("Streaming with pre-resolved tabID", tabStreamId);
if (!tabStreamId) {
resetGateState();
- sendRecordingError("Unable to resolve tab stream id", true);
+ const underlying = tabIDError.current
+ ? `: ${tabIDError.current}`
+ : "";
+ sendRecordingError(
+ `Unable to resolve tab stream id${underlying}`,
+ false,
+ );
return;
}
startStream(data, tabStreamId, null, permissions, permissions2);
@@ -3600,33 +3627,54 @@ const Recorder = () => {
const getStreamID = async (id) => {
perfMark("Recorder getStreamID.enter", { tabId: id });
debug("getStreamID()", id);
+ tabIDError.current = null;
let streamId;
- if (IS_OFFSCREEN_HOST) {
- // chrome.tabCapture isn't callable from offscreen; delegate to SW.
- const endOff = perfSpan("Recorder offscreen-request-stream(tab)");
- const response = await chrome.runtime
- .sendMessage({
- type: "offscreen-request-stream",
- mode: "tab",
+ try {
+ if (IS_OFFSCREEN_HOST) {
+ // chrome.tabCapture isn't callable from offscreen; delegate to SW.
+ const endOff = perfSpan("Recorder offscreen-request-stream(tab)");
+ const response = await chrome.runtime
+ .sendMessage({
+ type: "offscreen-request-stream",
+ mode: "tab",
+ targetTabId: id,
+ })
+ .catch((err) => ({ ok: false, error: String(err) }));
+ endOff({ ok: Boolean(response?.ok) });
+ if (!response?.ok || !response.streamId) {
+ debug("Offscreen tab stream acquisition failed", response);
+ tabIDError.current =
+ response?.error || "offscreen-tab-stream-empty-response";
+ return;
+ }
+ streamId = response.streamId;
+ } else {
+ if (!chrome?.tabCapture?.getMediaStreamId) {
+ tabIDError.current = "chrome.tabCapture.getMediaStreamId unavailable";
+ return;
+ }
+ const endTc = perfSpan("Recorder tabCapture.getMediaStreamId");
+ streamId = await chrome.tabCapture.getMediaStreamId({
targetTabId: id,
- })
- .catch((err) => ({ ok: false, error: String(err) }));
- endOff({ ok: Boolean(response?.ok) });
- if (!response?.ok || !response.streamId) {
- debug("Offscreen tab stream acquisition failed", response);
- return;
+ });
+ endTc({ hasStreamId: Boolean(streamId) });
+ if (!streamId) {
+ tabIDError.current = "tabCapture.getMediaStreamId returned empty";
+ return;
+ }
}
- streamId = response.streamId;
- } else {
- const endTc = perfSpan("Recorder tabCapture.getMediaStreamId");
- streamId = await chrome.tabCapture.getMediaStreamId({
- targetTabId: id,
+ debug("Resolved tabCapture streamId", streamId);
+ tabID.current = streamId;
+ } catch (err) {
+ const errStr = String(err?.message || err);
+ debugWarn("getStreamID failed", errStr);
+ tabIDError.current = errStr;
+ } finally {
+ perfMark("Recorder getStreamID.done", {
+ ok: Boolean(tabID.current),
+ error: tabIDError.current,
});
- endTc({ hasStreamId: Boolean(streamId) });
}
- debug("Resolved tabCapture streamId", streamId);
- tabID.current = streamId;
- perfMark("Recorder getStreamID.done");
};
// chromeMediaSource constraints have no "no padding" knob: any combo
@@ -3675,6 +3723,10 @@ const Recorder = () => {
endWait({ attempts, hasId: true });
return tabID.current;
}
+ if (tabIDError.current) {
+ endWait({ attempts, hasId: false, error: tabIDError.current });
+ return null;
+ }
}
endWait({ attempts, hasId: false });
return null;
@@ -3733,7 +3785,9 @@ const Recorder = () => {
isTab.current = request.isTab;
if (request.isTab) {
recordedTabId.current = request.tabID ?? null;
- getStreamID(request.tabID);
+ getStreamID(request.tabID).catch((err) => {
+ tabIDError.current = String(err?.message || err);
+ });
}
} else {
isTab.current = false;
diff --git a/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js b/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
index 217623f..c8ee634 100644
--- a/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
+++ b/src/pages/Recorder/recorderStorage/opfs/opfsChunkWriter.js
@@ -121,18 +121,22 @@ export class OpfsChunkWriter {
});
}
- async open(recordingId) {
+ async open(recordingId, opts = {}) {
if (this._closed || this._aborted) {
throw new Error("opfs-chunk-writer-reused");
}
+ const extension = opts.extension === "webm" ? "webm" : "mp4";
const resp = await this._request(
- { type: "open", recordingId },
+ { type: "open", recordingId, extension },
OPEN_TIMEOUT_MS,
);
+ if (this._aborted || this._closed) {
+ // open reply lands after abort; skip storage write so it doesn't
+ // overwrite a real recording's backendRef.
+ return { backendRef: { backend: "opfs", fileName: resp.fileName || null } };
+ }
this._fileName = resp.fileName || null;
opfsDiag("open-ok", { fileName: this._fileName });
- // Persist immediately so a crash before chunkBackendRef.current syncs
- // still leaves the editor a pointer to the partial OPFS file.
if (this._fileName) {
try {
await chrome.storage.local.set({
@@ -215,6 +219,19 @@ export class OpfsChunkWriter {
async abort() {
if (this._aborted) return;
this._aborted = true;
+ // Clear storage only if it still points at this writer's file; the
+ // next recording may have already claimed it.
+ try {
+ const { lastRecordingBackendRef } = await chrome.storage.local.get([
+ "lastRecordingBackendRef",
+ ]);
+ if (
+ lastRecordingBackendRef?.backend === "opfs" &&
+ lastRecordingBackendRef.fileName === this._fileName
+ ) {
+ await chrome.storage.local.set({ lastRecordingBackendRef: null });
+ }
+ } catch {}
try {
await this._request({ type: "abort" }, ABORT_TIMEOUT_MS);
} catch {} finally {
diff --git a/src/pages/Recorder/recorderStorage/opfs/writerWorker.js b/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
index 0d54f66..fc0387e 100644
--- a/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
+++ b/src/pages/Recorder/recorderStorage/opfs/writerWorker.js
@@ -35,12 +35,13 @@ const post = (payload) => {
self.postMessage(payload);
};
-const openFile = async (recordingId) => {
+const openFile = async (recordingId, extension) => {
// Create NEW before deleting old: if creation fails, the previous
// recording remains intact for recovery, and a recovery editor
// already loading the old file doesn't lose it mid-load.
const dir = await navigator.storage.getDirectory();
- const name = `${FILE_PREFIX}${recordingId}.mp4`;
+ const ext = extension === "webm" ? "webm" : "mp4";
+ const name = `${FILE_PREFIX}${recordingId}.${ext}`;
const handle = await dir.getFileHandle(name, { create: true });
const sync = await handle.createSyncAccessHandle();
sync.truncate(0);
@@ -127,7 +128,7 @@ self.onmessage = (e) => {
if (msg.type === "open") {
enqueue(async () => {
try {
- const { fileName: fn } = await openFile(msg.recordingId);
+ const { fileName: fn } = await openFile(msg.recordingId, msg.extension);
post({ type: "ready", requestId: msg.requestId, ok: true, fileName: fn });
} catch (err) {
post({
diff --git a/src/pages/Recorder/webcodecs/MkvMuxerWrapper.ts b/src/pages/Recorder/webcodecs/MkvMuxerWrapper.ts
new file mode 100644
index 0000000..1df8217
--- /dev/null
+++ b/src/pages/Recorder/webcodecs/MkvMuxerWrapper.ts
@@ -0,0 +1,249 @@
+/*!
+ * Screenity WebCodecs Recorder - WebM/Matroska Muxer Wrapper
+ * Licensed under the GNU GPLv3.
+ */
+// @ts-nocheck
+
+import {
+ Output,
+ WebMOutputFormat,
+ StreamTarget,
+ EncodedVideoPacketSource,
+ EncodedAudioPacketSource,
+ EncodedPacket,
+} from "mediabunny";
+
+interface MkvMuxerWrapperOptions {
+ 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;
+}
+
+const FLUSH_BYTES = 1 * 1024 * 1024;
+const FLUSH_INTERVAL_MS = 1000;
+
+export class MkvMuxerWrapper {
+ private output: Output;
+ private target: StreamTarget;
+
+ private videoSource: EncodedVideoPacketSource;
+ private audioSource: EncodedAudioPacketSource | null = null;
+
+ private videoCodec: string;
+ private audioCodec: string;
+ private started = false;
+
+ private videoTimestampOffsetUs: number | null = null;
+ private audioTimestampOffsetUs: number | null = null;
+ private lastVideoTimestampUs = 0;
+ private lastAudioTimestampUs = 0;
+
+ private _lastDecoderConfig: any = null;
+
+ private _writeBuffer: Uint8Array[] = [];
+ private _writeBufferBytes = 0;
+ private _writeBufferTimer: any = null;
+ private _pendingFlush: Promise<void> = Promise.resolve();
+ private _closed = false;
+
+ private debug: boolean;
+ private log: (...args: any[]) => void;
+ private warn: (...args: any[]) => void;
+ private err: (...args: any[]) => void;
+
+ constructor(private options: MkvMuxerWrapperOptions) {
+ 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.videoCodec = options.videoCodec || "vp9";
+ this.audioCodec = options.audioCodec || "opus";
+
+ // appendOnly so chunks on disk stay playable if finalize() never runs.
+ const writable = new WritableStream<{ data: Uint8Array; position: number; type: string }>({
+ write: (chunk) => {
+ this.bufferWrite(chunk.data);
+ },
+ });
+ this.target = new StreamTarget(writable);
+ this.output = new Output({
+ format: new WebMOutputFormat({
+ appendOnly: true,
+ minimumClusterDuration: 1,
+ }),
+ target: this.target,
+ });
+
+ this.videoSource = new EncodedVideoPacketSource(this.videoCodec);
+ this.output.addVideoTrack(this.videoSource, {
+ frameRate: this.options.fps,
+ });
+ }
+
+ private bufferWrite(data: Uint8Array) {
+ if (this._closed) return;
+ const copy = new Uint8Array(data.byteLength);
+ copy.set(data);
+ this._writeBuffer.push(copy);
+ this._writeBufferBytes += copy.byteLength;
+
+ if (this._writeBufferBytes >= FLUSH_BYTES) {
+ this.flushWriteBuffer();
+ } else if (!this._writeBufferTimer) {
+ this._writeBufferTimer = setTimeout(
+ () => this.flushWriteBuffer(),
+ FLUSH_INTERVAL_MS,
+ );
+ }
+ }
+
+ private flushWriteBuffer() {
+ if (this._writeBufferTimer) {
+ clearTimeout(this._writeBufferTimer);
+ this._writeBufferTimer = null;
+ }
+ if (this._writeBuffer.length === 0) return;
+
+ const parts = this._writeBuffer;
+ const total = this._writeBufferBytes;
+ this._writeBuffer = [];
+ this._writeBufferBytes = 0;
+
+ const merged = new Uint8Array(total);
+ let offset = 0;
+ for (const p of parts) {
+ merged.set(p, offset);
+ offset += p.byteLength;
+ }
+
+ this._pendingFlush = this._pendingFlush
+ .catch(() => {})
+ .then(() => this.emitChunk(merged, null));
+ }
+
+ enableAudio() {
+ if (!this.audioSource) {
+ this.log("[MKV-MUXER] enableAudio");
+ this.audioSource = new EncodedAudioPacketSource(this.audioCodec);
+ this.output.addAudioTrack(this.audioSource);
+ }
+ }
+
+ addVideoChunk(chunk: EncodedVideoChunk, meta: any) {
+ if (meta?.decoderConfig) {
+ this._lastDecoderConfig = meta.decoderConfig;
+ }
+
+ const effectiveMeta =
+ !meta?.decoderConfig && this._lastDecoderConfig
+ ? { ...meta, decoderConfig: this._lastDecoderConfig }
+ : meta;
+
+ const packet = this.buildPacket(chunk, effectiveMeta, "video");
+ return this.videoSource.add(packet, effectiveMeta);
+ }
+
+ 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("[MKV-MUXER] start");
+ await this.output.start();
+ this.started = true;
+ }
+
+ async finalize() {
+ this.log("[MKV-MUXER] finalize");
+ try {
+ await this.output.finalize();
+ } finally {
+ this.flushWriteBuffer();
+ this._closed = true;
+ await this._pendingFlush;
+ }
+ }
+
+ async flushPending() {
+ this.flushWriteBuffer();
+ this._closed = true;
+ await this._pendingFlush;
+ }
+
+ setPausedOffset(_offsetUs: number) {}
+
+ setAudioOffset(offsetUs: number) {
+ this.audioTimestampOffsetUs = offsetUs;
+ }
+
+ private emitChunk(chunk: Uint8Array, timestampUs: number | null) {
+ try {
+ const res = this.options.onChunk?.(chunk, timestampUs ?? null);
+ if (res instanceof Promise) {
+ res.catch((err) => this.err("[MKV-MUXER] onChunk rejected", err));
+ }
+ } catch (err) {
+ this.err("[MKV-MUXER] onChunk error", err);
+ }
+ }
+
+ private buildPacket(
+ chunk: EncodedVideoChunk | EncodedAudioChunk,
+ meta: any,
+ type: "video" | "audio"
+ ) {
+ const data = new Uint8Array(chunk.byteLength);
+ chunk.copyTo(data);
+
+ const tsUs = this.normalizeTimestamp(type, chunk.timestamp, chunk.duration || 0);
+ const durSec = (chunk.duration || 0) / 1e6;
+
+ return new EncodedPacket(data, chunk.type, tsUs / 1e6, durSec);
+ }
+
+ 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;
+ return next;
+ }
+
+ if (typeof timestampUs !== "number") {
+ const fallback = last + durationUs;
+ (this as any)[key] = fallback;
+ this.warn("[MKV-MUX] video missing ts, fallback:", fallback);
+ return fallback;
+ }
+
+ if (this.videoTimestampOffsetUs == null) {
+ this.videoTimestampOffsetUs = timestampUs;
+ }
+
+ const relative = Math.max(
+ 0,
+ timestampUs - this.videoTimestampOffsetUs
+ );
+
+ (this as any)[key] = relative;
+ return relative;
+ }
+}
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
index d721d36..b1e570f 100644
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
@@ -22,16 +22,28 @@ const loadMp4MuxerWrapper = () => {
return _mp4MuxerWrapperPromise;
};
+let _mkvMuxerWrapperPromise = null;
+const loadMkvMuxerWrapper = () => {
+ if (!_mkvMuxerWrapperPromise) {
+ _mkvMuxerWrapperPromise = import("./MkvMuxerWrapper").then(
+ (m) => m.MkvMuxerWrapper,
+ );
+ }
+ return _mkvMuxerWrapperPromise;
+};
+
// Warm the mediabunny muxer chunk before start() so its first import doesn't
// add latency between the countdown ending and the recorder firing.
export function preloadWebCodecsModules() {
loadMp4MuxerWrapper().catch(() => {});
+ loadMkvMuxerWrapper().catch(() => {});
}
export class WebCodecsRecorder {
constructor(stream, options) {
this.stream = stream;
this.options = options;
+ this.containerKind = options.containerKind === "webm" ? "webm" : "mp4";
this.debug = options.debug ?? false;
this.log = (...args) => this.debug && console.log(...args);
@@ -234,10 +246,17 @@ export class WebCodecsRecorder {
try {
const support = await VideoEncoder.isConfigSupported(config);
if (support?.supported) {
+ const effectiveCodec =
+ (support.config || config).codec || overrideConfig.codec || "";
+ let cc;
+ if (/^vp09|^vp9/i.test(effectiveCodec)) cc = "vp9";
+ else if (/^vp08|^vp8/i.test(effectiveCodec)) cc = "vp8";
+ else if (/^av01|^av1/i.test(effectiveCodec)) cc = "av1";
+ else cc = "avc";
videoConfig = {
config: support.config || config,
- codec: (support.config || config).codec || overrideConfig.codec,
- containerCodec: "avc",
+ codec: effectiveCodec,
+ containerCodec: cc,
};
}
} catch (err) {
@@ -292,20 +311,41 @@ export class WebCodecsRecorder {
});
this.videoReader = this.videoProcessor.readable.getReader();
- this.log("[WCR] creating muxer...");
- const endLoadMuxer = perfSpan("WCR.loadMp4MuxerWrapper");
- const Mp4MuxerWrapper = await loadMp4MuxerWrapper();
- endLoadMuxer();
- 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,
- });
+ this.log("[WCR] creating muxer...", this.containerKind);
+ const muxerAudioCodec = this.options.enableAudio
+ ? this.containerKind === "webm"
+ ? "opus"
+ : "aac"
+ : undefined;
+ if (this.containerKind === "webm") {
+ const endLoadMuxer = perfSpan("WCR.loadMkvMuxerWrapper");
+ const MkvMuxerWrapper = await loadMkvMuxerWrapper();
+ endLoadMuxer();
+ this.muxer = new MkvMuxerWrapper({
+ width: this.targetWidth,
+ height: this.targetHeight,
+ fps,
+ videoBitrate: this.options.videoBitrate,
+ audioBitrate: this.options.audioBitrate,
+ videoCodec: videoConfig.containerCodec,
+ audioCodec: muxerAudioCodec,
+ onChunk: this.options.onChunk,
+ });
+ } else {
+ const endLoadMuxer = perfSpan("WCR.loadMp4MuxerWrapper");
+ const Mp4MuxerWrapper = await loadMp4MuxerWrapper();
+ endLoadMuxer();
+ this.muxer = new Mp4MuxerWrapper({
+ width: this.targetWidth,
+ height: this.targetHeight,
+ fps,
+ videoBitrate: this.options.videoBitrate,
+ audioBitrate: this.options.audioBitrate,
+ videoCodec: videoConfig.containerCodec,
+ audioCodec: muxerAudioCodec,
+ onChunk: this.options.onChunk,
+ });
+ }
if (this.options.enableAudio) {
this.muxer.enableAudio();
@@ -717,7 +757,7 @@ export class WebCodecsRecorder {
} catch {}
const candidateConfig = {
- codec: "mp4a.40.2",
+ codec: this.containerKind === "webm" ? "opus" : "mp4a.40.2",
sampleRate,
numberOfChannels,
bitrate: this.options.audioBitrate || 128000,
@@ -726,14 +766,14 @@ export class WebCodecsRecorder {
try {
const support = await AudioEncoder.isConfigSupported(candidateConfig);
if (!support.supported) {
- this.warn("[WCR] AAC unsupported");
+ this.warn("[WCR] audio codec unsupported", candidateConfig.codec);
return null;
}
this.audioSampleRate = support.config?.sampleRate || candidateConfig.sampleRate;
this.audioChannelCount = support.config?.numberOfChannels || numberOfChannels;
return support.config || candidateConfig;
} catch {
- this.warn("[WCR] AAC probe failed");
+ this.warn("[WCR] audio probe failed", candidateConfig.codec);
return null;
}
}
@@ -748,14 +788,21 @@ export class WebCodecsRecorder {
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" },
- ];
+ const candidates =
+ this.containerKind === "webm"
+ ? [
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-hardware" },
+ { codec: "vp09.00.10.08", containerCodec: "vp9", hw: "prefer-software" },
+ { codec: "vp8", containerCodec: "vp8", hw: "prefer-software" },
+ ]
+ : [
+ { 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 };
diff --git a/src/pages/Region/Recorder.jsx b/src/pages/Region/Recorder.jsx
index 3f16e53..957a9c1 100644
--- a/src/pages/Region/Recorder.jsx
+++ b/src/pages/Region/Recorder.jsx
@@ -799,6 +799,8 @@ const Recorder = () => {
);
const selectedVideoConfig =
probeResult?.details?.selectedVideoConfig || null;
+ const containerKind =
+ probeResult?.details?.containerKind === "webm" ? "webm" : "mp4";
let recorderToken = 0;
let codecFallbackTriggered = false;
let webcodecsFallbackTriggered = false;
@@ -925,17 +927,23 @@ const Recorder = () => {
.toString(36)
.slice(2, 8)}`;
try {
+ const regionExt =
+ probeResult?.details?.containerKind === "webm" ? "webm" : "mp4";
let selection = await chooseWriter({ preferOpfs: true });
let openResult = null;
try {
- openResult = await selection.writer.open(writerRecordingId);
+ openResult = await selection.writer.open(writerRecordingId, {
+ extension: regionExt,
+ });
} catch (openErr) {
if (selection.backend === "opfs") {
try {
await selection.writer.abort();
} catch {}
selection = await chooseWriter({ preferOpfs: false });
- openResult = await selection.writer.open(writerRecordingId);
+ openResult = await selection.writer.open(writerRecordingId, {
+ extension: regionExt,
+ });
} else {
throw openErr;
}
@@ -978,6 +986,7 @@ const Recorder = () => {
audioBitrate: hasAudioTrack ? audioBitsPerSecond : undefined,
enableAudio: hasAudioTrack,
videoEncoderConfig: selectedVideoConfig,
+ containerKind,
debug: DEBUG_RECORDER,
onFinalized: async () => {
await waitForDrain();
diff --git a/src/pages/Sandbox/context/ContentState.jsx b/src/pages/Sandbox/context/ContentState.jsx
index 6b7c66a..08c4e3e 100644
--- a/src/pages/Sandbox/context/ContentState.jsx
+++ b/src/pages/Sandbox/context/ContentState.jsx
@@ -763,15 +763,28 @@ const ContentState = (props) => {
elapsedMs: Date.now() - reconstructStartedAt,
type: blob?.type || null,
});
- if (blob.type === "video/mp4") {
+ let isFastWebm = false;
+ if (blob.type === "video/webm") {
+ try {
+ const { lastRecordingBackendRef } = await chrome.storage.local.get([
+ "lastRecordingBackendRef",
+ ]);
+ isFastWebm =
+ lastRecordingBackendRef?.backend === "opfs" &&
+ /\.webm$/i.test(lastRecordingBackendRef?.fileName || "");
+ } catch {}
+ }
+ if (blob.type === "video/mp4" || isFastWebm) {
if (DEBUG_RECORDER)
- console.log("[Screenity][Sandbox] reconstructVideo: fast MP4 path taken", {
+ console.log("[Screenity][Sandbox] reconstructVideo: fast path taken", {
size: blob.size,
+ type: blob.type,
+ isFastWebm,
});
setContentState((prev) => ({
...prev,
blob: blob,
- webm: null,
+ webm: isFastWebm ? blob : null,
mp4ready: true,
ready: true,
rawBlob: blob,
@@ -2899,7 +2912,9 @@ const ContentState = (props) => {
const download = async () => {
// ref: rapid clicks fire before state propagates
const latest = contentStateRef.current || contentState;
- if (latest.isFfmpegRunning || latest.downloading) return;
+ // isFfmpegRunning leaks from background poll handlers and would
+ // intermittently swallow real user clicks; downloading is the real lock.
+ if (latest.downloading) return;
setContentState((prev) => ({
...prev,
@@ -3014,7 +3029,7 @@ const ContentState = (props) => {
};
const downloadWEBM = async () => {
- if (contentState.isFfmpegRunning || contentState.downloadingWEBM) return;
+ if (contentState.downloadingWEBM) return;
const sourceBlob = contentState.blob || contentState.webm;
diff --git a/src/pages/Sandbox/recorderStorage/opfsChunkReader.js b/src/pages/Sandbox/recorderStorage/opfsChunkReader.js
index b8d58c1..9d75f4f 100644
--- a/src/pages/Sandbox/recorderStorage/opfsChunkReader.js
+++ b/src/pages/Sandbox/recorderStorage/opfsChunkReader.js
@@ -130,7 +130,10 @@ export class OpfsChunkReader {
err.code = "opfs-file-too-small";
throw err;
}
- const blob = new Blob([file], { type: "video/mp4" });
+ const isWebm = /\.webm$/i.test(this._fileName || "");
+ const blob = new Blob([file], {
+ type: isWebm ? "video/webm" : "video/mp4",
+ });
return {
blob,
byteSize: blob.size,