-
Notifications
You must be signed in to change notification settings - Fork 0
/
blVideoThread.hpp
256 lines (200 loc) · 7.87 KB
/
blVideoThread.hpp
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
#ifndef BL_VIDEOTHREAD_HPP
#define BL_VIDEOTHREAD_HPP
//-------------------------------------------------------------------
// FILE: blVideoThread.hpp
// CLASS: blVideoThread
// BASE CLASS: sf::Thread,blCaptureDevice
//
// PURPOSE: Based on sf::Thread and blCaptureDevice, it
// allows us to retrieve frames from a video source,
// which is usually limited to 30 frames per second,
// in a parallel thread, allowing the main application
// to run at much faster rates
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
//
// LISENSE: MIT-LICENCE
// http://www.opensource.org/licenses/mit-license.php
// DEPENDENCIES: sf::Thread -- Derived from sf::Thread to handle
// threading in a cross-platform way
//
// blCaptureDevice -- We use a capture device to
// grab frames from a video source
// blImage -- We use a blImage to store the
// last captured frame
//
// NOTES:
//
// DATE CREATED: Feb/03/2011
// DATE UPDATED:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes and libs needed for this file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Enums needed for this file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class blVideoThread : public blCaptureDevice
{
public: // Constructors and destructors
// Default constructor
blVideoThread();
// Destructor
~blVideoThread()
{
stopCapturingThread();
}
public: // Public functions
// Functions used to start,stop,
// pause and unpause the thread
void startCapturingThread();
void stopCapturingThread();
void pauseCapturingThread();
// Function used to know whether
// there's a new image available
const bool& isNewFrameAvailable()const;
// Function used to tell the video
// thread that we grabbed the frame
// and the it should go ahead and
// grab another one
void letThreadQueryAnotherFrame();
// Function that gets called
// when thread is running
virtual void threadLoop();
// Function used to get
// the captured frame
const blImage< blColor3<unsigned char> >& getFrame()const;
protected: // Protected variables
// Frame image used in this thread
blImage< blColor3<unsigned char> > m_frame;
// Boolean variable used to
// continue/terminate the thread
bool m_isCapturingThreadToBeTerminated;
// Boolean variable used for
// pausing the video grabbing
bool m_isCapturingThreadPaused;
// Boolean variable used
// to check if new image
// is available from this thread
bool m_isNewFrameAvailable;
// The thread that will
// keep running the thread
// loop function
sf::Thread m_thread;
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline blVideoThread::blVideoThread() : m_thread(&blVideoThread::threadLoop,this)
{
// Default all the booleans
m_isCapturingThreadToBeTerminated = true;
m_isCapturingThreadPaused = false;
m_isNewFrameAvailable = false;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread::startCapturingThread()
{
// Let's first make sure that we
// don't try to re-launch the thread
// after it's already been started
if(!m_isCapturingThreadToBeTerminated)
{
// Error -- Tried to restart a thread
// that had already been started
return;
}
// Check if the capture device has been set
if(this->isConnected())
{
// Since we have a capture device, we start the thread
m_thread.launch();
// Set boolean so that thread will run
m_isCapturingThreadToBeTerminated = false;
return;
}
// Error -- In this case, we don't yet
// have a capture device so we just return
// Set the boolean in order
// to terminate the thread
m_isCapturingThreadToBeTerminated = true;
return;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread::stopCapturingThread()
{
// Set the boolean in order
// to terminate the thread
m_isCapturingThreadToBeTerminated = true;
// Here we just terminate
// the running thread
m_thread.terminate();
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread::threadLoop()
{
while(!m_isCapturingThreadToBeTerminated)
{
if(!m_isCapturingThreadPaused &&
!m_isNewFrameAvailable &&
!m_isCapturingThreadToBeTerminated)
{
// If we cannot successfully query
// a frame, then we just stop the
// capturing thread
if(this->isConnected())
{
// Query a new frame and
// signal that a new frame
// is available
this->queryFrame(m_frame);
m_isNewFrameAvailable = true;
}
else
{
// The capturing device got
// disconnected, so we stop
// this capturing thread
m_isCapturingThreadToBeTerminated = true;
}
}
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread::pauseCapturingThread()
{
// If the thread is currently paused, then
// we unpause otherwise we pause it
if(m_isCapturingThreadPaused)
m_isCapturingThreadPaused = false;
else
m_isCapturingThreadPaused = true;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline const blImage< blColor3<unsigned char> >& blVideoThread::getFrame()const
{
return m_frame;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline const bool& blVideoThread::isNewFrameAvailable()const
{
return m_isNewFrameAvailable;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread::letThreadQueryAnotherFrame()
{
m_isNewFrameAvailable = false;
}
//-------------------------------------------------------------------
#endif // BL_VIDEOTHREAD_HPP