game-capture.c 44 KB

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