Azure Kinect Sensor SDK  refs/heads/develop
Documentation for https://github.com/Microsoft/Azure-Kinect-Sensor-SDK
record.hpp
1 
6 #ifndef K4A_RECORD_HPP
7 #define K4A_RECORD_HPP
8 
9 #include <k4a/k4a.hpp>
10 #include <k4arecord/record.h>
11 
12 namespace k4a
13 {
14 
22 class record
23 {
24 public:
30  record(k4a_record_t handle = nullptr) noexcept : m_handle(handle) {}
31 
34  record(record &&other) noexcept : m_handle(other.m_handle)
35  {
36  other.m_handle = nullptr;
37  }
38 
39  record(const record &) = delete;
40 
41  ~record()
42  {
43  // NOTE: flush is called internally when closing record
44  close();
45  }
46 
47  record &operator=(const record &) = delete;
48 
51  record &operator=(record &&other) noexcept
52  {
53  if (this != &other)
54  {
55  close();
56  m_handle = other.m_handle;
57  other.m_handle = nullptr;
58  }
59 
60  return *this;
61  }
62 
65  explicit operator bool() const noexcept
66  {
67  return is_valid();
68  }
69 
72  bool is_valid() const noexcept
73  {
74  return m_handle != nullptr;
75  }
76 
81  void close() noexcept
82  {
83  if (is_valid())
84  {
85  k4a_record_close(m_handle);
86  m_handle = nullptr;
87  }
88  }
89 
94  void flush()
95  {
96  if (m_handle)
97  {
98  k4a_result_t result = k4a_record_flush(m_handle);
99 
100  if (K4A_FAILED(result))
101  {
102  throw error("Failed to flush!");
103  }
104  }
105  }
106 
112  void add_tag(const char *name, const char *value)
113  {
114  k4a_result_t result = k4a_record_add_tag(m_handle, name, value);
115 
116  if (K4A_FAILED(result))
117  {
118  throw error("Failed to add tag!");
119  }
120  }
121 
128  {
129  k4a_result_t result = k4a_record_add_imu_track(m_handle);
130 
131  if (K4A_FAILED(result))
132  {
133  throw error("Failed to add imu_track!");
134  }
135  }
136 
142  void add_attachment(const char *attachment_name, const uint8_t *buffer, size_t buffer_size)
143  {
144  k4a_result_t result = k4a_record_add_attachment(m_handle, attachment_name, buffer, buffer_size);
145 
146  if (K4A_FAILED(result))
147  {
148  throw error("Failed to add attachment!");
149  }
150  }
151 
157  void add_custom_video_track(const char *track_name,
158  const char *codec_id,
159  const uint8_t *codec_context,
160  size_t codec_context_size,
161  const k4a_record_video_settings_t *track_settings)
162  {
163  k4a_result_t result = k4a_record_add_custom_video_track(m_handle,
164  track_name,
165  codec_id,
166  codec_context,
167  codec_context_size,
168  track_settings);
169 
170  if (K4A_FAILED(result))
171  {
172  throw error("Failed to add custom video track!");
173  }
174  }
175 
181  void add_custom_subtitle_track(const char *track_name,
182  const char *codec_id,
183  const uint8_t *codec_context,
184  size_t codec_context_size,
185  const k4a_record_subtitle_settings_t *track_settings)
186  {
187  k4a_result_t result = k4a_record_add_custom_subtitle_track(m_handle,
188  track_name,
189  codec_id,
190  codec_context,
191  codec_context_size,
192  track_settings);
193 
194  if (K4A_FAILED(result))
195  {
196  throw error("Failed to add custom subtitle track!");
197  }
198  }
199 
206  {
207  k4a_result_t result = k4a_record_write_header(m_handle);
208 
209  if (K4A_FAILED(result))
210  {
211  throw error("Failed to write header!");
212  }
213  }
214 
221  {
222  k4a_result_t result = k4a_record_write_capture(m_handle, capture.handle());
223 
224  if (K4A_FAILED(result))
225  {
226  throw error("Failed to write capture!");
227  }
228  }
229 
235  void write_imu_sample(const k4a_imu_sample_t &imu_sample)
236  {
237  k4a_result_t result = k4a_record_write_imu_sample(m_handle, imu_sample);
238 
239  if (K4A_FAILED(result))
240  {
241  throw error("Failed to write imu sample!");
242  }
243  }
244 
250  void write_custom_track_data(const char *track_name,
251  const std::chrono::microseconds device_timestamp_usec,
252  uint8_t *custom_data,
253  size_t custom_data_size)
254  {
255  k4a_result_t result = k4a_record_write_custom_track_data(m_handle,
256  track_name,
257  internal::clamp_cast<uint64_t>(
258  device_timestamp_usec.count()),
259  custom_data,
260  custom_data_size);
261 
262  if (K4A_FAILED(result))
263  {
264  throw error("Failed to write custom track data!");
265  }
266  }
267 
273  static record create(const char *path, const device &device, const k4a_device_configuration_t &device_configuration)
274  {
275  k4a_record_t handle = nullptr;
276  k4a_result_t result = k4a_record_create(path, device.handle(), device_configuration, &handle);
277 
278  if (K4A_FAILED(result))
279  {
280  throw error("Failed to create recorder!");
281  }
282 
283  return record(handle);
284  }
285 
286 private:
287  k4a_record_t m_handle;
288 };
289 
290 } // namespace k4a
291 
292 #endif
record & operator=(record &&other) noexcept
Moves another k4a::record into this k4a::record; other is set to invalid.
Definition: record.hpp:51
void add_tag(const char *name, const char *value)
Adds a tag to the recording Throws error on failure.
Definition: record.hpp:112
Structure containing additional metadata specific to custom subtitle tracks.
Definition: types.h:239
void add_custom_subtitle_track(const char *track_name, const char *codec_id, const uint8_t *codec_context, size_t codec_context_size, const k4a_record_subtitle_settings_t *track_settings)
Adds custom subtitle tracks to the recording Throws error on failure.
Definition: record.hpp:181
bool is_valid() const noexcept
Returns true if the k4a::record is valid, false otherwise.
Definition: record.hpp:72
k4a_result_t
Result code returned by Azure Kinect APIs.
Definition: k4atypes.h:217
Exception type thrown when a K4A API call fails.
Definition: k4a.hpp:33
void add_attachment(const char *attachment_name, const uint8_t *buffer, size_t buffer_size)
Adds an attachment to the recording Throws error on failure.
Definition: record.hpp:142
void add_custom_video_track(const char *track_name, const char *codec_id, const uint8_t *codec_context, size_t codec_context_size, const k4a_record_video_settings_t *track_settings)
Adds custom video tracks to the recording Throws error on failure.
Definition: record.hpp:157
void flush()
Flushes all pending recording data to disk.
Definition: record.hpp:94
#define K4A_FAILED(_result_)
Validate that a k4a_result_t is failed.
Definition: k4atypes.h:753
IMU sample.
Definition: k4atypes.h:1199
void write_custom_track_data(const char *track_name, const std::chrono::microseconds device_timestamp_usec, uint8_t *custom_data, size_t custom_data_size)
Writes data for a custom track to file Throws error on failure.
Definition: record.hpp:250
Definition: k4a.hpp:20
record(k4a_record_t handle=nullptr) noexcept
Creates a k4a::record from a k4a_record_t Takes ownership of the handle, i.e.
Definition: record.hpp:30
Wrapper for k4a_record_t.
Definition: record.hpp:22
Wrapper for k4a_capture_t.
Definition: k4a.hpp:414
void add_imu_track()
Adds the track header for recording IMU Throws error on failure.
Definition: record.hpp:127
void write_capture(const capture &capture)
Writes a camera capture to file Throws error on failure.
Definition: record.hpp:220
k4a_device_t handle() const noexcept
Returns the underlying k4a_device_t handle.
Definition: k4a.hpp:1145
Structure containing additional metadata specific to custom video tracks.
Definition: types.h:224
static record create(const char *path, const device &device, const k4a_device_configuration_t &device_configuration)
Opens a new recording file for writing Throws error on failure.
Definition: record.hpp:273
k4a_capture_t handle() const noexcept
Returns the underlying k4a_capture_t handle.
Definition: k4a.hpp:532
void write_imu_sample(const k4a_imu_sample_t &imu_sample)
Writes an imu sample to file Throws error on failure.
Definition: record.hpp:235
Wrapper for k4a_device_t.
Definition: k4a.hpp:1087
Configuration parameters for an Azure Kinect device.
Definition: k4atypes.h:917
void write_header()
Writes the recording header and metadata to file Throws error on failure.
Definition: record.hpp:205
record(record &&other) noexcept
Moves another k4a::record into a new k4a::record.
Definition: record.hpp:34
void close() noexcept
Closes a K4A recording.
Definition: record.hpp:81
Handle to a k4a recording opened for writing.
Definition: types.h:34