obs.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. /* Useful C++ classes and bindings for base obs data */
  15. #pragma once
  16. #include "obs.h"
  17. /* RAII wrappers */
  18. template<typename T, void addref(T), void release(T)> class OBSRef;
  19. using OBSSource = OBSRef<obs_source_t *, obs_source_addref, obs_source_release>;
  20. using OBSScene = OBSRef<obs_scene_t *, obs_scene_addref, obs_scene_release>;
  21. using OBSSceneItem =
  22. OBSRef<obs_sceneitem_t *, obs_sceneitem_addref, obs_sceneitem_release>;
  23. using OBSData = OBSRef<obs_data_t *, obs_data_addref, obs_data_release>;
  24. using OBSDataArray = OBSRef<obs_data_array_t *, obs_data_array_addref,
  25. obs_data_array_release>;
  26. using OBSOutput = OBSRef<obs_output_t *, obs_output_addref, obs_output_release>;
  27. using OBSEncoder =
  28. OBSRef<obs_encoder_t *, obs_encoder_addref, obs_encoder_release>;
  29. using OBSService =
  30. OBSRef<obs_service_t *, obs_service_addref, obs_service_release>;
  31. using OBSWeakSource = OBSRef<obs_weak_source_t *, obs_weak_source_addref,
  32. obs_weak_source_release>;
  33. using OBSWeakOutput = OBSRef<obs_weak_output_t *, obs_weak_output_addref,
  34. obs_weak_output_release>;
  35. using OBSWeakEncoder = OBSRef<obs_weak_encoder_t *, obs_weak_encoder_addref,
  36. obs_weak_encoder_release>;
  37. using OBSWeakService = OBSRef<obs_weak_service_t *, obs_weak_service_addref,
  38. obs_weak_service_release>;
  39. template<typename T, void addref(T), void release(T)> class OBSRef {
  40. T val;
  41. inline OBSRef &Replace(T valIn)
  42. {
  43. addref(valIn);
  44. release(val);
  45. val = valIn;
  46. return *this;
  47. }
  48. struct TakeOwnership {
  49. };
  50. inline OBSRef(T val, TakeOwnership) : val(val) {}
  51. public:
  52. inline OBSRef() : val(nullptr) {}
  53. inline OBSRef(T val_) : val(val_) { addref(val); }
  54. inline OBSRef(const OBSRef &ref) : val(ref.val) { addref(val); }
  55. inline OBSRef(OBSRef &&ref) : val(ref.val) { ref.val = nullptr; }
  56. inline ~OBSRef() { release(val); }
  57. inline OBSRef &operator=(T valIn) { return Replace(valIn); }
  58. inline OBSRef &operator=(const OBSRef &ref) { return Replace(ref.val); }
  59. inline OBSRef &operator=(OBSRef &&ref)
  60. {
  61. if (this != &ref) {
  62. release(val);
  63. val = ref.val;
  64. ref.val = nullptr;
  65. }
  66. return *this;
  67. }
  68. inline operator T() const { return val; }
  69. inline bool operator==(T p) const { return val == p; }
  70. inline bool operator!=(T p) const { return val != p; }
  71. friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
  72. friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
  73. friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
  74. friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
  75. friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
  76. friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
  77. friend OBSService OBSGetStrongRef(obs_weak_service_t *weak);
  78. friend OBSWeakService OBSGetWeakRef(obs_service_t *service);
  79. };
  80. inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
  81. {
  82. return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
  83. }
  84. inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
  85. {
  86. return {obs_source_get_weak_source(source),
  87. OBSWeakSource::TakeOwnership()};
  88. }
  89. inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
  90. {
  91. return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
  92. }
  93. inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
  94. {
  95. return {obs_output_get_weak_output(output),
  96. OBSWeakOutput::TakeOwnership()};
  97. }
  98. inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
  99. {
  100. return {obs_weak_encoder_get_encoder(weak),
  101. OBSEncoder::TakeOwnership()};
  102. }
  103. inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
  104. {
  105. return {obs_encoder_get_weak_encoder(encoder),
  106. OBSWeakEncoder::TakeOwnership()};
  107. }
  108. inline OBSService OBSGetStrongRef(obs_weak_service_t *weak)
  109. {
  110. return {obs_weak_service_get_service(weak),
  111. OBSService::TakeOwnership()};
  112. }
  113. inline OBSWeakService OBSGetWeakRef(obs_service_t *service)
  114. {
  115. return {obs_service_get_weak_service(service),
  116. OBSWeakService::TakeOwnership()};
  117. }
  118. /* objects that are not meant to be instanced */
  119. template<typename T, void destroy(T)> class OBSObj {
  120. T obj;
  121. public:
  122. inline OBSObj() : obj(nullptr) {}
  123. inline OBSObj(T obj_) : obj(obj_) {}
  124. inline OBSObj(const OBSObj &) = delete;
  125. inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
  126. inline ~OBSObj() { destroy(obj); }
  127. inline OBSObj &operator=(T obj_)
  128. {
  129. if (obj_ != obj)
  130. destroy(obj);
  131. obj = obj_;
  132. return *this;
  133. }
  134. inline OBSObj &operator=(const OBSObj &) = delete;
  135. inline OBSObj &operator=(OBSObj &&other)
  136. {
  137. if (obj)
  138. destroy(obj);
  139. obj = other.obj;
  140. other.obj = nullptr;
  141. return *this;
  142. }
  143. inline operator T() const { return obj; }
  144. inline bool operator==(T p) const { return obj == p; }
  145. inline bool operator!=(T p) const { return obj != p; }
  146. };
  147. using OBSDisplay = OBSObj<obs_display_t *, obs_display_destroy>;
  148. using OBSView = OBSObj<obs_view_t *, obs_view_destroy>;
  149. /* signal handler connection */
  150. class OBSSignal {
  151. signal_handler_t *handler;
  152. const char *signal;
  153. signal_callback_t callback;
  154. void *param;
  155. public:
  156. inline OBSSignal()
  157. : handler(nullptr),
  158. signal(nullptr),
  159. callback(nullptr),
  160. param(nullptr)
  161. {
  162. }
  163. inline OBSSignal(signal_handler_t *handler_, const char *signal_,
  164. signal_callback_t callback_, void *param_)
  165. : handler(handler_),
  166. signal(signal_),
  167. callback(callback_),
  168. param(param_)
  169. {
  170. signal_handler_connect_ref(handler, signal, callback, param);
  171. }
  172. inline void Disconnect()
  173. {
  174. signal_handler_disconnect(handler, signal, callback, param);
  175. handler = nullptr;
  176. signal = nullptr;
  177. callback = nullptr;
  178. param = nullptr;
  179. }
  180. inline ~OBSSignal() { Disconnect(); }
  181. inline void Connect(signal_handler_t *handler_, const char *signal_,
  182. signal_callback_t callback_, void *param_)
  183. {
  184. Disconnect();
  185. handler = handler_;
  186. signal = signal_;
  187. callback = callback_;
  188. param = param_;
  189. signal_handler_connect_ref(handler, signal, callback, param);
  190. }
  191. OBSSignal(const OBSSignal &) = delete;
  192. OBSSignal(OBSSignal &&other) noexcept
  193. : handler(other.handler),
  194. signal(other.signal),
  195. callback(other.callback),
  196. param(other.param)
  197. {
  198. other.handler = nullptr;
  199. other.signal = nullptr;
  200. other.callback = nullptr;
  201. other.param = nullptr;
  202. }
  203. OBSSignal &operator=(const OBSSignal &) = delete;
  204. OBSSignal &operator=(OBSSignal &&other) noexcept
  205. {
  206. Disconnect();
  207. handler = other.handler;
  208. signal = other.signal;
  209. callback = other.callback;
  210. param = other.param;
  211. other.handler = nullptr;
  212. other.signal = nullptr;
  213. other.callback = nullptr;
  214. other.param = nullptr;
  215. return *this;
  216. }
  217. };
  218. class OBSContext {
  219. public:
  220. inline OBSContext() {}
  221. inline OBSContext(const char *locale,
  222. const char *module_config_path = nullptr,
  223. profiler_name_store *store = nullptr)
  224. {
  225. obs_startup(locale, module_config_path, store);
  226. }
  227. inline ~OBSContext() { obs_shutdown(); }
  228. };