window-basic-settings-stream.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include <QMessageBox>
  2. #include "window-basic-settings.hpp"
  3. #include "obs-frontend-api.h"
  4. #include "obs-app.hpp"
  5. #include "window-basic-main.hpp"
  6. #include "qt-wrappers.hpp"
  7. #ifdef BROWSER_AVAILABLE
  8. #include <browser-panel.hpp>
  9. #include "auth-oauth.hpp"
  10. #endif
  11. struct QCef;
  12. struct QCefCookieManager;
  13. extern QCef *cef;
  14. extern QCefCookieManager *panel_cookies;
  15. enum class ListOpt : int {
  16. ShowAll = 1,
  17. Custom,
  18. };
  19. enum class Section : int {
  20. Connect,
  21. StreamKey,
  22. };
  23. inline bool OBSBasicSettings::IsCustomService() const
  24. {
  25. return ui->service->currentData().toInt() == (int)ListOpt::Custom;
  26. }
  27. void OBSBasicSettings::InitStreamPage()
  28. {
  29. ui->connectAccount2->setVisible(false);
  30. ui->disconnectAccount->setVisible(false);
  31. ui->bandwidthTestEnable->setVisible(false);
  32. int vertSpacing = ui->topStreamLayout->verticalSpacing();
  33. QMargins m = ui->topStreamLayout->contentsMargins();
  34. m.setBottom(vertSpacing / 2);
  35. ui->topStreamLayout->setContentsMargins(m);
  36. m = ui->loginPageLayout->contentsMargins();
  37. m.setTop(vertSpacing / 2);
  38. ui->loginPageLayout->setContentsMargins(m);
  39. m = ui->streamkeyPageLayout->contentsMargins();
  40. m.setTop(vertSpacing / 2);
  41. ui->streamkeyPageLayout->setContentsMargins(m);
  42. LoadServices(false);
  43. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  44. SLOT(UpdateServerList()));
  45. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  46. SLOT(UpdateKeyLink()));
  47. }
  48. void OBSBasicSettings::LoadStream1Settings()
  49. {
  50. obs_service_t *service_obj = main->GetService();
  51. const char *type = obs_service_get_type(service_obj);
  52. loading = true;
  53. obs_data_t *settings = obs_service_get_settings(service_obj);
  54. const char *service = obs_data_get_string(settings, "service");
  55. const char *server = obs_data_get_string(settings, "server");
  56. const char *key = obs_data_get_string(settings, "key");
  57. if (strcmp(type, "rtmp_custom") == 0) {
  58. ui->service->setCurrentIndex(0);
  59. ui->customServer->setText(server);
  60. bool use_auth = obs_data_get_bool(settings, "use_auth");
  61. const char *username =
  62. obs_data_get_string(settings, "username");
  63. const char *password =
  64. obs_data_get_string(settings, "password");
  65. ui->authUsername->setText(QT_UTF8(username));
  66. ui->authPw->setText(QT_UTF8(password));
  67. ui->useAuth->setChecked(use_auth);
  68. } else {
  69. int idx = ui->service->findText(service);
  70. if (idx == -1) {
  71. if (service && *service)
  72. ui->service->insertItem(1, service);
  73. idx = 1;
  74. }
  75. ui->service->setCurrentIndex(idx);
  76. bool bw_test = obs_data_get_bool(settings, "bwtest");
  77. ui->bandwidthTestEnable->setChecked(bw_test);
  78. }
  79. UpdateServerList();
  80. if (strcmp(type, "rtmp_common") == 0) {
  81. int idx = ui->server->findData(server);
  82. if (idx == -1) {
  83. if (server && *server)
  84. ui->server->insertItem(0, server, server);
  85. idx = 0;
  86. }
  87. ui->server->setCurrentIndex(idx);
  88. }
  89. ui->key->setText(key);
  90. lastService.clear();
  91. on_service_currentIndexChanged(0);
  92. obs_data_release(settings);
  93. UpdateKeyLink();
  94. bool streamActive = obs_frontend_streaming_active();
  95. ui->streamPage->setEnabled(!streamActive);
  96. loading = false;
  97. }
  98. void OBSBasicSettings::SaveStream1Settings()
  99. {
  100. bool customServer = IsCustomService();
  101. const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
  102. obs_service_t *oldService = main->GetService();
  103. OBSData hotkeyData = obs_hotkeys_save_service(oldService);
  104. obs_data_release(hotkeyData);
  105. OBSData settings = obs_data_create();
  106. obs_data_release(settings);
  107. if (!customServer) {
  108. obs_data_set_string(settings, "service",
  109. QT_TO_UTF8(ui->service->currentText()));
  110. obs_data_set_string(
  111. settings, "server",
  112. QT_TO_UTF8(ui->server->currentData().toString()));
  113. } else {
  114. obs_data_set_string(settings, "server",
  115. QT_TO_UTF8(ui->customServer->text()));
  116. obs_data_set_bool(settings, "use_auth",
  117. ui->useAuth->isChecked());
  118. if (ui->useAuth->isChecked()) {
  119. obs_data_set_string(
  120. settings, "username",
  121. QT_TO_UTF8(ui->authUsername->text()));
  122. obs_data_set_string(settings, "password",
  123. QT_TO_UTF8(ui->authPw->text()));
  124. }
  125. }
  126. obs_data_set_bool(settings, "bwtest",
  127. ui->bandwidthTestEnable->isChecked());
  128. obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));
  129. OBSService newService = obs_service_create(
  130. service_id, "default_service", settings, hotkeyData);
  131. obs_service_release(newService);
  132. if (!newService)
  133. return;
  134. main->SetService(newService);
  135. main->SaveService();
  136. main->auth = auth;
  137. if (!!main->auth)
  138. main->auth->LoadUI();
  139. }
  140. void OBSBasicSettings::UpdateKeyLink()
  141. {
  142. bool custom = IsCustomService();
  143. QString serviceName = ui->service->currentText();
  144. if (custom)
  145. serviceName = "";
  146. QString text = QTStr("Basic.AutoConfig.StreamPage.StreamKey");
  147. if (serviceName == "Twitch") {
  148. text += " <a href=\"https://";
  149. text += "www.twitch.tv/broadcast/dashboard/streamkey";
  150. text += "\">";
  151. text += QTStr(
  152. "Basic.AutoConfig.StreamPage.StreamKey.LinkToSite");
  153. text += "</a>";
  154. } else if (serviceName == "YouTube / YouTube Gaming") {
  155. text += " <a href=\"https://";
  156. text += "www.youtube.com/live_dashboard";
  157. text += "\">";
  158. text += QTStr(
  159. "Basic.AutoConfig.StreamPage.StreamKey.LinkToSite");
  160. text += "</a>";
  161. } else if (serviceName.startsWith("Restream.io")) {
  162. text += " <a href=\"https://";
  163. text += "restream.io/settings/streaming-setup?from=OBS";
  164. text += "\">";
  165. text += QTStr(
  166. "Basic.AutoConfig.StreamPage.StreamKey.LinkToSite");
  167. text += "</a>";
  168. }
  169. ui->streamKeyLabel->setText(text);
  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. }