-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ds5-timestamp.cpp
187 lines (158 loc) · 6.15 KB
/
ds5-timestamp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2016 Intel Corporation. All Rights Reserved.
#include "ds5-private.h"
#include "ds5-timestamp.h"
#include <mutex>
#include <chrono>
#include <vector>
#include <iterator>
#include "device.h"
#include "context.h"
#include "image.h"
namespace librealsense
{
ds5_timestamp_reader_from_metadata::ds5_timestamp_reader_from_metadata(std::unique_ptr<frame_timestamp_reader> backup_timestamp_reader)
:_backup_timestamp_reader(std::move(backup_timestamp_reader)), _has_metadata(pins), one_time_note(false)
{
reset();
}
bool ds5_timestamp_reader_from_metadata::has_metadata(const request_mapping& mode, const void * metadata, size_t metadata_size)
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
if(metadata == nullptr || metadata_size == 0)
{
return false;
}
for(uint32_t i=0; i<metadata_size; i++)
{
if(((byte*)metadata)[i] != 0)
{
return true;
}
}
return false;
}
rs2_time_t ds5_timestamp_reader_from_metadata::get_frame_timestamp(const request_mapping& mode, const platform::frame_object& fo)
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
auto pin_index = 0;
if (mode.pf->fourcc == 0x5a313620) // Z16
pin_index = 1;
if(!_has_metadata[pin_index])
{
_has_metadata[pin_index] = has_metadata(mode, fo.metadata, fo.metadata_size);
}
auto md = (librealsense::metadata_intel_basic*)(fo.metadata);
if(_has_metadata[pin_index] && md)
{
return (double)(md->header.timestamp)*TIMESTAMP_USEC_TO_MSEC;
}
else
{
if (!one_time_note)
{
LOG_WARNING("UVC metadata payloads not available. Please refer to the installation chapter for details.");
one_time_note = true;
}
return _backup_timestamp_reader->get_frame_timestamp(mode, fo);
}
}
unsigned long long ds5_timestamp_reader_from_metadata::get_frame_counter(const request_mapping & mode, const platform::frame_object& fo) const
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
auto pin_index = 0;
if (mode.pf->fourcc == 0x5a313620) // Z16
pin_index = 1;
if(_has_metadata[pin_index] && fo.metadata_size > platform::uvc_header_size)
{
auto md = (librealsense::metadata_intel_basic*)(fo.metadata);
if (md->capture_valid())
return md->payload.frame_counter;
}
return _backup_timestamp_reader->get_frame_counter(mode, fo);
}
void ds5_timestamp_reader_from_metadata::reset()
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
one_time_note = false;
for (auto i = 0; i < pins; ++i)
{
_has_metadata[i] = false;
}
}
rs2_timestamp_domain ds5_timestamp_reader_from_metadata::get_frame_timestamp_domain(const request_mapping & mode, const platform::frame_object& fo) const
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
auto pin_index = 0;
if (mode.pf->fourcc == 0x5a313620) // Z16
pin_index = 1;
return _has_metadata[pin_index] ? RS2_TIMESTAMP_DOMAIN_HARDWARE_CLOCK :
_backup_timestamp_reader->get_frame_timestamp_domain(mode,fo);
}
ds5_timestamp_reader::ds5_timestamp_reader(std::shared_ptr<platform::time_service> ts)
: counter(pins), _ts(ts)
{
reset();
}
void ds5_timestamp_reader::reset()
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
for (auto i = 0; i < pins; ++i)
{
counter[i] = 0;
}
}
rs2_time_t ds5_timestamp_reader::get_frame_timestamp(const request_mapping& mode, const platform::frame_object& fo)
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
return _ts->get_time();
}
unsigned long long ds5_timestamp_reader::get_frame_counter(const request_mapping & mode, const platform::frame_object& fo) const
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
auto pin_index = 0;
if (mode.pf->fourcc == 0x5a313620) // Z16
pin_index = 1;
return ++counter[pin_index];
}
rs2_timestamp_domain ds5_timestamp_reader::get_frame_timestamp_domain(const request_mapping & mode, const platform::frame_object& fo) const
{
return RS2_TIMESTAMP_DOMAIN_SYSTEM_TIME;
}
ds5_custom_hid_timestamp_reader::ds5_custom_hid_timestamp_reader()
{
counter.resize(sensors);
reset();
}
void ds5_custom_hid_timestamp_reader::reset()
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
for (auto i = 0; i < sensors; ++i)
{
counter[i] = 0;
}
}
rs2_time_t ds5_custom_hid_timestamp_reader::get_frame_timestamp(const request_mapping& /*mode*/, const platform::frame_object& fo)
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
static const uint8_t timestamp_offset = 17;
// The timewstamp shall be trimmed back to 32 bit to allow HID/UVC intra-stream sync
// See ds5_iio_hid_timestamp_reader description
auto timestamp = *((uint32_t*)((const uint8_t*)fo.pixels + timestamp_offset));
// TODO - verify units with custom report
return static_cast<rs2_time_t>(timestamp) * TIMESTAMP_USEC_TO_MSEC;
}
bool ds5_custom_hid_timestamp_reader::has_metadata(const request_mapping& /*mode*/, const void * /*metadata*/, size_t /*metadata_size*/) const
{
return true;
}
unsigned long long ds5_custom_hid_timestamp_reader::get_frame_counter(const request_mapping & mode, const platform::frame_object& fo) const
{
std::lock_guard<std::recursive_mutex> lock(_mtx);
return ++counter.front();
}
rs2_timestamp_domain ds5_custom_hid_timestamp_reader::get_frame_timestamp_domain(const request_mapping & /*mode*/, const platform::frame_object& /*fo*/) const
{
return RS2_TIMESTAMP_DOMAIN_HARDWARE_CLOCK;
}
}