game-capture.c 65 KB

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