forked from OfItselfSo/Tanta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
1295 lines (1151 loc) · 59.3 KB
/
frmMain.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.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MediaFoundation;
using MediaFoundation.ReadWrite;
using MediaFoundation.Misc;
using MediaFoundation.Transform;
using MediaFoundation.Alt;
using MediaFoundation.EVR;
using OISCommon;
using TantaCommon;
/// +------------------------------------------------------------------------------------------------------------------------------+
/// ¦ TERMS OF USE: MIT License ¦
/// +------------------------------------------------------------------------------------------------------------------------------¦
/// ¦Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation ¦
/// ¦files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, ¦
/// ¦modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software¦
/// ¦is furnished to do so, subject to the following conditions: ¦
/// ¦ ¦
/// ¦The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.¦
/// ¦ ¦
/// ¦THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ¦
/// ¦WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR ¦
/// ¦COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ¦
/// ¦ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ¦
/// +------------------------------------------------------------------------------------------------------------------------------+
/// Notes
/// Some parts of this code may be derived from the samples which ships with the MF.Net dll. These are
/// in turn derived from the original Microsoft samples. These have been placed in the public domain
/// without copyright.
///
/// The MF.Net library itself is licensed under the GNU Library or Lesser General Public License version 2.0 (LGPLv2)
///
/// SUPER IMPORTANT NOTE: You MUST use the [MTAThread] to decorate your entry point method. If you use the default [STAThread]
/// you may get errors. See the Program.cs file for details.
/// The function of this app is to allow the user to choose a video device and to both display the image data from that
/// video device on the screen and (optionally) write that data to a file for recording purposes. The app uses a
/// standard pipeline topology in which the video device acts as the source and the EVR acts as the sink.
///
/// However, a Transform has been inserted into the pipeline. In normal operation all this transform does is
/// present its input data to the output. If a sink writer has been configured on the Transform then the
/// MFT will also give the sample data to the sink writer which writes it out to disk as an MP4 file - thus
/// recording it.
///
/// In effect, the Transform demonstrates how to present pipeline based data to other non-pipleline based
/// objects.
namespace TantaCaptureToScreenAndFile
{
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// The main form for the application
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public partial class frmMain : frmOISBase
{
private const string DEFAULTLOGDIR = @"C:\Dump\Project Logs";
private const string APPLICATION_NAME = "TantaCaptureToScreenAndFile";
private const string APPLICATION_VERSION = "01.00";
private const string START_CAPTURE = "Start Capture";
private const string STOP_CAPTURE = "Stop Capture";
private const string RECORDING_IS_ON = "Recording is ON";
private const string RECORDING_IS_OFF = "Recording is OFF";
private const string DEFAULT_SOURCE_DEVICE = @"<No Video Device Selected>";
private const string DEFAULT_OUTPUT_FILE = @"C:\Dump\TantaCaptureToScreenAndFile_Capture.mp4";
// the call back handler for the mediaSession
private TantaAsyncCallbackHandler mediaSessionAsyncCallbackHandler = null;
// A session provides playback controls for the media content. The Media Session and the protected media path (PMP) session objects
// expose this interface. This interface is the primary interface that applications use to control the Media Foundation pipeline.
// In this app we want the copy to proceed as fast as possible so we do not implement any of the usual session control items.
protected IMFMediaSession mediaSession;
// Media sources are objects that generate media data. For example, the data might come from a video file, a network stream,
// or a hardware device, such as a camera. Each media source contains one or more streams, and each stream delivers
// data of one type, such as audio or video.
protected IMFMediaSource mediaSource;
// The Enhanced Video Renderer(EVR) implements this interface and it controls how the EVR presenter displays video.
// The EVR also a sink but we do not really use it as one - that functionality is largely internal to the pipeline.
// we only get access to this object once the topology has been resolved. We still have to release it though!
protected IMFVideoDisplayControl evrVideoDisplay;
// we are using a custom transform to intercept the information as it moves through the
// pipeline. If recording is enabled, it takes a copy of the media samples and then presents
// this data to a SinkWriter to be saved. This is an IMFTransform
protected MFTTantaSampleGrabber_Sync sampleGrabberTransform = null;
// this is the current type of the video stream. We need this to set up the sink writer
// properly. This must be released at the end
protected IMFMediaType currentVideoMediaType = null;
// our thread safe screen update delegate
public delegate void ThreadSafeScreenUpdate_Delegate(object obj, bool captureIsActive, string displayText);
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Constructor
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public frmMain()
{
bool retBOOL = false;
HResult hr = 0;
if (DesignMode == false)
{
// set the current directory equal to the exe directory. We do this because
// people can start from a link and if the start-in directory is not right
// it can put the log file in strange places
Directory.SetCurrentDirectory(Application.StartupPath);
// set up the Singleton g_Logger instance. Simply using it in a test
// creates it.
if (g_Logger == null)
{
// did not work, nothing will start say so now in a generic way
MessageBox.Show("Logger Class Failed to Initialize. Nothing will work well.");
return;
}
// record this in the logger for everybodys use
g_Logger.ApplicationMainForm = this;
g_Logger.DefaultDialogBoxTitle = APPLICATION_NAME;
try
{
// set the icon for this form and for all subsequent forms
g_Logger.AppIcon = new Icon(GetType(), "App.ico");
this.Icon = new Icon(GetType(), "App.ico");
}
catch (Exception)
{
}
// Register the global error handler as soon as we can in Main
// to make sure that we catch as many exceptions as possible
// this is a last resort. All execeptions should really be trapped
// and handled by the code.
OISGlobalExceptions ex1 = new OISGlobalExceptions();
Application.ThreadException += new ThreadExceptionEventHandler(ex1.OnThreadException);
// set the culture so our numbers convert consistently
System.Threading.Thread.CurrentThread.CurrentCulture = g_Logger.GetDefaultCulture();
}
InitializeComponent();
if (DesignMode == false)
{
// set up our logging
retBOOL = g_Logger.InitLogging(DEFAULTLOGDIR, APPLICATION_NAME, false);
if (retBOOL == false)
{
// did not work, nothing will start say so now in a generic way
MessageBox.Show("The log file failed to create. No log file will be recorded.");
}
// pump out the header
g_Logger.EmitStandardLogfileheader(APPLICATION_NAME);
LogMessage("");
LogMessage("Version: " + APPLICATION_VERSION);
LogMessage("");
// a bit of setup
buttonStartStopPlay.Text = START_CAPTURE;
textBoxPickedVideoDeviceURL.Text = DEFAULT_SOURCE_DEVICE;
buttonRecordingOnOff.Text = RECORDING_IS_OFF;
SyncScreenControlsToCaptureState(false, null);
textBoxOutputFileNameAndPath.Text = DEFAULT_OUTPUT_FILE;
// we always have to initialize MF. The 0x00020070 here is the WMF version
// number used by the MF.Net samples. Not entirely sure if it is appropriate
hr = MFExtern.MFStartup(0x00020070, MFStartup.Full);
if (hr != 0)
{
LogMessage("Constructor: call to MFExtern.MFStartup returned " + hr.ToString());
}
// set up our Video Picker Control
ctlTantaVideoPicker1.VideoDevicePickedEvent += new ctlTantaVideoPicker.VideoDevicePickedEventHandler(VideoDevicePickedHandler);
ctlTantaVideoPicker1.VideoFormatPickedEvent += new ctlTantaVideoPicker.VideoFormatPickedEventHandler(VideoFormatPickedHandler);
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// The form loaded handler
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
private void frmMain_Load(object sender, EventArgs e)
{
LogMessage("frmMain_Load");
try
{
// enumerate all video devices and display their formats
ctlTantaVideoPicker1.DisplayVideoCaptureDevices();
ctlTantaEVRStreamDisplay1.InitMediaPlayer();
}
catch (Exception ex)
{
// something went wrong
OISMessageBox("An error occurred\n\n" + ex.Message + "\n\nPlease see the logs");
LogMessage("frmMain_Load " + ex.Message);
LogMessage("frmMain_Load " + ex.StackTrace);
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// The form closing handler
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
LogMessage("frmMain_FormClosing");
try
{
// do everything to close all media devices
CloseAllMediaDevices();
// Shut down MF
MFExtern.MFShutdown();
}
catch
{
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// A centralized place to close down all media devices.
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
private void CloseAllMediaDevices()
{
HResult hr;
LogMessage("CloseAllMediaDevices");
// if we are recording we had better stop now
StopRecording();
// close and release our call back handler
if (mediaSessionAsyncCallbackHandler != null)
{
// stop any messaging or events in the call back handler
mediaSessionAsyncCallbackHandler.ShutDown();
mediaSessionAsyncCallbackHandler = null;
}
// close the session (this is NOT the same as shutting it down)
if (mediaSession != null)
{
hr = mediaSession.Close();
if (hr != HResult.S_OK)
{
// just log it
LogMessage("CloseAllMediaDevices call to mediaSession.Close failed. Err=" + hr.ToString());
}
}
// Shut down the media source
if (mediaSource != null)
{
hr = mediaSource.Shutdown();
if (hr != HResult.S_OK)
{
// just log it
LogMessage("CloseAllMediaDevices call to mediaSource.Shutdown failed. Err=" + hr.ToString());
}
Marshal.ReleaseComObject(mediaSource);
mediaSource = null;
}
// Shut down the media session (note we only closed it before).
if (mediaSession != null)
{
hr = mediaSession.Shutdown();
if (hr != HResult.S_OK)
{
// just log it
LogMessage("CloseAllMediaDevices call to mediaSession.Shutdown failed. Err=" + hr.ToString());
}
Marshal.ReleaseComObject(mediaSession);
mediaSession = null;
}
// close down the display
ctlTantaEVRStreamDisplay1.ShutDownFilePlayer();
// close the evrvideodisplay
if (evrVideoDisplay != null)
{
Marshal.ReleaseComObject(evrVideoDisplay);
evrVideoDisplay = null;
}
if (currentVideoMediaType != null)
{
Marshal.ReleaseComObject(currentVideoMediaType);
currentVideoMediaType = null;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Gets the source filename. Will never return null, will return ""
/// There is no set accessor, This is obtained off the screen.
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public string VideoCaptureDeviceName
{
get
{
if (textBoxPickedVideoDeviceURL.Text == null) textBoxPickedVideoDeviceURL.Text = "";
return textBoxPickedVideoDeviceURL.Text;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Gets the video device
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public TantaMFVideoFormatContainer VideoFormatContainer
{
get
{
if ((textBoxPickedVideoDeviceURL.Tag is TantaMFVideoFormatContainer) == false)
{
textBoxPickedVideoDeviceURL.Tag = null;
return null;
}
return (textBoxPickedVideoDeviceURL.Tag as TantaMFVideoFormatContainer);
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Gets the output filename. Will never return null, will return ""
/// There is no set accessor, This is obtained off the screen.
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public string OutputFileName
{
get
{
if (textBoxOutputFileNameAndPath.Text == null) textBoxOutputFileNameAndPath.Text = "";
return textBoxOutputFileNameAndPath.Text;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Starts/Stops the capture
///
/// Because this code is intended for demo purposes, and in the interests of
/// reducing complexity, it is extremely linear, step-by-step and kicked off
/// directly from a button press in the main form. Doubtless there is much
/// refactoring that could be done.
///
/// </summary>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
private void buttonStartStopPlay_Click(object sender, EventArgs e)
{
// this code toggles both the start and stop capture. Since the
// STOP code is much simpler we test for it first. We use the
// text on the button to detect if we are capturing or not.
if (buttonStartStopPlay.Text == STOP_CAPTURE)
{
// do everything to close all media devices
// the MF itself is still active.
CloseAllMediaDevices();
// re-enable our screen controls
SyncScreenControlsToCaptureState(false, null);
return;
}
// ####
// #### below here we assume we are starting the capture
// ####
try
{
// check our source filename is correct and usable
if ((VideoCaptureDeviceName == null) || (VideoCaptureDeviceName.Length == 0))
{
OISMessageBox("No Source Filename and path. Cannot continue.");
return;
}
if(VideoFormatContainer == null)
{
OISMessageBox("The video device and format is unknown.\n\nHave you selected a video device and format?");
return;
}
// check our output filename is correct and usable
if ((OutputFileName == null) || (OutputFileName.Length == 0))
{
OISMessageBox("No Output Filename and path. Cannot continue.");
return;
}
if (Path.IsPathRooted(OutputFileName) == false)
{
OISMessageBox("No Output Filename and path is not rooted. A full directory and path is required. Cannot continue.");
return;
}
// check the directory of the path exists
string dirName = Path.GetDirectoryName(OutputFileName);
if (Directory.Exists(dirName) == false)
{
OISMessageBox("The output directory does not exist. A full directory and path is required. Cannot continue.");
return;
}
// set up a session, topology and open the media source and sink etc
PrepareSessionAndTopology(VideoFormatContainer);
// disable our screen controls
SyncScreenControlsToCaptureState(true, null);
}
finally
{
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Sync the state on the screen controls to the current capture state
/// </summary>
/// <param name="captureIsActive">if true we set the controls to enabled</param>
/// <param name="displayText">Text to display in a message box. If null we ignore</param>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
private void SyncScreenControlsToCaptureState(bool captureIsActive, string displayText)
{
if (captureIsActive == false)
{
textBoxOutputFileNameAndPath.Enabled = true;
labelVideoCaptureDeviceName.Enabled = true;
textBoxOutputFileNameAndPath.Enabled = true;
labelOutputFileName.Enabled = true;
ctlTantaVideoPicker1.Enabled = true;
buttonStartStopPlay.Text = START_CAPTURE;
buttonRecordingOnOff.Enabled = false;
buttonRecordingOnOff.Text = RECORDING_IS_OFF;
checkBoxTimeBaseRebase.Enabled = false;
}
else
{
// set this
textBoxPickedVideoDeviceURL.Enabled = false;
labelVideoCaptureDeviceName.Enabled = false;
textBoxOutputFileNameAndPath.Enabled = false;
labelOutputFileName.Enabled = false;
ctlTantaVideoPicker1.Enabled = false;
buttonStartStopPlay.Text = STOP_CAPTURE;
buttonRecordingOnOff.Enabled = true;
buttonRecordingOnOff.Text = RECORDING_IS_OFF;
checkBoxTimeBaseRebase.Enabled = true;
}
if ((displayText != null) && (displayText.Length != 0))
{
OISMessageBox(displayText);
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Sets the enabled state on the screen controls in a thread safe way
/// </summary>
/// <param name="captureIsActive">if true we set the controls to enabled</param>
/// <param name="displayText">Text to display in a message box. If null we ignore</param>
/// <history>
/// 01 Nov 18 Cynic - Started
/// </history>
public void ThreadSafeScreenUpdate(object caller, bool captureIsActive, string displayText)
{
// log it - the logger is thread safe!
LogMessage("ThreadSafeScreenUpdate, wantEnable=" + captureIsActive.ToString());
// Ok, you probably already know this but I'll note it here because this is so important
// You do NOT want to update any form controls from a thread that is not the forms main
// thread. Very odd, intermittent and hard to debug problems will result. Even if your
// handler does not actually update any form controls do not do it! Sooner or later you
// or someone else will make changes that calls something that eventually updates a
// form or control and then you will have introduced a really hard to find bug.
// So, we always use the InvokeRequired...Invoke sequence to get us back on the form thread
if (InvokeRequired == true)
{
// call ourselves again but this time be on the form thread.
Invoke(new ThreadSafeScreenUpdate_Delegate(ThreadSafeScreenUpdate), new object[] { caller, captureIsActive, displayText });
return;
}
// if we get here we are assured we are on the form thread.
SyncScreenControlsToCaptureState(captureIsActive, displayText);
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Opens and prepares the media session and topology and opens the media source
/// and media sink.
///
/// Once the session and topology are setup, a MESessionTopologySet event
/// will be triggered in the callback handler. After that the events there
/// trigger other events and everything rolls along automatically.
/// </summary>
/// <param name="videoCaptureDevice">the video capture device name</param>
/// <history>
/// 01 Nov 18 Cynic - Originally Written
/// </history>
public void PrepareSessionAndTopology(TantaMFVideoFormatContainer videoFormatContainer)
{
HResult hr;
IMFSourceResolver pSourceResolver = null;
IMFTopology topologyObj = null;
IMFPresentationDescriptor sourcePresentationDescriptor = null;
int sourceStreamCount = 0;
bool streamIsSelected = false;
IMFStreamDescriptor videoStreamDescriptor = null;
IMFTopologyNode sourceVideoNode = null;
IMFTopologyNode outputSinkNodeVideo = null;
IMFTopologyNode transformNode = null;
LogMessage("PrepareSessionAndTopology ");
// we sanity check the video source device
if (videoFormatContainer == null)
{
throw new Exception("PrepareSessionAndTopology: videoFormatContainer is invalid. Cannot continue.");
}
if (videoFormatContainer.VideoDevice == null)
{
throw new Exception("PrepareSessionAndTopology: VideoDevice is invalid. Cannot continue.");
}
if ((videoFormatContainer.VideoDevice.SymbolicName == null) || (videoFormatContainer.VideoDevice.SymbolicName.Length == 0))
{
throw new Exception("PrepareSessionAndTopology: VideoDevice.SymbolicName is invalid. Cannot continue.");
}
try
{
// reset everything
CloseAllMediaDevices();
// Create the media session.
hr = MFExtern.MFCreateMediaSession(null, out mediaSession);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateMediaSession failed. Err=" + hr.ToString());
}
if (mediaSession == null)
{
throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateMediaSession failed. mediaSession == null");
}
// set up our media session call back handler.
mediaSessionAsyncCallbackHandler = new TantaAsyncCallbackHandler();
mediaSessionAsyncCallbackHandler.Initialize();
mediaSessionAsyncCallbackHandler.MediaSession = mediaSession;
mediaSessionAsyncCallbackHandler.MediaSessionAsyncCallBackError = HandleMediaSessionAsyncCallBackErrors;
mediaSessionAsyncCallbackHandler.MediaSessionAsyncCallBackEvent = HandleMediaSessionAsyncCallBackEvent;
// Register the callback handler with the session and tell it that events can
// start. This does not actually trigger an event it just lets the media session
// know that it can now send them if it wishes to do so.
hr = mediaSession.BeginGetEvent(mediaSessionAsyncCallbackHandler, null);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to mediaSession.BeginGetEvent failed. Err=" + hr.ToString());
}
// Create a new topology. A topology describes a collection of media sources, sinks, and transforms that are
// connected in a certain order. These objects are represented within the topology by topology nodes,
// which expose the IMFTopologyNode interface. A topology describes the path of multimedia data through these nodes.
hr = MFExtern.MFCreateTopology(out topologyObj);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateTopology failed. Err=" + hr.ToString());
}
if (topologyObj == null)
{
throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateTopology failed. topologyObj == null");
}
// ####
// #### we now create the media source, this is video device (camera)
// ####
// use the device symbolic name to create the media source for the video device. Media sources are objects that generate media data.
// For example, the data might come from a video file, a network stream, or a hardware device, such as a camera. Each
// media source contains one or more streams, and each stream delivers data of one type, such as audio or video.
mediaSource = TantaWMFUtils.GetMediaSourceFromTantaDevice(videoFormatContainer.VideoDevice);
if (mediaSource == null)
{
throw new Exception("PrepareSessionAndTopology call to mediaSource == null");
}
// A presentation is a set of related media streams that share a common presentation time. We now get a copy of the media
// source's presentation descriptor. Applications can use the presentation descriptor to select streams
// and to get information about the source content.
hr = mediaSource.CreatePresentationDescriptor(out sourcePresentationDescriptor);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to mediaSource.CreatePresentationDescriptor failed. Err=" + hr.ToString());
}
if (sourcePresentationDescriptor == null)
{
throw new Exception("PrepareSessionAndTopology call to mediaSource.CreatePresentationDescriptor failed. sourcePresentationDescriptor == null");
}
// Now we get the number of stream descriptors in the presentation. A presentation descriptor contains a list of one or more
// stream descriptors. These describe the streams in the presentation. Streams can be either selected or deselected. Only the
// selected streams produce data. Deselected streams are not active and do not produce any data.
hr = sourcePresentationDescriptor.GetStreamDescriptorCount(out sourceStreamCount);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to sourcePresentationDescriptor.GetStreamDescriptorCount failed. Err=" + hr.ToString());
}
if (sourceStreamCount == 0)
{
throw new Exception("PrepareSessionAndTopology call to sourcePresentationDescriptor.GetStreamDescriptorCount failed. sourceStreamCount == 0");
}
// Look at each stream, there can be more than one stream here
// Usually only one is enabled. This app uses the first "selected"
// stream we come to which has the appropriate media type
// look for the video stream
for (int i = 0; i < sourceStreamCount; i++)
{
// we require the major type to be video
Guid guidMajorType = TantaWMFUtils.GetMajorMediaTypeFromPresentationDescriptor(sourcePresentationDescriptor, i);
if (guidMajorType != MFMediaType.Video) continue;
// we also require the stream to be enabled
hr = sourcePresentationDescriptor.GetStreamDescriptorByIndex(i, out streamIsSelected, out videoStreamDescriptor);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to sourcePresentationDescriptor.GetStreamDescriptorByIndex(v) failed. Err=" + hr.ToString());
}
if (videoStreamDescriptor == null)
{
throw new Exception("PrepareSessionAndTopology call to sourcePresentationDescriptor.GetStreamDescriptorByIndex(v) failed. videoStreamDescriptor == null");
}
// if the stream is selected, leave now we will release the videoStream descriptor later
if (streamIsSelected == true) break;
// release the one we are not using
if (videoStreamDescriptor != null)
{
Marshal.ReleaseComObject(videoStreamDescriptor);
videoStreamDescriptor = null;
}
}
// by the time we get here we should have a video StreamDescriptor if
// we do not, then we cannot proceed.
if (videoStreamDescriptor == null)
{
throw new Exception("PrepareSessionAndTopology call to sourcePresentationDescriptor.GetStreamDescriptorByIndex failed. videoStreamDescriptor == null");
}
// sets the current media type on a stream descriptor by matching
// its mediaTypes to the video format container contents. We know we will
// get a match because our video format picker enumerated all the formats
// for us and thus we chose one we already know exists.
hr = TantaWMFUtils.SetCurrentMediaTypeOnIMFStreamDescriptorByFormatContainer(videoStreamDescriptor, videoFormatContainer);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to SetCurrentMediaTypeOnIMFStreamDescriptorByFormatContainer failed. Err=" + hr.ToString());
}
// ####
// #### when we create the sink writer to record the video data we will need the types from the stream to do
// #### this which is why we get this now.
// ####
currentVideoMediaType = TantaWMFUtils.GetCurrentMediaTypeFromStreamDescriptor(videoStreamDescriptor);
if (currentVideoMediaType == null)
{
throw new Exception("PrepareSessionAndTopology call to currentVideoMediaType == null");
}
// ####
// #### Create the custom sample grabber transform which will send a copy of the data
// #### to the SinkWriter for recording purposes
// ####
sampleGrabberTransform = new MFTTantaSampleGrabber_Sync();
// ####
// #### we now make up a topology branch for the video stream
// ####
// Create a source Video node for this stream.
sourceVideoNode = TantaWMFUtils.CreateSourceNodeForStream(mediaSource, sourcePresentationDescriptor, videoStreamDescriptor);
if (sourceVideoNode == null)
{
throw new Exception("PrepareSessionAndTopology call to CreateSourceNodeForStream(v) failed. sourceAudioNode == null");
}
// Create the Video sink node.
outputSinkNodeVideo = TantaWMFUtils.CreateEVRRendererOutputNodeForStream(this.ctlTantaEVRStreamDisplay1.DisplayPanelHandle);
if (outputSinkNodeVideo == null)
{
throw new Exception("PrepareSessionAndTopology call to MFCreateTopologyNode(v) failed. outputSinkNodeVideo == null");
}
// Create the transform node.
hr = MFExtern.MFCreateTopologyNode(MFTopologyType.TransformNode, out transformNode);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to MFExtern.MFCreateTopologyNode failed. Err=" + hr.ToString());
}
// set the transform object (it is an IMFTransform) as an object on the transform node. Since it is already
// instantiated the topology does not need a GUID or activator to create it
hr = transformNode.SetObject(sampleGrabberTransform);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to pTransformNode.SetObject failed. Err=" + hr.ToString());
}
// Add the nodes to the topology. First the source node
hr = topologyObj.AddNode(sourceVideoNode);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to topologyObj.AddNode(sourceAudioNode) failed. Err=" + hr.ToString());
}
// add the output Node
hr = topologyObj.AddNode(outputSinkNodeVideo);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to topologyObj.AddNode(outputSinkNodeVideo) failed. Err=" + hr.ToString());
}
// add the transform Node
hr = topologyObj.AddNode(transformNode);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to topologyObj.AddNode(transformNode) failed. Err=" + hr.ToString());
}
// Connect the output stream from the source node to the input of the transform and the output from the transform to the
// input stream of the output node. The parameters are:
// dwOutputIndex - Zero-based index of the output stream on this node.
// *pDownstreamNode - Pointer to the IMFTopologyNode interface of the node to connect to.
// dwInputIndexOnDownstreamNode - Zero-based index of the input stream on the other node.
// Note even though the streamID from the source may be non zero it the output index of this node
// is still 0 since that is the only stream we have configured on it.
hr = sourceVideoNode.ConnectOutput(0, transformNode, 0);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to sourceVideoNode.ConnectOutput failed. Err=" + hr.ToString());
}
hr = transformNode.ConnectOutput(0, outputSinkNodeVideo, 0);
if (hr != HResult.S_OK)
{
throw new Exception("PrepareSessionAndTopology call to transformNode.ConnectOutput failed. Err=" + hr.ToString());
}
// Set the topology on the media session.
// If SetTopology succeeds, the media session will queue an
// MESessionTopologySet event. We can use that to discover the
// EVR display object
hr = mediaSession.SetTopology(0, topologyObj);
if (hr != HResult.S_OK)
{
// we failed
throw new Exception("PrepareSessionAndTopology mediaSession.SetTopology failed, retVal=" + hr.ToString());
}
// Release the topology
if (topologyObj != null)
{
Marshal.ReleaseComObject(topologyObj);
}
}
catch (Exception ex)
{
LogMessage("PrepareSessionAndTopology Error: " + ex.Message);
OISMessageBox(ex.Message);
}
finally
{
// Clean up
if (pSourceResolver != null)
{
Marshal.ReleaseComObject(pSourceResolver);
}
if (sourcePresentationDescriptor != null)
{
Marshal.ReleaseComObject(sourcePresentationDescriptor);
}
if (videoStreamDescriptor != null)
{
Marshal.ReleaseComObject(videoStreamDescriptor);
}
if (sourceVideoNode != null)
{
Marshal.ReleaseComObject(sourceVideoNode);
}
if (outputSinkNodeVideo != null)
{
Marshal.ReleaseComObject(outputSinkNodeVideo);
}
if (transformNode != null)
{
Marshal.ReleaseComObject(transformNode);
}
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Handles events reported by the media session TantaAsyncCallbackHandler
/// </summary>
/// <param name="sender">the object sending the event</param>
/// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
/// <param name="mediaEventType">the eventType, this is just an enum</param>
/// <history>
/// 01 Nov 18 Cynic - Originally Written
/// </history>
private void HandleMediaSessionAsyncCallBackEvent(object sender, IMFMediaEvent pEvent, MediaEventType mediaEventType)
{
LogMessage("Media Event Type " + mediaEventType.ToString());
switch (mediaEventType)
{
case MediaEventType.MESessionTopologyStatus:
// Raised by the Media Session when the status of a topology changes.
// Get the topology changed status code. This is an enum in the event
int i;
HResult hr = pEvent.GetUINT32(MFAttributesClsid.MF_EVENT_TOPOLOGY_STATUS, out i);
if (hr != HResult.S_OK)
{
throw new Exception("HandleMediaSessionAsyncCallBackEvent call to pEvent to get the status code failed. Err=" + hr.ToString());
}
// the one we are most interested in is i == MFTopoStatus.Ready
// which we get then the Topology is built and ready to run
HandleTopologyStatusChanged(pEvent, mediaEventType, (MFTopoStatus)i);
break;
case MediaEventType.MESessionStarted:
// Raised when the IMFMediaSession::Start method completes asynchronously.
// PlayerState = TantaEVRPlayerStateEnum.Started;
break;
case MediaEventType.MESessionPaused:
// Raised when the IMFMediaSession::Pause method completes asynchronously.
// PlayerState = TantaEVRPlayerStateEnum.Paused;
break;
case MediaEventType.MESessionStopped:
// Raised when the IMFMediaSession::Stop method completes asynchronously.
break;
case MediaEventType.MESessionClosed:
// Raised when the IMFMediaSession::Close method completes asynchronously.
break;
case MediaEventType.MESessionCapabilitiesChanged:
// Raised by the Media Session when the session capabilities change.
// You can use IMFMediaEvent::GetValue to figure out what they are
break;
case MediaEventType.MESessionTopologySet:
// Raised after the IMFMediaSession::SetTopology method completes asynchronously.
// The Media Session raises this event after it resolves the topology into a full topology and queues the topology for playback.
break;
case MediaEventType.MESessionNotifyPresentationTime:
// Raised by the Media Session when a new presentation starts.
// This event indicates when the presentation will start and the offset between the presentation time and the source time.
break;
case MediaEventType.MEEndOfPresentation:
// Raised by a media source when a presentation ends. This event signals that all streams
// in the presentation are complete. The Media Session forwards this event to the application.
// we cannot sucessfully .Finalize_ on the SinkWriter
// if we call CloseAllMediaDevices directly from this thread
// so we use an asynchronous method
Task taskA = Task.Run(() => CloseAllMediaDevices());
// we have to be on the form thread to update the screen
ThreadSafeScreenUpdate(this, false, null);
break;
case MediaEventType.MESessionRateChanged:
// Raised by the Media Session when the playback rate changes. This event is sent after the
// IMFRateControl::SetRate method completes asynchronously.
break;
default:
LogMessage("Unhandled Media Event Type " + mediaEventType.ToString());
break;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Handles topology status changes reported by the media session TantaAsyncCallbackHandler
/// </summary>
/// <param name="sender">the object sending the event</param>
/// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
/// <param name="mediaEventType">the eventType</param>
/// <param name="topoStatus">the topology status flag</param>
/// <history>
/// 01 Nov 18 Cynic - Originally Written
/// </history>
private void HandleTopologyStatusChanged(IMFMediaEvent mediaEvent, MediaEventType mediaEventType, MFTopoStatus topoStatus)
{
LogMessage("HandleTopologyStatusChanged event type: " + mediaEventType.ToString() + ", topoStatus=" + topoStatus.ToString());
if (topoStatus == MFTopoStatus.Ready)
{
MediaSessionTopologyNowReady(mediaEvent);
}
else
{
// we are not interested in any other status changes
return;
}
}
/// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
/// <summary>
/// Called when the topology status changes to ready. This status change
/// is generally signaled by the media session when it is fully configured.
/// </summary>
/// <param name="sender">the object sending the event</param>
/// <param name="mediaEvent">the event generated by the media session. Do NOT release this here.</param>
/// <param name="mediaEventType">the eventType</param>
/// <param name="topoStatus">the topology status flag</param>
/// <history>
/// 01 Nov 18 Cynic - Originally Written
/// </history>
private void MediaSessionTopologyNowReady(IMFMediaEvent mediaEvent)
{
HResult hr;
object evrVideoService;
LogMessage("MediaSessionTopologyNowReady");
// we need to obtain a reference to the EVR Video Display Control.
// We used an Activator to configure this in the Topology and so
// there is no reference to it at this point. However the media session
// knows about it and so we can get it from that.
// Ask for the IMFVideoDisplayControl interface. This interface is implemented by the EVR and is