window-capture.c 15 KB

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