From 65d9f3c105fb5d799300c69a4f25a5a3021a6367 Mon Sep 17 00:00:00 2001 From: tprepscius Date: Thu, 30 Apr 2020 10:36:35 -0400 Subject: [PATCH] adds ability to get system time from frame, and makes monotonic os_time_service a possibility --- include/librealsense2/h/rs_frame.h | 8 ++++++++ include/librealsense2/hpp/rs_frame.hpp | 8 ++++++++ src/backend.h | 26 +++++++++++++++++++++++++- src/rs.cpp | 8 ++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/librealsense2/h/rs_frame.h b/include/librealsense2/h/rs_frame.h index 35a36db2ab..43205db0c9 100644 --- a/include/librealsense2/h/rs_frame.h +++ b/include/librealsense2/h/rs_frame.h @@ -102,6 +102,14 @@ rs2_timestamp_domain rs2_get_frame_timestamp_domain(const rs2_frame* frameset, r */ rs2_time_t rs2_get_frame_timestamp(const rs2_frame* frame, rs2_error** error); +/** +* retrieve timestamp from frame handle in milliseconds +* \param[in] frame handle returned from a callback +* \param[out] error if non-null, receives any error that occurs during this call, otherwise, errors are ignored +* \return the timestamp of the frame in milliseconds +*/ +rs2_time_t rs2_get_frame_system_time(const rs2_frame* frame, rs2_error** error); + /** * retrieve frame parent sensor from frame handle * \param[in] frame handle returned from a callback diff --git a/include/librealsense2/hpp/rs_frame.hpp b/include/librealsense2/hpp/rs_frame.hpp index 0ecf670af9..50994ca639 100644 --- a/include/librealsense2/hpp/rs_frame.hpp +++ b/include/librealsense2/hpp/rs_frame.hpp @@ -472,6 +472,14 @@ namespace rs2 return r; } + double get_system_time() const + { + rs2_error* e = nullptr; + auto r = rs2_get_frame_system_time(frame_ref, &e); + error::handle(e); + return r; + } + /** retrieve the timestamp domain * \return timestamp domain (clock name) for timestamp values */ diff --git a/src/backend.h b/src/backend.h index a402e3dfb5..06d2c7a8c6 100644 --- a/src/backend.h +++ b/src/backend.h @@ -100,7 +100,7 @@ namespace librealsense virtual ~time_service() = default; }; - class os_time_service: public time_service + class os_time_service_realtime: public time_service { public: rs2_time_t get_time() const override @@ -109,6 +109,30 @@ namespace librealsense } }; + class os_time_service_monotonic: public time_service + { + public: + + typedef std::chrono::high_resolution_clock clock_type; + clock_type clock; + clock_type::time_point startTime; + + os_time_service_monotonic () + { + startTime = clock.now(); + } + + rs2_time_t get_time() const override + { + const static double microsecondsInMillisecond = 1000000 / 1000; + double value = std::chrono::duration_cast(clock.now() - startTime).count() / microsecondsInMillisecond; + + return value; + } + }; + + typedef os_time_service_monotonic os_time_service; + struct guid { uint32_t data1; uint16_t data2, data3; uint8_t data4[8]; }; // subdevice and node fields are assigned by Host driver; unit and GUID are hard-coded in camera firmware struct extension_unit { int subdevice; uint8_t unit; int node; guid id; }; diff --git a/src/rs.cpp b/src/rs.cpp index 2fced98214..f5fca4c9f6 100644 --- a/src/rs.cpp +++ b/src/rs.cpp @@ -781,6 +781,14 @@ rs2_time_t rs2_get_frame_timestamp(const rs2_frame* frame_ref, rs2_error** error } HANDLE_EXCEPTIONS_AND_RETURN(0, frame_ref) +rs2_time_t rs2_get_frame_system_time(const rs2_frame* frame_ref, rs2_error** error) BEGIN_API_CALL +{ + VALIDATE_NOT_NULL(frame_ref); + return ((frame_interface*)frame_ref)->get_frame_system_time(); +} + +HANDLE_EXCEPTIONS_AND_RETURN(0, frame_ref) + rs2_timestamp_domain rs2_get_frame_timestamp_domain(const rs2_frame* frame_ref, rs2_error** error) BEGIN_API_CALL { VALIDATE_NOT_NULL(frame_ref);