Refactor client configuration

This commit is contained in:
Patrick Nicolas
2024-04-21 11:20:31 +02:00
parent 194709e3e0
commit b72acd87fc
10 changed files with 272 additions and 187 deletions
+11
View File
@@ -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);
+9
View File
@@ -19,6 +19,7 @@
#pragma once
#include "configuration.h"
#include "xr/hand_tracker.h"
#ifdef __ANDROID__
#include <android_native_app_glue.h>
@@ -134,6 +135,8 @@ class application : public singleton<application>
std::weak_ptr<scene> last_scene;
std::chrono::nanoseconds last_scene_cpu_time;
std::optional<configuration> 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;
}
};
+159
View File
@@ -0,0 +1,159 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "configuration.h"
#include "application.h"
#include <fstream>
#include <simdjson.h>
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 << "}";
}
+55
View File
@@ -0,0 +1,55 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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<std::string, server_data> 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();
};
+1 -1
View File
@@ -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();
+10 -141
View File
@@ -36,13 +36,10 @@
#include "wivrn_discover.h"
#include <cstdint>
#include <fstream>
#include <glm/gtc/quaternion.hpp>
#include <ios>
#include <magic_enum.hpp>
#include <simdjson.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>
#include <utils/ranges.h>
#include <vulkan/vulkan_raii.hpp>
@@ -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<wivrn_discover::service> 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<XrCompositionLayerProjectionView> 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<XrCompositionLayerBaseHeader *> 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<xr::passthrough_fb>();
+2 -20
View File
@@ -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 <vulkan/vulkan_raii.hpp>
#include "xr/passthrough.h"
#include <map>
#include <optional>
#include <vector>
@@ -43,16 +43,6 @@ class stream;
class lobby : public scene_impl<lobby>
{
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<lobby>
};
std::optional<wivrn_discover> discover;
std::map<std::string, server_data> servers;
char add_server_window_prettyname[200];
char add_server_window_hostname[200];
@@ -93,8 +82,6 @@ class lobby : public scene_impl<lobby>
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<lobby>
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<lobby>
void vibrate_on_hover();
void connect(server_data & data);
void connect(const configuration::server_data & data);
std::optional<glm::vec3> check_recenter_gesture(const std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> & joints);
std::optional<glm::vec3> check_recenter_action(XrTime predicted_display_time);
+21 -18
View File
@@ -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<std::string, std::string> 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", &microphone))
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<jni::object<"android/app/Application">>("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;
+3 -5
View File
@@ -75,13 +75,11 @@ static const std::array supported_formats =
vk::Format::eR8G8B8A8Srgb,
vk::Format::eB8G8R8A8Srgb};
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session, bool show_performance_metrics, bool enable_microphone)
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session)
{
std::shared_ptr<stream> 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> scenes::stream::create(std::unique_ptr<wivrn_ses
info.hand_tracking = application::get_hand_tracking_supported();
audio::get_audio_description(info);
if (not enable_microphone)
if (not application::get_config().microphone)
info.microphone = {};
self->network_session->send_control(info);
@@ -183,7 +181,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
void scenes::stream::on_focused()
{
if (show_performance_metrics)
if (application::get_config().show_performance_metrics)
{
swapchain_imgui = xr::swapchain(
session,
+1 -2
View File
@@ -106,7 +106,6 @@ private:
state state_ = state::initializing;
XrTime first_frame_time{};
const float dbrightness = 2;
bool show_performance_metrics = false;
std::vector<xr::swapchain> swapchains;
xr::swapchain swapchain_imgui;
@@ -124,7 +123,7 @@ private:
public:
~stream();
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session, bool show_performance_metrics, bool enable_microphone);
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session);
void render(XrTime predicted_display_time, bool should_render) override;
void on_focused() override;