game-capture.c 50 KB

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