game-capture.c 52 KB

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