Skip to content

Commit

Permalink
refactor(server_openvr): ♻️ Refactor around TrackedDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Oct 26, 2024
1 parent 1ece87d commit 8bf7d1c
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 220 deletions.
83 changes: 17 additions & 66 deletions alvr/server_openvr/cpp/alvr_server/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@
#include <cstring>
#include <string_view>

vr::ETrackedDeviceClass Controller::getControllerDeviceClass() {
if (Settings::Instance().m_controllerIsTracker)
return vr::TrackedDeviceClass_GenericTracker;
return vr::TrackedDeviceClass_Controller;
}

Controller::Controller(uint64_t deviceID, vr::EVRSkeletalTrackingLevel skeletonLevel)
: TrackedDevice(deviceID)
: TrackedDevice(
deviceID,
Settings::Instance().m_controllerIsTracker ? vr::TrackedDeviceClass_GenericTracker
: vr::TrackedDeviceClass_Controller
)
, m_skeletonLevel(skeletonLevel) {
Debug("Controller::constructor deviceID=%llu", deviceID);

m_pose = vr::DriverPose_t {};
m_pose.poseIsValid = false;
m_pose.deviceIsConnected = false;
m_pose.result = vr::TrackingResult_Uninitialized;

m_pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qWorldFromDriverRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qRotation = HmdQuaternion_Init(1, 0, 0, 0);
}

//
// ITrackedDeviceServerDriver
//

vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
bool Controller::activate() {
Debug("Controller::Activate deviceID=%llu", this->device_id);

auto vr_properties = vr::VRProperties();
auto vr_driver_input = vr::VRDriverInput();

this->object_id = unObjectId;
this->prop_container = vr_properties->TrackedDeviceToPropertyContainer(this->object_id);

SetOpenvrProps(this->device_id);

RegisterButtons(this->device_id);
Expand Down Expand Up @@ -120,38 +102,9 @@ vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
);
}

return vr::VRInitError_None;
}

void Controller::Deactivate() {
Debug("Controller::Deactivate deviceID=%llu", this->device_id);
this->object_id = vr::k_unTrackedDeviceIndexInvalid;
}

void Controller::EnterStandby() { }

void* Controller::GetComponent(const char* pchComponentNameAndVersion) {
Debug(
"Controller::GetComponent deviceID=%llu Name=%hs",
this->device_id,
pchComponentNameAndVersion
);

return NULL;
return true;
}

void PowerOff() { }

/** debug request from a client */
void Controller::DebugRequest(
const char* /*pchRequest*/, char* pchResponseBuffer, uint32_t unResponseBufferSize
) {
if (unResponseBufferSize >= 1)
pchResponseBuffer[0] = 0;
}

vr::DriverPose_t Controller::GetPose() { return m_pose; }

vr::VRInputComponentHandle_t Controller::getHapticComponent() { return m_compHaptic; }

void Controller::RegisterButton(uint64_t id) {
Expand Down Expand Up @@ -190,7 +143,7 @@ void Controller::RegisterButton(uint64_t id) {
void Controller::SetButton(uint64_t id, FfiButtonValue value) {
Debug("Controller::SetButton deviceID=%llu buttonID=%llu", this->device_id, id);

if (!this->isEnabled()) {
if (!this->last_pose.poseIsValid) {
return;
}

Expand Down Expand Up @@ -220,7 +173,7 @@ void Controller::SetButton(uint64_t id, FfiButtonValue value) {
}
}

bool Controller::onPoseUpdate(uint64_t targetTimestampNs, float predictionS, FfiHandData handData) {
bool Controller::OnPoseUpdate(uint64_t targetTimestampNs, float predictionS, FfiHandData handData) {
if (this->object_id == vr::k_unTrackedDeviceIndexInvalid) {
return false;
}
Expand Down Expand Up @@ -287,14 +240,15 @@ bool Controller::onPoseUpdate(uint64_t targetTimestampNs, float predictionS, Ffi
double linearVelocity[3] = { 0.0, 0.0, 0.0 };
vr::HmdVector3d_t angularVelocity = { 0.0, 0.0, 0.0 };

if (m_pose.poseIsValid) {
if (this->last_pose.poseIsValid) {
double dt = ((double)targetTimestampNs - (double)m_poseTargetTimestampNs) / NS_PER_S;

if (dt > 0.0) {
linearVelocity[0] = (pose.vecPosition[0] - m_pose.vecPosition[0]) / dt;
linearVelocity[1] = (pose.vecPosition[1] - m_pose.vecPosition[1]) / dt;
linearVelocity[2] = (pose.vecPosition[2] - m_pose.vecPosition[2]) / dt;
angularVelocity = AngularVelocityBetweenQuats(m_pose.qRotation, pose.qRotation, dt);
linearVelocity[0] = (pose.vecPosition[0] - this->last_pose.vecPosition[0]) / dt;
linearVelocity[1] = (pose.vecPosition[1] - this->last_pose.vecPosition[1]) / dt;
linearVelocity[2] = (pose.vecPosition[2] - this->last_pose.vecPosition[2]) / dt;
angularVelocity
= AngularVelocityBetweenQuats(this->last_pose.qRotation, pose.qRotation, dt);
}
}

Expand All @@ -309,12 +263,9 @@ bool Controller::onPoseUpdate(uint64_t targetTimestampNs, float predictionS, Ffi

pose.poseTimeOffset = predictionS;

m_pose = pose;
m_poseTargetTimestampNs = targetTimestampNs;
this->submit_pose(pose);

vr::VRServerDriverHost()->TrackedDevicePoseUpdated(
this->object_id, pose, sizeof(vr::DriverPose_t)
);
m_poseTargetTimestampNs = targetTimestampNs;

// Early return to skip updating the skeleton
if (!enabled) {
Expand Down
42 changes: 8 additions & 34 deletions alvr/server_openvr/cpp/alvr_server/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,13 @@
#include "openvr_driver.h"
#include <map>

class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {
class Controller : public TrackedDevice {
public:
Controller(uint64_t deviceID, vr::EVRSkeletalTrackingLevel skeletonLevel);

virtual ~Controller() {};

//
// ITrackedDeviceServerDriver
//

virtual vr::EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId);

virtual void Deactivate();

virtual void EnterStandby();

void* GetComponent(const char* pchComponentNameAndVersion);

virtual void PowerOff() {};

/** debug request from a client */
virtual void
DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize);

virtual vr::DriverPose_t GetPose();

vr::VRInputComponentHandle_t getHapticComponent();

void RegisterButton(uint64_t id);

void SetButton(uint64_t id, FfiButtonValue value);

bool onPoseUpdate(uint64_t targetTimestampNs, float predictionS, FfiHandData handData);

void GetBoneTransform(bool withController, vr::VRBoneTransform_t outBoneTransform[]);

vr::ETrackedDeviceClass getControllerDeviceClass();
bool OnPoseUpdate(uint64_t targetTimestampNs, float predictionS, FfiHandData handData);

private:
static const int SKELETON_BONE_COUNT = 31;
Expand All @@ -53,7 +23,6 @@ class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {
vr::VRInputComponentHandle_t m_compSkeleton = vr::k_ulInvalidInputComponentHandle;
vr::EVRSkeletalTrackingLevel m_skeletonLevel;

vr::DriverPose_t m_pose;
uint64_t m_poseTargetTimestampNs;

// These variables are used for controller hand animation
Expand All @@ -67,5 +36,10 @@ class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {
float m_triggerValue = 0;
float m_gripValue = 0;

bool isEnabled() { return m_pose.deviceIsConnected; }
vr::VRInputComponentHandle_t getHapticComponent();
void GetBoneTransform(bool withController, vr::VRBoneTransform_t outBoneTransform[]);

// TrackedDevice
bool activate() final;
void* get_component(const char* component_name_and_version) final { return nullptr; }
};
54 changes: 15 additions & 39 deletions alvr/server_openvr/cpp/alvr_server/HMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ vr::HmdRect2_t fov_to_projection(FfiFov fov) {
}

Hmd::Hmd()
: TrackedDevice(HEAD_ID)
: TrackedDevice(
HEAD_ID,
Settings::Instance().m_TrackingRefOnly ? vr::TrackedDeviceClass_TrackingReference
: vr::TrackedDeviceClass_HMD
)
, m_baseComponentsInitialized(false)
, m_streamComponentsInitialized(false) {
Debug("Hmd::constructor");
Expand All @@ -44,20 +48,8 @@ Hmd::Hmd()
this->views_config.fov[0] = dummy_fov;
this->views_config.fov[1] = dummy_fov;

m_pose = vr::DriverPose_t {};
m_pose.poseIsValid = true;
m_pose.result = vr::TrackingResult_Running_OK;
m_pose.deviceIsConnected = true;
m_pose.qWorldFromDriverRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qRotation = HmdQuaternion_Init(1, 0, 0, 0);

m_poseHistory = std::make_shared<PoseHistory>();

m_deviceClass = Settings::Instance().m_TrackingRefOnly
? vr::TrackedDeviceClass_TrackingReference
: vr::TrackedDeviceClass_HMD;

if (Settings::Instance().m_enableViveTrackerProxy) {
m_viveTrackerProxy = std::make_unique<ViveTrackerProxy>(*this);
if (!vr::VRServerDriverHost()->TrackedDeviceAdded(
Expand Down Expand Up @@ -87,14 +79,11 @@ Hmd::~Hmd() {
#endif
}

vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
bool Hmd::activate() {
Debug("Hmd::Activate");

auto vr_properties = vr::VRProperties();

this->object_id = unObjectId;
this->prop_container = vr_properties->TrackedDeviceToPropertyContainer(this->object_id);

SetOpenvrProps(this->device_id);

vr_properties->SetFloatProperty(
Expand Down Expand Up @@ -133,7 +122,7 @@ vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
if (!m_baseComponentsInitialized) {
m_baseComponentsInitialized = true;

if (IsHMD()) {
if (this->device_class == vr::TrackedDeviceClass_HMD) {
#ifdef _WIN32
m_D3DRender = std::make_shared<CD3DRender>();

Expand All @@ -150,13 +139,13 @@ vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
"graphics cards.\n",
Settings::Instance().m_nAdapterIndex
);
return vr::VRInitError_Driver_Failed;
return false;
}

int32_t nDisplayAdapterIndex;
if (!m_D3DRender->GetAdapterInfo(&nDisplayAdapterIndex, m_adapterName)) {
Error("Failed to get primary adapter info!\n");
return vr::VRInitError_Driver_Failed;
return false;
}

Info("Using %ls as primary graphics adapter.\n", m_adapterName.c_str());
Expand All @@ -167,28 +156,21 @@ vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
#endif
}

DriverReadyIdle(IsHMD());
DriverReadyIdle(this->device_class == vr::TrackedDeviceClass_HMD);
}

if (IsHMD()) {
if (this->device_class == vr::TrackedDeviceClass_HMD) {
vr::VREvent_Data_t eventData;
eventData.ipd = { 0.063 };
vr::VRServerDriverHost()->VendorSpecificEvent(
this->object_id, vr::VREvent_IpdChanged, eventData, 0
);
}

return vr::VRInitError_None;
}

void Hmd::Deactivate() {
Debug("Hmd::Deactivate");

this->object_id = vr::k_unTrackedDeviceIndexInvalid;
this->prop_container = vr::k_ulInvalidPropertyContainer;
return true;
}

void* Hmd::GetComponent(const char* component_name_and_version) {
void* Hmd::get_component(const char* component_name_and_version) {
Debug("Hmd::GetComponent %s", component_name_and_version);

// NB: "this" pointer needs to be statically cast to point to the correct vtable
Expand All @@ -207,8 +189,6 @@ void* Hmd::GetComponent(const char* component_name_and_version) {
return nullptr;
}

vr::DriverPose_t Hmd::GetPose() { return m_pose; }

void Hmd::OnPoseUpdated(uint64_t targetTimestampNs, FfiDeviceMotion motion) {
Debug("Hmd::OnPoseUpdated");

Expand All @@ -231,14 +211,10 @@ void Hmd::OnPoseUpdated(uint64_t targetTimestampNs, FfiDeviceMotion motion) {
pose.vecPosition[1] = motion.position[1];
pose.vecPosition[2] = motion.position[2];

m_pose = pose;
this->submit_pose(pose);

m_poseHistory->OnPoseUpdated(targetTimestampNs, motion);

vr::VRServerDriverHost()->TrackedDevicePoseUpdated(
this->object_id, pose, sizeof(vr::DriverPose_t)
);

if (m_viveTrackerProxy)
m_viveTrackerProxy->update();

Expand Down Expand Up @@ -266,7 +242,7 @@ void Hmd::StartStreaming() {
}

// Spin up a separate thread to handle the overlapped encoding/transmit step.
if (IsHMD()) {
if (this->device_class == vr::TrackedDeviceClass_HMD) {
#ifdef _WIN32
m_encoder = std::make_shared<CEncoder>();
try {
Expand Down
Loading

0 comments on commit 8bf7d1c

Please sign in to comment.