forked from Jack0r/OBS-DShowAudioPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDShowAudioPlugin.cpp
1329 lines (1060 loc) · 46.1 KB
/
DShowAudioPlugin.cpp
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
/********************************************************************************
Copyright (C) 2012 Hugh Bailey <obs.jim@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
#include "DShowAudioPlugin.h"
#include <ks.h>
//todo: 1700 line file. this is another one of those abominations.
//fix it jim
extern "C" __declspec(dllexport) bool LoadPlugin();
extern "C" __declspec(dllexport) void UnloadPlugin();
extern "C" __declspec(dllexport) CTSTR GetPluginName();
extern "C" __declspec(dllexport) CTSTR GetPluginDescription();
LocaleStringLookup *pluginLocale = NULL;
HINSTANCE hinstMain = NULL;
#define DSHOW_CLASSNAME TEXT("AudioSourceDShowCapture")
bool SourceListHasDevice(CTSTR lpDevice, XElement *sourceList)
{
UINT numSources = sourceList->NumElements();
for(UINT i=0; i<numSources; i++)
{
XElement *sourceElement = sourceList->GetElementByID(i);
if(scmpi(sourceElement->GetString(TEXT("class")), DSHOW_CLASSNAME) == 0)
{
XElement *data = sourceElement->GetElement(TEXT("data"));
if(scmpi(data->GetString(TEXT("device")), lpDevice) == 0)
return true;
if(scmpi(data->GetString(TEXT("audioDevice")), lpDevice) == 0)
return true;
}
}
return false;
}
bool CurrentDeviceExists(CTSTR lpDevice, bool bGlobal, bool &isGlobal)
{
isGlobal = false;
XElement *globalSources = API->GetGlobalSourceListElement();
if(globalSources)
{
if(SourceListHasDevice(lpDevice, globalSources))
{
isGlobal = true;
return true;
}
}
if(bGlobal)
{
XElement *sceneListElement = API->GetSceneListElement();
if(sceneListElement)
{
UINT numScenes = sceneListElement->NumElements();
for(UINT i=0; i<numScenes; i++)
{
XElement *sceneElement = sceneListElement->GetElementByID(i);
if(sceneElement)
{
XElement *sourceListElement = sceneElement->GetElement(TEXT("sources"));
if(sourceListElement)
{
if(SourceListHasDevice(lpDevice, sourceListElement))
return true;
}
}
}
}
}
else
{
XElement *sceneElement = API->GetSceneElement();
if(sceneElement)
{
XElement *sourceListElement = sceneElement->GetElement(TEXT("sources"));
if(sourceListElement)
{
if(SourceListHasDevice(lpDevice, sourceListElement))
return true;
}
}
}
return false;
}
IBaseFilter* GetExceptionDevice(CTSTR lpGUID)
{
String strGUID = lpGUID;
if(strGUID.Length() != 38)
return NULL;
strGUID = strGUID.Mid(1, strGUID.Length()-1);
StringList GUIDData;
strGUID.GetTokenList(GUIDData, '-', FALSE);
if (GUIDData.Num() != 5)
return NULL;
if (GUIDData[0].Length() != 8 ||
GUIDData[1].Length() != 4 ||
GUIDData[2].Length() != 4 ||
GUIDData[3].Length() != 4 ||
GUIDData[4].Length() != 12 )
{
return NULL;
}
GUID targetGUID;
targetGUID.Data1 = (UINT)tstring_base_to_uint(GUIDData[0], NULL, 16);
targetGUID.Data2 = (WORD)tstring_base_to_uint(GUIDData[1], NULL, 16);
targetGUID.Data3 = (WORD)tstring_base_to_uint(GUIDData[2], NULL, 16);
targetGUID.Data4[0] = (BYTE)tstring_base_to_uint(GUIDData[3].Left(2), NULL, 16);
targetGUID.Data4[1] = (BYTE)tstring_base_to_uint(GUIDData[3].Right(2), NULL, 16);
targetGUID.Data4[2] = (BYTE)tstring_base_to_uint(GUIDData[4].Left(2), NULL, 16);
targetGUID.Data4[3] = (BYTE)tstring_base_to_uint(GUIDData[4].Mid(2, 4), NULL, 16);
targetGUID.Data4[4] = (BYTE)tstring_base_to_uint(GUIDData[4].Mid(4, 6), NULL, 16);
targetGUID.Data4[5] = (BYTE)tstring_base_to_uint(GUIDData[4].Mid(6, 8), NULL, 16);
targetGUID.Data4[6] = (BYTE)tstring_base_to_uint(GUIDData[4].Mid(8, 10), NULL, 16);
targetGUID.Data4[7] = (BYTE)tstring_base_to_uint(GUIDData[4].Right(2), NULL, 16);
IBaseFilter *filter;
if(SUCCEEDED(CoCreateInstance(targetGUID, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&filter)))
return filter;
return NULL;
}
IBaseFilter* GetDeviceByValue(const IID &enumType, WSTR lpType, CTSTR lpName, WSTR lpType2, CTSTR lpName2)
{
//---------------------------------
// exception devices
if(scmpi(lpType2, L"DevicePath") == 0 && lpName2 && *lpName2 == '{')
return GetExceptionDevice(lpName2);
//---------------------------------
ICreateDevEnum *deviceEnum;
IEnumMoniker *videoDeviceEnum;
HRESULT err;
err = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void**)&deviceEnum);
if(FAILED(err))
{
AppWarning(TEXT("GetDeviceByName: CoCreateInstance for the device enum failed, result = %08lX"), err);
return NULL;
}
err = deviceEnum->CreateClassEnumerator(enumType, &videoDeviceEnum, 0);
if(FAILED(err))
{
AppWarning(TEXT("GetDeviceByName: deviceEnum->CreateClassEnumerator failed, result = %08lX"), err);
deviceEnum->Release();
return NULL;
}
SafeRelease(deviceEnum);
if(err == S_FALSE) //no devices, so NO ENUM FO U
return NULL;
//---------------------------------
IBaseFilter *bestFilter = NULL;
IMoniker *deviceInfo;
DWORD count;
while(videoDeviceEnum->Next(1, &deviceInfo, &count) == S_OK)
{
IPropertyBag *propertyData;
err = deviceInfo->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyData);
if(SUCCEEDED(err))
{
VARIANT valueThingy;
VARIANT valueThingy2;
VariantInit(&valueThingy);
VariantInit(&valueThingy2);
/*valueThingy.vt = VT_BSTR;
valueThingy.pbstrVal = NULL;
valueThingy2.vt = VT_BSTR;
valueThingy2.bstrVal = NULL;*/
if(SUCCEEDED(propertyData->Read(lpType, &valueThingy, NULL)))
{
if(lpType2 && lpName2)
{
if(FAILED(propertyData->Read(lpType2, &valueThingy2, NULL)))
nop();
}
SafeRelease(propertyData);
String strVal1 = (CWSTR)valueThingy.bstrVal;
if(strVal1 == lpName)
{
IBaseFilter *filter;
err = deviceInfo->BindToObject(NULL, 0, IID_IBaseFilter, (void**)&filter);
if(FAILED(err))
{
AppWarning(TEXT("GetDeviceByName: deviceInfo->BindToObject failed, result = %08lX"), err);
continue;
}
if(!bestFilter)
{
bestFilter = filter;
if(!lpType2 || !lpName2)
{
SafeRelease(deviceInfo);
SafeRelease(videoDeviceEnum);
return bestFilter;
}
}
else if(lpType2 && lpName2)
{
String strVal2 = (CWSTR)valueThingy2.bstrVal;
if(strVal2 == lpName2)
{
bestFilter->Release();
bestFilter = filter;
SafeRelease(deviceInfo);
SafeRelease(videoDeviceEnum);
return bestFilter;
}
}
else
filter->Release();
}
}
}
SafeRelease(deviceInfo);
}
SafeRelease(videoDeviceEnum);
return bestFilter;
}
IPin* GetOutputPin(IBaseFilter *filter, const GUID *majorType)
{
IPin *foundPin = NULL;
IEnumPins *pins;
if(!filter) return NULL;
if(FAILED(filter->EnumPins(&pins))) return NULL;
IPin *curPin;
ULONG num;
while(pins->Next(1, &curPin, &num) == S_OK)
{
if(majorType)
{
AM_MEDIA_TYPE *pinMediaType;
IEnumMediaTypes *mediaTypesEnum;
if(FAILED(curPin->EnumMediaTypes(&mediaTypesEnum)))
{
SafeRelease(curPin);
continue;
}
ULONG curVal = 0;
HRESULT hRes = mediaTypesEnum->Next(1, &pinMediaType, &curVal);
mediaTypesEnum->Release();
if(hRes != S_OK)
{
SafeRelease(curPin);
continue;
}
BOOL bDesiredMediaType = (pinMediaType->majortype == *majorType);
DeleteMediaType(pinMediaType);
if(!bDesiredMediaType)
{
SafeRelease(curPin);
continue;
}
}
//------------------------------
PIN_DIRECTION pinDir;
if(SUCCEEDED(curPin->QueryDirection(&pinDir)))
{
if(pinDir == PINDIR_OUTPUT)
{
IKsPropertySet *propertySet;
if(SUCCEEDED(curPin->QueryInterface(IID_IKsPropertySet, (void**)&propertySet)))
{
GUID pinCategory;
DWORD retSize;
PIN_INFO chi;
curPin->QueryPinInfo(&chi);
if(chi.pFilter)
chi.pFilter->Release();
if(SUCCEEDED(propertySet->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0, &pinCategory, sizeof(GUID), &retSize)))
{
if(pinCategory == PIN_CATEGORY_CAPTURE)
{
SafeRelease(propertySet);
SafeRelease(pins);
return curPin;
}
}
SafeRelease(propertySet);
}
}
}
SafeRelease(curPin);
}
SafeRelease(pins);
return foundPin;
}
void AddOutput(AM_MEDIA_TYPE *pMT, BYTE *capsData, bool bAllowV2, List<MediaOutputInfo> &outputInfoList)
{
VideoOutputType type = GetVideoOutputType(*pMT);
if(pMT->formattype == FORMAT_VideoInfo || (bAllowV2 && pMT->formattype == FORMAT_VideoInfo2))
{
VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(capsData);
VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(pMT->pbFormat);
BITMAPINFOHEADER *bmiHeader = GetVideoBMIHeader(pMT);
bool bUsingFourCC = false;
if(type == VideoOutputType_None)
{
type = GetVideoOutputTypeFromFourCC(bmiHeader->biCompression);
bUsingFourCC = true;
}
if(type != VideoOutputType_None)
{
MediaOutputInfo *outputInfo = outputInfoList.CreateNew();
if(pVSCC)
{
outputInfo->minFrameInterval = pVSCC->MinFrameInterval;
outputInfo->maxFrameInterval = pVSCC->MaxFrameInterval;
outputInfo->minCX = pVSCC->MinOutputSize.cx;
outputInfo->maxCX = pVSCC->MaxOutputSize.cx;
outputInfo->minCY = pVSCC->MinOutputSize.cy;
outputInfo->maxCY = pVSCC->MaxOutputSize.cy;
//actually due to the other code in GetResolutionFPSInfo, we can have this granularity
// back to the way it was. now, even if it's corrupted, it will always work
outputInfo->xGranularity = max(pVSCC->OutputGranularityX, 1);
outputInfo->yGranularity = max(pVSCC->OutputGranularityY, 1);
}
else
{
outputInfo->minCX = outputInfo->maxCX = bmiHeader->biWidth;
outputInfo->minCY = outputInfo->maxCY = bmiHeader->biHeight;
if(pVih->AvgTimePerFrame != 0)
outputInfo->minFrameInterval = outputInfo->maxFrameInterval = pVih->AvgTimePerFrame;
else
outputInfo->minFrameInterval = outputInfo->maxFrameInterval = 10000000/30; //elgato hack
outputInfo->xGranularity = outputInfo->yGranularity = 1;
}
outputInfo->mediaType = pMT;
outputInfo->videoType = type;
outputInfo->bUsingFourCC = bUsingFourCC;
return;
}
}
DeleteMediaType(pMT);
}
void GetOutputList(IPin *curPin, List<MediaOutputInfo> &outputInfoList)
{
HRESULT hRes;
IAMStreamConfig *config;
if(SUCCEEDED(curPin->QueryInterface(IID_IAMStreamConfig, (void**)&config)))
{
int count, size;
if(SUCCEEDED(hRes = config->GetNumberOfCapabilities(&count, &size)))
{
BYTE *capsData = (BYTE*)Allocate(size);
int priority = -1;
for(int i=0; i<count; i++)
{
AM_MEDIA_TYPE *pMT;
if(SUCCEEDED(config->GetStreamCaps(i, &pMT, capsData)))
AddOutput(pMT, capsData, false, outputInfoList);
}
Free(capsData);
}
else if(hRes == E_NOTIMPL) //...usually elgato.
{
IEnumMediaTypes *mediaTypes;
if(SUCCEEDED(curPin->EnumMediaTypes(&mediaTypes)))
{
ULONG i;
AM_MEDIA_TYPE *pMT;
if(mediaTypes->Next(1, &pMT, &i) == S_OK)
AddOutput(pMT, NULL, true, outputInfoList);
mediaTypes->Release();
}
}
SafeRelease(config);
}
}
inline bool ResolutionListHasValue(const List<SIZE> &resolutions, SIZE &size)
{
bool bHasResolution = false;
for(UINT i=0; i<resolutions.Num(); i++)
{
SIZE &testSize = resolutions[i];
if(size.cx == testSize.cx && size.cy == testSize.cy)
{
bHasResolution = true;
break;
}
}
return bHasResolution;
}
struct FPSInterval
{
inline FPSInterval(UINT64 minVal, UINT64 maxVal) : minFrameInterval(minVal), maxFrameInterval(maxVal) {}
UINT64 minFrameInterval, maxFrameInterval;
};
struct FPSInfo
{
List<FPSInterval> supportedIntervals;
};
bool GetClosestResolution(List<MediaOutputInfo> &outputList, SIZE &resolution, UINT64 &frameInterval)
{
LONG width, height;
UINT64 internalFrameInterval = 10000000/UINT64(API->GetMaxFPS());
API->GetBaseSize((UINT&)width, (UINT&)height);
LONG bestDistance = 0x7FFFFFFF;
SIZE bestSize;
UINT64 maxFrameInterval = 0;
UINT64 bestFrameInterval = 0xFFFFFFFFFFFFFFFFLL;
for(UINT i=0; i<outputList.Num(); i++)
{
MediaOutputInfo &outputInfo = outputList[i];
LONG outputWidth = outputInfo.minCX;
do
{
LONG distWidth = width-outputWidth;
if(distWidth < 0)
break;
if(distWidth > bestDistance)
{
outputWidth += outputInfo.xGranularity;
continue;
}
LONG outputHeight = outputInfo.minCY;
do
{
LONG distHeight = height-outputHeight;
if(distHeight < 0)
break;
LONG totalDist = distHeight+distWidth;
if((totalDist <= bestDistance) || (totalDist == bestDistance && outputInfo.minFrameInterval < bestFrameInterval))
{
bestDistance = totalDist;
bestSize.cx = outputWidth;
bestSize.cy = outputHeight;
maxFrameInterval = outputInfo.maxFrameInterval;
bestFrameInterval = outputInfo.minFrameInterval;
}
outputHeight += outputInfo.yGranularity;
}while((UINT)outputHeight <= outputInfo.maxCY);
outputWidth += outputInfo.xGranularity;
}while((UINT)outputWidth <= outputInfo.maxCX);
}
if(bestDistance != 0x7FFFFFFF)
{
resolution.cx = bestSize.cx;
resolution.cy = bestSize.cy;
if(internalFrameInterval > maxFrameInterval)
frameInterval = maxFrameInterval;
else if(internalFrameInterval < bestFrameInterval)
frameInterval = bestFrameInterval;
else
frameInterval = internalFrameInterval;
return true;
}
return false;
}
struct ConfigDialogData
{
CTSTR lpName;
XElement *data;
List<MediaOutputInfo> outputList;
List<SIZE> resolutions;
StringList deviceNameList;
StringList deviceIDList;
StringList audioNameList;
StringList audioIDList;
StringList audioGUID;
StringList crossbarList;
StringList crossbarIDList;
GUID listGUID[100];
bool bGlobalSource;
bool bCreating;
bool bDShowHasAudio, bForceCustomAudioDevice, bHasAudio;
~ConfigDialogData()
{
ClearOutputList();
}
void ClearOutputList()
{
for(UINT i=0; i<outputList.Num(); i++)
outputList[i].FreeData();
outputList.Clear();
}
void GetResolutions(List<SIZE> &resolutions)
{
resolutions.Clear();
for(UINT i=0; i<outputList.Num(); i++)
{
MediaOutputInfo &outputInfo = outputList[i];
SIZE size;
size.cx = outputInfo.minCX;
size.cy = outputInfo.minCY;
if(!ResolutionListHasValue(resolutions, size))
resolutions << size;
size.cx = outputInfo.maxCX;
size.cy = outputInfo.maxCY;
if(!ResolutionListHasValue(resolutions, size))
resolutions << size;
}
//sort
for(UINT i=0; i<resolutions.Num(); i++)
{
SIZE &rez = resolutions[i];
for(UINT j=i+1; j<resolutions.Num(); j++)
{
SIZE &testRez = resolutions[j];
if(testRez.cy < rez.cy)
{
resolutions.SwapValues(i, j);
j = i;
}
}
}
}
bool GetResolutionFPSInfo(SIZE &resolution, FPSInfo &fpsInfo)
{
fpsInfo.supportedIntervals.Clear();
for(UINT i=0; i<outputList.Num(); i++)
{
MediaOutputInfo &outputInfo = outputList[i];
if( UINT(resolution.cx) >= outputInfo.minCX && UINT(resolution.cx) <= outputInfo.maxCX &&
UINT(resolution.cy) >= outputInfo.minCY && UINT(resolution.cy) <= outputInfo.maxCY )
{
if((resolution.cx-outputInfo.minCX) % outputInfo.xGranularity || (resolution.cy-outputInfo.minCY) % outputInfo.yGranularity)
return false;
fpsInfo.supportedIntervals << FPSInterval(outputInfo.minFrameInterval, outputInfo.maxFrameInterval);
}
}
return fpsInfo.supportedIntervals.Num() != 0;
}
};
#define DEV_EXCEPTION_COUNT 1
CTSTR lpExceptionNames[DEV_EXCEPTION_COUNT] = {TEXT("Elgato Game Capture HD")};
CTSTR lpExceptionGUIDs[DEV_EXCEPTION_COUNT] = {TEXT("{39F50F4C-99E1-464a-B6F9-D605B4FB5918}")};
String guidToString(GUID guid) {
return FormattedString(TEXT("{%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X}"), guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
}
void FillOutListOfDevices(HWND hwndCombo, GUID matchGUID, StringList *deviceList, StringList *deviceIDList, StringList *GUIDList, GUID *GUID_dump)
{
//deviceIDList->Clear();
//deviceList->Clear();
//if(hwndCombo != NULL) SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
//------------------------------------------
for(int i=0; i<DEV_EXCEPTION_COUNT; i++)
{
IBaseFilter *exceptionFilter = GetExceptionDevice(lpExceptionGUIDs[i]);
if(exceptionFilter)
{
deviceList->Add(lpExceptionNames[i]);
deviceIDList->Add(lpExceptionGUIDs[i]);
if(hwndCombo != NULL) SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)lpExceptionNames[i]);
exceptionFilter->Release();
}
}
//------------------------------------------
ICreateDevEnum *deviceEnum;
IEnumMoniker *videoDeviceEnum;
HRESULT err;
err = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void**)&deviceEnum);
if(FAILED(err))
{
AppWarning(TEXT("FillOutListDevices: CoCreateInstance for the device enum failed, result = %08lX"), err);
return;
}
err = deviceEnum->CreateClassEnumerator(matchGUID, &videoDeviceEnum, 0);
if(FAILED(err))
{
AppWarning(TEXT("FillOutListDevices: deviceEnum->CreateClassEnumerator failed, result = %08lX"), err);
deviceEnum->Release();
return;
}
SafeRelease(deviceEnum);
if(err == S_FALSE) //no devices
return;
//------------------------------------------
IMoniker *deviceInfo;
DWORD count;
while(videoDeviceEnum->Next(1, &deviceInfo, &count) == S_OK)
{
IPropertyBag *propertyData;
err = deviceInfo->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyData);
if(SUCCEEDED(err))
{
VARIANT friendlyNameValue, devicePathValue;
friendlyNameValue.vt = VT_BSTR;
friendlyNameValue.bstrVal = NULL;
devicePathValue.vt = VT_BSTR;
devicePathValue.bstrVal = NULL;
err = propertyData->Read(L"FriendlyName", &friendlyNameValue, NULL);
propertyData->Read(L"DevicePath", &devicePathValue, NULL);
if(SUCCEEDED(err))
{
IBaseFilter *filter;
err = deviceInfo->BindToObject(NULL, 0, IID_IBaseFilter, (void**)&filter);
if(SUCCEEDED(err))
{
String strDeviceName = (CWSTR)friendlyNameValue.bstrVal;
deviceList->Add(strDeviceName);
if(GUIDList != NULL) {
if(matchGUID == CLSID_AudioInputDeviceCategory) GUIDList->Add("CLSID_AudioInputDeviceCategory");
else if(matchGUID == CLSID_VideoInputDeviceCategory) GUIDList->Add("CLSID_VideoInputDeviceCategory");
else if(matchGUID == CLSID_AudioRendererCategory) GUIDList->Add("CLSID_AudioRendererCategory");
else GUIDList->Add("Unknown device category");
GUID_dump[deviceList->Num()] = matchGUID;
}
UINT count = 0;
UINT id = INVALID;
while((id = deviceList->FindNextValueIndexI(strDeviceName, id)) != INVALID) count++;
if(count > 1)
strDeviceName << TEXT(" (") << UIntString(count) << TEXT(")");
String strDeviceID = (CWSTR)devicePathValue.bstrVal;
if(hwndCombo != NULL) SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)strDeviceName.Array());
deviceIDList->Add(strDeviceID);
SafeRelease(filter);
}
}
SafeRelease(propertyData);
}
SafeRelease(deviceInfo);
}
SafeRelease(videoDeviceEnum);
}
bool GetResolution(HWND hwndResolution, SIZE &resolution, BOOL bSelChange)
{
String strResolution;
if(bSelChange)
strResolution = GetCBText(hwndResolution);
else
strResolution = GetEditText(hwndResolution);
if(strResolution.NumTokens('x') != 2)
return false;
String strCX = strResolution.GetToken(0, 'x');
String strCY = strResolution.GetToken(1, 'x');
if(strCX.IsEmpty() || strCX.IsEmpty() || !ValidIntString(strCX) || !ValidIntString(strCY))
return false;
UINT cx = strCX.ToInt();
UINT cy = strCY.ToInt();
if(cx < 32 || cy < 32 || cx > 4096 || cy > 4096)
return false;
resolution.cx = cx;
resolution.cy = cy;
return true;
}
struct ColorSelectionData
{
HDC hdcDesktop;
HDC hdcDestination;
HBITMAP hBitmap;
bool bValid;
inline ColorSelectionData() : hdcDesktop(NULL), hdcDestination(NULL), hBitmap(NULL), bValid(false) {}
inline ~ColorSelectionData() {Clear();}
inline bool Init()
{
hdcDesktop = GetDC(NULL);
if(!hdcDesktop)
return false;
hdcDestination = CreateCompatibleDC(hdcDesktop);
if(!hdcDestination)
return false;
hBitmap = CreateCompatibleBitmap(hdcDesktop, 1, 1);
if(!hBitmap)
return false;
SelectObject(hdcDestination, hBitmap);
bValid = true;
return true;
}
inline void Clear()
{
if(hdcDesktop)
{
ReleaseDC(NULL, hdcDesktop);
hdcDesktop = NULL;
}
if(hdcDestination)
{
DeleteDC(hdcDestination);
hdcDestination = NULL;
}
if(hBitmap)
{
DeleteObject(hBitmap);
hBitmap = NULL;
}
bValid = false;
}
inline DWORD GetColor()
{
POINT p;
if(GetCursorPos(&p))
{
BITMAPINFO data;
zero(&data, sizeof(data));
data.bmiHeader.biSize = sizeof(data.bmiHeader);
data.bmiHeader.biWidth = 1;
data.bmiHeader.biHeight = 1;
data.bmiHeader.biPlanes = 1;
data.bmiHeader.biBitCount = 24;
data.bmiHeader.biCompression = BI_RGB;
data.bmiHeader.biSizeImage = 4;
if(BitBlt(hdcDestination, 0, 0, 1, 1, hdcDesktop, p.x, p.y, SRCCOPY|CAPTUREBLT))
{
DWORD buffer;
if(GetDIBits(hdcDestination, hBitmap, 0, 1, &buffer, &data, DIB_RGB_COLORS))
return 0xFF000000|buffer;
}
else
{
int err = GetLastError();
nop();
}
}
return 0xFF000000;
}
};
void OpenPropertyPages(HWND hwnd, String devicename, String deviceid, GUID matchGUID) {
IBaseFilter *filter = GetDeviceByValue(matchGUID,
L"FriendlyName", devicename,
L"DevicePath", deviceid);
if(filter)
{
ISpecifyPropertyPages *propPages;
CAUUID cauuid;
if(SUCCEEDED(filter->QueryInterface(IID_ISpecifyPropertyPages, (void**)&propPages)))
{
if(SUCCEEDED(propPages->GetPages(&cauuid)))
{
if(cauuid.cElems)
{
OleCreatePropertyFrame(hwnd, 0, 0, NULL, 1, (LPUNKNOWN*)&filter, cauuid.cElems, cauuid.pElems, 0, 0, NULL);
CoTaskMemFree(cauuid.pElems);
}
}
propPages->Release();
}
filter->Release();
}
return;
}
INT_PTR CALLBACK ConfigureDialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool bSelectingColor = false;
static bool bMouseDown = false;
static ColorSelectionData colorData;
switch(message)
{
case WM_INITDIALOG:
{
SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
ConfigDialogData *configData = (ConfigDialogData*)lParam;
HWND hwndAudioList = GetDlgItem(hwnd, IDC_AUDIOLIST);
HWND hwndCrossbarlist = GetDlgItem(hwnd, IDC_CROSSBARLIST);
//------------------------------------------
configData->bDShowHasAudio = configData->data->GetInt(TEXT("dshowHasAudio")) != 0;
//------------------------------------------
String strAudioDevice = configData->data->GetString(TEXT("audioDevice"));
LocalizeWindow(hwnd, pluginLocale);
SendMessage(GetDlgItem(hwnd, IDC_AUDIOLIST), CB_RESETCONTENT, 0, 0);
configData->audioIDList.Clear();
configData->audioNameList.Clear();
FillOutListOfDevices(GetDlgItem(hwnd, IDC_AUDIOLIST), CLSID_AudioInputDeviceCategory, &configData->audioNameList, &configData->audioIDList, &configData->audioGUID, configData->listGUID);
FillOutListOfDevices(GetDlgItem(hwnd, IDC_AUDIOLIST), CLSID_VideoInputDeviceCategory, &configData->audioNameList, &configData->audioIDList, &configData->audioGUID, configData->listGUID);
FillOutListOfDevices(GetDlgItem(hwnd, IDC_AUDIOLIST), CLSID_AudioRendererCategory, &configData->audioNameList, &configData->audioIDList, &configData->audioGUID, configData->listGUID);
//FillOutListOfDevices(GetDlgItem(hwnd, IDC_AUDIOLIST), KSCATEGORY_AUDIO, &configData->audioNameList, &configData->audioIDList, &configData->audioGUID, configData->listGUID);
//FillOutListOfDevices(GetDlgItem(hwnd, IDC_CROSSBARLIST), AM_KSCATEGORY_CROSSBAR, &configData->crossbarList, &configData->crossbarIDList, NULL, NULL);
//SendMessage(hwndCrossbarlist, CB_SETCURSEL, 0, 0);
//ConfigureDialogProc(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CROSSBARLIST, CBN_SELCHANGE), (LPARAM)hwndCrossbarlist);
UINT audioDeviceID = CB_ERR;
if(strAudioDevice.IsValid())
audioDeviceID = (UINT)SendMessage(hwndAudioList, CB_FINDSTRINGEXACT, -1, (LPARAM)strAudioDevice.Array());
if(audioDeviceID == CB_ERR)
{
SendMessage(hwndAudioList, CB_SETCURSEL, 0, 0);
ConfigureDialogProc(hwnd, WM_COMMAND, MAKEWPARAM(IDC_AUDIOLIST, CBN_SELCHANGE), (LPARAM)hwndAudioList);
}
else
{
SendMessage(hwndAudioList, CB_SETCURSEL, audioDeviceID, 0);
ConfigureDialogProc(hwnd, WM_COMMAND, MAKEWPARAM(IDC_AUDIOLIST, CBN_SELCHANGE), (LPARAM)hwndAudioList);
}
//------------------------------------------
HWND hwndTemp;
int soundOutputType = configData->data->GetInt(TEXT("soundOutputType"));
switch(soundOutputType)
{
case 0: hwndTemp = GetDlgItem(hwnd, IDC_NOSOUND); break;
case 1: hwndTemp = GetDlgItem(hwnd, IDC_OUTPUTSOUND); break;
case 2: hwndTemp = GetDlgItem(hwnd, IDC_PLAYDESKTOPSOUND); break;
}
EnableWindow(GetDlgItem(hwnd, IDC_AUDIOLIST), true);
SendMessage(hwndTemp, BM_SETCHECK, BST_CHECKED, 0);
EnableWindow(GetDlgItem(hwnd, IDC_TIMEOFFSET), soundOutputType == 1);
EnableWindow(GetDlgItem(hwnd, IDC_TIMEOFFSET_EDIT), soundOutputType == 1);
EnableWindow(GetDlgItem(hwnd, IDC_VOLUME), soundOutputType != 0);
//------------------------------------------
float fVol = configData->data->GetFloat(TEXT("volume"), 1.0f);
SetVolumeControlValue(GetDlgItem(hwnd, IDC_VOLUME), fVol);
//------------------------------------------
int pos = configData->data->GetInt(TEXT("soundTimeOffset"));
SendMessage(GetDlgItem(hwnd, IDC_TIMEOFFSET), UDM_SETRANGE32, -150, 10000);
SendMessage(GetDlgItem(hwnd, IDC_TIMEOFFSET), UDM_SETPOS32, 0, pos);
//------------------------------------------
return TRUE;
}
case WM_DESTROY:
break;