forked from thedeemon/gep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
History.cs
1686 lines (1543 loc) · 85.5 KB
/
History.cs
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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Microsoft.Win32;
using DirectShowLib;
using System.Reflection;
namespace gep
{
abstract class HistoryItem
{
public abstract string Define(CodeGenBase cg);
public abstract string Build(CodeGenBase cg, Graph graph);
public string var, clsname, RealName, Name;
public string srcFileName, dstFileName;
}
abstract class CodeGenBase
{
public abstract string DefineAddFilterDS(HIAddFilterDS hi);
public abstract string DefineAddFilterDMO(HIAddFilterDMO hi);
public abstract string DefineSetFormat(HISetFormat hi);
public abstract string DefineConnect(HIConnect hi);
public abstract string BuildAddFilterDS(HIAddFilterDS hi, Graph graph);
public abstract string BuildAddFilterOther(HIAddFilterOther hi, Graph graph);
public abstract string SetFormat(HISetFormat hi);
protected abstract string SetSampleGrabberMediaType(AMMediaType mt, HistoryItem hi);
public string Connect(HIConnect hi)
{
HistoryItem h1 = history.FindByRealName(hi.filter1);
HistoryItem h2 = history.FindByRealName(hi.filter2);
string var1 = (h1 != null) ? h1.var : "?";
string var2 = (h2 != null) ? h2.var : "?";
string pair = h1.Name + " and " + h2.Name;
return directConnect ? connectDirectTpl.GenerateWith(new string[] {
"$pair", pair, "$majortype", hi.majortype, "$var1", var1, "$var2", var2, "$pin1", hi.pin1, "$pin2", hi.pin2
})
: connectTpl.GenerateWith(new string[] {
"$pair", pair, "$majortype", hi.majortype, "$var1", var1, "$var2", var2
});
}
public string DefineAddFilterOther(HIAddFilterOther hi)
{
return defineDsTpl.GenerateWith(new string[] {
"$clsname", hi.CatVar, "$guid", hi.CatGuid, "$file", "", "$xguid", xguid(hi.CatGuid)
});
}
public string BuildAddFilterDMO(HIAddFilterDMO hi)
{
return addFiltDMOTpl.GenerateWith(new string[] {
"$name", hi.Name, "$var", hi.var, "$clsname", hi.clsname, "$dmocat", hi.clsname + "_cat"
}) + Insert(hi, null);
}
public abstract string GenCode(bool useDirectConnect, Graph graph);
public bool needCreateFilterProc = false, needGetPin = false, directConnect = true, createFiltersByName = true;
public History History { set { history = value; } }
static string snipversion = "1.5.0";
public CodeSnippet[] snippets;
public void SaveTemplates()
{
if (snippets == null) return;
string keyname = Program.mainform.keyname;
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyname, true);
if (rk == null)
rk = Registry.CurrentUser.CreateSubKey(keyname);
if (rk != null)
{
string lang = ToString()+".";
foreach(CodeSnippet snp in snippets)
rk.SetValue(lang + snp.Codename, snp.Text);
rk.SetValue("snipversion", snipversion);
}
rk.Close();
}
public void LoadTemplates()
{
string keyname = Program.mainform.keyname;
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyname);
if (rk == null)
return;
string lang = ToString() + ".";
var v = (string)rk.GetValue("snipversion");
if (v == null || v != snipversion) return; // this is an old version, do not load
foreach (CodeSnippet snp in snippets)
{
string s = (string)rk.GetValue(lang + snp.Codename);
if (s != null) snp.Text = s;
}
rk.Close();
}
protected History history;
protected Dictionary<string, string> known = new Dictionary<string, string>(); // guid => CLSID_Shit
protected List<string> srcFileNames = new List<string>();
protected List<string> dstFileNames = new List<string>();
protected CodeSnippet insertTpl, setSrcFileTpl, setDstFileTpl, connectTpl, connectDirectTpl, defineDsTpl, addFiltDMOTpl;
protected string Insert(HistoryItem hi, Graph graph)
{
string ins = insertTpl.GenerateWith(new string[] {
"$var", hi.var, "$name", hi.Name
});
string setfname = "";
if (hi.srcFileName != null)
{
srcFileNames.Add(hi.srcFileName);
int n = srcFileNames.Count;
setfname = setSrcFileTpl.GenerateWith(new string[] {
"$srcvar", hi.var + "_src", "$var", hi.var, "$filename", "srcFile" + n
});
}
if (hi.dstFileName != null)
{
dstFileNames.Add(hi.dstFileName);
int n = dstFileNames.Count;
setfname = setDstFileTpl.GenerateWith(new string[] {
"$dstvar", hi.var + "_sink", "$var", hi.var, "$filename", "dstFile" + n
});
}
string setmtype = "";
if (hi.RealName != null && graph != null)
{
AMMediaType mt = graph.SampleGrabberMediaType(hi.RealName);
if (mt != null)
{
setmtype = SetSampleGrabberMediaType(mt, hi);
DsUtils.FreeAMMediaType(mt);
}
}
return ins + setfname + setmtype + "\r\n";
}
protected static string xguid(string guid)
{
StringBuilder sb = new StringBuilder();
sb.Append("0x"); sb.Append(guid.Substring(1, 8));
sb.Append(", 0x"); sb.Append(guid.Substring(10, 4));
sb.Append(", 0x"); sb.Append(guid.Substring(15, 4));
sb.Append(", 0x"); sb.Append(guid.Substring(20, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(22, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(25, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(27, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(29, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(31, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(33, 2));
sb.Append(", 0x"); sb.Append(guid.Substring(35, 2));
return sb.ToString();
}
public abstract string MediaTypeToString(Guid guid);
}
abstract class HIAddFilter : HistoryItem
{ }
class HIAddFilterDS : HIAddFilter
{
public string CLSID, FileName;
public HIAddFilterDS(string _Name, string _CLSID, string realName, string _FileName,
string SrcFileName, string DstFileName, History h)
{
Name = _Name; CLSID = _CLSID; RealName = realName; FileName = _FileName;
srcFileName = SrcFileName; dstFileName = DstFileName;
h.MakeVarName(_Name, out clsname, out var);
}
public override string Define(CodeGenBase cg)
{
return cg.DefineAddFilterDS(this);
}
public override string Build(CodeGenBase cg, Graph graph)
{
return cg.BuildAddFilterDS(this, graph);
}
}
class HIAddFilterOther : HIAddFilter
{
public string DisplayName, CatGuid, CatVar;
public HIAddFilterOther(string _Name, string _DisplayName, string realname, string catguid, string catname,
string SrcFileName, string DstFileName, History h)
{
Name = _Name; DisplayName = _DisplayName; RealName = realname;
srcFileName = SrcFileName; dstFileName = DstFileName; CatGuid = catguid;
h.MakeVarName(_Name, out clsname, out var);
string t;
h.MakeVarName(catname, out CatVar, out t);
}
public override string Define(CodeGenBase cg)
{
cg.needCreateFilterProc = true;
return cg.DefineAddFilterOther(this);
}
public override string Build(CodeGenBase cg, Graph graph)
{
return cg.BuildAddFilterOther(this, graph);
}
}
class HIAddFilterDMO : HIAddFilter
{
public string dmoClsid, dmoCat;
public HIAddFilterDMO(string _Name, string realname, string _dmoClsid, string catGuid, History h)
{
Name = _Name; RealName = realname;
dmoClsid = _dmoClsid;
dmoCat = catGuid;
h.MakeVarName(_Name, out clsname, out var);
}
public override string Define(CodeGenBase cg)
{
return cg.DefineAddFilterDMO(this);
}
public override string Build(CodeGenBase cg, Graph graph)
{
return cg.BuildAddFilterDMO(this);
}
}
class HIConnect : HistoryItem
{
public string filter1, filter2, pin1, pin2;
//public int weight = 1;
public string majortype;
public Guid majortypeguid;
public HIConnect(string _filter1, string _pin1, string _filter2, string _pin2, Guid type)
{
filter1 = _filter1; filter2 = _filter2; pin1 = _pin1; pin2 = _pin2;
majortypeguid = type;
}
public override string Define(CodeGenBase cg)
{
majortype = cg.MediaTypeToString(majortypeguid);
return cg.DefineConnect(this);
}
public override string Build(CodeGenBase cg, Graph graph)
{
return cg.Connect(this);
}
}
class HISetFormat : HistoryItem
{
public AMMediaType mt;
public string filter, pin;
public HISetFormat(string _filter, string _pin, AMMediaType _mt, History h)
{
filter = _filter; pin = _pin; mt = _mt;
string dummy;
h.MakeVarName("mt", out dummy, out var);
}
public override string Define(CodeGenBase cg)
{
cg.needGetPin = true;
return cg.DefineSetFormat(this);
}
public override string Build(CodeGenBase cg, Graph graph)
{
return cg.SetFormat(this);
}
}
class History
{
List<HistoryItem> history = new List<HistoryItem>();
List<TempHistoryItem> temp = new List<TempHistoryItem>();
public IEnumerable<HistoryItem> Items
{
get
{
foreach (HistoryItem i in history)
yield return i;
}
}
public HistoryItem FindByRealName(string realname) //finds a filter
{
foreach (HistoryItem i in history)
if (i.RealName == realname)
return i;
foreach (HistoryItem i in TempHistoryItem.hitems(temp))
if (i.RealName == realname)
return i;
return null;
}
public void AddFilter(FilterProps fp, string realname, string srcFileName, string dstFileName, Filter f)
{
if (fp.DisplayName.StartsWith("@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}")) //DirectShow filters
history.Add(new HIAddFilterDS(fp.FriendlyName, fp.CLSID, realname, fp.FileName, srcFileName, dstFileName, this));
else
{
HIAddFilterDMO hidmo = TryMakingDMO(fp, realname, f);
if (hidmo != null)
history.Add(hidmo);
else
history.Add(new HIAddFilterOther(fp.Name, fp.DisplayName, realname, fp.catguid, fp.CategoryName, srcFileName, dstFileName, this));
}
}
public void AddFilterIfNew(FilterProps fp, string realname, string srcfilename, string dstfilename, Filter f)
{
if (FindByRealName(realname) == null)
{
HIAddFilter hi = TryMakingDMO(fp, realname, f);
if (hi==null) hi = new HIAddFilterDS(fp.FriendlyName, fp.CLSID, realname, fp.FileName, srcfilename, dstfilename, this);
temp.Add(new THIAddFilter(hi, f));
}
}
HIAddFilterDMO TryMakingDMO(FilterProps fp, string realname, Filter f)
{
var guids = f.ReadDMOGuids();
if (guids.HasValue)
return new HIAddFilterDMO(fp.FriendlyName, realname,
Graph.GuidToString(guids.Value.fst), Graph.GuidToString(guids.Value.snd), this);
return null;
}
public void ConnectIfNew(Pin p1, Pin p2, PinConnection con)
{
AMMediaType mt = new AMMediaType();
p1.IPin.ConnectionMediaType(mt);
Guid type = mt.majorType;
DsUtils.FreeAMMediaType(mt);
if (!HasConnection(p1,p2, history) && !HasConnection(p1,p2, TempHistoryItem.hitems(temp)))
temp.Add(new THIConnect(
new HIConnect(p1.Filter.Name, p1.Name, p2.Filter.Name, p2.Name, type), con));
}
public void RemoveConnection(Pin p1, Pin p2)
{
int i = FindConnection(p1, p2, history);
if (i >= 0) history.RemoveAt(i);
}
static int FindConnection(Pin p1, Pin p2, List<HistoryItem> lst)
{
for (int i = 0; i < lst.Count; i++)
{
HistoryItem hi = lst[i];
HIConnect hc = hi as HIConnect;
if (hc != null && hc.filter1 == p1.Filter.Name && hc.filter2 == p2.Filter.Name
&& hc.pin1 == p1.Name && hc.pin2 == p2.Name)
return i;
}
return -1;
}
static bool HasConnection(Pin p1, Pin p2, IEnumerable<HistoryItem> lst)
{
foreach (HistoryItem hi in lst)
{
HIConnect hc = hi as HIConnect;
if (hc != null && hc.filter1 == p1.Filter.Name && hc.filter2 == p2.Filter.Name
&& hc.pin1 == p1.Name && hc.pin2 == p2.Name)
return true;
}
return false;
}
public void RemoveFilter(string realname) //deletes filter with given realname and all setformats on it
{
history.RemoveAll(delegate(HistoryItem hi)
{
if (hi is HISetFormat) return ((HISetFormat) hi).filter == realname;
return hi.RealName == realname;
});
}
Dictionary<string, int> vars = new Dictionary<string, int>();
public void MakeVarName(string fltname, out string clsname, out string var)
{
StringBuilder sb1 = new StringBuilder();
foreach (char c in fltname)
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
sb1.Append(c);
string s = sb1.ToString();
clsname = "CLSID_" + s;
var = "p" + s;
if (vars.ContainsKey(var))
{
int k = vars[var] + 1;
vars[var] = k;
var += k.ToString();
}
else
vars[var] = 1;
}
public void SetFormat(Pin pin, AMMediaType mt)
{
history.Add(new HISetFormat(pin.Filter.Name, pin.Name, mt, this));
}
public void CommitAdded() //sort items in temp and add to history
{
temp.ForEach(delegate(TempHistoryItem thi) { thi.CalcOrder(); });
temp.Sort();
history.AddRange(TempHistoryItem.hitems(temp));
temp.Clear();
}
}
class CodeGenCPP : CodeGenBase
{
CodeSnippet addFiltMonTpl, addFiltDsTpl, checkTpl, addFiltByNameTpl;
public CodeGenCPP()
{
InitGuidsTable();
defineDsTpl = new CodeSnippet("Define CLSID for a custom DirectShow filter", "defineDsTpl",
"// $guid\r\n" +
"DEFINE_GUID($clsname,\r\n$xguid); //$file\r\n\r\n",
"$clsname - name for CLSID value\r\n" +
"$guid - GUID digits\r\n" +
"$xguid - GUID as a sequence of 0x.. values\r\n" +
"$file - name of file containing the filter\r\n");
defineDsTpl.SetVars(new string[] {
"$clsname", "CLSID_DivXDecoderFilter",
"$guid", "{78766964-0000-0010-8000-00AA00389B71}",
"$xguid", "0x78766964, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71",
"$file", "divxdec.ax"
});
addFiltDsTpl = new CodeSnippet("Create a DirectShow filter", "addFiltDsTpl",
" //add $name\r\n" +
" CComPtr<IBaseFilter> $var;\r\n"+
" hr = $var.CoCreateInstance($clsname);\r\n"+
" CHECK_HR(hr, _T(\"Can't create $name\"));\r\n",
"$name - name of the filter\r\n" +
"$var - variable to hold IBaseFilter\r\n" +
"$clsname - name of CLSID value for this filter");
addFiltDsTpl.SetVars(new string[] {
"$name", "DV Splitter",
"$var", "pDVSplitter",
"$clsname", "CLSID_DVSplitter"
});
addFiltDMOTpl = new CodeSnippet("Create a DMO filter", "addFiltDMOTpl",
" //add $name\r\n" +
" CComPtr<IBaseFilter> $var;\r\n" +
" hr = $var.CoCreateInstance(CLSID_DMOWrapperFilter);\r\n" +
" CHECK_HR(hr, _T(\"Can't create DMO Wrapper\"));\r\n" +
" CComQIPtr<IDMOWrapperFilter, &IID_IDMOWrapperFilter> $var_wrapper($var);\r\n" +
" if (!$var_wrapper)\r\n" +
" CHECK_HR(E_NOINTERFACE, _T(\"Can't get IDMOWrapperFilter\"));\r\n" +
" hr = $var_wrapper->Init($clsname, $dmocat);\r\n" +
" CHECK_HR(hr, _T(\"DMO Wrapper Init failed\"));\r\n",
"$name - name of the filter\r\n" +
"$var - variable to hold IBaseFilter\r\n" +
"$clsname - name of CLSID value for this DMO\r\n" +
"$dmocat - name of CLSID value for DMO category");
addFiltDMOTpl.SetVars(new string[] {
"$name", "MP3 Decoder DMO",
"$var", "pMP3DecoderDMO",
"$clsname", "CLSID_MP3DecoderDMO",
"$dmocat", "DMOCATEGORY_AUDIO_DECODER"
});
insertTpl = new CodeSnippet("Insert filter to graph", "insertTpl",
" hr = pGraph->AddFilter($var, L\"$name\");\r\n" +
" CHECK_HR(hr, _T(\"Can't add $name to graph\"));\r\n",
"$var - variable holding IBaseFilter\r\n" +
"$name - name of the filter");
insertTpl.SetVars(new string[] {
"$name", "DivX Decoder Filter",
"$var", "pDivXDecoderFilter"
});
setSrcFileTpl = new CodeSnippet("Set source file to IFileSourceFilter", "setSrcFileTpl",
" //set source filename\r\n" +
" CComQIPtr<IFileSourceFilter, &IID_IFileSourceFilter> $srcvar($var);\r\n" +
" if (!$srcvar)\r\n" +
" CHECK_HR(E_NOINTERFACE, _T(\"Can't get IFileSourceFilter\"));\r\n" +
" hr = $srcvar->Load($filename, NULL);\r\n" +
" CHECK_HR(hr, _T(\"Can't load file\"));\r\n",
"$srcvar - variable to hold IFileSourceFilter\r\n" +
"$var - variable holding IBaseFilter\r\n" +
"$filename - variable holding name of file to open");
setSrcFileTpl.SetVars(new string[] {
"$srcvar", "pFileSourceAsync_src",
"$var", "pFileSourceAsync",
"$filename", "srcFile1"
});
setDstFileTpl = new CodeSnippet("Set destination file to IFileSinkFilter", "setDstFileTpl",
" //set destination filename\r\n" +
" CComQIPtr<IFileSinkFilter, &IID_IFileSinkFilter> $dstvar($var);\r\n" +
" if (!$dstvar)\r\n" +
" CHECK_HR(E_NOINTERFACE, _T(\"Can't get IFileSinkFilter\"));\r\n" +
" hr = $dstvar->SetFileName($filename, NULL);\r\n" +
" CHECK_HR(hr, _T(\"Can't set filename\"));\r\n",
"$dstvar - variable to hold IFileSinkFilter\r\n" +
"$var - variable holding IBaseFilter\r\n" +
"$filename - variable holding name of file to open");
setDstFileTpl.SetVars(new string[] {
"$dstvar", "pFilewriter_sink",
"$var", "pFilewriter",
"$filename", "dstFile1"
});
addFiltMonTpl = new CodeSnippet("Create a filter by display name", "addFiltMonTpl",
" //add $name\r\n" +
" CComPtr<IBaseFilter> $var = CreateFilter(L\"$displayname\");\r\n",
"$name - name of the filter\r\n" +
"$var - variable holding IBaseFilter\r\n" +
"$displayname - display name of the filter\r\n");
addFiltMonTpl.SetVars(new string[] {
"$name", "Fraps Video Decompressor",
"$var", "pFrapsVideoDecompressor",
"$displayname", @"@device:cm:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\fps1"
});
addFiltByNameTpl = new CodeSnippet("Create filter by its name and category", "addFiltByNameTpl",
" //add $name\r\n" +
" CComPtr<IBaseFilter> $var = CreateFilterByName(L\"$name\", $category);\r\n",
"$name - name of the filter\r\n" +
"$var - variable holding IBaseFilter\r\n" +
"$category - CLSID of this filter's category\r\n");
addFiltByNameTpl.SetVars(new string[] {
"$name", "Fraps Video Decompressor",
"$var", "pFrapsVideoDecompressor",
"$category", "CLSID_VideoCompressors"
});
connectTpl = new CodeSnippet("Connect two filters", "connectTpl",
" //connect $pair\r\n" +
" hr = pBuilder->RenderStream(NULL, &$majortype, $var1, NULL, $var2);\r\n" +
" CHECK_HR(hr, _T(\"Can't connect $pair\"));\r\n\r\n",
"$pair - names of connecting filters\r\n" +
"$majortype - major media type of connection\r\n" +
"$var1, $var2 - variables holding IBaseFilter of connecting filters");
connectTpl.SetVars(new string[] {
"$pair", "File Source (Async.) and AVI Splitter",
"$majortype", "MEDIATYPE_Stream",
"$var1", "pFileSourceAsync",
"$var2", "pAVISplitter"
});
connectDirectTpl = new CodeSnippet("Connect two filters directly", "connectDirectTpl",
" //connect $pair\r\n" +
" hr = pGraph->ConnectDirect(GetPin($var1, L\"$pin1\"), GetPin($var2, L\"$pin2\"), NULL);\r\n" +
" CHECK_HR(hr, _T(\"Can't connect $pair\"));\r\n\r\n",
"$pair - names of connecting filters\r\n" +
"$var1, $var2 - variables holding IBaseFilter of connecting filters\r\n"+
"$pin1, $pin2 - names of connecting pins");
connectDirectTpl.SetVars(new string[] {
"$pair", "File Source (Async.) and AVI Splitter",
"$var1", "pFileSourceAsync",
"$var2", "pAVISplitter",
"$pin1", "Output",
"$pin2", "input pin"
});
checkTpl = new CodeSnippet("Check HRESULT for errors", "checkTpl",
"BOOL hrcheck(HRESULT hr, TCHAR* errtext)\r\n" +
"{\r\n" +
" if (hr >= S_OK)\r\n" +
" return FALSE;\r\n" +
" TCHAR szErr[MAX_ERROR_TEXT_LEN];\r\n" +
" DWORD res = AMGetErrorText(hr, szErr, MAX_ERROR_TEXT_LEN);\r\n" +
" if (res)\r\n" +
" _tprintf(_T(\"Error %x: %s\\n%s\\n\"),hr, errtext,szErr);\r\n" +
" else\r\n" +
" _tprintf(_T(\"Error %x: %s\\n\"), hr, errtext);\r\n" +
" return TRUE;\r\n" +
"}\r\n\r\n" +
"//change this macro to fit your style of error handling\r\n" +
"#define CHECK_HR(hr, msg) if (hrcheck(hr, msg)) return hr;\r\n"
, "");
snippets = new CodeSnippet[] {
defineDsTpl, addFiltDsTpl, addFiltMonTpl, addFiltByNameTpl, addFiltDMOTpl, setSrcFileTpl, setDstFileTpl,
insertTpl, connectTpl, connectDirectTpl, checkTpl
};
LoadTemplates();
}
public override string ToString()
{
return "C++";
}
private int lastguidnum = 0;
Dictionary<string, string> guidnames = new Dictionary<string, string>();
string GuidName(string guid) // guid must be "{123.."
{
if (guidnames.ContainsKey(guid))
return guidnames[guid];
lastguidnum++;
string s = string.Format("GUID{0}", lastguidnum);
guidnames.Add(guid, s);
return s;
}
public override string DefineAddFilterDS(HIAddFilterDS hi)
{
string guid = hi.CLSID, s;
if (known.TryGetValue(guid, out s))
{
hi.clsname = s;
return ""; //no need to define
}
return defineDsTpl.GenerateWith(new string[] {
"$clsname", hi.clsname, "$guid", hi.CLSID, "$file", hi.FileName, "$xguid", xguid(guid)
});
}
public override string DefineAddFilterDMO(HIAddFilterDMO hi)
{
return defineDsTpl.GenerateWith(new string[] {
"$clsname", hi.clsname, "$guid", hi.dmoClsid, "$file", "DMO", "$xguid", xguid(hi.dmoClsid)
}) + defineDsTpl.GenerateWith(new string[] {
"$clsname", hi.clsname + "_cat", "$guid", hi.dmoCat, "$file", "DMO category", "$xguid", xguid(hi.dmoCat)
});
}
string DefineIfNotKnown(string guidname, string known_prefix, string guid)
{
if (guidname.StartsWith(known_prefix)) return "";
return defineDsTpl.GenerateWith(new string[] {
"$clsname", guidname, "$guid", guid, "$file", "", "$xguid", xguid(guid)
});
}
public override string DefineSetFormat(HISetFormat hi)
{
return DefineIfNotKnown(MediaSubTypeToString(hi.mt.subType), "MEDIASUBTYPE", Graph.GuidToString(hi.mt.subType)) +
DefineIfNotKnown(MediaTypeToString(hi.mt.majorType), "MEDIATYPE", Graph.GuidToString(hi.mt.majorType));
}
public override string DefineConnect(HIConnect hi)
{
return DefineIfNotKnown(hi.majortype, "MEDIATYPE", Graph.GuidToString(hi.majortypeguid));
}
public override string BuildAddFilterDS(HIAddFilterDS hi, Graph graph)
{
return addFiltDsTpl.GenerateWith(new string[] {
"$name", hi.Name, "$var", hi.var, "$clsname", hi.clsname
}) + Insert(hi, graph);
}
public override string BuildAddFilterOther(HIAddFilterOther hi, Graph graph)
{
CodeSnippet addFilt = createFiltersByName ? addFiltByNameTpl : addFiltMonTpl;
return addFilt.GenerateWith(new string[] {
"$name", hi.Name, "$var", hi.var, "$displayname", hi.DisplayName.Replace("\\","\\\\"), "$category", hi.CatVar
}) + Insert(hi, graph);
}
void CreateMediaType(string var, AMMediaType mt, StringBuilder sb)
{
sb.AppendFormat(" AM_MEDIA_TYPE {0};\r\n", var);
sb.AppendFormat(" ZeroMemory(&{0}, sizeof(AM_MEDIA_TYPE));\r\n", var);
sb.AppendFormat(" {0}.majortype = {1};\r\n", var, MediaTypeToString(mt.majorType));
sb.AppendFormat(" {0}.subtype = {1};\r\n", var, MediaSubTypeToString(mt.subType));
string fmtype = CodeSnippet.Translate("FORMAT_" + DsToString.MediaFormatTypeToString(mt.formatType), new string[] {
"WaveEx", "WaveFormatEx",
"Mpeg", "MPEG",
"FORMAT_Null", "GUID_NULL"
});
sb.AppendFormat(" {0}.formattype = {1};\r\n", var, fmtype);
sb.AppendFormat(" {0}.bFixedSizeSamples = {1};\r\n", var, mt.fixedSizeSamples.ToString().ToUpperInvariant());
sb.AppendFormat(" {0}.cbFormat = {1};\r\n", var, mt.formatSize);
sb.AppendFormat(" {0}.lSampleSize = {1};\r\n", var, mt.sampleSize);
sb.AppendFormat(" {0}.bTemporalCompression = {1};\r\n", var, mt.temporalCompression.ToString().ToUpperInvariant());
string fmtvar = var.Replace("pmt", "format");
MediaTypeProps mtp = MediaTypeProps.CreateMTProps(mt);
string fmt_struct = mtp.FormatClass().ToUpperInvariant();
sb.AppendFormat(" {1} {0};\r\n", fmtvar, fmt_struct);
sb.AppendFormat(" ZeroMemory(&{0}, sizeof({1}));\r\n", fmtvar, fmt_struct);
mtp.IterFormatFields(false, delegate(string fldname, string fldvalue)
{
if (!fldvalue.StartsWith("new"))
sb.AppendFormat(" {0}.{1} = {2};\r\n", fmtvar, CodeSnippet.Translate(fldname, dict), fldvalue);
});
sb.AppendFormat(" {0}.pbFormat = (BYTE*)&{1};\r\n", var, fmtvar);
}
public override string SetFormat(HISetFormat hi)
{
HistoryItem fhi = history.FindByRealName(hi.filter);
string fvar = (fhi != null) ? fhi.var : "?";
StringBuilder sb = new StringBuilder();
CreateMediaType(hi.var, hi.mt, sb);
string iscvar = hi.var.Replace("pmt", "isc");
sb.AppendFormat(" CComQIPtr<IAMStreamConfig, &IID_IAMStreamConfig> {0}(GetPin({1}, L\"{2}\"));\r\n", iscvar, fvar, hi.pin);
sb.AppendFormat(" hr = {1}->SetFormat(&{0});\r\n", hi.var, iscvar);
sb.Append(" CHECK_HR(hr, _T(\"Can't set format\"));\r\n");
sb.AppendLine();
return sb.ToString();
}
protected override string SetSampleGrabberMediaType(AMMediaType mt, HistoryItem hi)
{
string mtvar = hi.var + "_pmt";
StringBuilder sb = new StringBuilder();
CreateMediaType(mtvar, mt, sb);
string isgvar = mtvar.Replace("pmt", "isg");
sb.AppendFormat(" CComQIPtr<ISampleGrabber, &IID_ISampleGrabber> {0}({1});\r\n", isgvar, hi.var);
sb.AppendFormat(" hr = {1}->SetMediaType(&{0});\r\n", mtvar, isgvar);
sb.Append(" CHECK_HR(hr, _T(\"Can't set media type to sample grabber\"));\r\n");
sb.AppendLine();
return sb.ToString();
}
readonly string[] dict = new string[] {
"AvgTimePerFrame", "AvgTimePerFrame", //vih2
"BitErrorRate", "dwBitErrorRate",
"BitRate", "dwBitRate",
"BmiHeader", "bmiHeader",
"ControlFlags", "dwControlFlags",
"CopyProtectFlags", "dwCopyProtectFlags",
"InterlaceFlags", "dwInterlaceFlags",
"PictAspectRatioX", "dwPictAspectRatioX",
"PictAspectRatioY", "dwPictAspectRatioY",
"Reserved2", "dwReserved2",
"SrcRect", "rcSource",
"TargetRect", "rcTarget",
"BitCount", "biBitCount", //bmih
"ClrImportant", "biClrImportant",
"ClrUsed", "biClrUsed",
"Compression", "biCompression",
"Height", "biHeight",
".ImageSize", ".biSizeImage",
"Planes", "biPlanes",
".Size", ".biSize",
"Width", "biWidth",
"XPelsPerMeter", "biXPelsPerMeter",
"YPelsPerMeter", "biYPelsPerMeter"
};
/*KeyValuePair<string, string> FormatFieldsFilter(KeyValuePair<string, string> p)
{
if (!p.Value.StartsWith("new"))
return new KeyValuePair<string, string>(CodeSnippet.Translate(p.Key, dict), p.Value);
}*/
public string MediaSubTypeToString(Guid guid)
{
foreach (FieldInfo m in typeof (MediaSubType).GetFields())
if ((Guid) m.GetValue(null) == guid)
return "MEDIASUBTYPE_" + m.Name.ToUpperInvariant();
return GuidName(Graph.GuidToString(guid));
}
public override string MediaTypeToString(Guid guid)
{
foreach (FieldInfo m in typeof (MediaType).GetFields())
if ((Guid) m.GetValue(null) == guid)
return "MEDIATYPE_" + m.Name;
return GuidName(Graph.GuidToString(guid));
}
public override string GenCode(bool useDirectConnect, Graph graph)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("//Don't forget to change project settings:");
sb.AppendLine("//1. C++: add include path to DirectShow include folder (such as c:\\dxsdk\\include)");
sb.AppendLine("//2. Link: add link path to DirectShow lib folder (such as c:\\dxsdk\\lib).");
sb.AppendLine("//3. Link: add strmiids.lib and quartz.lib");
sb.AppendLine();
sb.AppendLine("#include \"stdafx.h\"");
sb.AppendLine("#include <DShow.h>");
sb.AppendLine("#include <atlbase.h>");
sb.AppendLine("#include <initguid.h>");
sb.AppendLine("#include <dvdmedia.h>");
bool useSG = false, useDMO = false;
foreach (HistoryItem hi in history.Items)
{
HIAddFilterDS hds = hi as HIAddFilterDS;
if (hds != null && hds.CLSID == "{C1F400A0-3F08-11D3-9F0B-006008039E37}") // sample grabber
useSG = true;
HIAddFilterDMO hidmo = hi as HIAddFilterDMO;
if (hidmo != null)
useDMO = true;
}
if (useSG)
{
sb.AppendLine("// take this file from GraphEditPlus folder and use it ");
sb.AppendLine("#include \"SampleGrabber.h\" // if your version of Windows SDK doesn't know about SampleGrabber");
}
if (useDMO)
{
sb.AppendLine("#include <dmodshow.h> // we're going to use DMO Wrapper Filter");
sb.AppendLine("#include <dmoreg.h>");
sb.AppendLine("#pragma comment(lib,\"dmoguids.lib\")");
}
sb.AppendLine();
sb.AppendLine(checkTpl.Generate());
if (useDirectConnect) needGetPin = true;
directConnect = useDirectConnect;
StringBuilder sb_def = new StringBuilder();
Dictionary<string, bool> defined = new Dictionary<string, bool>(); //guid => define_guid text
foreach (HistoryItem hi in history.Items)
{
string def = hi.Define(this);
if (!defined.ContainsKey(def)) //do not repeat
{
sb_def.Append(def);
defined.Add(def, true);
}
if (def.Length > 0) sb_def.AppendLine();
}
if (needCreateFilterProc)
{
if (createFiltersByName)
{
sb.AppendLine("CComPtr<IBaseFilter> CreateFilterByName(const WCHAR* filterName, const GUID& category)");
sb.AppendLine("{");
sb.AppendLine(" HRESULT hr = S_OK;");
sb.AppendLine(" CComPtr<ICreateDevEnum> pSysDevEnum;");
sb.AppendLine(" hr = pSysDevEnum.CoCreateInstance(CLSID_SystemDeviceEnum);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't create System Device Enumerator\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine();
sb.AppendLine(" CComPtr<IEnumMoniker> pEnumCat;");
sb.AppendLine(" hr = pSysDevEnum->CreateClassEnumerator(category, &pEnumCat, 0);");
sb.AppendLine();
sb.AppendLine(" if (hr == S_OK) ");
sb.AppendLine(" {");
sb.AppendLine(" CComPtr<IMoniker> pMoniker;");
sb.AppendLine(" ULONG cFetched;");
sb.AppendLine(" while(pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)");
sb.AppendLine(" {");
sb.AppendLine(" CComPtr<IPropertyBag> pPropBag;");
sb.AppendLine(" hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag);");
sb.AppendLine(" if (SUCCEEDED(hr))");
sb.AppendLine(" {");
sb.AppendLine(" VARIANT varName;");
sb.AppendLine(" VariantInit(&varName);");
sb.AppendLine(" hr = pPropBag->Read(L\"FriendlyName\", &varName, 0);");
sb.AppendLine(" if (SUCCEEDED(hr))");
sb.AppendLine(" {");
sb.AppendLine(" if (wcscmp(filterName, varName.bstrVal)==0) {");
sb.AppendLine(" CComPtr<IBaseFilter> pFilter;");
sb.AppendLine(" hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)&pFilter);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't bind moniker to filter object\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine(" return pFilter;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" VariantClear(&varName); ");
sb.AppendLine(" }");
sb.AppendLine(" pMoniker.Release();");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" return NULL;");
sb.AppendLine("}");
}
else
{
sb.AppendLine("CComPtr<IBaseFilter> CreateFilter(WCHAR* displayName)");
sb.AppendLine("{");
sb.AppendLine(" CComPtr<IBindCtx> pBindCtx;");
sb.AppendLine(" HRESULT hr = CreateBindCtx(0, &pBindCtx);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't create bind context\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine();
sb.AppendLine(" ULONG chEaten = 0;");
sb.AppendLine(" CComPtr<IMoniker> pMoniker;");
sb.AppendLine(" hr = MkParseDisplayName(pBindCtx, displayName, &chEaten, &pMoniker);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't create parse display name of the filter\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine();
sb.AppendLine(" CComPtr<IBaseFilter> pFilter;");
sb.AppendLine(" if (SUCCEEDED(hr))");
sb.AppendLine(" {");
sb.AppendLine(" hr = pMoniker->BindToObject(pBindCtx, NULL, IID_IBaseFilter, (void**)&pFilter);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't bind moniker to filter object\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine(" }");
sb.AppendLine(" return pFilter;");
sb.AppendLine("}");
}
sb.AppendLine();
}
if (needGetPin)
{
sb.AppendLine("CComPtr<IPin> GetPin(IBaseFilter *pFilter, LPCOLESTR pinname)");
sb.AppendLine("{");
sb.AppendLine(" CComPtr<IEnumPins> pEnum;");
sb.AppendLine(" CComPtr<IPin> pPin;");
sb.AppendLine();
sb.AppendLine(" HRESULT hr = pFilter->EnumPins(&pEnum);");
sb.AppendLine(" if (hrcheck(hr, _T(\"Can't enumerate pins.\")))");
sb.AppendLine(" return NULL;");
sb.AppendLine();
sb.AppendLine(" while(pEnum->Next(1, &pPin, 0) == S_OK)");
sb.AppendLine(" {");
sb.AppendLine(" PIN_INFO pinfo;");
sb.AppendLine(" pPin->QueryPinInfo(&pinfo);");
sb.AppendLine(" BOOL found = !wcsicmp(pinname, pinfo.achName);");
sb.AppendLine(" if (pinfo.pFilter) pinfo.pFilter->Release();");
sb.AppendLine(" if (found)");
sb.AppendLine(" return pPin;");
sb.AppendLine(" pPin.Release();");
sb.AppendLine(" }");
sb.AppendLine(" printf(\"Pin not found!\\n\");");
sb.AppendLine(" return NULL; ");
sb.AppendLine("}");
sb.AppendLine();
}
sb.AppendLine(sb_def.ToString());
StringBuilder sb_bld = new StringBuilder();
foreach (HistoryItem hi in history.Items)
sb_bld.AppendLine(hi.Build(this, graph));
sb.AppendLine();
sb.Append("HRESULT BuildGraph(IGraphBuilder *pGraph");
for(int i = 0; i < srcFileNames.Count; i++)
sb.Append(", LPCOLESTR srcFile" + (i + 1).ToString());
for (int i = 0; i < dstFileNames.Count; i++)
sb.Append(", LPCOLESTR dstFile" + (i + 1).ToString());
sb.AppendLine(")");
sb.AppendLine("{");
sb.AppendLine(" HRESULT hr = S_OK;");
sb.AppendLine();
sb.AppendLine(" //graph builder");
sb.AppendLine(" CComPtr<ICaptureGraphBuilder2> pBuilder;");
sb.AppendLine(" hr = pBuilder.CoCreateInstance(CLSID_CaptureGraphBuilder2);");
sb.AppendLine(" CHECK_HR(hr, _T(\"Can't create Capture Graph Builder\"));");
sb.AppendLine(" hr = pBuilder->SetFiltergraph(pGraph);");
sb.AppendLine(" CHECK_HR(hr, _T(\"Can't SetFiltergraph\"));");
sb.AppendLine();
sb.Append(sb_bld.ToString());
sb.AppendLine(" return S_OK;");
sb.Append("}");
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("//int _tmain(int argc, _TCHAR* argv[]) //use this line in VS2008");
sb.AppendLine("int main(int argc, char* argv[])");
sb.AppendLine("{");
sb.AppendLine(" CoInitialize(NULL);");
sb.AppendLine(" CComPtr<IGraphBuilder> graph;");
sb.AppendLine(" graph.CoCreateInstance(CLSID_FilterGraph);");
sb.AppendLine();
sb.AppendLine(" printf(\"Building graph...\\n\");");
sb.Append(" HRESULT hr = BuildGraph(graph");
foreach (string fn in srcFileNames)
sb.Append(", L\"" + fn.Replace("\\","\\\\") + "\"");
foreach (string fn in dstFileNames)
sb.Append(", L\"" + fn.Replace("\\", "\\\\") + "\"");
sb.AppendLine(");");
sb.AppendLine(" if (hr==S_OK) {");
sb.AppendLine(" printf(\"Running\");");
sb.AppendLine(" CComQIPtr<IMediaControl, &IID_IMediaControl> mediaControl(graph);");
sb.AppendLine(" hr = mediaControl->Run();");
sb.AppendLine(" CHECK_HR(hr, _T(\"Can't run the graph\"));");
sb.AppendLine(" CComQIPtr<IMediaEvent, &IID_IMediaEvent> mediaEvent(graph);");
sb.AppendLine(" BOOL stop = FALSE;");
sb.AppendLine(" MSG msg;");
sb.AppendLine(" while(!stop) ");