game-capture.c 49 KB

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