From a6827ae8e66745407132d73515fa9aad5615800b Mon Sep 17 00:00:00 2001 From: Ylva Selling Date: Thu, 23 Feb 2023 16:10:10 -0500 Subject: [PATCH] Add syncable to syncengine & improve synchronization --- modules/video/include/videoplayer.h | 2 +- modules/video/src/videoplayer.cpp | 74 ++++++++++++++--------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/modules/video/include/videoplayer.h b/modules/video/include/videoplayer.h index 725f0e727a..ae600e6a78 100644 --- a/modules/video/include/videoplayer.h +++ b/modules/video/include/videoplayer.h @@ -110,7 +110,6 @@ private: double correctVideoPlaybackTime() const; bool isWithingStartEndTime() const; void updateFrameDuration(); - void syncToSimulationTime(); void stepFrameForward(); void stepFrameBackward(); @@ -136,6 +135,7 @@ private: // Syncing with multiple nodes double _correctPlaybackTime = 0.0; + double _deltaTime = 0.0; // Video stretching: map to simulation time animation mode double _startJ200Time = 0.0; diff --git a/modules/video/src/videoplayer.cpp b/modules/video/src/videoplayer.cpp index 22d0b2cd13..87b854f098 100644 --- a/modules/video/src/videoplayer.cpp +++ b/modules/video/src/videoplayer.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -300,6 +301,8 @@ VideoPlayer::VideoPlayer(const ghoul::Dictionary& dictionary) swapBuffersMpv(); }); + global::syncEngine->addSyncable(this); + keys = { {MpvKey::Pause, "pause"}, {MpvKey::Params, "video-params"}, @@ -326,6 +329,7 @@ VideoPlayer::VideoPlayer(const ghoul::Dictionary& dictionary) {MpvKey::IsSeeking, MPV_FORMAT_DOUBLE}, {MpvKey::Mute, MPV_FORMAT_STRING} }; + } void VideoPlayer::pause() { @@ -810,24 +814,51 @@ void VideoPlayer::destroy() { void VideoPlayer::preSync(bool isMaster) { if (isMaster) { - syncToSimulationTime(); - _correctPlaybackTime = _currentVideoTime; + if (_playbackMode == PlaybackMode::MapToSimulationTime) { + _correctPlaybackTime = correctVideoPlaybackTime(); + double now = global::timeManager->time().j2000Seconds(); + _deltaTime = now - _timeAtLastRender; + } + else if (_playbackMode == PlaybackMode::RealTimeLoop) { + _correctPlaybackTime = _currentVideoTime; + } } } void VideoPlayer::encode(SyncBuffer* syncBuffer) { syncBuffer->encode(_correctPlaybackTime); + syncBuffer->encode(_deltaTime); } void VideoPlayer::decode(SyncBuffer* syncBuffer) { syncBuffer->decode(_correctPlaybackTime); + syncBuffer->decode(_deltaTime); } void VideoPlayer::postSync(bool isMaster) { - if (!isMaster) { - if (abs(_currentVideoTime - _correctPlaybackTime) < glm::epsilon()) { - seekToTime(_correctPlaybackTime); + if (_playbackMode == PlaybackMode::MapToSimulationTime) { + // If we are in valid times, step frames accordingly + if (isWithingStartEndTime()) { + double now = global::timeManager->time().j2000Seconds(); + if (_deltaTime > _frameDuration) { + // Stepping forwards + stepFrameForward(); + _timeAtLastRender = now; // Only used on master node + } + else if (_deltaTime < -_frameDuration) { + // Stepping backwards + stepFrameBackward(); + _timeAtLastRender = now; // Only used on master node + } } + else if (!_isPaused) { + pause(); + } + } + // Make sure we are at the correct time + bool shouldSeek = abs(_correctPlaybackTime - _currentVideoTime) > _seekThreshold; + if (shouldSeek) { + seekToTime(_correctPlaybackTime); } } @@ -873,39 +904,6 @@ double VideoPlayer::correctVideoPlaybackTime() const { return percentage * _videoDuration; } -void VideoPlayer::syncToSimulationTime() { - if (!global::windowDelegate->isMaster()) { - return; - } - if (_playbackMode == PlaybackMode::MapToSimulationTime) { - // If we are in valid times, step frames accordingly - if (isWithingStartEndTime()) { - double now = global::timeManager->time().j2000Seconds(); - double deltaTime = now - _timeAtLastRender; - if (deltaTime > _frameDuration) { - // Stepping forwards - stepFrameForward(); - _timeAtLastRender = now; - } - else if (deltaTime < -_frameDuration) { - // Stepping backwards - stepFrameBackward(); - _timeAtLastRender = now; - } - } - else if (!_isPaused) { - pause(); - } - // Make sure we are at the correct time - double time = correctVideoPlaybackTime(); - bool shouldSeek = abs(time - _currentVideoTime) > _seekThreshold; - if (shouldSeek) { - seekToTime(time); - } - } -} - - void VideoPlayer::createFBO(int width, int height) { LINFO(fmt::format("Creating new FBO with width: {} and height: {}", width, height));