window-capture.c 16 KB

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