window-basic-settings-stream.cpp 15 KB

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