game-capture.c 54 KB

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