@@ -26,7 +26,7 @@ const fps = 25;
26
26
export class VideoRecorder {
27
27
private _process : ChildProcess | null = null ;
28
28
private _gracefullyClose : ( ( ) => Promise < void > ) | null = null ;
29
- private _lastWritePromise : Promise < void > ;
29
+ private _lastWritePromise : Promise < void > | undefined ;
30
30
private _lastFrameTimestamp : number = 0 ;
31
31
private _lastFrameBuffer : Buffer | null = null ;
32
32
private _lastWriteTimestamp : number = 0 ;
@@ -47,7 +47,6 @@ export class VideoRecorder {
47
47
48
48
private constructor ( progress : Progress ) {
49
49
this . _progress = progress ;
50
- this . _lastWritePromise = Promise . resolve ( ) ;
51
50
}
52
51
53
52
private async _launch ( options : types . PageScreencastOptions ) {
@@ -89,22 +88,24 @@ export class VideoRecorder {
89
88
assert ( this . _process ) ;
90
89
if ( ! this . _isRunning ( ) )
91
90
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 ) ) ;
95
94
this . _lastFrameBuffer = frame ;
96
95
this . _lastFrameTimestamp = timestamp ;
97
96
this . _lastWriteTimestamp = Date . now ( ) ;
98
97
}
99
98
100
- private async _flushLastFrame ( repeatCount : number ) : Promise < void > {
99
+ private async _flushLastFrame ( durationSec : number ) : Promise < void > {
101
100
assert ( this . _process ) ;
102
101
const frame = this . _lastFrameBuffer ;
103
102
if ( ! frame )
104
103
return ;
105
104
const previousWrites = this . _lastWritePromise ;
106
105
let finishedWriting : ( ) => void ;
107
106
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)` ) ;
108
109
await previousWrites ;
109
110
for ( let i = 0 ; i < repeatCount ; i ++ ) {
110
111
const callFinish = i === ( repeatCount - 1 ) ;
@@ -124,8 +125,8 @@ export class VideoRecorder {
124
125
125
126
if ( this . _lastWriteTimestamp ) {
126
127
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 ) ) ;
129
130
}
130
131
131
132
const close = this . _gracefullyClose ;
0 commit comments