window-basic-settings-stream.cpp 14 KB

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