From 96ac0bd11b1c18a7ad13ce40d0241962a3501f32 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Tue, 14 Jan 2020 10:36:04 +0100 Subject: [PATCH] ffmpeg-encoder: Fix weird bug with std::map crashing in constructor --- source/encoders/ffmpeg-encoder.cpp | 35 ++++++++++++++++-------------- source/encoders/ffmpeg-encoder.hpp | 3 +++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/source/encoders/ffmpeg-encoder.cpp b/source/encoders/ffmpeg-encoder.cpp index e5aabc26..c7d06be2 100644 --- a/source/encoders/ffmpeg-encoder.cpp +++ b/source/encoders/ffmpeg-encoder.cpp @@ -70,22 +70,6 @@ ffmpeg_manager::ffmpeg_manager() : _factories(), _handlers(), _debug_handler() register_handler("prores_aw", ::std::make_shared()); register_handler("h264_nvenc", ::std::make_shared()); register_handler("hevc_nvenc", ::std::make_shared()); - - // Encoders - void* iterator = nullptr; - const AVCodec* codec = nullptr; - for (codec = av_codec_iterate(&iterator); codec != nullptr; codec = av_codec_iterate(&iterator)) { - if (!av_codec_is_encoder(codec)) - continue; - if ((codec->type == AVMediaType::AVMEDIA_TYPE_AUDIO) || (codec->type == AVMediaType::AVMEDIA_TYPE_VIDEO)) { - try { - auto factory = std::make_shared(codec); - factory->register_encoder(); - _factories.emplace(codec, factory); - } catch (...) { - } - } - } } ffmpeg_manager::~ffmpeg_manager() @@ -115,6 +99,25 @@ bool ffmpeg_manager::has_handler(std::string codec) return (_handlers.find(codec) != _handlers.end()); } +void encoder::ffmpeg::ffmpeg_manager::register_encoders() +{ + // Encoders + void* iterator = nullptr; + const AVCodec* codec = nullptr; + for (codec = av_codec_iterate(&iterator); codec != nullptr; codec = av_codec_iterate(&iterator)) { + if (!av_codec_is_encoder(codec)) + continue; + if ((codec->type == AVMediaType::AVMEDIA_TYPE_AUDIO) || (codec->type == AVMediaType::AVMEDIA_TYPE_VIDEO)) { + try { + auto factory = std::make_shared(codec); + factory->register_encoder(); + _factories.emplace(codec, factory); + } catch (...) { + } + } + } +} + static void* _create(obs_data_t* settings, obs_encoder_t* encoder) noexcept try { return reinterpret_cast(new ffmpeg_instance(settings, encoder)); diff --git a/source/encoders/ffmpeg-encoder.hpp b/source/encoders/ffmpeg-encoder.hpp index be0cb159..13d979e4 100644 --- a/source/encoders/ffmpeg-encoder.hpp +++ b/source/encoders/ffmpeg-encoder.hpp @@ -78,6 +78,7 @@ namespace encoder::ffmpeg { static void initialize() { _instance = ::std::make_shared(); + _instance->register_encoders(); } static void finalize() @@ -104,6 +105,8 @@ namespace encoder::ffmpeg { std::shared_ptr get_handler(std::string codec); bool has_handler(std::string codec); + + void register_encoders(); }; struct ffmpeg_info {