window-basic-settings-stream.cpp 14 KB

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