From bceb3f15621c0026fa1560d44657a3ad226bfacb Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Fri, 25 Oct 2019 16:31:32 -0400
Subject: [PATCH] video settings: only use video device names for display

Change-Id: I7ffc37e8df24d97885ac52ce45145ee0c1f659d0
---
 rendermanager.cpp  |  6 ++--
 settingswidget.cpp | 82 ++++++++++++++++++++++++++++++----------------
 videoview.cpp      | 10 +++---
 3 files changed, 62 insertions(+), 36 deletions(-)

diff --git a/rendermanager.cpp b/rendermanager.cpp
index b489272..e834a83 100644
--- a/rendermanager.cpp
+++ b/rendermanager.cpp
@@ -318,7 +318,7 @@ RenderManager::removeDistantRenderer(const std::string& id)
 void
 RenderManager::slotDeviceEvent()
 {
-    auto defaultDeviceName = avModel_.getDefaultDeviceName();
+    auto defaultDevice = avModel_.getDefaultDevice();
     auto currentCaptureDevice = avModel_.getCurrentVideoCaptureDevice();
     // decide whether a device has plugged, unplugged, or nothing has changed
     auto deviceList = avModel_.getDevices();
@@ -342,14 +342,14 @@ RenderManager::slotDeviceEvent()
             stopPreviewing();
         } else if (deviceEvent == DeviceEvent::RemovedCurrent &&
                    currentDeviceListSize > 0) {
-            avModel_.setCurrentVideoCaptureDevice(defaultDeviceName);
+            avModel_.setCurrentVideoCaptureDevice(defaultDevice);
             startPreviewing(true);
         } else {
             startPreviewing();
         }
     } else if (deviceEvent == DeviceEvent::Added &&
                currentDeviceListSize == 1) {
-        avModel_.setCurrentVideoCaptureDevice(defaultDeviceName);
+        avModel_.setCurrentVideoCaptureDevice(defaultDevice);
     }
 
     emit videoDeviceListChanged();
diff --git a/settingswidget.cpp b/settingswidget.cpp
index 9f51c99..8941fd9 100644
--- a/settingswidget.cpp
+++ b/settingswidget.cpp
@@ -271,7 +271,7 @@ void SettingsWidget::slotAccountListChanged()
     } else {
         disconnectAccountConnections();
     }
-    auto device = LRCInstance::avModel().getDefaultDeviceName();
+    auto device = LRCInstance::avModel().getDefaultDevice();
     if (LRCInstance::avModel().getCurrentVideoCaptureDevice().empty()) {
         LRCInstance::avModel().setCurrentVideoCaptureDevice(device);
     }
@@ -1042,13 +1042,26 @@ void SettingsWidget::slotAudioInputIndexChanged(int index)
 
 void SettingsWidget::slotDeviceBoxCurrentIndexChanged(int index)
 {
-    std::string device = ui->deviceBox->itemData(index, Qt::DisplayRole)
+    std::string deviceName = ui->deviceBox->itemData(index, Qt::DisplayRole)
         .toString()
         .toStdString();
-    LRCInstance::avModel().setCurrentVideoCaptureDevice(device);
-    LRCInstance::avModel().setDefaultDevice(device);
-    setFormatListForDevice(device);
-    startPreviewing(true);
+    auto devices = LRCInstance::avModel().getDevices();
+    try {
+        auto iter = std::find_if(devices.begin(), devices.end(),
+            [deviceName](const std::string& d) {
+                auto settings = LRCInstance::avModel().getDeviceSettings(d);
+                return settings.name == deviceName;
+            });
+        if (iter == devices.end()) {
+            qWarning() << "Couldn't find device: " << deviceName.c_str();
+            return;
+        }
+        auto deviceId = *iter;
+        LRCInstance::avModel().setCurrentVideoCaptureDevice(deviceId);
+        LRCInstance::avModel().setDefaultDevice(deviceId);
+        setFormatListForDevice(deviceId);
+        startPreviewing(true);
+    } catch (...) {}
 }
 
 void SettingsWidget::slotFormatBoxCurrentIndexChanged(int index)
@@ -1056,8 +1069,12 @@ void SettingsWidget::slotFormatBoxCurrentIndexChanged(int index)
     auto resolution = formatIndexList_.at(index).first;
     auto rate = formatIndexList_.at(index).second;
     auto device = LRCInstance::avModel().getCurrentVideoCaptureDevice();
-    lrc::api::video::Settings settings {{}, device, rate, resolution};
-    LRCInstance::avModel().setDeviceSettings(settings);
+    try {
+        auto settings = LRCInstance::avModel().getDeviceSettings(device);
+        settings.rate = rate;
+        settings.size = resolution;
+        LRCInstance::avModel().setDeviceSettings(settings);
+    } catch (...) {}
 }
 
 void SettingsWidget::startPreviewing(bool force)
@@ -1080,29 +1097,33 @@ void SettingsWidget::setFormatListForDevice(const std::string& device)
     if (deviceCapabilities.size() == 0) {
         return;
     }
-    auto currentSettings = LRCInstance::avModel().getDeviceSettings(device);
-    auto currentChannel = currentSettings.channel;
-    currentChannel = currentChannel.empty() ? "default" : currentChannel;
-    auto channelCaps = deviceCapabilities.at(currentChannel);
-
-    ui->formatBox->blockSignals(true);
-    ui->formatBox->clear();
-    formatIndexList_.clear();
-
-    for (auto[resolution, frameRateList] : channelCaps) {
-        for (auto rate : frameRateList) {
-            formatIndexList_.append(QPair<std::string, float>(resolution, rate));
-            auto sizeRateString = QString("%1 [%2 fps]")
-                .arg(QString::fromStdString(resolution))
-                .arg(round(rate));
-            ui->formatBox->addItem(sizeRateString.toUtf8());
-            if (resolution == currentSettings.size && rate == currentSettings.rate) {
-                ui->formatBox->setCurrentIndex(ui->formatBox->count() - 1);
+    try {
+        auto currentSettings = LRCInstance::avModel().getDeviceSettings(device);
+        auto currentChannel = currentSettings.channel;
+        currentChannel = currentChannel.empty() ? "default" : currentChannel;
+        auto channelCaps = deviceCapabilities.at(currentChannel);
+
+        ui->formatBox->blockSignals(true);
+        ui->formatBox->clear();
+        formatIndexList_.clear();
+
+        for (auto[resolution, frameRateList] : channelCaps) {
+            for (auto rate : frameRateList) {
+                formatIndexList_.append(QPair<std::string, float>(resolution, rate));
+                auto sizeRateString = QString("%1 [%2 fps]")
+                    .arg(QString::fromStdString(resolution))
+                    .arg(round(rate));
+                ui->formatBox->addItem(sizeRateString.toUtf8());
+                if (resolution == currentSettings.size && rate == currentSettings.rate) {
+                    ui->formatBox->setCurrentIndex(ui->formatBox->count() - 1);
+                }
             }
         }
-    }
 
-    ui->formatBox->blockSignals(false);
+        ui->formatBox->blockSignals(false);
+    } catch(const std::exception& e) {
+        qWarning() << e.what();
+    }
 }
 
 void SettingsWidget::slotSetHardwareAccel(bool state)
@@ -1178,7 +1199,10 @@ SettingsWidget::populateVideoSettings()
         auto currentCaptureDevice = LRCInstance::avModel().getCurrentVideoCaptureDevice();
         auto deviceIndex = Utils::indexInVector(devices, currentCaptureDevice);
         for (auto d : devices) {
-            ui->deviceBox->addItem(QString::fromStdString(d).toUtf8());
+            try {
+                auto settings = LRCInstance::avModel().getDeviceSettings(d);
+                ui->deviceBox->addItem(QString::fromStdString(settings.name).toUtf8());
+            } catch (...) {}
         }
         ui->deviceBox->setCurrentIndex(deviceIndex);
         setFormatListForDevice(LRCInstance::avModel().getCurrentVideoCaptureDevice());
diff --git a/videoview.cpp b/videoview.cpp
index b1e97c5..82e9602 100644
--- a/videoview.cpp
+++ b/videoview.cpp
@@ -435,15 +435,17 @@ VideoView::resetPreview()
     } else {
         auto device = LRCInstance::avModel().getCurrentVideoCaptureDevice();
         if (device.empty()) {
-            device = LRCInstance::avModel().getDefaultDeviceName();
+            device = LRCInstance::avModel().getDefaultDevice();
         }
         if (device.empty()) {
             previewWidget_->setVisible(false);
             return;
         }
-        auto settings = LRCInstance::avModel().getDeviceSettings(device);
-        width = QString::fromStdString(settings.size).split("x")[0].toInt();
-        height = QString::fromStdString(settings.size).split("x")[1].toInt();
+        try {
+            auto settings = LRCInstance::avModel().getDeviceSettings(device);
+            width = QString::fromStdString(settings.size).split("x")[0].toInt();
+            height = QString::fromStdString(settings.size).split("x")[1].toInt();
+        } catch (...) {}
     }
     auto newSize = previewWidget_->getScaledSize(width, height);
     previewWidget_->setupGeometry(newSize);
-- 
GitLab