Skip to content

Commit 63c668e

Browse files
committed
fix: ⚡ Reduce delay when setting chaperone
1 parent d9a10d1 commit 63c668e

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,34 @@ namespace alvr_chaperone {
1111
using namespace alvr_chaperone;
1212
#endif
1313

14-
static std::mutex chaperone_mutex;
14+
std::mutex chaperone_mutex;
1515

16-
#ifdef __linux__
17-
vr::HmdMatrix34_t GetRawZeroPose() {
18-
vr::HmdMatrix34_t out = {};
16+
void InitChaperoneClient() {
17+
#ifndef __APPLE__
1918
std::unique_lock<std::mutex> lock(chaperone_mutex);
19+
2020
vr::EVRInitError error;
2121
vr::VR_Init(&error, vr::VRApplication_Utility);
22+
2223
if (error != vr::VRInitError_None) {
23-
Warn("Failed to init OpenVR client to get raw zero pose! Error: %d", error);
24-
return out;
24+
Warn("Failed to init OpenVR client to update Chaperone boundary! Error: %d", error);
25+
return;
2526
}
26-
out = vr::VRSystem()->GetRawZeroPoseToStandingAbsoluteTrackingPose();
27-
vr::VR_Shutdown();
28-
return out;
27+
#endif
2928
}
29+
30+
void ShutdownChaperoneClient() {
31+
#ifndef __APPLE__
32+
std::unique_lock<std::mutex> lock(chaperone_mutex);
33+
34+
vr::VR_Shutdown();
3035
#endif
36+
}
3137

32-
void SetChaperone(float areaWidth, float areaHeight) {
38+
void SetChaperoneArea(float areaWidth, float areaHeight) {
3339
#ifndef __APPLE__
40+
std::unique_lock<std::mutex> lock(chaperone_mutex);
41+
3442
const vr::HmdMatrix34_t MATRIX_IDENTITY = {
3543
{{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}}};
3644

@@ -45,16 +53,6 @@ void SetChaperone(float areaWidth, float areaHeight) {
4553
perimeterPoints[3][0] = 1.0f * areaWidth;
4654
perimeterPoints[3][1] = -1.0f * areaHeight;
4755

48-
std::unique_lock<std::mutex> lock(chaperone_mutex);
49-
50-
vr::EVRInitError error;
51-
vr::VR_Init(&error, vr::VRApplication_Utility);
52-
53-
if (error != vr::VRInitError_None) {
54-
Warn("Failed to init OpenVR client to update Chaperone boundary! Error: %d", error);
55-
return;
56-
}
57-
5856
vr::VRChaperoneSetup()->RoomSetupStarting();
5957
vr::VRChaperoneSetup()->SetWorkingPerimeter(
6058
reinterpret_cast<vr::HmdVector2_t *>(perimeterPoints), 4);
@@ -66,7 +64,21 @@ void SetChaperone(float areaWidth, float areaHeight) {
6664
// Hide SteamVR Chaperone
6765
vr::VRSettings()->SetFloat(
6866
vr::k_pch_CollisionBounds_Section, vr::k_pch_CollisionBounds_FadeDistance_Float, 0.0f);
67+
#endif
68+
}
6969

70+
#ifdef __linux__
71+
vr::HmdMatrix34_t GetRawZeroPose() {
72+
vr::HmdMatrix34_t out = {};
73+
std::unique_lock<std::mutex> lock(chaperone_mutex);
74+
vr::EVRInitError error;
75+
vr::VR_Init(&error, vr::VRApplication_Utility);
76+
if (error != vr::VRInitError_None) {
77+
Warn("Failed to init OpenVR client to get raw zero pose! Error: %d", error);
78+
return out;
79+
}
80+
out = vr::VRSystem()->GetRawZeroPoseToStandingAbsoluteTrackingPose();
7081
vr::VR_Shutdown();
71-
#endif
72-
}
82+
return out;
83+
}
84+
#endif

alvr/server/cpp/alvr_server/bindings.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ extern "C" void ShutdownSteamvr();
142142

143143
extern "C" void SetOpenvrProperty(unsigned long long deviceID, FfiOpenvrProperty prop);
144144
extern "C" void RegisterButton(unsigned long long buttonID);
145-
extern "C" void SetChaperone(float areaWidth, float areaHeight);
146145
extern "C" void SetViewsConfig(FfiViewsConfig config);
147146
extern "C" void SetBattery(unsigned long long deviceID, float gauge_value, bool is_plugged);
148147
extern "C" void SetButton(unsigned long long buttonID, FfiButtonValue value);
149148

149+
extern "C" void InitChaperoneClient();
150+
extern "C" void ShutdownChaperoneClient();
151+
extern "C" void SetChaperoneArea(float areaWidth, float areaHeight);
152+
150153
extern "C" void CaptureFrame();
151154

152155
// NalParsing.cpp

alvr/server/src/connection.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,8 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
925925
let control_sender = Arc::clone(&control_sender);
926926
let client_hostname = client_hostname.clone();
927927
move || {
928+
unsafe { crate::InitChaperoneClient() };
929+
928930
let mut disconnection_deadline = Instant::now() + KEEPALIVE_TIMEOUT;
929931
while IS_STREAMING.value() {
930932
let packet = match control_receiver.recv(STREAMING_RECV_TIMEOUT) {
@@ -954,7 +956,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
954956
);
955957

956958
let area = packet.unwrap_or(Vec2::new(2.0, 2.0));
957-
unsafe { crate::SetChaperone(area.x, area.y) };
959+
unsafe { crate::SetChaperoneArea(area.x, area.y) };
958960
}
959961
}
960962
ClientControlPacket::RequestIdr => {
@@ -1059,6 +1061,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
10591061

10601062
disconnection_deadline = Instant::now() + KEEPALIVE_TIMEOUT;
10611063
}
1064+
unsafe { crate::ShutdownChaperoneClient() };
10621065

10631066
SERVER_DATA_MANAGER.write().update_client_list(
10641067
client_hostname,

alvr/server/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,11 @@ pub unsafe extern "C" fn HmdDriverFactory(
358358
if set_default_chap {
359359
// call this when inside a new thread. Calling this on the parent thread will crash
360360
// SteamVR
361-
unsafe { SetChaperone(2.0, 2.0) };
361+
unsafe {
362+
InitChaperoneClient();
363+
SetChaperoneArea(2.0, 2.0);
364+
ShutdownChaperoneClient();
365+
}
362366
}
363367

364368
connection::handshake_loop();

0 commit comments

Comments
 (0)