Skip to content

Commit

Permalink
Fix for sskodje#272 : empty folder created when recording to path.
Browse files Browse the repository at this point in the history
  • Loading branch information
sskodje authored and yujahyf97 committed Apr 19, 2024
1 parent d696ef7 commit fc64695
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 19 deletions.
92 changes: 73 additions & 19 deletions ScreenRecorderLibNative/RecordingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,81 @@ HRESULT RecordingManager::ConfigureOutputDir(_In_ std::wstring path) {
}
}
}
if (!m_SnapshotOptions->GetSnapshotsDirectory().empty()) {
std::error_code ec;
if (std::filesystem::exists(m_SnapshotOptions->GetSnapshotsDirectory()) || std::filesystem::create_directories(m_SnapshotOptions->GetSnapshotsDirectory(), ec))

return S_OK;
}

HRESULT RecordingManager::TakeSnapshot(_In_ std::wstring path)
{
if (path.empty()) {
if (!GetSnapshotOptions()->GetSnapshotsDirectory().empty())
{
LOG_DEBUG(L"Snapshot output folder is ready");
path = GetSnapshotOptions()->GetSnapshotsDirectory() + L"\\" + s2ws(CurrentTimeToFormattedString(true)) + GetSnapshotOptions()->GetImageExtension();
}
else
}
return TakeSnapshot(path, nullptr);
}

HRESULT RecordingManager::TakeSnapshot(_In_ IStream *stream)
{
return TakeSnapshot(L"", stream);
}

HRESULT RecordingManager::TakeSnapshot(_In_opt_ std::wstring path, _In_opt_ IStream *stream, _In_opt_ ID3D11Texture2D *pTexture) {
if (!m_IsRecording) {
return E_NOT_VALID_STATE;
}
HRESULT hr = E_FAIL;
CComPtr<ID3D11Texture2D> processedTexture;
if (!pTexture) {
CAPTURED_FRAME capturedFrame{};
hr = m_CaptureManager->CopyCurrentFrame(&capturedFrame);
RETURN_ON_BAD_HR(hr);
hr = ProcessTexture(capturedFrame.Frame, &processedTexture, capturedFrame.PtrInfo);
SafeRelease(&capturedFrame.Frame);
}
else {
processedTexture = pTexture;
}
RECT videoInputFrameRect{};
RETURN_ON_BAD_HR(hr = InitializeRects(m_CaptureManager->GetOutputSize(), &videoInputFrameRect, nullptr));

if (!path.empty()) {
std::wstring directory = std::filesystem::path(path).parent_path().wstring();
if (!std::filesystem::exists(directory))
{
// Failed to create snapshot directory.
LOG_ERROR(L"failed to create snapshot output folder");
if (RecordingFailedCallback != nullptr)
RecordingFailedCallback(L"Failed to create snapshot output folder: " + s2ws(ec.message()), L"");
return E_FAIL;
std::error_code ec;
if (std::filesystem::create_directories(directory, ec)) {
LOG_DEBUG(L"Snapshot output folder created");
}
else {
// Failed to create snapshot directory.
LOG_ERROR(L"failed to create snapshot output folder");
return E_FAIL;
}
}
RETURN_ON_BAD_HR(hr = SaveTextureAsVideoSnapshot(processedTexture, path, videoInputFrameRect));
LOG_TRACE(L"Wrote snapshot to %s", path.c_str());
if (RecordingSnapshotCreatedCallback != nullptr) {
RecordingSnapshotCreatedCallback(path);
}
}
return S_OK;
else if (stream) {
RETURN_ON_BAD_HR(hr = SaveTextureAsVideoSnapshot(processedTexture, stream, videoInputFrameRect));
LOG_TRACE(L"Wrote snapshot to stream");
if (RecordingSnapshotCreatedCallback != nullptr) {
RecordingSnapshotCreatedCallback(L"");
}
}
else {
LOG_ERROR("Snapshot failed: No valid stream or path provided.");
hr = E_INVALIDARG;
}

return hr;
}

HRESULT RecordingManager::BeginRecording(_In_opt_ IStream *stream) {
HRESULT RecordingManager::BeginRecording(_In_ IStream *stream) {
return BeginRecording(L"", stream);
}

Expand Down Expand Up @@ -532,13 +588,11 @@ REC_RESULT RecordingManager::StartRecorderLoop(_In_ const std::vector<RECORDING_
}
if (recorderMode == RecorderModeInternal::Video) {
if (GetSnapshotOptions()->IsSnapshotWithVideoEnabled() && IsTimeToTakeSnapshot()) {
if (SUCCEEDED(renderHr = SaveTextureAsVideoSnapshot(pTextureToRender, videoInputFrameRect))) {
previousSnapshotTaken = steady_clock::now();
}
else {
_com_error err(renderHr);
LOG_ERROR("Error saving video snapshot: %ls", err.ErrorMessage());
}
if (GetSnapshotOptions()->GetSnapshotsDirectory().empty())
return S_FALSE;
wstring snapshotPath = GetSnapshotOptions()->GetSnapshotsDirectory() + L"\\" + s2ws(CurrentTimeToFormattedString(true)) + GetSnapshotOptions()->GetImageExtension();
TakeSnapshot(snapshotPath, nullptr, pTextureToRender);
previousSnapshotTaken = steady_clock::now();
}
//m_OutputManager->WriteFrameToImage(pTextureToRender, L"D:\\test\\beforemodel.png");
if (pPtrInfo) {
Expand Down
4 changes: 4 additions & 0 deletions ScreenRecorderLibNative/RecordingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class RecordingManager
HRESULT ConfigureOutputDir(_In_ std::wstring path);
REC_RESULT StartRecorderLoop(_In_ const std::vector<RECORDING_SOURCE *> &sources, _In_ const std::vector<RECORDING_OVERLAY *> &overlays, _In_opt_ IStream *pStream);


HRESULT TakeSnapshot(_In_opt_ std::wstring path, _In_opt_ IStream *pStream, _In_opt_ ID3D11Texture2D *pTexture = nullptr);
HRESULT BeginRecording(_In_opt_ std::wstring path, _In_opt_ IStream *pStream);

/// <summary>
/// Creates adjusted source and output rects from a recording frame rect. The source rect is normalized to start on [0,0], and the output is adjusted for any cropping.
/// </summary>
Expand Down

0 comments on commit fc64695

Please sign in to comment.