From debf976121681414eeb8ab26814828c423e05dc6 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:57:29 +0200 Subject: [PATCH] code: Migrate filter::denoising to new dynamic loader --- source/filters/filter-denoising.cpp | 39 +++++++++++++++-------------- source/filters/filter-denoising.hpp | 2 +- source/plugin.cpp | 9 ------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/source/filters/filter-denoising.cpp b/source/filters/filter-denoising.cpp index ae6d9765..41d1a984 100644 --- a/source/filters/filter-denoising.cpp +++ b/source/filters/filter-denoising.cpp @@ -144,7 +144,7 @@ void denoising_instance::update(obs_data_t* data) // Check if the user changed which Denoising provider we use. denoising_provider provider = static_cast(obs_data_get_int(data, ST_KEY_PROVIDER)); if (provider == denoising_provider::AUTOMATIC) { - provider = denoising_factory::get()->find_ideal_provider(); + provider = denoising_factory::instance()->find_ideal_provider(); } // Check if the provider was changed, and if so switch. @@ -621,26 +621,27 @@ denoising_provider streamfx::filter::denoising::denoising_factory::find_ideal_pr return denoising_provider::AUTOMATIC; } -std::shared_ptr _video_denoising_factory_instance = nullptr; - -void denoising_factory::initialize() +std::shared_ptr denoising_factory::instance() { - try { - if (!_video_denoising_factory_instance) - _video_denoising_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 denoising_factory()); + winst = instance; } + return instance; } -void denoising_factory::finalize() -{ - _video_denoising_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr denoising_factory::get() -{ - return _video_denoising_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = denoising_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-denoising.hpp b/source/filters/filter-denoising.hpp index bdb7aedb..99760ff1 100644 --- a/source/filters/filter-denoising.hpp +++ b/source/filters/filter-denoising.hpp @@ -108,7 +108,7 @@ namespace streamfx::filter::denoising { public: // Singleton static void initialize(); static void finalize(); - static std::shared_ptr<::streamfx::filter::denoising::denoising_factory> get(); + static std::shared_ptr<::streamfx::filter::denoising::denoising_factory> instance(); }; } // namespace streamfx::filter::denoising diff --git a/source/plugin.cpp b/source/plugin.cpp index 2990c7db..af25aea1 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -19,9 +19,6 @@ #ifdef ENABLE_FILTER_AUTOFRAMING #include "filters/filter-autoframing.hpp" #endif -#ifdef ENABLE_FILTER_DENOISING -#include "filters/filter-denoising.hpp" -#endif #ifdef ENABLE_FILTER_DISPLACEMENT #include "filters/filter-displacement.hpp" #endif @@ -128,9 +125,6 @@ MODULE_EXPORT bool obs_module_load(void) #ifdef ENABLE_FILTER_AUTOFRAMING streamfx::filter::autoframing::autoframing_factory::initialize(); #endif -#ifdef ENABLE_FILTER_DENOISING - streamfx::filter::denoising::denoising_factory::initialize(); -#endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::initialize(); #endif @@ -157,9 +151,6 @@ MODULE_EXPORT void obs_module_unload(void) #ifdef ENABLE_FILTER_AUTOFRAMING streamfx::filter::autoframing::autoframing_factory::finalize(); #endif -#ifdef ENABLE_FILTER_DENOISING - streamfx::filter::denoising::denoising_factory::finalize(); -#endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::finalize(); #endif