ds5-timestamp.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2016 Intel Corporation. All Rights Reserved.
3 
4 #include "ds5-private.h"
5 #include "ds5-timestamp.h"
6 
7 #include <mutex>
8 #include <chrono>
9 #include <vector>
10 #include <iterator>
11 
12 #include "device.h"
13 #include "context.h"
14 #include "image.h"
15 
16 namespace librealsense
17 {
18  ds5_timestamp_reader_from_metadata::ds5_timestamp_reader_from_metadata(std::unique_ptr<frame_timestamp_reader> backup_timestamp_reader)
19  :_backup_timestamp_reader(std::move(backup_timestamp_reader)), _has_metadata(pins), one_time_note(false)
20  {
21  reset();
22  }
23 
24  bool ds5_timestamp_reader_from_metadata::has_metadata(const std::shared_ptr<frame_interface>& frame)
25  {
26  std::lock_guard<std::recursive_mutex> lock(_mtx);
27 
28  auto f = std::dynamic_pointer_cast<librealsense::frame>(frame);
29  if (!f)
30  {
31  LOG_ERROR("Frame is not valid. Failed to downcast to librealsense::frame.");
32  return false;
33  }
34  auto md = f->additional_data.metadata_blob;
35  auto mds = f->additional_data.metadata_size;
36 
37  if (mds)
38  return true;
39 
40  return false;
41  }
42 
44  {
45  std::lock_guard<std::recursive_mutex> lock(_mtx);
46 
47  auto f = std::dynamic_pointer_cast<librealsense::frame>(frame);
48  if (!f)
49  {
50  LOG_ERROR("Frame is not valid. Failed to downcast to librealsense::frame.");
51  return 0;
52  }
53  size_t pin_index = 0;
54 
55  if (frame->get_stream()->get_format() == RS2_FORMAT_Z16)
56  pin_index = 1;
57 
58  _has_metadata[pin_index] = has_metadata(frame);
59 
60  auto md = (librealsense::metadata_intel_basic*)(f->additional_data.metadata_blob.data());
61  if(_has_metadata[pin_index] && md)
62  {
63  return (double)(md->header.timestamp)*TIMESTAMP_USEC_TO_MSEC;
64  }
65  else
66  {
67  if (!one_time_note)
68  {
69  LOG_WARNING("UVC metadata payloads not available. Please refer to the installation chapter for details.");
70  one_time_note = true;
71  }
72  return _backup_timestamp_reader->get_frame_timestamp(frame);
73  }
74  }
75 
76  unsigned long long ds5_timestamp_reader_from_metadata::get_frame_counter(const std::shared_ptr<frame_interface>& frame) const
77  {
78  std::lock_guard<std::recursive_mutex> lock(_mtx);
79 
80  auto f = std::dynamic_pointer_cast<librealsense::frame>(frame);
81  if (!f)
82  {
83  LOG_ERROR("Frame is not valid. Failed to downcast to librealsense::frame.");
84  return 0;
85  }
86  size_t pin_index = 0;
87 
88  if (frame->get_stream()->get_format() == RS2_FORMAT_Z16)
89  pin_index = 1;
90 
91  if(_has_metadata[pin_index] && f->additional_data.metadata_size > platform::uvc_header_size)
92  {
93  auto md = (librealsense::metadata_intel_basic*)(f->additional_data.metadata_blob.data());
94  if (md->capture_valid())
95  return md->payload.frame_counter;
96  }
97 
98  return _backup_timestamp_reader->get_frame_counter(frame);
99  }
100 
102  {
103  std::lock_guard<std::recursive_mutex> lock(_mtx);
104  one_time_note = false;
105  for (auto i = 0; i < pins; ++i)
106  {
107  _has_metadata[i] = false;
108  }
109  }
110 
112  {
113  std::lock_guard<std::recursive_mutex> lock(_mtx);
114  auto pin_index = 0;
115  if (frame->get_stream()->get_format() == RS2_FORMAT_Z16)
116  pin_index = 1;
117 
119  _backup_timestamp_reader->get_frame_timestamp_domain(frame);
120  }
121 
122  ds5_timestamp_reader::ds5_timestamp_reader(std::shared_ptr<platform::time_service> ts)
123  : counter(pins), _ts(ts)
124  {
125  reset();
126  }
127 
129  {
130  std::lock_guard<std::recursive_mutex> lock(_mtx);
131  for (auto i = 0; i < pins; ++i)
132  {
133  counter[i] = 0;
134  }
135  }
136 
137  rs2_time_t ds5_timestamp_reader::get_frame_timestamp(const std::shared_ptr<frame_interface>& frame)
138  {
139  std::lock_guard<std::recursive_mutex> lock(_mtx);
140  return _ts->get_time();
141  }
142 
143  unsigned long long ds5_timestamp_reader::get_frame_counter(const std::shared_ptr<frame_interface>& frame) const
144  {
145  std::lock_guard<std::recursive_mutex> lock(_mtx);
146  auto pin_index = 0;
147  if (frame->get_stream()->get_format() == RS2_FORMAT_Z16)
148  pin_index = 1;
149 
150  return ++counter[pin_index];
151  }
152 
153  rs2_timestamp_domain ds5_timestamp_reader::get_frame_timestamp_domain(const std::shared_ptr<frame_interface>& frame) const
154  {
156  }
157 
159  {
160  counter.resize(sensors);
161  reset();
162  }
163 
165  {
166  std::lock_guard<std::recursive_mutex> lock(_mtx);
167  for (auto i = 0; i < sensors; ++i)
168  {
169  counter[i] = 0;
170  }
171  }
172 
174  {
175  std::lock_guard<std::recursive_mutex> lock(_mtx);
176  static const uint8_t timestamp_offset = 17;
177  auto f = std::dynamic_pointer_cast<librealsense::frame>(frame);
178  if (!f)
179  {
180  LOG_ERROR("Frame is not valid. Failed to downcast to librealsense::frame.");
181  return 0;
182  }
183 
184  // The timewstamp shall be trimmed back to 32 bit to allow HID/UVC intra-stream sync
185  // See ds5_iio_hid_timestamp_reader description
186  auto timestamp = *((uint32_t*)((const uint8_t*)f->get_frame_data() + timestamp_offset));
187  // TODO - verify units with custom report
188  return static_cast<rs2_time_t>(timestamp) * TIMESTAMP_USEC_TO_MSEC;
189  }
190 
191  bool ds5_custom_hid_timestamp_reader::has_metadata(const std::shared_ptr<frame_interface>& frame) const
192  {
193  return true;
194  }
195 
196  unsigned long long ds5_custom_hid_timestamp_reader::get_frame_counter(const std::shared_ptr<frame_interface>& frame) const
197  {
198  std::lock_guard<std::recursive_mutex> lock(_mtx);
199  return ++counter.front();
200  }
201 
203  {
205  }
206 }
metadata_intel_basic - a subset of the full metadata required to provide the essential sensor attribu...
Definition: src/metadata.h:688
static const textual_icon lock
Definition: model-views.h:218
static const double TIMESTAMP_USEC_TO_MSEC
Definition: src/types.h:92
unsigned long long get_frame_counter(const std::shared_ptr< frame_interface > &frame) const override
constexpr uint8_t uvc_header_size
Definition: backend.h:165
#define LOG_WARNING(...)
Definition: src/types.h:241
bool has_metadata(const std::shared_ptr< frame_interface > &frame) const
unsigned char uint8_t
Definition: stdint.h:78
GLdouble f
ds5_timestamp_reader(std::shared_ptr< platform::time_service > ts)
ds5_timestamp_reader_from_metadata(std::unique_ptr< frame_timestamp_reader > backup_timestamp_reader)
std::unique_ptr< frame_timestamp_reader > _backup_timestamp_reader
Definition: ds5-timestamp.h:15
GLuint counter
Definition: glext.h:5684
rs2_time_t get_frame_timestamp(const std::shared_ptr< frame_interface > &frame) override
rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr< frame_interface > &frame) const override
unsigned int uint32_t
Definition: stdint.h:80
std::vector< std::atomic< bool > > _has_metadata
Definition: ds5-timestamp.h:17
rs2_time_t get_frame_timestamp(const std::shared_ptr< frame_interface > &frame) override
unsigned long long get_frame_counter(const std::shared_ptr< frame_interface > &frame) const override
#define LOG_ERROR(...)
Definition: src/types.h:242
std::shared_ptr< platform::time_service > _ts
Definition: ds5-timestamp.h:39
unsigned long long get_frame_counter(const std::shared_ptr< frame_interface > &frame) const override
rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr< frame_interface > &frame) const override
rs2_timestamp_domain get_frame_timestamp_domain(const std::shared_ptr< frame_interface > &frame) const override
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
int i
bool has_metadata(const std::shared_ptr< frame_interface > &frame)
rs2_time_t get_frame_timestamp(const std::shared_ptr< frame_interface > &frame) override
double rs2_time_t
Definition: rs_types.h:300
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:13