Azure Kinect Sensor SDK  refs/pull/548/merge
Documentation for https://github.com/Microsoft/Azure-Kinect-Sensor-SDK
playback.hpp
1 
7 #ifndef K4A_PLAYBACK_HPP
8 #define K4A_PLAYBACK_HPP
9 
10 #include <k4a/k4a.hpp>
11 #include <k4arecord/playback.h>
12 
13 #include <algorithm>
14 #include <chrono>
15 #include <string>
16 #include <vector>
17 
18 namespace k4a
19 {
20 
28 class playback
29 {
30 public:
36  playback(k4a_playback_t handle = nullptr) noexcept : m_handle(handle) {}
37 
40  playback(playback &&other) noexcept : m_handle(other.m_handle)
41  {
42  other.m_handle = nullptr;
43  }
44 
45  playback(const playback &) = delete;
46 
47  ~playback()
48  {
49  close();
50  }
51 
52  playback &operator=(const playback &) = delete;
53 
56  playback &operator=(playback &&other) noexcept
57  {
58  if (this != &other)
59  {
60  close();
61  m_handle = other.m_handle;
62  other.m_handle = nullptr;
63  }
64 
65  return *this;
66  }
67 
70  operator bool() const noexcept
71  {
72  return m_handle != nullptr;
73  }
74 
79  void close() noexcept
80  {
81  if (m_handle != nullptr)
82  {
83  k4a_playback_close(m_handle);
84  m_handle = nullptr;
85  }
86  }
87 
93  std::vector<uint8_t> get_raw_calibration() const
94  {
95  std::vector<uint8_t> calibration;
96  size_t buffer = 0;
97  k4a_buffer_result_t result = k4a_playback_get_raw_calibration(m_handle, &calibration[0], &buffer);
98 
99  if (result == K4A_BUFFER_RESULT_TOO_SMALL && buffer > 1)
100  {
101  calibration.resize(buffer);
102  result = k4a_playback_get_raw_calibration(m_handle, &calibration[0], &buffer);
103  }
104 
105  if (result != K4A_BUFFER_RESULT_SUCCEEDED)
106  {
107  throw error("Failed to read raw device calibration from recording!");
108  }
109 
110  return calibration;
111  }
112 
119  {
120  calibration calib;
121  k4a_result_t result = k4a_playback_get_calibration(m_handle, &calib);
122 
123  if (K4A_RESULT_SUCCEEDED != result)
124  {
125  throw error("Failed to read device calibration from recording!");
126  }
127 
128  return calib;
129  }
130 
136  {
138  k4a_result_t result = k4a_playback_get_record_configuration(m_handle, &config);
139 
140  if (K4A_RESULT_SUCCEEDED != result)
141  {
142  throw error("Failed to read record configuration!");
143  }
144 
145  return config;
146  }
147 
155  {
156  k4a_capture_t capture_handle;
157  k4a_stream_result_t result = k4a_playback_get_next_capture(m_handle, &capture_handle);
158 
159  if (K4A_STREAM_RESULT_SUCCEEDED == result)
160  {
161  *cap = capture(capture_handle);
162  return true;
163  }
164  else if (K4A_STREAM_RESULT_EOF == result)
165  {
166  return false;
167  }
168 
169  throw error("Failed to get next capture!");
170  }
171 
179  {
180  k4a_capture_t capture_handle;
181  k4a_stream_result_t result = k4a_playback_get_previous_capture(m_handle, &capture_handle);
182 
183  if (K4A_STREAM_RESULT_SUCCEEDED == result)
184  {
185  *cap = capture(capture_handle);
186  return true;
187  }
188  else if (K4A_STREAM_RESULT_EOF == result)
189  {
190  return false;
191  }
192 
193  throw error("Failed to get next capture!");
194  }
195 
201  bool get_tag(const char *name, std::string *out) const
202  {
203  std::string tag;
204  size_t buffer = 0;
205  k4a_buffer_result_t result = k4a_playback_get_tag(m_handle, name, &tag[0], &buffer);
206 
207  if (result == K4A_BUFFER_RESULT_TOO_SMALL && buffer > 0)
208  {
209  tag.resize(buffer);
210  result = k4a_playback_get_tag(m_handle, name, &tag[0], &buffer);
211  if (result == K4A_BUFFER_RESULT_SUCCEEDED && tag[buffer - 1] == 0)
212  {
213  // std::string expects there to not be as null terminator at the end of its data but
214  // k4a_playback_get_tag adds a null terminator, so we drop the last character of the string after we
215  // get the result back.
216  tag.resize(buffer - 1);
217  }
218  }
219 
220  if (result != K4A_BUFFER_RESULT_SUCCEEDED)
221  {
222  return false;
223  }
224 
225  *out = std::move(tag);
226 
227  return true;
228  }
229 
237  {
238  k4a_stream_result_t result = k4a_playback_get_next_imu_sample(m_handle, sample);
239 
240  if (K4A_STREAM_RESULT_SUCCEEDED == result)
241  {
242  return true;
243  }
244  else if (K4A_STREAM_RESULT_EOF == result)
245  {
246  return false;
247  }
248 
249  throw error("Failed to get next IMU sample!");
250  }
251 
259  {
260  k4a_stream_result_t result = k4a_playback_get_previous_imu_sample(m_handle, sample);
261 
262  if (K4A_STREAM_RESULT_SUCCEEDED == result)
263  {
264  return true;
265  }
266  else if (K4A_STREAM_RESULT_EOF == result)
267  {
268  return false;
269  }
270 
271  throw error("Failed to get previous IMU sample!");
272  }
273 
279  void seek_timestamp(std::chrono::microseconds offset, k4a_playback_seek_origin_t origin)
280  {
281  k4a_result_t result = k4a_playback_seek_timestamp(m_handle, offset.count(), origin);
282 
283  if (K4A_RESULT_SUCCEEDED != result)
284  {
285  throw error("Failed to seek recording!");
286  }
287  }
288 
293  std::chrono::microseconds get_last_timestamp() const noexcept
294  {
295  return std::chrono::microseconds(k4a_playback_get_last_timestamp_usec(m_handle));
296  }
297 
306  {
307  k4a_result_t result = k4a_playback_set_color_conversion(m_handle, format);
308 
309  if (K4A_RESULT_SUCCEEDED != result)
310  {
311  throw error("Failed to set color conversion!");
312  }
313  }
314 
320  static playback open(const char *path)
321  {
322  k4a_playback_t handle = nullptr;
323  k4a_result_t result = k4a_playback_open(path, &handle);
324 
325  if (K4A_RESULT_SUCCEEDED != result)
326  {
327  throw error("Failed to open recording!");
328  }
329 
330  return playback(handle);
331  }
332 
333 private:
334  k4a_playback_t m_handle;
335 };
336 
337 } // namespace k4a
338 
339 #endif
The result was successful.
Definition: k4atypes.h:233
The result was successful.
Definition: types.h:68
playback(k4a_playback_t handle=nullptr) noexcept
Creates a k4a::playback from a k4a_playback_t Takes ownership of the handle, i.e. ...
Definition: playback.hpp:36
std::chrono::microseconds get_last_timestamp() const noexcept
Get the last valid timestamp in the recording.
Definition: playback.hpp:293
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
playback & operator=(playback &&other) noexcept
Moves another k4a::playback into this k4a::playback; other is set to invalid.
Definition: playback.hpp:56
Handle to an Azure Kinect capture.
Definition: k4atypes.h:122
k4a_buffer_result_t
Result code returned by Azure Kinect APIs.
Definition: k4atypes.h:231
void seek_timestamp(std::chrono::microseconds offset, k4a_playback_seek_origin_t origin)
Seeks to a specific time point in the recording Throws error on failure.
Definition: playback.hpp:279
k4a_stream_result_t
Return codes returned by Azure Kinect playback API.
Definition: types.h:66
bool get_next_imu_sample(k4a_imu_sample_t *sample)
Get the next IMU sample in the recording Returns true if a sample was available, false if there are n...
Definition: playback.hpp:236
IMU sample.
Definition: k4atypes.h:1157
static playback open(const char *path)
Opens a K4A recording for playback.
Definition: playback.hpp:320
calibration get_calibration() const
Get the camera calibration for the K4A device that made the recording, which is used for all transfor...
Definition: playback.hpp:118
std::vector< uint8_t > get_raw_calibration() const
Get the raw calibration blob for the K4A device that made the recording.
Definition: playback.hpp:93
k4a_record_configuration_t get_record_configuration() const
Gets the configuration of the recording.
Definition: playback.hpp:135
Definition: k4a.hpp:20
void close() noexcept
Closes a K4A recording.
Definition: playback.hpp:79
playback(playback &&other) noexcept
Moves another k4a::playback into a new k4a::playback.
Definition: playback.hpp:40
bool get_next_capture(capture *cap)
Get the next capture in the recording Returns true if a capture was available, false if there are non...
Definition: playback.hpp:154
Wrapper for k4a_capture_t.
Definition: k4a.hpp:402
bool get_previous_imu_sample(k4a_imu_sample_t *sample)
Get the previous IMU sample in the recording Returns true if a sample was available, false if there are none left Throws error on failure.
Definition: playback.hpp:258
k4a_playback_seek_origin_t
Playback seeking positions.
Definition: types.h:81
Structure containing the device configuration used to record.
Definition: types.h:105
The result was successful.
Definition: k4atypes.h:219
Handle to a k4a recording opened for playback.
Definition: types.h:49
void set_color_conversion(k4a_image_format_t format)
Set the image format that color captures will be converted to.
Definition: playback.hpp:305
The input buffer was too small.
Definition: k4atypes.h:235
bool get_previous_capture(capture *cap)
Get the next capture in the recording Returns true if a capture was available, false if there are non...
Definition: playback.hpp:178
Wrapper for k4a_playback_t.
Definition: playback.hpp:28
Wrapper for k4a_calibration_t.
Definition: k4a.hpp:614
bool get_tag(const char *name, std::string *out) const
Reads the value of a tag from the recording Returns false if the tag does not exist.
Definition: playback.hpp:201
The end of the data stream was reached.
Definition: types.h:70
k4a_image_format_t
Image format type.
Definition: k4atypes.h:332