1
0

window-capture.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 update_settings(struct window_capture *wc, obs_data_t *s)
  121. {
  122. int method = (int)obs_data_get_int(s, "method");
  123. const char *window = obs_data_get_string(s, "window");
  124. int priority = (int)obs_data_get_int(s, "priority");
  125. bfree(wc->title);
  126. bfree(wc->class);
  127. bfree(wc->executable);
  128. build_window_strings(window, &wc->class, &wc->title, &wc->executable);
  129. wc->method = choose_method(method, wc->wgc_supported, wc->class);
  130. wc->priority = (enum window_priority)priority;
  131. wc->cursor = obs_data_get_bool(s, "cursor");
  132. wc->use_wildcards = obs_data_get_bool(s, "use_wildcards");
  133. wc->compatibility = obs_data_get_bool(s, "compatibility");
  134. wc->client_area = obs_data_get_bool(s, "client_area");
  135. if (wc->title != NULL) {
  136. blog(LOG_INFO,
  137. "[window-capture: '%s'] update settings:\n"
  138. "\texecutable: %s\n"
  139. "\tmethod selected: %s\n"
  140. "\tmethod chosen: %s\n",
  141. obs_source_get_name(wc->source), wc->executable,
  142. get_method_name(method), get_method_name(wc->method));
  143. blog(LOG_DEBUG, "\tclass: %s", wc->class);
  144. }
  145. }
  146. /* ------------------------------------------------------------------------- */
  147. static const char *wc_getname(void *unused)
  148. {
  149. UNUSED_PARAMETER(unused);
  150. return TEXT_WINDOW_CAPTURE;
  151. }
  152. #define WINRT_IMPORT(func) \
  153. do { \
  154. exports->func = os_dlsym(module, #func); \
  155. if (!exports->func) { \
  156. success = false; \
  157. blog(LOG_ERROR, \
  158. "Could not load function '%s' from " \
  159. "module '%s'", \
  160. #func, module_name); \
  161. } \
  162. } while (false)
  163. static bool load_winrt_imports(struct winrt_exports *exports, void *module,
  164. const char *module_name)
  165. {
  166. bool success = true;
  167. WINRT_IMPORT(winrt_capture_supported);
  168. WINRT_IMPORT(winrt_capture_cursor_toggle_supported);
  169. WINRT_IMPORT(winrt_capture_init);
  170. WINRT_IMPORT(winrt_capture_free);
  171. WINRT_IMPORT(winrt_capture_active);
  172. WINRT_IMPORT(winrt_capture_show_cursor);
  173. WINRT_IMPORT(winrt_capture_render);
  174. WINRT_IMPORT(winrt_capture_width);
  175. WINRT_IMPORT(winrt_capture_height);
  176. return success;
  177. }
  178. static void *wc_create(obs_data_t *settings, obs_source_t *source)
  179. {
  180. struct window_capture *wc = bzalloc(sizeof(struct window_capture));
  181. wc->source = source;
  182. obs_enter_graphics();
  183. const bool uses_d3d11 = gs_get_device_type() == GS_DEVICE_DIRECT3D_11;
  184. obs_leave_graphics();
  185. if (uses_d3d11) {
  186. static const char *const module = "libobs-winrt";
  187. bool use_winrt_capture = false;
  188. wc->winrt_module = os_dlopen(module);
  189. if (wc->winrt_module &&
  190. load_winrt_imports(&wc->exports, wc->winrt_module,
  191. module) &&
  192. wc->exports.winrt_capture_supported()) {
  193. wc->wgc_supported = true;
  194. }
  195. }
  196. update_settings(wc, settings);
  197. return wc;
  198. }
  199. static void wc_actual_destroy(void *data)
  200. {
  201. struct window_capture *wc = data;
  202. if (wc->capture_winrt) {
  203. wc->exports.winrt_capture_free(wc->capture_winrt);
  204. }
  205. obs_enter_graphics();
  206. dc_capture_free(&wc->capture);
  207. obs_leave_graphics();
  208. bfree(wc->title);
  209. bfree(wc->class);
  210. bfree(wc->executable);
  211. if (wc->winrt_module)
  212. os_dlclose(wc->winrt_module);
  213. bfree(wc);
  214. }
  215. static void wc_destroy(void *data)
  216. {
  217. obs_queue_task(OBS_TASK_GRAPHICS, wc_actual_destroy, data, false);
  218. }
  219. static void wc_update(void *data, obs_data_t *settings)
  220. {
  221. struct window_capture *wc = data;
  222. update_settings(wc, settings);
  223. /* forces a reset */
  224. wc->window = NULL;
  225. wc->check_window_timer = WC_CHECK_TIMER;
  226. wc->previously_failed = false;
  227. }
  228. static uint32_t wc_width(void *data)
  229. {
  230. struct window_capture *wc = data;
  231. return (wc->method == METHOD_WGC)
  232. ? wc->exports.winrt_capture_width(wc->capture_winrt)
  233. : wc->capture.width;
  234. }
  235. static uint32_t wc_height(void *data)
  236. {
  237. struct window_capture *wc = data;
  238. return (wc->method == METHOD_WGC)
  239. ? wc->exports.winrt_capture_height(wc->capture_winrt)
  240. : wc->capture.height;
  241. }
  242. static void wc_defaults(obs_data_t *defaults)
  243. {
  244. obs_data_set_default_int(defaults, "method", METHOD_AUTO);
  245. obs_data_set_default_bool(defaults, "cursor", true);
  246. obs_data_set_default_bool(defaults, "compatibility", false);
  247. obs_data_set_default_bool(defaults, "client_area", true);
  248. }
  249. static void update_settings_visibility(obs_properties_t *props,
  250. struct window_capture *wc)
  251. {
  252. const enum window_capture_method method = wc->method;
  253. const bool bitblt_options = method == METHOD_BITBLT;
  254. const bool wgc_options = method == METHOD_WGC;
  255. const bool wgc_cursor_toggle =
  256. wgc_options &&
  257. wc->exports.winrt_capture_cursor_toggle_supported();
  258. obs_property_t *p = obs_properties_get(props, "cursor");
  259. obs_property_set_visible(p, bitblt_options || wgc_cursor_toggle);
  260. p = obs_properties_get(props, "compatibility");
  261. obs_property_set_visible(p, bitblt_options);
  262. p = obs_properties_get(props, "client_area");
  263. obs_property_set_visible(p, wgc_options);
  264. }
  265. static bool wc_capture_method_changed(obs_properties_t *props,
  266. obs_property_t *p, obs_data_t *settings)
  267. {
  268. struct window_capture *wc = obs_properties_get_param(props);
  269. update_settings(wc, settings);
  270. update_settings_visibility(props, wc);
  271. return true;
  272. }
  273. extern bool check_window_property_setting(obs_properties_t *ppts,
  274. obs_property_t *p,
  275. obs_data_t *settings, const char *val,
  276. size_t idx);
  277. static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
  278. obs_data_t *settings)
  279. {
  280. struct window_capture *wc = obs_properties_get_param(props);
  281. update_settings(wc, settings);
  282. update_settings_visibility(props, wc);
  283. check_window_property_setting(props, p, settings, "window", 0);
  284. return true;
  285. }
  286. static obs_properties_t *wc_properties(void *data)
  287. {
  288. struct window_capture *wc = data;
  289. obs_properties_t *ppts = obs_properties_create();
  290. obs_properties_set_param(ppts, wc, NULL);
  291. obs_property_t *p;
  292. p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
  293. OBS_COMBO_TYPE_LIST,
  294. OBS_COMBO_FORMAT_STRING);
  295. fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
  296. obs_property_set_modified_callback(p, wc_window_changed);
  297. p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
  298. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  299. obs_property_list_add_int(p, TEXT_METHOD_AUTO, METHOD_AUTO);
  300. obs_property_list_add_int(p, TEXT_METHOD_BITBLT, METHOD_BITBLT);
  301. obs_property_list_add_int(p, TEXT_METHOD_WGC, METHOD_WGC);
  302. obs_property_list_item_disable(p, 2, !wc->wgc_supported);
  303. obs_property_set_modified_callback(p, wc_capture_method_changed);
  304. p = obs_properties_add_list(ppts, "priority", TEXT_MATCH_PRIORITY,
  305. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  306. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  307. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  308. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  309. obs_properties_add_bool(ppts, "cursor", TEXT_CAPTURE_CURSOR);
  310. obs_properties_add_bool(ppts, "compatibility", TEXT_COMPATIBILITY);
  311. obs_properties_add_bool(ppts, "client_area", TEXT_CLIENT_AREA);
  312. return ppts;
  313. }
  314. static void wc_hide(void *data)
  315. {
  316. struct window_capture *wc = data;
  317. if (wc->capture_winrt) {
  318. wc->exports.winrt_capture_free(wc->capture_winrt);
  319. wc->capture_winrt = NULL;
  320. }
  321. memset(&wc->last_rect, 0, sizeof(wc->last_rect));
  322. }
  323. #define RESIZE_CHECK_TIME 0.2f
  324. #define CURSOR_CHECK_TIME 0.2f
  325. static void wc_tick(void *data, float seconds)
  326. {
  327. struct window_capture *wc = data;
  328. RECT rect;
  329. bool reset_capture = false;
  330. if (!obs_source_showing(wc->source))
  331. return;
  332. if (!wc->window || !IsWindow(wc->window)) {
  333. if (!wc->title && !wc->class)
  334. return;
  335. wc->check_window_timer += seconds;
  336. if (wc->check_window_timer < WC_CHECK_TIMER) {
  337. if (wc->capture.valid)
  338. dc_capture_free(&wc->capture);
  339. return;
  340. }
  341. if (wc->capture_winrt) {
  342. wc->exports.winrt_capture_free(wc->capture_winrt);
  343. wc->capture_winrt = NULL;
  344. }
  345. wc->check_window_timer = 0.0f;
  346. wc->window = (wc->method == METHOD_WGC)
  347. ? find_window_top_level(INCLUDE_MINIMIZED,
  348. wc->priority,
  349. wc->class,
  350. wc->title,
  351. wc->executable)
  352. : find_window(INCLUDE_MINIMIZED,
  353. wc->priority, wc->class,
  354. wc->title, wc->executable);
  355. if (!wc->window) {
  356. if (wc->capture.valid)
  357. dc_capture_free(&wc->capture);
  358. return;
  359. }
  360. wc->previously_failed = false;
  361. reset_capture = true;
  362. } else if (IsIconic(wc->window)) {
  363. return;
  364. }
  365. wc->cursor_check_time += seconds;
  366. if (wc->cursor_check_time > CURSOR_CHECK_TIME) {
  367. DWORD foreground_pid, target_pid;
  368. // Can't just compare the window handle in case of app with child windows
  369. if (!GetWindowThreadProcessId(GetForegroundWindow(),
  370. &foreground_pid))
  371. foreground_pid = 0;
  372. if (!GetWindowThreadProcessId(wc->window, &target_pid))
  373. target_pid = 0;
  374. const bool cursor_hidden = foreground_pid && target_pid &&
  375. foreground_pid != target_pid;
  376. wc->capture.cursor_hidden = cursor_hidden;
  377. if (wc->capture_winrt)
  378. wc->exports.winrt_capture_show_cursor(wc->capture_winrt,
  379. !cursor_hidden);
  380. wc->cursor_check_time = 0.0f;
  381. }
  382. obs_enter_graphics();
  383. if (wc->method == METHOD_BITBLT) {
  384. GetClientRect(wc->window, &rect);
  385. if (!reset_capture) {
  386. wc->resize_timer += seconds;
  387. if (wc->resize_timer >= RESIZE_CHECK_TIME) {
  388. if ((rect.bottom - rect.top) !=
  389. (wc->last_rect.bottom -
  390. wc->last_rect.top) ||
  391. (rect.right - rect.left) !=
  392. (wc->last_rect.right -
  393. wc->last_rect.left))
  394. reset_capture = true;
  395. wc->resize_timer = 0.0f;
  396. }
  397. }
  398. if (reset_capture) {
  399. wc->resize_timer = 0.0f;
  400. wc->last_rect = rect;
  401. dc_capture_free(&wc->capture);
  402. dc_capture_init(&wc->capture, 0, 0,
  403. rect.right - rect.left,
  404. rect.bottom - rect.top, wc->cursor,
  405. wc->compatibility);
  406. }
  407. dc_capture_capture(&wc->capture, wc->window);
  408. } else if (wc->method == METHOD_WGC) {
  409. if (wc->window && (wc->capture_winrt == NULL)) {
  410. if (!wc->previously_failed) {
  411. wc->capture_winrt =
  412. wc->exports.winrt_capture_init(
  413. wc->cursor, wc->window,
  414. wc->client_area);
  415. if (!wc->capture_winrt) {
  416. wc->previously_failed = true;
  417. }
  418. }
  419. }
  420. }
  421. obs_leave_graphics();
  422. }
  423. static void wc_render(void *data, gs_effect_t *effect)
  424. {
  425. struct window_capture *wc = data;
  426. gs_effect_t *const opaque = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  427. if (wc->method == METHOD_WGC) {
  428. if (wc->capture_winrt) {
  429. if (wc->exports.winrt_capture_active(
  430. wc->capture_winrt)) {
  431. wc->exports.winrt_capture_render(
  432. wc->capture_winrt, opaque);
  433. } else {
  434. wc->exports.winrt_capture_free(
  435. wc->capture_winrt);
  436. wc->capture_winrt = NULL;
  437. }
  438. }
  439. } else {
  440. dc_capture_render(&wc->capture, opaque);
  441. }
  442. UNUSED_PARAMETER(effect);
  443. }
  444. struct obs_source_info window_capture_info = {
  445. .id = "window_capture",
  446. .type = OBS_SOURCE_TYPE_INPUT,
  447. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  448. .get_name = wc_getname,
  449. .create = wc_create,
  450. .destroy = wc_destroy,
  451. .update = wc_update,
  452. .video_render = wc_render,
  453. .hide = wc_hide,
  454. .video_tick = wc_tick,
  455. .get_width = wc_width,
  456. .get_height = wc_height,
  457. .get_defaults = wc_defaults,
  458. .get_properties = wc_properties,
  459. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  460. };