From 16076a05c8c645c986bbfdf9a40622715a5e7931 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 26 Oct 2025 14:11:24 +0100 Subject: [PATCH] Rework settings GUI Only allow simple encoder configuration in GUI, now with 10-bit encoding checkbox. --- dashboard/CMakeLists.txt | 1 - dashboard/qml/SettingsPage.qml | 253 +++----- dashboard/rectangle_partitionner.cpp | 21 - dashboard/rectangle_partitionner.h | 935 --------------------------- dashboard/settings.cpp | 654 +++++++++++-------- dashboard/settings.h | 144 ++--- server/driver/configuration.cpp | 2 - 7 files changed, 521 insertions(+), 1489 deletions(-) delete mode 100644 dashboard/rectangle_partitionner.cpp delete mode 100644 dashboard/rectangle_partitionner.h diff --git a/dashboard/CMakeLists.txt b/dashboard/CMakeLists.txt index ab6702a8..fdd1b0bf 100644 --- a/dashboard/CMakeLists.txt +++ b/dashboard/CMakeLists.txt @@ -15,7 +15,6 @@ add_executable(wivrn-dashboard dashboard_utils.cpp escape_string.cpp settings.cpp - rectangle_partitionner.cpp adb.cpp apk_installer.cpp firewall.cpp diff --git a/dashboard/qml/SettingsPage.qml b/dashboard/qml/SettingsPage.qml index 9c456111..c3ad4bb3 100644 --- a/dashboard/qml/SettingsPage.qml +++ b/dashboard/qml/SettingsPage.qml @@ -35,7 +35,7 @@ Kirigami.ScrollablePage { Controls.CheckBox { id: manual_foveation - checked: true + checked: config.scale != -1 Layout.row: 0 Layout.column: 0 text: i18nc("automatic foveation setup", "Manual foveation") @@ -96,6 +96,7 @@ Kirigami.ScrollablePage { Kirigami.FormData.label: i18n("Bitrate:") from: 1 to: 200 + value: config.bitrate textFromValue: (value, locale) => i18nc("bitrate", "%1 Mbit/s", value) valueFromText: function (text, locale) { @@ -105,155 +106,59 @@ Kirigami.ScrollablePage { } return Number.fromLocaleString(text); } + + onValueModified: config.bitrate = value } - Controls.ComboBox { - id: encoder_layout - Kirigami.FormData.label: i18n("Encoder layout:") - model: [ - { - label: i18nc("encoder preset", "Automatic"), - value: null - }, - { - label: i18nc("encoder preset", "Manual"), - value: null - }, - { - label: i18nc("encoder preset", "Default preset"), - value: [ - { - width: 0.5, - height: 1, - offset_x: 0, - offset_y: 0 - }, - { - width: 0.5, - height: 1, - offset_x: 0.5, - offset_y: 0 - } - ] - }, - { - label: i18nc("encoder preset", "Low latency preset"), - value: [ - { - width: 0.5, - height: 1, - offset_x: 0.5, - offset_y: 0, - encoder: 'x264', - codec: 'h264' - }, - { - width: 0.5, - height: 1, - offset_x: 0, - offset_y: 0 - } - ] - }, - { - label: i18nc("encoder preset", "Safe preset"), - value: [ - { - width: 1, - height: 1, - offset_x: 0, - offset_y: 0 - }] + Kirigami.InlineMessage { + Layout.fillWidth: true + text: i18n("The current encoder configuration is not supported") + type: Kirigami.MessageType.Information + visible: !config.simpleConfig + actions: [ + Kirigami.Action { + text: i18n("Reset") + onTriggered: config.encoder = Settings.EncoderAuto } ] - textRole: "label" - valueRole: "value" - onCurrentValueChanged: { - if (currentValue) - { - config.set_encoder_preset(currentValue); - partitionner.currentIndex = -1; - } - } - } - - Controls.Label { - visible: encoder_layout.currentIndex > 0 - text: i18n("To add a new encoder, split an existing encoder by clicking near an edge.\nDrag an edge to resize or remove encoders.") - wrapMode: Text.WordWrap - Layout.fillWidth: true - } - - RectanglePartitionner { - id: partitionner - implicitWidth: 600 - implicitHeight: 300 - visible: encoder_layout.currentIndex > 0 - settings: config - onCodecChanged: { - for (var i = 0; i < codec_combo.model.length; i++) { - if (codec_combo.model[i].name == codec) - codec_combo.currentIndex = i; - } - } - onEncoderChanged: { - for (var i = 0; i < encoder_combo.model.length; i++) { - if (encoder_combo.model[i].name == encoder) - encoder_combo.currentIndex = i; - } - } - onEncoderLayoutChanged: encoder_layout.currentIndex = 1 // Manual layout } RowLayout { Kirigami.FormData.label: i18n("Encoder:") - enabled: partitionner.selected - visible: encoder_layout.currentIndex > 0 + enabled: config.simpleConfig Controls.ComboBox { id: encoder_combo model: [ - // Keep it in sync with rectangle_partitionner.h (encoder_name_from_setting) { - name: "auto", label: i18nc("automatic encoder setup", "Auto"), - codecs: "auto" + encoder: Settings.EncoderAuto }, { - name: "nvenc", label: i18n("nvenc (NVIDIA GPUs)"), - codecs: "auto,h264,h265,av1" + encoder: Settings.Nvenc }, { - name: "vaapi", label: i18n("vaapi (AMD and Intel GPUs)"), - codecs: "auto,h264,h265,av1" + encoder: Settings.Vaapi }, { - name: "x264", - label: i18n("x264 (software encoding)"), - codecs: "h264" - }, - { - name: "vulkan", label: i18n("Vulkan (Vulkan Video)"), - codecs: "h264" + encoder: Settings.Vulkan + }, + { + label: i18n("x264 (software encoding)"), + encoder: Settings.X264 } ] + onCurrentIndexChanged: config.encoder = model[currentIndex].encoder textRole: "label" - onCurrentIndexChanged: { - partitionner.encoder = encoder_combo.model[currentIndex].name; - - // Check if the currently selected codec is supported by the new encoder - var supported_codecs = model[currentIndex].codecs.split(","); - var current_codec = codec_combo.model[codec_combo.currentIndex].name; - - if (!supported_codecs.includes(current_codec)) { - for (var i = 0; i < codec_combo.model.length; i++) { - if (supported_codecs.includes(codec_combo.model[i].name)) { - codec_combo.currentIndex = i; - break; - } - } + Connections { + target: config + function onEncoderChanged() { + var encoder = config.encoder; + var i = encoder_combo.model.findIndex( item => item.encoder == encoder) + if (i > -1) + encoder_combo.currentIndex = i } } } @@ -262,44 +167,62 @@ Kirigami.ScrollablePage { } } - Controls.ComboBox { - id: codec_combo + RowLayout { Kirigami.FormData.label: i18n("Codec:") - enabled: partitionner.selected - visible: encoder_layout.currentIndex > 0 - model: [ - // Keep it in sync with rectangle_partitionner.h (codec_name_from_setting) - { - name: "auto", - label: i18nc("automatic codec setup", "Auto") - }, - { - name: "h264", - label: i18n("H.264") - }, - { - name: "h265", - label: i18n("H.265") - }, - { - name: "av1", - label: i18n("AV1") + Controls.ComboBox { + id: codec_combo + enabled: config.simpleConfig && config.encoder != Settings.EncoderAuto + model: [ + { + label: i18nc("automatic codec setup", "Auto"), + codec: Settings.CodecAuto + }, + { + label: i18n("H.264"), + codec: Settings.H264 + }, + { + label: i18n("H.265"), + codec: Settings.H265 + }, + { + label: i18n("AV1"), + codec: Settings.Av1 + } + ] + onCurrentIndexChanged: config.codec = model[currentIndex].codec + textRole: "label" + + delegate: Controls.ItemDelegate { + required property string label + required property var codec + + width: codec_combo.width + text: i18n(label) + highlighted: ListView.isCurrentItem + enabled: config.allowedCodecs.includes(codec) } - ] - textRole: "label" - onCurrentIndexChanged: partitionner.codec = model[currentIndex].name - - delegate: Controls.ItemDelegate { - required property string label - required property string name - - width: codec_combo.width - text: i18n(label) - highlighted: ListView.isCurrentItem - enabled: encoder_combo.model[encoder_combo.currentIndex].codecs.split(",").includes(name) + Connections { + target: config + function onCodecChanged() { + var codec = config.codec; + var i = codec_combo.model.findIndex( item => item.codec == codec) + if (i > -1) + codec_combo.currentIndex = i + } + } + } + Controls.CheckBox { + enabled: config.can10bit + text: i18n("10-bits") + checked: config.tenbit + } + Kirigami.ContextualHelpButton { + toolTipText: i18n("10-bit encoding improves image quality but is not supported by all codecs and hardware") } } + Kirigami.Separator { Kirigami.FormData.isSection: true } @@ -453,9 +376,7 @@ Kirigami.ScrollablePage { } function save() { - config.bitrate = bitrate.value * 1000000; config.scale = manual_foveation.checked ? 1 - scale_slider.value / 100.0 : -1; - config.manualEncoders = encoder_layout.currentIndex > 0; let openvr = openvr_combobox.model.get(openvr_combobox.currentIndex) if (openvr.is_custom) { config.openvr = openvr_text.text; @@ -471,22 +392,10 @@ Kirigami.ScrollablePage { } function load() { - bitrate.value = config.bitrate / 1000000; - if (config.scale > 0) { scale_slider.value = Math.round(100 - config.scale * 100); - manual_foveation.checked = true; } else { scale_slider.value = 50; - manual_foveation.checked = false; - } - - if (config.manualEncoders) { - // auto_encoders.checked = false; - encoder_layout.currentIndex = 1; - } else { - // auto_encoders.checked = true; - encoder_layout.currentIndex = 0; } debug_gui.checked = config.debugGui; diff --git a/dashboard/rectangle_partitionner.cpp b/dashboard/rectangle_partitionner.cpp deleted file mode 100644 index 68bc5801..00000000 --- a/dashboard/rectangle_partitionner.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2024 Guillaume Meunier - * - * 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 "rectangle_partitionner.h" - -#include "moc_rectangle_partitionner.cpp" diff --git a/dashboard/rectangle_partitionner.h b/dashboard/rectangle_partitionner.h deleted file mode 100644 index b2373bc4..00000000 --- a/dashboard/rectangle_partitionner.h +++ /dev/null @@ -1,935 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2024 Guillaume Meunier - * - * 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 -#include -#include -#include -#include -#include -#include -#include - -#include "settings.h" -#include - -enum class side -{ - top, - bottom, - left, - right -}; - -struct edge -{ - side edge_side; - int rectangle_index; - - bool operator==(const edge & other) const noexcept - { - return edge_side == other.edge_side and rectangle_index == other.rectangle_index; - } - - double position = 0; - double min = std::numeric_limits::max(); - double max = std::numeric_limits::lowest(); -}; - -namespace -{ -const double split_edge_threshold = 0.05; -const double move_edge_threshold = 0.02; -const double magnetism_threshold = 0.05; -const int num_subdiv = 20; -const double epsilon = 1.0 / (2 * num_subdiv); - -QRectF rectangle_from_encoder(const encoder & enc) -{ - QRectF r; - r.setX(enc.offset_x); - r.setY(enc.offset_y); - r.setWidth(enc.width); - r.setHeight(enc.height); - return r; -} - -int hovered_rectangle(QPointF position, const std::vector & encoders) -{ - for (const auto [i, enc]: std::views::enumerate(encoders)) - { - if (rectangle_from_encoder(enc).contains(position)) - return i; - } - - return -1; -} - -Qt::CursorShape get_cursor_shape(QPointF position, const std::vector & rectangles) -{ - for (const encoder & i: rectangles) - { - QRectF r = rectangle_from_encoder(i); - if (r.contains(position)) - { - QPointF rel_pos = position - r.topLeft(); - double dx = std::min(rel_pos.x(), r.width() - rel_pos.x()); - double dy = std::min(rel_pos.y(), r.height() - rel_pos.y()); - - if (dx < move_edge_threshold and position.x() >= epsilon and position.x() <= 1 - epsilon) - return Qt::SizeHorCursor; - else if (dy < move_edge_threshold and position.y() >= epsilon and position.y() <= 1 - epsilon) - return Qt::SizeVerCursor; - else if (dx < split_edge_threshold and position.y() >= epsilon and position.y() <= 1 - epsilon) - return Qt::SplitVCursor; - else if (dy < split_edge_threshold and position.x() >= epsilon and position.x() <= 1 - epsilon) - return Qt::SplitHCursor; - else - return Qt::ArrowCursor; - - break; - } - } - - return Qt::ArrowCursor; -} - -std::vector horizontal_edges(const std::vector & rectangles) -{ - std::vector edges; - - for (const auto & [i, enc]: std::views::enumerate(rectangles)) - { - if (enc.top() > epsilon) - { - edges.emplace_back(side::top, i, enc.top(), enc.left(), enc.right()); - } - - if (rectangles[i].bottom() < 1 - epsilon) - { - edges.emplace_back(side::bottom, i, enc.bottom(), enc.left(), enc.right()); - } - } - - return edges; -} - -std::vector vertical_edges(const std::vector & rectangles) -{ - std::vector edges; - - for (const auto & [i, enc]: std::views::enumerate(rectangles)) - { - if (enc.left() > epsilon) - { - edges.emplace_back(side::left, i, enc.left(), enc.top(), enc.bottom()); - } - - if (enc.right() < 1 - epsilon) - { - edges.emplace_back(side::right, i, enc.right(), enc.top(), enc.bottom()); - } - } - - return edges; -} - -std::vector> partition_edges(std::vector edges) -{ - auto comp = [](const edge & a, const edge & b) { - if (a.position < b.position) - return true; - if (a.position > b.position) - return false; - - if (a.min < b.min) - return true; - if (a.min > b.min) - return false; - - if (a.max < b.max) - return true; - return false; - }; - - std::ranges::sort(edges, comp); - - std::vector> partitionned; - - double current_position = std::numeric_limits::lowest(); - double current_max = 0; - - for (auto & e: edges) - { - if (e.position != current_position) - { - current_position = e.position; - current_max = e.max; - partitionned.push_back({e}); - } - else - { - if (e.min >= current_max) - { - // All the following edges will be after the last partition - current_max = e.max; - partitionned.push_back({e}); - } - else - { - // Add the edge to the current partition - current_max = std::max(current_max, e.max); - partitionned.back().push_back(e); - } - } - } - - return partitionned; -} - -std::vector hovered_edges(QPointF position, const std::vector> & partitionned_edges, bool horizontal) -{ - if (horizontal) - position = position.transposed(); - - for (const auto & edge_list: partitionned_edges) - { - for (const auto & edge: edge_list) - { - if (std::abs(position.x() - edge.position) < move_edge_threshold and position.y() >= edge.min and position.y() <= edge.max) - { - return edge_list; - } - } - } - - return {}; -} - -bool assert_rectangle_list_is_partition(const std::vector & rectangles) -{ - return true; - - bool ok = true; - - QRectF bounding_box{0, 0, 1, 1}; - - for (const auto & i: rectangles) - { - if (i.width < 0 or i.height < 0) - { - qDebug() << "Invalid rectangle" /*<< i*/; - ok = false; - } - } - - std::vector rectangle_y_boundaries; - for (auto & i: rectangles) - { - rectangle_y_boundaries.push_back(i.top()); - rectangle_y_boundaries.push_back(i.bottom()); - } - - for (double y: rectangle_y_boundaries) - { - if (y < bounding_box.top()) - continue; - if (y >= bounding_box.bottom()) - continue; - - qDebug() << "Checking line" << y; - std::vector> horizontal_segments; - for (const auto & i: rectangles) - { - if (y >= i.top() and y < i.bottom()) - { - horizontal_segments.emplace_back(i.left(), i.right()); - qDebug() << " Adding segment" << i.left() << "-" << i.right(); - } - } - - std::ranges::sort(horizontal_segments); - - if (horizontal_segments.empty()) - { - qDebug() << "Line" << y << "empty"; - ok = false; - } - - if (horizontal_segments.front().first != bounding_box.left()) - { - qDebug() << "Line" << y << "does not start at the left of the bounding box" << (horizontal_segments.front().first - bounding_box.left()); - ok = false; - } - - if (horizontal_segments.back().second != bounding_box.right()) - { - qDebug() << "Last rectangle of line" << y << "does not end at the right of the bounding box" << (horizontal_segments.back().second - bounding_box.right()); - ok = false; - } - - for (int i = 1; i < horizontal_segments.size(); ++i) - { - if (horizontal_segments[i - 1].second < horizontal_segments[i].first) - { - qDebug() << "Overlap in line" << y << (horizontal_segments[i - 1].second - horizontal_segments[i].first); - ok = false; - } - else if (horizontal_segments[i - 1].second > horizontal_segments[i].first) - { - qDebug() << "Gap in line" << y << (horizontal_segments[i - 1].second - horizontal_segments[i].first); - ok = false; - } - } - } - - return ok; -} -} // namespace - -class RectanglePartitionner : public QQuickPaintedItem -{ - Q_OBJECT - QML_ELEMENT - - Q_PROPERTY(Settings * settings READ settings WRITE setSettings NOTIFY settingsChanged) - Q_PROPERTY(bool selected READ selected NOTIFY selectedChanged) - Q_PROPERTY(QString encoder READ encoder WRITE setEncoder NOTIFY encoderChanged) - Q_PROPERTY(QString codec READ codec WRITE setCodec NOTIFY codecChanged) - Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) - - int m_currentIndex = -1; - Settings * m_settings = nullptr; - - std::vector selection; - std::vector hovered; - - double min_drag_position; - double max_drag_position; - - QLine split_line; - QRect split_clip_rect; - Qt::CursorShape cursor_shape = Qt::ArrowCursor; - - enum class mouse_event_type - { - move, - press, - release - }; - - Kirigami::Platform::PlatformTheme theme; - -public: - RectanglePartitionner(QQuickItem * parent = nullptr) : - QQuickPaintedItem(parent), - theme(this) - { - setFlag(ItemHasContents); - setAcceptedMouseButtons(Qt::LeftButton); - setAcceptHoverEvents(true); - - theme.setColorSet(Kirigami::Platform::PlatformTheme::View); - setFillColor(theme.backgroundColor()); - - connect(&theme, &Kirigami::Platform::PlatformTheme::colorsChanged, this, [&]() { - setFillColor(theme.backgroundColor()); - update(); - }); - - // connect(this, &QQuickItem::enabledChanged, this, [&](){ - // if (isEnabled()) - // theme.setColorGroup(Kirigami::Platform::PlatformTheme::Active); - // else - // theme.setColorGroup(Kirigami::Platform::PlatformTheme::Disabled); - // }); - } - - int currentIndex() const - { - return m_currentIndex; - } - - bool selected() const - { - return m_currentIndex >= 0 and m_currentIndex < m_settings->encoders().size(); - } - - void setCurrentIndex(int value) - { - if (m_currentIndex != value) - { - m_currentIndex = value; - currentIndexChanged(value); - selectedChanged(selected()); - encoderChanged(encoder()); - codecChanged(codec()); - - update(); - } - } - - Settings * settings() const - { - return m_settings; - } - - void onEncodersChanged() - { - update(); - } - - void setSettings(Settings * value) - { - if (m_settings != value) - { - if (m_settings) - disconnect(m_settings, &Settings::encodersChanged, this, &RectanglePartitionner::onEncodersChanged); - - settingsChanged(m_settings = value); - - if (m_settings) - connect(m_settings, &Settings::encodersChanged, this, &RectanglePartitionner::onEncodersChanged); - } - } - - QString encoder() const - { - if (not m_settings or m_currentIndex < 0 or m_currentIndex >= m_settings->encoders().size()) - return ""; - - return QString::fromStdString(Settings::encoder_from_id(m_settings->encoders()[m_currentIndex].name)); - } - - QString codec() const - { - if (not m_settings or m_currentIndex < 0 or m_currentIndex >= m_settings->encoders().size()) - return ""; - - return QString::fromStdString(Settings::codec_from_id(m_settings->encoders()[m_currentIndex].codec)); - } - - void setEncoder(QString value) - { - qDebug() << "Setting encoder to " << value; - auto encoders = m_settings->encoders(); - auto converted_value = Settings::encoder_id_from_string(value.toStdString()); - - if (not m_settings or m_currentIndex < 0 or m_currentIndex >= encoders.size()) - return; - - if (encoders[m_currentIndex].name == converted_value) - return; - - encoders[m_currentIndex].name = converted_value; - m_settings->set_encoders(encoders); - update(); - encoderLayoutChanged(); - } - - void setCodec(QString value) - { - qDebug() << "Setting codec to " << value; - auto encoders = m_settings->encoders(); - auto converted_value = Settings::codec_id_from_string(value.toStdString()); - - if (not m_settings or m_currentIndex < 0 or m_currentIndex >= encoders.size()) - return; - - if (encoders[m_currentIndex].codec == converted_value) - return; - - encoders[m_currentIndex].codec = Settings::codec_id_from_string(value.toStdString()); - m_settings->set_encoders(encoders); - update(); - encoderLayoutChanged(); - } - - QRect frameRect() - { - return {0, 0, (int)width() - 1, (int)height() - 1}; - } - - QPointF from_position(QPoint position) - { - auto r = frameRect(); - - return QPointF{ - std::clamp(double(position.x() - r.x()) / (r.width() - 1), 0., 1.), - std::clamp(double(position.y() - r.y()) / (r.height() - 1), 0., 1.), - }; - } - - QPoint to_position(QPointF position) - { - auto r = frameRect(); - - return QPoint( - std::round(r.x() + position.x() * (r.width() - 1)), - std::round(r.y() + position.y() * (r.height() - 1))); - } - - QRect to_position(QRectF position) - { - return QRect{to_position(position.topLeft()), to_position(position.bottomRight())}; - } - - QRect rectangle_position(int index) - { - return to_position(rectangle_from_encoder(m_settings->encoders().at(index))); - } - - // Keep it in sync with SettingsPage.qml (codec_combo.model) - QString codec_name_from_setting(QString value) - { - if (value == "h264") - return i18n("H.264"); - if (value == "h265") - return i18n("H.265"); - if (value == "av1") - return i18n("AV1"); - - return i18nc("automatic codec setup", "Auto"); - } - - // Keep it in sync with SettingsPage.qml (encoder_combo.model) - QString encoder_name_from_setting(QString value) - { - if (value == "nvenc") - return i18n("nvenc"); - if (value == "vaapi") - return i18n("vaapi"); - if (value == "x264") - return i18n("x264"); - if (value == "vulkan") - return i18n("Vulkan"); - - return i18nc("automatic encoder setup", "Auto"); - } - - // BEGIN Painting functions - void paint_single_rectangle_border(QPainter * painter, const ::encoder & enc) - { - QRectF r = rectangle_from_encoder(enc); - QRect rect = to_position(r); - - QPen pen = painter->pen(); - painter->setPen(QPen{theme.disabledTextColor()}); - painter->drawRect(rect); - painter->setPen(pen); - } - - void paint_single_rectangle(QPainter * painter, const ::encoder & enc, bool selected) - { - QRectF r = rectangle_from_encoder(enc); - QRect rect = to_position(r); - - if (selected) - { - painter->fillRect(rect.adjusted(1, 1, 0, 0), theme.focusColor()); - } - - QString codec = QString::fromStdString(Settings::codec_from_id(enc.codec)); - QString encoder = QString::fromStdString(Settings::encoder_from_id(enc.name)); - - QFont font = painter->font(); - QFont font2 = font; - - font2.setPixelSize(24); - - QString text = QString("%1\n%2").arg(encoder_name_from_setting(encoder), codec_name_from_setting(codec)); - - QFontMetrics metrics{font2}; - QSize size = metrics.size(0, text); - - if (double ratio = std::max((double)size.width() / rect.width(), (double)size.height() / rect.height()); ratio > 1) - { - int pixel_size = font2.pixelSize() / ratio; - if (pixel_size > 0) - font2.setPixelSize(pixel_size); - } - - QPen pen = painter->pen(); - - painter->setPen(QPen{theme.textColor()}); - painter->setFont(font2); - painter->drawText(rect, Qt::AlignCenter, text); - painter->setFont(font); - painter->setPen(pen); - } - - void paint(QPainter * painter) override - { - if (not m_settings) - return; - - for (auto [i, enc]: std::views::enumerate(m_settings->encoders())) - { - bool is_selected = i == m_currentIndex and isEnabled(); - paint_single_rectangle(painter, enc, is_selected); - } - - if (not hovered.empty() and selection.empty()) - { - for (auto & enc: m_settings->encoders()) - { - paint_single_rectangle_border(painter, enc); - } - - painter->setPen(QPen(Qt::blue, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - - auto r = frameRect(); - - QList lines; - lines.reserve(hovered.size()); - for (const edge & i: hovered) - { - QLineF line; - switch (i.edge_side) - { - case side::top: - case side::bottom: - line = QLineF(i.min, i.position, i.max, i.position); - break; - - case side::left: - case side::right: - line = QLineF(i.position, i.min, i.position, i.max); - break; - } - - int x1 = std::round(r.x() + line.x1() * (r.width() - 1)); - int x2 = std::round(r.x() + line.x2() * (r.width() - 1)); - - int y1 = std::round(r.y() + line.y1() * (r.height() - 1)); - int y2 = std::round(r.y() + line.y2() * (r.height() - 1)); - - lines.emplace_back(x1, y1, x2, y2); - } - - painter->drawLines(lines); - - auto horz_partitionned = partition_edges(horizontal_edges(m_settings->encoders())); - auto vert_partitionned = partition_edges(vertical_edges(m_settings->encoders())); - } - else if ((cursor_shape == Qt::SplitHCursor or cursor_shape == Qt::SplitVCursor) and not split_line.isNull()) - { - auto now = std::chrono::duration_cast>(std::chrono::steady_clock::now().time_since_epoch()).count(); - - QPen pen(theme.disabledTextColor(), 2, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin); - pen.setDashPattern({5, 5}); - pen.setDashOffset(-now * 15); - painter->setPen(pen); - - painter->setClipping(true); - painter->setClipRect(split_clip_rect.adjusted(1, 1, 0, 0)); - painter->drawLine(split_line); - painter->setClipping(false); - update(); - - for (auto & enc: m_settings->encoders()) - { - paint_single_rectangle_border(painter, enc); - } - } - else - { - for (auto & enc: m_settings->encoders()) - { - paint_single_rectangle_border(painter, enc); - } - } - } - // END Painting functions - - // BEGIN Mouse callbacks - void mouseMoveEvent(QMouseEvent * event) override - { - auto pos = event->position().toPoint(); - - auto encoders = m_settings->encoders(); - - if (not selection.empty()) - { - double position; - - if (selection.front().edge_side == side::top or selection.front().edge_side == side::bottom) - position = from_position(pos).y(); - else - position = from_position(pos).x(); - - position = std::clamp(position, min_drag_position, max_drag_position); - position = std::round(position * num_subdiv) / num_subdiv; // magnetism - - for (edge & e: selection) - { - switch (e.edge_side) - { - case side::top: - encoders[e.rectangle_index].set_top(position); - break; - - case side::bottom: - encoders[e.rectangle_index].set_bottom(position); - break; - - case side::left: - encoders[e.rectangle_index].set_left(position); - break; - - case side::right: - encoders[e.rectangle_index].set_right(position); - break; - } - } - - update(); - m_settings->set_encoders(encoders); - encoderLayoutChanged(); - } - } - - void hoverMoveEvent(QHoverEvent * event) override - { - if (not m_settings) - return; - - QPoint pos = event->position().toPoint(); - - const auto & encoders = m_settings->encoders(); - - hovered.clear(); - - if (auto horz_edges = hovered_edges(from_position(pos), partition_edges(horizontal_edges(encoders)), true); not horz_edges.empty()) - { - hovered = std::move(horz_edges); - update(); - } - else if (auto vert_edges = hovered_edges(from_position(pos), partition_edges(vertical_edges(encoders)), false); not vert_edges.empty()) - { - hovered = std::move(vert_edges); - update(); - } - else - { - int n = hovered_rectangle(from_position(pos), encoders); - } - - Qt::CursorShape new_cursor = get_cursor_shape(from_position(pos), encoders); - if (new_cursor != cursor_shape) - { - update(); - cursor_shape = new_cursor; - setCursor(QCursor(new_cursor)); - } - - if (cursor_shape == Qt::SplitHCursor) - { - int n = hovered_rectangle(from_position(pos), encoders); - - double x = std::round(from_position(pos).x() * num_subdiv) / num_subdiv; // magnetism - int px = to_position({x, 0}).x(); - - split_line = {px, rectangle_position(n).top(), px, rectangle_position(n).bottom()}; - split_clip_rect = rectangle_position(n); - update(); - } - else if (cursor_shape == Qt::SplitVCursor) - { - int n = hovered_rectangle(from_position(pos), encoders); - - double y = std::round(from_position(pos).y() * num_subdiv) / num_subdiv; // magnetism - int py = to_position({0, y}).y(); - - split_line = {rectangle_position(n).left(), py, rectangle_position(n).right(), py}; - split_clip_rect = rectangle_position(n); - update(); - } - else - { - split_line = {0, 0, 0, 0}; - split_clip_rect = {0, 0, 0, 0}; - } - } - - void mousePressEvent(QMouseEvent * event) override - { - if (not m_settings) - return; - - selection.clear(); - split_line = {0, 0, 0, 0}; - auto encoders = m_settings->encoders(); - - if (auto horz_edges = hovered_edges(from_position(event->pos()), partition_edges(horizontal_edges(encoders)), true); not horz_edges.empty()) - { - selection = std::move(horz_edges); - } - else if (auto vert_edges = hovered_edges(from_position(event->pos()), partition_edges(vertical_edges(encoders)), false); not vert_edges.empty()) - { - selection = std::move(vert_edges); - } - else if (int n = hovered_rectangle(from_position(event->pos()), encoders); n >= 0) - { - ::encoder & hovered = encoders[n]; - - // Compute distance to edges - auto pos = from_position(event->pos()); - - double dx = std::min(pos.x() - hovered.left(), hovered.right() - pos.x()); - double dy = std::min(pos.y() - hovered.top(), hovered.bottom() - pos.y()); - - if (dx < split_edge_threshold) - { - qDebug() << "Splitting rectangle " << n << " vertically"; - double y = std::round(pos.y() * num_subdiv) / num_subdiv; // magnetism - - // Split vertically - ::encoder new_rectangle = hovered; - - hovered.set_bottom(y); - new_rectangle.set_top(hovered.bottom()); - - encoders.push_back(new_rectangle); - update(); - m_settings->set_encoders(encoders); - encoderLayoutChanged(); - - if (not assert_rectangle_list_is_partition(encoders)) - abort(); - } - else if (dy < split_edge_threshold) - { - qDebug() << "Splitting rectangle " << n << " horizontally"; - double x = std::round(pos.x() * num_subdiv) / num_subdiv; // magnetism - - // Split horizontally - ::encoder new_rectangle = hovered; - - hovered.set_right(x); - new_rectangle.set_left(hovered.right()); - - encoders.push_back(new_rectangle); - update(); - m_settings->set_encoders(encoders); - encoderLayoutChanged(); - - if (not assert_rectangle_list_is_partition(encoders)) - abort(); - } - else - { - qDebug() << "Selecting rectangle " << n; - setCurrentIndex(n); - } - } - - if (not selection.empty()) - { - hovered.clear(); - - min_drag_position = std::numeric_limits::lowest(); - max_drag_position = std::numeric_limits::max(); - - for (auto & e: selection) - { - switch (e.edge_side) - { - case side::top: - max_drag_position = std::min(max_drag_position, encoders[e.rectangle_index].bottom()); - break; - case side::bottom: - min_drag_position = std::max(min_drag_position, encoders[e.rectangle_index].top()); - break; - case side::left: - max_drag_position = std::min(max_drag_position, encoders[e.rectangle_index].right()); - break; - case side::right: - min_drag_position = std::max(min_drag_position, encoders[e.rectangle_index].left()); - break; - } - } - } - } - - void mouseReleaseEvent(QMouseEvent * event) override - { - if (not m_settings) - return; - - selection.clear(); - auto encoders = m_settings->encoders(); - - // Delete empty rectangles - bool changed = false; - for (int i = 0; i < encoders.size();) - { - if (encoders[i].width <= 0.0001 or encoders[i].height <= 0.0001) // TODO configurable thresholds - { - qDebug() << "Removing rectangle " << i; - encoders.erase(encoders.begin() + i); - changed = true; - - if (m_currentIndex > i) - m_currentIndex--; - else if (m_currentIndex == i) - { - m_currentIndex = -1; - } - } - else - { - ++i; - } - } - - if (changed) - { - if (not assert_rectangle_list_is_partition(encoders)) - abort(); - - m_settings->set_encoders(encoders); - setCurrentIndex(m_currentIndex); - update(); - encoderLayoutChanged(); - } - } - - void hoverLeaveEvent(QHoverEvent * event) override - { - hovered.clear(); - split_line = {0, 0, 0, 0}; - split_clip_rect = {0, 0, 0, 0}; - update(); - } - // END Mouse callbacks - -Q_SIGNALS: - void settingsChanged(Settings *); - void selectedChanged(bool); - void encoderChanged(QString); - void codecChanged(QString); - void currentIndexChanged(int); - - void encoderLayoutChanged(); -}; diff --git a/dashboard/settings.cpp b/dashboard/settings.cpp index 7622f7c2..da491ec8 100644 --- a/dashboard/settings.cpp +++ b/dashboard/settings.cpp @@ -24,90 +24,112 @@ #include "wivrn_server.h" #include #include -#include -#include -#include #include #include #include namespace { -const std::vector> encoder_ids{ - {encoder::encoder_name::nvenc, "nvenc"}, - {encoder::encoder_name::vaapi, "vaapi"}, - {encoder::encoder_name::x264, "x264"}, - {encoder::encoder_name::vulkan, "vulkan"}, +const std::vector> encoder_ids{ + {Settings::encoder_name::Nvenc, "nvenc"}, + {Settings::encoder_name::Vaapi, "vaapi"}, + {Settings::encoder_name::X264, "x264"}, + {Settings::encoder_name::Vulkan, "vulkan"}, }; -const std::vector> codec_ids{ - {encoder::video_codec::h264, "h264"}, - {encoder::video_codec::h264, "avc"}, - {encoder::video_codec::h265, "h265"}, - {encoder::video_codec::h265, "hevc"}, - {encoder::video_codec::av1, "av1"}, - {encoder::video_codec::av1, "AV1"}, +const std::vector> codec_ids{ + {Settings::video_codec::H264, "h264"}, + {Settings::video_codec::H264, "avc"}, + {Settings::video_codec::H265, "h265"}, + {Settings::video_codec::H265, "hevc"}, + {Settings::video_codec::Av1, "av1"}, + {Settings::video_codec::Av1, "AV1"}, }; + +bool can10bit(Settings::video_codec c) +{ + switch (c) + { + case Settings::CodecAuto: + case Settings::H264: + return false; + case Settings::H265: + case Settings::Av1: + return true; + } + return false; +} } // namespace -std::optional Settings::encoder_id_from_string(std::string_view s) +Settings::encoder_name Settings::encoder_id_from_string(std::string_view s) { for (auto & [i, j]: encoder_ids) { if (j == s) return i; } - return std::nullopt; + return Settings::encoder_name::EncoderAuto; } -std::optional Settings::codec_id_from_string(std::string_view s) +Settings::video_codec Settings::codec_id_from_string(std::string_view s) { for (auto & [i, j]: codec_ids) { if (j == s) return i; } - return std::nullopt; + return Settings::video_codec::CodecAuto; } -const std::string & Settings::encoder_from_id(std::optional id) +const std::string & Settings::encoder_from_id(Settings::encoder_name id) { static const std::string default_value = "auto"; - if (not id) - return default_value; for (auto & [i, j]: encoder_ids) { - if (i == *id) + if (i == id) return j; } return default_value; } -const std::string & Settings::codec_from_id(std::optional id) +const std::string & Settings::codec_from_id(Settings::video_codec id) { static const std::string default_value = "auto"; - if (not id) - return default_value; for (auto & [i, j]: codec_ids) { - if (i == *id) + if (i == id) return j; } return default_value; } +void Settings::emitAllChanged() +{ + simpleConfigChanged(); + encoderChanged(); + codecChanged(); + tenbitChanged(); + bitrateChanged(); + scaleChanged(); + tcpOnlyChanged(); + applicationChanged(); + openvrChanged(); + debugGuiChanged(); + steamVrLhChanged(); +} + void Settings::load(const wivrn_server * server) { // Encoders configuration - nlohmann::json json_doc; try { auto conf = server->jsonConfiguration(); - json_doc = nlohmann::json::parse(conf.toUtf8()); + m_jsonSettings = nlohmann::json::parse(conf.toUtf8()); + emitAllChanged(); } catch (std::exception & e) { @@ -115,260 +137,366 @@ void Settings::load(const wivrn_server * server) restore_defaults(); return; } +} - std::vector new_encoders; +bool Settings::simpleConfig() const +{ + // Simple configuration: either unset, or all encoders + codecs are the same try { - nlohmann::json json_encoders; - - if (json_doc.contains("encoders")) + auto it = m_jsonSettings.find("encoders"); + if (it == m_jsonSettings.end()) + return true; + std::optional encoder; + std::optional codec; + for (const auto & item: *it) { - json_encoders = json_doc["encoders"]; - set_manualEncoders(true); - } - else if (json_doc.contains("encoders.disabled")) - { - json_encoders = json_doc["encoders.disabled"]; - set_manualEncoders(false); - } - else - { - set_manualEncoders(false); - } - - for (auto & i: json_encoders) - { - encoder enc; - enc.name = encoder_id_from_string(i.value("encoder", "auto")); - enc.codec = codec_id_from_string(i.value("codec", "auto")); - enc.width = i.value("width", 1.0); - enc.height = i.value("height", 1.0); - enc.offset_x = i.value("offset_x", 0.0); - enc.offset_y = i.value("offset_y", 0.0); - enc.group = i.value("group", 0); // TODO: handle groups - new_encoders.push_back(enc); - } - } - catch (...) - { - // QMessageBox msgbox{QMessageBox::Information, tr("Invalid settings"), tr("The encoder configuration is invalid, the default values will be restored."), QMessageBox::Close, this}; - // msgbox.exec(); - - new_encoders.clear(); - } - set_encoders(new_encoders); - - if (encoders().empty()) - { - set_manualEncoders(false); - std::vector new_encoders; - new_encoders.push_back(encoder{ - .width = 1, - .height = 1, - .offset_x = 0, - .offset_y = 0, - }); - set_encoders(new_encoders); - } - - // Foveation - try - { - if (json_doc.contains("scale")) - { - set_scale(json_doc.value("scale", 1.0)); - } - else - { - set_scale(-1); - } - } - catch (...) - { - set_scale(-1); - } - - // Bitrate - try - { - set_bitrate(json_doc.value("bitrate", 50'000'000)); - } - catch (...) - { - set_bitrate(50'000'000); - } - - // Automatically started application - std::vector application; - try - { - if (json_doc["application"].is_array()) - application = json_doc.value>("application", {}); - else if (json_doc["application"].is_string()) - application.push_back(json_doc["application"]); - - set_application(escape_string(application)); - } - catch (...) - { - set_application(""); - } - - // Advanced options (debug window, steamvr_lh) - try - { - set_debugGui(json_doc.value("debug-gui", false)); - } - catch (...) - { - set_debugGui(false); - } - - try - { - set_steamVrLh(json_doc.value("use-steamvr-lh", false)); - } - catch (...) - { - set_steamVrLh(false); - } - - // OpenVR compat library - try - { - if (auto it = json_doc.find("openvr-compat-path"); it != json_doc.end()) - { - if (it->is_null()) - set_openvr("-"); + if (auto it = item.find("encoder"); it != item.end()) + { + auto e = encoder_id_from_string(std::string(*it)); + if (encoder and e != encoder) + return false; + encoder = e; + } else - set_openvr(QString::fromStdString(*it)); + { + encoder = encoder_name::EncoderAuto; + } + + if (auto it = item.find("codec"); it != item.end()) + { + auto e = codec_id_from_string(std::string(*it)); + if (codec and e != codec) + return false; + codec = e; + } + else + { + codec = video_codec::CodecAuto; + } } - else + return true; + } + catch (...) + { + return false; + } +} + +Settings::encoder_name Settings::encoder() const +{ + try + { + auto it = m_jsonSettings.find("encoders"); + if (it == m_jsonSettings.end()) + return encoder_name::EncoderAuto; + for (const auto & item: *it) { - set_openvr(""); + if (auto it = item.find("encoder"); it != item.end()) + return encoder_id_from_string(std::string(*it)); + return encoder_name::EncoderAuto; } } catch (...) { - set_openvr(""); } + return encoder_name::EncoderAuto; +} + +void Settings::set_encoder(const encoder_name & value) +{ + auto old = encoder(); + switch (value) + { + case EncoderAuto: + m_jsonSettings.erase("encoders"); + break; + case Nvenc: + case Vaapi: + case Vulkan: { + auto & enc = m_jsonSettings["encoders"] = nlohmann::json::array(); + for (float offset: {0.f, 0.5f}) + { + auto obj = nlohmann::json::object({ + {"width", 0.5}, + {"encoder", encoder_from_id(value)}, + }); + if (offset) + obj["offset_x"] = offset; + if (value == Vulkan) + obj["codec"] = "h264"; + enc.push_back(obj); + } + } + break; + case encoder_name::X264: + m_jsonSettings["encoders"] = + nlohmann::json::parse(R"([{"encoder": "x264", "codec": "h264"}])"); + break; + } + if (value != old) + { + encoderChanged(); + codecChanged(); + if (not can10bit()) + set_tenbit(false); + } + simpleConfigChanged(); +} + +Settings::video_codec Settings::codec() const +{ + try + { + auto it = m_jsonSettings.find("encoders"); + if (it == m_jsonSettings.end()) + return video_codec::CodecAuto; + for (const auto & item: *it) + { + if (auto it = item.find("codec"); it != item.end()) + return codec_id_from_string(std::string(*it)); + return video_codec::CodecAuto; + } + } + catch (...) + { + } + return video_codec::CodecAuto; +} + +void Settings::set_codec(const video_codec & value) +{ + auto old = codec(); + auto it = m_jsonSettings.find("encoders"); + if (it == m_jsonSettings.end() or not it->is_array()) + return; + for (auto & item: *it) + { + if (not item.is_object()) + continue; + if (value == CodecAuto) + item.erase("codec"); + else + item["codec"] = codec_from_id(value); + } + if (value != old) + { + if (::can10bit(value) != ::can10bit(old)) + set_tenbit(::can10bit(value)); + codecChanged(); + } +} + +bool Settings::tenbit() const +{ + try + { + return m_jsonSettings.value("bit-depth", 8) == 10; + } + catch (...) + {} + return false; +} + +void Settings::set_tenbit(const bool & value) +{ + auto old = tenbit(); + m_jsonSettings["bit-depth"] = value ? 10 : 8; + if (value != old) + tenbitChanged(); +} + +int Settings::bitrate() const +{ + try + { + return m_jsonSettings.value("bitrate", 50'000'000) / 1'000'000; + } + catch (...) + { + return 50; + } +} + +void Settings::set_bitrate(const int & value) +{ + auto old = bitrate(); + m_jsonSettings["bitrate"] = value * 1'000'000; + if (old != value) + bitrateChanged(); +} + +float Settings::scale() const +{ + // Foveation + auto it = m_jsonSettings.find("scale"); + if (it != m_jsonSettings.end() and it->is_number()) + return *it; + return -1; +} + +void Settings::set_scale(const float & value) +{ + auto old = scale(); + if (value == -1) + m_jsonSettings.erase("scale"); + else + m_jsonSettings["scale"] = value; + if (old != value) + scaleChanged(); +} + +QString Settings::application() const +{ + // Automatically started application + try + { + std::vector application; + auto it = m_jsonSettings.find("application"); + if (it == m_jsonSettings.end()) + return ""; + if (it->is_array()) + application = *it; + else if (it->is_string()) + application.push_back(*it); + + return escape_string(application); + } + catch (...) + { + return ""; + } +} + +void Settings::set_application(const QString & value) +{ + auto old = application(); + if (value.isEmpty()) + m_jsonSettings.erase("application"); + else + m_jsonSettings["application"] = unescape_string(value); + if (old != value) + applicationChanged(); +} + +bool Settings::debugGui() const +{ + // Advanced options (debug window, steamvr_lh) + auto it = m_jsonSettings.find("debug-gui"); + if (it != m_jsonSettings.end() and it->is_boolean()) + return *it; + return false; +} + +void Settings::set_debugGui(const bool & value) +{ + auto old = debugGui(); + m_jsonSettings["debug-gui"] = value; + if (old != value) + debugGuiChanged(); +} + +bool Settings::steamVrLh() const +{ + auto it = m_jsonSettings.find("use-steamvr-lh"); + if (it != m_jsonSettings.end() and it->is_boolean()) + return *it; + return false; +} + +void Settings::set_steamVrLh(const bool & value) +{ + auto old = steamVrLh(); + m_jsonSettings["use-steamvr-lh"] = value; + if (old != value) + steamVrLhChanged(); +} + +bool Settings::tcpOnly() const +{ + auto it = m_jsonSettings.find("tcp-only"); + if (it != m_jsonSettings.end() and it->is_boolean()) + return *it; + return false; +} + +void Settings::set_tcpOnly(const bool & value) +{ + auto old = tcpOnly(); + m_jsonSettings["tcp-only"] = value; + if (old != value) + tcpOnlyChanged(); +} + +QString Settings::openvr() const +{ + // OpenVR compat library + if (auto it = m_jsonSettings.find("openvr-compat-path"); it != m_jsonSettings.end()) + { + if (it->is_null()) + return "-"; + if (it->is_string()) + return QString::fromStdString(*it); + } + return ""; +} + +void Settings::set_openvr(const QString & value) +{ + auto old = openvr(); + if (value == "-") + m_jsonSettings["openvr-compat-path"] = nullptr; + else if (value == "") + m_jsonSettings.erase("openvr-compat-path"); + else + m_jsonSettings["openvr-compat-path"] = value.toStdString(); + if (old != value) + tcpOnlyChanged(); +} + +QList Settings::allowedCodecs() const +{ + switch (encoder()) + { + case encoder_name::EncoderAuto: + return {video_codec::CodecAuto}; + case encoder_name::Nvenc: + case encoder_name::Vaapi: + return { + video_codec::CodecAuto, + video_codec::H264, + video_codec::H265, + video_codec::Av1, + }; + case encoder_name::X264: + case encoder_name::Vulkan: + return {video_codec::H264}; + } + return {video_codec::CodecAuto}; +} + +bool Settings::can10bit() const +{ + return ::can10bit(codec()); } void Settings::save(wivrn_server * server) { - nlohmann::json json_doc; - try - { - auto conf = server->jsonConfiguration(); - json_doc = nlohmann::json::parse(conf.toUtf8()); - } - catch (...) - { - } - - // Remove all optional keys that might not be overwritten - auto it = json_doc.find("scale"); - if (it != json_doc.end()) - json_doc.erase(it); - - it = json_doc.find("encoders.disabled"); - if (it != json_doc.end()) - json_doc.erase(it); - - it = json_doc.find("encoders"); - if (it != json_doc.end()) - json_doc.erase(it); - - it = json_doc.find("application"); - if (it != json_doc.end()) - json_doc.erase(it); - - if (scale() > 0) - json_doc["scale"] = scale(); - - json_doc["bitrate"] = bitrate(); - - std::vector encoders; - - for (auto & enc: m_encoders) - { - nlohmann::json encoder; - - if (auto value = encoder_from_id(enc.name); value != "auto") - encoder["encoder"] = value; - if (auto value = codec_from_id(enc.codec); value != "auto") - encoder["codec"] = value; - encoder["width"] = enc.width; - encoder["height"] = enc.height; - encoder["offset_x"] = enc.offset_x; - encoder["offset_y"] = enc.offset_y; - - // TODO encoder groups - - encoders.push_back(encoder); - } - - std::ranges::stable_sort(encoders, [](const nlohmann::json & i, const nlohmann::json & j) { - double size_i = i.value("width", 0.0) * i.value("height", 0.0); - double size_j = j.value("width", 0.0) * j.value("height", 0.0); - return size_i < size_j; - }); - - if (manualEncoders()) - json_doc["encoders"] = encoders; - else - json_doc["encoders.disabled"] = encoders; - - if (application() != "") - json_doc["application"] = unescape_string(application()); - - json_doc["debug-gui"] = debugGui(); - json_doc["use-steamvr-lh"] = steamVrLh(); - - if (openvr() == "-") - json_doc["openvr-compat-path"] = nullptr; - else if (openvr() == "") - json_doc.erase("openvr-compat-path"); - else - json_doc["openvr-compat-path"] = openvr().toStdString(); - - server->setJsonConfiguration(QString::fromStdString(json_doc.dump(2))); + server->setJsonConfiguration(QString::fromStdString(m_jsonSettings.dump(2))); } void Settings::restore_defaults() { - set_encoders({}); - set_encoderPassthrough({}); - set_bitrate(50'000'000); - set_scale(-1); - set_application(""); - set_debugGui(false); - set_steamVrLh(false); - set_tcpOnly(false); -} - -void Settings::set_encoder_preset(QJSValue preset) -{ - QJSEngine engine; - const QJsonArray json_encoders = engine.fromScriptValue(preset).toArray(); - - std::vector encoders; - for (const QJsonValue & json_encoder_value: json_encoders) - { - QJsonObject json_encoder = json_encoder_value.toObject(); - - encoders.push_back(encoder{ - .name = encoder_id_from_string(json_encoder["encoder"].toString("auto").toStdString()), - .width = json_encoder["width"].toDouble(), - .height = json_encoder["height"].toDouble(), - .offset_x = json_encoder["offset_x"].toDouble(), - .offset_y = json_encoder["offset_y"].toDouble(), - .codec = codec_id_from_string(json_encoder["codec"].toString("auto").toStdString()), - }); - } - - set_manualEncoders(true); - set_encoders(encoders); + m_jsonSettings.erase("encoders"); + m_jsonSettings.erase("encoder-passthrough"); + m_jsonSettings.erase("scale"); + m_jsonSettings.erase("bitrate"); + m_jsonSettings.erase("application"); + m_jsonSettings.erase("debug-gui"); + m_jsonSettings.erase("use-steamvr-lh"); + m_jsonSettings.erase("tcp-only"); + m_jsonSettings.erase("application"); + emitAllChanged(); } bool Settings::flatpak() const diff --git a/dashboard/settings.h b/dashboard/settings.h index 9a5e667c..dc6ef7ac 100644 --- a/dashboard/settings.h +++ b/dashboard/settings.h @@ -26,114 +26,63 @@ #include #include -#define SETTER_GETTER_NOTIFY(type, prop_name) \ -private: \ - type m_##prop_name; \ - \ -public: \ - const type & prop_name() const \ - { \ - return m_##prop_name; \ - } \ - void set_##prop_name(const type & value) \ - { \ - m_##prop_name = value; \ - prop_name##Changed(value); \ - } \ -Q_SIGNALS: \ - void prop_name##Changed(const type &); +#define SETTER_GETTER_NOTIFY(type, prop_name) \ +public: \ + type prop_name() const; \ + void set_##prop_name(const type & value); \ +Q_SIGNALS: \ + void prop_name##Changed(); class wivrn_server; -struct encoder -{ - enum class video_codec - { - h264, - h265, - av1, - }; - - enum class encoder_name - { - nvenc, - vaapi, - x264, - vulkan - }; - - std::optional name; - double width; - double height; - double offset_x; - double offset_y; - std::optional group; - std::optional codec; - std::map options; - std::optional device; - - double top() const - { - return offset_y; - } - void set_top(double value) - { - height = bottom() - value; - offset_y = value; - } - - double bottom() const - { - return offset_y + height; - } - void set_bottom(double value) - { - height = value - top(); - } - - double left() const - { - return offset_x; - } - void set_left(double value) - { - width = right() - value; - offset_x = value; - } - - double right() const - { - return offset_x + width; - } - void set_right(double value) - { - width = value - left(); - } -}; - class Settings : public QObject { Q_OBJECT QML_ELEMENT +public: + enum encoder_name + { + EncoderAuto, + Nvenc, + Vaapi, + X264, + Vulkan, + }; + Q_ENUM(encoder_name) - Q_PROPERTY(bool manualEncoders READ manualEncoders WRITE set_manualEncoders NOTIFY manualEncodersChanged) - Q_PROPERTY(std::vector encoders READ encoders WRITE set_encoders NOTIFY encodersChanged) - Q_PROPERTY(encoder encoderPassthrough READ encoderPassthrough WRITE set_encoderPassthrough NOTIFY encoderPassthroughChanged) + enum video_codec + { + CodecAuto, + H264, + H265, + Av1, + }; + Q_ENUM(video_codec) + + Q_PROPERTY(bool simpleConfig READ simpleConfig NOTIFY simpleConfigChanged) + Q_PROPERTY(encoder_name encoder READ encoder WRITE set_encoder NOTIFY encoderChanged) + Q_PROPERTY(video_codec codec READ codec WRITE set_codec NOTIFY codecChanged) + Q_PROPERTY(QList allowedCodecs READ allowedCodecs NOTIFY encoderChanged) + Q_PROPERTY(bool can10bit READ can10bit NOTIFY codecChanged) + Q_PROPERTY(bool tenbit READ tenbit WRITE set_tenbit NOTIFY tenbitChanged) Q_PROPERTY(int bitrate READ bitrate WRITE set_bitrate NOTIFY bitrateChanged) Q_PROPERTY(float scale READ scale WRITE set_scale NOTIFY scaleChanged) + + Q_PROPERTY(bool tcpOnly READ tcpOnly WRITE set_tcpOnly NOTIFY tcpOnlyChanged) Q_PROPERTY(QString application READ application WRITE set_application NOTIFY applicationChanged) + Q_PROPERTY(QString openvr READ openvr WRITE set_openvr NOTIFY openvrChanged) + Q_PROPERTY(bool debugGui READ debugGui WRITE set_debugGui NOTIFY debugGuiChanged) Q_PROPERTY(bool steamVrLh READ steamVrLh WRITE set_steamVrLh NOTIFY steamVrLhChanged) - Q_PROPERTY(bool tcpOnly READ tcpOnly WRITE set_tcpOnly NOTIFY tcpOnlyChanged) - Q_PROPERTY(QString openvr READ openvr WRITE set_openvr NOTIFY openvrChanged) Q_PROPERTY(bool flatpak READ flatpak CONSTANT) Q_PROPERTY(bool debug_gui_supported READ debug_gui CONSTANT) Q_PROPERTY(bool steamvr_lh_supported READ steamvr_lh CONSTANT) - SETTER_GETTER_NOTIFY(bool, manualEncoders) - SETTER_GETTER_NOTIFY(std::vector, encoders) - SETTER_GETTER_NOTIFY(encoder, encoderPassthrough) + SETTER_GETTER_NOTIFY(bool, simpleConfig) + SETTER_GETTER_NOTIFY(encoder_name, encoder) + SETTER_GETTER_NOTIFY(video_codec, codec) + SETTER_GETTER_NOTIFY(bool, tenbit) SETTER_GETTER_NOTIFY(int, bitrate) SETTER_GETTER_NOTIFY(float, scale) SETTER_GETTER_NOTIFY(QString, application) @@ -141,6 +90,9 @@ class Settings : public QObject SETTER_GETTER_NOTIFY(bool, steamVrLh) SETTER_GETTER_NOTIFY(bool, tcpOnly) SETTER_GETTER_NOTIFY(QString, openvr) +private: + nlohmann::json m_jsonSettings = nlohmann::json::object(); + void emitAllChanged(); public: Settings(QObject * parent = nullptr) : @@ -149,16 +101,18 @@ public: Q_INVOKABLE void load(const wivrn_server * server); Q_INVOKABLE void save(wivrn_server * server); Q_INVOKABLE void restore_defaults(); - Q_INVOKABLE void set_encoder_preset(QJSValue preset); + + QList allowedCodecs() const; + bool can10bit() const; bool flatpak() const; bool debug_gui() const; bool steamvr_lh() const; - static std::optional encoder_id_from_string(std::string_view s); - static std::optional codec_id_from_string(std::string_view s); - static const std::string & encoder_from_id(std::optional id); - static const std::string & codec_from_id(std::optional id); + static encoder_name encoder_id_from_string(std::string_view s); + static video_codec codec_id_from_string(std::string_view s); + static const std::string & encoder_from_id(encoder_name id); + static const std::string & codec_from_id(video_codec id); }; #undef SETTER_GETTER_NOTIFY diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 19c0494d..f9d3d793 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -206,8 +206,6 @@ configuration::configuration() if (auto it = json.find("tcp-only"); it != json.end()) tcp_only = *it; - else if (auto it = json.find("tcp_only"); it != json.end()) - tcp_only = *it; if (auto it = json.find("publish-service"); it != json.end()) {