window-basic-settings-stream.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #include <QMessageBox>
  2. #include <QUrl>
  3. #include "window-basic-settings.hpp"
  4. #include "obs-frontend-api.h"
  5. #include "obs-app.hpp"
  6. #include "window-basic-main.hpp"
  7. #include "qt-wrappers.hpp"
  8. #include "url-push-button.hpp"
  9. #ifdef BROWSER_AVAILABLE
  10. #include <browser-panel.hpp>
  11. #include "auth-oauth.hpp"
  12. #endif
  13. struct QCef;
  14. struct QCefCookieManager;
  15. extern QCef *cef;
  16. extern QCefCookieManager *panel_cookies;
  17. enum class ListOpt : int {
  18. ShowAll = 1,
  19. Custom,
  20. };
  21. enum class Section : int {
  22. Connect,
  23. StreamKey,
  24. };
  25. inline bool OBSBasicSettings::IsCustomService() const
  26. {
  27. return ui->service->currentData().toInt() == (int)ListOpt::Custom;
  28. }
  29. void OBSBasicSettings::InitStreamPage()
  30. {
  31. ui->connectAccount2->setVisible(false);
  32. ui->disconnectAccount->setVisible(false);
  33. ui->bandwidthTestEnable->setVisible(false);
  34. ui->twitchAddonDropdown->setVisible(false);
  35. ui->twitchAddonLabel->setVisible(false);
  36. int vertSpacing = ui->topStreamLayout->verticalSpacing();
  37. QMargins m = ui->topStreamLayout->contentsMargins();
  38. m.setBottom(vertSpacing / 2);
  39. ui->topStreamLayout->setContentsMargins(m);
  40. m = ui->loginPageLayout->contentsMargins();
  41. m.setTop(vertSpacing / 2);
  42. ui->loginPageLayout->setContentsMargins(m);
  43. m = ui->streamkeyPageLayout->contentsMargins();
  44. m.setTop(vertSpacing / 2);
  45. ui->streamkeyPageLayout->setContentsMargins(m);
  46. LoadServices(false);
  47. ui->twitchAddonDropdown->addItem(
  48. QTStr("Basic.Settings.Stream.TTVAddon.None"));
  49. ui->twitchAddonDropdown->addItem(
  50. QTStr("Basic.Settings.Stream.TTVAddon.BTTV"));
  51. ui->twitchAddonDropdown->addItem(
  52. QTStr("Basic.Settings.Stream.TTVAddon.FFZ"));
  53. ui->twitchAddonDropdown->addItem(
  54. QTStr("Basic.Settings.Stream.TTVAddon.Both"));
  55. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  56. SLOT(UpdateServerList()));
  57. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  58. SLOT(UpdateKeyLink()));
  59. connect(ui->customServer, SIGNAL(textChanged(const QString &)), this,
  60. SLOT(UpdateKeyLink()));
  61. connect(ui->customServer, SIGNAL(editingFinished(const QString &)),
  62. this, SLOT(UpdateKeyLink()));
  63. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  64. SLOT(UpdateMoreInfoLink()));
  65. }
  66. void OBSBasicSettings::LoadStream1Settings()
  67. {
  68. obs_service_t *service_obj = main->GetService();
  69. const char *type = obs_service_get_type(service_obj);
  70. loading = true;
  71. obs_data_t *settings = obs_service_get_settings(service_obj);
  72. const char *service = obs_data_get_string(settings, "service");
  73. const char *server = obs_data_get_string(settings, "server");
  74. const char *key = obs_data_get_string(settings, "key");
  75. if (strcmp(type, "rtmp_custom") == 0) {
  76. ui->service->setCurrentIndex(0);
  77. ui->customServer->setText(server);
  78. bool use_auth = obs_data_get_bool(settings, "use_auth");
  79. const char *username =
  80. obs_data_get_string(settings, "username");
  81. const char *password =
  82. obs_data_get_string(settings, "password");
  83. ui->authUsername->setText(QT_UTF8(username));
  84. ui->authPw->setText(QT_UTF8(password));
  85. ui->useAuth->setChecked(use_auth);
  86. } else {
  87. int idx = ui->service->findText(service);
  88. if (idx == -1) {
  89. if (service && *service)
  90. ui->service->insertItem(1, service);
  91. idx = 1;
  92. }
  93. ui->service->setCurrentIndex(idx);
  94. bool bw_test = obs_data_get_bool(settings, "bwtest");
  95. ui->bandwidthTestEnable->setChecked(bw_test);
  96. idx = config_get_int(main->Config(), "Twitch", "AddonChoice");
  97. ui->twitchAddonDropdown->setCurrentIndex(idx);
  98. }
  99. UpdateServerList();
  100. if (strcmp(type, "rtmp_common") == 0) {
  101. int idx = ui->server->findData(server);
  102. if (idx == -1) {
  103. if (server && *server)
  104. ui->server->insertItem(0, server, server);
  105. idx = 0;
  106. }
  107. ui->server->setCurrentIndex(idx);
  108. }
  109. ui->key->setText(key);
  110. lastService.clear();
  111. on_service_currentIndexChanged(0);
  112. obs_data_release(settings);
  113. UpdateKeyLink();
  114. UpdateMoreInfoLink();
  115. bool streamActive = obs_frontend_streaming_active();
  116. ui->streamPage->setEnabled(!streamActive);
  117. loading = false;
  118. }
  119. void OBSBasicSettings::SaveStream1Settings()
  120. {
  121. bool customServer = IsCustomService();
  122. const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
  123. obs_service_t *oldService = main->GetService();
  124. OBSData hotkeyData = obs_hotkeys_save_service(oldService);
  125. obs_data_release(hotkeyData);
  126. OBSData settings = obs_data_create();
  127. obs_data_release(settings);
  128. if (!customServer) {
  129. obs_data_set_string(settings, "service",
  130. QT_TO_UTF8(ui->service->currentText()));
  131. obs_data_set_string(
  132. settings, "server",
  133. QT_TO_UTF8(ui->server->currentData().toString()));
  134. } else {
  135. obs_data_set_string(settings, "server",
  136. QT_TO_UTF8(ui->customServer->text()));
  137. obs_data_set_bool(settings, "use_auth",
  138. ui->useAuth->isChecked());
  139. if (ui->useAuth->isChecked()) {
  140. obs_data_set_string(
  141. settings, "username",
  142. QT_TO_UTF8(ui->authUsername->text()));
  143. obs_data_set_string(settings, "password",
  144. QT_TO_UTF8(ui->authPw->text()));
  145. }
  146. }
  147. if (!!auth && strcmp(auth->service(), "Twitch") == 0) {
  148. bool choiceExists = config_has_user_value(
  149. main->Config(), "Twitch", "AddonChoice");
  150. int currentChoice =
  151. config_get_int(main->Config(), "Twitch", "AddonChoice");
  152. int newChoice = ui->twitchAddonDropdown->currentIndex();
  153. config_set_int(main->Config(), "Twitch", "AddonChoice",
  154. newChoice);
  155. if (choiceExists && currentChoice != newChoice)
  156. forceAuthReload = true;
  157. obs_data_set_bool(settings, "bwtest",
  158. ui->bandwidthTestEnable->isChecked());
  159. } else {
  160. obs_data_set_bool(settings, "bwtest", false);
  161. }
  162. obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));
  163. OBSService newService = obs_service_create(
  164. service_id, "default_service", settings, hotkeyData);
  165. obs_service_release(newService);
  166. if (!newService)
  167. return;
  168. main->SetService(newService);
  169. main->SaveService();
  170. main->auth = auth;
  171. if (!!main->auth)
  172. main->auth->LoadUI();
  173. }
  174. void OBSBasicSettings::UpdateMoreInfoLink()
  175. {
  176. if (IsCustomService()) {
  177. ui->moreInfoButton->hide();
  178. return;
  179. }
  180. QString serviceName = ui->service->currentText();
  181. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  182. obs_property_t *services = obs_properties_get(props, "service");
  183. OBSData settings = obs_data_create();
  184. obs_data_release(settings);
  185. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  186. obs_property_modified(services, settings);
  187. const char *more_info_link =
  188. obs_data_get_string(settings, "more_info_link");
  189. if (!more_info_link || (*more_info_link == '\0')) {
  190. ui->moreInfoButton->hide();
  191. } else {
  192. ui->moreInfoButton->setTargetUrl(QUrl(more_info_link));
  193. ui->moreInfoButton->show();
  194. }
  195. obs_properties_destroy(props);
  196. }
  197. void OBSBasicSettings::UpdateKeyLink()
  198. {
  199. QString serviceName = ui->service->currentText();
  200. QString customServer = ui->customServer->text();
  201. QString streamKeyLink;
  202. if (serviceName == "Twitch") {
  203. streamKeyLink =
  204. "https://www.twitch.tv/broadcast/dashboard/streamkey";
  205. } else if (serviceName.startsWith("YouTube")) {
  206. streamKeyLink = "https://www.youtube.com/live_dashboard";
  207. } else if (serviceName.startsWith("Restream.io")) {
  208. streamKeyLink =
  209. "https://restream.io/settings/streaming-setup?from=OBS";
  210. } else if (serviceName == "Facebook Live" ||
  211. (customServer.contains("fbcdn.net") && IsCustomService())) {
  212. streamKeyLink =
  213. "https://www.facebook.com/live/producer?ref=OBS";
  214. } else if (serviceName.startsWith("Twitter")) {
  215. streamKeyLink = "https://www.pscp.tv/account/producer";
  216. } else if (serviceName.startsWith("YouStreamer")) {
  217. streamKeyLink = "https://app.youstreamer.com/stream/";
  218. } else if (serviceName == "Trovo") {
  219. streamKeyLink = "https://studio.trovo.live/mychannel/stream";
  220. }
  221. if (QString(streamKeyLink).isNull()) {
  222. ui->getStreamKeyButton->hide();
  223. } else {
  224. ui->getStreamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  225. ui->getStreamKeyButton->show();
  226. }
  227. }
  228. void OBSBasicSettings::LoadServices(bool showAll)
  229. {
  230. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  231. OBSData settings = obs_data_create();
  232. obs_data_release(settings);
  233. obs_data_set_bool(settings, "show_all", showAll);
  234. obs_property_t *prop = obs_properties_get(props, "show_all");
  235. obs_property_modified(prop, settings);
  236. ui->service->blockSignals(true);
  237. ui->service->clear();
  238. QStringList names;
  239. obs_property_t *services = obs_properties_get(props, "service");
  240. size_t services_count = obs_property_list_item_count(services);
  241. for (size_t i = 0; i < services_count; i++) {
  242. const char *name = obs_property_list_item_string(services, i);
  243. names.push_back(name);
  244. }
  245. if (showAll)
  246. names.sort(Qt::CaseInsensitive);
  247. for (QString &name : names)
  248. ui->service->addItem(name);
  249. if (!showAll) {
  250. ui->service->addItem(
  251. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  252. QVariant((int)ListOpt::ShowAll));
  253. }
  254. ui->service->insertItem(
  255. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  256. QVariant((int)ListOpt::Custom));
  257. if (!lastService.isEmpty()) {
  258. int idx = ui->service->findText(lastService);
  259. if (idx != -1)
  260. ui->service->setCurrentIndex(idx);
  261. }
  262. obs_properties_destroy(props);
  263. ui->service->blockSignals(false);
  264. }
  265. static inline bool is_auth_service(const std::string &service)
  266. {
  267. return Auth::AuthType(service) != Auth::Type::None;
  268. }
  269. void OBSBasicSettings::on_service_currentIndexChanged(int)
  270. {
  271. bool showMore = ui->service->currentData().toInt() ==
  272. (int)ListOpt::ShowAll;
  273. if (showMore)
  274. return;
  275. std::string service = QT_TO_UTF8(ui->service->currentText());
  276. bool custom = IsCustomService();
  277. ui->disconnectAccount->setVisible(false);
  278. ui->bandwidthTestEnable->setVisible(false);
  279. ui->twitchAddonDropdown->setVisible(false);
  280. ui->twitchAddonLabel->setVisible(false);
  281. #ifdef BROWSER_AVAILABLE
  282. if (cef) {
  283. if (lastService != service.c_str()) {
  284. QString key = ui->key->text();
  285. bool can_auth = is_auth_service(service);
  286. int page = can_auth && (!loading || key.isEmpty())
  287. ? (int)Section::Connect
  288. : (int)Section::StreamKey;
  289. ui->streamStackWidget->setCurrentIndex(page);
  290. ui->streamKeyWidget->setVisible(true);
  291. ui->streamKeyLabel->setVisible(true);
  292. ui->connectAccount2->setVisible(can_auth);
  293. }
  294. } else {
  295. ui->connectAccount2->setVisible(false);
  296. }
  297. #else
  298. ui->connectAccount2->setVisible(false);
  299. #endif
  300. ui->useAuth->setVisible(custom);
  301. ui->authUsernameLabel->setVisible(custom);
  302. ui->authUsername->setVisible(custom);
  303. ui->authPwLabel->setVisible(custom);
  304. ui->authPwWidget->setVisible(custom);
  305. if (custom) {
  306. ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
  307. ui->serverStackedWidget);
  308. ui->serverStackedWidget->setCurrentIndex(1);
  309. ui->serverStackedWidget->setVisible(true);
  310. ui->serverLabel->setVisible(true);
  311. on_useAuth_toggled();
  312. } else {
  313. ui->serverStackedWidget->setCurrentIndex(0);
  314. }
  315. #ifdef BROWSER_AVAILABLE
  316. auth.reset();
  317. if (!!main->auth &&
  318. service.find(main->auth->service()) != std::string::npos) {
  319. auth = main->auth;
  320. OnAuthConnected();
  321. }
  322. #endif
  323. }
  324. void OBSBasicSettings::UpdateServerList()
  325. {
  326. QString serviceName = ui->service->currentText();
  327. bool showMore = ui->service->currentData().toInt() ==
  328. (int)ListOpt::ShowAll;
  329. if (showMore) {
  330. LoadServices(true);
  331. ui->service->showPopup();
  332. return;
  333. } else {
  334. lastService = serviceName;
  335. }
  336. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  337. obs_property_t *services = obs_properties_get(props, "service");
  338. OBSData settings = obs_data_create();
  339. obs_data_release(settings);
  340. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  341. obs_property_modified(services, settings);
  342. obs_property_t *servers = obs_properties_get(props, "server");
  343. ui->server->clear();
  344. size_t servers_count = obs_property_list_item_count(servers);
  345. for (size_t i = 0; i < servers_count; i++) {
  346. const char *name = obs_property_list_item_name(servers, i);
  347. const char *server = obs_property_list_item_string(servers, i);
  348. ui->server->addItem(name, server);
  349. }
  350. obs_properties_destroy(props);
  351. }
  352. void OBSBasicSettings::on_show_clicked()
  353. {
  354. if (ui->key->echoMode() == QLineEdit::Password) {
  355. ui->key->setEchoMode(QLineEdit::Normal);
  356. ui->show->setText(QTStr("Hide"));
  357. } else {
  358. ui->key->setEchoMode(QLineEdit::Password);
  359. ui->show->setText(QTStr("Show"));
  360. }
  361. }
  362. void OBSBasicSettings::on_authPwShow_clicked()
  363. {
  364. if (ui->authPw->echoMode() == QLineEdit::Password) {
  365. ui->authPw->setEchoMode(QLineEdit::Normal);
  366. ui->authPwShow->setText(QTStr("Hide"));
  367. } else {
  368. ui->authPw->setEchoMode(QLineEdit::Password);
  369. ui->authPwShow->setText(QTStr("Show"));
  370. }
  371. }
  372. OBSService OBSBasicSettings::SpawnTempService()
  373. {
  374. bool custom = IsCustomService();
  375. const char *service_id = custom ? "rtmp_custom" : "rtmp_common";
  376. OBSData settings = obs_data_create();
  377. obs_data_release(settings);
  378. if (!custom) {
  379. obs_data_set_string(settings, "service",
  380. QT_TO_UTF8(ui->service->currentText()));
  381. obs_data_set_string(
  382. settings, "server",
  383. QT_TO_UTF8(ui->server->currentData().toString()));
  384. } else {
  385. obs_data_set_string(settings, "server",
  386. QT_TO_UTF8(ui->customServer->text()));
  387. }
  388. obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));
  389. OBSService newService = obs_service_create(service_id, "temp_service",
  390. settings, nullptr);
  391. obs_service_release(newService);
  392. return newService;
  393. }
  394. void OBSBasicSettings::OnOAuthStreamKeyConnected()
  395. {
  396. #ifdef BROWSER_AVAILABLE
  397. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  398. if (a) {
  399. bool validKey = !a->key().empty();
  400. if (validKey)
  401. ui->key->setText(QT_UTF8(a->key().c_str()));
  402. ui->streamKeyWidget->setVisible(false);
  403. ui->streamKeyLabel->setVisible(false);
  404. ui->connectAccount2->setVisible(false);
  405. ui->disconnectAccount->setVisible(true);
  406. if (strcmp(a->service(), "Twitch") == 0) {
  407. ui->bandwidthTestEnable->setVisible(true);
  408. ui->twitchAddonLabel->setVisible(true);
  409. ui->twitchAddonDropdown->setVisible(true);
  410. } else {
  411. ui->bandwidthTestEnable->setChecked(false);
  412. }
  413. }
  414. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  415. #endif
  416. }
  417. void OBSBasicSettings::OnAuthConnected()
  418. {
  419. std::string service = QT_TO_UTF8(ui->service->currentText());
  420. Auth::Type type = Auth::AuthType(service);
  421. if (type == Auth::Type::OAuth_StreamKey) {
  422. OnOAuthStreamKeyConnected();
  423. }
  424. if (!loading) {
  425. stream1Changed = true;
  426. EnableApplyButton(true);
  427. }
  428. }
  429. void OBSBasicSettings::on_connectAccount_clicked()
  430. {
  431. #ifdef BROWSER_AVAILABLE
  432. std::string service = QT_TO_UTF8(ui->service->currentText());
  433. OAuth::DeleteCookies(service);
  434. auth = OAuthStreamKey::Login(this, service);
  435. if (!!auth)
  436. OnAuthConnected();
  437. #endif
  438. }
  439. #define DISCONNECT_COMFIRM_TITLE \
  440. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
  441. #define DISCONNECT_COMFIRM_TEXT \
  442. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"
  443. void OBSBasicSettings::on_disconnectAccount_clicked()
  444. {
  445. QMessageBox::StandardButton button;
  446. button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
  447. QTStr(DISCONNECT_COMFIRM_TEXT));
  448. if (button == QMessageBox::No) {
  449. return;
  450. }
  451. main->auth.reset();
  452. auth.reset();
  453. std::string service = QT_TO_UTF8(ui->service->currentText());
  454. #ifdef BROWSER_AVAILABLE
  455. OAuth::DeleteCookies(service);
  456. #endif
  457. ui->bandwidthTestEnable->setChecked(false);
  458. ui->streamKeyWidget->setVisible(true);
  459. ui->streamKeyLabel->setVisible(true);
  460. ui->connectAccount2->setVisible(true);
  461. ui->disconnectAccount->setVisible(false);
  462. ui->bandwidthTestEnable->setVisible(false);
  463. ui->twitchAddonDropdown->setVisible(false);
  464. ui->twitchAddonLabel->setVisible(false);
  465. ui->key->setText("");
  466. }
  467. void OBSBasicSettings::on_useStreamKey_clicked()
  468. {
  469. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  470. }
  471. void OBSBasicSettings::on_useAuth_toggled()
  472. {
  473. if (!IsCustomService())
  474. return;
  475. bool use_auth = ui->useAuth->isChecked();
  476. ui->authUsernameLabel->setVisible(use_auth);
  477. ui->authUsername->setVisible(use_auth);
  478. ui->authPwLabel->setVisible(use_auth);
  479. ui->authPwWidget->setVisible(use_auth);
  480. }