diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index de6c989c..cb2498c1 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -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 diff --git a/server/driver/audio_pulse.cpp b/server/driver/audio_pulse.cpp index e01a7f42..71ca2a9c 100644 --- a/server/driver/audio_pulse.cpp +++ b/server/driver/audio_pulse.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" diff --git a/server/driver/clock_offset.h b/server/driver/clock_offset.h index 3dc5a006..d031463d 100644 --- a/server/driver/clock_offset.h +++ b/server/driver/clock_offset.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 diff --git a/server/driver/ffmpeg/VideoEncoderFFMPEG.h b/server/driver/ffmpeg/VideoEncoderFFMPEG.h index 4ef4700d..dc4213cd 100644 --- a/server/driver/ffmpeg/VideoEncoderFFMPEG.h +++ b/server/driver/ffmpeg/VideoEncoderFFMPEG.h @@ -24,7 +24,7 @@ #include #include -class VideoEncoderFFMPEG : public VideoEncoder +class VideoEncoderFFMPEG : public xrt::drivers::wivrn::VideoEncoder { public: using Codec = xrt::drivers::wivrn::video_codec; diff --git a/server/driver/ffmpeg/VideoEncoderVA.cpp b/server/driver/ffmpeg/VideoEncoderVA.cpp index c4fd4aae..f5c556cf 100644 --- a/server/driver/ffmpeg/VideoEncoderVA.cpp +++ b/server/driver/ffmpeg/VideoEncoderVA.cpp @@ -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; diff --git a/server/driver/ffmpeg/VideoEncoderVA.h b/server/driver/ffmpeg/VideoEncoderVA.h index 674c855c..8afff7e5 100644 --- a/server/driver/ffmpeg/VideoEncoderVA.h +++ b/server/driver/ffmpeg/VideoEncoderVA.h @@ -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, diff --git a/server/driver/offset_estimator.cpp b/server/driver/offset_estimator.cpp new file mode 100644 index 00000000..bf7e6c47 --- /dev/null +++ b/server/driver/offset_estimator.cpp @@ -0,0 +1,80 @@ +/* + * 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 new file mode 100644 index 00000000..7a74c46c --- /dev/null +++ b/server/driver/offset_estimator.h @@ -0,0 +1,46 @@ +/* + * 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/video_encoder.cpp b/server/driver/video_encoder.cpp index 4797f686..a4bf220d 100644 --- a/server/driver/video_encoder.cpp +++ b/server/driver/video_encoder.cpp @@ -17,6 +17,9 @@ * along with this program. If not, see . */ +// 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" diff --git a/server/driver/video_encoder.h b/server/driver/video_encoder.h index 4cf4468a..a4528cf5 100644 --- a/server/driver/video_encoder.h +++ b/server/driver/video_encoder.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"; diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index e56caecb..b331fe93 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -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 #include #include #include diff --git a/server/driver/wivrn_comp_target.h b/server/driver/wivrn_comp_target.h index 620f4ce5..598fcb62 100644 --- a/server/driver/wivrn_comp_target.h +++ b/server/driver/wivrn_comp_target.h @@ -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 cnx, struct comp_compositor *c, float fps); diff --git a/server/driver/wivrn_hmd.h b/server/driver/wivrn_hmd.h index abc78e74..8f4c2f36 100644 --- a/server/driver/wivrn_hmd.h +++ b/server/driver/wivrn_hmd.h @@ -29,6 +29,8 @@ #include #include +struct comp_target; + class wivrn_hmd : public xrt_device { std::mutex mutex; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index f146ae20..9315a3ac 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -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 a, std::chrono::duration std::unique_ptr right_hand; clock_offset offset; + offset_estimator offset_est; std::chrono::steady_clock::time_point offset_expiration{}; std::mutex csv_mutex; diff --git a/server/target_instance_wivrn.cpp b/server/target_instance_wivrn.cpp index 49b9ba96..8d5cf9b2 100644 --- a/server/target_instance_wivrn.cpp +++ b/server/target_instance_wivrn.cpp @@ -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; 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);