2018-11-13 19:04:13 +01:00
|
|
|
// FFMPEG Video Encoder Integration for OBS Studio
|
2019-07-22 13:53:25 +02:00
|
|
|
// Copyright (c) 2019 Michael Fabian Dirks <info@xaymar.com>
|
2018-11-13 19:04:13 +01:00
|
|
|
//
|
2019-07-22 13:53:25 +02:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2018-11-13 19:04:13 +01:00
|
|
|
//
|
2019-07-22 13:53:25 +02:00
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
2018-11-13 19:04:13 +01:00
|
|
|
//
|
2019-07-22 13:53:25 +02:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
2018-11-13 19:04:13 +01:00
|
|
|
|
|
|
|
|
#include "plugin.hpp"
|
2019-08-03 09:45:56 +02:00
|
|
|
#include <map>
|
2019-07-06 13:08:23 +02:00
|
|
|
#include <memory>
|
2019-07-24 19:51:05 +02:00
|
|
|
#include "encoder.hpp"
|
2019-07-07 16:34:15 +02:00
|
|
|
#include "ui/debug_handler.hpp"
|
|
|
|
|
#include "ui/handler.hpp"
|
|
|
|
|
#include "utility.hpp"
|
2018-11-13 19:04:13 +01:00
|
|
|
|
|
|
|
|
extern "C" {
|
2019-07-07 16:34:15 +02:00
|
|
|
#include <obs-module.h>
|
|
|
|
|
#include <obs.h>
|
2018-11-13 19:04:13 +01:00
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4244)
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 13:29:06 +02:00
|
|
|
// Initializers and finalizers.
|
|
|
|
|
std::list<std::function<void()>> obsffmpeg::initializers;
|
|
|
|
|
|
|
|
|
|
std::list<std::function<void()>> obsffmpeg::finalizers;
|
|
|
|
|
|
2019-07-07 13:42:39 +02:00
|
|
|
// Codec to Handler mapping.
|
|
|
|
|
static std::map<std::string, std::shared_ptr<obsffmpeg::ui::handler>> codec_to_handler_map;
|
2019-07-07 16:34:15 +02:00
|
|
|
static std::shared_ptr<obsffmpeg::ui::handler> debug_handler = std::make_shared<obsffmpeg::ui::debug_handler>();
|
2019-07-07 13:42:39 +02:00
|
|
|
|
2019-07-24 14:19:00 +02:00
|
|
|
void obsffmpeg::register_codec_handler(std::string const codec, std::shared_ptr<obsffmpeg::ui::handler> const handler)
|
2019-07-07 13:42:39 +02:00
|
|
|
{
|
|
|
|
|
codec_to_handler_map.emplace(codec, handler);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 14:19:00 +02:00
|
|
|
std::shared_ptr<obsffmpeg::ui::handler> obsffmpeg::find_codec_handler(std::string const codec)
|
2019-07-07 13:42:39 +02:00
|
|
|
{
|
|
|
|
|
auto found = codec_to_handler_map.find(codec);
|
|
|
|
|
if (found == codec_to_handler_map.end())
|
2019-07-07 16:34:15 +02:00
|
|
|
return debug_handler;
|
2019-07-07 13:42:39 +02:00
|
|
|
return found->second;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 14:19:00 +02:00
|
|
|
bool obsffmpeg::has_codec_handler(std::string const codec)
|
2019-07-21 11:45:47 +02:00
|
|
|
{
|
|
|
|
|
auto found = codec_to_handler_map.find(codec);
|
|
|
|
|
return (found != codec_to_handler_map.end());
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 09:45:56 +02:00
|
|
|
static std::map<const AVCodec*, std::shared_ptr<obsffmpeg::encoder_factory>> generic_factories;
|
2019-07-06 13:08:23 +02:00
|
|
|
|
2019-08-03 09:45:56 +02:00
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4996) // Handled by software and precompiler branch
|
2018-11-13 19:04:13 +01:00
|
|
|
MODULE_EXPORT bool obs_module_load(void)
|
2019-07-07 13:29:06 +02:00
|
|
|
try {
|
2019-08-03 09:45:56 +02:00
|
|
|
#if FF_API_NEXT
|
|
|
|
|
if (avcodec_version() < AV_VERSION_INT(58, 0, 0)) {
|
|
|
|
|
// Initialize avcodec.
|
|
|
|
|
avcodec_register_all();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-07-06 13:08:23 +02:00
|
|
|
|
2019-07-07 13:29:06 +02:00
|
|
|
// Run all initializers.
|
2019-07-24 14:19:00 +02:00
|
|
|
for (auto const func : obsffmpeg::initializers) {
|
2019-07-07 13:29:06 +02:00
|
|
|
func();
|
|
|
|
|
}
|
2019-07-06 13:08:23 +02:00
|
|
|
|
2019-07-07 13:29:06 +02:00
|
|
|
// Register all codecs.
|
2019-08-03 09:45:56 +02:00
|
|
|
#if FF_API_NEXT
|
|
|
|
|
if (avcodec_version() < AV_VERSION_INT(58, 0, 0)) {
|
|
|
|
|
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<obsffmpeg::encoder_factory>(cdc);
|
|
|
|
|
ptr->register_encoder();
|
|
|
|
|
generic_factories.emplace(cdc, ptr);
|
|
|
|
|
}
|
2019-07-07 13:29:06 +02:00
|
|
|
}
|
2019-08-03 09:45:56 +02:00
|
|
|
} else {
|
|
|
|
|
#endif
|
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR >= 58
|
|
|
|
|
void* storage = nullptr;
|
|
|
|
|
const AVCodec* cdc = nullptr;
|
|
|
|
|
for (cdc = av_codec_iterate(&storage); cdc != nullptr; cdc = av_codec_iterate(&storage)) {
|
|
|
|
|
if (!av_codec_is_encoder(cdc))
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-01-02 12:31:38 +01:00
|
|
|
if (/*(cdc->type == AVMediaType::AVMEDIA_TYPE_AUDIO)
|
|
|
|
|
|| */(cdc->type == AVMediaType::AVMEDIA_TYPE_VIDEO)) {
|
2019-08-03 09:45:56 +02:00
|
|
|
auto ptr = std::make_shared<obsffmpeg::encoder_factory>(cdc);
|
|
|
|
|
ptr->register_encoder();
|
|
|
|
|
generic_factories.emplace(cdc, ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if FF_API_NEXT
|
2018-11-13 19:04:13 +01:00
|
|
|
}
|
2019-08-03 09:45:56 +02:00
|
|
|
#endif
|
2019-07-07 13:29:06 +02:00
|
|
|
|
|
|
|
|
return true;
|
2019-07-24 19:51:05 +02:00
|
|
|
} catch (std::exception& ex) {
|
2019-07-07 13:29:06 +02:00
|
|
|
PLOG_ERROR("Exception during initalization: %s.", ex.what());
|
|
|
|
|
return false;
|
|
|
|
|
} catch (...) {
|
|
|
|
|
PLOG_ERROR("Unrecognized exception during initalization.");
|
2018-11-13 19:04:13 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-03 09:45:56 +02:00
|
|
|
#pragma warning(pop)
|
2018-11-13 19:04:13 +01:00
|
|
|
|
|
|
|
|
MODULE_EXPORT void obs_module_unload(void)
|
2019-07-07 13:29:06 +02:00
|
|
|
try {
|
|
|
|
|
// Run all finalizers.
|
2019-07-24 14:19:00 +02:00
|
|
|
for (auto const func : obsffmpeg::finalizers) {
|
2019-07-07 13:29:06 +02:00
|
|
|
func();
|
2018-11-13 19:04:13 +01:00
|
|
|
}
|
2019-07-24 19:51:05 +02:00
|
|
|
} catch (std::exception& ex) {
|
2019-07-07 13:29:06 +02:00
|
|
|
PLOG_ERROR("Exception during finalizing: %s.", ex.what());
|
|
|
|
|
} catch (...) {
|
|
|
|
|
PLOG_ERROR("Unrecognized exception during finalizing.");
|
2018-11-13 19:04:13 +01:00
|
|
|
}
|