Skip to content

Commit

Permalink
fix restart bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed May 8, 2021
1 parent 83646bd commit 7118bcc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
30 changes: 16 additions & 14 deletions Plugins/uWindowCapture/uWindowCapture/WindowsGraphicsCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ WindowsGraphicsCapture::WindowsGraphicsCapture(HMONITOR hMonitor)
}


void WindowsGraphicsCapture::CreateItem()
bool WindowsGraphicsCapture::CreateItem()
{
if (!IsSupported()) return;
if (!IsSupported()) return false;

CallWinRtApiWithExceptionCheck([&]
{
Expand All @@ -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;
}


Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -267,7 +268,7 @@ bool WindowsGraphicsCapture::ShouldStop() const
void WindowsGraphicsCapture::Restart()
{
Stop();
CreateItem();
if (!CreateItem()) return;
Start();
isSessionRestartRequested_ = false;
}
Expand All @@ -284,6 +285,7 @@ void WindowsGraphicsCapture::Update(float dt)
if (!IsStarted()) return;

stopTimer_ = stopTimer_ + dt;
restartTimer_ = restartTimer_ + dt;
}


Expand Down Expand Up @@ -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<std::chrono::milliseconds>(currentTime - latestFrameTime_);
if (dt > timeoutThresh)
constexpr float timeoutThresh = 1.f;
if (restartTimer_ > timeoutThresh)
{
isSessionRestartRequested_ = true;
}
Expand Down Expand Up @@ -457,6 +457,7 @@ void WindowsGraphicsCaptureManager::UpdateFromMainThread(float dt)

void WindowsGraphicsCaptureManager::UpdateFromCaptureThread()
{
UpdateRemoveInstances();
UpdateAddInstances();

{
Expand Down Expand Up @@ -513,6 +514,7 @@ void WindowsGraphicsCaptureManager::UpdateRemoveInstances()

void WindowsGraphicsCaptureManager::StopAllInstances()
{
UpdateRemoveInstances();
UpdateAddInstances();

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class WindowsGraphicsCapture
const wchar_t * GetDisplayName() const;

private:
void CreateItem();
bool CreateItem();
bool CreatePoolAndSession();
void DestroyPoolAndSession();

Expand All @@ -68,11 +68,13 @@ class WindowsGraphicsCapture
Callback callback_;
bool isStarted_ = false;
std::mutex sessionAndPoolMutex_;
std::chrono::time_point<std::chrono::steady_clock> latestFrameTime_;

std::atomic<bool> isCursorCaptureEnabled_ = { true };

std::atomic<float> stopTimer_ = { 0.f };
std::atomic<bool> hasStopRequested_ = { false };
std::atomic<bool> isCursorCaptureEnabled_ = { true };

std::atomic<float> restartTimer_ = { 0.f };
std::atomic<bool> isSessionRestartRequested_ = { false };
};

Expand Down

0 comments on commit 7118bcc

Please sign in to comment.