obs-view.c 3.0 KB

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