gfx-source-texture: Support for new obs-source managed class

This also adds support for proper source management in unmanaged cases.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2018-11-07 14:09:03 +01:00
parent 58661bf3ac
commit f54efe7704
2 changed files with 71 additions and 28 deletions

View File

@ -19,61 +19,95 @@
gfx::source_texture::~source_texture() gfx::source_texture::~source_texture()
{ {
if (child) { if (child && parent) {
if (parent) { obs_source_remove_active_child(parent->get(), child->get());
obs_source_remove_active_child(parent, child);
}
obs_source_release(child);
} }
parent.reset();
child.reset();
} }
gfx::source_texture::source_texture(obs_source_t* _parent) gfx::source_texture::source_texture(obs_source_t* _parent)
{ {
if (!_parent) {
throw std::invalid_argument("parent must not be null");
}
parent = std::make_unique<obs::source>(_parent, false, false);
render_target = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE); render_target = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
parent = _parent; }
gfx::source_texture::source_texture(obs_source_t* _source, obs_source_t* _parent) : source_texture(_parent)
{
if (!_source) {
throw std::invalid_argument("source must not be null");
}
if (!obs_source_add_active_child(_parent, _source)) {
throw std::runtime_error("parent is contained in child");
}
child = std::make_unique<obs::source>(_source, true, true);
} }
gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent) gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent)
{ {
child = obs_get_source_by_name(_name); if (!_name) {
if (!child) { throw std::invalid_argument("name must not be null");
throw std::invalid_argument("No such source.");
} }
if (!obs_source_add_active_child(parent, child)) { obs_source_t* source = obs_get_source_by_name(_name);
throw std::runtime_error("Recursion is not allowed."); if (!source) {
throw std::invalid_argument("source does not exist");
} }
if (!obs_source_add_active_child(_parent, source)) {
throw std::runtime_error("parent is contained in child");
}
child = std::make_unique<obs::source>(source, true, true);
} }
gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) : source_texture(_name.c_str(), _parent) gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) : source_texture(_name.c_str(), _parent)
{} {}
gfx::source_texture::source_texture(obs_source_t* _source, obs_source_t* _parent) : source_texture(_parent) gfx::source_texture::source_texture(std::shared_ptr<obs::source> child, std::shared_ptr<obs::source> parent)
{ {
child = _source;
if (!child) { if (!child) {
throw std::invalid_argument("No such source."); throw std::invalid_argument("child must not be null");
} }
if (!obs_source_add_active_child(parent, child)) { if (!parent) {
throw std::runtime_error("Recursion is not allowed."); throw std::invalid_argument("parent must not be null");
} }
obs_source_addref(child); if (!obs_source_add_active_child(parent->get(), child->get())) {
throw std::runtime_error("parent is contained in child");
}
this->child = child;
this->parent = parent;
} }
gfx::source_texture::source_texture(std::shared_ptr<obs::source> child, obs_source_t* _parent)
: source_texture(child, std::make_shared<obs::source>(_parent, false, false))
{}
obs_source_t* gfx::source_texture::get_object() obs_source_t* gfx::source_texture::get_object()
{ {
return child; if (child) {
return child->get();
}
return nullptr;
} }
obs_source_t* gfx::source_texture::get_parent() obs_source_t* gfx::source_texture::get_parent()
{ {
return parent; return parent->get();
}
void gfx::source_texture::clear()
{
if (child && parent) {
obs_source_remove_active_child(parent->get(), child->get());
}
child->clear();
child.reset();
} }
std::shared_ptr<gs::texture> gfx::source_texture::render(size_t width, size_t height) std::shared_ptr<gs::texture> gfx::source_texture::render(size_t width, size_t height)
{ {
if (!child) {
throw std::invalid_argument("Missing source to render.");
}
if ((width == 0) || (width >= 16384)) { if ((width == 0) || (width >= 16384)) {
throw std::runtime_error("Width too large or too small."); throw std::runtime_error("Width too large or too small.");
} }
@ -87,7 +121,9 @@ std::shared_ptr<gs::texture> gfx::source_texture::render(size_t width, size_t he
vec4_zero(&black); vec4_zero(&black);
gs_ortho(0, (float_t)width, 0, (float_t)height, 0, 1); gs_ortho(0, (float_t)width, 0, (float_t)height, 0, 1);
gs_clear(GS_CLEAR_COLOR, &black, 0, 0); gs_clear(GS_CLEAR_COLOR, &black, 0, 0);
obs_source_video_render(child); if (child) {
obs_source_video_render(child->get());
}
} }
std::shared_ptr<gs::texture> tex; std::shared_ptr<gs::texture> tex;

View File

@ -20,6 +20,7 @@
#include <string> #include <string>
#include "gs-rendertarget.h" #include "gs-rendertarget.h"
#include "gs-texture.h" #include "gs-texture.h"
#include "obs-source.hpp"
extern "C" { extern "C" {
#include <obs.h> #include <obs.h>
@ -27,8 +28,8 @@ extern "C" {
namespace gfx { namespace gfx {
class source_texture { class source_texture {
obs_source_t* parent; std::shared_ptr<obs::source> parent;
obs_source_t* child; std::shared_ptr<obs::source> child;
std::shared_ptr<gs::rendertarget> render_target; std::shared_ptr<gs::rendertarget> render_target;
@ -36,13 +37,19 @@ namespace gfx {
public: public:
~source_texture(); ~source_texture();
source_texture(obs_source_t* src, obs_source_t* parent);
source_texture(const char* name, obs_source_t* parent); source_texture(const char* name, obs_source_t* parent);
source_texture(std::string name, obs_source_t* parent); source_texture(std::string name, obs_source_t* parent);
source_texture(obs_source_t* src, obs_source_t* parent);
source_texture(std::shared_ptr<obs::source> child, std::shared_ptr<obs::source> parent);
source_texture(std::shared_ptr<obs::source> child, obs_source_t* parent);
std::shared_ptr<gs::texture> render(size_t width, size_t height);
public: // Unsafe Methods
void clear();
obs_source_t* get_object(); obs_source_t* get_object();
obs_source_t* get_parent(); obs_source_t* get_parent();
std::shared_ptr<gs::texture> render(size_t width, size_t height);
}; };
} // namespace gfx } // namespace gfx