game-capture.c 55 KB

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