window-basic-auto-config.cpp 27 KB

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