mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-10 22:05:06 +00:00
obs-source-tracker: Source Tracking Compatibility Layer
Allows for enumerating any kind of source, as long as it has a name and is not private. As obs_enum_scenes is a 23.x and above function, this class should be used instead to remain backwards compatible.
This commit is contained in:
parent
d56c0d6f25
commit
551cbb83b1
4 changed files with 250 additions and 0 deletions
|
@ -305,6 +305,8 @@ SET(PROJECT_PRIVATE
|
|||
"${PROJECT_SOURCE_DIR}/source/util-memory.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/obs-source.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/obs-source.cpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/obs-source-tracker.hpp"
|
||||
"${PROJECT_SOURCE_DIR}/source/obs-source-tracker.cpp"
|
||||
)
|
||||
|
||||
source_group("Data Files\\Locale" FILES ${PROJECT_DATA_LOCALE})
|
||||
|
|
164
source/obs-source-tracker.cpp
Normal file
164
source/obs-source-tracker.cpp
Normal file
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
* Modern effects for a modern Streamer
|
||||
* Copyright (C) 2017-2018 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#include "obs-source-tracker.hpp"
|
||||
|
||||
static std::shared_ptr<obs::source_tracker> source_tracker_instance;
|
||||
|
||||
void obs::source_tracker::source_create_handler(void* ptr, calldata_t* data)
|
||||
{
|
||||
obs::source_tracker* self = reinterpret_cast<obs::source_tracker*>(ptr);
|
||||
|
||||
obs_source_t* target = nullptr;
|
||||
calldata_get_ptr(data, "source", &target);
|
||||
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char* name = obs_source_get_name(target);
|
||||
if (!name) {
|
||||
// Do not track unnamed sources.
|
||||
return;
|
||||
}
|
||||
|
||||
obs_weak_source_t* weak = obs_source_get_weak_source(target);
|
||||
if (!weak) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
self->source_map.insert({std::string(name), weak});
|
||||
}
|
||||
|
||||
void obs::source_tracker::source_destroy_handler(void* ptr, calldata_t* data)
|
||||
{
|
||||
obs::source_tracker* self = reinterpret_cast<obs::source_tracker*>(ptr);
|
||||
|
||||
obs_source_t* target = nullptr;
|
||||
calldata_get_ptr(data, "source", &target);
|
||||
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char* name = obs_source_get_name(target);
|
||||
if (!name) {
|
||||
// Not tracking unnamed sources.
|
||||
return;
|
||||
}
|
||||
|
||||
auto found = self->source_map.find(std::string(name));
|
||||
if (found == self->source_map.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
obs_weak_source_release(found->second);
|
||||
self->source_map.erase(found);
|
||||
}
|
||||
|
||||
void obs::source_tracker::initialize()
|
||||
{
|
||||
source_tracker_instance = std::make_shared<obs::source_tracker>();
|
||||
}
|
||||
|
||||
void obs::source_tracker::finalize()
|
||||
{
|
||||
source_tracker_instance.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<obs::source_tracker> obs::source_tracker::get()
|
||||
{
|
||||
return source_tracker_instance;
|
||||
}
|
||||
|
||||
obs::source_tracker::source_tracker()
|
||||
{
|
||||
auto osi = obs_get_signal_handler();
|
||||
signal_handler_connect(osi, "source_create", &source_create_handler, this);
|
||||
signal_handler_connect(osi, "source_destroy", &source_destroy_handler, this);
|
||||
}
|
||||
|
||||
obs::source_tracker::~source_tracker()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
for (auto kv : this->source_map) {
|
||||
obs_weak_source_release(kv.second);
|
||||
}
|
||||
this->source_map.clear();
|
||||
}
|
||||
|
||||
void obs::source_tracker::enumerate(enumerate_cb_t ecb, filter_cb_t fcb) {
|
||||
// Need func-local copy, otherwise we risk corruption if a new source is created or destroyed.
|
||||
auto source_map_copy = this->source_map;
|
||||
for (auto kv : this->source_map) {
|
||||
obs_source_t* source = obs_weak_source_get_source(kv.second);
|
||||
if (!source) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fcb) {
|
||||
if (fcb(kv.first, source)) {
|
||||
obs_source_release(source);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (ecb) {
|
||||
if (ecb(kv.first, source)) {
|
||||
obs_source_release(source);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
obs_source_release(source);
|
||||
}
|
||||
}
|
||||
|
||||
bool obs::source_tracker::filter_sources(std::string name, obs_source_t* source)
|
||||
{
|
||||
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
||||
}
|
||||
|
||||
bool obs::source_tracker::filter_audio_sources(std::string name, obs_source_t* source)
|
||||
{
|
||||
uint32_t flags = obs_source_get_flags(source);
|
||||
return !(flags & OBS_SOURCE_AUDIO) || (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
||||
}
|
||||
|
||||
bool obs::source_tracker::filter_video_sources(std::string name, obs_source_t* source)
|
||||
{
|
||||
uint32_t flags = obs_source_get_flags(source);
|
||||
return !(flags & OBS_SOURCE_VIDEO) || (obs_source_get_type(source) != OBS_SOURCE_TYPE_INPUT);
|
||||
}
|
||||
|
||||
bool obs::source_tracker::filter_transitions(std::string name, obs_source_t* source)
|
||||
{
|
||||
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_TRANSITION);
|
||||
}
|
||||
|
||||
bool obs::source_tracker::filter_scenes(std::string name, obs_source_t* source)
|
||||
{
|
||||
return (obs_source_get_type(source) != OBS_SOURCE_TYPE_SCENE);
|
||||
}
|
81
source/obs-source-tracker.hpp
Normal file
81
source/obs-source-tracker.hpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Modern effects for a modern Streamer
|
||||
* Copyright (C) 2017-2018 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
// OBS
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4201)
|
||||
#endif
|
||||
#include <obs.h>
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
namespace obs {
|
||||
class source_tracker {
|
||||
friend class std::_Ref_count_obj<obs::source_tracker>;
|
||||
|
||||
std::map<std::string, obs_weak_source_t*> source_map;
|
||||
|
||||
static void source_create_handler(void* ptr, calldata_t* data);
|
||||
static void source_destroy_handler(void* ptr, calldata_t* data);
|
||||
|
||||
public: // Singleton
|
||||
static void initialize();
|
||||
static void finalize();
|
||||
static std::shared_ptr<obs::source_tracker> get();
|
||||
|
||||
private:
|
||||
source_tracker();
|
||||
~source_tracker();
|
||||
|
||||
public:
|
||||
// Callback function for enumerating sources.
|
||||
//
|
||||
// @param std::string Name of the Source
|
||||
// @param obs_source_t* Source
|
||||
// @return true to abort enumeration, false to keep going.
|
||||
typedef std::function<bool(std::string, obs_source_t*)> enumerate_cb_t;
|
||||
|
||||
// Filter function for enumerating sources.
|
||||
//
|
||||
// @param std::string Name of the Source
|
||||
// @param obs_source_t* Source
|
||||
// @return true to skip, false to pass along.
|
||||
typedef std::function<bool(std::string, obs_source_t*)> filter_cb_t;
|
||||
|
||||
//! Enumerate all tracked sources
|
||||
//
|
||||
// @param enumerate_cb The function called for each tracked source.
|
||||
// @param filter_cb Filter function to narrow down results.
|
||||
void enumerate(enumerate_cb_t enumerate_cb, filter_cb_t filter_cb = nullptr);
|
||||
|
||||
public:
|
||||
static bool filter_sources(std::string name, obs_source_t* source);
|
||||
static bool filter_audio_sources(std::string name, obs_source_t* source);
|
||||
static bool filter_video_sources(std::string name, obs_source_t* source);
|
||||
static bool filter_transitions(std::string name, obs_source_t* source);
|
||||
static bool filter_scenes(std::string name, obs_source_t* source);
|
||||
};
|
||||
} // namespace obs
|
|
@ -22,6 +22,7 @@
|
|||
#include "filter-displacement.hpp"
|
||||
#include "filter-shape.hpp"
|
||||
#include "filter-transform.hpp"
|
||||
#include "obs-source-tracker.hpp"
|
||||
|
||||
std::list<std::function<void()>> initializerFunctions;
|
||||
std::list<std::function<void()>> finalizerFunctions;
|
||||
|
@ -30,6 +31,7 @@ MODULE_EXPORT bool obs_module_load(void)
|
|||
{
|
||||
P_LOG_INFO("Loading Version %u.%u.%u (Build %u)", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR,
|
||||
PROJECT_VERSION_PATCH, PROJECT_VERSION_TWEAK);
|
||||
obs::source_tracker::initialize();
|
||||
for (auto func : initializerFunctions) {
|
||||
func();
|
||||
}
|
||||
|
@ -43,6 +45,7 @@ MODULE_EXPORT void obs_module_unload(void)
|
|||
for (auto func : finalizerFunctions) {
|
||||
func();
|
||||
}
|
||||
obs::source_tracker::finalize();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
|
Loading…
Reference in a new issue