From 6e53c897038cf4240c1eada4dcaad0972852bfc4 Mon Sep 17 00:00:00 2001 From: Johann Garces Date: Mon, 19 Oct 2020 13:15:09 -0700 Subject: [PATCH] UI: Remove QtNetwork Dependency from Settings UI OBS does not ship OpenSSL and has a curl helper already to use. --- CMakeLists.txt | 1 - UI/CMakeLists.txt | 2 - .../encoder-settings-provider-facebook.cpp | 99 +++++++++++-------- .../encoder-settings-provider-facebook.hpp | 11 +-- UI/pre-stream-wizard/pre-stream-wizard.cpp | 4 +- cmake/Modules/CopyMSVCBins.cmake | 2 - 6 files changed, 62 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 99d05ac7610cce..6e4a67175808ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -257,7 +257,6 @@ if(NOT INSTALLER_RUN) endif() find_package(Qt5Widgets ${FIND_MODE}) - find_package(Qt5Network ${FIND_MODE}) endif() add_subdirectory(deps) diff --git a/UI/CMakeLists.txt b/UI/CMakeLists.txt index 449c166da58db4..7a852690fd915b 100644 --- a/UI/CMakeLists.txt +++ b/UI/CMakeLists.txt @@ -50,7 +50,6 @@ set(CMAKE_AUTOMOC TRUE) find_package(Qt5Widgets ${FIND_MODE}) find_package(Qt5Svg ${FIND_MODE}) find_package(Qt5Xml ${FIND_MODE}) -find_package(Qt5Network ${FIND_MODE}) find_package(FFmpeg REQUIRED COMPONENTS avcodec avutil avformat) @@ -422,7 +421,6 @@ target_link_libraries(obs Qt5::Widgets Qt5::Svg Qt5::Xml - Qt5::Network obs-frontend-api ${FFMPEG_LIBRARIES} ${LIBCURL_LIBRARIES} diff --git a/UI/pre-stream-wizard/encoder-settings-provider-facebook.cpp b/UI/pre-stream-wizard/encoder-settings-provider-facebook.cpp index 7ba4a23c46f6eb..733f33c56909b2 100644 --- a/UI/pre-stream-wizard/encoder-settings-provider-facebook.cpp +++ b/UI/pre-stream-wizard/encoder-settings-provider-facebook.cpp @@ -5,25 +5,24 @@ #include #include #include -#include -#include #include #include #include #include #include -#include #include "obs-app.hpp" #include "obs-config.h" +#include "remote-text.hpp" +#include + namespace StreamWizard { FacebookEncoderSettingsProvider::FacebookEncoderSettingsProvider(QObject *parent) : QObject(parent) { currentSettings_ = nullptr; - restclient_ = new QNetworkAccessManager(this); } void FacebookEncoderSettingsProvider::setEncoderRequest( @@ -36,10 +35,11 @@ void FacebookEncoderSettingsProvider::run() { // Base URL for request QUrl requestUrl( - "https://graph.facebook.com/v6.0/video_encoder_settings"); + "https://graph.facebook.com/v8.0/video_encoder_settings"); QUrlQuery inputVideoSettingsQuery = inputVideoQueryFromCurrentSettings(); requestUrl.setQuery(inputVideoSettingsQuery); + if (requestUrl.isValid()) { makeRequest(requestUrl); } else { @@ -51,21 +51,41 @@ void FacebookEncoderSettingsProvider::run() void FacebookEncoderSettingsProvider::makeRequest(QUrl &url) { - blog(LOG_INFO, "FacebookEncoderSettingsProvider creating request"); - QNetworkRequest request(url); - request.setHeader(QNetworkRequest::ContentTypeHeader, - "application/json"); - - // GET is made async - networkReply_ = restclient_->get(request); - pendingResponse_ = true; - // This is the callback when data is ready - connect(restclient_, &QNetworkAccessManager::finished, this, - &FacebookEncoderSettingsProvider::handleResponse); - - // This is a fast API, timeout at 3 seconds and show error - QTimer::singleShot(3000, this, - &FacebookEncoderSettingsProvider::handleTimeout); + blog(LOG_INFO, "FacebookEncoderSettingsProvider sending request"); + + bool requestSuccess = false; + + std::string urlString = url.toString().toStdString(); + std::string reply; + std::string error; + long responseCode = 0; + const char *contentType = "application/json"; + const char *postData = nullptr; + std::vector extraHeaders = std::vector(); + int timeout = 3; // seconds + + auto apiRequestBlock = [&]() { + requestSuccess = GetRemoteFile(urlString.c_str(), reply, error, + &responseCode, contentType, + postData, extraHeaders, nullptr, + timeout); + }; + + ExecuteFuncSafeBlock(apiRequestBlock); + + if (!requestSuccess || responseCode >= 400) { + handleTimeout(); + blog(LOG_WARNING, "Server response with error: %s", + error.c_str()); + } + + if (reply.empty()) { + handleEmpty(); + blog(LOG_WARNING, "Server response was empty"); + } + + QByteArray jsonBytes = QByteArray::fromStdString(reply); + handleResponse(jsonBytes); } QUrlQuery FacebookEncoderSettingsProvider::inputVideoQueryFromCurrentSettings() @@ -182,23 +202,9 @@ void addBool(const QJsonObject &json, const char *jsonKey, SettingsMap *map, } } -void FacebookEncoderSettingsProvider::handleResponse(QNetworkReply *reply) +void FacebookEncoderSettingsProvider::handleResponse(QByteArray reply) { - // In timeout and errors this method may still be called - if (!pendingResponse_) { - return; - } - pendingResponse_ = false; - - if (reply->error()) { - emit returnErrorDescription( - QTStr("PreLiveWizard.Configure.Error.JsonParse"), - reply->errorString()); - } - // Converts byte array into JSON doc as a copy - QByteArray replyAll = reply->readAll(); - QJsonDocument jsonDoc = QJsonDocument::fromJson(replyAll); - reply->deleteLater(); + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply); // Parse bytes to json object if (!jsonDoc.isObject()) { @@ -230,6 +236,10 @@ void FacebookEncoderSettingsProvider::handleResponse(QNetworkReply *reply) QJsonObject videoSettingsJsob = rmtpSettings["video_codec_settings"].toObject(); + if (videoSettingsJsob.isEmpty()) { + handleEmpty(); + } + // Create map to send to wizard SettingsMap *settingsMap = new SettingsMap(); @@ -260,6 +270,11 @@ void FacebookEncoderSettingsProvider::handleResponse(QNetworkReply *reply) addInt(videoSettingsJsob, "buffer_size", settingsMap, SettingsResponseKeys.streamBufferSize); + // If Empty emit to empty / error state + if (settingsMap->isEmpty()) { + handleEmpty(); + } + // Wrap into shared pointer and emit QSharedPointer settingsMapShrdPtr = QSharedPointer(settingsMap); @@ -268,16 +283,18 @@ void FacebookEncoderSettingsProvider::handleResponse(QNetworkReply *reply) void FacebookEncoderSettingsProvider::handleTimeout() { - if (!pendingResponse_) - return; - - pendingResponse_ = false; - QString errorTitle = QTStr("PreLiveWizard.Configure.Error.JsonParse"); QString errorDescription = QTStr("PreLiveWizard.Error.NetworkTimeout"); emit returnErrorDescription(errorTitle, errorDescription); } +void FacebookEncoderSettingsProvider::handleEmpty() +{ + emit returnErrorDescription( + QTStr("PreLiveWizard.Configure.Error.NoData"), + QTStr("PreLiveWizard.Configure.Error.NoData.Description")); +} + void FacebookEncoderSettingsProvider::jsonParseError() { emit returnErrorDescription( diff --git a/UI/pre-stream-wizard/encoder-settings-provider-facebook.hpp b/UI/pre-stream-wizard/encoder-settings-provider-facebook.hpp index a06ddd51a1cbed..b665f73bdcf74d 100644 --- a/UI/pre-stream-wizard/encoder-settings-provider-facebook.hpp +++ b/UI/pre-stream-wizard/encoder-settings-provider-facebook.hpp @@ -6,8 +6,6 @@ #include #include #include -#include -#include #include "pre-stream-current-settings.hpp" @@ -36,18 +34,15 @@ class FacebookEncoderSettingsProvider : public QObject { private: QSharedPointer currentSettings_; - QNetworkAccessManager *restclient_; - QNetworkReply *networkReply_; bool pendingResponse_ = false; void makeRequest(QUrl &url); QUrlQuery inputVideoQueryFromCurrentSettings(); QString getOBSVersionString(); - void jsonParseError(); - -private slots: - void handleResponse(QNetworkReply *reply); + void handleResponse(QByteArray reply); void handleTimeout(); + void handleEmpty(); + void jsonParseError(); }; } // namespace StreamWizard diff --git a/UI/pre-stream-wizard/pre-stream-wizard.cpp b/UI/pre-stream-wizard/pre-stream-wizard.cpp index 1ba41777582716..8bb9c9fe9e2c81 100644 --- a/UI/pre-stream-wizard/pre-stream-wizard.cpp +++ b/UI/pre-stream-wizard/pre-stream-wizard.cpp @@ -123,7 +123,6 @@ void PreStreamWizard::onPageChanged(int id) case Page_Loading: requestSettings(); - setButtons(CancelOnly); break; case Page_Selections: @@ -134,8 +133,7 @@ void PreStreamWizard::onPageChanged(int id) case Page_Complete: setButtons(FinishOnly); if (newSettingsMap_ != nullptr && !newSettingsMap_.isNull()) { - // ToDo: messaging in edge case this could be empty - // and still make it here? + // ToDo: messaging in edge case this could be empty? emit applySettings(newSettingsMap_); }; break; diff --git a/cmake/Modules/CopyMSVCBins.cmake b/cmake/Modules/CopyMSVCBins.cmake index 938fc853730f47..504d7b1ca511d0 100644 --- a/cmake/Modules/CopyMSVCBins.cmake +++ b/cmake/Modules/CopyMSVCBins.cmake @@ -159,7 +159,6 @@ file(GLOB QT_DEBUG_BIN_FILES "${Qt5Core_DIR}/../../../bin/Qt5Widgetsd.dll" "${Qt5Core_DIR}/../../../bin/Qt5Svgd.dll" "${Qt5Core_DIR}/../../../bin/Qt5Xmld.dll" - "${Qt5Core_DIR}/../../../bin/Qt5Networkd.dll" "${Qt5Core_DIR}/../../../bin/libGLESv2d.dll" "${Qt5Core_DIR}/../../../bin/libEGLd.dll") file(GLOB QT_DEBUG_PLAT_BIN_FILES @@ -178,7 +177,6 @@ file(GLOB QT_BIN_FILES "${Qt5Core_DIR}/../../../bin/Qt5Widgets.dll" "${Qt5Core_DIR}/../../../bin/Qt5Svg.dll" "${Qt5Core_DIR}/../../../bin/Qt5Xml.dll" - "${Qt5Core_DIR}/../../../bin/Qt5Network.dll" "${Qt5Core_DIR}/../../../bin/libGLESv2.dll" "${Qt5Core_DIR}/../../../bin/libEGL.dll") file(GLOB QT_PLAT_BIN_FILES