window-basic-settings-stream.cpp 14 KB

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