From cfd5617ce8eb6d7c81930bd2c8cf523b458df3d3 Mon Sep 17 00:00:00 2001
From: agsantos <aline.gondimsantos@savoirfairelinux.com>
Date: Wed, 20 Oct 2021 17:23:26 -0400
Subject: [PATCH] screensharing: add preference to set frame rate

GitLab: #514
Change-Id: I0f3dbf2dd31f84dc50a2fd17d153a299d06012fb
---
 .vscode/settings.json                         |  3 +-
 src/constant/JamiStrings.qml                  |  2 +
 .../components/SettingsComboBox.qml           |  1 +
 src/settingsview/components/VideoSettings.qml | 46 ++++++++++++++++++-
 src/videodevices.cpp                          | 26 ++++++++++-
 src/videodevices.h                            |  7 +++
 6 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/.vscode/settings.json b/.vscode/settings.json
index 3b42f1b10..01506c8be 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -5,6 +5,7 @@
         "string": "cpp",
         "string_view": "cpp",
         "ranges": "cpp",
-        "thread": "cpp"
+        "thread": "cpp",
+        "xstring": "cpp"
     }
 }
\ No newline at end of file
diff --git a/src/constant/JamiStrings.qml b/src/constant/JamiStrings.qml
index a54d991f1..a72008c53 100644
--- a/src/constant/JamiStrings.qml
+++ b/src/constant/JamiStrings.qml
@@ -176,6 +176,8 @@ Item {
     property string selectFPS: qsTr("Select video frame rate (frames per second)")
     property string enableHWAccel: qsTr("Enable hardware acceleration")
     property string previewUnavailable: qsTr("Preview unavailable")
+    property string screenSharing: qsTr("Screen Sharing")
+    property string selectScreenSharingFPS: qsTr("Select screen sharing frame rate (frames per second)")
 
     // BackupKeyPage
     property string  backupAccountInfos: qsTr("Your account only exists on this device. " +
diff --git a/src/settingsview/components/SettingsComboBox.qml b/src/settingsview/components/SettingsComboBox.qml
index ed0aa2467..63a4f7b76 100644
--- a/src/settingsview/components/SettingsComboBox.qml
+++ b/src/settingsview/components/SettingsComboBox.qml
@@ -35,6 +35,7 @@ RowLayout {
     property alias enabled: comboBoxOfLayout.enabled
     property alias fontPointSize: comboBoxOfLayout.font.pointSize
     property alias modelIndex: comboBoxOfLayout.currentIndex
+    property alias modelSize: comboBoxOfLayout.count
 
     property int heightOfLayout: 30
     property int widthOfComboBox: 50
diff --git a/src/settingsview/components/VideoSettings.qml b/src/settingsview/components/VideoSettings.qml
index 50fd6649b..34076e66f 100644
--- a/src/settingsview/components/VideoSettings.qml
+++ b/src/settingsview/components/VideoSettings.qml
@@ -120,6 +120,7 @@ ColumnLayout {
         Layout.leftMargin: JamiTheme.preferredMarginSize
 
         enabled: VideoDevices.listSize !== 0
+        opacity: enabled ? 1.0 : 0.5
 
         fontPointSize: JamiTheme.settingsFontSize
         widthOfComboBox: itemWidth
@@ -147,6 +148,7 @@ ColumnLayout {
         Layout.leftMargin: JamiTheme.preferredMarginSize
 
         enabled: VideoDevices.listSize !== 0
+        opacity: enabled ? 1.0 : 0.5
 
         widthOfComboBox: itemWidth
         fontPointSize: JamiTheme.settingsFontSize
@@ -168,6 +170,7 @@ ColumnLayout {
         Layout.leftMargin: JamiTheme.preferredMarginSize
 
         enabled: VideoDevices.listSize !== 0
+        opacity: enabled ? 1.0 : 0.5
 
         widthOfComboBox: itemWidth
         fontPointSize: JamiTheme.settingsFontSize
@@ -200,6 +203,8 @@ ColumnLayout {
     Rectangle {
         id: rectBox
 
+        visible: VideoDevices.listSize !== 0
+
         Layout.alignment: Qt.AlignHCenter
         Layout.preferredHeight: width * aspectRatio
 
@@ -217,7 +222,6 @@ ColumnLayout {
 
             lrcInstance: LRCInstance
 
-            visible: VideoDevices.listSize !== 0
             layer.enabled: true
             layer.effect: OpacityMask {
                 maskSource: rectBox
@@ -240,4 +244,44 @@ ColumnLayout {
         horizontalAlignment: Text.AlignHCenter
         verticalAlignment: Text.AlignVCenter
     }
+
+    ElidedTextLabel {
+        id: screenSharingSetTitle
+        visible: screenSharingFPSComboBoxSetting.modelSize > 0
+        Layout.fillWidth: true
+        Layout.preferredHeight: JamiTheme.preferredFieldHeight
+
+        eText: JamiStrings.screenSharing
+        fontSize: JamiTheme.headerFontSize
+        maxWidth: itemWidth * 2
+    }
+
+    SettingsComboBox {
+        id: screenSharingFPSComboBoxSetting
+
+        visible: modelSize > 0
+
+        Layout.fillWidth: true
+        Layout.preferredHeight: JamiTheme.preferredFieldHeight
+        Layout.leftMargin: JamiTheme.preferredMarginSize
+        Layout.bottomMargin: JamiTheme.preferredMarginSize
+
+        widthOfComboBox: itemWidth
+        fontPointSize: JamiTheme.settingsFontSize
+
+        tipText: JamiStrings.selectScreenSharingFPS
+        labelText: JamiStrings.fps
+        currentSelectionText: VideoDevices.screenSharingDefaultFps.toString()
+        placeholderText: VideoDevices.screenSharingDefaultFps.toString()
+        comboModel: ListModel { id: screenSharingFpsModel }
+        role: "FPS"
+        Component.onCompleted: {
+            var elements = VideoDevices.getScreenSharingFpsModel()
+            for (var item in elements) {
+                screenSharingFpsModel.append({"FPS": elements[item]})
+            }
+        }
+
+        onActivated: VideoDevices.setDisplayFPS(screenSharingFpsModel.get(modelIndex).FPS)
+    }
 }
diff --git a/src/videodevices.cpp b/src/videodevices.cpp
index 94cb0bbe6..e4d1d5dd5 100644
--- a/src/videodevices.cpp
+++ b/src/videodevices.cpp
@@ -206,6 +206,14 @@ VideoDevices::VideoDevices(LRCInstance* lrcInstance, QObject* parent)
             this,
             &VideoDevices::onVideoDeviceEvent);
 
+    auto displaySettings = lrcInstance_->avModel().getDeviceSettings(DEVICE_DESKTOP);
+
+    auto desktopfpsSource = lrcInstance_->avModel().getDeviceCapabilities(DEVICE_DESKTOP);
+    if (desktopfpsSource.contains(CHANNEL_DEFAULT) && !desktopfpsSource[CHANNEL_DEFAULT].empty()) {
+        desktopfpsSourceModel_ = desktopfpsSource[CHANNEL_DEFAULT][0].second;
+        if (desktopfpsSourceModel_.indexOf(displaySettings.rate) >= 0)
+            set_screenSharingDefaultFps(displaySettings.rate);
+    }
     updateData();
 }
 
@@ -305,6 +313,22 @@ VideoDevices::setDefaultDeviceFps(int index)
     updateData();
 }
 
+void
+VideoDevices::setDisplayFPS(const QString& fps)
+{
+    auto settings = lrcInstance_->avModel().getDeviceSettings(DEVICE_DESKTOP);
+    settings.id = DEVICE_DESKTOP;
+    settings.rate = fps.toInt();
+    lrcInstance_->avModel().setDeviceSettings(settings);
+    set_screenSharingDefaultFps(fps.toInt());
+}
+
+QVariant
+VideoDevices::getScreenSharingFpsModel()
+{
+    return QVariant::fromValue(desktopfpsSourceModel_.toList());
+}
+
 void
 VideoDevices::updateData()
 {
@@ -315,7 +339,7 @@ VideoDevices::updateData()
         auto defaultDeviceSettings = lrcInstance_->avModel().getDeviceSettings(defaultDevice);
         auto defaultDeviceCap = lrcInstance_->avModel().getDeviceCapabilities(defaultDevice);
         auto currentResRateList = defaultDeviceCap[defaultDeviceSettings.channel.isEmpty()
-                                                       ? "default"
+                                                       ? CHANNEL_DEFAULT
                                                        : defaultDeviceSettings.channel];
         lrc::api::video::FrameratesList fpsList;
 
diff --git a/src/videodevices.h b/src/videodevices.h
index e6adab009..3ac428491 100644
--- a/src/videodevices.h
+++ b/src/videodevices.h
@@ -153,6 +153,7 @@ class VideoDevices : public QObject
     QML_RO_PROPERTY(QString, defaultName)
     QML_RO_PROPERTY(QString, defaultRes)
     QML_RO_PROPERTY(int, defaultFps)
+    QML_PROPERTY(int, screenSharingDefaultFps)
 
 public:
     explicit VideoDevices(LRCInstance* lrcInstance, QObject* parent = nullptr);
@@ -166,10 +167,12 @@ public:
 
     Q_INVOKABLE QVariant fpsFilterModel();
     Q_INVOKABLE QVariant fpsSourceModel();
+    Q_INVOKABLE QVariant getScreenSharingFpsModel();
 
     Q_INVOKABLE void setDefaultDevice(int index, bool useSourceModel = false);
     Q_INVOKABLE void setDefaultDeviceRes(int index);
     Q_INVOKABLE void setDefaultDeviceFps(int index);
+    Q_INVOKABLE void setDisplayFPS(const QString& fps);
 
     const lrc::api::video::ResRateList& get_defaultResRateList();
     void set_defaultResRateList(lrc::api::video::ResRateList resRateList);
@@ -202,4 +205,8 @@ private:
 
     lrc::api::video::ResRateList defaultResRateList_;
     lrc::api::video::FrameratesList defaultFpsList_;
+    lrc::api::video::FrameratesList desktopfpsSourceModel_;
+
+    constexpr static const char DEVICE_DESKTOP[] = "desktop";
+    constexpr static const char CHANNEL_DEFAULT[] = "default";
 };
-- 
GitLab