obs-video.c 37 KB

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