From cee3ac7b9903267d0bb0ffddf80b82d2796ebf1d Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Fri, 2 Aug 2024 23:40:11 +0900 Subject: [PATCH] move eye tracker to its own device --- server/CMakeLists.txt | 1 + server/driver/wivrn_eye_tracker.cpp | 112 ++++++++++++++++++++++++++++ server/driver/wivrn_eye_tracker.h | 53 +++++++++++++ server/driver/wivrn_hmd.cpp | 22 ++---- server/driver/wivrn_hmd.h | 1 - server/driver/wivrn_session.cpp | 12 ++- server/driver/wivrn_session.h | 2 + 7 files changed, 184 insertions(+), 19 deletions(-) create mode 100644 server/driver/wivrn_eye_tracker.cpp create mode 100644 server/driver/wivrn_eye_tracker.h diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 02c68808..04cf0bac 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -78,6 +78,7 @@ add_executable(wivrn-server driver/wivrn_pacer.cpp driver/wivrn_comp_target.cpp driver/wivrn_controller.cpp + driver/wivrn_eye_tracker.cpp driver/pose_list.cpp driver/view_list.cpp driver/hand_joints_list.cpp diff --git a/server/driver/wivrn_eye_tracker.cpp b/server/driver/wivrn_eye_tracker.cpp new file mode 100644 index 00000000..82a45802 --- /dev/null +++ b/server/driver/wivrn_eye_tracker.cpp @@ -0,0 +1,112 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * Copyright (C) 2024 galister + * + * 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 "wivrn_eye_tracker.h" + +#include "wivrn_packets.h" +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" + +#include "util/u_logging.h" + +#include +#include +#include + +static void wivrn_eye_tracker_destroy(xrt_device * xdev); + +static void wivrn_eye_tracker_update_inputs(xrt_device * xdev); + +static void wivrn_eye_tracker_get_tracked_pose(xrt_device * xdev, + xrt_input_name name, + uint64_t at_timestamp_ns, + xrt_space_relation * out_relation); + +wivrn_eye_tracker::wivrn_eye_tracker(xrt_device * hmd, + std::shared_ptr cnx) : + xrt_device{}, gaze(device_id::EYE_GAZE), cnx(cnx) +{ + xrt_device * base = this; + base->tracking_origin = hmd->tracking_origin; + + base->update_inputs = wivrn_eye_tracker_update_inputs; + base->get_tracked_pose = wivrn_eye_tracker_get_tracked_pose; + base->destroy = wivrn_eye_tracker_destroy; + name = XRT_DEVICE_EYE_GAZE_INTERACTION; + device_type = XRT_DEVICE_TYPE_EYE_TRACKER; + eye_gaze_supported = true; + + // Print name. + strcpy(str, "WiVRn Eye Tracker"); + strcpy(serial, "WiVRn Eye Tracker"); + + gaze_input.active = true; + gaze_input.name = XRT_INPUT_GENERIC_EYE_GAZE_POSE; + + // Setup input. + inputs = &gaze_input; + input_count = 1; +} + +void wivrn_eye_tracker::update_inputs() +{ + // Empty +} + +xrt_space_relation wivrn_eye_tracker::get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns) +{ + if (name == XRT_INPUT_GENERIC_EYE_GAZE_POSE) + { + auto [_, relation] = gaze.get_at(at_timestamp_ns); + return relation; + } + + U_LOG_E("Unknown input name"); + return {}; +} + +void wivrn_eye_tracker::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) +{ + gaze.update_tracking(tracking, offset); +} + +/* + * + * Functions + * + */ + +static void wivrn_eye_tracker_destroy(xrt_device * xdev) +{ + static_cast(xdev)->unregister(); +} + +static void wivrn_eye_tracker_update_inputs(xrt_device * xdev) +{ + static_cast(xdev)->update_inputs(); +} + +static void wivrn_eye_tracker_get_tracked_pose(xrt_device * xdev, + xrt_input_name name, + uint64_t at_timestamp_ns, + xrt_space_relation * out_relation) +{ + *out_relation = static_cast(xdev)->get_tracked_pose(name, at_timestamp_ns); +} diff --git a/server/driver/wivrn_eye_tracker.h b/server/driver/wivrn_eye_tracker.h new file mode 100644 index 00000000..428de797 --- /dev/null +++ b/server/driver/wivrn_eye_tracker.h @@ -0,0 +1,53 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * Copyright (C) 2024 galister + * + * 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 "wivrn_packets.h" +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" +#include "xrt/xrt_tracking.h" + +#include "pose_list.h" +#include "wivrn_session.h" + +#include +#include +#include + +class wivrn_eye_tracker : public xrt_device +{ + std::mutex mutex; + xrt_input gaze_input; + pose_list gaze; + + std::shared_ptr cnx; + +public: + wivrn_eye_tracker(xrt_device * hmd, std::shared_ptr cnx); + void unregister() + { + cnx = nullptr; + } + + void update_inputs(); + void update_tracking(const from_headset::tracking &, const clock_offset &); + xrt_space_relation get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns); +}; diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index c3835520..e0b4720c 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -180,7 +180,7 @@ bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_in wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, const from_headset::headset_info_packet & info) : - xrt_device{}, gaze(device_id::EYE_GAZE), cnx(cnx) + xrt_device{}, cnx(cnx) { xrt_device * base = this; @@ -196,7 +196,6 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, orientation_tracking_supported = true; // hand_tracking_supported = true; position_tracking_supported = true; - eye_gaze_supported = info.eye_gaze; // Print name. strcpy(str, "WiVRn HMD"); @@ -246,27 +245,20 @@ void wivrn_hmd::update_inputs() xrt_space_relation wivrn_hmd::get_tracked_pose(xrt_input_name name, uint64_t at_timestamp_ns) { - if (name == XRT_INPUT_GENERIC_HEAD_POSE) + if (name != XRT_INPUT_GENERIC_HEAD_POSE) { - auto [extrapolation_time, res] = views.get_at(at_timestamp_ns); - cnx->add_predict_offset(extrapolation_time); - return res.relation; + U_LOG_E("Unknown input name"); + return {}; } - if (name == XRT_INPUT_GENERIC_EYE_GAZE_POSE) - { - auto [_, relation] = gaze.get_at(at_timestamp_ns); - return relation; - } - - U_LOG_E("Unknown input name"); - return {}; + 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) { views.update_tracking(tracking, offset); - gaze.update_tracking(tracking, offset); } void wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation, diff --git a/server/driver/wivrn_hmd.h b/server/driver/wivrn_hmd.h index 065762aa..aabeecfb 100644 --- a/server/driver/wivrn_hmd.h +++ b/server/driver/wivrn_hmd.h @@ -47,7 +47,6 @@ class wivrn_hmd : public xrt_device }; view_list views; - pose_list gaze; std::array foveation_parameters{}; std::shared_ptr cnx; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 1a5389d3..db388131 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -31,6 +31,7 @@ #include "audio/audio_setup.h" #include "wivrn_comp_target.h" #include "wivrn_controller.h" +#include "wivrn_eye_tracker.h" #include "wivrn_hmd.h" #include "xrt/xrt_session.h" @@ -143,9 +144,6 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi if (self->hmd) usysds->base.base.static_roles.head = devices->xdevs[n++] = self->hmd.get(); - if (self->hmd->eye_gaze_supported) - usysds->base.base.static_roles.eyes = self->hmd.get(); - if (self->left_hand) { devices->xdevs[n++] = self->left_hand.get(); @@ -156,6 +154,12 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi devices->xdevs[n++] = self->right_hand.get(); usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get(); } + if (info.eye_gaze) + { + self->eye_tracker = std::make_unique(self->hmd.get(), self); + usysds->base.base.static_roles.eyes = self->eye_tracker.get(); + devices->xdevs[n++] = self->eye_tracker.get(); + } devices->xdev_count = n; u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get()); @@ -250,6 +254,8 @@ void wivrn_session::operator()(from_headset::tracking && tracking) hmd->update_tracking(tracking, offset); left_hand->update_tracking(tracking, offset); right_hand->update_tracking(tracking, offset); + if (eye_tracker) + eye_tracker->update_tracking(tracking, offset); } void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 22154567..f579c82b 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -32,6 +32,7 @@ class wivrn_hmd; class wivrn_controller; +class wivrn_eye_tracker; struct audio_device; struct u_system; @@ -79,6 +80,7 @@ class wivrn_session : public std::enable_shared_from_this std::unique_ptr hmd; std::unique_ptr left_hand; std::unique_ptr right_hand; + std::unique_ptr eye_tracker; wivrn_comp_target * comp_target; clock_offset_estimator offset_est;