obs-view.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. obs_source_release(view->channels[i]);
  40. memset(view->channels, 0, sizeof(view->channels));
  41. pthread_mutex_destroy(&view->channels_mutex);
  42. }
  43. void obs_view_destroy(obs_view_t *view)
  44. {
  45. if (view) {
  46. obs_view_free(view);
  47. bfree(view);
  48. }
  49. }
  50. obs_source_t *obs_view_get_source(obs_view_t *view, uint32_t channel)
  51. {
  52. obs_source_t *source;
  53. assert(channel < MAX_CHANNELS);
  54. if (!view) return NULL;
  55. if (channel >= MAX_CHANNELS) return NULL;
  56. pthread_mutex_lock(&view->channels_mutex);
  57. source = view->channels[channel];
  58. if (source)
  59. obs_source_addref(source);
  60. pthread_mutex_unlock(&view->channels_mutex);
  61. return source;
  62. }
  63. void obs_view_set_source(obs_view_t *view, uint32_t channel,
  64. obs_source_t *source)
  65. {
  66. struct obs_source *prev_source;
  67. assert(channel < MAX_CHANNELS);
  68. if (!view) return;
  69. if (channel >= MAX_CHANNELS) return;
  70. pthread_mutex_lock(&view->channels_mutex);
  71. obs_source_addref(source);
  72. prev_source = view->channels[channel];
  73. view->channels[channel] = source;
  74. pthread_mutex_unlock(&view->channels_mutex);
  75. if (source)
  76. obs_source_activate(source, AUX_VIEW);
  77. if (prev_source) {
  78. obs_source_deactivate(prev_source, AUX_VIEW);
  79. obs_source_release(prev_source);
  80. }
  81. }
  82. void obs_view_render(obs_view_t *view)
  83. {
  84. if (!view) return;
  85. pthread_mutex_lock(&view->channels_mutex);
  86. for (size_t i = 0; i < MAX_CHANNELS; i++) {
  87. struct obs_source *source;
  88. source = view->channels[i];
  89. if (source) {
  90. if (source->removed) {
  91. obs_source_release(source);
  92. view->channels[i] = NULL;
  93. } else {
  94. obs_source_video_render(source);
  95. }
  96. }
  97. }
  98. pthread_mutex_unlock(&view->channels_mutex);
  99. }