window-capture.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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)(BOOL cursor,
  27. HWND window,
  28. BOOL client_area);
  29. typedef void (*PFN_winrt_capture_free)(struct winrt_capture *capture);
  30. typedef BOOL (*PFN_winrt_capture_active)(const struct winrt_capture *capture);
  31. typedef void (*PFN_winrt_capture_show_cursor)(struct winrt_capture *capture,
  32. BOOL visible);
  33. typedef void (*PFN_winrt_capture_render)(struct winrt_capture *capture,
  34. gs_effect_t *effect);
  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 winrt_capture_init;
  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 wgc_supported;
  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. 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, wc->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);
  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. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  199. {
  200. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  201. wc->source = source;
  202. pthread_mutex_init(&wc->update_mutex, NULL);
  203. obs_enter_graphics();
  204. const bool uses_d3d11 = gs_get_device_type() == GS_DEVICE_DIRECT3D_11;
  205. obs_leave_graphics();
  206. if (uses_d3d11) {
  207. static const char *const module = "libobs-winrt";
  208. wc->winrt_module = os_dlopen(module);
  209. if (wc->winrt_module &&
  210. load_winrt_imports(&wc->exports, wc->winrt_module,
  211. module) &&
  212. wc->exports.winrt_capture_supported()) {
  213. wc->wgc_supported = true;
  214. }
  215. }
  216. update_settings(wc, settings);
  217. log_settings(wc, settings);
  218. return wc;
  219. }
  220. static void wc_actual_destroy(void *data)
  221. {
  222. struct window_capture *wc = data;
  223. if (wc->capture_winrt) {
  224. wc->exports.winrt_capture_free(wc->capture_winrt);
  225. }
  226. obs_enter_graphics();
  227. dc_capture_free(&wc->capture);
  228. obs_leave_graphics();
  229. bfree(wc->title);
  230. bfree(wc->class);
  231. bfree(wc->executable);
  232. if (wc->winrt_module)
  233. os_dlclose(wc->winrt_module);
  234. pthread_mutex_destroy(&wc->update_mutex);
  235. bfree(wc);
  236. }
  237. static void wc_destroy(void *data)
  238. {
  239. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  240. }
  241. static void wc_update(void *data, obs_data_t *settings)
  242. {
  243. struct window_capture *wc = data;
  244. update_settings(wc, settings);
  245. log_settings(wc, settings);
  246. /* forces a reset */
  247. wc->window = NULL;
  248. wc->check_window_timer = WC_CHECK_TIMER;
  249. wc->previously_failed = false;
  250. }
  251. static uint32_t wc_width(void *data)
  252. {
  253. struct window_capture *wc = data;
  254. return (wc->method == METHOD_WGC)
  255. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  256. : wc->capture.width;
  257. }
  258. static uint32_t wc_height(void *data)
  259. {
  260. struct window_capture *wc = data;
  261. return (wc->method == METHOD_WGC)
  262. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  263. : wc->capture.height;
  264. }
  265. static void wc_defaults(obs_data_t *defaults)
  266. {
  267. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  268. obs_data_set_default_bool(defaults, "cursor", true);
  269. obs_data_set_default_bool(defaults, "compatibility", false);
  270. obs_data_set_default_bool(defaults, "client_area", true);
  271. }
  272. static void update_settings_visibility(obs_properties_t *props,
  273. struct window_capture *wc)
  274. {
  275. pthread_mutex_lock(&wc->update_mutex);
  276. const enum window_capture_method method = wc->method;
  277. const bool bitblt_options = method == METHOD_BITBLT;
  278. const bool wgc_options = method == METHOD_WGC;
  279. const bool wgc_cursor_toggle =
  280. wgc_options &&
  281. wc->exports.winrt_capture_cursor_toggle_supported();
  282. obs_property_t *p = obs_properties_get(props, "cursor");
  283. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  284. p = obs_properties_get(props, "compatibility");
  285. obs_property_set_visible(p, bitblt_options);
  286. p = obs_properties_get(props, "client_area");
  287. obs_property_set_visible(p, wgc_options);
  288. pthread_mutex_unlock(&wc->update_mutex);
  289. }
  290. static bool wc_capture_method_changed(obs_properties_t *props,
  291. obs_property_t *p, obs_data_t *settings)
  292. {
  293. UNUSED_PARAMETER(p);
  294. struct window_capture *wc = obs_properties_get_param(props);
  295. update_settings(wc, settings);
  296. update_settings_visibility(props, wc);
  297. return true;
  298. }
  299. extern bool check_window_property_setting(obs_properties_t *ppts,
  300. obs_property_t *p,
  301. obs_data_t *settings, const char *val,
  302. size_t idx);
  303. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  304. obs_data_t *settings)
  305. {
  306. struct window_capture *wc = obs_properties_get_param(props);
  307. update_settings(wc, settings);
  308. update_settings_visibility(props, wc);
  309. check_window_property_setting(props, p, settings, "window", 0);
  310. return true;
  311. }
  312. static obs_properties_t *wc_properties(void *data)
  313. {
  314. struct window_capture *wc = data;
  315. obs_properties_t *ppts = obs_properties_create();
  316. obs_properties_set_param(ppts, wc, NULL);
  317. obs_property_t *p;
  318. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  319. OBS_COMBO_TYPE_LIST,
  320. OBS_COMBO_FORMAT_STRING);
  321. fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  322. obs_property_set_modified_callback(p, wc_window_changed);
  323. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  324. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  325. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  326. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  327. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  328. obs_property_list_item_disable(p, 2, !wc->wgc_supported);
  329. obs_property_set_modified_callback(p, wc_capture_method_changed);
  330. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  331. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  332. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  333. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  334. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  335. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  336. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  337. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  338. return ppts;
  339. }
  340. static void wc_hide(void *data)
  341. {
  342. struct window_capture *wc = data;
  343. if (wc->capture_winrt) {
  344. wc->exports.winrt_capture_free(wc->capture_winrt);
  345. wc->capture_winrt = NULL;
  346. }
  347. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  348. }
  349. #define RESIZE_CHECK_TIME 0.2f
  350. #define CURSOR_CHECK_TIME 0.2f
  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. return;
  361. wc->check_window_timer += seconds;
  362. if (wc->check_window_timer < WC_CHECK_TIMER) {
  363. if (wc->capture.valid)
  364. dc_capture_free(&wc->capture);
  365. return;
  366. }
  367. if (wc->capture_winrt) {
  368. wc->exports.winrt_capture_free(wc->capture_winrt);
  369. wc->capture_winrt = NULL;
  370. }
  371. wc->check_window_timer = 0.0f;
  372. wc->window = (wc->method == METHOD_WGC)
  373. ? find_window_top_level(INCLUDE_MINIMIZED,
  374. wc->priority,
  375. wc->class,
  376. wc->title,
  377. wc->executable)
  378. : find_window(INCLUDE_MINIMIZED,
  379. wc->priority, wc->class,
  380. wc->title, wc->executable);
  381. if (!wc->window) {
  382. if (wc->capture.valid)
  383. dc_capture_free(&wc->capture);
  384. return;
  385. }
  386. wc->previously_failed = false;
  387. reset_capture = true;
  388. } else if (IsIconic(wc->window)) {
  389. return;
  390. }
  391. wc->cursor_check_time += seconds;
  392. if (wc->cursor_check_time > CURSOR_CHECK_TIME) {
  393. DWORD foreground_pid, target_pid;
  394. // Can't just compare the window handle in case of app with child windows
  395. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  396. &foreground_pid))
  397. foreground_pid = 0;
  398. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  399. target_pid = 0;
  400. const bool cursor_hidden = foreground_pid && target_pid &&
  401. foreground_pid != target_pid;
  402. wc->capture.cursor_hidden = cursor_hidden;
  403. if (wc->capture_winrt)
  404. wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  405. !cursor_hidden);
  406. wc->cursor_check_time = 0.0f;
  407. }
  408. obs_enter_graphics();
  409. if (wc->method == METHOD_BITBLT) {
  410. GetClientRect(wc->window, &rect);
  411. if (!reset_capture) {
  412. wc->resize_timer += seconds;
  413. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  414. if ((rect.bottom - rect.top) !=
  415. (wc->last_rect.bottom -
  416. wc->last_rect.top) ||
  417. (rect.right - rect.left) !=
  418. (wc->last_rect.right -
  419. wc->last_rect.left))
  420. reset_capture = true;
  421. wc->resize_timer = 0.0f;
  422. }
  423. }
  424. if (reset_capture) {
  425. wc->resize_timer = 0.0f;
  426. wc->last_rect = rect;
  427. dc_capture_free(&wc->capture);
  428. dc_capture_init(&wc->capture, 0, 0,
  429. rect.right - rect.left,
  430. rect.bottom - rect.top, wc->cursor,
  431. wc->compatibility);
  432. }
  433. dc_capture_capture(&wc->capture, wc->window);
  434. } else if (wc->method == METHOD_WGC) {
  435. if (wc->window && (wc->capture_winrt == NULL)) {
  436. if (!wc->previously_failed) {
  437. wc->capture_winrt =
  438. wc->exports.winrt_capture_init(
  439. wc->cursor, wc->window,
  440. wc->client_area);
  441. if (!wc->capture_winrt) {
  442. wc->previously_failed = true;
  443. }
  444. }
  445. }
  446. }
  447. obs_leave_graphics();
  448. }
  449. static void wc_render(void *data, gs_effect_t *effect)
  450. {
  451. struct window_capture *wc = data;
  452. gs_effect_t *const opaque = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  453. if (wc->method == METHOD_WGC) {
  454. if (wc->capture_winrt) {
  455. if (wc->exports.winrt_capture_active(
  456. wc->capture_winrt)) {
  457. wc->exports.winrt_capture_render(
  458. wc->capture_winrt, opaque);
  459. } else {
  460. wc->exports.winrt_capture_free(
  461. wc->capture_winrt);
  462. wc->capture_winrt = NULL;
  463. }
  464. }
  465. } else {
  466. dc_capture_render(&wc->capture, opaque);
  467. }
  468. UNUSED_PARAMETER(effect);
  469. }
  470. struct obs_source_info window_capture_info = {
  471. .id = "window_capture",
  472. .type = OBS_SOURCE_TYPE_INPUT,
  473. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  474. .get_name = wc_getname,
  475. .create = wc_create,
  476. .destroy = wc_destroy,
  477. .update = wc_update,
  478. .video_render = wc_render,
  479. .hide = wc_hide,
  480. .video_tick = wc_tick,
  481. .get_width = wc_width,
  482. .get_height = wc_height,
  483. .get_defaults = wc_defaults,
  484. .get_properties = wc_properties,
  485. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  486. };