obs-video.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 <time.h>
  15. #include <stdlib.h>
  16. #include "obs.h"
  17. #include "obs-internal.h"
  18. #include "graphics/vec4.h"
  19. #include "media-io/format-conversion.h"
  20. #include "media-io/video-frame.h"
  21. #ifdef _WIN32
  22. #define WIN32_MEAN_AND_LEAN
  23. #include <windows.h>
  24. #endif
  25. static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time)
  26. {
  27. struct obs_core_data *data = &obs->data;
  28. struct obs_source *source;
  29. uint64_t delta_time;
  30. float seconds;
  31. if (!last_time)
  32. last_time = cur_time - obs->video.video_frame_interval_ns;
  33. delta_time = cur_time - last_time;
  34. seconds = (float)((double)delta_time / 1000000000.0);
  35. /* ------------------------------------- */
  36. /* call tick callbacks */
  37. pthread_mutex_lock(&data->draw_callbacks_mutex);
  38. for (size_t i = data->tick_callbacks.num; i > 0; i--) {
  39. struct tick_callback *callback;
  40. callback = data->tick_callbacks.array + (i - 1);
  41. callback->tick(callback->param, seconds);
  42. }
  43. pthread_mutex_unlock(&data->draw_callbacks_mutex);
  44. /* ------------------------------------- */
  45. /* get an array of all sources to tick */
  46. da_clear(data->sources_to_tick);
  47. pthread_mutex_lock(&data->sources_mutex);
  48. source = data->sources;
  49. while (source) {
  50. obs_source_t *s = obs_source_get_ref(source);
  51. if (s)
  52. da_push_back(data->sources_to_tick, &s);
  53. source = (struct obs_source *)source->context.hh_uuid.next;
  54. }
  55. pthread_mutex_unlock(&data->sources_mutex);
  56. /* ------------------------------------- */
  57. /* call the tick function of each source */
  58. for (size_t i = 0; i < data->sources_to_tick.num; i++) {
  59. obs_source_t *s = data->sources_to_tick.array[i];
  60. obs_source_video_tick(s, seconds);
  61. obs_source_release(s);
  62. }
  63. return cur_time;
  64. }
  65. /* in obs-display.c */
  66. extern void render_display(struct obs_display *display);
  67. static inline void render_displays(void)
  68. {
  69. struct obs_display *display;
  70. if (!obs->data.valid)
  71. return;
  72. gs_enter_context(obs->video.graphics);
  73. /* render extra displays/swaps */
  74. pthread_mutex_lock(&obs->data.displays_mutex);
  75. display = obs->data.first_display;
  76. while (display) {
  77. render_display(display);
  78. display = display->next;
  79. }
  80. pthread_mutex_unlock(&obs->data.displays_mutex);
  81. gs_leave_context();
  82. }
  83. static inline void set_render_size(uint32_t width, uint32_t height)
  84. {
  85. gs_enable_depth_test(false);
  86. gs_set_cull_mode(GS_NEITHER);
  87. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  88. gs_set_viewport(0, 0, width, height);
  89. }
  90. static inline void unmap_last_surface(struct obs_core_video_mix *video)
  91. {
  92. for (int c = 0; c < NUM_CHANNELS; ++c) {
  93. if (video->mapped_surfaces[c]) {
  94. gs_stagesurface_unmap(video->mapped_surfaces[c]);
  95. video->mapped_surfaces[c] = NULL;
  96. }
  97. }
  98. }
  99. static inline bool can_reuse_mix_texture(const struct obs_core_video_mix *mix,
  100. size_t *idx)
  101. {
  102. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  103. const struct obs_core_video_mix *other =
  104. obs->video.mixes.array[i];
  105. if (other == mix)
  106. break;
  107. if (other->view != mix->view)
  108. continue;
  109. if (other->render_space != mix->render_space)
  110. continue;
  111. if (other->ovi.base_width != mix->ovi.base_width ||
  112. other->ovi.base_height != mix->ovi.base_height)
  113. continue;
  114. if (!other->texture_rendered)
  115. continue;
  116. *idx = i;
  117. return true;
  118. }
  119. return false;
  120. }
  121. static inline void draw_mix_texture(const size_t mix_idx)
  122. {
  123. gs_texture_t *tex = obs->video.mixes.array[mix_idx]->render_texture;
  124. gs_effect_t *effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  125. gs_eparam_t *param = gs_effect_get_param_by_name(effect, "image");
  126. gs_effect_set_texture_srgb(param, tex);
  127. gs_enable_framebuffer_srgb(true);
  128. while (gs_effect_loop(effect, "Draw"))
  129. gs_draw_sprite(tex, 0, 0, 0);
  130. gs_enable_framebuffer_srgb(false);
  131. }
  132. static const char *render_main_texture_name = "render_main_texture";
  133. static inline void render_main_texture(struct obs_core_video_mix *video)
  134. {
  135. uint32_t base_width = video->ovi.base_width;
  136. uint32_t base_height = video->ovi.base_height;
  137. profile_start(render_main_texture_name);
  138. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_MAIN_TEXTURE,
  139. render_main_texture_name);
  140. struct vec4 clear_color;
  141. vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 0.0f);
  142. gs_set_render_target_with_color_space(video->render_texture, NULL,
  143. video->render_space);
  144. gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
  145. set_render_size(base_width, base_height);
  146. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  147. for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) {
  148. struct draw_callback *const callback =
  149. obs->data.draw_callbacks.array + (i - 1);
  150. callback->draw(callback->param, base_width, base_height);
  151. }
  152. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  153. /* In some cases we can reuse a previous mix's texture and save re-rendering everything */
  154. size_t reuse_idx;
  155. if (can_reuse_mix_texture(video, &reuse_idx))
  156. draw_mix_texture(reuse_idx);
  157. else
  158. obs_view_render(video->view);
  159. video->texture_rendered = true;
  160. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  161. for (size_t i = 0; i < obs->data.rendered_callbacks.num; ++i) {
  162. struct rendered_callback *const callback =
  163. &obs->data.rendered_callbacks.array[i];
  164. callback->rendered(callback->param);
  165. }
  166. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  167. GS_DEBUG_MARKER_END();
  168. profile_end(render_main_texture_name);
  169. }
  170. static inline gs_effect_t *
  171. get_scale_effect_internal(struct obs_core_video_mix *mix)
  172. {
  173. struct obs_core_video *video = &obs->video;
  174. const struct video_output_info *info =
  175. video_output_get_info(mix->video);
  176. /* if the dimension is under half the size of the original image,
  177. * bicubic/lanczos can't sample enough pixels to create an accurate
  178. * image, so use the bilinear low resolution effect instead */
  179. if (info->width < (mix->ovi.base_width / 2) &&
  180. info->height < (mix->ovi.base_height / 2)) {
  181. return video->bilinear_lowres_effect;
  182. }
  183. switch (mix->ovi.scale_type) {
  184. case OBS_SCALE_BILINEAR:
  185. return video->default_effect;
  186. case OBS_SCALE_LANCZOS:
  187. return video->lanczos_effect;
  188. case OBS_SCALE_AREA:
  189. return video->area_effect;
  190. case OBS_SCALE_BICUBIC:
  191. default:;
  192. }
  193. return video->bicubic_effect;
  194. }
  195. static inline bool resolution_close(struct obs_core_video_mix *mix,
  196. uint32_t width, uint32_t height)
  197. {
  198. long width_cmp = (long)mix->ovi.base_width - (long)width;
  199. long height_cmp = (long)mix->ovi.base_height - (long)height;
  200. return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
  201. }
  202. static inline gs_effect_t *get_scale_effect(struct obs_core_video_mix *mix,
  203. uint32_t width, uint32_t height)
  204. {
  205. struct obs_core_video *video = &obs->video;
  206. if (resolution_close(mix, width, height)) {
  207. return video->default_effect;
  208. } else {
  209. /* if the scale method couldn't be loaded, use either bicubic
  210. * or bilinear by default */
  211. gs_effect_t *effect = get_scale_effect_internal(mix);
  212. if (!effect)
  213. effect = !!video->bicubic_effect
  214. ? video->bicubic_effect
  215. : video->default_effect;
  216. return effect;
  217. }
  218. }
  219. static const char *render_output_texture_name = "render_output_texture";
  220. static inline gs_texture_t *
  221. render_output_texture(struct obs_core_video_mix *mix)
  222. {
  223. struct obs_video_info *const ovi = &mix->ovi;
  224. gs_texture_t *texture = mix->render_texture;
  225. gs_texture_t *target = mix->output_texture;
  226. const uint32_t width = gs_texture_get_width(target);
  227. const uint32_t height = gs_texture_get_height(target);
  228. if ((width == ovi->base_width) && (height == ovi->base_height))
  229. return texture;
  230. profile_start(render_output_texture_name);
  231. gs_effect_t *effect = get_scale_effect(mix, width, height);
  232. gs_technique_t *tech = gs_effect_get_technique(effect, "Draw");
  233. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  234. gs_eparam_t *bres =
  235. gs_effect_get_param_by_name(effect, "base_dimension");
  236. gs_eparam_t *bres_i =
  237. gs_effect_get_param_by_name(effect, "base_dimension_i");
  238. size_t passes, i;
  239. gs_set_render_target(target, NULL);
  240. set_render_size(width, height);
  241. if (bres) {
  242. struct vec2 base;
  243. vec2_set(&base, (float)mix->ovi.base_width,
  244. (float)mix->ovi.base_height);
  245. gs_effect_set_vec2(bres, &base);
  246. }
  247. if (bres_i) {
  248. struct vec2 base_i;
  249. vec2_set(&base_i, 1.0f / (float)mix->ovi.base_width,
  250. 1.0f / (float)mix->ovi.base_height);
  251. gs_effect_set_vec2(bres_i, &base_i);
  252. }
  253. gs_effect_set_texture_srgb(image, texture);
  254. gs_enable_framebuffer_srgb(true);
  255. gs_enable_blending(false);
  256. passes = gs_technique_begin(tech);
  257. for (i = 0; i < passes; i++) {
  258. gs_technique_begin_pass(tech, i);
  259. gs_draw_sprite(texture, 0, width, height);
  260. gs_technique_end_pass(tech);
  261. }
  262. gs_technique_end(tech);
  263. gs_enable_blending(true);
  264. gs_enable_framebuffer_srgb(false);
  265. profile_end(render_output_texture_name);
  266. return target;
  267. }
  268. static void render_convert_plane(gs_effect_t *effect, gs_texture_t *target,
  269. const char *tech_name)
  270. {
  271. gs_technique_t *tech = gs_effect_get_technique(effect, tech_name);
  272. const uint32_t width = gs_texture_get_width(target);
  273. const uint32_t height = gs_texture_get_height(target);
  274. gs_set_render_target(target, NULL);
  275. set_render_size(width, height);
  276. size_t passes = gs_technique_begin(tech);
  277. for (size_t i = 0; i < passes; i++) {
  278. gs_technique_begin_pass(tech, i);
  279. gs_draw(GS_TRIS, 0, 3);
  280. gs_technique_end_pass(tech);
  281. }
  282. gs_technique_end(tech);
  283. }
  284. static const char *render_convert_texture_name = "render_convert_texture";
  285. static void render_convert_texture(struct obs_core_video_mix *video,
  286. gs_texture_t *const *const convert_textures,
  287. gs_texture_t *texture)
  288. {
  289. profile_start(render_convert_texture_name);
  290. gs_effect_t *effect = obs->video.conversion_effect;
  291. gs_eparam_t *color_vec0 =
  292. gs_effect_get_param_by_name(effect, "color_vec0");
  293. gs_eparam_t *color_vec1 =
  294. gs_effect_get_param_by_name(effect, "color_vec1");
  295. gs_eparam_t *color_vec2 =
  296. gs_effect_get_param_by_name(effect, "color_vec2");
  297. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  298. gs_eparam_t *width_i = gs_effect_get_param_by_name(effect, "width_i");
  299. gs_eparam_t *height_i = gs_effect_get_param_by_name(effect, "height_i");
  300. gs_eparam_t *sdr_white_nits_over_maximum = gs_effect_get_param_by_name(
  301. effect, "sdr_white_nits_over_maximum");
  302. gs_eparam_t *hdr_lw = gs_effect_get_param_by_name(effect, "hdr_lw");
  303. struct vec4 vec0, vec1, vec2;
  304. vec4_set(&vec0, video->color_matrix[4], video->color_matrix[5],
  305. video->color_matrix[6], video->color_matrix[7]);
  306. vec4_set(&vec1, video->color_matrix[0], video->color_matrix[1],
  307. video->color_matrix[2], video->color_matrix[3]);
  308. vec4_set(&vec2, video->color_matrix[8], video->color_matrix[9],
  309. video->color_matrix[10], video->color_matrix[11]);
  310. gs_enable_blending(false);
  311. if (convert_textures[0]) {
  312. const float hdr_nominal_peak_level =
  313. obs->video.hdr_nominal_peak_level;
  314. const float multiplier =
  315. obs_get_video_sdr_white_level() / 10000.f;
  316. gs_effect_set_texture(image, texture);
  317. gs_effect_set_vec4(color_vec0, &vec0);
  318. gs_effect_set_float(sdr_white_nits_over_maximum, multiplier);
  319. gs_effect_set_float(hdr_lw, hdr_nominal_peak_level);
  320. render_convert_plane(effect, convert_textures[0],
  321. video->conversion_techs[0]);
  322. if (convert_textures[1]) {
  323. gs_effect_set_texture(image, texture);
  324. gs_effect_set_vec4(color_vec1, &vec1);
  325. if (!convert_textures[2])
  326. gs_effect_set_vec4(color_vec2, &vec2);
  327. gs_effect_set_float(width_i, video->conversion_width_i);
  328. gs_effect_set_float(height_i,
  329. video->conversion_height_i);
  330. gs_effect_set_float(sdr_white_nits_over_maximum,
  331. multiplier);
  332. gs_effect_set_float(hdr_lw, hdr_nominal_peak_level);
  333. render_convert_plane(effect, convert_textures[1],
  334. video->conversion_techs[1]);
  335. if (convert_textures[2]) {
  336. gs_effect_set_texture(image, texture);
  337. gs_effect_set_vec4(color_vec2, &vec2);
  338. gs_effect_set_float(width_i,
  339. video->conversion_width_i);
  340. gs_effect_set_float(height_i,
  341. video->conversion_height_i);
  342. gs_effect_set_float(sdr_white_nits_over_maximum,
  343. multiplier);
  344. gs_effect_set_float(hdr_lw,
  345. hdr_nominal_peak_level);
  346. render_convert_plane(
  347. effect, convert_textures[2],
  348. video->conversion_techs[2]);
  349. }
  350. }
  351. }
  352. gs_enable_blending(true);
  353. video->texture_converted = true;
  354. profile_end(render_convert_texture_name);
  355. }
  356. static const char *stage_output_texture_name = "stage_output_texture";
  357. static inline void
  358. stage_output_texture(struct obs_core_video_mix *video, int cur_texture,
  359. gs_texture_t *const *const convert_textures,
  360. gs_texture_t *output_texture,
  361. gs_stagesurf_t *const *const copy_surfaces,
  362. size_t channel_count)
  363. {
  364. profile_start(stage_output_texture_name);
  365. unmap_last_surface(video);
  366. if (!video->gpu_conversion) {
  367. gs_stagesurf_t *copy = copy_surfaces[0];
  368. if (copy)
  369. gs_stage_texture(copy, output_texture);
  370. video->active_copy_surfaces[cur_texture][0] = copy;
  371. for (size_t i = 1; i < NUM_CHANNELS; ++i)
  372. video->active_copy_surfaces[cur_texture][i] = NULL;
  373. video->textures_copied[cur_texture] = true;
  374. } else if (video->texture_converted) {
  375. for (size_t i = 0; i < channel_count; i++) {
  376. gs_stagesurf_t *copy = copy_surfaces[i];
  377. if (copy)
  378. gs_stage_texture(copy, convert_textures[i]);
  379. video->active_copy_surfaces[cur_texture][i] = copy;
  380. }
  381. for (size_t i = channel_count; i < NUM_CHANNELS; ++i)
  382. video->active_copy_surfaces[cur_texture][i] = NULL;
  383. video->textures_copied[cur_texture] = true;
  384. }
  385. profile_end(stage_output_texture_name);
  386. }
  387. static inline bool queue_frame(struct obs_core_video_mix *video,
  388. bool raw_active,
  389. struct obs_vframe_info *vframe_info)
  390. {
  391. bool duplicate =
  392. !video->gpu_encoder_avail_queue.size ||
  393. (video->gpu_encoder_queue.size && vframe_info->count > 1);
  394. if (duplicate) {
  395. struct obs_tex_frame *tf =
  396. deque_data(&video->gpu_encoder_queue,
  397. video->gpu_encoder_queue.size - sizeof(*tf));
  398. /* texture-based encoding is stopping */
  399. if (!tf) {
  400. return false;
  401. }
  402. tf->count++;
  403. os_sem_post(video->gpu_encode_semaphore);
  404. goto finish;
  405. }
  406. struct obs_tex_frame tf;
  407. deque_pop_front(&video->gpu_encoder_avail_queue, &tf, sizeof(tf));
  408. if (tf.released) {
  409. #ifdef _WIN32
  410. gs_texture_acquire_sync(tf.tex, tf.lock_key, GS_WAIT_INFINITE);
  411. #endif
  412. tf.released = false;
  413. }
  414. /* the vframe_info->count > 1 case causing a copy can only happen if by
  415. * some chance the very first frame has to be duplicated for whatever
  416. * reason. otherwise, it goes to the 'duplicate' case above, which
  417. * will ensure better performance. */
  418. if (raw_active || vframe_info->count > 1) {
  419. gs_copy_texture(tf.tex, video->convert_textures_encode[0]);
  420. #ifndef _WIN32
  421. /* Y and UV textures are views of the same texture on D3D, and
  422. * gs_copy_texture will copy all views of the underlying
  423. * texture. On other platforms, these are two distinct textures
  424. * that must be copied separately. */
  425. gs_copy_texture(tf.tex_uv, video->convert_textures_encode[1]);
  426. #endif
  427. } else {
  428. gs_texture_t *tex = video->convert_textures_encode[0];
  429. gs_texture_t *tex_uv = video->convert_textures_encode[1];
  430. video->convert_textures_encode[0] = tf.tex;
  431. video->convert_textures_encode[1] = tf.tex_uv;
  432. tf.tex = tex;
  433. tf.tex_uv = tex_uv;
  434. }
  435. tf.count = 1;
  436. tf.timestamp = vframe_info->timestamp;
  437. tf.released = true;
  438. #ifdef _WIN32
  439. tf.handle = gs_texture_get_shared_handle(tf.tex);
  440. gs_texture_release_sync(tf.tex, ++tf.lock_key);
  441. #endif
  442. deque_push_back(&video->gpu_encoder_queue, &tf, sizeof(tf));
  443. os_sem_post(video->gpu_encode_semaphore);
  444. finish:
  445. return --vframe_info->count;
  446. }
  447. extern void full_stop(struct obs_encoder *encoder);
  448. static inline void encode_gpu(struct obs_core_video_mix *video, bool raw_active,
  449. struct obs_vframe_info *vframe_info)
  450. {
  451. while (queue_frame(video, raw_active, vframe_info))
  452. ;
  453. }
  454. static const char *output_gpu_encoders_name = "output_gpu_encoders";
  455. static void output_gpu_encoders(struct obs_core_video_mix *video,
  456. bool raw_active)
  457. {
  458. profile_start(output_gpu_encoders_name);
  459. if (!video->texture_converted)
  460. goto end;
  461. if (!video->vframe_info_buffer_gpu.size)
  462. goto end;
  463. struct obs_vframe_info vframe_info;
  464. deque_pop_front(&video->vframe_info_buffer_gpu, &vframe_info,
  465. sizeof(vframe_info));
  466. pthread_mutex_lock(&video->gpu_encoder_mutex);
  467. encode_gpu(video, raw_active, &vframe_info);
  468. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  469. end:
  470. profile_end(output_gpu_encoders_name);
  471. }
  472. static inline void render_video(struct obs_core_video_mix *video,
  473. bool raw_active, const bool gpu_active,
  474. int cur_texture)
  475. {
  476. gs_begin_scene();
  477. gs_enable_depth_test(false);
  478. gs_set_cull_mode(GS_NEITHER);
  479. render_main_texture(video);
  480. if (raw_active || gpu_active) {
  481. gs_texture_t *const *convert_textures = video->convert_textures;
  482. gs_stagesurf_t *const *copy_surfaces =
  483. video->copy_surfaces[cur_texture];
  484. size_t channel_count = NUM_CHANNELS;
  485. gs_texture_t *output_texture = render_output_texture(video);
  486. if (gpu_active) {
  487. convert_textures = video->convert_textures_encode;
  488. #ifdef _WIN32
  489. copy_surfaces = video->copy_surfaces_encode;
  490. channel_count = 1;
  491. #endif
  492. gs_flush();
  493. }
  494. if (video->gpu_conversion) {
  495. render_convert_texture(video, convert_textures,
  496. output_texture);
  497. }
  498. if (gpu_active) {
  499. gs_flush();
  500. output_gpu_encoders(video, raw_active);
  501. }
  502. if (raw_active) {
  503. stage_output_texture(video, cur_texture,
  504. convert_textures, output_texture,
  505. copy_surfaces, channel_count);
  506. }
  507. }
  508. gs_set_render_target(NULL, NULL);
  509. gs_enable_blending(true);
  510. gs_end_scene();
  511. }
  512. static inline bool download_frame(struct obs_core_video_mix *video,
  513. int prev_texture, struct video_data *frame)
  514. {
  515. if (!video->textures_copied[prev_texture])
  516. return false;
  517. for (int channel = 0; channel < NUM_CHANNELS; ++channel) {
  518. gs_stagesurf_t *surface =
  519. video->active_copy_surfaces[prev_texture][channel];
  520. if (surface) {
  521. if (!gs_stagesurface_map(surface, &frame->data[channel],
  522. &frame->linesize[channel]))
  523. return false;
  524. video->mapped_surfaces[channel] = surface;
  525. }
  526. }
  527. return true;
  528. }
  529. static const uint8_t *set_gpu_converted_plane(uint32_t width, uint32_t height,
  530. uint32_t linesize_input,
  531. uint32_t linesize_output,
  532. const uint8_t *in, uint8_t *out)
  533. {
  534. if ((width == linesize_input) && (width == linesize_output)) {
  535. size_t total = (size_t)width * (size_t)height;
  536. memcpy(out, in, total);
  537. in += total;
  538. } else {
  539. for (size_t y = 0; y < height; y++) {
  540. memcpy(out, in, width);
  541. out += linesize_output;
  542. in += linesize_input;
  543. }
  544. }
  545. return in;
  546. }
  547. static void set_gpu_converted_data(struct video_frame *output,
  548. const struct video_data *input,
  549. const struct video_output_info *info)
  550. {
  551. switch (info->format) {
  552. case VIDEO_FORMAT_I420: {
  553. const uint32_t width = info->width;
  554. const uint32_t height = info->height;
  555. set_gpu_converted_plane(width, height, input->linesize[0],
  556. output->linesize[0], input->data[0],
  557. output->data[0]);
  558. const uint32_t width_d2 = width / 2;
  559. const uint32_t height_d2 = height / 2;
  560. set_gpu_converted_plane(width_d2, height_d2, input->linesize[1],
  561. output->linesize[1], input->data[1],
  562. output->data[1]);
  563. set_gpu_converted_plane(width_d2, height_d2, input->linesize[2],
  564. output->linesize[2], input->data[2],
  565. output->data[2]);
  566. break;
  567. }
  568. case VIDEO_FORMAT_NV12: {
  569. const uint32_t width = info->width;
  570. const uint32_t height = info->height;
  571. const uint32_t height_d2 = height / 2;
  572. if (input->linesize[1]) {
  573. set_gpu_converted_plane(width, height,
  574. input->linesize[0],
  575. output->linesize[0],
  576. input->data[0],
  577. output->data[0]);
  578. set_gpu_converted_plane(width, height_d2,
  579. input->linesize[1],
  580. output->linesize[1],
  581. input->data[1],
  582. output->data[1]);
  583. } else {
  584. const uint8_t *const in_uv = set_gpu_converted_plane(
  585. width, height, input->linesize[0],
  586. output->linesize[0], input->data[0],
  587. output->data[0]);
  588. set_gpu_converted_plane(width, height_d2,
  589. input->linesize[0],
  590. output->linesize[1], in_uv,
  591. output->data[1]);
  592. }
  593. break;
  594. }
  595. case VIDEO_FORMAT_I444: {
  596. const uint32_t width = info->width;
  597. const uint32_t height = info->height;
  598. set_gpu_converted_plane(width, height, input->linesize[0],
  599. output->linesize[0], input->data[0],
  600. output->data[0]);
  601. set_gpu_converted_plane(width, height, input->linesize[1],
  602. output->linesize[1], input->data[1],
  603. output->data[1]);
  604. set_gpu_converted_plane(width, height, input->linesize[2],
  605. output->linesize[2], input->data[2],
  606. output->data[2]);
  607. break;
  608. }
  609. case VIDEO_FORMAT_I010: {
  610. const uint32_t width = info->width;
  611. const uint32_t height = info->height;
  612. set_gpu_converted_plane(width * 2, height, input->linesize[0],
  613. output->linesize[0], input->data[0],
  614. output->data[0]);
  615. const uint32_t height_d2 = height / 2;
  616. set_gpu_converted_plane(width, height_d2, input->linesize[1],
  617. output->linesize[1], input->data[1],
  618. output->data[1]);
  619. set_gpu_converted_plane(width, height_d2, input->linesize[2],
  620. output->linesize[2], input->data[2],
  621. output->data[2]);
  622. break;
  623. }
  624. case VIDEO_FORMAT_P010: {
  625. const uint32_t width_x2 = info->width * 2;
  626. const uint32_t height = info->height;
  627. const uint32_t height_d2 = height / 2;
  628. if (input->linesize[1]) {
  629. set_gpu_converted_plane(width_x2, height,
  630. input->linesize[0],
  631. output->linesize[0],
  632. input->data[0],
  633. output->data[0]);
  634. set_gpu_converted_plane(width_x2, height_d2,
  635. input->linesize[1],
  636. output->linesize[1],
  637. input->data[1],
  638. output->data[1]);
  639. } else {
  640. const uint8_t *const in_uv = set_gpu_converted_plane(
  641. width_x2, height, input->linesize[0],
  642. output->linesize[0], input->data[0],
  643. output->data[0]);
  644. set_gpu_converted_plane(width_x2, height_d2,
  645. input->linesize[0],
  646. output->linesize[1], in_uv,
  647. output->data[1]);
  648. }
  649. break;
  650. }
  651. case VIDEO_FORMAT_P216: {
  652. const uint32_t width_x2 = info->width * 2;
  653. const uint32_t height = info->height;
  654. set_gpu_converted_plane(width_x2, height, input->linesize[0],
  655. output->linesize[0], input->data[0],
  656. output->data[0]);
  657. set_gpu_converted_plane(width_x2, height, input->linesize[1],
  658. output->linesize[1], input->data[1],
  659. output->data[1]);
  660. break;
  661. }
  662. case VIDEO_FORMAT_P416: {
  663. const uint32_t height = info->height;
  664. set_gpu_converted_plane(info->width * 2, height,
  665. input->linesize[0], output->linesize[0],
  666. input->data[0], output->data[0]);
  667. set_gpu_converted_plane(info->width * 4, height,
  668. input->linesize[1], output->linesize[1],
  669. input->data[1], output->data[1]);
  670. break;
  671. }
  672. case VIDEO_FORMAT_NONE:
  673. case VIDEO_FORMAT_YVYU:
  674. case VIDEO_FORMAT_YUY2:
  675. case VIDEO_FORMAT_UYVY:
  676. case VIDEO_FORMAT_RGBA:
  677. case VIDEO_FORMAT_BGRA:
  678. case VIDEO_FORMAT_BGRX:
  679. case VIDEO_FORMAT_Y800:
  680. case VIDEO_FORMAT_BGR3:
  681. case VIDEO_FORMAT_I412:
  682. case VIDEO_FORMAT_I422:
  683. case VIDEO_FORMAT_I210:
  684. case VIDEO_FORMAT_I40A:
  685. case VIDEO_FORMAT_I42A:
  686. case VIDEO_FORMAT_YUVA:
  687. case VIDEO_FORMAT_YA2L:
  688. case VIDEO_FORMAT_AYUV:
  689. case VIDEO_FORMAT_V210:
  690. case VIDEO_FORMAT_R10L:
  691. /* unimplemented */
  692. ;
  693. }
  694. }
  695. static inline void copy_rgbx_frame(struct video_frame *output,
  696. const struct video_data *input,
  697. const struct video_output_info *info)
  698. {
  699. uint8_t *in_ptr = input->data[0];
  700. uint8_t *out_ptr = output->data[0];
  701. /* if the line sizes match, do a single copy */
  702. if (input->linesize[0] == output->linesize[0]) {
  703. memcpy(out_ptr, in_ptr,
  704. (size_t)input->linesize[0] * (size_t)info->height);
  705. } else {
  706. const size_t copy_size = (size_t)info->width * 4;
  707. for (size_t y = 0; y < info->height; y++) {
  708. memcpy(out_ptr, in_ptr, copy_size);
  709. in_ptr += input->linesize[0];
  710. out_ptr += output->linesize[0];
  711. }
  712. }
  713. }
  714. static inline void output_video_data(struct obs_core_video_mix *video,
  715. struct video_data *input_frame, int count)
  716. {
  717. const struct video_output_info *info;
  718. struct video_frame output_frame;
  719. bool locked;
  720. info = video_output_get_info(video->video);
  721. locked = video_output_lock_frame(video->video, &output_frame, count,
  722. input_frame->timestamp);
  723. if (locked) {
  724. if (video->gpu_conversion) {
  725. set_gpu_converted_data(&output_frame, input_frame,
  726. info);
  727. } else {
  728. copy_rgbx_frame(&output_frame, input_frame, info);
  729. }
  730. video_output_unlock_frame(video->video);
  731. }
  732. }
  733. void add_ready_encoder_group(obs_encoder_t *encoder)
  734. {
  735. obs_weak_encoder_t *weak = obs_encoder_get_weak_encoder(encoder);
  736. pthread_mutex_lock(&obs->video.encoder_group_mutex);
  737. da_push_back(obs->video.ready_encoder_groups, &weak);
  738. pthread_mutex_unlock(&obs->video.encoder_group_mutex);
  739. }
  740. static inline void video_sleep(struct obs_core_video *video, uint64_t *p_time,
  741. uint64_t interval_ns)
  742. {
  743. struct obs_vframe_info vframe_info;
  744. uint64_t cur_time = *p_time;
  745. uint64_t t = cur_time + interval_ns;
  746. int count;
  747. if (os_sleepto_ns(t)) {
  748. *p_time = t;
  749. count = 1;
  750. } else {
  751. const uint64_t udiff = os_gettime_ns() - cur_time;
  752. int64_t diff;
  753. memcpy(&diff, &udiff, sizeof(diff));
  754. const uint64_t clamped_diff = (diff > (int64_t)interval_ns)
  755. ? (uint64_t)diff
  756. : interval_ns;
  757. count = (int)(clamped_diff / interval_ns);
  758. *p_time = cur_time + interval_ns * count;
  759. }
  760. video->total_frames += count;
  761. video->lagged_frames += count - 1;
  762. vframe_info.timestamp = cur_time;
  763. vframe_info.count = count;
  764. pthread_mutex_lock(&video->encoder_group_mutex);
  765. for (size_t i = 0; i < video->ready_encoder_groups.num; i++) {
  766. obs_encoder_t *encoder = obs_weak_encoder_get_encoder(
  767. video->ready_encoder_groups.array[i]);
  768. obs_weak_encoder_release(video->ready_encoder_groups.array[i]);
  769. if (!encoder)
  770. continue;
  771. if (encoder->encoder_group) {
  772. struct encoder_group *group = encoder->encoder_group;
  773. pthread_mutex_lock(&group->mutex);
  774. if (group->encoders_added == group->encoders_started &&
  775. !group->start_timestamp) {
  776. group->start_timestamp = *p_time;
  777. }
  778. pthread_mutex_unlock(&group->mutex);
  779. }
  780. obs_encoder_release(encoder);
  781. }
  782. da_clear(video->ready_encoder_groups);
  783. pthread_mutex_unlock(&video->encoder_group_mutex);
  784. pthread_mutex_lock(&obs->video.mixes_mutex);
  785. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  786. struct obs_core_video_mix *video = obs->video.mixes.array[i];
  787. bool raw_active = video->raw_was_active;
  788. bool gpu_active = video->gpu_was_active;
  789. if (raw_active)
  790. deque_push_back(&video->vframe_info_buffer,
  791. &vframe_info, sizeof(vframe_info));
  792. if (gpu_active)
  793. deque_push_back(&video->vframe_info_buffer_gpu,
  794. &vframe_info, sizeof(vframe_info));
  795. }
  796. pthread_mutex_unlock(&obs->video.mixes_mutex);
  797. }
  798. static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
  799. static const char *output_frame_render_video_name = "render_video";
  800. static const char *output_frame_download_frame_name = "download_frame";
  801. static const char *output_frame_gs_flush_name = "gs_flush";
  802. static const char *output_frame_output_video_data_name = "output_video_data";
  803. static inline void output_frame(struct obs_core_video_mix *video)
  804. {
  805. const bool raw_active = video->raw_was_active;
  806. const bool gpu_active = video->gpu_was_active;
  807. int cur_texture = video->cur_texture;
  808. int prev_texture = cur_texture == 0 ? NUM_TEXTURES - 1
  809. : cur_texture - 1;
  810. struct video_data frame;
  811. bool frame_ready = 0;
  812. memset(&frame, 0, sizeof(struct video_data));
  813. profile_start(output_frame_gs_context_name);
  814. gs_enter_context(obs->video.graphics);
  815. profile_start(output_frame_render_video_name);
  816. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_RENDER_VIDEO,
  817. output_frame_render_video_name);
  818. render_video(video, raw_active, gpu_active, cur_texture);
  819. GS_DEBUG_MARKER_END();
  820. profile_end(output_frame_render_video_name);
  821. if (raw_active) {
  822. profile_start(output_frame_download_frame_name);
  823. frame_ready = download_frame(video, prev_texture, &frame);
  824. profile_end(output_frame_download_frame_name);
  825. }
  826. profile_start(output_frame_gs_flush_name);
  827. gs_flush();
  828. profile_end(output_frame_gs_flush_name);
  829. gs_leave_context();
  830. profile_end(output_frame_gs_context_name);
  831. if (raw_active && frame_ready) {
  832. struct obs_vframe_info vframe_info;
  833. deque_pop_front(&video->vframe_info_buffer, &vframe_info,
  834. sizeof(vframe_info));
  835. frame.timestamp = vframe_info.timestamp;
  836. profile_start(output_frame_output_video_data_name);
  837. output_video_data(video, &frame, vframe_info.count);
  838. profile_end(output_frame_output_video_data_name);
  839. }
  840. if (++video->cur_texture == NUM_TEXTURES)
  841. video->cur_texture = 0;
  842. }
  843. static inline void output_frames(void)
  844. {
  845. pthread_mutex_lock(&obs->video.mixes_mutex);
  846. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  847. struct obs_core_video_mix *mix = obs->video.mixes.array[i];
  848. if (mix->view) {
  849. output_frame(mix);
  850. } else {
  851. obs->video.mixes.array[i] = NULL;
  852. obs_free_video_mix(mix);
  853. da_erase(obs->video.mixes, i);
  854. i--;
  855. num--;
  856. }
  857. }
  858. pthread_mutex_unlock(&obs->video.mixes_mutex);
  859. }
  860. #define NBSP "\xC2\xA0"
  861. static void clear_base_frame_data(struct obs_core_video_mix *video)
  862. {
  863. video->texture_rendered = false;
  864. video->texture_converted = false;
  865. deque_free(&video->vframe_info_buffer);
  866. video->cur_texture = 0;
  867. }
  868. static void clear_raw_frame_data(struct obs_core_video_mix *video)
  869. {
  870. memset(video->textures_copied, 0, sizeof(video->textures_copied));
  871. deque_free(&video->vframe_info_buffer);
  872. }
  873. static void clear_gpu_frame_data(struct obs_core_video_mix *video)
  874. {
  875. deque_free(&video->vframe_info_buffer_gpu);
  876. }
  877. extern THREAD_LOCAL bool is_graphics_thread;
  878. static void execute_graphics_tasks(void)
  879. {
  880. struct obs_core_video *video = &obs->video;
  881. bool tasks_remaining = true;
  882. while (tasks_remaining) {
  883. pthread_mutex_lock(&video->task_mutex);
  884. if (video->tasks.size) {
  885. struct obs_task_info info;
  886. deque_pop_front(&video->tasks, &info, sizeof(info));
  887. info.task(info.param);
  888. }
  889. tasks_remaining = !!video->tasks.size;
  890. pthread_mutex_unlock(&video->task_mutex);
  891. }
  892. }
  893. #ifdef _WIN32
  894. struct winrt_exports {
  895. void (*winrt_initialize)();
  896. void (*winrt_uninitialize)();
  897. struct winrt_disaptcher *(*winrt_dispatcher_init)();
  898. void (*winrt_dispatcher_free)(struct winrt_disaptcher *dispatcher);
  899. void (*winrt_capture_thread_start)();
  900. void (*winrt_capture_thread_stop)();
  901. };
  902. #define WINRT_IMPORT(func) \
  903. do { \
  904. exports->func = os_dlsym(module, #func); \
  905. if (!exports->func) { \
  906. success = false; \
  907. blog(LOG_ERROR, \
  908. "Could not load function '%s' from " \
  909. "module '%s'", \
  910. #func, module_name); \
  911. } \
  912. } while (false)
  913. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  914. const char *module_name)
  915. {
  916. bool success = true;
  917. WINRT_IMPORT(winrt_initialize);
  918. WINRT_IMPORT(winrt_uninitialize);
  919. WINRT_IMPORT(winrt_dispatcher_init);
  920. WINRT_IMPORT(winrt_dispatcher_free);
  921. WINRT_IMPORT(winrt_capture_thread_start);
  922. WINRT_IMPORT(winrt_capture_thread_stop);
  923. return success;
  924. }
  925. struct winrt_state {
  926. bool loaded;
  927. void *winrt_module;
  928. struct winrt_exports exports;
  929. struct winrt_disaptcher *dispatcher;
  930. };
  931. static void init_winrt_state(struct winrt_state *winrt)
  932. {
  933. static const char *const module_name = "libobs-winrt";
  934. winrt->winrt_module = os_dlopen(module_name);
  935. winrt->loaded = winrt->winrt_module &&
  936. load_winrt_imports(&winrt->exports, winrt->winrt_module,
  937. module_name);
  938. winrt->dispatcher = NULL;
  939. if (winrt->loaded) {
  940. winrt->exports.winrt_initialize();
  941. winrt->dispatcher = winrt->exports.winrt_dispatcher_init();
  942. gs_enter_context(obs->video.graphics);
  943. winrt->exports.winrt_capture_thread_start();
  944. gs_leave_context();
  945. }
  946. }
  947. static void uninit_winrt_state(struct winrt_state *winrt)
  948. {
  949. if (winrt->winrt_module) {
  950. if (winrt->loaded) {
  951. winrt->exports.winrt_capture_thread_stop();
  952. if (winrt->dispatcher)
  953. winrt->exports.winrt_dispatcher_free(
  954. winrt->dispatcher);
  955. winrt->exports.winrt_uninitialize();
  956. }
  957. os_dlclose(winrt->winrt_module);
  958. }
  959. }
  960. #endif // #ifdef _WIN32
  961. static const char *tick_sources_name = "tick_sources";
  962. static const char *render_displays_name = "render_displays";
  963. static const char *output_frame_name = "output_frame";
  964. static inline void update_active_state(struct obs_core_video_mix *video)
  965. {
  966. const bool raw_was_active = video->raw_was_active;
  967. const bool gpu_was_active = video->gpu_was_active;
  968. const bool was_active = video->was_active;
  969. bool raw_active = os_atomic_load_long(&video->raw_active) > 0;
  970. const bool gpu_active =
  971. os_atomic_load_long(&video->gpu_encoder_active) > 0;
  972. const bool active = raw_active || gpu_active;
  973. if (!was_active && active)
  974. clear_base_frame_data(video);
  975. if (!raw_was_active && raw_active)
  976. clear_raw_frame_data(video);
  977. if (!gpu_was_active && gpu_active)
  978. clear_gpu_frame_data(video);
  979. video->gpu_was_active = gpu_active;
  980. video->raw_was_active = raw_active;
  981. video->was_active = active;
  982. }
  983. static inline void update_active_states(void)
  984. {
  985. pthread_mutex_lock(&obs->video.mixes_mutex);
  986. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
  987. update_active_state(obs->video.mixes.array[i]);
  988. pthread_mutex_unlock(&obs->video.mixes_mutex);
  989. }
  990. static inline bool stop_requested(void)
  991. {
  992. bool success = true;
  993. pthread_mutex_lock(&obs->video.mixes_mutex);
  994. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
  995. if (!video_output_stopped(obs->video.mixes.array[i]->video))
  996. success = false;
  997. pthread_mutex_unlock(&obs->video.mixes_mutex);
  998. return success;
  999. }
  1000. bool obs_graphics_thread_loop(struct obs_graphics_context *context)
  1001. {
  1002. uint64_t frame_start = os_gettime_ns();
  1003. uint64_t frame_time_ns;
  1004. update_active_states();
  1005. profile_start(context->video_thread_name);
  1006. gs_enter_context(obs->video.graphics);
  1007. gs_begin_frame();
  1008. gs_leave_context();
  1009. profile_start(tick_sources_name);
  1010. context->last_time =
  1011. tick_sources(obs->video.video_time, context->last_time);
  1012. profile_end(tick_sources_name);
  1013. #ifdef _WIN32
  1014. MSG msg;
  1015. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  1016. TranslateMessage(&msg);
  1017. DispatchMessage(&msg);
  1018. }
  1019. #endif
  1020. profile_start(output_frame_name);
  1021. output_frames();
  1022. profile_end(output_frame_name);
  1023. profile_start(render_displays_name);
  1024. render_displays();
  1025. profile_end(render_displays_name);
  1026. execute_graphics_tasks();
  1027. frame_time_ns = os_gettime_ns() - frame_start;
  1028. profile_end(context->video_thread_name);
  1029. profile_reenable_thread();
  1030. video_sleep(&obs->video, &obs->video.video_time, context->interval);
  1031. context->frame_time_total_ns += frame_time_ns;
  1032. context->fps_total_ns += (obs->video.video_time - context->last_time);
  1033. context->fps_total_frames++;
  1034. if (context->fps_total_ns >= 1000000000ULL) {
  1035. obs->video.video_fps =
  1036. (double)context->fps_total_frames /
  1037. ((double)context->fps_total_ns / 1000000000.0);
  1038. obs->video.video_avg_frame_time_ns =
  1039. context->frame_time_total_ns /
  1040. (uint64_t)context->fps_total_frames;
  1041. context->frame_time_total_ns = 0;
  1042. context->fps_total_ns = 0;
  1043. context->fps_total_frames = 0;
  1044. }
  1045. return !stop_requested();
  1046. }
  1047. void *obs_graphics_thread(void *param)
  1048. {
  1049. #ifdef _WIN32
  1050. struct winrt_state winrt;
  1051. init_winrt_state(&winrt);
  1052. #endif // #ifdef _WIN32
  1053. is_graphics_thread = true;
  1054. const uint64_t interval = obs->video.video_frame_interval_ns;
  1055. obs->video.video_time = os_gettime_ns();
  1056. os_set_thread_name("libobs: graphics thread");
  1057. const char *video_thread_name = profile_store_name(
  1058. obs_get_profiler_name_store(),
  1059. "obs_graphics_thread(%g" NBSP "ms)", interval / 1000000.);
  1060. profile_register_root(video_thread_name, interval);
  1061. srand((unsigned int)time(NULL));
  1062. struct obs_graphics_context context;
  1063. context.interval = interval;
  1064. context.frame_time_total_ns = 0;
  1065. context.fps_total_ns = 0;
  1066. context.fps_total_frames = 0;
  1067. context.last_time = 0;
  1068. context.video_thread_name = video_thread_name;
  1069. #ifdef __APPLE__
  1070. while (obs_graphics_thread_loop_autorelease(&context))
  1071. #else
  1072. while (obs_graphics_thread_loop(&context))
  1073. #endif
  1074. ;
  1075. #ifdef _WIN32
  1076. uninit_winrt_state(&winrt);
  1077. #endif
  1078. UNUSED_PARAMETER(param);
  1079. return NULL;
  1080. }