obs-video.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(obs_t obs, 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. source_video_tick(obs->sources.array[i], seconds);
  28. *last_time = cur_time;
  29. }
  30. static inline void render_displays(obs_t obs)
  31. {
  32. size_t i;
  33. struct vec4 clear_color;
  34. vec4_set(&clear_color, 0.3f, 0.0f, 0.0f, 1.0f);
  35. //gs_enable_depthtest(false);
  36. gs_ortho(0.0f, (float)obs->output_width,
  37. 0.0f, (float)obs->output_height,
  38. -100.0f, 100.0f);
  39. for (i = 0; i < obs->displays.num; i++) {
  40. display_t display = obs->displays.array[i];
  41. gs_load_swapchain(display->swap);
  42. gs_beginscene();
  43. gs_setviewport(0, 0, gs_getwidth(), gs_getheight());
  44. if (display->source)
  45. source_video_render(display->source);
  46. gs_endscene();
  47. gs_present();
  48. }
  49. gs_load_swapchain(NULL);
  50. gs_beginscene();
  51. gs_clear(GS_CLEAR_COLOR|GS_CLEAR_DEPTH|GS_CLEAR_STENCIL,
  52. &clear_color, 1.0f, 0);
  53. gs_setviewport(0, 0, gs_getwidth(), gs_getheight());
  54. if (obs->primary_source)
  55. source_video_render(obs->primary_source);
  56. gs_endscene();
  57. gs_present();
  58. }
  59. static bool swap_frame(obs_t obs, uint64_t timestamp)
  60. {
  61. stagesurf_t last_surface = obs->copy_surfaces[obs->cur_texture];
  62. stagesurf_t surface;
  63. struct video_frame frame;
  64. if (obs->copy_mapped) {
  65. stagesurface_unmap(last_surface);
  66. obs->copy_mapped = false;
  67. }
  68. obs->textures_copied[obs->cur_texture] = true;
  69. gs_stage_texture(last_surface, NULL);
  70. if (++obs->cur_texture == NUM_TEXTURES)
  71. obs->cur_texture = 0;
  72. if (obs->textures_copied[obs->cur_texture]) {
  73. surface = obs->copy_surfaces[obs->cur_texture];
  74. obs->copy_mapped = stagesurface_map(surface, &frame.data,
  75. &frame.row_size);
  76. if (obs->copy_mapped) {
  77. frame.timestamp = timestamp;
  78. video_output_frame(obs->video, &frame);
  79. }
  80. }
  81. return obs->copy_mapped;
  82. }
  83. void *obs_video_thread(void *param)
  84. {
  85. struct obs_data *obs = param;
  86. uint64_t last_time = 0;
  87. while (video_output_wait(obs->video)) {
  88. uint64_t cur_time = video_gettime(obs->video);
  89. tick_sources(obs, cur_time, &last_time);
  90. render_displays(obs);
  91. swap_frame(obs, cur_time);
  92. }
  93. return NULL;
  94. }