Skip to content

Commit

Permalink
Fixed issue with ImageRecordingSource being stale when toggling betwe…
Browse files Browse the repository at this point in the history
…en preview off and on.
  • Loading branch information
sskodje committed Sep 26, 2024
1 parent 179a54b commit 883f538
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
29 changes: 14 additions & 15 deletions ScreenRecorderLibNative/ImageReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@ HRESULT ImageReader::GetNativeSize(_In_ RECORDING_SOURCE_BASE &recordingSource,

HRESULT ImageReader::AcquireNextFrame(_In_ DWORD timeoutMillis, _Outptr_opt_result_maybenull_ ID3D11Texture2D **ppFrame)
{
if (m_Texture && m_LastGrabTimeStamp.QuadPart == 0) {
if (ppFrame) {
CComPtr<ID3D11Texture2D> pStagingTexture = nullptr;
D3D11_TEXTURE2D_DESC desc;
m_Texture->GetDesc(&desc);
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.MiscFlags = 0;
desc.Usage = D3D11_USAGE_DEFAULT;

RETURN_ON_BAD_HR(m_Device->CreateTexture2D(&desc, nullptr, &pStagingTexture));
m_DeviceContext->CopyResource(pStagingTexture, m_Texture);
*ppFrame = pStagingTexture;
(*ppFrame)->AddRef();
QueryPerformanceCounter(&m_LastGrabTimeStamp);
}
if (m_Texture && ppFrame) {
CComPtr<ID3D11Texture2D> pStagingTexture = nullptr;
D3D11_TEXTURE2D_DESC desc;
m_Texture->GetDesc(&desc);
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.MiscFlags = 0;
desc.Usage = D3D11_USAGE_DEFAULT;

RETURN_ON_BAD_HR(m_Device->CreateTexture2D(&desc, nullptr, &pStagingTexture));
m_DeviceContext->CopyResource(pStagingTexture, m_Texture);
*ppFrame = pStagingTexture;
(*ppFrame)->AddRef();
QueryPerformanceCounter(&m_LastGrabTimeStamp);

return S_OK;
}
else {
Expand Down
13 changes: 9 additions & 4 deletions ScreenRecorderLibNative/ScreenCaptureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ DWORD WINAPI CaptureThreadProc(_In_ void *Param)

*pData->ThreadResult = {};
pData->ThreadResult->RecordingResult = S_OK;

bool isPreviewEnabled = pSource->IsVideoFramePreviewEnabled.value_or(false);
// Main duplication loop
std::chrono::steady_clock::time_point WaitForFrameBegin = (std::chrono::steady_clock::time_point::min)();
while (true)
Expand All @@ -854,7 +856,10 @@ DWORD WINAPI CaptureThreadProc(_In_ void *Param)
isSourceDirty = true;
goto Start;
}

if (isPreviewEnabled != pSource->IsVideoFramePreviewEnabled.value_or(false)) {
isPreviewEnabled = pSource->IsVideoFramePreviewEnabled.value_or(false);
isSharedSurfaceDirty = true;
}
if (!isCapturingVideo) {
Sleep(1);
if (pSource->IsVideoCaptureEnabled.value_or(true)) {
Expand Down Expand Up @@ -963,15 +968,15 @@ DWORD WINAPI CaptureThreadProc(_In_ void *Param)
}
pData->TotalUpdatedFrameCount++;
QueryPerformanceCounter(&pData->LastUpdateTimeStamp);
}
}
}
catch (const AccessViolationException &ex) {
hr = EXCEPTION_ACCESS_VIOLATION;
}
catch (...) {
hr = E_UNEXPECTED;
}
}
}
Exit:
if (pData->ThreadResult) {
//E_ABORT is returned when the capture loop should be stopped, but the recording continue. On other errors, we check how to handle them.
Expand Down Expand Up @@ -1012,7 +1017,7 @@ DWORD WINAPI CaptureThreadProc(_In_ void *Param)
CoUninitialize();
LOG_DEBUG("Exiting CaptureThreadProc");
return 0;
}
}


DWORD WINAPI OverlayCaptureThreadProc(_In_ void *Param) {
Expand Down

0 comments on commit 883f538

Please sign in to comment.