diff --git a/client/application.cpp b/client/application.cpp index 77cc1014..4b55fa61 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -1219,6 +1219,8 @@ void application::initialize() XR_EXT_PERFORMANCE_SETTINGS_EXTENSION_NAME, XR_EXT_USER_PRESENCE_EXTENSION_NAME, + XR_ANDROID_FACE_TRACKING_EXTENSION_NAME, + XR_BD_BODY_TRACKING_EXTENSION_NAME, XR_FB_BODY_TRACKING_EXTENSION_NAME, diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 14148549..6e338668 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -1231,8 +1231,6 @@ void scenes::lobby::on_focused() }, }; - face_tracker = xr::make_face_tracker(instance, system, session); - // 0.4mm / pixel std::vector vps{ { diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index f6bd1887..d98bc8e7 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -722,7 +722,7 @@ void scenes::lobby::gui_settings() ImGui::EndDisabled(); } { - ImGui::BeginDisabled(std::holds_alternative(face_tracker)); + ImGui::BeginDisabled(system.face_tracker_supported() == xr::face_tracker_type::none); bool enabled = config.check_feature(feature::face_tracking); if (ImGui::Checkbox(_S("Enable face tracking"), &enabled)) { @@ -1422,6 +1422,7 @@ static const char * get_face_icon(XrTime predicted_display_time, xr::face_tracke const char * result = nullptr; std::visit(utils::overloaded{ [](std::monostate &) {}, + [&](xr::android_face_tracker &) { result = ICON_FA_FACE_SMILE_WINK; }, [&](xr::htc_face_tracker &) { result = ICON_FA_FACE_SMILE_WINK; }, [&](auto & ft) { ft.get_weights(predicted_display_time, expression); @@ -1504,6 +1505,10 @@ void scenes::lobby::draw_features_status(XrTime predicted_display_time) .icon_disabled = ICON_FA_FACE_MEH_BLANK, }); } + else if (system.face_tracker_supported() != xr::face_tracker_type::none and config.check_feature(feature::face_tracking)) + { + face_tracker = xr::make_face_tracker(instance, system, session); + } if (system.body_tracker_supported() != xr::body_tracker_type::none) { diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 17144c9e..eade56a9 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -259,6 +259,9 @@ std::shared_ptr scenes::stream::create(std::unique_ptr + * Copyright (C) 2025 Sapphire + * + * 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 "android_face_tracker.h" +#include "wivrn_packets.h" +#include "xr/instance.h" +#include "xr/session.h" +#include + +xr::android_face_tracker::android_face_tracker(instance & inst, session & s) : + handle(inst.get_proc("xrDestroyFaceTrackerANDROID")) +{ + auto xrCreateFaceTrackerANDROID = inst.get_proc("xrCreateFaceTrackerANDROID"); + xrGetFaceCalibrationStateANDROID = inst.get_proc("xrGetFaceCalibrationStateANDROID"); + xrGetFaceStateANDROID = inst.get_proc("xrGetFaceStateANDROID"); + assert(xrCreateFaceTrackerANDROID); + + XrFaceTrackerCreateInfoANDROID create_info{ + .type = XR_TYPE_FACE_TRACKER_CREATE_INFO_ANDROID, + .next = nullptr, + }; + + CHECK_XR(xrCreateFaceTrackerANDROID(s, &create_info, &id)); +} + +void xr::android_face_tracker::get_weights(XrTime time, wivrn::from_headset::tracking::android_face & out_expressions) +{ + if (!id || !xrGetFaceStateANDROID) + return; + + XrBool32 is_calibrated; + if (XR_SUCCEEDED(xrGetFaceCalibrationStateANDROID(id, &is_calibrated))) + out_expressions.is_calibrated = is_calibrated; + else + out_expressions.is_calibrated = false; + + XrFaceStateGetInfoANDROID info{ + .type = XR_TYPE_FACE_STATE_GET_INFO_ANDROID, + .next = nullptr, + .time = time, + }; + + XrFaceStateANDROID state{ + .type = XR_TYPE_FACE_STATE_ANDROID, + .next = nullptr, + .parametersCapacityInput = (uint32_t)out_expressions.parameters.size(), + .parameters = out_expressions.parameters.data(), + .regionConfidencesCapacityInput = (uint32_t)out_expressions.confidences.size(), + .regionConfidences = out_expressions.confidences.data(), + }; + + CHECK_XR(xrGetFaceStateANDROID(id, &info, &state)); + out_expressions.state = state.faceTrackingState; + out_expressions.is_valid = state.isValid; +} diff --git a/client/xr/android_face_tracker.h b/client/xr/android_face_tracker.h new file mode 100644 index 00000000..616e475a --- /dev/null +++ b/client/xr/android_face_tracker.h @@ -0,0 +1,42 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 galister + * Copyright (C) 2025 Sapphire + * + * 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 "utils/handle.h" +#include "wivrn_packets.h" +#include + +namespace xr +{ +class instance; +class session; + +class android_face_tracker : public utils::handle +{ + PFN_xrGetFaceCalibrationStateANDROID xrGetFaceCalibrationStateANDROID{}; + PFN_xrGetFaceStateANDROID xrGetFaceStateANDROID{}; + +public: + using packet_type = wivrn::from_headset::tracking::android_face; + android_face_tracker(instance & inst, session & s); + + void get_weights(XrTime, packet_type & out_expressions); +}; +} // namespace xr diff --git a/client/xr/face_tracker.cpp b/client/xr/face_tracker.cpp index 49ec969f..e770b494 100644 --- a/client/xr/face_tracker.cpp +++ b/client/xr/face_tracker.cpp @@ -24,6 +24,13 @@ xr::face_tracker_type xr::face_tracker_supported(xr::instance & instance, xr::system & system) { + if (instance.has_extension(XR_ANDROID_FACE_TRACKING_EXTENSION_NAME)) + { + auto properties = system.android_face_tracking_properties(); + if (properties.supportsFaceTracking) + return xr::face_tracker_type::android; + } + if (instance.has_extension(XR_FB_FACE_TRACKING2_EXTENSION_NAME)) { auto properties = system.fb_face_tracking2_properties(); @@ -57,6 +64,13 @@ xr::face_tracker_type xr::face_tracker_supported(xr::instance & instance, xr::sy xr::face_tracker xr::make_face_tracker(xr::instance & instance, xr::system & system, xr::session & session) { + if (instance.has_extension(XR_ANDROID_FACE_TRACKING_EXTENSION_NAME)) + { + auto properties = system.android_face_tracking_properties(); + if (properties.supportsFaceTracking) + return xr::face_tracker(std::in_place_type_t(), instance, session); + } + if (instance.has_extension(XR_FB_FACE_TRACKING2_EXTENSION_NAME)) { auto properties = system.fb_face_tracking2_properties(); diff --git a/client/xr/face_tracker.h b/client/xr/face_tracker.h index 36ad1af4..58a7db03 100644 --- a/client/xr/face_tracker.h +++ b/client/xr/face_tracker.h @@ -18,6 +18,7 @@ #pragma once +#include "android_face_tracker.h" #include "fb_face_tracker2.h" #include "htc_face_tracker.h" #include "pico_face_tracker.h" @@ -31,7 +32,7 @@ namespace xr class instance; class session; -using face_tracker = std::variant; +using face_tracker = std::variant; face_tracker make_face_tracker(xr::instance &, xr::system &, xr::session &); diff --git a/client/xr/system.cpp b/client/xr/system.cpp index 2b475b6f..0ad34e48 100644 --- a/client/xr/system.cpp +++ b/client/xr/system.cpp @@ -131,6 +131,24 @@ XrSystemUserPresencePropertiesEXT xr::system::user_presence_properties() const return presence_prop; } +XrSystemFaceTrackingPropertiesANDROID xr::system::android_face_tracking_properties() const +{ + if (!id) + throw std::invalid_argument("this"); + + XrSystemFaceTrackingPropertiesANDROID face_tracking_prop{ + .type = XR_TYPE_SYSTEM_FACE_TRACKING_PROPERTIES_ANDROID, + }; + + XrSystemProperties prop{ + .type = XR_TYPE_SYSTEM_PROPERTIES, + .next = &face_tracking_prop, + }; + CHECK_XR(xrGetSystemProperties(*inst, id, &prop)); + + return face_tracking_prop; +} + XrSystemFaceTrackingProperties2FB xr::system::fb_face_tracking2_properties() const { if (!id) diff --git a/client/xr/system.h b/client/xr/system.h index 5797e921..3b1cb8a1 100644 --- a/client/xr/system.h +++ b/client/xr/system.h @@ -50,6 +50,7 @@ enum class body_tracker_type enum class face_tracker_type { none, + android, fb, htc, pico, @@ -83,6 +84,7 @@ public: XrSystemHandTrackingPropertiesEXT hand_tracking_properties() const; XrSystemEyeGazeInteractionPropertiesEXT eye_gaze_interaction_properties() const; XrSystemUserPresencePropertiesEXT user_presence_properties() const; + XrSystemFaceTrackingPropertiesANDROID android_face_tracking_properties() const; XrSystemFaceTrackingProperties2FB fb_face_tracking2_properties() const; XrSystemFacialTrackingPropertiesHTC htc_face_tracking_properties() const; XrSystemBodyTrackingPropertiesFB fb_body_tracking_properties() const; diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index d68951cb..cf91565f 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -38,7 +38,7 @@ namespace wivrn { -static constexpr int protocol_revision = 2; +static constexpr int protocol_revision = 0; enum class device_id : uint8_t { @@ -211,6 +211,7 @@ struct visibility_mask_changed enum face_type : uint8_t { none, + android, fb2, htc, }; @@ -301,6 +302,15 @@ struct tracking std::array views; std::vector device_poses; + struct android_face + { + std::array parameters; + std::array confidences; + XrFaceTrackingStateANDROID state; + bool is_calibrated; + bool is_valid; + }; + struct fb_face2 { std::array weights; @@ -317,7 +327,7 @@ struct tracking bool lip_active; }; - std::variant face; + std::variant face; }; struct trackings diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index e55f7a1c..36691eb1 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -35,6 +35,7 @@ set(XRT_FEATURE_WINDOW_PEEK OFF) set(XRT_FEATURE_OPENXR_ACTIVE_ACTION_SET_PRIORITY ON) set(XRT_FEATURE_RENDERDOC ${WIVRN_FEATURE_RENDERDOC}) +set(XRT_FEATURE_OPENXR_FACE_TRACKING_ANDROID ON) set(XRT_FEATURE_OPENXR_FACE_TRACKING2_FB ON) set(XRT_FEATURE_OPENXR_FACIAL_TRACKING_HTC ON) set(XRT_FEATURE_OPENXR_HAND_TRACKING_EXT ON) @@ -101,6 +102,7 @@ if (WIVRN_BUILD_SERVER) driver/wivrn_controller.cpp driver/wivrn_hand_interaction.cpp driver/wivrn_eye_tracker.cpp + driver/wivrn_android_face_tracker.cpp driver/wivrn_fb_face2_tracker.cpp driver/wivrn_htc_face_tracker.cpp driver/wivrn_generic_tracker.cpp diff --git a/server/driver/wivrn_android_face_tracker.cpp b/server/driver/wivrn_android_face_tracker.cpp new file mode 100644 index 00000000..8d593bb8 --- /dev/null +++ b/server/driver/wivrn_android_face_tracker.cpp @@ -0,0 +1,118 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * Copyright (C) 2024 galister + * Copyright (C) 2025 Sapphire + * + * 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_android_face_tracker.h" + +#include "wivrn_packets.h" +#include "wivrn_session.h" + +#include "os/os_time.h" +#include "util/u_logging.h" +#include "utils/method.h" +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" +#include "xrt/xrt_results.h" + +#include +#include +#include + +namespace wivrn +{ + +wivrn_android_face_tracker::wivrn_android_face_tracker(xrt_device * hmd, + wivrn::wivrn_session & cnx) : + xrt_device{ + .name = XRT_DEVICE_ANDROID_FACE_TRACKING, + .device_type = XRT_DEVICE_TYPE_FACE_TRACKER, + .str = "WiVRn Android Face Tracker", + .serial = "WiVRn Android Face Tracker", + .tracking_origin = hmd->tracking_origin, + .input_count = 1, + .inputs = &face_input, + .supported = { + .face_tracking = true, + }, + .update_inputs = method_pointer<&wivrn_android_face_tracker::update_inputs>, + .get_face_tracking = method_pointer<&wivrn_android_face_tracker::get_face_tracking>, + .get_face_calibration_state_android = method_pointer<&wivrn_android_face_tracker::get_face_calibration_state_android>, + .destroy = [](xrt_device *) {}, + }, + face_input{ + .active = true, + .name = XRT_INPUT_ANDROID_FACE_TRACKING, + }, + cnx(cnx) +{ +} + +xrt_result_t wivrn_android_face_tracker::update_inputs() +{ + return XRT_SUCCESS; +} + +void wivrn_android_face_tracker::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) +{ + auto * face = std::get_if(&tracking.face); + if (not(face and face->is_valid)) + return; + + wivrn_android_face_data data{ + .parameters = face->parameters, + .confidences = face->confidences, + .state = (xrt_face_tracking_state_android)face->state, + .is_calibrated = face->is_calibrated, + .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); +} + +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); + + inout_value->face_expression_set_android.state = data.state; + inout_value->face_expression_set_android.is_valid = data.is_valid; + + if (not data.is_valid) + return XRT_SUCCESS; + + memcpy(&inout_value->face_expression_set_android.parameters, data.parameters.data(), sizeof(float) * data.parameters.size()); + memcpy(&inout_value->face_expression_set_android.region_confidences, data.confidences.data(), sizeof(float) * data.confidences.size()); + + return XRT_SUCCESS; + } + + return XRT_ERROR_NOT_IMPLEMENTED; +} + +xrt_result_t wivrn_android_face_tracker::get_face_calibration_state_android(bool * out_face_is_calibrated) +{ + auto [_, data] = face_list.get_at(os_monotonic_get_ns()); + *out_face_is_calibrated = data.is_calibrated; + return XRT_SUCCESS; +} +} // namespace wivrn \ No newline at end of file diff --git a/server/driver/wivrn_android_face_tracker.h b/server/driver/wivrn_android_face_tracker.h new file mode 100644 index 00000000..2fba2429 --- /dev/null +++ b/server/driver/wivrn_android_face_tracker.h @@ -0,0 +1,99 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * Copyright (C) 2024 galister + * Copyright (C) 2025 Sapphire + * + * 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 "history.h" + +#include +#include + +namespace wivrn +{ + +struct clock_offset; +class wivrn_session; + +struct wivrn_android_face_data +{ + std::array parameters; + std::array confidences; + xrt_face_tracking_state_android state; + bool is_calibrated; + bool is_valid; +}; + +class android_face_list : public history +{ +public: + static wivrn_android_face_data interpolate(const wivrn_android_face_data & a, const wivrn_android_face_data & b, float t) + { + if (not a.is_valid) + { + // in case neither is valid, both will be zeroed, + // so return the later one for timestamp's sake + return b; + } + else if (not b.is_valid) + { + return a; + } + + wivrn_android_face_data result = b; + + for (size_t i = 0; i < result.parameters.size(); i++) + { + result.parameters[i] = std::clamp(std::lerp(a.parameters[i], b.parameters[i], t), 0.0f, 1.0f); + } + for (size_t i = 0; i < result.confidences.size(); i++) + { + result.confidences[i] = std::clamp(std::lerp(a.confidences[i], b.confidences[i], t), 0.0f, 1.0f); + } + return result; + } + + bool update_tracking(const XrTime & production_timestamp, const XrTime & timestamp, const wivrn_android_face_data & data, const clock_offset & offset) + { + return this->add_sample(production_timestamp, timestamp, data, offset); + } +}; + +class wivrn_android_face_tracker : public xrt_device +{ + android_face_list face_list; + xrt_input face_input; + + wivrn::wivrn_session & cnx; + +public: + using base = xrt_device; + wivrn_android_face_tracker(xrt_device * hmd, wivrn::wivrn_session & cnx); + + xrt_result_t update_inputs(); + void update_tracking(const from_headset::tracking &, const clock_offset &); + xrt_result_t get_face_tracking(enum xrt_input_name facial_expression_type, int64_t at_timestamp_ns, struct xrt_facial_expression_set * out_value); + xrt_result_t get_face_calibration_state_android(bool * out_face_is_calibrated); +}; +} // namespace wivrn diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 7d6e1d7a..63b52dc9 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -34,6 +34,7 @@ #include "utils/scoped_lock.h" #include "audio/audio_setup.h" +#include "wivrn_android_face_tracker.h" #include "wivrn_comp_target.h" #include "wivrn_config.h" #include "wivrn_eye_tracker.h" @@ -228,6 +229,12 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection } auto face = get_info().face_tracking; + if (face == from_headset::face_type::android || is_forced_extension("ANDROID_face_tracking")) + { + android_face_tracker = std::make_unique(&hmd, *this); + static_roles.face = android_face_tracker.get(); + xdevs[xdev_count++] = android_face_tracker.get(); + } if (face == from_headset::face_type::fb2 || is_forced_extension("FB_face_tracking2")) { fb_face2_tracker = std::make_unique(&hmd, *this); @@ -552,7 +559,9 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) comp_target->foveation->update_tracking(tracking, offset); } - if (fb_face2_tracker) + if (android_face_tracker) + android_face_tracker->update_tracking(tracking, offset); + else if (fb_face2_tracker) fb_face2_tracker->update_tracking(tracking, offset); else if (htc_face_tracker) htc_face_tracker->update_tracking(tracking, offset); @@ -1106,7 +1115,6 @@ xrt_result_t wivrn_session::get_roles(xrt_system_roles * out_roles) return XRT_SUCCESS; } -// FIXME: use feature_inc and feature_dec to toggle trackings xrt_result_t wivrn_session::feature_inc(xrt_device_feature_type type) { switch (type) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 9f198b3f..0723caf9 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -48,6 +48,7 @@ union xrt_session_event; namespace wivrn { class wivrn_eye_tracker; +class wivrn_android_face_tracker; class wivrn_fb_face2_tracker; class wivrn_htc_face_tracker; class wivrn_generic_tracker; @@ -123,6 +124,7 @@ class wivrn_session : public xrt_system_devices wivrn_hand_interaction right_hand_interaction; int32_t right_hand_interaction_index; std::unique_ptr eye_tracker; + std::unique_ptr android_face_tracker; std::unique_ptr fb_face2_tracker; std::unique_ptr htc_face_tracker; std::vector> generic_trackers;