From 0556db97df671585ea3a573c9c3601a4aa29fade Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:54:42 +0200 Subject: [PATCH] code: Migrate filter::virtual_greenscreen to new dynamic loader --- source/filters/filter-virtual-greenscreen.cpp | 41 ++++++++++--------- source/filters/filter-virtual-greenscreen.hpp | 2 +- source/plugin.cpp | 9 ---- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/source/filters/filter-virtual-greenscreen.cpp b/source/filters/filter-virtual-greenscreen.cpp index 772c3207..40450168 100644 --- a/source/filters/filter-virtual-greenscreen.cpp +++ b/source/filters/filter-virtual-greenscreen.cpp @@ -155,7 +155,7 @@ void virtual_greenscreen_instance::update(obs_data_t* data) // Check if the user changed which Denoising provider we use. virtual_greenscreen_provider provider = static_cast(obs_data_get_int(data, ST_KEY_PROVIDER)); if (provider == virtual_greenscreen_provider::AUTOMATIC) { - provider = virtual_greenscreen_factory::get()->find_ideal_provider(); + provider = virtual_greenscreen_factory::instance()->find_ideal_provider(); } // Check if the provider was changed, and if so switch. @@ -617,7 +617,7 @@ bool streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory::is_prov virtual_greenscreen_provider streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory::find_ideal_provider() { for (auto v : provider_priority) { - if (virtual_greenscreen_factory::get()->is_provider_available(v)) { + if (virtual_greenscreen_factory::instance()->is_provider_available(v)) { return v; break; } @@ -625,26 +625,27 @@ virtual_greenscreen_provider streamfx::filter::virtual_greenscreen::virtual_gree return virtual_greenscreen_provider::AUTOMATIC; } -std::shared_ptr _video_superresolution_factory_instance = nullptr; - -void virtual_greenscreen_factory::initialize() +std::shared_ptr virtual_greenscreen_factory::instance() { - try { - if (!_video_superresolution_factory_instance) - _video_superresolution_factory_instance = std::make_shared(); - } catch (const std::exception& ex) { - D_LOG_ERROR("Failed to initialize due to error: %s", ex.what()); - } catch (...) { - D_LOG_ERROR("Failed to initialize due to unknown error.", ""); + static std::weak_ptr winst; + static std::mutex mtx; + + std::unique_lock lock(mtx); + auto instance = winst.lock(); + if (!instance) { + instance = std::shared_ptr(new virtual_greenscreen_factory()); + winst = instance; } + return instance; } -void virtual_greenscreen_factory::finalize() -{ - _video_superresolution_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr virtual_greenscreen_factory::get() -{ - return _video_superresolution_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = virtual_greenscreen_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-virtual-greenscreen.hpp b/source/filters/filter-virtual-greenscreen.hpp index d253b4fd..fe0d369a 100644 --- a/source/filters/filter-virtual-greenscreen.hpp +++ b/source/filters/filter-virtual-greenscreen.hpp @@ -109,7 +109,7 @@ namespace streamfx::filter::virtual_greenscreen { public: // Singleton static void initialize(); static void finalize(); - static std::shared_ptr<::streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory> get(); + static std::shared_ptr<::streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory> instance(); }; } // namespace streamfx::filter::virtual_greenscreen diff --git a/source/plugin.cpp b/source/plugin.cpp index ae091d62..f9e27b30 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -28,9 +28,6 @@ #ifdef ENABLE_FILTER_UPSCALING #include "filters/filter-upscaling.hpp" #endif -#ifdef ENABLE_FILTER_VIRTUAL_GREENSCREEN -#include "filters/filter-virtual-greenscreen.hpp" -#endif #ifdef ENABLE_FRONTEND #include "ui/ui.hpp" @@ -142,9 +139,6 @@ MODULE_EXPORT bool obs_module_load(void) #endif #ifdef ENABLE_FILTER_UPSCALING streamfx::filter::upscaling::upscaling_factory::initialize(); -#endif -#ifdef ENABLE_FILTER_VIRTUAL_GREENSCREEN - streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory::initialize(); #endif } @@ -177,9 +171,6 @@ MODULE_EXPORT void obs_module_unload(void) #endif #ifdef ENABLE_FILTER_UPSCALING streamfx::filter::upscaling::upscaling_factory::finalize(); -#endif -#ifdef ENABLE_FILTER_VIRTUAL_GREENSCREEN - streamfx::filter::virtual_greenscreen::virtual_greenscreen_factory::finalize(); #endif }