-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterGraphTools.cs
1271 lines (1075 loc) · 45.2 KB
/
FilterGraphTools.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
/****************************************************************************
While the underlying libraries are covered by LGPL, this sample is released
as public domain. It 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.
*****************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using DirectShowLib;
#if !USING_NET11
using System.Runtime.InteropServices.ComTypes;
#endif
namespace DirectShowLib.Utils
{
/// <summary>
/// A collection of methods to do common DirectShow tasks.
/// </summary>
public sealed class FilterGraphTools
{
private FilterGraphTools(){}
/// <summary>
/// Add a filter to a DirectShow Graph using its CLSID
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="clsid">a valid CLSID. This object must implement IBaseFilter</param>
/// <param name="name">the name used in the graph (may be null)</param>
/// <returns>an instance of the filter if the method successfully created it, null if not</returns>
/// <remarks>
/// You can use <see cref="IsThisComObjectInstalled">IsThisComObjectInstalled</see> to check is the CLSID is valid before calling this method
/// </remarks>
/// <example>This sample shows how to programmatically add a NVIDIA Video decoder filter to a graph
/// <code>
/// Guid nvidiaVideoDecClsid = new Guid("71E4616A-DB5E-452B-8CA5-71D9CC7805E9");
///
/// if (FilterGraphTools.IsThisComObjectInstalled(nvidiaVideoDecClsid))
/// {
/// filter = FilterGraphTools.AddFilterFromClsid(graphBuilder, nvidiaVideoDecClsid, "NVIDIA Video Decoder");
/// }
/// else
/// {
/// // use another filter...
/// }
/// </code>
/// </example>
/// <seealso cref="IsThisComObjectInstalled"/>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occur when the filter is add to the graph</exception>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static IBaseFilter AddFilterFromClsid(IGraphBuilder graphBuilder, Guid clsid, string name)
{
int hr = 0;
IBaseFilter filter = null;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
try
{
Type type = Type.GetTypeFromCLSID(clsid);
filter = (IBaseFilter) Activator.CreateInstance(type);
hr = graphBuilder.AddFilter(filter, name);
DsError.ThrowExceptionForHR(hr);
}
catch
{
if (filter != null)
{
Marshal.ReleaseComObject(filter);
filter = null;
}
}
return filter;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static object CreateComObjectFromClsid(Guid clsid)
{
Type type = Type.GetTypeFromCLSID(clsid);
return Activator.CreateInstance(type);
}
/// <summary>
/// Add a filter to a DirectShow Graph using its name
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="deviceCategory">the filter category (see DirectShowLib.FilterCategory)</param>
/// <param name="friendlyName">the filter name (case-sensitive)</param>
/// <returns>an instance of the filter if the method successfully created it, null if not</returns>
/// <example>This sample shows how to programmatically add a NVIDIA Video decoder filter to a graph
/// <code>
/// filter = FilterGraphTools.AddFilterByName(graphBuilder, FilterCategory.LegacyAmFilterCategory, "NVIDIA Video Decoder");
/// </code>
/// </example>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occur when the filter is add to the graph</exception>
public static IBaseFilter AddFilterByName(IGraphBuilder graphBuilder, Guid deviceCategory, string friendlyName)
{
int hr = 0;
IBaseFilter filter = null;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
DsDevice[] devices = DsDevice.GetDevicesOfCat(deviceCategory);
for (int i = 0; i < devices.Length; i++)
{
if (string.IsNullOrEmpty(devices[i].Name)) //if the name is empty ignore the filter
continue;
else
{
if (!devices[i].Name.Equals(friendlyName))
continue;
}
hr = (graphBuilder as IFilterGraph2).AddSourceFilterForMoniker(devices[i].Mon, null, friendlyName, out filter);
DsError.ThrowExceptionForHR(hr);
break;
}
return filter;
}
/// <summary>
/// Add a filter to a DirectShow Graph using its Moniker's device path
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="devicePath">a moniker path</param>
/// <param name="name">the name to use for the filter in the graph</param>
/// <returns>an instance of the filter if the method successfully creates it, null if not</returns>
/// <example>This sample shows how to programmatically add a NVIDIA Video decoder filter to a graph
/// <code>
/// string devicePath = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{71E4616A-DB5E-452B-8CA5-71D9CC7805E9}";
/// filter = FilterGraphTools.AddFilterByDevicePath(graphBuilder, devicePath, "NVIDIA Video Decoder");
/// </code>
/// </example>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occur when the filter is add to the graph</exception>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static IBaseFilter AddFilterByDevicePath(IGraphBuilder graphBuilder, string devicePath, string name)
{
int hr = 0;
IBaseFilter filter = null;
#if USING_NET11
UCOMIBindCtx bindCtx = null;
UCOMIMoniker moniker = null;
#else
IBindCtx bindCtx = null;
IMoniker moniker = null;
#endif
int eaten;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
try
{
hr = NativeMethods.CreateBindCtx(0, out bindCtx);
Marshal.ThrowExceptionForHR(hr);
hr = NativeMethods.MkParseDisplayName(bindCtx, devicePath, out eaten, out moniker);
Marshal.ThrowExceptionForHR(hr);
hr = (graphBuilder as IFilterGraph2).AddSourceFilterForMoniker(moniker, bindCtx, name, out filter);
DsError.ThrowExceptionForHR(hr);
}
catch
{
// An error occur. Just returning null...
}
finally
{
if (bindCtx != null) Marshal.ReleaseComObject(bindCtx);
if (moniker != null) Marshal.ReleaseComObject(moniker);
}
return filter;
}
public static IBaseFilter CreateFilterFromPath(Guid category, string devicePath)
{
object source = null;
Guid iid = typeof(IBaseFilter).GUID;
foreach (DsDevice device in DsDevice.GetDevicesOfCat(category))
{
if (device.DevicePath.CompareTo(devicePath) == 0)
{
device.Mon.BindToObject(null, null, ref iid, out source);
break;
}
}
return (IBaseFilter)source;
}
/// <summary>
/// Find a filter in a DirectShow Graph using its name
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="filterName">the filter name to find (case-sensitive)</param>
/// <returns>an instance of the filter if found, null if not</returns>
/// <seealso cref="FindFilterByClsid"/>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static IBaseFilter FindFilterByName(IGraphBuilder graphBuilder, string filterName)
{
int hr = 0;
IBaseFilter filter = null;
IEnumFilters enumFilters = null;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
if (hr == 0)
{
IBaseFilter[] filters = new IBaseFilter[1];
while(enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
{
FilterInfo filterInfo;
hr = filters[0].QueryFilterInfo(out filterInfo);
if (hr == 0)
{
if (filterInfo.pGraph != null)
Marshal.ReleaseComObject(filterInfo.pGraph);
if (filterInfo.achName.Equals(filterName))
{
filter = filters[0];
break;
}
}
Marshal.ReleaseComObject(filters[0]);
}
Marshal.ReleaseComObject(enumFilters);
}
return filter;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static IBaseFilter FindFilterByClsid(IGraphBuilder graphBuilder, string filterClsid)
{
int hr = 0;
IBaseFilter filter = null;
IEnumFilters enumFilters = null;
Guid gClsid = new Guid(filterClsid);
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
if (hr == 0)
{
while (true)
{
IBaseFilter[] filters = new IBaseFilter[1];
int fetched;
// Get the next filter
IntPtr d = Marshal.AllocCoTaskMem(4);
try
{
//int hr = emtDvr.Next(1, amtDvr, d);
hr = enumFilters.Next(1, filters, d);
DsError.ThrowExceptionForHR(hr);
fetched = Marshal.ReadInt32(d);
}
finally
{
Marshal.FreeCoTaskMem(d);
}
if (fetched > 0)
{
Guid clsid;
hr = filters[0].GetClassID(out clsid);
if ((hr == 0) && (clsid == gClsid))
{
filter = filters[0];
break;
}
Marshal.ReleaseComObject(filters[0]);
}
else
break;
}
Marshal.ReleaseComObject(enumFilters);
}
return filter;
}
/// <summary>
/// Find a filter in a DirectShow Graph using its CLSID
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="filterClsid">the CLSID to find</param>
/// <returns>an instance of the filter if found, null if not</returns>
/// <seealso cref="FindFilterByName"/>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static IBaseFilter FindFilterByClsid(IGraphBuilder graphBuilder, Guid filterClsid)
{
int hr = 0;
IBaseFilter filter = null;
IEnumFilters enumFilters = null;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
if (hr == 0)
{
IBaseFilter[] filters = new IBaseFilter[1];
while(enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
{
Guid clsid;
hr = filters[0].GetClassID(out clsid);
if ((hr == 0) && (clsid == filterClsid))
{
filter = filters[0];
break;
}
Marshal.ReleaseComObject(filters[0]);
}
Marshal.ReleaseComObject(enumFilters);
}
return filter;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static void SetGraphOutputFile(IGraphBuilder graphBuilder, string OutputFile)
{
int hr = 0;
IEnumFilters enumFilters = null;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
if (hr == 0)
{
IBaseFilter[] filters = new IBaseFilter[1];
while (enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
{
Guid clsid;
IFileSinkFilter fsf = filters[0] as IFileSinkFilter;
if (fsf != null)
{
hr = fsf.SetFileName(OutputFile, null);
Marshal.ReleaseComObject(filters[0]);
break;
}
Marshal.ReleaseComObject(filters[0]);
}
Marshal.ReleaseComObject(enumFilters);
}
}
/// <summary>
/// Render a filter's pin in a DirectShow Graph
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="source">the filter containing the pin to render</param>
/// <param name="pinName">the pin name</param>
/// <returns>true if rendering is a success, false if not</returns>
/// <example>
/// <code>
/// hr = graphBuilder.AddSourceFilter(@"foo.avi", "Source Filter", out filter);
/// DsError.ThrowExceptionForHR(hr);
///
/// if (!FilterGraphTools.RenderPin(graphBuilder, filter, "Output"))
/// {
/// // Something went wrong...
/// }
/// </code>
/// </example>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder or source is null</exception>
/// <remarks>This method assumes that the filter is part of the given graph</remarks>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static bool RenderPin(IGraphBuilder graphBuilder, IBaseFilter source, string pinName)
{
int hr = 0;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
if (source == null)
throw new ArgumentNullException("source");
IPin pin = DsFindPin.ByName(source, pinName);
if (pin != null)
{
hr = graphBuilder.Render(pin);
Marshal.ReleaseComObject(pin);
return (hr >= 0);
}
return false;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static IBaseFilter GetConnectedFilter(IBaseFilter source, int iIndex)
{
IBaseFilter retFilter = null;
int hr = 0;
IPin sPin = DsFindPin.ByConnectionStatus(source, PinConnectedStatus.Connected, iIndex);
IPin dPin = null;
try
{
hr = sPin.ConnectedTo(out dPin);
DsError.ThrowExceptionForHR(hr);
retFilter = GetFilterFromPin(dPin);
}
finally
{
if (dPin != null)
Marshal.ReleaseComObject(dPin);
dPin = null;
if (sPin != null)
Marshal.ReleaseComObject(sPin);
sPin = null;
}
return retFilter;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static IBaseFilter GetConnectedFilter(IBaseFilter source, PinDirection vDir, int iIndex)
{
IBaseFilter retFilter = null;
int hr = 0;
IPin sPin = DsFindPin.ByDirection(source, vDir, iIndex);
IPin dPin = null;
try
{
hr = sPin.ConnectedTo(out dPin);
DsError.ThrowExceptionForHR(hr);
if (dPin != null)
retFilter = GetFilterFromPin(dPin);
}
finally
{
if (dPin != null)
Marshal.ReleaseComObject(dPin);
dPin = null;
if (sPin != null)
Marshal.ReleaseComObject(sPin);
sPin = null;
}
return retFilter;
}
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public static IBaseFilter GetFilterFromPin(IPin pin)
{
PinInfo pi = default(PinInfo);
IBaseFilter filter;
int hr;
//try
//{
hr = pin.QueryPinInfo(out pi);
DsError.ThrowExceptionForHR(hr);
filter = pi.filter;
return filter;
//}
//finally
//{
// DsUtils.FreePinInfo(pi);
//}
}
/// <summary>
/// Disconnect all pins on a given filter
/// </summary>
/// <param name="filter">the filter on which to disconnect all the pins</param>
/// <exception cref="System.ArgumentNullException">Thrown if filter is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occured during the disconnection process</exception>
/// <remarks>Both input and output pins are disconnected</remarks>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void DisconnectPins(IBaseFilter filter)
{
int hr = 0;
if (filter == null)
throw new ArgumentNullException("filter");
IEnumPins enumPins;
IPin[] pins = new IPin[1];
hr = filter.EnumPins(out enumPins);
DsError.ThrowExceptionForHR(hr);
try
{
while(enumPins.Next(pins.Length, pins, IntPtr.Zero) == 0)
{
try
{
hr = pins[0].Disconnect();
DsError.ThrowExceptionForHR(hr);
}
finally
{
Marshal.ReleaseComObject(pins[0]);
}
}
}
finally
{
Marshal.ReleaseComObject(enumPins);
}
}
/// <summary>
/// Disconnect pins of all the filters in a DirectShow Graph
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if the method can't enumerate its filters</exception>
/// <remarks>This method doesn't throw an exception if an error occurs during pin disconnections</remarks>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void DisconnectAllPins(IGraphBuilder graphBuilder)
{
int hr = 0;
IEnumFilters enumFilters;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
DsError.ThrowExceptionForHR(hr);
try
{
IBaseFilter[] filters = new IBaseFilter[1];
while(enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
{
try
{
DisconnectPins(filters[0]);
}
catch{}
Marshal.ReleaseComObject(filters[0]);
}
}
finally
{
Marshal.ReleaseComObject(enumFilters);
}
}
/// <summary>
/// Remove and release all filters from a DirectShow Graph
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if the method can't enumerate its filters</exception>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void RemoveAllFilters(IGraphBuilder graphBuilder)
{
int hr = 0;
IEnumFilters enumFilters;
ArrayList filtersArray = new ArrayList();
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
hr = graphBuilder.EnumFilters(out enumFilters);
DsError.ThrowExceptionForHR(hr);
try
{
IBaseFilter[] filters = new IBaseFilter[1];
while(enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
{
filtersArray.Add(filters[0]);
}
}
finally
{
Marshal.ReleaseComObject(enumFilters);
}
foreach(IBaseFilter filter in filtersArray)
{
hr = graphBuilder.RemoveFilter(filter);
Marshal.ReleaseComObject(filter);
}
}
/// <summary>
/// Save a DirectShow Graph to a GRF file
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="fileName">the file to be saved</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occur during the file creation</exception>
/// <seealso cref="LoadGraphFile"/>
/// <remarks>This method overwrites any existing file</remarks>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void SaveGraphFile(IGraphBuilder graphBuilder, string fileName)
{
int hr = 0;
IStorage storage = null;
#if USING_NET11
UCOMIStream stream = null;
#else
IStream stream = null;
#endif
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
try
{
hr = NativeMethods.StgCreateDocfile(
fileName,
STGM.Create | STGM.Transacted | STGM.ReadWrite | STGM.ShareExclusive,
0,
out storage
);
Marshal.ThrowExceptionForHR(hr);
hr = storage.CreateStream(
@"ActiveMovieGraph",
STGM.Write | STGM.Create | STGM.ShareExclusive,
0,
0,
out stream
);
Marshal.ThrowExceptionForHR(hr);
hr = (graphBuilder as IPersistStream).Save(stream, true);
Marshal.ThrowExceptionForHR(hr);
hr = storage.Commit(STGC.Default);
Marshal.ThrowExceptionForHR(hr);
}
finally
{
if (stream != null)
Marshal.ReleaseComObject(stream);
if (storage != null)
Marshal.ReleaseComObject(storage);
}
}
/// <summary>
/// Load a DirectShow Graph from a file
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="fileName">the file to be loaded</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the given file is not a valid graph file</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if errors occur during loading</exception>
/// <seealso cref="SaveGraphFile"/>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void LoadGraphFile(IGraphBuilder graphBuilder, string fileName)
{
int hr = 0;
IStorage storage = null;
#if USING_NET11
UCOMIStream stream = null;
#else
IStream stream = null;
#endif
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
try
{
if (NativeMethods.StgIsStorageFile(fileName) != 0)
throw new ArgumentException();
hr = NativeMethods.StgOpenStorage(
fileName,
null,
STGM.Transacted | STGM.Read | STGM.ShareDenyWrite,
IntPtr.Zero,
0,
out storage
);
Marshal.ThrowExceptionForHR(hr);
hr = storage.OpenStream(
@"ActiveMovieGraph",
IntPtr.Zero,
STGM.Read | STGM.ShareExclusive,
0,
out stream
);
Marshal.ThrowExceptionForHR(hr);
hr = (graphBuilder as IPersistStream).Load(stream);
Marshal.ThrowExceptionForHR(hr);
}
finally
{
if (stream != null)
Marshal.ReleaseComObject(stream);
if (storage != null)
Marshal.ReleaseComObject(storage);
}
}
/// <summary>
/// Check if a DirectShow filter can display Property Pages
/// </summary>
/// <param name="filter">A DirectShow Filter</param>
/// <exception cref="System.ArgumentNullException">Thrown if filter is null</exception>
/// <seealso cref="ShowFilterPropertyPage"/>
/// <returns>true if the filter has Property Pages, false if not</returns>
/// <remarks>
/// This method is intended to be used with <see cref="ShowFilterPropertyPage">ShowFilterPropertyPage</see>
/// </remarks>
public static bool HasPropertyPages(IBaseFilter filter)
{
if (filter == null)
throw new ArgumentNullException("filter");
return ((filter as ISpecifyPropertyPages) != null);
}
/// <summary>
/// Display Property Pages of a given DirectShow filter
/// </summary>
/// <param name="filter">A DirectShow Filter</param>
/// <param name="parent">A hwnd handle of a window to contain the pages</param>
/// <exception cref="System.ArgumentNullException">Thrown if filter is null</exception>
/// <seealso cref="HasPropertyPages"/>
/// <remarks>
/// You can check if a filter supports Property Pages with the <see cref="HasPropertyPages">HasPropertyPages</see> method.<br/>
/// <strong>Warning</strong> : This method is blocking. It only returns when the Property Pages are closed.
/// </remarks>
/// <example>This sample shows how to check if a filter supports Property Pages and displays them
/// <code>
/// if (FilterGraphTools.HasPropertyPages(myFilter))
/// {
/// FilterGraphTools.ShowFilterPropertyPage(myFilter, myForm.Handle);
/// }
/// </code>
/// </example>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void ShowFilterPropertyPage(IBaseFilter filter, IntPtr parent)
{
int hr = 0;
FilterInfo filterInfo;
DsCAUUID caGuid;
object[] objs;
if (filter == null)
throw new ArgumentNullException("filter");
if (HasPropertyPages(filter))
{
hr = filter.QueryFilterInfo(out filterInfo);
DsError.ThrowExceptionForHR(hr);
if (filterInfo.pGraph != null)
Marshal.ReleaseComObject(filterInfo.pGraph);
hr = (filter as ISpecifyPropertyPages).GetPages(out caGuid);
DsError.ThrowExceptionForHR(hr);
try
{
objs = new object[1];
objs[0] = filter;
NativeMethods.OleCreatePropertyFrame(
parent, 0, 0,
filterInfo.achName,
objs.Length, objs,
caGuid.cElems, caGuid.pElems,
0, 0,
IntPtr.Zero
);
}
finally
{
Marshal.FreeCoTaskMem(caGuid.pElems);
}
}
}
/// <summary>
/// Check if a COM Object is available
/// </summary>
/// <param name="clsid">The CLSID of this object</param>
/// <example>This sample shows how to check if the MPEG-2 Demultiplexer filter is available
/// <code>
/// if (FilterGraphTools.IsThisComObjectInstalled(typeof(MPEG2Demultiplexer).GUID))
/// {
/// // Use it...
/// }
/// </code>
/// </example>
/// <returns>true if the object is available, false if not</returns>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static bool IsThisComObjectInstalled(Guid clsid)
{
bool retval = false;
try
{
Type type = Type.GetTypeFromCLSID(clsid);
object o = Activator.CreateInstance(type);
retval = true;
Marshal.ReleaseComObject(o);
}
catch{}
return retval;
}
/// <summary>
/// Check if the Video Mixing Renderer 9 Filter is available
/// <seealso cref="IsThisComObjectInstalled"/>
/// </summary>
/// <remarks>
/// This method uses <see cref="IsThisComObjectInstalled">IsThisComObjectInstalled</see> internally
/// </remarks>
/// <returns>true if VMR9 is present, false if not</returns>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static bool IsVMR9Present()
{
return IsThisComObjectInstalled(typeof(VideoMixingRenderer9).GUID);
}
/// <summary>
/// Check if the Video Mixing Renderer 7 Filter is available
/// <seealso cref="IsThisComObjectInstalled"/>
/// </summary>
/// <remarks>
/// This method uses <see cref="IsThisComObjectInstalled">IsThisComObjectInstalled</see> internally
/// </remarks>
/// <returns>true if VMR7 is present, false if not</returns>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static bool IsVMR7Present()
{
return IsThisComObjectInstalled(typeof(VideoMixingRenderer).GUID);
}
/// <summary>
/// Connect pins from two filters
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="upFilter">the upstream filter</param>
/// <param name="sourcePinName">the upstream filter pin name</param>
/// <param name="downFilter">the downstream filter</param>
/// <param name="destPinName">the downstream filter pin name</param>
/// <param name="useIntelligentConnect">indicate if the method should use DirectShow's Intelligent Connect</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder, upFilter or downFilter are null</exception>
/// <exception cref="System.ArgumentException">Thrown if pin names are not found in filters</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if pins can't connect</exception>
/// <remarks>
/// If useIntelligentConnect is true, this method can add missing filters between the two pins.<br/>
/// If useIntelligentConnect is false, this method works only if the two media types are compatible.
/// </remarks>
public static void ConnectFilters(IGraphBuilder graphBuilder, IBaseFilter upFilter, string sourcePinName, IBaseFilter downFilter, string destPinName, bool useIntelligentConnect)
{
ConnectFilters(graphBuilder, upFilter, sourcePinName, downFilter, destPinName, useIntelligentConnect, false);
}
public static void ConnectFilters(IGraphBuilder graphBuilder, IBaseFilter upFilter, string sourcePinName, IBaseFilter downFilter, string destPinName, bool useIntelligentConnect, bool ignoreConnectedPins)
{
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
if (upFilter == null)
throw new ArgumentNullException("upFilter");
if (downFilter == null)
throw new ArgumentNullException("downFilter");
IPin sourcePin, destPin;
IPin cPin = null;
sourcePin = DsFindPin.ByName(upFilter, sourcePinName);
if (sourcePin == null)
throw new ArgumentException("The source filter has no pin called : " + sourcePinName, sourcePinName);
destPin = DsFindPin.ByName(downFilter, destPinName);
if (destPin == null)
throw new ArgumentException("The downstream filter has no pin called : " + destPinName, destPinName);
try
{
sourcePin.ConnectedTo(out cPin);
if(cPin == null || !ignoreConnectedPins)
ConnectFilters(graphBuilder, sourcePin, destPin, useIntelligentConnect);
}
finally
{
if (cPin != null)
Marshal.ReleaseComObject(cPin);
Marshal.ReleaseComObject(sourcePin);
Marshal.ReleaseComObject(destPin);
}
}
/// <summary>
/// Connect pins from two filters
/// </summary>
/// <param name="graphBuilder">the IGraphBuilder interface of the graph</param>
/// <param name="sourcePin">the source (upstream / output) pin</param>
/// <param name="destPin">the destination (downstream / input) pin</param>
/// <param name="useIntelligentConnect">indicates if the method should use DirectShow's Intelligent Connect</param>
/// <exception cref="System.ArgumentNullException">Thrown if graphBuilder, sourcePin or destPin are null</exception>
/// <exception cref="System.Runtime.InteropServices.COMException">Thrown if pins can't connect</exception>
/// <remarks>
/// If useIntelligentConnect is true, this method can add missing filters between the two pins.<br/>
/// If useIntelligentConnect is false, this method works only if the two media types are compatible.
/// </remarks>
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
public static void ConnectFilters(IGraphBuilder graphBuilder, IPin sourcePin, IPin destPin, bool useIntelligentConnect)
{
int hr = 0;
if (graphBuilder == null)
throw new ArgumentNullException("graphBuilder");
if (sourcePin == null)
throw new ArgumentNullException("sourcePin");
if (destPin == null)
throw new ArgumentNullException("destPin");
if (useIntelligentConnect)
{
hr = graphBuilder.Connect(sourcePin, destPin);
DsError.ThrowExceptionForHR(hr);