1
0

window-basic-settings-stream.cpp 13 KB

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