Skip to content

Commit 60789f0

Browse files
RoseFlunderStuart Alldritt
authored andcommitted
Switch from the sensor sdk playback c-api to the c++ wrapper (opencv#73)
1 parent f06d937 commit 60789f0

File tree

4 files changed

+20
-58
lines changed

4 files changed

+20
-58
lines changed

include/azure_kinect_ros_driver/k4a_calibration_transform_data.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
#include <k4a/k4a.h>
1414
#include <k4a/k4a.hpp>
15-
#include <k4arecord/playback.h>
15+
#include <k4arecord/playback.hpp>
1616
#include <ros/ros.h>
1717
#include <tf2_ros/static_transform_broadcaster.h>
1818
#include <tf2/LinearMath/Quaternion.h>
@@ -29,7 +29,7 @@ class K4ACalibrationTransformData
2929
public:
3030

3131
void initialize(const k4a::device &device, k4a_depth_mode_t depthMode, k4a_color_resolution_t resolution, K4AROSDeviceParams params);
32-
void initialize(const k4a_playback_t &k4a_playback_handle, K4AROSDeviceParams params);
32+
void initialize(const k4a::playback &k4a_playback_handle, K4AROSDeviceParams params);
3333
int getDepthWidth();
3434
int getDepthHeight();
3535
int getColorWidth();

include/azure_kinect_ros_driver/k4a_ros_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
#include <k4a/k4a.h>
1616
#include <k4a/k4a.hpp>
17-
#include <k4arecord/playback.h>
17+
#include <k4arecord/playback.hpp>
1818
#include <ros/ros.h>
1919
#include <sensor_msgs/CameraInfo.h>
2020
#include <sensor_msgs/Image.h>
@@ -121,7 +121,7 @@ class K4AROSDevice
121121
K4ACalibrationTransformData calibration_data_;
122122

123123
// K4A Recording
124-
k4a_playback_t k4a_playback_handle_;
124+
k4a::playback k4a_playback_handle_;
125125
std::mutex k4a_playback_handle_mutex_;
126126

127127
#if defined(K4A_BODY_TRACKING)

src/k4a_calibration_transform_data.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ void K4ACalibrationTransformData::initialize(const k4a::device &device,
2828
initialize(params);
2929
}
3030

31-
void K4ACalibrationTransformData::initialize(const k4a_playback_t &k4a_playback_handle,
31+
void K4ACalibrationTransformData::initialize(const k4a::playback &k4a_playback_handle,
3232
const K4AROSDeviceParams params)
3333
{
34-
if (k4a_playback_get_calibration(k4a_playback_handle, &k4a_calibration_) != K4A_RESULT_SUCCEEDED)
35-
{
36-
throw std::runtime_error("Failed to get playback calibration!");
37-
}
34+
k4a_calibration_ = k4a_playback_handle.get_calibration();
3835
initialize(params);
3936
}
4037

src/k4a_ros_device.cpp

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,14 @@ K4AROSDevice::K4AROSDevice(const NodeHandle &n, const NodeHandle &p) : k4a_devic
5252
{
5353
ROS_INFO("Node is started in playback mode");
5454
ROS_INFO_STREAM("Try to open recording file " << params_.recording_file);
55-
// Open recording file
56-
if (k4a_playback_open(params_.recording_file.c_str(), &k4a_playback_handle_) != K4A_RESULT_SUCCEEDED)
57-
{
58-
ROS_ERROR_STREAM("Failed to open recording");
59-
return;
60-
}
61-
uint64_t recording_length = k4a_playback_get_last_timestamp_usec(k4a_playback_handle_);
62-
ROS_INFO_STREAM("Successfully openend recording file. Recording is " << recording_length / 1000000 << " seconds long");
6355

64-
k4a_record_configuration_t record_config;
65-
if (k4a_playback_get_record_configuration(k4a_playback_handle_, &record_config) != K4A_RESULT_SUCCEEDED)
66-
{
67-
ROS_ERROR_STREAM("Failed to get record configuration");
68-
return;
69-
}
56+
// Open recording file and print its length
57+
k4a_playback_handle_ = k4a::playback::open(params_.recording_file.c_str());
58+
auto recording_length = k4a_playback_handle_.get_recording_length();
59+
ROS_INFO_STREAM("Successfully openend recording file. Recording is " << recording_length.count() / 1000000 << " seconds long");
60+
61+
// Get the recordings configuration to overwrite node parameters
62+
k4a_record_configuration_t record_config = k4a_playback_handle_.get_record_configuration();
7063

7164
// Overwrite fps param with recording configuration for a correct loop rate in the frame publisher thread
7265
switch (record_config.camera_fps)
@@ -95,7 +88,7 @@ K4AROSDevice::K4AROSDevice(const NodeHandle &n, const NodeHandle &p) : k4a_devic
9588
// This is necessary because at the moment there are only checks in place which use BgraPixel size
9689
else if (params_.color_enabled && record_config.color_track_enabled && record_config.color_format != K4A_IMAGE_FORMAT_COLOR_BGRA32)
9790
{
98-
k4a_playback_set_color_conversion(k4a_playback_handle_, K4A_IMAGE_FORMAT_COLOR_BGRA32);
91+
k4a_playback_handle_.set_color_conversion(K4A_IMAGE_FORMAT_COLOR_BGRA32);
9992
}
10093

10194
// Disable depth if the recording has neither ir track nor depth track
@@ -256,7 +249,7 @@ K4AROSDevice::~K4AROSDevice()
256249

257250
if (k4a_playback_handle_)
258251
{
259-
k4a_playback_close(k4a_playback_handle_);
252+
k4a_playback_handle_.close();
260253
}
261254

262255
#if defined(K4A_BODY_TRACKING)
@@ -833,8 +826,6 @@ void K4AROSDevice::framePublisherThread()
833826

834827
while (running_ && ros::ok() && !ros::isShuttingDown())
835828
{
836-
bool success = true;
837-
838829
if (k4a_device_)
839830
{
840831
// TODO: consider appropriate capture timeout based on camera framerate
@@ -848,21 +839,13 @@ void K4AROSDevice::framePublisherThread()
848839
else if (k4a_playback_handle_)
849840
{
850841
std::lock_guard<std::mutex> guard(k4a_playback_handle_mutex_);
851-
k4a_capture_t capture_temp;
852-
k4a_stream_result_t stream_result = k4a_playback_get_next_capture(k4a_playback_handle_, &capture_temp);
853-
854-
if (stream_result == K4A_STREAM_RESULT_EOF)
842+
if (!k4a_playback_handle_.get_next_capture(&capture))
855843
{
856844
// rewind recording if looping is enabled
857845
if (params_.recording_loop_enabled)
858846
{
859-
if (k4a_playback_seek_timestamp(k4a_playback_handle_, 0, K4A_PLAYBACK_SEEK_BEGIN) != K4A_RESULT_SUCCEEDED)
860-
{
861-
ROS_FATAL("Error seeking recording to beginning. node cannot continue.");
862-
ros::requestShutdown();
863-
return;
864-
}
865-
stream_result = k4a_playback_get_next_capture(k4a_playback_handle_, &capture_temp);
847+
k4a_playback_handle_.seek_timestamp(std::chrono::microseconds(0), K4A_PLAYBACK_SEEK_BEGIN);
848+
k4a_playback_handle_.get_next_capture(&capture);
866849
imu_stream_end_of_file_ = false;
867850
last_imu_time_usec_ = 0;
868851
}
@@ -873,14 +856,7 @@ void K4AROSDevice::framePublisherThread()
873856
return;
874857
}
875858
}
876-
else if (stream_result != K4A_STREAM_RESULT_SUCCEEDED)
877-
{
878-
ROS_FATAL("Failed to get next capture from recording file: node cannot continue.");
879-
ros::requestShutdown();
880-
return;
881-
}
882859

883-
capture = k4a::capture(capture_temp);
884860
last_capture_time_usec_ = getCaptureTimestamp(capture).count();
885861
}
886862

@@ -1141,14 +1117,11 @@ void K4AROSDevice::imuPublisherThread()
11411117
{
11421118
ros::Rate loop_rate(300);
11431119

1144-
k4a_wait_result_t waitResult;
11451120
k4a_result_t result;
1146-
11471121
k4a_imu_sample_t sample;
11481122

11491123
while (running_ && ros::ok() && !ros::isShuttingDown())
11501124
{
1151-
11521125
if (k4a_device_)
11531126
{
11541127
// IMU messages are delivered in batches at 300 Hz. Drain the queue of IMU messages by
@@ -1177,12 +1150,11 @@ void K4AROSDevice::imuPublisherThread()
11771150
while (last_imu_time_usec_ <= last_capture_time_usec_ && !imu_stream_end_of_file_)
11781151
{
11791152
std::lock_guard<std::mutex> guard(k4a_playback_handle_mutex_);
1180-
k4a_stream_result_t stream_result = k4a_playback_get_next_imu_sample(k4a_playback_handle_, &sample);
1181-
if (stream_result == K4A_STREAM_RESULT_EOF)
1153+
if (!k4a_playback_handle_.get_next_imu_sample(&sample))
11821154
{
11831155
imu_stream_end_of_file_ = true;
11841156
}
1185-
else if (stream_result == K4A_STREAM_RESULT_SUCCEEDED)
1157+
else
11861158
{
11871159
ImuPtr imu_msg(new Imu);
11881160
result = getImuFrame(sample, imu_msg);
@@ -1192,13 +1164,6 @@ void K4AROSDevice::imuPublisherThread()
11921164

11931165
last_imu_time_usec_ = sample.acc_timestamp_usec;
11941166
}
1195-
else
1196-
{
1197-
ROS_FATAL("Failed to get next imu sample from recording. node cannot continue.");
1198-
ros::requestShutdown();
1199-
return;
1200-
}
1201-
12021167
}
12031168
}
12041169

0 commit comments

Comments
 (0)