2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2019-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2019-01-27 21:43:49 +00:00
|
|
|
|
|
|
|
#include "obs-source-tracker.hpp"
|
2020-07-26 16:38:49 +00:00
|
|
|
#include "obs/obs-tools.hpp"
|
2022-05-10 20:52:35 +00:00
|
|
|
#include "util/util-logging.hpp"
|
|
|
|
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
|
|
|
#include <mutex>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "warning-enable.hpp"
|
|
|
|
|
2022-05-10 20:52:35 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ST_PREFIX "<%s> "
|
|
|
|
#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ST_PREFIX "<obs::source_tracker> "
|
|
|
|
#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__)
|
|
|
|
#endif
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
streamfx::obs::source_tracker::source_tracker() : _sources(), _mutex()
|
2022-05-10 20:52:35 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
auto osi = obs_get_signal_handler();
|
|
|
|
if (osi) {
|
|
|
|
signal_handler_connect(osi, "source_create", &source_create_handler, this);
|
|
|
|
signal_handler_connect(osi, "source_destroy", &source_destroy_handler, this);
|
|
|
|
signal_handler_connect(osi, "source_rename", &source_rename_handler, this);
|
|
|
|
} else {
|
|
|
|
D_LOG_WARNING("No global signal handler was present at initialization.", nullptr)
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2023-02-16 04:33:33 +00:00
|
|
|
|
|
|
|
// Enumerate all current sources and filters.
|
|
|
|
obs_enum_all_sources(
|
|
|
|
[](void* param, obs_source_t* source) {
|
|
|
|
auto* self = reinterpret_cast<::streamfx::obs::source_tracker*>(param);
|
|
|
|
self->insert_source(source);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
this);
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
streamfx::obs::source_tracker::~source_tracker()
|
2022-05-10 20:52:35 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
auto osi = obs_get_signal_handler();
|
|
|
|
if (osi) {
|
|
|
|
signal_handler_disconnect(osi, "source_create", &source_create_handler, this);
|
|
|
|
signal_handler_disconnect(osi, "source_destroy", &source_destroy_handler, this);
|
|
|
|
signal_handler_disconnect(osi, "source_rename", &source_rename_handler, this);
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2023-02-16 04:33:33 +00:00
|
|
|
|
|
|
|
this->_sources.clear();
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
void streamfx::obs::source_tracker::enumerate(enumerate_cb_t ecb, filter_cb_t fcb)
|
2022-05-10 20:52:35 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
// Need func-local copy, otherwise we risk corruption if a new source is created or destroyed.
|
|
|
|
decltype(_sources) _clone;
|
|
|
|
{
|
|
|
|
std::lock_guard<decltype(_mutex)> lock(_mutex);
|
|
|
|
_clone = _sources;
|
|
|
|
}
|
2022-05-10 20:52:35 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
for (auto kv : _clone) {
|
|
|
|
auto wsource = kv.second;
|
|
|
|
try {
|
|
|
|
auto source = wsource.lock();
|
|
|
|
|
|
|
|
if (fcb) {
|
|
|
|
if (fcb(kv.first, source)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 20:52:35 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
if (ecb) {
|
|
|
|
if (ecb(kv.first, source)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
continue;
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2022-05-10 20:52:35 +00:00
|
|
|
void streamfx::obs::source_tracker::insert_source(obs_source_t* source)
|
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
const char* name = obs_source_get_name(source);
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// Don't track unnamed sources.
|
|
|
|
if (!name || (strnlen(name, 1) == 0)) {
|
|
|
|
D_LOG_DEBUG("Unnamed source '0x%08zX' left untracked.", source);
|
2019-01-27 21:43:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// Insert the newly tracked source into the map.
|
|
|
|
std::lock_guard<decltype(_mutex)> lock(_mutex);
|
|
|
|
_sources.emplace(std::string{name}, ::streamfx::obs::weak_source{source});
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:52:35 +00:00
|
|
|
void streamfx::obs::source_tracker::remove_source(obs_source_t* source)
|
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
std::lock_guard<decltype(_mutex)> lock(_mutex);
|
|
|
|
const char* name = obs_source_get_name(source);
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// Try and find the source by name.
|
|
|
|
if (name) {
|
|
|
|
if (auto kv = _sources.find(std::string{name}); kv != _sources.end()) {
|
|
|
|
_sources.erase(kv);
|
2020-07-26 16:38:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-27 14:07:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// Try and find the source by pointer.
|
|
|
|
for (auto kv = _sources.begin(); kv != _sources.end(); kv++) {
|
|
|
|
if (kv->second == source) {
|
|
|
|
_sources.erase(kv);
|
2019-04-27 14:07:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2019-04-27 14:07:50 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// If we're still here, there's something wrong.
|
2022-05-10 20:52:35 +00:00
|
|
|
if (name) {
|
2023-02-16 04:33:33 +00:00
|
|
|
D_LOG_ERROR("Attempt to remove untracked source '0x%08zX' with name %s failed.", source, name);
|
2022-05-10 20:52:35 +00:00
|
|
|
throw std::runtime_error("Failed to find given source.");
|
2020-07-26 16:38:49 +00:00
|
|
|
}
|
2019-04-27 14:07:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
void streamfx::obs::source_tracker::rename_source(std::string_view old_name, std::string_view new_name, obs_source_t* source)
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2022-05-10 20:52:35 +00:00
|
|
|
if (old_name == new_name) {
|
|
|
|
throw std::runtime_error("New and old name are identical.");
|
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
std::lock_guard<decltype(_mutex)> lock(_mutex);
|
|
|
|
|
|
|
|
// Remove the previously tracked entry.
|
|
|
|
if (auto kv = _sources.find(std::string{old_name}); kv != _sources.end()) {
|
|
|
|
_sources.erase(kv);
|
2022-05-10 20:52:35 +00:00
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
// And then add the new entry.
|
|
|
|
_sources.emplace(std::string{new_name}, ::streamfx::obs::weak_source{source});
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
bool streamfx::obs::source_tracker::filter_sources(std::string, ::streamfx::obs::source source)
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
bool streamfx::obs::source_tracker::filter_audio_sources(std::string, ::streamfx::obs::source source)
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
uint32_t flags = obs_source_get_output_flags(source);
|
|
|
|
return !(flags & OBS_SOURCE_AUDIO) || (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
bool streamfx::obs::source_tracker::filter_video_sources(std::string, ::streamfx::obs::source source)
|
2019-04-19 07:42:15 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
uint32_t flags = obs_source_get_output_flags(source);
|
|
|
|
return !(flags & OBS_SOURCE_VIDEO) || (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
bool streamfx::obs::source_tracker::filter_transitions(std::string, ::streamfx::obs::source source)
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_TRANSITION);
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
bool streamfx::obs::source_tracker::filter_scenes(std::string, ::streamfx::obs::source source)
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE);
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
void streamfx::obs::source_tracker::source_create_handler(void* ptr, calldata_t* data) noexcept
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
auto* self = reinterpret_cast<streamfx::obs::source_tracker*>(ptr);
|
|
|
|
try {
|
|
|
|
obs_source_t* source = nullptr;
|
|
|
|
if (calldata_get_ptr(data, "source", &source); !source) {
|
|
|
|
throw std::runtime_error("Missing 'source' parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self->insert_source(source);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
DLOG_ERROR("Event 'source_create' caused exception: %s", ex.what());
|
|
|
|
} catch (...) {
|
|
|
|
DLOG_ERROR("Event 'source_create' caused unknown exception.", nullptr);
|
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
void streamfx::obs::source_tracker::source_destroy_handler(void* ptr, calldata_t* data) noexcept
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
auto* self = reinterpret_cast<streamfx::obs::source_tracker*>(ptr);
|
|
|
|
try {
|
|
|
|
obs_source_t* source = nullptr;
|
|
|
|
if (calldata_get_ptr(data, "source", &source); !source) {
|
|
|
|
throw std::runtime_error("Missing 'source' parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self->remove_source(source);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
DLOG_ERROR("Event 'source_destroy' caused exception: %s", ex.what());
|
|
|
|
} catch (...) {
|
|
|
|
DLOG_ERROR("Event 'source_destroy' caused unknown exception.", nullptr);
|
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 04:33:33 +00:00
|
|
|
void streamfx::obs::source_tracker::source_rename_handler(void* ptr, calldata_t* data) noexcept
|
2019-01-27 21:43:49 +00:00
|
|
|
{
|
2023-02-16 04:33:33 +00:00
|
|
|
auto* self = reinterpret_cast<streamfx::obs::source_tracker*>(ptr);
|
|
|
|
try {
|
|
|
|
obs_source_t* source = nullptr;
|
|
|
|
if (calldata_get_ptr(data, "source", &source); !source) {
|
|
|
|
throw std::runtime_error("Missing 'source' parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* old_name = nullptr;
|
|
|
|
if (calldata_get_string(data, "prev_name", &old_name); !old_name) {
|
|
|
|
throw std::runtime_error("Missing 'prev_name' parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* new_name = nullptr;
|
|
|
|
if (calldata_get_string(data, "new_name", &new_name); !new_name) {
|
|
|
|
throw std::runtime_error("Missing 'new_name' parameter.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self->rename_source(old_name, new_name, source);
|
|
|
|
} catch (...) {
|
|
|
|
DLOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
|
|
|
|
}
|
2019-01-27 21:43:49 +00:00
|
|
|
}
|
2022-05-10 20:52:35 +00:00
|
|
|
|
|
|
|
std::shared_ptr<streamfx::obs::source_tracker> streamfx::obs::source_tracker::get()
|
|
|
|
{
|
|
|
|
static std::mutex inst_mtx;
|
|
|
|
static std::weak_ptr<streamfx::obs::source_tracker> inst_weak;
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(inst_mtx);
|
|
|
|
if (inst_weak.expired()) {
|
|
|
|
auto instance = std::shared_ptr<streamfx::obs::source_tracker>(new streamfx::obs::source_tracker());
|
|
|
|
inst_weak = instance;
|
|
|
|
return instance;
|
|
|
|
} else {
|
|
|
|
return inst_weak.lock();
|
|
|
|
}
|
|
|
|
}
|