window-capture.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. #include <stdlib.h>
  2. #include <util/dstr.h>
  3. #include <util/threading.h>
  4. #include <util/windows/window-helpers.h>
  5. #include "dc-capture.h"
  6. #include "../../libobs/util/platform.h"
  7. #include "../../libobs-winrt/winrt-capture.h"
  8. /* clang-format off */
  9. #define TEXT_WINDOW_CAPTURE obs_module_text("WindowCapture")
  10. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  11. #define TEXT_METHOD obs_module_text("WindowCapture.Method")
  12. #define TEXT_METHOD_AUTO obs_module_text("WindowCapture.Method.Auto")
  13. #define TEXT_METHOD_BITBLT obs_module_text("WindowCapture.Method.BitBlt")
  14. #define TEXT_METHOD_WGC obs_module_text("WindowCapture.Method.WindowsGraphicsCapture")
  15. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  16. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  17. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  18. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  19. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  20. #define TEXT_COMPATIBILITY obs_module_text("Compatibility")
  21. #define TEXT_CLIENT_AREA obs_module_text("ClientArea")
  22. /* clang-format on */
  23. #define WC_CHECK_TIMER 1.0f
  24. #define RESIZE_CHECK_TIME 0.2f
  25. #define CURSOR_CHECK_TIME 0.2f
  26. typedef BOOL (*PFN_winrt_capture_supported)();
  27. typedef BOOL (*PFN_winrt_capture_cursor_toggle_supported)();
  28. typedef struct winrt_capture *(*PFN_winrt_capture_init_window)(
  29. BOOL cursor, HWND window, BOOL client_area);
  30. typedef void (*PFN_winrt_capture_free)(struct winrt_capture *capture);
  31. typedef BOOL (*PFN_winrt_capture_active)(const struct winrt_capture *capture);
  32. typedef BOOL (*PFN_winrt_capture_show_cursor)(struct winrt_capture *capture,
  33. BOOL visible);
  34. typedef enum gs_color_space (*PFN_winrt_capture_get_color_space)(
  35. const struct winrt_capture *capture);
  36. typedef void (*PFN_winrt_capture_render)(struct winrt_capture *capture);
  37. typedef uint32_t (*PFN_winrt_capture_width)(const struct winrt_capture *capture);
  38. typedef uint32_t (*PFN_winrt_capture_height)(
  39. const struct winrt_capture *capture);
  40. struct winrt_exports {
  41. PFN_winrt_capture_supported winrt_capture_supported;
  42. PFN_winrt_capture_cursor_toggle_supported
  43. winrt_capture_cursor_toggle_supported;
  44. PFN_winrt_capture_init_window winrt_capture_init_window;
  45. PFN_winrt_capture_free winrt_capture_free;
  46. PFN_winrt_capture_active winrt_capture_active;
  47. PFN_winrt_capture_show_cursor winrt_capture_show_cursor;
  48. PFN_winrt_capture_get_color_space winrt_capture_get_color_space;
  49. PFN_winrt_capture_render winrt_capture_render;
  50. PFN_winrt_capture_width winrt_capture_width;
  51. PFN_winrt_capture_height winrt_capture_height;
  52. };
  53. enum window_capture_method {
  54. METHOD_AUTO,
  55. METHOD_BITBLT,
  56. METHOD_WGC,
  57. };
  58. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_SetThreadDpiAwarenessContext)(
  59. DPI_AWARENESS_CONTEXT);
  60. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetThreadDpiAwarenessContext)(VOID);
  61. typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetWindowDpiAwarenessContext)(HWND);
  62. struct window_capture {
  63. obs_source_t *source;
  64. pthread_mutex_t update_mutex;
  65. char *title;
  66. char *class;
  67. char *executable;
  68. enum window_capture_method method;
  69. enum window_priority priority;
  70. bool cursor;
  71. bool compatibility;
  72. bool client_area;
  73. bool use_wildcards; /* TODO */
  74. struct dc_capture capture;
  75. bool previously_failed;
  76. void *winrt_module;
  77. struct winrt_exports exports;
  78. struct winrt_capture *capture_winrt;
  79. float resize_timer;
  80. float check_window_timer;
  81. float cursor_check_time;
  82. HWND window;
  83. RECT last_rect;
  84. PFN_SetThreadDpiAwarenessContext set_thread_dpi_awareness_context;
  85. PFN_GetThreadDpiAwarenessContext get_thread_dpi_awareness_context;
  86. PFN_GetWindowDpiAwarenessContext get_window_dpi_awareness_context;
  87. };
  88. static const char *wgc_partial_match_classes[] = {
  89. "Chrome",
  90. "Mozilla",
  91. NULL,
  92. };
  93. static const char *wgc_whole_match_classes[] = {
  94. "ApplicationFrameWindow",
  95. "Windows.UI.Core.CoreWindow",
  96. "XLMAIN", /* excel*/
  97. "PPTFrameClass", /* powerpoint */
  98. "OpusApp", /* word */
  99. NULL,
  100. };
  101. static enum window_capture_method
  102. choose_method(enum window_capture_method method, bool wgc_supported,
  103. const char *current_class)
  104. {
  105. if (!wgc_supported)
  106. return METHOD_BITBLT;
  107. if (method != METHOD_AUTO)
  108. return method;
  109. if (!current_class)
  110. return METHOD_BITBLT;
  111. const char **class = wgc_partial_match_classes;
  112. while (*class) {
  113. if (astrstri(current_class, *class) != NULL) {
  114. return METHOD_WGC;
  115. }
  116. class ++;
  117. }
  118. class = wgc_whole_match_classes;
  119. while (*class) {
  120. if (astrcmpi(current_class, *class) == 0) {
  121. return METHOD_WGC;
  122. }
  123. class ++;
  124. }
  125. return METHOD_BITBLT;
  126. }
  127. static const char *get_method_name(int method)
  128. {
  129. const char *method_name = "";
  130. switch (method) {
  131. case METHOD_AUTO:
  132. method_name = "Automatic";
  133. break;
  134. case METHOD_BITBLT:
  135. method_name = "BitBlt";
  136. break;
  137. case METHOD_WGC:
  138. method_name = "WGC";
  139. break;
  140. }
  141. return method_name;
  142. }
  143. static void log_settings(struct window_capture *wc, obs_data_t *s)
  144. {
  145. int method = (int)obs_data_get_int(s, "method");
  146. if (wc->title != NULL) {
  147. blog(LOG_INFO,
  148. "[window-capture: '%s'] update settings:\n"
  149. "\texecutable: %s\n"
  150. "\tmethod selected: %s\n"
  151. "\tmethod chosen: %s\n",
  152. obs_source_get_name(wc->source), wc->executable,
  153. get_method_name(method), get_method_name(wc->method));
  154. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  155. }
  156. }
  157. extern bool wgc_supported;
  158. static void update_settings(struct window_capture *wc, obs_data_t *s)
  159. {
  160. pthread_mutex_lock(&wc->update_mutex);
  161. int method = (int)obs_data_get_int(s, "method");
  162. const char *window = obs_data_get_string(s, "window");
  163. int priority = (int)obs_data_get_int(s, "priority");
  164. bfree(wc->title);
  165. bfree(wc->class);
  166. bfree(wc->executable);
  167. ms_build_window_strings(window, &wc->class, &wc->title,
  168. &wc->executable);
  169. wc->method = choose_method(method, wgc_supported, wc->class);
  170. wc->priority = (enum window_priority)priority;
  171. wc->cursor = obs_data_get_bool(s, "cursor");
  172. wc->use_wildcards = obs_data_get_bool(s, "use_wildcards");
  173. wc->compatibility = obs_data_get_bool(s, "compatibility");
  174. wc->client_area = obs_data_get_bool(s, "client_area");
  175. pthread_mutex_unlock(&wc->update_mutex);
  176. }
  177. /* ------------------------------------------------------------------------- */
  178. static const char *wc_getname(void *unused)
  179. {
  180. UNUSED_PARAMETER(unused);
  181. return TEXT_WINDOW_CAPTURE;
  182. }
  183. #define WINRT_IMPORT(func) \
  184. do { \
  185. exports->func = (PFN_##func)os_dlsym(module, #func); \
  186. if (!exports->func) { \
  187. success = false; \
  188. blog(LOG_ERROR, \
  189. "Could not load function '%s' from " \
  190. "module '%s'", \
  191. #func, module_name); \
  192. } \
  193. } while (false)
  194. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  195. const char *module_name)
  196. {
  197. bool success = true;
  198. WINRT_IMPORT(winrt_capture_supported);
  199. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  200. WINRT_IMPORT(winrt_capture_init_window);
  201. WINRT_IMPORT(winrt_capture_free);
  202. WINRT_IMPORT(winrt_capture_active);
  203. WINRT_IMPORT(winrt_capture_show_cursor);
  204. WINRT_IMPORT(winrt_capture_get_color_space);
  205. WINRT_IMPORT(winrt_capture_render);
  206. WINRT_IMPORT(winrt_capture_width);
  207. WINRT_IMPORT(winrt_capture_height);
  208. return success;
  209. }
  210. extern bool graphics_uses_d3d11;
  211. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  212. {
  213. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  214. wc->source = source;
  215. pthread_mutex_init(&wc->update_mutex, NULL);
  216. if (graphics_uses_d3d11) {
  217. static const char *const module = "libobs-winrt";
  218. wc->winrt_module = os_dlopen(module);
  219. if (wc->winrt_module) {
  220. load_winrt_imports(&wc->exports, wc->winrt_module,
  221. module);
  222. }
  223. }
  224. const HMODULE hModuleUser32 = GetModuleHandle(L"User32.dll");
  225. if (hModuleUser32) {
  226. PFN_SetThreadDpiAwarenessContext
  227. set_thread_dpi_awareness_context =
  228. (PFN_SetThreadDpiAwarenessContext)GetProcAddress(
  229. hModuleUser32,
  230. "SetThreadDpiAwarenessContext");
  231. PFN_GetThreadDpiAwarenessContext
  232. get_thread_dpi_awareness_context =
  233. (PFN_GetThreadDpiAwarenessContext)GetProcAddress(
  234. hModuleUser32,
  235. "GetThreadDpiAwarenessContext");
  236. PFN_GetWindowDpiAwarenessContext
  237. get_window_dpi_awareness_context =
  238. (PFN_GetWindowDpiAwarenessContext)GetProcAddress(
  239. hModuleUser32,
  240. "GetWindowDpiAwarenessContext");
  241. if (set_thread_dpi_awareness_context &&
  242. get_thread_dpi_awareness_context &&
  243. get_window_dpi_awareness_context) {
  244. wc->set_thread_dpi_awareness_context =
  245. set_thread_dpi_awareness_context;
  246. wc->get_thread_dpi_awareness_context =
  247. get_thread_dpi_awareness_context;
  248. wc->get_window_dpi_awareness_context =
  249. get_window_dpi_awareness_context;
  250. }
  251. }
  252. update_settings(wc, settings);
  253. log_settings(wc, settings);
  254. return wc;
  255. }
  256. static void wc_actual_destroy(void *data)
  257. {
  258. struct window_capture *wc = data;
  259. if (wc->capture_winrt) {
  260. wc->exports.winrt_capture_free(wc->capture_winrt);
  261. }
  262. obs_enter_graphics();
  263. dc_capture_free(&wc->capture);
  264. obs_leave_graphics();
  265. bfree(wc->title);
  266. bfree(wc->class);
  267. bfree(wc->executable);
  268. if (wc->winrt_module)
  269. os_dlclose(wc->winrt_module);
  270. pthread_mutex_destroy(&wc->update_mutex);
  271. bfree(wc);
  272. }
  273. static void wc_destroy(void *data)
  274. {
  275. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  276. }
  277. static void force_reset(struct window_capture *wc)
  278. {
  279. wc->window = NULL;
  280. wc->resize_timer = RESIZE_CHECK_TIME;
  281. wc->check_window_timer = WC_CHECK_TIMER;
  282. wc->cursor_check_time = CURSOR_CHECK_TIME;
  283. wc->previously_failed = false;
  284. }
  285. static void wc_update(void *data, obs_data_t *settings)
  286. {
  287. struct window_capture *wc = data;
  288. update_settings(wc, settings);
  289. log_settings(wc, settings);
  290. force_reset(wc);
  291. }
  292. static uint32_t wc_width(void *data)
  293. {
  294. struct window_capture *wc = data;
  295. return (wc->method == METHOD_WGC)
  296. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  297. : wc->capture.width;
  298. }
  299. static uint32_t wc_height(void *data)
  300. {
  301. struct window_capture *wc = data;
  302. return (wc->method == METHOD_WGC)
  303. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  304. : wc->capture.height;
  305. }
  306. static void wc_defaults(obs_data_t *defaults)
  307. {
  308. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  309. obs_data_set_default_bool(defaults, "cursor", true);
  310. obs_data_set_default_bool(defaults, "compatibility", false);
  311. obs_data_set_default_bool(defaults, "client_area", true);
  312. }
  313. static void update_settings_visibility(obs_properties_t *props,
  314. struct window_capture *wc)
  315. {
  316. pthread_mutex_lock(&wc->update_mutex);
  317. const enum window_capture_method method = wc->method;
  318. const bool bitblt_options = method == METHOD_BITBLT;
  319. const bool wgc_options = method == METHOD_WGC;
  320. const bool wgc_cursor_toggle =
  321. wgc_options &&
  322. wc->exports.winrt_capture_cursor_toggle_supported();
  323. obs_property_t *p = obs_properties_get(props, "cursor");
  324. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  325. p = obs_properties_get(props, "compatibility");
  326. obs_property_set_visible(p, bitblt_options);
  327. p = obs_properties_get(props, "client_area");
  328. obs_property_set_visible(p, wgc_options);
  329. pthread_mutex_unlock(&wc->update_mutex);
  330. }
  331. static bool wc_capture_method_changed(obs_properties_t *props,
  332. obs_property_t *p, obs_data_t *settings)
  333. {
  334. UNUSED_PARAMETER(p);
  335. struct window_capture *wc = obs_properties_get_param(props);
  336. if (!wc)
  337. return false;
  338. update_settings(wc, settings);
  339. update_settings_visibility(props, wc);
  340. return true;
  341. }
  342. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  343. obs_data_t *settings)
  344. {
  345. struct window_capture *wc = obs_properties_get_param(props);
  346. if (!wc)
  347. return false;
  348. update_settings(wc, settings);
  349. update_settings_visibility(props, wc);
  350. ms_check_window_property_setting(props, p, settings, "window", 0);
  351. return true;
  352. }
  353. static obs_properties_t *wc_properties(void *data)
  354. {
  355. struct window_capture *wc = data;
  356. obs_properties_t *ppts = obs_properties_create();
  357. obs_properties_set_param(ppts, wc, NULL);
  358. obs_property_t *p;
  359. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  360. OBS_COMBO_TYPE_LIST,
  361. OBS_COMBO_FORMAT_STRING);
  362. ms_fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  363. obs_property_set_modified_callback(p, wc_window_changed);
  364. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  365. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  366. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  367. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  368. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  369. obs_property_list_item_disable(p, 2, !wgc_supported);
  370. obs_property_set_modified_callback(p, wc_capture_method_changed);
  371. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  372. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  373. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  374. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  375. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  376. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  377. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  378. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  379. return ppts;
  380. }
  381. static void wc_hide(void *data)
  382. {
  383. struct window_capture *wc = data;
  384. if (wc->capture_winrt) {
  385. wc->exports.winrt_capture_free(wc->capture_winrt);
  386. wc->capture_winrt = NULL;
  387. }
  388. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  389. }
  390. static void wc_tick(void *data, float seconds)
  391. {
  392. struct window_capture *wc = data;
  393. RECT rect;
  394. bool reset_capture = false;
  395. if (!obs_source_showing(wc->source))
  396. return;
  397. if (!wc->window || !IsWindow(wc->window)) {
  398. if (!wc->title && !wc->class) {
  399. if (wc->capture.valid)
  400. dc_capture_free(&wc->capture);
  401. return;
  402. }
  403. wc->check_window_timer += seconds;
  404. if (wc->check_window_timer < WC_CHECK_TIMER) {
  405. if (wc->capture.valid)
  406. dc_capture_free(&wc->capture);
  407. return;
  408. }
  409. if (wc->capture_winrt) {
  410. wc->exports.winrt_capture_free(wc->capture_winrt);
  411. wc->capture_winrt = NULL;
  412. }
  413. wc->check_window_timer = 0.0f;
  414. wc->window =
  415. (wc->method == METHOD_WGC)
  416. ? ms_find_window_top_level(INCLUDE_MINIMIZED,
  417. wc->priority,
  418. wc->class, wc->title,
  419. wc->executable)
  420. : ms_find_window(INCLUDE_MINIMIZED,
  421. wc->priority, wc->class,
  422. wc->title, wc->executable);
  423. if (!wc->window) {
  424. if (wc->capture.valid)
  425. dc_capture_free(&wc->capture);
  426. return;
  427. }
  428. wc->previously_failed = false;
  429. reset_capture = true;
  430. } else if (IsIconic(wc->window) || !IsWindowVisible(wc->window)) {
  431. return; /* If HWND is invisible, WGC module can't be initialized successfully */
  432. }
  433. wc->cursor_check_time += seconds;
  434. if (wc->cursor_check_time >= CURSOR_CHECK_TIME) {
  435. DWORD foreground_pid, target_pid;
  436. // Can't just compare the window handle in case of app with child windows
  437. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  438. &foreground_pid))
  439. foreground_pid = 0;
  440. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  441. target_pid = 0;
  442. const bool cursor_hidden = foreground_pid && target_pid &&
  443. foreground_pid != target_pid;
  444. wc->capture.cursor_hidden = cursor_hidden;
  445. if (wc->capture_winrt &&
  446. !wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  447. !cursor_hidden)) {
  448. force_reset(wc);
  449. return;
  450. }
  451. wc->cursor_check_time = 0.0f;
  452. }
  453. obs_enter_graphics();
  454. if (wc->method == METHOD_BITBLT) {
  455. DPI_AWARENESS_CONTEXT previous = NULL;
  456. if (wc->get_window_dpi_awareness_context != NULL) {
  457. const DPI_AWARENESS_CONTEXT context =
  458. wc->get_window_dpi_awareness_context(
  459. wc->window);
  460. previous =
  461. wc->set_thread_dpi_awareness_context(context);
  462. }
  463. GetClientRect(wc->window, &rect);
  464. if (!reset_capture) {
  465. wc->resize_timer += seconds;
  466. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  467. if ((rect.bottom - rect.top) !=
  468. (wc->last_rect.bottom -
  469. wc->last_rect.top) ||
  470. (rect.right - rect.left) !=
  471. (wc->last_rect.right -
  472. wc->last_rect.left))
  473. reset_capture = true;
  474. wc->resize_timer = 0.0f;
  475. }
  476. }
  477. if (reset_capture) {
  478. wc->resize_timer = 0.0f;
  479. wc->last_rect = rect;
  480. dc_capture_free(&wc->capture);
  481. dc_capture_init(&wc->capture, 0, 0,
  482. rect.right - rect.left,
  483. rect.bottom - rect.top, wc->cursor,
  484. wc->compatibility);
  485. }
  486. dc_capture_capture(&wc->capture, wc->window);
  487. if (previous)
  488. wc->set_thread_dpi_awareness_context(previous);
  489. } else if (wc->method == METHOD_WGC) {
  490. if (wc->window && (wc->capture_winrt == NULL)) {
  491. if (!wc->previously_failed) {
  492. wc->capture_winrt =
  493. wc->exports.winrt_capture_init_window(
  494. wc->cursor, wc->window,
  495. wc->client_area);
  496. if (!wc->capture_winrt) {
  497. wc->previously_failed = true;
  498. }
  499. }
  500. }
  501. }
  502. obs_leave_graphics();
  503. }
  504. static void wc_render(void *data, gs_effect_t *effect)
  505. {
  506. struct window_capture *wc = data;
  507. if (wc->method == METHOD_WGC) {
  508. if (wc->capture_winrt) {
  509. if (wc->exports.winrt_capture_active(
  510. wc->capture_winrt)) {
  511. wc->exports.winrt_capture_render(
  512. wc->capture_winrt);
  513. } else {
  514. wc->exports.winrt_capture_free(
  515. wc->capture_winrt);
  516. wc->capture_winrt = NULL;
  517. }
  518. }
  519. } else {
  520. dc_capture_render(
  521. &wc->capture,
  522. obs_source_get_texcoords_centered(wc->source));
  523. }
  524. UNUSED_PARAMETER(effect);
  525. }
  526. enum gs_color_space
  527. wc_get_color_space(void *data, size_t count,
  528. const enum gs_color_space *preferred_spaces)
  529. {
  530. struct window_capture *wc = data;
  531. enum gs_color_space capture_space = GS_CS_SRGB;
  532. if ((wc->method == METHOD_WGC) && wc->capture_winrt) {
  533. capture_space = wc->exports.winrt_capture_get_color_space(
  534. wc->capture_winrt);
  535. }
  536. enum gs_color_space space = capture_space;
  537. for (size_t i = 0; i < count; ++i) {
  538. const enum gs_color_space preferred_space = preferred_spaces[i];
  539. space = preferred_space;
  540. if (preferred_space == capture_space)
  541. break;
  542. }
  543. return space;
  544. }
  545. struct obs_source_info window_capture_info = {
  546. .id = "window_capture",
  547. .type = OBS_SOURCE_TYPE_INPUT,
  548. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  549. OBS_SOURCE_SRGB,
  550. .get_name = wc_getname,
  551. .create = wc_create,
  552. .destroy = wc_destroy,
  553. .update = wc_update,
  554. .video_render = wc_render,
  555. .hide = wc_hide,
  556. .video_tick = wc_tick,
  557. .get_width = wc_width,
  558. .get_height = wc_height,
  559. .get_defaults = wc_defaults,
  560. .get_properties = wc_properties,
  561. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  562. .video_get_color_space = wc_get_color_space,
  563. };