window-basic-settings-stream.cpp 15 KB

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