-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideoframegrabber.cpp
More file actions
136 lines (107 loc) · 3.01 KB
/
videoframegrabber.cpp
File metadata and controls
136 lines (107 loc) · 3.01 KB
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
#include <stdexcept>
#include <utility>
#include <QDebug>
#include <QImage>
#include <QLoggingCategory>
#include <QMutexLocker>
#include <QSize>
#include <QThread>
#include "videoframegrabber.h"
#include "abstractvideosource.h"
Q_LOGGING_CATEGORY(GRABBER, "videoframegrabber")
namespace vfg {
namespace core {
VideoFrameGrabber::VideoFrameGrabber(
std::shared_ptr<vfg::core::AbstractVideoSource> newAvs,
QObject *parent) :
QObject(parent),
avs(std::move(newAvs))
{
if(!avs) {
qCCritical(GRABBER) << "Invalid video source";
throw std::runtime_error("Video source must be a valid object");
}
}
bool VideoFrameGrabber::hasVideo() const
{
QMutexLocker lock(&mutex);
return avs && avs->hasVideo();
}
void VideoFrameGrabber::setVideoSource(
std::shared_ptr<vfg::core::AbstractVideoSource> newAvs)
{
QMutexLocker lock(&mutex);
if(!newAvs) {
qCCritical(GRABBER) << "Invalid video source";
throw std::runtime_error("Video source must be a valid object");
}
disconnect(avs.get(), 0);
avs = std::move(newAvs);
currentFrame = 0;
}
int VideoFrameGrabber::lastFrame() const
{
QMutexLocker lock(&mutex);
return currentFrame;
}
void VideoFrameGrabber::requestFrame(const int frameNum)
{
QMutexLocker ml(&mutex);
if(!avs->isValidFrame(frameNum)) {
qCCritical(GRABBER) << "Frame number out of range:" << frameNum;
emit errorOccurred(tr("Frame number out of range: %1")
.arg(QString::number(frameNum)));
return;
}
currentFrame = frameNum;
emit frameGrabbed(frameNum, avs->getFrame(frameNum));
}
void VideoFrameGrabber::requestNextFrame()
{
QMutexLocker ml(&mutex);
const auto nextFrame = currentFrame + 1;
if(nextFrame >= avs->getNumFrames()) {
qCDebug(GRABBER) << "Reached last frame";
emit errorOccurred(tr("Reached last frame"));
return;
}
++currentFrame;
emit frameGrabbed(nextFrame, avs->getFrame(nextFrame));
}
void VideoFrameGrabber::requestPreviousFrame()
{
QMutexLocker ml(&mutex);
if(currentFrame == 0) {
qCDebug(GRABBER) << "Reached first frame";
emit errorOccurred(tr("Reached first frame"));
return;
}
--currentFrame;
emit frameGrabbed(currentFrame, avs->getFrame(currentFrame));
}
QImage VideoFrameGrabber::getFrame(const int frameNum)
{
QMutexLocker ml(&mutex);
if(!avs->isValidFrame(frameNum)) {
qCCritical(GRABBER) << "Frame out of range:" << frameNum << "(" << avs->getNumFrames() << ")";
return {};
}
return avs->getFrame(frameNum);
}
bool VideoFrameGrabber::isValidFrame(const int frameNum) const
{
QMutexLocker lock(&mutex);
return frameNum >= 0 && frameNum < avs->getNumFrames();
}
int VideoFrameGrabber::totalFrames() const
{
QMutexLocker lock(&mutex);
return avs->getNumFrames();
}
QSize VideoFrameGrabber::resolution() const
{
QMutexLocker lock(&mutex);
return avs->resolution();
}
} // namespace core
} // namespace vfg