diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 3c227e90..ac1bf2de 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -85,8 +85,7 @@ private: std::unique_ptr network_session; std::atomic exiting = false; std::thread network_thread; - std::mutex tracking_control_mutex; - to_headset::tracking_control tracking_control{}; + thread_safe tracking_control{}; std::array, 2> interaction_profiles; // left and right hand std::atomic recenter_requested = false; std::atomic display_time_phase = 0; diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index 1164543f..e02b8719 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -389,8 +389,14 @@ void scenes::stream::gui_performance_metrics() ImPlot::PopStyleColor(5); { - std::lock_guard lock(tracking_control_mutex); - ImGui::Text("%s", fmt::format(_F("Estimated motion to photons latency: {}ms"), std::chrono::duration_cast(tracking_control.max_offset).count()).c_str()); + ImGui::Text( + "%s", + fmt::format( + _F("Estimated motion to photons latency: {}ms"), + std::chrono::duration_cast( + tracking_control.lock()->max_offset) + .count()) + .c_str()); if (is_gui_interactable()) ImGui::Text("%s", _S("Press the grip button to move the window")); @@ -417,7 +423,12 @@ void scenes::stream::gui_compact_view() f(_S("Upload"), 8 * compact_bandwidth_tx * 1e-6, "Mbit/s"); f(_S("CPU time"), compact_cpu_time * 1000, "ms"); f(_S("GPU time"), compact_gpu_time * 1000, "ms"); - f(_S("Motion to photon latency"), std::chrono::duration_cast(tracking_control.max_offset).count() * 1e-3f, "ms"); + f(_S("Motion to photon latency"), + std::chrono::duration_cast( + tracking_control.lock()->max_offset) + .count() * + 1e-3f, + "ms"); ImGui::EndTable(); } } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index afc3c958..9ce2322b 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -433,11 +433,7 @@ void scenes::stream::tracking() timer t(instance); int samples = 0; - to_headset::tracking_control control; - { - std::lock_guard lock(tracking_control_mutex); - control = tracking_control; - } + auto control = *tracking_control.lock(); XrDuration prediction = std::clamp(control.max_offset.count(), 0, 80'000'000); auto period = std::max(display_time_period.load(), 1'000'000); @@ -636,13 +632,13 @@ void scenes::stream::tracking() void scenes::stream::operator()(to_headset::tracking_control && packet) { - std::lock_guard lock(tracking_control_mutex); + auto locked = tracking_control.lock(); auto m = size_t(to_headset::tracking_control::id::microphone); if (audio_handle) audio_handle->set_mic_state(packet.enabled[m]); - tracking_control = packet; - tracking_control.min_offset = std::min(tracking_control.min_offset, tracking_control.max_offset); + *locked = packet; + locked->min_offset = std::min(locked->min_offset, locked->max_offset); } static device_id derived_from(device_id target)