game-capture.c 52 KB

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