AbstractRecordingStrategy.h
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 
24 #pragma once
25 
26 
27 // STD/STL
28 #include <chrono>
29 #include <filesystem>
30 #include <fstream>
31 #include <functional>
32 #include <memory>
33 #include <set>
34 #include <string>
35 #include <string_view>
36 #include <tuple>
37 
38 
39 class CByteImage;
40 
41 namespace cv
42 {
43  class Mat;
44 }
45 
46 namespace visionx::imrec
47 {
48 
49  /**
50  * Abstract interface of a recording strategy
51  */
53  {
54 
55  protected:
56  /**
57  * @brief Path to where the recording file should be written to
58  */
59  std::filesystem::path m_file_path;
60 
61  private:
62  /**
63  * @brief Flag to indicate whether the recording already started
64  */
65  bool m_recording;
66 
67  /**
68  * @brief File stream for the metadata file
69  */
70  std::ofstream m_metadata_file;
71 
72  /**
73  * @brief List of already written metadata variable names
74  */
75  std::set<std::string> m_written_metadata_variables;
76 
77  /**
78  * @brief Frame number which is additionally displayed in the filename
79  */
80  unsigned int m_sequence_number = 0;
81 
82  std::chrono::microseconds m_reference_time{0};
83 
84  public:
85  /**
86  * @brief Default constructor to start the recording manually
87  */
89 
90  /**
91  * @brief Constructor for any recording strategy, immediately starting the recording
92  * @param filePath Path to where the recording file should be written to
93  * @param fps Amount of frames being recorded per second
94  */
95  AbstractRecordingStrategy(const std::filesystem::path& filePath);
96 
97  /**
98  * @brief Destructor
99  */
100  virtual ~AbstractRecordingStrategy();
101 
102  /**
103  * @brief Indicates whether this instance is already initialised for recording
104  * @return True if it is initialised and ready to record, false if not
105  */
106  virtual bool isRecording() const;
107 
108  /**
109  * @brief Starts the recording manually if constructed empty
110  * @param filePath Path to where the recording file should be written to
111  * @param fps Amount of frames being recorded per second
112  */
113  virtual void startRecording();
114 
115  /**
116  * @brief Adds the given frame to the recording
117  * @param frame Frame to be added
118  */
119  virtual void recordFrame(const CByteImage& frame, std::chrono::microseconds timestamp);
120 
121  /**
122  * @brief Adds the given frame to the recording
123  * @param frame Frame to be added
124  */
125  virtual void recordFrame(const cv::Mat& frame, std::chrono::microseconds timestamp);
126 
127  /**
128  * @brief Stops the recording
129  */
130  virtual void stopRecording();
131 
132  /**
133  * @brief Gets the raw file path for the recording as configured
134  * @return Raw file path for the recording as configureds
135  */
136  virtual std::filesystem::path getFilePath() const;
137 
138  /**
139  * @brief Gets the path to the recording without filename
140  * @return Path to the recording without filename
141  */
142  virtual std::filesystem::path getPath() const;
143 
144  /**
145  * @brief Gets the stem of the configured file (filename without extension)
146  * @return Stem of the configured file
147  */
148  virtual std::filesystem::path getStem() const;
149 
150  /**
151  * @brief Gets the extension plus preceeded dot of the configured file (e.g. ".avi")
152  * @return Extention plus preceeded dot of the configured file
153  */
154  virtual std::filesystem::path getDotExtension() const;
155 
156  virtual std::filesystem::path getMetadataPath() const;
157 
158  virtual std::tuple<unsigned int, std::string>
159  writeMetadataFrame(const CByteImage& frame, std::chrono::microseconds timestamp);
160 
161  virtual std::tuple<unsigned int, std::string>
162  writeMetadataFrame(const cv::Mat& frame, std::chrono::microseconds timestamp);
163 
164  virtual void writeMetadataDatetime(const std::string& var_name,
165  std::chrono::microseconds timestamp);
166 
167  virtual void writeMetadataLine(const std::string& var_name,
168  std::string_view var_type,
169  std::string_view var_value);
170 
171  private:
172  virtual std::tuple<unsigned int, std::string>
173  writeMetadataFrameFileInfo(std::chrono::microseconds timestamp);
174  };
175 
176  /**
177  * Convenience alias for any recording strategy
178  */
179  using Recording = std::shared_ptr<AbstractRecordingStrategy>;
180 
181 } // namespace visionx::imrec
visionx::imrec::AbstractRecordingStrategy::m_file_path
std::filesystem::path m_file_path
Path to where the recording file should be written to.
Definition: AbstractRecordingStrategy.h:59
visionx::imrec::AbstractRecordingStrategy::AbstractRecordingStrategy
AbstractRecordingStrategy()
Default constructor to start the recording manually.
Definition: AbstractRecordingStrategy.cpp:46
visionx::imrec::AbstractRecordingStrategy
Abstract interface of a recording strategy.
Definition: AbstractRecordingStrategy.h:52
visionx::imrec::AbstractRecordingStrategy::isRecording
virtual bool isRecording() const
Indicates whether this instance is already initialised for recording.
Definition: AbstractRecordingStrategy.cpp:67
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:111
visionx::imrec::AbstractRecordingStrategy::writeMetadataFrame
virtual std::tuple< unsigned int, std::string > writeMetadataFrame(const CByteImage &frame, std::chrono::microseconds timestamp)
Definition: AbstractRecordingStrategy.cpp:154
visionx::imrec::AbstractRecordingStrategy::getStem
virtual std::filesystem::path getStem() const
Gets the stem of the configured file (filename without extension)
Definition: AbstractRecordingStrategy.cpp:134
visionx::imrec::AbstractRecordingStrategy::writeMetadataLine
virtual void writeMetadataLine(const std::string &var_name, std::string_view var_type, std::string_view var_value)
Definition: AbstractRecordingStrategy.cpp:212
visionx::imrec::AbstractRecordingStrategy::getDotExtension
virtual std::filesystem::path getDotExtension() const
Gets the extension plus preceeded dot of the configured file (e.g.
Definition: AbstractRecordingStrategy.cpp:140
visionx::imrec::AbstractRecordingStrategy::~AbstractRecordingStrategy
virtual ~AbstractRecordingStrategy()
Destructor.
Definition: AbstractRecordingStrategy.cpp:58
visionx::imrec::AbstractRecordingStrategy::writeMetadataDatetime
virtual void writeMetadataDatetime(const std::string &var_name, std::chrono::microseconds timestamp)
Definition: AbstractRecordingStrategy.cpp:203
visionx::imrec::AbstractRecordingStrategy::getPath
virtual std::filesystem::path getPath() const
Gets the path to the recording without filename.
Definition: AbstractRecordingStrategy.cpp:128
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
visionx::imrec::Recording
std::shared_ptr< AbstractRecordingStrategy > Recording
Convenience alias for any recording strategy.
Definition: AbstractRecordingStrategy.h:179
visionx::imrec::AbstractRecordingStrategy::recordFrame
virtual void recordFrame(const CByteImage &frame, std::chrono::microseconds timestamp)
Adds the given frame to the recording.
Definition: AbstractRecordingStrategy.cpp:85
visionx::imrec::AbstractRecordingStrategy::startRecording
virtual void startRecording()
Starts the recording manually if constructed empty.
Definition: AbstractRecordingStrategy.cpp:73
visionx::imrec::AbstractRecordingStrategy::getMetadataPath
virtual std::filesystem::path getMetadataPath() const
Definition: AbstractRecordingStrategy.cpp:146
cv
Definition: helper.h:34
visionx::imrec
Definition: json_conversions.h:35
visionx::imrec::AbstractRecordingStrategy::getFilePath
virtual std::filesystem::path getFilePath() const
Gets the raw file path for the recording as configured.
Definition: AbstractRecordingStrategy.cpp:122