From ada0122f9876c134d5b1bbde16e308fc71b04ae3 Mon Sep 17 00:00:00 2001 From: ob-yalian Date: Thu, 20 Aug 2026 17:12:20 +0800 Subject: [PATCH] feat: implement thread-safe image saving with mutex and update save logic --- .../include/orbbec_camera/ob_camera_node.h | 1 + orbbec_camera/src/ob_camera_node.cpp | 26 ++++++++++++++----- orbbec_camera/src/ros_service.cpp | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/orbbec_camera/include/orbbec_camera/ob_camera_node.h b/orbbec_camera/include/orbbec_camera/ob_camera_node.h index 8ad62006..4685b321 100644 --- a/orbbec_camera/include/orbbec_camera/ob_camera_node.h +++ b/orbbec_camera/include/orbbec_camera/ob_camera_node.h @@ -809,6 +809,7 @@ class OBCameraNode { std::unique_ptr d2c_viewer_ = nullptr; std::map save_images_; std::map save_images_count_; + std::mutex save_images_mutex_; int max_save_images_count_ = 10; std::atomic_bool save_point_cloud_{false}; std::atomic_bool save_colored_point_cloud_{false}; diff --git a/orbbec_camera/src/ob_camera_node.cpp b/orbbec_camera/src/ob_camera_node.cpp index 5fba339f..241a5e6c 100755 --- a/orbbec_camera/src/ob_camera_node.cpp +++ b/orbbec_camera/src/ob_camera_node.cpp @@ -6995,18 +6995,34 @@ void OBCameraNode::saveImageToFile(const stream_index_pair &stream_index, const const cv::Mat &image_to_save, const sensor_msgs::msg::Image &image_msg, const std::shared_ptr &frame) { - if (save_images_[stream_index]) { + if (save_images_[stream_index].load(std::memory_order_acquire)) { + int index = 0; + { + std::lock_guard lock(save_images_mutex_); + if (!save_images_[stream_index].load(std::memory_order_relaxed)) { + return; + } + index = save_images_count_[stream_index]++; + if (save_images_count_[stream_index] >= max_save_images_count_) { + save_images_[stream_index].store(false, std::memory_order_release); + } + } + auto now = std::chrono::system_clock::now(); auto in_time_t = std::chrono::system_clock::to_time_t(now); auto us = std::chrono::duration_cast(now.time_since_epoch()) % 1000000; + std::tm local_time{}; + if (localtime_r(&in_time_t, &local_time) == nullptr) { + RCLCPP_ERROR_STREAM(logger_, "Failed to convert image save timestamp to local time"); + return; + } std::stringstream ss; - ss << std::put_time(std::localtime(&in_time_t), "%Y%m%d_%H%M%S"); + ss << std::put_time(&local_time, "%Y%m%d_%H%M%S"); ss << "_" << std::setw(6) << std::setfill('0') << us.count(); const auto output_directory = std::filesystem::current_path() / "image"; auto fps = fps_[stream_index]; - const int index = save_images_count_[stream_index]; const std::string file_name = stream_name_[stream_index] + "_" + std::to_string(image_msg.width) + "x" + std::to_string(image_msg.height) + "_" + std::to_string(fps) + @@ -7091,10 +7107,6 @@ void OBCameraNode::saveImageToFile(const stream_index_pair &stream_index, const } metadata_ofs.close(); } - - if (++save_images_count_[stream_index] >= max_save_images_count_) { - save_images_[stream_index] = false; - } } } diff --git a/orbbec_camera/src/ros_service.cpp b/orbbec_camera/src/ros_service.cpp index 8c4be54b..fbb68ab7 100644 --- a/orbbec_camera/src/ros_service.cpp +++ b/orbbec_camera/src/ros_service.cpp @@ -1961,10 +1961,11 @@ void OBCameraNode::saveImageCallback(const std::shared_ptr& response) { (void)request; (void)response; + std::lock_guard lock(save_images_mutex_); for (const auto& stream_index : IMAGE_STREAMS) { if (enable_stream_[stream_index]) { - save_images_[stream_index] = true; save_images_count_[stream_index] = 0; + save_images_[stream_index].store(true, std::memory_order_release); } } }