window-capture.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. obs_source_get_name(wc->source), wc->executable,
  156. get_method_name(method), get_method_name(wc->method));
  157. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  158. }
  159. }
  160. extern bool wgc_supported;
  161. static void update_settings(struct window_capture *wc, obs_data_t *s)
  162. {
  163. pthread_mutex_lock(&wc->update_mutex);
  164. int method = (int)obs_data_get_int(s, "method");
  165. const char *window = obs_data_get_string(s, "window");
  166. int priority = (int)obs_data_get_int(s, "priority");
  167. bfree(wc->title);
  168. bfree(wc->class);
  169. bfree(wc->executable);
  170. ms_build_window_strings(window, &wc->class, &wc->title,
  171. &wc->executable);
  172. wc->method = choose_method(method, wgc_supported, wc->class);
  173. wc->priority = (enum window_priority)priority;
  174. wc->cursor = obs_data_get_bool(s, "cursor");
  175. wc->force_sdr = obs_data_get_bool(s, "force_sdr");
  176. wc->compatibility = obs_data_get_bool(s, "compatibility");
  177. wc->client_area = obs_data_get_bool(s, "client_area");
  178. pthread_mutex_unlock(&wc->update_mutex);
  179. }
  180. /* ------------------------------------------------------------------------- */
  181. static const char *wc_getname(void *unused)
  182. {
  183. UNUSED_PARAMETER(unused);
  184. return TEXT_WINDOW_CAPTURE;
  185. }
  186. #define WINRT_IMPORT(func) \
  187. do { \
  188. exports->func = (PFN_##func)os_dlsym(module, #func); \
  189. if (!exports->func) { \
  190. success = false; \
  191. blog(LOG_ERROR, \
  192. "Could not load function '%s' from " \
  193. "module '%s'", \
  194. #func, module_name); \
  195. } \
  196. } while (false)
  197. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  198. const char *module_name)
  199. {
  200. bool success = true;
  201. WINRT_IMPORT(winrt_capture_supported);
  202. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  203. WINRT_IMPORT(winrt_capture_init_window);
  204. WINRT_IMPORT(winrt_capture_free);
  205. WINRT_IMPORT(winrt_capture_active);
  206. WINRT_IMPORT(winrt_capture_show_cursor);
  207. WINRT_IMPORT(winrt_capture_get_color_space);
  208. WINRT_IMPORT(winrt_capture_render);
  209. WINRT_IMPORT(winrt_capture_width);
  210. WINRT_IMPORT(winrt_capture_height);
  211. return success;
  212. }
  213. extern bool graphics_uses_d3d11;
  214. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  215. {
  216. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  217. wc->source = source;
  218. pthread_mutex_init(&wc->update_mutex, NULL);
  219. if (graphics_uses_d3d11) {
  220. static const char *const module = "libobs-winrt";
  221. wc->winrt_module = os_dlopen(module);
  222. if (wc->winrt_module) {
  223. load_winrt_imports(&wc->exports, wc->winrt_module,
  224. module);
  225. }
  226. }
  227. const HMODULE hModuleUser32 = GetModuleHandle(L"User32.dll");
  228. if (hModuleUser32) {
  229. PFN_SetThreadDpiAwarenessContext
  230. set_thread_dpi_awareness_context =
  231. (PFN_SetThreadDpiAwarenessContext)GetProcAddress(
  232. hModuleUser32,
  233. "SetThreadDpiAwarenessContext");
  234. PFN_GetThreadDpiAwarenessContext
  235. get_thread_dpi_awareness_context =
  236. (PFN_GetThreadDpiAwarenessContext)GetProcAddress(
  237. hModuleUser32,
  238. "GetThreadDpiAwarenessContext");
  239. PFN_GetWindowDpiAwarenessContext
  240. get_window_dpi_awareness_context =
  241. (PFN_GetWindowDpiAwarenessContext)GetProcAddress(
  242. hModuleUser32,
  243. "GetWindowDpiAwarenessContext");
  244. if (set_thread_dpi_awareness_context &&
  245. get_thread_dpi_awareness_context &&
  246. get_window_dpi_awareness_context) {
  247. wc->set_thread_dpi_awareness_context =
  248. set_thread_dpi_awareness_context;
  249. wc->get_thread_dpi_awareness_context =
  250. get_thread_dpi_awareness_context;
  251. wc->get_window_dpi_awareness_context =
  252. get_window_dpi_awareness_context;
  253. }
  254. }
  255. update_settings(wc, settings);
  256. log_settings(wc, settings);
  257. return wc;
  258. }
  259. static void wc_actual_destroy(void *data)
  260. {
  261. struct window_capture *wc = data;
  262. if (wc->capture_winrt) {
  263. wc->exports.winrt_capture_free(wc->capture_winrt);
  264. }
  265. obs_enter_graphics();
  266. dc_capture_free(&wc->capture);
  267. obs_leave_graphics();
  268. bfree(wc->title);
  269. bfree(wc->class);
  270. bfree(wc->executable);
  271. if (wc->winrt_module)
  272. os_dlclose(wc->winrt_module);
  273. pthread_mutex_destroy(&wc->update_mutex);
  274. bfree(wc);
  275. }
  276. static void wc_destroy(void *data)
  277. {
  278. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  279. }
  280. static void force_reset(struct window_capture *wc)
  281. {
  282. wc->window = NULL;
  283. wc->resize_timer = RESIZE_CHECK_TIME;
  284. wc->check_window_timer = WC_CHECK_TIMER;
  285. wc->cursor_check_time = CURSOR_CHECK_TIME;
  286. wc->previously_failed = false;
  287. }
  288. static void wc_update(void *data, obs_data_t *settings)
  289. {
  290. struct window_capture *wc = data;
  291. update_settings(wc, settings);
  292. log_settings(wc, settings);
  293. force_reset(wc);
  294. }
  295. static uint32_t wc_width(void *data)
  296. {
  297. struct window_capture *wc = data;
  298. return (wc->method == METHOD_WGC)
  299. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  300. : wc->capture.width;
  301. }
  302. static uint32_t wc_height(void *data)
  303. {
  304. struct window_capture *wc = data;
  305. return (wc->method == METHOD_WGC)
  306. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  307. : wc->capture.height;
  308. }
  309. static void wc_defaults(obs_data_t *defaults)
  310. {
  311. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  312. obs_data_set_default_bool(defaults, "cursor", true);
  313. obs_data_set_default_bool(defaults, "force_sdr", false);
  314. obs_data_set_default_bool(defaults, "compatibility", false);
  315. obs_data_set_default_bool(defaults, "client_area", true);
  316. }
  317. static void update_settings_visibility(obs_properties_t *props,
  318. struct window_capture *wc)
  319. {
  320. pthread_mutex_lock(&wc->update_mutex);
  321. const enum window_capture_method method = wc->method;
  322. const bool bitblt_options = method == METHOD_BITBLT;
  323. const bool wgc_options = method == METHOD_WGC;
  324. const bool wgc_cursor_toggle =
  325. wgc_options &&
  326. wc->exports.winrt_capture_cursor_toggle_supported();
  327. obs_property_t *p = obs_properties_get(props, "cursor");
  328. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  329. p = obs_properties_get(props, "compatibility");
  330. obs_property_set_visible(p, bitblt_options);
  331. p = obs_properties_get(props, "client_area");
  332. obs_property_set_visible(p, wgc_options);
  333. p = obs_properties_get(props, "force_sdr");
  334. obs_property_set_visible(p, wgc_cursor_toggle);
  335. pthread_mutex_unlock(&wc->update_mutex);
  336. }
  337. static bool wc_capture_method_changed(obs_properties_t *props,
  338. obs_property_t *p, obs_data_t *settings)
  339. {
  340. UNUSED_PARAMETER(p);
  341. struct window_capture *wc = obs_properties_get_param(props);
  342. if (!wc)
  343. return false;
  344. update_settings(wc, settings);
  345. update_settings_visibility(props, wc);
  346. return true;
  347. }
  348. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  349. obs_data_t *settings)
  350. {
  351. struct window_capture *wc = obs_properties_get_param(props);
  352. if (!wc)
  353. return false;
  354. update_settings(wc, settings);
  355. update_settings_visibility(props, wc);
  356. ms_check_window_property_setting(props, p, settings, "window", 0);
  357. return true;
  358. }
  359. static obs_properties_t *wc_properties(void *data)
  360. {
  361. struct window_capture *wc = data;
  362. obs_properties_t *ppts = obs_properties_create();
  363. obs_properties_set_param(ppts, wc, NULL);
  364. obs_property_t *p;
  365. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  366. OBS_COMBO_TYPE_LIST,
  367. OBS_COMBO_FORMAT_STRING);
  368. ms_fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  369. obs_property_set_modified_callback(p, wc_window_changed);
  370. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  371. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  372. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  373. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  374. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  375. obs_property_list_item_disable(p, 2, !wgc_supported);
  376. obs_property_set_modified_callback(p, wc_capture_method_changed);
  377. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  378. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  379. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  380. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  381. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  382. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  383. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  384. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  385. obs_properties_add_bool(ppts, "force_sdr", TEXT_FORCE_SDR);
  386. return ppts;
  387. }
  388. static void wc_hide(void *data)
  389. {
  390. struct window_capture *wc = data;
  391. if (wc->capture_winrt) {
  392. wc->exports.winrt_capture_free(wc->capture_winrt);
  393. wc->capture_winrt = NULL;
  394. }
  395. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  396. }
  397. static void wc_tick(void *data, float seconds)
  398. {
  399. struct window_capture *wc = data;
  400. RECT rect;
  401. bool reset_capture = false;
  402. if (!obs_source_showing(wc->source))
  403. return;
  404. if (!wc->window || !IsWindow(wc->window)) {
  405. if (!wc->title && !wc->class) {
  406. if (wc->capture.valid)
  407. dc_capture_free(&wc->capture);
  408. return;
  409. }
  410. wc->check_window_timer += seconds;
  411. if (wc->check_window_timer < WC_CHECK_TIMER) {
  412. if (wc->capture.valid)
  413. dc_capture_free(&wc->capture);
  414. return;
  415. }
  416. if (wc->capture_winrt) {
  417. wc->exports.winrt_capture_free(wc->capture_winrt);
  418. wc->capture_winrt = NULL;
  419. }
  420. wc->check_window_timer = 0.0f;
  421. wc->window =
  422. (wc->method == METHOD_WGC)
  423. ? ms_find_window_top_level(INCLUDE_MINIMIZED,
  424. wc->priority,
  425. wc->class, wc->title,
  426. wc->executable)
  427. : ms_find_window(INCLUDE_MINIMIZED,
  428. wc->priority, wc->class,
  429. wc->title, wc->executable);
  430. if (!wc->window) {
  431. if (wc->capture.valid)
  432. dc_capture_free(&wc->capture);
  433. return;
  434. }
  435. wc->previously_failed = false;
  436. reset_capture = true;
  437. } else if (IsIconic(wc->window) || !IsWindowVisible(wc->window)) {
  438. return; /* If HWND is invisible, WGC module can't be initialized successfully */
  439. }
  440. wc->cursor_check_time += seconds;
  441. if (wc->cursor_check_time >= CURSOR_CHECK_TIME) {
  442. DWORD foreground_pid, target_pid;
  443. // Can't just compare the window handle in case of app with child windows
  444. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  445. &foreground_pid))
  446. foreground_pid = 0;
  447. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  448. target_pid = 0;
  449. const bool cursor_hidden = foreground_pid && target_pid &&
  450. foreground_pid != target_pid;
  451. wc->capture.cursor_hidden = cursor_hidden;
  452. if (wc->capture_winrt &&
  453. !wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  454. !cursor_hidden)) {
  455. force_reset(wc);
  456. return;
  457. }
  458. wc->cursor_check_time = 0.0f;
  459. }
  460. obs_enter_graphics();
  461. if (wc->method == METHOD_BITBLT) {
  462. DPI_AWARENESS_CONTEXT previous = NULL;
  463. if (wc->get_window_dpi_awareness_context != NULL) {
  464. const DPI_AWARENESS_CONTEXT context =
  465. wc->get_window_dpi_awareness_context(
  466. wc->window);
  467. previous =
  468. wc->set_thread_dpi_awareness_context(context);
  469. }
  470. GetClientRect(wc->window, &rect);
  471. if (!reset_capture) {
  472. wc->resize_timer += seconds;
  473. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  474. if ((rect.bottom - rect.top) !=
  475. (wc->last_rect.bottom -
  476. wc->last_rect.top) ||
  477. (rect.right - rect.left) !=
  478. (wc->last_rect.right -
  479. wc->last_rect.left))
  480. reset_capture = true;
  481. wc->resize_timer = 0.0f;
  482. }
  483. }
  484. if (reset_capture) {
  485. wc->resize_timer = 0.0f;
  486. wc->last_rect = rect;
  487. dc_capture_free(&wc->capture);
  488. dc_capture_init(&wc->capture, 0, 0,
  489. rect.right - rect.left,
  490. rect.bottom - rect.top, wc->cursor,
  491. wc->compatibility);
  492. }
  493. dc_capture_capture(&wc->capture, wc->window);
  494. if (previous)
  495. wc->set_thread_dpi_awareness_context(previous);
  496. } else if (wc->method == METHOD_WGC) {
  497. if (wc->window && (wc->capture_winrt == NULL)) {
  498. if (!wc->previously_failed) {
  499. wc->capture_winrt =
  500. wc->exports.winrt_capture_init_window(
  501. wc->cursor, wc->window,
  502. wc->client_area, wc->force_sdr);
  503. if (!wc->capture_winrt) {
  504. wc->previously_failed = true;
  505. }
  506. }
  507. }
  508. }
  509. obs_leave_graphics();
  510. }
  511. static void wc_render(void *data, gs_effect_t *effect)
  512. {
  513. struct window_capture *wc = data;
  514. if (wc->method == METHOD_WGC) {
  515. if (wc->capture_winrt) {
  516. if (wc->exports.winrt_capture_active(
  517. wc->capture_winrt)) {
  518. wc->exports.winrt_capture_render(
  519. wc->capture_winrt);
  520. } else {
  521. wc->exports.winrt_capture_free(
  522. wc->capture_winrt);
  523. wc->capture_winrt = NULL;
  524. }
  525. }
  526. } else {
  527. dc_capture_render(
  528. &wc->capture,
  529. obs_source_get_texcoords_centered(wc->source));
  530. }
  531. UNUSED_PARAMETER(effect);
  532. }
  533. enum gs_color_space
  534. wc_get_color_space(void *data, size_t count,
  535. const enum gs_color_space *preferred_spaces)
  536. {
  537. struct window_capture *wc = data;
  538. enum gs_color_space capture_space = GS_CS_SRGB;
  539. if ((wc->method == METHOD_WGC) && wc->capture_winrt) {
  540. capture_space = wc->exports.winrt_capture_get_color_space(
  541. wc->capture_winrt);
  542. }
  543. enum gs_color_space space = capture_space;
  544. for (size_t i = 0; i < count; ++i) {
  545. const enum gs_color_space preferred_space = preferred_spaces[i];
  546. space = preferred_space;
  547. if (preferred_space == capture_space)
  548. break;
  549. }
  550. return space;
  551. }
  552. struct obs_source_info window_capture_info = {
  553. .id = "window_capture",
  554. .type = OBS_SOURCE_TYPE_INPUT,
  555. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  556. OBS_SOURCE_SRGB,
  557. .get_name = wc_getname,
  558. .create = wc_create,
  559. .destroy = wc_destroy,
  560. .update = wc_update,
  561. .video_render = wc_render,
  562. .hide = wc_hide,
  563. .video_tick = wc_tick,
  564. .get_width = wc_width,
  565. .get_height = wc_height,
  566. .get_defaults = wc_defaults,
  567. .get_properties = wc_properties,
  568. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  569. .video_get_color_space = wc_get_color_space,
  570. };