-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAsyncVideoSource.cs
210 lines (189 loc) · 5.97 KB
/
AsyncVideoSource.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
namespace AForge.Video
{
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Threading;
public class AsyncVideoSource : IVideoSource
{
private int framesProcessed;
private Thread imageProcessingThread;
private AutoResetEvent isNewFrameAvailable;
private ManualResetEvent isProcessingThreadAvailable;
private Bitmap lastVideoFrame;
private readonly IVideoSource nestedVideoSource;
private bool skipFramesIfBusy;
public event NewFrameEventHandler NewFrame;
public event PlayingFinishedEventHandler PlayingFinished
{
add
{
this.nestedVideoSource.PlayingFinished += value;
}
remove
{
this.nestedVideoSource.PlayingFinished -= value;
}
}
public event VideoSourceErrorEventHandler VideoSourceError
{
add
{
this.nestedVideoSource.VideoSourceError += value;
}
remove
{
this.nestedVideoSource.VideoSourceError -= value;
}
}
public AsyncVideoSource(IVideoSource nestedVideoSource)
{
this.nestedVideoSource = nestedVideoSource;
}
public AsyncVideoSource(IVideoSource nestedVideoSource, bool skipFramesIfBusy)
{
this.nestedVideoSource = nestedVideoSource;
this.skipFramesIfBusy = skipFramesIfBusy;
}
private void Free()
{
if (this.imageProcessingThread != null)
{
this.nestedVideoSource.NewFrame -= new NewFrameEventHandler(this.nestedVideoSource_NewFrame);
this.lastVideoFrame = null;
this.isNewFrameAvailable.Set();
this.imageProcessingThread.Join();
this.imageProcessingThread = null;
this.isNewFrameAvailable.Close();
this.isNewFrameAvailable = null;
this.isProcessingThreadAvailable.Close();
this.isProcessingThreadAvailable = null;
}
}
private void imageProcessingThread_Worker()
{
while (true)
{
this.isNewFrameAvailable.WaitOne();
if (this.lastVideoFrame == null)
{
return;
}
this.isProcessingThreadAvailable.Reset();
if (this.NewFrame != null)
{
this.NewFrame(this, new NewFrameEventArgs(this.lastVideoFrame));
}
this.lastVideoFrame.Dispose();
this.framesProcessed++;
this.isProcessingThreadAvailable.Set();
}
}
private void nestedVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (this.NewFrame != null)
{
if (this.skipFramesIfBusy)
{
if (!this.isProcessingThreadAvailable.WaitOne(0, false))
{
return;
}
}
else
{
this.isProcessingThreadAvailable.WaitOne();
}
this.lastVideoFrame = (Bitmap) eventArgs.Frame.Clone();
this.isNewFrameAvailable.Set();
}
}
public void SignalToStop()
{
this.nestedVideoSource.SignalToStop();
}
public void Start()
{
if (!this.IsRunning)
{
this.framesProcessed = 0;
this.isNewFrameAvailable = new AutoResetEvent(false);
this.isProcessingThreadAvailable = new ManualResetEvent(true);
this.imageProcessingThread = new Thread(new ThreadStart(this.imageProcessingThread_Worker));
this.imageProcessingThread.Start();
this.nestedVideoSource.NewFrame += new NewFrameEventHandler(this.nestedVideoSource_NewFrame);
this.nestedVideoSource.Start();
}
}
public void Stop()
{
this.nestedVideoSource.Stop();
this.Free();
}
public void WaitForStop()
{
this.nestedVideoSource.WaitForStop();
this.Free();
}
public int BytesReceived
{
get
{
return this.nestedVideoSource.BytesReceived;
}
}
public int FramesProcessed
{
get
{
int framesProcessed = this.framesProcessed;
this.framesProcessed = 0;
return framesProcessed;
}
}
public int FramesReceived
{
get
{
return this.nestedVideoSource.FramesReceived;
}
}
public bool IsRunning
{
get
{
bool isRunning = this.nestedVideoSource.IsRunning;
if (!isRunning)
{
this.Free();
}
return isRunning;
}
}
public IVideoSource NestedVideoSource
{
get
{
return this.nestedVideoSource;
}
}
public bool SkipFramesIfBusy
{
get
{
return this.skipFramesIfBusy;
}
set
{
this.skipFramesIfBusy = value;
}
}
public string Source
{
get
{
return this.nestedVideoSource.Source;
}
}
}
}