Control the tracking loop from server side

On server side, collect each tracking request, create an explicit pattern
describing how the client shall execute them.
This commit is contained in:
Patrick Nicolas
2026-01-29 09:51:52 +01:00
parent 0a71756024
commit fc01002317
28 changed files with 523 additions and 556 deletions
+4 -3
View File
@@ -98,6 +98,10 @@ if (WIVRN_BUILD_SERVER)
driver/app_pacer.cpp
driver/clock_offset.cpp
driver/configuration.cpp
driver/hand_joints_list.cpp
driver/pose_list.cpp
driver/tracking_control.cpp
driver/view_list.cpp
driver/wivrn_hmd.cpp
driver/wivrn_pacer.cpp
driver/wivrn_comp_target.cpp
@@ -109,9 +113,6 @@ if (WIVRN_BUILD_SERVER)
driver/wivrn_generic_tracker.cpp
driver/wivrn_foveation.cpp
driver/wivrn_uinput.cpp
driver/pose_list.cpp
driver/view_list.cpp
driver/hand_joints_list.cpp
driver/wivrn_session.cpp
driver/wivrn_connection.cpp
driver/xrt_cast.cpp
+2 -2
View File
@@ -354,10 +354,10 @@ void pipewire_device::mic_state_changed(void * self_v, pw_stream_state old, pw_s
case PW_STREAM_STATE_UNCONNECTED:
case PW_STREAM_STATE_CONNECTING:
case PW_STREAM_STATE_PAUSED:
self->session.set_enabled(to_headset::tracking_control::id::microphone, false);
self->session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, false});
return;
case PW_STREAM_STATE_STREAMING:
self->session.set_enabled(to_headset::tracking_control::id::microphone, true);
self->session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, true});
return;
}
}
+1 -1
View File
@@ -426,7 +426,7 @@ struct pulse_device : public audio_device
if (not mic_pipe)
throw std::system_error(errno, std::system_category(), "failed to open mic pipe " + microphone->socket.string());
mic_thread = std::thread([this]() { run_mic(); });
session.set_enabled(to_headset::tracking_control::id::microphone, true);
session.send_control(to_headset::feature_control{to_headset::feature_control::microphone, true});
}
if (info.speaker)
+4 -6
View File
@@ -79,7 +79,7 @@ protected:
}
public:
std::pair<std::chrono::nanoseconds, Data> get_at(XrTime at_timestamp_ns)
std::pair<XrTime, Data> get_at(XrTime at_timestamp_ns)
{
std::lock_guard lock(mutex);
@@ -110,24 +110,22 @@ public:
if (before)
produced = std::max(produced, before->produced_timestamp);
std::chrono::nanoseconds ex(std::max<XrDuration>(0, at_timestamp_ns - produced));
if (before and after)
{
float t = float(after->at_timestamp_ns - at_timestamp_ns) /
(after->at_timestamp_ns - before->at_timestamp_ns);
return {ex, Derived::interpolate(*before, *after, t)};
return {produced, Derived::interpolate(*before, *after, t)};
}
if (before)
{
if (at_timestamp_ns > before->at_timestamp_ns + U_TIME_1S_IN_NS)
return {};
return {ex, *before};
return {produced, *before};
}
if (after)
return {ex, *after};
return {produced, *after};
return {};
}
+105
View File
@@ -0,0 +1,105 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 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 "tracking_control.h"
#include "driver/wivrn_connection.h"
#include "wivrn_config.h"
#include <magic_enum_containers.hpp>
#include <ranges>
bool wivrn::tracking_control::advance(std::chrono::steady_clock::time_point now)
{
if (next > now)
return false;
next += step;
return true;
}
void wivrn::tracking_control::add_request(device_id device, XrTime now, XrTime at_ns, XrTime produced_ns)
{
std::lock_guard lock(mutex);
XrDuration prediction = at_ns - now;
reqs[device].min_prediction = std::min(reqs[device].min_prediction, prediction);
reqs[device].max_prediction = std::max(reqs[device].max_prediction, prediction);
if (produced_ns)
motions_to_photons = std::max(motions_to_photons, at_ns - produced_ns);
if (not std::ranges::contains(last_control.pattern, device, &to_headset::tracking_control::sample::device))
{
last_control.pattern.push_back({.device = device, .prediction_ns = prediction});
cnx.send_control(to_headset::tracking_control(last_control));
}
}
void wivrn::tracking_control::resolve(XrDuration frame_time, XrDuration latency)
{
decltype(this->reqs) reqs;
{
std::lock_guard lock(mutex);
std::swap(reqs, this->reqs);
}
wivrn::to_headset::tracking_control res{};
for (auto [device, req]: std::ranges::enumerate_view(reqs))
{
// Skip unset data
if (req.min_prediction > req.max_prediction)
continue;
XrDuration step = frame_time;
switch (device_id(device))
{
// High frequency polling for those
case device_id::HEAD:
case device_id::LEFT_GRIP:
case device_id::LEFT_AIM:
case device_id::LEFT_PALM:
case device_id::RIGHT_GRIP:
case device_id::RIGHT_AIM:
case device_id::RIGHT_PALM:
case device_id::LEFT_PINCH_POSE:
case device_id::RIGHT_PINCH_POSE:
case device_id::EYE_GAZE:
step = 3'000'000;
default:
break;
}
req.min_prediction = std::clamp(req.min_prediction + latency, 0l, max_extrapolation_ns);
req.max_prediction = std::clamp(req.max_prediction + latency, 0l, max_extrapolation_ns);
for (int64_t t = req.min_prediction; t < req.max_prediction + step; t += step)
{
res.pattern.push_back({
.device = device_id(device),
.prediction_ns = t,
});
}
}
{
std::lock_guard lock(mutex);
res.motions_to_photons = motions_to_photons;
last_control = res;
motions_to_photons = 0;
}
cnx.send_control(std::move(res));
}
+53
View File
@@ -0,0 +1,53 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 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 "wivrn_packets.h"
#include <magic_enum_containers.hpp>
#include <mutex>
namespace wivrn
{
class wivrn_connection;
class tracking_control
{
static constexpr std::chrono::milliseconds step{1000};
struct requests
{
XrDuration min_prediction = std::numeric_limits<XrDuration>::max();
XrDuration max_prediction = std::numeric_limits<XrDuration>::lowest();
};
std::mutex mutex;
magic_enum::containers::array<device_id, requests> reqs;
XrDuration motions_to_photons = 0;
to_headset::tracking_control last_control;
wivrn_connection & cnx;
public:
std::chrono::steady_clock::time_point next;
tracking_control(wivrn_connection & cnx) : cnx(cnx), next(std::chrono::steady_clock::now() + step) {}
bool advance(std::chrono::steady_clock::time_point now);
void add_request(device_id device, int64_t now, int64_t at_ns, int64_t produced_ns);
void resolve(XrDuration frame_time, XrDuration latency);
};
} // namespace wivrn
+4 -7
View File
@@ -31,9 +31,7 @@
#include "xrt/xrt_device.h"
#include "xrt/xrt_results.h"
#include <cmath>
#include <cstdint>
#include <cstring>
namespace wivrn
{
@@ -84,16 +82,15 @@ void wivrn_android_face_tracker::update_tracking(const from_headset::tracking &
.is_valid = face->is_valid,
};
if (not face_list.update_tracking(tracking.production_timestamp, tracking.timestamp, data, offset))
cnx.set_enabled(to_headset::tracking_control::id::face, false);
face_list.update_tracking(face->sample_time, tracking.timestamp, data, offset);
}
xrt_result_t wivrn_android_face_tracker::get_face_tracking(enum xrt_input_name facial_expression_type, int64_t at_timestamp_ns, struct xrt_facial_expression_set * inout_value)
{
if (facial_expression_type == XRT_INPUT_ANDROID_FACE_TRACKING)
{
cnx.set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(at_timestamp_ns);
auto [production_timestamp, data] = face_list.get_at(at_timestamp_ns);
cnx.add_tracking_request(device_id::FACE, at_timestamp_ns, production_timestamp);
inout_value->face_expression_set_android.state = data.state;
inout_value->face_expression_set_android.is_valid = data.is_valid;
@@ -118,4 +115,4 @@ xrt_result_t wivrn_android_face_tracker::get_face_calibration_state_android(bool
*out_face_is_calibrated = data.is_calibrated;
return XRT_SUCCESS;
}
} // namespace wivrn
} // namespace wivrn
+17 -27
View File
@@ -23,6 +23,7 @@
#include "driver/xrt_cast.h"
#include "math/m_api.h"
#include "utils/method.h"
#include "wivrn_config.h"
#include "wivrn_session.h"
#include "util/u_logging.h"
@@ -643,12 +644,10 @@ wivrn_controller::wivrn_controller(xrt_device_name name,
math_quat_from_euler_angles(&rotation_angles, &offset.orientation);
palm.set_derived(&grip, offset, true);
cnx->set_enabled(palm.device, false);
}
else if (not cnx->get_info().palm_pose)
{
palm.set_derived(&grip, XRT_POSE_IDENTITY, false);
cnx->set_enabled(palm.device, false);
}
if (name != XRT_DEVICE_EXT_HAND_INTERACTION)
@@ -795,31 +794,32 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_
{
XrTime production_timestamp;
device_id device;
XrTime now = os_monotonic_get_ns();
auto target_ns = std::min(at_timestamp_ns, now + max_extrapolation_ns);
switch (name)
{
case XRT_INPUT_TOUCH_AIM_POSE:
case XRT_INPUT_HAND_AIM_POSE:
std::tie(production_timestamp, *res, device) = aim.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res, device) = aim.get_pose_at(target_ns);
break;
case XRT_INPUT_TOUCH_GRIP_POSE:
case XRT_INPUT_HAND_GRIP_POSE:
std::tie(production_timestamp, *res, device) = grip.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res, device) = grip.get_pose_at(target_ns);
break;
case XRT_INPUT_GENERIC_PALM_POSE:
std::tie(production_timestamp, *res, device) = palm.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res, device) = palm.get_pose_at(target_ns);
break;
case XRT_INPUT_HAND_PINCH_POSE:
std::tie(production_timestamp, *res, device) = pinch_ext.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res, device) = pinch_ext.get_pose_at(target_ns);
break;
case XRT_INPUT_HAND_POKE_POSE:
std::tie(production_timestamp, *res, device) = poke_ext.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res, device) = poke_ext.get_pose_at(target_ns);
break;
default:
U_LOG_XDEV_UNSUPPORTED_INPUT(this, u_log_get_global_level(), name);
return XRT_ERROR_INPUT_UNSUPPORTED;
}
cnx->set_enabled(device, true);
cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp));
cnx->add_tracking_request(device, at_timestamp_ns, production_timestamp, now);
if (auto out = tracking_dump())
{
auto device = [&] {
@@ -852,10 +852,9 @@ xrt_result_t wivrn_controller::get_hand_tracking(xrt_input_name name, int64_t de
case XRT_INPUT_HT_UNOBSTRUCTED_LEFT:
case XRT_INPUT_HT_UNOBSTRUCTED_RIGHT: {
*out_timestamp_ns = desired_timestamp_ns;
std::chrono::nanoseconds extrapolation_time;
std::tie(extrapolation_time, *out_value) = joints.get_at(desired_timestamp_ns);
cnx->add_predict_offset(extrapolation_time);
cnx->set_enabled(joints.hand_id == 0 ? to_headset::tracking_control::id::left_hand : to_headset::tracking_control::id::right_hand, true);
XrTime production_timestamp;
std::tie(production_timestamp, *out_value) = joints.get_at(desired_timestamp_ns);
cnx->add_tracking_request(joints.hand_id == 0 ? device_id::LEFT_HAND : device_id::RIGHT_HAND, desired_timestamp_ns, production_timestamp);
return XRT_SUCCESS;
}
@@ -878,25 +877,16 @@ void wivrn_controller::set_derived_pose(const from_headset::derived_pose & deriv
auto source = list(derived.source);
auto target = list(derived.target);
if (source and target)
{
target->set_derived(source, xrt_cast(derived.relation));
if (source != target)
cnx->set_enabled(derived.target, false);
}
}
void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
{
if (not aim.update_tracking(tracking, offset))
cnx->set_enabled(aim.device, false);
if (not grip.update_tracking(tracking, offset))
cnx->set_enabled(grip.device, false);
if (not palm.update_tracking(tracking, offset))
cnx->set_enabled(palm.device, false);
if (not pinch_ext.update_tracking(tracking, offset))
cnx->set_enabled(pinch_ext.device, false);
if (not poke_ext.update_tracking(tracking, offset))
cnx->set_enabled(poke_ext.device, false);
aim.update_tracking(tracking, offset);
grip.update_tracking(tracking, offset);
palm.update_tracking(tracking, offset);
pinch_ext.update_tracking(tracking, offset);
poke_ext.update_tracking(tracking, offset);
if (auto out = tracking_dump(); out and offset)
{
auto locked = out->lock();
+6 -5
View File
@@ -21,21 +21,20 @@
#include "wivrn_eye_tracker.h"
#include "wivrn_packets.h"
#include "wivrn_session.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_device.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <openxr/openxr.h>
namespace wivrn
{
wivrn_eye_tracker::wivrn_eye_tracker(xrt_device * hmd) :
wivrn_eye_tracker::wivrn_eye_tracker(xrt_device * hmd, wivrn_session & cnx) :
xrt_device{
.name = XRT_DEVICE_EYE_GAZE_INTERACTION,
.device_type = XRT_DEVICE_TYPE_EYE_TRACKER,
@@ -55,7 +54,8 @@ wivrn_eye_tracker::wivrn_eye_tracker(xrt_device * hmd) :
.active = true,
.name = XRT_INPUT_GENERIC_EYE_GAZE_POSE,
},
gaze(device_id::EYE_GAZE)
gaze(device_id::EYE_GAZE),
cnx(cnx)
{
}
@@ -68,8 +68,9 @@ xrt_result_t wivrn_eye_tracker::get_tracked_pose(xrt_input_name name, int64_t at
{
if (name == XRT_INPUT_GENERIC_EYE_GAZE_POSE)
{
auto [_, relation] = gaze.get_at(at_timestamp_ns);
auto [production_timestamp, relation] = gaze.get_at(at_timestamp_ns);
*out_relation = relation;
cnx.add_tracking_request(device_id::EYE_GAZE, at_timestamp_ns, production_timestamp);
return XRT_SUCCESS;
}
+3 -1
View File
@@ -30,16 +30,18 @@
namespace wivrn
{
class wivrn_session;
class wivrn_eye_tracker : public xrt_device
{
std::mutex mutex;
xrt_input gaze_input;
pose_list gaze;
wivrn_session & cnx;
public:
using base = xrt_device;
wivrn_eye_tracker(xrt_device * hmd);
wivrn_eye_tracker(xrt_device * hmd, wivrn_session &);
xrt_result_t update_inputs();
void update_tracking(const from_headset::tracking &, const clock_offset &);
+3 -6
View File
@@ -29,9 +29,7 @@
#include "xrt/xrt_device.h"
#include "xrt/xrt_results.h"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <openxr/openxr.h>
namespace wivrn
@@ -81,16 +79,15 @@ void wivrn_fb_face2_tracker::update_tracking(const from_headset::tracking & trac
.time = offset.from_headset(face->time),
};
if (not face_list.update_tracking(tracking.production_timestamp, tracking.timestamp, data, offset))
cnx.set_enabled(to_headset::tracking_control::id::face, false);
face_list.update_tracking(face->timestamp, tracking.timestamp, data, offset);
}
xrt_result_t wivrn_fb_face2_tracker::get_face_tracking(enum xrt_input_name facial_expression_type, int64_t at_timestamp_ns, struct xrt_facial_expression_set * inout_value)
{
if (facial_expression_type == XRT_INPUT_FB_FACE_TRACKING2_VISUAL)
{
cnx.set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(at_timestamp_ns);
auto [production_timestamp, data] = face_list.get_at(at_timestamp_ns);
cnx.add_tracking_request(device_id::FACE, at_timestamp_ns, production_timestamp);
inout_value->face_expression_set2_fb.is_valid = data.is_valid;
inout_value->face_expression_set2_fb.sample_time_ns = data.time;
+6 -18
View File
@@ -30,7 +30,6 @@
#include "xrt/xrt_defines.h"
#include "xrt/xrt_results.h"
#include "xrt_cast.h"
#include <chrono>
#include <format>
using namespace xrt::auxiliary::math;
@@ -76,11 +75,6 @@ bool tracker_pose_list::update_tracking(XrTime produced_timestamp, XrTime timest
return add_sample(produced_timestamp, timestamp, convert_pose(pose), offset);
}
std::pair<std::chrono::nanoseconds, xrt_space_relation> tracker_pose_list::get_pose_at(XrTime at_timestamp_ns)
{
return get_at(at_timestamp_ns);
}
static xrt_space_relation_flags convert_flags(uint8_t flags)
{
std::underlying_type_t<xrt_space_relation_flags> out_flags = 0;
@@ -121,8 +115,7 @@ wivrn_generic_tracker::wivrn_generic_tracker(int index, xrt_device * hmd, wivrn_
.get_tracked_pose = method_pointer<&wivrn_generic_tracker::get_tracked_pose>,
.destroy = [](xrt_device *) {},
},
cnx(cnx),
index(index)
cnx(cnx)
{
auto unique_name = std::format("WiVRn Generic Tracker #{}", index + 1);
strlcpy(str, unique_name.c_str(), std::size(str));
@@ -140,17 +133,16 @@ xrt_result_t wivrn_generic_tracker::update_inputs()
{
return XRT_SUCCESS;
}
xrt_result_t wivrn_generic_tracker::get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * res)
{
std::chrono::nanoseconds extrapolation_time;
XrTime production_timestamp;
if (name == XRT_INPUT_GENERIC_TRACKER_POSE)
{
std::tie(extrapolation_time, *res) = poses.get_pose_at(at_timestamp_ns);
std::tie(production_timestamp, *res) = poses.get_at(at_timestamp_ns);
active = true;
cnx.update_tracker_enabled();
cnx.add_predict_offset(extrapolation_time);
cnx.add_tracking_request(device_id::BODY, at_timestamp_ns, production_timestamp);
return XRT_SUCCESS;
}
@@ -160,10 +152,6 @@ xrt_result_t wivrn_generic_tracker::get_tracked_pose(xrt_input_name name, int64_
void wivrn_generic_tracker::update_tracking(const from_headset::body_tracking & tracking, const from_headset::body_tracking::pose & pose, const clock_offset & offset)
{
if (!poses.update_tracking(tracking.production_timestamp, tracking.timestamp, pose, offset))
{
active = false;
cnx.update_tracker_enabled();
}
poses.update_tracking(tracking.production_timestamp, tracking.timestamp, pose, offset);
}
} // namespace wivrn
-9
View File
@@ -36,8 +36,6 @@ public:
bool update_tracking(XrTime produced_timestamp, XrTime timestamp, const from_headset::body_tracking::pose & pose, const clock_offset & offset);
std::pair<std::chrono::nanoseconds, xrt_space_relation> get_pose_at(XrTime at_timestamp_ns);
static xrt_space_relation convert_pose(const wivrn::from_headset::body_tracking::pose &);
};
@@ -45,10 +43,8 @@ class wivrn_generic_tracker : public xrt_device
{
tracker_pose_list poses;
xrt_input pose_input;
std::atomic_bool active = false;
wivrn_session & cnx;
int index;
public:
using base = xrt_device;
@@ -58,10 +54,5 @@ public:
xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * out_relation);
void update_tracking(const from_headset::body_tracking & tracking, const from_headset::body_tracking::pose & pose, const clock_offset & offset);
bool is_active()
{
return active;
}
};
} // namespace wivrn
+10 -7
View File
@@ -18,6 +18,8 @@
*/
#include "wivrn_hmd.h"
#include "os/os_time.h"
#include "wivrn_config.h"
#include "wivrn_session.h"
#include "xrt/xrt_defines.h"
@@ -129,9 +131,11 @@ xrt_result_t wivrn_hmd::get_tracked_pose(xrt_input_name name, int64_t at_timesta
return XRT_ERROR_INPUT_UNSUPPORTED;
}
auto [production_timestamp, view] = views.get_at(at_timestamp_ns);
auto now = os_monotonic_get_ns();
auto target_ns = std::min(now + max_extrapolation_ns, at_timestamp_ns);
auto [production_timestamp, view] = views.get_at(target_ns);
*res = view.relation;
cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp));
cnx->add_tracking_request(device_id::HEAD, at_timestamp_ns, production_timestamp, now);
return XRT_SUCCESS;
}
@@ -143,7 +147,6 @@ void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const c
void wivrn_hmd::update_battery(const from_headset::battery & new_battery)
{
// We will only request a new sample if the current one is consumed
cnx->set_enabled(to_headset::tracking_control::id::battery, false);
std::lock_guard lock(mutex);
battery = new_battery;
}
@@ -163,8 +166,10 @@ xrt_result_t wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
xrt_fov * out_fovs,
xrt_pose * out_poses)
{
auto [production_timestamp, view] = views.get_at(at_timestamp_ns);
cnx->add_predict_offset(std::chrono::nanoseconds(at_timestamp_ns - production_timestamp));
auto now = os_monotonic_get_ns();
auto target_ns = std::min(now + max_extrapolation_ns, at_timestamp_ns);
auto [production_timestamp, view] = views.get_at(target_ns);
cnx->add_tracking_request(device_id::HEAD, at_timestamp_ns, production_timestamp, now);
int flags = view.relation.relation_flags;
@@ -196,8 +201,6 @@ xrt_result_t wivrn_hmd::get_battery_status(bool * out_present,
bool * out_charging,
float * out_charge)
{
cnx->set_enabled(to_headset::tracking_control::id::battery, true);
std::lock_guard lock(mutex);
*out_present = battery.present;
*out_charging = battery.charging;
+5 -8
View File
@@ -29,9 +29,7 @@
#include "xrt/xrt_device.h"
#include "xrt/xrt_results.h"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <openxr/openxr.h>
namespace wivrn
@@ -89,16 +87,15 @@ void wivrn_htc_face_tracker::update_tracking(const from_headset::tracking & trac
.eye_active = face->eye_active,
.lip_active = face->lip_active};
if (not face_list.update_tracking(tracking.production_timestamp, tracking.timestamp, data, offset))
cnx.set_enabled(to_headset::tracking_control::id::face, false);
face_list.update_tracking(face->timestamp, tracking.timestamp, data, offset);
}
xrt_result_t wivrn_htc_face_tracker::get_face_tracking(enum xrt_input_name facial_expression_type, int64_t at_timestamp_ns, struct xrt_facial_expression_set * inout_value)
{
if (facial_expression_type == XRT_INPUT_HTC_EYE_FACE_TRACKING)
{
cnx.set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(at_timestamp_ns);
auto [production_timestamp, data] = face_list.get_at(at_timestamp_ns);
cnx.add_tracking_request(device_id::FACE, at_timestamp_ns, production_timestamp);
inout_value->base_expression_set_htc.is_active = data.eye_active;
inout_value->base_expression_set_htc.sample_time_ns = data.eye_sample_time;
@@ -112,8 +109,8 @@ xrt_result_t wivrn_htc_face_tracker::get_face_tracking(enum xrt_input_name facia
}
else if (facial_expression_type == XRT_INPUT_HTC_LIP_FACE_TRACKING)
{
cnx.set_enabled(to_headset::tracking_control::id::face, true);
auto [_, data] = face_list.get_at(at_timestamp_ns);
auto [production_timestamp, data] = face_list.get_at(at_timestamp_ns);
cnx.add_tracking_request(device_id::FACE, at_timestamp_ns, production_timestamp);
inout_value->base_expression_set_htc.is_active = data.lip_active;
inout_value->base_expression_set_htc.sample_time_ns = data.lip_sample_time;
+6
View File
@@ -76,6 +76,12 @@ wivrn_pacer::~wivrn_pacer()
compute_cv.notify_all();
}
uint64_t wivrn_pacer::get_frame_duration()
{
std::lock_guard lock(mutex);
return frame_duration_ns;
}
void wivrn_pacer::set_frame_duration(uint64_t frame_duration_ns)
{
std::lock_guard lock(mutex);
+1
View File
@@ -76,6 +76,7 @@ public:
wivrn_pacer(uint64_t frame_duration);
~wivrn_pacer();
uint64_t get_frame_duration();
void set_frame_duration(uint64_t frame_duration);
void predict(
+28 -97
View File
@@ -111,38 +111,6 @@ bool is_forced_extension(const char * ext_name)
return strstr(val, ext_name);
}
void wivrn::tracking_control_t::send(wivrn_connection & connection, bool now)
{
std::lock_guard lock(mutex);
if (std::chrono::steady_clock::now() < next_sample and not now)
return;
connection.send_stream(to_headset::tracking_control{
.min_offset = std::chrono::nanoseconds(min.exchange(80'000'000)),
.max_offset = std::chrono::nanoseconds(max.exchange(0)),
.enabled = enabled,
});
if (not now)
next_sample += std::chrono::seconds(1);
}
bool wivrn::tracking_control_t::get_enabled(to_headset::tracking_control::id id)
{
std::lock_guard lock(mutex);
return this->enabled[size_t(id)];
}
bool wivrn::tracking_control_t::set_enabled(to_headset::tracking_control::id id, bool enabled)
{
std::lock_guard lock(mutex);
bool changed = enabled != this->enabled[size_t(id)];
if (!changed)
return false;
U_LOG_I("%s tracking: %s", std::string(magic_enum::enum_name(id)).c_str(), enabled ? "enabled" : "disabled");
this->enabled[size_t(id)] = enabled;
return changed;
}
wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection, u_system & system) :
xrt_system_devices{
.get_roles = [](xrt_system_devices * self, xrt_system_roles * out_roles) { return ((wivrn_session *)self)->get_roles(out_roles); },
@@ -152,6 +120,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
},
connection(std::move(connection)),
xrt_system(system),
control(*this->connection),
hmd(this, get_info()),
left_controller(XRT_DEVICE_TOUCH_CONTROLLER, 0, &hmd, this),
right_controller(XRT_DEVICE_TOUCH_CONTROLLER, 1, &hmd, this),
@@ -247,7 +216,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
#endif
if (get_info().eye_gaze || is_forced_extension("EXT_eye_gaze_interaction"))
{
eye_tracker = std::make_unique<wivrn_eye_tracker>(&hmd);
eye_tracker = std::make_unique<wivrn_eye_tracker>(&hmd, *this);
static_roles.eyes = eye_tracker.get();
xdevs[xdev_count++] = eye_tracker.get();
}
@@ -328,7 +297,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
try
{
uinput_handler.emplace();
tracking_control.set_enabled(to_headset::tracking_control::id::hid_input, true);
send_control(to_headset::feature_control{to_headset::feature_control::hid_input, true});
}
catch (...)
{
@@ -447,6 +416,16 @@ void wivrn_session::unset_comp_target()
comp_target = nullptr;
}
void wivrn_session::add_tracking_request(device_id device, int64_t at_ns, int64_t produced_ns, int64_t now)
{
control.add_request(device, now, at_ns, produced_ns);
}
void wivrn_session::add_tracking_request(device_id device, int64_t at_ns, int64_t produced_ns)
{
control.add_request(device, os_monotonic_get_ns(), at_ns, produced_ns);
}
void wivrn_session::operator()(from_headset::headset_info_packet &&)
{
U_LOG_W("unexpected headset info packet, ignoring");
@@ -518,7 +497,8 @@ static xrt_device_name get_name(interaction_profile profile)
}
throw std::runtime_error("invalid interaction profile id " + std::to_string(int(profile)));
}
void wivrn_session::operator()(from_headset::trackings && tracking)
void wivrn_session::operator()(const from_headset::tracking & tracking)
{
auto left = (roles.left == -1 || roles.left == left_controller_index || roles.left == left_hand_interaction_index) ? get_name(tracking.interaction_profiles[0]) : XRT_DEVICE_INVALID;
auto right = (roles.right == -1 || roles.right == right_controller_index || roles.right == right_hand_interaction_index) ? get_name(tracking.interaction_profiles[1]) : XRT_DEVICE_INVALID;
@@ -545,8 +525,6 @@ void wivrn_session::operator()(from_headset::trackings && tracking)
{
left_controller.reset_history();
roles.left = left_controller_index;
set_enabled(device_id::LEFT_PINCH_POSE, false);
set_enabled(device_id::LEFT_POKE, false);
}
else
{
@@ -566,8 +544,6 @@ void wivrn_session::operator()(from_headset::trackings && tracking)
{
right_controller.reset_history();
roles.right = right_controller_index;
set_enabled(device_id::RIGHT_PINCH_POSE, false);
set_enabled(device_id::RIGHT_POKE, false);
}
else
{
@@ -578,12 +554,6 @@ void wivrn_session::operator()(from_headset::trackings && tracking)
++roles.generation_id;
}
for (auto & item: tracking.items)
(*this)(item);
}
void wivrn_session::operator()(const from_headset::tracking & tracking)
{
if (tracking.state_flags & from_headset::tracking::state_flags::recentered)
{
U_LOG_I("recentering requested");
@@ -593,6 +563,12 @@ void wivrn_session::operator()(const from_headset::tracking & tracking)
auto offset = offset_est.get_offset();
if (offset)
{
XrDuration latency = os_monotonic_get_ns() - offset.from_headset(tracking.production_timestamp);
tracking_latency = std::lerp(tracking_latency.load(), latency, 0.1);
}
hmd.update_tracking(tracking, offset);
if (roles.left == left_hand_interaction_index)
left_hand_interaction.update_tracking(tracking, offset);
@@ -692,54 +668,6 @@ void wivrn_session::operator()(from_headset::timesync_response && timesync)
offset_est.add_sample(timesync);
}
static auto to_tracking_control(device_id id)
{
using tid = to_headset::tracking_control::id;
switch (id)
{
case device_id::LEFT_AIM:
return tid::left_aim;
case device_id::LEFT_GRIP:
return tid::left_grip;
case device_id::LEFT_PALM:
return tid::left_palm;
case device_id::LEFT_PINCH_POSE:
return tid::left_pinch;
case device_id::LEFT_POKE:
return tid::left_poke;
case device_id::RIGHT_AIM:
return tid::right_aim;
case device_id::RIGHT_GRIP:
return tid::right_grip;
case device_id::RIGHT_PALM:
return tid::right_palm;
case device_id::RIGHT_PINCH_POSE:
return tid::right_pinch;
case device_id::RIGHT_POKE:
return tid::right_poke;
default:
break;
}
__builtin_unreachable();
}
void wivrn_session::set_enabled(to_headset::tracking_control::id id, bool enabled)
{
tracking_control.set_enabled(id, enabled);
}
void wivrn_session::set_enabled(device_id id, bool enabled)
{
if (tracking_control.set_enabled(to_tracking_control(id), enabled) and enabled)
tracking_control.send(*connection, true);
}
void wivrn_session::update_tracker_enabled()
{
bool active = std::ranges::any_of(generic_trackers, [](auto & t) { return t->is_active(); });
if (tracking_control.set_enabled(to_headset::tracking_control::id::generic_tracker, active) and active)
tracking_control.send(*connection, true);
}
void wivrn_session::operator()(from_headset::feedback && feedback)
{
clock_offset o = offset_est.get_offset();
@@ -1068,19 +996,22 @@ void wivrn_session::run_worker(std::stop_token stop)
std::this_thread::sleep_until(std::min(
{
refresh.next,
tracking_control.next(),
control.next,
offset_est.next(),
}));
auto now = std::chrono::steady_clock::now();
offset_est.request_sample(now, *connection);
tracking_control.send(*connection);
if (refresh.advance(now))
const bool do_refresh = refresh.advance(now);
const bool do_control = control.advance(now);
if (do_refresh or do_control)
{
std::shared_lock lock(comp_target_mutex);
if (comp_target)
{
if (comp_target->requested_refresh_rate == 0)
if (do_refresh and comp_target->requested_refresh_rate == 0)
refresh.adjust(*connection);
if (do_control)
control.resolve(comp_target->pacer.get_frame_duration(), tracking_latency);
}
}
poll_session_loss();
+5 -52
View File
@@ -21,6 +21,7 @@
#include "clock_offset.h"
#include "driver/app_pacer.h"
#include "tracking_control.h"
#include "utils/thread_safe.h"
#include "wivrn_connection.h"
#include "wivrn_controller.h"
@@ -30,7 +31,6 @@
#include "wivrn_uinput.h"
#include "xrt/xrt_results.h"
#include "xrt/xrt_system.h"
#include <atomic>
#include <fstream>
#include <map>
#include <memory>
@@ -55,47 +55,6 @@ struct audio_device;
struct wivrn_comp_target;
struct wivrn_comp_target_factory;
class tracking_control_t
{
using T = std::chrono::nanoseconds::rep;
std::atomic<T> min;
std::atomic<T> max;
std::chrono::steady_clock::time_point next_sample;
std::mutex mutex;
decltype(to_headset::tracking_control::enabled) enabled;
public:
tracking_control_t() :
next_sample(std::chrono::steady_clock::now())
{
enabled.fill(true);
}
void add(std::chrono::nanoseconds s)
{
auto sample = s.count();
T prev = max;
while (prev < sample and max.compare_exchange_weak(prev, sample))
{
}
if (sample > 0)
{
prev = min;
while (prev > sample and min.compare_exchange_weak(prev, sample))
{
}
}
}
void send(wivrn_connection & connection, bool now = false);
bool get_enabled(to_headset::tracking_control::id id);
// Return true if value changed
bool set_enabled(to_headset::tracking_control::id id, bool enabled);
std::chrono::steady_clock::time_point next() const
{
return next_sample;
}
};
class wivrn_session : public xrt_system_devices
{
friend wivrn_comp_target_factory;
@@ -115,7 +74,7 @@ class wivrn_session : public xrt_system_devices
.gamepad = -1,
};
tracking_control_t tracking_control;
tracking_control control;
wivrn_hmd hmd;
wivrn_controller left_controller;
@@ -137,6 +96,7 @@ class wivrn_session : public xrt_system_devices
wivrn_comp_target * comp_target;
clock_offset_estimator offset_est;
std::atomic<XrDuration> tracking_latency; // production to reception time
std::mutex csv_mutex;
std::ofstream feedback_csv;
@@ -188,14 +148,8 @@ public:
return hmd;
}
void add_predict_offset(std::chrono::nanoseconds off)
{
tracking_control.add(off);
}
void set_enabled(to_headset::tracking_control::id id, bool enabled);
void set_enabled(device_id id, bool enabled);
void update_tracker_enabled();
void add_tracking_request(device_id, int64_t at_ns, int64_t produced_ns, int64_t now);
void add_tracking_request(device_id, int64_t at_ns, int64_t produced_ns);
void operator()(from_headset::crypto_handshake &&) {}
void operator()(from_headset::pin_check_1 &&) {}
@@ -203,7 +157,6 @@ public:
void operator()(from_headset::headset_info_packet &&);
void operator()(const from_headset::settings_changed &);
void operator()(from_headset::handshake &&) {}
void operator()(from_headset::trackings &&);
void operator()(const from_headset::tracking &);
void operator()(from_headset::derived_pose &&);
void operator()(from_headset::hand_tracking &&);