window-basic-settings-stream.cpp 12.7 KB
Newer Older
1 2 3
#include <QMessageBox>

#include "window-basic-settings.hpp"
4
#include "obs-frontend-api.h"
5 6 7 8
#include "obs-app.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"

9 10 11 12 13 14 15 16
#ifdef BROWSER_AVAILABLE
#include <browser-panel.hpp>
#include "auth-oauth.hpp"
#endif

struct QCef;
struct QCefCookieManager;

J
jp9000 已提交
17
extern QCef *cef;
18 19
extern QCefCookieManager *panel_cookies;

20 21 22 23 24 25
enum class ListOpt : int {
	ShowAll = 1,
	Custom,
};

enum class Section : int {
26
	Connect,
27 28 29 30 31 32 33 34 35 36
	StreamKey,
};

inline bool OBSBasicSettings::IsCustomService() const
{
	return ui->service->currentData().toInt() == (int)ListOpt::Custom;
}

void OBSBasicSettings::InitStreamPage()
{
37 38
	ui->connectAccount2->setVisible(false);
	ui->disconnectAccount->setVisible(false);
39
	ui->bandwidthTestEnable->setVisible(false);
40

41 42 43 44 45 46
	int vertSpacing = ui->topStreamLayout->verticalSpacing();

	QMargins m = ui->topStreamLayout->contentsMargins();
	m.setBottom(vertSpacing / 2);
	ui->topStreamLayout->setContentsMargins(m);

47 48 49 50
	m = ui->loginPageLayout->contentsMargins();
	m.setTop(vertSpacing / 2);
	ui->loginPageLayout->setContentsMargins(m);

51 52 53 54 55 56
	m = ui->streamkeyPageLayout->contentsMargins();
	m.setTop(vertSpacing / 2);
	ui->streamkeyPageLayout->setContentsMargins(m);

	LoadServices(false);

J
jp9000 已提交
57 58 59 60
	connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
		SLOT(UpdateServerList()));
	connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
		SLOT(UpdateKeyLink()));
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

void OBSBasicSettings::LoadStream1Settings()
{
	obs_service_t *service_obj = main->GetService();
	const char *type = obs_service_get_type(service_obj);

	loading = true;

	obs_data_t *settings = obs_service_get_settings(service_obj);

	const char *service = obs_data_get_string(settings, "service");
	const char *server = obs_data_get_string(settings, "server");
	const char *key = obs_data_get_string(settings, "key");

	if (strcmp(type, "rtmp_custom") == 0) {
		ui->service->setCurrentIndex(0);
		ui->customServer->setText(server);
79 80

		bool use_auth = obs_data_get_bool(settings, "use_auth");
J
jp9000 已提交
81 82 83 84
		const char *username =
			obs_data_get_string(settings, "username");
		const char *password =
			obs_data_get_string(settings, "password");
85 86 87
		ui->authUsername->setText(QT_UTF8(username));
		ui->authPw->setText(QT_UTF8(password));
		ui->useAuth->setChecked(use_auth);
88 89 90 91 92 93 94 95
	} else {
		int idx = ui->service->findText(service);
		if (idx == -1) {
			if (service && *service)
				ui->service->insertItem(1, service);
			idx = 1;
		}
		ui->service->setCurrentIndex(idx);
96 97 98

		bool bw_test = obs_data_get_bool(settings, "bwtest");
		ui->bandwidthTestEnable->setChecked(bw_test);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	}

	UpdateServerList();

	if (strcmp(type, "rtmp_common") == 0) {
		int idx = ui->server->findData(server);
		if (idx == -1) {
			if (server && *server)
				ui->server->insertItem(0, server, server);
			idx = 0;
		}
		ui->server->setCurrentIndex(idx);
	}

	ui->key->setText(key);

	lastService.clear();
	on_service_currentIndexChanged(0);

	obs_data_release(settings);

	UpdateKeyLink();

122 123 124
	bool streamActive = obs_frontend_streaming_active();
	ui->streamPage->setEnabled(!streamActive);

125 126 127 128 129 130
	loading = false;
}

void OBSBasicSettings::SaveStream1Settings()
{
	bool customServer = IsCustomService();
J
jp9000 已提交
131
	const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
132 133 134 135 136 137 138 139 140 141

	obs_service_t *oldService = main->GetService();
	OBSData hotkeyData = obs_hotkeys_save_service(oldService);
	obs_data_release(hotkeyData);

	OBSData settings = obs_data_create();
	obs_data_release(settings);

	if (!customServer) {
		obs_data_set_string(settings, "service",
J
jp9000 已提交
142 143 144 145
				    QT_TO_UTF8(ui->service->currentText()));
		obs_data_set_string(
			settings, "server",
			QT_TO_UTF8(ui->server->currentData().toString()));
146 147
	} else {
		obs_data_set_string(settings, "server",
J
jp9000 已提交
148
				    QT_TO_UTF8(ui->customServer->text()));
149
		obs_data_set_bool(settings, "use_auth",
J
jp9000 已提交
150
				  ui->useAuth->isChecked());
151
		if (ui->useAuth->isChecked()) {
J
jp9000 已提交
152 153 154
			obs_data_set_string(
				settings, "username",
				QT_TO_UTF8(ui->authUsername->text()));
155
			obs_data_set_string(settings, "password",
J
jp9000 已提交
156
					    QT_TO_UTF8(ui->authPw->text()));
157
		}
158 159
	}

J
jp9000 已提交
160 161
	obs_data_set_bool(settings, "bwtest",
			  ui->bandwidthTestEnable->isChecked());
162 163
	obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));

J
jp9000 已提交
164 165
	OBSService newService = obs_service_create(
		service_id, "default_service", settings, hotkeyData);
166 167 168 169 170 171 172
	obs_service_release(newService);

	if (!newService)
		return;

	main->SetService(newService);
	main->SaveService();
173 174 175
	main->auth = auth;
	if (!!main->auth)
		main->auth->LoadUI();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}

void OBSBasicSettings::UpdateKeyLink()
{
	bool custom = IsCustomService();
	QString serviceName = ui->service->currentText();

	if (custom)
		serviceName = "";

	QString text = QTStr("Basic.AutoConfig.StreamPage.StreamKey");
	if (serviceName == "Twitch") {
		text += " <a href=\"https://";
		text += "www.twitch.tv/broadcast/dashboard/streamkey";
		text += "\">";
J
jp9000 已提交
191 192
		text += QTStr(
			"Basic.AutoConfig.StreamPage.StreamKey.LinkToSite");
193 194 195 196 197
		text += "</a>";
	} else if (serviceName == "YouTube / YouTube Gaming") {
		text += " <a href=\"https://";
		text += "www.youtube.com/live_dashboard";
		text += "\">";
J
jp9000 已提交
198 199
		text += QTStr(
			"Basic.AutoConfig.StreamPage.StreamKey.LinkToSite");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		text += "</a>";
	}

	ui->streamKeyLabel->setText(text);
}

void OBSBasicSettings::LoadServices(bool showAll)
{
	obs_properties_t *props = obs_get_service_properties("rtmp_common");

	OBSData settings = obs_data_create();
	obs_data_release(settings);

	obs_data_set_bool(settings, "show_all", showAll);

	obs_property_t *prop = obs_properties_get(props, "show_all");
	obs_property_modified(prop, settings);

	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);
	}

	if (showAll)
		names.sort();

	for (QString &name : names)
		ui->service->addItem(name);

	if (!showAll) {
		ui->service->addItem(
			QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
			QVariant((int)ListOpt::ShowAll));
	}

J
jp9000 已提交
242 243 244
	ui->service->insertItem(
		0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
		QVariant((int)ListOpt::Custom));
245 246 247 248 249 250 251 252 253 254 255 256

	if (!lastService.isEmpty()) {
		int idx = ui->service->findText(lastService);
		if (idx != -1)
			ui->service->setCurrentIndex(idx);
	}

	obs_properties_destroy(props);

	ui->service->blockSignals(false);
}

257 258 259 260 261
static inline bool is_auth_service(const std::string &service)
{
	return Auth::AuthType(service) != Auth::Type::None;
}

262 263
void OBSBasicSettings::on_service_currentIndexChanged(int)
{
J
jp9000 已提交
264 265
	bool showMore = ui->service->currentData().toInt() ==
			(int)ListOpt::ShowAll;
266 267 268 269 270 271
	if (showMore)
		return;

	std::string service = QT_TO_UTF8(ui->service->currentText());
	bool custom = IsCustomService();

272
	ui->disconnectAccount->setVisible(false);
273
	ui->bandwidthTestEnable->setVisible(false);
274 275 276 277 278 279 280

#ifdef BROWSER_AVAILABLE
	if (cef) {
		if (lastService != service.c_str()) {
			QString key = ui->key->text();
			bool can_auth = is_auth_service(service);
			int page = can_auth && (!loading || key.isEmpty())
J
jp9000 已提交
281 282
					   ? (int)Section::Connect
					   : (int)Section::StreamKey;
283 284 285 286 287 288 289 290 291 292 293 294 295

			ui->streamStackWidget->setCurrentIndex(page);
			ui->streamKeyWidget->setVisible(true);
			ui->streamKeyLabel->setVisible(true);
			ui->connectAccount2->setVisible(can_auth);
		}
	} else {
		ui->connectAccount2->setVisible(false);
	}
#else
	ui->connectAccount2->setVisible(false);
#endif

296 297 298 299 300 301
	ui->useAuth->setVisible(custom);
	ui->authUsernameLabel->setVisible(custom);
	ui->authUsername->setVisible(custom);
	ui->authPwLabel->setVisible(custom);
	ui->authPwWidget->setVisible(custom);

302 303
	if (custom) {
		ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
J
jp9000 已提交
304
						   ui->serverStackedWidget);
305 306 307 308

		ui->serverStackedWidget->setCurrentIndex(1);
		ui->serverStackedWidget->setVisible(true);
		ui->serverLabel->setVisible(true);
309
		on_useAuth_toggled();
310 311 312
	} else {
		ui->serverStackedWidget->setCurrentIndex(0);
	}
313 314 315 316 317 318 319 320 321 322

#ifdef BROWSER_AVAILABLE
	auth.reset();

	if (!!main->auth &&
	    service.find(main->auth->service()) != std::string::npos) {
		auth = main->auth;
		OnAuthConnected();
	}
#endif
323 324 325 326 327
}

void OBSBasicSettings::UpdateServerList()
{
	QString serviceName = ui->service->currentText();
J
jp9000 已提交
328 329
	bool showMore = ui->service->currentData().toInt() ==
			(int)ListOpt::ShowAll;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

	if (showMore) {
		LoadServices(true);
		ui->service->showPopup();
		return;
	} else {
		lastService = serviceName;
	}

	obs_properties_t *props = obs_get_service_properties("rtmp_common");
	obs_property_t *services = obs_properties_get(props, "service");

	OBSData settings = obs_data_create();
	obs_data_release(settings);

	obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
	obs_property_modified(services, settings);

	obs_property_t *servers = obs_properties_get(props, "server");

	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);
	}

	obs_properties_destroy(props);
}

void OBSBasicSettings::on_show_clicked()
{
	if (ui->key->echoMode() == QLineEdit::Password) {
		ui->key->setEchoMode(QLineEdit::Normal);
		ui->show->setText(QTStr("Hide"));
	} else {
		ui->key->setEchoMode(QLineEdit::Password);
		ui->show->setText(QTStr("Show"));
	}
}

373 374 375 376 377 378 379 380 381 382 383
void OBSBasicSettings::on_authPwShow_clicked()
{
	if (ui->authPw->echoMode() == QLineEdit::Password) {
		ui->authPw->setEchoMode(QLineEdit::Normal);
		ui->authPwShow->setText(QTStr("Hide"));
	} else {
		ui->authPw->setEchoMode(QLineEdit::Password);
		ui->authPwShow->setText(QTStr("Show"));
	}
}

384 385 386 387 388 389 390 391 392 393
OBSService OBSBasicSettings::SpawnTempService()
{
	bool custom = IsCustomService();
	const char *service_id = custom ? "rtmp_custom" : "rtmp_common";

	OBSData settings = obs_data_create();
	obs_data_release(settings);

	if (!custom) {
		obs_data_set_string(settings, "service",
J
jp9000 已提交
394 395 396 397
				    QT_TO_UTF8(ui->service->currentText()));
		obs_data_set_string(
			settings, "server",
			QT_TO_UTF8(ui->server->currentData().toString()));
398 399
	} else {
		obs_data_set_string(settings, "server",
J
jp9000 已提交
400
				    QT_TO_UTF8(ui->customServer->text()));
401 402 403
	}
	obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));

J
jp9000 已提交
404 405
	OBSService newService = obs_service_create(service_id, "temp_service",
						   settings, nullptr);
406 407 408 409
	obs_service_release(newService);

	return newService;
}
410 411 412 413

void OBSBasicSettings::OnOAuthStreamKeyConnected()
{
#ifdef BROWSER_AVAILABLE
J
jp9000 已提交
414
	OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
415 416 417 418 419 420 421

	if (a) {
		bool validKey = !a->key().empty();

		if (validKey)
			ui->key->setText(QT_UTF8(a->key().c_str()));

422 423 424 425
		ui->streamKeyWidget->setVisible(false);
		ui->streamKeyLabel->setVisible(false);
		ui->connectAccount2->setVisible(false);
		ui->disconnectAccount->setVisible(true);
426 427 428

		if (strcmp(a->service(), "Twitch") == 0)
			ui->bandwidthTestEnable->setVisible(true);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	}

	ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
#endif
}

void OBSBasicSettings::OnAuthConnected()
{
	std::string service = QT_TO_UTF8(ui->service->currentText());
	Auth::Type type = Auth::AuthType(service);

	if (type == Auth::Type::OAuth_StreamKey) {
		OnOAuthStreamKeyConnected();
	}

	if (!loading) {
		stream1Changed = true;
		EnableApplyButton(true);
	}
}

void OBSBasicSettings::on_connectAccount_clicked()
{
#ifdef BROWSER_AVAILABLE
	std::string service = QT_TO_UTF8(ui->service->currentText());

	auth = OAuthStreamKey::Login(this, service);
	if (!!auth)
		OnAuthConnected();
#endif
}

#define DISCONNECT_COMFIRM_TITLE \
	"Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
#define DISCONNECT_COMFIRM_TEXT \
	"Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"

void OBSBasicSettings::on_disconnectAccount_clicked()
{
	QMessageBox::StandardButton button;

J
jp9000 已提交
470 471
	button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
					 QTStr(DISCONNECT_COMFIRM_TEXT));
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	if (button == QMessageBox::No) {
		return;
	}

	main->auth.reset();
	auth.reset();

	std::string service = QT_TO_UTF8(ui->service->currentText());

#ifdef BROWSER_AVAILABLE
	OAuth::DeleteCookies(service);
#endif

	ui->streamKeyWidget->setVisible(true);
	ui->streamKeyLabel->setVisible(true);
	ui->connectAccount2->setVisible(true);
	ui->disconnectAccount->setVisible(false);
490
	ui->bandwidthTestEnable->setVisible(false);
491 492 493 494 495 496 497
	ui->key->setText("");
}

void OBSBasicSettings::on_useStreamKey_clicked()
{
	ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
}
498 499 500 501 502 503 504 505 506 507 508 509 510

void OBSBasicSettings::on_useAuth_toggled()
{
	if (!IsCustomService())
		return;

	bool use_auth = ui->useAuth->isChecked();

	ui->authUsernameLabel->setVisible(use_auth);
	ui->authUsername->setVisible(use_auth);
	ui->authPwLabel->setVisible(use_auth);
	ui->authPwWidget->setVisible(use_auth);
}