window-capture.c 24 KB

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