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