Skip to content

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

Audio drifting ahead of video, instant playback for large recordings, and correct scene dimensions on scaled displays

Alyssa Xb8b5a71c254a
diff --git a/package-lock.json b/package-lock.json
index 7337e99..4cde4ae 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "screenity",
- "version": "4.6.2",
+ "version": "4.6.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "screenity",
- "version": "4.6.2",
+ "version": "4.6.4",
"hasInstallScript": true,
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.27.1",
@@ -36,7 +36,7 @@
"gif.js": "^0.2.0",
"jszip": "^3.10.1",
"localforage": "^1.10.0",
- "mediabunny": "^1.46.0",
+ "mediabunny": "^1.51.0",
"patch-package": "^8.0.0",
"plyr": "3.7.8",
"plyr-react": "^5.3.0",
@@ -8992,9 +8992,9 @@
}
},
"node_modules/mediabunny": {
- "version": "1.46.0",
- "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.46.0.tgz",
- "integrity": "sha512-HIaMQ6PVMndrFb9sNCh9/uAsLFBfLzyAgbk3kN7ZqlRRq//9RPYqIOQtXYpkbWBZU8ZJkaAUNLDtwTcdKo7yQw==",
+ "version": "1.51.0",
+ "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.51.0.tgz",
+ "integrity": "sha512-u327374xU8Ho0gCaMII7fUK8t0PnqkabCox1k8uUwvgvGb9o6YQGZEG2Qr4DTe7nTMpzfL7ukgnHDvDROySZ+Q==",
"license": "MPL-2.0",
"workspaces": [
".",
diff --git a/package.json b/package.json
index c065a61..45efc1f 100755
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "screenity",
- "version": "4.6.4",
+ "version": "4.6.5",
"description": "The free and privacy-friendly screen recorder with no sign in needed.",
"scripts": {
"build": "cross-env NODE_ENV=production node utils/build.js",
@@ -18,10 +18,10 @@
"package": "npm run build:prod && cd build && zip -r ../extension.zip . && cd ..",
"package:release": "npm run build:release && cd build && zip -r ../extension.zip . && cd ..",
"build:cws": "npm run build:prod && cd build && zip -r ../build-cws.zip . && cd ..",
- "preflight:cws": "node scripts/cws-preflight.mjs",
+ "preflight:cws": "node scripts/assert-no-dev-env.mjs --require-prod-origin && node scripts/cws-preflight.mjs",
"preflight:cws:bless": "node scripts/cws-preflight.mjs --bless",
"release:cws": "npm run build:cws && npm run preflight:cws && dotenv -e .env.local -- chrome-webstore-upload upload --source build-cws.zip",
- "release:cws:force": "npm run build:cws && node scripts/cws-preflight.mjs --confirm && dotenv -e .env.local -- chrome-webstore-upload upload --source build-cws.zip",
+ "release:cws:force": "npm run build:cws && node scripts/assert-no-dev-env.mjs --require-prod-origin && node scripts/cws-preflight.mjs --confirm && dotenv -e .env.local -- chrome-webstore-upload upload --source build-cws.zip",
"release:cws:publish": "dotenv -e .env.local -- chrome-webstore-upload publish",
"release:cws:publish:10": "dotenv -e .env.local -- chrome-webstore-upload publish --deploy-percentage 10",
"release:cws:publish:50": "dotenv -e .env.local -- chrome-webstore-upload publish --deploy-percentage 50",
@@ -29,7 +29,7 @@
"lint": "eslint src",
"build:content-css": "sass src/pages/Content/styles/app.scss src/pages/Content/styles/app.css --no-source-map",
"check:i18n": "node scripts/check-i18n.mjs",
- "verify:zip": "node scripts/verify-no-secrets.mjs",
+ "verify:zip": "node scripts/verify-no-secrets.mjs && node scripts/assert-no-dev-env.mjs",
"release": "node scripts/release.mjs",
"test:unit": "node --test tests/unit/*.test.mjs",
"test:e2e": "playwright test --config tests/playwright.config.ts",
@@ -64,7 +64,7 @@
"gif.js": "^0.2.0",
"jszip": "^3.10.1",
"localforage": "^1.10.0",
- "mediabunny": "^1.46.0",
+ "mediabunny": "^1.51.0",
"patch-package": "^8.0.0",
"plyr": "3.7.8",
"plyr-react": "^5.3.0",
diff --git a/scripts/assert-no-dev-env.mjs b/scripts/assert-no-dev-env.mjs
new file mode 100644
index 0000000..9b47402
--- /dev/null
+++ b/scripts/assert-no-dev-env.mjs
@@ -0,0 +1,139 @@
+#!/usr/bin/env node
+// Release gate: refuse a build that points at a dev server. build:dev and build:local
+// bake in localhost, and build:local is indistinguishable from a release artifact.
+// Shipping one fails silently: installs, opens, reaches nothing. No --confirm escape.
+// usage: node scripts/assert-no-dev-env.mjs [build-dir] [--require-prod-origin]
+
+import { readFileSync, readdirSync, statSync, existsSync } from "node:fs";
+import { join, dirname } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+const args = process.argv.slice(2);
+const requireProdOrigin = args.includes("--require-prod-origin");
+const BUILD_DIR =
+ args.find((a) => !a.startsWith("--")) || join(__dirname, "..", "build");
+
+const DEV_ORIGIN = /https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?/g;
+
+// .map is deliberately NOT skipped: a production build still emits
+// recorderkeepalive.bundle.js.map, and its sourcesContent carries original
+// source that the minified bundle no longer shows. verify-no-secrets scans
+// maps for the same reason.
+const SKIP_EXTENSIONS = new Set([
+ ".png", ".jpg", ".jpeg", ".gif", ".svg", ".woff", ".woff2", ".ttf",
+ ".otf", ".ico", ".mp3", ".mp4", ".webm", ".wasm", ".bin",
+]);
+
+const walk = (dir) => {
+ const out = [];
+ for (const name of readdirSync(dir)) {
+ const path = join(dir, name);
+ if (statSync(path).isDirectory()) out.push(...walk(path));
+ else out.push(path);
+ }
+ return out;
+};
+
+if (!existsSync(BUILD_DIR)) {
+ console.error(`FAIL: ${BUILD_DIR} missing. Build first.`);
+ process.exit(1);
+}
+
+const findings = [];
+
+for (const file of walk(BUILD_DIR)) {
+ const ext = file.slice(file.lastIndexOf("."));
+ if (SKIP_EXTENSIONS.has(ext)) continue;
+
+ let contents;
+ try {
+ contents = readFileSync(file, "utf8");
+ } catch {
+ continue;
+ }
+
+ const matches = [...new Set(contents.match(DEV_ORIGIN) || [])];
+ if (matches.length) {
+ findings.push({
+ file: file.slice(BUILD_DIR.length + 1),
+ matches: matches.slice(0, 5),
+ });
+ }
+}
+
+// dev builds and build:local keep localhost in externally_connectable, prod strips it.
+// Second independent read: a bundle scan alone misses a build whose env was
+// empty, not local.
+const manifestPath = join(BUILD_DIR, "manifest.json");
+let prodOrigins = [];
+if (existsSync(manifestPath)) {
+ try {
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
+ const matches = manifest.externally_connectable?.matches || [];
+ const devMatches = matches.filter(
+ (m) => m.includes("localhost") || m.includes("127.0.0.1"),
+ );
+ if (devMatches.length) {
+ findings.push({
+ file: "manifest.json",
+ matches: devMatches,
+ note: "externally_connectable still lists dev origins",
+ });
+ }
+ prodOrigins = matches
+ .filter((m) => m.startsWith("https://"))
+ .map((m) => m.replace(/\/\*$/, ""));
+ } catch {
+ console.error("FAIL: build/manifest.json is not valid JSON.");
+ process.exit(1);
+ }
+}
+
+if (findings.length) {
+ console.error(`Dev-server references found in ${BUILD_DIR}:\n`);
+ for (const f of findings) {
+ console.error(` ${f.file}`);
+ if (f.note) console.error(` ${f.note}`);
+ console.error(` ${f.matches.join(", ")}`);
+ }
+ console.error(
+ "\nThis build must NOT be released. Rebuild with `npm run build`" +
+ " (or `build:release` for the self-hosted zip).",
+ );
+ process.exit(1);
+}
+
+// CWS only. Absence of dev origins isn't proof of wiring: an env file that
+// failed to load leaves no localhost to find either. Self-hosted has no app
+// base by design, hence opt-in.
+if (requireProdOrigin) {
+ if (!prodOrigins.length) {
+ console.error(
+ "FAIL: manifest lists no https origin to check the bundles against.",
+ );
+ process.exit(1);
+ }
+ const bundles = walk(BUILD_DIR).filter((f) => f.endsWith(".js"));
+ const found = prodOrigins.filter((origin) =>
+ bundles.some((f) => {
+ try {
+ return readFileSync(f, "utf8").includes(origin);
+ } catch {
+ return false;
+ }
+ }),
+ );
+ if (!found.length) {
+ console.error(
+ `FAIL: none of the production origins (${prodOrigins.join(", ")}) appear` +
+ " in any bundle.\nThe env almost certainly failed to load; this build" +
+ " points at nothing. Rebuild with `npm run build`.",
+ );
+ process.exit(1);
+ }
+ console.log(`Production origin baked in: ${found.join(", ")}`);
+}
+
+console.log(`No dev-server references in ${BUILD_DIR}.`);
+process.exit(0);
diff --git a/scripts/release.mjs b/scripts/release.mjs
index be0c870..d5aaf6f 100755
--- a/scripts/release.mjs
+++ b/scripts/release.mjs
@@ -122,6 +122,10 @@ console.log("Verifying build/ for secret leaks...");
sh("node scripts/verify-no-secrets.mjs");
console.log("");
+console.log("Verifying build/ has no dev-server references...");
+sh("node scripts/assert-no-dev-env.mjs");
+console.log("");
+
console.log("Creating build.zip...");
try {
sh("rm -f build.zip");
diff --git a/src/manifest.json b/src/manifest.json
index 817d4b0..cc08fc4 100755
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -3,7 +3,7 @@
"name": "__MSG_extName__",
"description": "__MSG_extDesc__",
"default_locale": "en",
- "version": "4.6.4",
+ "version": "4.6.5",
"background": {
"service_worker": "background.bundle.js"
},
@@ -39,6 +39,7 @@
"worker.js",
"vendor/*",
"cloudrecorder.html",
+ "localplaybackbridge.html",
"recorder.html",
"offscreenrecorder.html",
"wrapper.html",
diff --git a/src/pages/Background/alarms/handleAlarm.js b/src/pages/Background/alarms/handleAlarm.js
index 8de0ee4..adb0454 100644
--- a/src/pages/Background/alarms/handleAlarm.js
+++ b/src/pages/Background/alarms/handleAlarm.js
@@ -6,6 +6,8 @@ import { sweepRecorderTabs } from "../recording/sweepRecorderTabs.js";
import { diagEvent } from "../../utils/diagnosticLog";
import { lifecycle } from "../../utils/lifecycleLog";
import { chunksStore } from "../recording/chunkHandler";
+import { openExistingChunksStore } from "../../CloudRecorder/recorderStorage/chooseChunksStore";
+import { destroySessionDir } from "../../CloudRecorder/recorderStorage/opfsKvStore";
import { emitRecordingTelemetry } from "../recording/emitRecordingTelemetry";
import { markFastRecorderFailure } from "../../../media/fastRecorderGate";
import {
@@ -423,13 +425,54 @@ export const handleAlarm = async (alarm) => {
return;
}
- await chunksStore.clear().catch((err) => {
+ // Clearing IDB unconditionally left OPFS sessions on disk. Survivable at 250MB
+ // offers, a whole-recording leak now, so route to the backend the writer used.
+ const expiredStore =
+ localPlaybackOffer.storageBackend === "opfs" &&
+ localPlaybackOffer.opfsSessionId
+ ? openExistingChunksStore({
+ sessionId: localPlaybackOffer.opfsSessionId,
+ track: "screen",
+ backend: "opfs",
+ }).store
+ : chunksStore;
+ await expiredStore.clear().catch((err) => {
console.warn(
- "[Screenity][BG] Failed to clear chunksStore for local playback expiry",
+ "[Screenity][BG] Failed to clear chunk store for local playback expiry",
err,
);
});
+ if (
+ localPlaybackOffer.storageBackend === "opfs" &&
+ localPlaybackOffer.opfsSessionId
+ ) {
+ await destroySessionDir(localPlaybackOffer.opfsSessionId).catch(() => {});
+ }
await chrome.storage.local.remove([CLOUD_LOCAL_PLAYBACK_KEY]);
+ // Still "available" at TTL means the editor never collected it, the one
+ // outcome nothing else reports.
+ if (localPlaybackOffer.status !== "used") {
+ void emitRecordingTelemetry("local_playback_outcome", {
+ status: "expired",
+ reason: localPlaybackOffer.status || "available",
+ recordingSessionId: localPlaybackOffer.recordingSessionId || null,
+ projectId: localPlaybackOffer.projectId || null,
+ sceneId: localPlaybackOffer.sceneId || null,
+ mediaId: localPlaybackOffer.mediaId || null,
+ trackType: "screen",
+ offerId: localPlaybackOffer.offerId,
+ partial: Boolean(localPlaybackOffer.partial),
+ chunkCount: localPlaybackOffer.chunkCount || null,
+ availableBytes: localPlaybackOffer.estimatedBytes || null,
+ totalBytes: localPlaybackOffer.totalBytes || null,
+ elapsedMs: Number(localPlaybackOffer.createdAt)
+ ? Date.now() - Number(localPlaybackOffer.createdAt)
+ : null,
+ storageBackend: localPlaybackOffer.storageBackend || null,
+ container: localPlaybackOffer.container || null,
+ encoderKind: localPlaybackOffer.encoderKind || null,
+ });
+ }
await chrome.storage.local.set({
[CLOUD_LOCAL_PLAYBACK_EVENT_KEY]: {
event: "offer-expired",
diff --git a/src/pages/Background/auth/loginWithWebsite.js b/src/pages/Background/auth/loginWithWebsite.js
index c9b6c45..0e9e8e8 100644
--- a/src/pages/Background/auth/loginWithWebsite.js
+++ b/src/pages/Background/auth/loginWithWebsite.js
@@ -230,6 +230,12 @@ export const loginWithWebsite = async (arg = {}) => {
instantMode: instantMode || false,
};
} catch (err) {
+ console.warn(
+ "[Screenity][Auth] verify failed:",
+ err?.name,
+ err?.message,
+ `tokenInvalid=${isTokenInvalid}`,
+ );
if (isTokenInvalid) {
if (_depth >= MAX_REFRESH_DEPTH) {
console.warn(
diff --git a/src/pages/Background/lifecycleObserver.js b/src/pages/Background/lifecycleObserver.js
index daeec10..e8a1cb9 100644
--- a/src/pages/Background/lifecycleObserver.js
+++ b/src/pages/Background/lifecycleObserver.js
@@ -2,6 +2,7 @@
// recording-state keys and logs them to lifecycleLog so we don't have
// to instrument each BG call site. wired once at SW startup.
import { lifecycle } from "../utils/lifecycleLog";
+import { handleTabActivation } from "./listeners/onTabActivatedListener";
// Keys whose transitions we care about for cross-recording bug analysis.
// `lifecycleLog` itself is excluded to avoid infinite recursion.
@@ -81,6 +82,19 @@ export const initLifecycleObserver = () => {
} catch (err) {
console.warn("[Screenity][BG] reactive setIcon failed:", err);
}
+
+ // Hand the UI to the focused tab. onActivated has no branch for the
+ // countdown window, so a tab switched to mid-countdown never got it.
+ if (changes.recording.newValue === true) {
+ try {
+ chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
+ const tabId = tabs?.[0]?.id;
+ if (tabId) void handleTabActivation({ tabId });
+ });
+ } catch (err) {
+ console.warn("[Screenity][BG] recording-start UI handoff failed:", err);
+ }
+ }
}
});
};
diff --git a/src/pages/Background/listeners/onMessageExternalListener.js b/src/pages/Background/listeners/onMessageExternalListener.js
index ae9d4a0..63c8d7c 100644
--- a/src/pages/Background/listeners/onMessageExternalListener.js
+++ b/src/pages/Background/listeners/onMessageExternalListener.js
@@ -34,11 +34,23 @@ export const onMessageExternalListener = () => {
showProSplash: false,
});
// otherwise the drain listener clobbers this fresh token on next recording-end
- await chrome.storage.local.remove(["logoutPendingTokenClear"]);
+ await chrome.storage.local.remove([
+ "logoutPendingTokenClear",
+ "loginPendingAt",
+ "loginTabId",
+ ]);
+
+ // loginWithWebsite() consumes and clears originalTabId, so read it first
+ // or the close below is unreachable and every login leaks its tab.
+ const { originalTabId: loginOpenedFromTabId } =
+ await chrome.storage.local.get("originalTabId");
const auth = await loginWithWebsite();
if (!auth?.authenticated) {
+ console.warn(
+ "[Screenity][Auth] AUTH_SUCCESS token did not verify, staying logged out",
+ );
await chrome.storage.local.set({
isLoggedIn: false,
});
@@ -56,24 +68,12 @@ export const onMessageExternalListener = () => {
lastAuthCheck: Date.now(),
});
- const { originalTabId } = await chrome.storage.local.get(
- "originalTabId"
- );
-
- if (originalTabId) {
- chrome.tabs.update(originalTabId, { active: true });
- sendMessageTab(originalTabId, { type: "LOGIN_SUCCESS" });
-
- if (sender.tab?.id) {
- chrome.tabs.remove(sender.tab.id);
- }
-
- await chrome.storage.local.remove("originalTabId");
-
- sendMessageTab(originalTabId, {
- type: "check-auth",
- senderId: sender.tab.id,
- });
+ // loginWithWebsite does the refocus, but only this scope knows the
+ // sender, so the close lives here. Gated so a hand-opened /login stays put.
+ if (loginOpenedFromTabId && sender.tab?.id) {
+ try {
+ await chrome.tabs.remove(sender.tab.id);
+ } catch {}
}
return true;
diff --git a/src/pages/Background/listeners/onTabUpdatedListener.js b/src/pages/Background/listeners/onTabUpdatedListener.js
index d86221f..111cb41 100644
--- a/src/pages/Background/listeners/onTabUpdatedListener.js
+++ b/src/pages/Background/listeners/onTabUpdatedListener.js
@@ -7,41 +7,72 @@ const CLOUD_FEATURES_ENABLED =
const APP_BASE = process.env.SCREENITY_APP_BASE;
// debounce in-tab triggers so navigation chatter doesn't hammer /auth/refresh
const APP_AUTH_REFRESH_DEBOUNCE_MS = 10_000;
+// A login the user just started is worth polling harder, but /auth/refresh
+// allows 5 per user per minute, so stay under that or the retries 429 us out.
+const LOGIN_PENDING_DEBOUNCE_MS = 15_000;
+const LOGIN_PENDING_TTL_MS = 15 * 60_000;
let lastAppAuthRefreshAt = 0;
const tryAppAuthRefresh = async (url) => {
if (!CLOUD_FEATURES_ENABLED || !APP_BASE || !url) return;
if (!url.startsWith(APP_BASE)) return;
const now = Date.now();
- if (now - lastAppAuthRefreshAt < APP_AUTH_REFRESH_DEBOUNCE_MS) return;
const {
isLoggedIn,
wasLoggedIn,
hasSubscribedBefore,
screenityUser,
stayLoggedOut,
+ loginPendingAt,
+ loginTabId,
} = await chrome.storage.local.get([
"isLoggedIn",
"wasLoggedIn",
"hasSubscribedBefore",
"screenityUser",
"stayLoggedOut",
+ "loginPendingAt",
+ "loginTabId",
]);
if (isLoggedIn) return;
if (stayLoggedOut) return;
+ const loginPending =
+ Boolean(loginPendingAt) && now - loginPendingAt < LOGIN_PENDING_TTL_MS;
+ if (loginPendingAt && !loginPending) {
+ await chrome.storage.local.remove(["loginPendingAt", "loginTabId"]);
+ }
// Only refresh if there's prior evidence of an account, otherwise every
// logged-out visitor hits /auth/refresh and 401s. Mirrors the priorSignals
// gate in loginWithWebsite (force:true would bypass it).
const hasPriorSignals =
+ loginPending ||
Boolean(wasLoggedIn) ||
Boolean(hasSubscribedBefore) ||
Boolean(screenityUser);
if (!hasPriorSignals) return;
+ const debounceMs = loginPending
+ ? LOGIN_PENDING_DEBOUNCE_MS
+ : APP_AUTH_REFRESH_DEBOUNCE_MS;
+ if (now - lastAppAuthRefreshAt < debounceMs) return;
lastAppAuthRefreshAt = now;
- loginWithWebsite({ force: true }).catch(() => {});
+ const result = await loginWithWebsite({ force: true }).catch(() => null);
+ if (!result?.authenticated || !loginPending) return;
+ // Cookie refresh got there without AUTH_SUCCESS. loginWithWebsite already
+ // refocused the original tab, so all that's left is the login tab we opened.
+ await chrome.storage.local.remove(["loginPendingAt", "loginTabId"]);
+ if (loginTabId) {
+ try {
+ await chrome.tabs.remove(loginTabId);
+ } catch {}
+ }
};
export const handleTabUpdate = async (tabId, changeInfo, tab) => {
try {
+ // The app routes client-side, so a post-login redirect fires url without
+ // ever reaching status complete.
+ if (changeInfo.url) {
+ tryAppAuthRefresh(changeInfo.url).catch(() => {});
+ }
if (changeInfo.status === "complete") {
const {
recording,
diff --git a/src/pages/Background/messaging/handlers.js b/src/pages/Background/messaging/handlers.js
index 42eba38..ec9cc88 100644
--- a/src/pages/Background/messaging/handlers.js
+++ b/src/pages/Background/messaging/handlers.js
@@ -34,8 +34,6 @@ import {
sendMessageTab,
parseEditorTargetUrl,
resolveEditorTabForTarget,
- getValidatedEditorTab,
- setEditorTabReference,
} from "../tabManagement";
import {
handleRestart,
@@ -58,6 +56,7 @@ import {
CLOUD_LOCAL_PLAYBACK_EVENT_KEY,
CLOUD_LOCAL_PLAYBACK_ALARM,
} from "../recording/cloudLocalPlaybackConstants";
+import { emitRecordingTelemetry } from "../recording/emitRecordingTelemetry";
import { FIRST_CHUNK_WATCHDOG_ALARM, RECORDER_KEEPALIVE_ALARM } from "../alarms/alarmConstants";
import { desktopCapture } from "../recording/desktopCapture";
import {
@@ -208,8 +207,15 @@ const shouldShowReviewPrompt = async () => {
};
const STOP_RECORDING_TAB_DEBOUNCE_MS = 1200;
-const CLOUD_LOCAL_PLAYBACK_MAX_BYTES = 250 * 1024 * 1024;
-const CLOUD_LOCAL_PLAYBACK_MAX_CHUNKS = 4000;
+// Shared by concurrent unforced check-auth-status calls; see that handler.
+let inFlightUnforcedAuth = null;
+// Sanity backstop, not a transport limit: the bridge iframe hands the editor a Blob by
+// reference. Kept in step with LOCAL_SCREEN_PLAYBACK_MAX_BYTES in CloudRecorder.
+const CLOUD_LOCAL_PLAYBACK_MAX_BYTES = 8 * 1024 * 1024 * 1024;
+const CLOUD_LOCAL_PLAYBACK_MAX_CHUNKS = 20000;
+// Once the editor holds its own Blob our copy is dead weight, and it can now be the whole
+// recording rather than 250MB. Short TTL, but enough for an editor reload to re-request.
+const CLOUD_LOCAL_PLAYBACK_POST_USE_TTL_MS = 2 * 60 * 1000;
const CLOUD_LOCAL_PLAYBACK_MIN_TTL_MS = 60 * 1000;
const CLOUD_LOCAL_PLAYBACK_MAX_TTL_MS = 24 * 60 * 60 * 1000;
let stopRecordingTabInFlight = false;
@@ -243,6 +249,13 @@ const normalizeLocalPlaybackOffer = (offer = {}) => {
);
const estimatedBytes = Math.max(0, Number(offer.estimatedBytes) || 0);
const createdAt = Number(offer.createdAt) || now;
+ // Partial = only the leading chunks that survived mid-recording purging.
+ // totalBytes = finished upload size, so the editor knows what's missing.
+ const partial = Boolean(offer.partial);
+ const totalBytes = Math.max(
+ estimatedBytes,
+ Number(offer.totalBytes) || 0,
+ );
// storageBackend / opfsSessionId let the read-chunk + clear handlers route
// to the same backend the writer used. Older offers without these fields
@@ -270,6 +283,9 @@ const normalizeLocalPlaybackOffer = (offer = {}) => {
status: offer.status || "available",
chunkCount,
estimatedBytes,
+ partial,
+ availableBytes: estimatedBytes,
+ totalBytes,
mediaId: offer.mediaId || null,
bunnyVideoId: offer.bunnyVideoId || null,
storageBackend,
@@ -296,6 +312,41 @@ const offerScreenStore = (offer) => {
return chunksStore;
};
+// `status` and `reason` ride fields the server sanitizer already keeps, so this is
+// legible in prod before richer fields get allowlisted.
+const reportLocalPlaybackOutcome = (outcome, detail = {}) => {
+ const offer = detail.offer || null;
+ const now = Date.now();
+ const createdAt = Number(offer?.createdAt) || 0;
+ void emitRecordingTelemetry("local_playback_outcome", {
+ status: outcome,
+ reason: detail.reason || null,
+ recordingSessionId:
+ detail.recordingSessionId || offer?.recordingSessionId || null,
+ projectId: detail.projectId || offer?.projectId || null,
+ sceneId: detail.sceneId || offer?.sceneId || null,
+ mediaId: detail.mediaId || offer?.mediaId || null,
+ trackType: "screen",
+ offerId: detail.offerId || offer?.offerId || null,
+ partial:
+ typeof detail.partial === "boolean"
+ ? detail.partial
+ : Boolean(offer?.partial),
+ chunkCount: detail.chunkCount ?? offer?.chunkCount ?? null,
+ availableBytes:
+ detail.availableBytes ?? offer?.availableBytes ?? offer?.estimatedBytes ?? null,
+ totalBytes: detail.totalBytes ?? offer?.totalBytes ?? null,
+ localBytes: detail.localBytes ?? null,
+ // Time from offer creation to pickup (or give-up).
+ elapsedMs: createdAt ? now - createdAt : null,
+ storageBackend: detail.storageBackend || offer?.storageBackend || null,
+ container: detail.container || offer?.container || null,
+ encoderKind: detail.encoderKind || offer?.encoderKind || null,
+ purgeMode: detail.purgeMode || null,
+ transport: detail.transport || null,
+ });
+};
+
const isLocalPlaybackOfferExpired = (offer) =>
!offer || Number(offer.expiresAt || 0) <= Date.now();
@@ -988,35 +1039,20 @@ export const setupHandlers = () => {
clearInterval(keepAlive);
}
- // Editor-tab proxy fallback (the original path).
- let validated = await getValidatedEditorTab({
+ // The guarded resolver makes a concurrent open reuse the same in-flight tab
+ // instead of racing a second create.
+ const targetUrl = `${process.env.SCREENITY_APP_BASE}/editor/${projectId}/edit?load=true`;
+ const resolved = await resolveEditorTabForTarget({
+ targetUrl,
expectedProjectId: projectId,
expectedKind: "editor",
reason: "forward-create-scene",
+ focus: false,
});
- if (!validated.ok || !validated.tab?.id) {
- const targetUrl = `${process.env.SCREENITY_APP_BASE}/editor/${projectId}/edit?load=true`;
- try {
- const tab = await chrome.tabs.create({ url: targetUrl, active: false });
- if (tab?.id) {
- await setEditorTabReference({
- tabId: tab.id,
- tabUrl: targetUrl,
- source: "forward-create-scene:auto-open",
- expectedProjectId: projectId,
- });
- validated = { ok: true, tab: { id: tab.id }, reason: null };
- }
- } catch (err) {
- return {
- ok: false,
- error: `failed-to-open-editor-tab:${err?.message || err}`,
- };
- }
- if (!validated.tab?.id) {
- return { ok: false, error: "no-editor-tab" };
- }
+ if (!resolved?.tabId) {
+ return { ok: false, error: "no-editor-tab" };
}
+ const validated = { ok: true, tab: { id: resolved.tabId } };
const requestId =
(typeof crypto !== "undefined" && crypto.randomUUID?.()) ||
`scene-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`;
@@ -1199,19 +1235,24 @@ export const setupHandlers = () => {
}
try {
// deterministic timeout so a wedged offscreen can't hang the caller
- // forever. WebM is a full re-encode (minutes on large files), so it gets
- // a far longer ceiling than the packet-copy remux; the editor's
+ // forever. WebM and mp4x are full re-encodes (minutes on large files), so
+ // they get a far longer ceiling than the packet-copy remux; the editor's
// progress-reset stall guard catches a genuinely wedged conversion first.
- const isWebm = message.kind === "webm";
- const TIMEOUT_MS = isWebm ? 30 * 60_000 : 60_000;
+ const START_TYPE_BY_KIND = {
+ webm: "webm-start",
+ mp4x: "mp4x-start",
+ };
+ const isReencode = message.kind === "webm" || message.kind === "mp4x";
+ const TIMEOUT_MS = isReencode ? 30 * 60_000 : 60_000;
let timeoutId = null;
try {
const response = await Promise.race([
chrome.runtime.sendMessage({
- type: isWebm ? "webm-start" : "remux-start",
+ type: START_TYPE_BY_KIND[message.kind] || "remux-start",
requestId: message.requestId,
inputFileName: message.inputFileName,
outputFileName: message.outputFileName,
+ videoBitrate: message.videoBitrate,
}),
new Promise((_, reject) => {
timeoutId = setTimeout(
@@ -1798,7 +1839,19 @@ export const setupHandlers = () => {
// The popup mount opts out so a fresh install doesn't silently revive
// auth from a leftover website cookie (it would flash the paid welcome
// screen at a returning user who should just see "Log in").
- return await loginWithWebsite({ force: message?.force !== false });
+ const force = message?.force !== false;
+ if (force) return await loginWithWebsite({ force: true });
+ // Every content script sends this on mount, so an install/update backfill
+ // fires one per open tab at once; they all read `lastAuthCheck` before any
+ // writes it, so each would hit /auth/verify separately. Share one run.
+ // Scoped to this handler: AUTH_SUCCESS also calls loginWithWebsite
+ // unforced and must not join a run that started before the login cookie landed.
+ if (!inFlightUnforcedAuth) {
+ inFlightUnforcedAuth = loginWithWebsite({ force: false }).finally(() => {
+ inFlightUnforcedAuth = null;
+ });
+ }
+ return await inFlightUnforcedAuth;
},
);
registerMessage(
@@ -1840,10 +1893,16 @@ export const setupHandlers = () => {
if (currentTab?.id) {
await chrome.storage.local.set({ originalTabId: currentTab.id });
}
- chrome.tabs.create({
+ const loginTab = await chrome.tabs.create({
url: `${process.env.SCREENITY_APP_BASE}/login?extension=true`,
active: true,
});
+ // Marks an explicitly-started login so the tab-update fallback can finish it
+ // if AUTH_SUCCESS never arrives. Fresh installs have no other prior signal.
+ await chrome.storage.local.set({
+ loginPendingAt: Date.now(),
+ loginTabId: loginTab?.id ?? null,
+ });
});
registerMessage("handle-logout", async (message, sender, sendResponse) => {
if (!CLOUD_FEATURES_ENABLED) {
@@ -1863,6 +1922,8 @@ export const setupHandlers = () => {
"isSubscribed",
"isLoggedIn",
"proSubscription",
+ "loginPendingAt",
+ "loginTabId",
];
if (!recordingBusy) {
removeKeys.push("screenityToken");
@@ -1986,8 +2047,21 @@ export const setupHandlers = () => {
// serialize to avoid read-modify-write race losing clicks; cap array for long recordings
const CLICK_EVENTS_MAX = 5000;
+ // Per-click writes cost the recorded page ~5ms of deserialize each (storage.local
+ // broadcasts the whole array to every content script in every tab). Batch instead.
+ const CLICK_FLUSH_MS = 2000;
let _clickWriteQueue = Promise.resolve();
- function storeClick(click) {
+ let _pendingClicks = [];
+ let _clickFlushTimer = null;
+
+ function flushClicks() {
+ if (_clickFlushTimer) {
+ clearTimeout(_clickFlushTimer);
+ _clickFlushTimer = null;
+ }
+ if (_pendingClicks.length === 0) return _clickWriteQueue;
+ const batch = _pendingClicks;
+ _pendingClicks = [];
_clickWriteQueue = _clickWriteQueue
.catch(() => {})
.then(async () => {
@@ -1998,7 +2072,7 @@ export const setupHandlers = () => {
const { clickEvents = [] } = await chrome.storage.local.get({
clickEvents: [],
});
- const next = clickEvents.concat(click);
+ const next = clickEvents.concat(batch);
if (next.length > CLICK_EVENTS_MAX) {
next.splice(0, next.length - CLICK_EVENTS_MAX);
}
@@ -2011,8 +2085,26 @@ export const setupHandlers = () => {
} catch {
}
});
+ return _clickWriteQueue;
}
+ function storeClick(click) {
+ _pendingClicks.push(click);
+ // Trailing edge only: a leading write puts the expensive broadcast back on the
+ // first click of every burst, which is the case that hurts.
+ if (!_clickFlushTimer) {
+ _clickFlushTimer = setTimeout(flushClicks, CLICK_FLUSH_MS);
+ }
+ }
+
+ // Flush on stop so clickEvents is complete when the recorder reads it at scene
+ // creation, and no click from this recording leaks into the next one's array.
+ chrome.storage.onChanged.addListener((changes, area) => {
+ if (area !== "local" || !changes.recording) return;
+ if (changes.recording.newValue) return;
+ flushClicks();
+ });
+
function getMonitorForWindow(message, sender, sendResponse) {
chrome.system.display.getInfo((displays) => {
chrome.windows.getCurrent((win) => {
@@ -2326,6 +2418,9 @@ export const setupHandlers = () => {
trackType: "screen",
chunkCount: localPlaybackOffer.chunkCount,
estimatedBytes: localPlaybackOffer.estimatedBytes,
+ partial: Boolean(localPlaybackOffer.partial),
+ availableBytes: localPlaybackOffer.availableBytes || null,
+ totalBytes: localPlaybackOffer.totalBytes || null,
expiresAt: localPlaybackOffer.expiresAt,
source: localPlaybackOffer.source || "indexeddb-screen-chunks",
mediaId: localPlaybackOffer.mediaId || null,
@@ -2355,6 +2450,10 @@ export const setupHandlers = () => {
return { ok: false, error: "missing-local-screen-bytes" };
}
if (normalizedOffer.estimatedBytes > CLOUD_LOCAL_PLAYBACK_MAX_BYTES) {
+ reportLocalPlaybackOutcome("skipped", {
+ offer: normalizedOffer,
+ reason: "offer-too-large",
+ });
return {
ok: false,
error: "offer-too-large",
@@ -2383,11 +2482,29 @@ export const setupHandlers = () => {
sceneId: normalizedOffer.sceneId,
chunkCount: normalizedOffer.chunkCount,
estimatedBytes: normalizedOffer.estimatedBytes,
+ partial: normalizedOffer.partial,
expiresAt: normalizedOffer.expiresAt,
});
+ reportLocalPlaybackOutcome("registered", { offer: normalizedOffer });
return { ok: true, offer: normalizedOffer };
});
+ // Outcomes the recorder sees before an offer exists, so prod can distinguish
+ // "no offer was made" from "an offer went unused".
+ registerMessage("cloud-local-playback-report", async (message) => {
+ reportLocalPlaybackOutcome(message?.outcome || "skipped", {
+ reason: message?.reason || null,
+ recordingSessionId: message?.recordingSessionId || null,
+ projectId: message?.projectId || null,
+ sceneId: message?.sceneId || null,
+ partial: message?.partial,
+ chunkCount: message?.chunkCount ?? null,
+ availableBytes: message?.availableBytes ?? null,
+ totalBytes: message?.totalBytes ?? null,
+ purgeMode: message?.purgeMode || null,
+ });
+ return { ok: true };
+ });
registerMessage("cloud-local-playback-clear", async (message) => {
const result = await clearStoredLocalPlaybackOffer({
reason: message?.reason || "explicit-clear",
@@ -2467,13 +2584,16 @@ export const setupHandlers = () => {
if (!offer) {
return { ok: false, error: "offer-unavailable" };
}
+ const postUseExpiry = Date.now() + CLOUD_LOCAL_PLAYBACK_POST_USE_TTL_MS;
const updated = {
...offer,
status: "used",
usedAt: Date.now(),
usedBy: message?.usedBy || "editor",
updatedAt: Date.now(),
+ expiresAt: Math.min(Number(offer.expiresAt) || postUseExpiry, postUseExpiry),
};
+ await scheduleLocalPlaybackAlarm(updated);
await chrome.storage.local.set({
[CLOUD_LOCAL_PLAYBACK_KEY]: updated,
[CLOUD_LOCAL_PLAYBACK_EVENT_KEY]: {
@@ -2489,8 +2609,34 @@ export const setupHandlers = () => {
projectId: updated.projectId,
sceneId: updated.sceneId,
});
+ reportLocalPlaybackOutcome("used", {
+ offer: updated,
+ localBytes: Number(message?.localBytes) || null,
+ // "bridge" (Blob by reference) vs "runtime" (base64 copy): tells us whether
+ // the iframe path works in the field.
+ transport: message?.transport || null,
+ reason: message?.usedBy || "editor",
+ });
return { ok: true, offer: updated };
});
+ // The bridge is addressable by any script on the app page, so it sends only
+ // an offerId and gets back the registered location, or nothing if unknown.
+ registerMessage("cloud-local-playback-redeem-offer", async (message) => {
+ const offer = await getValidLocalPlaybackOffer({
+ offerId: message?.offerId || null,
+ });
+ if (!offer) return { ok: false, error: "offer-unavailable" };
+ return {
+ ok: true,
+ offer: {
+ offerId: offer.offerId,
+ chunkCount: offer.chunkCount,
+ storageBackend: offer.storageBackend || null,
+ opfsSessionId: offer.opfsSessionId || null,
+ container: offer.container || null,
+ },
+ };
+ });
registerMessage("cloud-local-playback-mark-fallback", async (message) => {
const offer = await getValidLocalPlaybackOffer({
offerId: message?.offerId || null,
@@ -2520,6 +2666,10 @@ export const setupHandlers = () => {
offerId: updated.offerId,
reason: updated.fallbackReason,
});
+ reportLocalPlaybackOutcome("fallback", {
+ offer: updated,
+ reason: updated.fallbackReason,
+ });
return { ok: true, offer: updated };
});
registerMessage("finish-multi-recording", async () => {
diff --git a/src/pages/Background/offscreen/ensureRemuxOffscreen.js b/src/pages/Background/offscreen/ensureRemuxOffscreen.js
index e33ac92..d2bf481 100644
--- a/src/pages/Background/offscreen/ensureRemuxOffscreen.js
+++ b/src/pages/Background/offscreen/ensureRemuxOffscreen.js
@@ -9,7 +9,19 @@
*/
const REMUX_OFFSCREEN_URL = "remuxoffscreen.html";
-export const ensureRemuxOffscreen = async () => {
+// Concurrent remux requests each passed the check below and called createDocument.
+// Chrome permits one document, so the loser fell back to slow in-editor BufferTarget.
+let inFlightEnsure = null;
+
+export const ensureRemuxOffscreen = () => {
+ if (inFlightEnsure) return inFlightEnsure;
+ inFlightEnsure = ensureRemuxOffscreenImpl().finally(() => {
+ inFlightEnsure = null;
+ });
+ return inFlightEnsure;
+};
+
+const ensureRemuxOffscreenImpl = async () => {
if (!chrome.offscreen || typeof chrome.offscreen.createDocument !== "function") {
throw new Error("offscreen-api-unavailable");
}
@@ -49,10 +61,21 @@ export const ensureRemuxOffscreen = async () => {
break;
}
- await chrome.offscreen.createDocument({
- url: REMUX_OFFSCREEN_URL,
- reasons: ["WORKERS"],
- justification:
- "Re-mux fragmented MP4 recording to standard MP4 using a worker that writes to OPFS via a sync access handle.",
- });
+ try {
+ await chrome.offscreen.createDocument({
+ url: REMUX_OFFSCREEN_URL,
+ reasons: ["WORKERS"],
+ justification:
+ "Re-mux fragmented MP4 recording to standard MP4 using a worker that writes to OPFS via a sync access handle.",
+ });
+ } catch (err) {
+ // getContexts and createDocument are not atomic, so something can claim the
+ // single slot in between. If what landed is ours, that is a success.
+ const contexts = await chrome.runtime.getContexts({}).catch(() => []);
+ const landed = contexts.find((c) => c.contextType === "OFFSCREEN_DOCUMENT");
+ const landedUrl =
+ typeof landed?.documentUrl === "string" ? landed.documentUrl : "";
+ if (landedUrl.endsWith(REMUX_OFFSCREEN_URL)) return;
+ throw err;
+ }
};
diff --git a/src/pages/Background/recording/recordingHelpers.js b/src/pages/Background/recording/recordingHelpers.js
index 4f8cb4e..46bc64a 100644
--- a/src/pages/Background/recording/recordingHelpers.js
+++ b/src/pages/Background/recording/recordingHelpers.js
@@ -346,15 +346,35 @@ export const handleGetStreamingData = async () => {
export const videoReady = async () => {
perfMark("BG.recordingHelpers videoReady.enter");
- const { recordingDuration, recordingTab, lastRecordingBackendRef } =
- await chrome.storage.local.get([
- "recordingDuration",
- "recordingTab",
- "lastRecordingBackendRef",
- ]);
+ const {
+ recordingDuration,
+ recordingTab,
+ lastRecordingBackendRef,
+ recording,
+ } = await chrome.storage.local.get([
+ "recordingDuration",
+ "recordingTab",
+ "lastRecordingBackendRef",
+ "recording",
+ ]);
diagEvent("sw-received-video-ready", {
recordingDurationMs: Number(recordingDuration) || 0,
});
+
+ // Writer sets this fire-and-forget, but the discard path below kills that
+ // context first (marker set, key nulled 11s later), so re-assert it here.
+ // Skipped while recording since the file isn't finalized yet.
+ if (
+ lastRecordingBackendRef?.backend === "opfs" &&
+ lastRecordingBackendRef?.fileName &&
+ !recording
+ ) {
+ try {
+ await chrome.storage.local.set({
+ lastRecordingFinalizedFileName: lastRecordingBackendRef.fileName,
+ });
+ } catch {}
+ }
chrome.runtime
.sendMessage({
type: "clear-recording-session-safe",
diff --git a/src/pages/Background/tabManagement/editorTab.js b/src/pages/Background/tabManagement/editorTab.js
index 6e3eabc..9bd9cf6 100644
--- a/src/pages/Background/tabManagement/editorTab.js
+++ b/src/pages/Background/tabManagement/editorTab.js
@@ -214,11 +214,48 @@ export const getValidatedEditorTab = async ({
};
};
-export const resolveEditorTabForTarget = async ({
+// Concurrent callers each validated before any tab reference existed and opened
+// duplicates. Keyed by kind:projectId, the second caller awaits the first.
+const inFlightEditorOpens = new Map();
+
+export const resolveEditorTabForTarget = (args) => {
+ const parsedTarget = parseEditorTargetUrl(args?.targetUrl);
+ const key = [
+ args?.expectedKind || parsedTarget?.kind || "editor",
+ args?.expectedProjectId || parsedTarget?.projectId || args?.targetUrl || "",
+ ].join(":");
+ const pending = inFlightEditorOpens.get(key);
+ if (pending) {
+ // The key deliberately omits focus so concurrent callers still share one
+ // tab, but joining a focus:false run (forward-create-scene is the only one)
+ // would silently drop this caller's focus request and leave the editor in
+ // the background. Re-assert it on the tab the shared run resolved.
+ if (args?.focus === false) return pending;
+ return pending.then(async (res) => {
+ if (res?.tabId) {
+ await focusTab(res.tabId, {
+ reason: `${args?.reason || "unknown"}:joined`,
+ projectId: args?.expectedProjectId || null,
+ kind: args?.expectedKind || null,
+ });
+ }
+ return res;
+ });
+ }
+ const run = resolveEditorTabForTargetImpl(args).finally(() =>
+ inFlightEditorOpens.delete(key),
+ );
+ inFlightEditorOpens.set(key, run);
+ return run;
+};
+
+const resolveEditorTabForTargetImpl = async ({
targetUrl,
expectedProjectId = null,
expectedKind = null,
reason = "unknown",
+ // focus false: background open (forward-create-scene must not steal focus).
+ focus = true,
}) => {
const parsedTarget = parseEditorTargetUrl(targetUrl);
const projectId = expectedProjectId || parsedTarget?.projectId || null;
@@ -231,26 +268,32 @@ export const resolveEditorTabForTarget = async ({
});
if (existing.ok && existing.tab?.id) {
+ if (!focus) {
+ return { tabId: existing.tab.id, reused: true, opened: false };
+ }
const focused = await focusTab(existing.tab.id, {
reason: `${reason}:reuse`,
projectId,
kind,
});
- if (focused) {
+ if (!focused) {
+ console.warn("[Screenity][BG] Failed to focus validated editor tab", {
+ reason,
+ tabId: existing.tab.id,
+ projectId,
+ kind,
+ });
+ } else {
console.info("[Screenity][BG] Reusing validated editor tab", {
reason,
tabId: existing.tab.id,
projectId,
kind,
});
- return { tabId: existing.tab.id, reused: true, opened: false };
}
- console.warn("[Screenity][BG] Failed to focus validated editor tab", {
- reason,
- tabId: existing.tab.id,
- projectId,
- kind,
- });
+ // Focus is best effort: tabs.update throws "Tabs cannot be edited right now"
+ // mid tab-drag, and falling through here opened a duplicate tab.
+ return { tabId: existing.tab.id, reused: true, opened: false };
} else {
console.info("[Screenity][BG] Stored editor tab not reusable", {
reason,
@@ -269,7 +312,7 @@ export const resolveEditorTabForTarget = async ({
return { tabId: null, reused: false, opened: false };
}
- const createdTab = await createTab(targetUrl, true);
+ const createdTab = await createTab(targetUrl, focus);
if (!createdTab?.id) {
console.warn("[Screenity][BG] Failed to open fallback editor tab", {
reason,
@@ -286,11 +329,13 @@ export const resolveEditorTabForTarget = async ({
source: `fallback-open:${reason}`,
expectedProjectId: projectId,
});
- await focusTab(createdTab.id, {
- reason: `${reason}:opened`,
- projectId,
- kind,
- });
+ if (focus) {
+ await focusTab(createdTab.id, {
+ reason: `${reason}:opened`,
+ projectId,
+ kind,
+ });
+ }
console.info("[Screenity][BG] Opened fallback editor tab", {
reason,
diff --git a/src/pages/Background/utils/executeScripts.js b/src/pages/Background/utils/executeScripts.js
index 12cd84f..b1f4c94 100644
--- a/src/pages/Background/utils/executeScripts.js
+++ b/src/pages/Background/utils/executeScripts.js
@@ -1,3 +1,36 @@
+// Backfills content scripts into tabs that were already open at install/update.
+// Manifest content_scripts only auto-inject on future page loads, so without
+// this those tabs can't record until they're reloaded.
+//
+// Paced deliberately: the bundle is ~1MB, and injecting it into every open tab
+// at once has each tab's renderer parse, execute and mount the React tree
+// simultaneously. With a couple of dozen tabs that saturates the CPU and the
+// whole browser goes sluggish, tab switching included.
+const INJECT_CONCURRENCY = 3;
+
+// executeScript resolves as a promise only when no callback is passed. The
+// callback form returns undefined, so awaiting these used to wait on nothing.
+// Restricted or closed tabs reject; there's nothing to do about those.
+const injectInto = (tabId, files) =>
+ chrome.scripting
+ .executeScript({ target: { tabId }, files })
+ .catch(() => {});
+
+const runPool = async (jobs, limit) => {
+ let next = 0;
+ const workers = Array.from(
+ { length: Math.min(limit, jobs.length) },
+ async () => {
+ while (next < jobs.length) {
+ const job = jobs[next];
+ next += 1;
+ await job();
+ }
+ },
+ );
+ await Promise.all(workers);
+};
+
export const executeScripts = async () => {
const contentScripts = chrome.runtime.getManifest().content_scripts;
const tabQueries = contentScripts.map((cs) =>
@@ -5,22 +38,23 @@ export const executeScripts = async () => {
);
const tabResults = await Promise.all(tabQueries);
- const executeScriptPromises = [];
+ const jobs = [];
for (let i = 0; i < tabResults.length; i++) {
- const tabs = tabResults[i];
const cs = contentScripts[i];
-
+ // Discarded and unloaded tabs have no renderer to inject into, and the
+ // manifest registration covers them whenever they do load. Injecting here
+ // would only wake tabs Chrome deliberately put to sleep.
+ const tabs = tabResults[i].filter(
+ (tab) =>
+ typeof tab.id === "number" && !tab.discarded && tab.status !== "unloaded",
+ );
+ // Visible tabs first: those are the ones where a missing content script is
+ // immediately noticeable, and they finish before the pool reaches the rest.
+ tabs.sort((a, b) => Number(Boolean(b.active)) - Number(Boolean(a.active)));
for (const tab of tabs) {
- const executeScriptPromise = chrome.scripting.executeScript(
- {
- target: { tabId: tab.id },
- files: cs.js,
- },
- () => chrome.runtime.lastError
- );
- executeScriptPromises.push(executeScriptPromise);
+ jobs.push(() => injectInto(tab.id, cs.js));
}
}
- await Promise.all(executeScriptPromises);
+ await runPool(jobs, INJECT_CONCURRENCY);
};
diff --git a/src/pages/CloudRecorder/CloudRecorder.jsx b/src/pages/CloudRecorder/CloudRecorder.jsx
index 0e64cf8..d7d357e 100644
--- a/src/pages/CloudRecorder/CloudRecorder.jsx
+++ b/src/pages/CloudRecorder/CloudRecorder.jsx
@@ -5,6 +5,7 @@ import {
sendStopRecording,
} from "../utils/recorderMessaging";
import { getBitrates, getResolutionForQuality } from "./recorderConfig";
+import { pickEncodedDims, scaleClickEvents } from "./encodedDims";
import BunnyTusUploader from "./bunnyTusUploader";
import localforage from "localforage";
import { createVideoProject } from "./createVideoProject";
@@ -50,6 +51,16 @@ import {
enforceOversampleRatio,
} from "../utils/captureResolution";
import { getCompressedLifecycleLog } from "../utils/lifecycleLog";
+import {
+ CHUNK_PURGE_COMFORT_HEADROOM_BYTES,
+ resolvePrefixBudget,
+ resolvePurgeMode,
+ emptyRetainedPrefix,
+ extendRetainedPrefix,
+ selectChunksToPurge,
+ selectPrefixChunksToDrop,
+ resolveLocalPlaybackPlan,
+} from "./localPlaybackRetention";
localforage.config({
driver: localforage.INDEXEDDB,
@@ -75,7 +86,9 @@ const STORAGE_LOW_HEADROOM_BYTES = 250 * 1024 * 1024;
const STORAGE_CRITICAL_HEADROOM_BYTES = 120 * 1024 * 1024;
const AUDIO_MAX_BUFFER_BYTES = 150 * 1024 * 1024;
const LOCAL_SCREEN_PLAYBACK_TTL_MS = 20 * 60 * 1000;
-const LOCAL_SCREEN_PLAYBACK_MAX_BYTES = 250 * 1024 * 1024;
+// The old 250MB was the base64 transport limit, not a storage or playback bound.
+// The bridge passes a Blob by reference now, so headroom governs and this is a backstop.
+const LOCAL_SCREEN_PLAYBACK_MAX_BYTES = 8 * 1024 * 1024 * 1024;
// Bytes kept past lastServerOffset before purging - covers in-flight PATCHes, HEAD resyncs, 409 retries.
const CHUNK_PURGE_SAFETY_WINDOW_BYTES = 32 * 1024 * 1024;
const MAX_UPLOAD_TELEMETRY_EVENTS = 300;
@@ -233,6 +246,9 @@ const CloudRecorder = () => {
const audioInputGain = useRef(null);
const audioOutputGain = useRef(null);
+ // Set only when we force the bus to mono, since getSettings() can report 2
+ // while mixing mono. Null lets the encoder read the track it actually encodes.
+ const audioBusChannels = useRef(null);
const uploadersInitialized = useRef(false);
const pendingStartRef = useRef(false);
@@ -304,6 +320,21 @@ const CloudRecorder = () => {
const screenChunksPurgedCountRef = useRef(0);
const cameraChunksPurgedCountRef = useRef(0);
const chunksPurgedBytesRef = useRef(0);
+ // A healthy OPFS write is a few ms. Sustained spikes while the page lags is
+ // the local-disk-contention signature.
+ const localWriteStatsRef = useRef({
+ count: 0,
+ totalMs: 0,
+ maxMs: 0,
+ slowCount: 0,
+ });
+ // Leading screen chunks [0, chunks) still on disk. `dropped` is sticky so a
+ // hole can never be reported as a contiguous prefix.
+ const screenRetainedPrefixRef = useRef(emptyRetainedPrefix());
+ const purgeModeRef = useRef("none");
+ // purgeConfirmedChunks runs un-awaited per chunk write. Overlapping passes
+ // would both consume ranges, leaving live chunks untracked and never deleted.
+ const purgeInFlightRef = useRef({ screen: false, camera: false });
const sessionStateIndexedRef = useRef(false);
const sessionHeartbeat = useRef(null);
const chunkStallTimer = useRef(null);
@@ -386,6 +417,9 @@ const CloudRecorder = () => {
const isInit = useRef(false);
const aCtx = useRef(null);
+ // audioCtxWallMs minus audioCtxTimeMs growing = the mix bus underran
+ // (samples never rendered, the drift source).
+ const aCtxCreatedAtRef = useRef(null);
// Live AudioContext interruption stats from attachAudioContextWatchdog,
// folded into the audio diag snapshot at finalize for upload telemetry.
const audioHealthRef = useRef(null);
@@ -728,6 +762,31 @@ const CloudRecorder = () => {
: typeof eventPayload.finalizedBytes === "number"
? eventPayload.finalizedBytes
: null,
+ // The heartbeat schema reads totalBytes, the fileSize mapping above
+ // serves the upload events.
+ totalBytes:
+ typeof eventPayload.totalBytes === "number"
+ ? eventPayload.totalBytes
+ : null,
+ // Set on recording_heartbeat. These existed in the server schema but
+ // no client path ever sent them.
+ availableBytes:
+ typeof eventPayload.availableBytes === "number"
+ ? eventPayload.availableBytes
+ : null,
+ localBytes:
+ typeof eventPayload.localBytes === "number"
+ ? eventPayload.localBytes
+ : null,
+ storageQuotaBytes:
+ typeof eventPayload.storageQuotaBytes === "number"
+ ? eventPayload.storageQuotaBytes
+ : null,
+ storageUsageBytes:
+ typeof eventPayload.storageUsageBytes === "number"
+ ? eventPayload.storageUsageBytes
+ : null,
+ localWriteStats: eventPayload.localWriteStats || null,
online: typeof navigator !== "undefined" ? navigator.onLine : null,
visibilityState:
typeof document !== "undefined" ? document.visibilityState : null,
@@ -1629,6 +1688,83 @@ const CloudRecorder = () => {
}
};
+ const currentPrefixBudget = () =>
+ resolvePrefixBudget({
+ headroom: storagePressureRef.current.headroom,
+ transportCapBytes: LOCAL_SCREEN_PLAYBACK_MAX_BYTES,
+ comfortBytes: CHUNK_PURGE_COMFORT_HEADROOM_BYTES,
+ lowBytes: STORAGE_LOW_HEADROOM_BYTES,
+ });
+
+ const currentPurgeMode = () =>
+ resolvePurgeMode({
+ headroom: storagePressureRef.current.headroom,
+ lowBytes: STORAGE_LOW_HEADROOM_BYTES,
+ });
+
+ // chunk_0 stays: it's the init segment the crash-recovery and Download
+ // paths need. Unconfirmed chunks stay too, see selectPrefixChunksToDrop.
+ const dropRetainedPrefix = async (store, keyPrefix, uploader, rangesRef) => {
+ const prefix = screenRetainedPrefixRef.current;
+ const { drop, keptUnconfirmed } = selectPrefixChunksToDrop({
+ prefix,
+ lastServerOffset: Number(uploader?.lastServerOffset) || 0,
+ safetyWindowBytes: CHUNK_PURGE_SAFETY_WINDOW_BYTES,
+ });
+ // Stop offering local playback either way: the opening is about to be
+ // partly gone, so an offer built from it would not decode.
+ screenRetainedPrefixRef.current = {
+ chunks: 0,
+ bytes: 0,
+ dropped: true,
+ endBytes: [],
+ };
+ if (!drop.length) return;
+
+ const endBytes = prefix.endBytes || [];
+ let removed = 0;
+ let freedBytes = 0;
+ for (const i of drop) {
+ try {
+ // eslint-disable-next-line no-await-in-loop
+ await store.removeItem(`${keyPrefix}${i}`);
+ } catch (err) {
+ console.warn(
+ `[CloudRecorder] purge retained prefix chunk_${i} failed`,
+ err,
+ );
+ break;
+ }
+ removed += 1;
+ freedBytes += (endBytes[i] || 0) - (endBytes[i - 1] || 0);
+ }
+ if (removed > 0) {
+ // The trailing pass below still sees these entries otherwise and bills
+ // the same bytes a second time.
+ const dropped = new Set(drop.slice(0, removed));
+ const tracked = rangesRef?.current;
+ if (Array.isArray(tracked)) {
+ for (let i = tracked.length - 1; i >= 0; i -= 1) {
+ if (dropped.has(tracked[i]?.index)) tracked.splice(i, 1);
+ }
+ }
+ chunksPurgedDuringRecordingRef.current = true;
+ screenChunksPurgedCountRef.current += removed;
+ // Only what actually left disk, or the heartbeat reports reclaimed
+ // space that is still occupied.
+ chunksPurgedBytesRef.current += freedBytes;
+ console.info(
+ "[CloudRecorder] Dropped retained playback prefix under storage pressure",
+ {
+ removed,
+ freedBytes,
+ keptUnconfirmed,
+ headroom: storagePressureRef.current.headroom,
+ },
+ );
+ }
+ };
+
// Purge IDB chunks Bunny has confirmed. chunk_0 (webm init segment) is preserved.
const purgeConfirmedChunks = async (
store,
@@ -1638,19 +1774,42 @@ const CloudRecorder = () => {
trackLabel,
) => {
if (!uploader || !rangesRef.current.length) return;
- const lastServerOffset = Number(uploader.lastServerOffset) || 0;
- if (lastServerOffset <= CHUNK_PURGE_SAFETY_WINDOW_BYTES) return;
- const cutoff = lastServerOffset - CHUNK_PURGE_SAFETY_WINDOW_BYTES;
+ const mode = currentPurgeMode();
+ purgeModeRef.current = mode;
+ if (purgeInFlightRef.current[trackLabel]) return;
+ purgeInFlightRef.current[trackLabel] = true;
+ try {
+ await runPurgePass(store, rangesRef, uploader, keyPrefix, trackLabel, mode);
+ } finally {
+ purgeInFlightRef.current[trackLabel] = false;
+ }
+ };
+
+ const runPurgePass = async (
+ store,
+ rangesRef,
+ uploader,
+ keyPrefix,
+ trackLabel,
+ mode,
+ ) => {
+ const isScreen = trackLabel === "screen";
+ if (isScreen && mode === "all") {
+ await dropRetainedPrefix(store, keyPrefix, uploader, rangesRef);
+ }
const ranges = rangesRef.current;
+ const { untrack, purge } = selectChunksToPurge({
+ ranges,
+ lastServerOffset: Number(uploader.lastServerOffset) || 0,
+ safetyWindowBytes: CHUNK_PURGE_SAFETY_WINDOW_BYTES,
+ prefixChunks: isScreen ? screenRetainedPrefixRef.current.chunks : 0,
+ });
+ if (!untrack.length) return;
let purgedCount = 0;
let purgedBytes = 0;
- while (ranges.length > 0 && ranges[0].endByte <= cutoff) {
- const entry = ranges[0];
- if (entry.index === 0) {
- ranges.shift();
- continue;
- }
+ for (const entry of purge) {
try {
+ // eslint-disable-next-line no-await-in-loop
await store.removeItem(`${keyPrefix}${entry.index}`);
} catch (err) {
console.warn(
@@ -1659,10 +1818,18 @@ const CloudRecorder = () => {
);
break;
}
- ranges.shift();
purgedCount++;
purgedBytes += entry.size || 0;
}
+ // Stop at the first chunk whose delete failed so it's retried next pass.
+ // Retained chunks leave the tracking list even though their files stay.
+ const consumed =
+ purgedCount === purge.length
+ ? untrack.length
+ : untrack.indexOf(purge[purgedCount]);
+ // splice, not reassignment: this runs un-awaited while ondataavailable
+ // pushes new ranges onto the tail.
+ ranges.splice(0, Math.max(0, consumed));
if (purgedCount > 0) {
chunksPurgedDuringRecordingRef.current = true;
chunksPurgedBytesRef.current += purgedBytes;
@@ -1703,6 +1870,22 @@ const CloudRecorder = () => {
}
};
+ // BG turns this into a /log/upload-event. chrome.storage.local alone told
+ // us nothing about whether local playback actually worked.
+ const reportLocalPlaybackOutcome = (outcome, detail = {}) => {
+ try {
+ chrome.runtime
+ .sendMessage({
+ type: "cloud-local-playback-report",
+ outcome,
+ recordingSessionId:
+ recorderSession.current?.id || recordingSessionId.current || null,
+ ...detail,
+ })
+ .catch(() => {});
+ } catch {}
+ };
+
const registerLocalScreenPlaybackOffer = async ({
projectId,
sceneId,
@@ -1712,26 +1895,13 @@ const CloudRecorder = () => {
return null;
}
- if (chunksPurgedDuringRecordingRef.current) {
- console.info(
- "[CloudRecorder] Local-first screen offer skipped: chunks were purged mid-recording",
- {
- projectId,
- sceneId,
- purgedScreenChunks: screenChunksPurgedCountRef.current,
- purgedBytes: chunksPurgedBytesRef.current,
- },
- );
- return null;
- }
-
- let chunkCount = 0;
+ let storeChunkCount = 0;
try {
- chunkCount = await chunksStore.length();
+ storeChunkCount = await chunksStore.length();
} catch {
- chunkCount = 0;
+ storeChunkCount = 0;
}
- if (!chunkCount) {
+ if (!storeChunkCount) {
console.info(
"[CloudRecorder] Local-first screen offer unavailable: no local chunks",
{
@@ -1739,39 +1909,69 @@ const CloudRecorder = () => {
sceneId,
},
);
+ reportLocalPlaybackOutcome("skipped", {
+ projectId,
+ sceneId,
+ reason: "no-local-chunks",
+ });
return null;
}
- const estimatedBytes =
+ const totalBytes =
screenUploader.current?.offset ||
sessionTrackState.current?.screen?.bytesRecorded ||
0;
- if (!estimatedBytes || estimatedBytes <= 0) {
+ if (!totalBytes || totalBytes <= 0) {
console.info(
"[CloudRecorder] Local-first screen offer unavailable: no byte estimate",
{
projectId,
sceneId,
- chunkCount,
+ chunkCount: storeChunkCount,
},
);
+ reportLocalPlaybackOutcome("skipped", {
+ projectId,
+ sceneId,
+ reason: "no-byte-estimate",
+ chunkCount: storeChunkCount,
+ });
return null;
}
- if (estimatedBytes > LOCAL_SCREEN_PLAYBACK_MAX_BYTES) {
- console.info(
- "[CloudRecorder] Local-first screen offer skipped: source too large",
- {
- projectId,
- sceneId,
- chunkCount,
- estimatedBytes,
- maxBytes: LOCAL_SCREEN_PLAYBACK_MAX_BYTES,
- },
- );
+ const plan = resolveLocalPlaybackPlan({
+ // The camera purge sets the shared flag too, which marked a fully
+ // retained screen source partial and sent the editor to a 404.
+ purged: screenChunksPurgedCountRef.current > 0,
+ prefix: screenRetainedPrefixRef.current,
+ totalBytes,
+ storeChunkCount,
+ maxBytes: LOCAL_SCREEN_PLAYBACK_MAX_BYTES,
+ });
+ if (!plan.chunkCount || !plan.availableBytes) {
+ console.info("[CloudRecorder] Local-first screen offer skipped", {
+ projectId,
+ sceneId,
+ reason: plan.reason,
+ totalBytes,
+ storeChunkCount,
+ purgedScreenChunks: screenChunksPurgedCountRef.current,
+ purgedBytes: chunksPurgedBytesRef.current,
+ });
+ reportLocalPlaybackOutcome("skipped", {
+ projectId,
+ sceneId,
+ reason: plan.reason,
+ totalBytes,
+ chunkCount: storeChunkCount,
+ purgeMode: purgeModeRef.current,
+ });
return null;
}
+ const chunkCount = plan.chunkCount;
+ const estimatedBytes = plan.availableBytes;
+
const now = Date.now();
const screenBackend = storageBackends.screen || "idb";
const offerOpfsSessionId =
@@ -1791,6 +1991,11 @@ const CloudRecorder = () => {
trackType: "screen",
chunkCount,
estimatedBytes,
+ // partial = the offer covers only the opening of the recording, so the
+ // editor falls back to the remote URL at the end.
+ partial: plan.partial,
+ availableBytes: estimatedBytes,
+ totalBytes,
mediaId: uploadMeta?.screen?.mediaId || null,
bunnyVideoId: uploadMeta?.screen?.videoId || null,
createdAt: now,
@@ -1823,11 +2028,23 @@ const CloudRecorder = () => {
offerId: result.offer.offerId,
chunkCount: result.offer.chunkCount,
estimatedBytes: result.offer.estimatedBytes,
+ partial: result.offer.partial,
+ totalBytes,
expiresAt: result.offer.expiresAt,
},
);
return result.offer;
}
+ reportLocalPlaybackOutcome("skipped", {
+ projectId,
+ sceneId,
+ reason: result?.error || "register-rejected",
+ chunkCount,
+ availableBytes: estimatedBytes,
+ totalBytes,
+ partial: plan.partial,
+ purgeMode: purgeModeRef.current,
+ });
} catch (err) {
console.warn(
"[CloudRecorder] Failed to register local-first screen offer",
@@ -1837,6 +2054,15 @@ const CloudRecorder = () => {
error: err?.message || err,
},
);
+ reportLocalPlaybackOutcome("skipped", {
+ projectId,
+ sceneId,
+ reason: "register-failed",
+ chunkCount,
+ availableBytes: estimatedBytes,
+ totalBytes,
+ partial: plan.partial,
+ });
}
return null;
@@ -2036,7 +2262,16 @@ const CloudRecorder = () => {
const micChosen = audioIntent.current?.micActive === true;
const hasUploader = Boolean(audioUploader.current);
const hasRecorder = Boolean(audioRecorder.current);
- if (!micChosen && !hasUploader && !hasRecorder) return null;
+ // System-audio-only sessions have no mic/uploader/recorder but still carry
+ // audio via the screen (or camera, camera-only mode) WebCodecs recorder;
+ // null there means the MediaRecorder fallback.
+ const muxAudioDiag =
+ screenRecorder.current?.getAudioDiag?.() ||
+ cameraRecorder.current?.getAudioDiag?.() ||
+ null;
+ if (!micChosen && !hasUploader && !hasRecorder && !muxAudioDiag) {
+ return null;
+ }
const track = rawMicStream.current?.getAudioTracks?.()[0] || null;
const audioState = sessionTrackState.current?.audio || {};
@@ -2074,6 +2309,15 @@ const CloudRecorder = () => {
audioInterruptedCount: audioHealthRef.current?.interruptedCount ?? 0,
audioInterruptedMs: audioHealthRef.current?.interruptedTotalMs ?? 0,
audioSawInterrupted: audioHealthRef.current?.sawInterrupted ?? false,
+ // Loss-stage discriminator: ctxTime lagging wall = mix-bus underrun.
+ // ctxTime tracking wall while receivedMs lags = reader loss after the bus.
+ audioCtxTimeMs: aCtx.current
+ ? Math.round((aCtx.current.currentTime || 0) * 1000)
+ : null,
+ audioCtxWallMs: aCtxCreatedAtRef.current
+ ? Date.now() - aCtxCreatedAtRef.current
+ : null,
+ ...(muxAudioDiag || {}),
};
} catch {
return null;
@@ -2120,12 +2364,20 @@ const CloudRecorder = () => {
lifecycleBundle = await getCompressedLifecycleLog();
} catch {}
}
+ // The server accepts encodeStats on any event type, so attach it here too.
+ const screenStats = screenRecorder.current?.getEncodeStats?.() || null;
+ const cameraStats = cameraRecorder.current?.getEncodeStats?.() || null;
void emitUploadTelemetry(eventType, {
status,
screenDiag,
cameraDiag,
audioDiag,
lifecycleBundle,
+ encodeStats: screenStats
+ ? { ...screenStats, track: "screen" }
+ : cameraStats
+ ? { ...cameraStats, track: "camera" }
+ : null,
});
}
} catch {}
@@ -4058,6 +4310,10 @@ const CloudRecorder = () => {
screenChunksPurgedCountRef.current = 0;
cameraChunksPurgedCountRef.current = 0;
chunksPurgedBytesRef.current = 0;
+ localWriteStatsRef.current = { count: 0, totalMs: 0, maxMs: 0, slowCount: 0 };
+ screenRetainedPrefixRef.current = emptyRetainedPrefix();
+ purgeModeRef.current = "none";
+ purgeInFlightRef.current = { screen: false, camera: false };
firstChunkLoggedRef.current = false;
resetUnloadGuard();
recoveryExportedRef.current = false;
@@ -4127,6 +4383,7 @@ const CloudRecorder = () => {
mimeType: screenOptions.mimeType,
videoBitsPerSecond: screenOptions.videoBitsPerSecond,
audioBitsPerSecond: screenOptions.audioBitsPerSecond,
+ audioChannels: audioBusChannels.current,
enableAudio: screenHasAudio,
createMediaRecorder,
onDataAvailable: async (blob) => {
@@ -4186,6 +4443,7 @@ const CloudRecorder = () => {
.set({ lastChunkAt: Date.now() })
.catch(() => {});
+ const writeStartedAt = performance.now();
await chunksStore
.setItem(`chunk_${index.current}`, {
index: index.current,
@@ -4200,6 +4458,12 @@ const CloudRecorder = () => {
sendStopRecording();
throw err;
});
+ const writeMs = performance.now() - writeStartedAt;
+ const writeStats = localWriteStatsRef.current;
+ writeStats.count += 1;
+ writeStats.totalMs += writeMs;
+ if (writeMs > writeStats.maxMs) writeStats.maxMs = writeMs;
+ if (writeMs >= 1000) writeStats.slowCount += 1;
patchTrackState("screen", {
durableChunkCount: index.current + 1,
@@ -4226,6 +4490,14 @@ const CloudRecorder = () => {
endByte,
size: blob?.size || 0,
});
+ screenRetainedPrefixRef.current = extendRetainedPrefix(
+ screenRetainedPrefixRef.current,
+ {
+ index: index.current,
+ endByte,
+ budgetBytes: currentPrefixBudget(),
+ },
+ );
void purgeConfirmedChunks(
chunksStore,
screenChunkByteRangesRef,
@@ -4448,6 +4720,7 @@ const CloudRecorder = () => {
mimeType: cameraOptions.mimeType,
videoBitsPerSecond: cameraOptions.videoBitsPerSecond,
audioBitsPerSecond: cameraOptions.audioBitsPerSecond,
+ audioChannels: audioBusChannels.current,
enableAudio: cameraHasAudio,
createMediaRecorder,
probeOptions: {
@@ -5087,10 +5360,55 @@ const CloudRecorder = () => {
const camera = cameraRecorder.current?.getDiagSnapshot?.();
const audio = getAudioDiagSnapshot();
if (!screen && !camera && !audio) return;
+ // offsetBytes used to stay 0 all session then jump at finalize, leaving
+ // mid-recording upload behaviour unreconstructable.
+ const uploadedBytes =
+ (Number(screenUploader.current?.offset) || 0) +
+ (Number(cameraUploader.current?.offset) || 0) +
+ (Number(audioUploader.current?.offset) || 0);
+ const trackState = sessionTrackState.current || {};
+ const recordedBytes =
+ (trackState.screen?.bytesRecorded || 0) +
+ (trackState.camera?.bytesRecorded || 0) +
+ (trackState.audio?.bytesRecorded || 0);
+ const pressure = storagePressureRef.current || {};
+ const screenStats = screenRecorder.current?.getEncodeStats?.() || null;
+ const cameraStats = cameraRecorder.current?.getEncodeStats?.() || null;
+ const writeStats = localWriteStatsRef.current || {};
void emitUploadTelemetry("recording_heartbeat", {
screenDiag: screen || null,
cameraDiag: camera || null,
audioDiag: audio || null,
+ offset: uploadedBytes,
+ totalBytes: recordedBytes,
+ // Polled, not live: at most STORAGE_CHECK_INTERVAL_MS stale.
+ availableBytes:
+ typeof pressure.headroom === "number" ? pressure.headroom : null,
+ localBytes: Math.max(
+ 0,
+ recordedBytes - (chunksPurgedBytesRef.current || 0),
+ ),
+ // Quota shrinking across samples means the OS disk itself is filling.
+ storageQuotaBytes:
+ typeof pressure.quota === "number" ? pressure.quota : null,
+ storageUsageBytes:
+ typeof pressure.usage === "number" ? pressure.usage : null,
+ encodeStats: screenStats
+ ? { ...screenStats, track: "screen" }
+ : cameraStats
+ ? { ...cameraStats, track: "camera" }
+ : null,
+ localWriteStats: writeStats.count
+ ? {
+ count: writeStats.count,
+ avgMs: Math.round(writeStats.totalMs / writeStats.count),
+ maxMs: Math.round(writeStats.maxMs),
+ slowCount: writeStats.slowCount,
+ // Server enum: opfs|idb|memory|unknown. Never null, or the
+ // sanitizer can drop the whole localWriteStats object.
+ backend: storageBackends.screen || "unknown",
+ }
+ : null,
});
} catch {}
}, 30_000);
@@ -5247,6 +5565,31 @@ const CloudRecorder = () => {
sceneId,
});
+ // uploadMeta's dims are a pre-encode guess, wrong on 27% of scenes (see
+ // encodedDims.js). Scene size, aspect ratio, baseResolution and auto-zoom
+ // all key off what we send here, so ask the encoder what it actually wrote.
+ const reportedScreenDims = {
+ width: uploadMeta.screen?.width || 1920,
+ height: uploadMeta.screen?.height || 1080,
+ };
+ let screenTrackSettings = null;
+ try {
+ screenTrackSettings =
+ screenStream.current?.getVideoTracks?.()[0]?.getSettings?.() || null;
+ } catch {}
+ const screenEncoded = pickEncodedDims({
+ diag: screenRecorder.current?.getDiagSnapshot?.() || null,
+ trackSettings: screenTrackSettings,
+ fallback: reportedScreenDims,
+ });
+ // Clicks have to move with the corrected dims, or auto-zoom pans to the
+ // wrong place.
+ const scaledClickEvents = scaleClickEvents(
+ clickEvents,
+ reportedScreenDims,
+ screenEncoded,
+ );
+
const payload = {
sceneId,
screenMediaId: uploadMeta.screen?.mediaId || null,
@@ -5262,10 +5605,7 @@ const CloudRecorder = () => {
: null,
thumbnail: uploadMeta.screen?.thumbnail || null,
dimensions: {
- screen: {
- width: uploadMeta.screen?.width || 1920,
- height: uploadMeta.screen?.height || 1080,
- },
+ screen: { width: screenEncoded.width, height: screenEncoded.height },
camera: uploadMeta.camera
? {
width: uploadMeta.camera?.width || 1920,
@@ -5274,7 +5614,7 @@ const CloudRecorder = () => {
}
: null,
},
- clickEvents,
+ clickEvents: scaledClickEvents,
surface,
instantMode: instantMode.current,
newProject: !recordingToScene && (!multiMode || multiSceneCount === 0),
@@ -5859,6 +6199,7 @@ const CloudRecorder = () => {
cameraChunksPurged: cameraChunksPurgedCountRef.current,
bytesFreed: chunksPurgedBytesRef.current,
safetyWindowBytes: CHUNK_PURGE_SAFETY_WINDOW_BYTES,
+ reason: purgeModeRef.current,
});
}
@@ -6809,11 +7150,26 @@ const CloudRecorder = () => {
// Some platforms reject hint sampleRates; fall back to default.
aCtx.current = new AudioContext();
}
+ aCtxCreatedAtRef.current = Date.now();
audioHealthRef.current = attachAudioContextWatchdog(
aCtx.current,
"CloudRecorder",
);
destination.current = aCtx.current.createMediaStreamDestination();
+ // A stereo destination duplicates a mono mic, wasting half the AAC bitrate.
+ // System audio only joins at start, so the lazy setMic path stays stereo.
+ audioBusChannels.current = null;
+ try {
+ const micTrack = micStream.current?.getAudioTracks?.()[0] || null;
+ const hasSystemAudio = Boolean(
+ screenStream.current?.getAudioTracks?.().length,
+ );
+ const micChannels = micTrack?.getSettings?.()?.channelCount || null;
+ if (micTrack && !hasSystemAudio && micChannels === 1) {
+ destination.current.channelCount = 1;
+ audioBusChannels.current = 1;
+ }
+ } catch {}
if (micStream.current?.getAudioTracks().length) {
audioInputGain.current = aCtx.current.createGain();
diff --git a/src/pages/CloudRecorder/encodedDims.js b/src/pages/CloudRecorder/encodedDims.js
new file mode 100644
index 0000000..5ddaf66
--- /dev/null
+++ b/src/pages/CloudRecorder/encodedDims.js
@@ -0,0 +1,50 @@
+// The dims sent at upload-init are a guess made before the encoder exists. For
+// region captures they're the CSS size of the drag handle, so on any display
+// with a device pixel ratio above 1 they understate the file (a 412x924
+// selection encodes 824x1848 at dpr 1, since tab capture renders at 2x). A
+// survey of 328 recent scenes found 27.4% disagreeing with their own media.
+//
+// These dims become the scene's width/height, aspectRatio, baseResolution,
+// and the divisor auto-zoom uses to turn clicks into percentages, so click
+// coordinates have to be rescaled to match whatever we end up reporting.
+
+// Prefer what the encoder wrote. Falling back to the live track is safe at stop
+// time (frames have flowed, so a cropped track finally reports the crop) but not
+// at start, which is why this runs at scene-create.
+export function pickEncodedDims({ diag, trackSettings, fallback }) {
+ const candidates = [
+ [diag?.encodeWidth, diag?.encodeHeight],
+ [trackSettings?.width, trackSettings?.height],
+ [fallback?.width, fallback?.height],
+ ];
+ for (const [w, h] of candidates) {
+ const width = Math.round(Number(w));
+ const height = Math.round(Number(h));
+ if (Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0) {
+ return { width, height };
+ }
+ }
+ return { width: 1920, height: 1080 };
+}
+
+// Clicks are CSS pixels (trackClicks.js, minus region origin); the server
+// divides by the reported dims, so fixing dims without rescaling clicks sends
+// auto-zoom to the top-left. Kept client-side so old extensions still work.
+export function scaleClickEvents(clickEvents, fromDims, toDims) {
+ if (!Array.isArray(clickEvents) || clickEvents.length === 0) return clickEvents;
+ const fw = Number(fromDims?.width);
+ const fh = Number(fromDims?.height);
+ const tw = Number(toDims?.width);
+ const th = Number(toDims?.height);
+ if (![fw, fh, tw, th].every((n) => Number.isFinite(n) && n > 0)) return clickEvents;
+ const sx = tw / fw;
+ const sy = th / fh;
+ // Guess was already right, the common case for full-screen capture at 1x.
+ if (Math.abs(sx - 1) < 0.001 && Math.abs(sy - 1) < 0.001) return clickEvents;
+ return clickEvents.map((c) => {
+ const x = Number(c?.x);
+ const y = Number(c?.y);
+ if (!Number.isFinite(x) || !Number.isFinite(y)) return c;
+ return { ...c, x: Math.round(x * sx), y: Math.round(y * sy) };
+ });
+}
diff --git a/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js b/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
index 1b72e68..7ff2f15 100644
--- a/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
+++ b/src/pages/CloudRecorder/encoder/WebCodecsTrackRecorder.js
@@ -22,6 +22,9 @@ export class WebCodecsTrackRecorder {
this.videoBitsPerSecond =
options.videoBitsPerSecond || 16_000_000;
this.audioBitsPerSecond = options.audioBitsPerSecond || 128_000;
+ // getSettings() can report 2 channels while the bus mixes mono, and a stereo
+ // encoder rejects mono AudioData (see WebCodecsRecorder).
+ this.audioChannels = options.audioChannels || null;
this.enableAudio = options.enableAudio !== false; // default true
// Used by cloud's camera path on macOS to force a software h264
// encoder, sidestepping VideoToolbox's per-process HW-slot serialization.
@@ -29,6 +32,7 @@ export class WebCodecsTrackRecorder {
// Namespaces the decoderConfig sidecar so dual-track keys don't collide.
this.trackKind = options.trackKind || "default";
this.encodeStats = null;
+ this.audioDiag = null;
// MediaRecorder-shape fields used by callers.
this._state = "inactive";
this.ondataavailable = null;
@@ -70,6 +74,14 @@ export class WebCodecsTrackRecorder {
}
}
+ getAudioDiag() {
+ try {
+ return this.audioDiag || this._recorder?.getAudioDiag?.() || null;
+ } catch {
+ return this.audioDiag || null;
+ }
+ }
+
// Diagnostic counters; safe to poll mid-recording and after stop.
// Returns null before the inner recorder is constructed.
getDiagSnapshot() {
@@ -114,6 +126,9 @@ export class WebCodecsTrackRecorder {
try {
this.encodeStats = this._recorder?.getEncodeStats?.() || null;
} catch {}
+ try {
+ this.audioDiag = this._recorder?.getAudioDiag?.() || null;
+ } catch {}
if (this._finalizedResolved) return;
this._finalizedResolved = true;
this._state = "inactive";
@@ -176,6 +191,7 @@ export class WebCodecsTrackRecorder {
enableAudio: this.enableAudio,
videoBitrate: this.videoBitsPerSecond,
audioBitrate: this.audioBitsPerSecond,
+ audioChannels: this.audioChannels,
preferSoftware: this.preferSoftware,
// Cap at 1080p; retina captures exceed AVC L4.2 (~2.2MP) and
// throw on first frame, leaving Bunny with the 28-byte init.
diff --git a/src/pages/CloudRecorder/encoder/chooseEncoder.js b/src/pages/CloudRecorder/encoder/chooseEncoder.js
index a6035c0..5feb1c1 100644
--- a/src/pages/CloudRecorder/encoder/chooseEncoder.js
+++ b/src/pages/CloudRecorder/encoder/chooseEncoder.js
@@ -208,6 +208,7 @@ export const chooseTrackEncoder = async ({
mimeType,
videoBitsPerSecond,
audioBitsPerSecond,
+ audioChannels,
enableAudio,
createMediaRecorder,
onDataAvailable,
@@ -258,6 +259,7 @@ export const chooseTrackEncoder = async ({
mimeType: "video/mp4",
videoBitsPerSecond,
audioBitsPerSecond,
+ audioChannels,
enableAudio,
preferSoftware:
forceSoftware ||
diff --git a/src/pages/CloudRecorder/localPlaybackRetention.js b/src/pages/CloudRecorder/localPlaybackRetention.js
new file mode 100644
index 0000000..3cdffc7
--- /dev/null
+++ b/src/pages/CloudRecorder/localPlaybackRetention.js
@@ -0,0 +1,173 @@
+// How much of a cloud recording stays on disk while it uploads, and what the
+// editor can play meanwhile. The scene is created before the upload, so its URL
+// 404s until upload completes and local chunks cover that window.
+// Pure functions, no browser deps, so tests/unit can exercise them.
+
+// Below this headroom, shrink the retained prefix. ~8x the low-space warning
+// (250MB) and ~17x the stop threshold (120MB).
+export const CHUNK_PURGE_COMFORT_HEADROOM_BYTES = 2 * 1024 * 1024 * 1024;
+// Prefix budget under pressure: worth playing, small enough not to matter on a
+// tight disk.
+export const LOCAL_PLAYBACK_PREFIX_PRESSURE_BYTES = 64 * 1024 * 1024;
+
+const headroomKnown = (headroom) =>
+ typeof headroom === "number" && Number.isFinite(headroom) && headroom > 0;
+
+// Unknown estimate counts as pressure but not as tight disk. Headroom is null
+// until the first async estimate, exactly when chunk_0 arrives, and a prefix
+// missing chunk_0 never becomes contiguous.
+const headroomIsBelow = (headroom, bar) =>
+ headroomKnown(headroom) ? headroom < bar : false;
+
+// Ceiling is the TRANSPORT cap: the base64 runtime loop refuses past
+// LOCAL_SCREEN_PLAYBACK_MAX_BYTES. Passed in so the Blob handoff can raise it
+// in one place.
+export const resolvePrefixBudget = ({
+ headroom,
+ transportCapBytes,
+ pressureBytes = LOCAL_PLAYBACK_PREFIX_PRESSURE_BYTES,
+ comfortBytes = CHUNK_PURGE_COMFORT_HEADROOM_BYTES,
+ lowBytes,
+}) => {
+ if (typeof lowBytes === "number" && headroomIsBelow(headroom, lowBytes)) {
+ return 0;
+ }
+ if (!headroomKnown(headroom) || headroomIsBelow(headroom, comfortBytes)) {
+ return Math.min(pressureBytes, transportCapBytes);
+ }
+ return transportCapBytes;
+};
+
+// "trailing" spares the leading prefix, "all" purges it too. No keep-everything
+// mode: quota-based headroom never lapses on a roomy disk, so the prefix budget
+// is the real bound.
+export const resolvePurgeMode = ({ headroom, lowBytes }) =>
+ typeof lowBytes === "number" && headroomIsBelow(headroom, lowBytes)
+ ? "all"
+ : "trailing";
+
+export const emptyRetainedPrefix = () => ({
+ chunks: 0,
+ bytes: 0,
+ dropped: false,
+ endBytes: [],
+});
+
+// `endByte` is the uploader's running total, the same byte stream the editor plays.
+// Once a chunk leaves the tracked ranges this is the only record of what the server has.
+export const extendRetainedPrefix = (
+ prefix,
+ { index, endByte, budgetBytes },
+) => {
+ if (prefix.dropped) return prefix;
+ if (prefix.chunks !== index) return prefix;
+ if (!(endByte > 0) || endByte > budgetBytes) return prefix;
+ return {
+ ...prefix,
+ chunks: index + 1,
+ bytes: endByte,
+ endBytes: [...(prefix.endBytes || []), endByte],
+ };
+};
+
+// Same cutoff the trailing purge uses. The prefix grows when a chunk reaches the
+// uploader, so dropping all of it deletes the only copy of bytes still in flight.
+export const selectPrefixChunksToDrop = ({
+ prefix,
+ lastServerOffset,
+ safetyWindowBytes,
+}) => {
+ const empty = { drop: [], bytes: 0, keptUnconfirmed: 0 };
+ if (!prefix || prefix.dropped || prefix.chunks <= 1) return empty;
+ const endBytes = prefix.endBytes || [];
+ const cutoff = (Number(lastServerOffset) || 0) - safetyWindowBytes;
+ if (!(cutoff > 0)) return { ...empty, keptUnconfirmed: prefix.chunks - 1 };
+
+ const drop = [];
+ let bytes = 0;
+ // chunk_0 is the init segment and never goes. Stop at the first unconfirmed
+ // chunk: the prefix is contiguous, so everything after it is unconfirmed too.
+ for (let i = 1; i < prefix.chunks; i += 1) {
+ const endByte = endBytes[i];
+ // No recorded endByte means it predates tracking, so treat as unconfirmed.
+ if (!(endByte > 0) || endByte > cutoff) break;
+ drop.push(i);
+ bytes += endByte - (endBytes[i - 1] || 0);
+ }
+ return { drop, bytes, keptUnconfirmed: prefix.chunks - 1 - drop.length };
+};
+
+export const shouldRetainChunk = (index, prefixChunks) =>
+ index === 0 || index < prefixChunks;
+
+// Both sets stop being tracked, only `purge` leaves disk. Ranges are ascending and
+// consumed from the front, so the retained set stays contiguous from chunk_0.
+export const selectChunksToPurge = ({
+ ranges,
+ lastServerOffset,
+ safetyWindowBytes,
+ prefixChunks,
+}) => {
+ const empty = { purge: [], untrack: [] };
+ if (!ranges?.length) return empty;
+ if (!(lastServerOffset > safetyWindowBytes)) return empty;
+ const cutoff = lastServerOffset - safetyWindowBytes;
+ const purge = [];
+ const untrack = [];
+ for (const entry of ranges) {
+ if (entry.endByte > cutoff) break;
+ untrack.push(entry);
+ if (!shouldRetainChunk(entry.index, prefixChunks)) purge.push(entry);
+ }
+ return { purge, untrack };
+};
+
+// Whole recording, retained opening, or nothing. Purging alone doesn't
+// disqualify an offer.
+export const resolveLocalPlaybackPlan = ({
+ purged,
+ prefix,
+ totalBytes,
+ storeChunkCount,
+ maxBytes,
+}) => {
+ if (
+ !purged &&
+ totalBytes > 0 &&
+ totalBytes <= maxBytes &&
+ storeChunkCount > 0
+ ) {
+ return {
+ chunkCount: storeChunkCount,
+ availableBytes: totalBytes,
+ partial: false,
+ };
+ }
+
+ // Needs the init segment plus at least one media chunk to decode.
+ if (
+ prefix &&
+ !prefix.dropped &&
+ prefix.chunks >= 2 &&
+ prefix.bytes > 0 &&
+ prefix.bytes <= maxBytes &&
+ storeChunkCount >= 2
+ ) {
+ return {
+ chunkCount: Math.min(prefix.chunks, storeChunkCount),
+ availableBytes: prefix.bytes,
+ partial: true,
+ };
+ }
+
+ return {
+ chunkCount: 0,
+ availableBytes: 0,
+ partial: false,
+ reason: prefix?.dropped
+ ? "prefix-dropped-storage-pressure"
+ : purged
+ ? "no-retained-prefix"
+ : "source-too-large",
+ };
+};
diff --git a/src/pages/Content/Wrapper.jsx b/src/pages/Content/Wrapper.jsx
index 9e6cb9f..b928b02 100644
--- a/src/pages/Content/Wrapper.jsx
+++ b/src/pages/Content/Wrapper.jsx
@@ -120,11 +120,21 @@ const Wrapper = () => {
const permissionsRef = useRef(null);
const regionCaptureRef = useRef(null);
const contentStateRef = useRef(contentState);
+ // Mount on first use, then keep it. Gating on drawingMode threw away the
+ // fabric instance (and its drawings) every time the toolbar closed; an
+ // unmounted canvas is already pointer-events:none, so staying mounted is free.
+ const [canvasEverOpened, setCanvasEverOpened] = useState(false);
useEffect(() => {
contentStateRef.current = contentState;
}, [contentState]);
+ useEffect(() => {
+ if (contentState.drawingMode || contentState.blurMode) {
+ setCanvasEverOpened(true);
+ }
+ }, [contentState.drawingMode, contentState.blurMode]);
+
// Delayed loader: only show after 800ms in a wait window
// (pre-countdown setup or post-stop finalize), never during
// countdown or active recording. Skip the flash when start
@@ -386,10 +396,13 @@ const Wrapper = () => {
}}
></div>
)}
- {(contentState.drawingMode || contentState.blurMode) && (
+ {(canvasEverOpened ||
+ contentState.drawingMode ||
+ contentState.blurMode) && (
// Key on multiSceneCount only: fresh fabric per scene,
// but drawings persist through the pre-record → record
- // transition within a scene.
+ // transition within a scene, and through the toolbar
+ // being closed and reopened.
<Canvas key={`canvas-${contentState.multiSceneCount || 0}`} />
)}
<CursorModes />
diff --git a/src/pages/Content/canvas/fabricCompat.js b/src/pages/Content/canvas/fabricCompat.js
index 3fe6da1..103891b 100644
--- a/src/pages/Content/canvas/fabricCompat.js
+++ b/src/pages/Content/canvas/fabricCompat.js
@@ -1,78 +1,49 @@
// Fabric v5 → v6 shim. v6 dropped the `fabric` namespace export, so
// this re-exports v6's named exports under the legacy `fabric.X` shape
-// our call sites use. Tree-shaking still works.
+// our call sites use.
+//
+// Only the names the canvas modules actually reference: webpack follows the
+// namespace object below, so re-exporting the rest of v6 "just in case" pinned
+// SVG parsing, image filters, and unused shape classes into the content bundle
+// that's injected into every page. Add a name back if a call site needs it.
import {
Canvas,
- StaticCanvas,
- ActiveSelection,
Circle,
Color,
Control,
- Ellipse,
FabricImage,
FabricObject,
- FabricText,
- Gradient,
Group,
- IText,
Line,
Path,
- Pattern,
PencilBrush,
- Point,
- Polygon,
- Polyline,
Rect,
- Shadow,
Textbox,
Triangle,
- classRegistry,
- config,
controlsUtils,
- filters,
- loadSVGFromString,
- loadSVGFromURL,
util,
- version,
} from "fabric";
// v5-style namespace; only the classes used in src/pages/Content/canvas.
+// Undo/redo goes through canvas.loadFromJSON, which resolves serialized type
+// names via fabric's own class registry: classes register on import, so every
+// shape we can create is already covered by the imports above.
export const fabric = {
Canvas,
- StaticCanvas,
- ActiveSelection,
Circle,
Color,
Control,
- Ellipse,
- Image: FabricImage,
- FabricImage,
- Object: FabricObject,
- FabricObject,
- Text: FabricText,
- FabricText,
- Gradient,
Group,
- IText,
+ Image: FabricImage,
Line,
+ Object: FabricObject,
Path,
- Pattern,
PencilBrush,
- Point,
- Polygon,
- Polyline,
Rect,
- Shadow,
Textbox,
Triangle,
- classRegistry,
- config,
controlsUtils,
- filters,
- loadSVGFromString,
- loadSVGFromURL,
util,
- version,
};
export default fabric;
diff --git a/src/pages/Content/canvas/modules/History.jsx b/src/pages/Content/canvas/modules/History.jsx
index e7fdf28..b24857e 100644
--- a/src/pages/Content/canvas/modules/History.jsx
+++ b/src/pages/Content/canvas/modules/History.jsx
@@ -1,4 +1,3 @@
-import { fabric } from "../fabricCompat";
const getState = (stateOrRef) =>
stateOrRef && stateOrRef.current ? stateOrRef.current : stateOrRef;
diff --git a/src/pages/Content/context/ContentState.jsx b/src/pages/Content/context/ContentState.jsx
index 91dd939..dbf0e7d 100644
--- a/src/pages/Content/context/ContentState.jsx
+++ b/src/pages/Content/context/ContentState.jsx
@@ -26,6 +26,9 @@ import {
import { triggerSupportDownload } from "../../utils/triggerSupportDownload";
export const contentStateContext = createContext();
+// Split out of contentStateContext: the 1s clock tick was re-rendering every
+// consumer in the page's shadow tree. Now it only hits the toolbar clock.
+export const timerContext = createContext([0, () => {}]);
export const contentStateRef = { current: null };
export let setContentState = () => {};
export let setTimer = () => {};
@@ -48,6 +51,8 @@ const deriveCursorMode = (effects, fallbackMode) => {
// tab/region recordings (they leak into other tabs today). Computation errs
// toward showing; setting this false restores the prior always-show behavior.
const ENABLE_TAB_SCOPED_UI = true;
+// How long a stop beep claimed by one tab silences the others.
+const STOP_BEEP_DEDUPE_MS = 5000;
const ContentState = (props) => {
const [timer, setTimerInternal] = React.useState(0);
@@ -65,6 +70,14 @@ const ContentState = (props) => {
const prevRecordingRef = useRef(null);
const hydratedRef = useRef(false);
const suppressStopBeepRef = useRef(false);
+ // The ref only silences this tab. Stopping from a tab that isn't the
+ // recording-UI owner made both beep, so the claim has to be cross-tab.
+ const claimStopBeep = () => {
+ suppressStopBeepRef.current = true;
+ try {
+ chrome.storage.local.set({ stopBeepHandledAt: Date.now() });
+ } catch {}
+ };
const suppressStartBeepRef = useRef(false);
const tabIdRef = useRef(null);
const activeTabRef = useRef(null);
@@ -285,7 +298,7 @@ const ContentState = (props) => {
const restartRecording = useCallback(() => {
// Suppress the stop beep: restart transitions recording true→false briefly.
- suppressStopBeepRef.current = true;
+ claimStopBeep();
const sourceTabId = tabIdRef.current ?? activeTabRef.current ?? null;
chrome.storage.local.set({ restarting: true });
setTimeout(() => {
@@ -383,7 +396,7 @@ const ContentState = (props) => {
try {
playBeep(stopBeepRef, "assets/sounds/beep.mp3");
} catch {}
- suppressStopBeepRef.current = true;
+ claimStopBeep();
chrome.runtime.sendMessage(
{ type: "stop-recording-tab", reason: "content-toolbar-stop" },
(res) => {
@@ -479,7 +492,7 @@ const ContentState = (props) => {
const dismissRecording = useCallback((reason = "user-dismiss") => {
setStartFlowOutcome("cancelled");
- suppressStopBeepRef.current = true;
+ claimStopBeep();
chrome.runtime.sendMessage({ type: "clear-recording-alarm" });
chrome.storage.local.set({
restarting: false,
@@ -1553,7 +1566,11 @@ const ContentState = (props) => {
if (suppressStopBeepRef.current) {
suppressStopBeepRef.current = false;
} else {
- playBeep(stopBeepRef, "assets/sounds/beep.mp3");
+ chrome.storage.local.get(["stopBeepHandledAt"], (r) => {
+ const at = Number(r?.stopBeepHandledAt) || 0;
+ if (Date.now() - at < STOP_BEEP_DEDUPE_MS) return;
+ playBeep(stopBeepRef, "assets/sounds/beep.mp3");
+ });
}
}
@@ -2161,47 +2178,6 @@ const ContentState = (props) => {
}
}, [contentState.backgroundEffect, contentState.backgroundEffectsActive]);
- useEffect(() => {
- if (!contentState.parentRef) return;
-
- const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
- if (isMac) return;
-
- const parentDiv = contentState.parentRef;
-
- const elements = parentDiv.querySelectorAll("*");
- elements.forEach((element) => {
- element.classList.add("screenity-scrollbar");
- });
-
- const observer = new MutationObserver((mutationsList) => {
- for (const mutation of mutationsList) {
- if (mutation.type === "childList") {
- const addedNodes = Array.from(mutation.addedNodes);
- const removedNodes = Array.from(mutation.removedNodes);
-
- addedNodes.forEach((node) => {
- if (node.nodeType === Node.ELEMENT_NODE) {
- node.classList.add("screenity-scrollbar");
- }
- });
-
- removedNodes.forEach((node) => {
- if (node.nodeType === Node.ELEMENT_NODE) {
- node.classList.remove("screenity-scrollbar");
- }
- });
- }
- }
- });
-
- observer.observe(parentDiv, { childList: true, subtree: true });
-
- return () => {
- observer.disconnect();
- };
- }, [contentState.parentRef]);
-
// Programmatically add custom scrollbars
useEffect(() => {
if (!contentState.shadowRef) return;
@@ -2243,12 +2219,10 @@ const ContentState = (props) => {
return () => {
observer.disconnect();
};
- }, [
- contentState.parentRef,
- contentState.shadowRef,
- contentState.bigTab,
- contentState.recordingType,
- ]);
+ // The observer already classes nodes as they're added, so re-running this
+ // on bigTab/recordingType only re-walked the whole shadow tree with
+ // querySelectorAll("*") on every popup tab switch to no effect.
+ }, [contentState.shadowRef]);
useEffect(() => {
if (!contentState.hideUI) {
@@ -2311,18 +2285,23 @@ const ContentState = (props) => {
}, []);
// Memoize: a fresh array literal would re-render every consumer per parent update.
+ // Timer stays out of this on purpose, so the 1s tick doesn't invalidate it.
const providerValue = useMemo(
- () => [contentState, setContentState, timer, setTimer],
- [contentState, timer],
+ () => [contentState, setContentState],
+ [contentState],
);
+ const timerValue = useMemo(() => [timer, setTimer], [timer]);
+
return (
<contentStateContext.Provider value={providerValue}>
- {props.children}
- <Shortcuts shortcuts={contentState.shortcuts} />
- {process.env.SCREENITY_DEV_MODE === "true" && (
- <DevHUD contentStateRef={contentStateRef} setContentState={setContentState} />
- )}
+ <timerContext.Provider value={timerValue}>
+ {props.children}
+ <Shortcuts shortcuts={contentState.shortcuts} />
+ {process.env.SCREENITY_DEV_MODE === "true" && (
+ <DevHUD contentStateRef={contentStateRef} setContentState={setContentState} />
+ )}
+ </timerContext.Provider>
</contentStateContext.Provider>
);
};
diff --git a/src/pages/Content/context/messaging/handlers.js b/src/pages/Content/context/messaging/handlers.js
index 77de683..10c31ad 100644
--- a/src/pages/Content/context/messaging/handlers.js
+++ b/src/pages/Content/context/messaging/handlers.js
@@ -20,7 +20,20 @@ export const setupHandlers = () => {
let lastToggleDrawingAt = 0;
const TOGGLE_DRAWING_COOLDOWN_MS = 400;
let projectReadySeq = 0;
+ // Only bounds the fallback runtime copy. The bridge has no size limit.
const LOCAL_PLAYBACK_MAX_BYTES = 250 * 1024 * 1024;
+ const BRIDGE_READY = "screenity-local-playback-bridge-ready";
+ const BRIDGE_REQUEST = "screenity-local-playback-bridge-request";
+ const BRIDGE_RESULT = "screenity-local-playback-bridge-result";
+ const BRIDGE_ORIGIN = (() => {
+ try {
+ return new URL(chrome.runtime.getURL("")).origin;
+ } catch {
+ return null;
+ }
+ })();
+ const BRIDGE_READY_TIMEOUT_MS = 5000;
+ const BRIDGE_BUILD_TIMEOUT_MS = 120000;
let latestLocalPlaybackOffer = null;
let latestLocalPlaybackProjectId = null;
let latestLocalPlaybackSceneId = null;
@@ -102,29 +115,110 @@ export const setupHandlers = () => {
} catch {}
};
- const fetchLocalPlaybackSourceFromExtension = async ({
- offerId,
- projectId,
- sceneId,
- }) => {
- const offerRes = await chrome.runtime.sendMessage({
- type: "cloud-local-playback-get-offer",
- offerId,
- projectId,
- sceneId,
+ // Preferred path: extension-origin iframe posts one Blob back by reference,
+ // O(1) in recording size. The base64 loop below copies every byte and is
+ // capped at LOCAL_PLAYBACK_MAX_BYTES.
+ let bridgeFrame = null;
+ let bridgeReady = null;
+ const bridgePending = new Map();
+
+ const ensureBridgeFrame = () => {
+ // Bridge only talks to the trusted app origin; fail fast into the runtime
+ // fallback instead of waiting out the ready timeout.
+ if (!BRIDGE_ORIGIN || !getProjectMessageTargetOrigin()) {
+ return Promise.reject(new Error("local-playback-bridge-untrusted-origin"));
+ }
+ if (bridgeReady) return bridgeReady;
+ bridgeReady = new Promise((resolve, reject) => {
+ let settled = false;
+ const timer = setTimeout(() => {
+ if (settled) return;
+ settled = true;
+ reject(new Error("local-playback-bridge-timeout"));
+ }, BRIDGE_READY_TIMEOUT_MS);
+
+ const onReady = (event) => {
+ if (event.source !== bridgeFrame?.contentWindow) return;
+ if (event.data?.source !== BRIDGE_READY) return;
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ window.removeEventListener("message", onReady);
+ resolve(bridgeFrame);
+ };
+ window.addEventListener("message", onReady);
+
+ try {
+ bridgeFrame = document.createElement("iframe");
+ bridgeFrame.src = chrome.runtime.getURL("localplaybackbridge.html");
+ bridgeFrame.setAttribute("aria-hidden", "true");
+ bridgeFrame.style.cssText =
+ "position:fixed;width:0;height:0;border:0;opacity:0;pointer-events:none;left:-9999px;";
+ (document.body || document.documentElement).appendChild(bridgeFrame);
+ } catch (err) {
+ settled = true;
+ clearTimeout(timer);
+ reject(err);
+ }
+ }).catch((err) => {
+ // Let a later attempt rebuild the frame rather than caching the failure.
+ bridgeReady = null;
+ teardownBridgeFrame();
+ throw err;
});
- if (!offerRes?.ok || !offerRes.offer) {
- throw new Error("local-playback-offer-unavailable");
+ return bridgeReady;
+ };
+
+ const teardownBridgeFrame = () => {
+ try {
+ bridgeFrame?.remove();
+ } catch {}
+ bridgeFrame = null;
+ };
+
+ const onBridgeMessage = (event) => {
+ if (!bridgeFrame || event.source !== bridgeFrame.contentWindow) return;
+ if (event.data?.source !== BRIDGE_RESULT) return;
+ const pending = bridgePending.get(event.data.requestId);
+ if (!pending) return;
+ bridgePending.delete(event.data.requestId);
+ pending(event.data);
+ };
+ window.addEventListener("message", onBridgeMessage);
+
+ const fetchLocalPlaybackBlobViaBridge = async (offer) => {
+ const frame = await ensureBridgeFrame();
+ const requestId = `${offer.offerId}:${Date.now()}`;
+ const reply = await new Promise((resolve, reject) => {
+ const timer = setTimeout(() => {
+ bridgePending.delete(requestId);
+ reject(new Error("local-playback-bridge-build-timeout"));
+ }, BRIDGE_BUILD_TIMEOUT_MS);
+ bridgePending.set(requestId, (data) => {
+ clearTimeout(timer);
+ resolve(data);
+ });
+ frame.contentWindow.postMessage(
+ { source: BRIDGE_REQUEST, requestId, offer },
+ BRIDGE_ORIGIN,
+ );
+ });
+
+ if (!reply?.ok || !(reply.blob instanceof Blob)) {
+ throw new Error(reply?.error || "local-playback-bridge-no-blob");
}
- const offer = offerRes.offer;
+ return reply;
+ };
+
+ // Fallback for anything the bridge can't serve (frame blocked by page CSP, storage
+ // unreachable). Same output shape, but copies every byte and only works below the cap.
+ const fetchLocalPlaybackBlobViaRuntime = async (offer) => {
if (
- !offer.chunkCount ||
!offer.estimatedBytes ||
offer.estimatedBytes > LOCAL_PLAYBACK_MAX_BYTES
) {
throw new Error("local-playback-offer-too-large-or-empty");
}
-
const parts = [];
for (let i = 0; i < offer.chunkCount; i += 1) {
// eslint-disable-next-line no-await-in-loop
@@ -146,17 +240,52 @@ export const setupHandlers = () => {
const mimeType = chunkRes.chunk.mimeType || "video/webm";
parts.push(new Blob([bytes], { type: mimeType }));
}
+ const blob = new Blob(parts, { type: parts[0]?.type || "video/webm" });
+ return { blob, size: blob.size, mimeType: blob.type };
+ };
- const blob = new Blob(parts, {
- type: parts[0]?.type || "video/webm",
+ const fetchLocalPlaybackSourceFromExtension = async ({
+ offerId,
+ projectId,
+ sceneId,
+ }) => {
+ const offerRes = await chrome.runtime.sendMessage({
+ type: "cloud-local-playback-get-offer",
+ offerId,
+ projectId,
+ sceneId,
});
+ if (!offerRes?.ok || !offerRes.offer) {
+ throw new Error("local-playback-offer-unavailable");
+ }
+ const offer = offerRes.offer;
+ if (!offer.chunkCount || !offer.estimatedBytes) {
+ throw new Error("local-playback-offer-empty");
+ }
+
+ let transport = "bridge";
+ let built;
+ try {
+ built = await fetchLocalPlaybackBlobViaBridge(offer);
+ } catch (bridgeErr) {
+ console.warn(
+ "[Screenity][Content] Local playback bridge unavailable, falling back to runtime copy",
+ { offerId: offer.offerId, error: bridgeErr?.message || bridgeErr },
+ );
+ transport = "runtime";
+ built = await fetchLocalPlaybackBlobViaRuntime(offer);
+ }
+
+ const blob = built.blob;
const url = URL.createObjectURL(blob);
return {
offer,
url,
+ blob,
+ transport,
size: blob.size || 0,
mimeType: blob.type || "video/webm",
- chunkCount: parts.length,
+ chunkCount: offer.chunkCount,
};
};
@@ -206,6 +335,8 @@ export const setupHandlers = () => {
projectId: source.offer.projectId || null,
sceneId: source.offer.sceneId || null,
usedBy: "app-editor",
+ localBytes: source.size || 0,
+ transport: source.transport || null,
});
} catch {}
@@ -241,6 +372,11 @@ export const setupHandlers = () => {
offerId: offer?.offerId || null,
chunkCount: offer?.chunkCount || 0,
estimatedBytes: offer?.estimatedBytes || 0,
+ // Local source covers only the opening of the recording, so the editor
+ // swaps to the remote URL once that exists.
+ partial: Boolean(offer?.partial),
+ availableBytes: offer?.availableBytes || offer?.estimatedBytes || 0,
+ totalBytes: offer?.totalBytes || null,
expiresAt: offer?.expiresAt || null,
source: offer?.source || "indexeddb-screen-chunks",
ready: Boolean(readySource?.url),
@@ -333,6 +469,9 @@ export const setupHandlers = () => {
localBytes: readySource?.size || null,
chunkCount: offer.chunkCount || 0,
estimatedBytes: offer.estimatedBytes || 0,
+ partial: Boolean(offer.partial),
+ availableBytes: offer.availableBytes || offer.estimatedBytes || 0,
+ totalBytes: offer.totalBytes || null,
expiresAt: offer.expiresAt || null,
source: offer.source || "indexeddb-screen-chunks",
},
@@ -1171,6 +1310,12 @@ export const setupHandlers = () => {
localBytes: activeLocalPlaybackSource.size || null,
chunkCount: latestLocalPlaybackOffer.chunkCount || 0,
estimatedBytes: latestLocalPlaybackOffer.estimatedBytes || 0,
+ partial: Boolean(latestLocalPlaybackOffer.partial),
+ availableBytes:
+ latestLocalPlaybackOffer.availableBytes ||
+ latestLocalPlaybackOffer.estimatedBytes ||
+ 0,
+ totalBytes: latestLocalPlaybackOffer.totalBytes || null,
expiresAt: latestLocalPlaybackOffer.expiresAt || null,
};
}
diff --git a/src/pages/Content/popup/components/Dropdown.jsx b/src/pages/Content/popup/components/Dropdown.jsx
index 7d00445..902b27f 100644
--- a/src/pages/Content/popup/components/Dropdown.jsx
+++ b/src/pages/Content/popup/components/Dropdown.jsx
@@ -98,17 +98,31 @@ const Dropdown = (props) => {
});
setLabel(chrome.i18n.getMessage("noCameraDropdownLabel"));
} else {
+ // Toggling on left the device at "none" and the label unset. Adopt the
+ // first camera here, same as the permission grant does.
+ const devices = contentState.videoInput || [];
+ const selected =
+ devices.find(
+ (device) => device.deviceId === contentState.defaultVideoInput
+ ) ||
+ devices[0] ||
+ null;
+ const patch = selected
+ ? {
+ cameraActive: true,
+ defaultVideoInput: selected.deviceId,
+ defaultVideoInputLabel: selected.label || "",
+ }
+ : { cameraActive: true };
setContentState((prevContentState) => ({
...prevContentState,
- cameraActive: true,
+ ...patch,
}));
- chrome.storage.local.set({
- cameraActive: true,
- });
+ chrome.storage.local.set(patch);
setLabel(
- contentState.videoInput.find(
- (device) => device.deviceId === contentState.defaultVideoInput
- ).label
+ selected
+ ? selected.label
+ : chrome.i18n.getMessage("noCameraDropdownLabel")
);
}
} else {
@@ -122,17 +136,29 @@ const Dropdown = (props) => {
});
setLabel(chrome.i18n.getMessage("noMicrophoneDropdownLabel"));
} else {
+ const devices = contentState.audioInput || [];
+ const selected =
+ devices.find(
+ (device) => device.deviceId === contentState.defaultAudioInput
+ ) ||
+ devices[0] ||
+ null;
+ const patch = selected
+ ? {
+ micActive: true,
+ defaultAudioInput: selected.deviceId,
+ defaultAudioInputLabel: selected.label || "",
+ }
+ : { micActive: true };
setContentState((prevContentState) => ({
...prevContentState,
- micActive: true,
+ ...patch,
}));
- chrome.storage.local.set({
- micActive: true,
- });
+ chrome.storage.local.set(patch);
setLabel(
- contentState.audioInput.find(
- (device) => device.deviceId === contentState.defaultAudioInput
- ).label
+ selected
+ ? selected.label
+ : chrome.i18n.getMessage("noMicrophoneDropdownLabel")
);
}
}
diff --git a/src/pages/Content/popup/styles/layout/_PopupContainer.scss b/src/pages/Content/popup/styles/layout/_PopupContainer.scss
index 807e644..c4e8377 100644
--- a/src/pages/Content/popup/styles/layout/_PopupContainer.scss
+++ b/src/pages/Content/popup/styles/layout/_PopupContainer.scss
@@ -29,8 +29,8 @@
justify-content: center;
gap: 8px;
z-index: 999999999;
+ // Opaque background, so the blur was never visible. See _Toolbar.scss.
background: rgba(240, 238, 238, 1);
- backdrop-filter: blur(10px);
padding-left: 8px;
padding-right: 8px;
padding-top: 6px;
diff --git a/src/pages/Content/region/Region.jsx b/src/pages/Content/region/Region.jsx
index d8b6878..9f69dda 100644
--- a/src/pages/Content/region/Region.jsx
+++ b/src/pages/Content/region/Region.jsx
@@ -275,7 +275,10 @@ const ResizableBox = () => {
outline: hideRegionUI ? "none" : "2px dashed #D9D9D9",
// Push outline inside the box so it isn't visible in recordings.
outlineOffset: "2px",
- boxShadow: "0 0 0 9999px rgba(0, 0, 0, 0.2)",
+ // Dims outside the crop box while choosing it. Once recording starts
+ // it's off-camera anyway, and this spread is a viewport-sized
+ // translucent layer the page below has to keep compositing.
+ boxShadow: hideRegionUI ? "none" : "0 0 0 9999px rgba(0, 0, 0, 0.2)",
borderRadius: "5px",
zIndex: 2,
boxSizing: "border-box",
diff --git a/src/pages/Content/styles/app.css b/src/pages/Content/styles/app.css
index dad72f0..9112af2 100644
--- a/src/pages/Content/styles/app.css
+++ b/src/pages/Content/styles/app.css
@@ -80,7 +80,6 @@ iframe.screenity-iframe {
gap: 8px;
z-index: 99999999999999;
background: rgb(240, 238, 238);
- backdrop-filter: blur(10px);
padding-left: 8px;
padding-right: 8px;
padding-top: 6px;
@@ -1570,7 +1569,6 @@ body.driver-active .OnboardingArrow {
gap: 8px;
z-index: 999999999;
background: rgb(240, 238, 238);
- backdrop-filter: blur(10px);
padding-left: 8px;
padding-right: 8px;
padding-top: 6px;
@@ -4617,5 +4615,3 @@ button {
.driver-popover.ScreenityOnboardingPopover {
z-index: 2147483647 !important;
}
-
-/*# sourceMappingURL=app.css.map */
diff --git a/src/pages/Content/toolbar/layout/ToolbarWrap.jsx b/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
index 82e2e9d..d836341 100644
--- a/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
+++ b/src/pages/Content/toolbar/layout/ToolbarWrap.jsx
@@ -23,7 +23,7 @@ import Toast from "../components/Toast";
import { CloseIconPopup } from "../components/SVG";
-import { contentStateContext } from "../../context/ContentState";
+import { contentStateContext, timerContext } from "../../context/ContentState";
import {
GrabIcon,
@@ -45,8 +45,8 @@ import {
import MicToggle from "../components/MicToggle";
const ToolbarWrap = () => {
- const [contentState, setContentState, t, setT] =
- useContext(contentStateContext);
+ const [contentState, setContentState] = useContext(contentStateContext);
+ const [t] = useContext(timerContext);
const [mode, setMode] = React.useState("");
const modeRef = React.useRef(mode);
const [hovering, setHovering] = React.useState(false);
diff --git a/src/pages/Content/toolbar/styles/layout/_Toolbar.scss b/src/pages/Content/toolbar/styles/layout/_Toolbar.scss
index ce5fc57..b266cf9 100644
--- a/src/pages/Content/toolbar/styles/layout/_Toolbar.scss
+++ b/src/pages/Content/toolbar/styles/layout/_Toolbar.scss
@@ -41,8 +41,9 @@ iframe.screenity-iframe {
justify-content: center;
gap: 8px;
z-index: 99999999999999;
+ // No backdrop-filter: the background is fully opaque, so it blurred nothing
+ // while still forcing a compositing layer and a backdrop sample per frame.
background: rgba(240, 238, 238, 1);
- backdrop-filter: blur(10px);
padding-left: 8px;
padding-right: 8px;
padding-top: 6px;
diff --git a/src/pages/Content/utils/BlurTool.jsx b/src/pages/Content/utils/BlurTool.jsx
index cfc36ba..3aa7482 100644
--- a/src/pages/Content/utils/BlurTool.jsx
+++ b/src/pages/Content/utils/BlurTool.jsx
@@ -31,7 +31,15 @@ const BlurTool = () => {
}
}, [contentState.showExtension]);
+ // Only while picking: blurred elements keep their class/filter rule
+ // regardless, so persistence doesn't need these. Capture-phase on
+ // document.body, ahead of the host page's own handlers on every page we inject into.
useLayoutEffect(() => {
+ if (!contentState.blurMode) return;
+ // useLayoutEffect runs before the effect that syncs blurModeRef, so seed it
+ // here or the first events after entering blur mode take the disabled path.
+ blurModeRef.current = true;
+
const updateOutline = (target) => {
if (!target) return;
hoveredElementRef.current = target;
@@ -135,8 +143,14 @@ const BlurTool = () => {
document.body.removeEventListener("mouseup", handleMouseUp, true);
document.body.removeEventListener("click", handleElementClick, true);
document.removeEventListener("scroll", handleScroll, true);
+ // handleMouseMove owns the pointer cursor and the hover outline, so
+ // leaving blur mode has to undo both here; nothing else is listening.
+ document.body.style.cursor = "auto";
+ hoveredElementRef.current = null;
+ setShowOutline(false);
+ setOutlineRect(null);
};
- }, []);
+ }, [contentState.blurMode]);
return (
<div>
diff --git a/src/pages/Content/utils/CursorModes.jsx b/src/pages/Content/utils/CursorModes.jsx
index 27e7e23..3d9d2a3 100644
--- a/src/pages/Content/utils/CursorModes.jsx
+++ b/src/pages/Content/utils/CursorModes.jsx
@@ -86,6 +86,9 @@ const CursorModes = () => {
// on modern trackpads.
const stateRafRef = useRef(null);
const scheduleCursorUpdate = () => {
+ // With no effect active the callback below does nothing, and we're injected on
+ // every page, so skip the rAF rather than schedule an empty frame per move.
+ if (effectsRef.current.size === 0) return;
if (stateRafRef.current !== null) return;
stateRafRef.current = requestAnimationFrame(() => {
stateRafRef.current = null;
diff --git a/src/pages/EditorApp/context/ContentState.jsx b/src/pages/EditorApp/context/ContentState.jsx
index 37cb053..e412802 100644
--- a/src/pages/EditorApp/context/ContentState.jsx
+++ b/src/pages/EditorApp/context/ContentState.jsx
@@ -41,6 +41,9 @@ const loadMb = () => {
// can stall on large recordings. Flag-gated; download falls back to the on-
// demand remux and the fragmented file if disabled or if the finalize fails.
const ENABLE_EAGER_MP4_FINALIZE = true;
+// Long enough to outlast the load-time blob swaps, short enough that the
+// pre-warm still beats the user reaching Download.
+const EAGER_MP4_SETTLE_MS = 1500;
// Route the MP4 -> WebM re-encode through the offscreen worker (OPFS-streamed,
// bounded memory) instead of the in-editor BufferTarget path that OOMs on large
@@ -1614,9 +1617,8 @@ const ContentState = (props) => {
const errCode = message?.errorCode || "UNKNOWN";
contentStateRef.current.openModal(
chrome.i18n.getMessage("opfsLoadErrorTitle"),
- // Never surface the raw internal `why` (e.g. "WebCodecs produced
- // no encoded video within 12000ms") — it's engineer-facing. The
- // raw string still rides along in the diag bundle via diagForward.
+ // Never show the raw internal `why` (e.g. "WebCodecs produced no
+ // encoded video within 12000ms"). It still reaches diagForward.
chrome.i18n.getMessage("opfsLoadErrorDescription"),
null,
chrome.i18n.getMessage("permissionsModalDismiss"),
@@ -3029,7 +3031,17 @@ const ContentState = (props) => {
return new Blob([target.buffer], { type: "video/mp4" });
};
- // OPFS sync access handle in an offscreen worker; bypasses BufferTarget's 2 GB cap
+ // ~0.08 bits per pixel per frame for screen content. 1080p30 lands on the
+ // converter's own 5 Mbps default, so only higher resolutions actually move.
+ const encodeBitrateForSource = () => {
+ const w = Number(contentStateRef.current?.width) || 0;
+ const h = Number(contentStateRef.current?.height) || 0;
+ if (!(w > 0) || !(h > 0)) return null;
+ const raw = Math.round(w * h * 30 * 0.08);
+ return Math.min(24_000_000, Math.max(2_000_000, raw));
+ };
+
+ // OPFS sync access handle in an offscreen worker, bypassing BufferTarget's 2GB cap.
const remuxViaOffscreenOpfs = async (
fragmentedBlob,
onProgress,
@@ -3128,6 +3140,7 @@ const ContentState = (props) => {
requestId,
inputFileName,
outputFileName,
+ videoBitrate: encodeBitrateForSource(),
});
if (!response || response.ok !== true) {
devLog("offscreen-remux-response-bad", response);
@@ -3268,13 +3281,64 @@ const ContentState = (props) => {
return { blob: remuxedBlob, path: remuxPath };
};
+ // Ask mediabunny, not a hardcoded profile string. isConfigSupported with
+ // avc1.42E01E reports false on machines that encode H.264 High fine.
+ const canEncodeMp4Video = async () => {
+ try {
+ const mb = await loadMb();
+ if (typeof mb.canEncodeVideo === "function") {
+ return await mb.canEncodeVideo("avc");
+ }
+ } catch {}
+ return false;
+ };
+
+ // WebM sources need a full re-encode, not a remux. MP4 cannot carry VP8 or
+ // VP9 in any form players accept.
+ const transcodeToMp4 = async (blob) => {
+ const inputBytes = blob?.size || 0;
+ diagForward("editor-transcode-start", { inputBytes });
+ try {
+ const out = await runRemuxWithStallGuard(
+ (pg) => remuxViaOffscreenOpfs(blob, pg, "mp4x"),
+ sharedFinalizeProgress,
+ );
+ diagForward("editor-transcode-ok", {
+ inputBytes,
+ outputBytes: out?.size || 0,
+ });
+ return { blob: out, path: "webm-transcode" };
+ } catch (err) {
+ diagForward("editor-transcode-fail", {
+ inputBytes,
+ reason: String(err?.message || err).slice(0, 120),
+ });
+ return { blob: null, path: null };
+ }
+ };
+
// Cache + dedupe the standard-MP4 finalize, keyed on the exact source blob so
- // an edit (which produces a new blob) re-finalizes. Lets the result be pre-
- // warmed in the background on editor-ready and reused instantly on download.
+ // an edit (which produces a new blob) re-finalizes.
const ensureStandardMp4 = () => {
const blob = contentStateRef.current?.blob;
- if (!blob || blob.type !== "video/mp4") {
- return Promise.resolve({ blob: null, path: null });
+ if (!blob) return Promise.resolve({ blob: null, path: null });
+ if (blob.type !== "video/mp4") {
+ const cur = standardMp4Ref.current;
+ if (cur && cur.forBlob === blob && cur.status !== "failed") {
+ return cur.promise;
+ }
+ const promise = transcodeToMp4(blob).then((res) => {
+ standardMp4Ref.current = {
+ status: res.blob ? "ready" : "failed",
+ promise,
+ forBlob: blob,
+ blob: res.blob,
+ path: res.path,
+ };
+ return res;
+ });
+ standardMp4Ref.current = { status: "pending", promise, forBlob: blob };
+ return promise;
}
const cur = standardMp4Ref.current;
if (
@@ -3306,7 +3370,12 @@ const ContentState = (props) => {
if (!contentState.ready) return;
if (contentState.blob?.type !== "video/mp4") return;
if (standardMp4Ref.current?.forBlob === contentState.blob) return;
- ensureStandardMp4().catch(() => {});
+ // The cache keys on blob identity and loading swaps the blob 2-3 times, so
+ // every swap started a conversion: 3 remuxes of one recording in 455ms.
+ const settle = setTimeout(() => {
+ ensureStandardMp4().catch(() => {});
+ }, EAGER_MP4_SETTLE_MS);
+ return () => clearTimeout(settle);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contentState.ready, contentState.blob]);
@@ -3317,11 +3386,38 @@ const ContentState = (props) => {
// downloading is the real lock
if (latest.downloading) return;
downloadCancelledRef.current = false;
+ const sourceType = latest.blob?.type || null;
+ // ensureStandardMp4 only finalizes fragmented MP4. Until transcode lands, a
+ // non-MP4 source ships under its real extension, not a renamed .mp4.
+ const sourceIsMp4 = sourceType === "video/mp4";
diagForward("editor-download-start", {
kind: "mp4",
bytes: latest.blob?.size ?? null,
+ sourceType,
});
+ // A WebM source is transcoded below. Only refuse when the machine has no
+ // H.264 encoder at all, and then hand back the source under its real name.
+ if (!sourceIsMp4 && !(await canEncodeMp4Video())) {
+ diagForward("editor-download-nonmp4-source", {
+ sourceType,
+ reason: "no-avc-encoder",
+ });
+ const ext = String(sourceType || "").includes("webm") ? ".webm" : ".bin";
+ try {
+ const url = URL.createObjectURL(latest.blob);
+ await requestDownload(url, ext);
+ URL.revokeObjectURL(url);
+ setContentState((prev) => ({ ...prev, saved: true }));
+ } catch (err) {
+ diagForward("editor-download-fail", {
+ kind: "mp4",
+ reason: `nonmp4-source:${String(err?.message || err).slice(0, 60)}`,
+ });
+ }
+ return;
+ }
+
setContentState((prev) => ({
...prev,
downloading: true,
@@ -3383,10 +3479,17 @@ const ContentState = (props) => {
}
} catch (err) {
console.error("MP4 download failed:", err);
- // tier 3: serve fMP4 as-is so the user doesn't lose the recording
+ // tier 3: serve the source as-is so the user doesn't lose the recording.
+ // The guard above returns early only when there's no H.264 encoder, so a
+ // WebM source whose transcode failed still lands here and has to keep its
+ // real extension. WebM bytes named .mp4 won't open in QuickTime.
try {
- const url = URL.createObjectURL(contentState.blob);
- await requestDownload(url, ".mp4");
+ const fallbackBlob = contentState.blob;
+ const fallbackExt = String(fallbackBlob?.type || "").includes("webm")
+ ? ".webm"
+ : ".mp4";
+ const url = URL.createObjectURL(fallbackBlob);
+ await requestDownload(url, fallbackExt);
URL.revokeObjectURL(url);
setContentState((prev) => ({ ...prev, saved: true }));
} catch (fallbackErr) {
diff --git a/src/pages/EditorApp/recorderStorage/opfsChunkReader.js b/src/pages/EditorApp/recorderStorage/opfsChunkReader.js
index 29c45f3..fb60714 100644
--- a/src/pages/EditorApp/recorderStorage/opfsChunkReader.js
+++ b/src/pages/EditorApp/recorderStorage/opfsChunkReader.js
@@ -14,6 +14,8 @@ const FINALIZE_HARD_TIMEOUT_MS = 60_000;
const FINALIZE_ABSOLUTE_TIMEOUT_MS = 300_000;
// don't flash the "finalizing" UI for the common <1s case
const SLOW_FINALIZE_NOTIFY_MS = 600;
+// 4x the recorder's 1s heartbeat, so a killed writer still fails fast.
+const WRITER_HEARTBEAT_STALE_MS = 4000;
const diagForward = (event, data) => {
try {
@@ -42,17 +44,29 @@ export class OpfsChunkReader {
async _readRefState() {
try {
- const { lastRecordingBackendRef, lastRecordingFinalizedFileName } =
- await chrome.storage.local.get([
- "lastRecordingBackendRef",
- "lastRecordingFinalizedFileName",
- ]);
+ const {
+ lastRecordingBackendRef,
+ lastRecordingFinalizedFileName,
+ lastRecordingWriterHeartbeat,
+ } = await chrome.storage.local.get([
+ "lastRecordingBackendRef",
+ "lastRecordingFinalizedFileName",
+ "lastRecordingWriterHeartbeat",
+ ]);
+ // Only a live recorder refreshes this, and only while finalizing. The file
+ // name has to match or a leftover from the previous recording counts.
+ const hb = lastRecordingWriterHeartbeat;
+ const writerAlive =
+ hb?.fileName === this._fileName &&
+ Number.isFinite(hb?.at) &&
+ Date.now() - hb.at < WRITER_HEARTBEAT_STALE_MS;
return {
sameFile: lastRecordingBackendRef?.fileName === this._fileName,
finalized: lastRecordingFinalizedFileName === this._fileName,
+ writerAlive,
};
} catch {
- return { sameFile: false, finalized: false };
+ return { sameFile: false, finalized: false, writerAlive: false };
}
}
@@ -87,7 +101,7 @@ export class OpfsChunkReader {
// !sameFile means a newer recording replaced the ref (our writer is
// gone, or we're opening an orphan file). sameFile && finalized means
- // the writer closed cleanly. otherwise wait for one of those — but
+ // the writer closed cleanly. otherwise wait for one of those, but
// also watch the file size: if the writer dies without emitting the
// finalized marker (recorder tab killed, SW restart mid-flush, track
// ended mid-write), polling will time out at 60s for no reason. When
@@ -102,6 +116,7 @@ export class OpfsChunkReader {
const SIZE_STABLE_MIN_WAIT_MS = 1500;
let writerDead = false;
let writerEmpty = false;
+ let writerAlive = false;
while (true) {
const elapsed = Date.now() - startedAt;
// only time out once the size stops changing; a still-growing file
@@ -119,10 +134,10 @@ export class OpfsChunkReader {
notifySlow();
}
await wait(FINALIZE_POLL_INTERVAL_MS);
- ({ sameFile, finalized } = await this._readRefState());
+ ({ sameFile, finalized, writerAlive } = await this._readRefState());
if (finalized || !sameFile) break;
// Size-stability check. Probing the file inside the poll loop is
- // cheap — getFile is metadata only on chromium.
+ // cheap: getFile is metadata only on chromium.
try {
const probe = await handle.getFile();
if (probe.size !== lastSize) {
@@ -131,7 +146,10 @@ export class OpfsChunkReader {
} else if (
sizeStableSince > 0 &&
Date.now() - sizeStableSince >= SIZE_STABLE_MS &&
- Date.now() - startedAt >= SIZE_STABLE_MIN_WAIT_MS
+ Date.now() - startedAt >= SIZE_STABLE_MIN_WAIT_MS &&
+ // A flush writes nothing for up to 15s per track, so a static file
+ // only means a dead writer once it stops checking in.
+ !writerAlive
) {
// A file stuck below the minimum (usually 0 bytes, from a session
// that never captured) used to be excluded here, so it waited out
diff --git a/src/pages/LocalPlaybackBridge/index.html b/src/pages/LocalPlaybackBridge/index.html
new file mode 100644
index 0000000..c9b626e
--- /dev/null
+++ b/src/pages/LocalPlaybackBridge/index.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>Screenity local playback bridge</title>
+ </head>
+ <body></body>
+</html>
diff --git a/src/pages/LocalPlaybackBridge/index.js b/src/pages/LocalPlaybackBridge/index.js
new file mode 100644
index 0000000..505f2f7
--- /dev/null
+++ b/src/pages/LocalPlaybackBridge/index.js
@@ -0,0 +1,135 @@
+// Hands the editor a recording's local bytes as one Blob, so playback starts
+// before the upload finishes. Iframe, not chrome.runtime: the base64 runtime
+// path was 92 round trips and ~295MB of copies for a 221MB file, capped at
+// 250MB, and Chrome 148 message_serialization still copies. postMessage passes
+// the Blob by reference (0.1ms for 400MB), and an extension-origin frame sees
+// the recorder's OPFS tree.
+
+import {
+ openExistingChunksStore,
+} from "../CloudRecorder/recorderStorage/chooseChunksStore";
+
+const APP_ORIGIN = (() => {
+ try {
+ const base = process.env.SCREENITY_APP_BASE;
+ return base ? new URL(base).origin : null;
+ } catch {
+ return null;
+ }
+})();
+
+const READY = "screenity-local-playback-bridge-ready";
+const REQUEST = "screenity-local-playback-bridge-request";
+const RESULT = "screenity-local-playback-bridge-result";
+
+const post = (payload) => {
+ if (!APP_ORIGIN || window.parent === window) return;
+ window.parent.postMessage({ ...payload, source: payload.source }, APP_ORIGIN);
+};
+
+const resolveStore = (offer) => {
+ if (offer?.storageBackend === "opfs" && offer.opfsSessionId) {
+ return openExistingChunksStore({
+ sessionId: offer.opfsSessionId,
+ track: "screen",
+ backend: "opfs",
+ }).store;
+ }
+ return openExistingChunksStore({
+ sessionId: offer?.opfsSessionId || null,
+ track: "screen",
+ backend: "idb",
+ }).store;
+};
+
+// getFile()'s Blob is a live OPFS view and throws NotFoundError once purged,
+// hence materialising each chunk. One chunk at a time caps peak memory, and the
+// final concat is by reference.
+const buildBlob = async (offer) => {
+ const store = resolveStore(offer);
+ const mimeType = offer.container || "video/webm";
+ const parts = [];
+ let bytes = 0;
+
+ for (let index = 0; index < offer.chunkCount; index += 1) {
+ // eslint-disable-next-line no-await-in-loop
+ const item = await store.getItem(`chunk_${index}`).catch(() => null);
+ if (!item?.chunk) {
+ throw new Error(`local-playback-chunk-missing:${index}`);
+ }
+ // eslint-disable-next-line no-await-in-loop
+ const buffer = await item.chunk.arrayBuffer();
+ parts.push(new Blob([buffer], { type: mimeType }));
+ bytes += buffer.byteLength;
+ }
+
+ if (!parts.length) throw new Error("local-playback-no-chunks");
+ return { blob: new Blob(parts, { type: mimeType }), bytes };
+};
+
+let inFlight = null;
+
+const onMessage = async (event) => {
+ if (!APP_ORIGIN || event.origin !== APP_ORIGIN) return;
+ if (event.source !== window.parent) return;
+ const data = event.data;
+ if (!data || data.source !== REQUEST) return;
+
+ const requestedOfferId = data.offer?.offerId || null;
+ const requestId = data.requestId || null;
+ if (!requestedOfferId) {
+ post({ source: RESULT, requestId, ok: false, error: "invalid-offer" });
+ return;
+ }
+
+ // Answer instead of going silent: the caller's only other signal is a 120s
+ // timeout, which stalled the second scene of a multi-scene handoff.
+ if (inFlight) {
+ post({ source: RESULT, requestId, ok: false, error: "bridge-busy" });
+ return;
+ }
+ inFlight = requestedOfferId;
+
+ const startedAt = Date.now();
+ try {
+ // Never read from the page-supplied offer: any script on the app origin can
+ // post here, and an unchecked backend plus chunkCount reads any recording.
+ const redeemed = await chrome.runtime.sendMessage({
+ type: "cloud-local-playback-redeem-offer",
+ offerId: requestedOfferId,
+ });
+ if (!redeemed?.ok || !redeemed.offer?.chunkCount) {
+ throw new Error(redeemed?.error || "local-playback-offer-unavailable");
+ }
+ const offer = redeemed.offer;
+ const { blob, bytes } = await buildBlob(offer);
+ post({
+ source: RESULT,
+ requestId,
+ ok: true,
+ offerId: offer.offerId,
+ blob,
+ size: bytes,
+ mimeType: blob.type,
+ chunkCount: offer.chunkCount,
+ elapsedMs: Date.now() - startedAt,
+ });
+ } catch (err) {
+ post({
+ source: RESULT,
+ requestId,
+ ok: false,
+ offerId: requestedOfferId,
+ error: err?.message || "local-playback-bridge-failed",
+ elapsedMs: Date.now() - startedAt,
+ });
+ } finally {
+ inFlight = null;
+ }
+};
+
+window.addEventListener("message", (event) => {
+ void onMessage(event);
+});
+
+post({ source: READY });
diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsx
index 5d8aed1..df8df21 100644
--- a/src/pages/Recorder/Recorder.jsx
+++ b/src/pages/Recorder/Recorder.jsx
@@ -15,7 +15,13 @@ import {
constrainTrackDown,
enforceOversampleRatio,
} from "../utils/captureResolution";
-import { getBitrates, getResolutionForQuality } from "./recorderConfig";
+import {
+ getBitrates,
+ getResolutionForQuality,
+ computeTargetVideoBps,
+ getFreeCaptureCaps,
+ orientBoxToSource,
+} from "./recorderConfig";
import {
WebCodecsRecorder,
preloadWebCodecsModules,
@@ -30,6 +36,7 @@ import { getUserMediaWithFallback } from "../utils/mediaDeviceFallback";
import { startAudioStream as acquireMicStream } from "../utils/startAudioStream";
import { shouldAcquireMicAtStart } from "../utils/micAcquisitionPolicy";
import { attachAudioContextWatchdog } from "../utils/audioContextWatchdog";
+import { beginFinalizeHeartbeat } from "../utils/finalizeHeartbeat";
import { IS_OFFSCREEN_HOST } from "../utils/recordingHost";
import { sendSystemAudioGuidanceToast } from "../utils/systemAudioGuidance";
import {
@@ -139,8 +146,6 @@ function logRecordingSnapshot(label, data) {
debug(`Recording snapshot: ${label}`, data);
}
-const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
-
const QUALITY_ORDER = ["240p", "360p", "480p", "720p", "1080p", "4k"];
const clampQualityValue = (value, maxValue) => {
@@ -151,43 +156,6 @@ const clampQualityValue = (value, maxValue) => {
: max;
};
-const getFreeCaptureCaps = async () => {
- try {
- const { isLoggedIn, isSubscribed } = await chrome.storage.local.get([
- "isLoggedIn",
- "isSubscribed",
- ]);
- const isPro = Boolean(isLoggedIn && isSubscribed);
- return {
- isPro,
- maxQuality: "1080p",
- maxFps: 60,
- };
- } catch {
- return {
- isPro: false,
- maxQuality: "1080p",
- maxFps: 60,
- };
- }
-};
-
-// bits/pixel/sec. 0.10 put 1080p30 at 6.2 Mbps, under the 0.15-0.25 screen
-// content wants, and text edges are the first thing H.264 discards.
-const VIDEO_BPP_FPS_PRO = 0.15;
-const VIDEO_BPP_FPS_FREE = 0.08;
-const VIDEO_BPS_MIN = 4_000_000;
-const VIDEO_BPS_MAX = 24_000_000;
-
-const computeTargetVideoBps = (width, height, fps, isPro = false) => {
- const pixels = Number(width) * Number(height);
- const rate = Number.isFinite(fps) && fps > 0 ? fps : 30;
- const factor = isPro ? VIDEO_BPP_FPS_PRO : VIDEO_BPP_FPS_FREE;
- const target = Math.round(pixels * rate * factor);
- return clamp(target, VIDEO_BPS_MIN, VIDEO_BPS_MAX);
-};
-
-
const Recorder = () => {
const isRestarting = useRef(false);
const pendingStartAfterRestart = useRef(false);
@@ -318,6 +286,9 @@ const Recorder = () => {
const aCtx = useRef(null);
const destination = useRef(null);
+ // Only set when we force the bus to mono: getSettings() can report 2 while the
+ // node mixes mono. Null lets the encoder read the track.
+ const audioBusChannels = useRef(null);
const audioInputSource = useRef(null);
const audioOutputSource = useRef(null);
const audioInputGain = useRef(null);
@@ -1399,8 +1370,9 @@ const Recorder = () => {
const effectiveQualityValue = isPro
? qualityValue
: clampQualityValue(qualityValue, maxQuality);
- const { audioBitsPerSecond, videoBitsPerSecond: bitratePreset } =
- getBitrates(effectiveQualityValue);
+ const { audio: audioBitsPerSecond, video: bitratePreset } = getBitrates(
+ effectiveQualityValue,
+ );
let videoBitsPerSecond = bitratePreset;
debug("Bitrates resolved", {
@@ -1602,10 +1574,17 @@ const Recorder = () => {
const videoTrack = liveStream.current?.getVideoTracks()[0] ?? null;
const settings = videoTrack?.getSettings() || {};
- const { width: qualityWidth, height: qualityHeight } =
- getResolutionForQuality(effectiveQualityValue);
- const trackWidth = settings.width ?? qualityWidth ?? 1920;
- const trackHeight = settings.height ?? qualityHeight ?? 1080;
+ const tier = getResolutionForQuality(effectiveQualityValue);
+ const trackWidth = settings.width ?? tier.width ?? 1920;
+ const trackHeight = settings.height ?? tier.height ?? 1080;
+ // The tier is written landscape and would bind on height against a portrait
+ // source; orient it to the source first so the tier caps the long edge, not the shape.
+ const { width: qualityWidth, height: qualityHeight } = orientBoxToSource(
+ trackWidth,
+ trackHeight,
+ tier.width,
+ tier.height,
+ );
// Fit inside the tier with aspect preserved, matching the encoder. Clamping
// each axis separately overstated the pixel count on non-16:9 sources
// (3440x1440 encodes to 1920x804), and the bitrate below derives from these.
@@ -1973,6 +1952,7 @@ const Recorder = () => {
fps,
videoBitrate: videoBitsPerSecond,
audioBitrate: hasAudioTrack ? audioBitsPerSecond : undefined,
+ audioChannels: audioBusChannels.current,
enableAudio: hasAudioTrack,
videoEncoderConfig: selectedVideoConfig,
containerKind,
@@ -3153,6 +3133,14 @@ const Recorder = () => {
// sequential awaits under contention, delaying editor-open.
void updateFreeFinalizeStatus("stopping", 0);
+ // Keeps the editor's reader waiting through a long flush instead of
+ // reading a truncated file. Cleared in the finally below.
+ const stopFinalizeHeartbeat = beginFinalizeHeartbeat(
+ chunkBackendRef.current?.backend === "opfs"
+ ? chunkBackendRef.current?.fileName
+ : null,
+ );
+
stopSessionHeartbeat();
stopRecordingTick();
persistSessionState("stopping");
@@ -3203,6 +3191,8 @@ const Recorder = () => {
}
} catch (err) {
debugError("stopRecording() error while stopping recorder", err);
+ } finally {
+ stopFinalizeHeartbeat();
}
await waitForDrain();
@@ -3891,6 +3881,24 @@ const Recorder = () => {
// gate mixing on `data.systemAudio`.
const sysTracks = helperVideoStream.current.getAudioTracks();
debug("System/tab audio tracks", sysTracks.length, "systemAudio:", data.systemAudio);
+
+ // Mic-only: a stereo destination just duplicates a mono mic, halving effective
+ // bitrate. Must be set before any source connects.
+ audioBusChannels.current = null;
+ try {
+ const micTrackForBus =
+ helperAudioStream.current?.getAudioTracks?.()[0] || null;
+ const willMixSystemAudio = sysTracks.length > 0 && data.systemAudio;
+ if (
+ micTrackForBus &&
+ !willMixSystemAudio &&
+ micTrackForBus.getSettings?.()?.channelCount === 1
+ ) {
+ destination.current.channelCount = 1;
+ audioBusChannels.current = 1;
+ }
+ } catch {}
+
if (sysTracks.length > 0 && data.systemAudio) {
const sysTrack = sysTracks[0];
const sysSource = aCtx.current.createMediaStreamSource(
diff --git a/src/pages/Recorder/recorderConfig.js b/src/pages/Recorder/recorderConfig.js
index 52efe70..5c0067e 100644
--- a/src/pages/Recorder/recorderConfig.js
+++ b/src/pages/Recorder/recorderConfig.js
@@ -42,3 +42,50 @@ export const VIDEO_QUALITIES = {
export function getResolutionForQuality(qualityValue = "default") {
return VIDEO_QUALITIES[qualityValue] || VIDEO_QUALITIES.default;
}
+
+// bits/pixel/sec. 0.10 put 1080p30 at 6.2 Mbps, under the 0.15-0.25 screen
+// content wants, and text edges are the first thing H.264 discards.
+export const VIDEO_BPP_FPS_PRO = 0.15;
+export const VIDEO_BPP_FPS_FREE = 0.08;
+export const VIDEO_BPS_MIN = 4_000_000;
+export const VIDEO_BPS_MAX = 24_000_000;
+
+export function computeTargetVideoBps(width, height, fps, isPro = false) {
+ const pixels = Number(width) * Number(height);
+ const rate = Number.isFinite(fps) && fps > 0 ? fps : 30;
+ const factor = isPro ? VIDEO_BPP_FPS_PRO : VIDEO_BPP_FPS_FREE;
+ const target = Math.round(pixels * rate * factor);
+ return Math.max(VIDEO_BPS_MIN, Math.min(VIDEO_BPS_MAX, target));
+}
+
+export async function getFreeCaptureCaps() {
+ try {
+ const { isLoggedIn, isSubscribed } = await chrome.storage.local.get([
+ "isLoggedIn",
+ "isSubscribed",
+ ]);
+ return {
+ isPro: Boolean(isLoggedIn && isSubscribed),
+ maxQuality: "1080p",
+ maxFps: 60,
+ };
+ } catch {
+ return { isPro: false, maxQuality: "1080p", maxFps: 60 };
+ }
+}
+
+// Every encode cap we ship is landscape, so a portrait source binds on HEIGHT
+// and collapses the width (412x924 encoded to 482x1080). Orient the box to the
+// source first so a cap means pixels on the long edge, not a shape.
+export function orientBoxToSource(sourceWidth, sourceHeight, boxWidth, boxHeight) {
+ const sw = Number(sourceWidth);
+ const sh = Number(sourceHeight);
+ const bw = Number(boxWidth);
+ const bh = Number(boxHeight);
+ if (![sw, sh, bw, bh].every((n) => Number.isFinite(n) && n > 0)) {
+ return { width: boxWidth, height: boxHeight };
+ }
+ return sh > sw === bh > bw
+ ? { width: bw, height: bh }
+ : { width: bh, height: bw };
+}
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
index 1c36689..dc2197f 100644
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
@@ -19,6 +19,7 @@ import {
computeAvcCappedDimensions,
computeEffectiveQueueCap,
computeStartupQueueCap,
+ applyEncoderConfigOverrides,
computeSteadyQueueCap,
isAacRateInSpec,
isReclaimErrorMessage,
@@ -33,6 +34,7 @@ import {
STARTUP_QUEUE_CAP,
} from "./recorderLogic";
import { getStartupFlags } from "./startupFlags";
+import { orientBoxToSource } from "../recorderConfig";
import { claimPrewarmedEncoder } from "../encoderPrewarm";
// Lazy-load Mp4MuxerWrapper so its ~3MB mediabunny static import doesn't
// drag into recorder.bundle.js cold start (dominates click-record latency).
@@ -112,6 +114,23 @@ export class WebCodecsRecorder {
this.audioSampleRate = null;
this.audioChannelCount = null;
this._firstAudioFrameSampleRate = null;
+ // µs written before the last stamp-rate change (folded in encodeAudioData).
+ this._audioTimeBaseUs = 0;
+ this._audioStampRate = null;
+ // Wall-clock reconciliation state. See reconcileAudioClock in readAudioLoop.
+ this._audioClockAnchorUs = null;
+ this._audioDriftWindowStartUs = null;
+ this._audioDriftWindowMinUs = Infinity;
+ this._injectedCatchUpSilenceEvents = 0;
+ this._injectedCatchUpSilenceUs = 0;
+ this._injectedCatchUpSettleUs = 0;
+ // Heartbeat audioDiag counters. See getAudioDiag.
+ this._audioDataReceived = 0;
+ this._audioDataEncoded = 0;
+ this._audioReceivedUs = 0;
+ this._paddedSilenceCount = 0;
+ this._audioClockAnchorInitialUs = null;
+ this._lastAudioLagUs = null;
this.resizeCanvas = null;
this.resizeCtx = null;
@@ -229,11 +248,11 @@ export class WebCodecsRecorder {
this._adoptedDecoderConfig = null;
this._forceNextKeyframe = false;
this._droppedForBackpressureCount = 0;
- // Audio: same pattern, deeper queue. Frames are smaller / more
- // frequent (~10-25ms vs 33ms video at 30fps).
+ // 100, not 10. At ~10-25ms per chunk, 10 was ~100-200ms of headroom against
+ // video's ~530ms, so any hiccup past ~200ms destroyed audio. ~1s, under 500KB.
this._audioEncoderMaxQueueSize = Number.isFinite(options.audioEncoderMaxQueueSize)
? options.audioEncoderMaxQueueSize
- : 10;
+ : 100;
this._droppedAudioForBackpressureCount = 0;
// Peak queue depth; leading indicator for backpressure tuning.
// Drops tell us when the encoder failed; this tells us when it
@@ -453,6 +472,7 @@ export class WebCodecsRecorder {
this._lastVideoReclaimRebuildAt = null;
this._lastAudioReclaimRebuildAt = null;
this._encodersClosedForPause = false;
+ this._finalizeRan = false;
this._finalizeOnFatalRan = false;
this._finalizingFromFatal = false;
this._audioDeviceChangePending = false;
@@ -477,6 +497,20 @@ export class WebCodecsRecorder {
this._frameDurationUs = null;
this._lastKeyFrameIndex = 0;
this.audioSamplesWritten = 0;
+ this._audioTimeBaseUs = 0;
+ this._audioStampRate = null;
+ this._audioClockAnchorUs = null;
+ this._audioDriftWindowStartUs = null;
+ this._audioDriftWindowMinUs = Infinity;
+ this._injectedCatchUpSilenceEvents = 0;
+ this._injectedCatchUpSilenceUs = 0;
+ this._injectedCatchUpSettleUs = 0;
+ this._audioDataReceived = 0;
+ this._audioDataEncoded = 0;
+ this._audioReceivedUs = 0;
+ this._paddedSilenceCount = 0;
+ this._audioClockAnchorInitialUs = null;
+ this._lastAudioLagUs = null;
if (this.running) return this._startPromise;
@@ -524,12 +558,20 @@ export class WebCodecsRecorder {
this.actualWidth = width;
this.actualHeight = height;
+ // Oriented to the source so a portrait crop doesn't bind on height
+ // (see orientBoxToSource); the pixel-area cap below is the encoder limit.
+ const cap = orientBoxToSource(
+ width,
+ height,
+ this.options?.width,
+ this.options?.height,
+ );
// Apply user/hard cap, even-rounding, and AVC pixel-area cap.
const dimResult = computeAvcCappedDimensions({
sourceWidth: width,
sourceHeight: height,
- userMaxWidth: this.options?.width,
- userMaxHeight: this.options?.height,
+ userMaxWidth: cap.width,
+ userMaxHeight: cap.height,
});
if (dimResult.capped.area) {
this.warn("[WCR] target clamped to AVC max pixel area", {
@@ -574,11 +616,14 @@ export class WebCodecsRecorder {
let videoConfig = null;
if (overrideConfig) {
- const config = {
- ...overrideConfig,
+ // Rate settings come from the caller, not the probe; an unsupported
+ // combination just falls through to chooseVideoEncoderConfig below.
+ const config = applyEncoderConfigOverrides(overrideConfig, {
width: this.targetWidth,
height: this.targetHeight,
- };
+ bitrate: safeBitrate,
+ framerate: fps,
+ });
this.log("[WCR] Using override encoder config", config);
try {
const support = await VideoEncoder.isConfigSupported(config);
@@ -813,11 +858,20 @@ export class WebCodecsRecorder {
}
this._stopping = true;
- if (!this.running) {
- this.log("[WCR] stop() called but recorder not running");
+ // Not `!running`: a source track ending (stopped screen share) clears it in
+ // readVideoLoop, and stop() then returned without flushing or finalizing.
+ if (this._finalizeRan || (!this.running && this.frameCount === 0)) {
+ this.log("[WCR] stop() called but nothing left to finalize", {
+ finalizeRan: this._finalizeRan,
+ running: this.running,
+ frameCount: this.frameCount,
+ });
this._stopping = false;
return;
}
+ // Claim the finalize before any await so a second stop() (the fatal path
+ // calls this too) can't run it twice.
+ this._finalizeRan = true;
if (this._startAlignTimer) {
clearTimeout(this._startAlignTimer);
@@ -877,6 +931,15 @@ export class WebCodecsRecorder {
endSpan({ ms });
};
try {
+ // E2E seam: stand in for a slow flush. Must sit after the loops have
+ // stopped, or the writer keeps appending and the file never goes static.
+ const forcedFinalizeDelayMs =
+ Number(
+ /** @type {any} */ (globalThis).__screenityForceFinalizeDelayMs,
+ ) || 0;
+ if (forcedFinalizeDelayMs > 0) {
+ await new Promise((r) => setTimeout(r, forcedFinalizeDelayMs));
+ }
await flushBounded(this.videoEncoder, "video");
await flushBounded(this.audioEncoder, "audio");
} catch (err) {
@@ -895,11 +958,7 @@ export class WebCodecsRecorder {
this._frameDurationUs
) {
try {
- const sampleRate = this.audioSampleRate || 48000;
- const audioEndUs =
- this.audioSamplesWritten > 0
- ? Math.round((this.audioSamplesWritten * 1_000_000) / sampleRate)
- : 0;
+ const audioEndUs = this._audioWrittenUs();
const holdStartUs = this._videoFrameIndex * this._frameDurationUs;
// Cushion for audio drain lag + muxer rounding; 150ms is imperceptible.
const cushionUs = 150_000;
@@ -1150,6 +1209,17 @@ export class WebCodecsRecorder {
droppedForBackpressureVideo: this._droppedForBackpressureCount ?? null,
droppedForBackpressureAudio:
this._droppedAudioForBackpressureCount ?? null,
+ // Steady-state catch-up silence after upstream AudioData loss.
+ // Nonzero = this recording would have drifted without reconciliation.
+ injectedCatchUpSilenceMs: Math.round(
+ (this._injectedCatchUpSilenceUs || 0) / 1000,
+ ),
+ injectedCatchUpSilenceEvents: this._injectedCatchUpSilenceEvents ?? 0,
+ // First 10s only. 0-150ms of settle jank is normal on healthy
+ // sessions, so it stays out of the alarm signal above.
+ injectedCatchUpSettleMs: Math.round(
+ (this._injectedCatchUpSettleUs || 0) / 1000,
+ ),
// Submissions to encode(), not muxer output (that's chunksOut).
framesEncoded: this.frameCount ?? null,
chunksOut: this._chunksOut ?? null,
@@ -1163,6 +1233,45 @@ export class WebCodecsRecorder {
};
}
+ // Audio mirror of screenDiag's framesFed/framesFromMSTP. audioLagMs is signed
+ // vs the first-packet epoch: growing positive = drift bug, negative = benign.
+ getAudioDiag() {
+ if (
+ !this.audioTrack &&
+ this._audioDataReceived === 0 &&
+ this._audioDataEncoded === 0
+ ) {
+ return null;
+ }
+ let lagMs = null;
+ if (this.running && this._audioClockAnchorInitialUs != null) {
+ let pausedUs = this.totalPausedDurationUs || 0;
+ if (this.paused && this.pauseStartUs != null) {
+ pausedUs += performance.now() * 1000 - this.pauseStartUs;
+ }
+ lagMs = Math.round(
+ (performance.now() * 1000 -
+ pausedUs -
+ this._audioClockAnchorInitialUs -
+ this._audioWrittenUs()) /
+ 1000,
+ );
+ } else if (this._lastAudioLagUs != null) {
+ lagMs = Math.round(this._lastAudioLagUs / 1000);
+ }
+ return {
+ audioDataReceived: this._audioDataReceived,
+ audioDataEncoded: this._audioDataEncoded,
+ // Real delivered audio only (no pads, no catch-up silence). Against
+ // audioCtxTimeMs it places loss before or after the mix bus.
+ audioReceivedMs: Math.round(this._audioReceivedUs / 1000),
+ audioWrittenMs: Math.round(this._audioWrittenUs() / 1000),
+ audioLagMs: lagMs,
+ paddedSilenceCount: this._paddedSilenceCount,
+ droppedAudioForBackpressure: this._droppedAudioForBackpressureCount,
+ };
+ }
+
getDiagSnapshot() {
let firstChunkLatencyMs = null;
if (this._firstChunkAt != null && this._videoStartUs != null) {
@@ -1180,6 +1289,10 @@ export class WebCodecsRecorder {
frameRateActual: this._frameRateActual,
frameWidthActual: this._frameWidthActual,
frameHeightActual: this._frameHeightActual,
+ // What we actually encoded, after the cap and resize canvas; the source
+ // dims above are what arrived, these are what any pixel math must use.
+ encodeWidth: this.targetWidth ?? null,
+ encodeHeight: this.targetHeight ?? null,
framesFed: this._framesFed,
chunksOut: this._chunksOut,
framesFromMSTP: this._framesFromMSTP,
@@ -1358,10 +1471,18 @@ export class WebCodecsRecorder {
this._videoStartUs = null;
this._frameDurationUs = null;
this._lastKeyFrameIndex = 0;
+ // Fold before zeroing keeps audioWrittenMs readable post-stop. The nulled
+ // anchor below makes getAudioDiag fall back to the last in-loop lag.
+ this._audioTimeBaseUs = this._audioWrittenUs();
this.audioSamplesWritten = 0;
this.audioSampleRate = null;
this.audioChannelCount = null;
this._firstAudioFrameSampleRate = null;
+ this._audioStampRate = null;
+ this._audioClockAnchorUs = null;
+ this._audioClockAnchorInitialUs = null;
+ this._audioDriftWindowStartUs = null;
+ this._audioDriftWindowMinUs = Infinity;
this.resizeCanvas = null;
this.resizeCtx = null;
@@ -2485,7 +2606,10 @@ export class WebCodecsRecorder {
const settings = this.audioTrack.getSettings();
const sampleRate = settings.sampleRate || 48000;
- const numberOfChannels = settings.channelCount || 2;
+ // options.audioChannels wins: getSettings() reports 2 while the node mixes
+ // mono (measured), and a stereo encoder rejects mono AudioData.
+ const numberOfChannels =
+ this.options.audioChannels || settings.channelCount || 2;
try {
chrome.storage.local.set({
@@ -2803,6 +2927,12 @@ export class WebCodecsRecorder {
this._staticFrameSyntheticCount += 1;
}
} else {
+ // E2E seam: exit the loop as if the source track ended, which is what
+ // stopping the screen share does.
+ if (/** @type {any} */ (globalThis).__screenityForceVideoTrackEnd) {
+ try { readResult.value?.close?.(); } catch {}
+ break;
+ }
if (readResult.done || !readResult.value) break;
frame = readResult.value;
this._framesFromMSTP += 1;
@@ -3066,6 +3196,14 @@ export class WebCodecsRecorder {
}
}
+ _audioWrittenUs() {
+ const rate = this._audioStampRate || this.audioSampleRate || 48000;
+ return (
+ this._audioTimeBaseUs +
+ Math.round((this.audioSamplesWritten * 1_000_000) / rate)
+ );
+ }
+
async readAudioLoop() {
while (this.paused && this.running) {
await new Promise((r) => setTimeout(r, 10));
@@ -3073,7 +3211,7 @@ export class WebCodecsRecorder {
if (!this.audioReader) return;
this.log("[WCR] audio loop start");
- const encodeAudioData = (audioData) => {
+ const encodeAudioData = (audioData, isCatchUpSilence = false) => {
// E2E hook: override the perceived rate for one frame to drive
// mismatch-rebuild in tests. Timestamps still use the real rate.
const _g = /** @type {any} */ (globalThis);
@@ -3172,9 +3310,23 @@ export class WebCodecsRecorder {
typeof audioData.numberOfFrames === "number"
? audioData.numberOfFrames
: 0;
- const tsUs = Math.round(
- (this.audioSamplesWritten * 1_000_000) / sampleRate
- );
+
+ // Stamp rate changed (mic switch): fold the counter at the OLD rate
+ // first, else the whole past timeline gets rescaled to the new one.
+ if (this._audioStampRate && sampleRate !== this._audioStampRate) {
+ this._audioTimeBaseUs += Math.round(
+ (this.audioSamplesWritten * 1_000_000) / this._audioStampRate,
+ );
+ this.audioSamplesWritten = 0;
+ }
+ this._audioStampRate = sampleRate;
+
+ // Before stamping, so injected catch-up silence lands ahead of this packet.
+ if (!isCatchUpSilence) {
+ reconcileAudioClock(sampleRate, frames);
+ }
+
+ const tsUs = this._audioWrittenUs();
const durUs = Math.round((frames * 1_000_000) / sampleRate);
// Track peak audio encode-queue depth (leading indicator).
@@ -3183,13 +3335,23 @@ export class WebCodecsRecorder {
this._peakAudioEncodeQueueSize = aQueueSize;
}
- // Backpressure mirror of the video path. Advance the sample
- // counter even on a drop so subsequent timestamps stay aligned
- // (brief silence gap, beats audio drifting out of sync).
- if (aQueueSize > this._audioEncoderMaxQueueSize) {
+ // E2E seam: force the real backpressure branch for N ms of audio, the
+ // encoder-side counterpart to __screenityForceAudioDrop above.
+ const forcedBpMs =
+ Number(
+ /** @type {any} */ (globalThis).__screenityForceAudioBackpressure,
+ ) || 0;
+ if (forcedBpMs > 0 && !isCatchUpSilence) {
+ /** @type {any} */ (globalThis).__screenityForceAudioBackpressure =
+ Math.max(0, forcedBpMs - (frames * 1000) / sampleRate);
+ }
+
+ // The muxer builds the audio timeline from encoded durations, so a drop
+ // shifts later audio earlier. Leave the counter for the reconciler.
+ if (forcedBpMs > 0 || aQueueSize > this._audioEncoderMaxQueueSize) {
+ if (isCatchUpSilence) return false;
this._droppedAudioForBackpressureCount += 1;
- this.audioSamplesWritten += frames;
- return;
+ return false;
}
try {
@@ -3197,19 +3359,18 @@ export class WebCodecsRecorder {
timestamp: tsUs,
});
} catch (encErr) {
- // Audio encode-queue overflow: drop as backpressure (mirrors video) and
- // advance the sample counter so timestamps stay aligned.
if (encErr?.name === "QuotaExceededError") {
+ if (isCatchUpSilence) return false;
this._droppedAudioForBackpressureCount += 1;
- this.audioSamplesWritten += frames;
this.warn(
"[WCR] audio encode() QuotaExceededError; dropping audio data",
);
- return;
+ return false;
}
throw encErr;
}
this.audioSamplesWritten += frames;
+ this._audioDataEncoded += 1;
if (this.debug && (this.audioSamplesWritten === frames || this.audioSamplesWritten % (sampleRate * 10) < frames)) {
this.log("[WCR] audio pts", {
@@ -3219,13 +3380,13 @@ export class WebCodecsRecorder {
sampleRate,
});
}
+ return true;
};
// Windows WASAPI loopback stops yielding on silent system audio,
// leaving audioReader.read() pending. Pad with silence.
const SILENCE_TIMEOUT_MS = 500;
const SILENCE_CHUNK_MS = 500;
- let paddedSilenceCount = 0;
const makeSilentAudioData = (durationMs) => {
const sampleRate = this.audioSampleRate || 48000;
@@ -3240,13 +3401,128 @@ export class WebCodecsRecorder {
sampleRate,
numberOfFrames: frames,
numberOfChannels: channels,
- timestamp: Math.round(
- (this.audioSamplesWritten * 1_000_000) / sampleRate,
- ),
+ timestamp: this._audioWrittenUs(),
data,
});
};
+ // Lost AudioData (MSTP overflow under load) shortens the track with
+ // contiguous PTS, so everything after the loss plays early.
+ const AUDIO_DRIFT_WINDOW_US = 2_000_000;
+ const AUDIO_DRIFT_MIN_INJECT_US = 50_000;
+ // Injections this early are one-time settle jank, measured at 50-134ms
+ // within the first ~7s of healthy runs. Bucketed apart from the alarm.
+ const AUDIO_CATCHUP_SETTLE_WINDOW_US = 10_000_000;
+ // Caps one catch-up event at 60s (e.g. a wall-clock jump across system
+ // sleep). The remainder is picked up by the next window.
+ const MAX_CATCHUP_CHUNKS_PER_EVENT = 120;
+
+ const injectCatchUpSilence = (deficitUs) => {
+ const inSettleWindow =
+ this._audioWrittenUs() < AUDIO_CATCHUP_SETTLE_WINDOW_US;
+ let remainingUs = deficitUs;
+ let chunks = 0;
+ while (
+ remainingUs >= AUDIO_DRIFT_MIN_INJECT_US &&
+ chunks < MAX_CATCHUP_CHUNKS_PER_EVENT
+ ) {
+ chunks += 1;
+ const chunkMs = Math.min(SILENCE_CHUNK_MS, remainingUs / 1000);
+ const silent = makeSilentAudioData(chunkMs);
+ const silentUs = Math.round(
+ (silent.numberOfFrames * 1_000_000) / silent.sampleRate,
+ );
+ // No await here, so encodeQueueSize only grows and past the cap every
+ // chunk is refused. Stop and leave the residual for the next window.
+ let encoded = false;
+ try {
+ encoded = encodeAudioData(silent, true) === true;
+ } finally {
+ try { silent.close?.(); } catch {}
+ }
+ if (!encoded) break;
+ remainingUs -= silentUs;
+ if (inSettleWindow) {
+ this._injectedCatchUpSettleUs += silentUs;
+ } else {
+ this._injectedCatchUpSilenceUs += silentUs;
+ }
+ }
+ const injectedUs = deficitUs - remainingUs;
+ if (injectedUs <= 0) return 0;
+ if (!inSettleWindow) {
+ this._injectedCatchUpSilenceEvents += 1;
+ }
+ this.warn("[WCR] audio behind wall clock; injected catch-up silence", {
+ deficitMs: Math.round(deficitUs / 1000),
+ injectedMs: Math.round(injectedUs / 1000),
+ settle: inSettleWindow,
+ events: this._injectedCatchUpSilenceEvents,
+ });
+ try {
+ diagForward("recorder-audio-clock-reconciled", {
+ deficitMs: Math.round(deficitUs / 1000),
+ settle: inSettleWindow,
+ totalInjectedMs: Math.round(
+ (this._injectedCatchUpSilenceUs + this._injectedCatchUpSettleUs) /
+ 1000,
+ ),
+ events: this._injectedCatchUpSilenceEvents,
+ });
+ } catch {}
+ return injectedUs;
+ };
+
+ // Anchor = global min of (elapsed - written), injection = windowed min lag.
+ // A windowed anchor hides accumulated loss, an unwindowed one pads mere delay.
+ const reconcileAudioClock = (sampleRate, incomingFrames) => {
+ // Pause cancels out: the counter freezes while paused and effNow
+ // subtracts the same span, so resume reads no deficit.
+ const effNowUs =
+ performance.now() * 1000 - (this.totalPausedDurationUs || 0);
+ // Counts the incoming packet: healthy delivery then reconciles to ~0 lag,
+ // so packet-arrival jitter never crosses the injection threshold.
+ const posUs =
+ this._audioWrittenUs() +
+ Math.round((incomingFrames * 1_000_000) / sampleRate);
+ const anchorCandidate = effNowUs - posUs;
+ if (
+ this._audioClockAnchorUs == null ||
+ anchorCandidate < this._audioClockAnchorUs
+ ) {
+ if (this._audioClockAnchorUs == null) {
+ this._audioClockAnchorInitialUs = anchorCandidate;
+ }
+ this._audioClockAnchorUs = anchorCandidate;
+ }
+ const lagUs = effNowUs - this._audioClockAnchorUs - posUs;
+ // Diagnostic only, against the fixed first-packet epoch. The ratcheting
+ // anchor above clamps to >= 0 and would hide audio running ahead.
+ this._lastAudioLagUs =
+ effNowUs - this._audioClockAnchorInitialUs - posUs;
+
+ if (this._audioDriftWindowStartUs == null) {
+ this._audioDriftWindowStartUs = effNowUs;
+ this._audioDriftWindowMinUs = lagUs;
+ return;
+ }
+ if (lagUs < this._audioDriftWindowMinUs) {
+ this._audioDriftWindowMinUs = lagUs;
+ }
+ if (effNowUs - this._audioDriftWindowStartUs < AUDIO_DRIFT_WINDOW_US) {
+ return;
+ }
+ const deficitUs = this._audioDriftWindowMinUs;
+ this._audioDriftWindowStartUs = effNowUs;
+ this._audioDriftWindowMinUs = lagUs;
+ if (deficitUs >= AUDIO_DRIFT_MIN_INJECT_US) {
+ const injectedUs = injectCatchUpSilence(deficitUs);
+ // Post-injection lag, not the stale pre-injection value, which
+ // would double-inject on a slow next packet.
+ this._audioDriftWindowMinUs = Math.max(0, lagUs - injectedUs);
+ }
+ };
+
try {
while (this.running) {
// Hoisted for the same reason as the video read: a losing read
@@ -3291,11 +3567,14 @@ export class WebCodecsRecorder {
} finally {
silent.close?.();
}
- paddedSilenceCount += 1;
- if (paddedSilenceCount === 1 || paddedSilenceCount % 20 === 0) {
+ this._paddedSilenceCount += 1;
+ if (
+ this._paddedSilenceCount === 1 ||
+ this._paddedSilenceCount % 20 === 0
+ ) {
this.log(
"[WCR] audio source quiet, padding silence",
- paddedSilenceCount,
+ this._paddedSilenceCount,
);
}
} catch (err) {
@@ -3308,6 +3587,35 @@ export class WebCodecsRecorder {
const { value: audioData, done } = readResult;
if (done || !audioData) break;
+ // E2E seam: drop delivered AudioData (value = ms to drop) ahead of every
+ // counter, so it reads exactly like upstream MSTP loss.
+ const forcedDropMs =
+ Number(
+ /** @type {any} */ (globalThis).__screenityForceAudioDrop,
+ ) || 0;
+ if (forcedDropMs > 0 && audioData.sampleRate) {
+ const chunkMs =
+ ((audioData.numberOfFrames || 0) * 1000) / audioData.sampleRate;
+ /** @type {any} */ (globalThis).__screenityForceAudioDrop = Math.max(
+ 0,
+ forcedDropMs - chunkMs,
+ );
+ audioData.close?.();
+ continue;
+ }
+ // Not while paused: the mic keeps delivering but those packets are
+ // dropped below, which read as heavy loss against audioDataEncoded.
+ if (!this.paused) {
+ this._audioDataReceived += 1;
+ if (
+ typeof audioData.numberOfFrames === "number" &&
+ audioData.sampleRate
+ ) {
+ this._audioReceivedUs += Math.round(
+ (audioData.numberOfFrames * 1_000_000) / audioData.sampleRate,
+ );
+ }
+ }
if (!this.audioTrack || this.audioTrack.readyState === "ended") {
this.warn("[WCR] audio lost");
this.options.onError?.({ type: "audio-lost" });
@@ -3376,18 +3684,38 @@ export class WebCodecsRecorder {
this.err("[WCR] audio loop error:", err);
}
- if (paddedSilenceCount > 0) {
+ if (
+ this._paddedSilenceCount > 0 ||
+ this._injectedCatchUpSilenceEvents > 0 ||
+ this._injectedCatchUpSettleUs > 0
+ ) {
this.log(
- "[WCR] audio loop exit, total silence chunks padded:",
- paddedSilenceCount,
+ "[WCR] audio loop exit, silence chunks padded:",
+ this._paddedSilenceCount,
+ "catch-up ms:",
+ Math.round(
+ (this._injectedCatchUpSilenceUs + this._injectedCatchUpSettleUs) /
+ 1000,
+ ),
);
try {
+ const paddedSilenceCount = this._paddedSilenceCount;
+ const catchUpSilenceMs = Math.round(
+ this._injectedCatchUpSilenceUs / 1000,
+ );
+ const catchUpSilenceEvents = this._injectedCatchUpSilenceEvents;
+ const catchUpSettleMs = Math.round(
+ this._injectedCatchUpSettleUs / 1000,
+ );
chrome.storage.local.get(["lastRecordingAudioSnapshot"], (res) => {
const prev = res?.lastRecordingAudioSnapshot || {};
chrome.storage.local.set({
lastRecordingAudioSnapshot: {
...prev,
paddedSilenceCount,
+ catchUpSilenceMs,
+ catchUpSilenceEvents,
+ catchUpSettleMs,
},
});
});
diff --git a/src/pages/Recorder/webcodecs/recorderLogic.js b/src/pages/Recorder/webcodecs/recorderLogic.js
index d698220..dfa19b9 100644
--- a/src/pages/Recorder/webcodecs/recorderLogic.js
+++ b/src/pages/Recorder/webcodecs/recorderLogic.js
@@ -38,6 +38,19 @@ export function isAacRateInSpec(rate) {
return Number.isFinite(rate) && AAC_SUPPORTED_RATES.has(rate);
}
+// The probe's config carries its own bitrate/framerate from probing at
+// 1280x720; reused verbatim it pinned every recording to the probe's 4 Mbps.
+// Codec from the probe, rate settings from the caller.
+export function applyEncoderConfigOverrides(
+ probeConfig,
+ { width, height, bitrate, framerate },
+) {
+ const config = { ...probeConfig, width, height };
+ if (Number.isFinite(bitrate) && bitrate > 0) config.bitrate = bitrate;
+ if (Number.isFinite(framerate) && framerate > 0) config.framerate = framerate;
+ return config;
+}
+
// Cap each dim by min(userMax, hardCap), preserve aspect, round even
// (H.264 requirement) with min 32, then scale down if total pixels
// still exceed avcMaxPixels.
diff --git a/src/pages/Region/Recorder.jsx b/src/pages/Region/Recorder.jsx
index f644857..49d6c47 100644
--- a/src/pages/Region/Recorder.jsx
+++ b/src/pages/Region/Recorder.jsx
@@ -5,6 +5,7 @@ import { getUserMediaWithFallback } from "../utils/mediaDeviceFallback";
import { startAudioStream as acquireMicStream } from "../utils/startAudioStream";
import { shouldAcquireMicAtStart } from "../utils/micAcquisitionPolicy";
import { attachAudioContextWatchdog } from "../utils/audioContextWatchdog";
+import { beginFinalizeHeartbeat } from "../utils/finalizeHeartbeat";
import { acquireDisplayMediaWithFocusRetry } from "../utils/acquireDisplayMedia";
import {
startTabKeepalive,
@@ -68,7 +69,6 @@ function logRecordingSnapshot(label, data) {
debug(`Recording snapshot: ${label}`, data);
}
-
const Recorder = () => {
const isRestarting = useRef(false);
const isDismissing = useRef(false);
@@ -101,6 +101,9 @@ const Recorder = () => {
const aCtx = useRef(null);
const destination = useRef(null);
+ // Only set when we force the bus to mono: getSettings() can report 2 while the
+ // node mixes mono. Null lets the encoder read the track.
+ const audioBusChannels = useRef(null);
const audioInputSource = useRef(null);
const audioOutputSource = useRef(null);
const audioInputGain = useRef(null);
@@ -937,6 +940,7 @@ const Recorder = () => {
probeResult?.details?.selectedVideoConfig || null;
const containerKind =
probeResult?.details?.containerKind === "webm" ? "webm" : "mp4";
+
let recorderToken = 0;
let codecFallbackTriggered = false;
let webcodecsFallbackTriggered = false;
@@ -1128,6 +1132,7 @@ const Recorder = () => {
fps,
videoBitrate: videoBitsPerSecond,
audioBitrate: hasAudioTrack ? audioBitsPerSecond : undefined,
+ audioChannels: audioBusChannels.current,
enableAudio: hasAudioTrack,
videoEncoderConfig: selectedVideoConfig,
containerKind,
@@ -2023,10 +2028,19 @@ const Recorder = () => {
useWebCodecs.current &&
recorder.current instanceof WebCodecsRecorder
) {
+ // Keeps the editor's reader waiting through a long flush instead of
+ // reading a truncated file.
+ const stopFinalizeHeartbeat = beginFinalizeHeartbeat(
+ chunkBackendRef.current?.backend === "opfs"
+ ? chunkBackendRef.current?.fileName
+ : null,
+ );
try {
await updateFreeFinalizeStatus("finalizing", 20);
await recorder.current.stop();
- } catch {}
+ } catch {} finally {
+ stopFinalizeHeartbeat();
+ }
await waitForDrain();
recorder.current = null;
} else {
@@ -2366,6 +2380,24 @@ const Recorder = () => {
helperAudioStream.current = micstream;
+ // Mic-only: a stereo destination just duplicates a mono mic, halving effective
+ // bitrate. Must be set before any source connects.
+ audioBusChannels.current = null;
+ try {
+ const micTrackForBus =
+ helperAudioStream.current?.getAudioTracks?.()[0] || null;
+ const willMixSystemAudio =
+ helperVideoStream.current.getAudioTracks().length > 0;
+ if (
+ micTrackForBus &&
+ !willMixSystemAudio &&
+ micTrackForBus.getSettings?.()?.channelCount === 1
+ ) {
+ destination.current.channelCount = 1;
+ audioBusChannels.current = 1;
+ }
+ } catch {}
+
if (
helperAudioStream.current != null &&
helperAudioStream.current.getAudioTracks().length > 0
diff --git a/src/pages/RemuxOffscreen/index.js b/src/pages/RemuxOffscreen/index.js
index 69e7a22..d1cbfb5 100644
--- a/src/pages/RemuxOffscreen/index.js
+++ b/src/pages/RemuxOffscreen/index.js
@@ -96,18 +96,20 @@ chrome.runtime.onMessage.addListener((message) => {
});
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
- if (
- !message ||
- (message.type !== "remux-start" && message.type !== "webm-start")
- ) {
+ const START_TYPES = {
+ "remux-start": "remux",
+ "webm-start": "webm",
+ "mp4x-start": "mp4x",
+ };
+ if (!message || !START_TYPES[message.type]) {
return undefined;
}
- const { requestId, inputFileName, outputFileName } = message;
+ const { requestId, inputFileName, outputFileName, videoBitrate } = message;
if (!requestId || !inputFileName || !outputFileName) {
sendResponse({ ok: false, error: "invalid-remux-start-payload" });
return false;
}
- const workerType = message.type === "webm-start" ? "webm" : "remux";
+ const workerType = START_TYPES[message.type];
try {
const w = ensureWorker();
devLog("remux-start-received", {
@@ -122,6 +124,7 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
requestId,
inputFileName,
outputFileName,
+ videoBitrate,
});
} catch (err) {
pending.delete(requestId);
diff --git a/src/pages/RemuxOffscreen/worker.js b/src/pages/RemuxOffscreen/worker.js
index a5cf139..99998a5 100644
--- a/src/pages/RemuxOffscreen/worker.js
+++ b/src/pages/RemuxOffscreen/worker.js
@@ -248,13 +248,14 @@ const remuxToOpfs = async ({ requestId, inputFileName, outputFileName }) => {
}
};
-// Re-encodes an MP4 (H.264) to WebM (VP9) from OPFS to OPFS, streaming output
-// so memory stays bounded on large files (the in-editor BufferTarget path OOMs
-// past ~2 GB). Far slower than the remux (it fully decodes and re-encodes
-// every frame), but it can't passthrough since WebM can't hold H.264.
-const convertWebmToOpfs = async ({ requestId, inputFileName, outputFileName }) => {
+// Neither direction can passthrough (WebM can't hold H.264, MP4 can't usefully hold VP8),
+// so it's a full re-encode. Streams OPFS to OPFS since BufferTarget OOMs past ~2GB.
+const convertViaEncoder = async (
+ { requestId, inputFileName, outputFileName, videoBitrate },
+ targetFormat = "webm",
+) => {
const startedAt = Date.now();
- devLog("webm-start", { requestId, inputFileName, outputFileName });
+ devLog(`${targetFormat}-start`, { requestId, inputFileName, outputFileName });
await sweepOpfsStaleFiles();
let dir;
@@ -288,10 +289,20 @@ const convertWebmToOpfs = async ({ requestId, inputFileName, outputFileName }) =
const writable = createOpfsWritable(syncHandle);
- await videoConverter.convertToWebM(inputFile, {
+ const convertOptions = {
target: new StreamTarget(writable),
onProgress: (p) => postProgress(requestId, p),
- });
+ };
+ // Without this the converter falls back to its 5 Mbps default, which is a
+ // 1080p figure: a 4K screencast came back visibly blocky on text.
+ if (Number.isFinite(videoBitrate) && videoBitrate > 0) {
+ convertOptions.videoBitrate = videoBitrate;
+ }
+ if (targetFormat === "mp4") {
+ await videoConverter.convertToMP4(inputFile, convertOptions);
+ } else {
+ await videoConverter.convertToWebM(inputFile, convertOptions);
+ }
try {
await writable.close();
@@ -301,13 +312,13 @@ const convertWebmToOpfs = async ({ requestId, inputFileName, outputFileName }) =
syncHandle.close();
handleReleased = true;
- devLog("webm-done", {
+ devLog(`${targetFormat}-done`, {
durationMs: Date.now() - startedAt,
outputBytes: finalSize,
});
postDone(requestId, outputFileName);
} catch (err) {
- devLog("webm-error", {
+ devLog(`${targetFormat}-error`, {
err: String(err?.message || err).slice(0, 200),
durationMs: Date.now() - startedAt,
});
@@ -329,5 +340,6 @@ self.onmessage = (e) => {
const msg = e.data;
if (!msg) return;
if (msg.type === "remux") remuxToOpfs(msg);
- else if (msg.type === "webm") convertWebmToOpfs(msg);
+ else if (msg.type === "webm") convertViaEncoder(msg, "webm");
+ else if (msg.type === "mp4x") convertViaEncoder(msg, "mp4");
};
diff --git a/src/pages/utils/finalizeHeartbeat.js b/src/pages/utils/finalizeHeartbeat.js
new file mode 100644
index 0000000..789a1e8
--- /dev/null
+++ b/src/pages/utils/finalizeHeartbeat.js
@@ -0,0 +1,29 @@
+// Proof that the recorder is still finalizing, for the editor's OPFS reader.
+// The reader gives up on a writer whose file stops growing for 3s, but a flush
+// writes nothing for up to 15s per track, so a healthy finalize looked dead and
+// the editor read a truncated file. Only a live page can keep this fresh.
+
+const HEARTBEAT_INTERVAL_MS = 1000;
+export const WRITER_HEARTBEAT_KEY = "lastRecordingWriterHeartbeat";
+
+// Returns a stop function. Fire-and-forget writes: awaiting storage under SW
+// contention has already cost this path seconds before.
+export const beginFinalizeHeartbeat = (fileName) => {
+ if (!fileName || typeof chrome === "undefined" || !chrome.storage?.local) {
+ return () => {};
+ }
+ const write = () => {
+ try {
+ chrome.storage.local.set({
+ [WRITER_HEARTBEAT_KEY]: { fileName, at: Date.now() },
+ });
+ } catch {}
+ };
+ write();
+ const timer = setInterval(write, HEARTBEAT_INTERVAL_MS);
+ return () => {
+ try {
+ clearInterval(timer);
+ } catch {}
+ };
+};
diff --git a/webpack.config.js b/webpack.config.js
index 3d6b509..d05fb8e 100755
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -23,14 +23,38 @@ const ASSET_PATH = process.env.ASSET_PATH || "/";
if (process.env.SCREENITY_SKIP_ENV) {
// open-source release build, no dotenv
-} else if (isDev || process.env.SCREENITY_USE_LOCAL_ENV === "1") {
- // SCREENITY_USE_LOCAL_ENV=1 lets you do a NODE_ENV=production
- // (minified, fast) build that still points at localhost; useful
- // for testing login/auth flows against a local dev server while
- // keeping the small prod-style bundle.
- require("dotenv").config({ path: ".env.local" });
+ console.log("[screenity] env: none (SCREENITY_SKIP_ENV), no app/api URLs baked");
} else {
- require("dotenv").config({ path: ".env.production" });
+ // SCREENITY_USE_LOCAL_ENV=1: minified prod-style build still pointing at
+ // localhost, for auth flows against a local dev server.
+ const envFile =
+ isDev || process.env.SCREENITY_USE_LOCAL_ENV === "1"
+ ? ".env.local"
+ : ".env.production";
+ const shadowKeys = [
+ "SCREENITY_APP_BASE",
+ "SCREENITY_API_BASE_URL",
+ "SCREENITY_WEBSITE_BASE",
+ ];
+ const preset = Object.fromEntries(shadowKeys.map((k) => [k, process.env[k]]));
+ const { parsed } = require("dotenv").config({ path: envFile });
+ // dotenv never overrides shell-exported vars, so those silently win. Warn loudly.
+ for (const key of shadowKeys) {
+ if (
+ preset[key] !== undefined &&
+ parsed?.[key] !== undefined &&
+ preset[key] !== parsed[key]
+ ) {
+ console.warn(
+ `[screenity] WARNING: shell ${key}=${preset[key]} overrides ${envFile} (${parsed[key]})`,
+ );
+ }
+ }
+ console.log(
+ `[screenity] env: ${envFile} SCREENITY_APP_BASE=${
+ process.env.SCREENITY_APP_BASE || "(unset)"
+ } SCREENITY_API_BASE_URL=${process.env.SCREENITY_API_BASE_URL || "(unset)"}`,
+ );
}
// Entry points for the different pages
@@ -67,6 +91,13 @@ const entryPoints = {
region: path.join(__dirname, "src", "pages", "Region", "index.jsx"),
download: path.join(__dirname, "src", "pages", "Download", "index.jsx"),
editor: path.join(__dirname, "src", "pages", "Editor", "index.jsx"),
+ localplaybackbridge: path.join(
+ __dirname,
+ "src",
+ "pages",
+ "LocalPlaybackBridge",
+ "index.js"
+ ),
remuxoffscreen: path.join(
__dirname,
"src",
@@ -119,6 +150,7 @@ const htmlPlugins = Object.keys(entryPoints)
cloudrecorder: "CloudRecorder",
offscreenrecorder: "OffscreenRecorder",
remuxoffscreen: "RemuxOffscreen",
+ localplaybackbridge: "LocalPlaybackBridge",
};
const folderName =
@@ -196,7 +228,9 @@ const config = {
// prefix so webpack's default _f608.bundle.js etc. don't trip it.
chunkFilename: "chunk.[name].[contenthash:8].bundle.js",
path: path.resolve(__dirname, "build"),
- clean: !isDev, // Only wipe build dir in production; dev keeps it to avoid re-copying 40MB of assets
+ // Clean one-shot builds so a dev build after a prod build can't mix stale
+ // artifacts. Only the hot-reload server skips the 40MB asset re-copy.
+ clean: !(isDev && process.env.WEBPACK_SERVE),
publicPath: ASSET_PATH,
},
module: {