window-capture.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 previously_failed;
  70. void *winrt_module;
  71. struct winrt_exports exports;
  72. struct winrt_capture *capture_winrt;
  73. float resize_timer;
  74. float check_window_timer;
  75. float cursor_check_time;
  76. HWND window;
  77. RECT last_rect;
  78. };
  79. static const char *wgc_partial_match_classes[] = {
  80. "Chrome",
  81. "Mozilla",
  82. NULL,
  83. };
  84. static const char *wgc_whole_match_classes[] = {
  85. "ApplicationFrameWindow",
  86. "Windows.UI.Core.CoreWindow",
  87. "XLMAIN", /* excel*/
  88. "PPTFrameClass", /* powerpoint */
  89. "OpusApp", /* word */
  90. NULL,
  91. };
  92. static enum window_capture_method
  93. choose_method(enum window_capture_method method, bool wgc_supported,
  94. const char *current_class)
  95. {
  96. if (!wgc_supported)
  97. return METHOD_BITBLT;
  98. if (method != METHOD_AUTO)
  99. return method;
  100. if (!current_class)
  101. return METHOD_BITBLT;
  102. const char **class = wgc_partial_match_classes;
  103. while (*class) {
  104. if (astrstri(current_class, *class) != NULL) {
  105. return METHOD_WGC;
  106. }
  107. class ++;
  108. }
  109. class = wgc_whole_match_classes;
  110. while (*class) {
  111. if (astrcmpi(current_class, *class) == 0) {
  112. return METHOD_WGC;
  113. }
  114. class ++;
  115. }
  116. return METHOD_BITBLT;
  117. }
  118. static const char *get_method_name(int method)
  119. {
  120. const char *method_name = "";
  121. switch (method) {
  122. case METHOD_AUTO:
  123. method_name = "Automatic";
  124. break;
  125. case METHOD_BITBLT:
  126. method_name = "BitBlt";
  127. break;
  128. case METHOD_WGC:
  129. method_name = "WGC";
  130. break;
  131. }
  132. return method_name;
  133. }
  134. static void log_settings(struct window_capture *wc, obs_data_t *s)
  135. {
  136. int method = (int)obs_data_get_int(s, "method");
  137. if (wc->title != NULL) {
  138. blog(LOG_INFO,
  139. "[window-capture: '%s'] update settings:\n"
  140. "\texecutable: %s\n"
  141. "\tmethod selected: %s\n"
  142. "\tmethod chosen: %s\n",
  143. obs_source_get_name(wc->source), wc->executable,
  144. get_method_name(method), get_method_name(wc->method));
  145. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  146. }
  147. }
  148. extern bool wgc_supported;
  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, 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. extern bool graphics_uses_d3d11;
  200. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  201. {
  202. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  203. wc->source = source;
  204. pthread_mutex_init(&wc->update_mutex, NULL);
  205. if (graphics_uses_d3d11) {
  206. static const char *const module = "libobs-winrt";
  207. wc->winrt_module = os_dlopen(module);
  208. if (wc->winrt_module) {
  209. load_winrt_imports(&wc->exports, wc->winrt_module,
  210. module);
  211. }
  212. }
  213. update_settings(wc, settings);
  214. log_settings(wc, settings);
  215. return wc;
  216. }
  217. static void wc_actual_destroy(void *data)
  218. {
  219. struct window_capture *wc = data;
  220. if (wc->capture_winrt) {
  221. wc->exports.winrt_capture_free(wc->capture_winrt);
  222. }
  223. obs_enter_graphics();
  224. dc_capture_free(&wc->capture);
  225. obs_leave_graphics();
  226. bfree(wc->title);
  227. bfree(wc->class);
  228. bfree(wc->executable);
  229. if (wc->winrt_module)
  230. os_dlclose(wc->winrt_module);
  231. pthread_mutex_destroy(&wc->update_mutex);
  232. bfree(wc);
  233. }
  234. static void wc_destroy(void *data)
  235. {
  236. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  237. }
  238. static void force_reset(struct window_capture *wc)
  239. {
  240. wc->window = NULL;
  241. wc->resize_timer = RESIZE_CHECK_TIME;
  242. wc->check_window_timer = WC_CHECK_TIMER;
  243. wc->cursor_check_time = CURSOR_CHECK_TIME;
  244. wc->previously_failed = false;
  245. }
  246. static void wc_update(void *data, obs_data_t *settings)
  247. {
  248. struct window_capture *wc = data;
  249. update_settings(wc, settings);
  250. log_settings(wc, settings);
  251. force_reset(wc);
  252. }
  253. static uint32_t wc_width(void *data)
  254. {
  255. struct window_capture *wc = data;
  256. return (wc->method == METHOD_WGC)
  257. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  258. : wc->capture.width;
  259. }
  260. static uint32_t wc_height(void *data)
  261. {
  262. struct window_capture *wc = data;
  263. return (wc->method == METHOD_WGC)
  264. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  265. : wc->capture.height;
  266. }
  267. static void wc_defaults(obs_data_t *defaults)
  268. {
  269. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  270. obs_data_set_default_bool(defaults, "cursor", true);
  271. obs_data_set_default_bool(defaults, "compatibility", false);
  272. obs_data_set_default_bool(defaults, "client_area", true);
  273. }
  274. static void update_settings_visibility(obs_properties_t *props,
  275. struct window_capture *wc)
  276. {
  277. pthread_mutex_lock(&wc->update_mutex);
  278. const enum window_capture_method method = wc->method;
  279. const bool bitblt_options = method == METHOD_BITBLT;
  280. const bool wgc_options = method == METHOD_WGC;
  281. const bool wgc_cursor_toggle =
  282. wgc_options &&
  283. wc->exports.winrt_capture_cursor_toggle_supported();
  284. obs_property_t *p = obs_properties_get(props, "cursor");
  285. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  286. p = obs_properties_get(props, "compatibility");
  287. obs_property_set_visible(p, bitblt_options);
  288. p = obs_properties_get(props, "client_area");
  289. obs_property_set_visible(p, wgc_options);
  290. pthread_mutex_unlock(&wc->update_mutex);
  291. }
  292. static bool wc_capture_method_changed(obs_properties_t *props,
  293. obs_property_t *p, obs_data_t *settings)
  294. {
  295. UNUSED_PARAMETER(p);
  296. struct window_capture *wc = obs_properties_get_param(props);
  297. update_settings(wc, settings);
  298. update_settings_visibility(props, wc);
  299. return true;
  300. }
  301. extern bool check_window_property_setting(obs_properties_t *ppts,
  302. obs_property_t *p,
  303. obs_data_t *settings, const char *val,
  304. size_t idx);
  305. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  306. obs_data_t *settings)
  307. {
  308. struct window_capture *wc = obs_properties_get_param(props);
  309. update_settings(wc, settings);
  310. update_settings_visibility(props, wc);
  311. check_window_property_setting(props, p, settings, "window", 0);
  312. return true;
  313. }
  314. static obs_properties_t *wc_properties(void *data)
  315. {
  316. struct window_capture *wc = data;
  317. obs_properties_t *ppts = obs_properties_create();
  318. obs_properties_set_param(ppts, wc, NULL);
  319. obs_property_t *p;
  320. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  321. OBS_COMBO_TYPE_LIST,
  322. OBS_COMBO_FORMAT_STRING);
  323. fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  324. obs_property_set_modified_callback(p, wc_window_changed);
  325. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  326. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  327. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  328. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  329. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  330. obs_property_list_item_disable(p, 2, !wgc_supported);
  331. obs_property_set_modified_callback(p, wc_capture_method_changed);
  332. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  333. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  334. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  335. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  336. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  337. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  338. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  339. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  340. return ppts;
  341. }
  342. static void wc_hide(void *data)
  343. {
  344. struct window_capture *wc = data;
  345. if (wc->capture_winrt) {
  346. wc->exports.winrt_capture_free(wc->capture_winrt);
  347. wc->capture_winrt = NULL;
  348. }
  349. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  350. }
  351. static void wc_tick(void *data, float seconds)
  352. {
  353. struct window_capture *wc = data;
  354. RECT rect;
  355. bool reset_capture = false;
  356. if (!obs_source_showing(wc->source))
  357. return;
  358. if (!wc->window || !IsWindow(wc->window)) {
  359. if (!wc->title && !wc->class) {
  360. if (wc->capture.valid)
  361. dc_capture_free(&wc->capture);
  362. return;
  363. }
  364. wc->check_window_timer += seconds;
  365. if (wc->check_window_timer < WC_CHECK_TIMER) {
  366. if (wc->capture.valid)
  367. dc_capture_free(&wc->capture);
  368. return;
  369. }
  370. if (wc->capture_winrt) {
  371. wc->exports.winrt_capture_free(wc->capture_winrt);
  372. wc->capture_winrt = NULL;
  373. }
  374. wc->check_window_timer = 0.0f;
  375. wc->window = (wc->method == METHOD_WGC)
  376. ? find_window_top_level(INCLUDE_MINIMIZED,
  377. wc->priority,
  378. wc->class,
  379. wc->title,
  380. wc->executable)
  381. : find_window(INCLUDE_MINIMIZED,
  382. wc->priority, wc->class,
  383. wc->title, wc->executable);
  384. if (!wc->window) {
  385. if (wc->capture.valid)
  386. dc_capture_free(&wc->capture);
  387. return;
  388. }
  389. wc->previously_failed = false;
  390. reset_capture = true;
  391. } else if (IsIconic(wc->window)) {
  392. return;
  393. }
  394. wc->cursor_check_time += seconds;
  395. if (wc->cursor_check_time >= CURSOR_CHECK_TIME) {
  396. DWORD foreground_pid, target_pid;
  397. // Can't just compare the window handle in case of app with child windows
  398. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  399. &foreground_pid))
  400. foreground_pid = 0;
  401. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  402. target_pid = 0;
  403. const bool cursor_hidden = foreground_pid && target_pid &&
  404. foreground_pid != target_pid;
  405. wc->capture.cursor_hidden = cursor_hidden;
  406. if (wc->capture_winrt &&
  407. !wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  408. !cursor_hidden)) {
  409. force_reset(wc);
  410. return;
  411. }
  412. wc->cursor_check_time = 0.0f;
  413. }
  414. obs_enter_graphics();
  415. if (wc->method == METHOD_BITBLT) {
  416. GetClientRect(wc->window, &rect);
  417. if (!reset_capture) {
  418. wc->resize_timer += seconds;
  419. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  420. if ((rect.bottom - rect.top) !=
  421. (wc->last_rect.bottom -
  422. wc->last_rect.top) ||
  423. (rect.right - rect.left) !=
  424. (wc->last_rect.right -
  425. wc->last_rect.left))
  426. reset_capture = true;
  427. wc->resize_timer = 0.0f;
  428. }
  429. }
  430. if (reset_capture) {
  431. wc->resize_timer = 0.0f;
  432. wc->last_rect = rect;
  433. dc_capture_free(&wc->capture);
  434. dc_capture_init(&wc->capture, 0, 0,
  435. rect.right - rect.left,
  436. rect.bottom - rect.top, wc->cursor,
  437. wc->compatibility);
  438. }
  439. dc_capture_capture(&wc->capture, wc->window);
  440. } else if (wc->method == METHOD_WGC) {
  441. if (wc->window && (wc->capture_winrt == NULL)) {
  442. if (!wc->previously_failed) {
  443. wc->capture_winrt =
  444. wc->exports.winrt_capture_init_window(
  445. wc->cursor, wc->window,
  446. wc->client_area);
  447. if (!wc->capture_winrt) {
  448. wc->previously_failed = true;
  449. }
  450. }
  451. }
  452. }
  453. obs_leave_graphics();
  454. }
  455. static void wc_render(void *data, gs_effect_t *effect)
  456. {
  457. struct window_capture *wc = data;
  458. gs_effect_t *const opaque = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  459. if (wc->method == METHOD_WGC) {
  460. if (wc->capture_winrt) {
  461. if (wc->exports.winrt_capture_active(
  462. wc->capture_winrt)) {
  463. wc->exports.winrt_capture_render(
  464. wc->capture_winrt, opaque);
  465. } else {
  466. wc->exports.winrt_capture_free(
  467. wc->capture_winrt);
  468. wc->capture_winrt = NULL;
  469. }
  470. }
  471. } else {
  472. dc_capture_render(&wc->capture, opaque);
  473. }
  474. UNUSED_PARAMETER(effect);
  475. }
  476. struct obs_source_info window_capture_info = {
  477. .id = "window_capture",
  478. .type = OBS_SOURCE_TYPE_INPUT,
  479. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  480. OBS_SOURCE_SRGB,
  481. .get_name = wc_getname,
  482. .create = wc_create,
  483. .destroy = wc_destroy,
  484. .update = wc_update,
  485. .video_render = wc_render,
  486. .hide = wc_hide,
  487. .video_tick = wc_tick,
  488. .get_width = wc_width,
  489. .get_height = wc_height,
  490. .get_defaults = wc_defaults,
  491. .get_properties = wc_properties,
  492. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  493. };