Forward XR_ANDROID_face_tracking

This commit is contained in:
Sapphire
2025-12-04 11:16:31 +01:00
committed by xytovl
parent ac0a926811
commit 51d8742029
16 changed files with 403 additions and 8 deletions
@@ -0,0 +1,118 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
* Copyright (C) 2024 galister <galister@librevr.org>
* Copyright (C) 2025 Sapphire <imsapphire0@gmail.com>
*
* 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 "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 <cmath>
#include <cstdint>
#include <cstring>
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<from_headset::tracking::android_face>(&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
@@ -0,0 +1,99 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
* Copyright (C) 2024 galister <galister@librevr.org>
* Copyright (C) 2025 Sapphire <imsapphire0@gmail.com>
*
* 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/>.
*/
#pragma once
#include "wivrn_packets.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_device.h"
#include "history.h"
#include <array>
#include <cmath>
namespace wivrn
{
struct clock_offset;
class wivrn_session;
struct wivrn_android_face_data
{
std::array<float, XRT_FACE_PARAMETER_COUNT_ANDROID> parameters;
std::array<float, XRT_FACE_REGION_CONFIDENCE_COUNT_ANDROID> confidences;
xrt_face_tracking_state_android state;
bool is_calibrated;
bool is_valid;
};
class android_face_list : public history<android_face_list, wivrn_android_face_data>
{
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
+10 -2
View File
@@ -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<wivrn_connection> 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<wivrn_android_face_tracker>(&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<wivrn_fb_face2_tracker>(&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)
+2
View File
@@ -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<wivrn_eye_tracker> eye_tracker;
std::unique_ptr<wivrn_android_face_tracker> android_face_tracker;
std::unique_ptr<wivrn_fb_face2_tracker> fb_face2_tracker;
std::unique_ptr<wivrn_htc_face_tracker> htc_face_tracker;
std::vector<std::unique_ptr<wivrn_generic_tracker>> generic_trackers;