game-capture.c 53 KB

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