From 63c668edcd63447b0a18650c8a6f1a6f52eaeab6 Mon Sep 17 00:00:00 2001 From: Riccardo Zaglia Date: Wed, 8 Nov 2023 18:00:04 +0800 Subject: [PATCH] fix: :zap: Reduce delay when setting chaperone --- .../cpp/alvr_server/ChaperoneUpdater.cpp | 56 +++++++++++-------- alvr/server/cpp/alvr_server/bindings.h | 5 +- alvr/server/src/connection.rs | 5 +- alvr/server/src/lib.rs | 6 +- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp b/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp index 9cb9d8ca79..bab074a49f 100644 --- a/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp +++ b/alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp @@ -11,26 +11,34 @@ namespace alvr_chaperone { using namespace alvr_chaperone; #endif -static std::mutex chaperone_mutex; +std::mutex chaperone_mutex; -#ifdef __linux__ -vr::HmdMatrix34_t GetRawZeroPose() { - vr::HmdMatrix34_t out = {}; +void InitChaperoneClient() { +#ifndef __APPLE__ std::unique_lock lock(chaperone_mutex); + vr::EVRInitError error; vr::VR_Init(&error, vr::VRApplication_Utility); + if (error != vr::VRInitError_None) { - Warn("Failed to init OpenVR client to get raw zero pose! Error: %d", error); - return out; + Warn("Failed to init OpenVR client to update Chaperone boundary! Error: %d", error); + return; } - out = vr::VRSystem()->GetRawZeroPoseToStandingAbsoluteTrackingPose(); - vr::VR_Shutdown(); - return out; +#endif } + +void ShutdownChaperoneClient() { +#ifndef __APPLE__ + std::unique_lock lock(chaperone_mutex); + + vr::VR_Shutdown(); #endif +} -void SetChaperone(float areaWidth, float areaHeight) { +void SetChaperoneArea(float areaWidth, float areaHeight) { #ifndef __APPLE__ + std::unique_lock lock(chaperone_mutex); + const vr::HmdMatrix34_t MATRIX_IDENTITY = { {{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}}}; @@ -45,16 +53,6 @@ void SetChaperone(float areaWidth, float areaHeight) { perimeterPoints[3][0] = 1.0f * areaWidth; perimeterPoints[3][1] = -1.0f * areaHeight; - std::unique_lock lock(chaperone_mutex); - - vr::EVRInitError error; - vr::VR_Init(&error, vr::VRApplication_Utility); - - if (error != vr::VRInitError_None) { - Warn("Failed to init OpenVR client to update Chaperone boundary! Error: %d", error); - return; - } - vr::VRChaperoneSetup()->RoomSetupStarting(); vr::VRChaperoneSetup()->SetWorkingPerimeter( reinterpret_cast(perimeterPoints), 4); @@ -66,7 +64,21 @@ void SetChaperone(float areaWidth, float areaHeight) { // Hide SteamVR Chaperone vr::VRSettings()->SetFloat( vr::k_pch_CollisionBounds_Section, vr::k_pch_CollisionBounds_FadeDistance_Float, 0.0f); +#endif +} +#ifdef __linux__ +vr::HmdMatrix34_t GetRawZeroPose() { + vr::HmdMatrix34_t out = {}; + std::unique_lock lock(chaperone_mutex); + vr::EVRInitError error; + vr::VR_Init(&error, vr::VRApplication_Utility); + if (error != vr::VRInitError_None) { + Warn("Failed to init OpenVR client to get raw zero pose! Error: %d", error); + return out; + } + out = vr::VRSystem()->GetRawZeroPoseToStandingAbsoluteTrackingPose(); vr::VR_Shutdown(); -#endif -} \ No newline at end of file + return out; +} +#endif \ No newline at end of file diff --git a/alvr/server/cpp/alvr_server/bindings.h b/alvr/server/cpp/alvr_server/bindings.h index 6f4049cf86..69a67b6315 100644 --- a/alvr/server/cpp/alvr_server/bindings.h +++ b/alvr/server/cpp/alvr_server/bindings.h @@ -142,11 +142,14 @@ extern "C" void ShutdownSteamvr(); extern "C" void SetOpenvrProperty(unsigned long long deviceID, FfiOpenvrProperty prop); extern "C" void RegisterButton(unsigned long long buttonID); -extern "C" void SetChaperone(float areaWidth, float areaHeight); extern "C" void SetViewsConfig(FfiViewsConfig config); extern "C" void SetBattery(unsigned long long deviceID, float gauge_value, bool is_plugged); extern "C" void SetButton(unsigned long long buttonID, FfiButtonValue value); +extern "C" void InitChaperoneClient(); +extern "C" void ShutdownChaperoneClient(); +extern "C" void SetChaperoneArea(float areaWidth, float areaHeight); + extern "C" void CaptureFrame(); // NalParsing.cpp diff --git a/alvr/server/src/connection.rs b/alvr/server/src/connection.rs index dd7d633256..ae7744c4e5 100644 --- a/alvr/server/src/connection.rs +++ b/alvr/server/src/connection.rs @@ -925,6 +925,8 @@ fn try_connect(mut client_ips: HashMap) -> ConResult { let control_sender = Arc::clone(&control_sender); let client_hostname = client_hostname.clone(); move || { + unsafe { crate::InitChaperoneClient() }; + let mut disconnection_deadline = Instant::now() + KEEPALIVE_TIMEOUT; while IS_STREAMING.value() { let packet = match control_receiver.recv(STREAMING_RECV_TIMEOUT) { @@ -954,7 +956,7 @@ fn try_connect(mut client_ips: HashMap) -> ConResult { ); let area = packet.unwrap_or(Vec2::new(2.0, 2.0)); - unsafe { crate::SetChaperone(area.x, area.y) }; + unsafe { crate::SetChaperoneArea(area.x, area.y) }; } } ClientControlPacket::RequestIdr => { @@ -1059,6 +1061,7 @@ fn try_connect(mut client_ips: HashMap) -> ConResult { disconnection_deadline = Instant::now() + KEEPALIVE_TIMEOUT; } + unsafe { crate::ShutdownChaperoneClient() }; SERVER_DATA_MANAGER.write().update_client_list( client_hostname, diff --git a/alvr/server/src/lib.rs b/alvr/server/src/lib.rs index 386c109edf..e6686b2153 100644 --- a/alvr/server/src/lib.rs +++ b/alvr/server/src/lib.rs @@ -358,7 +358,11 @@ pub unsafe extern "C" fn HmdDriverFactory( if set_default_chap { // call this when inside a new thread. Calling this on the parent thread will crash // SteamVR - unsafe { SetChaperone(2.0, 2.0) }; + unsafe { + InitChaperoneClient(); + SetChaperoneArea(2.0, 2.0); + ShutdownChaperoneClient(); + } } connection::handshake_loop();