forked from thedeemon/gep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleGrabberForm.cs
139 lines (127 loc) · 4.57 KB
/
SampleGrabberForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DirectShowLib;
using System.Runtime.InteropServices;
namespace gep
{
partial class SampleGrabberForm : Form
{
ISampleGrabber sampleGrabber;
SampleGrabberCallback cb;
Timer timer = new Timer();
Filter filter;
public SampleGrabberForm(ISampleGrabber sg, Filter f)
{
sampleGrabber = sg;
f.sampleGrabberForm = this;
filter = f;
cb = new SampleGrabberCallback();
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
Text = "Samples grabbed by " + f.Name;
}
void timer_Tick(object sender, EventArgs e)
{
lock (cb)
{
textBox.AppendText(cb.sb.ToString());
cb.sb = new StringBuilder();
}
}
private void SampleGrabberForm_Load(object sender, EventArgs e)
{
try
{
int hr = sampleGrabber.SetCallback(cb, 0);
DsError.ThrowExceptionForHR(hr);
}
catch (COMException ex)
{
Graph.ShowCOMException(ex, "Can't set callback");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception caught while setting callback for samplegrabber");
}
}
private void SampleGrabberForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
filter.sampleGrabberForm = null;
filter = null;
int hr = sampleGrabber.SetCallback(null, 0);
DsError.ThrowExceptionForHR(hr);
timer.Stop();
sampleGrabber = null;
}
catch (COMException ex)
{
Graph.ShowCOMException(ex, "Can't set callback to null");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception caught while setting callback for samplegrabber");
}
}
}//class
class SampleGrabberCallback : ISampleGrabberCB
{
public StringBuilder sb = new StringBuilder();
public SampleGrabberCallback()
{
}
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
return 0;
}
public int SampleCB(double SampleTime, IMediaSample pSample)
{
DateTime dt = DateTime.Now;
lock (this)
{
sb.AppendFormat("{0:T}.{1:D3}: ", dt, dt.Millisecond);
sb.AppendFormat("SampleTime={0:G4}, ", SampleTime);
if (pSample != null)
{
long start = 0, end = 0;
pSample.GetTime(out start, out end);
sb.AppendFormat("Time(start={0}, end={1}), ", start, end);
pSample.GetMediaTime(out start, out end);
sb.AppendFormat("MediaTime(start={0}, end={1}), ", start, end);
int len = pSample.GetActualDataLength();
sb.AppendFormat("data length={0}, ", len);
bool syncpoint = pSample.IsSyncPoint() == 0;
sb.AppendFormat("keyframe={0}", syncpoint);
if (pSample.IsDiscontinuity() == 0)
sb.Append(", Discontinuity");
if (pSample.IsPreroll() == 0)
sb.Append(", Preroll");
int n = Math.Min(len, 8);
IntPtr pbuf;
if (pSample.GetPointer(out pbuf) == 0)
{
byte[] buf = new byte[n];
Marshal.Copy(pbuf, buf, 0, n);
sb.Append(", Data=");
for (int i = 0; i < n; i++)
sb.AppendFormat("{0:X2}", buf[i]);
sb.Append("...");
}
}
else
sb.Append("pSample==NULL!");
sb.Append(Environment.NewLine);
}
Marshal.ReleaseComObject(pSample);
return 0;
}
}
}