window-capture.c 14 KB

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