window-basic-auto-config.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. #include <QMessageBox>
  2. #include <QScreen>
  3. #include <obs.hpp>
  4. #include "window-basic-auto-config.hpp"
  5. #include "window-basic-main.hpp"
  6. #include "qt-wrappers.hpp"
  7. #include "obs-app.hpp"
  8. #include "url-push-button.hpp"
  9. #include "ui_AutoConfigStartPage.h"
  10. #include "ui_AutoConfigVideoPage.h"
  11. #include "ui_AutoConfigStreamPage.h"
  12. #ifdef BROWSER_AVAILABLE
  13. #include <browser-panel.hpp>
  14. #endif
  15. #include "auth-oauth.hpp"
  16. #include "ui-config.h"
  17. #if YOUTUBE_ENABLED
  18. #include "youtube-api-wrappers.hpp"
  19. #endif
  20. struct QCef;
  21. struct QCefCookieManager;
  22. extern QCef *cef;
  23. extern QCefCookieManager *panel_cookies;
  24. #define wiz reinterpret_cast<AutoConfig *>(wizard())
  25. /* ------------------------------------------------------------------------- */
  26. #define SERVICE_PATH "service.json"
  27. static OBSData OpenServiceSettings(std::string &type)
  28. {
  29. char serviceJsonPath[512];
  30. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  31. SERVICE_PATH);
  32. if (ret <= 0)
  33. return OBSData();
  34. OBSData data =
  35. obs_data_create_from_json_file_safe(serviceJsonPath, "bak");
  36. obs_data_release(data);
  37. obs_data_set_default_string(data, "type", "rtmp_common");
  38. type = obs_data_get_string(data, "type");
  39. OBSData settings = obs_data_get_obj(data, "settings");
  40. obs_data_release(settings);
  41. return settings;
  42. }
  43. static void GetServiceInfo(std::string &type, std::string &service,
  44. std::string &server, std::string &key)
  45. {
  46. OBSData settings = OpenServiceSettings(type);
  47. service = obs_data_get_string(settings, "service");
  48. server = obs_data_get_string(settings, "server");
  49. key = obs_data_get_string(settings, "key");
  50. }
  51. /* ------------------------------------------------------------------------- */
  52. AutoConfigStartPage::AutoConfigStartPage(QWidget *parent)
  53. : QWizardPage(parent), ui(new Ui_AutoConfigStartPage)
  54. {
  55. ui->setupUi(this);
  56. setTitle(QTStr("Basic.AutoConfig.StartPage"));
  57. setSubTitle(QTStr("Basic.AutoConfig.StartPage.SubTitle"));
  58. OBSBasic *main = OBSBasic::Get();
  59. if (main->VCamEnabled()) {
  60. QRadioButton *prioritizeVCam = new QRadioButton(
  61. QTStr("Basic.AutoConfig.StartPage.PrioritizeVirtualCam"),
  62. this);
  63. QBoxLayout *box = reinterpret_cast<QBoxLayout *>(layout());
  64. box->insertWidget(2, prioritizeVCam);
  65. connect(prioritizeVCam, &QPushButton::clicked, this,
  66. &AutoConfigStartPage::PrioritizeVCam);
  67. }
  68. }
  69. AutoConfigStartPage::~AutoConfigStartPage()
  70. {
  71. delete ui;
  72. }
  73. int AutoConfigStartPage::nextId() const
  74. {
  75. return wiz->type == AutoConfig::Type::VirtualCam
  76. ? AutoConfig::TestPage
  77. : AutoConfig::VideoPage;
  78. }
  79. void AutoConfigStartPage::on_prioritizeStreaming_clicked()
  80. {
  81. wiz->type = AutoConfig::Type::Streaming;
  82. }
  83. void AutoConfigStartPage::on_prioritizeRecording_clicked()
  84. {
  85. wiz->type = AutoConfig::Type::Recording;
  86. }
  87. void AutoConfigStartPage::PrioritizeVCam()
  88. {
  89. wiz->type = AutoConfig::Type::VirtualCam;
  90. }
  91. /* ------------------------------------------------------------------------- */
  92. #define RES_TEXT(x) "Basic.AutoConfig.VideoPage." x
  93. #define RES_USE_CURRENT RES_TEXT("BaseResolution.UseCurrent")
  94. #define RES_USE_DISPLAY RES_TEXT("BaseResolution.Display")
  95. #define FPS_USE_CURRENT RES_TEXT("FPS.UseCurrent")
  96. #define FPS_PREFER_HIGH_FPS RES_TEXT("FPS.PreferHighFPS")
  97. #define FPS_PREFER_HIGH_RES RES_TEXT("FPS.PreferHighRes")
  98. AutoConfigVideoPage::AutoConfigVideoPage(QWidget *parent)
  99. : QWizardPage(parent), ui(new Ui_AutoConfigVideoPage)
  100. {
  101. ui->setupUi(this);
  102. setTitle(QTStr("Basic.AutoConfig.VideoPage"));
  103. setSubTitle(QTStr("Basic.AutoConfig.VideoPage.SubTitle"));
  104. obs_video_info ovi;
  105. obs_get_video_info(&ovi);
  106. long double fpsVal =
  107. (long double)ovi.fps_num / (long double)ovi.fps_den;
  108. QString fpsStr = (ovi.fps_den > 1) ? QString::number(fpsVal, 'f', 2)
  109. : QString::number(fpsVal, 'g', 2);
  110. ui->fps->addItem(QTStr(FPS_PREFER_HIGH_FPS),
  111. (int)AutoConfig::FPSType::PreferHighFPS);
  112. ui->fps->addItem(QTStr(FPS_PREFER_HIGH_RES),
  113. (int)AutoConfig::FPSType::PreferHighRes);
  114. ui->fps->addItem(QTStr(FPS_USE_CURRENT).arg(fpsStr),
  115. (int)AutoConfig::FPSType::UseCurrent);
  116. ui->fps->addItem(QStringLiteral("30"), (int)AutoConfig::FPSType::fps30);
  117. ui->fps->addItem(QStringLiteral("60"), (int)AutoConfig::FPSType::fps60);
  118. ui->fps->setCurrentIndex(0);
  119. QString cxStr = QString::number(ovi.base_width);
  120. QString cyStr = QString::number(ovi.base_height);
  121. int encRes = int(ovi.base_width << 16) | int(ovi.base_height);
  122. ui->canvasRes->addItem(QTStr(RES_USE_CURRENT).arg(cxStr, cyStr),
  123. (int)encRes);
  124. QList<QScreen *> screens = QGuiApplication::screens();
  125. for (int i = 0; i < screens.size(); i++) {
  126. QScreen *screen = screens[i];
  127. QSize as = screen->size();
  128. int as_width = as.width();
  129. int as_height = as.height();
  130. // Calculate physical screen resolution based on the virtual screen resolution
  131. // They might differ if scaling is enabled, e.g. for HiDPI screens
  132. as_width = round(as_width * screen->devicePixelRatio());
  133. as_height = round(as_height * screen->devicePixelRatio());
  134. encRes = as_width << 16 | as_height;
  135. QString str = QTStr(RES_USE_DISPLAY)
  136. .arg(QString::number(i + 1),
  137. QString::number(as_width),
  138. QString::number(as_height));
  139. ui->canvasRes->addItem(str, encRes);
  140. }
  141. auto addRes = [&](int cx, int cy) {
  142. encRes = (cx << 16) | cy;
  143. QString str = QString("%1x%2").arg(QString::number(cx),
  144. QString::number(cy));
  145. ui->canvasRes->addItem(str, encRes);
  146. };
  147. addRes(1920, 1080);
  148. addRes(1280, 720);
  149. ui->canvasRes->setCurrentIndex(0);
  150. }
  151. AutoConfigVideoPage::~AutoConfigVideoPage()
  152. {
  153. delete ui;
  154. }
  155. int AutoConfigVideoPage::nextId() const
  156. {
  157. return wiz->type == AutoConfig::Type::Recording
  158. ? AutoConfig::TestPage
  159. : AutoConfig::StreamPage;
  160. }
  161. bool AutoConfigVideoPage::validatePage()
  162. {
  163. int encRes = ui->canvasRes->currentData().toInt();
  164. wiz->baseResolutionCX = encRes >> 16;
  165. wiz->baseResolutionCY = encRes & 0xFFFF;
  166. wiz->fpsType = (AutoConfig::FPSType)ui->fps->currentData().toInt();
  167. obs_video_info ovi;
  168. obs_get_video_info(&ovi);
  169. switch (wiz->fpsType) {
  170. case AutoConfig::FPSType::PreferHighFPS:
  171. wiz->specificFPSNum = 0;
  172. wiz->specificFPSDen = 0;
  173. wiz->preferHighFPS = true;
  174. break;
  175. case AutoConfig::FPSType::PreferHighRes:
  176. wiz->specificFPSNum = 0;
  177. wiz->specificFPSDen = 0;
  178. wiz->preferHighFPS = false;
  179. break;
  180. case AutoConfig::FPSType::UseCurrent:
  181. wiz->specificFPSNum = ovi.fps_num;
  182. wiz->specificFPSDen = ovi.fps_den;
  183. wiz->preferHighFPS = false;
  184. break;
  185. case AutoConfig::FPSType::fps30:
  186. wiz->specificFPSNum = 30;
  187. wiz->specificFPSDen = 1;
  188. wiz->preferHighFPS = false;
  189. break;
  190. case AutoConfig::FPSType::fps60:
  191. wiz->specificFPSNum = 60;
  192. wiz->specificFPSDen = 1;
  193. wiz->preferHighFPS = false;
  194. break;
  195. }
  196. return true;
  197. }
  198. /* ------------------------------------------------------------------------- */
  199. enum class ListOpt : int {
  200. ShowAll = 1,
  201. Custom,
  202. };
  203. AutoConfigStreamPage::AutoConfigStreamPage(QWidget *parent)
  204. : QWizardPage(parent), ui(new Ui_AutoConfigStreamPage)
  205. {
  206. ui->setupUi(this);
  207. ui->bitrateLabel->setVisible(false);
  208. ui->bitrate->setVisible(false);
  209. ui->connectAccount2->setVisible(false);
  210. ui->disconnectAccount->setVisible(false);
  211. ui->connectedAccountLabel->setVisible(false);
  212. ui->connectedAccountText->setVisible(false);
  213. int vertSpacing = ui->topLayout->verticalSpacing();
  214. QMargins m = ui->topLayout->contentsMargins();
  215. m.setBottom(vertSpacing / 2);
  216. ui->topLayout->setContentsMargins(m);
  217. m = ui->loginPageLayout->contentsMargins();
  218. m.setTop(vertSpacing / 2);
  219. ui->loginPageLayout->setContentsMargins(m);
  220. m = ui->streamkeyPageLayout->contentsMargins();
  221. m.setTop(vertSpacing / 2);
  222. ui->streamkeyPageLayout->setContentsMargins(m);
  223. setTitle(QTStr("Basic.AutoConfig.StreamPage"));
  224. setSubTitle(QTStr("Basic.AutoConfig.StreamPage.SubTitle"));
  225. LoadServices(false);
  226. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  227. SLOT(ServiceChanged()));
  228. connect(ui->customServer, SIGNAL(textChanged(const QString &)), this,
  229. SLOT(ServiceChanged()));
  230. connect(ui->customServer, SIGNAL(textChanged(const QString &)), this,
  231. SLOT(UpdateKeyLink()));
  232. connect(ui->customServer, SIGNAL(editingFinished()), this,
  233. SLOT(UpdateKeyLink()));
  234. connect(ui->doBandwidthTest, SIGNAL(toggled(bool)), this,
  235. SLOT(ServiceChanged()));
  236. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  237. SLOT(UpdateServerList()));
  238. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  239. SLOT(UpdateKeyLink()));
  240. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  241. SLOT(UpdateMoreInfoLink()));
  242. connect(ui->useStreamKeyAdv, &QPushButton::clicked, this,
  243. [&]() { ui->streamKeyWidget->setVisible(true); });
  244. connect(ui->key, SIGNAL(textChanged(const QString &)), this,
  245. SLOT(UpdateCompleted()));
  246. connect(ui->regionUS, SIGNAL(toggled(bool)), this,
  247. SLOT(UpdateCompleted()));
  248. connect(ui->regionEU, SIGNAL(toggled(bool)), this,
  249. SLOT(UpdateCompleted()));
  250. connect(ui->regionAsia, SIGNAL(toggled(bool)), this,
  251. SLOT(UpdateCompleted()));
  252. connect(ui->regionOther, SIGNAL(toggled(bool)), this,
  253. SLOT(UpdateCompleted()));
  254. }
  255. AutoConfigStreamPage::~AutoConfigStreamPage()
  256. {
  257. delete ui;
  258. }
  259. bool AutoConfigStreamPage::isComplete() const
  260. {
  261. return ready;
  262. }
  263. int AutoConfigStreamPage::nextId() const
  264. {
  265. return AutoConfig::TestPage;
  266. }
  267. inline bool AutoConfigStreamPage::IsCustomService() const
  268. {
  269. return ui->service->currentData().toInt() == (int)ListOpt::Custom;
  270. }
  271. bool AutoConfigStreamPage::validatePage()
  272. {
  273. OBSData service_settings = obs_data_create();
  274. obs_data_release(service_settings);
  275. wiz->customServer = IsCustomService();
  276. const char *serverType = wiz->customServer ? "rtmp_custom"
  277. : "rtmp_common";
  278. if (!wiz->customServer) {
  279. obs_data_set_string(service_settings, "service",
  280. QT_TO_UTF8(ui->service->currentText()));
  281. }
  282. OBSService service = obs_service_create(serverType, "temp_service",
  283. service_settings, nullptr);
  284. obs_service_release(service);
  285. int bitrate = 10000;
  286. if (!ui->doBandwidthTest->isChecked()) {
  287. bitrate = ui->bitrate->value();
  288. wiz->idealBitrate = bitrate;
  289. }
  290. OBSData settings = obs_data_create();
  291. obs_data_release(settings);
  292. obs_data_set_int(settings, "bitrate", bitrate);
  293. obs_service_apply_encoder_settings(service, settings, nullptr);
  294. if (wiz->customServer) {
  295. QString server = ui->customServer->text();
  296. wiz->server = wiz->serverName = QT_TO_UTF8(server);
  297. } else {
  298. wiz->serverName = QT_TO_UTF8(ui->server->currentText());
  299. wiz->server = QT_TO_UTF8(ui->server->currentData().toString());
  300. }
  301. wiz->bandwidthTest = ui->doBandwidthTest->isChecked();
  302. wiz->startingBitrate = (int)obs_data_get_int(settings, "bitrate");
  303. wiz->idealBitrate = wiz->startingBitrate;
  304. wiz->regionUS = ui->regionUS->isChecked();
  305. wiz->regionEU = ui->regionEU->isChecked();
  306. wiz->regionAsia = ui->regionAsia->isChecked();
  307. wiz->regionOther = ui->regionOther->isChecked();
  308. wiz->serviceName = QT_TO_UTF8(ui->service->currentText());
  309. if (ui->preferHardware)
  310. wiz->preferHardware = ui->preferHardware->isChecked();
  311. wiz->key = QT_TO_UTF8(ui->key->text());
  312. if (!wiz->customServer) {
  313. if (wiz->serviceName == "Twitch")
  314. wiz->service = AutoConfig::Service::Twitch;
  315. else
  316. wiz->service = AutoConfig::Service::Other;
  317. } else {
  318. wiz->service = AutoConfig::Service::Other;
  319. }
  320. if (wiz->service != AutoConfig::Service::Twitch && wiz->bandwidthTest) {
  321. QMessageBox::StandardButton button;
  322. #define WARNING_TEXT(x) QTStr("Basic.AutoConfig.StreamPage.StreamWarning." x)
  323. button = OBSMessageBox::question(this, WARNING_TEXT("Title"),
  324. WARNING_TEXT("Text"));
  325. #undef WARNING_TEXT
  326. if (button == QMessageBox::No)
  327. return false;
  328. }
  329. return true;
  330. }
  331. void AutoConfigStreamPage::on_show_clicked()
  332. {
  333. if (ui->key->echoMode() == QLineEdit::Password) {
  334. ui->key->setEchoMode(QLineEdit::Normal);
  335. ui->show->setText(QTStr("Hide"));
  336. } else {
  337. ui->key->setEchoMode(QLineEdit::Password);
  338. ui->show->setText(QTStr("Show"));
  339. }
  340. }
  341. void AutoConfigStreamPage::OnOAuthStreamKeyConnected()
  342. {
  343. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  344. if (a) {
  345. bool validKey = !a->key().empty();
  346. if (validKey)
  347. ui->key->setText(QT_UTF8(a->key().c_str()));
  348. ui->streamKeyWidget->setVisible(false);
  349. ui->streamKeyLabel->setVisible(false);
  350. ui->connectAccount2->setVisible(false);
  351. ui->disconnectAccount->setVisible(true);
  352. ui->useStreamKeyAdv->setVisible(false);
  353. ui->connectedAccountLabel->setVisible(false);
  354. ui->connectedAccountText->setVisible(false);
  355. #if YOUTUBE_ENABLED
  356. if (IsYouTubeService(a->service())) {
  357. ui->key->clear();
  358. ui->connectedAccountLabel->setVisible(true);
  359. ui->connectedAccountText->setVisible(true);
  360. ui->connectedAccountText->setText(
  361. QTStr("Auth.LoadingChannel.Title"));
  362. QScopedPointer<QThread> thread(CreateQThread([&]() {
  363. std::shared_ptr<YoutubeApiWrappers> ytAuth =
  364. std::dynamic_pointer_cast<
  365. YoutubeApiWrappers>(auth);
  366. if (ytAuth.get()) {
  367. ChannelDescription cd;
  368. if (ytAuth->GetChannelDescription(cd)) {
  369. ui->connectedAccountText
  370. ->setText(cd.title);
  371. }
  372. }
  373. }));
  374. thread->start();
  375. thread->wait();
  376. }
  377. #endif
  378. }
  379. ui->stackedWidget->setCurrentIndex((int)Section::StreamKey);
  380. UpdateCompleted();
  381. }
  382. void AutoConfigStreamPage::OnAuthConnected()
  383. {
  384. std::string service = QT_TO_UTF8(ui->service->currentText());
  385. Auth::Type type = Auth::AuthType(service);
  386. if (type == Auth::Type::OAuth_StreamKey ||
  387. type == Auth::Type::OAuth_LinkedAccount) {
  388. OnOAuthStreamKeyConnected();
  389. }
  390. }
  391. void AutoConfigStreamPage::on_connectAccount_clicked()
  392. {
  393. std::string service = QT_TO_UTF8(ui->service->currentText());
  394. OAuth::DeleteCookies(service);
  395. auth = OAuthStreamKey::Login(this, service);
  396. if (!!auth) {
  397. OnAuthConnected();
  398. ui->useStreamKeyAdv->setVisible(false);
  399. }
  400. }
  401. #define DISCONNECT_COMFIRM_TITLE \
  402. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
  403. #define DISCONNECT_COMFIRM_TEXT \
  404. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"
  405. void AutoConfigStreamPage::on_disconnectAccount_clicked()
  406. {
  407. QMessageBox::StandardButton button;
  408. button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
  409. QTStr(DISCONNECT_COMFIRM_TEXT));
  410. if (button == QMessageBox::No) {
  411. return;
  412. }
  413. OBSBasic *main = OBSBasic::Get();
  414. main->auth.reset();
  415. auth.reset();
  416. std::string service = QT_TO_UTF8(ui->service->currentText());
  417. #ifdef BROWSER_AVAILABLE
  418. OAuth::DeleteCookies(service);
  419. #endif
  420. reset_service_ui_fields(service);
  421. ui->streamKeyWidget->setVisible(true);
  422. ui->streamKeyLabel->setVisible(true);
  423. ui->key->setText("");
  424. ui->connectedAccountLabel->setVisible(false);
  425. ui->connectedAccountText->setVisible(false);
  426. }
  427. void AutoConfigStreamPage::on_useStreamKey_clicked()
  428. {
  429. ui->stackedWidget->setCurrentIndex((int)Section::StreamKey);
  430. UpdateCompleted();
  431. }
  432. static inline bool is_auth_service(const std::string &service)
  433. {
  434. return Auth::AuthType(service) != Auth::Type::None;
  435. }
  436. static inline bool is_external_oauth(const std::string &service)
  437. {
  438. return Auth::External(service);
  439. }
  440. void AutoConfigStreamPage::reset_service_ui_fields(std::string &service)
  441. {
  442. // when account is already connected:
  443. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  444. #if YOUTUBE_ENABLED
  445. if (a && service == a->service() && IsYouTubeService(a->service())) {
  446. ui->connectedAccountLabel->setVisible(true);
  447. ui->connectedAccountText->setVisible(true);
  448. ui->connectAccount2->setVisible(false);
  449. ui->disconnectAccount->setVisible(true);
  450. return;
  451. }
  452. #endif
  453. bool external_oauth = is_external_oauth(service);
  454. if (external_oauth) {
  455. ui->streamKeyWidget->setVisible(false);
  456. ui->streamKeyLabel->setVisible(false);
  457. ui->connectAccount2->setVisible(true);
  458. ui->useStreamKeyAdv->setVisible(true);
  459. ui->stackedWidget->setCurrentIndex((int)Section::StreamKey);
  460. } else if (cef) {
  461. QString key = ui->key->text();
  462. bool can_auth = is_auth_service(service);
  463. int page = can_auth && key.isEmpty() ? (int)Section::Connect
  464. : (int)Section::StreamKey;
  465. ui->stackedWidget->setCurrentIndex(page);
  466. ui->streamKeyWidget->setVisible(true);
  467. ui->streamKeyLabel->setVisible(true);
  468. ui->connectAccount2->setVisible(can_auth);
  469. ui->useStreamKeyAdv->setVisible(false);
  470. } else {
  471. ui->connectAccount2->setVisible(false);
  472. ui->useStreamKeyAdv->setVisible(false);
  473. }
  474. ui->connectedAccountLabel->setVisible(false);
  475. ui->connectedAccountText->setVisible(false);
  476. ui->disconnectAccount->setVisible(false);
  477. }
  478. void AutoConfigStreamPage::ServiceChanged()
  479. {
  480. bool showMore = ui->service->currentData().toInt() ==
  481. (int)ListOpt::ShowAll;
  482. if (showMore)
  483. return;
  484. std::string service = QT_TO_UTF8(ui->service->currentText());
  485. bool regionBased = service == "Twitch";
  486. bool testBandwidth = ui->doBandwidthTest->isChecked();
  487. bool custom = IsCustomService();
  488. reset_service_ui_fields(service);
  489. /* Test three closest servers if "Auto" is available for Twitch */
  490. if (service == "Twitch" && wiz->twitchAuto)
  491. regionBased = false;
  492. ui->streamkeyPageLayout->removeWidget(ui->serverLabel);
  493. ui->streamkeyPageLayout->removeWidget(ui->serverStackedWidget);
  494. if (custom) {
  495. ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
  496. ui->serverStackedWidget);
  497. ui->region->setVisible(false);
  498. ui->serverStackedWidget->setCurrentIndex(1);
  499. ui->serverStackedWidget->setVisible(true);
  500. ui->serverLabel->setVisible(true);
  501. } else {
  502. if (!testBandwidth)
  503. ui->streamkeyPageLayout->insertRow(
  504. 2, ui->serverLabel, ui->serverStackedWidget);
  505. ui->region->setVisible(regionBased && testBandwidth);
  506. ui->serverStackedWidget->setCurrentIndex(0);
  507. ui->serverStackedWidget->setHidden(testBandwidth);
  508. ui->serverLabel->setHidden(testBandwidth);
  509. }
  510. wiz->testRegions = regionBased && testBandwidth;
  511. ui->bitrateLabel->setHidden(testBandwidth);
  512. ui->bitrate->setHidden(testBandwidth);
  513. OBSBasic *main = OBSBasic::Get();
  514. if (main->auth) {
  515. auto system_auth_service = main->auth->service();
  516. bool service_check = service == system_auth_service;
  517. #if YOUTUBE_ENABLED
  518. service_check =
  519. service_check ? service_check
  520. : IsYouTubeService(system_auth_service) &&
  521. IsYouTubeService(service);
  522. #endif
  523. if (service_check) {
  524. auth.reset();
  525. auth = main->auth;
  526. OnAuthConnected();
  527. }
  528. }
  529. UpdateCompleted();
  530. }
  531. void AutoConfigStreamPage::UpdateMoreInfoLink()
  532. {
  533. if (IsCustomService()) {
  534. ui->moreInfoButton->hide();
  535. return;
  536. }
  537. QString serviceName = ui->service->currentText();
  538. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  539. obs_property_t *services = obs_properties_get(props, "service");
  540. OBSData settings = obs_data_create();
  541. obs_data_release(settings);
  542. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  543. obs_property_modified(services, settings);
  544. const char *more_info_link =
  545. obs_data_get_string(settings, "more_info_link");
  546. if (!more_info_link || (*more_info_link == '\0')) {
  547. ui->moreInfoButton->hide();
  548. } else {
  549. ui->moreInfoButton->setTargetUrl(QUrl(more_info_link));
  550. ui->moreInfoButton->show();
  551. }
  552. obs_properties_destroy(props);
  553. }
  554. void AutoConfigStreamPage::UpdateKeyLink()
  555. {
  556. QString serviceName = ui->service->currentText();
  557. QString customServer = ui->customServer->text();
  558. bool isYoutube = false;
  559. QString streamKeyLink;
  560. if (serviceName == "Twitch") {
  561. streamKeyLink = "https://dashboard.twitch.tv/settings/stream";
  562. } else if (serviceName.startsWith("YouTube")) {
  563. streamKeyLink = "https://www.youtube.com/live_dashboard";
  564. isYoutube = true;
  565. } else if (serviceName.startsWith("Restream.io")) {
  566. streamKeyLink =
  567. "https://restream.io/settings/streaming-setup?from=OBS";
  568. } else if (serviceName == "Luzento.com - RTMP") {
  569. streamKeyLink =
  570. "https://cms.luzento.com/dashboard/stream-key?from=OBS";
  571. } else if (serviceName == "Facebook Live" ||
  572. (customServer.contains("fbcdn.net") && IsCustomService())) {
  573. streamKeyLink =
  574. "https://www.facebook.com/live/producer?ref=OBS";
  575. } else if (serviceName.startsWith("Twitter")) {
  576. streamKeyLink = "https://studio.twitter.com/producer/sources";
  577. } else if (serviceName.startsWith("YouStreamer")) {
  578. streamKeyLink = "https://www.app.youstreamer.com/stream/";
  579. } else if (serviceName == "Trovo") {
  580. streamKeyLink = "https://studio.trovo.live/mychannel/stream";
  581. } else if (serviceName == "Glimesh") {
  582. streamKeyLink = "https://glimesh.tv/users/settings/stream";
  583. } else if (serviceName.startsWith("OPENREC.tv")) {
  584. streamKeyLink =
  585. "https://www.openrec.tv/login?keep_login=true&url=https://www.openrec.tv/dashboard/live?from=obs";
  586. } else if (serviceName == "Brime Live") {
  587. streamKeyLink = "https://brimelive.com/obs-stream-key-link";
  588. }
  589. if (serviceName == "Dacast") {
  590. ui->streamKeyLabel->setText(
  591. QTStr("Basic.AutoConfig.StreamPage.EncoderKey"));
  592. } else {
  593. ui->streamKeyLabel->setText(
  594. QTStr("Basic.AutoConfig.StreamPage.StreamKey"));
  595. }
  596. if (QString(streamKeyLink).isNull()) {
  597. ui->streamKeyButton->hide();
  598. } else {
  599. ui->streamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  600. ui->streamKeyButton->show();
  601. }
  602. if (isYoutube) {
  603. ui->doBandwidthTest->setChecked(false);
  604. ui->doBandwidthTest->setEnabled(false);
  605. } else {
  606. ui->doBandwidthTest->setEnabled(true);
  607. }
  608. }
  609. void AutoConfigStreamPage::LoadServices(bool showAll)
  610. {
  611. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  612. OBSData settings = obs_data_create();
  613. obs_data_release(settings);
  614. obs_data_set_bool(settings, "show_all", showAll);
  615. obs_property_t *prop = obs_properties_get(props, "show_all");
  616. obs_property_modified(prop, settings);
  617. ui->service->blockSignals(true);
  618. ui->service->clear();
  619. QStringList names;
  620. obs_property_t *services = obs_properties_get(props, "service");
  621. size_t services_count = obs_property_list_item_count(services);
  622. for (size_t i = 0; i < services_count; i++) {
  623. const char *name = obs_property_list_item_string(services, i);
  624. names.push_back(name);
  625. }
  626. if (showAll)
  627. names.sort(Qt::CaseInsensitive);
  628. for (QString &name : names)
  629. ui->service->addItem(name);
  630. if (!showAll) {
  631. ui->service->addItem(
  632. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  633. QVariant((int)ListOpt::ShowAll));
  634. }
  635. ui->service->insertItem(
  636. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  637. QVariant((int)ListOpt::Custom));
  638. if (!lastService.isEmpty()) {
  639. int idx = ui->service->findText(lastService);
  640. if (idx != -1)
  641. ui->service->setCurrentIndex(idx);
  642. }
  643. obs_properties_destroy(props);
  644. ui->service->blockSignals(false);
  645. }
  646. void AutoConfigStreamPage::UpdateServerList()
  647. {
  648. QString serviceName = ui->service->currentText();
  649. bool showMore = ui->service->currentData().toInt() ==
  650. (int)ListOpt::ShowAll;
  651. if (showMore) {
  652. LoadServices(true);
  653. ui->service->showPopup();
  654. return;
  655. } else {
  656. lastService = serviceName;
  657. }
  658. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  659. obs_property_t *services = obs_properties_get(props, "service");
  660. OBSData settings = obs_data_create();
  661. obs_data_release(settings);
  662. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  663. obs_property_modified(services, settings);
  664. obs_property_t *servers = obs_properties_get(props, "server");
  665. ui->server->clear();
  666. size_t servers_count = obs_property_list_item_count(servers);
  667. for (size_t i = 0; i < servers_count; i++) {
  668. const char *name = obs_property_list_item_name(servers, i);
  669. const char *server = obs_property_list_item_string(servers, i);
  670. ui->server->addItem(name, server);
  671. }
  672. obs_properties_destroy(props);
  673. }
  674. void AutoConfigStreamPage::UpdateCompleted()
  675. {
  676. if (ui->stackedWidget->currentIndex() == (int)Section::Connect ||
  677. (ui->key->text().isEmpty() && !auth)) {
  678. ready = false;
  679. } else {
  680. bool custom = IsCustomService();
  681. if (custom) {
  682. ready = !ui->customServer->text().isEmpty();
  683. } else {
  684. ready = !wiz->testRegions ||
  685. ui->regionUS->isChecked() ||
  686. ui->regionEU->isChecked() ||
  687. ui->regionAsia->isChecked() ||
  688. ui->regionOther->isChecked();
  689. }
  690. }
  691. emit completeChanged();
  692. }
  693. /* ------------------------------------------------------------------------- */
  694. AutoConfig::AutoConfig(QWidget *parent) : QWizard(parent)
  695. {
  696. EnableThreadedMessageBoxes(true);
  697. calldata_t cd = {0};
  698. calldata_set_int(&cd, "seconds", 5);
  699. proc_handler_t *ph = obs_get_proc_handler();
  700. proc_handler_call(ph, "twitch_ingests_refresh", &cd);
  701. calldata_free(&cd);
  702. OBSBasic *main = reinterpret_cast<OBSBasic *>(parent);
  703. main->EnableOutputs(false);
  704. installEventFilter(CreateShortcutFilter());
  705. std::string serviceType;
  706. GetServiceInfo(serviceType, serviceName, server, key);
  707. #if defined(_WIN32) || defined(__APPLE__)
  708. setWizardStyle(QWizard::ModernStyle);
  709. #endif
  710. streamPage = new AutoConfigStreamPage();
  711. setPage(StartPage, new AutoConfigStartPage());
  712. setPage(VideoPage, new AutoConfigVideoPage());
  713. setPage(StreamPage, streamPage);
  714. setPage(TestPage, new AutoConfigTestPage());
  715. setWindowTitle(QTStr("Basic.AutoConfig"));
  716. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  717. obs_video_info ovi;
  718. obs_get_video_info(&ovi);
  719. baseResolutionCX = ovi.base_width;
  720. baseResolutionCY = ovi.base_height;
  721. /* ----------------------------------------- */
  722. /* check to see if Twitch's "auto" available */
  723. OBSData twitchSettings = obs_data_create();
  724. obs_data_release(twitchSettings);
  725. obs_data_set_string(twitchSettings, "service", "Twitch");
  726. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  727. obs_properties_apply_settings(props, twitchSettings);
  728. obs_property_t *p = obs_properties_get(props, "server");
  729. const char *first = obs_property_list_item_string(p, 0);
  730. twitchAuto = strcmp(first, "auto") == 0;
  731. obs_properties_destroy(props);
  732. /* ----------------------------------------- */
  733. /* load service/servers */
  734. customServer = serviceType == "rtmp_custom";
  735. QComboBox *serviceList = streamPage->ui->service;
  736. if (!serviceName.empty()) {
  737. serviceList->blockSignals(true);
  738. int count = serviceList->count();
  739. bool found = false;
  740. for (int i = 0; i < count; i++) {
  741. QString name = serviceList->itemText(i);
  742. if (name == serviceName.c_str()) {
  743. serviceList->setCurrentIndex(i);
  744. found = true;
  745. break;
  746. }
  747. }
  748. if (!found) {
  749. serviceList->insertItem(0, serviceName.c_str());
  750. serviceList->setCurrentIndex(0);
  751. }
  752. serviceList->blockSignals(false);
  753. }
  754. streamPage->UpdateServerList();
  755. streamPage->UpdateKeyLink();
  756. streamPage->UpdateMoreInfoLink();
  757. streamPage->lastService.clear();
  758. if (!customServer) {
  759. QComboBox *serverList = streamPage->ui->server;
  760. int idx = serverList->findData(QString(server.c_str()));
  761. if (idx == -1)
  762. idx = 0;
  763. serverList->setCurrentIndex(idx);
  764. } else {
  765. streamPage->ui->customServer->setText(server.c_str());
  766. int idx = streamPage->ui->service->findData(
  767. QVariant((int)ListOpt::Custom));
  768. streamPage->ui->service->setCurrentIndex(idx);
  769. }
  770. if (!key.empty())
  771. streamPage->ui->key->setText(key.c_str());
  772. int bitrate =
  773. config_get_int(main->Config(), "SimpleOutput", "VBitrate");
  774. streamPage->ui->bitrate->setValue(bitrate);
  775. streamPage->ServiceChanged();
  776. TestHardwareEncoding();
  777. if (!hardwareEncodingAvailable) {
  778. delete streamPage->ui->preferHardware;
  779. streamPage->ui->preferHardware = nullptr;
  780. } else {
  781. /* Newer generations of NVENC have a high enough quality to
  782. * bitrate ratio that if NVENC is available, it makes sense to
  783. * just always prefer hardware encoding by default */
  784. bool preferHardware = nvencAvailable ||
  785. os_get_physical_cores() <= 4;
  786. streamPage->ui->preferHardware->setChecked(preferHardware);
  787. }
  788. setOptions(QWizard::WizardOptions());
  789. setButtonText(QWizard::FinishButton,
  790. QTStr("Basic.AutoConfig.ApplySettings"));
  791. setButtonText(QWizard::BackButton, QTStr("Back"));
  792. setButtonText(QWizard::NextButton, QTStr("Next"));
  793. setButtonText(QWizard::CancelButton, QTStr("Cancel"));
  794. }
  795. AutoConfig::~AutoConfig()
  796. {
  797. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  798. main->EnableOutputs(true);
  799. EnableThreadedMessageBoxes(false);
  800. }
  801. void AutoConfig::TestHardwareEncoding()
  802. {
  803. size_t idx = 0;
  804. const char *id;
  805. while (obs_enum_encoder_types(idx++, &id)) {
  806. if (strcmp(id, "ffmpeg_nvenc") == 0)
  807. hardwareEncodingAvailable = nvencAvailable = true;
  808. else if (strcmp(id, "obs_qsv11") == 0)
  809. hardwareEncodingAvailable = qsvAvailable = true;
  810. else if (strcmp(id, "amd_amf_h264") == 0)
  811. hardwareEncodingAvailable = vceAvailable = true;
  812. }
  813. }
  814. bool AutoConfig::CanTestServer(const char *server)
  815. {
  816. if (!testRegions || (regionUS && regionEU && regionAsia && regionOther))
  817. return true;
  818. if (service == Service::Twitch) {
  819. if (astrcmp_n(server, "US West:", 8) == 0 ||
  820. astrcmp_n(server, "US East:", 8) == 0 ||
  821. astrcmp_n(server, "US Central:", 11) == 0) {
  822. return regionUS;
  823. } else if (astrcmp_n(server, "EU:", 3) == 0) {
  824. return regionEU;
  825. } else if (astrcmp_n(server, "Asia:", 5) == 0) {
  826. return regionAsia;
  827. } else if (regionOther) {
  828. return true;
  829. }
  830. } else {
  831. return true;
  832. }
  833. return false;
  834. }
  835. void AutoConfig::done(int result)
  836. {
  837. QWizard::done(result);
  838. if (result == QDialog::Accepted) {
  839. if (type == Type::Streaming)
  840. SaveStreamSettings();
  841. SaveSettings();
  842. }
  843. }
  844. inline const char *AutoConfig::GetEncoderId(Encoder enc)
  845. {
  846. switch (enc) {
  847. case Encoder::NVENC:
  848. return SIMPLE_ENCODER_NVENC;
  849. case Encoder::QSV:
  850. return SIMPLE_ENCODER_QSV;
  851. case Encoder::AMD:
  852. return SIMPLE_ENCODER_AMD;
  853. default:
  854. return SIMPLE_ENCODER_X264;
  855. }
  856. };
  857. void AutoConfig::SaveStreamSettings()
  858. {
  859. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  860. /* ---------------------------------- */
  861. /* save service */
  862. const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
  863. obs_service_t *oldService = main->GetService();
  864. OBSData hotkeyData = obs_hotkeys_save_service(oldService);
  865. obs_data_release(hotkeyData);
  866. OBSData settings = obs_data_create();
  867. obs_data_release(settings);
  868. if (!customServer)
  869. obs_data_set_string(settings, "service", serviceName.c_str());
  870. obs_data_set_string(settings, "server", server.c_str());
  871. obs_data_set_string(settings, "key", key.c_str());
  872. OBSService newService = obs_service_create(
  873. service_id, "default_service", settings, hotkeyData);
  874. obs_service_release(newService);
  875. if (!newService)
  876. return;
  877. main->SetService(newService);
  878. main->SaveService();
  879. main->auth = streamPage->auth;
  880. if (!!main->auth)
  881. main->auth->LoadUI();
  882. /* ---------------------------------- */
  883. /* save stream settings */
  884. config_set_int(main->Config(), "SimpleOutput", "VBitrate",
  885. idealBitrate);
  886. config_set_string(main->Config(), "SimpleOutput", "StreamEncoder",
  887. GetEncoderId(streamingEncoder));
  888. config_remove_value(main->Config(), "SimpleOutput", "UseAdvanced");
  889. }
  890. void AutoConfig::SaveSettings()
  891. {
  892. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  893. if (recordingEncoder != Encoder::Stream)
  894. config_set_string(main->Config(), "SimpleOutput", "RecEncoder",
  895. GetEncoderId(recordingEncoder));
  896. const char *quality = recordingQuality == Quality::High ? "Small"
  897. : "Stream";
  898. config_set_string(main->Config(), "Output", "Mode", "Simple");
  899. config_set_string(main->Config(), "SimpleOutput", "RecQuality",
  900. quality);
  901. config_set_int(main->Config(), "Video", "BaseCX", baseResolutionCX);
  902. config_set_int(main->Config(), "Video", "BaseCY", baseResolutionCY);
  903. config_set_int(main->Config(), "Video", "OutputCX", idealResolutionCX);
  904. config_set_int(main->Config(), "Video", "OutputCY", idealResolutionCY);
  905. if (fpsType != FPSType::UseCurrent) {
  906. config_set_uint(main->Config(), "Video", "FPSType", 0);
  907. config_set_string(main->Config(), "Video", "FPSCommon",
  908. std::to_string(idealFPSNum).c_str());
  909. }
  910. main->ResetVideo();
  911. main->ResetOutputs();
  912. config_save_safe(main->Config(), "tmp", nullptr);
  913. }