Skip to content

Commit

Permalink
UI: Remove QtNetwork Dependency from Settings UI
Browse files Browse the repository at this point in the history
OBS does not ship OpenSSL and has a curl helper already to use.
  • Loading branch information
JohannMG committed Oct 20, 2020
1 parent 8a16363 commit 6e53c89
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 57 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ if(NOT INSTALLER_RUN)
endif()

find_package(Qt5Widgets ${FIND_MODE})
find_package(Qt5Network ${FIND_MODE})
endif()

add_subdirectory(deps)
Expand Down
2 changes: 0 additions & 2 deletions UI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -422,7 +421,6 @@ target_link_libraries(obs
Qt5::Widgets
Qt5::Svg
Qt5::Xml
Qt5::Network
obs-frontend-api
${FFMPEG_LIBRARIES}
${LIBCURL_LIBRARIES}
Expand Down
99 changes: 58 additions & 41 deletions UI/pre-stream-wizard/encoder-settings-provider-facebook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
#include <QUrlQuery>
#include <QList>
#include <QPair>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMap>
#include <QTimer>

#include "obs-app.hpp"
#include "obs-config.h"

#include "remote-text.hpp"
#include <qt-wrappers.hpp>

namespace StreamWizard {

FacebookEncoderSettingsProvider::FacebookEncoderSettingsProvider(QObject *parent)
: QObject(parent)
{
currentSettings_ = nullptr;
restclient_ = new QNetworkAccessManager(this);
}

void FacebookEncoderSettingsProvider::setEncoderRequest(
Expand All @@ -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 {
Expand All @@ -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<std::string> extraHeaders = std::vector<std::string>();
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()
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<SettingsMap> settingsMapShrdPtr =
QSharedPointer<SettingsMap>(settingsMap);
Expand All @@ -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(
Expand Down
11 changes: 3 additions & 8 deletions UI/pre-stream-wizard/encoder-settings-provider-facebook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <QUrlQuery>
#include <QUrl>
#include <QSize>
#include <QNetworkAccessManager>
#include <QNetworkReply>

#include "pre-stream-current-settings.hpp"

Expand Down Expand Up @@ -36,18 +34,15 @@ class FacebookEncoderSettingsProvider : public QObject {

private:
QSharedPointer<EncoderSettingsRequest> 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
4 changes: 1 addition & 3 deletions UI/pre-stream-wizard/pre-stream-wizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ void PreStreamWizard::onPageChanged(int id)

case Page_Loading:
requestSettings();
setButtons(CancelOnly);
break;

case Page_Selections:
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions cmake/Modules/CopyMSVCBins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6e53c89

Please sign in to comment.