diff --git a/source/plugin.cpp b/source/plugin.cpp index e5c3d66..8d69ea1 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -31,44 +31,56 @@ extern "C" { #pragma warning(pop) } +// Initializers and finalizers. +std::list> obsffmpeg::initializers; + +std::list> obsffmpeg::finalizers; + static std::map> generic_factories; MODULE_EXPORT bool obs_module_load(void) -{ - try { - avcodec_register_all(); +try { + // Initialize avcodec. + avcodec_register_all(); - // Register all codecs. - AVCodec* cdc = nullptr; - while ((cdc = av_codec_next(cdc)) != nullptr) { - if (!av_codec_is_encoder(cdc)) - continue; - - if ((cdc->type == AVMediaType::AVMEDIA_TYPE_AUDIO) - || (cdc->type == AVMediaType::AVMEDIA_TYPE_VIDEO)) { - auto ptr = std::make_shared(cdc); - ptr->register_encoder(); - generic_factories.emplace(cdc, ptr); - } - } - - obsffmpeg::encoder::prores_aw::initialize(); - return true; - } catch (std::exception ex) { - PLOG_ERROR("Exception during initalization: %s.", ex.what()); - } catch (...) { - PLOG_ERROR("Unrecognized exception during initalization."); + // Run all initializers. + for (auto func : obsffmpeg::initializers) { + func(); } + + // Register all codecs. + AVCodec* cdc = nullptr; + while ((cdc = av_codec_next(cdc)) != nullptr) { + if (!av_codec_is_encoder(cdc)) + continue; + + if ((cdc->type == AVMediaType::AVMEDIA_TYPE_AUDIO) || (cdc->type == AVMediaType::AVMEDIA_TYPE_VIDEO)) { + auto ptr = std::make_shared(cdc); + ptr->register_encoder(); + generic_factories.emplace(cdc, ptr); + } + } + + obsffmpeg::encoder::prores_aw::initialize(); + return true; +} catch (std::exception ex) { + PLOG_ERROR("Exception during initalization: %s.", ex.what()); + return false; +} catch (...) { + PLOG_ERROR("Unrecognized exception during initalization."); return false; } MODULE_EXPORT void obs_module_unload(void) -{ - try { - obsffmpeg::encoder::prores_aw::finalize(); - } catch (std::exception ex) { - PLOG_ERROR("Exception during finalizing: %s.", ex.what()); - } catch (...) { - PLOG_ERROR("Unrecognized exception during finalizing."); +try { + obsffmpeg::encoder::prores_aw::finalize(); + + // Run all finalizers. + for (auto func : obsffmpeg::finalizers) { + func(); } +} catch (std::exception ex) { + PLOG_ERROR("Exception during finalizing: %s.", ex.what()); +} catch (...) { + PLOG_ERROR("Unrecognized exception during finalizing."); } diff --git a/source/plugin.hpp b/source/plugin.hpp index d3107bb..8599374 100644 --- a/source/plugin.hpp +++ b/source/plugin.hpp @@ -1,5 +1,5 @@ // FFMPEG Video Encoder Integration for OBS Studio -// Copyright (C) 2018 - 2018 Michael Fabian Dirks +// Copyright (C) 2018 - 2019 Michael Fabian Dirks // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -15,16 +15,18 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -#ifndef OBS_FFMPEG_PLUGIN_HPP -#define OBS_FFMPEG_PLUGIN_HPP #pragma once - #include #include +#include +#include namespace obsffmpeg { + extern std::list> initializers; + extern std::list> finalizers; } // namespace obsffmpeg -#endif OBS_FFMPEG_PLUGIN_HPP +MODULE_EXPORT bool obs_module_load(void); +MODULE_EXPORT void obs_module_unload(void);