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
42
44 const std::filesystem::path& filePath)
45{
46 this->startPlayback(filePath);
47}
48
53
54bool
56{
57 return this->videoCapture.isOpened();
58}
59
60unsigned int
62{
63 return this->fps;
64}
65
66unsigned int
68{
69 return this->frameCount;
70}
71
72unsigned int
74{
75 return this->frameHeight;
76}
77
78unsigned int
80{
81 return this->frameWidth;
82}
83
84void
86{
87 ARMARX_CHECK_LESS(frame, this->frameCount)
88 << "Desired frame number to set is above the number of frames in total";
89 if (frame != currentFrame)
90 {
91 this->videoCapture.set(cv::CAP_PROP_POS_FRAMES, static_cast<double>(frame));
92 this->currentFrame = frame;
93 }
94}
95
96unsigned int
98{
99 return this->currentFrame;
100}
101
102bool
104{
105 return this->currentFrame < this->frameCount;
106}
107
108void
110{
111 this->filePath = std::filesystem::canonical(file_path);
112 this->videoCapture.open(this->filePath.string());
113
114 ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened())
115 << "Could not open the output video file '" + this->filePath.string() + "' for writing";
116
117 this->fps = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FPS));
118 this->frameHeight =
119 static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_HEIGHT));
120 this->frameWidth = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_WIDTH));
121 this->frameCount = static_cast<unsigned int>(this->videoCapture.get(cv::CAP_PROP_FRAME_COUNT));
122 this->currentFrame = 0;
123}
124
125bool
127{
128 ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened())
129 << "Could not open the output video file '" + this->filePath.string() + "' for writing";
130
131 cv::Mat frame;
132 const bool success = this->videoCapture.read(frame);
133 ++this->currentFrame;
134
135 // Convert OpenCV BGR to common sense RGB and copy to buffer
136 if (success)
137 {
138 cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
139 std::memcpy(buffer,
140 frame.data,
141 this->frameHeight * this->frameWidth *
142 static_cast<unsigned int>(frame.channels()));
143 return true;
144 }
145
146 return false;
147}
148
149bool
151{
152 const bool success = this->getNextFrame(buffer.pixels);
153 ++this->currentFrame;
154 return success;
155}
156
157bool
159{
160 ARMARX_CHECK_EXPRESSION(this->videoCapture.isOpened())
161 << "Could not open the output video file '" + this->filePath.string() + "' for writing";
162
163 return this->videoCapture.read(buffer);
164}
165
166void
168{
169 if (this->videoCapture.isOpened())
170 {
171 this->videoCapture.release();
172 }
173}
virtual void setCurrentFrame(unsigned int frame) override
Sets the frame from there the playback should resume afterwards (seek)
virtual void stopPlayback() override
Stops the playback.
virtual unsigned int getFrameWidth() const override
Gets the width of a frame in pixel.
virtual unsigned int getFps() const override
Gets the amount of frames per second of the recording.
virtual bool hasNextFrame() const override
Indicates whether the recording has a consecutive frame.
virtual unsigned int getFrameCount() const override
Gets the total amout of frames in the recording.
virtual unsigned int getCurrentFrame() const override
Gets the current frame index of the playback.
virtual bool getNextFrame(void *buffer) override
Writes the next frame into a buffer of any form (RGB)
virtual void startPlayback(const std::filesystem::path &filePath) override
Starts the playback.
virtual bool isPlayingBack() const override
Indicates whether the instance is configured to be able to play back.
VideoPlaybackStrategy()
Default constructor to manually setup later.
virtual unsigned int getFrameHeight() const override
Gets the height of a frame in pixel.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...