VideoPlaybackStrategy.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package visionx::imrec
17  * @author Christian R. G. Dreher <christian.dreher@student.kit.edu>
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // STD/STL
28 #include <cstring>
29 #include <filesystem>
30 
31 // OpenCV 2
32 #include <opencv2/imgproc/imgproc.hpp>
33 
34 // ArmarX
37 
38 
40 {
41  this->filePath = "";
42 }
43 
44 
46 {
47  this->startPlayback(filePath);
48 }
49 
50 
52 {
53  this->stopPlayback();
54 }
55 
56 
57 bool
59 {
60  return this->videoCapture.isOpened();
61 }
62 
63 
64 unsigned int
66 {
67  return this->fps;
68 }
69 
70 
71 unsigned int
73 {
74  return this->frameCount;
75 }
76 
77 
78 unsigned int
80 {
81  return this->frameHeight;
82 }
83 
84 
85 unsigned int
87 {
88  return this->frameWidth;
89 }
90 
91 
92 void
94 {
95  ARMARX_CHECK_LESS(frame, this->frameCount) << "Desired frame number to set is above the number of frames in total";
96  if (frame != currentFrame)
97  {
98  this->videoCapture.set(cv::CAP_PROP_POS_FRAMES, static_cast<double>(frame));
99  this->currentFrame = frame;
100  }
101 }
102 
103 
104 unsigned int
106 {
107  return this->currentFrame;
108 }
109 
110 
111 bool
113 {
114  return this->currentFrame < this->frameCount;
115 }
116 
117 
118 void
120 {
121  this->filePath = std::filesystem::canonical(file_path);
122  this->videoCapture.open(this->filePath.string());
123 
124  ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened()) << "Could not open the output video file '" + this->filePath.string() + "' for writing";
125 
126  this->fps = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FPS));
127  this->frameHeight = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
128  this->frameWidth = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_WIDTH));
129  this->frameCount = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_COUNT));
130  this->currentFrame = 0;
131 }
132 
133 
134 bool
136 {
137  ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened()) << "Could not open the output video file '" + this->filePath.string() + "' for writing";
138 
139  cv::Mat frame;
140  const bool success = this->videoCapture.read(frame);
141  ++this->currentFrame;
142 
143  // Convert OpenCV BGR to common sense RGB and copy to buffer
144  if (success)
145  {
146  cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
147  std::memcpy(buffer, frame.data, this->frameHeight * this->frameWidth * static_cast<unsigned int>(frame.channels()));
148  return true;
149  }
150 
151  return false;
152 }
153 
154 
155 bool
157 {
158  const bool success = this->getNextFrame(buffer.pixels);
159  ++this->currentFrame;
160  return success;
161 }
162 
163 
164 bool
166 {
167  ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened()) << "Could not open the output video file '" + this->filePath.string() + "' for writing";
168 
169  return this->videoCapture.read(buffer);
170 }
171 
172 
173 void
175 {
176  if (this->videoCapture.isOpened())
177  {
178  this->videoCapture.release();
179  }
180 }
visionx::imrec::strats::VideoPlaybackStrategy::getNextFrame
virtual bool getNextFrame(void *buffer) override
Writes the next frame into a buffer of any form (RGB)
Definition: VideoPlaybackStrategy.cpp:135
visionx::imrec::strats::VideoPlaybackStrategy::startPlayback
virtual void startPlayback(const std::filesystem::path &filePath) override
Starts the playback.
Definition: VideoPlaybackStrategy.cpp:119
visionx::imrec::strats::VideoPlaybackStrategy::hasNextFrame
virtual bool hasNextFrame() const override
Indicates whether the recording has a consecutive frame.
Definition: VideoPlaybackStrategy.cpp:112
visionx::imrec::strats::VideoPlaybackStrategy::VideoPlaybackStrategy
VideoPlaybackStrategy()
Default constructor to manually setup later.
Definition: VideoPlaybackStrategy.cpp:39
ARMARX_CHECK_LESS
#define ARMARX_CHECK_LESS(lhs, rhs)
This macro evaluates whether lhs is less (<) than rhs and if it turns out to be false it will throw a...
Definition: ExpressionException.h:102
visionx::imrec::strats::VideoPlaybackStrategy::stopPlayback
virtual void stopPlayback() override
Stops the playback.
Definition: VideoPlaybackStrategy.cpp:174
visionx::imrec::strats::VideoPlaybackStrategy::isPlayingBack
virtual bool isPlayingBack() const override
Indicates whether the instance is configured to be able to play back.
Definition: VideoPlaybackStrategy.cpp:58
visionx::imrec::strats::VideoPlaybackStrategy::getFps
virtual unsigned int getFps() const override
Gets the amount of frames per second of the recording.
Definition: VideoPlaybackStrategy.cpp:65
visionx::imrec::strats::VideoPlaybackStrategy::getFrameWidth
virtual unsigned int getFrameWidth() const override
Gets the width of a frame in pixel.
Definition: VideoPlaybackStrategy.cpp:86
visionx::imrec::strats::VideoPlaybackStrategy::getCurrentFrame
virtual unsigned int getCurrentFrame() const override
Gets the current frame index of the playback.
Definition: VideoPlaybackStrategy.cpp:105
VideoPlaybackStrategy.h
ExpressionException.h
visionx::imrec::strats::VideoPlaybackStrategy::getFrameHeight
virtual unsigned int getFrameHeight() const override
Gets the height of a frame in pixel.
Definition: VideoPlaybackStrategy.cpp:79
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
visionx::imrec::strats::VideoPlaybackStrategy::setCurrentFrame
virtual void setCurrentFrame(unsigned int frame) override
Sets the frame from there the playback should resume afterwards (seek)
Definition: VideoPlaybackStrategy.cpp:93
Logging.h
visionx::imrec::strats::VideoPlaybackStrategy::getFrameCount
virtual unsigned int getFrameCount() const override
Gets the total amout of frames in the recording.
Definition: VideoPlaybackStrategy.cpp:72
visionx::imrec::strats::VideoPlaybackStrategy::~VideoPlaybackStrategy
virtual ~VideoPlaybackStrategy() override
Destructor.
Definition: VideoPlaybackStrategy.cpp:51