game-capture.c 57 KB

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