game-capture.c 55 KB

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