obs.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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)>
  19. 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 = OBSRef<obs_sceneitem_t*, obs_sceneitem_addref,
  23. 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 = OBSRef<obs_encoder_t*, obs_encoder_addref,
  29. obs_encoder_release>;
  30. using OBSService = OBSRef<obs_service_t*, obs_service_addref,
  31. 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. template<typename T, void addref(T), void release(T)>
  41. class OBSRef {
  42. T val;
  43. inline OBSRef &Replace(T valIn)
  44. {
  45. addref(valIn);
  46. release(val);
  47. val = valIn;
  48. return *this;
  49. }
  50. struct TakeOwnership {};
  51. inline OBSRef(T val, TakeOwnership) : val(val) {}
  52. public:
  53. inline OBSRef() : val(nullptr) {}
  54. inline OBSRef(T val_) : val(val_) {addref(val);}
  55. inline OBSRef(const OBSRef &ref) : val(ref.val) {addref(val);}
  56. inline OBSRef(OBSRef &&ref) : val(ref.val) {ref.val = nullptr;}
  57. inline ~OBSRef() {release(val);}
  58. inline OBSRef &operator=(T valIn) {return Replace(valIn);}
  59. inline OBSRef &operator=(const OBSRef &ref) {return Replace(ref.val);}
  60. inline OBSRef &operator=(OBSRef &&ref)
  61. {
  62. if (this != &ref) {
  63. release(val);
  64. val = ref.val;
  65. ref.val = nullptr;
  66. }
  67. return *this;
  68. }
  69. inline operator T() const {return val;}
  70. inline bool operator==(T p) const {return val == p;}
  71. inline bool operator!=(T p) const {return val != p;}
  72. friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
  73. friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
  74. friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
  75. friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
  76. friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
  77. friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
  78. friend OBSService OBSGetStrongRef(obs_weak_service_t *weak);
  79. friend OBSWeakService OBSGetWeakRef(obs_service_t *service);
  80. };
  81. inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
  82. {
  83. return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
  84. }
  85. inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
  86. {
  87. return {obs_source_get_weak_source(source),
  88. OBSWeakSource::TakeOwnership()};
  89. }
  90. inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
  91. {
  92. return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
  93. }
  94. inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
  95. {
  96. return {obs_output_get_weak_output(output),
  97. OBSWeakOutput::TakeOwnership()};
  98. }
  99. inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
  100. {
  101. return {obs_weak_encoder_get_encoder(weak),
  102. OBSEncoder::TakeOwnership()};
  103. }
  104. inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
  105. {
  106. return {obs_encoder_get_weak_encoder(encoder),
  107. OBSWeakEncoder::TakeOwnership()};
  108. }
  109. inline OBSService OBSGetStrongRef(obs_weak_service_t *weak)
  110. {
  111. return {obs_weak_service_get_service(weak),
  112. OBSService::TakeOwnership()};
  113. }
  114. inline OBSWeakService OBSGetWeakRef(obs_service_t *service)
  115. {
  116. return {obs_service_get_weak_service(service),
  117. OBSWeakService::TakeOwnership()};
  118. }
  119. /* objects that are not meant to be instanced */
  120. template<typename T, void destroy(T)> class OBSObj {
  121. T obj;
  122. public:
  123. inline OBSObj() : obj(nullptr) {}
  124. inline OBSObj(T obj_) : obj(obj_) {}
  125. inline OBSObj(const OBSObj&) = delete;
  126. inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
  127. inline ~OBSObj() {destroy(obj);}
  128. inline OBSObj &operator=(T obj_)
  129. {
  130. if (obj_ != obj)
  131. destroy(obj);
  132. obj = obj_;
  133. return *this;
  134. }
  135. inline OBSObj &operator=(const OBSObj&) = delete;
  136. inline OBSObj &operator=(OBSObj &&other)
  137. {
  138. if (obj)
  139. destroy(obj);
  140. obj = other.obj;
  141. other.obj = nullptr;
  142. return *this;
  143. }
  144. inline operator T() const {return obj;}
  145. inline bool operator==(T p) const {return obj == p;}
  146. inline bool operator!=(T p) const {return obj != p;}
  147. };
  148. using OBSDisplay = OBSObj<obs_display_t*, obs_display_destroy>;
  149. using OBSView = OBSObj<obs_view_t*, obs_view_destroy>;
  150. /* signal handler connection */
  151. class OBSSignal {
  152. signal_handler_t *handler;
  153. const char *signal;
  154. signal_callback_t callback;
  155. void *param;
  156. public:
  157. inline OBSSignal()
  158. : handler (nullptr),
  159. signal (nullptr),
  160. callback (nullptr),
  161. param (nullptr)
  162. {}
  163. inline OBSSignal(signal_handler_t *handler_,
  164. const char *signal_,
  165. signal_callback_t callback_,
  166. void *param_)
  167. : handler (handler_),
  168. signal (signal_),
  169. callback (callback_),
  170. param (param_)
  171. {
  172. signal_handler_connect_ref(handler, signal, callback, param);
  173. }
  174. inline void Disconnect()
  175. {
  176. signal_handler_disconnect(handler, signal, callback, param);
  177. handler = nullptr;
  178. signal = nullptr;
  179. callback = nullptr;
  180. param = nullptr;
  181. }
  182. inline ~OBSSignal() {Disconnect();}
  183. inline void Connect(signal_handler_t *handler_,
  184. const char *signal_,
  185. signal_callback_t callback_,
  186. void *param_)
  187. {
  188. Disconnect();
  189. handler = handler_;
  190. signal = signal_;
  191. callback = callback_;
  192. param = param_;
  193. signal_handler_connect_ref(handler, signal, callback, param);
  194. }
  195. OBSSignal(const OBSSignal&) = delete;
  196. OBSSignal(OBSSignal &&other)
  197. : handler (other.handler),
  198. signal (other.signal),
  199. callback(other.callback),
  200. param (other.param)
  201. {
  202. other.handler = nullptr;
  203. other.signal = nullptr;
  204. other.callback = nullptr;
  205. other.param = nullptr;
  206. }
  207. OBSSignal &operator=(const OBSSignal &) = delete;
  208. OBSSignal &operator=(OBSSignal &&other)
  209. {
  210. Disconnect();
  211. handler = other.handler;
  212. signal = other.signal;
  213. callback = other.callback;
  214. param = other.param;
  215. other.handler = nullptr;
  216. other.signal = nullptr;
  217. other.callback = nullptr;
  218. other.param = nullptr;
  219. return *this;
  220. }
  221. };
  222. class OBSContext {
  223. public:
  224. inline OBSContext() {}
  225. inline OBSContext(const char *locale,
  226. const char *module_config_path=nullptr,
  227. profiler_name_store *store=nullptr)
  228. {
  229. obs_startup(locale, module_config_path, store);
  230. }
  231. inline ~OBSContext()
  232. {
  233. obs_shutdown();
  234. }
  235. };