obs-video.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 3 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-data.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);
  24. delta_time = cur_time - *last_time;
  25. seconds = (float)((double)delta_time / 1000000000.0);
  26. for (i = 0; i < obs->sources.num; i++)
  27. obs_source_video_tick(obs->sources.array[i], seconds);
  28. *last_time = cur_time;
  29. }
  30. static inline void render_display(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. if (obs->primary_source)
  46. obs_source_video_render(obs->primary_source);
  47. gs_endscene();
  48. gs_present();
  49. }
  50. static inline void render_displays(void)
  51. {
  52. size_t i;
  53. pthread_mutex_lock(&obs->display_list_mutex);
  54. for (i = 0; i < obs->displays.num; i++) {
  55. struct obs_display *display = obs->displays.array[i];
  56. render_display(display);
  57. }
  58. pthread_mutex_unlock(&obs->display_list_mutex);
  59. render_display(NULL);
  60. }
  61. static bool swap_frame(uint64_t timestamp)
  62. {
  63. stagesurf_t last_surface = obs->copy_surfaces[obs->cur_texture];
  64. stagesurf_t surface;
  65. struct video_frame frame;
  66. if (obs->copy_mapped) {
  67. stagesurface_unmap(last_surface);
  68. obs->copy_mapped = false;
  69. }
  70. obs->textures_copied[obs->cur_texture] = true;
  71. //gs_stage_texture(last_surface, NULL);
  72. if (++obs->cur_texture == NUM_TEXTURES)
  73. obs->cur_texture = 0;
  74. if (obs->textures_copied[obs->cur_texture]) {
  75. surface = obs->copy_surfaces[obs->cur_texture];
  76. obs->copy_mapped = stagesurface_map(surface, &frame.data,
  77. &frame.row_size);
  78. if (obs->copy_mapped) {
  79. frame.timestamp = timestamp;
  80. video_output_frame(obs->video, &frame);
  81. }
  82. }
  83. return obs->copy_mapped;
  84. }
  85. void *obs_video_thread(void *param)
  86. {
  87. uint64_t last_time = 0;
  88. while (video_output_wait(obs->video)) {
  89. uint64_t cur_time = video_gettime(obs->video);
  90. gs_entercontext(obs_graphics());
  91. tick_sources(cur_time, &last_time);
  92. render_displays();
  93. swap_frame(cur_time);
  94. gs_leavecontext();
  95. }
  96. return NULL;
  97. }