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