yolobs-studio/libobs/obs.hpp

270 lines
7.3 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
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, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/* Useful C++ classes and bindings for base obs data */
#pragma once
#include "obs.h"
/* RAII wrappers */
2019-09-22 21:19:10 +00:00
template<typename T, void addref(T), void release(T)> class OBSRef;
using OBSSource = OBSRef<obs_source_t *, obs_source_addref, obs_source_release>;
using OBSScene = OBSRef<obs_scene_t *, obs_scene_addref, obs_scene_release>;
using OBSSceneItem =
OBSRef<obs_sceneitem_t *, obs_sceneitem_addref, obs_sceneitem_release>;
using OBSData = OBSRef<obs_data_t *, obs_data_addref, obs_data_release>;
using OBSDataArray = OBSRef<obs_data_array_t *, obs_data_array_addref,
obs_data_array_release>;
using OBSOutput = OBSRef<obs_output_t *, obs_output_addref, obs_output_release>;
using OBSEncoder =
OBSRef<obs_encoder_t *, obs_encoder_addref, obs_encoder_release>;
using OBSService =
OBSRef<obs_service_t *, obs_service_addref, obs_service_release>;
using OBSWeakSource = OBSRef<obs_weak_source_t *, obs_weak_source_addref,
obs_weak_source_release>;
using OBSWeakOutput = OBSRef<obs_weak_output_t *, obs_weak_output_addref,
obs_weak_output_release>;
using OBSWeakEncoder = OBSRef<obs_weak_encoder_t *, obs_weak_encoder_addref,
obs_weak_encoder_release>;
using OBSWeakService = OBSRef<obs_weak_service_t *, obs_weak_service_addref,
obs_weak_service_release>;
template<typename T, void addref(T), void release(T)> class OBSRef {
2016-02-23 23:16:51 +00:00
T val;
inline OBSRef &Replace(T valIn)
{
addref(valIn);
release(val);
val = valIn;
return *this;
}
2019-09-22 21:19:10 +00:00
struct TakeOwnership {
};
inline OBSRef(T val, TakeOwnership) : val(val) {}
2016-02-23 23:16:51 +00:00
public:
2019-09-22 21:19:10 +00:00
inline OBSRef() : val(nullptr) {}
inline OBSRef(T val_) : val(val_) { addref(val); }
inline OBSRef(const OBSRef &ref) : val(ref.val) { addref(val); }
inline OBSRef(OBSRef &&ref) : val(ref.val) { ref.val = nullptr; }
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
inline ~OBSRef() { release(val); }
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
inline OBSRef &operator=(T valIn) { return Replace(valIn); }
inline OBSRef &operator=(const OBSRef &ref) { return Replace(ref.val); }
2016-02-23 23:16:51 +00:00
inline OBSRef &operator=(OBSRef &&ref)
{
if (this != &ref) {
release(val);
val = ref.val;
ref.val = nullptr;
}
return *this;
}
2019-09-22 21:19:10 +00:00
inline operator T() const { return val; }
2020-10-01 20:15:25 +00:00
inline T Get() const { return val; }
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
inline bool operator==(T p) const { return val == p; }
inline bool operator!=(T p) const { return val != p; }
2016-02-23 23:16:51 +00:00
friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
friend OBSService OBSGetStrongRef(obs_weak_service_t *weak);
friend OBSWeakService OBSGetWeakRef(obs_service_t *service);
};
inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
{
return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
}
inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
{
return {obs_source_get_weak_source(source),
OBSWeakSource::TakeOwnership()};
}
inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
{
return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
}
inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
{
return {obs_output_get_weak_output(output),
OBSWeakOutput::TakeOwnership()};
}
inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
{
return {obs_weak_encoder_get_encoder(weak),
OBSEncoder::TakeOwnership()};
}
inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
{
return {obs_encoder_get_weak_encoder(encoder),
OBSWeakEncoder::TakeOwnership()};
}
inline OBSService OBSGetStrongRef(obs_weak_service_t *weak)
{
return {obs_weak_service_get_service(weak),
OBSService::TakeOwnership()};
}
inline OBSWeakService OBSGetWeakRef(obs_service_t *service)
{
return {obs_service_get_weak_service(service),
OBSWeakService::TakeOwnership()};
}
/* objects that are not meant to be instanced */
template<typename T, void destroy(T)> class OBSObj {
T obj;
public:
2019-09-22 21:19:10 +00:00
inline OBSObj() : obj(nullptr) {}
2016-02-23 23:16:51 +00:00
inline OBSObj(T obj_) : obj(obj_) {}
2019-09-22 21:19:10 +00:00
inline OBSObj(const OBSObj &) = delete;
2016-02-23 23:16:51 +00:00
inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
2019-09-22 21:19:10 +00:00
inline ~OBSObj() { destroy(obj); }
2016-02-23 23:16:51 +00:00
inline OBSObj &operator=(T obj_)
{
if (obj_ != obj)
destroy(obj);
obj = obj_;
return *this;
}
2019-09-22 21:19:10 +00:00
inline OBSObj &operator=(const OBSObj &) = delete;
2016-02-23 23:16:51 +00:00
inline OBSObj &operator=(OBSObj &&other)
{
if (obj)
destroy(obj);
2019-09-22 21:19:10 +00:00
obj = other.obj;
2016-02-23 23:16:51 +00:00
other.obj = nullptr;
return *this;
}
2019-09-22 21:19:10 +00:00
inline operator T() const { return obj; }
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
inline bool operator==(T p) const { return obj == p; }
inline bool operator!=(T p) const { return obj != p; }
2016-02-23 23:16:51 +00:00
};
2019-09-22 21:19:10 +00:00
using OBSDisplay = OBSObj<obs_display_t *, obs_display_destroy>;
using OBSView = OBSObj<obs_view_t *, obs_view_destroy>;
2016-02-23 23:16:51 +00:00
/* signal handler connection */
class OBSSignal {
2019-09-22 21:19:10 +00:00
signal_handler_t *handler;
const char *signal;
2016-02-23 23:16:51 +00:00
signal_callback_t callback;
2019-09-22 21:19:10 +00:00
void *param;
2016-02-23 23:16:51 +00:00
public:
inline OBSSignal()
2019-09-22 21:19:10 +00:00
: handler(nullptr),
signal(nullptr),
callback(nullptr),
param(nullptr)
{
}
inline OBSSignal(signal_handler_t *handler_, const char *signal_,
signal_callback_t callback_, void *param_)
: handler(handler_),
signal(signal_),
callback(callback_),
param(param_)
2016-02-23 23:16:51 +00:00
{
2018-12-16 16:14:58 +00:00
signal_handler_connect_ref(handler, signal, callback, param);
2016-02-23 23:16:51 +00:00
}
inline void Disconnect()
{
signal_handler_disconnect(handler, signal, callback, param);
2019-09-22 21:19:10 +00:00
handler = nullptr;
signal = nullptr;
2016-02-23 23:16:51 +00:00
callback = nullptr;
2019-09-22 21:19:10 +00:00
param = nullptr;
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
inline ~OBSSignal() { Disconnect(); }
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
inline void Connect(signal_handler_t *handler_, const char *signal_,
signal_callback_t callback_, void *param_)
2016-02-23 23:16:51 +00:00
{
Disconnect();
2019-09-22 21:19:10 +00:00
handler = handler_;
signal = signal_;
2016-02-23 23:16:51 +00:00
callback = callback_;
2019-09-22 21:19:10 +00:00
param = param_;
2018-12-16 16:14:58 +00:00
signal_handler_connect_ref(handler, signal, callback, param);
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
OBSSignal(const OBSSignal &) = delete;
2020-03-25 08:07:22 +00:00
OBSSignal(OBSSignal &&other) noexcept
2019-09-22 21:19:10 +00:00
: handler(other.handler),
signal(other.signal),
2016-02-23 23:16:51 +00:00
callback(other.callback),
2019-09-22 21:19:10 +00:00
param(other.param)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
other.handler = nullptr;
other.signal = nullptr;
2016-02-23 23:16:51 +00:00
other.callback = nullptr;
2019-09-22 21:19:10 +00:00
other.param = nullptr;
2016-02-23 23:16:51 +00:00
}
OBSSignal &operator=(const OBSSignal &) = delete;
2020-03-25 08:07:22 +00:00
OBSSignal &operator=(OBSSignal &&other) noexcept
2016-02-23 23:16:51 +00:00
{
Disconnect();
2019-09-22 21:19:10 +00:00
handler = other.handler;
signal = other.signal;
2016-02-23 23:16:51 +00:00
callback = other.callback;
2019-09-22 21:19:10 +00:00
param = other.param;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
other.handler = nullptr;
other.signal = nullptr;
2016-02-23 23:16:51 +00:00
other.callback = nullptr;
2019-09-22 21:19:10 +00:00
other.param = nullptr;
2016-02-23 23:16:51 +00:00
return *this;
}
};