ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
AzureKinectRecorder.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 // Copyright (c) Microsoft Corporation. All rights reserved.
9 // Licensed under the MIT License.
10 
11 // clang-format off
12 #include "visualization/visualizer/VisualizerWithKeyCallback.h" // must include first
13 // clang-format on
14 
16 
17 #include <RGBDImage.h>
18 #include <assert.h>
19 #include <k4a/k4a.h>
20 #include <k4arecord/record.h>
21 
22 #include <Eigen/Core>
23 #include <algorithm>
24 #include <atomic>
25 #include <ctime>
26 #include <iostream>
27 
31 
32 namespace cloudViewer {
33 namespace io {
34 
36  const AzureKinectSensorConfig& sensor_config, size_t sensor_index)
37  : RGBDRecorder(),
38  sensor_(AzureKinectSensor(sensor_config)),
39  device_index_(sensor_index) {}
40 
42 
45 }
46 
47 bool AzureKinectRecorder::OpenRecord(const std::string& filename) {
48  if (!is_record_created_) {
49  if (K4A_FAILED(k4a_plugin::k4a_record_create(
50  filename.c_str(), sensor_.device_,
52  &recording_))) {
53  utility::LogWarning("Unable to create recording file: {}",
54  filename);
55  return false;
56  }
58  utility::LogWarning("Unable to write header");
59  return false;
60  }
61  utility::LogInfo("Writing to header");
62 
63  is_record_created_ = true;
64  }
65  return true;
66 }
67 
69  if (is_record_created_) {
70  utility::LogInfo("Saving recording...");
71  if (K4A_FAILED(k4a_plugin::k4a_record_flush(recording_))) {
72  utility::LogWarning("Unable to flush record file");
73  return false;
74  }
76  utility::LogInfo("Done");
77 
78  is_record_created_ = false;
79  }
80  return true;
81 }
82 
83 std::shared_ptr<geometry::RGBDImage> AzureKinectRecorder::RecordFrame(
84  bool write, bool enable_align_depth_to_color) {
85  k4a_capture_t capture = sensor_.CaptureRawFrame();
86  if (capture != nullptr && is_record_created_ && write) {
88  capture))) {
89  utility::LogError("Unable to write to capture");
90  }
91  }
92 
94  capture, enable_align_depth_to_color
96  : nullptr);
97  if (im_rgbd == nullptr) {
98  utility::LogInfo("Invalid capture, skipping this frame");
99  return nullptr;
100  }
102  return im_rgbd;
103 }
104 } // namespace io
105 } // namespace cloudViewer
std::string filename
bool InitSensor() override
Initialize sensor.
bool OpenRecord(const std::string &filename) override
bool CloseRecord() override
Close the recorded mkv file.
AzureKinectRecorder(const AzureKinectSensorConfig &sensor_config, size_t sensor_index)
std::shared_ptr< geometry::RGBDImage > RecordFrame(bool write, bool enable_align_depth_to_color) override
_k4a_device_configuration_t ConvertToNativeConfig() const
AzureKinectSensorConfig sensor_config_
bool Connect(size_t sensor_index) override
static std::shared_ptr< geometry::RGBDImage > DecompressCapture(_k4a_capture_t *capture, _k4a_transformation_t *transformation)
_k4a_capture_t * CaptureRawFrame() const
_k4a_transformation_t * transform_depth_to_color_
#define LogWarning(...)
Definition: Logging.h:72
#define LogInfo(...)
Definition: Logging.h:81
#define LogError(...)
Definition: Logging.h:60
void k4a_capture_release(k4a_capture_t capture_handle)
Definition: K4aPlugin.cpp:396
k4a_result_t k4a_record_write_header(k4a_record_t recording_handle)
Definition: K4aPlugin.cpp:216
k4a_result_t k4a_record_flush(k4a_record_t recording_handle)
Definition: K4aPlugin.cpp:238
k4a_result_t k4a_record_create(const char *path, k4a_device_t device, const k4a_device_configuration_t device_config, k4a_record_t *recording_handle)
Definition: K4aPlugin.cpp:194
k4a_result_t k4a_record_write_capture(k4a_record_t recording_handle, k4a_capture_t capture_handle)
Definition: K4aPlugin.cpp:224
void k4a_record_close(k4a_record_t recording_handle)
Definition: K4aPlugin.cpp:244
Generic file read and write utility for python interface.