Skip to content

Commit 5dce248

Browse files
yury-saslushnikov
authored andcommitted
fix(screencast): correctly process videos with 1 frame (#4144)
1 parent 85ae3cc commit 5dce248

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/server/chromium/videoRecorder.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const fps = 25;
2626
export class VideoRecorder {
2727
private _process: ChildProcess | null = null;
2828
private _gracefullyClose: (() => Promise<void>) | null = null;
29-
private _lastWritePromise: Promise<void>;
29+
private _lastWritePromise: Promise<void> | undefined;
3030
private _lastFrameTimestamp: number = 0;
3131
private _lastFrameBuffer: Buffer | null = null;
3232
private _lastWriteTimestamp: number = 0;
@@ -47,7 +47,6 @@ export class VideoRecorder {
4747

4848
private constructor(progress: Progress) {
4949
this._progress = progress;
50-
this._lastWritePromise = Promise.resolve();
5150
}
5251

5352
private async _launch(options: types.PageScreencastOptions) {
@@ -89,22 +88,24 @@ export class VideoRecorder {
8988
assert(this._process);
9089
if (!this._isRunning())
9190
return;
92-
const repeatCount = this._lastFrameTimestamp ? Math.max(1, Math.round(25 * (timestamp - this._lastFrameTimestamp))) : 1;
93-
this._progress.log(`writing ${repeatCount} frame(s)`);
94-
this._lastWritePromise = this._flushLastFrame(repeatCount).catch(e => this._progress.log('Error while writing frame: ' + e));
91+
this._progress.log(`writing frame ` + timestamp);
92+
if (this._lastFrameBuffer)
93+
this._lastWritePromise = this._flushLastFrame(timestamp - this._lastFrameTimestamp).catch(e => this._progress.log('Error while writing frame: ' + e));
9594
this._lastFrameBuffer = frame;
9695
this._lastFrameTimestamp = timestamp;
9796
this._lastWriteTimestamp = Date.now();
9897
}
9998

100-
private async _flushLastFrame(repeatCount: number): Promise<void> {
99+
private async _flushLastFrame(durationSec: number): Promise<void> {
101100
assert(this._process);
102101
const frame = this._lastFrameBuffer;
103102
if (!frame)
104103
return;
105104
const previousWrites = this._lastWritePromise;
106105
let finishedWriting: () => void;
107106
const writePromise = new Promise<void>(fulfill => finishedWriting = fulfill);
107+
const repeatCount = Math.max(1, Math.round(fps * durationSec));
108+
this._progress.log(`flushing ${repeatCount} frame(s)`);
108109
await previousWrites;
109110
for (let i = 0; i < repeatCount; i++) {
110111
const callFinish = i === (repeatCount - 1);
@@ -124,8 +125,8 @@ export class VideoRecorder {
124125

125126
if (this._lastWriteTimestamp) {
126127
const durationSec = (Date.now() - this._lastWriteTimestamp) / 1000;
127-
if (durationSec > 1 / fps)
128-
this.writeFrame(this._lastFrameBuffer!, this._lastFrameTimestamp + durationSec);
128+
if (!this._lastWritePromise || durationSec > 1 / fps)
129+
this._flushLastFrame(durationSec).catch(e => this._progress.log('Error while writing frame: ' + e));
129130
}
130131

131132
const close = this._gracefullyClose;

test/screencast.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('screencast', suite => {
180180
}
181181
});
182182

183-
it('should emit video event', async ({browser, testInfo}) => {
183+
it('should expose video path', async ({browser, testInfo}) => {
184184
const videosPath = testInfo.outputPath('');
185185
const size = { width: 320, height: 240 };
186186
const context = await browser.newContext({
@@ -190,10 +190,10 @@ describe('screencast', suite => {
190190
});
191191
const page = await context.newPage();
192192
await page.evaluate(() => document.body.style.backgroundColor = 'red');
193-
await new Promise(r => setTimeout(r, 1000));
194-
await context.close();
195193
const path = await page.video()!.path();
196194
expect(path).toContain(videosPath);
195+
await context.close();
196+
expect(fs.existsSync(path)).toBeTruthy();
197197
});
198198

199199
it('should capture navigation', async ({browser, server, testInfo}) => {

0 commit comments

Comments
 (0)