window-basic-settings-stream.cpp 11 KB

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