window-basic-settings-stream.cpp 15 KB

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