window-basic-settings-stream.cpp 12 KB

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