-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvrchat_osc.cpp
More file actions
152 lines (125 loc) · 5.91 KB
/
vrchat_osc.cpp
File metadata and controls
152 lines (125 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// MIT License
//
// Copyright(c) 2022-2026 Matthieu Bucchianeri
// Copyright(c) 2025 Tymon Lindell (Ridge)
// Copyright(c) 2026 Bevergames2018
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this softwareand associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "pch.h"
#include <log.h>
#include <util.h>
#include "trackers.h"
#include <osc/OscReceivedElements.h>
#include <osc/OscPacketListener.h>
#include <ip/UdpSocket.h>
namespace {
using namespace logging;
using namespace trackers;
struct VRChatOSCEyeTracker : IEyeTracker, osc::OscPacketListener {
// VRChat's packets run over port 9000. This can be set to other ports if the software supports, we're using
// port 9020 here.
VRChatOSCEyeTracker() {
wil::unique_handle mutex;
*mutex.put() = OpenMutexW(SYNCHRONIZE, FALSE, L"Local\\baballonia-unique-id");
if (!mutex) {
TraceLoggingWrite(g_traceProvider, "VRChatOSCEyeTracker_NoBaballoniaService");
throw EyeTrackerNotSupportedException();
}
m_socket =
std::make_unique<UdpListeningReceiveSocket>(IpEndpointName(IpEndpointName::ANY_ADDRESS, 9020), this);
}
~VRChatOSCEyeTracker() override {
if (m_started) {
m_socket->AsynchronousBreak();
m_listeningThread.join();
}
}
void start() override {
m_listeningThread = std::thread([&]() { m_socket->Run(); });
m_started = true;
}
void stop() override {
}
bool getGaze(vr::HmdVector2_t& gaze) override {
std::unique_lock lock(m_mutex);
const auto now = std::chrono::high_resolution_clock::now();
if ((now - m_lastReceivedTime).count() > 1'000'000'000) {
return false;
}
gaze.v[0] = m_latestGaze.v[0];
gaze.v[1] = m_latestGaze.v[1];
return true;
}
std::string getType() const override {
return "VRChat OSC";
}
void ProcessMessage(const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint) override {
try {
if (std::string_view(m.AddressPattern()) == "/tracking/eye/LeftRightPitchYaw") {
const auto now = std::chrono::high_resolution_clock::now();
osc::ReceivedMessageArgumentStream args = m.ArgumentStream();
float leftPitch;
float leftYaw;
float rightPitch;
float rightYaw;
args >> leftPitch >> leftYaw >> rightPitch >> rightYaw >> osc::EndMessage;
// Convert degrees to radians for trigonometric functions
// Need to invert pitch because that's what mbucchia's code wants
const float leftPitchRad = leftPitch * (float)M_PI / 180.0f * -1.0f;
const float leftYawRad = leftYaw * (float)M_PI / 180.0f;
const float rightPitchRad = rightPitch * (float)M_PI / 180.0f * -1.0f;
const float rightYawRad = rightYaw * (float)M_PI / 180.0f;
vr::HmdVector3_t unitVector = {
(sin(leftYawRad) * cos(leftPitchRad) + sin(rightYawRad) * cos(rightPitchRad)) / 2,
(sin(leftPitchRad) + sin(rightPitchRad)) / 2,
(-cos(leftYawRad) * cos(leftPitchRad) - cos(rightYawRad) * cos(rightPitchRad)) / 2};
TraceLoggingWrite(g_traceProvider,
"VRChatOSCEyeTracker_ProcessMessage",
TLArg(leftPitch, "LeftPitch"),
TLArg(leftYaw, "LeftYaw"),
TLArg(rightPitch, "RightPitch"),
TLArg(rightYaw, "RightYaw"));
if (!(std::isnan(leftPitch) || std::isnan(leftYaw) || std::isnan(rightPitch) ||
std::isnan(rightYaw))) {
std::unique_lock lock(m_mutex);
m_latestGaze = unitVector;
m_lastReceivedTime = now;
}
}
} catch (osc::Exception& e) {
TraceLoggingWrite(g_traceProvider, "VRChatOSCEyeTracker_ProcessMessage", TLArg(e.what(), "Error"));
}
}
bool m_started{false};
std::thread m_listeningThread;
std::unique_ptr<UdpListeningReceiveSocket> m_socket;
mutable std::mutex m_mutex;
vr::HmdVector3_t m_latestGaze{};
std::chrono::high_resolution_clock::time_point m_lastReceivedTime{};
};
} // namespace
namespace trackers {
std::unique_ptr<IEyeTracker> createVRChatOSCEyeTracker() {
try {
return std::make_unique<VRChatOSCEyeTracker>();
} catch (EyeTrackerNotSupportedException&) {
return {};
}
}
} // namespace trackers