Skip to content

Commit

Permalink
Improve the handling of path string (#528)
Browse files Browse the repository at this point in the history
* Fix uninitialized string bug

* Update background-filter.cpp

* Update ort-session-utils.cpp

* Update ort-session-utils.cpp

* Update ort-session-utils.cpp

* Update ort-session-utils.cpp

* Update ort-session-utils.cpp

* Update ort-session-utils.cpp
  • Loading branch information
umireon authored Jan 18, 2024
1 parent 013e71f commit 403dc76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/FilterData.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct filter_data : public ORTModelData {
std::mutex outputLock;

#if _WIN32
const wchar_t *modelFilepath = nullptr;
std::wstring modelFilepath;
#else
const char *modelFilepath = nullptr;
std::string modelFilepath;
#endif
};

Expand Down
4 changes: 2 additions & 2 deletions src/background-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ void background_filter_update(void *data, obs_data_t *settings)
obs_log(LOG_INFO, " Blur Focus Depth: %f", tf->blurFocusDepth);
obs_log(LOG_INFO, " Disabled: %s", tf->isDisabled ? "true" : "false");
#ifdef _WIN32
obs_log(LOG_INFO, " Model file path: %S", tf->modelFilepath);
obs_log(LOG_INFO, " Model file path: %S", tf->modelFilepath.c_str());
#else
obs_log(LOG_INFO, " Model file path: %s", tf->modelFilepath);
obs_log(LOG_INFO, " Model file path: %s", tf->modelFilepath.c_str());
#endif
}

Expand Down
19 changes: 11 additions & 8 deletions src/ort-utils/ort-session-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifdef _WIN32
#include <dml_provider_factory.h>
#include <wchar.h>
#include <windows.h>
#endif // _WIN32

#include <obs-module.h>
Expand Down Expand Up @@ -51,17 +52,19 @@ int createOrtSession(filter_data *tf)
}

std::string modelFilepath_s(modelFilepath_rawPtr);
bfree(modelFilepath_rawPtr);

#if _WIN32
std::wstring modelFilepath_ws(modelFilepath_s.size(), L' ');
std::copy(modelFilepath_s.begin(), modelFilepath_s.end(),
modelFilepath_ws.begin());
tf->modelFilepath = modelFilepath_ws.c_str();
int outLength = MultiByteToWideChar(
CP_ACP, MB_PRECOMPOSED, modelFilepath_rawPtr, -1, nullptr, 0);
tf->modelFilepath = std::wstring(outLength, L'\0');
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, modelFilepath_rawPtr, -1,
tf->modelFilepath.data(), outLength);
#else
tf->modelFilepath = modelFilepath_s.c_str();
tf->modelFilepath = std::string(modelFilepath_rawPtr);
#endif

bfree(modelFilepath_rawPtr);

try {
#if defined(__linux__) && defined(__x86_64__) && \
!defined(DISABLE_ONNXRUNTIME_GPU)
Expand Down Expand Up @@ -92,8 +95,8 @@ int createOrtSession(filter_data *tf)
sessionOptions, coreml_flags));
}
#endif
tf->session.reset(new Ort::Session(*tf->env, tf->modelFilepath,
sessionOptions));
tf->session.reset(new Ort::Session(
*tf->env, tf->modelFilepath.c_str(), sessionOptions));
} catch (const std::exception &e) {
obs_log(LOG_ERROR, "%s", e.what());
return OBS_BGREMOVAL_ORT_SESSION_ERROR_STARTUP;
Expand Down

0 comments on commit 403dc76

Please sign in to comment.