window-capture.c 16 KB

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