window-basic-auto-config.cpp 28 KB

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