From 5507553173cc07b7defa42e966c194003f89bd6f Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 15 Sep 2020 15:21:50 -0700 Subject: [PATCH] fix(screencast): repeat previous frame instead of current (#3890) --- src/server/chromium/videoRecorder.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/server/chromium/videoRecorder.ts b/src/server/chromium/videoRecorder.ts index f5258d2fa0..8c81c761eb 100644 --- a/src/server/chromium/videoRecorder.ts +++ b/src/server/chromium/videoRecorder.ts @@ -95,19 +95,25 @@ export class VideoRecorder { assert(this._process); if (!this._isRunning()) return; - const duration = this._lastFrameTimestamp ? Math.max(1, Math.round(25 * (timestamp - this._lastFrameTimestamp))) : 1; - this._progress.log(`writing ${duration} frame(s)`); + const repeatCount = this._lastFrameTimestamp ? Math.max(1, Math.round(25 * (timestamp - this._lastFrameTimestamp))) : 1; + this._progress.log(`writing ${repeatCount} frame(s)`); + this._lastWritePromise = this._flushLastFrame(repeatCount).catch(e => this._progress.log('Error while writing frame: ' + e)); this._lastFrameBuffer = frame; this._lastFrameTimestamp = timestamp; this._lastWriteTimestamp = Date.now(); + } + private async _flushLastFrame(repeatCount: number): Promise { + assert(this._process); + const frame = this._lastFrameBuffer; + if (!frame) + return; const previousWrites = this._lastWritePromise; let finishedWriting: () => void; - this._lastWritePromise = new Promise(fulfill => finishedWriting = fulfill); - const writePromise = this._lastWritePromise; + const writePromise = new Promise(fulfill => finishedWriting = fulfill); await previousWrites; - for (let i = 0; i < duration; i++) { - const callFinish = i === (duration - 1); + for (let i = 0; i < repeatCount; i++) { + const callFinish = i === (repeatCount - 1); this._process.stdin.write(frame, (error: Error | null | undefined) => { if (error) this._progress.log(`ffmpeg failed to write: ${error}`);