-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathoxr_session_sharing.cpp
More file actions
140 lines (113 loc) · 5.22 KB
/
oxr_session_sharing.cpp
File metadata and controls
140 lines (113 loc) · 5.22 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
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include <deviceio_session/deviceio_session.hpp>
#include <deviceio_trackers/hand_tracker.hpp>
#include <deviceio_trackers/head_tracker.hpp>
#include <oxr/oxr_session.hpp>
#include <oxr_utils/os_time.hpp>
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
int main(int argc, char** argv)
try
{
std::cout << "OpenXR Session Sharing Example" << std::endl;
std::cout << "================================" << std::endl;
std::cout << std::endl;
// Step 1: Create OpenXR session directly with all required extensions
std::cout << "[Step 1] Creating standalone OpenXR session..." << std::endl;
// Collect extensions needed by our trackers
std::vector<std::string> extensions_vec{
"XR_KHR_convert_timespec_time", // Required for time conversion
"XR_EXT_hand_tracking" // Hand tracking
};
std::cout << " Required extensions:" << std::endl;
for (const auto& ext : extensions_vec)
{
std::cout << " - " << ext << std::endl;
}
auto oxr_session = std::make_shared<core::OpenXRSession>("SessionSharingExample", extensions_vec);
std::cout << " ✓ OpenXR session created" << std::endl;
std::cout << std::endl;
// Step 2: Get handles from the session
std::cout << "[Step 2] Getting session handles..." << std::endl;
auto handles = oxr_session->get_handles();
std::cout << " Instance: " << handles.instance << std::endl;
std::cout << " Session: " << handles.session << std::endl;
std::cout << " Space: " << handles.space << std::endl;
std::cout << std::endl;
// Step 3: Create Manager 1 with HandTracker using the shared session
std::cout << "[Step 3] Creating Manager 1 with HandTracker..." << std::endl;
auto hand_tracker = std::make_shared<core::HandTracker>();
std::vector<std::shared_ptr<core::ITracker>> trackers1 = { hand_tracker };
// run() throws exception on failure
auto session1 = core::DeviceIOSession::run(trackers1, handles);
std::cout << " ✓ Manager 1 using shared session" << std::endl;
std::cout << std::endl;
// Step 4: Create Manager 2 with HeadTracker using the SAME shared session
std::cout << "[Step 4] Creating Manager 2 with HeadTracker..." << std::endl;
auto head_tracker = std::make_shared<core::HeadTracker>();
std::vector<std::shared_ptr<core::ITracker>> trackers2 = { head_tracker };
// run() throws exception on failure
auto session2 = core::DeviceIOSession::run(trackers2, handles);
std::cout << " ✓ Manager 2 using shared session" << std::endl;
std::cout << std::endl;
// Step 5: Update both sessions - they share the same OpenXR session!
std::cout << "[Step 5] Testing both managers with shared session (10 frames)..." << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; ++i)
{
// Both sessions update using the same underlying OpenXR session
const int64_t graph_time_ns = core::os_monotonic_now_ns();
session1->update(graph_time_ns);
session2->update(graph_time_ns);
// Get data from both trackers
const auto& left_tracked = hand_tracker->get_left_hand(*session1);
const auto& head_tracked = head_tracker->get_head(*session2);
if (i % 3 == 0)
{
std::cout << "Frame " << i << ": "
<< "Hands=" << (left_tracked.data ? "ACTIVE" : "INACTIVE") << " | "
<< "Head=" << ((head_tracked.data && head_tracked.data->is_valid) ? "VALID" : "INVALID");
if (head_tracked.data && head_tracked.data->is_valid && head_tracked.data->pose)
{
const auto& pos = head_tracked.data->pose->position();
std::cout << " [" << pos.x() << ", " << pos.y() << ", " << pos.z() << "]";
}
std::cout << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
std::cout << std::endl;
std::cout << "✓ Both managers working with shared OpenXR session!" << std::endl;
std::cout << std::endl;
// Cleanup
std::cout << "[Cleanup]" << std::endl;
std::cout << " Destroying Manager 1..." << std::endl;
session1.reset(); // RAII cleanup
std::cout << " Destroying Manager 2..." << std::endl;
session2.reset(); // RAII cleanup
std::cout << " Destroying shared OpenXR session..." << std::endl;
oxr_session.reset(); // RAII cleanup
std::cout << std::endl;
std::cout << "✓ Session sharing test complete!" << std::endl;
std::cout << std::endl;
std::cout << "Summary:" << std::endl;
std::cout << " ✓ One OpenXR session created" << std::endl;
std::cout << " ✓ Two managers shared the same session" << std::endl;
std::cout << " ✓ HandTracker (Manager 1) and HeadTracker (Manager 2)" << std::endl;
std::cout << " ✓ Both updated successfully with shared session" << std::endl;
std::cout << std::endl;
return 0;
}
catch (const std::exception& e)
{
std::cerr << argv[0] << ": " << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cerr << argv[0] << ": Unknown error occurred" << std::endl;
return 1;
}