Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(video): allow to launch video recorder asynchronously (#38501)

Yury Semikhatsky6c2eea418aff
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index aa2a6b42fb..08b98e9793 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -437,9 +437,9 @@ class FrameSession {
this._windowId = windowId;
}
- let screencastOptions: types.PageScreencastOptions | undefined;
+ let videoOptions: types.VideoOptions | undefined;
if (!this._page.isStorageStatePage && this._isMainFrame() && hasUIWindow)
- screencastOptions = await this._crPage._page.screencast.initializeVideoRecorder();
+ videoOptions = this._crPage._page.screencast.launchVideoRecorder();
let lifecycleEventsEnabled: Promise<any>;
if (!this._isMainFrame())
@@ -528,8 +528,8 @@ class FrameSession {
promises.push(this._updateFileChooserInterception(true));
for (const initScript of this._crPage._page.allInitScripts())
promises.push(this._evaluateOnNewDocument(initScript, 'main', true /* runImmediately */));
- if (screencastOptions)
- promises.push(this._crPage._page.screencast.startVideoRecording(screencastOptions));
+ if (videoOptions)
+ promises.push(this._crPage._page.screencast.startVideoRecording(videoOptions));
}
promises.push(this._client.send('Runtime.runIfWaitingForDebugger'));
promises.push(this._firstNonInitialNavigationCommittedPromise);
diff --git a/packages/playwright-core/src/server/screencast.ts b/packages/playwright-core/src/server/screencast.ts
index 5ec7d98288..678056f7a0 100644
--- a/packages/playwright-core/src/server/screencast.ts
+++ b/packages/playwright-core/src/server/screencast.ts
@@ -15,7 +15,7 @@
*/
import path from 'path';
-import { assert, createGuid, mkdirIfNeeded } from '../utils';
+import { assert, createGuid, eventsHelper, RegisteredListener } from '../utils';
import { debugLogger } from '../utils';
import { VideoRecorder } from './videoRecorder';
import { Page } from './page';
@@ -26,12 +26,13 @@ import type * as types from './types';
export class Screencast {
private _page: Page;
private _videoRecorder: VideoRecorder | null = null;
- private _screencastId: string | null = null;
+ private _videoId: string | null = null;
private _screencastClients = new Set<unknown>();
// Aiming at 25 fps by default - each frame is 40ms, but we give some slack with 35ms.
// When throttling for tracing, 200ms between frames, except for 10 frames around the action.
private _frameThrottler = new FrameThrottler(10, 35, 200);
+ private _frameListener: RegisteredListener | null = null;
constructor(page: Page) {
this._page = page;
@@ -55,31 +56,33 @@ export class Screencast {
this._frameThrottler.recharge();
}
- async initializeVideoRecorder(): Promise<types.PageScreencastOptions | undefined> {
+ launchVideoRecorder(): types.VideoOptions | undefined {
const recordVideo = this._page.browserContext._options.recordVideo;
if (!recordVideo)
return undefined;
- const screencastId = createGuid();
- const outputFile = path.join(recordVideo.dir, screencastId + '.webm');
- const screencastOptions = {
+ assert(!this._videoId);
+ this._videoId = createGuid();
+ const outputFile = path.join(recordVideo.dir, this._videoId + '.webm');
+ const videoOptions = {
// validateBrowserContextOptions ensures correct video size.
...recordVideo.size!,
outputFile,
};
- await mkdirIfNeeded(path.join(recordVideo.dir, 'dummy'));
// Note: it is important to start video recorder before sending Screencast.startScreencast,
// and it is equally important to send Screencast.startScreencast before sending Target.resume.
- await this._createVideoRecorder(screencastId, screencastOptions);
+ const ffmpegPath = registry.findExecutable('ffmpeg')!.executablePathOrDie(this._page.browserContext._browser.sdkLanguage());
+ this._videoRecorder = new VideoRecorder(ffmpegPath, videoOptions);
+ this._frameListener = eventsHelper.addEventListener(this._page, Page.Events.ScreencastFrame, frame => this._videoRecorder!.writeFrame(frame.buffer, frame.frameSwapWallTime / 1000));
this._page.waitForInitializedOrError().then(p => {
if (p instanceof Error)
this.stopVideoRecording().catch(() => {});
});
- return screencastOptions;
+ return videoOptions;
}
- async startVideoRecording(options: types.PageScreencastOptions) {
- const screencastId = this._screencastId;
- assert(screencastId);
+ async startVideoRecording(options: types.VideoOptions) {
+ const videoId = this._videoId;
+ assert(videoId);
this._page.once(Page.Events.Close, () => this.stopVideoRecording().catch(() => {}));
const gotFirstFrame = new Promise(f => this._page.once(Page.Events.ScreencastFrame, f));
await this._startScreencast(this._videoRecorder, {
@@ -89,22 +92,25 @@ export class Screencast {
});
// Wait for the first frame before reporting video to the client.
gotFirstFrame.then(() => {
- this._page.browserContext._browser._videoStarted(this._page.browserContext, screencastId, options.outputFile, this._page.waitForInitializedOrError());
+ this._page.browserContext._browser._videoStarted(this._page.browserContext, videoId, options.outputFile, this._page.waitForInitializedOrError());
});
}
async stopVideoRecording(): Promise<void> {
- if (!this._screencastId)
+ if (!this._videoId)
return;
- const screencastId = this._screencastId;
- this._screencastId = null;
- const recorder = this._videoRecorder!;
+ if (this._frameListener)
+ eventsHelper.removeEventListeners([this._frameListener]);
+ this._frameListener = null;
+ const videoId = this._videoId;
+ this._videoId = null;
+ const videoRecorder = this._videoRecorder!;
this._videoRecorder = null;
- await this._stopScreencast(recorder);
- await recorder.stop().catch(() => {});
+ await this._stopScreencast(videoRecorder);
+ await videoRecorder.stop();
// Keep the video artifact in the map until encoding is fully finished, if the context
// starts closing before the video is fully written to disk it will wait for it.
- const video = this._page.browserContext._browser._takeVideo(screencastId);
+ const video = this._page.browserContext._browser._takeVideo(videoId);
video?.reportFinished();
}
@@ -131,13 +137,6 @@ export class Screencast {
if (!this._screencastClients.size)
await this._page.delegate.stopScreencast();
}
-
- private async _createVideoRecorder(screencastId: string, options: types.PageScreencastOptions): Promise<void> {
- assert(!this._screencastId);
- const ffmpegPath = registry.findExecutable('ffmpeg')!.executablePathOrDie(this._page.browserContext._browser.sdkLanguage());
- this._videoRecorder = await VideoRecorder.launch(this._page, ffmpegPath, options);
- this._screencastId = screencastId;
- }
}
class FrameThrottler {
diff --git a/packages/playwright-core/src/server/types.ts b/packages/playwright-core/src/server/types.ts
index b189b92db9..9f5ef8c7ea 100644
--- a/packages/playwright-core/src/server/types.ts
+++ b/packages/playwright-core/src/server/types.ts
@@ -42,12 +42,19 @@ export type PointerActionWaitOptions = CommonActionOptions & {
trial?: boolean;
};
-export type PageScreencastOptions = {
+export type VideoOptions = {
width: number,
height: number,
outputFile: string,
};
+export type ScreencastFrame = {
+ buffer: Buffer,
+ frameSwapWallTime: number,
+ width: number,
+ height: number,
+};
+
export type Credentials = {
username: string;
password: string;
diff --git a/packages/playwright-core/src/server/videoRecorder.ts b/packages/playwright-core/src/server/videoRecorder.ts
index fa2d7dc424..5a91f25fb0 100644
--- a/packages/playwright-core/src/server/videoRecorder.ts
+++ b/packages/playwright-core/src/server/videoRecorder.ts
@@ -14,8 +14,7 @@
* limitations under the License.
*/
-import { assert, debugLogger, monotonicTime } from '../utils';
-import { Page } from './page';
+import { assert, debugLogger, mkdirIfNeeded, monotonicTime } from '../utils';
import { launchProcess } from './utils/processLauncher';
import type * as types from './types';
@@ -33,22 +32,17 @@ export class VideoRecorder {
private _frameQueue: Buffer[] = [];
private _isStopped = false;
private _ffmpegPath: string;
+ private _launchPromise: Promise<Error | null>;
- static async launch(page: Page, ffmpegPath: string, options: types.PageScreencastOptions): Promise<VideoRecorder> {
+ constructor(ffmpegPath: string, options: types.VideoOptions) {
+ this._ffmpegPath = ffmpegPath;
if (!options.outputFile.endsWith('.webm'))
throw new Error('File must have .webm extension');
-
- const recorder = new VideoRecorder(page, ffmpegPath);
- await recorder._launch(options);
- return recorder;
- }
-
- private constructor(page: Page, ffmpegPath: string) {
- this._ffmpegPath = ffmpegPath;
- page.on(Page.Events.ScreencastFrame, frame => this.writeFrame(frame.buffer, frame.frameSwapWallTime / 1000));
+ this._launchPromise = this._launch(options).catch(e => e);
}
- private async _launch(options: types.PageScreencastOptions) {
+ private async _launch(options: types.VideoOptions) {
+ await mkdirIfNeeded(options.outputFile);
// How to tune the codec:
// 1. Read vp8 documentation to figure out the options.
// https://www.webmproject.org/docs/encoder-parameters/
@@ -118,6 +112,14 @@ export class VideoRecorder {
}
writeFrame(frame: Buffer, timestamp: number) {
+ this._launchPromise.then(error => {
+ if (error)
+ return;
+ this._writeFrame(frame, timestamp);
+ });
+ }
+
+ private _writeFrame(frame: Buffer, timestamp: number) {
assert(this._process);
if (this._isStopped)
return;
@@ -151,14 +153,22 @@ export class VideoRecorder {
}
async stop() {
+ // Only report the error on stop. This allows to make the constructor synchronous.
+ const error = await this._launchPromise;
+ if (error)
+ throw error;
if (this._isStopped || !this._lastFrame)
return;
// Pad with at least 1s of the last frame in the end for convenience.
// This also ensures non-empty videos with 1 frame.
const addTime = Math.max((monotonicTime() - this._lastWriteNodeTime) / 1000, 1);
- this.writeFrame(Buffer.from([]), this._lastFrame.timestamp + addTime);
+ this._writeFrame(Buffer.from([]), this._lastFrame.timestamp + addTime);
this._isStopped = true;
- await this._lastWritePromise;
- await this._gracefullyClose!();
+ try {
+ await this._lastWritePromise;
+ await this._gracefullyClose!();
+ } catch (e) {
+ debugLogger.log('error', `ffmpeg failed to stop: ${String(e)}`);
+ }
}
}
diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts
index 63814882b1..ea8b6a5a6f 100644
--- a/packages/playwright-core/src/server/webkit/wkPage.ts
+++ b/packages/playwright-core/src/server/webkit/wkPage.ts
@@ -821,9 +821,10 @@ export class WKPage implements PageDelegate {
}
private async _initializeVideoRecording() {
- const screencastOptions = await this._page.screencast.initializeVideoRecorder();
- if (screencastOptions)
- await this._page.screencast.startVideoRecording(screencastOptions);
+ const screencast = this._page.screencast;
+ const videoOptions = screencast.launchVideoRecorder();
+ if (videoOptions)
+ await screencast.startVideoRecording(videoOptions);
}
private validateScreenshotDimension(side: number, omitDeviceScaleFactor: boolean) {