Skip to content

Commit

Permalink
fix: ⚡ Reduce delay when setting chaperone
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Nov 8, 2023
1 parent d9a10d1 commit 63c668e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
56 changes: 34 additions & 22 deletions alvr/server/cpp/alvr_server/ChaperoneUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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<std::mutex> lock(chaperone_mutex);

vr::VR_Shutdown();
#endif
}

void SetChaperone(float areaWidth, float areaHeight) {
void SetChaperoneArea(float areaWidth, float areaHeight) {
#ifndef __APPLE__
std::unique_lock<std::mutex> 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}}};

Expand All @@ -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<std::mutex> 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<vr::HmdVector2_t *>(perimeterPoints), 4);
Expand All @@ -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<std::mutex> 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
}
return out;
}
#endif
5 changes: 4 additions & 1 deletion alvr/server/cpp/alvr_server/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion alvr/server/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> 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) {
Expand Down Expand Up @@ -954,7 +956,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> 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 => {
Expand Down Expand Up @@ -1059,6 +1061,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {

disconnection_deadline = Instant::now() + KEEPALIVE_TIMEOUT;
}
unsafe { crate::ShutdownChaperoneClient() };

SERVER_DATA_MANAGER.write().update_client_list(
client_hostname,
Expand Down
6 changes: 5 additions & 1 deletion alvr/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 63c668e

Please sign in to comment.