obs.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 OBSWeakSource = OBSRef<obs_weak_source_t*, obs_weak_source_addref,
  31. obs_weak_source_release>;
  32. using OBSWeakOutput = OBSRef<obs_weak_output_t*, obs_weak_output_addref,
  33. obs_weak_output_release>;
  34. using OBSWeakEncoder = OBSRef<obs_weak_encoder_t*, obs_weak_encoder_addref,
  35. obs_weak_encoder_release>;
  36. template<typename T, void addref(T), void release(T)>
  37. class OBSRef {
  38. T val;
  39. inline OBSRef &Replace(T valIn)
  40. {
  41. addref(valIn);
  42. release(val);
  43. val = valIn;
  44. return *this;
  45. }
  46. struct TakeOwnership {};
  47. inline OBSRef(T val, TakeOwnership) : val(val) {}
  48. public:
  49. inline OBSRef() : val(nullptr) {}
  50. inline OBSRef(T val_) : val(val_) {addref(val);}
  51. inline OBSRef(const OBSRef &ref) : val(ref.val) {addref(val);}
  52. inline OBSRef(OBSRef &&ref) : val(ref.val) {ref.val = nullptr;}
  53. inline ~OBSRef() {release(val);}
  54. inline OBSRef &operator=(T valIn) {return Replace(valIn);}
  55. inline OBSRef &operator=(const OBSRef &ref) {return Replace(ref.val);}
  56. inline OBSRef &operator=(OBSRef &&ref)
  57. {
  58. if (this != &ref) {
  59. release(val);
  60. val = ref.val;
  61. ref.val = nullptr;
  62. }
  63. return *this;
  64. }
  65. inline operator T() const {return val;}
  66. inline bool operator==(T p) const {return val == p;}
  67. inline bool operator!=(T p) const {return val != p;}
  68. friend OBSSource OBSGetStrongRef(obs_weak_source_t *weak);
  69. friend OBSWeakSource OBSGetWeakRef(obs_source_t *source);
  70. friend OBSOutput OBSGetStrongRef(obs_weak_output_t *weak);
  71. friend OBSWeakOutput OBSGetWeakRef(obs_output_t *output);
  72. friend OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak);
  73. friend OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder);
  74. };
  75. inline OBSSource OBSGetStrongRef(obs_weak_source_t *weak)
  76. {
  77. return {obs_weak_source_get_source(weak), OBSSource::TakeOwnership()};
  78. }
  79. inline OBSWeakSource OBSGetWeakRef(obs_source_t *source)
  80. {
  81. return {obs_source_get_weak_source(source),
  82. OBSWeakSource::TakeOwnership()};
  83. }
  84. inline OBSOutput OBSGetStrongRef(obs_weak_output_t *weak)
  85. {
  86. return {obs_weak_output_get_output(weak), OBSOutput::TakeOwnership()};
  87. }
  88. inline OBSWeakOutput OBSGetWeakRef(obs_output_t *output)
  89. {
  90. return {obs_output_get_weak_output(output),
  91. OBSWeakOutput::TakeOwnership()};
  92. }
  93. inline OBSEncoder OBSGetStrongRef(obs_weak_encoder_t *weak)
  94. {
  95. return {obs_weak_encoder_get_encoder(weak),
  96. OBSEncoder::TakeOwnership()};
  97. }
  98. inline OBSWeakEncoder OBSGetWeakRef(obs_encoder_t *encoder)
  99. {
  100. return {obs_encoder_get_weak_encoder(encoder),
  101. OBSWeakEncoder::TakeOwnership()};
  102. }
  103. /* objects that are not meant to be instanced */
  104. template<typename T, void destroy(T)> class OBSObj {
  105. T obj;
  106. public:
  107. inline OBSObj() : obj(nullptr) {}
  108. inline OBSObj(T obj_) : obj(obj_) {}
  109. inline OBSObj(const OBSObj&) = delete;
  110. inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
  111. inline ~OBSObj() {destroy(obj);}
  112. inline OBSObj &operator=(T obj_)
  113. {
  114. if (obj_ != obj)
  115. destroy(obj);
  116. obj = obj_;
  117. return *this;
  118. }
  119. inline OBSObj &operator=(const OBSObj&) = delete;
  120. inline OBSObj &operator=(OBSObj &&other)
  121. {
  122. if (obj)
  123. destroy(obj);
  124. obj = other.obj;
  125. other.obj = nullptr;
  126. return *this;
  127. }
  128. inline operator T() const {return obj;}
  129. inline bool operator==(T p) const {return obj == p;}
  130. inline bool operator!=(T p) const {return obj != p;}
  131. };
  132. using OBSDisplay = OBSObj<obs_display_t*, obs_display_destroy>;
  133. using OBSView = OBSObj<obs_view_t*, obs_view_destroy>;
  134. /* signal handler connection */
  135. class OBSSignal {
  136. signal_handler_t *handler;
  137. const char *signal;
  138. signal_callback_t callback;
  139. void *param;
  140. public:
  141. inline OBSSignal()
  142. : handler (nullptr),
  143. signal (nullptr),
  144. callback (nullptr),
  145. param (nullptr)
  146. {}
  147. inline OBSSignal(signal_handler_t *handler_,
  148. const char *signal_,
  149. signal_callback_t callback_,
  150. void *param_)
  151. : handler (handler_),
  152. signal (signal_),
  153. callback (callback_),
  154. param (param_)
  155. {
  156. signal_handler_connect(handler, signal, callback, param);
  157. }
  158. inline void Disconnect()
  159. {
  160. signal_handler_disconnect(handler, signal, callback, param);
  161. handler = nullptr;
  162. signal = nullptr;
  163. callback = nullptr;
  164. param = nullptr;
  165. }
  166. inline ~OBSSignal() {Disconnect();}
  167. inline void Connect(signal_handler_t *handler_,
  168. const char *signal_,
  169. signal_callback_t callback_,
  170. void *param_)
  171. {
  172. Disconnect();
  173. handler = handler_;
  174. signal = signal_;
  175. callback = callback_;
  176. param = param_;
  177. signal_handler_connect(handler, signal, callback, param);
  178. }
  179. OBSSignal(const OBSSignal&) = delete;
  180. OBSSignal(OBSSignal &&other)
  181. : handler (other.handler),
  182. signal (other.signal),
  183. callback(other.callback),
  184. param (other.param)
  185. {
  186. other.handler = nullptr;
  187. other.signal = nullptr;
  188. other.callback = nullptr;
  189. other.param = nullptr;
  190. }
  191. OBSSignal &operator=(const OBSSignal &) = delete;
  192. OBSSignal &operator=(OBSSignal &&other)
  193. {
  194. Disconnect();
  195. handler = other.handler;
  196. signal = other.signal;
  197. callback = other.callback;
  198. param = other.param;
  199. other.handler = nullptr;
  200. other.signal = nullptr;
  201. other.callback = nullptr;
  202. other.param = nullptr;
  203. return *this;
  204. }
  205. };