window-basic-settings-stream.cpp 15 KB

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