Request tracking extrapolation on client
Add the timestamp where data was requested, use this to compute how far in the future the application is requesting samples. With this information, client requests 2 tracking samples: one at the current time and one in the future. Server now keeps track of actual samples + one predicted sample, and is most of the time in the interpolation use case.
This commit is contained in:
@@ -75,6 +75,7 @@ private:
|
||||
std::unique_ptr<wivrn_session> network_session;
|
||||
std::atomic<bool> exiting = false;
|
||||
std::thread network_thread;
|
||||
std::atomic<std::chrono::nanoseconds::rep> tracking_prediction_offset;
|
||||
std::optional<std::thread> tracking_thread;
|
||||
std::thread video_thread;
|
||||
|
||||
@@ -128,6 +129,7 @@ public:
|
||||
void operator()(to_headset::video_stream_data_shard &&);
|
||||
void operator()(to_headset::haptics &&);
|
||||
void operator()(to_headset::timesync_query &&);
|
||||
void operator()(to_headset::prediction_offset &&);
|
||||
void operator()(to_headset::audio_stream_description &&);
|
||||
void operator()(to_headset::video_stream_description &&);
|
||||
void operator()(audio_data &&);
|
||||
|
||||
@@ -118,10 +118,8 @@ void scenes::stream::tracking()
|
||||
XrSpace view_space = application::view();
|
||||
const XrDuration tracking_period = 1'000'000; // Send tracking data every 1ms
|
||||
const XrDuration dt = 100'000; // Wake up 0.1ms before measuring the position
|
||||
const XrDuration extrapolation_horizon = 1; // 50'000'000;
|
||||
|
||||
XrTime t0 = instance.now();
|
||||
t0 = t0 - t0 % tracking_period;
|
||||
|
||||
while (not exiting)
|
||||
{
|
||||
@@ -134,11 +132,16 @@ void scenes::stream::tracking()
|
||||
// If thread can't keep up, skip timestamps
|
||||
t0 += tracking_period * ((now - t0) / tracking_period);
|
||||
|
||||
for (XrTime Δt = 0; Δt < extrapolation_horizon; Δt += tracking_period)
|
||||
XrDuration prediction = tracking_prediction_offset;
|
||||
// 1 or 2 samples
|
||||
for (XrDuration Δt = 0; Δt <= prediction; Δt += std::max<XrDuration>(1, prediction))
|
||||
{
|
||||
from_headset::tracking packet{};
|
||||
from_headset::hand_tracking hands{};
|
||||
|
||||
packet.production_timestamp = t0;
|
||||
hands.production_timestamp = t0;
|
||||
|
||||
packet.timestamp = t0 + Δt;
|
||||
hands.timestamp = t0 + Δt;
|
||||
|
||||
@@ -187,3 +190,9 @@ void scenes::stream::tracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void scenes::stream::operator()(to_headset::prediction_offset && packet)
|
||||
{
|
||||
if (packet.offset.count() >= 0)
|
||||
tracking_prediction_offset = packet.offset.count();
|
||||
}
|
||||
|
||||
@@ -141,6 +141,7 @@ struct tracking
|
||||
XrFovf fov;
|
||||
};
|
||||
|
||||
XrTime production_timestamp;
|
||||
XrTime timestamp;
|
||||
XrViewStateFlags flags;
|
||||
|
||||
@@ -169,6 +170,7 @@ struct hand_tracking
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
XrTime production_timestamp;
|
||||
XrTime timestamp;
|
||||
std::optional<std::array<pose, XR_HAND_JOINT_COUNT_EXT>> left;
|
||||
std::optional<std::array<pose, XR_HAND_JOINT_COUNT_EXT>> right;
|
||||
@@ -321,7 +323,12 @@ struct timesync_query
|
||||
XrTime query;
|
||||
};
|
||||
|
||||
using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query>;
|
||||
struct prediction_offset
|
||||
{
|
||||
std::chrono::nanoseconds offset;
|
||||
};
|
||||
|
||||
using packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data, video_stream_data_shard, haptics, timesync_query, prediction_offset>;
|
||||
|
||||
} // namespace to_headset
|
||||
|
||||
|
||||
@@ -120,8 +120,9 @@ static xrt_hand_joint_set convert_joints(const std::optional<std::array<from_hea
|
||||
|
||||
void hand_joints_list::update_tracking(const from_headset::hand_tracking & tracking, const clock_offset & offset)
|
||||
{
|
||||
if (hand_id == 0)
|
||||
add_sample(tracking.timestamp, convert_joints(tracking.left), offset);
|
||||
else
|
||||
add_sample(tracking.timestamp, convert_joints(tracking.right), offset);
|
||||
add_sample(
|
||||
tracking.production_timestamp,
|
||||
tracking.timestamp,
|
||||
convert_joints(hand_id == 0 ? tracking.left : tracking.right),
|
||||
offset);
|
||||
}
|
||||
|
||||
+38
-27
@@ -22,46 +22,51 @@
|
||||
#include "clock_offset.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
template <typename Derived, typename Data, bool extrapolate = false, size_t MaxSamples = 10>
|
||||
class history
|
||||
{
|
||||
struct TimedData : public Data
|
||||
{
|
||||
TimedData(const Data & d, uint64_t t) :
|
||||
Data(d), at_timestamp_ns(t) {}
|
||||
uint64_t at_timestamp_ns;
|
||||
XrTime produced_timestamp;
|
||||
XrTime at_timestamp_ns;
|
||||
};
|
||||
|
||||
std::mutex mutex;
|
||||
std::vector<TimedData> data;
|
||||
std::list<TimedData> data;
|
||||
|
||||
protected:
|
||||
void add_sample(uint64_t timestamp, const Data & sample, const clock_offset & offset)
|
||||
void add_sample(XrTime produced_timestamp, XrTime timestamp, const Data & sample, const clock_offset & offset)
|
||||
{
|
||||
XrTime produced = offset.from_headset(produced_timestamp);
|
||||
XrTime t = offset.from_headset(timestamp);
|
||||
std::lock_guard lock(mutex);
|
||||
|
||||
uint64_t t = offset.from_headset(timestamp);
|
||||
// Only keep one predicted value
|
||||
// It should be the last one
|
||||
if (not data.empty() and t != produced and data.back().at_timestamp_ns != data.back().produced_timestamp)
|
||||
data.pop_back();
|
||||
|
||||
// Insert the new sample
|
||||
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, uint64_t t) { return sample.at_timestamp_ns < t; });
|
||||
|
||||
if (it == data.end())
|
||||
data.emplace_back(sample, t);
|
||||
data.emplace_back(sample, produced, t);
|
||||
else if (it->at_timestamp_ns == t)
|
||||
*it = TimedData(sample, t);
|
||||
*it = TimedData(sample, produced, t);
|
||||
else
|
||||
data.emplace(it, sample, t);
|
||||
data.emplace(it, sample, produced, t);
|
||||
|
||||
if (data.size() > MaxSamples)
|
||||
data.erase(data.begin());
|
||||
while (data.size() > MaxSamples)
|
||||
data.pop_front();
|
||||
}
|
||||
|
||||
public:
|
||||
Data get_at(uint64_t at_timestamp_ns)
|
||||
std::pair<std::chrono::nanoseconds, Data> get_at(XrTime at_timestamp_ns)
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
std::chrono::nanoseconds ex(0);
|
||||
|
||||
if (data.empty())
|
||||
{
|
||||
@@ -70,36 +75,42 @@ public:
|
||||
|
||||
if (data.size() == 1)
|
||||
{
|
||||
return data[0];
|
||||
return {ex, data.front()};
|
||||
}
|
||||
|
||||
if (data.front().at_timestamp_ns > at_timestamp_ns)
|
||||
{
|
||||
if (extrapolate)
|
||||
return Derived::extrapolate(data[0], data[1], data[0].at_timestamp_ns, data[1].at_timestamp_ns, at_timestamp_ns);
|
||||
{
|
||||
auto second = data.begin();
|
||||
auto first = second++;
|
||||
return {ex, Derived::extrapolate(*first, *second, first->at_timestamp_ns, second->at_timestamp_ns, at_timestamp_ns)};
|
||||
}
|
||||
else
|
||||
return data.front();
|
||||
return {ex, data.front()};
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < data.size(); ++i)
|
||||
for (auto after = data.begin(), before = after++; after != data.end(); before = after++)
|
||||
{
|
||||
if (data[i].at_timestamp_ns > at_timestamp_ns)
|
||||
if (after->at_timestamp_ns > at_timestamp_ns)
|
||||
{
|
||||
float t = float(data[i].at_timestamp_ns - at_timestamp_ns) /
|
||||
(data[i].at_timestamp_ns - data[i - 1].at_timestamp_ns);
|
||||
return Derived::interpolate(data[i - 1], data[i], t);
|
||||
ex = std::chrono::nanoseconds(at_timestamp_ns - std::min(before->produced_timestamp, after->produced_timestamp));
|
||||
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)};
|
||||
}
|
||||
}
|
||||
|
||||
ex = std::chrono::nanoseconds(at_timestamp_ns - data.back().produced_timestamp);
|
||||
if (extrapolate)
|
||||
{
|
||||
const auto & d0 = data[data.size() - 2];
|
||||
const auto & d1 = data.back();
|
||||
return Derived::extrapolate(d0, d1, d0.at_timestamp_ns, d1.at_timestamp_ns, at_timestamp_ns);
|
||||
auto prev = data.rbegin();
|
||||
auto last = prev++;
|
||||
return {ex, Derived::extrapolate(*prev, *last, prev->at_timestamp_ns, prev->at_timestamp_ns, at_timestamp_ns)};
|
||||
}
|
||||
else
|
||||
{
|
||||
return data.back();
|
||||
return {ex, data.back()};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -67,7 +67,7 @@ void pose_list::update_tracking(const from_headset::tracking & tracking, const c
|
||||
if (pose.device != device)
|
||||
continue;
|
||||
|
||||
add_sample(tracking.timestamp, convert_pose(pose), offset);
|
||||
add_sample(tracking.production_timestamp, tracking.timestamp, convert_pose(pose), offset);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ void view_list::update_tracking(const from_headset::tracking & tracking, const c
|
||||
view.fovs[eye] = xrt_cast(tracking.views[eye].fov);
|
||||
}
|
||||
|
||||
add_sample(tracking.timestamp, view, offset);
|
||||
add_sample(tracking.production_timestamp, tracking.timestamp, view, offset);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,18 +309,22 @@ void wivrn_controller::set_inputs(device_id input_id, float value, uint64_t last
|
||||
|
||||
xrt_space_relation wivrn_controller::get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns)
|
||||
{
|
||||
std::chrono::nanoseconds extrapolation_time;
|
||||
xrt_space_relation res;
|
||||
switch (name)
|
||||
{
|
||||
case XRT_INPUT_TOUCH_AIM_POSE:
|
||||
return aim.get_at(at_timestamp_ns);
|
||||
|
||||
std::tie(extrapolation_time, res) = aim.get_at(at_timestamp_ns);
|
||||
break;
|
||||
case XRT_INPUT_TOUCH_GRIP_POSE:
|
||||
return grip.get_at(at_timestamp_ns);
|
||||
|
||||
std::tie(extrapolation_time, res) = grip.get_at(at_timestamp_ns);
|
||||
break;
|
||||
default:
|
||||
U_LOG_W("Unknown input name requested");
|
||||
return {};
|
||||
}
|
||||
cnx->add_predict_offset(extrapolation_time);
|
||||
return res;
|
||||
}
|
||||
|
||||
std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_input_name name, uint64_t desired_timestamp_ns)
|
||||
@@ -328,8 +332,11 @@ std::pair<xrt_hand_joint_set, uint64_t> wivrn_controller::get_hand_tracking(xrt_
|
||||
switch (name)
|
||||
{
|
||||
case XRT_INPUT_GENERIC_HAND_TRACKING_LEFT:
|
||||
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT:
|
||||
return {joints.get_at(desired_timestamp_ns), desired_timestamp_ns};
|
||||
case XRT_INPUT_GENERIC_HAND_TRACKING_RIGHT: {
|
||||
auto [extrapolation_time, data] = joints.get_at(desired_timestamp_ns);
|
||||
cnx->add_predict_offset(extrapolation_time);
|
||||
return {data, desired_timestamp_ns};
|
||||
}
|
||||
|
||||
default:
|
||||
U_LOG_W("Unknown input name requested");
|
||||
|
||||
@@ -295,7 +295,9 @@ xrt_space_relation wivrn_hmd::get_tracked_pose(xrt_input_name name, uint64_t at_
|
||||
return {};
|
||||
}
|
||||
|
||||
return views.get_at(at_timestamp_ns).relation;
|
||||
auto [extrapolation_time, res] = views.get_at(at_timestamp_ns);
|
||||
cnx->add_predict_offset(extrapolation_time);
|
||||
return res.relation;
|
||||
}
|
||||
|
||||
void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset)
|
||||
@@ -310,7 +312,8 @@ void wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
|
||||
xrt_fov * out_fovs,
|
||||
xrt_pose * out_poses)
|
||||
{
|
||||
auto view = views.get_at(at_timestamp_ns);
|
||||
auto [extrapolation_time, view] = views.get_at(at_timestamp_ns);
|
||||
cnx->add_predict_offset(extrapolation_time);
|
||||
|
||||
int flags = view.relation.relation_flags;
|
||||
|
||||
|
||||
@@ -70,6 +70,18 @@ struct wivrn_comp_target_factory : public comp_target_factory
|
||||
}
|
||||
};
|
||||
|
||||
void xrt::drivers::wivrn::max_accumulator::send(wivrn_connection & connection)
|
||||
{
|
||||
if (std::chrono::steady_clock::now() < next_sample)
|
||||
return;
|
||||
|
||||
if (auto offset = max.exchange(0))
|
||||
connection.send_stream(to_headset::prediction_offset{
|
||||
.offset = std::chrono::nanoseconds(offset),
|
||||
});
|
||||
next_sample += std::chrono::seconds(1);
|
||||
}
|
||||
|
||||
xrt::drivers::wivrn::wivrn_session::wivrn_session(xrt::drivers::wivrn::TCP && tcp) :
|
||||
connection(std::move(tcp))
|
||||
{
|
||||
@@ -254,6 +266,7 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
|
||||
if (self and not self->quit)
|
||||
{
|
||||
self->offset_est.request_sample(self->connection);
|
||||
self->predict_offset.send(self->connection);
|
||||
self->connection.poll(*self, 20);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -43,6 +43,26 @@ namespace xrt::drivers::wivrn
|
||||
{
|
||||
struct wivrn_comp_target;
|
||||
|
||||
class max_accumulator
|
||||
{
|
||||
using T = std::chrono::nanoseconds::rep;
|
||||
std::atomic<T> max;
|
||||
std::chrono::steady_clock::time_point next_sample;
|
||||
|
||||
public:
|
||||
max_accumulator() :
|
||||
next_sample(std::chrono::steady_clock::now()) {}
|
||||
void add(std::chrono::nanoseconds s)
|
||||
{
|
||||
auto sample = s.count();
|
||||
T prev = max;
|
||||
while (prev < sample and max.compare_exchange_weak(prev, sample))
|
||||
{
|
||||
}
|
||||
}
|
||||
void send(wivrn_connection & connection);
|
||||
};
|
||||
|
||||
class wivrn_session : public std::enable_shared_from_this<wivrn_session>
|
||||
{
|
||||
friend wivrn_comp_target_factory;
|
||||
@@ -58,6 +78,9 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
|
||||
|
||||
clock_offset_estimator offset_est;
|
||||
|
||||
// offsets in ns of each requested frame from its generation time
|
||||
max_accumulator predict_offset;
|
||||
|
||||
std::mutex csv_mutex;
|
||||
std::ofstream feedback_csv;
|
||||
|
||||
@@ -74,6 +97,11 @@ public:
|
||||
|
||||
clock_offset get_offset();
|
||||
|
||||
void add_predict_offset(std::chrono::nanoseconds off)
|
||||
{
|
||||
predict_offset.add(off);
|
||||
}
|
||||
|
||||
void operator()(from_headset::handshake &&) {}
|
||||
void operator()(from_headset::headset_info_packet &&);
|
||||
void operator()(from_headset::tracking &&);
|
||||
|
||||
Reference in New Issue
Block a user