game-capture.c 55 KB

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