game-capture.c 53 KB

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