Add "scale" configuration value

This commit is contained in:
Patrick Nicolas committed 2022-11-14 20:41:15 +01:00
1 parent dc7f2cda21
commit a2a4d72106
5 files changed
+173 -74

No files matched your search

+1
View File
@@ -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
+105
View File
@@ -0,0 +1,105 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <[email protected]>
* Copyright (C) 2022 Patrick Nicolas <[email protected]>
*
* 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 <filesystem>
#include <fstream>
#define JSON_DISABLE_ENUM_SERIALIZATION 1
#define JSON_DIAGNOSTICS 1
#include <nlohmann/json.hpp>
#include <stdlib.h>
#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<std::string>());
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;
}
+48
View File
@@ -0,0 +1,48 @@
/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <[email protected]>
* Copyright (C) 2022 Patrick Nicolas <[email protected]>
*
* 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 <map>
#include <optional>
#include <string>
#include "wivrn_packets.h"
struct configuration
{
struct encoder
{
std::string name;
std::optional<double> width;
std::optional<double> height;
std::optional<double> offset_x;
std::optional<double> offset_y;
std::optional<int> bitrate;
std::optional<int> group;
std::optional<xrt::drivers::wivrn::video_codec> codec;
std::map<std::string, std::string> options;
};
std::vector<encoder> encoders;
std::optional<double> scale;
static configuration read_user_configuration();
};
+13 -71
View File
@@ -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 <nlohmann/json.hpp>
#include <string>
#include <vulkan/vulkan.h>
@@ -83,83 +81,27 @@ static std::vector<xrt::drivers::wivrn::encoder_settings> 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<encoder_settings> 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<xrt::drivers::wivrn::encoder_settings> 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<double>() * width);
if (encoder.contains("height"))
settings.height = std::ceil(encoder["height"].get<double>() * height);
if (encoder.contains("offset_x"))
settings.offset_x = std::ceil(encoder["offset_x"].get<double>() * width);
if (encoder.contains("offset_y"))
settings.offset_y = std::ceil(encoder["offset_y"].get<double>() * 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<std::string>());
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);
+6 -3
View File
@@ -38,6 +38,7 @@
#include <stdio.h>
#include <openxr/openxr.h>
#include "configuration.h"
#include "wivrn_comp_target.h"
/*
@@ -112,8 +113,10 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> 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<xrt::drivers::wivrn::wivrn_session> 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;