diff --git a/source/filters/filter-autoframing.cpp b/source/filters/filter-autoframing.cpp index 787b60dd..069211d9 100644 --- a/source/filters/filter-autoframing.cpp +++ b/source/filters/filter-autoframing.cpp @@ -345,7 +345,7 @@ void autoframing_instance::update(obs_data_t* data) { // Check if the user changed which Denoising provider we use. auto provider = static_cast(obs_data_get_int(data, ST_KEY_ADVANCED_PROVIDER)); if (provider == tracking_provider::AUTOMATIC) { - provider = autoframing_factory::get()->find_ideal_provider(); + provider = autoframing_factory::instance()->find_ideal_provider(); } // Check if the provider was changed, and if so switch. @@ -1255,26 +1255,27 @@ tracking_provider streamfx::filter::autoframing::autoframing_factory::find_ideal return tracking_provider::INVALID; } -std::shared_ptr _filter_autoframing_factory_instance = nullptr; - -void autoframing_factory::initialize() +std::shared_ptr autoframing_factory::instance() { - try { - if (!_filter_autoframing_factory_instance) - _filter_autoframing_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 autoframing_factory()); + winst = instance; } + return instance; } -void autoframing_factory::finalize() -{ - _filter_autoframing_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr autoframing_factory::get() -{ - return _filter_autoframing_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = autoframing_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-autoframing.hpp b/source/filters/filter-autoframing.hpp index 1dba7b6c..f5409d0f 100644 --- a/source/filters/filter-autoframing.hpp +++ b/source/filters/filter-autoframing.hpp @@ -171,6 +171,6 @@ namespace streamfx::filter::autoframing { public: // Singleton static void initialize(); static void finalize(); - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::filter::autoframing diff --git a/source/plugin.cpp b/source/plugin.cpp index af25aea1..c47b2692 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -16,9 +16,6 @@ #include "encoders/encoder-aom-av1.hpp" #endif -#ifdef ENABLE_FILTER_AUTOFRAMING -#include "filters/filter-autoframing.hpp" -#endif #ifdef ENABLE_FILTER_DISPLACEMENT #include "filters/filter-displacement.hpp" #endif @@ -122,9 +119,6 @@ MODULE_EXPORT bool obs_module_load(void) // Filters { -#ifdef ENABLE_FILTER_AUTOFRAMING - streamfx::filter::autoframing::autoframing_factory::initialize(); -#endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::initialize(); #endif @@ -148,9 +142,6 @@ MODULE_EXPORT void obs_module_unload(void) // Filters { -#ifdef ENABLE_FILTER_AUTOFRAMING - streamfx::filter::autoframing::autoframing_factory::finalize(); -#endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::finalize(); #endif