obs-view.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, enum view_type type)
  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. view->type = type;
  26. return true;
  27. }
  28. obs_view_t *obs_view_create(void)
  29. {
  30. struct obs_view *view = bzalloc(sizeof(struct obs_view));
  31. if (!obs_view_init(view, AUX_VIEW)) {
  32. bfree(view);
  33. view = NULL;
  34. }
  35. return view;
  36. }
  37. void obs_view_free(struct obs_view *view)
  38. {
  39. if (!view)
  40. return;
  41. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  42. struct obs_source *source = view->channels[i];
  43. if (source) {
  44. obs_source_deactivate(source, view->type);
  45. obs_source_release(source);
  46. }
  47. }
  48. memset(view->channels, 0, sizeof(view->channels));
  49. pthread_mutex_destroy(&view->channels_mutex);
  50. }
  51. void obs_view_destroy(obs_view_t *view)
  52. {
  53. if (view) {
  54. obs_view_free(view);
  55. bfree(view);
  56. }
  57. }
  58. obs_source_t *obs_view_get_source(obs_view_t *view, uint32_t channel)
  59. {
  60. obs_source_t *source;
  61. assert(channel < MAX_CHANNELS);
  62. if (!view)
  63. return NULL;
  64. if (channel >= MAX_CHANNELS)
  65. return NULL;
  66. pthread_mutex_lock(&view->channels_mutex);
  67. source = obs_source_get_ref(view->channels[channel]);
  68. pthread_mutex_unlock(&view->channels_mutex);
  69. return source;
  70. }
  71. void obs_view_set_source(obs_view_t *view, uint32_t channel, 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, view->type);
  86. if (prev_source) {
  87. obs_source_deactivate(prev_source, view->type);
  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. video_t *obs_view_add(obs_view_t *view)
  111. {
  112. if (!obs->data.main_canvas->mix)
  113. return NULL;
  114. return obs_view_add2(view, &obs->data.main_canvas->mix->ovi);
  115. }
  116. video_t *obs_view_add2(obs_view_t *view, struct obs_video_info *ovi)
  117. {
  118. if (!view || !ovi)
  119. return NULL;
  120. struct obs_core_video_mix *mix = obs_create_video_mix(ovi);
  121. if (!mix) {
  122. return NULL;
  123. }
  124. mix->view = view;
  125. pthread_mutex_lock(&obs->video.mixes_mutex);
  126. da_push_back(obs->video.mixes, &mix);
  127. pthread_mutex_unlock(&obs->video.mixes_mutex);
  128. return mix->video;
  129. }
  130. void obs_view_remove(obs_view_t *view)
  131. {
  132. if (!view)
  133. return;
  134. pthread_mutex_lock(&obs->video.mixes_mutex);
  135. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  136. if (obs->video.mixes.array[i]->view == view)
  137. obs->video.mixes.array[i]->view = NULL;
  138. }
  139. pthread_mutex_unlock(&obs->video.mixes_mutex);
  140. }
  141. void obs_view_enum_video_info(obs_view_t *view, bool (*enum_proc)(void *, struct obs_video_info *), void *param)
  142. {
  143. pthread_mutex_lock(&obs->video.mixes_mutex);
  144. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  145. struct obs_core_video_mix *mix = obs->video.mixes.array[i];
  146. if (mix->view != view)
  147. continue;
  148. if (!enum_proc(param, &mix->ovi))
  149. break;
  150. }
  151. pthread_mutex_unlock(&obs->video.mixes_mutex);
  152. }