game-capture.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  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("Compatibility")
  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)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. "(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. NULL
  782. };
  783. static bool is_blacklisted_exe(const char *exe)
  784. {
  785. char cur_exe[MAX_PATH];
  786. if (!exe)
  787. return false;
  788. for (const char **vals = blacklisted_exes; *vals; vals++) {
  789. strcpy(cur_exe, *vals);
  790. strcat(cur_exe, ".exe");
  791. if (strcmpi(cur_exe, exe) == 0)
  792. return true;
  793. }
  794. return false;
  795. }
  796. static bool target_suspended(struct game_capture *gc)
  797. {
  798. return thread_is_suspended(gc->process_id, gc->thread_id);
  799. }
  800. static bool init_events(struct game_capture *gc);
  801. static bool init_hook(struct game_capture *gc)
  802. {
  803. struct dstr exe = {0};
  804. bool blacklisted_process = false;
  805. if (gc->config.mode == CAPTURE_MODE_ANY) {
  806. if (get_window_exe(&exe, gc->next_window)) {
  807. info("attempting to hook fullscreen process: %s",
  808. exe.array);
  809. }
  810. } else {
  811. if (get_window_exe(&exe, gc->next_window)) {
  812. info("attempting to hook process: %s", exe.array);
  813. }
  814. }
  815. blacklisted_process = is_blacklisted_exe(exe.array);
  816. if (blacklisted_process)
  817. info("cannot capture %s due to being blacklisted", exe.array);
  818. dstr_free(&exe);
  819. if (blacklisted_process) {
  820. return false;
  821. }
  822. if (target_suspended(gc)) {
  823. return false;
  824. }
  825. if (!open_target_process(gc)) {
  826. return false;
  827. }
  828. if (!init_keepalive(gc)) {
  829. return false;
  830. }
  831. if (!init_pipe(gc)) {
  832. return false;
  833. }
  834. if (!attempt_existing_hook(gc)) {
  835. if (!inject_hook(gc)) {
  836. return false;
  837. }
  838. }
  839. if (!init_texture_mutexes(gc)) {
  840. return false;
  841. }
  842. if (!init_hook_info(gc)) {
  843. return false;
  844. }
  845. if (!init_events(gc)) {
  846. return false;
  847. }
  848. SetEvent(gc->hook_init);
  849. gc->window = gc->next_window;
  850. gc->next_window = NULL;
  851. gc->active = true;
  852. gc->retrying = 0;
  853. return true;
  854. }
  855. static void setup_window(struct game_capture *gc, HWND window)
  856. {
  857. HANDLE hook_restart;
  858. HANDLE process;
  859. GetWindowThreadProcessId(window, &gc->process_id);
  860. if (gc->process_id) {
  861. process = open_process(PROCESS_QUERY_INFORMATION,
  862. false, gc->process_id);
  863. if (process) {
  864. gc->is_app = is_app(process);
  865. if (gc->is_app) {
  866. gc->app_sid = get_app_sid(process);
  867. }
  868. CloseHandle(process);
  869. }
  870. }
  871. /* do not wait if we're re-hooking a process */
  872. hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  873. if (hook_restart) {
  874. gc->wait_for_target_startup = false;
  875. CloseHandle(hook_restart);
  876. }
  877. /* otherwise if it's an unhooked process, always wait a bit for the
  878. * target process to start up before starting the hook process;
  879. * sometimes they have important modules to load first or other hooks
  880. * (such as steam) need a little bit of time to load. ultimately this
  881. * helps prevent crashes */
  882. if (gc->wait_for_target_startup) {
  883. gc->retry_interval = 3.0f;
  884. gc->wait_for_target_startup = false;
  885. } else {
  886. gc->next_window = window;
  887. }
  888. }
  889. static void get_fullscreen_window(struct game_capture *gc)
  890. {
  891. HWND window = GetForegroundWindow();
  892. MONITORINFO mi = {0};
  893. HMONITOR monitor;
  894. DWORD styles;
  895. RECT rect;
  896. gc->next_window = NULL;
  897. if (!window) {
  898. return;
  899. }
  900. if (!GetWindowRect(window, &rect)) {
  901. return;
  902. }
  903. /* ignore regular maximized windows */
  904. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  905. if ((styles & WS_MAXIMIZE) != 0 && (styles & WS_BORDER) != 0) {
  906. return;
  907. }
  908. monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
  909. if (!monitor) {
  910. return;
  911. }
  912. mi.cbSize = sizeof(mi);
  913. if (!GetMonitorInfo(monitor, &mi)) {
  914. return;
  915. }
  916. if (rect.left == mi.rcMonitor.left &&
  917. rect.right == mi.rcMonitor.right &&
  918. rect.bottom == mi.rcMonitor.bottom &&
  919. rect.top == mi.rcMonitor.top) {
  920. setup_window(gc, window);
  921. } else {
  922. gc->wait_for_target_startup = true;
  923. }
  924. }
  925. static void get_selected_window(struct game_capture *gc)
  926. {
  927. HWND window;
  928. if (dstr_cmpi(&gc->class, "dwm") == 0) {
  929. wchar_t class_w[512];
  930. os_utf8_to_wcs(gc->class.array, 0, class_w, 512);
  931. window = FindWindowW(class_w, NULL);
  932. } else {
  933. window = find_window(INCLUDE_MINIMIZED,
  934. gc->priority,
  935. gc->class.array,
  936. gc->title.array,
  937. gc->executable.array);
  938. }
  939. if (window) {
  940. setup_window(gc, window);
  941. } else {
  942. gc->wait_for_target_startup = true;
  943. }
  944. }
  945. static void try_hook(struct game_capture *gc)
  946. {
  947. if (gc->config.mode == CAPTURE_MODE_ANY) {
  948. get_fullscreen_window(gc);
  949. } else {
  950. get_selected_window(gc);
  951. }
  952. if (gc->next_window) {
  953. gc->thread_id = GetWindowThreadProcessId(gc->next_window,
  954. &gc->process_id);
  955. // Make sure we never try to hook ourselves (projector)
  956. if (gc->process_id == GetCurrentProcessId())
  957. return;
  958. if (!gc->thread_id && gc->process_id)
  959. return;
  960. if (!gc->process_id) {
  961. warn("error acquiring, failed to get window "
  962. "thread/process ids: %lu",
  963. GetLastError());
  964. gc->error_acquiring = true;
  965. return;
  966. }
  967. if (!init_hook(gc)) {
  968. stop_capture(gc);
  969. }
  970. } else {
  971. gc->active = false;
  972. }
  973. }
  974. static inline bool init_events(struct game_capture *gc)
  975. {
  976. if (!gc->hook_restart) {
  977. gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  978. if (!gc->hook_restart) {
  979. warn("init_events: failed to get hook_restart "
  980. "event: %lu", GetLastError());
  981. return false;
  982. }
  983. }
  984. if (!gc->hook_stop) {
  985. gc->hook_stop = open_event_gc(gc, EVENT_CAPTURE_STOP);
  986. if (!gc->hook_stop) {
  987. warn("init_events: failed to get hook_stop event: %lu",
  988. GetLastError());
  989. return false;
  990. }
  991. }
  992. if (!gc->hook_init) {
  993. gc->hook_init = open_event_gc(gc, EVENT_HOOK_INIT);
  994. if (!gc->hook_init) {
  995. warn("init_events: failed to get hook_init event: %lu",
  996. GetLastError());
  997. return false;
  998. }
  999. }
  1000. if (!gc->hook_ready) {
  1001. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1002. if (!gc->hook_ready) {
  1003. warn("init_events: failed to get hook_ready event: %lu",
  1004. GetLastError());
  1005. return false;
  1006. }
  1007. }
  1008. if (!gc->hook_exit) {
  1009. gc->hook_exit = open_event_gc(gc, EVENT_HOOK_EXIT);
  1010. if (!gc->hook_exit) {
  1011. warn("init_events: failed to get hook_exit event: %lu",
  1012. GetLastError());
  1013. return false;
  1014. }
  1015. }
  1016. return true;
  1017. }
  1018. enum capture_result {
  1019. CAPTURE_FAIL,
  1020. CAPTURE_RETRY,
  1021. CAPTURE_SUCCESS
  1022. };
  1023. static inline enum capture_result init_capture_data(struct game_capture *gc)
  1024. {
  1025. gc->cx = gc->global_hook_info->cx;
  1026. gc->cy = gc->global_hook_info->cy;
  1027. gc->pitch = gc->global_hook_info->pitch;
  1028. if (gc->data) {
  1029. UnmapViewOfFile(gc->data);
  1030. gc->data = NULL;
  1031. }
  1032. CloseHandle(gc->hook_data_map);
  1033. gc->hook_data_map = open_map_plus_id(gc, SHMEM_TEXTURE,
  1034. gc->global_hook_info->map_id);
  1035. if (!gc->hook_data_map) {
  1036. DWORD error = GetLastError();
  1037. if (error == 2) {
  1038. return CAPTURE_RETRY;
  1039. } else {
  1040. warn("init_capture_data: failed to open file "
  1041. "mapping: %lu", error);
  1042. }
  1043. return CAPTURE_FAIL;
  1044. }
  1045. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  1046. gc->global_hook_info->map_size);
  1047. if (!gc->data) {
  1048. warn("init_capture_data: failed to map data view: %lu",
  1049. GetLastError());
  1050. return CAPTURE_FAIL;
  1051. }
  1052. return CAPTURE_SUCCESS;
  1053. }
  1054. #define PIXEL_16BIT_SIZE 2
  1055. #define PIXEL_32BIT_SIZE 4
  1056. static inline uint32_t convert_5_to_8bit(uint16_t val)
  1057. {
  1058. return (uint32_t)((double)(val & 0x1F) * (255.0/31.0));
  1059. }
  1060. static inline uint32_t convert_6_to_8bit(uint16_t val)
  1061. {
  1062. return (uint32_t)((double)(val & 0x3F) * (255.0/63.0));
  1063. }
  1064. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  1065. uint8_t *data, uint32_t pitch)
  1066. {
  1067. uint8_t *input = gc->texture_buffers[cur_texture];
  1068. uint32_t gc_cx = gc->cx;
  1069. uint32_t gc_cy = gc->cy;
  1070. uint32_t gc_pitch = gc->pitch;
  1071. for (uint32_t y = 0; y < gc_cy; y++) {
  1072. uint8_t *row = input + (gc_pitch * y);
  1073. uint8_t *out = data + (pitch * y);
  1074. for (uint32_t x = 0; x < gc_cx; x += 8) {
  1075. __m128i pixels_blue, pixels_green, pixels_red;
  1076. __m128i pixels_result;
  1077. __m128i *pixels_dest;
  1078. __m128i *pixels_src = (__m128i*)(row + x * sizeof(uint16_t));
  1079. __m128i pixels = _mm_load_si128(pixels_src);
  1080. __m128i zero = _mm_setzero_si128();
  1081. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1082. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1083. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1084. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1085. __m128i green_channel_mask = _mm_set1_epi32(0x000007E0);
  1086. __m128i green_offset = _mm_set1_epi32(0x00000008);
  1087. __m128i red_channel_mask = _mm_set1_epi32(0x0000F800);
  1088. __m128i red_offset = _mm_set1_epi32(0x00000300);
  1089. pixels_blue = _mm_and_si128(pixels_low, blue_channel_mask);
  1090. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1091. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1092. pixels_green = _mm_and_si128(pixels_low, green_channel_mask);
  1093. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  1094. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1095. pixels_red = _mm_and_si128(pixels_low, red_channel_mask);
  1096. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1097. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1098. pixels_result = _mm_set1_epi32(0xFF000000);
  1099. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  1100. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  1101. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1102. pixels_dest = (__m128i*)(out + x * sizeof(uint32_t));
  1103. _mm_store_si128(pixels_dest, pixels_result);
  1104. pixels_blue = _mm_and_si128(pixels_high, blue_channel_mask);
  1105. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1106. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1107. pixels_green = _mm_and_si128(pixels_high, green_channel_mask);
  1108. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  1109. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1110. pixels_red = _mm_and_si128(pixels_high, red_channel_mask);
  1111. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1112. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1113. pixels_result = _mm_set1_epi32(0xFF000000);
  1114. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  1115. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  1116. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1117. pixels_dest = (__m128i*)(out + (x + 4) * sizeof(uint32_t));
  1118. _mm_store_si128(pixels_dest, pixels_result);
  1119. }
  1120. }
  1121. }
  1122. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  1123. uint8_t *data, uint32_t pitch)
  1124. {
  1125. uint8_t *input = gc->texture_buffers[cur_texture];
  1126. uint32_t gc_cx = gc->cx;
  1127. uint32_t gc_cy = gc->cy;
  1128. uint32_t gc_pitch = gc->pitch;
  1129. for (uint32_t y = 0; y < gc_cy; y++) {
  1130. uint8_t *row = input + (gc_pitch * y);
  1131. uint8_t *out = data + (pitch * y);
  1132. for (uint32_t x = 0; x < gc_cx; x += 8) {
  1133. __m128i pixels_blue, pixels_green, pixels_red, pixels_alpha;
  1134. __m128i pixels_result;
  1135. __m128i *pixels_dest;
  1136. __m128i *pixels_src = (__m128i*)(row + x * sizeof(uint16_t));
  1137. __m128i pixels = _mm_load_si128(pixels_src);
  1138. __m128i zero = _mm_setzero_si128();
  1139. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1140. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1141. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1142. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1143. __m128i green_channel_mask = _mm_set1_epi32(0x000003E0);
  1144. __m128i green_offset = _mm_set1_epi32(0x000000C);
  1145. __m128i red_channel_mask = _mm_set1_epi32(0x00007C00);
  1146. __m128i red_offset = _mm_set1_epi32(0x00000180);
  1147. __m128i alpha_channel_mask = _mm_set1_epi32(0x00008000);
  1148. __m128i alpha_offset = _mm_set1_epi32(0x00000001);
  1149. __m128i alpha_mask32 = _mm_set1_epi32(0xFF000000);
  1150. pixels_blue = _mm_and_si128(pixels_low, blue_channel_mask);
  1151. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1152. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1153. pixels_green = _mm_and_si128(pixels_low, green_channel_mask);
  1154. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  1155. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1156. pixels_red = _mm_and_si128(pixels_low, red_channel_mask);
  1157. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1158. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1159. pixels_alpha = _mm_and_si128(pixels_low, alpha_channel_mask);
  1160. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1161. pixels_alpha = _mm_sub_epi32(pixels_alpha, alpha_offset);
  1162. pixels_alpha = _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1163. pixels_result = pixels_red;
  1164. pixels_result = _mm_or_si128(pixels_result, pixels_alpha);
  1165. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  1166. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  1167. pixels_dest = (__m128i*)(out + x * sizeof(uint32_t));
  1168. _mm_store_si128(pixels_dest, pixels_result);
  1169. pixels_blue = _mm_and_si128(pixels_high, blue_channel_mask);
  1170. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1171. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1172. pixels_green = _mm_and_si128(pixels_high, green_channel_mask);
  1173. pixels_green = _mm_add_epi32(pixels_green, green_offset);
  1174. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1175. pixels_red = _mm_and_si128(pixels_high, red_channel_mask);
  1176. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1177. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1178. pixels_alpha = _mm_and_si128(pixels_high, alpha_channel_mask);
  1179. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1180. pixels_alpha = _mm_sub_epi32(pixels_alpha, alpha_offset);
  1181. pixels_alpha = _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1182. pixels_result = pixels_red;
  1183. pixels_result = _mm_or_si128(pixels_result, pixels_alpha);
  1184. pixels_result = _mm_or_si128(pixels_result, pixels_blue);
  1185. pixels_result = _mm_or_si128(pixels_result, pixels_green);
  1186. pixels_dest = (__m128i*)(out + (x + 4) * sizeof(uint32_t));
  1187. _mm_store_si128(pixels_dest, pixels_result);
  1188. }
  1189. }
  1190. }
  1191. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  1192. uint8_t *data, uint32_t pitch)
  1193. {
  1194. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  1195. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  1196. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  1197. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  1198. }
  1199. }
  1200. static void copy_shmem_tex(struct game_capture *gc)
  1201. {
  1202. int cur_texture = gc->shmem_data->last_tex;
  1203. HANDLE mutex = NULL;
  1204. uint32_t pitch;
  1205. int next_texture;
  1206. uint8_t *data;
  1207. if (cur_texture < 0 || cur_texture > 1)
  1208. return;
  1209. next_texture = cur_texture == 1 ? 0 : 1;
  1210. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  1211. mutex = gc->texture_mutexes[cur_texture];
  1212. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  1213. mutex = gc->texture_mutexes[next_texture];
  1214. cur_texture = next_texture;
  1215. } else {
  1216. return;
  1217. }
  1218. if (gs_texture_map(gc->texture, &data, &pitch)) {
  1219. if (gc->convert_16bit) {
  1220. copy_16bit_tex(gc, cur_texture, data, pitch);
  1221. } else if (pitch == gc->pitch) {
  1222. memcpy(data, gc->texture_buffers[cur_texture],
  1223. pitch * gc->cy);
  1224. } else {
  1225. uint8_t *input = gc->texture_buffers[cur_texture];
  1226. uint32_t best_pitch =
  1227. pitch < gc->pitch ? pitch : gc->pitch;
  1228. for (uint32_t y = 0; y < gc->cy; y++) {
  1229. uint8_t *line_in = input + gc->pitch * y;
  1230. uint8_t *line_out = data + pitch * y;
  1231. memcpy(line_out, line_in, best_pitch);
  1232. }
  1233. }
  1234. gs_texture_unmap(gc->texture);
  1235. }
  1236. ReleaseMutex(mutex);
  1237. }
  1238. static inline bool is_16bit_format(uint32_t format)
  1239. {
  1240. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  1241. format == DXGI_FORMAT_B5G6R5_UNORM;
  1242. }
  1243. static inline bool init_shmem_capture(struct game_capture *gc)
  1244. {
  1245. enum gs_color_format format;
  1246. gc->texture_buffers[0] =
  1247. (uint8_t*)gc->data + gc->shmem_data->tex1_offset;
  1248. gc->texture_buffers[1] =
  1249. (uint8_t*)gc->data + gc->shmem_data->tex2_offset;
  1250. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  1251. format = gc->convert_16bit ?
  1252. GS_BGRA : convert_format(gc->global_hook_info->format);
  1253. obs_enter_graphics();
  1254. gs_texture_destroy(gc->texture);
  1255. gc->texture = gs_texture_create(gc->cx, gc->cy, format, 1, NULL,
  1256. GS_DYNAMIC);
  1257. obs_leave_graphics();
  1258. if (!gc->texture) {
  1259. warn("init_shmem_capture: failed to create texture");
  1260. return false;
  1261. }
  1262. gc->copy_texture = copy_shmem_tex;
  1263. return true;
  1264. }
  1265. static inline bool init_shtex_capture(struct game_capture *gc)
  1266. {
  1267. obs_enter_graphics();
  1268. gs_texture_destroy(gc->texture);
  1269. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  1270. obs_leave_graphics();
  1271. if (!gc->texture) {
  1272. warn("init_shtex_capture: failed to open shared handle");
  1273. return false;
  1274. }
  1275. return true;
  1276. }
  1277. static bool start_capture(struct game_capture *gc)
  1278. {
  1279. debug("Starting capture");
  1280. if (gc->global_hook_info->type == CAPTURE_TYPE_MEMORY) {
  1281. if (!init_shmem_capture(gc)) {
  1282. return false;
  1283. }
  1284. info("memory capture successful");
  1285. } else {
  1286. if (!init_shtex_capture(gc)) {
  1287. return false;
  1288. }
  1289. info("shared texture capture successful");
  1290. }
  1291. return true;
  1292. }
  1293. static inline bool capture_valid(struct game_capture *gc)
  1294. {
  1295. if (!gc->dwm_capture && !IsWindow(gc->window))
  1296. return false;
  1297. return !object_signalled(gc->target_process);
  1298. }
  1299. static void check_foreground_window(struct game_capture *gc, float seconds)
  1300. {
  1301. // Hides the cursor if the user isn't actively in the game
  1302. gc->cursor_check_time += seconds;
  1303. if (gc->cursor_check_time >= 0.1f) {
  1304. DWORD foreground_process_id;
  1305. GetWindowThreadProcessId(GetForegroundWindow(),
  1306. &foreground_process_id);
  1307. if (gc->process_id != foreground_process_id)
  1308. gc->cursor_hidden = true;
  1309. else
  1310. gc->cursor_hidden = false;
  1311. gc->cursor_check_time = 0.0f;
  1312. }
  1313. }
  1314. static void game_capture_tick(void *data, float seconds)
  1315. {
  1316. struct game_capture *gc = data;
  1317. bool deactivate = os_atomic_set_bool(&gc->deactivate_hook, false);
  1318. bool activate_now = os_atomic_set_bool(&gc->activate_hook_now, false);
  1319. if (activate_now) {
  1320. HWND hwnd = (HWND)os_atomic_load_long(&gc->hotkey_window);
  1321. if (is_uwp_window(hwnd))
  1322. hwnd = get_uwp_actual_window(hwnd);
  1323. if (get_window_exe(&gc->executable, hwnd)) {
  1324. get_window_title(&gc->title, hwnd);
  1325. get_window_class(&gc->class, hwnd);
  1326. gc->priority = WINDOW_PRIORITY_CLASS;
  1327. gc->retry_time = 10.0f;
  1328. gc->activate_hook = true;
  1329. } else {
  1330. deactivate = false;
  1331. activate_now = false;
  1332. }
  1333. } else if (deactivate) {
  1334. gc->activate_hook = false;
  1335. }
  1336. if (!obs_source_showing(gc->source)) {
  1337. if (gc->showing) {
  1338. if (gc->active)
  1339. stop_capture(gc);
  1340. gc->showing = false;
  1341. }
  1342. return;
  1343. } else if (!gc->showing) {
  1344. gc->retry_time = 10.0f;
  1345. }
  1346. if (gc->hook_stop && object_signalled(gc->hook_stop)) {
  1347. debug("hook stop signal received");
  1348. stop_capture(gc);
  1349. }
  1350. if (gc->active && deactivate) {
  1351. stop_capture(gc);
  1352. }
  1353. if (gc->active && !gc->hook_ready && gc->process_id) {
  1354. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1355. }
  1356. if (gc->injector_process && object_signalled(gc->injector_process)) {
  1357. DWORD exit_code = 0;
  1358. GetExitCodeProcess(gc->injector_process, &exit_code);
  1359. close_handle(&gc->injector_process);
  1360. if (exit_code != 0) {
  1361. warn("inject process failed: %ld", (long)exit_code);
  1362. gc->error_acquiring = true;
  1363. } else if (!gc->capturing) {
  1364. gc->retry_interval = ERROR_RETRY_INTERVAL;
  1365. stop_capture(gc);
  1366. }
  1367. }
  1368. if (gc->hook_ready && object_signalled(gc->hook_ready)) {
  1369. debug("capture initializing!");
  1370. enum capture_result result = init_capture_data(gc);
  1371. if (result == CAPTURE_SUCCESS)
  1372. gc->capturing = start_capture(gc);
  1373. else
  1374. debug("init_capture_data failed");
  1375. if (result != CAPTURE_RETRY && !gc->capturing) {
  1376. gc->retry_interval = ERROR_RETRY_INTERVAL;
  1377. stop_capture(gc);
  1378. }
  1379. }
  1380. gc->retry_time += seconds;
  1381. if (!gc->active) {
  1382. if (!gc->error_acquiring &&
  1383. gc->retry_time > gc->retry_interval) {
  1384. if (gc->config.mode == CAPTURE_MODE_ANY ||
  1385. gc->activate_hook) {
  1386. try_hook(gc);
  1387. gc->retry_time = 0.0f;
  1388. }
  1389. }
  1390. } else {
  1391. if (!capture_valid(gc)) {
  1392. info("capture window no longer exists, "
  1393. "terminating capture");
  1394. stop_capture(gc);
  1395. } else {
  1396. if (gc->copy_texture) {
  1397. obs_enter_graphics();
  1398. gc->copy_texture(gc);
  1399. obs_leave_graphics();
  1400. }
  1401. if (gc->config.cursor) {
  1402. check_foreground_window(gc, seconds);
  1403. obs_enter_graphics();
  1404. cursor_capture(&gc->cursor_data);
  1405. obs_leave_graphics();
  1406. }
  1407. gc->fps_reset_time += seconds;
  1408. if (gc->fps_reset_time >= gc->retry_interval) {
  1409. reset_frame_interval(gc);
  1410. gc->fps_reset_time = 0.0f;
  1411. }
  1412. }
  1413. }
  1414. if (!gc->showing)
  1415. gc->showing = true;
  1416. }
  1417. static inline void game_capture_render_cursor(struct game_capture *gc)
  1418. {
  1419. POINT p = {0};
  1420. HWND window;
  1421. if (!gc->global_hook_info->base_cx ||
  1422. !gc->global_hook_info->base_cy)
  1423. return;
  1424. window = !!gc->global_hook_info->window
  1425. ? (HWND)(uintptr_t)gc->global_hook_info->window
  1426. : gc->window;
  1427. ClientToScreen(window, &p);
  1428. float x_scale = (float)gc->global_hook_info->cx /
  1429. (float)gc->global_hook_info->base_cx;
  1430. float y_scale = (float)gc->global_hook_info->cy /
  1431. (float)gc->global_hook_info->base_cy;
  1432. cursor_draw(&gc->cursor_data, -p.x, -p.y, x_scale, y_scale,
  1433. gc->global_hook_info->base_cx,
  1434. gc->global_hook_info->base_cy);
  1435. }
  1436. static void game_capture_render(void *data, gs_effect_t *effect)
  1437. {
  1438. struct game_capture *gc = data;
  1439. if (!gc->texture || !gc->active)
  1440. return;
  1441. effect = obs_get_base_effect(gc->config.allow_transparency ?
  1442. OBS_EFFECT_DEFAULT : OBS_EFFECT_OPAQUE);
  1443. while (gs_effect_loop(effect, "Draw")) {
  1444. obs_source_draw(gc->texture, 0, 0, 0, 0,
  1445. gc->global_hook_info->flip);
  1446. if (gc->config.allow_transparency && gc->config.cursor &&
  1447. !gc->cursor_hidden) {
  1448. game_capture_render_cursor(gc);
  1449. }
  1450. }
  1451. if (!gc->config.allow_transparency && gc->config.cursor &&
  1452. !gc->cursor_hidden) {
  1453. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1454. while (gs_effect_loop(effect, "Draw")) {
  1455. game_capture_render_cursor(gc);
  1456. }
  1457. }
  1458. }
  1459. static uint32_t game_capture_width(void *data)
  1460. {
  1461. struct game_capture *gc = data;
  1462. return gc->active ? gc->global_hook_info->cx : 0;
  1463. }
  1464. static uint32_t game_capture_height(void *data)
  1465. {
  1466. struct game_capture *gc = data;
  1467. return gc->active ? gc->global_hook_info->cy : 0;
  1468. }
  1469. static const char *game_capture_name(void *unused)
  1470. {
  1471. UNUSED_PARAMETER(unused);
  1472. return TEXT_GAME_CAPTURE;
  1473. }
  1474. static void game_capture_defaults(obs_data_t *settings)
  1475. {
  1476. obs_data_set_default_string(settings, SETTING_MODE, SETTING_MODE_ANY);
  1477. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1478. (int)WINDOW_PRIORITY_EXE);
  1479. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1480. obs_data_set_default_bool(settings, SETTING_FORCE_SCALING, false);
  1481. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1482. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1483. obs_data_set_default_string(settings, SETTING_SCALE_RES, "0x0");
  1484. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1485. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1486. obs_data_set_default_bool(settings, SETTING_ANTI_CHEAT_HOOK, true);
  1487. }
  1488. static bool mode_callback(obs_properties_t *ppts,
  1489. obs_property_t *p, obs_data_t *settings)
  1490. {
  1491. bool capture_window;
  1492. if (using_older_non_mode_format(settings)) {
  1493. capture_window = !obs_data_get_bool(settings,
  1494. SETTING_ANY_FULLSCREEN);
  1495. } else {
  1496. const char *mode = obs_data_get_string(settings, SETTING_MODE);
  1497. capture_window = strcmp(mode, SETTING_MODE_WINDOW) == 0;
  1498. }
  1499. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1500. obs_property_set_visible(p, capture_window);
  1501. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1502. obs_property_set_visible(p, capture_window);
  1503. return true;
  1504. }
  1505. static bool use_scaling_callback(obs_properties_t *ppts, obs_property_t *p,
  1506. obs_data_t *settings)
  1507. {
  1508. bool use_scale = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  1509. p = obs_properties_get(ppts, SETTING_SCALE_RES);
  1510. obs_property_set_enabled(p, use_scale);
  1511. return true;
  1512. }
  1513. static void insert_preserved_val(obs_property_t *p, const char *val)
  1514. {
  1515. char *class = NULL;
  1516. char *title = NULL;
  1517. char *executable = NULL;
  1518. struct dstr desc = {0};
  1519. build_window_strings(val, &class, &title, &executable);
  1520. dstr_printf(&desc, "[%s]: %s", executable, title);
  1521. obs_property_list_insert_string(p, 1, desc.array, val);
  1522. obs_property_list_item_disable(p, 1, true);
  1523. dstr_free(&desc);
  1524. bfree(class);
  1525. bfree(title);
  1526. bfree(executable);
  1527. }
  1528. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1529. obs_data_t *settings)
  1530. {
  1531. const char *cur_val;
  1532. bool match = false;
  1533. size_t i = 0;
  1534. cur_val = obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  1535. if (!cur_val) {
  1536. return false;
  1537. }
  1538. for (;;) {
  1539. const char *val = obs_property_list_item_string(p, i++);
  1540. if (!val)
  1541. break;
  1542. if (strcmp(val, cur_val) == 0) {
  1543. match = true;
  1544. break;
  1545. }
  1546. }
  1547. if (cur_val && *cur_val && !match) {
  1548. insert_preserved_val(p, cur_val);
  1549. return true;
  1550. }
  1551. UNUSED_PARAMETER(ppts);
  1552. return false;
  1553. }
  1554. static const double default_scale_vals[] = {
  1555. 1.25,
  1556. 1.5,
  1557. 2.0,
  1558. 2.5,
  1559. 3.0
  1560. };
  1561. #define NUM_DEFAULT_SCALE_VALS \
  1562. (sizeof(default_scale_vals) / sizeof(default_scale_vals[0]))
  1563. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc,
  1564. LPRECT rc, LPARAM data)
  1565. {
  1566. *(HMONITOR*)data = monitor;
  1567. UNUSED_PARAMETER(hdc);
  1568. UNUSED_PARAMETER(rc);
  1569. return false;
  1570. }
  1571. static bool window_not_blacklisted(const char *title, const char *class,
  1572. const char *exe)
  1573. {
  1574. UNUSED_PARAMETER(title);
  1575. UNUSED_PARAMETER(class);
  1576. return !is_blacklisted_exe(exe);
  1577. }
  1578. static obs_properties_t *game_capture_properties(void *data)
  1579. {
  1580. HMONITOR monitor;
  1581. uint32_t cx = 1920;
  1582. uint32_t cy = 1080;
  1583. /* scaling is free form, this is mostly just to provide some common
  1584. * values */
  1585. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1586. (LPARAM)&monitor);
  1587. if (success) {
  1588. MONITORINFO mi = {0};
  1589. mi.cbSize = sizeof(mi);
  1590. if (!!GetMonitorInfo(monitor, &mi)) {
  1591. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1592. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1593. }
  1594. }
  1595. /* update from deprecated settings */
  1596. if (data) {
  1597. struct game_capture *gc = data;
  1598. obs_data_t *settings = obs_source_get_settings(gc->source);
  1599. if (using_older_non_mode_format(settings)) {
  1600. bool any = obs_data_get_bool(settings,
  1601. SETTING_ANY_FULLSCREEN);
  1602. const char *mode = any ?
  1603. SETTING_MODE_ANY : SETTING_MODE_WINDOW;
  1604. obs_data_set_string(settings, SETTING_MODE, mode);
  1605. }
  1606. obs_data_release(settings);
  1607. }
  1608. obs_properties_t *ppts = obs_properties_create();
  1609. obs_property_t *p;
  1610. p = obs_properties_add_list(ppts, SETTING_MODE, TEXT_MODE,
  1611. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1612. obs_property_list_add_string(p, TEXT_MODE_ANY, SETTING_MODE_ANY);
  1613. obs_property_list_add_string(p, TEXT_MODE_WINDOW, SETTING_MODE_WINDOW);
  1614. obs_property_list_add_string(p, TEXT_MODE_HOTKEY, SETTING_MODE_HOTKEY);
  1615. obs_property_set_modified_callback(p, mode_callback);
  1616. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1617. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1618. obs_property_list_add_string(p, "", "");
  1619. fill_window_list(p, INCLUDE_MINIMIZED, window_not_blacklisted);
  1620. obs_property_set_modified_callback(p, window_changed_callback);
  1621. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1622. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1623. OBS_COMBO_FORMAT_INT);
  1624. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1625. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1626. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1627. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1628. TEXT_SLI_COMPATIBILITY);
  1629. p = obs_properties_add_bool(ppts, SETTING_FORCE_SCALING,
  1630. TEXT_FORCE_SCALING);
  1631. obs_property_set_modified_callback(p, use_scaling_callback);
  1632. p = obs_properties_add_list(ppts, SETTING_SCALE_RES, TEXT_SCALE_RES,
  1633. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  1634. for (size_t i = 0; i < NUM_DEFAULT_SCALE_VALS; i++) {
  1635. char scale_str[64];
  1636. uint32_t new_cx =
  1637. (uint32_t)((double)cx / default_scale_vals[i]) & ~2;
  1638. uint32_t new_cy =
  1639. (uint32_t)((double)cy / default_scale_vals[i]) & ~2;
  1640. sprintf(scale_str, "%"PRIu32"x%"PRIu32, new_cx, new_cy);
  1641. obs_property_list_add_string(p, scale_str, scale_str);
  1642. }
  1643. obs_property_set_enabled(p, false);
  1644. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1645. TEXT_ALLOW_TRANSPARENCY);
  1646. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1647. TEXT_LIMIT_FRAMERATE);
  1648. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1649. obs_properties_add_bool(ppts, SETTING_ANTI_CHEAT_HOOK,
  1650. TEXT_ANTI_CHEAT_HOOK);
  1651. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1652. TEXT_CAPTURE_OVERLAYS);
  1653. UNUSED_PARAMETER(data);
  1654. return ppts;
  1655. }
  1656. struct obs_source_info game_capture_info = {
  1657. .id = "game_capture",
  1658. .type = OBS_SOURCE_TYPE_INPUT,
  1659. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1660. OBS_SOURCE_DO_NOT_DUPLICATE,
  1661. .get_name = game_capture_name,
  1662. .create = game_capture_create,
  1663. .destroy = game_capture_destroy,
  1664. .get_width = game_capture_width,
  1665. .get_height = game_capture_height,
  1666. .get_defaults = game_capture_defaults,
  1667. .get_properties = game_capture_properties,
  1668. .update = game_capture_update,
  1669. .video_tick = game_capture_tick,
  1670. .video_render = game_capture_render
  1671. };