From a2a4d721069f1c73bd038b4186fa2a02514d68bd Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Mon, 14 Nov 2022 20:41:15 +0100 Subject: [PATCH] Add "scale" configuration value --- server/CMakeLists.txt | 1 + server/driver/configuration.cpp | 105 +++++++++++++++++++++++++++++ server/driver/configuration.h | 48 +++++++++++++ server/driver/encoder_settings.cpp | 84 ++++------------------- server/driver/wivrn_hmd.cpp | 9 ++- 5 files changed, 173 insertions(+), 74 deletions(-) create mode 100644 server/driver/configuration.cpp create mode 100644 server/driver/configuration.h diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 3e971303..d6b21c95 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -54,6 +54,7 @@ add_executable(wivrn-server hostname.cpp avahi_publisher.cpp + driver/configuration.cpp driver/encoder_settings.cpp driver/wivrn_hmd.cpp driver/wivrn_comp_target.cpp diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp new file mode 100644 index 00000000..cfbc6770 --- /dev/null +++ b/server/driver/configuration.cpp @@ -0,0 +1,105 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 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 +#include +#define JSON_DISABLE_ENUM_SERIALIZATION 1 +#define JSON_DIAGNOSTICS 1 +#include +#include + +#include "util/u_logging.h" + +#include "configuration.h" + +static std::filesystem::path get_config_base_dir() +{ + const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME"); + if (xdg_config_home) + return xdg_config_home; + const char * home = std::getenv("HOME"); + if (home) + return std::filesystem::path(home) / ".config"; + return "."; +} + +static std::filesystem::path get_config_file() +{ + return get_config_base_dir() / "wivrn" / "config.json"; +} + +namespace xrt::drivers::wivrn +{ + +NLOHMANN_JSON_SERIALIZE_ENUM(video_codec, { + {video_codec(-1), ""}, + {h264, "h264"}, + {h264, "avc"}, + {h265, "h265"}, + {h265, "hevc"}, + }) +} + +configuration configuration::read_user_configuration() +{ + configuration result; + auto config_file_path = get_config_file(); + if (not std::filesystem::exists(config_file_path)) + return result; + std::ifstream file(config_file_path); + try + { + auto json = nlohmann::json::parse(file); + + if (json.contains("scale")) + result.scale = json["scale"]; + + if (json.contains("encoders")) + { + for (const auto & encoder: json["encoders"]) + { + configuration::encoder e; + e.name = encoder.at("encoder"); + +#define SET_IF(property) \ + if (encoder.contains(#property)) \ + e.property = encoder[#property]; + + SET_IF(width); + SET_IF(height); + SET_IF(offset_x); + SET_IF(offset_y); + SET_IF(bitrate); + SET_IF(group); + SET_IF(codec); + if (e.codec == xrt::drivers::wivrn::video_codec(-1)) + throw std::runtime_error("invalid codec value " + encoder["codec"].get()); + SET_IF(options); + result.encoders.push_back(e); + } + } + } + catch (const std::exception & e) + { + U_LOG_E("Invalid configuration file: %s", e.what()); + return {}; + } + + return result; +} diff --git a/server/driver/configuration.h b/server/driver/configuration.h new file mode 100644 index 00000000..0b0deb1d --- /dev/null +++ b/server/driver/configuration.h @@ -0,0 +1,48 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 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 +#include +#include + +#include "wivrn_packets.h" + +struct configuration +{ + struct encoder + { + std::string name; + std::optional width; + std::optional height; + std::optional offset_x; + std::optional offset_y; + std::optional bitrate; + std::optional group; + std::optional codec; + std::map options; + }; + + std::vector encoders; + std::optional scale; + + static configuration read_user_configuration(); + +}; diff --git a/server/driver/encoder_settings.cpp b/server/driver/encoder_settings.cpp index 092babf1..f4bafc96 100644 --- a/server/driver/encoder_settings.cpp +++ b/server/driver/encoder_settings.cpp @@ -20,10 +20,8 @@ #include "encoder_settings.h" #include "video_encoder.h" #include "vk/vk_helpers.h" +#include "configuration.h" -#define JSON_DISABLE_ENUM_SERIALIZATION 1 -#define JSON_DIAGNOSTICS 1 -#include #include #include @@ -83,83 +81,27 @@ static std::vector get_encoder_default_se return {settings}; } -static std::filesystem::path get_config_base_dir() -{ - const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) - return xdg_config_home; - const char * home = std::getenv("HOME"); - if (home) - return std::filesystem::path(home) / ".config"; - return "."; -} - -static std::filesystem::path get_config_file() -{ - return get_config_base_dir() / "wivrn" / "config.json"; -} - -static nlohmann::json get_configuration() -{ - auto config_file_path = get_config_file(); - if (std::filesystem::exists(config_file_path)) - { - std::ifstream file(config_file_path); - return nlohmann::json::parse(file); - } - return nlohmann::json{}; -} - -namespace xrt::drivers::wivrn { - -NLOHMANN_JSON_SERIALIZE_ENUM(video_codec, { - {video_codec(-1), ""}, - {h264, "h264"}, {h264, "avc"}, - {h265, "h265"}, {h265, "hevc"}, -}) -} - std::vector xrt::drivers::wivrn::get_encoder_settings(vk_bundle * vk, uint16_t width, uint16_t height) { try { - const auto & config = get_configuration(); - if (not config.contains("encoders")) + const auto & config = configuration::read_user_configuration(); + if (config.encoders.empty()) return get_encoder_default_settings(vk, width, height); std::vector res; int next_group = 0; - for (const auto& encoder: config["encoders"]) + for (const auto& encoder: config.encoders) { xrt::drivers::wivrn::encoder_settings settings{}; - settings.encoder_name = encoder.at("encoder"); - settings.width = width; - settings.height = height; - settings.bitrate = default_bitrate; - settings.codec = xrt::drivers::wivrn::h264; - settings.group = next_group; - - if (encoder.contains("width")) - settings.width = std::ceil(encoder["width"].get() * width); - if (encoder.contains("height")) - settings.height = std::ceil(encoder["height"].get() * height); - if (encoder.contains("offset_x")) - settings.offset_x = std::ceil(encoder["offset_x"].get() * width); - if (encoder.contains("offset_y")) - settings.offset_y = std::ceil(encoder["offset_y"].get() * height); - - if (encoder.contains("bitrate")) - settings.bitrate = encoder["bitrate"]; - - if (encoder.contains("group")) - settings.group = encoder["group"]; - - if (encoder.contains("codec")) - settings.codec = encoder["codec"]; - if (settings.codec == video_codec(-1)) - throw std::runtime_error("invalid codec value " + encoder["codec"].get()); - - if (encoder.contains("options")) - settings.options = encoder["options"]; + settings.encoder_name = encoder.name; + settings.width = std::ceil(encoder.width.value_or(1) * width); + settings.height = std::ceil(encoder.height.value_or(1) * height); + settings.offset_x = std::ceil(encoder.offset_x.value_or(0) * width); + settings.offset_y = std::ceil(encoder.offset_y.value_or(0) * height); + settings.bitrate = encoder.bitrate.value_or(default_bitrate); + settings.codec = encoder.codec.value_or(xrt::drivers::wivrn::h264); + settings.group = encoder.group.value_or(next_group); + settings.options = encoder.options; next_group = std::max(next_group, settings.group + 1); res.push_back(settings); diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 59955e94..4bb037a8 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -38,6 +38,7 @@ #include #include +#include "configuration.h" #include "wivrn_comp_target.h" /* @@ -112,8 +113,10 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, inputs = &pose_input; input_count = 1; - auto eye_width = info.recommended_eye_width; - auto eye_height = info.recommended_eye_height; + const auto config = configuration::read_user_configuration(); + + auto eye_width = info.recommended_eye_width * config.scale.value_or(1); + auto eye_height = info.recommended_eye_height * config.scale.value_or(1); fps = info.preferred_refresh_rate; // Setup info. @@ -121,7 +124,7 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr cnx, hmd->blend_mode_count = 1; hmd->distortion.models = XRT_DISTORTION_MODEL_NONE; hmd->distortion.preferred = XRT_DISTORTION_MODEL_NONE; - hmd->screens[0].w_pixels = info.recommended_eye_width * 2; + hmd->screens[0].w_pixels = eye_width * 2; hmd->screens[0].h_pixels = eye_height; hmd->screens[0].nominal_frame_interval_ns = 1000000000 / fps;