fix(preview): improve browser recording quality #8839
Mergedmaria-rcksmerged 11 commits into
main fromrefs/pull/8839/headmerged by maria-rcks View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/preview/Manager.ts line 3151OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3093,0 +3131,21 @@3131+ typeof measuredSize.height !== "number" ||3132+ !Number.isInteger(measuredSize.width) ||3133+ !Number.isInteger(measuredSize.height) ||3134+ measuredSize.width <= 0 ||3135+ measuredSize.height <= 03136+ ) {3137+ throw new Error("Preview media source dimensions are unavailable.");3138+ }3139+ return { width: measuredSize.width, height: measuredSize.height };3140+ },3141+ );3142+ const sourceId = yield* attempt(3143+ {3144+ operation: "recording.getMediaSourceId",3145+ tabId,3146+ webContentsId: wc.id,3147+ },3148+ () => wc.getMediaSourceId(requestWebContents),3149+ );3150+ return { sourceId, ...size } satisfies DesktopPreviewRecordingSource;3151+ }).pipe(Effect.onError(() => stopFrameCapture(tabId, "recording").pipe(Effect.ignore)));apps/desktop/src/preview/Manager.ts line 3142OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3093,0 +3122,21 @@3122+ webContentsId: wc.id,3123+ },3124+ () => {3125+ if (3126+ typeof measuredSize !== "object" ||3127+ measuredSize === null ||3128+ !("width" in measuredSize) ||3129+ !("height" in measuredSize) ||3130+ typeof measuredSize.width !== "number" ||3131+ typeof measuredSize.height !== "number" ||3132+ !Number.isInteger(measuredSize.width) ||3133+ !Number.isInteger(measuredSize.height) ||3134+ measuredSize.width <= 0 ||3135+ measuredSize.height <= 03136+ ) {3137+ throw new Error("Preview media source dimensions are unavailable.");3138+ }3139+ return { width: measuredSize.width, height: measuredSize.height };3140+ },3141+ );3142+ const sourceId = yield* attempt(
- apps/desktop/src/preview/Manager.ts lines 3118 to 3140OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3093,0 +3120,21 @@3120+ operation: "recording.measureSource",3121+ tabId,3122+ webContentsId: wc.id,3123+ },3124+ () => {3125+ if (3126+ typeof measuredSize !== "object" ||3127+ measuredSize === null ||3128+ !("width" in measuredSize) ||3129+ !("height" in measuredSize) ||3130+ typeof measuredSize.width !== "number" ||3131+ typeof measuredSize.height !== "number" ||3132+ !Number.isInteger(measuredSize.width) ||3133+ !Number.isInteger(measuredSize.height) ||3134+ measuredSize.width <= 0 ||3135+ measuredSize.height <= 03136+ ) {3137+ throw new Error("Preview media source dimensions are unavailable.");3138+ }3139+ return { width: measuredSize.width, height: measuredSize.height };3140+ },
- apps/desktop/src/preview/Manager.ts line 3148OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3093,0 +3128,21 @@3128+ !("width" in measuredSize) ||3129+ !("height" in measuredSize) ||3130+ typeof measuredSize.width !== "number" ||3131+ typeof measuredSize.height !== "number" ||3132+ !Number.isInteger(measuredSize.width) ||3133+ !Number.isInteger(measuredSize.height) ||3134+ measuredSize.width <= 0 ||3135+ measuredSize.height <= 03136+ ) {3137+ throw new Error("Preview media source dimensions are unavailable.");3138+ }3139+ return { width: measuredSize.width, height: measuredSize.height };3140+ },3141+ );3142+ const sourceId = yield* attempt(3143+ {3144+ operation: "recording.getMediaSourceId",3145+ tabId,3146+ webContentsId: wc.id,3147+ },3148+ () => wc.getMediaSourceId(requestWebContents),
- apps/web/src/browser/browserRecording.ts lines 444 to 448OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -533,4 +441,8 @@533441 });534442 }535443 try {536- const blob = new Blob(recording.chunks, { type: recording.mimeType });444+ const mimeType =445+ recording.recorder.mimeType ||446+ recording.chunks.find((chunk) => chunk.type.length > 0)?.type;447+ if (!mimeType) {448+ throw new Error("MediaRecorder did not report its output format.");
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/browser/browserRecording.ts line 260Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -226,14 +198,16 @@226198 await stopped;227199 };228200201+const stopMediaStream = (stream: MediaStream | null): void => {202+ for (const track of stream?.getTracks() ?? []) track.stop();203+};204+229205 const clearActiveRecording = (recording: ActiveRecording): void => {206+ recording.releaseSurfaceActivity?.();207+ recording.releaseSurfaceActivity = null;230208 if (activeRecordings.get(recording.tabId) !== recording) return;231- recording.settleFirstFrameSize("cancelled");232209 activeRecordings.delete(recording.tabId);233- if (activeRecordings.size === 0) {234- unsubscribeFrames?.();235- unsubscribeFrames = null;236- }237210 publishActiveRecordingTabIds();238211 };239212213+const waitForBrowserRecordingPaint = async (): Promise<void> => {
- @@ -39,5 +39,8 @@3939 }40404141 return {42- left: HIDDEN_BROWSER_WEBVIEW_OFFSET,43- top: HIDDEN_BROWSER_WEBVIEW_OFFSET,42+ // Chromium can cull a guest that has never produced an onscreen frame.43+ // Keep active background work behind the app so native tab capture can44+ // start before the user has ever opened this preview.45+ left: renderingActive ? 0 : HIDDEN_BROWSER_WEBVIEW_OFFSET,46+ top: renderingActive ? 0 : HIDDEN_BROWSER_WEBVIEW_OFFSET,
- @@ -3093,0 +3127,21 @@3127+ !("height" in measuredSize) ||3128+ typeof measuredSize.width !== "number" ||3129+ typeof measuredSize.height !== "number" ||3130+ !Number.isInteger(measuredSize.width) ||3131+ !Number.isInteger(measuredSize.height) ||3132+ measuredSize.width <= 0 ||3133+ measuredSize.height <= 03134+ ) {3135+ return yield* new PreviewRecordingSourceSizeUnavailableError({3136+ tabId,3137+ webContentsId: wc.id,3138+ });3139+ }3140+ yield* attemptPromise(3141+ {3142+ operation: "recording.warmSource",3143+ tabId,3144+ webContentsId: wc.id,3145+ },3146+ () => wc.capturePage().then(() => undefined),3147+ );
- @@ -425,7 +389,5 @@425389 throw recordingStartupCancelledError(recording);426390 };427391 await throwIfStartupCancelled();428- const hasFirstFrame = await waitForFirstFrameSize(recording);429- await throwIfStartupCancelled();430- if (!hasFirstFrame) {431- const cause = new Error(`No valid recording frame arrived for tab ${tabId}.`);392+ try {393+ recording.stream = await captureTabMediaStream(source, frameRate);
- @@ -228 +201,21 @@228201202+const stopMediaStream = (stream: MediaStream | null): void => {203+ for (const track of stream?.getTracks() ?? []) track.stop();204+};205+206+const captureTabMediaStreamWithTimeout = async (207+ source: DesktopPreviewRecordingSource,208+ frameRate: number,209+): Promise<MediaStream> => {210+ let acceptStream = true;211+ let timeoutId: number | null = null;212+ const streamPromise = captureTabMediaStream(source, frameRate).then((stream) => {213+ if (!acceptStream) stopMediaStream(stream);214+ return stream;215+ });216+ try {217+ return await Promise.race([218+ streamPromise,219+ new Promise<never>((_, reject) => {220+ timeoutId = window.setTimeout(221+ () => reject(new Error("Browser recording media capture did not settle.")),
maria-rcks merged this3958111
maria-rcks closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
cefe4431-da73-464e-849d-85405318640fYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Warning
Your free Security trial is over. An organization admin can activate Security or dismiss this notice.
Comment `@coderabbitai help` to get the list of available commands.