window-capture.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. typedef BOOL (*PFN_winrt_capture_supported)();
  25. typedef BOOL (*PFN_winrt_capture_cursor_toggle_supported)();
  26. typedef struct winrt_capture *(*PFN_winrt_capture_init_window)(
  27. BOOL cursor, HWND window, BOOL client_area);
  28. typedef void (*PFN_winrt_capture_free)(struct winrt_capture *capture);
  29. typedef BOOL (*PFN_winrt_capture_active)(const struct winrt_capture *capture);
  30. typedef void (*PFN_winrt_capture_show_cursor)(struct winrt_capture *capture,
  31. BOOL visible);
  32. typedef void (*PFN_winrt_capture_render)(struct winrt_capture *capture,
  33. gs_effect_t *effect);
  34. typedef uint32_t (*PFN_winrt_capture_width)(const struct winrt_capture *capture);
  35. typedef uint32_t (*PFN_winrt_capture_height)(
  36. const struct winrt_capture *capture);
  37. struct winrt_exports {
  38. PFN_winrt_capture_supported winrt_capture_supported;
  39. PFN_winrt_capture_cursor_toggle_supported
  40. winrt_capture_cursor_toggle_supported;
  41. PFN_winrt_capture_init_window winrt_capture_init_window;
  42. PFN_winrt_capture_free winrt_capture_free;
  43. PFN_winrt_capture_active winrt_capture_active;
  44. PFN_winrt_capture_show_cursor winrt_capture_show_cursor;
  45. PFN_winrt_capture_render winrt_capture_render;
  46. PFN_winrt_capture_width winrt_capture_width;
  47. PFN_winrt_capture_height winrt_capture_height;
  48. };
  49. enum window_capture_method {
  50. METHOD_AUTO,
  51. METHOD_BITBLT,
  52. METHOD_WGC,
  53. };
  54. struct window_capture {
  55. obs_source_t *source;
  56. pthread_mutex_t update_mutex;
  57. char *title;
  58. char *class;
  59. char *executable;
  60. enum window_capture_method method;
  61. enum window_priority priority;
  62. bool cursor;
  63. bool compatibility;
  64. bool client_area;
  65. bool use_wildcards; /* TODO */
  66. struct dc_capture capture;
  67. bool wgc_supported;
  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. static void update_settings(struct window_capture *wc, obs_data_t *s)
  148. {
  149. pthread_mutex_lock(&wc->update_mutex);
  150. int method = (int)obs_data_get_int(s, "method");
  151. const char *window = obs_data_get_string(s, "window");
  152. int priority = (int)obs_data_get_int(s, "priority");
  153. bfree(wc->title);
  154. bfree(wc->class);
  155. bfree(wc->executable);
  156. build_window_strings(window, &wc->class, &wc->title, &wc->executable);
  157. wc->method = choose_method(method, wc->wgc_supported, wc->class);
  158. wc->priority = (enum window_priority)priority;
  159. wc->cursor = obs_data_get_bool(s, "cursor");
  160. wc->use_wildcards = obs_data_get_bool(s, "use_wildcards");
  161. wc->compatibility = obs_data_get_bool(s, "compatibility");
  162. wc->client_area = obs_data_get_bool(s, "client_area");
  163. pthread_mutex_unlock(&wc->update_mutex);
  164. }
  165. /* ------------------------------------------------------------------------- */
  166. static const char *wc_getname(void *unused)
  167. {
  168. UNUSED_PARAMETER(unused);
  169. return TEXT_WINDOW_CAPTURE;
  170. }
  171. #define WINRT_IMPORT(func) \
  172. do { \
  173. exports->func = (PFN_##func)os_dlsym(module, #func); \
  174. if (!exports->func) { \
  175. success = false; \
  176. blog(LOG_ERROR, \
  177. "Could not load function '%s' from " \
  178. "module '%s'", \
  179. #func, module_name); \
  180. } \
  181. } while (false)
  182. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  183. const char *module_name)
  184. {
  185. bool success = true;
  186. WINRT_IMPORT(winrt_capture_supported);
  187. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  188. WINRT_IMPORT(winrt_capture_init_window);
  189. WINRT_IMPORT(winrt_capture_free);
  190. WINRT_IMPORT(winrt_capture_active);
  191. WINRT_IMPORT(winrt_capture_show_cursor);
  192. WINRT_IMPORT(winrt_capture_render);
  193. WINRT_IMPORT(winrt_capture_width);
  194. WINRT_IMPORT(winrt_capture_height);
  195. return success;
  196. }
  197. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  198. {
  199. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  200. wc->source = source;
  201. pthread_mutex_init(&wc->update_mutex, NULL);
  202. obs_enter_graphics();
  203. const bool uses_d3d11 = gs_get_device_type() == GS_DEVICE_DIRECT3D_11;
  204. obs_leave_graphics();
  205. if (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. wc->exports.winrt_capture_supported()) {
  212. wc->wgc_supported = true;
  213. }
  214. }
  215. update_settings(wc, settings);
  216. log_settings(wc, settings);
  217. return wc;
  218. }
  219. static void wc_actual_destroy(void *data)
  220. {
  221. struct window_capture *wc = data;
  222. if (wc->capture_winrt) {
  223. wc->exports.winrt_capture_free(wc->capture_winrt);
  224. }
  225. obs_enter_graphics();
  226. dc_capture_free(&wc->capture);
  227. obs_leave_graphics();
  228. bfree(wc->title);
  229. bfree(wc->class);
  230. bfree(wc->executable);
  231. if (wc->winrt_module)
  232. os_dlclose(wc->winrt_module);
  233. pthread_mutex_destroy(&wc->update_mutex);
  234. bfree(wc);
  235. }
  236. static void wc_destroy(void *data)
  237. {
  238. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  239. }
  240. static void wc_update(void *data, obs_data_t *settings)
  241. {
  242. struct window_capture *wc = data;
  243. update_settings(wc, settings);
  244. log_settings(wc, settings);
  245. /* forces a reset */
  246. wc->window = NULL;
  247. wc->check_window_timer = WC_CHECK_TIMER;
  248. wc->previously_failed = false;
  249. }
  250. static uint32_t wc_width(void *data)
  251. {
  252. struct window_capture *wc = data;
  253. return (wc->method == METHOD_WGC)
  254. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  255. : wc->capture.width;
  256. }
  257. static uint32_t wc_height(void *data)
  258. {
  259. struct window_capture *wc = data;
  260. return (wc->method == METHOD_WGC)
  261. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  262. : wc->capture.height;
  263. }
  264. static void wc_defaults(obs_data_t *defaults)
  265. {
  266. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  267. obs_data_set_default_bool(defaults, "cursor", true);
  268. obs_data_set_default_bool(defaults, "compatibility", false);
  269. obs_data_set_default_bool(defaults, "client_area", true);
  270. }
  271. static void update_settings_visibility(obs_properties_t *props,
  272. struct window_capture *wc)
  273. {
  274. pthread_mutex_lock(&wc->update_mutex);
  275. const enum window_capture_method method = wc->method;
  276. const bool bitblt_options = method == METHOD_BITBLT;
  277. const bool wgc_options = method == METHOD_WGC;
  278. const bool wgc_cursor_toggle =
  279. wgc_options &&
  280. wc->exports.winrt_capture_cursor_toggle_supported();
  281. obs_property_t *p = obs_properties_get(props, "cursor");
  282. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  283. p = obs_properties_get(props, "compatibility");
  284. obs_property_set_visible(p, bitblt_options);
  285. p = obs_properties_get(props, "client_area");
  286. obs_property_set_visible(p, wgc_options);
  287. pthread_mutex_unlock(&wc->update_mutex);
  288. }
  289. static bool wc_capture_method_changed(obs_properties_t *props,
  290. obs_property_t *p, obs_data_t *settings)
  291. {
  292. UNUSED_PARAMETER(p);
  293. struct window_capture *wc = obs_properties_get_param(props);
  294. update_settings(wc, settings);
  295. update_settings_visibility(props, wc);
  296. return true;
  297. }
  298. extern bool check_window_property_setting(obs_properties_t *ppts,
  299. obs_property_t *p,
  300. obs_data_t *settings, const char *val,
  301. size_t idx);
  302. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  303. obs_data_t *settings)
  304. {
  305. struct window_capture *wc = obs_properties_get_param(props);
  306. update_settings(wc, settings);
  307. update_settings_visibility(props, wc);
  308. check_window_property_setting(props, p, settings, "window", 0);
  309. return true;
  310. }
  311. static obs_properties_t *wc_properties(void *data)
  312. {
  313. struct window_capture *wc = data;
  314. obs_properties_t *ppts = obs_properties_create();
  315. obs_properties_set_param(ppts, wc, NULL);
  316. obs_property_t *p;
  317. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  318. OBS_COMBO_TYPE_LIST,
  319. OBS_COMBO_FORMAT_STRING);
  320. fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  321. obs_property_set_modified_callback(p, wc_window_changed);
  322. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  323. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  324. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  325. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  326. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  327. obs_property_list_item_disable(p, 2, !wc->wgc_supported);
  328. obs_property_set_modified_callback(p, wc_capture_method_changed);
  329. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  330. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  331. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  332. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  333. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  334. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  335. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  336. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  337. return ppts;
  338. }
  339. static void wc_hide(void *data)
  340. {
  341. struct window_capture *wc = data;
  342. if (wc->capture_winrt) {
  343. wc->exports.winrt_capture_free(wc->capture_winrt);
  344. wc->capture_winrt = NULL;
  345. }
  346. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  347. }
  348. #define RESIZE_CHECK_TIME 0.2f
  349. #define CURSOR_CHECK_TIME 0.2f
  350. static void wc_tick(void *data, float seconds)
  351. {
  352. struct window_capture *wc = data;
  353. RECT rect;
  354. bool reset_capture = false;
  355. if (!obs_source_showing(wc->source))
  356. return;
  357. if (!wc->window || !IsWindow(wc->window)) {
  358. if (!wc->title && !wc->class) {
  359. if (wc->capture.valid)
  360. dc_capture_free(&wc->capture);
  361. return;
  362. }
  363. wc->check_window_timer += seconds;
  364. if (wc->check_window_timer < WC_CHECK_TIMER) {
  365. if (wc->capture.valid)
  366. dc_capture_free(&wc->capture);
  367. return;
  368. }
  369. if (wc->capture_winrt) {
  370. wc->exports.winrt_capture_free(wc->capture_winrt);
  371. wc->capture_winrt = NULL;
  372. }
  373. wc->check_window_timer = 0.0f;
  374. wc->window = (wc->method == METHOD_WGC)
  375. ? find_window_top_level(INCLUDE_MINIMIZED,
  376. wc->priority,
  377. wc->class,
  378. wc->title,
  379. wc->executable)
  380. : find_window(INCLUDE_MINIMIZED,
  381. wc->priority, wc->class,
  382. wc->title, wc->executable);
  383. if (!wc->window) {
  384. if (wc->capture.valid)
  385. dc_capture_free(&wc->capture);
  386. return;
  387. }
  388. wc->previously_failed = false;
  389. reset_capture = true;
  390. } else if (IsIconic(wc->window)) {
  391. return;
  392. }
  393. wc->cursor_check_time += seconds;
  394. if (wc->cursor_check_time > CURSOR_CHECK_TIME) {
  395. DWORD foreground_pid, target_pid;
  396. // Can't just compare the window handle in case of app with child windows
  397. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  398. &foreground_pid))
  399. foreground_pid = 0;
  400. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  401. target_pid = 0;
  402. const bool cursor_hidden = foreground_pid && target_pid &&
  403. foreground_pid != target_pid;
  404. wc->capture.cursor_hidden = cursor_hidden;
  405. if (wc->capture_winrt)
  406. wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  407. !cursor_hidden);
  408. wc->cursor_check_time = 0.0f;
  409. }
  410. obs_enter_graphics();
  411. if (wc->method == METHOD_BITBLT) {
  412. GetClientRect(wc->window, &rect);
  413. if (!reset_capture) {
  414. wc->resize_timer += seconds;
  415. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  416. if ((rect.bottom - rect.top) !=
  417. (wc->last_rect.bottom -
  418. wc->last_rect.top) ||
  419. (rect.right - rect.left) !=
  420. (wc->last_rect.right -
  421. wc->last_rect.left))
  422. reset_capture = true;
  423. wc->resize_timer = 0.0f;
  424. }
  425. }
  426. if (reset_capture) {
  427. wc->resize_timer = 0.0f;
  428. wc->last_rect = rect;
  429. dc_capture_free(&wc->capture);
  430. dc_capture_init(&wc->capture, 0, 0,
  431. rect.right - rect.left,
  432. rect.bottom - rect.top, wc->cursor,
  433. wc->compatibility);
  434. }
  435. dc_capture_capture(&wc->capture, wc->window);
  436. } else if (wc->method == METHOD_WGC) {
  437. if (wc->window && (wc->capture_winrt == NULL)) {
  438. if (!wc->previously_failed) {
  439. wc->capture_winrt =
  440. wc->exports.winrt_capture_init_window(
  441. wc->cursor, wc->window,
  442. wc->client_area);
  443. if (!wc->capture_winrt) {
  444. wc->previously_failed = true;
  445. }
  446. }
  447. }
  448. }
  449. obs_leave_graphics();
  450. }
  451. static void wc_render(void *data, gs_effect_t *effect)
  452. {
  453. struct window_capture *wc = data;
  454. gs_effect_t *const opaque = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  455. if (wc->method == METHOD_WGC) {
  456. if (wc->capture_winrt) {
  457. if (wc->exports.winrt_capture_active(
  458. wc->capture_winrt)) {
  459. wc->exports.winrt_capture_render(
  460. wc->capture_winrt, opaque);
  461. } else {
  462. wc->exports.winrt_capture_free(
  463. wc->capture_winrt);
  464. wc->capture_winrt = NULL;
  465. }
  466. }
  467. } else {
  468. dc_capture_render(&wc->capture, opaque);
  469. }
  470. UNUSED_PARAMETER(effect);
  471. }
  472. struct obs_source_info window_capture_info = {
  473. .id = "window_capture",
  474. .type = OBS_SOURCE_TYPE_INPUT,
  475. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  476. .get_name = wc_getname,
  477. .create = wc_create,
  478. .destroy = wc_destroy,
  479. .update = wc_update,
  480. .video_render = wc_render,
  481. .hide = wc_hide,
  482. .video_tick = wc_tick,
  483. .get_width = wc_width,
  484. .get_height = wc_height,
  485. .get_defaults = wc_defaults,
  486. .get_properties = wc_properties,
  487. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  488. };