obs.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. T val;
  21. inline OBSRef &Replace(T valIn)
  22. {
  23. addref(valIn);
  24. release(val);
  25. val = valIn;
  26. return *this;
  27. }
  28. public:
  29. inline OBSRef() : val(nullptr) {}
  30. inline OBSRef(T val_) : val(val_) {addref(val);}
  31. inline OBSRef(const OBSRef &ref) : val(ref.val) {addref(val);}
  32. inline OBSRef(OBSRef &&ref) : val(ref.val) {ref.val = nullptr;}
  33. inline ~OBSRef() {release(val);}
  34. inline OBSRef &operator=(T valIn) {return Replace(valIn);}
  35. inline OBSRef &operator=(const OBSRef &ref) {return Replace(ref.val);}
  36. inline OBSRef &operator=(OBSRef &&ref)
  37. {
  38. if (this != &ref) {
  39. release(val);
  40. val = ref.val;
  41. ref.val = nullptr;
  42. }
  43. return *this;
  44. }
  45. inline operator T() const {return val;}
  46. inline bool operator==(T p) const {return val == p;}
  47. inline bool operator!=(T p) const {return val != p;}
  48. };
  49. using OBSSource = OBSRef<obs_source_t*, obs_source_addref, obs_source_release>;
  50. using OBSScene = OBSRef<obs_scene_t*, obs_scene_addref, obs_scene_release>;
  51. using OBSSceneItem = OBSRef<obs_sceneitem_t*, obs_sceneitem_addref,
  52. obs_sceneitem_release>;
  53. using OBSData = OBSRef<obs_data_t*, obs_data_addref, obs_data_release>;
  54. using OBSDataArray = OBSRef<obs_data_array_t*, obs_data_array_addref,
  55. obs_data_array_release>;
  56. /* objects that are not meant to be instanced */
  57. template<typename T, void destroy(T)> class OBSObj {
  58. T obj;
  59. public:
  60. inline OBSObj() : obj(nullptr) {}
  61. inline OBSObj(T obj_) : obj(obj_) {}
  62. inline OBSObj(const OBSObj&) = delete;
  63. inline OBSObj(OBSObj &&other) : obj(other.obj) { other.obj = nullptr; }
  64. inline ~OBSObj() {destroy(obj);}
  65. inline OBSObj &operator=(T obj_)
  66. {
  67. if (obj_ != obj)
  68. destroy(obj);
  69. obj = obj_;
  70. return *this;
  71. }
  72. inline OBSObj &operator=(const OBSObj&) = delete;
  73. inline OBSObj &operator=(OBSObj &&other)
  74. {
  75. if (obj)
  76. destroy(obj);
  77. obj = other.obj;
  78. other.obj = nullptr;
  79. return *this;
  80. }
  81. inline operator T() const {return obj;}
  82. inline bool operator==(T p) const {return obj == p;}
  83. inline bool operator!=(T p) const {return obj != p;}
  84. };
  85. using OBSDisplay = OBSObj<obs_display_t*, obs_display_destroy>;
  86. using OBSEncoder = OBSObj<obs_encoder_t*, obs_encoder_destroy>;
  87. using OBSView = OBSObj<obs_view_t*, obs_view_destroy>;
  88. using OBSOutput = OBSObj<obs_output_t*, obs_output_destroy>;
  89. /* signal handler connection */
  90. class OBSSignal {
  91. signal_handler_t *handler;
  92. const char *signal;
  93. signal_callback_t callback;
  94. void *param;
  95. public:
  96. inline OBSSignal()
  97. : handler (nullptr),
  98. signal (nullptr),
  99. callback (nullptr),
  100. param (nullptr)
  101. {}
  102. inline OBSSignal(signal_handler_t *handler_,
  103. const char *signal_,
  104. signal_callback_t callback_,
  105. void *param_)
  106. : handler (handler_),
  107. signal (signal_),
  108. callback (callback_),
  109. param (param_)
  110. {
  111. signal_handler_connect(handler, signal, callback, param);
  112. }
  113. inline void Disconnect()
  114. {
  115. signal_handler_disconnect(handler, signal, callback, param);
  116. handler = nullptr;
  117. signal = nullptr;
  118. callback = nullptr;
  119. param = nullptr;
  120. }
  121. inline ~OBSSignal() {Disconnect();}
  122. inline void Connect(signal_handler_t *handler_,
  123. const char *signal_,
  124. signal_callback_t callback_,
  125. void *param_)
  126. {
  127. Disconnect();
  128. handler = handler_;
  129. signal = signal_;
  130. callback = callback_;
  131. param = param_;
  132. signal_handler_connect(handler, signal, callback, param);
  133. }
  134. OBSSignal(const OBSSignal&) = delete;
  135. OBSSignal(OBSSignal &&other)
  136. : handler (other.handler),
  137. signal (other.signal),
  138. callback(other.callback),
  139. param (other.param)
  140. {
  141. other.handler = nullptr;
  142. other.signal = nullptr;
  143. other.callback = nullptr;
  144. other.param = nullptr;
  145. }
  146. OBSSignal &operator=(const OBSSignal &) = delete;
  147. OBSSignal &operator=(OBSSignal &&other)
  148. {
  149. Disconnect();
  150. handler = other.handler;
  151. signal = other.signal;
  152. callback = other.callback;
  153. param = other.param;
  154. other.handler = nullptr;
  155. other.signal = nullptr;
  156. other.callback = nullptr;
  157. other.param = nullptr;
  158. return *this;
  159. }
  160. };