game-capture.c 53 KB

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