-
Notifications
You must be signed in to change notification settings - Fork 16
/
abstractimagegrabber.cpp
231 lines (185 loc) · 5.18 KB
/
abstractimagegrabber.cpp
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
/****************************************************************************
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
****************************************************************************/
#include "abstractimagegrabber.h"
#include "helpers/audiotimer.h"
#if QT_VERSION >= 0x050000
#include <QtConcurrent/QtConcurrentRun>
#else
#include <QtConcurrentRun>
#endif
#include <QMutexLocker>
#include <QEventLoop>
#include <QTimer>
#include <QElapsedTimer>
#include <QFuture>
AbstractImageGrabber::AbstractImageGrabber(QObject *parent)
: AbstractGrabber(parent)
, m_prevPts(-1)
, m_latency(0)
, m_grabbedFrameCount(0)
, m_isStopRequest(false)
, m_isPauseRequest(false)
, m_timer(0)
, m_initTime(0)
{
}
AbstractImageGrabber::~AbstractImageGrabber()
{
setStopRequest(true);
future.waitForFinished();
}
void AbstractImageGrabber::setLatency(int latency)
{
QMutexLocker locker(&m_latencyMutex);
if (m_latency != latency) {
m_latency = latency;
Q_EMIT latencyChanged(latency);
}
}
int AbstractImageGrabber::latency() const
{
QMutexLocker locker(&m_latencyMutex);
return m_latency;
}
void AbstractImageGrabber::setInitializationTime(int ms)
{
if (m_initTime != ms) {
m_initTime = ms;
}
}
int AbstractImageGrabber::initializationTime() const
{
return m_initTime;
}
int AbstractImageGrabber::grabbedFrameCount() const
{
//QMutexLocker locker(&m_grabbedFrameCountMutex);
return m_grabbedFrameCount;
}
bool AbstractImageGrabber::start()
{
if (state() == AbstractGrabber::StoppedState) {
waitForInitialization();
startGrabbing();
return true;
}
return false;
}
void AbstractImageGrabber::stop()
{
if (state() != AbstractGrabber::StoppedState) {
setStopRequest(true);
}
}
void AbstractImageGrabber::suspend()
{
if (state() == AbstractGrabber::ActiveState) {
setPauseRequest(true);
}
}
void AbstractImageGrabber::resume()
{
if (state() == AbstractGrabber::SuspendedState)
startGrabbing();
}
void AbstractImageGrabber::startGrabbing()
{
//init grabbed frames
m_grabbedFrameCount = 0;
future = QtConcurrent::run(this, &AbstractImageGrabber::grab);
setState(AbstractGrabber::ActiveState);
}
void AbstractImageGrabber::grab()
{
QImage frame;//this stores grabbed image
QEventLoop latencyLoop;
QElapsedTimer timer;
if (!m_timer) {
timer.start();
}
m_prevPts = -1;
int pts = -1;
Q_FOREVER {
//check if we must finish grabbing
if (isStopRequest() || isPauseRequest())
break;
frame = captureFrame();
setGrabbedFrameCount(grabbedFrameCount() + 1);
pts = m_timer ? m_timer->elapsed() : timer.elapsed();
if (m_prevPts != pts) {
m_prevPts = pts;
Q_EMIT frameAvailable(frame, pts);
}
//check if we must finish grabbing
if (isStopRequest() || isPauseRequest())
break;
//wait for set by user milliseconds
QTimer::singleShot(latency(), &latencyLoop, SLOT(quit()));
latencyLoop.exec();
}
setState(isStopRequest() ? AbstractGrabber::StoppedState : AbstractGrabber::SuspendedState);
if (isStopRequest())
m_prevPts = -1;
//reset stop and pause flags
setStopRequest(false);
setPauseRequest(false);
Q_EMIT frameAvailable(QImage(), pts);
}
void AbstractImageGrabber::setGrabbedFrameCount(int count)
{
QMutexLocker locker(&m_grabbedFrameCountMutex);
if (m_grabbedFrameCount != count) {
m_grabbedFrameCount = count;
Q_EMIT grabbedFrameCountChanged(count);
}
}
void AbstractImageGrabber::setStopRequest(bool stop)
{
QMutexLocker locker(&m_stopPauseMutex);
if (m_isStopRequest != stop) {
m_isStopRequest = stop;
}
}
bool AbstractImageGrabber::isStopRequest() const
{
QMutexLocker locker(&m_stopPauseMutex);
return m_isStopRequest;
}
void AbstractImageGrabber::setPauseRequest(bool pause)
{
QMutexLocker locker(&m_stopPauseMutex);
if (m_isPauseRequest != pause) {
m_isPauseRequest = pause;
}
}
bool AbstractImageGrabber::isPauseRequest() const
{
QMutexLocker locker(&m_stopPauseMutex);
return m_isPauseRequest;
}
void AbstractImageGrabber::waitForInitialization()
{
QEventLoop initLoop;
QTimer::singleShot(m_initTime, &initLoop, SLOT(quit()));
initLoop.exec();
Q_EMIT initialized();
}
void AbstractImageGrabber::setTimer(AudioTimer *timer)
{
m_timer = timer;
}