obs.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 release(T)> class OBSRefAutoRelease;
  19. template<typename T, void addref(T), void release(T)> class OBSRef;
  20. using OBSSource = OBSRef<obs_source_t *, obs_source_addref, obs_source_release>;
  21. using OBSScene = OBSRef<obs_scene_t *, obs_scene_addref, obs_scene_release>;
  22. using OBSSceneItem =
  23. OBSRef<obs_sceneitem_t *, obs_sceneitem_addref, obs_sceneitem_release>;
  24. using OBSData = OBSRef<obs_data_t *, obs_data_addref, obs_data_release>;
  25. using OBSDataArray = OBSRef<obs_data_array_t *, obs_data_array_addref,
  26. obs_data_array_release>;
  27. using OBSOutput = OBSRef<obs_output_t *, obs_output_addref, obs_output_release>;
  28. using OBSEncoder =
  29. OBSRef<obs_encoder_t *, obs_encoder_addref, obs_encoder_release>;
  30. using OBSService =
  31. OBSRef<obs_service_t *, obs_service_addref, obs_service_release>;
  32. using OBSWeakSource = OBSRef<obs_weak_source_t *, obs_weak_source_addref,
  33. obs_weak_source_release>;
  34. using OBSWeakOutput = OBSRef<obs_weak_output_t *, obs_weak_output_addref,
  35. obs_weak_output_release>;
  36. using OBSWeakEncoder = OBSRef<obs_weak_encoder_t *, obs_weak_encoder_addref,
  37. obs_weak_encoder_release>;
  38. using OBSWeakService = OBSRef<obs_weak_service_t *, obs_weak_service_addref,
  39. obs_weak_service_release>;
  40. #define OBS_AUTORELEASE
  41. using OBSSourceAutoRelease =
  42. OBSRefAutoRelease<obs_source_t *, obs_source_release>;
  43. using OBSSceneAutoRelease = OBSRefAutoRelease<obs_scene_t *, obs_scene_release>;
  44. using OBSSceneItemAutoRelease =
  45. OBSRefAutoRelease<obs_sceneitem_t *, obs_sceneitem_release>;
  46. using OBSDataAutoRelease = OBSRefAutoRelease<obs_data_t *, obs_data_release>;
  47. using OBSDataArrayAutoRelease =
  48. OBSRefAutoRelease<obs_data_array_t *, obs_data_array_release>;
  49. using OBSOutputAutoRelease =
  50. OBSRefAutoRelease<obs_output_t *, obs_output_release>;
  51. using OBSEncoderAutoRelease =
  52. OBSRefAutoRelease<obs_encoder_t *, obs_encoder_release>;
  53. using OBSServiceAutoRelease =
  54. OBSRefAutoRelease<obs_service_t *, obs_service_release>;
  55. using OBSWeakSourceAutoRelease =
  56. OBSRefAutoRelease<obs_weak_source_t *, obs_weak_source_release>;
  57. using OBSWeakOutputAutoRelease =
  58. OBSRefAutoRelease<obs_weak_output_t *, obs_weak_output_release>;
  59. using OBSWeakEncoderAutoRelease =
  60. OBSRefAutoRelease<obs_weak_encoder_t *, obs_weak_encoder_release>;
  61. using OBSWeakServiceAutoRelease =
  62. OBSRefAutoRelease<obs_weak_service_t *, obs_weak_service_release>;
  63. template<typename T, void release(T)> class OBSRefAutoRelease {
  64. protected:
  65. T val;
  66. public:
  67. inline OBSRefAutoRelease() : val(nullptr) {}
  68. inline OBSRefAutoRelease(T val_) : val(val_) {}
  69. OBSRefAutoRelease(const OBSRefAutoRelease &ref) = delete;
  70. inline OBSRefAutoRelease(OBSRefAutoRelease &&ref) : val(ref.val)
  71. {
  72. ref.val = nullptr;
  73. }
  74. inline ~OBSRefAutoRelease() { release(val); }
  75. inline operator T() const { return val; }
  76. inline T Get() const { return val; }
  77. inline bool operator==(T p) const { return val == p; }
  78. inline bool operator!=(T p) const { return val != p; }
  79. inline OBSRefAutoRelease &operator=(OBSRefAutoRelease &&ref)
  80. {
  81. if (this != &ref) {
  82. release(val);
  83. val = ref.val;
  84. ref.val = nullptr;
  85. }
  86. return *this;
  87. }
  88. inline OBSRefAutoRelease &operator=(T new_val)
  89. {
  90. release(val);
  91. val = new_val;
  92. return *this;
  93. }
  94. };
  95. template<typename T, void addref(T), void release(T)>
  96. class OBSRef : public OBSRefAutoRelease<T, release> {
  97. inline OBSRef &Replace(T valIn)
  98. {
  99. addref(valIn);
  100. release(this->val);
  101. this->val = valIn;
  102. return *this;
  103. }
  104. struct TakeOwnership {
  105. };
  106. inline OBSRef(T val_, TakeOwnership)
  107. : OBSRefAutoRelease<T, release>::OBSRefAutoRelease(val_)
  108. {
  109. }
  110. public:
  111. inline OBSRef()
  112. : OBSRefAutoRelease<T, release>::OBSRefAutoRelease(nullptr)
  113. {
  114. }
  115. inline OBSRef(const OBSRef &ref)
  116. : OBSRefAutoRelease<T, release>::OBSRefAutoRelease(ref.val)
  117. {
  118. addref(this->val);
  119. }
  120. inline OBSRef(T val_)
  121. : OBSRefAutoRelease<T, release>::OBSRefAutoRelease(val_)
  122. {
  123. addref(this->val);
  124. }
  125. inline OBSRef &operator=(const OBSRef &ref) { return Replace(ref.val); }
  126. inline OBSRef &operator=(T valIn) { return Replace(valIn); }
  127. friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
  128. friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
  129. friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
  130. friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
  131. friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
  132. friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
  133. friend OBSService OBSGetStrongRef(obs_weak_service_t *weak);
  134. friend OBSWeakService OBSGetWeakRef(obs_service_t *service);
  135. };
  136. inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
  137. {
  138. return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
  139. }
  140. inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
  141. {
  142. return {obs_source_get_weak_source(source),
  143. OBSWeakSource::TakeOwnership()};
  144. }
  145. inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
  146. {
  147. return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
  148. }
  149. inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
  150. {
  151. return {obs_output_get_weak_output(output),
  152. OBSWeakOutput::TakeOwnership()};
  153. }
  154. inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
  155. {
  156. return {obs_weak_encoder_get_encoder(weak),
  157. OBSEncoder::TakeOwnership()};
  158. }
  159. inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
  160. {
  161. return {obs_encoder_get_weak_encoder(encoder),
  162. OBSWeakEncoder::TakeOwnership()};
  163. }
  164. inline OBSService OBSGetStrongRef(obs_weak_service_t *weak)
  165. {
  166. return {obs_weak_service_get_service(weak),
  167. OBSService::TakeOwnership()};
  168. }
  169. inline OBSWeakService OBSGetWeakRef(obs_service_t *service)
  170. {
  171. return {obs_service_get_weak_service(service),
  172. OBSWeakService::TakeOwnership()};
  173. }
  174. /* objects that are not meant to be instanced */
  175. template<typename T, void destroy(T)> class OBSObj {
  176. T obj;
  177. public:
  178. inline OBSObj() : obj(nullptr) {}
  179. inline OBSObj(T obj_) : obj(obj_) {}
  180. inline OBSObj(const OBSObj &) = delete;
  181. inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
  182. inline ~OBSObj() { destroy(obj); }
  183. inline OBSObj &operator=(T obj_)
  184. {
  185. if (obj_ != obj)
  186. destroy(obj);
  187. obj = obj_;
  188. return *this;
  189. }
  190. inline OBSObj &operator=(const OBSObj &) = delete;
  191. inline OBSObj &operator=(OBSObj &&other)
  192. {
  193. if (obj)
  194. destroy(obj);
  195. obj = other.obj;
  196. other.obj = nullptr;
  197. return *this;
  198. }
  199. inline operator T() const { return obj; }
  200. inline bool operator==(T p) const { return obj == p; }
  201. inline bool operator!=(T p) const { return obj != p; }
  202. };
  203. using OBSDisplay = OBSObj<obs_display_t *, obs_display_destroy>;
  204. using OBSView = OBSObj<obs_view_t *, obs_view_destroy>;
  205. /* signal handler connection */
  206. class OBSSignal {
  207. signal_handler_t *handler;
  208. const char *signal;
  209. signal_callback_t callback;
  210. void *param;
  211. public:
  212. inline OBSSignal()
  213. : handler(nullptr),
  214. signal(nullptr),
  215. callback(nullptr),
  216. param(nullptr)
  217. {
  218. }
  219. inline OBSSignal(signal_handler_t *handler_, const char *signal_,
  220. signal_callback_t callback_, void *param_)
  221. : handler(handler_),
  222. signal(signal_),
  223. callback(callback_),
  224. param(param_)
  225. {
  226. signal_handler_connect_ref(handler, signal, callback, param);
  227. }
  228. inline void Disconnect()
  229. {
  230. signal_handler_disconnect(handler, signal, callback, param);
  231. handler = nullptr;
  232. signal = nullptr;
  233. callback = nullptr;
  234. param = nullptr;
  235. }
  236. inline ~OBSSignal() { Disconnect(); }
  237. inline void Connect(signal_handler_t *handler_, const char *signal_,
  238. signal_callback_t callback_, void *param_)
  239. {
  240. Disconnect();
  241. handler = handler_;
  242. signal = signal_;
  243. callback = callback_;
  244. param = param_;
  245. signal_handler_connect_ref(handler, signal, callback, param);
  246. }
  247. OBSSignal(const OBSSignal &) = delete;
  248. OBSSignal(OBSSignal &&other) noexcept
  249. : handler(other.handler),
  250. signal(other.signal),
  251. callback(other.callback),
  252. param(other.param)
  253. {
  254. other.handler = nullptr;
  255. other.signal = nullptr;
  256. other.callback = nullptr;
  257. other.param = nullptr;
  258. }
  259. OBSSignal &operator=(const OBSSignal &) = delete;
  260. OBSSignal &operator=(OBSSignal &&other) noexcept
  261. {
  262. Disconnect();
  263. handler = other.handler;
  264. signal = other.signal;
  265. callback = other.callback;
  266. param = other.param;
  267. other.handler = nullptr;
  268. other.signal = nullptr;
  269. other.callback = nullptr;
  270. other.param = nullptr;
  271. return *this;
  272. }
  273. };