From b72acd87fc8e20936d75ce0f4f181d6012bc9e41 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 21 Apr 2024 11:19:09 +0200 Subject: [PATCH] Refactor client configuration --- client/application.cpp | 11 +++ client/application.h | 9 ++ client/configuration.cpp | 159 ++++++++++++++++++++++++++++++++++++ client/configuration.h | 55 +++++++++++++ client/main.cpp | 2 +- client/scenes/lobby.cpp | 151 +++------------------------------- client/scenes/lobby.h | 22 +---- client/scenes/lobby_gui.cpp | 39 +++++---- client/scenes/stream.cpp | 8 +- client/scenes/stream.h | 3 +- 10 files changed, 272 insertions(+), 187 deletions(-) create mode 100644 client/configuration.cpp create mode 100644 client/configuration.h diff --git a/client/application.cpp b/client/application.cpp index 18f09f3b..26251a67 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -809,6 +809,17 @@ void application::initialize() world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE); // world_space = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_LOCAL); + config.emplace(xr_system_id); + try + { + xr_session.set_refresh_rate(config->preferred_refresh_rate); + } + catch (std::exception & e) + { + spdlog::warn("failed to set refresh rate to {}: {}", config->preferred_refresh_rate, e.what()); + config->preferred_refresh_rate = 0; + } + if (hand_tracking_supported) { left_hand = xr_session.create_hand_tracker(XR_HAND_LEFT_EXT); diff --git a/client/application.h b/client/application.h index 92db7de1..5853f102 100644 --- a/client/application.h +++ b/client/application.h @@ -19,6 +19,7 @@ #pragma once +#include "configuration.h" #include "xr/hand_tracker.h" #ifdef __ANDROID__ #include @@ -134,6 +135,8 @@ class application : public singleton std::weak_ptr last_scene; std::chrono::nanoseconds last_scene_cpu_time; + std::optional config; + void loop(); std::mutex debug_report_mutex; @@ -405,4 +408,10 @@ public: { return instance().xr_extensions; } + + static configuration & get_config() + { + assert(instance().config); + return *instance().config; + } }; diff --git a/client/configuration.cpp b/client/configuration.cpp new file mode 100644 index 00000000..c959dcf4 --- /dev/null +++ b/client/configuration.cpp @@ -0,0 +1,159 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "configuration.h" + +#include "application.h" +#include +#include + +static std::string json_string(const std::string & in) +{ + std::string out; + out.reserve(in.size() + 2); + + out += '"'; + + for (char c: in) + { + switch (c) + { + case '\b': + out += "\\b"; + break; + + case '\f': + out += "\\f"; + break; + + case '\n': + out += "\\n"; + break; + + case '\r': + out += "\\r"; + break; + + case '\t': + out += "\\t"; + break; + + case '"': + out += "\\\""; + break; + + case '\\': + out += "\\\\"; + break; + + default: + out += c; + break; + } + } + + out += '"'; + + return out; +} + +configuration::configuration(xr::system & system) +{ + passthrough_enabled = system.passthrough_supported() == xr::system::passthrough_type::color; + try + { + *this = configuration(application::get_config_path() / "client.json"); + if (system.passthrough_supported() == xr::system::passthrough_type::no_passthrough) + passthrough_enabled = false; + } + catch (std::exception & e) + { + spdlog::warn("Cannot read configuration: {}", e.what()); + } +} + +configuration::configuration(const std::string & path) +{ + simdjson::dom::parser parser; + simdjson::dom::element root = parser.load(path); + for (simdjson::dom::object i: simdjson::dom::array(root["servers"])) + { + server_data data{ + .autoconnect = i["autoconnect"].get_bool(), + .manual = i["manual"].get_bool(), + .visible = false, + .compatible = true, + .service = { + .name = (std::string)i["pretty_name"], + .hostname = (std::string)i["hostname"], + .port = (int)i["port"].get_int64(), + .txt = {{"cookie", (std::string)i["cookie"]}} + + }}; + servers.emplace(data.service.txt["cookie"], data); + } + + if (auto val = root["show_performance_metrics"]; val.is_bool()) + show_performance_metrics = val.get_bool(); + + if (auto val = root["preferred_refresh_rate"]; val.is_double()) + { + preferred_refresh_rate = val.get_double(); + } + + if (auto val = root["microphone"]; val.is_bool()) + microphone = val.get_bool(); + + if (auto val = root["passthrough_enabled"]; val.is_bool()) + passthrough_enabled = val.get_bool(); +} + +void configuration::save() +{ + std::stringstream ss; + + for (auto & [cookie, server_data]: servers) + { + if (server_data.autoconnect || server_data.manual) + { + ss << "{"; + ss << "\"autoconnect\":" << std::boolalpha << server_data.autoconnect << ","; + ss << "\"manual\":" << std::boolalpha << server_data.manual << ","; + ss << "\"pretty_name\":" << json_string(server_data.service.name) << ","; + ss << "\"hostname\":" << json_string(server_data.service.hostname) << ","; + ss << "\"port\":" << server_data.service.port << ","; + ss << "\"cookie\":" << json_string(cookie); + ss << "},"; + } + } + + std::string servers_str = ss.str(); + if (servers_str != "") + servers_str.pop_back(); // Remove last comma + + std::ofstream json(application::get_config_path() / "client.json"); + + json << "{\"servers\":[" << servers_str << "]," + << "\"show_performance_metrics\":" << std::boolalpha << show_performance_metrics; + if (preferred_refresh_rate != 0.) + json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate; + json << ",\"microphone\":" << std::boolalpha << microphone; + json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled; + json << "}"; +} diff --git a/client/configuration.h b/client/configuration.h new file mode 100644 index 00000000..01124c3a --- /dev/null +++ b/client/configuration.h @@ -0,0 +1,55 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "wivrn_discover.h" + +namespace xr +{ +class system; +} + +class configuration +{ +public: + struct server_data + { + bool autoconnect; + bool manual; + bool visible; + bool compatible; + + wivrn_discover::service service; + }; + + std::map servers; + float preferred_refresh_rate = 0; + bool show_performance_metrics = false; + bool microphone = false; + bool passthrough_enabled = false; + +private: + configuration(const std::string &); + +public: + configuration(xr::system &); + + void save(); +}; diff --git a/client/main.cpp b/client/main.cpp index ac9a2302..74315c89 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -107,7 +107,7 @@ void real_main() freeaddrinfo(addresses); if (not session) throw std::runtime_error("Unable to connect to " + server_address + ":" + std::to_string(port)); - app.push_scene(scenes::stream::create(std::move(session), false, true)); + app.push_scene(scenes::stream::create(std::move(session))); } app.run(); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 56aaf3bd..023fb97a 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -36,13 +36,10 @@ #include "wivrn_discover.h" #include -#include #include -#include #include #include #include -#include #include #include #include @@ -93,56 +90,6 @@ static std::string choose_webxr_profile() __builtin_unreachable(); } -static std::string json_string(const std::string & in) -{ - std::string out; - out.reserve(in.size() + 2); - - out += '"'; - - for (char c: in) - { - switch (c) - { - case '\b': - out += "\\b"; - break; - - case '\f': - out += "\\f"; - break; - - case '\n': - out += "\\n"; - break; - - case '\r': - out += "\\r"; - break; - - case '\t': - out += "\\t"; - break; - - case '"': - out += "\\\""; - break; - - case '\\': - out += "\\\\"; - break; - - default: - out += c; - break; - } - } - - out += '"'; - - return out; -} - static const std::array supported_formats = { vk::Format::eR8G8B8A8Srgb, vk::Format::eB8G8R8A8Srgb, @@ -165,55 +112,8 @@ scenes::lobby::lobby() force_autoconnect = true; passthrough_supported = system.passthrough_supported(); - passthrough_enabled = passthrough_supported == xr::system::passthrough_type::color; - - try - { - simdjson::dom::parser parser; - simdjson::dom::element root = parser.load(application::get_config_path() / "client.json"); - for (simdjson::dom::object i: simdjson::dom::array(root["servers"])) - { - server_data data{ - .autoconnect = i["autoconnect"].get_bool(), - .manual = i["manual"].get_bool(), - .visible = false, - .compatible = true, - .service = { - .name = (std::string)i["pretty_name"], - .hostname = (std::string)i["hostname"], - .port = (int)i["port"].get_int64(), - .txt = {{"cookie", (std::string)i["cookie"]}} - - }}; - servers.emplace(data.service.txt["cookie"], data); - } - - auto json_show_plots = root["show_performance_metrics"]; - if (json_show_plots.is_bool()) - show_performance_metrics = json_show_plots.get_bool(); - - auto rate = root["preferred_refresh_rate"]; - if (rate.is_double()) - { - preferred_refresh_rate = rate.get_double(); - session.set_refresh_rate(preferred_refresh_rate); - } - - auto mic = root["microphone"]; - if (mic.is_bool()) - microphone = mic.get_bool(); - - auto passthrough = root["passthrough_enabled"]; - if (passthrough.is_bool()) - passthrough_enabled = passthrough.get_bool(); - } - catch (std::exception & e) - { - spdlog::warn("Cannot read configuration: {}", e.what()); - servers.clear(); - show_performance_metrics = false; - } + auto & servers = application::get_config().servers; spdlog::info("{} known server(s):", servers.size()); for (auto & i: servers) { @@ -245,40 +145,6 @@ scenes::lobby::lobby() spdlog::info("Using format {}", vk::to_string(swapchain_format)); } -void scenes::lobby::save_config() -{ - std::stringstream ss; - - for (auto & [cookie, server_data]: servers) - { - if (server_data.autoconnect || server_data.manual) - { - ss << "{"; - ss << "\"autoconnect\":" << std::boolalpha << server_data.autoconnect << ","; - ss << "\"manual\":" << std::boolalpha << server_data.manual << ","; - ss << "\"pretty_name\":" << json_string(server_data.service.name) << ","; - ss << "\"hostname\":" << json_string(server_data.service.hostname) << ","; - ss << "\"port\":" << server_data.service.port << ","; - ss << "\"cookie\":" << json_string(cookie); - ss << "},"; - } - } - - std::string servers_str = ss.str(); - if (servers_str != "") - servers_str.pop_back(); // Remove last comma - - std::ofstream json(application::get_config_path() / "client.json"); - - json << "{\"servers\":[" << servers_str << "]," - << "\"show_performance_metrics\":" << std::boolalpha << show_performance_metrics; - if (preferred_refresh_rate != 0.) - json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate; - json << ",\"microphone\":" << std::boolalpha << microphone; - json << ",\"passthrough_enabled\":" << std::boolalpha << passthrough_enabled; - json << "}"; -} - static std::string ip_address_to_string(const in_addr & addr) { char buf[100]; @@ -413,6 +279,7 @@ void scenes::lobby::update_server_list() std::vector discovered_services = discover->get_services(); // TODO: only if discovered_services changed + auto & servers = application::get_config().servers; for (auto & [cookie, data]: servers) { data.visible = false; @@ -448,7 +315,7 @@ void scenes::lobby::update_server_list() // Newly discovered server: add it to the list servers.emplace( cookie, - server_data{ + configuration::server_data{ .autoconnect = false, .manual = false, .visible = true, @@ -465,7 +332,7 @@ void scenes::lobby::update_server_list() } } -void scenes::lobby::connect(server_data & data) +void scenes::lobby::connect(const configuration::server_data & data) { server_name = data.service.name; async_error.reset(); @@ -614,7 +481,7 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) { auto session = async_session.get(); if (session) - next_scene = stream::create(std::move(session), show_performance_metrics, microphone); + next_scene = stream::create(std::move(session)); async_session.reset(); } @@ -639,6 +506,7 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) imgui_ctx->set_current(); if (!async_session.valid() && !next_scene && !ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup)) { + const auto & servers = application::get_config().servers; for (auto && [cookie, data]: servers) { if (data.visible && (data.autoconnect || force_autoconnect) && data.compatible) @@ -713,14 +581,14 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) assert(renderer); renderer->start_frame(); std::vector lobby_layer_views; - if (!passthrough_enabled) + if (not application::get_config().passthrough_enabled) lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1}); auto controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0}); renderer->end_frame(); // After end_frame because the command buffers are submitted in end_frame - if (!passthrough_enabled) + if (not application::get_config().passthrough_enabled) { for (auto & swapchain: swapchains_lobby) swapchain.release(); @@ -748,7 +616,7 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) XrEnvironmentBlendMode blend_mode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE; std::vector layers_base; - if (passthrough_enabled) + if (application::get_config().passthrough_enabled) { std::visit( overloaded{ @@ -864,6 +732,7 @@ void scenes::lobby::on_focused() void scenes::lobby::setup_passthrough() { + auto & passthrough_enabled = application::get_config().passthrough_enabled; if (not passthrough_enabled) { passthrough.emplace(); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 8ac9a884..01bc46d2 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -19,6 +19,7 @@ #pragma once +#include "configuration.h" #include "render/scene_data.h" #include "scene.h" #include "scenes/hand_model.h" @@ -26,7 +27,6 @@ #include #include "xr/passthrough.h" -#include #include #include @@ -43,16 +43,6 @@ class stream; class lobby : public scene_impl { - struct server_data - { - bool autoconnect; - bool manual; - bool visible; - bool compatible; - - wivrn_discover::service service; - }; - enum class connection_status { idle, @@ -62,7 +52,6 @@ class lobby : public scene_impl }; std::optional discover; - std::map servers; char add_server_window_prettyname[200]; char add_server_window_hostname[200]; @@ -93,8 +82,6 @@ class lobby : public scene_impl xr::system::passthrough_type passthrough_supported; xr::passthrough passthrough; - void save_config(); - void update_server_list(); XrCompositionLayerQuad draw_gui(XrTime predicted_display_time); @@ -118,11 +105,6 @@ class lobby : public scene_impl tab last_current_tab = tab::server_list; ImTextureID about_picture; - float preferred_refresh_rate = 0; - bool show_performance_metrics = false; - bool microphone = false; - bool passthrough_enabled = false; - void gui_connecting(); void gui_server_list(); void gui_add_server(); @@ -134,7 +116,7 @@ class lobby : public scene_impl void vibrate_on_hover(); - void connect(server_data & data); + void connect(const configuration::server_data & data); std::optional check_recenter_gesture(const std::array & joints); std::optional check_recenter_action(XrTime predicted_display_time); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index d4f00fc9..c5d8fded 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -179,7 +179,7 @@ void scenes::lobby::gui_add_server() if (ImGui::Button("Save", button_size)) { current_tab = tab::server_list; - server_data data{ + configuration::server_data data{ .manual = true, .service = { .name = add_server_window_prettyname, @@ -188,17 +188,19 @@ void scenes::lobby::gui_add_server() }, }; - servers.emplace("manual-" + data.service.name, data); - save_config(); + auto & config = application::get_config(); + config.servers.emplace("manual-" + data.service.name, data); + config.save(); } vibrate_on_hover(); } void scenes::lobby::gui_server_list() { + auto & config = application::get_config(); // Build an index of the cookies sorted by server name std::multimap sorted_cookies; - for (auto && [cookie, data]: servers) + for (auto && [cookie, data]: config.servers) { sorted_cookies.emplace(data.service.name, cookie); } @@ -224,7 +226,7 @@ void scenes::lobby::gui_server_list() ImGui::PushStyleColor(ImGuiCol_HeaderActive, 0); for (const auto & [name, cookie]: sorted_cookies) { - server_data & data = servers.at(cookie); + configuration::server_data & data = config.servers.at(cookie); bool is_selected = (cookie == selected_item); ImGui::SetCursorPos(pos); @@ -242,7 +244,7 @@ void scenes::lobby::gui_server_list() { ImGui::SetCursorPos(ImVec2(pos.x, pos.y + 50)); if (ImGui::Checkbox(("Autoconnect##" + cookie).c_str(), &data.autoconnect)) - save_config(); + config.save(); vibrate_on_hover(); } @@ -310,8 +312,8 @@ void scenes::lobby::gui_server_list() if (cookie_to_remove != "") { - servers.erase(cookie_to_remove); - save_config(); + config.servers.erase(cookie_to_remove); + config.save(); } // Check if an automatic connection has started @@ -331,6 +333,7 @@ void scenes::lobby::gui_server_list() void scenes::lobby::gui_settings() { + auto & config = application::get_config(); ImGuiStyle & style = ImGui::GetStyle(); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(20, 20)); @@ -347,8 +350,8 @@ void scenes::lobby::gui_settings() if (ImGui::Selectable(fmt::format("{}", rate).c_str(), rate == current) and rate != current) { session.set_refresh_rate(rate); - preferred_refresh_rate = rate; - save_config(); + config.preferred_refresh_rate = rate; + config.save(); } } ImGui::EndCombo(); @@ -357,10 +360,10 @@ void scenes::lobby::gui_settings() } } - if (ImGui::Checkbox("Enable microphone", µphone)) + if (ImGui::Checkbox("Enable microphone", &config.microphone)) { #ifdef __ANDROID__ - if (microphone) + if (config.microphone) { jni::object<""> act(application::native_app()->activity->clazz); auto app = act.call>("getApplication"); @@ -380,26 +383,26 @@ void scenes::lobby::gui_settings() } } #endif - save_config(); + config.save(); } vibrate_on_hover(); ImGui::BeginDisabled(passthrough_supported == xr::system::passthrough_type::no_passthrough); - if (ImGui::Checkbox("Enable video passthrough in lobby", &passthrough_enabled)) + if (ImGui::Checkbox("Enable video passthrough in lobby", &config.passthrough_enabled)) { setup_passthrough(); - save_config(); + config.save(); } vibrate_on_hover(); ImGui::EndDisabled(); - if (ImGui::Checkbox("Show performance metrics", &show_performance_metrics)) - save_config(); + if (ImGui::Checkbox("Show performance metrics", &config.show_performance_metrics)) + config.save(); vibrate_on_hover(); ImGui::PopStyleVar(); - if (show_performance_metrics) + if (config.show_performance_metrics) { float win_width = ImGui::GetWindowSize().x; float win_height = ImGui::GetWindowSize().y; diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index f213ad99..ca2dbe4a 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -75,13 +75,11 @@ static const std::array supported_formats = vk::Format::eR8G8B8A8Srgb, vk::Format::eB8G8R8A8Srgb}; -std::shared_ptr scenes::stream::create(std::unique_ptr network_session, bool show_performance_metrics, bool enable_microphone) +std::shared_ptr scenes::stream::create(std::unique_ptr network_session) { std::shared_ptr self{new stream}; spdlog::info("decoder_mutex.native_handle() = {}", (void *)self->decoder_mutex.native_handle()); - self->network_session = std::move(network_session); - self->show_performance_metrics = show_performance_metrics; from_headset::headset_info_packet info{}; @@ -115,7 +113,7 @@ std::shared_ptr scenes::stream::create(std::unique_ptrnetwork_session->send_control(info); @@ -183,7 +181,7 @@ std::shared_ptr scenes::stream::create(std::unique_ptr swapchains; xr::swapchain swapchain_imgui; @@ -124,7 +123,7 @@ private: public: ~stream(); - static std::shared_ptr create(std::unique_ptr session, bool show_performance_metrics, bool enable_microphone); + static std::shared_ptr create(std::unique_ptr session); void render(XrTime predicted_display_time, bool should_render) override; void on_focused() override;