window-basic-settings-stream.cpp 11 KB

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