diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 0ef914d0..eba7e05c 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -69,7 +69,7 @@ add_executable(wivrn-server encoder/encoder_settings.cpp encoder/video_encoder.cpp encoder/yuv_converter.cpp - driver/offset_estimator.cpp + driver/clock_offset.cpp driver/wivrn_hmd.cpp driver/wivrn_comp_target.cpp driver/wivrn_controller.cpp diff --git a/server/driver/clock_offset.cpp b/server/driver/clock_offset.cpp new file mode 100644 index 00000000..2ae18c11 --- /dev/null +++ b/server/driver/clock_offset.cpp @@ -0,0 +1,146 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "clock_offset.h" + +#include "driver/wivrn_connection.h" +#include "os/os_time.h" +#include "util/u_logging.h" + +static const size_t num_samples = 100; + +void clock_offset_estimator::request_sample(wivrn_connection & connection) +{ + if (std::chrono::steady_clock::now() < next_sample) + return; + + next_sample = std::chrono::steady_clock::now() + std::chrono::seconds(1); + xrt::drivers::wivrn::to_headset::timesync_query timesync{}; + timesync.query = std::chrono::nanoseconds(os_monotonic_get_ns()); + connection.send_stream(timesync); +} + +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()); + clock_offset_estimator::sample sample{base_sample, now}; + std::lock_guard lock(mutex); + if (samples.size() < num_samples) + { + samples.push_back(sample); + } + else + { + int64_t latency = 0; + for (const auto& s: samples) + latency += (s.received - s.query).count(); + latency /= samples.size(); + // packets with too high latency are likely to be retransmitted + if ((sample.received - sample.query).count() > 3 * latency) + { + U_LOG_D("drop packet for latency %ld > %ld", (sample.received - sample.query).count() /1000, latency/1000); + return; + } + + samples[sample_index] = sample; + sample_index = (sample_index + 1) % num_samples; + } + + // Linear regression + // X = time on server + // Y = time on headset + // in order to maintain accuracy, use x = X-x0, y = Y-y0 + // where x0 and y0 are means of X an Y + size_t n = samples.size(); + double inv_n = 1. / n; + double x0 = 0; + double y0 = 0; + for (const auto & s: samples) + { + x0 += (s.query + s.received).count() * 0.5; + y0 += s.response; + } + x0 /= n; + y0 /= n; + + if (samples.size() < num_samples) + { + offset.a = 1; + offset.b = y0 - x0; + return; + } + + double sum_x = 0; + double sum_y = 0; + double sum_x2 = 0; + double sum_xy = 0; + for (const auto & s: samples) + { +#if 1 + // assume symmetrical latency + double x = (s.query + s.received).count() * 0.5 - x0; +#else + // assume latency is only on server -> headset link + double x = (s.received).count() - x0; +#endif + double y = s.response - y0; + sum_x += x; + sum_y += y; + sum_x2 += x * x; + sum_xy += x * y; + } + + double mean_x = sum_x * inv_n; + double mean_y = sum_y * inv_n; + // y = ax + b + double cov = inv_n * sum_xy - mean_x * mean_y; + double v = inv_n * sum_x2 - mean_x * mean_x; + double a = cov / v; + double b = mean_y - a * mean_x; + + offset.a = a; + offset.b = y0 + b - int64_t(a * x0); + U_LOG_D("clock relations: headset = a*x+b where a=%f b=%ldµs", offset.a, offset.b / 1000); +} + +clock_offset clock_offset_estimator::get_offset() +{ + std::lock_guard lock(mutex); + return offset; +} + +int64_t clock_offset::from_headset(uint64_t 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; +} + +std::chrono::nanoseconds clock_offset::to_headset(uint64_t 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); +} diff --git a/server/driver/clock_offset.h b/server/driver/clock_offset.h index d031463d..475b62dc 100644 --- a/server/driver/clock_offset.h +++ b/server/driver/clock_offset.h @@ -1,7 +1,7 @@ /* * WiVRn VR streaming - * Copyright (C) 2022 Guillaume Meunier - * Copyright (C) 2022 Patrick Nicolas + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,20 +19,47 @@ #pragma once +#include "wivrn_packets.h" + #include #include +#include -namespace xrt::drivers::wivrn -{ +class wivrn_connection; struct clock_offset { - std::chrono::nanoseconds epoch_offset{}; + // y: headset time + // x: server time + // y = ax+b + int64_t b = 0; + double a = 1; - uint64_t from_headset(uint64_t) const; + operator bool() { return b != 0;} + + int64_t from_headset(uint64_t) const; std::chrono::nanoseconds to_headset(uint64_t timestamp_ns) const; }; -} // namespace xrt::drivers::wivrn +class clock_offset_estimator +{ + struct sample: public xrt::drivers::wivrn::from_headset::timesync_response + { + std::chrono::nanoseconds received; + }; + + std::mutex mutex; + std::vector samples; + size_t sample_index = 0; + clock_offset offset; + + std::chrono::steady_clock::time_point next_sample{}; + + public: + void request_sample(wivrn_connection& connection); + void add_sample(const xrt::drivers::wivrn::from_headset::timesync_response& sample); + + clock_offset get_offset(); +}; diff --git a/server/driver/offset_estimator.cpp b/server/driver/offset_estimator.cpp deleted file mode 100644 index bf7e6c47..00000000 --- a/server/driver/offset_estimator.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2023 Guillaume Meunier - * Copyright (C) 2023 Patrick Nicolas - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "offset_estimator.h" - -#include "clock_offset.h" -#include "util/u_logging.h" -#include "wivrn_packets.h" - -static const double lowpass = 0.8; -static const double max_rtt_ratio = 3; - -namespace xrt::drivers::wivrn -{ - -// We need to estimate the time offset with the headset, -// We first send a packet with current time t0, headset fills its current time t1 when it processes it then sends it back. We receive it back ad t2 -// Normal estimate is that PC time is (t1+t2)/2 when headset processes it, however due to asymmetrical network load this is not what is observed. - -// We try to estimate the reception time as t = t2 + (t1 - t2) * x -// U (input vector) is t0, t1, t2 -// filtered_U is lowpass filtered -// The value we want to minimize is the variation of offset - -clock_offset offset_estimator::get_offset(const from_headset::timesync_response & packet, int64_t now, const clock_offset old_offset) -{ - int64_t rtt = now - packet.query.count(); - Eigen::Vector3d U(packet.query.count(), packet.response, now); - - // initial assumption: latency is symmetrical - if (filtered_U == Eigen::Vector3d::Zero()) - { - filtered_U = U; - int64_t offset = filtered_U[1] - 0.5 * (filtered_U[0] + filtered_U[2]); - return clock_offset{std::chrono::nanoseconds(offset)}; - } - - auto mean_rtt = filtered_U[2] - filtered_U[0]; - filtered_U = filtered_U + lowpass * (U - filtered_U); - // Packet took too long, probably a retransmit so skip it - // as we don't know on which way it was retransmitted - if (rtt > max_rtt_ratio * mean_rtt) - { - U_LOG_D("skip packet with RTT %ldms", rtt / 1000000); - return old_offset; - } - - auto tmp = U - filtered_U; - A = A * 0.99 + tmp * tmp.transpose(); - - auto x = (A(0,1) - A(0,2) - A(1,2) + A(2,2)) / (A(0,0) - 2*A(0,2) + A(2,2)); - x = std::clamp(x, 0., 1.); - - int64_t t = std::lerp(now, packet.query.count(), x); - int64_t offset = packet.response - t; - U_LOG_D("offset estimator x=%f offset diff %ldµs", x, (old_offset.epoch_offset.count() - offset)/1000); - if (old_offset.epoch_offset.count() != 0) - { - offset = std::lerp(offset, old_offset.epoch_offset.count(), lowpass); - } - return clock_offset{std::chrono::nanoseconds(offset)}; -} - -} diff --git a/server/driver/offset_estimator.h b/server/driver/offset_estimator.h deleted file mode 100644 index 7a74c46c..00000000 --- a/server/driver/offset_estimator.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2023 Guillaume Meunier - * Copyright (C) 2023 Patrick Nicolas - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include - -#include - -namespace xrt::drivers::wivrn -{ - -struct clock_offset; - -namespace from_headset -{ -struct timesync_response; -} - -class offset_estimator -{ - Eigen::Vector3d filtered_U = Eigen::Vector3d::Zero(); - Eigen::Matrix3d A = Eigen::Matrix3d::Zero(); - -public: - clock_offset get_offset(const from_headset::timesync_response & packet, int64_t now, clock_offset old_offset); -}; - -} // namespace xrt::drivers::wivrn - diff --git a/server/driver/pose_list.h b/server/driver/pose_list.h index 300cc11e..5a556749 100644 --- a/server/driver/pose_list.h +++ b/server/driver/pose_list.h @@ -40,7 +40,7 @@ class history std::vector data; protected: - void add_sample(uint64_t timestamp, const Data & sample, const xrt::drivers::wivrn::clock_offset & offset) + void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset) { std::lock_guard lock(mutex); @@ -107,7 +107,7 @@ public: pose_list(xrt::drivers::wivrn::device_id id) : device(id) {} - void update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const xrt::drivers::wivrn::clock_offset & offset); + void update_tracking(const xrt::drivers::wivrn::from_headset::tracking &, const clock_offset & offset); static xrt_space_relation convert_pose(const xrt::drivers::wivrn::from_headset::tracking::pose &); }; diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 47197b29..4d1954a4 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -529,7 +529,7 @@ static VkResult comp_wivrn_present(struct comp_target * ct, assert(index < cn->image_count); - if (cn->c->base.slot.layer_count == 0) + if (cn->c->base.slot.layer_count == 0 or not cn->cnx->get_offset()) { // TODO: Tell the headset that there is no image to display assert(cn->psc.images[index].status == image_acquired); diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index b2239535..1639ef9b 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -28,6 +28,7 @@ #include "xrt_cast.h" #include +#include #include #include #include diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index a925813a..789cdaae 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -20,7 +20,6 @@ #include "wivrn_session.h" #include "audio_setup.h" #include "main/comp_target.h" -#include "os/os_time.h" #include "util/u_builders.h" #include "util/u_logging.h" @@ -162,8 +161,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi clock_offset wivrn_session::get_offset() { - std::lock_guard lock(mutex); - return offset; + return offset_est.get_offset(); } void wivrn_session::operator()(from_headset::headset_info_packet &&) @@ -172,7 +170,8 @@ void wivrn_session::operator()(from_headset::headset_info_packet &&) } void wivrn_session::operator()(from_headset::tracking && tracking) { - if (offset.epoch_offset.count() == 0) + auto offset = offset_est.get_offset(); + if (not offset) return; hmd->update_tracking(tracking, offset); @@ -183,6 +182,8 @@ void wivrn_session::operator()(from_headset::tracking && tracking) void wivrn_session::operator()(from_headset::inputs && inputs) { auto offset = get_offset(); + if (not offset) + return; left_hand->set_inputs(inputs, offset); right_hand->set_inputs(inputs, offset); } @@ -195,19 +196,15 @@ static auto lerp(std::chrono::duration a, std::chrono::durationon_feedback(feedback, o); if (feedback.received_first_packet) @@ -230,16 +227,6 @@ void wivrn_session::operator()(audio_data && data) audio_handle->process_mic_data(std::move(data)); } -uint64_t clock_offset::from_headset(uint64_t ts) const -{ - return ts - epoch_offset.count(); -} - -std::chrono::nanoseconds clock_offset::to_headset(uint64_t timestamp_ns) const -{ - return std::chrono::nanoseconds(timestamp_ns) + epoch_offset; -} - void wivrn_session::run(std::weak_ptr weak_self) { while (true) @@ -249,13 +236,7 @@ void wivrn_session::run(std::weak_ptr weak_self) auto self = weak_self.lock(); if (self and not self->quit) { - if (std::chrono::steady_clock::now() > self->offset_expiration) - { - self->offset_expiration = std::chrono::steady_clock::now() + std::chrono::seconds(1); - to_headset::timesync_query timesync{}; - timesync.query = std::chrono::nanoseconds(os_monotonic_get_ns()); - self->connection.send_stream(timesync); - } + self->offset_est.request_sample(self->connection); self->connection.poll(*self, 20); } else diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 0568c1d0..53f3e5b2 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -20,7 +20,6 @@ #pragma once #include "clock_offset.h" -#include "offset_estimator.h" #include "wivrn_connection.h" #include "wivrn_packets.h" #include "xrt/xrt_system.h" @@ -51,16 +50,13 @@ class wivrn_session : public std::enable_shared_from_this std::atomic quit = false; std::thread thread; - std::mutex mutex; std::unique_ptr hmd; std::unique_ptr left_hand; std::unique_ptr right_hand; wivrn_comp_target * comp_target; - clock_offset offset; - offset_estimator offset_est; - std::chrono::steady_clock::time_point offset_expiration{}; + clock_offset_estimator offset_est; std::mutex csv_mutex; std::ofstream feedback_csv; @@ -76,8 +72,7 @@ public: xrt_space_overseer ** out_xspovrs, xrt_system_compositor ** out_xsysc); - clock_offset - get_offset(); + clock_offset get_offset(); void operator()(from_headset::handshake&&) {} void operator()(from_headset::headset_info_packet &&);