From e3e81625d9817f8fc715e3af484940d63b560b52 Mon Sep 17 00:00:00 2001 From: Matthew Rayfield Date: Thu, 14 Sep 2017 14:25:59 -0400 Subject: [PATCH] added ability to sync video with capture --- src/CCapture.js | 83 +++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/src/CCapture.js b/src/CCapture.js index d8d9a7b..662a8e8 100755 --- a/src/CCapture.js +++ b/src/CCapture.js @@ -707,22 +707,28 @@ function CCapture( settings ) { return _performanceTime; }; - function hookCurrentTime() {  - if( !this._hooked ) { - this._hooked = true; - this._hookedTime = this.currentTime || 0; - this.pause(); - media.push( this ); - } - return this._hookedTime + _settings.startTime; - }; - - try { - Object.defineProperty( HTMLVideoElement.prototype, 'currentTime', { get: hookCurrentTime } ) - Object.defineProperty( HTMLAudioElement.prototype, 'currentTime', { get: hookCurrentTime } ) - } catch (err) { - _log(err); - } + if (_settings.syncVideo) { + _settings.syncVideo.pause(); + _settings.syncVideo.addEventListener('seeked', _callCallbacks); + } + else { + function hookCurrentTime() {  + if( !this._hooked ) { + this._hooked = true; + this._hookedTime = this.currentTime || 0; + this.pause(); + media.push( this ); + } + return this._hookedTime + _settings.startTime; + }; + + try { + Object.defineProperty( HTMLVideoElement.prototype, 'currentTime', { get: hookCurrentTime } ) + Object.defineProperty( HTMLAudioElement.prototype, 'currentTime', { get: hookCurrentTime } ) + } catch (err) { + _log(err); + } + } } @@ -757,6 +763,12 @@ function CCapture( settings ) { window.Date.prototype.getTime = _oldGetTime; window.Date.now = _oldNow; window.performance.now = _oldPerformanceNow; + + if (_settings.syncVideo) { + _settings.syncVideo.removeEventListener('seeked', _callCallbacks); + _settings.syncVideo.play(); + _callCallbacks(); + } } function _updateTime() { @@ -847,21 +859,8 @@ function CCapture( settings ) { } } - - function _process() { - - var step = 1000 / _settings.framerate; - var dt = ( _frameCount + _intermediateFrameCount / _settings.motionBlurFrames ) * step; - - _time = _startTime + dt; - _performanceTime = _performanceStartTime + dt; - - media.forEach( function( v ) { - v._hookedTime = dt / 1000; - } ); - _updateTime(); - _log( 'Frame: ' + _frameCount + ' ' + _intermediateFrameCount ); + function _callCallbacks() { for( var j = 0; j < _timeouts.length; j++ ) { if( _time >= _timeouts[ j ].triggerTime ) { @@ -886,6 +885,30 @@ function CCapture( settings ) { } ); _requestAnimationFrameCallbacks = []; + } + + function _process() { + + var step = 1000 / _settings.framerate; + var dt = ( _frameCount + _intermediateFrameCount / _settings.motionBlurFrames ) * step; + + _time = _startTime + dt; + _performanceTime = _performanceStartTime + dt; + + media.forEach( function( v ) { + v._hookedTime = dt / 1000; + } ); + + _updateTime(); + _log( 'Frame: ' + _frameCount + ' ' + _intermediateFrameCount ); + + if (_settings.syncVideo) { + _settings.syncVideo.currentTime += step / 1000; + } + else { + _callCallbacks(); + } + } function _save( callback ) {