obs-video.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /******************************************************************************
  2. Copyright (C) 2013 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. #include "graphics/vec4.h"
  17. static void tick_sources(uint64_t cur_time, uint64_t *last_time)
  18. {
  19. size_t i;
  20. uint64_t delta_time;
  21. float seconds;
  22. if (!last_time)
  23. *last_time = cur_time - video_getframetime(obs->video.video);
  24. delta_time = cur_time - *last_time;
  25. seconds = (float)((double)delta_time / 1000000000.0);
  26. for (i = 0; i < obs->data.sources.num; i++)
  27. obs_source_video_tick(obs->data.sources.array[i], seconds);
  28. *last_time = cur_time;
  29. }
  30. static inline void render_begin(struct obs_display *display)
  31. {
  32. struct vec4 clear_color;
  33. uint32_t width, height;
  34. gs_load_swapchain(display ? display->swap : NULL);
  35. gs_getsize(&width, &height);
  36. gs_beginscene();
  37. vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
  38. gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH | GS_CLEAR_STENCIL,
  39. &clear_color, 1.0f, 0);
  40. gs_enable_depthtest(false);
  41. /* gs_enable_blending(false); */
  42. gs_setcullmode(GS_NEITHER);
  43. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  44. gs_setviewport(0, 0, width, height);
  45. }
  46. static inline void render_end(struct obs_display *display)
  47. {
  48. gs_endscene();
  49. gs_present();
  50. }
  51. static void render_display(struct obs_display *display)
  52. {
  53. size_t i;
  54. render_begin(display);
  55. for (i = 0; i < MAX_CHANNELS; i++) {
  56. struct obs_source **p_source;
  57. p_source = (display) ? display->channels+i :
  58. obs->data.channels+i;
  59. if (*p_source) {
  60. if ((*p_source)->removed) {
  61. obs_source_release(*p_source);
  62. *p_source = NULL;
  63. } else {
  64. obs_source_video_render(*p_source);
  65. }
  66. }
  67. }
  68. render_end(display);
  69. }
  70. static inline void render_displays(void)
  71. {
  72. size_t i;
  73. if (!obs->data.valid)
  74. return;
  75. /* render extra displays/swaps */
  76. pthread_mutex_lock(&obs->data.displays_mutex);
  77. for (i = 0; i < obs->data.displays.num; i++) {
  78. render_display(obs->data.displays.array[i]);
  79. }
  80. pthread_mutex_unlock(&obs->data.displays_mutex);
  81. /* render main display */
  82. render_display(NULL);
  83. }
  84. static bool swap_frame(uint64_t timestamp)
  85. {
  86. struct obs_video *video = &obs->video;
  87. stagesurf_t last_surface = video->copy_surfaces[video->cur_texture];
  88. stagesurf_t surface;
  89. struct video_frame frame;
  90. /* the last frame stays mapped until rendering starts with the next */
  91. if (video->copy_mapped) {
  92. stagesurface_unmap(last_surface);
  93. video->copy_mapped = false;
  94. }
  95. video->textures_copied[video->cur_texture] = true;
  96. /* TODO: texture staging */
  97. //gs_stage_texture(last_surface, );
  98. if (++video->cur_texture == NUM_TEXTURES)
  99. video->cur_texture = 0;
  100. if (video->textures_copied[video->cur_texture]) {
  101. surface = video->copy_surfaces[video->cur_texture];
  102. video->copy_mapped = stagesurface_map(surface, &frame.data,
  103. &frame.row_size);
  104. if (video->copy_mapped) {
  105. frame.timestamp = timestamp;
  106. video_output_frame(video->video, &frame);
  107. }
  108. }
  109. return video->copy_mapped;
  110. }
  111. void *obs_video_thread(void *param)
  112. {
  113. uint64_t last_time = 0;
  114. while (video_output_wait(obs->video.video)) {
  115. uint64_t cur_time = video_gettime(obs->video.video);
  116. gs_entercontext(obs_graphics());
  117. tick_sources(cur_time, &last_time);
  118. render_displays();
  119. swap_frame(cur_time);
  120. gs_leavecontext();
  121. }
  122. return NULL;
  123. }