Merge branch 'sync' into sneed

This commit is contained in:
Vindicus999
2026-09-27 18:51:13 -04:00
172 changed files with 27992 additions and 15036 deletions
+1 -1
View File
@@ -139,7 +139,7 @@ void app_pacer::predict(int64_t now_ns,
// Sync phase with compositor
last_display_time = compositor_display_time + period * ((period / 2 + last_display_time - compositor_display_time) / period);
if (cpu_time > period or gpu_time > period or (min_ready > last_display_time and min_ready < last_display_time + period))
if (min_ready > last_display_time and min_ready < last_display_time + period)
{
// We are limited by app time, don't wait
*out_wake_up_time = now_ns;
+6
View File
@@ -200,6 +200,12 @@ configuration::configuration()
if (auto it = json.find("use-steamvr-lh"); it != json.end())
use_steamvr_lh = *it;
if (auto it = json.find("lh-max-extrapolation"); it != json.end())
lh_max_extrapolation = *it;
if (auto it = json.find("lh-stick-deadzone"); it != json.end())
lh_stick_deadzone = *it;
if (auto it = json.find("bit-depth"); it != json.end())
bit_depth = *it;
+2
View File
@@ -57,6 +57,8 @@ struct configuration
std::vector<std::string> application;
bool debug_gui = false;
bool use_steamvr_lh = false;
std::optional<int64_t> lh_max_extrapolation;
std::optional<float> lh_stick_deadzone;
bool hid_forwarding = false;
bool tcp_only = false;
int port = wivrn::default_port;
+1 -1
View File
@@ -285,7 +285,7 @@ std::pair<XrTime, xrt_space_relation> pose_list::get_at(XrTime at_timestamp_ns)
(*orientation.dy)[1],
(*orientation.dy)[2],
(*orientation.dy)[3]};
Eigen::Quaternionf half_ω = dq * q;
Eigen::Quaternionf half_ω = dq * q.conjugate();
flags(XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT);
ret.angular_velocity = {
@@ -25,6 +25,7 @@
#include "wivrn_session.h"
#include "os/os_time.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "xrt/xrt_defines.h"
@@ -39,6 +40,7 @@ namespace wivrn
wivrn_android_face_tracker::wivrn_android_face_tracker(xrt_device * hmd,
wivrn::wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_ANDROID_FACE_TRACKING,
.device_type = XRT_DEVICE_TYPE_FACE_TRACKER,
.str = "WiVRn Android Face Tracker",
+9 -5
View File
@@ -21,15 +21,17 @@
#include "wivrn_body_tracker.h"
#include "driver/pose_list.h"
#include "driver/wivrn_generic_tracker.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "utils/overloaded.h"
#include "wivrn_packets.h"
#include "wivrn_session.h"
#include "xrt_cast.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_results.h"
#include <unordered_map>
static_assert(std::to_underlying(XR_BODY_JOINT_COUNT_FB) == std::to_underlying(XRT_BODY_JOINT_COUNT_FB));
@@ -249,6 +251,7 @@ void body_joints_list::update_tracking(const wivrn::from_headset::bd_body & trac
wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx, std::function<void(xrt_device &)> device_add_callback) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_FB_BODY_TRACKING, // FIXME: replace with something more appropriate, when there is one
.device_type = XRT_DEVICE_TYPE_BODY_TRACKER,
.tracking_origin = hmd->tracking_origin,
@@ -338,12 +341,13 @@ wivrn_body_tracker::wivrn_body_tracker(xrt_device * hmd, wivrn::wivrn_session &
for (auto & [joint, body_part]: joint_body_parts)
{
auto & name = body_part_pretty_names.at(body_part);
auto [it, success] = virtual_trackers.emplace(std::piecewise_construct,
std::forward_as_tuple(joint),
std::forward_as_tuple(name, hmd, cnx));
[[maybe_unused]] auto [it, success] = virtual_trackers.emplace(std::piecewise_construct,
std::forward_as_tuple(joint),
std::forward_as_tuple(name, hmd, cnx));
assert(success);
device_add_callback(it->second);
}
for (auto & [_, dev]: virtual_trackers)
device_add_callback(dev);
(*this)(*cnx.get_settings());
}
+2 -1
View File
@@ -260,7 +260,8 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
if (client_port >= 0)
{
stream.connect(client_address.sin6_addr, client_port);
client_address.sin6_port = htons(client_port);
stream.connect(client_address);
stream.set_send_buffer_size(1024 * 1024 * 5);
}
else
+5 -3
View File
@@ -21,13 +21,16 @@
#include "wivrn_controller.h"
#include "configuration.h"
#include "driver/xrt_cast.h"
#include "math/m_api.h"
#include "utils/method.h"
#include "wivrn_config.h"
#include "wivrn_session.h"
#include "math/m_api.h"
#include "os/os_time.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "xrt/xrt_defines.h"
#include <array>
#include <format>
#include <magic_enum.hpp>
@@ -35,8 +38,6 @@
#include <optional>
#include <stdexcept>
#include "xrt/xrt_defines.h"
namespace wivrn
{
@@ -593,6 +594,7 @@ wivrn_controller::wivrn_controller(xrt_device_name name,
xrt_device * hmd,
wivrn::wivrn_session * cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = name,
.device_type = hand_id == 0 ? XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER : XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER,
.hmd = nullptr,
+2
View File
@@ -25,6 +25,7 @@
#include "xrt/xrt_defines.h"
#include "xrt/xrt_device.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
@@ -36,6 +37,7 @@ namespace wivrn
wivrn_eye_tracker::wivrn_eye_tracker(wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_EYE_GAZE_INTERACTION,
.device_type = XRT_DEVICE_TYPE_EYE_TRACKER,
.str = "WiVRn Eye Tracker",
+2
View File
@@ -23,6 +23,7 @@
#include "wivrn_packets.h"
#include "wivrn_session.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "xrt/xrt_defines.h"
@@ -38,6 +39,7 @@ namespace wivrn
wivrn_fb_face2_tracker::wivrn_fb_face2_tracker(xrt_device * hmd,
wivrn::wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_FB_FACE_TRACKING2,
.device_type = XRT_DEVICE_TYPE_FACE_TRACKER,
.str = "WiVRn FB v2 Face Tracker",
+2
View File
@@ -25,6 +25,7 @@
#include "xrt/xrt_device.h"
#include "os/os_time.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
@@ -35,6 +36,7 @@ static_assert(gp_count == 18);
wivrn_gamepad::wivrn_gamepad(wivrn::wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_XBOX_CONTROLLER,
.device_type = XRT_DEVICE_TYPE_GAMEPAD,
.str = "WiVRn Gamepad",
+9 -32
View File
@@ -19,54 +19,27 @@
*/
#include "wivrn_generic_tracker.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "wivrn_session.h"
#include "math/m_api.h"
#include "math/m_eigen_interop.hpp"
#include "math/m_space.h"
#include "math/m_vec3.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_results.h"
#include <format>
using namespace xrt::auxiliary::math;
#include <format>
namespace wivrn
{
xrt_space_relation tracker_pose_list::interpolate(const xrt_space_relation & a, const xrt_space_relation & b, float t)
{
xrt_space_relation result;
xrt_space_relation_flags flags = xrt_space_relation_flags(a.relation_flags & b.relation_flags);
m_space_relation_interpolate(const_cast<xrt_space_relation *>(&a), const_cast<xrt_space_relation *>(&b), t, flags, &result);
return result;
return pose_list::interpolate(a, b, t);
}
xrt_space_relation tracker_pose_list::extrapolate(const xrt_space_relation & a, const xrt_space_relation & b, int64_t ta, int64_t tb, int64_t t)
{
float h = (tb - ta) / 1.e9;
xrt_space_relation res = t < ta ? a : b;
xrt_vec3 lin_vel = res.relation_flags & XRT_SPACE_RELATION_LINEAR_VELOCITY_VALID_BIT ? res.linear_velocity : (b.pose.position - a.pose.position) / h;
float dt = (t - tb) / 1.e9;
float dt2_over_2 = dt * dt / 2;
res.pose.position = res.pose.position + lin_vel * dt;
if (res.relation_flags & XRT_SPACE_RELATION_ANGULAR_VELOCITY_VALID_BIT)
{
xrt_vec3 dtheta = res.angular_velocity * dt;
xrt_quat dq;
math_quat_exp(&dtheta, &dq);
map_quat(res.pose.orientation) = map_quat(res.pose.orientation) * map_quat(dq);
}
return res;
return pose_list::extrapolate(a, b, ta, tb, t);
}
void tracker_pose_list::update_tracking(XrTime produced_timestamp, XrTime timestamp, const xrt_space_relation & pose, const clock_offset & offset)
@@ -76,6 +49,7 @@ void tracker_pose_list::update_tracking(XrTime produced_timestamp, XrTime timest
wivrn_generic_tracker::wivrn_generic_tracker(std::string name, xrt_device * hmd, wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_VIVE_TRACKER,
.device_type = XRT_DEVICE_TYPE_GENERIC_TRACKER,
.hmd = nullptr,
@@ -118,7 +92,10 @@ xrt_result_t wivrn_generic_tracker::get_tracked_pose(xrt_input_name name, int64_
XrTime production_timestamp;
if (!enabled)
{
*res = XRT_SPACE_RELATION_ZERO;
return XRT_ERROR_INPUT_UNSUPPORTED;
}
if (name == XRT_INPUT_GENERIC_TRACKER_POSE)
{
+27 -18
View File
@@ -26,6 +26,7 @@
#include "xrt/xrt_device.h"
#include "util/u_device.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
@@ -70,28 +71,27 @@ xrt_result_t wivrn_hmd::get_visibility_mask(xrt_visibility_mask_type type, uint3
wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx,
const from_headset::headset_info_packet & info) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_GENERIC_HMD,
.device_type = XRT_DEVICE_TYPE_HMD,
.str = "WiVRn HMD",
.serial = "WiVRn HMD",
.hmd = &hmd_parts,
.tracking_origin = &tracking_origin,
.input_count = 1,
.inputs = &pose_input,
.supported = {
.orientation_tracking = true,
.position_tracking = true,
.presence = info.user_presence,
.battery_status = true,
},
.update_inputs = [](xrt_device *) { return XRT_SUCCESS; },
.update_inputs = method_pointer<&wivrn_hmd::update_inputs>,
.get_tracked_pose = method_pointer<&wivrn_hmd::get_tracked_pose>,
.get_presence = method_pointer<&wivrn_hmd::get_presence>,
.get_view_poses = method_pointer<&wivrn_hmd::get_view_poses>,
.get_visibility_mask = method_pointer<&wivrn_hmd::get_visibility_mask>,
.get_battery_status = method_pointer<&wivrn_hmd::get_battery_status>,
.destroy = [](xrt_device *) {},
},
presence(true, os_monotonic_get_ns()),
cnx(cnx)
{
xrt_device_init(static_cast<xrt_device *>(this));
@@ -103,6 +103,13 @@ wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx,
auto eye_height = info.render_eye_height;
eye_height = ((eye_height + 3) / 4) * 4;
inputs_array.emplace_back(true, 0, XRT_INPUT_GENERIC_HEAD_POSE);
if (info.user_presence)
inputs_array.emplace_back(true, presence.get_unsafe().change_time, XRT_INPUT_GENERIC_HEAD_DETECT);
input_count = inputs_array.size();
inputs = inputs_array.data();
// Setup info.
hmd->view_count = 2;
hmd->blend_modes[hmd->blend_mode_count++] = XRT_BLEND_MODE_OPAQUE;
@@ -129,6 +136,18 @@ wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx,
hmd->distortion.fov[1] = xrt_cast(info.fov[1]);
}
xrt_result_t wivrn_hmd::update_inputs()
{
if (not supported.presence)
return XRT_SUCCESS;
auto p = presence.lock();
xrt_input & input = inputs_array.at(1);
input.value.boolean = p->value;
input.timestamp = p->change_time;
return XRT_SUCCESS;
}
xrt_result_t wivrn_hmd::get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * res)
{
if (name != XRT_INPUT_GENERIC_HEAD_POSE)
@@ -156,13 +175,6 @@ void wivrn_hmd::update_battery(const from_headset::battery & new_battery)
*battery.lock() = new_battery;
}
xrt_result_t wivrn_hmd::get_presence(bool * out_presence)
{
*out_presence = presence;
return XRT_SUCCESS;
}
xrt_result_t wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
int64_t at_timestamp_ns,
xrt_view_type view_type,
@@ -240,13 +252,10 @@ void wivrn_hmd::update_visibility_mask(const from_headset::visibility_mask_chang
m->at(mask.view_index) = mask.data;
}
bool wivrn_hmd::update_presence(bool new_presence)
void wivrn_hmd::update_presence(bool new_presence, int64_t timestamp)
{
if (this->presence.exchange(new_presence) != new_presence)
{
U_LOG_I("user presence changed to %s", new_presence ? "true" : "false");
return true;
}
return false;
auto p = presence.lock();
p->value = new_presence;
p->change_time = timestamp;
}
} // namespace wivrn
+12 -7
View File
@@ -24,6 +24,8 @@
#include "xrt/xrt_device.h"
#include "xrt/xrt_tracking.h"
#include <inplace_vector.hpp>
#include "view_list.h"
#include <array>
@@ -33,18 +35,21 @@ namespace wivrn
{
class wivrn_session;
struct wivrn_hmd_presence_data
{
bool value;
int64_t change_time;
};
class wivrn_hmd : public xrt_device
{
xrt_input pose_input{
.active = true,
.name = XRT_INPUT_GENERIC_HEAD_POSE,
};
beman::inplace_vector::inplace_vector<xrt_input, 2> inputs_array;
xrt_hmd_parts hmd_parts{};
view_list views;
thread_safe<from_headset::battery> battery{};
std::atomic<bool> presence{true};
thread_safe<wivrn_hmd_presence_data> presence;
thread_safe<std::array<std::optional<from_headset::visibility_mask_changed::masks>, 2>> visibility_mask;
wivrn::wivrn_session * cnx;
@@ -58,8 +63,8 @@ public:
void set_foveated_size(uint32_t width, uint32_t height);
xrt_result_t update_inputs();
xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation *);
xrt_result_t get_presence(bool * out_presence);
xrt_result_t get_view_poses(const xrt_vec3 * default_eye_relation,
int64_t at_timestamp_ns,
xrt_view_type view_type,
@@ -74,6 +79,6 @@ public:
void update_battery(const from_headset::battery &);
void update_tracking(const from_headset::tracking &, const clock_offset &);
void update_visibility_mask(const from_headset::visibility_mask_changed &);
bool update_presence(bool);
void update_presence(bool new_presence, int64_t timestamp);
};
} // namespace wivrn
+2
View File
@@ -23,6 +23,7 @@
#include "wivrn_packets.h"
#include "wivrn_session.h"
#include "util/u_device_id.h"
#include "util/u_logging.h"
#include "utils/method.h"
#include "xrt/xrt_defines.h"
@@ -38,6 +39,7 @@ namespace wivrn
wivrn_htc_face_tracker::wivrn_htc_face_tracker(xrt_device * hmd,
wivrn::wivrn_session & cnx) :
xrt_device{
.id = u_device_id_generate(),
.name = XRT_DEVICE_HTC_FACE_TRACKING,
.device_type = XRT_DEVICE_TYPE_FACE_TRACKER,
.str = "WiVRn HTC Face Tracker",
+102 -86
View File
@@ -43,6 +43,7 @@
#include "wivrn_packets.h"
#include "xr/to_string.h"
#include "b_body_tracker.h"
#include "b_hand_tracker.h"
#include "b_system.h"
#include "target_builder_helpers.h"
@@ -82,6 +83,7 @@ bool is_forced_extension(const char * ext_name)
wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection, b_system & system) :
xrt_system_devices{
.get_roles = method_pointer<&wivrn_session::get_roles>,
.create_body_tracker = b_body_tracker_create,
.create_hand_tracker = b_hand_tracker_create,
.feature_inc = method_pointer<&wivrn_session::feature_inc>,
.feature_dec = method_pointer<&wivrn_session::feature_dec>,
@@ -133,43 +135,52 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
roles.gamepad = static_xdev_count;
static_xdevs[static_xdev_count++] = &gamepad_device.emplace(*this);
auto conf = configuration();
#if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE
auto use_steamvr_lh = configuration().use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH");
xrt_system_devices * lhdevs = NULL;
auto use_steamvr_lh = conf.use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH");
if (use_steamvr_lh)
{
if (conf.lh_stick_deadzone > 0.01f)
setenv("LH_STICK_DEADZONE", std::format("{:.2}", *conf.lh_stick_deadzone).c_str(), true);
if (conf.lh_max_extrapolation.has_value())
setenv("LH_MAX_EXTRAPOLATION_MS", std::format("{}", *conf.lh_max_extrapolation).c_str(), true);
U_LOG_W("=====================");
U_LOG_W("Disregard lighthousedb / chaperone related error messages from the lighthouse driver. These are irrelevant in case of WiVRn.");
U_LOG_W("If getting a SIGSEGV right after this, you are likely using an unsupported SteamVR version!");
U_LOG_W("=====================");
if (steamvr_lh_create_devices(nullptr, &lhdevs) == XRT_SUCCESS)
int original_count = static_xdev_count;
if (steamvr_lh_create_devices(nullptr, this) == XRT_SUCCESS)
{
for (int i = 0; i < lhdevs->static_xdev_count; i++)
for (int i = original_count; i < static_xdev_count; i++)
{
auto lhdev = lhdevs->static_xdevs[i];
auto lhdev = static_xdevs[i];
switch (lhdev->device_type)
{
case XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER:
roles.left = static_xdev_count;
roles.left = i;
static_roles.hand_tracking.unobstructed.left = nullptr;
static_roles.hand_tracking.conforming.left = lhdev;
break;
case XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER:
roles.right = static_xdev_count;
roles.right = i;
static_roles.hand_tracking.unobstructed.right = nullptr;
static_roles.hand_tracking.conforming.right = lhdev;
break;
case XRT_DEVICE_TYPE_ANY_HAND_CONTROLLER:
if (roles.left == left_controller_index)
{
roles.left = static_xdev_count;
roles.left = i;
static_roles.hand_tracking.unobstructed.left = nullptr;
static_roles.hand_tracking.conforming.left = lhdev;
}
else if (roles.right == right_controller_index)
{
roles.right = static_xdev_count;
roles.right = i;
static_roles.hand_tracking.unobstructed.right = nullptr;
static_roles.hand_tracking.conforming.right = lhdev;
}
@@ -177,7 +188,6 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
default:
break;
}
static_xdevs[static_xdev_count++] = lhdev;
}
}
}
@@ -255,7 +265,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> connection
strlcpy(xrt_system.base.properties.name, system_name.c_str(), std::size(xrt_system.base.properties.name));
}
if (configuration().hid_forwarding)
if (conf.hid_forwarding)
{
try
{
@@ -316,6 +326,9 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
}
self->system_compositor = *out_xsysc;
struct xrt_pose t_stage_local = XRT_POSE_IDENTITY;
t_stage_local.position.y = 1.6;
t_builder_create_space_overseer_legacy(
&self->xrt_system.broadcast,
&self->hmd,
@@ -326,6 +339,7 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
self->static_xdevs,
self->static_xdev_count,
false,
&t_stage_local,
false,
out_xspovrs);
self->space_overseer = *out_xspovrs;
@@ -347,12 +361,6 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptr<wivrn_connecti
}
}
auto dump_file = std::getenv("WIVRN_DUMP_TIMINGS");
if (dump_file)
{
self->feedback_csv.open(dump_file);
}
*out_xsysd = self.release();
return XRT_SUCCESS;
}
@@ -371,13 +379,27 @@ void wivrn_session::stop()
worker_thread = std::jthread();
}
bool wivrn_session::request_stop()
void wivrn_session::request_stop()
{
assert(mnd_ipc_server);
bool b = net_thread.request_stop();
stop_application(std::nullopt, 2l * U_TIME_1S_IN_NS);
int64_t end = os_monotonic_get_ns() + 3l * U_TIME_1S_IN_NS;
while (os_monotonic_get_ns() < end)
{
{
scoped_lock lock(xrt_system.sessions.mutex);
if (xrt_system.sessions.count == 0)
{
U_LOG_I("No more sessions, exiting");
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
net_thread.request_stop();
worker_thread.request_stop();
ipc_server_stop(mnd_ipc_server);
return b;
}
void wivrn_session::pause_session()
@@ -386,14 +408,9 @@ void wivrn_session::pause_session()
// notify clients about session pause
update_client_states(false, false);
if (get_info().user_presence)
{
(*this)(from_headset::user_presence_changed{
.present = false,
});
}
hmd.update_presence(false, os_monotonic_get_ns());
update_client_states(false, false);
// pause session components
@@ -442,13 +459,6 @@ void wivrn_session::resume_session()
compositor.resume();
if (get_info().user_presence)
{
(*this)(from_headset::user_presence_changed{
.present = true,
});
}
(*this)(from_headset::get_application_list{
.language = get_info().language,
.country = get_info().country,
@@ -459,6 +469,7 @@ void wivrn_session::resume_session()
worker_thread = std::jthread([this](std::stop_token stop) { return run_worker(stop); });
update_client_states(true, true);
hmd.update_presence(false, os_monotonic_get_ns());
}
clock_offset wivrn_session::get_offset()
@@ -791,17 +802,17 @@ void wivrn_session::operator()(from_headset::feedback && feedback)
compositor.on_feedback(feedback, o);
if (feedback.received_first_packet)
dump_time("receive_begin", feedback.frame_index, o.from_headset(feedback.received_first_packet), feedback.stream_index);
trace::instant_feedback("receive_begin", o.from_headset(feedback.received_first_packet), feedback.frame_index, feedback.stream_index);
if (feedback.received_last_packet)
dump_time("receive_end", feedback.frame_index, o.from_headset(feedback.received_last_packet), feedback.stream_index);
trace::instant_feedback("receive_end", o.from_headset(feedback.received_last_packet), feedback.frame_index, feedback.stream_index);
if (feedback.sent_to_decoder)
dump_time("decode_begin", feedback.frame_index, o.from_headset(feedback.sent_to_decoder), feedback.stream_index);
trace::instant_feedback("decode_begin", o.from_headset(feedback.sent_to_decoder), feedback.frame_index, feedback.stream_index);
if (feedback.received_from_decoder)
dump_time("decode_end", feedback.frame_index, o.from_headset(feedback.received_from_decoder), feedback.stream_index);
trace::instant_feedback("decode_end", o.from_headset(feedback.received_from_decoder), feedback.frame_index, feedback.stream_index);
if (feedback.blitted)
dump_time("blit", feedback.frame_index, o.from_headset(feedback.blitted), feedback.stream_index);
trace::instant_feedback("blit", o.from_headset(feedback.blitted), feedback.frame_index, feedback.stream_index);
if (feedback.displayed)
dump_time("display", feedback.frame_index, o.from_headset(feedback.displayed), feedback.stream_index);
trace::instant_feedback("display", o.from_headset(feedback.displayed), feedback.frame_index, feedback.stream_index);
}
void wivrn_session::operator()(from_headset::battery && battery)
@@ -845,14 +856,9 @@ void wivrn_session::operator()(from_headset::session_state_changed && event)
}
void wivrn_session::operator()(from_headset::user_presence_changed && event)
{
if (hmd.update_presence(event.present))
push_event(
{
.presence_change = {
.type = XRT_SESSION_EVENT_USER_PRESENCE_CHANGE,
.is_user_present = event.present,
},
});
clock_offset o = offset_est.get_offset();
U_LOG_I("user presence changed to %s", event.present ? "true" : "false");
hmd.update_presence(event.present, o ? o.from_headset(event.change_time) : os_monotonic_get_ns());
}
void wivrn_session::operator()(from_headset::refresh_rate_changed && event)
@@ -966,30 +972,7 @@ void wivrn_session::operator()(const from_headset::set_active_application & req)
void wivrn_session::operator()(const from_headset::stop_application & req)
{
assert(mnd_ipc_server);
scoped_lock lock(mnd_ipc_server->global_state.lock);
for (auto & t: mnd_ipc_server->threads)
{
if (t.ics.client_state.id == req.id)
{
if (!t.ics.xs)
{
U_LOG_W("Unable to stop app %s: no session!", t.ics.client_state.info.application_name);
break;
}
U_LOG_I("Request exit for application %s", t.ics.client_state.info.application_name);
xrt_result_t xret = xrt_session_request_exit(t.ics.xs);
if (xret != XRT_SUCCESS)
{
U_LOG_W("Failed to request exit for application %s: %s", t.ics.client_state.info.application_name, u_str_xrt_result(xret));
}
auto when = os_monotonic_get_ns() + 10l * U_TIME_1S_IN_NS;
session_loss.lock()->emplace(req.id, when);
break;
}
}
stop_application(req.id, 10l * U_TIME_1S_IN_NS);
}
void wivrn_session::operator()(audio_data && data)
@@ -1085,10 +1068,17 @@ void wivrn_session::run_net(std::stop_token stop)
for (auto & haptics: uinput_handler->read_rumble())
send_stream(std::move(haptics));
}
if (auto locked = net_exception.lock(); *locked)
{
std::exception_ptr ex;
std::swap(ex, *locked);
std::rethrow_exception(ex);
}
}
catch (const std::exception & e)
{
U_LOG_W("Exception in network thread: %s, Session paused.", e.what());
U_LOG_W("Network exception: %s, Session paused.", e.what());
pause_session();
reconnect(stop);
@@ -1159,20 +1149,14 @@ void wivrn_session::set_foveated_size(uint32_t width, uint32_t height)
hmd.set_foveated_size(width, height);
}
void wivrn_session::dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream, const char * extra)
{
trace::instant_feedback(event.c_str(), time, frame, stream);
if (feedback_csv)
{
std::lock_guard lock(csv_mutex);
feedback_csv << std::quoted(event) << "," << frame << "," << time << "," << (int)stream << extra << std::endl;
}
}
void wivrn_session::quit_if_no_client()
{
scoped_lock lock(xrt_system.sessions.mutex);
if (xrt_system.sessions.count == 0)
uint32_t count;
{
scoped_lock lock(xrt_system.sessions.mutex);
count = xrt_system.sessions.count;
}
if (count == 0)
{
U_LOG_I("No OpenXR client connected, exiting");
request_stop();
@@ -1291,6 +1275,7 @@ void wivrn_session::reconnect(std::stop_token stop)
}
connection->reset(stop, std::move(*tcp), [this]() { quit_if_no_client(); });
*net_exception.lock() = nullptr;
// validate if headset is compatible with the current session
@@ -1338,6 +1323,37 @@ void wivrn_session::reconnect(std::stop_token stop)
}
}
void wivrn_session::stop_application(std::optional<uint32_t> id, int64_t timeout_ns)
{
assert(mnd_ipc_server);
scoped_lock lock(mnd_ipc_server->global_state.lock);
for (auto & t: mnd_ipc_server->threads)
{
if (t.state == IPC_THREAD_READY || t.state == IPC_THREAD_STOPPING)
continue;
uint32_t client_id = t.ics.client_state.id;
if (not id.has_value() or client_id == *id)
{
if (!t.ics.xs)
{
U_LOG_W("Unable to stop app %s: no session!", t.ics.client_state.info.application_name);
continue;
}
U_LOG_I("Request exit for application %s", t.ics.client_state.info.application_name);
xrt_result_t xret = xrt_session_request_exit(t.ics.xs);
if (xret != XRT_SUCCESS)
{
U_LOG_W("Failed to request exit for application %s: %s", t.ics.client_state.info.application_name, u_str_xrt_result(xret));
}
auto when = os_monotonic_get_ns() + timeout_ns;
session_loss.lock()->emplace(client_id, when);
}
}
}
void wivrn_session::update_client_states(bool visible, bool focused)
{
assert(mnd_ipc_server);
+24 -9
View File
@@ -40,7 +40,6 @@
#include "wivrn_uinput.h"
#include "xrt/xrt_results.h"
#include "xrt/xrt_system.h"
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
@@ -110,9 +109,6 @@ class wivrn_session : public xrt_system_devices
clock_offset_estimator offset_est;
std::atomic<XrDuration> tracking_latency; // production to reception time
std::mutex csv_mutex;
std::ofstream feedback_csv;
std::unique_ptr<audio_device> audio_handle;
// when sessions shall be destroyed, key is client id, value is timestamp
@@ -121,6 +117,8 @@ class wivrn_session : public xrt_system_devices
std::jthread net_thread;
std::jthread worker_thread;
thread_safe<std::exception_ptr> net_exception;
wivrn_session(std::unique_ptr<wivrn_connection> connection, b_system &);
public:
@@ -136,7 +134,7 @@ public:
void start(ipc_server *);
void stop();
bool request_stop();
void request_stop();
void quit_if_no_client();
clock_offset get_offset();
@@ -204,21 +202,35 @@ public:
template <typename T>
void send_stream(T && packet)
{
connection->send_stream(std::forward<T>(packet));
try
{
connection->send_stream(std::forward<T>(packet));
}
catch (std::exception & e)
{
*net_exception.lock() = std::current_exception();
throw;
}
}
template <typename T>
void send_control(T && packet)
{
connection->send_control(std::forward<T>(packet));
try
{
connection->send_control(std::forward<T>(packet));
}
catch (std::exception & e)
{
*net_exception.lock() = std::current_exception();
throw;
}
}
xrt_result_t push_event(const xrt_session_event &);
void set_foveated_size(uint32_t width, uint32_t height);
void dump_time(const std::string & event, uint64_t frame, int64_t time, uint8_t stream = -1, const char * extra = "");
private:
void run_net(std::stop_token stop);
void run_worker(std::stop_token stop);
@@ -227,6 +239,9 @@ private:
void pause_session();
void resume_session();
// nullopt for id stops all apps
void stop_application(std::optional<uint32_t> id, int64_t timeout_ns);
void update_client_states(bool visible, bool focused);
void poll_session_loss();
+1 -1
View File
@@ -28,7 +28,7 @@ wivrn::fd_base open_uinput_or_throw(int flags = O_WRONLY)
int fd = ::open(p, flags | O_NONBLOCK);
if (fd >= 0)
return fd;
if (errno != ENOENT) // exists but inaccessbile
if (errno != ENOENT) // exists but inaccessible
throw std::system_error(errno, std::generic_category(), "error while opening uinput");
}
throw std::runtime_error("no uinput device found");