diff --git a/src/browser/provider/built-in/dedicated/chrome/cdp-client/index.ts b/src/browser/provider/built-in/dedicated/chrome/cdp-client/index.ts index 2cf7edb6f6..bd527ca742 100644 --- a/src/browser/provider/built-in/dedicated/chrome/cdp-client/index.ts +++ b/src/browser/provider/built-in/dedicated/chrome/cdp-client/index.ts @@ -51,8 +51,9 @@ export class BrowserClient { private readonly _runtimeInfo: RuntimeInfo; private _parentTarget?: remoteChrome.TargetInfo; private readonly debugLogger: debug.Debugger; - private readonly _videoFramesBuffer: VideoFrameData[]; + private _videoFramesBuffer: VideoFrameData[]; private _lastFrame: VideoFrameData | null; + private _screencastFrameListenerAttached = false; public constructor (runtimeInfo: RuntimeInfo) { this._runtimeInfo = runtimeInfo; @@ -358,16 +359,32 @@ export class BrowserClient { if (!client) return; - client.Page.on('screencastFrame', (event: ScreencastFrameEvent) => { - this._videoFramesBuffer.push({ - data: event.data, - sessionId: event.sessionId, + if (!this._screencastFrameListenerAttached) { + client.Page.on('screencastFrame', (event: ScreencastFrameEvent) => { + this._videoFramesBuffer.push({ + data: event.data, + sessionId: event.sessionId, + }); }); - }); + + this._screencastFrameListenerAttached = true; + } await client.Page.startScreencast(SCREENCAST_OPTIONS as StartScreencastRequest); } + public async stopCapturingVideo (): Promise { + const client = await this.getActiveClient(); + + if (!client) + return; + + await client.Page.stopScreencast(); + + this._lastFrame = null; + this._videoFramesBuffer = []; + } + public async getVideoFrameData (): Promise { const currentVideoFrame = this._videoFramesBuffer.shift() || this._lastFrame; diff --git a/src/browser/provider/built-in/dedicated/chrome/index.js b/src/browser/provider/built-in/dedicated/chrome/index.js index 811a8e7038..34d5cd3ba8 100644 --- a/src/browser/provider/built-in/dedicated/chrome/index.js +++ b/src/browser/provider/built-in/dedicated/chrome/index.js @@ -128,6 +128,12 @@ export default { await browserClient.startCapturingVideo(); }, + async stopCapturingVideo (browserId) { + const { browserClient } = this.openedBrowsers[browserId]; + + await browserClient.stopCapturingVideo(); + }, + async getVideoFrameData (browserId) { const { browserClient } = this.openedBrowsers[browserId]; diff --git a/src/browser/provider/index.ts b/src/browser/provider/index.ts index 7b5b85cc80..ef53d1debd 100644 --- a/src/browser/provider/index.ts +++ b/src/browser/provider/index.ts @@ -417,6 +417,10 @@ export default class BrowserProvider { await this.plugin.startCapturingVideo(browserId); } + public async stopCapturingVideo (browserId: string): Promise { + await this.plugin.stopCapturingVideo(browserId); + } + public async hasCustomActionForBrowser (browserId: string): Promise { return this.plugin.hasCustomActionForBrowser(browserId); } diff --git a/src/video-recorder/process.js b/src/video-recorder/process.js index b81575ebfb..bcf6550176 100644 --- a/src/video-recorder/process.js +++ b/src/video-recorder/process.js @@ -130,6 +130,10 @@ export default class VideoRecorder extends AsyncEmitter { await this.connection.provider.startCapturingVideo(this.connection.id); } + async _stopCapturing () { + await this.connection.provider.stopCapturingVideo(this.connection.id); + } + async init () { this.ffmpegProcess = spawn(this.ffmpegPath, this.optionsList, { stdio: 'pipe' }); @@ -183,6 +187,7 @@ export default class VideoRecorder extends AsyncEmitter { this.closed = true; + await this._stopCapturing(); await this.capturingPromise; await this.dispose(); }