obs-view.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /******************************************************************************
  2. Copyright (C) 2014 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. #include "obs.h"
  15. #include "obs-internal.h"
  16. bool obs_view_init(struct obs_view *view)
  17. {
  18. if (!view) return false;
  19. pthread_mutex_init_value(&view->channels_mutex);
  20. if (pthread_mutex_init(&view->channels_mutex, NULL) != 0) {
  21. blog(LOG_ERROR, "obs_view_init: Failed to create mutex");
  22. return false;
  23. }
  24. return true;
  25. }
  26. obs_view_t *obs_view_create(void)
  27. {
  28. struct obs_view *view = bzalloc(sizeof(struct obs_view));
  29. if (!obs_view_init(view)) {
  30. bfree(view);
  31. view = NULL;
  32. }
  33. return view;
  34. }
  35. void obs_view_free(struct obs_view *view)
  36. {
  37. if (!view) return;
  38. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  39. struct obs_source *source = view->channels[i];
  40. if (source) {
  41. obs_source_deactivate(source, AUX_VIEW);
  42. obs_source_release(source);
  43. }
  44. }
  45. memset(view->channels, 0, sizeof(view->channels));
  46. pthread_mutex_destroy(&view->channels_mutex);
  47. }
  48. void obs_view_destroy(obs_view_t *view)
  49. {
  50. if (view) {
  51. obs_view_free(view);
  52. bfree(view);
  53. }
  54. }
  55. obs_source_t *obs_view_get_source(obs_view_t *view, uint32_t channel)
  56. {
  57. obs_source_t *source;
  58. assert(channel < MAX_CHANNELS);
  59. if (!view) return NULL;
  60. if (channel >= MAX_CHANNELS) return NULL;
  61. pthread_mutex_lock(&view->channels_mutex);
  62. source = view->channels[channel];
  63. if (source)
  64. obs_source_addref(source);
  65. pthread_mutex_unlock(&view->channels_mutex);
  66. return source;
  67. }
  68. void obs_view_set_source(obs_view_t *view, uint32_t channel,
  69. obs_source_t *source)
  70. {
  71. struct obs_source *prev_source;
  72. assert(channel < MAX_CHANNELS);
  73. if (!view) return;
  74. if (channel >= MAX_CHANNELS) return;
  75. pthread_mutex_lock(&view->channels_mutex);
  76. obs_source_addref(source);
  77. prev_source = view->channels[channel];
  78. view->channels[channel] = source;
  79. pthread_mutex_unlock(&view->channels_mutex);
  80. if (source)
  81. obs_source_activate(source, AUX_VIEW);
  82. if (prev_source) {
  83. obs_source_deactivate(prev_source, AUX_VIEW);
  84. obs_source_release(prev_source);
  85. }
  86. }
  87. void obs_view_render(obs_view_t *view)
  88. {
  89. if (!view) return;
  90. pthread_mutex_lock(&view->channels_mutex);
  91. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  92. struct obs_source *source;
  93. source = view->channels[i];
  94. if (source) {
  95. if (source->removed) {
  96. obs_source_release(source);
  97. view->channels[i] = NULL;
  98. } else {
  99. obs_source_video_render(source);
  100. }
  101. }
  102. }
  103. pthread_mutex_unlock(&view->channels_mutex);
  104. }