window-basic-settings-stream.cpp 13 KB

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