window-capture.c 19 KB

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