obs.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. val = ref.val;
  40. ref.val = nullptr;
  41. }
  42. return *this;
  43. }
  44. inline operator T() const {return val;}
  45. inline bool operator==(T p) const {return val == p;}
  46. inline bool operator!=(T p) const {return val != p;}
  47. };
  48. using OBSSource = OBSRef<obs_source_t*, obs_source_addref, obs_source_release>;
  49. using OBSScene = OBSRef<obs_scene_t*, obs_scene_addref, obs_scene_release>;
  50. using OBSSceneItem = OBSRef<obs_sceneitem_t*, obs_sceneitem_addref,
  51. obs_sceneitem_release>;
  52. using OBSData = OBSRef<obs_data_t*, obs_data_addref, obs_data_release>;
  53. using OBSDataArray = OBSRef<obs_data_array_t*, obs_data_array_addref,
  54. obs_data_array_release>;
  55. /* objects that are not meant to be instanced */
  56. template<typename T, void destroy(T)> class OBSObj {
  57. T obj;
  58. public:
  59. inline OBSObj() : obj(nullptr) {}
  60. inline OBSObj(T obj_) : obj(obj_) {}
  61. inline ~OBSObj() {destroy(obj);}
  62. inline OBSObj &operator=(T obj_)
  63. {
  64. if (obj_ != obj)
  65. destroy(obj);
  66. obj = obj_;
  67. return *this;
  68. }
  69. inline operator T() const {return obj;}
  70. inline bool operator==(T p) const {return obj == p;}
  71. inline bool operator!=(T p) const {return obj != p;}
  72. };
  73. using OBSDisplay = OBSObj<obs_display_t*, obs_display_destroy>;
  74. using OBSEncoder = OBSObj<obs_encoder_t*, obs_encoder_destroy>;
  75. using OBSView = OBSObj<obs_view_t*, obs_view_destroy>;
  76. using OBSOutput = OBSObj<obs_output_t*, obs_output_destroy>;
  77. /* signal handler connection */
  78. class OBSSignal {
  79. signal_handler_t *handler;
  80. const char *signal;
  81. signal_callback_t callback;
  82. void *param;
  83. public:
  84. inline OBSSignal()
  85. : handler (nullptr),
  86. signal (nullptr),
  87. callback (nullptr),
  88. param (nullptr)
  89. {}
  90. inline OBSSignal(signal_handler_t *handler_,
  91. const char *signal_,
  92. signal_callback_t callback_,
  93. void *param_)
  94. : handler (handler_),
  95. signal (signal_),
  96. callback (callback_),
  97. param (param_)
  98. {
  99. signal_handler_connect(handler, signal, callback, param);
  100. }
  101. inline void Disconnect()
  102. {
  103. signal_handler_disconnect(handler, signal, callback, param);
  104. handler = nullptr;
  105. signal = nullptr;
  106. callback = nullptr;
  107. param = nullptr;
  108. }
  109. inline ~OBSSignal() {Disconnect();}
  110. inline void Connect(signal_handler_t *handler_,
  111. const char *signal_,
  112. signal_callback_t callback_,
  113. void *param_)
  114. {
  115. Disconnect();
  116. handler = handler_;
  117. signal = signal_;
  118. callback = callback_;
  119. param = param_;
  120. signal_handler_connect(handler, signal, callback, param);
  121. }
  122. };