window-capture.c 15 KB

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