From a258ad538f8c439bade532a67c53aa2bff405c74 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Mon, 14 Nov 2022 00:18:54 +0100 Subject: [PATCH] move config in ~/.config/wivrn/config.json --- .github/workflows/Build.yml | 2 +- CMakeLists.txt | 1 + server/CMakeLists.txt | 1 + server/driver/encoder_settings.cpp | 184 +++++++++++++---------------- 4 files changed, 88 insertions(+), 100 deletions(-) diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 4af5b074..9e27bb1e 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -23,7 +23,7 @@ jobs: - name: Install dependencies run: > - sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev + sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev - name: Configure CMake # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. diff --git a/CMakeLists.txt b/CMakeLists.txt index 5184e3b5..853c6b6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,5 +85,6 @@ if (WIVRN_BUILD_CLIENT) endif() if (WIVRN_BUILD_SERVER) + find_package(nlohmann_json REQUIRED) add_subdirectory(server) endif() diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 30e89eb1..3e971303 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -114,6 +114,7 @@ target_link_libraries( PkgConfig::AVAHI Eigen3::Eigen wivrn-common + nlohmann_json::nlohmann_json ) install(TARGETS wivrn-server) diff --git a/server/driver/encoder_settings.cpp b/server/driver/encoder_settings.cpp index 036562fc..092babf1 100644 --- a/server/driver/encoder_settings.cpp +++ b/server/driver/encoder_settings.cpp @@ -18,10 +18,12 @@ */ #include "encoder_settings.h" -#include "util/u_config_json.h" #include "video_encoder.h" #include "vk/vk_helpers.h" +#define JSON_DISABLE_ENUM_SERIALIZATION 1 +#define JSON_DIAGNOSTICS 1 +#include #include #include @@ -81,110 +83,94 @@ 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) { - u_config_json config_json{}; - std::unique_ptr raii(&config_json, u_config_json_close); - - u_config_json_open_or_create_main_file(&config_json); - if (!config_json.file_loaded) + try { - U_LOG_D("no config file, using default"); - return get_encoder_default_settings(vk, width, height); - } - - const cJSON * config_wivrn = u_json_get(config_json.root, "config_wivrn"); - if (config_wivrn == NULL) - { - U_LOG_D("no config_wivrn entry, using default"); - return get_encoder_default_settings(vk, width, height); - } - - const cJSON * config_encoder = u_json_get(config_wivrn, "encoders"); - if (config_encoder == NULL) - { - U_LOG_D("no config_wivrn.encoders entry, using default"); - return get_encoder_default_settings(vk, width, height); - } - std::vector res; - int num_encoders = cJSON_GetArraySize(config_encoder); - - int next_group = 0; - - for (int i = 0; i < num_encoders; ++i) - { - cJSON * encoder = cJSON_GetArrayItem(config_encoder, i); - xrt::drivers::wivrn::encoder_settings settings{}; - - auto name = u_json_get(encoder, "encoder"); - if (name && cJSON_IsString(name)) - settings.encoder_name = name->valuestring; - else + const auto & config = get_configuration(); + if (not config.contains("encoders")) + return get_encoder_default_settings(vk, width, height); + std::vector res; + int next_group = 0; + for (const auto& encoder: config["encoders"]) { - U_LOG_E("missing \"encoder\" key in config_wivrn"); - continue; + 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"]; + + next_group = std::max(next_group, settings.group + 1); + res.push_back(settings); } - - settings.width = width; - settings.height = height; - settings.bitrate = default_bitrate; - settings.codec = xrt::drivers::wivrn::h264; - settings.group = next_group; - - auto js_width = u_json_get(encoder, "width"); - if (js_width && cJSON_IsNumber(js_width)) - settings.width = std::ceil(js_width->valuedouble * width); - - auto js_height = u_json_get(encoder, "height"); - if (js_height && cJSON_IsNumber(js_height)) - settings.height = std::ceil(js_height->valuedouble * height); - - auto offset_x = u_json_get(encoder, "offset_x"); - if (offset_x && cJSON_IsNumber(offset_x)) - settings.offset_x = std::floor(offset_x->valuedouble * width); - auto offset_y = u_json_get(encoder, "offset_y"); - if (offset_y && cJSON_IsNumber(offset_y)) - settings.offset_y = std::floor(offset_y->valuedouble * height); - - auto bitrate = u_json_get(encoder, "bitrate"); - if (bitrate && cJSON_IsNumber(bitrate)) - settings.bitrate = bitrate->valueint; - - auto group = u_json_get(encoder, "group"); - if (group && cJSON_IsNumber(group)) - settings.group = group->valueint; - - auto codec = u_json_get(encoder, "codec"); - if (codec && cJSON_IsString(codec)) - { - if (codec->valuestring == std::string("h264")) - settings.codec = xrt::drivers::wivrn::h264; - else if (codec->valuestring == std::string("h265")) - settings.codec = xrt::drivers::wivrn::h265; - else - { - U_LOG_E("invalid video codec %s", codec->valuestring); - continue; - } - } - auto options = u_json_get(encoder, "options"); - if (options && cJSON_IsObject(options)) - { - for (auto child = options->child; child; child = child->next) - { - if (cJSON_IsString(child)) - { - settings.options[child->string] = child->valuestring; - } - } - } - - next_group = std::max(next_group, settings.group + 1); - - res.push_back(settings); + return res; } - - return res; + catch (const std::exception & e) + { + U_LOG_E("Failed to read encoder configuration: %s", e.what()); + } + return get_encoder_default_settings(vk, width, height); } VkImageTiling xrt::drivers::wivrn::get_required_tiling(vk_bundle * vk,