-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblVideoThread2.hpp
230 lines (176 loc) · 6.85 KB
/
blVideoThread2.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
#ifndef BL_VIDEOTHREAD2_HPP
#define BL_VIDEOTHREAD2_HPP
//-------------------------------------------------------------------
// FILE: blVideoThread2.hpp
// CLASS: blVideoThread2
// BASE CLASS: blVideoThread
//
// PURPOSE: Based on blVideoThread, this class has a buffer
// that is always available and the same size,
// and that new pixel data is fed serially to the
// frame buffer
//
// AUTHOR: Vincenzo Barbato
// http://www.barbatolabs.com
// navyenzo@gmail.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: - The difference in this class is that the
// frame buffer is always available and that
// new data is continuously fed to the image
// one pixel at a time
//
// DATE CREATED: May/10/2011
// DATE UPDATED:
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes and libs needed for this file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Enums needed for this file
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class blVideoThread2 : public blVideoThread
{
public: // Constructors and destructors
// Default constructor
blVideoThread2();
// Destructor
~blVideoThread2()
{
}
public: // Public functions
// 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 buffer which is
// used to always have a buffer
// available to read continuously
blImage< blColor3<unsigned char> > m_frameBuffer;
private: // Private functions
// Function used to resize the
// frame buffer to match the
// size of the main capturing
// frame
void resizeFrameBuffer();
// Function used to copy the
// frame into the frame buffer
void copyFrameIntoFrameBuffer();
private: // Private variables
// Variable used to check whether
// the frame buffer is being resized.
// This prevents the main program thread
// to try to read the buffer while we're
// resizing it
bool m_isFrameBufferBeingResized;
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline blVideoThread2::blVideoThread2() : blVideoThread()
{
// Default the booleans
m_isFrameBufferBeingResized = false;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread2::threadLoop()
{
while(!m_isCapturingThreadToBeTerminated)
{
if(!m_isCapturingThreadPaused &&
!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);
// Now that we have a frame
// we simply copy it to the
// buffer
// Let's make sure that the
// frame buffer is the same
// size as the frame
if(m_frameBuffer.size1() != this->m_frame.size1() ||
m_frameBuffer.size2() != this->m_frame.size2())
{
resizeFrameBuffer();
}
// Copy the frame into
// the frame buffer
copyFrameIntoFrameBuffer();
}
else
{
// The capturing device got
// disconnected, so we stop
// this capturing thread
m_isCapturingThreadToBeTerminated = true;
}
}
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread2::resizeFrameBuffer()
{
m_isFrameBufferBeingResized = true;
// Resize the frame buffer
m_frameBuffer.create(this->m_frame.size1(),
this->m_frame.size2());
// Reset the resizing flag
m_isFrameBufferBeingResized = false;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline void blVideoThread2::copyFrameIntoFrameBuffer()
{
for(int i = 0; i < m_frameBuffer.size1(); ++i)
{
for(int j = 0; j < m_frameBuffer.size2(); ++j)
{
m_frameBuffer[i][j] = this->m_frame[i][j];
}
}
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
inline const blImage< blColor3<unsigned char> >& blVideoThread2::getFrame()const
{
// Since this function is being called
// from the main program thread or another
// thread, we could be currently resizing
// the frame buffer, and thus we could
// have a problem
// So if we're currently resizing the
// frame buffer, we wait till the
// buffer is resized
while(m_isFrameBufferBeingResized)
{
}
// By now we know that the frame
// buffer has been resized, so we
// simply return it
return m_frameBuffer;
}
//-------------------------------------------------------------------
#endif // BL_VIDEOTHREAD2_HPP