Files
WiVRn/client/scenes/stream_tracking.cpp
T
Patrick Nicolas 7658f427e6 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.
2024-04-04 13:41:36 +02:00

199 lines
6.3 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2022 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 "application.h"
#include "stream.h"
#include "utils/ranges.h"
#include <spdlog/spdlog.h>
#include <thread>
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)
{
XrSpaceVelocity velocity{
.type = XR_TYPE_SPACE_VELOCITY,
};
XrSpaceLocation location{
.type = XR_TYPE_SPACE_LOCATION,
.next = &velocity,
};
xrLocateSpace(space, reference, time, &location);
from_headset::tracking::pose res{
.device = device,
.pose = location.pose,
.linear_velocity = velocity.linearVelocity,
.angular_velocity = velocity.angularVelocity,
.flags = 0,
};
if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT)
res.flags |= from_headset::tracking::orientation_valid;
if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT)
res.flags |= from_headset::tracking::position_valid;
if (velocity.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT)
res.flags |= from_headset::tracking::linear_velocity_valid;
if (velocity.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT)
res.flags |= from_headset::tracking::angular_velocity_valid;
if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)
res.flags |= from_headset::tracking::orientation_tracked;
if (location.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
res.flags |= from_headset::tracking::position_tracked;
return res;
}
static std::optional<std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT>> locate_hands(xr::hand_tracker & hand, XrSpace space, XrTime time)
{
auto joints = hand.locate(space, time);
if (joints)
{
std::array<from_headset::hand_tracking::pose, XR_HAND_JOINT_COUNT_EXT> poses;
for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
{
poses[i] = {
.pose = (*joints)[i].first.pose,
.linear_velocity = (*joints)[i].second.linearVelocity,
.angular_velocity = (*joints)[i].second.angularVelocity,
.radius = (*joints)[i].first.radius,
};
if ((*joints)[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::orientation_valid;
if ((*joints)[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::position_valid;
if ((*joints)[i].second.velocityFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::linear_velocity_valid;
if ((*joints)[i].second.velocityFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT)
poses[i].flags |= from_headset::hand_tracking::angular_velocity_valid;
if ((*joints)[i].first.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT)
poses[i].flags |= from_headset::hand_tracking::orientation_tracked;
if ((*joints)[i].first.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
poses[i].flags |= from_headset::hand_tracking::position_tracked;
}
return poses;
}
else
return std::nullopt;
}
void scenes::stream::tracking()
{
std::vector<std::pair<device_id, XrSpace>> spaces = {
{device_id::HEAD, application::view()},
{device_id::LEFT_AIM, application::left_aim()},
{device_id::LEFT_GRIP, application::left_grip()},
{device_id::RIGHT_AIM, application::right_aim()},
{device_id::RIGHT_GRIP, application::right_grip()}};
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
XrTime t0 = instance.now();
while (not exiting)
{
try
{
XrTime now = instance.now();
if (now < t0)
std::this_thread::sleep_for(std::chrono::nanoseconds(t0 - now - dt));
// If thread can't keep up, skip timestamps
t0 += tracking_period * ((now - t0) / 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;
try
{
auto [flags, views] = session.locate_views(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, t0 + Δt, view_space);
assert(views.size() == packet.views.size());
for (auto [i, j]: utils::zip(views, packet.views))
{
j.pose = i.pose;
j.fov = i.fov;
}
packet.flags = flags;
for (auto [device, space]: spaces)
{
packet.device_poses.push_back(locate_space(device, space, world_space, t0 + Δt));
}
network_session->send_stream(packet);
if (application::get_hand_tracking_supported())
{
hands.left = locate_hands(application::get_left_hand(), world_space, hands.timestamp);
hands.right = locate_hands(application::get_right_hand(), world_space, hands.timestamp);
network_session->send_stream(hands);
}
}
catch (const std::system_error & e)
{
if (e.code().category() != xr::error_category() or
e.code().value() != XR_ERROR_TIME_INVALID)
throw;
}
}
t0 += tracking_period;
}
catch (std::exception & e)
{
spdlog::info("Exception in tracking thread, exiting: {}", e.what());
exit();
}
}
}
void scenes::stream::operator()(to_headset::prediction_offset && packet)
{
if (packet.offset.count() >= 0)
tracking_prediction_offset = packet.offset.count();
}