H264RecordingStrategy.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 // OpenCV 2
28 #include <opencv2/core/core.hpp>
29 #include <opencv2/highgui/highgui.hpp> // cv::VideoWriter
30 
31 // Simox
32 #include <SimoxUtility/algorithm.h>
33 
34 
36 {
37  // pass
38 }
39 
40 
42  const std::filesystem::path& file_path,
43  const std::string& filename,
44  const double fps) :
45  visionx::imrec::AbstractRecordingStrategy(file_path), m_fps{fps}
46 {
47  std::string filename_ext = filename;
48  if (not simox::alg::ends_with(filename_ext, ".mp4"))
49  {
50  filename_ext += ".mp4";
51  }
52 
53  m_file_path /= filename_ext;
54 }
55 
56 
58 {
59  // pass
60 }
61 
62 
63 void
65 {
67  m_h264_video_writer = std::make_unique<cv::VideoWriter>();
68 }
69 
70 
71 void
73  const cv::Mat& frame,
74  const std::chrono::microseconds timestamp)
75 {
76  writeMetadataFrame(frame, timestamp);
77 
78  if (not m_h264_video_writer->isOpened())
79  {
80  const int fourcc = cv::VideoWriter::fourcc('X', '2', '6', '4');
81  m_h264_video_writer->open(getFilePath().string(), fourcc, m_fps, frame.size());
82 
83  if (not m_h264_video_writer->isOpened())
84  {
85  ARMARX_ERROR << deactivateSpam() << "Could not open the output video file '"
86  << getFilePath().string() << "' for writing. FRAME DROPPED!";
87  return;
88  }
89  }
90 
91  m_h264_video_writer->write(frame);
92 }
93 
94 
95 void
97 {
99  m_h264_video_writer->release();
100 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::imrec::strats::H264RecordingStrategy::~H264RecordingStrategy
~H264RecordingStrategy() override
Definition: H264RecordingStrategy.cpp:57
visionx::imrec::strats::H264RecordingStrategy::H264RecordingStrategy
H264RecordingStrategy()
Definition: H264RecordingStrategy.cpp:35
visionx::imrec::AbstractRecordingStrategy
Abstract interface of a recording strategy.
Definition: AbstractRecordingStrategy.h:54
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:116
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
H264RecordingStrategy.h
visionx::imrec::strats::H264RecordingStrategy::recordFrame
void recordFrame(const cv::Mat &frame, std::chrono::microseconds timestamp) override
Adds the given frame to the recording.
Definition: H264RecordingStrategy.cpp:72
filename
std::string filename
Definition: VisualizationRobot.cpp:83
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
visionx::imrec::AbstractRecordingStrategy::startRecording
virtual void startRecording()
Starts the recording manually if constructed empty.
Definition: AbstractRecordingStrategy.cpp:78
visionx::imrec::strats::H264RecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: H264RecordingStrategy.cpp:64
visionx::imrec::strats::H264RecordingStrategy::stopRecording
void stopRecording() override
Stops the recording.
Definition: H264RecordingStrategy.cpp:96