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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
|
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "vsrc_gfxcapture_winrt.hpp"
#include "vsrc_gfxcapture_shader.h"
#include <dwmapi.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <dispatcherqueue.h>
#include <windows.foundation.h>
#include <windows.graphics.capture.h>
#include <windows.graphics.capture.interop.h>
#include <windows.graphics.directx.direct3d11.h>
#if HAVE_IDIRECT3DDXGIINTERFACEACCESS
#include <windows.graphics.directx.direct3d11.interop.h>
#endif
extern "C" {
#include "libavutil/avassert.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_d3d11va.h"
#include "avfilter.h"
#include "filters.h"
#include "video.h"
#include "vsrc_gfxcapture.h"
}
#include <cinttypes>
#include <condition_variable>
#include <cwchar>
#include <memory>
#include <mutex>
#include <regex>
#include <string>
#include <thread>
#include <type_traits>
using namespace ABI::Windows::System;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Graphics::Capture;
using namespace ABI::Windows::Graphics::DirectX::Direct3D11;
using namespace Windows::Graphics::DirectX::Direct3D11;
using Microsoft::WRL::ComPtr;
using ABI::Windows::Graphics::SizeInt32;
using ABI::Windows::Foundation::TimeSpan;
using ABI::Windows::Graphics::DirectX::DirectXPixelFormat;
#define TIMESPAN_RES 10000000
#define TIMESPAN_RES64 INT64_C(10000000)
#define CAPTURE_POOL_SIZE 2
enum {
WM_WGC_THREAD_SHUTDOWN = WM_APP + 1
};
#define CCTX(ctx) static_cast<GfxCaptureContext*>(ctx)
typedef struct GfxCaptureFunctions {
hmodule_ptr_t graphicscapture_handle;
hmodule_ptr_t combase_handle;
HRESULT (WINAPI *RoInitialize)(RO_INIT_TYPE initType);
void (WINAPI *RoUninitialize)(void);
HRESULT (WINAPI *RoGetActivationFactory)(HSTRING activatableClassId, REFIID iid, void **factory);
HRESULT (WINAPI *WindowsCreateStringReference)(PCWSTR sourceString, UINT32 length, HSTRING_HEADER *hstringHeader, HSTRING *string);
hmodule_ptr_t dwmapi_handle;
HRESULT (WINAPI *DwmGetWindowAttribute)(HWND hwnd, DWORD dwAttribute, PVOID pvAttribute, DWORD cbAttribute);
hmodule_ptr_t d3d11_handle;
HRESULT (WINAPI *CreateDirect3D11DeviceFromDXGIDevice)(IDXGIDevice *dxgiDevice, IInspectable **graphicsDevice);
hmodule_ptr_t coremsg_handle;
HRESULT (WINAPI *CreateDispatcherQueueController)(DispatcherQueueOptions options, PDISPATCHERQUEUECONTROLLER *dispatcherQueueController);
hmodule_ptr_t user32_handle;
DPI_AWARENESS_CONTEXT (WINAPI *SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext);
hmodule_ptr_t kernel32_handle;
HRESULT (WINAPI *SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
hmodule_ptr_t d3dcompiler_handle;
HRESULT (WINAPI *D3DCompile)(LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, const D3D10_SHADER_MACRO *pDefines, ID3DInclude *pInclude,
LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs);
} GfxCaptureFunctions;
// This struct contains all data handled by the capture thread
struct GfxCaptureContextWgc {
ComPtr<IDispatcherQueueController> dispatcher_queue_controller;
ComPtr<IDispatcherQueue> dispatcher_queue;
ComPtr<IGraphicsCaptureItem> capture_item;
ComPtr<IDirect3DDevice> d3d_device;
ComPtr<IDirect3D11CaptureFramePool> frame_pool;
ComPtr<IGraphicsCaptureSession> capture_session;
EventRegistrationToken frame_arrived_token { 0 };
EventRegistrationToken closed_token { 0 };
std::mutex frame_arrived_mutex;
std::condition_variable frame_arrived_cond;
bool window_closed { false };
uint64_t frame_seq { 0 };
SizeInt32 cap_size { 0, 0 };
RECT client_area_offsets { 0, 0, 0, 0 };
};
struct GfxCaptureContextD3D {
ComPtr<ID3D11VertexShader> vertex_shader;
ComPtr<ID3D11PixelShader> pixel_shader;
ComPtr<ID3D11SamplerState> sampler_state;
ComPtr<ID3D11Buffer> shader_cb;
ComPtr<ID3D11DeviceContext> deferred_ctx;
};
struct GfxCaptureContextCpp {
GfxCaptureFunctions fn;
std::unique_ptr<GfxCaptureContextWgc> wgc;
std::unique_ptr<GfxCaptureContextD3D> d3d;
std::thread wgc_thread;
DWORD wgc_thread_id { 0 };
std::mutex wgc_thread_init_mutex;
std::condition_variable wgc_thread_init_cond;
volatile int wgc_thread_init_res { INT_MAX };
std::recursive_mutex wgc_thread_uninit_mutex;
volatile int wgc_thread_res { 0 };
HWND capture_hwnd { nullptr };
HMONITOR capture_hmonitor { nullptr };
AVBufferRef *device_ref { nullptr };
AVHWDeviceContext *device_ctx { nullptr };
AVD3D11VADeviceContext *device_hwctx { nullptr };
AVBufferRef *frames_ref { nullptr };
AVHWFramesContext *frames_ctx { nullptr };
AVD3D11VAFramesContext *frames_hwctx { nullptr };
int64_t first_pts { 0 };
int64_t last_pts { 0 };
};
template <typename T>
static HRESULT get_activation_factory(GfxCaptureContextCpp *ctx, PCWSTR clsid, T** factory) {
HSTRING_HEADER hsheader = { 0 };
HSTRING hs = NULL;
HRESULT hr = ctx->fn.WindowsCreateStringReference(clsid, (UINT32)wcslen(clsid), &hsheader, &hs);
if (FAILED(hr))
return hr;
return ctx->fn.RoGetActivationFactory(hs, IID_PPV_ARGS(factory));
}
#define CHECK_HR(fcall, action) \
do { \
HRESULT fhr = fcall; \
if (FAILED(fhr)) { \
av_log(avctx, AV_LOG_ERROR, #fcall " failed: 0x%08lX\n", fhr); \
action; \
} \
} while (0)
#define CHECK_HR_RET(...) CHECK_HR((__VA_ARGS__), return AVERROR_EXTERNAL)
#define CHECK_HR_FAIL(...) CHECK_HR((__VA_ARGS__), ret = AVERROR_EXTERNAL; goto fail)
#define CHECK_HR_LOG(...) CHECK_HR((__VA_ARGS__), (void)0)
/****************************************************
* Windows Graphics Capture Worker Thread *
* All wgc_* functions must run only on WGC thread! *
****************************************************/
static void wgc_frame_arrived_handler(const std::unique_ptr<GfxCaptureContextWgc> &wgctx) {
{
std::lock_guard lock(wgctx->frame_arrived_mutex);
wgctx->frame_seq += 1;
}
wgctx->frame_arrived_cond.notify_one();
}
static void wgc_closed_handler(const std::unique_ptr<GfxCaptureContextWgc> &wgctx) {
{
std::lock_guard lock(wgctx->frame_arrived_mutex);
wgctx->window_closed = true;
}
wgctx->frame_arrived_cond.notify_one();
}
static void wgc_stop_capture_session(AVFilterContext *avctx) noexcept
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
if (wgctx->closed_token.value && wgctx->capture_item) {
CHECK_HR_LOG(wgctx->capture_item->remove_Closed(wgctx->closed_token));
wgctx->closed_token.value = 0;
}
if (wgctx->frame_arrived_token.value && wgctx->frame_pool) {
CHECK_HR_LOG(wgctx->frame_pool->remove_FrameArrived(wgctx->frame_arrived_token));
wgctx->frame_arrived_token.value = 0;
}
if (wgctx->capture_session) {
ComPtr<IClosable> closable;
if (SUCCEEDED(wgctx->capture_session.As(&closable))) {
CHECK_HR_LOG(closable->Close());
} else {
av_log(avctx, AV_LOG_ERROR, "Failed to get capture session IClosable interface\n");
}
}
if (wgctx->frame_pool) {
ComPtr<IClosable> closable;
if (SUCCEEDED(wgctx->frame_pool.As(&closable))) {
CHECK_HR_LOG(closable->Close());
} else {
av_log(avctx, AV_LOG_ERROR, "Failed to get frame pool IClosable interface\n");
}
}
wgctx->capture_session.Reset();
wgctx->frame_pool.Reset();
wgctx->capture_item.Reset();
wgctx->d3d_device.Reset();
}
static int wgc_calculate_client_area(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
if (!ctx->capture_hwnd) {
wgctx->client_area_offsets.left = 0;
wgctx->client_area_offsets.top = 0;
wgctx->client_area_offsets.right = 0;
wgctx->client_area_offsets.bottom = 0;
return 0;
}
RECT client_rect = {};
RECT frame_bounds = {};
RECT window_rect = {};
if (IsIconic(ctx->capture_hwnd)) {
av_log(avctx, AV_LOG_VERBOSE, "Capture window is iconic, no client area\n");
return 0;
}
if (!GetClientRect(ctx->capture_hwnd, &client_rect)) {
av_log(avctx, AV_LOG_ERROR, "GetClientRect failed\n");
return AVERROR_EXTERNAL;
}
SetLastError(0);
if (!MapWindowPoints(ctx->capture_hwnd, nullptr, (POINT*)&client_rect, 2) && GetLastError()) {
av_log(avctx, AV_LOG_ERROR, "MapWindowPoints failed\n");
return AVERROR_EXTERNAL;
}
if (FAILED(ctx->fn.DwmGetWindowAttribute(ctx->capture_hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame_bounds, sizeof(window_rect))))
av_log(avctx, AV_LOG_DEBUG, "DwmGetWindowAttribute failed\n");
if (!GetWindowRect(ctx->capture_hwnd, &window_rect))
av_log(avctx, AV_LOG_DEBUG, "GetWindowRect failed\n");
if (wgctx->cap_size.Width == frame_bounds.right - frame_bounds.left ||
wgctx->cap_size.Height == frame_bounds.bottom - frame_bounds.top) {
av_log(avctx, AV_LOG_DEBUG, "Using window rect from DWMWA_EXTENDED_FRAME_BOUNDS\n");
} else if (wgctx->cap_size.Width == window_rect.right - window_rect.left ||
wgctx->cap_size.Height == window_rect.bottom - window_rect.top) {
av_log(avctx, AV_LOG_DEBUG, "Using window rect from GetWindowRect\n");
frame_bounds = window_rect;
} else {
if ((frame_bounds.top == frame_bounds.bottom || frame_bounds.left == frame_bounds.right) &&
(window_rect.top == window_rect.bottom || window_rect.left == window_rect.right))
{
av_log(avctx, AV_LOG_ERROR, "No valid window rect found\n");
return AVERROR_EXTERNAL;
}
av_log(avctx, AV_LOG_VERBOSE, "Failed to get valid window rect, client area may be inaccurate\n");
return 0;
}
wgctx->client_area_offsets.left = FFMAX(client_rect.left - frame_bounds.left, 0);
wgctx->client_area_offsets.top = FFMAX(client_rect.top - frame_bounds.top, 0);
wgctx->client_area_offsets.right = FFMAX(frame_bounds.right - client_rect.right, 0);
wgctx->client_area_offsets.bottom = FFMAX(frame_bounds.bottom - client_rect.bottom, 0);
av_log(avctx, AV_LOG_DEBUG, "Client area offsets: left=%ld top=%ld right=%ld bottom=%ld\n",
wgctx->client_area_offsets.left, wgctx->client_area_offsets.top,
wgctx->client_area_offsets.right, wgctx->client_area_offsets.bottom);
return 0;
}
static int wgc_setup_gfxcapture_session(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
int ret;
ComPtr<IDirect3D11CaptureFramePoolStatics2> frame_pool_statics;
ComPtr<ID3D11Device> d3d11_device = ctx->device_hwctx->device;
ComPtr<ID3D10Multithread> d3d10_multithread;
ComPtr<IDXGIDevice> dxgi_device;
ComPtr<IGraphicsCaptureSession2> session2;
ComPtr<IGraphicsCaptureSession3> session3;
ComPtr<IGraphicsCaptureSession5> session5;
DirectXPixelFormat fmt = DirectXPixelFormat::DirectXPixelFormat_B8G8R8A8UIntNormalized;
if (cctx->out_fmt != AV_PIX_FMT_BGRA)
fmt = DirectXPixelFormat::DirectXPixelFormat_R16G16B16A16Float;
CHECK_HR_RET(wgctx->capture_item->get_Size(&wgctx->cap_size));
ret = wgc_calculate_client_area(avctx);
if (ret < 0)
return ret;
CHECK_HR_RET(d3d11_device.As(&d3d10_multithread));
d3d10_multithread->SetMultithreadProtected(TRUE);
CHECK_HR_RET(d3d11_device.As(&dxgi_device));
CHECK_HR_RET(ctx->fn.CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(), &wgctx->d3d_device));
CHECK_HR_RET(get_activation_factory<IDirect3D11CaptureFramePoolStatics2>(ctx, RuntimeClass_Windows_Graphics_Capture_Direct3D11CaptureFramePool, &frame_pool_statics));
CHECK_HR_RET(frame_pool_statics->CreateFreeThreaded(wgctx->d3d_device.Get(), fmt, CAPTURE_POOL_SIZE, wgctx->cap_size, &wgctx->frame_pool));
CHECK_HR_RET(wgctx->frame_pool->CreateCaptureSession(wgctx->capture_item.Get(), &wgctx->capture_session));
if (SUCCEEDED(wgctx->capture_session.As(&session2))) {
if (FAILED(session2->put_IsCursorCaptureEnabled(cctx->capture_cursor))) {
av_log(avctx, AV_LOG_WARNING, "Failed setting cursor capture mode\n");
}
} else {
av_log(avctx, AV_LOG_WARNING, "Cursor capture unavailable\n");
}
if (SUCCEEDED(wgctx->capture_session.As(&session3))) {
// this one is weird, it can return failure but still work
if (FAILED(session3->put_IsBorderRequired(cctx->display_border))) {
av_log(avctx, AV_LOG_WARNING, "Failed setting border drawing mode\n");
}
} else {
av_log(avctx, AV_LOG_WARNING, "Disabling border drawing unavailable\n");
}
if (SUCCEEDED(wgctx->capture_session.As(&session5))) {
TimeSpan ivl = { av_rescale_q(1, av_inv_q(cctx->frame_rate), AVRational{1, TIMESPAN_RES}) };
if (FAILED(session5->put_MinUpdateInterval(ivl))) {
av_log(avctx, AV_LOG_WARNING, "Failed setting minimum update interval, framerate may be limited\n");
}
} else {
av_log(avctx, AV_LOG_WARNING, "Setting minimum update interval unavailable, framerate may be limited\n");
}
wgctx->window_closed = 0;
CHECK_HR_RET(wgctx->capture_item->add_Closed(
create_cb_handler<ITypedEventHandler<GraphicsCaptureItem*,IInspectable*>, IGraphicsCaptureItem*, IInspectable*>(
[avctx, ctx](auto, auto) {
av_log(avctx, AV_LOG_INFO, "Capture item closed\n");
wgc_closed_handler(ctx->wgc);
return S_OK;
}).Get(), &wgctx->closed_token));
CHECK_HR_RET(wgctx->frame_pool->add_FrameArrived(
create_cb_handler<ITypedEventHandler<Direct3D11CaptureFramePool*,IInspectable*>, IDirect3D11CaptureFramePool*, IInspectable*>(
[avctx, ctx](auto, auto) {
av_log(avctx, AV_LOG_TRACE, "Frame arrived\n");
wgc_frame_arrived_handler(ctx->wgc);
return S_OK;
}).Get(), &wgctx->frame_arrived_token));
return 0;
}
static int wgc_setup_gfxcapture_capture(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
HRESULT hr;
int ret;
ComPtr<IGraphicsCaptureItemInterop> capture_item_interop;
CHECK_HR_RET(get_activation_factory<IGraphicsCaptureItemInterop>(ctx, RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureItem, &capture_item_interop));
if (ctx->capture_hmonitor) {
hr = capture_item_interop->CreateForMonitor(ctx->capture_hmonitor, IID_PPV_ARGS(&wgctx->capture_item));
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup graphics capture for monitor (0x%08lX)\n", hr);
return AVERROR_EXTERNAL;
}
} else if (ctx->capture_hwnd) {
hr = capture_item_interop->CreateForWindow(ctx->capture_hwnd, IID_PPV_ARGS(&wgctx->capture_item));
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup graphics capture for window (0x%08lX)\n", hr);
return AVERROR_EXTERNAL;
}
}
ret = wgc_setup_gfxcapture_session(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup graphics capture pool\n");
return ret;
}
hr = ctx->wgc->capture_session->StartCapture();
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "Failed to start graphics capture session (0x%08lX)\n", hr);
return AVERROR_EXTERNAL;
}
return 0;
}
static int wgc_try_get_next_frame(AVFilterContext *avctx, ComPtr<IDirect3D11CaptureFrame> &capture_frame)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
ComPtr<IDirect3DSurface> capture_surface;
ComPtr<IDirect3DDxgiInterfaceAccess> dxgi_interface_access;
ComPtr<ID3D11Texture2D> frame_texture;
SizeInt32 frame_size = { 0, 0 };
CHECK_HR_RET(wgctx->frame_pool->TryGetNextFrame(&capture_frame));
if (!capture_frame)
return AVERROR(EAGAIN);
CHECK_HR_RET(capture_frame->get_ContentSize(&frame_size));
if (frame_size.Width != wgctx->cap_size.Width || frame_size.Height != wgctx->cap_size.Height) {
av_log(avctx, AV_LOG_VERBOSE, "Capture size changed to %dx%d\n", frame_size.Width, frame_size.Height);
DirectXPixelFormat fmt = DirectXPixelFormat::DirectXPixelFormat_B8G8R8A8UIntNormalized;
if (cctx->out_fmt != AV_PIX_FMT_BGRA)
fmt = DirectXPixelFormat::DirectXPixelFormat_R16G16B16A16Float;
CHECK_HR_RET(wgctx->frame_pool->Recreate(wgctx->d3d_device.Get(), fmt, CAPTURE_POOL_SIZE, frame_size));
wgctx->cap_size = frame_size;
int ret = wgc_calculate_client_area(avctx);
if (ret < 0)
return ret;
}
return 0;
}
static int wgc_setup_winrt(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
MSG msg;
// pre-create the message-queue
PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE);
DispatcherQueueOptions options = { 0 };
options.dwSize = sizeof(DispatcherQueueOptions);
options.threadType = DISPATCHERQUEUE_THREAD_TYPE::DQTYPE_THREAD_CURRENT;
options.apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE::DQTAT_COM_NONE;
CHECK_HR_RET(ctx->fn.CreateDispatcherQueueController(options, &wgctx->dispatcher_queue_controller));
CHECK_HR_RET(wgctx->dispatcher_queue_controller->get_DispatcherQueue(&wgctx->dispatcher_queue));
return 0;
}
static void wgc_thread_uninit(AVFilterContext *avctx) noexcept
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
wgc_stop_capture_session(avctx);
ctx->wgc.reset();
ctx->fn.RoUninitialize();
}
static int wgc_thread_init(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
HRESULT hr;
int ret;
ctx->wgc = std::make_unique<GfxCaptureContextWgc>();
ctx->fn.SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
hr = ctx->fn.RoInitialize(RO_INIT_MULTITHREADED);
if (FAILED(hr)) {
av_log(avctx, AV_LOG_ERROR, "Failed to initialize WinRT\n");
ctx->wgc.reset();
return AVERROR_EXTERNAL;
}
ret = wgc_setup_winrt(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup WinRT\n");
goto fail;
}
ret = wgc_setup_gfxcapture_capture(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup graphics capture\n");
goto fail;
}
return 0;
fail:
wgc_thread_uninit(avctx);
return ret;
}
static int wgc_thread_worker(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
ComPtr<IAsyncAction> async;
MSG msg;
av_log(avctx, AV_LOG_DEBUG, "Starting message loop\n");
while (BOOL res = GetMessage(&msg, NULL, 0, 0)) {
if (res == -1) {
av_log(avctx, AV_LOG_ERROR, "Failed to get message\n");
return AVERROR(EIO);
}
if (!msg.hwnd && msg.message == WM_WGC_THREAD_SHUTDOWN) {
av_log(avctx, AV_LOG_DEBUG, "Initializing WGC thread shutdown\n");
wgc_stop_capture_session(avctx);
if (FAILED(wgctx->dispatcher_queue_controller->ShutdownQueueAsync(&async))) {
av_log(avctx, AV_LOG_ERROR, "Failed to shutdown dispatcher queue\n");
return AVERROR_EXTERNAL;
}
async->put_Completed(create_cb_handler<IAsyncActionCompletedHandler, IAsyncAction*, AsyncStatus>(
[avctx, ctx](auto, auto status) {
PostThreadMessage(ctx->wgc_thread_id, WM_QUIT, 0, 0);
av_log(avctx, AV_LOG_DEBUG, "WGC thread async shutdown completed: %d\n", (int)status);
return S_OK;
}).Get());
continue;
}
av_log(avctx, AV_LOG_TRACE, "Got message: %u\n", msg.message);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!async) {
av_log(avctx, AV_LOG_ERROR, "WGC Thread message loop ended without proper shutdown\n");
return AVERROR_EXTERNAL;
}
av_log(avctx, AV_LOG_DEBUG, "Message loop ended\n");
return msg.wParam;
}
static void wgc_thread_entry(AVFilterContext *avctx) noexcept
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
{
static const wchar_t name_prefix[] = L"wgc_winrt@0x";
wchar_t thread_name[FF_ARRAY_ELEMS(name_prefix) + sizeof(void*) * 2] = { 0 };
swprintf(thread_name, FF_ARRAY_ELEMS(thread_name), L"%s%" PRIxPTR, name_prefix, (uintptr_t)avctx);
ctx->fn.SetThreadDescription(GetCurrentThread(), thread_name);
std::lock_guard init_lock(ctx->wgc_thread_init_mutex);
ctx->wgc_thread_id = GetCurrentThreadId();
try {
ctx->wgc_thread_init_res = wgc_thread_init(avctx);
} catch (const std::bad_alloc &) {
ctx->wgc_thread_init_res = AVERROR(ENOMEM);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception in WGC thread init: %s\n", e.what());
ctx->wgc_thread_init_res = AVERROR_BUG;
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "Unhandled exception in WGC thread init\n");
ctx->wgc_thread_init_res = AVERROR_BUG;
}
ctx->wgc_thread_init_cond.notify_all();
if (ctx->wgc_thread_init_res < 0) {
ctx->wgc_thread_res = ctx->wgc_thread_init_res;
return;
}
}
int ret;
try {
ret = wgc_thread_worker(avctx);
} catch (const std::bad_alloc &) {
ret = AVERROR(ENOMEM);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception in WGC thread worker: %s\n", e.what());
ret = AVERROR_BUG;
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "Unhandled exception in WGC thread worker\n");
ret = AVERROR_BUG;
}
std::lock_guard uninit_lock(ctx->wgc_thread_uninit_mutex);
wgc_thread_uninit(avctx);
ctx->wgc_thread_res = ret;
}
/***********************************
* WGC Thread Management Functions *
***********************************/
static int stop_wgc_thread(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
int ret = 0;
if (ctx->wgc_thread.joinable()) {
if (ctx->wgc_thread_id && !PostThreadMessage(ctx->wgc_thread_id, WM_WGC_THREAD_SHUTDOWN, 0, 0))
av_log(avctx, AV_LOG_ERROR, "Failed to post shutdown message to WGC thread\n");
ctx->wgc_thread.join();
ret = ctx->wgc_thread_res;
ctx->wgc_thread_id = 0;
}
return ret;
}
static int start_wgc_thread(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
if (ctx->wgc_thread.joinable() || ctx->wgc_thread_id) {
av_log(avctx, AV_LOG_ERROR, "Double-creation of WGC thread\n");
return AVERROR_BUG;
}
std::unique_lock wgc_lock(ctx->wgc_thread_init_mutex);
ctx->wgc_thread_init_res = INT_MAX;
try {
ctx->wgc_thread = std::thread(wgc_thread_entry, avctx);
} catch (const std::system_error &e) {
av_log(avctx, AV_LOG_ERROR, "Failed to create WGC thread: %s\n", e.what());
return AVERROR_EXTERNAL;
}
if (!ctx->wgc_thread_init_cond.wait_for(wgc_lock, std::chrono::seconds(1), [&]() {
return ctx->wgc_thread_init_res != INT_MAX;
})) {
av_log(avctx, AV_LOG_ERROR, "WGC thread init timed out\n");
return AVERROR(ETIMEDOUT);
}
return ctx->wgc_thread_init_res;
}
template <typename F>
static int run_on_wgc_thread(AVFilterContext *avctx, F &&cb)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
std::lock_guard uninit_lock(ctx->wgc_thread_uninit_mutex);
if (!wgctx) {
av_log(avctx, AV_LOG_ERROR, "WGC thread not initialized\n");
return AVERROR(ENOSYS);
}
struct CBData {
std::mutex mutex;
std::condition_variable cond;
bool done { false };
bool cancel { false };
int ret { AVERROR_BUG };
};
auto cbdata = std::make_shared<CBData>();
boolean res = 0;
CHECK_HR_RET(wgctx->dispatcher_queue->TryEnqueue(
create_cb_handler<IDispatcherQueueHandler>(
[cb = std::forward<F>(cb), cbdata]() {
{
std::lock_guard lock(cbdata->mutex);
if (cbdata->cancel)
return S_OK;
try {
cbdata->ret = cb();
} catch (const std::bad_alloc &) {
cbdata->ret = AVERROR(ENOMEM);
} catch (...) {
cbdata->ret = AVERROR_BUG;
}
cbdata->done = true;
}
cbdata->cond.notify_one();
return S_OK;
}).Get(), &res));
if (!res) {
av_log(avctx, AV_LOG_ERROR, "Failed to enqueue WGC thread callback\n");
return AVERROR_EXTERNAL;
}
std::unique_lock cblock(cbdata->mutex);
if (!cbdata->cond.wait_for(cblock, std::chrono::seconds(1), [&]() { return cbdata->done; })) {
cbdata->cancel = true;
av_log(avctx, AV_LOG_ERROR, "WGC thread callback timed out\n");
return AVERROR(ETIMEDOUT);
}
return cbdata->ret;
}
/*******************************
* Standard AVFilter functions *
*******************************/
static int build_regex(AVFilterContext *avctx, const char *pattern, std::regex *out)
{
if (!pattern)
return 0;
std::string pat(pattern);
auto flags = std::regex::ECMAScript | std::regex::optimize;
if (pat.rfind("(?i)", 0) == 0 || pat.rfind("(?I)", 0) == 0) {
pat.erase(0, 4);
flags |= std::regex::icase;
} else if(pat.rfind("(?c)", 0) == 0 || pat.rfind("(?C)", 0) == 0) {
pat.erase(0, 4);
}
try {
*out = std::regex(pat, flags);
} catch (const std::regex_error &e) {
av_log(avctx, AV_LOG_ERROR, "Failed to compile regex '%s': %s\n", pat.c_str(), e.what());
return AVERROR(EINVAL);
}
av_log(avctx, AV_LOG_DEBUG, "Built regex: %s\n", pattern);
return 0;
}
static int wstring_to_utf8(const wchar_t *in, std::string *out)
{
int utf8size = WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
if (utf8size <= 0)
return AVERROR(EINVAL);
// over-writing std::string by one is valid in C++17 according to 27.4.3.6 if and only if it's overwritten with 0
out->resize(utf8size - 1);
if (WideCharToMultiByte(CP_UTF8, 0, in, -1, out->data(), utf8size, nullptr, nullptr) != utf8size)
return AVERROR_EXTERNAL;
return 0;
}
static int get_window_exe_name(HWND hwnd, std::string *out)
{
out->clear();
DWORD pid = 0;
if (!GetWindowThreadProcessId(hwnd, &pid))
return AVERROR(ENOENT);
handle_ptr_t proc(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid));
if (!proc)
return AVERROR(EACCES);
std::wstring image_name;
DWORD image_name_size = 512;
for (;;) {
DWORD len = image_name_size;
image_name.resize(len);
if (QueryFullProcessImageNameW(proc.get(), 0, image_name.data(), &len)) {
image_name.resize(len);
break;
}
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
image_name_size *= 2;
continue;
}
return AVERROR_EXTERNAL;
}
if (image_name.empty())
return AVERROR_EXTERNAL;
const wchar_t *base = image_name.c_str();
size_t pos = image_name.find_last_of(L"\\/");
if (pos != std::string::npos)
base += pos + 1;
return wstring_to_utf8(base, out);
}
static int find_capture_source(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
int cur_idx = 0;
ctx->capture_hwnd = NULL;
ctx->capture_hmonitor = NULL;
if (cctx->user_hmonitor) {
ctx->capture_hmonitor = (HMONITOR)(uintptr_t)cctx->user_hmonitor;
return 0;
} else if (cctx->user_hwnd) {
ctx->capture_hwnd = (HWND)(uintptr_t)cctx->user_hwnd;
return 0;
} else if (cctx->monitor_idx >= 0) {
auto cb = make_win32_callback([&](HMONITOR hmonitor, HDC, LPRECT) {
if (cur_idx++ == cctx->monitor_idx) {
av_log(avctx, AV_LOG_DEBUG, "Found capture monitor: %d\n", cctx->monitor_idx);
ctx->capture_hmonitor = hmonitor;
return FALSE;
}
return TRUE;
});
if (EnumDisplayMonitors(NULL, NULL, cb->proc, cb->lparam) || !ctx->capture_hmonitor)
return AVERROR(ENOENT);
return 0;
} else if (cctx->window_text || cctx->window_class || cctx->window_exe) {
std::regex text_regex;
if (build_regex(avctx, cctx->window_text, &text_regex) < 0)
return AVERROR(EINVAL);
std::regex class_regex;
if (build_regex(avctx, cctx->window_class, &class_regex) < 0)
return AVERROR(EINVAL);
std::regex exe_regex;
if (build_regex(avctx, cctx->window_exe, &exe_regex) < 0)
return AVERROR(EINVAL);
std::string window_text;
std::wstring window_text_w;
std::string window_class;
std::wstring window_class_w;
std::string window_exe;
auto cb = make_win32_callback([&](HWND hwnd) {
RECT r = { 0 };
if (!GetWindowRect(hwnd, &r) || r.right <= r.left || r.bottom <= r.top || !IsWindowVisible(hwnd))
return TRUE;
window_text_w.resize(GetWindowTextLengthW(hwnd) + 1);
int len = GetWindowTextW(hwnd, window_text_w.data(), (int)window_text_w.size());
if (len >= 0) {
window_text_w.resize(len);
if (wstring_to_utf8(window_text_w.c_str(), &window_text) < 0)
window_text.clear();
} else {
window_text.clear();
}
window_class_w.resize(256);
len = GetClassNameW(hwnd, window_class_w.data(), (int)window_class_w.size());
if (len >= 0) {
window_class_w.resize(len);
if (wstring_to_utf8(window_class_w.c_str(), &window_class) < 0)
window_class.clear();
} else {
window_class.clear();
}
get_window_exe_name(hwnd, &window_exe);
av_log(avctx, AV_LOG_TRACE, "Checking window: hwnd=%p text=%s class=%s exe=%s\n",
hwnd, window_text.c_str(), window_class.c_str(), window_exe.c_str());
if (cctx->window_text) {
if (window_text.empty() || !std::regex_search(window_text, text_regex))
return TRUE;
}
if (cctx->window_class) {
if (window_class.empty() || !std::regex_search(window_class, class_regex))
return TRUE;
}
if (cctx->window_exe) {
if (window_exe.empty() || !std::regex_search(window_exe, exe_regex))
return TRUE;
}
av_log(avctx, AV_LOG_VERBOSE, "Found capture window: %s (Class: %s, Exe: %s)\n",
window_text.c_str(), window_class.c_str(), window_exe.c_str());
ctx->capture_hwnd = hwnd;
return FALSE;
});
if (EnumWindows(cb->proc, cb->lparam) || !ctx->capture_hwnd)
return AVERROR(ENOENT);
if (cctx->monitor_idx == GFX_MONITOR_IDX_WINDOW) {
ctx->capture_hmonitor = MonitorFromWindow(ctx->capture_hwnd, MONITOR_DEFAULTTONEAREST);
ctx->capture_hwnd = NULL;
if (!ctx->capture_hmonitor) {
av_log(avctx, AV_LOG_ERROR, "Failed to get monitor for capture window\n");
return AVERROR(ENOENT);
}
}
return 0;
}
av_log(avctx, AV_LOG_ERROR, "No capture source specified\n");
return AVERROR(EINVAL);
}
static av_cold void gfxcapture_uninit(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
if (!ctx)
return;
stop_wgc_thread(avctx);
ctx->d3d.reset();
av_buffer_unref(&ctx->frames_ref);
av_buffer_unref(&ctx->device_ref);
delete ctx;
cctx->ctx = nullptr;
}
template<typename T>
static av_cold void GetProcAddressTyped(const hmodule_ptr_t &hModule, LPCSTR lpProcName, T *out) {
*out = reinterpret_cast<T>(GetProcAddress(hModule.get(), lpProcName));
}
static av_cold int load_functions(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
#define LOAD_DLL(handle, name) \
handle = hmodule_ptr_t(LoadLibraryExW(L##name, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); \
if (!handle) { \
av_log(avctx, AV_LOG_ERROR, "Failed opening " #name "\n"); \
return AVERROR(ENOSYS); \
}
#define LOAD_FUNC(handle, name) \
GetProcAddressTyped(handle, #name, &ctx->fn.name); \
if (!ctx->fn.name) { \
av_log(avctx, AV_LOG_ERROR, "Failed loading " #name "\n"); \
return AVERROR(ENOSYS); \
}
// this handle is not used anywhere, but letting it get auto-freed during RoUninit causes crashes
LOAD_DLL(ctx->fn.graphicscapture_handle, "graphicscapture.dll");
LOAD_DLL(ctx->fn.combase_handle, "combase.dll");
LOAD_DLL(ctx->fn.dwmapi_handle, "dwmapi.dll");
LOAD_DLL(ctx->fn.d3d11_handle, "d3d11.dll");
LOAD_DLL(ctx->fn.coremsg_handle, "coremessaging.dll");
LOAD_DLL(ctx->fn.user32_handle, "user32.dll");
LOAD_DLL(ctx->fn.kernel32_handle, "kernel32.dll");
LOAD_DLL(ctx->fn.d3dcompiler_handle, "d3dcompiler_47.dll");
LOAD_FUNC(ctx->fn.combase_handle, RoInitialize);
LOAD_FUNC(ctx->fn.combase_handle, RoUninitialize);
LOAD_FUNC(ctx->fn.combase_handle, RoGetActivationFactory);
LOAD_FUNC(ctx->fn.combase_handle, WindowsCreateStringReference);
LOAD_FUNC(ctx->fn.dwmapi_handle, DwmGetWindowAttribute);
LOAD_FUNC(ctx->fn.d3d11_handle, CreateDirect3D11DeviceFromDXGIDevice);
LOAD_FUNC(ctx->fn.coremsg_handle, CreateDispatcherQueueController);
LOAD_FUNC(ctx->fn.user32_handle, SetThreadDpiAwarenessContext);
LOAD_FUNC(ctx->fn.kernel32_handle, SetThreadDescription);
LOAD_FUNC(ctx->fn.d3dcompiler_handle, D3DCompile);
#undef LOAD_FUNC
#undef LOAD_DLL
return 0;
}
static av_cold int gfxcapture_init(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
int ret = 0;
GfxCaptureContextCpp *ctx = cctx->ctx = new GfxCaptureContextCpp();
ctx->d3d = std::make_unique<GfxCaptureContextD3D>();
ret = load_functions(avctx);
if (ret < 0) {
ctx->fn.RoUninitialize = nullptr;
goto fail;
}
return 0;
fail:
gfxcapture_uninit(avctx);
return ret;
}
static int init_hwframes_ctx(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
int ret = 0;
ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
if (!ctx->frames_ref)
return AVERROR(ENOMEM);
ctx->frames_ctx = (AVHWFramesContext*)ctx->frames_ref->data;
ctx->frames_hwctx = (AVD3D11VAFramesContext*)ctx->frames_ctx->hwctx;
ctx->frames_ctx->format = AV_PIX_FMT_D3D11;
ctx->frames_ctx->width = cctx->canvas_width;
ctx->frames_ctx->height = cctx->canvas_height;
ctx->frames_ctx->sw_format = (AVPixelFormat)cctx->out_fmt;
if (avctx->extra_hw_frames > 0)
ctx->frames_ctx->initial_pool_size = 8 + avctx->extra_hw_frames;
ctx->frames_hwctx->BindFlags = D3D11_BIND_RENDER_TARGET;
ret = av_hwframe_ctx_init(ctx->frames_ref);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to initialise hardware frames context: %d.\n", ret);
goto fail;
}
return 0;
fail:
av_buffer_unref(&ctx->frames_ref);
return ret;
}
static int setup_gfxcapture_capture(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
int ret = 0;
stop_wgc_thread(avctx);
ret = find_capture_source(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to find capture source\n");
return ret;
}
ret = start_wgc_thread(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to start WGC thread\n");
return ret;
}
int cap_w = wgctx->cap_size.Width - cctx->crop_left - cctx->crop_right;
int cap_h = wgctx->cap_size.Height - cctx->crop_top - cctx->crop_bottom;
if (!cctx->capture_border) {
cap_w -= wgctx->client_area_offsets.left + wgctx->client_area_offsets.right;
cap_h -= wgctx->client_area_offsets.top + wgctx->client_area_offsets.bottom;
}
if (cctx->canvas_width == 0)
cctx->canvas_width = cap_w;
else if (cctx->canvas_width < 0)
cctx->canvas_width = (cap_w / cctx->canvas_width) * cctx->canvas_width;
if (cctx->canvas_height == 0)
cctx->canvas_height = cap_h;
else if (cctx->canvas_height < 0)
cctx->canvas_height = (cap_h / cctx->canvas_height) * cctx->canvas_height;
return 0;
}
static int prepare_render_resources(AVFilterContext *avctx)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextD3D> &d3dctx = ctx->d3d;
HRESULT hr;
ComPtr<ID3DBlob> vs_blob, ps_blob, err_blob;
CD3D11_SAMPLER_DESC sampler_desc(CD3D11_DEFAULT{});
UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
hr = ctx->fn.D3DCompile(render_shader_src, sizeof(render_shader_src) - 1, NULL, NULL, NULL, "main_vs", "vs_4_0", flags, 0, &vs_blob, &err_blob);
if (FAILED(hr)) {
if (err_blob) {
av_log(avctx, AV_LOG_ERROR, "Failed compiling vertex shader: %.*s\n", (int)err_blob->GetBufferSize(), (char*)err_blob->GetBufferPointer());
} else {
av_log(avctx, AV_LOG_ERROR, "Failed compiling vertex shader: 0x%08lX\n", hr);
}
return AVERROR_EXTERNAL;
}
const char *ps_entry = "main_ps_bicubic";
if (cctx->resize_mode == GFX_RESIZE_CROP || cctx->scale_mode == GFX_SCALE_POINT) {
ps_entry = "main_ps";
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
}
hr = ctx->fn.D3DCompile(render_shader_src, sizeof(render_shader_src) - 1, NULL, NULL, NULL, ps_entry, "ps_4_0", flags, 0, &ps_blob, &err_blob);
if (FAILED(hr)) {
if (err_blob) {
av_log(avctx, AV_LOG_ERROR, "Failed compiling pixel shader: %.*s\n", (int)err_blob->GetBufferSize(), (char*)err_blob->GetBufferPointer());
} else {
av_log(avctx, AV_LOG_ERROR, "Failed compiling pixel shader: 0x%08lX\n", hr);
}
return AVERROR_EXTERNAL;
}
CHECK_HR_RET(ctx->device_hwctx->device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), NULL, &d3dctx->vertex_shader));
CHECK_HR_RET(ctx->device_hwctx->device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), NULL, &d3dctx->pixel_shader));
CHECK_HR_RET(ctx->device_hwctx->device->CreateSamplerState(&sampler_desc, &d3dctx->sampler_state));
D3D11_BUFFER_DESC cb_desc = { 0 };
cb_desc.ByteWidth = 48;
cb_desc.Usage = D3D11_USAGE_DYNAMIC;
cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cb_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
CHECK_HR_RET(ctx->device_hwctx->device->CreateBuffer(&cb_desc, NULL, &d3dctx->shader_cb));
CHECK_HR_RET(ctx->device_hwctx->device->CreateDeferredContext(0, &d3dctx->deferred_ctx));
return 0;
}
static int gfxcapture_config_props(AVFilterLink *outlink)
{
AVFilterContext *avctx = outlink->src;
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
FilterLink *link = ff_filter_link(outlink);
int ret;
if (avctx->hw_device_ctx) {
ctx->device_ctx = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
if (ctx->device_ctx->type != AV_HWDEVICE_TYPE_D3D11VA) {
av_log(avctx, AV_LOG_ERROR, "Non-D3D11VA input hw_device_ctx\n");
return AVERROR(EINVAL);
}
ctx->device_ref = av_buffer_ref(avctx->hw_device_ctx);
if (!ctx->device_ref)
return AVERROR(ENOMEM);
av_log(avctx, AV_LOG_VERBOSE, "Using provided hw_device_ctx\n");
} else {
ret = av_hwdevice_ctx_create(&ctx->device_ref, AV_HWDEVICE_TYPE_D3D11VA, NULL, NULL, 0);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to create D3D11VA device.\n");
return ret;
}
ctx->device_ctx = (AVHWDeviceContext*)ctx->device_ref->data;
av_log(avctx, AV_LOG_VERBOSE, "Created internal hw_device_ctx\n");
}
ctx->device_hwctx = (AVD3D11VADeviceContext*)ctx->device_ctx->hwctx;
ret = prepare_render_resources(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to prepare render resources\n");
return ret;
}
ret = setup_gfxcapture_capture(avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to setup graphics capture\n");
return ret;
}
ret = init_hwframes_ctx(avctx);
if (ret < 0)
return ret;
link->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
if (!link->hw_frames_ctx)
return AVERROR(ENOMEM);
std::lock_guard wgc_lock(ctx->wgc_thread_uninit_mutex);
if (!ctx->wgc) {
av_log(avctx, AV_LOG_ERROR, "WGC thread died prematurely\n");
return AVERROR(ENOSYS);
}
outlink->w = ctx->frames_ctx->width;
outlink->h = ctx->frames_ctx->height;
outlink->time_base = AVRational{1, TIMESPAN_RES};
outlink->alpha_mode = cctx->premult_alpha ? AVALPHA_MODE_PREMULTIPLIED : AVALPHA_MODE_STRAIGHT;
link->frame_rate = cctx->frame_rate;
av_log(avctx, AV_LOG_DEBUG, "Capture setup with res %dx%d\n", outlink->w, outlink->h);
return 0;
}
static int render_capture_to_frame(AVFilterContext *avctx, AVFrame *frame, const ComPtr<ID3D11Texture2D> &src_tex)
{
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextD3D> &d3dctx = ctx->d3d;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
ID3D11Device *dev = ctx->device_hwctx->device;
ID3D11DeviceContext *dev_ctx = ctx->device_hwctx->device_context;
ComPtr<ID3D11DeviceContext> &def_ctx = d3dctx->deferred_ctx;
D3D11_TEXTURE2D_DESC dst_tex_desc;
reinterpret_cast<ID3D11Texture2D*>(frame->data[0])->GetDesc(&dst_tex_desc);
D3D11_TEXTURE2D_DESC src_tex_desc;
src_tex->GetDesc(&src_tex_desc);
D3D11_RENDER_TARGET_VIEW_DESC target_desc = {};
target_desc.Format = dst_tex_desc.Format;
if (dst_tex_desc.ArraySize > 1) {
target_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
target_desc.Texture2DArray.ArraySize = 1;
target_desc.Texture2DArray.FirstArraySlice = (uintptr_t)frame->data[1];
target_desc.Texture2DArray.MipSlice = 0;
} else {
target_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
target_desc.Texture2D.MipSlice = 0;
}
ComPtr<ID3D11RenderTargetView> rtv;
CHECK_HR_RET(dev->CreateRenderTargetView(
reinterpret_cast<ID3D11Resource*>(frame->data[0]), &target_desc, &rtv));
ComPtr<ID3D11ShaderResourceView> srv;
CHECK_HR_RET(dev->CreateShaderResourceView(src_tex.Get(), nullptr, &srv));
int crop_left = cctx->crop_left;
int crop_top = cctx->crop_top;
int crop_right = cctx->crop_right;
int crop_bottom = cctx->crop_bottom;
if (!cctx->capture_border) {
crop_left += wgctx->client_area_offsets.left;
crop_top += wgctx->client_area_offsets.top;
crop_right += wgctx->client_area_offsets.right;
crop_bottom += wgctx->client_area_offsets.bottom;
}
// Using the actual capture frame size here adjusts for jank that can happen during rapid
// resizing of the source window. The capture frame pool is only recreated once a frame
// of changed size came out of it, so we need to cut/pad such frames to fit.
// Just discarding such frames can lead to visible stutter if the source window is being
// resized continuously, so this code does its best to adjust them instead. With the risk
// of slight clamping artifacts when enlarging rapidly.
int cropped_w = wgctx->cap_size.Width - crop_left - crop_right;
int cropped_h = wgctx->cap_size.Height - crop_top - crop_bottom;
D3D11_VIEWPORT viewport = { 0 };
viewport.MinDepth = 0.f;
viewport.MaxDepth = 1.f;
switch (cctx->resize_mode) {
case GFX_RESIZE_CROP:
viewport.Width = (float)cropped_w;
viewport.Height = (float)cropped_h;
break;
case GFX_RESIZE_SCALE:
viewport.Width = dst_tex_desc.Width;
viewport.Height = dst_tex_desc.Height;
break;
case GFX_RESIZE_SCALE_ASPECT: {
float scale = FFMIN(dst_tex_desc.Width / (float)cropped_w,
dst_tex_desc.Height / (float)cropped_h);
viewport.Width = cropped_w * scale;
viewport.Height = cropped_h * scale;
break;
}
default:
av_log(avctx, AV_LOG_ERROR, "Invalid scaling mode\n");
return AVERROR_BUG;
};
def_ctx->RSSetViewports(1, &viewport);
D3D11_MAPPED_SUBRESOURCE map;
CHECK_HR_RET(def_ctx->Map(d3dctx->shader_cb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &map));
{
float *cb_f = static_cast<float*>(map.pData);
uint32_t *cb_u = static_cast<uint32_t*>(map.pData);
cb_f[0] = (float)cropped_w;
cb_f[1] = (float)cropped_h;
cb_f[2] = viewport.Width;
cb_f[3] = viewport.Height;
cb_f[4] = crop_left / (float)src_tex_desc.Width; // min_u
cb_f[5] = crop_top / (float)src_tex_desc.Height; // min_v
cb_f[6] = (crop_left + cropped_w) / (float)src_tex_desc.Width; // max_u
cb_f[7] = (crop_top + cropped_h) / (float)src_tex_desc.Height; // max_v
cb_u[8] = !cctx->premult_alpha; // to_unpremult
cb_u[9] = src_tex_desc.Format == DXGI_FORMAT_R16G16B16A16_FLOAT &&
dst_tex_desc.Format != DXGI_FORMAT_R16G16B16A16_FLOAT; // to_srgb
}
def_ctx->Unmap(d3dctx->shader_cb.Get(), 0);
def_ctx->OMSetRenderTargets(1, rtv.GetAddressOf(), nullptr);
const float clear_color[4] = {0.f, 0.f, 0.f, 1.f};
def_ctx->ClearRenderTargetView(rtv.Get(), clear_color);
def_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
def_ctx->VSSetShader(d3dctx->vertex_shader.Get(), nullptr, 0);
def_ctx->VSSetConstantBuffers(0, 1, d3dctx->shader_cb.GetAddressOf());
def_ctx->PSSetShader(d3dctx->pixel_shader.Get(), nullptr, 0);
def_ctx->PSSetSamplers(0, 1, d3dctx->sampler_state.GetAddressOf());
def_ctx->PSSetShaderResources(0, 1, srv.GetAddressOf());
def_ctx->PSSetConstantBuffers(0, 1, d3dctx->shader_cb.GetAddressOf());
def_ctx->Draw(3, 0);
ComPtr<ID3D11CommandList> cmd_list;
CHECK_HR_RET(def_ctx->FinishCommandList(FALSE, &cmd_list));
dev_ctx->ExecuteCommandList(cmd_list.Get(), FALSE);
return 0;
}
static int process_frame_if_exists(AVFilterLink *outlink)
{
AVFilterContext *avctx = outlink->src;
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
int ret;
AVFrame *frame = nullptr;
ret = run_on_wgc_thread(avctx, [&]() {
ComPtr<IDirect3D11CaptureFrame> capture_frame;
ComPtr<IDirect3DSurface> capture_surface;
ComPtr<IDirect3DDxgiInterfaceAccess> dxgi_interface_access;
ComPtr<ID3D11Texture2D> frame_texture;
TimeSpan frame_time = { 0 };
int res = wgc_try_get_next_frame(avctx, capture_frame);
if (res < 0)
return res;
CHECK_HR_RET(capture_frame->get_SystemRelativeTime(&frame_time));
CHECK_HR_RET(capture_frame->get_Surface(&capture_surface));
CHECK_HR_RET(capture_surface.As(&dxgi_interface_access));
CHECK_HR_RET(dxgi_interface_access->GetInterface(IID_PPV_ARGS(&frame_texture)));
if (!frame_texture)
return AVERROR(EAGAIN);
frame = ff_get_video_buffer(outlink, cctx->canvas_width, cctx->canvas_height);
if (!frame)
return AVERROR(ENOMEM);
frame->pts = frame_time.Duration;
return render_capture_to_frame(avctx, frame, frame_texture);
});
if (ret < 0)
return ret;
frame->sample_aspect_ratio = AVRational{1, 1};
if (ctx->frames_ctx->sw_format == AV_PIX_FMT_RGBAF16) {
// According to MSDN, all floating point formats contain sRGB image data with linear 1.0 gamma.
frame->color_range = AVCOL_RANGE_JPEG;
frame->color_primaries = AVCOL_PRI_BT709;
frame->color_trc = AVCOL_TRC_LINEAR;
frame->colorspace = AVCOL_SPC_RGB;
} else {
// According to MSDN, all integer formats contain sRGB image data
frame->color_range = AVCOL_RANGE_JPEG;
frame->color_primaries = AVCOL_PRI_BT709;
frame->color_trc = AVCOL_TRC_IEC61966_2_1;
frame->colorspace = AVCOL_SPC_RGB;
}
ctx->last_pts = frame->pts;
if (!ctx->first_pts)
ctx->first_pts = frame->pts;
frame->pts -= ctx->first_pts;
return ff_filter_frame(outlink, frame);
}
static int gfxcapture_activate(AVFilterContext *avctx)
{
AVFilterLink *outlink = avctx->outputs[0];
GfxCaptureContext *cctx = CCTX(avctx->priv);
GfxCaptureContextCpp *ctx = cctx->ctx;
std::unique_ptr<GfxCaptureContextWgc> &wgctx = ctx->wgc;
std::lock_guard wgc_lock(ctx->wgc_thread_uninit_mutex);
if (!wgctx) {
av_log(avctx, AV_LOG_ERROR, "WGC thread not initialized\n");
return AVERROR(ENOSYS);
}
if (!ff_outlink_frame_wanted(outlink))
return FFERROR_NOT_READY;
for (;;) {
uint64_t last_seq = wgctx->frame_seq;
int ret = process_frame_if_exists(outlink);
if (ret != AVERROR(EAGAIN))
return ret;
std::unique_lock frame_lock(wgctx->frame_arrived_mutex);
if (wgctx->window_closed && wgctx->frame_seq == last_seq) {
ff_outlink_set_status(outlink, AVERROR_EOF, ctx->last_pts - ctx->first_pts + 1);
break;
}
if (!wgctx->frame_arrived_cond.wait_for(frame_lock, std::chrono::seconds(1), [&]() {
return wgctx->frame_seq != last_seq || wgctx->window_closed;
}))
break;
}
return 0;
}
av_cold void ff_gfxcapture_uninit(AVFilterContext *avctx) noexcept
{
try {
gfxcapture_uninit(avctx);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during uninit: %s\n", e.what());
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during uninit\n");
}
}
av_cold int ff_gfxcapture_init(AVFilterContext *avctx) noexcept
{
try {
return gfxcapture_init(avctx);
} catch (const std::bad_alloc&) {
return AVERROR(ENOMEM);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during init: %s\n", e.what());
return AVERROR_BUG;
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during init\n");
return AVERROR_BUG;
}
}
int ff_gfxcapture_activate(AVFilterContext *avctx) noexcept
{
try {
return gfxcapture_activate(avctx);
} catch (const std::bad_alloc&) {
return AVERROR(ENOMEM);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during activate: %s\n", e.what());
return AVERROR_BUG;
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during activate\n");
return AVERROR_BUG;
}
}
int ff_gfxcapture_config_props(AVFilterLink *outlink) noexcept
{
AVFilterContext *avctx = outlink->src;
try {
return gfxcapture_config_props(outlink);
} catch (const std::bad_alloc&) {
return AVERROR(ENOMEM);
} catch (const std::exception &e) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during config_props: %s\n", e.what());
return AVERROR_BUG;
} catch (...) {
av_log(avctx, AV_LOG_ERROR, "unhandled exception during config_props\n");
return AVERROR_BUG;
}
}
|