window-capture.c 25 KB

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