Skip to content

Commit

Permalink
adds ability to get system time from frame, and makes monotonic os_ti…
Browse files Browse the repository at this point in the history
…me_service a possibility
  • Loading branch information
timprepscius committed Apr 30, 2020
1 parent 842ee1e commit 65d9f3c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/librealsense2/h/rs_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions include/librealsense2/hpp/rs_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
26 changes: 25 additions & 1 deletion src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<std::chrono::microseconds>(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; };
Expand Down
8 changes: 8 additions & 0 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 65d9f3c

Please sign in to comment.