[go: up one dir, main page]

Menu

[760ccc]: / Filters.cpp  Maximize  Restore  History

Download this file

1153 lines (918 with data), 33.5 kB

   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
#define WIN32_LEAN_AND_MEAN
#pragma warning(disable:4244)
#pragma warning(disable:4711)
#include <streams.h>
#include <stdio.h>
#include <olectl.h>
#include <dvdmedia.h>
#include <smartptr.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#include <windows.h>
#include <shlobj.h>
#include <shellapi.h>
#include <dxgi1_2.h>
#include <d3d11.h>
#include <memory>
#include <algorithm>
#include <string>
#pragma comment(lib, "D3D11.lib")
//////////////////////////////////////////////////////////////////////////
// This file contains routines to register / Unregister the
// Directshow filter 'Virtual Cam'
// We do not use the inbuilt BaseClasses routines as we need to register as
// a capture source
//////////////////////////////////////////////////////////////////////////
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")
#pragma comment(lib, "advapi32")
#pragma comment(lib, "winmm")
#pragma comment(lib, "ole32")
#pragma comment(lib, "oleaut32")
#ifdef _DEBUG
#pragma comment(lib, "strmbasd")
#else
#pragma comment(lib, "strmbase")
#endif
#include <olectl.h>
#include <initguid.h>
#include <dllsetup.h>
#define CreateComObject(clsid, iid, var) CoCreateInstance( clsid, NULL, CLSCTX_INPROC_SERVER, iid, (void **)&var);
#define BYTES_ALIGN 4
#pragma once
#define DECLARE_PTR(type, ptr, expr) type* ptr = (type*)(expr);
#ifdef DEBUG
#define REGISTER_FILTERGRAPH
#endif
bool m_bFormatAlreadySet=true;
//HBITMAP m_hDIB;
//BYTE *m_pDIBData;
HDC m_hDC, m_hDeskDC;
//BYTE *pData;
BITMAPINFO lBmpInfo;
//std::unique_ptr<BYTE> pBuf(new BYTE[lBmpInfo.bmiHeader.biSizeImage]);
D3D11_MAPPED_SUBRESOURCE mapResource;
//unsigned int frame_count=100;
void cleanrefs();
EXTERN_C const GUID CLSID_Virtualcam3;
STDAPI AMovieSetupRegisterServer( CLSID clsServer, LPCWSTR szDescription, LPCWSTR szFileName, LPCWSTR szThreadingModel = L"Both", LPCWSTR szServerType = L"InprocServer32" );
STDAPI AMovieSetupUnregisterServer( CLSID clsServer );
DEFINE_GUID(CLSID_Virtualcam3,
0x6f7b75af,0xefdf,0x4a03,0x97,0x71,0xe7,0x7f,0x4b,0x86,0x5e,0x84);
class DECLSPEC_UUID("6F7B75AF-EFDF-4A03-9771-E77F4B865E84") ICaptureConfig;
int width1 = GetSystemMetrics(SM_CXSCREEN);
int height1 = GetSystemMetrics(SM_CYSCREEN);
FILE *fp;char letra;
//int inic=0;
//int lresult=0;
HRESULT hr(E_FAIL);
HDC _hdc;
HWND _hwnd;
HBITMAP _bitmap;
//int _width=width1, _height=height1;
HDC lHDC;
template <typename T>
class CComPtrCustom
{
public:
CComPtrCustom(T *aPtrElement)
:element(aPtrElement)
{
}
CComPtrCustom()
:element(nullptr)
{
}
virtual ~CComPtrCustom()
{
Release();
}
T* Detach()
{
auto lOutPtr = element;
element = nullptr;
return lOutPtr;
}
T* detach()
{
return Detach();
}
void Release()
{
if (element == nullptr)
return;
auto k = element->Release();
element = nullptr;
}
CComPtrCustom& operator = (T *pElement)
{
Release();
if (pElement == nullptr)
return *this;
auto k = pElement->AddRef();
element = pElement;
return *this;
}
void Swap(CComPtrCustom& other)
{
T* pTemp = element;
element = other.element;
other.element = pTemp;
}
T* operator->()
{
return element;
}
operator T*()
{
return element;
}
operator T*() const
{
return element;
}
T* get()
{
return element;
}
T* get() const
{
return element;
}
T** operator &()
{
return &element;
}
bool operator !()const
{
return element == nullptr;
}
operator bool()const
{
return element != nullptr;
}
bool operator == (const T *pElement)const
{
return element == pElement;
}
CComPtrCustom(const CComPtrCustom& aCComPtrCustom)
{
if (aCComPtrCustom.operator!())
{
element = nullptr;
return;
}
element = aCComPtrCustom;
auto h = element->AddRef();
h++;
}
CComPtrCustom& operator = (const CComPtrCustom& aCComPtrCustom)
{
Release();
element = aCComPtrCustom;
auto k = element->AddRef();
return *this;
}
_Check_return_ HRESULT CopyTo(T** ppT) throw()
{
if (ppT == NULL)
return E_POINTER;
*ppT = element;
if (element)
element->AddRef();
return S_OK;
}
HRESULT CoCreateInstance(const CLSID aCLSID)
{
T* lPtrTemp;
auto lresult = ::CoCreateInstance(aCLSID, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&lPtrTemp));
if (SUCCEEDED(lresult))
{
if (lPtrTemp != nullptr)
{
Release();
element = lPtrTemp;
}
}
return lresult;
}
protected:
T* element;
};
// Driver types supported
D3D_DRIVER_TYPE gDriverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE //D3D_DRIVER_TYPE_HARDWARE esse é o tipo mais rapido
};
UINT gNumDriverTypes = ARRAYSIZE(gDriverTypes);
// Feature levels supported
D3D_FEATURE_LEVEL gFeatureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1
};
//CComPtrCustom<IDXGIAdapter> lDxgiAdapter;
UINT gNumFeatureLevels = ARRAYSIZE(gFeatureLevels);
CComPtrCustom<ID3D11Device> lDevice;
CComPtrCustom<ID3D11DeviceContext> lImmediateContext;
CComPtrCustom<IDXGIOutputDuplication> lDeskDupl;
CComPtrCustom<ID3D11Texture2D> lAcquiredDesktopImage;
CComPtrCustom<ID3D11Texture2D> lGDIImage; //inutil???
CComPtrCustom<ID3D11Texture2D> lDestImage;
DXGI_OUTPUT_DESC lOutputDesc;
DXGI_OUTDUPL_DESC lOutputDuplDesc;
UINT Output = 0;
D3D11_TEXTURE2D_DESC desc;
CComPtrCustom<IDXGIResource> lDesktopResource;
DXGI_OUTDUPL_FRAME_INFO lFrameInfo;
//void LocalOutput(const char *str, ...);
//void LocalOutput(const wchar_t *str, ...);
class Cvcam3Stream;
class Cvcam3 : public CSource // public ICaptureConfig
{
public:
// DECLARE_IUNKNOWN;
//////////////////////////////////////////////////////////////////////////
// IUnknown
//////////////////////////////////////////////////////////////////////////
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
IFilterGraph *GetGraph() {return m_pGraph;}
// ICaptureConfig
void SetCaptureRect(int x, int y, int w=width1, int h=height1, int fps=30);
private:
Cvcam3(LPUNKNOWN lpunk, HRESULT *phr);
};
class ICaptureConfig : public IUnknown
{
public:
virtual void SetCaptureRect(int x=0, int y=0, int w=width1, int h=height1, int fps=30);
};
class Cvcam3Stream : public CSourceStream, public IAMStreamConfig, public IKsPropertySet
{
public:
// DECLARE_IUNKNOWN;
//////////////////////////////////////////////////////////////////////////
// IUnknown
//////////////////////////////////////////////////////////////////////////
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP_(ULONG) AddRef() { return GetOwner()->AddRef(); } \
STDMETHODIMP_(ULONG) Release() { return GetOwner()->Release(); }
// static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
//////////////////////////////////////////////////////////////////////////
// IQualityControl
//////////////////////////////////////////////////////////////////////////
STDMETHODIMP Notify(IBaseFilter * pSender, Quality q);
//////////////////////////////////////////////////////////////////////////
// IAMStreamConfig
//////////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE SetFormat(AM_MEDIA_TYPE *pmt);
HRESULT STDMETHODCALLTYPE GetFormat(AM_MEDIA_TYPE **ppmt);
HRESULT STDMETHODCALLTYPE GetNumberOfCapabilities(int *piCount, int *piSize);
HRESULT STDMETHODCALLTYPE GetStreamCaps(int iIndex, AM_MEDIA_TYPE **pmt, BYTE *pSCC);
//////////////////////////////////////////////////////////////////////////
// IKsPropertySet
//////////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE Set(REFGUID guidPropSet, DWORD dwID, void *pInstanceData, DWORD cbInstanceData, void *pPropData, DWORD cbPropData);
HRESULT STDMETHODCALLTYPE Get(REFGUID guidPropSet, DWORD dwPropID, void *pInstanceData,DWORD cbInstanceData, void *pPropData, DWORD cbPropData, DWORD *pcbReturned);
HRESULT STDMETHODCALLTYPE QuerySupported(REFGUID guidPropSet, DWORD dwPropID, DWORD *pTypeSupport);
//////////////////////////////////////////////////////////////////////////
// CSourceStream
//////////////////////////////////////////////////////////////////////////
Cvcam3Stream(HRESULT *phr, Cvcam3 *pParent, LPCWSTR pPinName);
~Cvcam3Stream();
HRESULT FillBuffer(IMediaSample *pms);
HRESULT DecideBufferSize(IMemAllocator *pIMemAlloc, ALLOCATOR_PROPERTIES *pProperties);
HRESULT CheckMediaType(const CMediaType *pMediaType);
HRESULT GetMediaType(int iPosition, CMediaType *pmt);
HRESULT SetMediaType(const CMediaType *pmt);
HRESULT OnThreadCreate(void);
HRESULT OnThreadDestroy(void);
// ICaptureConfig
void SetCaptureRect(int x, int y, int w=width1, int h=height1, int fps=30);
private:
Cvcam3 *m_pParent;
REFERENCE_TIME m_rtLastTime;
HBITMAP m_hLogoBmp;
CCritSec m_cSharedState;
IReferenceClock *m_pClock;
int m_x, m_y, m_w=width1, m_h=height1;
};
const AMOVIESETUP_MEDIATYPE AMSMediaTypesvcam3 =
{
&MEDIATYPE_Video,
&MEDIASUBTYPE_NULL
};
const AMOVIESETUP_PIN AMSPinvcam3=
{
L"Output", // Pin string name
FALSE, // Is it rendered
TRUE, // Is it an output
FALSE, // Can we have none
FALSE, // Can we have many
&CLSID_NULL, // Connects to filter
NULL, // Connects to pin
1, // Number of types
&AMSMediaTypesvcam3 // Pin Media types
};
const AMOVIESETUP_FILTER AMSFiltervcam3 =
{
&CLSID_Virtualcam3, // Filter CLSID
L"Virtual Cam3", // String name
MERIT_DO_NOT_USE, // Filter merit
1, // Number pins
&AMSPinvcam3 // Pin details
};
CFactoryTemplate g_Templates[] =
{
{
L"Virtual Cam",
&CLSID_Virtualcam3,
Cvcam3::CreateInstance,
NULL,
&AMSFiltervcam3
},
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
STDAPI RegisterFilters( BOOL bRegister )
{
HRESULT hr = NOERROR;
WCHAR achFileName[MAX_PATH];
char achTemp[MAX_PATH];
ASSERT(g_hInst != 0);
if( 0 == GetModuleFileNameA(g_hInst, achTemp, sizeof(achTemp)))
return AmHresultFromWin32(GetLastError());
MultiByteToWideChar(CP_ACP, 0L, achTemp, lstrlenA(achTemp) + 1,
achFileName, NUMELMS(achFileName));
hr = CoInitialize(0);
if(bRegister)
{
hr = AMovieSetupRegisterServer(CLSID_Virtualcam3, L"Virtual Cam3", achFileName, L"Both", L"InprocServer32");
}
if( SUCCEEDED(hr) )
{
IFilterMapper2 *fm = 0;
hr = CreateComObject( CLSID_FilterMapper2, IID_IFilterMapper2, fm );
if( SUCCEEDED(hr) )
{
if(bRegister)
{
IMoniker *pMoniker = 0;
REGFILTER2 rf2;
rf2.dwVersion = 1;
rf2.dwMerit = MERIT_DO_NOT_USE;
rf2.cPins = 1;
rf2.rgPins = &AMSPinvcam3;
hr = fm->RegisterFilter(CLSID_Virtualcam3, L"Virtual Cam3", &pMoniker, &CLSID_VideoInputDeviceCategory, NULL, &rf2);
}
else
{
hr = fm->UnregisterFilter(&CLSID_VideoInputDeviceCategory, 0, CLSID_Virtualcam3);
}
}
// release interface
//
if(fm)
fm->Release();
}
if( SUCCEEDED(hr) && !bRegister )
hr = AMovieSetupUnregisterServer( CLSID_Virtualcam3 );
CoFreeUnusedLibraries();
CoUninitialize();
return hr;
}
STDAPI DllRegisterServer()
{
return RegisterFilters(TRUE);
}
STDAPI DllUnregisterServer()
{
return RegisterFilters(FALSE);
}
extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
return DllEntryPoint((HINSTANCE)(hModule), dwReason, lpReserved);
}
//////////////////////////////////////////////////////////////////////////
// Cvcam3 is the source filter which masquerades as a capture device
//////////////////////////////////////////////////////////////////////////
CUnknown * WINAPI Cvcam3::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
{
ASSERT(phr);
CUnknown *punk = new Cvcam3(lpunk, phr);
return punk;
}
Cvcam3::Cvcam3(LPUNKNOWN lpunk, HRESULT *phr) :
CSource(NAME("Virtual Cam3"), lpunk, CLSID_Virtualcam3)
{
ASSERT(phr);
CAutoLock cAutoLock(&m_cStateLock);
// Create the one and only output pin
m_paStreams = (CSourceStream **) new Cvcam3Stream*[1];
m_paStreams[0] = new Cvcam3Stream(phr, this, L"Virtual Cam");
}
HRESULT Cvcam3::QueryInterface(REFIID riid, void **ppv)
{
//Forward request for IAMStreamConfig & IKsPropertySet to the pin
if(riid == _uuidof(IAMStreamConfig) || riid == _uuidof(IKsPropertySet))
return m_paStreams[0]->QueryInterface(riid, ppv);
else
return CSource::QueryInterface(riid, ppv);
if(riid == __uuidof(ICaptureConfig))
{
return GetInterface((ICaptureConfig*)this, ppv);
}
else
return CSource::QueryInterface(riid, ppv);
}
void Cvcam3::SetCaptureRect(int x, int y, int w, int h, int fps)
{
((Cvcam3Stream*)m_paStreams[0])->SetCaptureRect(x, y, w, h, fps);
}
//////////////////////////////////////////////////////////////////////////
// Cvcam3Stream is the one and only output pin of Cvcam3 which handles
// all the stuff.
//////////////////////////////////////////////////////////////////////////
Cvcam3Stream::Cvcam3Stream(HRESULT *phr, Cvcam3 *pParent, LPCWSTR pPinName) :
CSourceStream(NAME("Virtual Cam"),phr, pParent, pPinName), m_pParent(pParent)
{
// Set the default media type as 320x240x24@15
GetMediaType(4, &m_mt);
m_hDeskDC = GetDC(GetDesktopWindow());
m_hDC = CreateCompatibleDC(0);
SetCaptureRect(0, 0, width1, height1, 30);
}
void Cvcam3Stream::SetCaptureRect(int x, int y, int w, int h, int fps)
{
if(IsConnected()) return;
D3D_FEATURE_LEVEL lFeatureLevel;
// Create device
for (UINT DriverTypeIndex = 0; DriverTypeIndex < gNumDriverTypes; ++DriverTypeIndex)
{
hr = D3D11CreateDevice(nullptr,gDriverTypes[DriverTypeIndex],nullptr,
0,gFeatureLevels, gNumFeatureLevels, D3D11_SDK_VERSION,&lDevice,
&lFeatureLevel,&lImmediateContext);
if (SUCCEEDED(hr))
{
//FALTA FAZER LOOP???
// Device creation success, no need to loop anymore
}
//lDevice.Release();
//lImmediateContext.Release();
}
/*
fp=fopen("c:\\w1\\log.txt","w+");
setvbuf (fp, NULL ,_IONBF, 1024 );
letra=0x41; fputc(letra,fp);
fclose(fp);
*/
//if (FAILED(hr)) return hr;
lDevice->AddRef();
lImmediateContext->AddRef();
//Sleep(100);
//if (lDevice == nullptr) break;
// Get DXGI device
CComPtrCustom<IDXGIDevice> lDxgiDevice=nullptr;
hr = lDevice->QueryInterface(IID_PPV_ARGS(&lDxgiDevice));
//if (FAILED(hr)) break;
// Get DXGI adapter
CComPtrCustom<IDXGIAdapter> lDxgiAdapter=nullptr;
hr = lDxgiDevice->GetParent(__uuidof(IDXGIAdapter),reinterpret_cast<void**>(&lDxgiAdapter));
//if (FAILED(hr)) break;
//lDxgiDevice.Release();
// Get output
CComPtrCustom<IDXGIOutput> lDxgiOutput=nullptr;
hr = lDxgiAdapter->EnumOutputs(Output,&lDxgiOutput);
//if (FAILED(hr))
// break;
lDxgiAdapter.Release();
hr = lDxgiOutput->GetDesc(&lOutputDesc);
//if (FAILED(hr))
// break;
// QI for Output 1
CComPtrCustom<IDXGIOutput1> lDxgiOutput1=nullptr;
hr = lDxgiOutput->QueryInterface(IID_PPV_ARGS(&lDxgiOutput1));
//if (FAILED(hr))
// break;
lDxgiOutput.Release();
if (lDeskDupl) {
lDeskDupl->Release();
lDeskDupl = nullptr;
}
// Create desktop duplication
hr = lDxgiOutput1->DuplicateOutput(lDevice,&lDeskDupl);
//if (FAILED(hr))
// break;
lDxgiOutput1.Release();
lDxgiOutput1=nullptr;
// Create GUI drawing texture
RtlZeroMemory(&lOutputDesc, sizeof(DXGI_OUTPUT_DESC)); // a verificar
lDeskDupl->GetDesc(&lOutputDuplDesc); // a verificar
//D3D11_TEXTURE2D_DESC desc;
//if (FAILED(hr))
// break;
//if (lGDIImage == nullptr)
// break;
// Create CPU access texture
desc.Width = lOutputDuplDesc.ModeDesc.Width;
desc.Height = lOutputDuplDesc.ModeDesc.Height;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; //lOutputDuplDesc.ModeDesc.Format;
desc.ArraySize = 1;
desc.BindFlags = 0;
desc.MiscFlags = 0;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.MipLevels = 1;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; // desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.Usage = D3D11_USAGE_STAGING;
hr = lDevice->CreateTexture2D(&desc, NULL, &lDestImage);
//if (FAILED(hr))
// break;
//if (lDestImage == nullptr)
// break;
//CComPtrCustom<IDXGIResource> lDesktopResource;
//CODIGO ANTIGO DO Vcam3
m_x = x;
m_y = y;
m_w = width1;
m_h = height1;
// Measure capture rate
DWORD tick = GetTickCount();
double actualFPS = 1000.0 / (double)tick;
// if(fps < actualFPS)
fps=30;
actualFPS = fps;
DECLARE_PTR(VIDEOINFOHEADER, pvi, m_mt.AllocFormatBuffer(sizeof(VIDEOINFOHEADER)));
ZeroMemory(pvi, sizeof(VIDEOINFOHEADER));
pvi->bmiHeader.biCompression = BI_RGB;
pvi->bmiHeader.biBitCount = 32; //GetDeviceCaps(m_hDeskDC, BITSPIXEL);
pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pvi->bmiHeader.biWidth = width1; //m_w;
pvi->bmiHeader.biHeight = height1; //m_h;
pvi->bmiHeader.biPlanes = 1;
pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
pvi->bmiHeader.biClrImportant = 0;
pvi->AvgTimePerFrame = 10000000 / actualFPS;
SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
m_mt.SetType(&MEDIATYPE_Video);
m_mt.SetFormatType(&FORMAT_VideoInfo);
m_mt.SetTemporalCompression(FALSE);
// Work out the GUID for the subtype from the header info.
const GUID SubTypeGUID = GetBitmapSubtype(&pvi->bmiHeader);
m_mt.SetSubtype(&SubTypeGUID);
m_mt.SetSampleSize(pvi->bmiHeader.biSizeImage);
}
Cvcam3Stream::~Cvcam3Stream()
{
if (lAcquiredDesktopImage)
{
lAcquiredDesktopImage->Release();
lAcquiredDesktopImage = NULL;
}
if (lDesktopResource) lDesktopResource.Release();
if (lDeskDupl)
{lDeskDupl->Release();
lDeskDupl=nullptr;
}
if(lGDIImage) //inutil????
{lGDIImage->Release();
lGDIImage=nullptr;
}
if (lDestImage)
{lDestImage->Release();
lDestImage=nullptr;
}
//cleanrefs();
//if (sptr)
//{delete[] sptr; sptr=nullptr;}
} //~cvcam3stream
HRESULT Cvcam3Stream::QueryInterface(REFIID riid, void **ppv)
{
// Standard OLE stuff
if(riid == _uuidof(IAMStreamConfig))
*ppv = (IAMStreamConfig*)this;
else if(riid == _uuidof(IKsPropertySet))
*ppv = (IKsPropertySet*)this;
else
return CSourceStream::QueryInterface(riid, ppv);
AddRef();
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
// This is the routine where we create the data being output by the Virtual
// Camera device.
//////////////////////////////////////////////////////////////////////////
HRESULT Cvcam3Stream::FillBuffer(IMediaSample *pms)
{
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);
lDeskDupl->ReleaseFrame();
//lDeskDupl->AcquireNextFrame(0, &lFrameInfo, &lDesktopResource)) && lFrameInfo.LastPresentTime.QuadPart)
hr=lDeskDupl->AcquireNextFrame(33, &lFrameInfo, &lDesktopResource);
if (FAILED(hr)) goto velho;
if (hr == DXGI_ERROR_WAIT_TIMEOUT) goto velho;
// QI for ID3D11Texture2D
// If still holding old frame, destroy it
if (lAcquiredDesktopImage)
{
lAcquiredDesktopImage->Release();
lAcquiredDesktopImage = NULL;
}
hr = lDesktopResource->QueryInterface(IID_PPV_ARGS(&lAcquiredDesktopImage));
lDesktopResource.Release();
//lDeskDupl->ReleaseFrame();
//if (lAcquiredDesktopImage == nullptr)
// break;
// Copy image into GDI drawing texture
velho:
lImmediateContext->CopyResource(lDestImage, lAcquiredDesktopImage);
lAcquiredDesktopImage.Release();
//UINT subresource = D3D11CalcSubresource(0, 0, 0);
hr=lImmediateContext->Map(lDestImage,0,D3D11_MAP_READ, 0,&mapResource); //hr=lImmediateContext->Map(lDestImage,subresource,D3D11_MAP_READ, NULL,&mapResource);
lImmediateContext->Unmap(lDestImage, 0); //lImmediateContext->Unmap(lDestImage, subresource);
/*
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);
*/
// long lDataLen;
BYTE *pData = (BYTE*)mapResource.pData;
pms->GetPointer(&pData);
//memcpy(pData,mapResource.pData, mapResource.DepthPitch); // OK!!!!!!!!!!!!
BYTE* sptr = reinterpret_cast<BYTE*>(mapResource.pData);
UINT offset = height1 -1;
//memcpy(pData, sptr, mapResource.DepthPitch);
for (size_t h = 0; h < lOutputDuplDesc.ModeDesc.Height; ++h)
{
memcpy_s(pData, mapResource.RowPitch, sptr + (offset * mapResource.RowPitch), mapResource.RowPitch ); //memcpy_s(dptr, lBmpRowPitch, sptr, lRowPitch);
offset= offset-1;
pData += mapResource.RowPitch;
}
return NOERROR;
} // FillBuffer
//
// Notify
// Ignore quality management messages sent from the downstream filter
STDMETHODIMP Cvcam3Stream::Notify(IBaseFilter * pSender, Quality q)
{
return E_NOTIMPL;
} // Notify
//////////////////////////////////////////////////////////////////////////
// This is called when the output format has been negotiated
//////////////////////////////////////////////////////////////////////////
HRESULT Cvcam3Stream::SetMediaType(const CMediaType *pmt)
{
DECLARE_PTR(VIDEOINFOHEADER, pvi, pmt->Format());
HRESULT hr = CSourceStream::SetMediaType(pmt);
//MakeDIB();
return hr;
}
// See Directshow help topic for IAMStreamConfig for details on this method
HRESULT Cvcam3Stream::GetMediaType(int iPosition, CMediaType *pmt)
{
CheckPointer(pmt, E_POINTER);
CAutoLock cAutoLock(m_pFilter->pStateLock());
if(m_bFormatAlreadySet) {
// you can only have one option, buddy, if setFormat already called. (see SetFormat's msdn)
if(iPosition != 0)
return E_INVALIDARG;
VIDEOINFO *pvi = (VIDEOINFO *) m_mt.Format();
// Set() copies these in for us pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader); // calculates the size for us, after we gave it the width and everything else we already chucked into it
// pmt->SetSampleSize(pvi->bmiHeader.biSizeImage);
// nobody uses sample size anyway :P
pmt->Set(m_mt);
VIDEOINFOHEADER *pVih1 = (VIDEOINFOHEADER*) m_mt.pbFormat;
VIDEOINFO *pviHere = (VIDEOINFO *) pmt->pbFormat;
return S_OK;
}
if(iPosition < 0) return E_INVALIDARG;
if(iPosition > 8) return VFW_S_NO_MORE_ITEMS;
if(iPosition == 0)
{
iPosition=1;
//*pmt = m_mt;
//return S_OK;
}
// DECLARE_PTR(VIDEOINFOHEADER, pvi, pmt->AllocFormatBuffer(sizeof(VIDEOINFOHEADER)));
// ZeroMemory(pvi, sizeof(VIDEOINFOHEADER));
VIDEOINFO *pvi = (VIDEOINFO *) pmt->AllocFormatBuffer(sizeof(VIDEOINFO));
if(NULL == pvi)
return(E_OUTOFMEMORY);
ZeroMemory(pvi, sizeof(VIDEOINFO));
pvi->bmiHeader.biCompression = BI_RGB;
pvi->bmiHeader.biBitCount = 32;
pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pvi->bmiHeader.biWidth = width1; // width1 * iPosition;
pvi->bmiHeader.biHeight = height1; // height1 * iPosition;
pvi->bmiHeader.biPlanes = 1;
pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
pvi->bmiHeader.biClrImportant = 0;
pvi->AvgTimePerFrame = 1000000;
SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
pmt->SetType(&MEDIATYPE_Video);
pmt->SetFormatType(&FORMAT_VideoInfo);
pmt->SetTemporalCompression(FALSE);
// Work out the GUID for the subtype from the header info.
if(*pmt->Subtype() == GUID_NULL) {
const GUID SubTypeGUID = GetBitmapSubtype(&pvi->bmiHeader);
pmt->SetSubtype(&SubTypeGUID);
}
/*
// Work out the GUID for the subtype from the header info.
const GUID SubTypeGUID = GetBitmapSubtype(&pvi->bmiHeader);
pmt->SetSubtype(&SubTypeGUID);
pmt->SetSampleSize(pvi->bmiHeader.biSizeImage);
*/
return NOERROR;
} // GetMediaType
// This method is called to see if a given output format is supported
HRESULT Cvcam3Stream::CheckMediaType(const CMediaType *pMediaType)
{
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)(pMediaType->Format());
if(*pMediaType != m_mt) return E_INVALIDARG;
return S_OK;
} // CheckMediaType
// This method is called after the pins are connected to allocate buffers to stream data
HRESULT Cvcam3Stream::DecideBufferSize(IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *pProperties)
{
CAutoLock cAutoLock(m_pFilter->pStateLock());
HRESULT hr = NOERROR;
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) m_mt.Format();
pProperties->cBuffers = 1;
pProperties->cbBuffer = pvi->bmiHeader.biSizeImage;
ALLOCATOR_PROPERTIES Actual;
hr = pAlloc->SetProperties(pProperties,&Actual);
if(FAILED(hr)) return hr;
if(Actual.cbBuffer < pProperties->cbBuffer) return E_FAIL;
return NOERROR;
} // DecideBufferSize
// Called when graph is run
HRESULT Cvcam3Stream::OnThreadCreate()
{
/*
fp=fopen("c:\\w1\\log.txt","w+");
setvbuf (fp, NULL ,_IONBF, 1024 );
letra=0x41; fputc(letra,fp);
fclose(fp);
*/
m_rtLastTime = 0;
return NOERROR;
} // OnThreadCreate
HRESULT Cvcam3Stream::OnThreadDestroy()
{
m_rtLastTime = 0;
return NOERROR;
} // OnThreadCreate
//////////////////////////////////////////////////////////////////////////
// IAMStreamConfig
//////////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE Cvcam3Stream::SetFormat(AM_MEDIA_TYPE *pmt)
{
DECLARE_PTR(VIDEOINFOHEADER, pvi, m_mt.pbFormat);
m_mt = *pmt;
IPin* pin;
ConnectedTo(&pin);
if(pin)
{
IFilterGraph *pGraph = m_pParent->GetGraph();
pGraph->Reconnect(this);
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE Cvcam3Stream::GetFormat(AM_MEDIA_TYPE **ppmt)
{
*ppmt = CreateMediaType(&m_mt);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Cvcam3Stream::GetNumberOfCapabilities(int *piCount, int *piSize)
{
*piCount = 8;
*piSize = sizeof(VIDEO_STREAM_CONFIG_CAPS);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Cvcam3Stream::GetStreamCaps(int iIndex, AM_MEDIA_TYPE **pmt, BYTE *pSCC)
{
*pmt = CreateMediaType(&m_mt);
DECLARE_PTR(VIDEOINFOHEADER, pvi, (*pmt)->pbFormat);
if (iIndex == 0) iIndex = 4;
pvi->bmiHeader.biCompression = BI_RGB;
pvi->bmiHeader.biBitCount = 32;
pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pvi->bmiHeader.biWidth =width1;//width1 * iIndex;
pvi->bmiHeader.biHeight =height1; //height1 * iIndex;
pvi->bmiHeader.biPlanes = 1;
pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
pvi->bmiHeader.biClrImportant = 0;
SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
(*pmt)->majortype = MEDIATYPE_Video;
(*pmt)->subtype = MEDIASUBTYPE_RGB32;
(*pmt)->formattype = FORMAT_VideoInfo;
(*pmt)->bTemporalCompression = FALSE;
(*pmt)->bFixedSizeSamples= FALSE;
(*pmt)->lSampleSize = pvi->bmiHeader.biSizeImage;
(*pmt)->cbFormat = sizeof(VIDEOINFOHEADER);
DECLARE_PTR(VIDEO_STREAM_CONFIG_CAPS, pvscc, pSCC);
pvscc->guid = FORMAT_VideoInfo;
pvscc->VideoStandard = AnalogVideo_None;
pvscc->InputSize.cx = width1;
pvscc->InputSize.cy = height1;
pvscc->MinCroppingSize.cx = width1;
pvscc->MinCroppingSize.cy = height1;
pvscc->MaxCroppingSize.cx = width1;
pvscc->MaxCroppingSize.cy = height1;
pvscc->CropGranularityX = 1;
pvscc->CropGranularityY = 1;
pvscc->CropAlignX = 1;
pvscc->CropAlignY = 1;
pvscc->MinOutputSize.cx = width1;
pvscc->MinOutputSize.cy = height1;
pvscc->MaxOutputSize.cx = width1;
pvscc->MaxOutputSize.cy = height1;
pvscc->OutputGranularityX = 1;
pvscc->OutputGranularityY = 1;
pvscc->StretchTapsX = 0;
pvscc->StretchTapsY = 0;
pvscc->ShrinkTapsX = 0;
pvscc->ShrinkTapsY = 0;
pvscc->MinFrameInterval = 200000; //50 fps
pvscc->MaxFrameInterval = 50000000; // 0.2 fps
pvscc->MinBitsPerSecond = (80 * 60 * 3 * 8) / 5;
pvscc->MaxBitsPerSecond = width1 * height1 * 3 * 8 * 50;
SetFormat(*pmt);
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
// IKsPropertySet
//////////////////////////////////////////////////////////////////////////
HRESULT Cvcam3Stream::Set(REFGUID guidPropSet, DWORD dwID, void *pInstanceData,
DWORD cbInstanceData, void *pPropData, DWORD cbPropData)
{// Set: Cannot set any properties.
return E_NOTIMPL;
}
// Get: Return the pin category (our only property).
HRESULT Cvcam3Stream::Get(
REFGUID guidPropSet, // Which property set.
DWORD dwPropID, // Which property in that set.
void *pInstanceData, // Instance data (ignore).
DWORD cbInstanceData, // Size of the instance data (ignore).
void *pPropData, // Buffer to receive the property data.
DWORD cbPropData, // Size of the buffer.
DWORD *pcbReturned // Return the size of the property.
)
{
if (guidPropSet != AMPROPSETID_Pin) return E_PROP_SET_UNSUPPORTED;
if (dwPropID != AMPROPERTY_PIN_CATEGORY) return E_PROP_ID_UNSUPPORTED;
if (pPropData == NULL && pcbReturned == NULL) return E_POINTER;
if (pcbReturned) *pcbReturned = sizeof(GUID);
if (pPropData == NULL) return S_OK; // Caller just wants to know the size.
if (cbPropData < sizeof(GUID)) return E_UNEXPECTED;// The buffer is too small.
*(GUID *)pPropData = PIN_CATEGORY_CAPTURE;
return S_OK;
}
// QuerySupported: Query whether the pin supports the specified property.
HRESULT Cvcam3Stream::QuerySupported(REFGUID guidPropSet, DWORD dwPropID, DWORD *pTypeSupport)
{
if (guidPropSet != AMPROPSETID_Pin) return E_PROP_SET_UNSUPPORTED;
if (dwPropID != AMPROPERTY_PIN_CATEGORY) return E_PROP_ID_UNSUPPORTED;
// We support getting this property, but not setting it.
if (pTypeSupport) *pTypeSupport = KSPROPERTY_SUPPORT_GET;
return S_OK;
}
void cleanrefs()
{
if (lAcquiredDesktopImage)
{
lAcquiredDesktopImage->Release();
lAcquiredDesktopImage = NULL;
}
if (lDesktopResource) lDesktopResource.Release();
if (lDeskDupl)
{lDeskDupl->Release();
lDeskDupl=nullptr;
}
if(lGDIImage) //inutil????
{lGDIImage->Release();
lGDIImage=nullptr;
}
if (lDestImage)
{lDestImage->Release();
lDestImage=nullptr;
}
//if (sptr)
//{delete[] sptr; sptr=nullptr;}
}