Skip to content

Commit

Permalink
fix: ros2 gstreamer timestamps (#83)
Browse files Browse the repository at this point in the history
* fix: correct gstreamer timestamp offset calculation for image header timestamp

* docs: add use_gst_timestamps parameter to the readme file
  • Loading branch information
drwnz committed Jun 15, 2022
1 parent 20df7e5 commit f9100e4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Note that GStreamer is licensed under the LGPL, and GStreamer plugins have their
* `frame_id`: The [tf2](https://index.ros.org/p/tf2/) frame ID
* `reopen_on_eof`: Re-open the stream if it ends (EOF)
* `sync_sink`: Synchronize the app sink (sometimes setting this to `false` can resolve problems with sub-par framerates)
* `use_gst_timestamps`: Use the GStreamer buffer timestamps for the image message header timestamps (setting this to `false` results in header timestamps being the time that the image buffer transfer is completed)
* `image_encoding`: image encoding ("rgb8", "mono8", "yuv422", "jpeg")
* `use_sensor_data_qos`: The flag to use sensor data qos for camera topic(image, camera_info)

Expand Down
2 changes: 1 addition & 1 deletion include/gscam/gscam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GSCam : public rclcpp::Node

// ROS Inteface
// Calibration between ros::Time and gst timestamps
double time_offset_;
uint64_t time_offset_;
camera_info_manager::CameraInfoManager camera_info_manager_;
image_transport::CameraPublisher camera_pub_;
// Case of a jpeg only publisher
Expand Down
6 changes: 3 additions & 3 deletions src/gscam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ bool GSCam::init_stream()
GstClock * clock = gst_system_clock_obtain();
GstClockTime ct = gst_clock_get_time(clock);
gst_object_unref(clock);
time_offset_ = now().seconds() - GST_TIME_AS_USECONDS(ct) / 1e6;
RCLCPP_INFO(get_logger(), "Time offset: %.3f", time_offset_);
time_offset_ = now().nanoseconds() - GST_TIME_AS_NSECONDS(ct);
RCLCPP_INFO(get_logger(), "Time offset: %.6f", rclcpp::Time(time_offset_).seconds());

gst_element_set_state(pipeline_, GST_STATE_PAUSED);

Expand Down Expand Up @@ -353,7 +353,7 @@ void GSCam::publish_stream()
sensor_msgs::msg::CameraInfo::SharedPtr cinfo;
cinfo.reset(new sensor_msgs::msg::CameraInfo(cur_cinfo));
if (use_gst_timestamps_) {
cinfo->header.stamp = rclcpp::Time(GST_TIME_AS_USECONDS(buf->pts + bt) / 1e6 + time_offset_);
cinfo->header.stamp = rclcpp::Time(GST_TIME_AS_NSECONDS(buf->pts + bt) + time_offset_);
} else {
cinfo->header.stamp = now();
}
Expand Down

0 comments on commit f9100e4

Please sign in to comment.