window-capture.c 14 KB

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