UI: Just use json directly for service lookups

This commit accomplishes three different things:

- Makes it much less difficult to communicate service settings
  between the UI and the plugin.

- Refactors some code and makes it cleaner and easier to modify to our
  needs (although there is still a lot of work to do on that front
  because of heavy code duplication issues between auto-config and the
  stream settings pane).

- Significantly reatly reduces the number of times the json file has to
  be opened and parsed.

This also kind of denotes a bit of a failure on the plugin communication
aspect. The properties system is too limited and jank for a lot of
things we would like to do at times.
This commit is contained in:
jp9000
2022-04-14 01:54:01 -07:00
parent a4c215869f
commit 2fa5ffe4df
8 changed files with 178 additions and 142 deletions

View File

@@ -2,6 +2,7 @@
#include <QUrl>
#include "window-basic-settings.hpp"
#include "streaming-helpers.hpp"
#include "obs-frontend-api.h"
#include "obs-app.hpp"
#include "window-basic-main.hpp"
@@ -20,6 +21,8 @@
#include "youtube-api-wrappers.hpp"
#endif
using namespace json11;
struct QCef;
struct QCefCookieManager;
@@ -246,6 +249,14 @@ void OBSBasicSettings::SaveStream1Settings()
SaveCheckBox(ui->ignoreRecommended, "Stream1", "IgnoreRecommended");
}
inline void OBSBasicSettings::GetServicesJson()
{
if (!servicesLoaded && servicesRoot.is_null()) {
servicesRoot = get_services_json();
servicesLoaded = true;
}
}
void OBSBasicSettings::UpdateMoreInfoLink()
{
if (IsCustomService()) {
@@ -254,42 +265,32 @@ void OBSBasicSettings::UpdateMoreInfoLink()
}
QString serviceName = ui->service->currentText();
obs_properties_t *props = obs_get_service_properties("rtmp_common");
obs_property_t *services = obs_properties_get(props, "service");
OBSDataAutoRelease settings = obs_data_create();
GetServicesJson();
Json service =
get_service_from_json(servicesRoot, QT_TO_UTF8(serviceName));
obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
obs_property_modified(services, settings);
const std::string &more_info_link =
service["more_info_link"].string_value();
const char *more_info_link =
obs_data_get_string(settings, "more_info_link");
if (!more_info_link || (*more_info_link == '\0')) {
if (more_info_link.empty()) {
ui->moreInfoButton->hide();
} else {
ui->moreInfoButton->setTargetUrl(QUrl(more_info_link));
ui->moreInfoButton->setTargetUrl(QUrl(more_info_link.c_str()));
ui->moreInfoButton->show();
}
obs_properties_destroy(props);
}
void OBSBasicSettings::UpdateKeyLink()
{
QString serviceName = ui->service->currentText();
QString customServer = ui->customServer->text().trimmed();
QString streamKeyLink;
obs_properties_t *props = obs_get_service_properties("rtmp_common");
obs_property_t *services = obs_properties_get(props, "service");
OBSDataAutoRelease settings = obs_data_create();
obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
obs_property_modified(services, settings);
streamKeyLink = obs_data_get_string(settings, "stream_key_link");
GetServicesJson();
Json service =
get_service_from_json(servicesRoot, QT_TO_UTF8(serviceName));
std::string streamKeyLink = service["stream_key_link"].string_value();
if (customServer.contains("fbcdn.net") && IsCustomService()) {
streamKeyLink =
"https://www.facebook.com/live/producer?ref=OBS";
@@ -303,37 +304,29 @@ void OBSBasicSettings::UpdateKeyLink()
QTStr("Basic.AutoConfig.StreamPage.StreamKey"));
}
if (QString(streamKeyLink).isNull() ||
QString(streamKeyLink).isEmpty()) {
if (streamKeyLink.empty()) {
ui->getStreamKeyButton->hide();
} else {
ui->getStreamKeyButton->setTargetUrl(QUrl(streamKeyLink));
ui->getStreamKeyButton->setTargetUrl(
QUrl(streamKeyLink.c_str()));
ui->getStreamKeyButton->show();
}
obs_properties_destroy(props);
}
void OBSBasicSettings::LoadServices(bool showAll)
{
obs_properties_t *props = obs_get_service_properties("rtmp_common");
OBSDataAutoRelease settings = obs_data_create();
obs_data_set_bool(settings, "show_all", showAll);
obs_property_t *prop = obs_properties_get(props, "show_all");
obs_property_modified(prop, settings);
GetServicesJson();
auto &services = servicesRoot["services"].array_items();
ui->service->blockSignals(true);
ui->service->clear();
QStringList names;
obs_property_t *services = obs_properties_get(props, "service");
size_t services_count = obs_property_list_item_count(services);
for (size_t i = 0; i < services_count; i++) {
const char *name = obs_property_list_item_string(services, i);
names.push_back(name);
for (const Json &service : services) {
if (!showAll && !service["common"].bool_value())
continue;
names.push_back(service["name"].string_value().c_str());
}
if (showAll)
@@ -358,8 +351,6 @@ void OBSBasicSettings::LoadServices(bool showAll)
ui->service->setCurrentIndex(idx);
}
obs_properties_destroy(props);
ui->service->blockSignals(false);
}
@@ -498,26 +489,17 @@ void OBSBasicSettings::UpdateServerList()
lastService = serviceName;
}
obs_properties_t *props = obs_get_service_properties("rtmp_common");
obs_property_t *services = obs_properties_get(props, "service");
OBSDataAutoRelease settings = obs_data_create();
obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
obs_property_modified(services, settings);
obs_property_t *servers = obs_properties_get(props, "server");
GetServicesJson();
Json service =
get_service_from_json(servicesRoot, QT_TO_UTF8(serviceName));
ui->server->clear();
size_t servers_count = obs_property_list_item_count(servers);
for (size_t i = 0; i < servers_count; i++) {
const char *name = obs_property_list_item_name(servers, i);
const char *server = obs_property_list_item_string(servers, i);
ui->server->addItem(name, server);
auto &servers = service["servers"].array_items();
for (const Json &server : servers) {
ui->server->addItem(server["name"].string_value().c_str(),
server["url"].string_value().c_str());
}
obs_properties_destroy(props);
}
void OBSBasicSettings::on_show_clicked()