forked from JMS-1/DVB.NET---VCR.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplayGraph.cs
More file actions
1452 lines (1246 loc) · 46.6 KB
/
DisplayGraph.cs
File metadata and controls
1452 lines (1246 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;
using JMS.DVB.DeviceAccess;
using JMS.DVB.DeviceAccess.BDAElements;
using JMS.DVB.DeviceAccess.Enumerators;
using JMS.DVB.DeviceAccess.Interfaces;
using JMS.DVB.DirectShow.Filters;
using JMS.DVB.DirectShow.Interfaces;
namespace JMS.DVB.DirectShow
{
/// <summary>
/// Diese Klasse repräsentiert einen <i>Direct Show</i> Graphen zur Anzeige von
/// Bild- und Tondaten.
/// </summary>
public class DisplayGraph : IDisposable
{
/// <summary>
/// Schalter zur Protokollierung von elementaren Operationen.
/// </summary>
public static readonly BooleanSwitch DirectShowTraceSwitch = new BooleanSwitch( "DirectShowTrace", "Reports DirectShow Graph Operations" );
/// <summary>
/// Beschreibt das Videobild.
/// </summary>
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
private struct MPEG2VideoInfo
{
/// <summary>
/// Linke Seite des Quellbereiches.
/// </summary>
public Int32 SourceRectLeft;
/// <summary>
/// Obere Seite des Quellbereiches.
/// </summary>
public Int32 SourceRectTop;
/// <summary>
/// Rechte Seite des Quellbereiches.
/// </summary>
public Int32 SourceRectRight;
/// <summary>
/// Untere Seite des Quellbereiches.
/// </summary>
public Int32 SourceRectBottom;
/// <summary>
/// Linke Seite des Zielbereiches.
/// </summary>
public Int32 TargetRectLeft;
/// <summary>
/// Obere Seite des Zielbereiches.
/// </summary>
public Int32 TargetRectTop;
/// <summary>
/// Rechte Seite des Zielbereiches.
/// </summary>
public Int32 TargetRectRight;
/// <summary>
/// Untere Seite des Zielbereiches.
/// </summary>
public Int32 TargetRectBottom;
/// <summary>
/// Datenrate in Bits.
/// </summary>
public Int32 BitRate;
/// <summary>
/// Rate der Fehler.
/// </summary>
public Int32 BitErrorRate;
/// <summary>
/// Darstellungsdauer eines Bildes.
/// </summary>
public Int64 TimePerFrame;
/// <summary>
/// Informationen zur Bildstruktur.
/// </summary>
public Int32 InterlaceFlags;
/// <summary>
/// Copyrighteinstellungen des Datenstroms.
/// </summary>
public Int32 CopyProtectionFlags;
/// <summary>
/// Erstes Element des Bildverhältnisses.
/// </summary>
public Int32 AspectRatioX;
/// <summary>
/// Zweites Element des Bildverhältnisses.
/// </summary>
public Int32 AspectRatioY;
/// <summary>
/// Steuerinformationen.
/// </summary>
public Int32 ControlFlags;
/// <summary>
/// Reserviert.
/// </summary>
private Int32 _Reserved;
/// <summary>
/// Größe des Bildes.
/// </summary>
public Int32 BitmpapSize;
/// <summary>
/// Breite des Bildes.
/// </summary>
public Int32 BitmapWidth;
/// <summary>
/// Höhe des Bildes.
/// </summary>
public Int32 BitmapHeight;
/// <summary>
/// Farbtiefe des Bildes.
/// </summary>
public Int16 BitmapPlanes;
/// <summary>
/// Anzahl der Bits.
/// </summary>
public Int16 BitmapBitCount;
/// <summary>
/// Kompressionsart.
/// </summary>
public Int32 BitmapCompression;
/// <summary>
/// Größe des Bildes.
/// </summary>
public Int32 BitmapSizeImage;
/// <summary>
/// Erstes Element des Auflösung.
/// </summary>
public Int32 BitmapXPelsPerMeter;
/// <summary>
/// Zweites Element des Auflösung.
/// </summary>
public Int32 BitmapYPelsPerMeter;
/// <summary>
/// (unbekannt)
/// </summary>
public Int32 BitmapClrUsed;
/// <summary>
/// (unbekannt)
/// </summary>
public Int32 BitmapClrImportant;
/// <summary>
/// Startzeit.
/// </summary>
public Int32 StartTimeCode;
/// <summary>
/// Anzahl der Bytes im MPEG2 Sequenzkopf.
/// </summary>
public Int32 SequenceHeaderBytes;
/// <summary>
/// MPEG2 Profil.
/// </summary>
public Int32 Profile;
/// <summary>
/// MPEG2 Level.
/// </summary>
public Int32 Level;
/// <summary>
/// Zusätzliche Einstellungen.
/// </summary>
public Int32 Flags;
/// <summary>
/// Füllfeld.
/// </summary>
public Int32 _Padding;
/// <summary>
/// Freifeld für den Sequenzkopf.
/// </summary>
[MarshalAs( UnmanagedType.ByValArray, SizeConst = 100 )]
public byte[] _Empty;
}
/// <summary>
/// Beschreibt ein Tondatenformat.
/// </summary>
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
private struct AudioFormat
{
/// <summary>
/// Format.
/// </summary>
public UInt16 Format;
/// <summary>
/// Anzahl der Kanäle.
/// </summary>
public UInt16 Channels;
/// <summary>
/// Anzahl der Messwerte pro Sekunde.
/// </summary>
public UInt32 SamplesPerSec;
/// <summary>
/// Anzahl der Bytes pro Sekunde.
/// </summary>
public UInt32 BytesPerSec;
/// <summary>
/// Speichergranularität.
/// </summary>
public UInt16 BlockAlign;
/// <summary>
/// Bits pro Messwert.
/// </summary>
public UInt16 BitsPerSample;
/// <summary>
/// Größe.
/// </summary>
public UInt16 Size;
/// <summary>
/// Layer.
/// </summary>
public UInt16 HeadLayer;
/// <summary>
/// Bitrate.
/// </summary>
public UInt32 HeadBitrate;
/// <summary>
/// Standardmodus.
/// </summary>
public UInt16 HeadMode;
/// <summary>
/// Erweiterter Modus.
/// </summary>
public UInt16 HeadModeExt;
/// <summary>
/// Verstärkung.
/// </summary>
public UInt16 HeadEmphasis;
/// <summary>
/// Detailinformationen.
/// </summary>
public UInt16 HeadFlags;
/// <summary>
/// Zeitstempel.
/// </summary>
public UInt64 PTS;
}
/// <summary>
/// Erzeugt einen <i>Device Context</i>.
/// </summary>
/// <param name="hdc">Ein vorhandener Kontext.</param>
/// <returns>Der angeforderte kompatible Kontext.</returns>
[DllImport( "gdi32.dll" )]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr CreateCompatibleDC( IntPtr hdc );
/// <summary>
/// Gibt die mit einem <i>Device Context</i> verbundenen Ressourcen wieder frei.
/// </summary>
/// <param name="hdc">Ein vorhandener Kontext, der nicht mehr benötigt wird.</param>
/// <returns>Gesetzt, wenn die Freigabe erfolgreich war.</returns>
[DllImport( "gdi32.dll" )]
[SuppressUnmanagedCodeSecurity]
private static extern bool DeleteDC( IntPtr hdc );
/// <summary>
/// Erzeugt ein Bitfeld für einen <i>Device Context</i>.
/// </summary>
/// <param name="hdc">Der zu verwendende Kontext.</param>
/// <param name="width">Die Breite des Feldes in Pixel.</param>
/// <param name="height">Die Höhe des Feldes in Pixel.</param>
/// <returns>Das gewünschte, zum Kontext passende Feld.</returns>
[DllImport( "gdi32.dll" )]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr CreateCompatibleBitmap( IntPtr hdc, Int32 width, Int32 height );
/// <summary>
/// Aktiviert ein graphisches Objekt.
/// </summary>
/// <param name="hdc">Der betroffene <i>Devoce Context</i>.</param>
/// <param name="bmp">Das zu aktivierende Objekt.</param>
/// <returns>Das bisher aktive Objekt.</returns>
[DllImport( "gdi32.dll" )]
[SuppressUnmanagedCodeSecurity]
private static extern IntPtr SelectObject( IntPtr hdc, IntPtr bmp );
/// <summary>
/// Gibt die mit einem graphischen Objekt verbunden Ressourcen frei.
/// </summary>
/// <param name="obj">Das nicht mehr benötigte Objekt.</param>
/// <returns>Gesetzt, wenn die Freigabe erfolgreich war.</returns>
[DllImport( "gdi32.dll" )]
[SuppressUnmanagedCodeSecurity]
private static extern bool DeleteObject( IntPtr obj );
/// <summary>
/// Der Name des Darstellungsfilters.
/// </summary>
private const string Filter_VMR = "VMR";
/// <summary>
/// Der Name des Quellfiters.
/// </summary>
private const string Filter_Injector = "TS";
/// <summary>
/// Verwaltet die globale Anmeldung des Graphen.
/// </summary>
private ROTRegister m_Register = null;
/// <summary>
/// Verwaltet den eigentlichen Graphen.
/// </summary>
private IGraphBuilder m_Graph = null;
/// <summary>
/// Alle Filter dieses Graphen.
/// </summary>
private Dictionary<string, TypedComIdentity<IBaseFilter>> m_Filters = new Dictionary<string, TypedComIdentity<IBaseFilter>>();
/// <summary>
/// Die Protokolldatei für das Erzeugen des Graphen.
/// </summary>
private FileStream m_LogFile = null;
/// <summary>
/// Die bevorzugte Verzögerung, mit der empfangene Daten abgespielt werden.
/// </summary>
public const int DefaultAVDelay = 500;
/// <summary>
/// Die Verzögerung, mit der empfangene Daten abgespielt werden.
/// </summary>
public int AVDelay = DefaultAVDelay;
/// <summary>
/// Der Moniker des bevorzugten H.264 HDTV Videocodecs.
/// </summary>
public string H264Decoder = null;
/// <summary>
/// Der Moniker des bevorzugten MPEG-2 SDTV Codecs.
/// </summary>
public string MPEG2Decoder = null;
/// <summary>
/// Der Moniker des bevorzugten MP2 Codecs.
/// </summary>
public string MP2Decoder = null;
/// <summary>
/// Der Moniker des bevorzugten Dolby Digital (AC3) Codecs.
/// </summary>
public string AC3Decoder = null;
/// <summary>
/// Beschreibt die nicht lineare Abbildung einer Lautstärkeangabe in Prozent in die
/// BDA Notation.
/// </summary>
private static int[] m_VolumeMap;
/// <summary>
/// DVB.NET Filter zur Einspeisung der Daten in den Graphen.
/// </summary>
public TSFilter InjectorFilter { get; private set; }
/// <summary>
/// Meldet oder legt fest, ob für den Bilddatenstrom Synchronisationspunkte
/// generiert werden sollen.
/// </summary>
public bool EnforceVideoSynchronisation { get; set; }
/// <summary>
/// Das zugehörige Fenster.
/// </summary>
private Control m_VideoWindow = null;
/// <summary>
/// Gesetzt, sobald das Fenster erzeugt wurde.
/// </summary>
private bool m_VideoWindowCreated = false;
/// <summary>
/// Die aktuelle Datenstromkennung des Bildsignals.
/// </summary>
private ushort m_VideoPID = 0;
/// <summary>
/// Die aktuelle Datenstromkennung des Tonsignals.
/// </summary>
private ushort m_AudioPID = 0;
/// <summary>
/// Die Systemuhr.
/// </summary>
private volatile NoMarshalComObjects.ReferenceClock m_Clock = null;
/// <summary>
/// Die aktuelle Lautstärke.
/// </summary>
private byte? m_LastVolume = null;
/// <summary>
/// Meldet oder setzt ob das spezielle DirectShow Format für den CyberLink H.264
/// Video Codec verwendet werden soll.
/// </summary>
public bool? UseCyberlink = null;
/// <summary>
/// Merke einmal eingestellte Bildparameter und wendet diese beim Neuerzeugen
/// eines Graphen mit Videobild auf diesen an.
/// </summary>
private PictureParameters m_PictureParameters = null;
/// <summary>
/// Initialisiert globale Variablen für die Anzeige eines DirectShow Graphen.
/// </summary>
static DisplayGraph()
{
// Create the map
m_VolumeMap = new int[101];
// Fill it
for (int volume = m_VolumeMap.Length; volume-- > 1; )
m_VolumeMap[volume] = ((int) Math.Round( 5000 * Math.Log( volume, 10 ) )) - 10000;
// Silence
m_VolumeMap[0] = -10000;
}
/// <summary>
/// Erzeugt einen neuen Graphen.
/// </summary>
public DisplayGraph()
{
}
/// <summary>
/// Initialisiert die Filterstruktur.
/// </summary>
public void CreateGraph()
{
// Cleanup
DestroyGraph();
// Check log
var logFile = BDASettings.BDALogPath;
if (logFile != null)
m_LogFile = new FileStream( logFile.FullName, FileMode.Create, FileAccess.Write, FileShare.Read );
// Create new graph builder
m_Graph = (IGraphBuilder) Activator.CreateInstance( Type.GetTypeFromCLSID( BDAEnvironment.GraphBuilderClassIdentifier ) );
// Enable logging
if (m_LogFile != null)
m_Graph.SetLogFile( m_LogFile.SafeFileHandle );
// See if we should register the graph
m_Register = BDASettings.RegisterBDAGRaph( DirectShowObject, false );
// Create filter
InjectorFilter = new TSFilter( this );
try
{
// Check for statistics
InjectorFilter.EnableStatistics = BDASettings.GenerateTSStatistics;
// Register in graph
AddFilter( Filter_Injector, InjectorFilter );
}
catch
{
// Cleanup
InjectorFilter.Dispose();
// Forward
throw;
}
}
/// <summary>
/// Bereitet die Anzeige vor.
/// </summary>
/// <returns>Gesetzt, wenn die Anzeige bisher noch nicht vorbereitet war.</returns>
private bool PrepareShow()
{
// Once only
if (m_VideoWindowCreated)
return false;
// Lock out
m_VideoWindowCreated = true;
// Must have a VMR when displaying video
CreateVMR();
// Preload filters
for (int i = 0; ++i > 0; )
{
// Get the name and the filter
var filterName = string.Format( "Filter{0}", i );
var filter = ConfigurationManager.AppSettings[filterName];
// Process
if (filter == null)
break;
else if (filter.Length > 0)
AddFilter( "Preloaded" + filterName, filter );
}
// This was the first call
return true;
}
/// <summary>
/// Lädt einen bestimmten Decoderfilter, sofern explicit konfiguriert.
/// </summary>
/// <param name="moniker">Der eindeutige Name des Filters.</param>
/// <param name="forType">Die Art des Filters - nur zur Benennung im Graphen verwendet.</param>
/// <param name="processor">Optionale Verarbeitungsmethode für den neuen Filter.</param>
private void LoadDecoder( string moniker, string forType, Action<TypedComIdentity<IBaseFilter>> processor = null )
{
// Not set
if (string.IsNullOrEmpty( moniker ))
return;
// Check it
var name = string.Format( "{0}Decoder", forType );
// Process
using (var decoder = ComIdentity.Create<IBaseFilter>( moniker ))
{
// Create
DirectShowObject.AddFilter( decoder.Interface, name );
// Call helper
if (processor != null)
processor( decoder );
}
}
/// <summary>
/// Versucht den DVB.NET Datenstrom direkt mit dem zugehörigen Decoder zu verbinden.
/// </summary>
/// <param name="decoder">Ein manuell angelegter Decoder.</param>
/// <param name="source">Der zu verwendende Ausgang.</param>
/// <param name="mediaType">Das verwendete Format.</param>
/// <returns>Gesetzt, wenn die Verbindung aufgebaut wurde.</returns>
private bool TryDirectConnect( TypedComIdentity<IBaseFilter> decoder, OutputPin source, MediaType mediaType )
{
// In normal cases we should directly connect to the filter so try
var connected = false;
// Try manual connect
decoder.InspectAllPins( p => p.QueryDirection() == PinDirection.Input,
pin =>
{
// Skip on error
try
{
// Get the raw interface for the media type
var type = mediaType.GetReference();
// Process
using (var iFace = ComIdentity.Create<IPin>( pin ))
source.Connect( iFace.Interface, type );
// Did it
connected = true;
}
catch (Exception)
{
}
// First pin only - even if it can not be used!
return false;
} );
// Failed
if (!connected)
return false;
// Find the output of the decoder and render it
decoder.InspectAllPins( p => p.QueryDirection() == PinDirection.Output,
pin =>
{
// Create helper
using (var pinWrapper = ComIdentity.Create<IPin>( pin ))
DirectShowObject.Render( pinWrapper.Interface );
// Did it
return false;
} );
// Report
return connected;
}
/// <summary>
/// Erzuegt den Anzeigegraphen.
/// </summary>
/// <param name="mpeg4">Gesetzt für H.264 Bildinformationen - ansonsten wird MPEG-2 erwartet.</param>
/// <param name="ac3">Gesetzt für AC3 Toninformationen - ansonsten wird MP2 erwartet.</param>
public void Show( bool mpeg4, bool ac3 )
{
// Startup
bool firstCall = PrepareShow();
// Leave fullscreen before stopping graph
bool fullscreen = FullScreen;
if (fullscreen)
FullScreen = false;
// Stop the graph
Stop();
// Forget the clock
DisposeClock();
// Remove all filters not created by us
var noRemove = new HashSet<IntPtr>( m_Filters.Values.Select( f => f.Interface ) );
((IFilterGraph) m_Graph).InspectFilters( filter =>
{
// Create report structure
var info = new FilterInfo();
// Load it
filter.QueryFilterInfo( ref info );
// Forget about graph
BDAEnvironment.Release( ref info.Graph );
// Process using raw interfaces
using (var id = ComIdentity.Create( filter ))
if (!noRemove.Contains( id.Interface ))
try
{
// Repoert
if (DirectShowTraceSwitch.Enabled)
Trace.WriteLine( string.Format( Properties.Resources.Trace_RemoveFilter, info.Name ), DirectShowTraceSwitch.DisplayName );
// Process
DirectShowObject.RemoveFilter( id.Interface );
}
catch (Exception e)
{
// Report
Trace.WriteLine( string.Format( Properties.Resources.Trace_Exception_RemoveFilter, info.Name, e.Message ), DirectShowTraceSwitch.DisplayName );
}
} );
// Create media types
var videoType = CreateVideoType( mpeg4, UseCyberlink );
var audioType = CreateAudioType( ac3 );
// Configure injection pins
InjectorFilter.SetAudioType( audioType );
InjectorFilter.SetVideoType( videoType );
// Helper
var audioConnected = false;
var videoConnected = false;
// Pre-loaded decoders
LoadDecoder( mpeg4 ? H264Decoder : MPEG2Decoder, mpeg4 ? "HDTV-Video" : "SDTV-Video", decoder => videoConnected = TryDirectConnect( decoder, InjectorFilter.VideoPin, videoType ) );
LoadDecoder( ac3 ? AC3Decoder : MP2Decoder, ac3 ? "AC3-Audio" : "MP2-Audio", decoder => audioConnected = TryDirectConnect( decoder, InjectorFilter.AudioPin, audioType ) );
// Create display
if (!audioConnected)
DirectShowObject.Render( InjectorFilter.AudioPin.Interface );
if (!videoConnected)
DirectShowObject.Render( InjectorFilter.VideoPin.Interface );
// Show for the first time
var vmr = VMR;
if (firstCall)
if (vmr != null)
if (m_VideoWindow != null)
{
// Respect window settings
vmr.ClippingWindow = m_VideoWindow;
vmr.AdjustSize( m_VideoWindow );
}
// Use it
m_Clock = new NoMarshalComObjects.ReferenceClock( Activator.CreateInstance( Type.GetTypeFromCLSID( Constants.CLSID_SystemClock ) ), true );
// Attach to graph
GraphAsFilter.SetSyncSource( m_Clock.ComInterface );
// Time to restart the graph
Run();
// Reinstall volume
if (m_LastVolume.HasValue)
Volume = m_LastVolume.Value;
// Reinstall picture parameters
if (m_PictureParameters != null)
if (vmr != null)
m_PictureParameters.Update( vmr );
// Back to full screen mode
if (fullscreen)
FullScreen = true;
}
/// <summary>
/// Liest oder verändert die Darstellungsparameter des Videobildes.
/// </summary>
/// <exception cref="ArgumentNullException">Die Eigenschaft darf nicht <i>null</i> sein.</exception>
public PictureParameters PictureParameters
{
get
{
// None
var vmr = VMR;
if (vmr == null)
return null;
// Be safe
try
{
// Always create new
return new PictureParameters( vmr );
}
catch
{
// Maybe there is no hardware support
return null;
}
}
set
{
// Validate
if (value == null)
throw new ArgumentNullException( "value" );
// Remember last setting
m_PictureParameters = value;
// Store
var vmr = VMR;
if (vmr != null)
value.Update( vmr );
}
}
/// <summary>
/// Liest oder verändert die Lautstärke.
/// </summary>
public byte Volume
{
get
{
// Find audio control
var audio = (IBasicAudio) DirectShowObject;
if (audio == null)
return 0;
// Load the data
int current = audio.Volume;
// Find the first match
for (int i = m_VolumeMap.Length; i-- > 0; )
if (current >= m_VolumeMap[i])
return (byte) i;
// Must be silent
return 0;
}
set
{
// Correct
if (value > 100)
value = 100;
// Remember
m_LastVolume = value;
// Be safe
try
{
// Find audio control
var audio = (IBasicAudio) DirectShowObject;
if (audio != null)
audio.Volume = m_VolumeMap[value];
}
catch
{
// Ignore any error
}
}
}
/// <summary>
/// Legt fest, wo die Ausgabe des Bilds erfolgen soll.
/// </summary>
public Control VideoWindow
{
set
{
// Remember
m_VideoWindow = value;
// Register
m_VideoWindow.SizeChanged += ( s, e ) => { var v = VMR; if (v != null) v.AdjustSize( m_VideoWindow ); };
// Connect
var vmr = VMR;
if (vmr != null)
vmr.ClippingWindow = m_VideoWindow;
}
}
/// <summary>
/// Erzeugt die Anzeigekomponent des Graphen.
/// </summary>
private void CreateVMR()
{
// Test state
var vmr = VMR;
if (vmr != null)
return;
// Create the VMR
vmr = VMR.Create();
try
{
// Add the VMR
AddFilter( Filter_VMR, vmr );
}
catch
{
// Cleanup
vmr.Dispose();
// Forward
throw;
}
}
/// <summary>
/// Meldet das aktuelle Seitenverhältnis des Bildes.
/// </summary>
public double? CurrentAspectRatio
{
get
{
// Report
var vmr = VMR;
if (vmr == null)
return null;
else
return vmr.CurrentAspectRatio;
}
}
/// <summary>
/// Gibt alle internen Strukturen frei.
/// </summary>
private void DestroyGraph()
{
// Get rid of clock
DisposeClock();
// Try stop and delete all filters
if (null != m_Graph)
try
{
// Stop self
GraphAsFilter.Stop();
}
catch
{
}
// Filters first
foreach (var filter in m_Filters.Values)
filter.Dispose();
// Clear list
m_Filters.Clear();
// Forward
using (m_Register)
m_Register = null;
// Must cleanup COM references
BDAEnvironment.Release( ref m_Graph );
// Must cleanup file
using (m_LogFile)
m_LogFile = null;
// Clear helpers and reset flags
m_VideoWindowCreated = false;
m_VideoPID = 0;
m_AudioPID = 0;
// TS Filter
using (InjectorFilter)
InjectorFilter = null;
}
/// <summary>
/// Legt die Datenstromkennungen für Bild und Ton fest.
/// </summary>
/// <param name="video">Die Datenstromkennung für das Bild.</param>
/// <param name="audio">Die Datenstromkennung für den Ton.</param>
public void SetPIDs( ushort video, ushort audio )
{
// Remember
m_VideoPID = video;
m_AudioPID = audio;
}
/// <summary>
/// Liest oder setzt die Vollbilddarstellung.
/// </summary>
public bool FullScreen
{
get
{
// Get the video window
var video = DirectShowObject as IVideoWindow;
// Report
return (video.FullScreenMode != 0);
}
set
{
// Get the video window
var video = DirectShowObject as IVideoWindow;
// Change fullscreen mode
video.FullScreenMode = value ? -1 : 0;
}
}
/// <summary>
/// Deaktiviert die Bildüberlagerung.
/// </summary>
public void ClearOverlayBitmap()
{
// Create structure
var param = new VMRAlphaBitmap { Flags = VMRAlphaBitmapFlags.Disable };
// Forward
using (var vmr = VMR.MarshalToManaged())
((IVMRMixerBitmap) vmr.Object).SetAlphaBitmap( ref param );
}
/// <summary>
/// Aktiviert die Bildüberlagerung.
/// </summary>