obs-view.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #include "obs.h"
  15. #include "obs-internal.h"
  16. bool obs_view_init(struct obs_view *view)
  17. {
  18. if (!view)
  19. return false;
  20. pthread_mutex_init_value(&view->channels_mutex);
  21. if (pthread_mutex_init(&view->channels_mutex, NULL) != 0) {
  22. blog(LOG_ERROR, "obs_view_init: Failed to create mutex");
  23. return false;
  24. }
  25. return true;
  26. }
  27. obs_view_t *obs_view_create(void)
  28. {
  29. struct obs_view *view = bzalloc(sizeof(struct obs_view));
  30. if (!obs_view_init(view)) {
  31. bfree(view);
  32. view = NULL;
  33. }
  34. return view;
  35. }
  36. void obs_view_free(struct obs_view *view)
  37. {
  38. if (!view)
  39. return;
  40. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  41. struct obs_source *source = view->channels[i];
  42. if (source) {
  43. obs_source_deactivate(source, AUX_VIEW);
  44. obs_source_release(source);
  45. }
  46. }
  47. memset(view->channels, 0, sizeof(view->channels));
  48. pthread_mutex_destroy(&view->channels_mutex);
  49. }
  50. void obs_view_destroy(obs_view_t *view)
  51. {
  52. if (view) {
  53. obs_view_free(view);
  54. bfree(view);
  55. }
  56. }
  57. obs_source_t *obs_view_get_source(obs_view_t *view, uint32_t channel)
  58. {
  59. obs_source_t *source;
  60. assert(channel < MAX_CHANNELS);
  61. if (!view)
  62. return NULL;
  63. if (channel >= MAX_CHANNELS)
  64. return NULL;
  65. pthread_mutex_lock(&view->channels_mutex);
  66. source = obs_source_get_ref(view->channels[channel]);
  67. pthread_mutex_unlock(&view->channels_mutex);
  68. return source;
  69. }
  70. void obs_view_set_source(obs_view_t *view, uint32_t channel,
  71. obs_source_t *source)
  72. {
  73. struct obs_source *prev_source;
  74. assert(channel < MAX_CHANNELS);
  75. if (!view)
  76. return;
  77. if (channel >= MAX_CHANNELS)
  78. return;
  79. pthread_mutex_lock(&view->channels_mutex);
  80. source = obs_source_get_ref(source);
  81. prev_source = view->channels[channel];
  82. view->channels[channel] = source;
  83. pthread_mutex_unlock(&view->channels_mutex);
  84. if (source)
  85. obs_source_activate(source, AUX_VIEW);
  86. if (prev_source) {
  87. obs_source_deactivate(prev_source, AUX_VIEW);
  88. obs_source_release(prev_source);
  89. }
  90. }
  91. void obs_view_render(obs_view_t *view)
  92. {
  93. if (!view)
  94. return;
  95. pthread_mutex_lock(&view->channels_mutex);
  96. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  97. struct obs_source *source;
  98. source = view->channels[i];
  99. if (source) {
  100. if (source->removed) {
  101. obs_source_release(source);
  102. view->channels[i] = NULL;
  103. } else {
  104. obs_source_video_render(source);
  105. }
  106. }
  107. }
  108. pthread_mutex_unlock(&view->channels_mutex);
  109. }
  110. static inline size_t find_mix_for_view(obs_view_t *view)
  111. {
  112. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  113. if (obs->video.mixes.array[i]->view == view)
  114. return i;
  115. }
  116. return DARRAY_INVALID;
  117. }
  118. static inline void set_main_mix()
  119. {
  120. size_t idx = find_mix_for_view(&obs->data.main_view);
  121. struct obs_core_video_mix *mix = NULL;
  122. if (idx != DARRAY_INVALID)
  123. mix = obs->video.mixes.array[idx];
  124. obs->video.main_mix = mix;
  125. }
  126. video_t *obs_view_add(obs_view_t *view)
  127. {
  128. if (!obs->video.main_mix)
  129. return NULL;
  130. return obs_view_add2(view, &obs->video.main_mix->ovi);
  131. }
  132. video_t *obs_view_add2(obs_view_t *view, struct obs_video_info *ovi)
  133. {
  134. if (!view || !ovi)
  135. return NULL;
  136. struct obs_core_video_mix *mix = obs_create_video_mix(ovi);
  137. if (!mix) {
  138. return NULL;
  139. }
  140. mix->view = view;
  141. pthread_mutex_lock(&obs->video.mixes_mutex);
  142. da_push_back(obs->video.mixes, &mix);
  143. set_main_mix();
  144. pthread_mutex_unlock(&obs->video.mixes_mutex);
  145. return mix->video;
  146. }
  147. void obs_view_remove(obs_view_t *view)
  148. {
  149. if (!view)
  150. return;
  151. pthread_mutex_lock(&obs->video.mixes_mutex);
  152. size_t idx = find_mix_for_view(view);
  153. if (idx != DARRAY_INVALID)
  154. obs->video.mixes.array[idx]->view = NULL;
  155. set_main_mix();
  156. pthread_mutex_unlock(&obs->video.mixes_mutex);
  157. }
  158. bool obs_view_get_video_info(obs_view_t *view, struct obs_video_info *ovi)
  159. {
  160. if (!view)
  161. return false;
  162. pthread_mutex_lock(&obs->video.mixes_mutex);
  163. size_t idx = find_mix_for_view(view);
  164. if (idx != DARRAY_INVALID) {
  165. *ovi = obs->video.mixes.array[idx]->ovi;
  166. pthread_mutex_unlock(&obs->video.mixes_mutex);
  167. return true;
  168. }
  169. pthread_mutex_unlock(&obs->video.mixes_mutex);
  170. return false;
  171. }