game-capture.c 54 KB

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