Improve timesync
Initial timesync algorithm assumed that latency was symmetrical, but it appears that it is not. Add a mechanism to estimate where latency happens, which leads to much more stable values: about 200µs variation between estimations.
This commit is contained in:
@@ -64,6 +64,7 @@ add_executable(wivrn-server
|
||||
driver/audio_setup.cpp
|
||||
driver/configuration.cpp
|
||||
driver/encoder_settings.cpp
|
||||
driver/offset_estimator.cpp
|
||||
driver/wivrn_hmd.cpp
|
||||
driver/wivrn_comp_target.cpp
|
||||
driver/wivrn_controller.cpp
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "audio_pulse.h"
|
||||
|
||||
#include "../wivrn_ipc.h"
|
||||
#include "os/os_time.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/sync_queue.h"
|
||||
#include "wivrn_session.h"
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
struct clock_offset
|
||||
{
|
||||
std::chrono::nanoseconds epoch_offset{};
|
||||
@@ -34,4 +35,4 @@ struct clock_offset
|
||||
to_headset(uint64_t timestamp_ns) const;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
|
||||
class VideoEncoderFFMPEG : public VideoEncoder
|
||||
class VideoEncoderFFMPEG : public xrt::drivers::wivrn::VideoEncoder
|
||||
{
|
||||
public:
|
||||
using Codec = xrt::drivers::wivrn::video_codec;
|
||||
|
||||
@@ -106,7 +106,7 @@ get_render_device(vk_bundle * vk)
|
||||
|
||||
} // namespace
|
||||
|
||||
VideoEncoderVA::VideoEncoderVA(vk_bundle * vk, const encoder_settings & settings, float fps) :
|
||||
VideoEncoderVA::VideoEncoderVA(vk_bundle * vk, const xrt::drivers::wivrn::encoder_settings & settings, float fps) :
|
||||
vk(vk), width(settings.width), height(settings.height), offset_x(settings.offset_x), offset_y(settings.offset_y)
|
||||
{
|
||||
codec = settings.codec;
|
||||
|
||||
@@ -36,7 +36,7 @@ struct AVFilterContext;
|
||||
class VideoEncoderVA : public VideoEncoderFFMPEG
|
||||
{
|
||||
public:
|
||||
VideoEncoderVA(vk_bundle * vk, const encoder_settings & settings, float fps);
|
||||
VideoEncoderVA(vk_bundle * vk, const xrt::drivers::wivrn::encoder_settings & settings, float fps);
|
||||
|
||||
void
|
||||
SetImages(int width,
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2023 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2023 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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)};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2023 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2023 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <Eigen/Core>
|
||||
|
||||
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
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Include first because of incompatibility between Eigen and X includes
|
||||
#include "wivrn_session.h"
|
||||
|
||||
#include "video_encoder.h"
|
||||
#include "rs.h"
|
||||
#include "util/u_logging.h"
|
||||
|
||||
@@ -29,11 +29,12 @@
|
||||
|
||||
#include "encoder_settings.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "wivrn_session.h"
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
class wivrn_session;
|
||||
|
||||
inline const char * encoder_nvenc = "nvenc";
|
||||
inline const char * encoder_vaapi = "vaapi";
|
||||
inline const char * encoder_x264 = "x264";
|
||||
|
||||
@@ -22,11 +22,8 @@
|
||||
#include "math/m_space.h"
|
||||
#include "util/u_pacing.h"
|
||||
#include "video_encoder.h"
|
||||
#include "vk/vk_cmd.h"
|
||||
#include "vk/vk_cmd_pool.h"
|
||||
#include "xrt/xrt_config_have.h"
|
||||
#include "xrt_cast.h"
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "main/comp_target.h"
|
||||
#include "wivrn_session.h"
|
||||
|
||||
#include "main/comp_target.h"
|
||||
|
||||
comp_target * comp_target_wivrn_create(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx, struct comp_compositor *c, float fps);
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
struct comp_target;
|
||||
|
||||
class wivrn_hmd : public xrt_device
|
||||
{
|
||||
std::mutex mutex;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "wivrn_session.h"
|
||||
#include "audio_setup.h"
|
||||
#include "main/comp_target.h"
|
||||
#include "os/os_time.h"
|
||||
#include "util/u_logging.h"
|
||||
|
||||
@@ -173,13 +174,9 @@ static auto lerp(std::chrono::duration<Rep, Period> a, std::chrono::duration<Rep
|
||||
|
||||
void wivrn_session::operator()(from_headset::timesync_response && timesync)
|
||||
{
|
||||
auto now = std::chrono::nanoseconds(os_monotonic_get_ns());
|
||||
auto new_offset = std::chrono::nanoseconds(timesync.response) - lerp(timesync.query, now, 0.5);
|
||||
auto now = os_monotonic_get_ns();
|
||||
std::lock_guard lock(mutex);
|
||||
if (not offset.epoch_offset.count())
|
||||
offset.epoch_offset = new_offset;
|
||||
else
|
||||
offset.epoch_offset = lerp(offset.epoch_offset, new_offset, 0.2);
|
||||
offset = offset_est.get_offset(timesync, now, offset);
|
||||
}
|
||||
|
||||
void wivrn_session::operator()(from_headset::feedback && feedback)
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "clock_offset.h"
|
||||
#include "main/comp_target.h"
|
||||
#include "offset_estimator.h"
|
||||
#include "wivrn_connection.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include "xrt/xrt_system.h"
|
||||
@@ -33,6 +33,7 @@
|
||||
class wivrn_hmd;
|
||||
class wivrn_controller;
|
||||
struct audio_device;
|
||||
struct comp_target_factory;
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
@@ -55,6 +56,7 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
|
||||
std::unique_ptr<wivrn_controller> right_hand;
|
||||
|
||||
clock_offset offset;
|
||||
offset_estimator offset_est;
|
||||
std::chrono::steady_clock::time_point offset_expiration{};
|
||||
|
||||
std::mutex csv_mutex;
|
||||
|
||||
@@ -7,13 +7,13 @@
|
||||
*/
|
||||
|
||||
#include "xrt/xrt_config_build.h"
|
||||
#include "xrt/xrt_instance.h"
|
||||
#include "xrt/xrt_system.h"
|
||||
#include "main/comp_main_interface.h"
|
||||
|
||||
#include "os/os_time.h"
|
||||
|
||||
#include "util/u_debug.h"
|
||||
#include "util/u_system_helpers.h"
|
||||
#include "util/u_misc.h"
|
||||
#include "util/u_trace_marker.h"
|
||||
#include "util/u_builders.h"
|
||||
|
||||
@@ -32,7 +32,7 @@ extern std::unique_ptr<TCP> tcp;
|
||||
static xrt_result_t
|
||||
wivrn_instance_create_system(struct xrt_instance * xinst,
|
||||
struct xrt_system_devices ** out_xsysd,
|
||||
struct xrt_space_overseer ** out_xspovrs,
|
||||
struct xrt_space_overseer ** out_xspovrs,
|
||||
struct xrt_system_compositor ** out_xsysc)
|
||||
{
|
||||
assert(out_xsysd != NULL);
|
||||
|
||||
Reference in New Issue
Block a user