-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
Copy pathgame-capture.c
2339 lines (1947 loc) · 67.8 KB
/
game-capture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <inttypes.h>
#include <obs-module.h>
#include <obs-hotkey.h>
#include <util/dstr.h>
#include <util/platform.h>
#include <util/threading.h>
#include <util/windows/window-helpers.h>
#include <windows.h>
#include <dxgi.h>
#include <util/sse-intrin.h>
#include <util/util_uint64.h>
#include <ipc-util/pipe.h>
#include <util/windows/obfuscate.h>
#include "inject-library.h"
#include "compat-helpers.h"
#include "graphics-hook-info.h"
#include "graphics-hook-ver.h"
#include "cursor-capture.h"
#include "app-helpers.h"
#include "audio-helpers.h"
#include "nt-stuff.h"
#define do_log(level, format, ...) \
blog(level, "[game-capture: '%s'] " format, obs_source_get_name(gc->source), ##__VA_ARGS__)
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
#define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
#define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
/* clang-format off */
#define SETTING_MODE "capture_mode"
#define SETTING_CAPTURE_WINDOW "window"
#define SETTING_WINDOW_PRIORITY "priority"
#define SETTING_COMPATIBILITY "sli_compatibility"
#define SETTING_CURSOR "capture_cursor"
#define SETTING_TRANSPARENCY "allow_transparency"
#define SETTING_PREMULTIPLIED_ALPHA "premultiplied_alpha"
#define SETTING_LIMIT_FRAMERATE "limit_framerate"
#define SETTING_CAPTURE_OVERLAYS "capture_overlays"
#define SETTING_ANTI_CHEAT_HOOK "anti_cheat_hook"
#define SETTING_HOOK_RATE "hook_rate"
#define SETTING_RGBA10A2_SPACE "rgb10a2_space"
#define SETTINGS_COMPAT_INFO "compat_info"
/* deprecated */
#define SETTING_ANY_FULLSCREEN "capture_any_fullscreen"
#define SETTING_MODE_ANY "any_fullscreen"
#define SETTING_MODE_WINDOW "window"
#define SETTING_MODE_HOTKEY "hotkey"
#define HOTKEY_START "hotkey_start"
#define HOTKEY_STOP "hotkey_stop"
#define TEXT_MODE obs_module_text("Mode")
#define TEXT_GAME_CAPTURE obs_module_text("GameCapture")
#define TEXT_ANY_FULLSCREEN obs_module_text("GameCapture.AnyFullscreen")
#define TEXT_SLI_COMPATIBILITY obs_module_text("SLIFix")
#define TEXT_ALLOW_TRANSPARENCY obs_module_text("AllowTransparency")
#define TEXT_PREMULTIPLIED_ALPHA obs_module_text("PremultipliedAlpha")
#define TEXT_WINDOW obs_module_text("WindowCapture.Window")
#define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
#define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
#define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
#define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
#define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
#define TEXT_LIMIT_FRAMERATE obs_module_text("GameCapture.LimitFramerate")
#define TEXT_CAPTURE_OVERLAYS obs_module_text("GameCapture.CaptureOverlays")
#define TEXT_ANTI_CHEAT_HOOK obs_module_text("GameCapture.AntiCheatHook")
#define TEXT_HOOK_RATE obs_module_text("GameCapture.HookRate")
#define TEXT_HOOK_RATE_SLOW obs_module_text("GameCapture.HookRate.Slow")
#define TEXT_HOOK_RATE_NORMAL obs_module_text("GameCapture.HookRate.Normal")
#define TEXT_HOOK_RATE_FAST obs_module_text("GameCapture.HookRate.Fast")
#define TEXT_HOOK_RATE_FASTEST obs_module_text("GameCapture.HookRate.Fastest")
#define TEXT_RGBA10A2_SPACE obs_module_text("GameCapture.Rgb10a2Space")
#define TEXT_RGBA10A2_SPACE_SRGB obs_module_text("GameCapture.Rgb10a2Space.Srgb")
#define TEXT_RGBA10A2_SPACE_2100PQ obs_module_text("GameCapture.Rgb10a2Space.2100PQ")
#define TEXT_MODE_ANY TEXT_ANY_FULLSCREEN
#define TEXT_MODE_WINDOW obs_module_text("GameCapture.CaptureWindow")
#define TEXT_MODE_HOTKEY obs_module_text("GameCapture.UseHotkey")
#define TEXT_HOTKEY_START obs_module_text("GameCapture.HotkeyStart")
#define TEXT_HOTKEY_STOP obs_module_text("GameCapture.HotkeyStop")
/* clang-format on */
#define DEFAULT_RETRY_INTERVAL 2.0f
#define ERROR_RETRY_INTERVAL 4.0f
enum capture_mode { CAPTURE_MODE_ANY, CAPTURE_MODE_WINDOW, CAPTURE_MODE_HOTKEY };
enum hook_rate { HOOK_RATE_SLOW, HOOK_RATE_NORMAL, HOOK_RATE_FAST, HOOK_RATE_FASTEST };
#define RGBA10A2_SPACE_SRGB "srgb"
#define RGBA10A2_SPACE_2100PQ "2100pq"
struct game_capture_config {
char *title;
char *class;
char *executable;
enum window_priority priority;
enum capture_mode mode;
bool cursor;
bool force_shmem;
bool allow_transparency;
bool premultiplied_alpha;
bool limit_framerate;
bool capture_overlays;
bool anticheat_hook;
enum hook_rate hook_rate;
bool is_10a2_2100pq;
bool capture_audio;
};
typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT);
typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetThreadDpiAwarenessContext)(VOID);
typedef DPI_AWARENESS_CONTEXT(WINAPI *PFN_GetWindowDpiAwarenessContext)(HWND);
struct game_capture {
obs_source_t *source;
obs_source_t *audio_source;
struct cursor_data cursor_data;
HANDLE injector_process;
uint32_t cx;
uint32_t cy;
uint32_t pitch;
DWORD process_id;
DWORD thread_id;
HWND next_window;
HWND window;
float retry_time;
float fps_reset_time;
float retry_interval;
struct dstr title;
struct dstr class;
struct dstr executable;
enum window_priority priority;
obs_hotkey_pair_id hotkey_pair;
volatile long hotkey_window;
volatile bool deactivate_hook;
volatile bool activate_hook_now;
bool wait_for_target_startup;
bool showing;
bool active;
bool capturing;
bool activate_hook;
bool process_is_64bit;
bool error_acquiring;
bool dwm_capture;
bool initial_config;
bool convert_16bit;
bool is_app;
bool cursor_hidden;
struct game_capture_config config;
ipc_pipe_server_t pipe;
gs_texture_t *texture;
gs_texture_t *extra_texture;
gs_texrender_t *extra_texrender;
bool is_10a2_2100pq;
bool linear_sample;
struct hook_info *global_hook_info;
HANDLE keepalive_mutex;
HANDLE hook_init;
HANDLE hook_restart;
HANDLE hook_stop;
HANDLE hook_ready;
HANDLE hook_exit;
HANDLE hook_data_map;
HANDLE global_hook_info_map;
HANDLE target_process;
HANDLE texture_mutexes[2];
wchar_t *app_sid;
int retrying;
float cursor_check_time;
union {
struct {
struct shmem_data *shmem_data;
uint8_t *texture_buffers[2];
};
struct shtex_data *shtex_data;
void *data;
};
void (*copy_texture)(struct game_capture *);
PFN_SetThreadDpiAwarenessContext set_thread_dpi_awareness_context;
PFN_GetThreadDpiAwarenessContext get_thread_dpi_awareness_context;
PFN_GetWindowDpiAwarenessContext get_window_dpi_awareness_context;
};
struct graphics_offsets offsets32 = {0};
struct graphics_offsets offsets64 = {0};
static inline bool use_anticheat(struct game_capture *gc)
{
return gc->config.anticheat_hook && !gc->is_app;
}
static inline HANDLE open_mutex_plus_id(struct game_capture *gc, const wchar_t *name, DWORD id)
{
wchar_t new_name[64];
_snwprintf(new_name, 64, L"%s%lu", name, id);
return gc->is_app ? open_app_mutex(gc->app_sid, new_name) : open_mutex(new_name);
}
static inline HANDLE open_mutex_gc(struct game_capture *gc, const wchar_t *name)
{
return open_mutex_plus_id(gc, name, gc->process_id);
}
static inline HANDLE open_event_plus_id(struct game_capture *gc, const wchar_t *name, DWORD id)
{
wchar_t new_name[64];
_snwprintf(new_name, 64, L"%s%lu", name, id);
return gc->is_app ? open_app_event(gc->app_sid, new_name) : open_event(new_name);
}
static inline HANDLE open_event_gc(struct game_capture *gc, const wchar_t *name)
{
return open_event_plus_id(gc, name, gc->process_id);
}
static inline HANDLE open_map_plus_id(struct game_capture *gc, const wchar_t *name, DWORD id)
{
wchar_t new_name[64];
swprintf(new_name, 64, L"%s%lu", name, id);
debug("map id: %S", new_name);
return gc->is_app ? open_app_map(gc->app_sid, new_name) : OpenFileMappingW(GC_MAPPING_FLAGS, false, new_name);
}
static inline HANDLE open_hook_info(struct game_capture *gc)
{
return open_map_plus_id(gc, SHMEM_HOOK_INFO, gc->process_id);
}
static inline enum gs_color_format convert_format(uint32_t format)
{
switch (format) {
case DXGI_FORMAT_R8G8B8A8_UNORM:
return GS_RGBA;
case DXGI_FORMAT_B8G8R8X8_UNORM:
return GS_BGRX;
case DXGI_FORMAT_B8G8R8A8_UNORM:
return GS_BGRA;
case DXGI_FORMAT_R10G10B10A2_UNORM:
return GS_R10G10B10A2;
case DXGI_FORMAT_R16G16B16A16_UNORM:
return GS_RGBA16;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
return GS_RGBA16F;
case DXGI_FORMAT_R32G32B32A32_FLOAT:
return GS_RGBA32F;
}
return GS_UNKNOWN;
}
static void close_handle(HANDLE *p_handle)
{
HANDLE handle = *p_handle;
if (handle) {
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
*p_handle = NULL;
}
}
static inline HMODULE kernel32(void)
{
static HMODULE kernel32_handle = NULL;
if (!kernel32_handle)
kernel32_handle = GetModuleHandleW(L"kernel32");
return kernel32_handle;
}
static inline HANDLE open_process(DWORD desired_access, bool inherit_handle, DWORD process_id)
{
typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
static PFN_OpenProcess open_process_proc = NULL;
if (!open_process_proc)
open_process_proc =
(PFN_OpenProcess)ms_get_obfuscated_func(kernel32(), "NuagUykjcxr", 0x1B694B59451ULL);
return open_process_proc(desired_access, inherit_handle, process_id);
}
static inline float hook_rate_to_float(enum hook_rate rate)
{
switch (rate) {
case HOOK_RATE_SLOW:
return 2.0f;
case HOOK_RATE_FAST:
return 0.5f;
case HOOK_RATE_FASTEST:
return 0.1f;
case HOOK_RATE_NORMAL:
/* FALLTHROUGH */
default:
return 1.0f;
}
}
static void stop_capture(struct game_capture *gc)
{
ipc_pipe_server_free(&gc->pipe);
if (gc->hook_stop) {
SetEvent(gc->hook_stop);
}
if (gc->global_hook_info) {
UnmapViewOfFile(gc->global_hook_info);
gc->global_hook_info = NULL;
}
if (gc->data) {
UnmapViewOfFile(gc->data);
gc->data = NULL;
}
if (gc->app_sid) {
LocalFree(gc->app_sid);
gc->app_sid = NULL;
}
close_handle(&gc->hook_restart);
close_handle(&gc->hook_stop);
close_handle(&gc->hook_ready);
close_handle(&gc->hook_exit);
close_handle(&gc->hook_init);
close_handle(&gc->hook_data_map);
close_handle(&gc->keepalive_mutex);
close_handle(&gc->global_hook_info_map);
close_handle(&gc->target_process);
close_handle(&gc->texture_mutexes[0]);
close_handle(&gc->texture_mutexes[1]);
obs_enter_graphics();
gs_texrender_destroy(gc->extra_texrender);
gc->extra_texrender = NULL;
gs_texture_destroy(gc->extra_texture);
gc->extra_texture = NULL;
gs_texture_destroy(gc->texture);
gc->texture = NULL;
obs_leave_graphics();
if (gc->active)
info("capture stopped");
// if it was previously capturing, send an unhooked signal
if (gc->capturing) {
signal_handler_t *sh = obs_source_get_signal_handler(gc->source);
calldata_t data = {0};
calldata_set_ptr(&data, "source", gc->source);
signal_handler_signal(sh, "unhooked", &data);
calldata_free(&data);
// Also update audio source to stop capturing
if (gc->audio_source)
reconfigure_audio_source(gc->audio_source, NULL);
}
gc->copy_texture = NULL;
gc->wait_for_target_startup = false;
gc->active = false;
gc->capturing = false;
if (gc->retrying)
gc->retrying--;
}
static inline void free_config(struct game_capture_config *config)
{
bfree(config->title);
bfree(config->class);
bfree(config->executable);
memset(config, 0, sizeof(*config));
}
static void game_capture_destroy(void *data)
{
struct game_capture *gc = data;
stop_capture(gc);
if (gc->audio_source)
destroy_audio_source(gc->source, &gc->audio_source);
signal_handler_t *sh = obs_source_get_signal_handler(gc->source);
signal_handler_disconnect(sh, "rename", rename_audio_source, &gc->audio_source);
if (gc->hotkey_pair)
obs_hotkey_pair_unregister(gc->hotkey_pair);
obs_enter_graphics();
cursor_data_free(&gc->cursor_data);
obs_leave_graphics();
dstr_free(&gc->title);
dstr_free(&gc->class);
dstr_free(&gc->executable);
free_config(&gc->config);
bfree(gc);
}
static inline bool using_older_non_mode_format(obs_data_t *settings)
{
return obs_data_has_user_value(settings, SETTING_ANY_FULLSCREEN) &&
!obs_data_has_user_value(settings, SETTING_MODE);
}
static inline void get_config(struct game_capture_config *cfg, obs_data_t *settings, const char *window)
{
const char *mode_str = NULL;
ms_build_window_strings(window, &cfg->class, &cfg->title, &cfg->executable);
if (using_older_non_mode_format(settings)) {
bool any = obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
mode_str = any ? SETTING_MODE_ANY : SETTING_MODE_WINDOW;
} else {
mode_str = obs_data_get_string(settings, SETTING_MODE);
}
if (mode_str && strcmp(mode_str, SETTING_MODE_WINDOW) == 0)
cfg->mode = CAPTURE_MODE_WINDOW;
else if (mode_str && strcmp(mode_str, SETTING_MODE_HOTKEY) == 0)
cfg->mode = CAPTURE_MODE_HOTKEY;
else
cfg->mode = CAPTURE_MODE_ANY;
cfg->priority = (enum window_priority)obs_data_get_int(settings, SETTING_WINDOW_PRIORITY);
cfg->force_shmem = obs_data_get_bool(settings, SETTING_COMPATIBILITY);
cfg->cursor = obs_data_get_bool(settings, SETTING_CURSOR);
cfg->allow_transparency = obs_data_get_bool(settings, SETTING_TRANSPARENCY);
cfg->premultiplied_alpha = obs_data_get_bool(settings, SETTING_PREMULTIPLIED_ALPHA);
cfg->limit_framerate = obs_data_get_bool(settings, SETTING_LIMIT_FRAMERATE);
cfg->capture_overlays = obs_data_get_bool(settings, SETTING_CAPTURE_OVERLAYS);
cfg->anticheat_hook = obs_data_get_bool(settings, SETTING_ANTI_CHEAT_HOOK);
cfg->hook_rate = (enum hook_rate)obs_data_get_int(settings, SETTING_HOOK_RATE);
cfg->is_10a2_2100pq = strcmp(obs_data_get_string(settings, SETTING_RGBA10A2_SPACE), "2100pq") == 0;
cfg->capture_audio = obs_data_get_bool(settings, SETTING_CAPTURE_AUDIO);
}
static inline int s_cmp(const char *str1, const char *str2)
{
if (!str1 || !str2)
return -1;
return strcmp(str1, str2);
}
static inline bool capture_needs_reset(struct game_capture_config *cfg1, struct game_capture_config *cfg2)
{
if (cfg1->mode != cfg2->mode) {
return true;
} else if (cfg1->mode == CAPTURE_MODE_WINDOW &&
(s_cmp(cfg1->class, cfg2->class) != 0 || s_cmp(cfg1->title, cfg2->title) != 0 ||
s_cmp(cfg1->executable, cfg2->executable) != 0 || cfg1->priority != cfg2->priority)) {
return true;
} else if (cfg1->force_shmem != cfg2->force_shmem) {
return true;
} else if (cfg1->limit_framerate != cfg2->limit_framerate) {
return true;
} else if (cfg1->capture_overlays != cfg2->capture_overlays) {
return true;
}
return false;
}
static bool hotkey_start(void *data, obs_hotkey_pair_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
struct game_capture *gc = data;
if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
info("Activate hotkey pressed");
os_atomic_set_long(&gc->hotkey_window, (long)(uintptr_t)GetForegroundWindow());
os_atomic_set_bool(&gc->deactivate_hook, true);
os_atomic_set_bool(&gc->activate_hook_now, true);
}
return true;
}
static bool hotkey_stop(void *data, obs_hotkey_pair_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
struct game_capture *gc = data;
if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
info("Deactivate hotkey pressed");
os_atomic_set_bool(&gc->deactivate_hook, true);
}
return true;
}
static void game_capture_get_hooked(void *data, calldata_t *cd)
{
struct game_capture *gc = data;
if (!gc)
return;
calldata_set_bool(cd, "hooked", gc->capturing);
if (gc->capturing) {
calldata_set_string(cd, "title", gc->title.array);
calldata_set_string(cd, "class", gc->class.array);
calldata_set_string(cd, "executable", gc->executable.array);
} else {
calldata_set_string(cd, "title", "");
calldata_set_string(cd, "class", "");
calldata_set_string(cd, "executable", "");
}
}
static void game_capture_update(void *data, obs_data_t *settings)
{
struct game_capture *gc = data;
struct game_capture_config cfg;
bool reset_capture = false;
const char *window = obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
get_config(&cfg, settings, window);
reset_capture = capture_needs_reset(&cfg, &gc->config);
gc->error_acquiring = false;
if (cfg.mode == CAPTURE_MODE_HOTKEY && gc->config.mode != CAPTURE_MODE_HOTKEY) {
gc->activate_hook = false;
} else {
gc->activate_hook = !!window && !!*window;
}
free_config(&gc->config);
gc->config = cfg;
gc->retry_interval = DEFAULT_RETRY_INTERVAL * hook_rate_to_float(gc->config.hook_rate);
gc->wait_for_target_startup = false;
gc->is_10a2_2100pq = gc->config.is_10a2_2100pq;
dstr_free(&gc->title);
dstr_free(&gc->class);
dstr_free(&gc->executable);
if (cfg.mode == CAPTURE_MODE_WINDOW) {
dstr_copy(&gc->title, gc->config.title);
dstr_copy(&gc->class, gc->config.class);
dstr_copy(&gc->executable, gc->config.executable);
gc->priority = gc->config.priority;
}
if (!gc->initial_config) {
if (reset_capture) {
stop_capture(gc);
}
} else {
gc->initial_config = false;
}
/* Linked audio capture source stuff */
setup_audio_source(gc->source, &gc->audio_source, cfg.mode == CAPTURE_MODE_WINDOW ? window : NULL,
cfg.capture_audio, cfg.priority);
}
extern void wait_for_hook_initialization(void);
static void *game_capture_create(obs_data_t *settings, obs_source_t *source)
{
struct game_capture *gc = bzalloc(sizeof(*gc));
wait_for_hook_initialization();
gc->source = source;
gc->initial_config = true;
gc->retry_interval = DEFAULT_RETRY_INTERVAL * hook_rate_to_float(gc->config.hook_rate);
gc->hotkey_pair = obs_hotkey_pair_register_source(gc->source, HOTKEY_START, TEXT_HOTKEY_START, HOTKEY_STOP,
TEXT_HOTKEY_STOP, hotkey_start, hotkey_stop, gc, gc);
const HMODULE hModuleUser32 = GetModuleHandle(L"User32.dll");
if (hModuleUser32) {
PFN_SetThreadDpiAwarenessContext set_thread_dpi_awareness_context =
(PFN_SetThreadDpiAwarenessContext)GetProcAddress(hModuleUser32, "SetThreadDpiAwarenessContext");
PFN_GetThreadDpiAwarenessContext get_thread_dpi_awareness_context =
(PFN_GetThreadDpiAwarenessContext)GetProcAddress(hModuleUser32, "GetThreadDpiAwarenessContext");
PFN_GetWindowDpiAwarenessContext get_window_dpi_awareness_context =
(PFN_GetWindowDpiAwarenessContext)GetProcAddress(hModuleUser32, "GetWindowDpiAwarenessContext");
if (set_thread_dpi_awareness_context && get_thread_dpi_awareness_context &&
get_window_dpi_awareness_context) {
gc->set_thread_dpi_awareness_context = set_thread_dpi_awareness_context;
gc->get_thread_dpi_awareness_context = get_thread_dpi_awareness_context;
gc->get_window_dpi_awareness_context = get_window_dpi_awareness_context;
}
}
signal_handler_t *sh = obs_source_get_signal_handler(source);
signal_handler_add(sh, "void unhooked(ptr source)");
signal_handler_add(sh, "void hooked(ptr source, string title, string class, string executable)");
proc_handler_t *ph = obs_source_get_proc_handler(source);
proc_handler_add(ph,
"void get_hooked(out bool hooked, out string title, out string class, out string executable)",
game_capture_get_hooked, gc);
signal_handler_connect(sh, "rename", rename_audio_source, &gc->audio_source);
game_capture_update(gc, settings);
return gc;
}
#define STOP_BEING_BAD \
" This is most likely due to security software. Please make sure " \
"that the OBS installation folder is excluded/ignored in the " \
"settings of the security software you are using."
static bool check_file_integrity(struct game_capture *gc, const char *file, const char *name)
{
DWORD error;
HANDLE handle;
wchar_t *w_file = NULL;
if (!file || !*file) {
warn("Game capture %s not found." STOP_BEING_BAD, name);
return false;
}
if (!os_utf8_to_wcs_ptr(file, 0, &w_file)) {
warn("Could not convert file name to wide string");
return false;
}
handle = CreateFileW(w_file, GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
bfree(w_file);
if (handle != INVALID_HANDLE_VALUE) {
CloseHandle(handle);
return true;
}
error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND) {
warn("Game capture file '%s' not found." STOP_BEING_BAD, file);
} else if (error == ERROR_ACCESS_DENIED) {
warn("Game capture file '%s' could not be loaded." STOP_BEING_BAD, file);
} else {
warn("Game capture file '%s' could not be loaded: %lu." STOP_BEING_BAD, file, error);
}
return false;
}
static inline bool is_64bit_windows(void)
{
#ifdef _WIN64
return true;
#else
BOOL x86 = false;
bool success = !!IsWow64Process(GetCurrentProcess(), &x86);
return success && !!x86;
#endif
}
static inline bool is_64bit_process(HANDLE process)
{
BOOL x86 = true;
if (is_64bit_windows()) {
bool success = !!IsWow64Process(process, &x86);
if (!success) {
return false;
}
}
return !x86;
}
static inline bool open_target_process(struct game_capture *gc)
{
gc->target_process = open_process(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, false, gc->process_id);
if (!gc->target_process) {
warn("could not open process: %s", gc->config.executable);
return false;
}
gc->process_is_64bit = is_64bit_process(gc->target_process);
gc->is_app = is_app(gc->target_process);
if (gc->is_app) {
gc->app_sid = get_app_sid(gc->target_process);
}
return true;
}
static inline bool init_keepalive(struct game_capture *gc)
{
wchar_t new_name[64];
swprintf(new_name, 64, WINDOW_HOOK_KEEPALIVE L"%lu", gc->process_id);
gc->keepalive_mutex = gc->is_app ? create_app_mutex(gc->app_sid, new_name)
: CreateMutexW(NULL, false, new_name);
if (!gc->keepalive_mutex) {
warn("Failed to create keepalive mutex: %lu", GetLastError());
return false;
}
return true;
}
static inline bool init_texture_mutexes(struct game_capture *gc)
{
gc->texture_mutexes[0] = open_mutex_gc(gc, MUTEX_TEXTURE1);
gc->texture_mutexes[1] = open_mutex_gc(gc, MUTEX_TEXTURE2);
if (!gc->texture_mutexes[0] || !gc->texture_mutexes[1]) {
DWORD error = GetLastError();
if (error == 2) {
if (!gc->retrying) {
gc->retrying = 2;
info("hook not loaded yet, retrying..");
}
} else {
warn("failed to open texture mutexes: %lu", GetLastError());
}
return false;
}
return true;
}
/* if there's already a hook in the process, then signal and start */
static inline bool attempt_existing_hook(struct game_capture *gc)
{
gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
if (gc->hook_restart) {
debug("existing hook found, signaling process: %s", gc->config.executable);
SetEvent(gc->hook_restart);
return true;
}
return false;
}
static inline void reset_frame_interval(struct game_capture *gc)
{
struct obs_video_info ovi;
uint64_t interval = 0;
if (obs_get_video_info(&ovi)) {
interval = util_mul_div64(ovi.fps_den, 1000000000ULL, ovi.fps_num);
/* Always limit capture framerate to some extent. If a game
* running at 900 FPS is being captured without some sort of
* limited capture interval, it will dramatically reduce
* performance. */
if (!gc->config.limit_framerate)
interval /= 2;
}
gc->global_hook_info->frame_interval = interval;
}
static inline bool init_hook_info(struct game_capture *gc)
{
gc->global_hook_info_map = open_hook_info(gc);
if (!gc->global_hook_info_map) {
warn("init_hook_info: get_hook_info failed: %lu", GetLastError());
return false;
}
gc->global_hook_info =
MapViewOfFile(gc->global_hook_info_map, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(*gc->global_hook_info));
if (!gc->global_hook_info) {
warn("init_hook_info: failed to map data view: %lu", GetLastError());
return false;
}
if (gc->config.force_shmem) {
warn("init_hook_info: user is forcing shared memory "
"(multi-adapter compatibility mode)");
}
gc->global_hook_info->offsets = gc->process_is_64bit ? offsets64 : offsets32;
gc->global_hook_info->capture_overlay = gc->config.capture_overlays;
gc->global_hook_info->force_shmem = gc->config.force_shmem;
gc->global_hook_info->UNUSED_use_scale = false;
gc->global_hook_info->allow_srgb_alias = true;
reset_frame_interval(gc);
obs_enter_graphics();
if (!gs_shared_texture_available()) {
warn("init_hook_info: shared texture capture unavailable");
gc->global_hook_info->force_shmem = true;
}
obs_leave_graphics();
return true;
}
static void pipe_log(void *param, uint8_t *data, size_t size)
{
struct game_capture *gc = param;
if (data && size)
info("%s", data);
}
static inline bool init_pipe(struct game_capture *gc)
{
char name[64];
snprintf(name, sizeof(name), "%s%lu", PIPE_NAME, gc->process_id);
if (!ipc_pipe_server_start(&gc->pipe, name, pipe_log, gc)) {
warn("init_pipe: failed to start pipe");
return false;
}
return true;
}
static inline int inject_library(HANDLE process, const wchar_t *dll)
{
return inject_library_obf(process, dll, "D|hkqkW`kl{k\\osofj", 0xa178ef3655e5ade7, "[uawaRzbhh{tIdkj~~",
0x561478dbd824387c, "[fr}pboIe`dlN}", 0x395bfbc9833590fd, "\\`zs}gmOzhhBq",
0x12897dd89168789a, "GbfkDaezbp~X", 0x76aff7238788f7db);
}
static inline bool hook_direct(struct game_capture *gc, const char *hook_path_rel)
{
wchar_t hook_path_abs_w[MAX_PATH];
wchar_t *hook_path_rel_w;
wchar_t *path_ret;
HANDLE process;
int ret;
os_utf8_to_wcs_ptr(hook_path_rel, 0, &hook_path_rel_w);
if (!hook_path_rel_w) {
warn("hook_direct: could not convert string");
return false;
}
path_ret = _wfullpath(hook_path_abs_w, hook_path_rel_w, MAX_PATH);
bfree(hook_path_rel_w);
if (path_ret == NULL) {
warn("hook_direct: could not make absolute path");
return false;
}
process = open_process(PROCESS_ALL_ACCESS, false, gc->process_id);
if (!process) {
warn("hook_direct: could not open process: %s (%lu)", gc->config.executable, GetLastError());
return false;
}
ret = inject_library(process, hook_path_abs_w);
CloseHandle(process);
if (ret != 0) {
warn("hook_direct: inject failed: %d", ret);
return false;
}
return true;
}
static inline bool create_inject_process(struct game_capture *gc, const char *inject_path, const char *hook_dll)
{
wchar_t *command_line_w = malloc(4096 * sizeof(wchar_t));
wchar_t *inject_path_w;
wchar_t *hook_dll_w;
bool anti_cheat = use_anticheat(gc);
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
bool success = false;
os_utf8_to_wcs_ptr(inject_path, 0, &inject_path_w);
os_utf8_to_wcs_ptr(hook_dll, 0, &hook_dll_w);
si.cb = sizeof(si);
swprintf(command_line_w, 4096, L"\"%s\" \"%s\" %lu %lu", inject_path_w, hook_dll_w, (unsigned long)anti_cheat,
anti_cheat ? gc->thread_id : gc->process_id);
success = !!CreateProcessW(inject_path_w, command_line_w, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &si,
&pi);
if (success) {
CloseHandle(pi.hThread);
gc->injector_process = pi.hProcess;
} else {
warn("Failed to create inject helper process: %lu", GetLastError());
}
free(command_line_w);
bfree(inject_path_w);
bfree(hook_dll_w);
return success;
}
extern char *get_hook_path(bool b64);
static inline bool inject_hook(struct game_capture *gc)
{
bool matching_architecture;
bool success = false;
char *inject_path;
char *hook_path;
if (gc->process_is_64bit) {
inject_path = obs_module_file("inject-helper64.exe");
} else {
inject_path = obs_module_file("inject-helper32.exe");
}
hook_path = get_hook_path(gc->process_is_64bit);
if (!check_file_integrity(gc, inject_path, "inject helper")) {
goto cleanup;
}
if (!check_file_integrity(gc, hook_path, "graphics hook")) {
goto cleanup;
}
#ifdef _WIN64
matching_architecture = gc->process_is_64bit;
#else
matching_architecture = !gc->process_is_64bit;
#endif
if (matching_architecture && !use_anticheat(gc)) {
info("using direct hook");
success = hook_direct(gc, hook_path);
} else {
info("using helper (%s hook)", use_anticheat(gc) ? "compatibility" : "direct");
success = create_inject_process(gc, inject_path, hook_path);
}
cleanup:
bfree(inject_path);
bfree(hook_path);
return success;
}
static const char *blacklisted_exes[] = {
"explorer",
"steam",
"battle.net",
"galaxyclient",
"skype",
"uplay",
"origin",
"devenv",
"taskmgr",
"chrome",
"discord",
"firefox",
"systemsettings",
"applicationframehost",
"cmd",
"shellexperiencehost",
"winstore.app",
"searchui",
"lockapp",
"windowsinternal.composableshell.experiences.textinput.inputapp",
NULL,
};
static bool is_blacklisted_exe(const char *exe)
{
char cur_exe[MAX_PATH];
if (!exe)
return false;
for (const char **vals = blacklisted_exes; *vals; vals++) {
strcpy(cur_exe, *vals);
strcat(cur_exe, ".exe");
if (strcmpi(cur_exe, exe) == 0)
return true;
}
return false;
}
static bool target_suspended(struct game_capture *gc)
{
return thread_is_suspended(gc->process_id, gc->thread_id);