window-basic-settings-stream.cpp 13 KB

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