From 7118bcc28849be4105ee4525947b6859920458b7 Mon Sep 17 00:00:00 2001 From: hecomi Date: Sat, 8 May 2021 12:20:48 +0900 Subject: [PATCH] fix restart bug. --- .../uWindowCapture/WindowsGraphicsCapture.cpp | 30 ++++++++++--------- .../uWindowCapture/WindowsGraphicsCapture.h | 8 +++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.cpp b/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.cpp index 6b2e52b..acf3376 100644 --- a/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.cpp +++ b/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.cpp @@ -128,9 +128,9 @@ WindowsGraphicsCapture::WindowsGraphicsCapture(HMONITOR hMonitor) } -void WindowsGraphicsCapture::CreateItem() +bool WindowsGraphicsCapture::CreateItem() { - if (!IsSupported()) return; + if (!IsSupported()) return false; CallWinRtApiWithExceptionCheck([&] { @@ -152,11 +152,12 @@ void WindowsGraphicsCapture::CreateItem() } }, "WindowsGraphicsCapture::CreateItem()"); - if (item_) - { - item_.DisplayName().c_str(); - size_ = item_.Size(); - } + if (!item_) return false; + + item_.DisplayName().c_str(); + size_ = item_.Size(); + + return true; } @@ -221,7 +222,7 @@ void WindowsGraphicsCapture::Start() if (CreatePoolAndSession()) { isStarted_ = true; - latestFrameTime_ = std::chrono::high_resolution_clock::now(); + restartTimer_ = 0.f; if (const auto& manager = WindowManager::GetWindowsGraphicsCaptureManager()) { @@ -267,7 +268,7 @@ bool WindowsGraphicsCapture::ShouldStop() const void WindowsGraphicsCapture::Restart() { Stop(); - CreateItem(); + if (!CreateItem()) return; Start(); isSessionRestartRequested_ = false; } @@ -284,6 +285,7 @@ void WindowsGraphicsCapture::Update(float dt) if (!IsStarted()) return; stopTimer_ = stopTimer_ + dt; + restartTimer_ = restartTimer_ + dt; } @@ -327,14 +329,12 @@ WindowsGraphicsCapture::Result WindowsGraphicsCapture::TryGetLatestResult() if (frame_) { - latestFrameTime_ = std::chrono::high_resolution_clock::now(); + restartTimer_ = 0.f; } else { - constexpr std::chrono::milliseconds timeoutThresh(1000); - const auto currentTime = std::chrono::high_resolution_clock::now(); - const auto dt = std::chrono::duration_cast(currentTime - latestFrameTime_); - if (dt > timeoutThresh) + constexpr float timeoutThresh = 1.f; + if (restartTimer_ > timeoutThresh) { isSessionRestartRequested_ = true; } @@ -457,6 +457,7 @@ void WindowsGraphicsCaptureManager::UpdateFromMainThread(float dt) void WindowsGraphicsCaptureManager::UpdateFromCaptureThread() { + UpdateRemoveInstances(); UpdateAddInstances(); { @@ -513,6 +514,7 @@ void WindowsGraphicsCaptureManager::UpdateRemoveInstances() void WindowsGraphicsCaptureManager::StopAllInstances() { + UpdateRemoveInstances(); UpdateAddInstances(); { diff --git a/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.h b/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.h index 00d5ac2..aa3f307 100644 --- a/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.h +++ b/Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.h @@ -54,7 +54,7 @@ class WindowsGraphicsCapture const wchar_t * GetDisplayName() const; private: - void CreateItem(); + bool CreateItem(); bool CreatePoolAndSession(); void DestroyPoolAndSession(); @@ -68,11 +68,13 @@ class WindowsGraphicsCapture Callback callback_; bool isStarted_ = false; std::mutex sessionAndPoolMutex_; - std::chrono::time_point latestFrameTime_; + + std::atomic isCursorCaptureEnabled_ = { true }; std::atomic stopTimer_ = { 0.f }; std::atomic hasStopRequested_ = { false }; - std::atomic isCursorCaptureEnabled_ = { true }; + + std::atomic restartTimer_ = { 0.f }; std::atomic isSessionRestartRequested_ = { false }; };