game-capture.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. #include <inttypes.h>
  2. #include <obs-module.h>
  3. #include <util/platform.h>
  4. #include <windows.h>
  5. #include <dxgi.h>
  6. #include <emmintrin.h>
  7. #include <ipc-util/pipe.h>
  8. #include "obfuscate.h"
  9. #include "inject-library.h"
  10. #include "graphics-hook-info.h"
  11. #include "window-helpers.h"
  12. #include "cursor-capture.h"
  13. #define do_log(level, format, ...) \
  14. blog(level, "[game-capture: '%s'] " format, \
  15. obs_source_get_name(gc->source), ##__VA_ARGS__)
  16. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  17. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  18. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  19. #define SETTING_ANY_FULLSCREEN "capture_any_fullscreen"
  20. #define SETTING_CAPTURE_WINDOW "window"
  21. #define SETTING_ACTIVE_WINDOW "active_window"
  22. #define SETTING_WINDOW_PRIORITY "priority"
  23. #define SETTING_COMPATIBILITY "sli_compatibility"
  24. #define SETTING_FORCE_SCALING "force_scaling"
  25. #define SETTING_SCALE_RES "scale_res"
  26. #define SETTING_CURSOR "capture_cursor"
  27. #define SETTING_TRANSPARENCY "allow_transparency"
  28. #define SETTING_LIMIT_FRAMERATE "limit_framerate"
  29. #define SETTING_CAPTURE_OVERLAYS "capture_overlays"
  30. #define SETTING_ANTI_CHEAT_HOOK "anti_cheat_hook"
  31. #define TEXT_GAME_CAPTURE obs_module_text("GameCapture")
  32. #define TEXT_ANY_FULLSCREEN obs_module_text("GameCapture.AnyFullscreen")
  33. #define TEXT_SLI_COMPATIBILITY obs_module_text("Compatibility")
  34. #define TEXT_ALLOW_TRANSPARENCY obs_module_text("AllowTransparency")
  35. #define TEXT_FORCE_SCALING obs_module_text("GameCapture.ForceScaling")
  36. #define TEXT_SCALE_RES obs_module_text("GameCapture.ScaleRes")
  37. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  38. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  39. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  40. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  41. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  42. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  43. #define TEXT_LIMIT_FRAMERATE obs_module_text("GameCapture.LimitFramerate")
  44. #define TEXT_CAPTURE_OVERLAYS obs_module_text("GameCapture.CaptureOverlays")
  45. #define TEXT_ANTI_CHEAT_HOOK obs_module_text("GameCapture.AntiCheatHook")
  46. #define DEFAULT_RETRY_INTERVAL 2.0f
  47. #define ERROR_RETRY_INTERVAL 4.0f
  48. struct game_capture_config {
  49. char *title;
  50. char *class;
  51. char *executable;
  52. enum window_priority priority;
  53. uint32_t scale_cx;
  54. uint32_t scale_cy;
  55. bool cursor : 1;
  56. bool force_shmem : 1;
  57. bool capture_any_fullscreen : 1;
  58. bool force_scaling : 1;
  59. bool allow_transparency : 1;
  60. bool limit_framerate : 1;
  61. bool capture_overlays : 1;
  62. bool anticheat_hook : 1;
  63. };
  64. struct game_capture {
  65. obs_source_t *source;
  66. struct cursor_data cursor_data;
  67. HANDLE injector_process;
  68. uint32_t cx;
  69. uint32_t cy;
  70. uint32_t pitch;
  71. DWORD process_id;
  72. DWORD thread_id;
  73. HWND next_window;
  74. HWND window;
  75. float retry_time;
  76. float fps_reset_time;
  77. float retry_interval;
  78. bool wait_for_target_startup : 1;
  79. bool active : 1;
  80. bool capturing : 1;
  81. bool activate_hook : 1;
  82. bool process_is_64bit : 1;
  83. bool error_acquiring : 1;
  84. bool dwm_capture : 1;
  85. bool initial_config : 1;
  86. bool convert_16bit : 1;
  87. struct game_capture_config config;
  88. ipc_pipe_server_t pipe;
  89. gs_texture_t *texture;
  90. struct hook_info *global_hook_info;
  91. HANDLE keep_alive;
  92. HANDLE hook_restart;
  93. HANDLE hook_stop;
  94. HANDLE hook_ready;
  95. HANDLE hook_exit;
  96. HANDLE hook_data_map;
  97. HANDLE global_hook_info_map;
  98. HANDLE target_process;
  99. HANDLE texture_mutexes[2];
  100. union {
  101. struct {
  102. struct shmem_data *shmem_data;
  103. uint8_t *texture_buffers[2];
  104. };
  105. struct shtex_data *shtex_data;
  106. void *data;
  107. };
  108. void (*copy_texture)(struct game_capture*);
  109. };
  110. struct graphics_offsets offsets32 = {0};
  111. struct graphics_offsets offsets64 = {0};
  112. static inline enum gs_color_format convert_format(uint32_t format)
  113. {
  114. switch (format) {
  115. case DXGI_FORMAT_R8G8B8A8_UNORM: return GS_RGBA;
  116. case DXGI_FORMAT_B8G8R8X8_UNORM: return GS_BGRX;
  117. case DXGI_FORMAT_B8G8R8A8_UNORM: return GS_BGRA;
  118. case DXGI_FORMAT_R10G10B10A2_UNORM: return GS_R10G10B10A2;
  119. case DXGI_FORMAT_R16G16B16A16_UNORM: return GS_RGBA16;
  120. case DXGI_FORMAT_R16G16B16A16_FLOAT: return GS_RGBA16F;
  121. case DXGI_FORMAT_R32G32B32A32_FLOAT: return GS_RGBA32F;
  122. }
  123. return GS_UNKNOWN;
  124. }
  125. static void close_handle(HANDLE *p_handle)
  126. {
  127. HANDLE handle = *p_handle;
  128. if (handle) {
  129. if (handle != INVALID_HANDLE_VALUE)
  130. CloseHandle(handle);
  131. *p_handle = NULL;
  132. }
  133. }
  134. static inline HMODULE kernel32(void)
  135. {
  136. static HMODULE kernel32_handle = NULL;
  137. if (!kernel32_handle)
  138. kernel32_handle = GetModuleHandleW(L"kernel32");
  139. return kernel32_handle;
  140. }
  141. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  142. DWORD process_id)
  143. {
  144. static HANDLE (WINAPI *open_process_proc)(DWORD, BOOL, DWORD) = NULL;
  145. if (!open_process_proc)
  146. open_process_proc = get_obfuscated_func(kernel32(),
  147. "NuagUykjcxr", 0x1B694B59451ULL);
  148. return open_process_proc(desired_access, inherit_handle, process_id);
  149. }
  150. static void stop_capture(struct game_capture *gc)
  151. {
  152. ipc_pipe_server_free(&gc->pipe);
  153. if (gc->hook_stop) {
  154. SetEvent(gc->hook_stop);
  155. }
  156. if (gc->global_hook_info) {
  157. UnmapViewOfFile(gc->global_hook_info);
  158. gc->global_hook_info = NULL;
  159. }
  160. if (gc->data) {
  161. UnmapViewOfFile(gc->data);
  162. gc->data = NULL;
  163. }
  164. close_handle(&gc->keep_alive);
  165. close_handle(&gc->hook_restart);
  166. close_handle(&gc->hook_stop);
  167. close_handle(&gc->hook_ready);
  168. close_handle(&gc->hook_exit);
  169. close_handle(&gc->hook_data_map);
  170. close_handle(&gc->global_hook_info_map);
  171. close_handle(&gc->target_process);
  172. close_handle(&gc->texture_mutexes[0]);
  173. close_handle(&gc->texture_mutexes[1]);
  174. if (gc->texture) {
  175. obs_enter_graphics();
  176. gs_texture_destroy(gc->texture);
  177. obs_leave_graphics();
  178. gc->texture = NULL;
  179. }
  180. gc->copy_texture = NULL;
  181. gc->wait_for_target_startup = false;
  182. gc->active = false;
  183. gc->capturing = false;
  184. }
  185. static inline void free_config(struct game_capture_config *config)
  186. {
  187. bfree(config->title);
  188. bfree(config->class);
  189. bfree(config->executable);
  190. memset(config, 0, sizeof(*config));
  191. }
  192. static void game_capture_destroy(void *data)
  193. {
  194. struct game_capture *gc = data;
  195. stop_capture(gc);
  196. obs_enter_graphics();
  197. cursor_data_free(&gc->cursor_data);
  198. obs_leave_graphics();
  199. free_config(&gc->config);
  200. bfree(gc);
  201. }
  202. static inline void get_config(struct game_capture_config *cfg,
  203. obs_data_t *settings, const char *window)
  204. {
  205. int ret;
  206. const char *scale_str;
  207. build_window_strings(window, &cfg->class, &cfg->title,
  208. &cfg->executable);
  209. cfg->capture_any_fullscreen = obs_data_get_bool(settings,
  210. SETTING_ANY_FULLSCREEN);
  211. cfg->priority = (enum window_priority)obs_data_get_int(settings,
  212. SETTING_WINDOW_PRIORITY);
  213. cfg->force_shmem = obs_data_get_bool(settings,
  214. SETTING_COMPATIBILITY);
  215. cfg->cursor = obs_data_get_bool(settings, SETTING_CURSOR);
  216. cfg->allow_transparency = obs_data_get_bool(settings,
  217. SETTING_TRANSPARENCY);
  218. cfg->force_scaling = obs_data_get_bool(settings,
  219. SETTING_FORCE_SCALING);
  220. cfg->limit_framerate = obs_data_get_bool(settings,
  221. SETTING_LIMIT_FRAMERATE);
  222. cfg->capture_overlays = obs_data_get_bool(settings,
  223. SETTING_CAPTURE_OVERLAYS);
  224. cfg->anticheat_hook = obs_data_get_bool(settings,
  225. SETTING_ANTI_CHEAT_HOOK);
  226. scale_str = obs_data_get_string(settings, SETTING_SCALE_RES);
  227. ret = sscanf(scale_str, "%"PRIu32"x%"PRIu32,
  228. &cfg->scale_cx, &cfg->scale_cy);
  229. cfg->scale_cx &= ~2;
  230. cfg->scale_cy &= ~2;
  231. if (cfg->force_scaling) {
  232. if (ret != 2 || cfg->scale_cx == 0 || cfg->scale_cy == 0) {
  233. cfg->scale_cx = 0;
  234. cfg->scale_cy = 0;
  235. }
  236. }
  237. }
  238. static inline int s_cmp(const char *str1, const char *str2)
  239. {
  240. if (!str1 || !str2)
  241. return -1;
  242. return strcmp(str1, str2);
  243. }
  244. static inline bool capture_needs_reset(struct game_capture_config *cfg1,
  245. struct game_capture_config *cfg2)
  246. {
  247. if (cfg1->capture_any_fullscreen != cfg2->capture_any_fullscreen) {
  248. return true;
  249. } else if (!cfg1->capture_any_fullscreen &&
  250. (s_cmp(cfg1->class, cfg2->class) != 0 ||
  251. s_cmp(cfg1->title, cfg2->title) != 0 ||
  252. s_cmp(cfg1->executable, cfg2->executable) != 0 ||
  253. cfg1->priority != cfg2->priority)) {
  254. return true;
  255. } else if (cfg1->force_scaling != cfg2->force_scaling) {
  256. return true;
  257. } else if (cfg1->force_scaling &&
  258. (cfg1->scale_cx != cfg2->scale_cx ||
  259. cfg1->scale_cy != cfg2->scale_cy)) {
  260. return true;
  261. } else if (cfg1->force_shmem != cfg2->force_shmem) {
  262. return true;
  263. } else if (cfg1->limit_framerate != cfg2->limit_framerate) {
  264. return true;
  265. } else if (cfg1->capture_overlays != cfg2->capture_overlays) {
  266. return true;
  267. }
  268. return false;
  269. }
  270. static void game_capture_update(void *data, obs_data_t *settings)
  271. {
  272. struct game_capture *gc = data;
  273. struct game_capture_config cfg;
  274. bool reset_capture = false;
  275. const char *window = obs_data_get_string(settings,
  276. SETTING_CAPTURE_WINDOW);
  277. get_config(&cfg, settings, window);
  278. reset_capture = capture_needs_reset(&cfg, &gc->config);
  279. if (cfg.force_scaling && (cfg.scale_cx == 0 || cfg.scale_cy == 0)) {
  280. gc->error_acquiring = true;
  281. warn("error acquiring, scale is bad");
  282. } else {
  283. gc->error_acquiring = false;
  284. }
  285. free_config(&gc->config);
  286. gc->config = cfg;
  287. gc->activate_hook = !!window && !!*window;
  288. gc->retry_interval = DEFAULT_RETRY_INTERVAL;
  289. if (!gc->initial_config) {
  290. if (reset_capture) {
  291. stop_capture(gc);
  292. }
  293. } else {
  294. gc->initial_config = false;
  295. }
  296. }
  297. static void *game_capture_create(obs_data_t *settings, obs_source_t *source)
  298. {
  299. struct game_capture *gc = bzalloc(sizeof(*gc));
  300. gc->source = source;
  301. gc->initial_config = true;
  302. gc->retry_interval = DEFAULT_RETRY_INTERVAL;
  303. game_capture_update(gc, settings);
  304. return gc;
  305. }
  306. static inline HANDLE create_event_id(bool manual_reset, bool initial_state,
  307. const char *name, DWORD process_id)
  308. {
  309. char new_name[128];
  310. sprintf(new_name, "%s%lu", name, process_id);
  311. return CreateEventA(NULL, manual_reset, initial_state, new_name);
  312. }
  313. static inline HANDLE open_event_id(const char *name, DWORD process_id)
  314. {
  315. char new_name[128];
  316. sprintf(new_name, "%s%lu", name, process_id);
  317. return OpenEventA(EVENT_ALL_ACCESS, false, new_name);
  318. }
  319. #define STOP_BEING_BAD \
  320. " This is most likely due to security software. Please make sure " \
  321. "that the OBS installation folder is excluded/ignored in the " \
  322. "settings of the security software you are using."
  323. static bool check_file_integrity(struct game_capture *gc, const char *file,
  324. const char *name)
  325. {
  326. DWORD error;
  327. HANDLE handle;
  328. wchar_t *w_file = NULL;
  329. if (!file || !*file) {
  330. warn("Game capture %s not found." STOP_BEING_BAD, name);
  331. return false;
  332. }
  333. if (!os_utf8_to_wcs_ptr(file, 0, &w_file)) {
  334. warn("Could not convert file name to wide string");
  335. return false;
  336. }
  337. handle = CreateFileW(w_file, GENERIC_READ | GENERIC_EXECUTE,
  338. FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  339. bfree(w_file);
  340. if (handle != INVALID_HANDLE_VALUE) {
  341. CloseHandle(handle);
  342. return true;
  343. }
  344. error = GetLastError();
  345. if (error == ERROR_FILE_NOT_FOUND) {
  346. warn("Game capture file '%s' not found."
  347. STOP_BEING_BAD, file);
  348. } else if (error == ERROR_ACCESS_DENIED) {
  349. warn("Game capture file '%s' could not be loaded."
  350. STOP_BEING_BAD, file);
  351. } else {
  352. warn("Game capture file '%s' could not be loaded: %lu."
  353. STOP_BEING_BAD, file, error);
  354. }
  355. return false;
  356. }
  357. static inline bool is_64bit_windows(void)
  358. {
  359. #ifdef _WIN64
  360. return true;
  361. #else
  362. BOOL x86 = false;
  363. bool success = !!IsWow64Process(GetCurrentProcess(), &x86);
  364. return success && !!x86;
  365. #endif
  366. }
  367. static inline bool is_64bit_process(HANDLE process)
  368. {
  369. BOOL x86 = true;
  370. if (is_64bit_windows()) {
  371. bool success = !!IsWow64Process(process, &x86);
  372. if (!success) {
  373. return false;
  374. }
  375. }
  376. return !x86;
  377. }
  378. static inline bool open_target_process(struct game_capture *gc)
  379. {
  380. gc->target_process = open_process(
  381. PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  382. false, gc->process_id);
  383. if (!gc->target_process) {
  384. warn("could not open process: %s", gc->config.executable);
  385. return false;
  386. }
  387. gc->process_is_64bit = is_64bit_process(gc->target_process);
  388. return true;
  389. }
  390. static inline bool init_keepalive(struct game_capture *gc)
  391. {
  392. gc->keep_alive = create_event_id(false, false, EVENT_HOOK_KEEPALIVE,
  393. gc->process_id);
  394. if (!gc->keep_alive) {
  395. warn("failed to create keepalive event");
  396. return false;
  397. }
  398. return true;
  399. }
  400. static inline bool init_texture_mutexes(struct game_capture *gc)
  401. {
  402. gc->texture_mutexes[0] = get_mutex_plus_id(MUTEX_TEXTURE1,
  403. gc->process_id);
  404. gc->texture_mutexes[1] = get_mutex_plus_id(MUTEX_TEXTURE2,
  405. gc->process_id);
  406. if (!gc->texture_mutexes[0] || !gc->texture_mutexes[1]) {
  407. warn("failed to create texture mutexes: %lu", GetLastError());
  408. return false;
  409. }
  410. return true;
  411. }
  412. /* if there's already a hook in the process, then signal and start */
  413. static inline bool attempt_existing_hook(struct game_capture *gc)
  414. {
  415. gc->hook_restart = open_event_id(EVENT_CAPTURE_RESTART, gc->process_id);
  416. if (gc->hook_restart) {
  417. debug("existing hook found, signaling process: %s",
  418. gc->config.executable);
  419. SetEvent(gc->hook_restart);
  420. return true;
  421. }
  422. return false;
  423. }
  424. static inline void reset_frame_interval(struct game_capture *gc)
  425. {
  426. struct obs_video_info ovi;
  427. uint64_t interval = 0;
  428. if (obs_get_video_info(&ovi)) {
  429. interval = ovi.fps_den * 1000000000ULL / ovi.fps_num;
  430. /* Always limit capture framerate to some extent. If a game
  431. * running at 900 FPS is being captured without some sort of
  432. * limited capture interval, it will dramatically reduce
  433. * performance. */
  434. if (!gc->config.limit_framerate)
  435. interval /= 2;
  436. }
  437. gc->global_hook_info->frame_interval = interval;
  438. }
  439. static inline bool init_hook_info(struct game_capture *gc)
  440. {
  441. gc->global_hook_info_map = get_hook_info(gc->process_id);
  442. if (!gc->global_hook_info_map) {
  443. warn("init_hook_info: get_hook_info failed: %lu",
  444. GetLastError());
  445. return false;
  446. }
  447. gc->global_hook_info = MapViewOfFile(gc->global_hook_info_map,
  448. FILE_MAP_ALL_ACCESS, 0, 0,
  449. sizeof(*gc->global_hook_info));
  450. if (!gc->global_hook_info) {
  451. warn("init_hook_info: failed to map data view: %lu",
  452. GetLastError());
  453. return false;
  454. }
  455. gc->global_hook_info->offsets = gc->process_is_64bit ?
  456. offsets64 : offsets32;
  457. gc->global_hook_info->capture_overlay = gc->config.capture_overlays;
  458. gc->global_hook_info->force_shmem = gc->config.force_shmem;
  459. gc->global_hook_info->use_scale = gc->config.force_scaling;
  460. gc->global_hook_info->cx = gc->config.scale_cx;
  461. gc->global_hook_info->cy = gc->config.scale_cy;
  462. reset_frame_interval(gc);
  463. obs_enter_graphics();
  464. if (!gs_shared_texture_available())
  465. gc->global_hook_info->force_shmem = true;
  466. obs_leave_graphics();
  467. obs_enter_graphics();
  468. if (!gs_shared_texture_available())
  469. gc->global_hook_info->force_shmem = true;
  470. obs_leave_graphics();
  471. return true;
  472. }
  473. static void pipe_log(void *param, uint8_t *data, size_t size)
  474. {
  475. struct game_capture *gc = param;
  476. if (data && size)
  477. info("%s", data);
  478. }
  479. static inline bool init_pipe(struct game_capture *gc)
  480. {
  481. char name[64];
  482. sprintf(name, "%s%lu", PIPE_NAME, gc->process_id);
  483. if (!ipc_pipe_server_start(&gc->pipe, name, pipe_log, gc)) {
  484. warn("init_pipe: failed to start pipe");
  485. return false;
  486. }
  487. return true;
  488. }
  489. static inline int inject_library(HANDLE process, const wchar_t *dll)
  490. {
  491. return inject_library_obf(process, dll,
  492. "D|hkqkW`kl{k\\osofj", 0xa178ef3655e5ade7,
  493. "[uawaRzbhh{tIdkj~~", 0x561478dbd824387c,
  494. "[fr}pboIe`dlN}", 0x395bfbc9833590fd,
  495. "\\`zs}gmOzhhBq", 0x12897dd89168789a,
  496. "GbfkDaezbp~X", 0x76aff7238788f7db);
  497. }
  498. static inline bool hook_direct(struct game_capture *gc,
  499. const char *hook_path_rel)
  500. {
  501. wchar_t hook_path_abs_w[MAX_PATH];
  502. wchar_t *hook_path_rel_w;
  503. wchar_t *path_ret;
  504. HANDLE process;
  505. int ret;
  506. os_utf8_to_wcs_ptr(hook_path_rel, 0, &hook_path_rel_w);
  507. if (!hook_path_rel_w) {
  508. warn("hook_direct: could not convert string");
  509. return false;
  510. }
  511. path_ret = _wfullpath(hook_path_abs_w, hook_path_rel_w, MAX_PATH);
  512. bfree(hook_path_rel_w);
  513. if (path_ret == NULL) {
  514. warn("hook_direct: could not make absolute path");
  515. return false;
  516. }
  517. process = open_process(PROCESS_ALL_ACCESS, false, gc->process_id);
  518. if (!process) {
  519. warn("hook_direct: could not open process: %s (%lu)",
  520. gc->config.executable, GetLastError());
  521. return false;
  522. }
  523. ret = inject_library(process, hook_path_abs_w);
  524. CloseHandle(process);
  525. if (ret != 0) {
  526. warn("hook_direct: inject failed: %d", ret);
  527. return false;
  528. }
  529. return true;
  530. }
  531. static inline bool create_inject_process(struct game_capture *gc,
  532. const char *inject_path, const char *hook_dll)
  533. {
  534. wchar_t *command_line_w = malloc(4096 * sizeof(wchar_t));
  535. wchar_t *inject_path_w;
  536. wchar_t *hook_dll_w;
  537. bool anti_cheat = gc->config.anticheat_hook;
  538. PROCESS_INFORMATION pi = {0};
  539. STARTUPINFO si = {0};
  540. bool success = false;
  541. os_utf8_to_wcs_ptr(inject_path, 0, &inject_path_w);
  542. os_utf8_to_wcs_ptr(hook_dll, 0, &hook_dll_w);
  543. si.cb = sizeof(si);
  544. swprintf(command_line_w, 4096, L"\"%s\" \"%s\" %lu %lu",
  545. inject_path_w, hook_dll_w,
  546. (unsigned long)anti_cheat,
  547. anti_cheat ? gc->thread_id : gc->process_id);
  548. success = !!CreateProcessW(inject_path_w, command_line_w, NULL, NULL,
  549. false, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
  550. if (success) {
  551. CloseHandle(pi.hThread);
  552. gc->injector_process = pi.hProcess;
  553. } else {
  554. warn("Failed to create inject helper process: %lu",
  555. GetLastError());
  556. }
  557. free(command_line_w);
  558. bfree(inject_path_w);
  559. bfree(hook_dll_w);
  560. return success;
  561. }
  562. static inline bool inject_hook(struct game_capture *gc)
  563. {
  564. bool matching_architecture;
  565. bool success = false;
  566. const char *hook_dll;
  567. char *inject_path;
  568. char *hook_path;
  569. if (gc->process_is_64bit) {
  570. hook_dll = "graphics-hook64.dll";
  571. inject_path = obs_module_file("inject-helper64.exe");
  572. } else {
  573. hook_dll = "graphics-hook32.dll";
  574. inject_path = obs_module_file("inject-helper32.exe");
  575. }
  576. hook_path = obs_module_file(hook_dll);
  577. if (!check_file_integrity(gc, inject_path, "inject helper")) {
  578. goto cleanup;
  579. }
  580. if (!check_file_integrity(gc, hook_path, "graphics hook")) {
  581. goto cleanup;
  582. }
  583. #ifdef _WIN64
  584. matching_architecture = gc->process_is_64bit;
  585. #else
  586. matching_architecture = !gc->process_is_64bit;
  587. #endif
  588. if (matching_architecture && !gc->config.anticheat_hook) {
  589. info("using direct hook");
  590. success = hook_direct(gc, hook_path);
  591. } else {
  592. info("using helper (%s hook)", gc->config.anticheat_hook ?
  593. "compatibility" : "direct");
  594. success = create_inject_process(gc, inject_path, hook_dll);
  595. }
  596. cleanup:
  597. bfree(inject_path);
  598. bfree(hook_path);
  599. return success;
  600. }
  601. static bool init_hook(struct game_capture *gc)
  602. {
  603. if (gc->config.capture_any_fullscreen) {
  604. struct dstr name = {0};
  605. if (get_window_exe(&name, gc->next_window)) {
  606. info("attempting to hook fullscreen process: %s",
  607. name.array);
  608. dstr_free(&name);
  609. }
  610. } else {
  611. info("attempting to hook process: %s", gc->config.executable);
  612. }
  613. if (!open_target_process(gc)) {
  614. return false;
  615. }
  616. if (!init_keepalive(gc)) {
  617. return false;
  618. }
  619. if (!init_texture_mutexes(gc)) {
  620. return false;
  621. }
  622. if (!init_hook_info(gc)) {
  623. return false;
  624. }
  625. if (!init_pipe(gc)) {
  626. return false;
  627. }
  628. if (!attempt_existing_hook(gc)) {
  629. if (!inject_hook(gc)) {
  630. return false;
  631. }
  632. }
  633. gc->window = gc->next_window;
  634. gc->next_window = NULL;
  635. gc->active = true;
  636. return true;
  637. }
  638. static void get_fullscreen_window(struct game_capture *gc)
  639. {
  640. HWND window = GetForegroundWindow();
  641. MONITORINFO mi = {0};
  642. HMONITOR monitor;
  643. DWORD styles;
  644. RECT rect;
  645. gc->next_window = NULL;
  646. if (!window) {
  647. return;
  648. }
  649. if (!GetWindowRect(window, &rect)) {
  650. return;
  651. }
  652. /* ignore regular maximized windows */
  653. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  654. if ((styles & WS_MAXIMIZE) != 0 && (styles & WS_BORDER) != 0) {
  655. return;
  656. }
  657. monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
  658. if (!monitor) {
  659. return;
  660. }
  661. mi.cbSize = sizeof(mi);
  662. if (!GetMonitorInfo(monitor, &mi)) {
  663. return;
  664. }
  665. if (rect.left == mi.rcMonitor.left &&
  666. rect.right == mi.rcMonitor.right &&
  667. rect.bottom == mi.rcMonitor.bottom &&
  668. rect.top == mi.rcMonitor.top) {
  669. /* always wait a bit for the target process to start up before
  670. * starting the hook process; sometimes they have important
  671. * modules to load first or other hooks (such as steam) need a
  672. * little bit of time to load. ultimately this helps prevent
  673. * crashes */
  674. if (gc->wait_for_target_startup) {
  675. gc->retry_interval = 3.0f;
  676. gc->wait_for_target_startup = false;
  677. } else {
  678. gc->next_window = window;
  679. }
  680. } else {
  681. gc->wait_for_target_startup = true;
  682. }
  683. }
  684. static void get_selected_window(struct game_capture *gc)
  685. {
  686. if (strcmpi(gc->config.class, "dwm") == 0) {
  687. wchar_t class_w[512];
  688. os_utf8_to_wcs(gc->config.class, 0, class_w, 512);
  689. gc->next_window = FindWindowW(class_w, NULL);
  690. } else {
  691. gc->next_window = find_window(INCLUDE_MINIMIZED,
  692. gc->config.priority,
  693. gc->config.class,
  694. gc->config.title,
  695. gc->config.executable);
  696. }
  697. }
  698. static void try_hook(struct game_capture *gc)
  699. {
  700. if (gc->config.capture_any_fullscreen) {
  701. get_fullscreen_window(gc);
  702. } else {
  703. get_selected_window(gc);
  704. }
  705. if (gc->next_window) {
  706. gc->thread_id = GetWindowThreadProcessId(gc->next_window,
  707. &gc->process_id);
  708. if (!gc->thread_id || !gc->process_id) {
  709. warn("error acquiring, failed to get window "
  710. "thread/process ids: %lu",
  711. GetLastError());
  712. gc->error_acquiring = true;
  713. return;
  714. }
  715. if (!init_hook(gc)) {
  716. stop_capture(gc);
  717. }
  718. } else {
  719. gc->active = false;
  720. }
  721. }
  722. static inline bool init_events(struct game_capture *gc)
  723. {
  724. if (!gc->hook_restart) {
  725. gc->hook_restart = get_event_plus_id(EVENT_CAPTURE_RESTART,
  726. gc->process_id);
  727. if (!gc->hook_restart) {
  728. warn("init_events: failed to get hook_restart "
  729. "event: %lu", GetLastError());
  730. return false;
  731. }
  732. }
  733. if (!gc->hook_stop) {
  734. gc->hook_stop = get_event_plus_id(EVENT_CAPTURE_STOP,
  735. gc->process_id);
  736. if (!gc->hook_stop) {
  737. warn("init_events: failed to get hook_stop event: %lu",
  738. GetLastError());
  739. return false;
  740. }
  741. }
  742. if (!gc->hook_ready) {
  743. gc->hook_ready = get_event_plus_id(EVENT_HOOK_READY,
  744. gc->process_id);
  745. if (!gc->hook_ready) {
  746. warn("init_events: failed to get hook_ready event: %lu",
  747. GetLastError());
  748. return false;
  749. }
  750. }
  751. if (!gc->hook_exit) {
  752. gc->hook_exit = get_event_plus_id(EVENT_HOOK_EXIT,
  753. gc->process_id);
  754. if (!gc->hook_exit) {
  755. warn("init_events: failed to get hook_exit event: %lu",
  756. GetLastError());
  757. return false;
  758. }
  759. }
  760. return true;
  761. }
  762. enum capture_result {
  763. CAPTURE_FAIL,
  764. CAPTURE_RETRY,
  765. CAPTURE_SUCCESS
  766. };
  767. static inline enum capture_result init_capture_data(struct game_capture *gc)
  768. {
  769. char name[64];
  770. sprintf(name, "%s%u", SHMEM_TEXTURE, gc->global_hook_info->map_id);
  771. gc->cx = gc->global_hook_info->cx;
  772. gc->cy = gc->global_hook_info->cy;
  773. gc->pitch = gc->global_hook_info->pitch;
  774. if (gc->data) {
  775. UnmapViewOfFile(gc->data);
  776. gc->data = NULL;
  777. }
  778. CloseHandle(gc->hook_data_map);
  779. gc->hook_data_map = OpenFileMappingA(FILE_MAP_ALL_ACCESS, false, name);
  780. if (!gc->hook_data_map) {
  781. DWORD error = GetLastError();
  782. if (error == 2) {
  783. return CAPTURE_RETRY;
  784. } else {
  785. warn("init_capture_data: failed to open file "
  786. "mapping: %lu", error);
  787. }
  788. return CAPTURE_FAIL;
  789. }
  790. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  791. gc->global_hook_info->map_size);
  792. if (!gc->data) {
  793. warn("init_capture_data: failed to map data view: %lu",
  794. GetLastError());
  795. return CAPTURE_FAIL;
  796. }
  797. return CAPTURE_SUCCESS;
  798. }
  799. #define PIXEL_16BIT_SIZE 2
  800. #define PIXEL_32BIT_SIZE 4
  801. static inline uint32_t convert_5_to_8bit(uint16_t val)
  802. {
  803. return (uint32_t)((double)(val & 0x1F) * (255.0/31.0));
  804. }
  805. static inline uint32_t convert_6_to_8bit(uint16_t val)
  806. {
  807. return (uint32_t)((double)(val & 0x3F) * (255.0/63.0));
  808. }
  809. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  810. uint8_t *data, uint32_t pitch)
  811. {
  812. uint8_t *input = gc->texture_buffers[cur_texture];
  813. uint32_t gc_cx = gc->cx;
  814. uint32_t gc_cy = gc->cy;
  815. uint32_t gc_pitch = gc->pitch;
  816. for (uint32_t y = 0; y < gc_cy; y++) {
  817. uint8_t *row = input + (gc_pitch * y);
  818. uint8_t *out = data + (pitch * y);
  819. for (uint32_t x = 0; x < gc_cx; x += 8) {
  820. __m128i pixels_blue, pixels_green, pixels_red;
  821. __m128i pixels_result;
  822. __m128i *pixels_dest;
  823. __m128i *pixels_src = (__m128i*)(row + x * sizeof(uint16_t));
  824. __m128i pixels = _mm_load_si128(pixels_src);
  825. __m128i zero = _mm_setzero_si128();
  826. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  827. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  828. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  829. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  830. __m128i green_channel_mask = _mm_set1_epi32(0x000007E0);
  831. __m128i green_offset = _mm_set1_epi32(0x00000008);
  832. __m128i red_channel_mask = _mm_set1_epi32(0x0000F800);
  833. __m128i red_offset = _mm_set1_epi32(0x00000300);
  834. pixels_blue = _mm_and_si128(pixels_low, blue_channel_mask);
  835. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  836. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  837. pixels_green = _mm_and_si128(pixels_low, green_channel_mask);
  838. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  839. pixels_green = _mm_slli_epi32(pixels_green, 5);
  840. pixels_red = _mm_and_si128(pixels_low, red_channel_mask);
  841. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  842. pixels_red = _mm_slli_epi32(pixels_red, 8);
  843. pixels_result = _mm_set1_epi32(0xFF000000);
  844. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  845. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  846. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  847. pixels_dest = (__m128i*)(out + x * sizeof(uint32_t));
  848. _mm_store_si128(pixels_dest, pixels_result);
  849. pixels_blue = _mm_and_si128(pixels_high, blue_channel_mask);
  850. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  851. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  852. pixels_green = _mm_and_si128(pixels_high, green_channel_mask);
  853. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  854. pixels_green = _mm_slli_epi32(pixels_green, 5);
  855. pixels_red = _mm_and_si128(pixels_high, red_channel_mask);
  856. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  857. pixels_red = _mm_slli_epi32(pixels_red, 8);
  858. pixels_result = _mm_set1_epi32(0xFF000000);
  859. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  860. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  861. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  862. pixels_dest = (__m128i*)(out + (x + 4) * sizeof(uint32_t));
  863. _mm_store_si128(pixels_dest, pixels_result);
  864. }
  865. }
  866. }
  867. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  868. uint8_t *data, uint32_t pitch)
  869. {
  870. uint8_t *input = gc->texture_buffers[cur_texture];
  871. uint32_t gc_cx = gc->cx;
  872. uint32_t gc_cy = gc->cy;
  873. uint32_t gc_pitch = gc->pitch;
  874. for (uint32_t y = 0; y < gc_cy; y++) {
  875. uint8_t *row = input + (gc_pitch * y);
  876. uint8_t *out = data + (pitch * y);
  877. for (uint32_t x = 0; x < gc_cx; x += 8) {
  878. __m128i pixels_blue, pixels_green, pixels_red, pixels_alpha;
  879. __m128i pixels_result;
  880. __m128i *pixels_dest;
  881. __m128i *pixels_src = (__m128i*)(row + x * sizeof(uint16_t));
  882. __m128i pixels = _mm_load_si128(pixels_src);
  883. __m128i zero = _mm_setzero_si128();
  884. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  885. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  886. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  887. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  888. __m128i green_channel_mask = _mm_set1_epi32(0x000003E0);
  889. __m128i green_offset = _mm_set1_epi32(0x000000C);
  890. __m128i red_channel_mask = _mm_set1_epi32(0x00007C00);
  891. __m128i red_offset = _mm_set1_epi32(0x00000180);
  892. __m128i alpha_channel_mask = _mm_set1_epi32(0x00008000);
  893. __m128i alpha_offset = _mm_set1_epi32(0x00000001);
  894. __m128i alpha_mask32 = _mm_set1_epi32(0xFF000000);
  895. pixels_blue = _mm_and_si128(pixels_low, blue_channel_mask);
  896. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  897. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  898. pixels_green = _mm_and_si128(pixels_low, green_channel_mask);
  899. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  900. pixels_green = _mm_slli_epi32(pixels_green, 6);
  901. pixels_red = _mm_and_si128(pixels_low, red_channel_mask);
  902. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  903. pixels_red = _mm_slli_epi32(pixels_red, 9);
  904. pixels_alpha = _mm_and_si128(pixels_low, alpha_channel_mask);
  905. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  906. pixels_alpha = _mm_sub_epi32(pixels_alpha, alpha_offset);
  907. pixels_alpha = _mm_andnot_si128(pixels_alpha, alpha_mask32);
  908. pixels_result = pixels_red;
  909. pixels_result = _mm_or_si128(pixels_result, pixels_alpha);
  910. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  911. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  912. pixels_dest = (__m128i*)(out + x * sizeof(uint32_t));
  913. _mm_store_si128(pixels_dest, pixels_result);
  914. pixels_blue = _mm_and_si128(pixels_high, blue_channel_mask);
  915. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  916. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  917. pixels_green = _mm_and_si128(pixels_high, green_channel_mask);
  918. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  919. pixels_green = _mm_slli_epi32(pixels_green, 6);
  920. pixels_red = _mm_and_si128(pixels_high, red_channel_mask);
  921. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  922. pixels_red = _mm_slli_epi32(pixels_red, 9);
  923. pixels_alpha = _mm_and_si128(pixels_high, alpha_channel_mask);
  924. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  925. pixels_alpha = _mm_sub_epi32(pixels_alpha, alpha_offset);
  926. pixels_alpha = _mm_andnot_si128(pixels_alpha, alpha_mask32);
  927. pixels_result = pixels_red;
  928. pixels_result = _mm_or_si128(pixels_result, pixels_alpha);
  929. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  930. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  931. pixels_dest = (__m128i*)(out + (x + 4) * sizeof(uint32_t));
  932. _mm_store_si128(pixels_dest, pixels_result);
  933. }
  934. }
  935. }
  936. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  937. uint8_t *data, uint32_t pitch)
  938. {
  939. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  940. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  941. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  942. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  943. }
  944. }
  945. static void copy_shmem_tex(struct game_capture *gc)
  946. {
  947. int cur_texture = gc->shmem_data->last_tex;
  948. HANDLE mutex = NULL;
  949. uint32_t pitch;
  950. int next_texture;
  951. uint8_t *data;
  952. if (cur_texture < 0 || cur_texture > 1)
  953. return;
  954. next_texture = cur_texture == 1 ? 0 : 1;
  955. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  956. mutex = gc->texture_mutexes[cur_texture];
  957. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  958. mutex = gc->texture_mutexes[next_texture];
  959. cur_texture = next_texture;
  960. } else {
  961. return;
  962. }
  963. if (gs_texture_map(gc->texture, &data, &pitch)) {
  964. if (gc->convert_16bit) {
  965. copy_16bit_tex(gc, cur_texture, data, pitch);
  966. } else if (pitch == gc->pitch) {
  967. memcpy(data, gc->texture_buffers[cur_texture],
  968. pitch * gc->cy);
  969. } else {
  970. uint8_t *input = gc->texture_buffers[cur_texture];
  971. uint32_t best_pitch =
  972. pitch < gc->pitch ? pitch : gc->pitch;
  973. for (uint32_t y = 0; y < gc->cy; y++) {
  974. uint8_t *line_in = input + gc->pitch * y;
  975. uint8_t *line_out = data + pitch * y;
  976. memcpy(line_out, line_in, best_pitch);
  977. }
  978. }
  979. gs_texture_unmap(gc->texture);
  980. }
  981. ReleaseMutex(mutex);
  982. }
  983. static inline bool is_16bit_format(uint32_t format)
  984. {
  985. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  986. format == DXGI_FORMAT_B5G6R5_UNORM;
  987. }
  988. static inline bool init_shmem_capture(struct game_capture *gc)
  989. {
  990. enum gs_color_format format;
  991. gc->texture_buffers[0] =
  992. (uint8_t*)gc->data + gc->shmem_data->tex1_offset;
  993. gc->texture_buffers[1] =
  994. (uint8_t*)gc->data + gc->shmem_data->tex2_offset;
  995. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  996. format = gc->convert_16bit ?
  997. GS_BGRA : convert_format(gc->global_hook_info->format);
  998. obs_enter_graphics();
  999. gs_texture_destroy(gc->texture);
  1000. gc->texture = gs_texture_create(gc->cx, gc->cy, format, 1, NULL,
  1001. GS_DYNAMIC);
  1002. obs_leave_graphics();
  1003. if (!gc->texture) {
  1004. warn("init_shmem_capture: failed to create texture");
  1005. return false;
  1006. }
  1007. gc->copy_texture = copy_shmem_tex;
  1008. return true;
  1009. }
  1010. static inline bool init_shtex_capture(struct game_capture *gc)
  1011. {
  1012. obs_enter_graphics();
  1013. gs_texture_destroy(gc->texture);
  1014. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  1015. obs_leave_graphics();
  1016. if (!gc->texture) {
  1017. warn("init_shtex_capture: failed to open shared handle");
  1018. return false;
  1019. }
  1020. return true;
  1021. }
  1022. static bool start_capture(struct game_capture *gc)
  1023. {
  1024. if (!init_events(gc)) {
  1025. return false;
  1026. }
  1027. if (gc->global_hook_info->type == CAPTURE_TYPE_MEMORY) {
  1028. if (!init_shmem_capture(gc)) {
  1029. return false;
  1030. }
  1031. } else {
  1032. if (!init_shtex_capture(gc)) {
  1033. return false;
  1034. }
  1035. }
  1036. return true;
  1037. }
  1038. static inline bool capture_valid(struct game_capture *gc)
  1039. {
  1040. if (!gc->dwm_capture && !IsWindow(gc->window))
  1041. return false;
  1042. return !object_signalled(gc->target_process);
  1043. }
  1044. static void game_capture_tick(void *data, float seconds)
  1045. {
  1046. struct game_capture *gc = data;
  1047. if (gc->hook_stop && object_signalled(gc->hook_stop)) {
  1048. stop_capture(gc);
  1049. }
  1050. if (gc->active && !gc->hook_ready && gc->process_id) {
  1051. gc->hook_ready = get_event_plus_id(EVENT_HOOK_READY,
  1052. gc->process_id);
  1053. }
  1054. if (gc->injector_process && object_signalled(gc->injector_process)) {
  1055. DWORD exit_code = 0;
  1056. GetExitCodeProcess(gc->injector_process, &exit_code);
  1057. close_handle(&gc->injector_process);
  1058. if (exit_code != 0) {
  1059. warn("inject process failed: %ld", (long)exit_code);
  1060. gc->error_acquiring = true;
  1061. } else if (!gc->capturing) {
  1062. gc->retry_interval = ERROR_RETRY_INTERVAL;
  1063. stop_capture(gc);
  1064. }
  1065. }
  1066. if (gc->hook_ready && object_signalled(gc->hook_ready)) {
  1067. enum capture_result result = init_capture_data(gc);
  1068. if (result == CAPTURE_SUCCESS)
  1069. gc->capturing = start_capture(gc);
  1070. if (result != CAPTURE_RETRY && !gc->capturing) {
  1071. gc->retry_interval = ERROR_RETRY_INTERVAL;
  1072. stop_capture(gc);
  1073. }
  1074. }
  1075. gc->retry_time += seconds;
  1076. if (!gc->active) {
  1077. if (!obs_source_showing(gc->source)) {
  1078. gc->retry_time = 0.0f;
  1079. return;
  1080. }
  1081. if (!gc->error_acquiring &&
  1082. gc->retry_time > gc->retry_interval) {
  1083. if (gc->config.capture_any_fullscreen ||
  1084. gc->activate_hook) {
  1085. try_hook(gc);
  1086. gc->retry_time = 0.0f;
  1087. }
  1088. }
  1089. } else {
  1090. if (!capture_valid(gc)) {
  1091. info("capture window no longer exists, "
  1092. "terminating capture");
  1093. stop_capture(gc);
  1094. } else {
  1095. if (gc->copy_texture) {
  1096. obs_enter_graphics();
  1097. gc->copy_texture(gc);
  1098. obs_leave_graphics();
  1099. }
  1100. if (gc->config.cursor) {
  1101. obs_enter_graphics();
  1102. cursor_capture(&gc->cursor_data);
  1103. obs_leave_graphics();
  1104. }
  1105. gc->fps_reset_time += seconds;
  1106. if (gc->fps_reset_time >= gc->retry_interval) {
  1107. reset_frame_interval(gc);
  1108. gc->fps_reset_time = 0.0f;
  1109. }
  1110. }
  1111. }
  1112. }
  1113. static inline void game_capture_render_cursor(struct game_capture *gc)
  1114. {
  1115. POINT p = {0};
  1116. if (!gc->global_hook_info->window ||
  1117. !gc->global_hook_info->base_cx ||
  1118. !gc->global_hook_info->base_cy)
  1119. return;
  1120. ClientToScreen((HWND)(uintptr_t)gc->global_hook_info->window, &p);
  1121. float x_scale = (float)gc->global_hook_info->cx /
  1122. (float)gc->global_hook_info->base_cx;
  1123. float y_scale = (float)gc->global_hook_info->cy /
  1124. (float)gc->global_hook_info->base_cy;
  1125. cursor_draw(&gc->cursor_data, -p.x, -p.y, x_scale, y_scale,
  1126. gc->global_hook_info->base_cx,
  1127. gc->global_hook_info->base_cy);
  1128. }
  1129. static void game_capture_render(void *data, gs_effect_t *effect)
  1130. {
  1131. struct game_capture *gc = data;
  1132. if (!gc->texture)
  1133. return;
  1134. effect = obs_get_base_effect(gc->config.allow_transparency ?
  1135. OBS_EFFECT_DEFAULT : OBS_EFFECT_OPAQUE);
  1136. while (gs_effect_loop(effect, "Draw")) {
  1137. obs_source_draw(gc->texture, 0, 0, 0, 0,
  1138. gc->global_hook_info->flip);
  1139. if (gc->config.allow_transparency && gc->config.cursor) {
  1140. game_capture_render_cursor(gc);
  1141. }
  1142. }
  1143. if (!gc->config.allow_transparency && gc->config.cursor) {
  1144. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1145. while (gs_effect_loop(effect, "Draw")) {
  1146. game_capture_render_cursor(gc);
  1147. }
  1148. }
  1149. }
  1150. static uint32_t game_capture_width(void *data)
  1151. {
  1152. struct game_capture *gc = data;
  1153. return gc->active ? gc->global_hook_info->cx : 0;
  1154. }
  1155. static uint32_t game_capture_height(void *data)
  1156. {
  1157. struct game_capture *gc = data;
  1158. return gc->active ? gc->global_hook_info->cy : 0;
  1159. }
  1160. static const char *game_capture_name(void *unused)
  1161. {
  1162. UNUSED_PARAMETER(unused);
  1163. return TEXT_GAME_CAPTURE;
  1164. }
  1165. static void game_capture_defaults(obs_data_t *settings)
  1166. {
  1167. obs_data_set_default_bool(settings, SETTING_ANY_FULLSCREEN, true);
  1168. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1169. (int)WINDOW_PRIORITY_EXE);
  1170. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1171. obs_data_set_default_bool(settings, SETTING_FORCE_SCALING, false);
  1172. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1173. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1174. obs_data_set_default_string(settings, SETTING_SCALE_RES, "0x0");
  1175. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1176. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1177. obs_data_set_default_bool(settings, SETTING_ANTI_CHEAT_HOOK, true);
  1178. }
  1179. static bool any_fullscreen_callback(obs_properties_t *ppts,
  1180. obs_property_t *p, obs_data_t *settings)
  1181. {
  1182. bool any_fullscreen = obs_data_get_bool(settings,
  1183. SETTING_ANY_FULLSCREEN);
  1184. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1185. obs_property_set_enabled(p, !any_fullscreen);
  1186. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1187. obs_property_set_enabled(p, !any_fullscreen);
  1188. return true;
  1189. }
  1190. static bool use_scaling_callback(obs_properties_t *ppts, obs_property_t *p,
  1191. obs_data_t *settings)
  1192. {
  1193. bool use_scale = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  1194. p = obs_properties_get(ppts, SETTING_SCALE_RES);
  1195. obs_property_set_enabled(p, use_scale);
  1196. return true;
  1197. }
  1198. static void insert_preserved_val(obs_property_t *p, const char *val)
  1199. {
  1200. char *class = NULL;
  1201. char *title = NULL;
  1202. char *executable = NULL;
  1203. struct dstr desc = {0};
  1204. build_window_strings(val, &class, &title, &executable);
  1205. dstr_printf(&desc, "[%s]: %s", executable, title);
  1206. obs_property_list_insert_string(p, 1, desc.array, val);
  1207. obs_property_list_item_disable(p, 1, true);
  1208. dstr_free(&desc);
  1209. bfree(class);
  1210. bfree(title);
  1211. bfree(executable);
  1212. }
  1213. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1214. obs_data_t *settings)
  1215. {
  1216. const char *cur_val;
  1217. bool match = false;
  1218. size_t i = 0;
  1219. cur_val = obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  1220. if (!cur_val) {
  1221. return false;
  1222. }
  1223. for (;;) {
  1224. const char *val = obs_property_list_item_string(p, i++);
  1225. if (!val)
  1226. break;
  1227. if (strcmp(val, cur_val) == 0) {
  1228. match = true;
  1229. break;
  1230. }
  1231. }
  1232. if (cur_val && *cur_val && !match) {
  1233. insert_preserved_val(p, cur_val);
  1234. return true;
  1235. }
  1236. UNUSED_PARAMETER(ppts);
  1237. return false;
  1238. }
  1239. static const double default_scale_vals[] = {
  1240. 1.25,
  1241. 1.5,
  1242. 2.0,
  1243. 2.5,
  1244. 3.0
  1245. };
  1246. #define NUM_DEFAULT_SCALE_VALS \
  1247. (sizeof(default_scale_vals) / sizeof(default_scale_vals[0]))
  1248. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc,
  1249. LPRECT rc, LPARAM data)
  1250. {
  1251. *(HMONITOR*)data = monitor;
  1252. UNUSED_PARAMETER(hdc);
  1253. UNUSED_PARAMETER(rc);
  1254. return false;
  1255. }
  1256. static obs_properties_t *game_capture_properties(void *data)
  1257. {
  1258. HMONITOR monitor;
  1259. uint32_t cx = 1920;
  1260. uint32_t cy = 1080;
  1261. /* scaling is free form, this is mostly just to provide some common
  1262. * values */
  1263. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1264. (LPARAM)&monitor);
  1265. if (success) {
  1266. MONITORINFO mi = {0};
  1267. mi.cbSize = sizeof(mi);
  1268. if (!!GetMonitorInfo(monitor, &mi)) {
  1269. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1270. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1271. }
  1272. }
  1273. obs_properties_t *ppts = obs_properties_create();
  1274. obs_property_t *p;
  1275. p = obs_properties_add_bool(ppts, SETTING_ANY_FULLSCREEN,
  1276. TEXT_ANY_FULLSCREEN);
  1277. obs_property_set_modified_callback(p, any_fullscreen_callback);
  1278. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1279. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1280. obs_property_list_add_string(p, "", "");
  1281. fill_window_list(p, INCLUDE_MINIMIZED);
  1282. obs_property_set_modified_callback(p, window_changed_callback);
  1283. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1284. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1285. OBS_COMBO_FORMAT_INT);
  1286. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1287. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1288. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1289. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1290. TEXT_SLI_COMPATIBILITY);
  1291. p = obs_properties_add_bool(ppts, SETTING_FORCE_SCALING,
  1292. TEXT_FORCE_SCALING);
  1293. obs_property_set_modified_callback(p, use_scaling_callback);
  1294. p = obs_properties_add_list(ppts, SETTING_SCALE_RES, TEXT_SCALE_RES,
  1295. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  1296. for (size_t i = 0; i < NUM_DEFAULT_SCALE_VALS; i++) {
  1297. char scale_str[64];
  1298. uint32_t new_cx =
  1299. (uint32_t)((double)cx / default_scale_vals[i]) & ~2;
  1300. uint32_t new_cy =
  1301. (uint32_t)((double)cy / default_scale_vals[i]) & ~2;
  1302. sprintf(scale_str, "%"PRIu32"x%"PRIu32, new_cx, new_cy);
  1303. obs_property_list_add_string(p, scale_str, scale_str);
  1304. }
  1305. obs_property_set_enabled(p, false);
  1306. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1307. TEXT_ALLOW_TRANSPARENCY);
  1308. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1309. TEXT_LIMIT_FRAMERATE);
  1310. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1311. obs_properties_add_bool(ppts, SETTING_ANTI_CHEAT_HOOK,
  1312. TEXT_ANTI_CHEAT_HOOK);
  1313. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1314. TEXT_CAPTURE_OVERLAYS);
  1315. UNUSED_PARAMETER(data);
  1316. return ppts;
  1317. }
  1318. struct obs_source_info game_capture_info = {
  1319. .id = "game_capture",
  1320. .type = OBS_SOURCE_TYPE_INPUT,
  1321. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1322. OBS_SOURCE_DO_NOT_DUPLICATE,
  1323. .get_name = game_capture_name,
  1324. .create = game_capture_create,
  1325. .destroy = game_capture_destroy,
  1326. .get_width = game_capture_width,
  1327. .get_height = game_capture_height,
  1328. .get_defaults = game_capture_defaults,
  1329. .get_properties = game_capture_properties,
  1330. .update = game_capture_update,
  1331. .video_tick = game_capture_tick,
  1332. .video_render = game_capture_render
  1333. };