obs.hpp 6.2 KB

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