game-capture.c 64 KB

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