game-capture.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. #include <inttypes.h>
  2. #include <obs-module.h>
  3. #include <obs-hotkey.h>
  4. #include <util/platform.h>
  5. #include <util/threading.h>
  6. #include <windows.h>
  7. #include <dxgi.h>
  8. #include <util/sse-intrin.h>
  9. #include <util/util_uint64.h>
  10. #include <ipc-util/pipe.h>
  11. #include "obfuscate.h"
  12. #include "inject-library.h"
  13. #include "graphics-hook-info.h"
  14. #include "graphics-hook-ver.h"
  15. #include "window-helpers.h"
  16. #include "cursor-capture.h"
  17. #include "app-helpers.h"
  18. #include "nt-stuff.h"
  19. #define do_log(level, format, ...) \
  20. blog(level, "[game-capture: '%s'] " format, \
  21. obs_source_get_name(gc->source), ##__VA_ARGS__)
  22. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  23. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  24. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  25. /* clang-format off */
  26. #define SETTING_MODE "capture_mode"
  27. #define SETTING_CAPTURE_WINDOW "window"
  28. #define SETTING_ACTIVE_WINDOW "active_window"
  29. #define SETTING_WINDOW_PRIORITY "priority"
  30. #define SETTING_COMPATIBILITY "sli_compatibility"
  31. #define SETTING_FORCE_SCALING "force_scaling"
  32. #define SETTING_SCALE_RES "scale_res"
  33. #define SETTING_CURSOR "capture_cursor"
  34. #define SETTING_TRANSPARENCY "allow_transparency"
  35. #define SETTING_LIMIT_FRAMERATE "limit_framerate"
  36. #define SETTING_CAPTURE_OVERLAYS "capture_overlays"
  37. #define SETTING_ANTI_CHEAT_HOOK "anti_cheat_hook"
  38. #define SETTING_HOOK_RATE "hook_rate"
  39. /* deprecated */
  40. #define SETTING_ANY_FULLSCREEN "capture_any_fullscreen"
  41. #define SETTING_MODE_ANY "any_fullscreen"
  42. #define SETTING_MODE_WINDOW "window"
  43. #define SETTING_MODE_HOTKEY "hotkey"
  44. #define HOTKEY_START "hotkey_start"
  45. #define HOTKEY_STOP "hotkey_stop"
  46. #define TEXT_MODE obs_module_text("Mode")
  47. #define TEXT_GAME_CAPTURE obs_module_text("GameCapture")
  48. #define TEXT_ANY_FULLSCREEN obs_module_text("GameCapture.AnyFullscreen")
  49. #define TEXT_SLI_COMPATIBILITY obs_module_text("SLIFix")
  50. #define TEXT_ALLOW_TRANSPARENCY obs_module_text("AllowTransparency")
  51. #define TEXT_FORCE_SCALING obs_module_text("GameCapture.ForceScaling")
  52. #define TEXT_SCALE_RES obs_module_text("GameCapture.ScaleRes")
  53. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  54. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  55. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  56. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  57. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  58. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  59. #define TEXT_LIMIT_FRAMERATE obs_module_text("GameCapture.LimitFramerate")
  60. #define TEXT_CAPTURE_OVERLAYS obs_module_text("GameCapture.CaptureOverlays")
  61. #define TEXT_ANTI_CHEAT_HOOK obs_module_text("GameCapture.AntiCheatHook")
  62. #define TEXT_HOOK_RATE obs_module_text("GameCapture.HookRate")
  63. #define TEXT_HOOK_RATE_SLOW obs_module_text("GameCapture.HookRate.Slow")
  64. #define TEXT_HOOK_RATE_NORMAL obs_module_text("GameCapture.HookRate.Normal")
  65. #define TEXT_HOOK_RATE_FAST obs_module_text("GameCapture.HookRate.Fast")
  66. #define TEXT_HOOK_RATE_FASTEST obs_module_text("GameCapture.HookRate.Fastest")
  67. #define TEXT_MODE_ANY TEXT_ANY_FULLSCREEN
  68. #define TEXT_MODE_WINDOW obs_module_text("GameCapture.CaptureWindow")
  69. #define TEXT_MODE_HOTKEY obs_module_text("GameCapture.UseHotkey")
  70. #define TEXT_HOTKEY_START obs_module_text("GameCapture.HotkeyStart")
  71. #define TEXT_HOTKEY_STOP obs_module_text("GameCapture.HotkeyStop")
  72. /* clang-format on */
  73. #define DEFAULT_RETRY_INTERVAL 2.0f
  74. #define ERROR_RETRY_INTERVAL 4.0f
  75. enum capture_mode {
  76. CAPTURE_MODE_ANY,
  77. CAPTURE_MODE_WINDOW,
  78. CAPTURE_MODE_HOTKEY
  79. };
  80. enum hook_rate {
  81. HOOK_RATE_SLOW,
  82. HOOK_RATE_NORMAL,
  83. HOOK_RATE_FAST,
  84. HOOK_RATE_FASTEST
  85. };
  86. struct game_capture_config {
  87. char *title;
  88. char *class;
  89. char *executable;
  90. enum window_priority priority;
  91. enum capture_mode mode;
  92. uint32_t scale_cx;
  93. uint32_t scale_cy;
  94. bool cursor;
  95. bool force_shmem;
  96. bool force_scaling;
  97. bool allow_transparency;
  98. bool limit_framerate;
  99. bool capture_overlays;
  100. bool anticheat_hook;
  101. enum hook_rate hook_rate;
  102. };
  103. struct game_capture {
  104. obs_source_t *source;
  105. struct cursor_data cursor_data;
  106. HANDLE injector_process;
  107. uint32_t cx;
  108. uint32_t cy;
  109. uint32_t pitch;
  110. DWORD process_id;
  111. DWORD thread_id;
  112. HWND next_window;
  113. HWND window;
  114. float retry_time;
  115. float fps_reset_time;
  116. float retry_interval;
  117. struct dstr title;
  118. struct dstr class;
  119. struct dstr executable;
  120. enum window_priority priority;
  121. obs_hotkey_pair_id hotkey_pair;
  122. volatile long hotkey_window;
  123. volatile bool deactivate_hook;
  124. volatile bool activate_hook_now;
  125. bool wait_for_target_startup;
  126. bool showing;
  127. bool active;
  128. bool capturing;
  129. bool activate_hook;
  130. bool process_is_64bit;
  131. bool error_acquiring;
  132. bool dwm_capture;
  133. bool initial_config;
  134. bool convert_16bit;
  135. bool is_app;
  136. bool cursor_hidden;
  137. struct game_capture_config config;
  138. ipc_pipe_server_t pipe;
  139. gs_texture_t *texture;
  140. struct hook_info *global_hook_info;
  141. HANDLE keepalive_mutex;
  142. HANDLE hook_init;
  143. HANDLE hook_restart;
  144. HANDLE hook_stop;
  145. HANDLE hook_ready;
  146. HANDLE hook_exit;
  147. HANDLE hook_data_map;
  148. HANDLE global_hook_info_map;
  149. HANDLE target_process;
  150. HANDLE texture_mutexes[2];
  151. wchar_t *app_sid;
  152. int retrying;
  153. float cursor_check_time;
  154. union {
  155. struct {
  156. struct shmem_data *shmem_data;
  157. uint8_t *texture_buffers[2];
  158. };
  159. struct shtex_data *shtex_data;
  160. void *data;
  161. };
  162. void (*copy_texture)(struct game_capture *);
  163. };
  164. struct graphics_offsets offsets32 = {0};
  165. struct graphics_offsets offsets64 = {0};
  166. static inline bool use_anticheat(struct game_capture *gc)
  167. {
  168. return gc->config.anticheat_hook && !gc->is_app;
  169. }
  170. static inline HANDLE open_mutex_plus_id(struct game_capture *gc,
  171. const wchar_t *name, DWORD id)
  172. {
  173. wchar_t new_name[64];
  174. _snwprintf(new_name, 64, L"%s%lu", name, id);
  175. return gc->is_app ? open_app_mutex(gc->app_sid, new_name)
  176. : open_mutex(new_name);
  177. }
  178. static inline HANDLE open_mutex_gc(struct game_capture *gc, const wchar_t *name)
  179. {
  180. return open_mutex_plus_id(gc, name, gc->process_id);
  181. }
  182. static inline HANDLE open_event_plus_id(struct game_capture *gc,
  183. const wchar_t *name, DWORD id)
  184. {
  185. wchar_t new_name[64];
  186. _snwprintf(new_name, 64, L"%s%lu", name, id);
  187. return gc->is_app ? open_app_event(gc->app_sid, new_name)
  188. : open_event(new_name);
  189. }
  190. static inline HANDLE open_event_gc(struct game_capture *gc, const wchar_t *name)
  191. {
  192. return open_event_plus_id(gc, name, gc->process_id);
  193. }
  194. static inline HANDLE open_map_plus_id(struct game_capture *gc,
  195. const wchar_t *name, DWORD id)
  196. {
  197. wchar_t new_name[64];
  198. swprintf(new_name, 64, L"%s%lu", name, id);
  199. debug("map id: %S", new_name);
  200. return gc->is_app ? open_app_map(gc->app_sid, new_name)
  201. : OpenFileMappingW(GC_MAPPING_FLAGS, false, new_name);
  202. }
  203. static inline HANDLE open_hook_info(struct game_capture *gc)
  204. {
  205. return open_map_plus_id(gc, SHMEM_HOOK_INFO, gc->process_id);
  206. }
  207. static inline enum gs_color_format convert_format(uint32_t format)
  208. {
  209. switch (format) {
  210. case DXGI_FORMAT_R8G8B8A8_UNORM:
  211. return GS_RGBA;
  212. case DXGI_FORMAT_B8G8R8X8_UNORM:
  213. return GS_BGRX;
  214. case DXGI_FORMAT_B8G8R8A8_UNORM:
  215. return GS_BGRA;
  216. case DXGI_FORMAT_R10G10B10A2_UNORM:
  217. return GS_R10G10B10A2;
  218. case DXGI_FORMAT_R16G16B16A16_UNORM:
  219. return GS_RGBA16;
  220. case DXGI_FORMAT_R16G16B16A16_FLOAT:
  221. return GS_RGBA16F;
  222. case DXGI_FORMAT_R32G32B32A32_FLOAT:
  223. return GS_RGBA32F;
  224. }
  225. return GS_UNKNOWN;
  226. }
  227. static void close_handle(HANDLE *p_handle)
  228. {
  229. HANDLE handle = *p_handle;
  230. if (handle) {
  231. if (handle != INVALID_HANDLE_VALUE)
  232. CloseHandle(handle);
  233. *p_handle = NULL;
  234. }
  235. }
  236. static inline HMODULE kernel32(void)
  237. {
  238. static HMODULE kernel32_handle = NULL;
  239. if (!kernel32_handle)
  240. kernel32_handle = GetModuleHandleW(L"kernel32");
  241. return kernel32_handle;
  242. }
  243. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  244. DWORD process_id)
  245. {
  246. static HANDLE(WINAPI * open_process_proc)(DWORD, BOOL, DWORD) = NULL;
  247. if (!open_process_proc)
  248. open_process_proc = get_obfuscated_func(
  249. kernel32(), "NuagUykjcxr", 0x1B694B59451ULL);
  250. return open_process_proc(desired_access, inherit_handle, process_id);
  251. }
  252. static inline float hook_rate_to_float(enum hook_rate rate)
  253. {
  254. switch (rate) {
  255. case HOOK_RATE_SLOW:
  256. return 2.0f;
  257. case HOOK_RATE_FAST:
  258. return 0.5f;
  259. case HOOK_RATE_FASTEST:
  260. return 0.1f;
  261. case HOOK_RATE_NORMAL:
  262. /* FALLTHROUGH */
  263. default:
  264. return 1.0f;
  265. }
  266. }
  267. static void stop_capture(struct game_capture *gc)
  268. {
  269. ipc_pipe_server_free(&gc->pipe);
  270. if (gc->hook_stop) {
  271. SetEvent(gc->hook_stop);
  272. }
  273. if (gc->global_hook_info) {
  274. UnmapViewOfFile(gc->global_hook_info);
  275. gc->global_hook_info = NULL;
  276. }
  277. if (gc->data) {
  278. UnmapViewOfFile(gc->data);
  279. gc->data = NULL;
  280. }
  281. if (gc->app_sid) {
  282. LocalFree(gc->app_sid);
  283. gc->app_sid = NULL;
  284. }
  285. close_handle(&gc->hook_restart);
  286. close_handle(&gc->hook_stop);
  287. close_handle(&gc->hook_ready);
  288. close_handle(&gc->hook_exit);
  289. close_handle(&gc->hook_init);
  290. close_handle(&gc->hook_data_map);
  291. close_handle(&gc->keepalive_mutex);
  292. close_handle(&gc->global_hook_info_map);
  293. close_handle(&gc->target_process);
  294. close_handle(&gc->texture_mutexes[0]);
  295. close_handle(&gc->texture_mutexes[1]);
  296. if (gc->texture) {
  297. obs_enter_graphics();
  298. gs_texture_destroy(gc->texture);
  299. obs_leave_graphics();
  300. gc->texture = NULL;
  301. }
  302. if (gc->active)
  303. info("capture stopped");
  304. gc->copy_texture = NULL;
  305. gc->wait_for_target_startup = false;
  306. gc->active = false;
  307. gc->capturing = false;
  308. if (gc->retrying)
  309. gc->retrying--;
  310. }
  311. static inline void free_config(struct game_capture_config *config)
  312. {
  313. bfree(config->title);
  314. bfree(config->class);
  315. bfree(config->executable);
  316. memset(config, 0, sizeof(*config));
  317. }
  318. static void game_capture_destroy(void *data)
  319. {
  320. struct game_capture *gc = data;
  321. stop_capture(gc);
  322. if (gc->hotkey_pair)
  323. obs_hotkey_pair_unregister(gc->hotkey_pair);
  324. obs_enter_graphics();
  325. cursor_data_free(&gc->cursor_data);
  326. obs_leave_graphics();
  327. dstr_free(&gc->title);
  328. dstr_free(&gc->class);
  329. dstr_free(&gc->executable);
  330. free_config(&gc->config);
  331. bfree(gc);
  332. }
  333. static inline bool using_older_non_mode_format(obs_data_t *settings)
  334. {
  335. return obs_data_has_user_value(settings, SETTING_ANY_FULLSCREEN) &&
  336. !obs_data_has_user_value(settings, SETTING_MODE);
  337. }
  338. static inline void get_config(struct game_capture_config *cfg,
  339. obs_data_t *settings, const char *window)
  340. {
  341. int ret;
  342. const char *scale_str;
  343. const char *mode_str = NULL;
  344. build_window_strings(window, &cfg->class, &cfg->title,
  345. &cfg->executable);
  346. if (using_older_non_mode_format(settings)) {
  347. bool any = obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
  348. mode_str = any ? SETTING_MODE_ANY : SETTING_MODE_WINDOW;
  349. } else {
  350. mode_str = obs_data_get_string(settings, SETTING_MODE);
  351. }
  352. if (mode_str && strcmp(mode_str, SETTING_MODE_WINDOW) == 0)
  353. cfg->mode = CAPTURE_MODE_WINDOW;
  354. else if (mode_str && strcmp(mode_str, SETTING_MODE_HOTKEY) == 0)
  355. cfg->mode = CAPTURE_MODE_HOTKEY;
  356. else
  357. cfg->mode = CAPTURE_MODE_ANY;
  358. cfg->priority = (enum window_priority)obs_data_get_int(
  359. settings, SETTING_WINDOW_PRIORITY);
  360. cfg->force_shmem = obs_data_get_bool(settings, SETTING_COMPATIBILITY);
  361. cfg->cursor = obs_data_get_bool(settings, SETTING_CURSOR);
  362. cfg->allow_transparency =
  363. obs_data_get_bool(settings, SETTING_TRANSPARENCY);
  364. cfg->force_scaling = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  365. cfg->limit_framerate =
  366. obs_data_get_bool(settings, SETTING_LIMIT_FRAMERATE);
  367. cfg->capture_overlays =
  368. obs_data_get_bool(settings, SETTING_CAPTURE_OVERLAYS);
  369. cfg->anticheat_hook =
  370. obs_data_get_bool(settings, SETTING_ANTI_CHEAT_HOOK);
  371. cfg->hook_rate =
  372. (enum hook_rate)obs_data_get_int(settings, SETTING_HOOK_RATE);
  373. scale_str = obs_data_get_string(settings, SETTING_SCALE_RES);
  374. ret = sscanf(scale_str, "%" PRIu32 "x%" PRIu32, &cfg->scale_cx,
  375. &cfg->scale_cy);
  376. cfg->scale_cx &= ~2;
  377. cfg->scale_cy &= ~2;
  378. if (cfg->force_scaling) {
  379. if (ret != 2 || cfg->scale_cx == 0 || cfg->scale_cy == 0) {
  380. cfg->scale_cx = 0;
  381. cfg->scale_cy = 0;
  382. }
  383. }
  384. }
  385. static inline int s_cmp(const char *str1, const char *str2)
  386. {
  387. if (!str1 || !str2)
  388. return -1;
  389. return strcmp(str1, str2);
  390. }
  391. static inline bool capture_needs_reset(struct game_capture_config *cfg1,
  392. struct game_capture_config *cfg2)
  393. {
  394. if (cfg1->mode != cfg2->mode) {
  395. return true;
  396. } else if (cfg1->mode == CAPTURE_MODE_WINDOW &&
  397. (s_cmp(cfg1->class, cfg2->class) != 0 ||
  398. s_cmp(cfg1->title, cfg2->title) != 0 ||
  399. s_cmp(cfg1->executable, cfg2->executable) != 0 ||
  400. cfg1->priority != cfg2->priority)) {
  401. return true;
  402. } else if (cfg1->force_scaling != cfg2->force_scaling) {
  403. return true;
  404. } else if (cfg1->force_scaling && (cfg1->scale_cx != cfg2->scale_cx ||
  405. cfg1->scale_cy != cfg2->scale_cy)) {
  406. return true;
  407. } else if (cfg1->force_shmem != cfg2->force_shmem) {
  408. return true;
  409. } else if (cfg1->limit_framerate != cfg2->limit_framerate) {
  410. return true;
  411. } else if (cfg1->capture_overlays != cfg2->capture_overlays) {
  412. return true;
  413. }
  414. return false;
  415. }
  416. static bool hotkey_start(void *data, obs_hotkey_pair_id id,
  417. obs_hotkey_t *hotkey, bool pressed)
  418. {
  419. struct game_capture *gc = data;
  420. if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
  421. info("Activate hotkey pressed");
  422. os_atomic_set_long(&gc->hotkey_window,
  423. (long)(uintptr_t)GetForegroundWindow());
  424. os_atomic_set_bool(&gc->deactivate_hook, true);
  425. os_atomic_set_bool(&gc->activate_hook_now, true);
  426. }
  427. return true;
  428. }
  429. static bool hotkey_stop(void *data, obs_hotkey_pair_id id, obs_hotkey_t *hotkey,
  430. bool pressed)
  431. {
  432. struct game_capture *gc = data;
  433. if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
  434. info("Deactivate hotkey pressed");
  435. os_atomic_set_bool(&gc->deactivate_hook, true);
  436. }
  437. return true;
  438. }
  439. static void game_capture_update(void *data, obs_data_t *settings)
  440. {
  441. struct game_capture *gc = data;
  442. struct game_capture_config cfg;
  443. bool reset_capture = false;
  444. const char *window =
  445. obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  446. get_config(&cfg, settings, window);
  447. reset_capture = capture_needs_reset(&cfg, &gc->config);
  448. if (cfg.force_scaling && (cfg.scale_cx == 0 || cfg.scale_cy == 0)) {
  449. gc->error_acquiring = true;
  450. warn("error acquiring, scale is bad");
  451. } else {
  452. gc->error_acquiring = false;
  453. }
  454. if (cfg.mode == CAPTURE_MODE_HOTKEY &&
  455. gc->config.mode != CAPTURE_MODE_HOTKEY) {
  456. gc->activate_hook = false;
  457. } else {
  458. gc->activate_hook = !!window && !!*window;
  459. }
  460. free_config(&gc->config);
  461. gc->config = cfg;
  462. gc->retry_interval = DEFAULT_RETRY_INTERVAL *
  463. hook_rate_to_float(gc->config.hook_rate);
  464. gc->wait_for_target_startup = false;
  465. dstr_free(&gc->title);
  466. dstr_free(&gc->class);
  467. dstr_free(&gc->executable);
  468. if (cfg.mode == CAPTURE_MODE_WINDOW) {
  469. dstr_copy(&gc->title, gc->config.title);
  470. dstr_copy(&gc->class, gc->config.class);
  471. dstr_copy(&gc->executable, gc->config.executable);
  472. gc->priority = gc->config.priority;
  473. }
  474. if (!gc->initial_config) {
  475. if (reset_capture) {
  476. stop_capture(gc);
  477. }
  478. } else {
  479. gc->initial_config = false;
  480. }
  481. }
  482. extern void wait_for_hook_initialization(void);
  483. static void *game_capture_create(obs_data_t *settings, obs_source_t *source)
  484. {
  485. struct game_capture *gc = bzalloc(sizeof(*gc));
  486. wait_for_hook_initialization();
  487. gc->source = source;
  488. gc->initial_config = true;
  489. gc->retry_interval = DEFAULT_RETRY_INTERVAL *
  490. hook_rate_to_float(gc->config.hook_rate);
  491. gc->hotkey_pair = obs_hotkey_pair_register_source(
  492. gc->source, HOTKEY_START, TEXT_HOTKEY_START, HOTKEY_STOP,
  493. TEXT_HOTKEY_STOP, hotkey_start, hotkey_stop, gc, gc);
  494. game_capture_update(gc, settings);
  495. return gc;
  496. }
  497. #define STOP_BEING_BAD \
  498. " This is most likely due to security software. Please make sure " \
  499. "that the OBS installation folder is excluded/ignored in the " \
  500. "settings of the security software you are using."
  501. static bool check_file_integrity(struct game_capture *gc, const char *file,
  502. const char *name)
  503. {
  504. DWORD error;
  505. HANDLE handle;
  506. wchar_t *w_file = NULL;
  507. if (!file || !*file) {
  508. warn("Game capture %s not found." STOP_BEING_BAD, name);
  509. return false;
  510. }
  511. if (!os_utf8_to_wcs_ptr(file, 0, &w_file)) {
  512. warn("Could not convert file name to wide string");
  513. return false;
  514. }
  515. handle = CreateFileW(w_file, GENERIC_READ | GENERIC_EXECUTE,
  516. FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  517. bfree(w_file);
  518. if (handle != INVALID_HANDLE_VALUE) {
  519. CloseHandle(handle);
  520. return true;
  521. }
  522. error = GetLastError();
  523. if (error == ERROR_FILE_NOT_FOUND) {
  524. warn("Game capture file '%s' not found." STOP_BEING_BAD, file);
  525. } else if (error == ERROR_ACCESS_DENIED) {
  526. warn("Game capture file '%s' could not be loaded." STOP_BEING_BAD,
  527. file);
  528. } else {
  529. warn("Game capture file '%s' could not be loaded: %lu." STOP_BEING_BAD,
  530. file, error);
  531. }
  532. return false;
  533. }
  534. static inline bool is_64bit_windows(void)
  535. {
  536. #ifdef _WIN64
  537. return true;
  538. #else
  539. BOOL x86 = false;
  540. bool success = !!IsWow64Process(GetCurrentProcess(), &x86);
  541. return success && !!x86;
  542. #endif
  543. }
  544. static inline bool is_64bit_process(HANDLE process)
  545. {
  546. BOOL x86 = true;
  547. if (is_64bit_windows()) {
  548. bool success = !!IsWow64Process(process, &x86);
  549. if (!success) {
  550. return false;
  551. }
  552. }
  553. return !x86;
  554. }
  555. static inline bool open_target_process(struct game_capture *gc)
  556. {
  557. gc->target_process = open_process(
  558. PROCESS_QUERY_INFORMATION | SYNCHRONIZE, false, gc->process_id);
  559. if (!gc->target_process) {
  560. warn("could not open process: %s", gc->config.executable);
  561. return false;
  562. }
  563. gc->process_is_64bit = is_64bit_process(gc->target_process);
  564. gc->is_app = is_app(gc->target_process);
  565. if (gc->is_app) {
  566. gc->app_sid = get_app_sid(gc->target_process);
  567. }
  568. return true;
  569. }
  570. static inline bool init_keepalive(struct game_capture *gc)
  571. {
  572. wchar_t new_name[64];
  573. swprintf(new_name, 64, WINDOW_HOOK_KEEPALIVE L"%lu", gc->process_id);
  574. gc->keepalive_mutex = gc->is_app
  575. ? create_app_mutex(gc->app_sid, new_name)
  576. : CreateMutexW(NULL, false, new_name);
  577. if (!gc->keepalive_mutex) {
  578. warn("Failed to create keepalive mutex: %lu", GetLastError());
  579. return false;
  580. }
  581. return true;
  582. }
  583. static inline bool init_texture_mutexes(struct game_capture *gc)
  584. {
  585. gc->texture_mutexes[0] = open_mutex_gc(gc, MUTEX_TEXTURE1);
  586. gc->texture_mutexes[1] = open_mutex_gc(gc, MUTEX_TEXTURE2);
  587. if (!gc->texture_mutexes[0] || !gc->texture_mutexes[1]) {
  588. DWORD error = GetLastError();
  589. if (error == 2) {
  590. if (!gc->retrying) {
  591. gc->retrying = 2;
  592. info("hook not loaded yet, retrying..");
  593. }
  594. } else {
  595. warn("failed to open texture mutexes: %lu",
  596. GetLastError());
  597. }
  598. return false;
  599. }
  600. return true;
  601. }
  602. /* if there's already a hook in the process, then signal and start */
  603. static inline bool attempt_existing_hook(struct game_capture *gc)
  604. {
  605. gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  606. if (gc->hook_restart) {
  607. debug("existing hook found, signaling process: %s",
  608. gc->config.executable);
  609. SetEvent(gc->hook_restart);
  610. return true;
  611. }
  612. return false;
  613. }
  614. static inline void reset_frame_interval(struct game_capture *gc)
  615. {
  616. struct obs_video_info ovi;
  617. uint64_t interval = 0;
  618. if (obs_get_video_info(&ovi)) {
  619. interval =
  620. util_mul_div64(ovi.fps_den, 1000000000ULL, ovi.fps_num);
  621. /* Always limit capture framerate to some extent. If a game
  622. * running at 900 FPS is being captured without some sort of
  623. * limited capture interval, it will dramatically reduce
  624. * performance. */
  625. if (!gc->config.limit_framerate)
  626. interval /= 2;
  627. }
  628. gc->global_hook_info->frame_interval = interval;
  629. }
  630. static inline bool init_hook_info(struct game_capture *gc)
  631. {
  632. gc->global_hook_info_map = open_hook_info(gc);
  633. if (!gc->global_hook_info_map) {
  634. warn("init_hook_info: get_hook_info failed: %lu",
  635. GetLastError());
  636. return false;
  637. }
  638. gc->global_hook_info = MapViewOfFile(gc->global_hook_info_map,
  639. FILE_MAP_ALL_ACCESS, 0, 0,
  640. sizeof(*gc->global_hook_info));
  641. if (!gc->global_hook_info) {
  642. warn("init_hook_info: failed to map data view: %lu",
  643. GetLastError());
  644. return false;
  645. }
  646. if (gc->config.force_shmem) {
  647. warn("init_hook_info: user is forcing shared memory "
  648. "(multi-adapter compatibility mode)");
  649. }
  650. gc->global_hook_info->offsets = gc->process_is_64bit ? offsets64
  651. : offsets32;
  652. gc->global_hook_info->capture_overlay = gc->config.capture_overlays;
  653. gc->global_hook_info->force_shmem = gc->config.force_shmem;
  654. gc->global_hook_info->use_scale = gc->config.force_scaling;
  655. if (gc->config.scale_cx)
  656. gc->global_hook_info->cx = gc->config.scale_cx;
  657. if (gc->config.scale_cy)
  658. gc->global_hook_info->cy = gc->config.scale_cy;
  659. reset_frame_interval(gc);
  660. obs_enter_graphics();
  661. if (!gs_shared_texture_available()) {
  662. warn("init_hook_info: shared texture capture unavailable");
  663. gc->global_hook_info->force_shmem = true;
  664. }
  665. obs_leave_graphics();
  666. return true;
  667. }
  668. static void pipe_log(void *param, uint8_t *data, size_t size)
  669. {
  670. struct game_capture *gc = param;
  671. if (data && size)
  672. info("%s", data);
  673. }
  674. static inline bool init_pipe(struct game_capture *gc)
  675. {
  676. char name[64];
  677. sprintf(name, "%s%lu", PIPE_NAME, gc->process_id);
  678. if (!ipc_pipe_server_start(&gc->pipe, name, pipe_log, gc)) {
  679. warn("init_pipe: failed to start pipe");
  680. return false;
  681. }
  682. return true;
  683. }
  684. static inline int inject_library(HANDLE process, const wchar_t *dll)
  685. {
  686. return inject_library_obf(process, dll, "D|hkqkW`kl{k\\osofj",
  687. 0xa178ef3655e5ade7, "[uawaRzbhh{tIdkj~~",
  688. 0x561478dbd824387c, "[fr}pboIe`dlN}",
  689. 0x395bfbc9833590fd, "\\`zs}gmOzhhBq",
  690. 0x12897dd89168789a, "GbfkDaezbp~X",
  691. 0x76aff7238788f7db);
  692. }
  693. static inline bool hook_direct(struct game_capture *gc,
  694. const char *hook_path_rel)
  695. {
  696. wchar_t hook_path_abs_w[MAX_PATH];
  697. wchar_t *hook_path_rel_w;
  698. wchar_t *path_ret;
  699. HANDLE process;
  700. int ret;
  701. os_utf8_to_wcs_ptr(hook_path_rel, 0, &hook_path_rel_w);
  702. if (!hook_path_rel_w) {
  703. warn("hook_direct: could not convert string");
  704. return false;
  705. }
  706. path_ret = _wfullpath(hook_path_abs_w, hook_path_rel_w, MAX_PATH);
  707. bfree(hook_path_rel_w);
  708. if (path_ret == NULL) {
  709. warn("hook_direct: could not make absolute path");
  710. return false;
  711. }
  712. process = open_process(PROCESS_ALL_ACCESS, false, gc->process_id);
  713. if (!process) {
  714. warn("hook_direct: could not open process: %s (%lu)",
  715. gc->config.executable, GetLastError());
  716. return false;
  717. }
  718. ret = inject_library(process, hook_path_abs_w);
  719. CloseHandle(process);
  720. if (ret != 0) {
  721. warn("hook_direct: inject failed: %d", ret);
  722. return false;
  723. }
  724. return true;
  725. }
  726. static inline bool create_inject_process(struct game_capture *gc,
  727. const char *inject_path,
  728. const char *hook_dll)
  729. {
  730. wchar_t *command_line_w = malloc(4096 * sizeof(wchar_t));
  731. wchar_t *inject_path_w;
  732. wchar_t *hook_dll_w;
  733. bool anti_cheat = use_anticheat(gc);
  734. PROCESS_INFORMATION pi = {0};
  735. STARTUPINFO si = {0};
  736. bool success = false;
  737. os_utf8_to_wcs_ptr(inject_path, 0, &inject_path_w);
  738. os_utf8_to_wcs_ptr(hook_dll, 0, &hook_dll_w);
  739. si.cb = sizeof(si);
  740. swprintf(command_line_w, 4096, L"\"%s\" \"%s\" %lu %lu", inject_path_w,
  741. hook_dll_w, (unsigned long)anti_cheat,
  742. anti_cheat ? gc->thread_id : gc->process_id);
  743. success = !!CreateProcessW(inject_path_w, command_line_w, NULL, NULL,
  744. false, CREATE_NO_WINDOW, NULL, NULL, &si,
  745. &pi);
  746. if (success) {
  747. CloseHandle(pi.hThread);
  748. gc->injector_process = pi.hProcess;
  749. } else {
  750. warn("Failed to create inject helper process: %lu",
  751. GetLastError());
  752. }
  753. free(command_line_w);
  754. bfree(inject_path_w);
  755. bfree(hook_dll_w);
  756. return success;
  757. }
  758. extern char *get_hook_path(bool b64);
  759. static inline bool inject_hook(struct game_capture *gc)
  760. {
  761. bool matching_architecture;
  762. bool success = false;
  763. char *inject_path;
  764. char *hook_path;
  765. if (gc->process_is_64bit) {
  766. inject_path = obs_module_file("inject-helper64.exe");
  767. } else {
  768. inject_path = obs_module_file("inject-helper32.exe");
  769. }
  770. hook_path = get_hook_path(gc->process_is_64bit);
  771. if (!check_file_integrity(gc, inject_path, "inject helper")) {
  772. goto cleanup;
  773. }
  774. if (!check_file_integrity(gc, hook_path, "graphics hook")) {
  775. goto cleanup;
  776. }
  777. #ifdef _WIN64
  778. matching_architecture = gc->process_is_64bit;
  779. #else
  780. matching_architecture = !gc->process_is_64bit;
  781. #endif
  782. if (matching_architecture && !use_anticheat(gc)) {
  783. info("using direct hook");
  784. success = hook_direct(gc, hook_path);
  785. } else {
  786. info("using helper (%s hook)",
  787. use_anticheat(gc) ? "compatibility" : "direct");
  788. success = create_inject_process(gc, inject_path, hook_path);
  789. }
  790. cleanup:
  791. bfree(inject_path);
  792. bfree(hook_path);
  793. return success;
  794. }
  795. static const char *blacklisted_exes[] = {
  796. "explorer",
  797. "steam",
  798. "battle.net",
  799. "galaxyclient",
  800. "skype",
  801. "uplay",
  802. "origin",
  803. "devenv",
  804. "taskmgr",
  805. "chrome",
  806. "discord",
  807. "firefox",
  808. "systemsettings",
  809. "applicationframehost",
  810. "cmd",
  811. "shellexperiencehost",
  812. "winstore.app",
  813. "searchui",
  814. "lockapp",
  815. "windowsinternal.composableshell.experiences.textinput.inputapp",
  816. NULL,
  817. };
  818. static bool is_blacklisted_exe(const char *exe)
  819. {
  820. char cur_exe[MAX_PATH];
  821. if (!exe)
  822. return false;
  823. for (const char **vals = blacklisted_exes; *vals; vals++) {
  824. strcpy(cur_exe, *vals);
  825. strcat(cur_exe, ".exe");
  826. if (strcmpi(cur_exe, exe) == 0)
  827. return true;
  828. }
  829. return false;
  830. }
  831. static bool target_suspended(struct game_capture *gc)
  832. {
  833. return thread_is_suspended(gc->process_id, gc->thread_id);
  834. }
  835. static bool init_events(struct game_capture *gc);
  836. static bool init_hook(struct game_capture *gc)
  837. {
  838. struct dstr exe = {0};
  839. bool blacklisted_process = false;
  840. if (gc->config.mode == CAPTURE_MODE_ANY) {
  841. if (get_window_exe(&exe, gc->next_window)) {
  842. info("attempting to hook fullscreen process: %s",
  843. exe.array);
  844. }
  845. } else {
  846. if (get_window_exe(&exe, gc->next_window)) {
  847. info("attempting to hook process: %s", exe.array);
  848. }
  849. }
  850. blacklisted_process = is_blacklisted_exe(exe.array);
  851. if (blacklisted_process)
  852. info("cannot capture %s due to being blacklisted", exe.array);
  853. dstr_free(&exe);
  854. if (blacklisted_process) {
  855. return false;
  856. }
  857. if (target_suspended(gc)) {
  858. return false;
  859. }
  860. if (!open_target_process(gc)) {
  861. return false;
  862. }
  863. if (!init_keepalive(gc)) {
  864. return false;
  865. }
  866. if (!init_pipe(gc)) {
  867. return false;
  868. }
  869. if (!attempt_existing_hook(gc)) {
  870. if (!inject_hook(gc)) {
  871. return false;
  872. }
  873. }
  874. if (!init_texture_mutexes(gc)) {
  875. return false;
  876. }
  877. if (!init_hook_info(gc)) {
  878. return false;
  879. }
  880. if (!init_events(gc)) {
  881. return false;
  882. }
  883. SetEvent(gc->hook_init);
  884. gc->window = gc->next_window;
  885. gc->next_window = NULL;
  886. gc->active = true;
  887. gc->retrying = 0;
  888. return true;
  889. }
  890. static void setup_window(struct game_capture *gc, HWND window)
  891. {
  892. HANDLE hook_restart;
  893. HANDLE process;
  894. GetWindowThreadProcessId(window, &gc->process_id);
  895. if (gc->process_id) {
  896. process = open_process(PROCESS_QUERY_INFORMATION, false,
  897. gc->process_id);
  898. if (process) {
  899. gc->is_app = is_app(process);
  900. if (gc->is_app) {
  901. gc->app_sid = get_app_sid(process);
  902. }
  903. CloseHandle(process);
  904. }
  905. }
  906. /* do not wait if we're re-hooking a process */
  907. hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  908. if (hook_restart) {
  909. gc->wait_for_target_startup = false;
  910. CloseHandle(hook_restart);
  911. }
  912. /* otherwise if it's an unhooked process, always wait a bit for the
  913. * target process to start up before starting the hook process;
  914. * sometimes they have important modules to load first or other hooks
  915. * (such as steam) need a little bit of time to load. ultimately this
  916. * helps prevent crashes */
  917. if (gc->wait_for_target_startup) {
  918. gc->retry_interval =
  919. 3.0f * hook_rate_to_float(gc->config.hook_rate);
  920. gc->wait_for_target_startup = false;
  921. } else {
  922. gc->next_window = window;
  923. }
  924. }
  925. static void get_fullscreen_window(struct game_capture *gc)
  926. {
  927. HWND window = GetForegroundWindow();
  928. MONITORINFO mi = {0};
  929. HMONITOR monitor;
  930. DWORD styles;
  931. RECT rect;
  932. gc->next_window = NULL;
  933. if (!window) {
  934. return;
  935. }
  936. if (!GetWindowRect(window, &rect)) {
  937. return;
  938. }
  939. /* ignore regular maximized windows */
  940. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  941. if ((styles & WS_MAXIMIZE) != 0 && (styles & WS_BORDER) != 0) {
  942. return;
  943. }
  944. monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
  945. if (!monitor) {
  946. return;
  947. }
  948. mi.cbSize = sizeof(mi);
  949. if (!GetMonitorInfo(monitor, &mi)) {
  950. return;
  951. }
  952. if (rect.left == mi.rcMonitor.left &&
  953. rect.right == mi.rcMonitor.right &&
  954. rect.bottom == mi.rcMonitor.bottom &&
  955. rect.top == mi.rcMonitor.top) {
  956. setup_window(gc, window);
  957. } else {
  958. gc->wait_for_target_startup = true;
  959. }
  960. }
  961. static void get_selected_window(struct game_capture *gc)
  962. {
  963. HWND window;
  964. if (dstr_cmpi(&gc->class, "dwm") == 0) {
  965. wchar_t class_w[512];
  966. os_utf8_to_wcs(gc->class.array, 0, class_w, 512);
  967. window = FindWindowW(class_w, NULL);
  968. } else {
  969. window = find_window(INCLUDE_MINIMIZED, gc->priority,
  970. gc->class.array, gc->title.array,
  971. gc->executable.array);
  972. }
  973. if (window) {
  974. setup_window(gc, window);
  975. } else {
  976. gc->wait_for_target_startup = true;
  977. }
  978. }
  979. static void try_hook(struct game_capture *gc)
  980. {
  981. if (gc->config.mode == CAPTURE_MODE_ANY) {
  982. get_fullscreen_window(gc);
  983. } else {
  984. get_selected_window(gc);
  985. }
  986. if (gc->next_window) {
  987. gc->thread_id = GetWindowThreadProcessId(gc->next_window,
  988. &gc->process_id);
  989. // Make sure we never try to hook ourselves (projector)
  990. if (gc->process_id == GetCurrentProcessId())
  991. return;
  992. if (!gc->thread_id && gc->process_id)
  993. return;
  994. if (!gc->process_id) {
  995. warn("error acquiring, failed to get window "
  996. "thread/process ids: %lu",
  997. GetLastError());
  998. gc->error_acquiring = true;
  999. return;
  1000. }
  1001. if (!init_hook(gc)) {
  1002. stop_capture(gc);
  1003. }
  1004. } else {
  1005. gc->active = false;
  1006. }
  1007. }
  1008. static inline bool init_events(struct game_capture *gc)
  1009. {
  1010. if (!gc->hook_restart) {
  1011. gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  1012. if (!gc->hook_restart) {
  1013. warn("init_events: failed to get hook_restart "
  1014. "event: %lu",
  1015. GetLastError());
  1016. return false;
  1017. }
  1018. }
  1019. if (!gc->hook_stop) {
  1020. gc->hook_stop = open_event_gc(gc, EVENT_CAPTURE_STOP);
  1021. if (!gc->hook_stop) {
  1022. warn("init_events: failed to get hook_stop event: %lu",
  1023. GetLastError());
  1024. return false;
  1025. }
  1026. }
  1027. if (!gc->hook_init) {
  1028. gc->hook_init = open_event_gc(gc, EVENT_HOOK_INIT);
  1029. if (!gc->hook_init) {
  1030. warn("init_events: failed to get hook_init event: %lu",
  1031. GetLastError());
  1032. return false;
  1033. }
  1034. }
  1035. if (!gc->hook_ready) {
  1036. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1037. if (!gc->hook_ready) {
  1038. warn("init_events: failed to get hook_ready event: %lu",
  1039. GetLastError());
  1040. return false;
  1041. }
  1042. }
  1043. if (!gc->hook_exit) {
  1044. gc->hook_exit = open_event_gc(gc, EVENT_HOOK_EXIT);
  1045. if (!gc->hook_exit) {
  1046. warn("init_events: failed to get hook_exit event: %lu",
  1047. GetLastError());
  1048. return false;
  1049. }
  1050. }
  1051. return true;
  1052. }
  1053. enum capture_result { CAPTURE_FAIL, CAPTURE_RETRY, CAPTURE_SUCCESS };
  1054. static inline bool init_data_map(struct game_capture *gc, HWND window)
  1055. {
  1056. wchar_t name[64];
  1057. swprintf(name, 64, SHMEM_TEXTURE "_%" PRIu64 "_",
  1058. (uint64_t)(uintptr_t)window);
  1059. gc->hook_data_map =
  1060. open_map_plus_id(gc, name, gc->global_hook_info->map_id);
  1061. return !!gc->hook_data_map;
  1062. }
  1063. static inline enum capture_result init_capture_data(struct game_capture *gc)
  1064. {
  1065. gc->cx = gc->global_hook_info->cx;
  1066. gc->cy = gc->global_hook_info->cy;
  1067. gc->pitch = gc->global_hook_info->pitch;
  1068. if (gc->data) {
  1069. UnmapViewOfFile(gc->data);
  1070. gc->data = NULL;
  1071. }
  1072. CloseHandle(gc->hook_data_map);
  1073. DWORD error = 0;
  1074. if (!init_data_map(gc, gc->window)) {
  1075. HWND retry_hwnd = (HWND)(uintptr_t)gc->global_hook_info->window;
  1076. error = GetLastError();
  1077. /* if there's an error, just override. some windows don't play
  1078. * nice. */
  1079. if (init_data_map(gc, retry_hwnd)) {
  1080. error = 0;
  1081. }
  1082. }
  1083. if (!gc->hook_data_map) {
  1084. if (error == 2) {
  1085. return CAPTURE_RETRY;
  1086. } else {
  1087. warn("init_capture_data: failed to open file "
  1088. "mapping: %lu",
  1089. error);
  1090. }
  1091. return CAPTURE_FAIL;
  1092. }
  1093. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  1094. gc->global_hook_info->map_size);
  1095. if (!gc->data) {
  1096. warn("init_capture_data: failed to map data view: %lu",
  1097. GetLastError());
  1098. return CAPTURE_FAIL;
  1099. }
  1100. return CAPTURE_SUCCESS;
  1101. }
  1102. #define PIXEL_16BIT_SIZE 2
  1103. #define PIXEL_32BIT_SIZE 4
  1104. static inline uint32_t convert_5_to_8bit(uint16_t val)
  1105. {
  1106. return (uint32_t)((double)(val & 0x1F) * (255.0 / 31.0));
  1107. }
  1108. static inline uint32_t convert_6_to_8bit(uint16_t val)
  1109. {
  1110. return (uint32_t)((double)(val & 0x3F) * (255.0 / 63.0));
  1111. }
  1112. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  1113. uint8_t *data, uint32_t pitch)
  1114. {
  1115. uint8_t *input = gc->texture_buffers[cur_texture];
  1116. uint32_t gc_cx = gc->cx;
  1117. uint32_t gc_cy = gc->cy;
  1118. uint32_t gc_pitch = gc->pitch;
  1119. for (size_t y = 0; y < gc_cy; y++) {
  1120. uint8_t *row = input + (gc_pitch * y);
  1121. uint8_t *out = data + (pitch * y);
  1122. for (size_t x = 0; x < gc_cx; x += 8) {
  1123. __m128i pixels_blue, pixels_green, pixels_red;
  1124. __m128i pixels_result;
  1125. __m128i *pixels_dest;
  1126. __m128i *pixels_src =
  1127. (__m128i *)(row + x * sizeof(uint16_t));
  1128. __m128i pixels = _mm_load_si128(pixels_src);
  1129. __m128i zero = _mm_setzero_si128();
  1130. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1131. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1132. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1133. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1134. __m128i green_channel_mask = _mm_set1_epi32(0x000007E0);
  1135. __m128i green_offset = _mm_set1_epi32(0x00000008);
  1136. __m128i red_channel_mask = _mm_set1_epi32(0x0000F800);
  1137. __m128i red_offset = _mm_set1_epi32(0x00000300);
  1138. pixels_blue =
  1139. _mm_and_si128(pixels_low, blue_channel_mask);
  1140. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1141. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1142. pixels_green =
  1143. _mm_and_si128(pixels_low, green_channel_mask);
  1144. pixels_green =
  1145. _mm_add_epi32(pixels_green, green_offset);
  1146. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1147. pixels_red =
  1148. _mm_and_si128(pixels_low, red_channel_mask);
  1149. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1150. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1151. pixels_result = _mm_set1_epi32(0xFF000000);
  1152. pixels_result =
  1153. _mm_or_si128(pixels_result, pixels_blue);
  1154. pixels_result =
  1155. _mm_or_si128(pixels_result, pixels_green);
  1156. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1157. pixels_dest = (__m128i *)(out + x * sizeof(uint32_t));
  1158. _mm_store_si128(pixels_dest, pixels_result);
  1159. pixels_blue =
  1160. _mm_and_si128(pixels_high, blue_channel_mask);
  1161. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1162. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1163. pixels_green =
  1164. _mm_and_si128(pixels_high, green_channel_mask);
  1165. pixels_green =
  1166. _mm_add_epi32(pixels_green, green_offset);
  1167. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1168. pixels_red =
  1169. _mm_and_si128(pixels_high, red_channel_mask);
  1170. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1171. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1172. pixels_result = _mm_set1_epi32(0xFF000000);
  1173. pixels_result =
  1174. _mm_or_si128(pixels_result, pixels_blue);
  1175. pixels_result =
  1176. _mm_or_si128(pixels_result, pixels_green);
  1177. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1178. pixels_dest =
  1179. (__m128i *)(out + (x + 4) * sizeof(uint32_t));
  1180. _mm_store_si128(pixels_dest, pixels_result);
  1181. }
  1182. }
  1183. }
  1184. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  1185. uint8_t *data, uint32_t pitch)
  1186. {
  1187. uint8_t *input = gc->texture_buffers[cur_texture];
  1188. uint32_t gc_cx = gc->cx;
  1189. uint32_t gc_cy = gc->cy;
  1190. uint32_t gc_pitch = gc->pitch;
  1191. for (size_t y = 0; y < gc_cy; y++) {
  1192. uint8_t *row = input + (gc_pitch * y);
  1193. uint8_t *out = data + (pitch * y);
  1194. for (size_t x = 0; x < gc_cx; x += 8) {
  1195. __m128i pixels_blue, pixels_green, pixels_red,
  1196. pixels_alpha;
  1197. __m128i pixels_result;
  1198. __m128i *pixels_dest;
  1199. __m128i *pixels_src =
  1200. (__m128i *)(row + x * sizeof(uint16_t));
  1201. __m128i pixels = _mm_load_si128(pixels_src);
  1202. __m128i zero = _mm_setzero_si128();
  1203. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1204. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1205. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1206. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1207. __m128i green_channel_mask = _mm_set1_epi32(0x000003E0);
  1208. __m128i green_offset = _mm_set1_epi32(0x000000C);
  1209. __m128i red_channel_mask = _mm_set1_epi32(0x00007C00);
  1210. __m128i red_offset = _mm_set1_epi32(0x00000180);
  1211. __m128i alpha_channel_mask = _mm_set1_epi32(0x00008000);
  1212. __m128i alpha_offset = _mm_set1_epi32(0x00000001);
  1213. __m128i alpha_mask32 = _mm_set1_epi32(0xFF000000);
  1214. pixels_blue =
  1215. _mm_and_si128(pixels_low, blue_channel_mask);
  1216. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1217. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1218. pixels_green =
  1219. _mm_and_si128(pixels_low, green_channel_mask);
  1220. pixels_green =
  1221. _mm_add_epi32(pixels_green, green_offset);
  1222. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1223. pixels_red =
  1224. _mm_and_si128(pixels_low, red_channel_mask);
  1225. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1226. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1227. pixels_alpha =
  1228. _mm_and_si128(pixels_low, alpha_channel_mask);
  1229. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1230. pixels_alpha =
  1231. _mm_sub_epi32(pixels_alpha, alpha_offset);
  1232. pixels_alpha =
  1233. _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1234. pixels_result = pixels_red;
  1235. pixels_result =
  1236. _mm_or_si128(pixels_result, pixels_alpha);
  1237. pixels_result =
  1238. _mm_or_si128(pixels_result, pixels_blue);
  1239. pixels_result =
  1240. _mm_or_si128(pixels_result, pixels_green);
  1241. pixels_dest = (__m128i *)(out + x * sizeof(uint32_t));
  1242. _mm_store_si128(pixels_dest, pixels_result);
  1243. pixels_blue =
  1244. _mm_and_si128(pixels_high, blue_channel_mask);
  1245. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1246. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1247. pixels_green =
  1248. _mm_and_si128(pixels_high, green_channel_mask);
  1249. pixels_green =
  1250. _mm_add_epi32(pixels_green, green_offset);
  1251. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1252. pixels_red =
  1253. _mm_and_si128(pixels_high, red_channel_mask);
  1254. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1255. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1256. pixels_alpha =
  1257. _mm_and_si128(pixels_high, alpha_channel_mask);
  1258. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1259. pixels_alpha =
  1260. _mm_sub_epi32(pixels_alpha, alpha_offset);
  1261. pixels_alpha =
  1262. _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1263. pixels_result = pixels_red;
  1264. pixels_result =
  1265. _mm_or_si128(pixels_result, pixels_alpha);
  1266. pixels_result =
  1267. _mm_or_si128(pixels_result, pixels_blue);
  1268. pixels_result =
  1269. _mm_or_si128(pixels_result, pixels_green);
  1270. pixels_dest =
  1271. (__m128i *)(out + (x + 4) * sizeof(uint32_t));
  1272. _mm_store_si128(pixels_dest, pixels_result);
  1273. }
  1274. }
  1275. }
  1276. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  1277. uint8_t *data, uint32_t pitch)
  1278. {
  1279. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  1280. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  1281. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  1282. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  1283. }
  1284. }
  1285. static void copy_shmem_tex(struct game_capture *gc)
  1286. {
  1287. int cur_texture;
  1288. HANDLE mutex = NULL;
  1289. uint32_t pitch;
  1290. int next_texture;
  1291. uint8_t *data;
  1292. if (!gc->shmem_data)
  1293. return;
  1294. cur_texture = gc->shmem_data->last_tex;
  1295. if (cur_texture < 0 || cur_texture > 1)
  1296. return;
  1297. next_texture = cur_texture == 1 ? 0 : 1;
  1298. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  1299. mutex = gc->texture_mutexes[cur_texture];
  1300. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  1301. mutex = gc->texture_mutexes[next_texture];
  1302. cur_texture = next_texture;
  1303. } else {
  1304. return;
  1305. }
  1306. if (gs_texture_map(gc->texture, &data, &pitch)) {
  1307. if (gc->convert_16bit) {
  1308. copy_16bit_tex(gc, cur_texture, data, pitch);
  1309. } else if (pitch == gc->pitch) {
  1310. memcpy(data, gc->texture_buffers[cur_texture],
  1311. (size_t)pitch * (size_t)gc->cy);
  1312. } else {
  1313. uint8_t *input = gc->texture_buffers[cur_texture];
  1314. uint32_t best_pitch = pitch < gc->pitch ? pitch
  1315. : gc->pitch;
  1316. for (size_t y = 0; y < gc->cy; y++) {
  1317. uint8_t *line_in = input + gc->pitch * y;
  1318. uint8_t *line_out = data + pitch * y;
  1319. memcpy(line_out, line_in, best_pitch);
  1320. }
  1321. }
  1322. gs_texture_unmap(gc->texture);
  1323. }
  1324. ReleaseMutex(mutex);
  1325. }
  1326. static inline bool is_16bit_format(uint32_t format)
  1327. {
  1328. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  1329. format == DXGI_FORMAT_B5G6R5_UNORM;
  1330. }
  1331. static inline bool init_shmem_capture(struct game_capture *gc)
  1332. {
  1333. enum gs_color_format format;
  1334. gc->texture_buffers[0] =
  1335. (uint8_t *)gc->data + gc->shmem_data->tex1_offset;
  1336. gc->texture_buffers[1] =
  1337. (uint8_t *)gc->data + gc->shmem_data->tex2_offset;
  1338. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  1339. format = gc->convert_16bit
  1340. ? GS_BGRA
  1341. : convert_format(gc->global_hook_info->format);
  1342. obs_enter_graphics();
  1343. gs_texture_destroy(gc->texture);
  1344. gc->texture =
  1345. gs_texture_create(gc->cx, gc->cy, format, 1, NULL, GS_DYNAMIC);
  1346. obs_leave_graphics();
  1347. if (!gc->texture) {
  1348. warn("init_shmem_capture: failed to create texture");
  1349. return false;
  1350. }
  1351. gc->copy_texture = copy_shmem_tex;
  1352. return true;
  1353. }
  1354. static inline bool init_shtex_capture(struct game_capture *gc)
  1355. {
  1356. obs_enter_graphics();
  1357. gs_texture_destroy(gc->texture);
  1358. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  1359. obs_leave_graphics();
  1360. if (!gc->texture) {
  1361. warn("init_shtex_capture: failed to open shared handle");
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. static bool start_capture(struct game_capture *gc)
  1367. {
  1368. debug("Starting capture");
  1369. /* prevent from using a DLL version that's higher than current */
  1370. if (gc->global_hook_info->hook_ver_major > HOOK_VER_MAJOR) {
  1371. warn("cannot initialize hook, DLL hook version is "
  1372. "%" PRIu32 ".%" PRIu32
  1373. ", current plugin hook major version is %d.%d",
  1374. gc->global_hook_info->hook_ver_major,
  1375. gc->global_hook_info->hook_ver_minor, HOOK_VER_MAJOR,
  1376. HOOK_VER_MINOR);
  1377. return false;
  1378. }
  1379. if (gc->global_hook_info->type == CAPTURE_TYPE_MEMORY) {
  1380. if (!init_shmem_capture(gc)) {
  1381. return false;
  1382. }
  1383. info("memory capture successful");
  1384. } else {
  1385. if (!init_shtex_capture(gc)) {
  1386. return false;
  1387. }
  1388. info("shared texture capture successful");
  1389. }
  1390. return true;
  1391. }
  1392. static inline bool capture_valid(struct game_capture *gc)
  1393. {
  1394. if (!gc->dwm_capture && !IsWindow(gc->window))
  1395. return false;
  1396. return !object_signalled(gc->target_process);
  1397. }
  1398. static void check_foreground_window(struct game_capture *gc, float seconds)
  1399. {
  1400. // Hides the cursor if the user isn't actively in the game
  1401. gc->cursor_check_time += seconds;
  1402. if (gc->cursor_check_time >= 0.1f) {
  1403. DWORD foreground_process_id;
  1404. GetWindowThreadProcessId(GetForegroundWindow(),
  1405. &foreground_process_id);
  1406. if (gc->process_id != foreground_process_id)
  1407. gc->cursor_hidden = true;
  1408. else
  1409. gc->cursor_hidden = false;
  1410. gc->cursor_check_time = 0.0f;
  1411. }
  1412. }
  1413. static void game_capture_tick(void *data, float seconds)
  1414. {
  1415. struct game_capture *gc = data;
  1416. bool deactivate = os_atomic_set_bool(&gc->deactivate_hook, false);
  1417. bool activate_now = os_atomic_set_bool(&gc->activate_hook_now, false);
  1418. if (activate_now) {
  1419. HWND hwnd = (HWND)(uintptr_t)os_atomic_load_long(
  1420. &gc->hotkey_window);
  1421. if (is_uwp_window(hwnd))
  1422. hwnd = get_uwp_actual_window(hwnd);
  1423. if (get_window_exe(&gc->executable, hwnd)) {
  1424. get_window_title(&gc->title, hwnd);
  1425. get_window_class(&gc->class, hwnd);
  1426. gc->priority = WINDOW_PRIORITY_CLASS;
  1427. gc->retry_time = 10.0f * hook_rate_to_float(
  1428. gc->config.hook_rate);
  1429. gc->activate_hook = true;
  1430. } else {
  1431. deactivate = false;
  1432. activate_now = false;
  1433. }
  1434. } else if (deactivate) {
  1435. gc->activate_hook = false;
  1436. }
  1437. if (!obs_source_showing(gc->source)) {
  1438. if (gc->showing) {
  1439. if (gc->active)
  1440. stop_capture(gc);
  1441. gc->showing = false;
  1442. }
  1443. return;
  1444. } else if (!gc->showing) {
  1445. gc->retry_time =
  1446. 10.0f * hook_rate_to_float(gc->config.hook_rate);
  1447. }
  1448. if (gc->hook_stop && object_signalled(gc->hook_stop)) {
  1449. debug("hook stop signal received");
  1450. stop_capture(gc);
  1451. }
  1452. if (gc->active && deactivate) {
  1453. stop_capture(gc);
  1454. }
  1455. if (gc->active && !gc->hook_ready && gc->process_id) {
  1456. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1457. }
  1458. if (gc->injector_process && object_signalled(gc->injector_process)) {
  1459. DWORD exit_code = 0;
  1460. GetExitCodeProcess(gc->injector_process, &exit_code);
  1461. close_handle(&gc->injector_process);
  1462. if (exit_code != 0) {
  1463. warn("inject process failed: %ld", (long)exit_code);
  1464. gc->error_acquiring = true;
  1465. } else if (!gc->capturing) {
  1466. gc->retry_interval =
  1467. ERROR_RETRY_INTERVAL *
  1468. hook_rate_to_float(gc->config.hook_rate);
  1469. stop_capture(gc);
  1470. }
  1471. }
  1472. if (gc->hook_ready && object_signalled(gc->hook_ready)) {
  1473. debug("capture initializing!");
  1474. enum capture_result result = init_capture_data(gc);
  1475. if (result == CAPTURE_SUCCESS)
  1476. gc->capturing = start_capture(gc);
  1477. else
  1478. debug("init_capture_data failed");
  1479. if (result != CAPTURE_RETRY && !gc->capturing) {
  1480. gc->retry_interval =
  1481. ERROR_RETRY_INTERVAL *
  1482. hook_rate_to_float(gc->config.hook_rate);
  1483. stop_capture(gc);
  1484. }
  1485. }
  1486. gc->retry_time += seconds;
  1487. if (!gc->active) {
  1488. if (!gc->error_acquiring &&
  1489. gc->retry_time > gc->retry_interval) {
  1490. if (gc->config.mode == CAPTURE_MODE_ANY ||
  1491. gc->activate_hook) {
  1492. try_hook(gc);
  1493. gc->retry_time = 0.0f;
  1494. }
  1495. }
  1496. } else {
  1497. if (!capture_valid(gc)) {
  1498. info("capture window no longer exists, "
  1499. "terminating capture");
  1500. stop_capture(gc);
  1501. } else {
  1502. if (gc->copy_texture) {
  1503. obs_enter_graphics();
  1504. gc->copy_texture(gc);
  1505. obs_leave_graphics();
  1506. }
  1507. if (gc->config.cursor) {
  1508. check_foreground_window(gc, seconds);
  1509. obs_enter_graphics();
  1510. cursor_capture(&gc->cursor_data);
  1511. obs_leave_graphics();
  1512. }
  1513. gc->fps_reset_time += seconds;
  1514. if (gc->fps_reset_time >= gc->retry_interval) {
  1515. reset_frame_interval(gc);
  1516. gc->fps_reset_time = 0.0f;
  1517. }
  1518. }
  1519. }
  1520. if (!gc->showing)
  1521. gc->showing = true;
  1522. }
  1523. static inline void game_capture_render_cursor(struct game_capture *gc)
  1524. {
  1525. POINT p = {0};
  1526. HWND window;
  1527. if (!gc->global_hook_info->base_cx || !gc->global_hook_info->base_cy)
  1528. return;
  1529. window = !!gc->global_hook_info->window
  1530. ? (HWND)(uintptr_t)gc->global_hook_info->window
  1531. : gc->window;
  1532. ClientToScreen(window, &p);
  1533. float x_scale = (float)gc->global_hook_info->cx /
  1534. (float)gc->global_hook_info->base_cx;
  1535. float y_scale = (float)gc->global_hook_info->cy /
  1536. (float)gc->global_hook_info->base_cy;
  1537. cursor_draw(&gc->cursor_data, -p.x, -p.y, x_scale, y_scale,
  1538. gc->global_hook_info->base_cx,
  1539. gc->global_hook_info->base_cy);
  1540. }
  1541. static void game_capture_render(void *data, gs_effect_t *effect)
  1542. {
  1543. struct game_capture *gc = data;
  1544. if (!gc->texture || !gc->active)
  1545. return;
  1546. effect = obs_get_base_effect(gc->config.allow_transparency
  1547. ? OBS_EFFECT_DEFAULT
  1548. : OBS_EFFECT_OPAQUE);
  1549. while (gs_effect_loop(effect, "Draw")) {
  1550. obs_source_draw(gc->texture, 0, 0, 0, 0,
  1551. gc->global_hook_info->flip);
  1552. if (gc->config.allow_transparency && gc->config.cursor &&
  1553. !gc->cursor_hidden) {
  1554. game_capture_render_cursor(gc);
  1555. }
  1556. }
  1557. if (!gc->config.allow_transparency && gc->config.cursor &&
  1558. !gc->cursor_hidden) {
  1559. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1560. while (gs_effect_loop(effect, "Draw")) {
  1561. game_capture_render_cursor(gc);
  1562. }
  1563. }
  1564. }
  1565. static uint32_t game_capture_width(void *data)
  1566. {
  1567. struct game_capture *gc = data;
  1568. return gc->active ? gc->cx : 0;
  1569. }
  1570. static uint32_t game_capture_height(void *data)
  1571. {
  1572. struct game_capture *gc = data;
  1573. return gc->active ? gc->cy : 0;
  1574. }
  1575. static const char *game_capture_name(void *unused)
  1576. {
  1577. UNUSED_PARAMETER(unused);
  1578. return TEXT_GAME_CAPTURE;
  1579. }
  1580. static void game_capture_defaults(obs_data_t *settings)
  1581. {
  1582. obs_data_set_default_string(settings, SETTING_MODE, SETTING_MODE_ANY);
  1583. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1584. (int)WINDOW_PRIORITY_EXE);
  1585. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1586. obs_data_set_default_bool(settings, SETTING_FORCE_SCALING, false);
  1587. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1588. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1589. obs_data_set_default_string(settings, SETTING_SCALE_RES, "0x0");
  1590. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1591. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1592. obs_data_set_default_bool(settings, SETTING_ANTI_CHEAT_HOOK, true);
  1593. obs_data_set_default_int(settings, SETTING_HOOK_RATE,
  1594. (int)HOOK_RATE_NORMAL);
  1595. }
  1596. static bool mode_callback(obs_properties_t *ppts, obs_property_t *p,
  1597. obs_data_t *settings)
  1598. {
  1599. bool capture_window;
  1600. if (using_older_non_mode_format(settings)) {
  1601. capture_window =
  1602. !obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
  1603. } else {
  1604. const char *mode = obs_data_get_string(settings, SETTING_MODE);
  1605. capture_window = strcmp(mode, SETTING_MODE_WINDOW) == 0;
  1606. }
  1607. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1608. obs_property_set_visible(p, capture_window);
  1609. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1610. obs_property_set_visible(p, capture_window);
  1611. return true;
  1612. }
  1613. static bool use_scaling_callback(obs_properties_t *ppts, obs_property_t *p,
  1614. obs_data_t *settings)
  1615. {
  1616. bool use_scale = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  1617. p = obs_properties_get(ppts, SETTING_SCALE_RES);
  1618. obs_property_set_enabled(p, use_scale);
  1619. return true;
  1620. }
  1621. static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
  1622. {
  1623. char *class = NULL;
  1624. char *title = NULL;
  1625. char *executable = NULL;
  1626. struct dstr desc = {0};
  1627. build_window_strings(val, &class, &title, &executable);
  1628. dstr_printf(&desc, "[%s]: %s", executable, title);
  1629. obs_property_list_insert_string(p, idx, desc.array, val);
  1630. obs_property_list_item_disable(p, idx, true);
  1631. dstr_free(&desc);
  1632. bfree(class);
  1633. bfree(title);
  1634. bfree(executable);
  1635. }
  1636. bool check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
  1637. obs_data_t *settings, const char *val,
  1638. size_t idx)
  1639. {
  1640. const char *cur_val;
  1641. bool match = false;
  1642. size_t i = 0;
  1643. cur_val = obs_data_get_string(settings, val);
  1644. if (!cur_val) {
  1645. return false;
  1646. }
  1647. for (;;) {
  1648. const char *val = obs_property_list_item_string(p, i++);
  1649. if (!val)
  1650. break;
  1651. if (strcmp(val, cur_val) == 0) {
  1652. match = true;
  1653. break;
  1654. }
  1655. }
  1656. if (cur_val && *cur_val && !match) {
  1657. insert_preserved_val(p, cur_val, idx);
  1658. return true;
  1659. }
  1660. UNUSED_PARAMETER(ppts);
  1661. return false;
  1662. }
  1663. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1664. obs_data_t *settings)
  1665. {
  1666. return check_window_property_setting(ppts, p, settings,
  1667. SETTING_CAPTURE_WINDOW, 1);
  1668. }
  1669. static const double default_scale_vals[] = {1.25, 1.5, 2.0, 2.5, 3.0};
  1670. #define NUM_DEFAULT_SCALE_VALS \
  1671. (sizeof(default_scale_vals) / sizeof(default_scale_vals[0]))
  1672. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc, LPRECT rc,
  1673. LPARAM data)
  1674. {
  1675. *(HMONITOR *)data = monitor;
  1676. UNUSED_PARAMETER(hdc);
  1677. UNUSED_PARAMETER(rc);
  1678. return false;
  1679. }
  1680. static bool window_not_blacklisted(const char *title, const char *class,
  1681. const char *exe)
  1682. {
  1683. UNUSED_PARAMETER(title);
  1684. UNUSED_PARAMETER(class);
  1685. return !is_blacklisted_exe(exe);
  1686. }
  1687. static obs_properties_t *game_capture_properties(void *data)
  1688. {
  1689. HMONITOR monitor;
  1690. uint32_t cx = 1920;
  1691. uint32_t cy = 1080;
  1692. /* scaling is free form, this is mostly just to provide some common
  1693. * values */
  1694. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1695. (LPARAM)&monitor);
  1696. if (success) {
  1697. MONITORINFO mi = {0};
  1698. mi.cbSize = sizeof(mi);
  1699. if (!!GetMonitorInfo(monitor, &mi)) {
  1700. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1701. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1702. }
  1703. }
  1704. /* update from deprecated settings */
  1705. if (data) {
  1706. struct game_capture *gc = data;
  1707. obs_data_t *settings = obs_source_get_settings(gc->source);
  1708. if (using_older_non_mode_format(settings)) {
  1709. bool any = obs_data_get_bool(settings,
  1710. SETTING_ANY_FULLSCREEN);
  1711. const char *mode = any ? SETTING_MODE_ANY
  1712. : SETTING_MODE_WINDOW;
  1713. obs_data_set_string(settings, SETTING_MODE, mode);
  1714. }
  1715. obs_data_release(settings);
  1716. }
  1717. obs_properties_t *ppts = obs_properties_create();
  1718. obs_property_t *p;
  1719. p = obs_properties_add_list(ppts, SETTING_MODE, TEXT_MODE,
  1720. OBS_COMBO_TYPE_LIST,
  1721. OBS_COMBO_FORMAT_STRING);
  1722. obs_property_list_add_string(p, TEXT_MODE_ANY, SETTING_MODE_ANY);
  1723. obs_property_list_add_string(p, TEXT_MODE_WINDOW, SETTING_MODE_WINDOW);
  1724. obs_property_list_add_string(p, TEXT_MODE_HOTKEY, SETTING_MODE_HOTKEY);
  1725. obs_property_set_modified_callback(p, mode_callback);
  1726. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1727. OBS_COMBO_TYPE_LIST,
  1728. OBS_COMBO_FORMAT_STRING);
  1729. obs_property_list_add_string(p, "", "");
  1730. fill_window_list(p, INCLUDE_MINIMIZED, window_not_blacklisted);
  1731. obs_property_set_modified_callback(p, window_changed_callback);
  1732. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1733. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1734. OBS_COMBO_FORMAT_INT);
  1735. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1736. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1737. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1738. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1739. TEXT_SLI_COMPATIBILITY);
  1740. p = obs_properties_add_bool(ppts, SETTING_FORCE_SCALING,
  1741. TEXT_FORCE_SCALING);
  1742. obs_property_set_modified_callback(p, use_scaling_callback);
  1743. p = obs_properties_add_list(ppts, SETTING_SCALE_RES, TEXT_SCALE_RES,
  1744. OBS_COMBO_TYPE_EDITABLE,
  1745. OBS_COMBO_FORMAT_STRING);
  1746. for (size_t i = 0; i < NUM_DEFAULT_SCALE_VALS; i++) {
  1747. char scale_str[64];
  1748. uint32_t new_cx =
  1749. (uint32_t)((double)cx / default_scale_vals[i]) & ~2;
  1750. uint32_t new_cy =
  1751. (uint32_t)((double)cy / default_scale_vals[i]) & ~2;
  1752. sprintf(scale_str, "%" PRIu32 "x%" PRIu32, new_cx, new_cy);
  1753. obs_property_list_add_string(p, scale_str, scale_str);
  1754. }
  1755. obs_property_set_enabled(p, false);
  1756. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1757. TEXT_ALLOW_TRANSPARENCY);
  1758. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1759. TEXT_LIMIT_FRAMERATE);
  1760. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1761. obs_properties_add_bool(ppts, SETTING_ANTI_CHEAT_HOOK,
  1762. TEXT_ANTI_CHEAT_HOOK);
  1763. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1764. TEXT_CAPTURE_OVERLAYS);
  1765. p = obs_properties_add_list(ppts, SETTING_HOOK_RATE, TEXT_HOOK_RATE,
  1766. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1767. obs_property_list_add_int(p, TEXT_HOOK_RATE_SLOW, HOOK_RATE_SLOW);
  1768. obs_property_list_add_int(p, TEXT_HOOK_RATE_NORMAL, HOOK_RATE_NORMAL);
  1769. obs_property_list_add_int(p, TEXT_HOOK_RATE_FAST, HOOK_RATE_FAST);
  1770. obs_property_list_add_int(p, TEXT_HOOK_RATE_FASTEST, HOOK_RATE_FASTEST);
  1771. UNUSED_PARAMETER(data);
  1772. return ppts;
  1773. }
  1774. struct obs_source_info game_capture_info = {
  1775. .id = "game_capture",
  1776. .type = OBS_SOURCE_TYPE_INPUT,
  1777. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1778. OBS_SOURCE_DO_NOT_DUPLICATE,
  1779. .get_name = game_capture_name,
  1780. .create = game_capture_create,
  1781. .destroy = game_capture_destroy,
  1782. .get_width = game_capture_width,
  1783. .get_height = game_capture_height,
  1784. .get_defaults = game_capture_defaults,
  1785. .get_properties = game_capture_properties,
  1786. .update = game_capture_update,
  1787. .video_tick = game_capture_tick,
  1788. .video_render = game_capture_render,
  1789. .icon_type = OBS_ICON_TYPE_GAME_CAPTURE,
  1790. };