window-basic-settings-stream.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 = "https://dashboard.twitch.tv/settings/stream";
  204. } else if (serviceName.startsWith("YouTube")) {
  205. streamKeyLink = "https://www.youtube.com/live_dashboard";
  206. } else if (serviceName.startsWith("Restream.io")) {
  207. streamKeyLink =
  208. "https://restream.io/settings/streaming-setup?from=OBS";
  209. } else if (serviceName == "Facebook Live" ||
  210. (customServer.contains("fbcdn.net") && IsCustomService())) {
  211. streamKeyLink =
  212. "https://www.facebook.com/live/producer?ref=OBS";
  213. } else if (serviceName.startsWith("Twitter")) {
  214. streamKeyLink = "https://www.pscp.tv/account/producer";
  215. } else if (serviceName.startsWith("YouStreamer")) {
  216. streamKeyLink = "https://app.youstreamer.com/stream/";
  217. } else if (serviceName == "Trovo") {
  218. streamKeyLink = "https://studio.trovo.live/mychannel/stream";
  219. }
  220. if (QString(streamKeyLink).isNull()) {
  221. ui->getStreamKeyButton->hide();
  222. } else {
  223. ui->getStreamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  224. ui->getStreamKeyButton->show();
  225. }
  226. }
  227. void OBSBasicSettings::LoadServices(bool showAll)
  228. {
  229. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  230. OBSData settings = obs_data_create();
  231. obs_data_release(settings);
  232. obs_data_set_bool(settings, "show_all", showAll);
  233. obs_property_t *prop = obs_properties_get(props, "show_all");
  234. obs_property_modified(prop, settings);
  235. ui->service->blockSignals(true);
  236. ui->service->clear();
  237. QStringList names;
  238. obs_property_t *services = obs_properties_get(props, "service");
  239. size_t services_count = obs_property_list_item_count(services);
  240. for (size_t i = 0; i < services_count; i++) {
  241. const char *name = obs_property_list_item_string(services, i);
  242. names.push_back(name);
  243. }
  244. if (showAll)
  245. names.sort(Qt::CaseInsensitive);
  246. for (QString &name : names)
  247. ui->service->addItem(name);
  248. if (!showAll) {
  249. ui->service->addItem(
  250. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  251. QVariant((int)ListOpt::ShowAll));
  252. }
  253. ui->service->insertItem(
  254. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  255. QVariant((int)ListOpt::Custom));
  256. if (!lastService.isEmpty()) {
  257. int idx = ui->service->findText(lastService);
  258. if (idx != -1)
  259. ui->service->setCurrentIndex(idx);
  260. }
  261. obs_properties_destroy(props);
  262. ui->service->blockSignals(false);
  263. }
  264. static inline bool is_auth_service(const std::string &service)
  265. {
  266. return Auth::AuthType(service) != Auth::Type::None;
  267. }
  268. void OBSBasicSettings::on_service_currentIndexChanged(int)
  269. {
  270. bool showMore = ui->service->currentData().toInt() ==
  271. (int)ListOpt::ShowAll;
  272. if (showMore)
  273. return;
  274. std::string service = QT_TO_UTF8(ui->service->currentText());
  275. bool custom = IsCustomService();
  276. ui->disconnectAccount->setVisible(false);
  277. ui->bandwidthTestEnable->setVisible(false);
  278. ui->twitchAddonDropdown->setVisible(false);
  279. ui->twitchAddonLabel->setVisible(false);
  280. #ifdef BROWSER_AVAILABLE
  281. if (cef) {
  282. if (lastService != service.c_str()) {
  283. QString key = ui->key->text();
  284. bool can_auth = is_auth_service(service);
  285. int page = can_auth && (!loading || key.isEmpty())
  286. ? (int)Section::Connect
  287. : (int)Section::StreamKey;
  288. ui->streamStackWidget->setCurrentIndex(page);
  289. ui->streamKeyWidget->setVisible(true);
  290. ui->streamKeyLabel->setVisible(true);
  291. ui->connectAccount2->setVisible(can_auth);
  292. }
  293. } else {
  294. ui->connectAccount2->setVisible(false);
  295. }
  296. #else
  297. ui->connectAccount2->setVisible(false);
  298. #endif
  299. ui->useAuth->setVisible(custom);
  300. ui->authUsernameLabel->setVisible(custom);
  301. ui->authUsername->setVisible(custom);
  302. ui->authPwLabel->setVisible(custom);
  303. ui->authPwWidget->setVisible(custom);
  304. if (custom) {
  305. ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
  306. ui->serverStackedWidget);
  307. ui->serverStackedWidget->setCurrentIndex(1);
  308. ui->serverStackedWidget->setVisible(true);
  309. ui->serverLabel->setVisible(true);
  310. on_useAuth_toggled();
  311. } else {
  312. ui->serverStackedWidget->setCurrentIndex(0);
  313. }
  314. #ifdef BROWSER_AVAILABLE
  315. auth.reset();
  316. if (!!main->auth &&
  317. service.find(main->auth->service()) != std::string::npos) {
  318. auth = main->auth;
  319. OnAuthConnected();
  320. }
  321. #endif
  322. }
  323. void OBSBasicSettings::UpdateServerList()
  324. {
  325. QString serviceName = ui->service->currentText();
  326. bool showMore = ui->service->currentData().toInt() ==
  327. (int)ListOpt::ShowAll;
  328. if (showMore) {
  329. LoadServices(true);
  330. ui->service->showPopup();
  331. return;
  332. } else {
  333. lastService = serviceName;
  334. }
  335. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  336. obs_property_t *services = obs_properties_get(props, "service");
  337. OBSData settings = obs_data_create();
  338. obs_data_release(settings);
  339. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  340. obs_property_modified(services, settings);
  341. obs_property_t *servers = obs_properties_get(props, "server");
  342. ui->server->clear();
  343. size_t servers_count = obs_property_list_item_count(servers);
  344. for (size_t i = 0; i < servers_count; i++) {
  345. const char *name = obs_property_list_item_name(servers, i);
  346. const char *server = obs_property_list_item_string(servers, i);
  347. ui->server->addItem(name, server);
  348. }
  349. obs_properties_destroy(props);
  350. }
  351. void OBSBasicSettings::on_show_clicked()
  352. {
  353. if (ui->key->echoMode() == QLineEdit::Password) {
  354. ui->key->setEchoMode(QLineEdit::Normal);
  355. ui->show->setText(QTStr("Hide"));
  356. } else {
  357. ui->key->setEchoMode(QLineEdit::Password);
  358. ui->show->setText(QTStr("Show"));
  359. }
  360. }
  361. void OBSBasicSettings::on_authPwShow_clicked()
  362. {
  363. if (ui->authPw->echoMode() == QLineEdit::Password) {
  364. ui->authPw->setEchoMode(QLineEdit::Normal);
  365. ui->authPwShow->setText(QTStr("Hide"));
  366. } else {
  367. ui->authPw->setEchoMode(QLineEdit::Password);
  368. ui->authPwShow->setText(QTStr("Show"));
  369. }
  370. }
  371. OBSService OBSBasicSettings::SpawnTempService()
  372. {
  373. bool custom = IsCustomService();
  374. const char *service_id = custom ? "rtmp_custom" : "rtmp_common";
  375. OBSData settings = obs_data_create();
  376. obs_data_release(settings);
  377. if (!custom) {
  378. obs_data_set_string(settings, "service",
  379. QT_TO_UTF8(ui->service->currentText()));
  380. obs_data_set_string(
  381. settings, "server",
  382. QT_TO_UTF8(ui->server->currentData().toString()));
  383. } else {
  384. obs_data_set_string(settings, "server",
  385. QT_TO_UTF8(ui->customServer->text()));
  386. }
  387. obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));
  388. OBSService newService = obs_service_create(service_id, "temp_service",
  389. settings, nullptr);
  390. obs_service_release(newService);
  391. return newService;
  392. }
  393. void OBSBasicSettings::OnOAuthStreamKeyConnected()
  394. {
  395. #ifdef BROWSER_AVAILABLE
  396. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  397. if (a) {
  398. bool validKey = !a->key().empty();
  399. if (validKey)
  400. ui->key->setText(QT_UTF8(a->key().c_str()));
  401. ui->streamKeyWidget->setVisible(false);
  402. ui->streamKeyLabel->setVisible(false);
  403. ui->connectAccount2->setVisible(false);
  404. ui->disconnectAccount->setVisible(true);
  405. if (strcmp(a->service(), "Twitch") == 0) {
  406. ui->bandwidthTestEnable->setVisible(true);
  407. ui->twitchAddonLabel->setVisible(true);
  408. ui->twitchAddonDropdown->setVisible(true);
  409. } else {
  410. ui->bandwidthTestEnable->setChecked(false);
  411. }
  412. }
  413. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  414. #endif
  415. }
  416. void OBSBasicSettings::OnAuthConnected()
  417. {
  418. std::string service = QT_TO_UTF8(ui->service->currentText());
  419. Auth::Type type = Auth::AuthType(service);
  420. if (type == Auth::Type::OAuth_StreamKey) {
  421. OnOAuthStreamKeyConnected();
  422. }
  423. if (!loading) {
  424. stream1Changed = true;
  425. EnableApplyButton(true);
  426. }
  427. }
  428. void OBSBasicSettings::on_connectAccount_clicked()
  429. {
  430. #ifdef BROWSER_AVAILABLE
  431. std::string service = QT_TO_UTF8(ui->service->currentText());
  432. OAuth::DeleteCookies(service);
  433. auth = OAuthStreamKey::Login(this, service);
  434. if (!!auth)
  435. OnAuthConnected();
  436. #endif
  437. }
  438. #define DISCONNECT_COMFIRM_TITLE \
  439. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
  440. #define DISCONNECT_COMFIRM_TEXT \
  441. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"
  442. void OBSBasicSettings::on_disconnectAccount_clicked()
  443. {
  444. QMessageBox::StandardButton button;
  445. button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
  446. QTStr(DISCONNECT_COMFIRM_TEXT));
  447. if (button == QMessageBox::No) {
  448. return;
  449. }
  450. main->auth.reset();
  451. auth.reset();
  452. std::string service = QT_TO_UTF8(ui->service->currentText());
  453. #ifdef BROWSER_AVAILABLE
  454. OAuth::DeleteCookies(service);
  455. #endif
  456. ui->bandwidthTestEnable->setChecked(false);
  457. ui->streamKeyWidget->setVisible(true);
  458. ui->streamKeyLabel->setVisible(true);
  459. ui->connectAccount2->setVisible(true);
  460. ui->disconnectAccount->setVisible(false);
  461. ui->bandwidthTestEnable->setVisible(false);
  462. ui->twitchAddonDropdown->setVisible(false);
  463. ui->twitchAddonLabel->setVisible(false);
  464. ui->key->setText("");
  465. }
  466. void OBSBasicSettings::on_useStreamKey_clicked()
  467. {
  468. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  469. }
  470. void OBSBasicSettings::on_useAuth_toggled()
  471. {
  472. if (!IsCustomService())
  473. return;
  474. bool use_auth = ui->useAuth->isChecked();
  475. ui->authUsernameLabel->setVisible(use_auth);
  476. ui->authUsername->setVisible(use_auth);
  477. ui->authPwLabel->setVisible(use_auth);
  478. ui->authPwWidget->setVisible(use_auth);
  479. }