Use XrTime for all time data

This commit is contained in:
Patrick Nicolas
2024-03-24 14:01:44 +01:00
parent f951034de4
commit 8e3979cc8c
11 changed files with 47 additions and 63 deletions
+15 -24
View File
@@ -31,14 +31,15 @@ void clock_offset_estimator::request_sample(wivrn_connection & connection)
return;
next_sample = std::chrono::steady_clock::now() + sample_interval.load();
xrt::drivers::wivrn::to_headset::timesync_query timesync{};
timesync.query = std::chrono::nanoseconds(os_monotonic_get_ns());
connection.send_stream(timesync);
connection.send_stream(
xrt::drivers::wivrn::to_headset::timesync_query{
.query = XrTime(os_monotonic_get_ns()),
});
}
void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset::timesync_response & base_sample)
{
auto now = std::chrono::nanoseconds(os_monotonic_get_ns());
XrTime now = os_monotonic_get_ns();
clock_offset_estimator::sample sample{base_sample, now};
std::lock_guard lock(mutex);
if (samples.size() < num_samples)
@@ -50,12 +51,12 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
sample_interval = std::chrono::seconds(1);
int64_t latency = 0;
for (const auto& s: samples)
latency += (s.received - s.query).count();
latency += s.received - s.query;
latency /= samples.size();
// packets with too high latency are likely to be retransmitted
if ((sample.received - sample.query).count() > 3 * latency)
if (sample.received - sample.query > 3 * latency)
{
U_LOG_D("drop packet for latency %ld > %ld", (sample.received - sample.query).count() /1000, latency/1000);
U_LOG_D("drop packet for latency %ldµs > %ldµs", (sample.received - sample.query) /1000, latency/1000);
return;
}
@@ -74,7 +75,7 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
double y0 = 0;
for (const auto & s: samples)
{
x0 += (s.query + s.received).count() * 0.5;
x0 += (s.query + s.received) * 0.5;
y0 += s.response;
}
x0 /= n;
@@ -95,10 +96,10 @@ void clock_offset_estimator::add_sample(const xrt::drivers::wivrn::from_headset:
{
#if 1
// assume symmetrical latency
double x = (s.query + s.received).count() * 0.5 - x0;
double x = (s.query + s.received)* 0.5 - x0;
#else
// assume latency is only on server -> headset link
double x = (s.received).count() - x0;
double x = s.received - x0;
#endif
double y = s.response - y0;
sum_x += x;
@@ -126,22 +127,12 @@ clock_offset clock_offset_estimator::get_offset()
return offset;
}
int64_t clock_offset::from_headset(uint64_t ts) const
XrTime clock_offset::from_headset(XrTime ts) const
{
int64_t res = (int64_t(ts) - b) / a;
#ifndef NDEBUG
if (res < 0)
U_LOG_W("negative from_headset: %ld", res);
#endif
return res;
return (ts - b) / a;
}
std::chrono::nanoseconds clock_offset::to_headset(uint64_t timestamp_ns) const
XrTime clock_offset::to_headset(XrTime timestamp_ns) const
{
int64_t res = int64_t(a * timestamp_ns) + b;
#ifndef NDEBUG
if (res < 0)
U_LOG_W("negative to_headset: %ld", res);
#endif
return std::chrono::nanoseconds(res);
return a * timestamp_ns + b;
}
+3 -4
View File
@@ -38,17 +38,16 @@ struct clock_offset
operator bool() { return b != 0;}
int64_t from_headset(uint64_t) const;
XrTime from_headset(XrTime) const;
std::chrono::nanoseconds
to_headset(uint64_t timestamp_ns) const;
XrTime to_headset(XrTime timestamp_ns) const;
};
class clock_offset_estimator
{
struct sample: public xrt::drivers::wivrn::from_headset::timesync_response
{
std::chrono::nanoseconds received;
XrTime received;
};
std::mutex mutex;
+1 -1
View File
@@ -537,7 +537,7 @@ static VkResult comp_wivrn_present(struct comp_target * ct,
cn->psc.images[index].frame_index = cn->current_frame_id;
auto & view_info = cn->psc.images[index].view_info;
view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns).count();
view_info.display_time = cn->cnx->get_offset().to_headset(desired_present_time_ns);
for (int eye = 0; eye < 2; ++eye)
{
const auto &slot = cn->c->base.slot;
-6
View File
@@ -211,12 +211,6 @@ void wivrn_session::operator()(from_headset::inputs && inputs)
right_hand->set_inputs(inputs, offset);
}
template <typename Rep, typename Period>
static auto lerp(std::chrono::duration<Rep, Period> a, std::chrono::duration<Rep, Period> b, double t)
{
return std::chrono::duration<Rep, Period>(Rep(std::lerp(a.count(), b.count(), t)));
}
void wivrn_session::operator()(from_headset::timesync_response && timesync)
{
offset_est.add_sample(timesync);