game-capture.c 68 KB

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