game-capture.c 53 KB

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