diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 7ae44400..cac6bfcf 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -882,10 +882,19 @@ void scenes::lobby::render(const XrFrameState & frame_state) if (next_scene) { - if (!next_scene->alive()) - next_scene.reset(); - else if (next_scene->current_state() == scenes::stream::state::streaming) - application::push_scene(next_scene); + switch (next_scene->current_state()) + { + case scenes::stream::state::streaming: + application::push_scene(next_scene); + break; + + case scenes::stream::state::shutdown: + current_tab = tab::server_list; + break; + + default: + break; + } } update_server_list(); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 9524db45..873ef795 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -224,6 +224,7 @@ class lobby : public scene_impl void gui_connecting(locked_notifiable & request); void gui_enter_pin(locked_notifiable & request); void gui_connected(XrTime predicted_display_time); + void gui_disconnected(); void gui_server_list(); void gui_new_server(); void gui_settings(); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index cef90804..7037333c 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -249,11 +249,7 @@ void scenes::lobby::gui_enter_pin(locked_notifiable & pin_requ void scenes::lobby::gui_connected(XrTime predicted_display_time) { - if (not next_scene) - { - current_tab = tab::server_list; - return; - } + assert(next_scene); if (next_scene->apps.draw_gui(*imgui_ctx, _("Disconnect")) == app_launcher::Cancel) { @@ -262,6 +258,42 @@ void scenes::lobby::gui_connected(XrTime predicted_display_time) } } +void scenes::lobby::gui_disconnected() +{ + using constants::style::button_size; + std::string close_button_label = _("Close"); + + if (!async_error) + { + async_error = next_scene->pop_stream_error(); + + if (!async_error) + { + next_scene.reset(); + ImGui::CloseCurrentPopup(); + return; + } + } + + ImGui::Dummy({1000, 1}); + + ImGui::PushFont(nullptr, constants::gui::font_size_large); + if (server_name == "") + CenterTextH(fmt::format(_F("Disconnected"))); + else + CenterTextH(fmt::format(_F("Disconnected from {}"), server_name)); + ImGui::PopFont(); + + ImGui::Text("%s", async_error->c_str()); + ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - button_size.x - ImGui::GetStyle().WindowPadding.x); + + if (ImGui::Button(close_button_label.c_str(), button_size)) + { + async_error.reset(); + } + imgui_ctx->vibrate_on_hover(); +} + void scenes::lobby::gui_new_server() { using constants::style::button_size; @@ -496,8 +528,12 @@ void scenes::lobby::gui_server_list() config.save(); } + // Check if we need to report errors after a disconnect + if (next_scene and next_scene->current_state() == scenes::stream::state::shutdown and not ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup)) + ImGui::OpenPopup("disconnected"); + // Check if an automatic connection has started - if ((async_session.valid() || next_scene) and not ImGui::IsPopupOpen("connecting")) + if ((async_session.valid() || next_scene) and not ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup)) ImGui::OpenPopup("connecting"); const auto & popup_layer = imgui_ctx->layers()[1]; @@ -516,6 +552,13 @@ void scenes::lobby::gui_server_list() ImGui::EndPopup(); } + ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + if (ImGui::BeginPopupModal("disconnected", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) + { + gui_disconnected(); + ImGui::EndPopup(); + } + ImGui::SetNextWindowSize({800, 0}); ImGui::SetNextWindowPos({popup_layer_center.x, popup_layer_center.y}, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); if (ImGui::BeginPopupModal("add or edit server", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index b1fa75fa..95998466 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -562,7 +562,7 @@ void scenes::stream::push_blit_handle(shard_accumulator * decoder, std::shared_p if (state_ != state::streaming and not(decoders[0].empty() and decoders[1].empty())) { - state_ = state::streaming; + set_state(state::streaming); spdlog::info("Stream scene ready at t={}", instance.now()); } } @@ -759,7 +759,7 @@ bool scenes::stream::is_gui_interactable() const void scenes::stream::render(const XrFrameState & frame_state) { - if (exiting) + if (state_ == state::shutdown) application::pop_scene(); display_time_phase = frame_state.predictedDisplayTime % frame_state.predictedDisplayPeriod; @@ -768,7 +768,7 @@ void scenes::stream::render(const XrFrameState & frame_state) last_display_time = frame_state.predictedDisplayTime; std::shared_lock lock(decoder_mutex); - if (not frame_state.shouldRender or (decoders[0].empty() and decoders[1].empty()) or exiting) + if (not frame_state.shouldRender or (decoders[0].empty() and decoders[1].empty()) or state_ == state::shutdown) { // TODO: stop/restart video stream session.begin_frame(); @@ -848,7 +848,7 @@ void scenes::stream::render(const XrFrameState & frame_state) blit_handle->feedback.blitted = instance.now(); if (blit_handle->feedback.blitted - blit_handle->feedback.received_from_decoder > 1'000'000'000) - state_ = stream::state::stalled; + set_state(state::stalled); ++blit_handle->feedback.times_displayed; blit_handle->feedback.displayed = frame_state.predictedDisplayTime; @@ -1136,7 +1136,7 @@ void scenes::stream::render(const XrFrameState & frame_state) void scenes::stream::exit() { - exiting = true; + state_ = state::shutdown; } void scenes::stream::setup(const to_headset::video_stream_description & description) @@ -1261,6 +1261,18 @@ scene::meta & scenes::stream::get_meta_scene() return m; } +std::optional scenes::stream::pop_stream_error() +{ + auto queue = stream_error_queue.lock(); + if (queue->empty()) + return std::nullopt; + + auto err = std::make_optional(queue->front()); + queue->pop(); + + return err; +} + void scenes::stream::on_xr_event(const xr::event & event) { switch (event.header.type) diff --git a/client/scenes/stream.h b/client/scenes/stream.h index e83f1bc0..75d9e2ae 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -32,6 +32,7 @@ #include "wivrn_packets.h" #include "xr/space.h" #include +#include #include #include #include @@ -45,7 +46,8 @@ public: { initializing, streaming, - stalled + stalled, + shutdown, }; static const size_t image_buffer_size = 3; @@ -72,7 +74,6 @@ private: std::array, decoder_count> common_frame(XrTime display_time); std::unique_ptr network_session; - std::atomic exiting = false; std::thread network_thread; thread_safe tracking_control{}; std::array, 2> interaction_profiles; // left and right hand @@ -104,7 +105,16 @@ private: std::unordered_multimap haptics_actions; std::vector> input_actions; - state state_ = state::initializing; + std::atomic state_ = state::initializing; + + void set_state(state new_state) + { + state prev = state_; + if (prev == state::shutdown) + return; + + state_.compare_exchange_strong(prev, new_state); + } xr::swapchain swapchain; @@ -136,7 +146,7 @@ private: struct gui_toast { - std::string current_toast; + std::string content; bool is_urgent = false; }; @@ -150,6 +160,8 @@ private: thread_safe> gui_toast; std::atomic gui_status_last_change; + thread_safe> stream_error_queue; + XrAction plots_toggle_1 = XR_NULL_HANDLE; XrAction plots_toggle_2 = XR_NULL_HANDLE; XrAction recenter_left = XR_NULL_HANDLE; @@ -230,14 +242,10 @@ public: } void exit(); - bool alive() const - { - return !exiting; - } - void start_application(std::string appid); static meta & get_meta_scene(); + std::optional pop_stream_error(); private: void process_packets(); diff --git a/client/scenes/stream_gui.cpp b/client/scenes/stream_gui.cpp index b4fa3477..04a6c121 100644 --- a/client/scenes/stream_gui.cpp +++ b/client/scenes/stream_gui.cpp @@ -739,7 +739,7 @@ void scenes::stream::gui_toasts() return; } - ImGui::Text("%s", (*toast)->current_toast.c_str()); + ImGui::Text("%s", (*toast)->content.c_str()); } void scenes::stream::draw_gui(XrTime predicted_display_time, XrDuration predicted_display_period) diff --git a/client/scenes/stream_network.cpp b/client/scenes/stream_network.cpp index 5f772879..96fe64be 100644 --- a/client/scenes/stream_network.cpp +++ b/client/scenes/stream_network.cpp @@ -33,7 +33,7 @@ void scenes::stream::process_packets() #ifdef __ANDROID__ application::instance().setup_jni(); #endif - while (not exiting) + while (state_ != state::shutdown) { try { @@ -51,13 +51,21 @@ void scenes::stream::operator()(to_headset::server_message && message) { switch (message.kind) { - case to_headset::server_message::kind::stream_toast: - case to_headset::server_message::kind::stream_toast_urgent: + case to_headset::server_message::kind::toast: + case to_headset::server_message::kind::toast_urgent: { auto toast = gui_toast.lock(); - toast->emplace(message.msg, message.kind == to_headset::server_message::kind::stream_toast_urgent); + toast->emplace(message.msg, message.kind == to_headset::server_message::kind::toast_urgent); gui_status_last_change = instance.now(); break; + } + + case to_headset::server_message::kind::error: { + auto queue = stream_error_queue.lock(); + queue->emplace(std::move(message.msg)); + + break; + } } } diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 5ba77581..fac49cb7 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -341,7 +341,7 @@ void scenes::stream::tracking() XrDuration frame_duration; XrTime pattern_begin = instance.now(); - while (not exiting) + while (state_ != state::shutdown) { try { diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 578ac5cc..1a814e31 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -612,8 +612,12 @@ struct server_message { enum class kind : uint8_t { - stream_toast, - stream_toast_urgent, + // in-stream toasts (not buffered) + toast, + toast_urgent, + + // displayed in lobby after disconnect (buffered) + error, }; kind kind; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index f9e91709..86c97a48 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -1182,9 +1182,14 @@ void wivrn_session::reconnect(std::stop_token stop) const auto & info = connection->info(); const auto [warn_conn, refuse_conn] = validate_headset_info(info); + // TODO: add server-side localization + if (refuse_conn) { - // FIXME: send a message to client and prompt the user to restart the server + send_control(to_headset::server_message{ + .kind = to_headset::server_message::kind::error, + .msg = "Headset incompatible with session.", + }); connection->shutdown(); throw std::runtime_error("headset config incompatible with current session"); @@ -1192,13 +1197,12 @@ void wivrn_session::reconnect(std::stop_token stop) if (warn_conn) { - U_LOG_W("Session resumed with outdated settings. Restart the server for settings to apply."); - - // TODO: add server-side localization send_control(to_headset::server_message{ - .kind = to_headset::server_message::kind::stream_toast_urgent, + .kind = to_headset::server_message::kind::toast_urgent, .msg = "Stream resumed with outdated settings. Restart the server for settings to apply.", }); + + U_LOG_W("Session resumed with outdated settings. Restart the server for settings to apply."); } // update headset info