window-basic-auto-config.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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->doBandwidthTest, SIGNAL(toggled(bool)), this,
  219. SLOT(ServiceChanged()));
  220. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  221. SLOT(UpdateServerList()));
  222. connect(ui->service, SIGNAL(currentIndexChanged(int)), this,
  223. SLOT(UpdateKeyLink()));
  224. connect(ui->key, SIGNAL(textChanged(const QString &)), this,
  225. SLOT(UpdateCompleted()));
  226. connect(ui->regionUS, SIGNAL(toggled(bool)), this,
  227. SLOT(UpdateCompleted()));
  228. connect(ui->regionEU, SIGNAL(toggled(bool)), this,
  229. SLOT(UpdateCompleted()));
  230. connect(ui->regionAsia, SIGNAL(toggled(bool)), this,
  231. SLOT(UpdateCompleted()));
  232. connect(ui->regionOther, SIGNAL(toggled(bool)), this,
  233. SLOT(UpdateCompleted()));
  234. }
  235. AutoConfigStreamPage::~AutoConfigStreamPage()
  236. {
  237. delete ui;
  238. }
  239. bool AutoConfigStreamPage::isComplete() const
  240. {
  241. return ready;
  242. }
  243. int AutoConfigStreamPage::nextId() const
  244. {
  245. return AutoConfig::TestPage;
  246. }
  247. inline bool AutoConfigStreamPage::IsCustomService() const
  248. {
  249. return ui->service->currentData().toInt() == (int)ListOpt::Custom;
  250. }
  251. bool AutoConfigStreamPage::validatePage()
  252. {
  253. OBSData service_settings = obs_data_create();
  254. obs_data_release(service_settings);
  255. wiz->customServer = IsCustomService();
  256. const char *serverType = wiz->customServer ? "rtmp_custom"
  257. : "rtmp_common";
  258. if (!wiz->customServer) {
  259. obs_data_set_string(service_settings, "service",
  260. QT_TO_UTF8(ui->service->currentText()));
  261. }
  262. OBSService service = obs_service_create(serverType, "temp_service",
  263. service_settings, nullptr);
  264. obs_service_release(service);
  265. int bitrate = 10000;
  266. if (!ui->doBandwidthTest->isChecked()) {
  267. bitrate = ui->bitrate->value();
  268. wiz->idealBitrate = bitrate;
  269. }
  270. OBSData settings = obs_data_create();
  271. obs_data_release(settings);
  272. obs_data_set_int(settings, "bitrate", bitrate);
  273. obs_service_apply_encoder_settings(service, settings, nullptr);
  274. if (wiz->customServer) {
  275. QString server = ui->customServer->text();
  276. wiz->server = wiz->serverName = QT_TO_UTF8(server);
  277. } else {
  278. wiz->serverName = QT_TO_UTF8(ui->server->currentText());
  279. wiz->server = QT_TO_UTF8(ui->server->currentData().toString());
  280. }
  281. wiz->bandwidthTest = ui->doBandwidthTest->isChecked();
  282. wiz->startingBitrate = (int)obs_data_get_int(settings, "bitrate");
  283. wiz->idealBitrate = wiz->startingBitrate;
  284. wiz->regionUS = ui->regionUS->isChecked();
  285. wiz->regionEU = ui->regionEU->isChecked();
  286. wiz->regionAsia = ui->regionAsia->isChecked();
  287. wiz->regionOther = ui->regionOther->isChecked();
  288. wiz->serviceName = QT_TO_UTF8(ui->service->currentText());
  289. if (ui->preferHardware)
  290. wiz->preferHardware = ui->preferHardware->isChecked();
  291. wiz->key = QT_TO_UTF8(ui->key->text());
  292. if (!wiz->customServer) {
  293. if (wiz->serviceName == "Twitch")
  294. wiz->service = AutoConfig::Service::Twitch;
  295. else if (wiz->serviceName == "Smashcast")
  296. wiz->service = AutoConfig::Service::Smashcast;
  297. else
  298. wiz->service = AutoConfig::Service::Other;
  299. } else {
  300. wiz->service = AutoConfig::Service::Other;
  301. }
  302. if (wiz->service != AutoConfig::Service::Twitch && wiz->bandwidthTest) {
  303. QMessageBox::StandardButton button;
  304. #define WARNING_TEXT(x) QTStr("Basic.AutoConfig.StreamPage.StreamWarning." x)
  305. button = OBSMessageBox::question(this, WARNING_TEXT("Title"),
  306. WARNING_TEXT("Text"));
  307. #undef WARNING_TEXT
  308. if (button == QMessageBox::No)
  309. return false;
  310. }
  311. return true;
  312. }
  313. void AutoConfigStreamPage::on_show_clicked()
  314. {
  315. if (ui->key->echoMode() == QLineEdit::Password) {
  316. ui->key->setEchoMode(QLineEdit::Normal);
  317. ui->show->setText(QTStr("Hide"));
  318. } else {
  319. ui->key->setEchoMode(QLineEdit::Password);
  320. ui->show->setText(QTStr("Show"));
  321. }
  322. }
  323. void AutoConfigStreamPage::OnOAuthStreamKeyConnected()
  324. {
  325. #ifdef BROWSER_AVAILABLE
  326. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  327. if (a) {
  328. bool validKey = !a->key().empty();
  329. if (validKey)
  330. ui->key->setText(QT_UTF8(a->key().c_str()));
  331. ui->streamKeyWidget->setVisible(!validKey);
  332. ui->streamKeyLabel->setVisible(!validKey);
  333. ui->connectAccount2->setVisible(!validKey);
  334. ui->disconnectAccount->setVisible(validKey);
  335. }
  336. ui->stackedWidget->setCurrentIndex((int)Section::StreamKey);
  337. UpdateCompleted();
  338. #endif
  339. }
  340. void AutoConfigStreamPage::OnAuthConnected()
  341. {
  342. std::string service = QT_TO_UTF8(ui->service->currentText());
  343. Auth::Type type = Auth::AuthType(service);
  344. if (type == Auth::Type::OAuth_StreamKey) {
  345. OnOAuthStreamKeyConnected();
  346. }
  347. }
  348. void AutoConfigStreamPage::on_connectAccount_clicked()
  349. {
  350. #ifdef BROWSER_AVAILABLE
  351. std::string service = QT_TO_UTF8(ui->service->currentText());
  352. OAuth::DeleteCookies(service);
  353. auth = OAuthStreamKey::Login(this, service);
  354. if (!!auth)
  355. OnAuthConnected();
  356. #endif
  357. }
  358. #define DISCONNECT_COMFIRM_TITLE \
  359. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
  360. #define DISCONNECT_COMFIRM_TEXT \
  361. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"
  362. void AutoConfigStreamPage::on_disconnectAccount_clicked()
  363. {
  364. QMessageBox::StandardButton button;
  365. button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
  366. QTStr(DISCONNECT_COMFIRM_TEXT));
  367. if (button == QMessageBox::No) {
  368. return;
  369. }
  370. OBSBasic *main = OBSBasic::Get();
  371. main->auth.reset();
  372. auth.reset();
  373. std::string service = QT_TO_UTF8(ui->service->currentText());
  374. #ifdef BROWSER_AVAILABLE
  375. OAuth::DeleteCookies(service);
  376. #endif
  377. ui->streamKeyWidget->setVisible(true);
  378. ui->streamKeyLabel->setVisible(true);
  379. ui->connectAccount2->setVisible(true);
  380. ui->disconnectAccount->setVisible(false);
  381. ui->key->setText("");
  382. }
  383. void AutoConfigStreamPage::on_useStreamKey_clicked()
  384. {
  385. ui->stackedWidget->setCurrentIndex((int)Section::StreamKey);
  386. UpdateCompleted();
  387. }
  388. static inline bool is_auth_service(const std::string &service)
  389. {
  390. return Auth::AuthType(service) != Auth::Type::None;
  391. }
  392. void AutoConfigStreamPage::ServiceChanged()
  393. {
  394. bool showMore = ui->service->currentData().toInt() ==
  395. (int)ListOpt::ShowAll;
  396. if (showMore)
  397. return;
  398. std::string service = QT_TO_UTF8(ui->service->currentText());
  399. bool regionBased = service == "Twitch" || service == "Smashcast";
  400. bool testBandwidth = ui->doBandwidthTest->isChecked();
  401. bool custom = IsCustomService();
  402. ui->disconnectAccount->setVisible(false);
  403. #ifdef BROWSER_AVAILABLE
  404. if (cef) {
  405. if (lastService != service.c_str()) {
  406. bool can_auth = is_auth_service(service);
  407. int page = can_auth ? (int)Section::Connect
  408. : (int)Section::StreamKey;
  409. ui->stackedWidget->setCurrentIndex(page);
  410. ui->streamKeyWidget->setVisible(true);
  411. ui->streamKeyLabel->setVisible(true);
  412. ui->connectAccount2->setVisible(can_auth);
  413. auth.reset();
  414. if (lastService.isEmpty())
  415. lastService = service.c_str();
  416. }
  417. } else {
  418. ui->connectAccount2->setVisible(false);
  419. }
  420. #else
  421. ui->connectAccount2->setVisible(false);
  422. #endif
  423. /* Test three closest servers if "Auto" is available for Twitch */
  424. if (service == "Twitch" && wiz->twitchAuto)
  425. regionBased = false;
  426. ui->streamkeyPageLayout->removeWidget(ui->serverLabel);
  427. ui->streamkeyPageLayout->removeWidget(ui->serverStackedWidget);
  428. if (custom) {
  429. ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
  430. ui->serverStackedWidget);
  431. ui->region->setVisible(false);
  432. ui->serverStackedWidget->setCurrentIndex(1);
  433. ui->serverStackedWidget->setVisible(true);
  434. ui->serverLabel->setVisible(true);
  435. } else {
  436. if (!testBandwidth)
  437. ui->streamkeyPageLayout->insertRow(
  438. 2, ui->serverLabel, ui->serverStackedWidget);
  439. ui->region->setVisible(regionBased && testBandwidth);
  440. ui->serverStackedWidget->setCurrentIndex(0);
  441. ui->serverStackedWidget->setHidden(testBandwidth);
  442. ui->serverLabel->setHidden(testBandwidth);
  443. }
  444. wiz->testRegions = regionBased && testBandwidth;
  445. ui->bitrateLabel->setHidden(testBandwidth);
  446. ui->bitrate->setHidden(testBandwidth);
  447. #ifdef BROWSER_AVAILABLE
  448. OBSBasic *main = OBSBasic::Get();
  449. if (!!main->auth &&
  450. service.find(main->auth->service()) != std::string::npos) {
  451. auth = main->auth;
  452. OnAuthConnected();
  453. }
  454. #endif
  455. UpdateCompleted();
  456. }
  457. void AutoConfigStreamPage::UpdateKeyLink()
  458. {
  459. if (IsCustomService()) {
  460. ui->doBandwidthTest->setEnabled(true);
  461. return;
  462. }
  463. QString serviceName = ui->service->currentText();
  464. bool isYoutube = false;
  465. QString streamKeyLink;
  466. if (serviceName == "Twitch") {
  467. streamKeyLink =
  468. "https://www.twitch.tv/broadcast/dashboard/streamkey";
  469. } else if (serviceName == "YouTube / YouTube Gaming") {
  470. streamKeyLink = "https://www.youtube.com/live_dashboard";
  471. isYoutube = true;
  472. } else if (serviceName.startsWith("Restream.io")) {
  473. streamKeyLink =
  474. "https://restream.io/settings/streaming-setup?from=OBS";
  475. } else if (serviceName == "Facebook Live") {
  476. streamKeyLink = "https://www.facebook.com/live/create?ref=OBS";
  477. } else if (serviceName.startsWith("Twitter")) {
  478. streamKeyLink = "https://www.pscp.tv/account/producer";
  479. } else if (serviceName.startsWith("YouStreamer")) {
  480. streamKeyLink = "https://www.app.youstreamer.com/stream/";
  481. } else if (serviceName == "Trovo") {
  482. streamKeyLink = "https://studio.trovo.live/mychannel/stream";
  483. }
  484. if (QString(streamKeyLink).isNull()) {
  485. ui->streamKeyButton->hide();
  486. } else {
  487. ui->streamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  488. ui->streamKeyButton->show();
  489. }
  490. if (isYoutube) {
  491. ui->doBandwidthTest->setChecked(false);
  492. ui->doBandwidthTest->setEnabled(false);
  493. } else {
  494. ui->doBandwidthTest->setEnabled(true);
  495. }
  496. }
  497. void AutoConfigStreamPage::LoadServices(bool showAll)
  498. {
  499. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  500. OBSData settings = obs_data_create();
  501. obs_data_release(settings);
  502. obs_data_set_bool(settings, "show_all", showAll);
  503. obs_property_t *prop = obs_properties_get(props, "show_all");
  504. obs_property_modified(prop, settings);
  505. ui->service->blockSignals(true);
  506. ui->service->clear();
  507. QStringList names;
  508. obs_property_t *services = obs_properties_get(props, "service");
  509. size_t services_count = obs_property_list_item_count(services);
  510. for (size_t i = 0; i < services_count; i++) {
  511. const char *name = obs_property_list_item_string(services, i);
  512. names.push_back(name);
  513. }
  514. if (showAll)
  515. names.sort();
  516. for (QString &name : names)
  517. ui->service->addItem(name);
  518. if (!showAll) {
  519. ui->service->addItem(
  520. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  521. QVariant((int)ListOpt::ShowAll));
  522. }
  523. ui->service->insertItem(
  524. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  525. QVariant((int)ListOpt::Custom));
  526. if (!lastService.isEmpty()) {
  527. int idx = ui->service->findText(lastService);
  528. if (idx != -1)
  529. ui->service->setCurrentIndex(idx);
  530. }
  531. obs_properties_destroy(props);
  532. ui->service->blockSignals(false);
  533. }
  534. void AutoConfigStreamPage::UpdateServerList()
  535. {
  536. QString serviceName = ui->service->currentText();
  537. bool showMore = ui->service->currentData().toInt() ==
  538. (int)ListOpt::ShowAll;
  539. if (showMore) {
  540. LoadServices(true);
  541. ui->service->showPopup();
  542. return;
  543. } else {
  544. lastService = serviceName;
  545. }
  546. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  547. obs_property_t *services = obs_properties_get(props, "service");
  548. OBSData settings = obs_data_create();
  549. obs_data_release(settings);
  550. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  551. obs_property_modified(services, settings);
  552. obs_property_t *servers = obs_properties_get(props, "server");
  553. ui->server->clear();
  554. size_t servers_count = obs_property_list_item_count(servers);
  555. for (size_t i = 0; i < servers_count; i++) {
  556. const char *name = obs_property_list_item_name(servers, i);
  557. const char *server = obs_property_list_item_string(servers, i);
  558. ui->server->addItem(name, server);
  559. }
  560. obs_properties_destroy(props);
  561. }
  562. void AutoConfigStreamPage::UpdateCompleted()
  563. {
  564. if (ui->stackedWidget->currentIndex() == (int)Section::Connect ||
  565. (ui->key->text().isEmpty() && !auth)) {
  566. ready = false;
  567. } else {
  568. bool custom = IsCustomService();
  569. if (custom) {
  570. ready = !ui->customServer->text().isEmpty();
  571. } else {
  572. ready = !wiz->testRegions ||
  573. ui->regionUS->isChecked() ||
  574. ui->regionEU->isChecked() ||
  575. ui->regionAsia->isChecked() ||
  576. ui->regionOther->isChecked();
  577. }
  578. }
  579. emit completeChanged();
  580. }
  581. /* ------------------------------------------------------------------------- */
  582. AutoConfig::AutoConfig(QWidget *parent) : QWizard(parent)
  583. {
  584. EnableThreadedMessageBoxes(true);
  585. calldata_t cd = {0};
  586. calldata_set_int(&cd, "seconds", 5);
  587. proc_handler_t *ph = obs_get_proc_handler();
  588. proc_handler_call(ph, "twitch_ingests_refresh", &cd);
  589. calldata_free(&cd);
  590. OBSBasic *main = reinterpret_cast<OBSBasic *>(parent);
  591. main->EnableOutputs(false);
  592. installEventFilter(CreateShortcutFilter());
  593. std::string serviceType;
  594. GetServiceInfo(serviceType, serviceName, server, key);
  595. #ifdef _WIN32
  596. setWizardStyle(QWizard::ModernStyle);
  597. #endif
  598. streamPage = new AutoConfigStreamPage();
  599. setPage(StartPage, new AutoConfigStartPage());
  600. setPage(VideoPage, new AutoConfigVideoPage());
  601. setPage(StreamPage, streamPage);
  602. setPage(TestPage, new AutoConfigTestPage());
  603. setWindowTitle(QTStr("Basic.AutoConfig"));
  604. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  605. obs_video_info ovi;
  606. obs_get_video_info(&ovi);
  607. baseResolutionCX = ovi.base_width;
  608. baseResolutionCY = ovi.base_height;
  609. /* ----------------------------------------- */
  610. /* check to see if Twitch's "auto" available */
  611. OBSData twitchSettings = obs_data_create();
  612. obs_data_release(twitchSettings);
  613. obs_data_set_string(twitchSettings, "service", "Twitch");
  614. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  615. obs_properties_apply_settings(props, twitchSettings);
  616. obs_property_t *p = obs_properties_get(props, "server");
  617. const char *first = obs_property_list_item_string(p, 0);
  618. twitchAuto = strcmp(first, "auto") == 0;
  619. obs_properties_destroy(props);
  620. /* ----------------------------------------- */
  621. /* load service/servers */
  622. customServer = serviceType == "rtmp_custom";
  623. QComboBox *serviceList = streamPage->ui->service;
  624. if (!serviceName.empty()) {
  625. serviceList->blockSignals(true);
  626. int count = serviceList->count();
  627. bool found = false;
  628. for (int i = 0; i < count; i++) {
  629. QString name = serviceList->itemText(i);
  630. if (name == serviceName.c_str()) {
  631. serviceList->setCurrentIndex(i);
  632. found = true;
  633. break;
  634. }
  635. }
  636. if (!found) {
  637. serviceList->insertItem(0, serviceName.c_str());
  638. serviceList->setCurrentIndex(0);
  639. }
  640. serviceList->blockSignals(false);
  641. }
  642. streamPage->UpdateServerList();
  643. streamPage->UpdateKeyLink();
  644. streamPage->lastService.clear();
  645. if (!customServer) {
  646. QComboBox *serverList = streamPage->ui->server;
  647. int idx = serverList->findData(QString(server.c_str()));
  648. if (idx == -1)
  649. idx = 0;
  650. serverList->setCurrentIndex(idx);
  651. } else {
  652. streamPage->ui->customServer->setText(server.c_str());
  653. int idx = streamPage->ui->service->findData(
  654. QVariant((int)ListOpt::Custom));
  655. streamPage->ui->service->setCurrentIndex(idx);
  656. }
  657. if (!key.empty())
  658. streamPage->ui->key->setText(key.c_str());
  659. int bitrate =
  660. config_get_int(main->Config(), "SimpleOutput", "VBitrate");
  661. streamPage->ui->bitrate->setValue(bitrate);
  662. streamPage->ServiceChanged();
  663. TestHardwareEncoding();
  664. if (!hardwareEncodingAvailable) {
  665. delete streamPage->ui->preferHardware;
  666. streamPage->ui->preferHardware = nullptr;
  667. } else {
  668. /* Newer generations of NVENC have a high enough quality to
  669. * bitrate ratio that if NVENC is available, it makes sense to
  670. * just always prefer hardware encoding by default */
  671. bool preferHardware = nvencAvailable ||
  672. os_get_physical_cores() <= 4;
  673. streamPage->ui->preferHardware->setChecked(preferHardware);
  674. }
  675. setOptions(0);
  676. setButtonText(QWizard::FinishButton,
  677. QTStr("Basic.AutoConfig.ApplySettings"));
  678. setButtonText(QWizard::BackButton, QTStr("Back"));
  679. setButtonText(QWizard::NextButton, QTStr("Next"));
  680. setButtonText(QWizard::CancelButton, QTStr("Cancel"));
  681. }
  682. AutoConfig::~AutoConfig()
  683. {
  684. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  685. main->EnableOutputs(true);
  686. EnableThreadedMessageBoxes(false);
  687. }
  688. void AutoConfig::TestHardwareEncoding()
  689. {
  690. size_t idx = 0;
  691. const char *id;
  692. while (obs_enum_encoder_types(idx++, &id)) {
  693. if (strcmp(id, "ffmpeg_nvenc") == 0)
  694. hardwareEncodingAvailable = nvencAvailable = true;
  695. else if (strcmp(id, "obs_qsv11") == 0)
  696. hardwareEncodingAvailable = qsvAvailable = true;
  697. else if (strcmp(id, "amd_amf_h264") == 0)
  698. hardwareEncodingAvailable = vceAvailable = true;
  699. }
  700. }
  701. bool AutoConfig::CanTestServer(const char *server)
  702. {
  703. if (!testRegions || (regionUS && regionEU && regionAsia && regionOther))
  704. return true;
  705. if (service == Service::Twitch) {
  706. if (astrcmp_n(server, "US West:", 8) == 0 ||
  707. astrcmp_n(server, "US East:", 8) == 0 ||
  708. astrcmp_n(server, "US Central:", 11) == 0) {
  709. return regionUS;
  710. } else if (astrcmp_n(server, "EU:", 3) == 0) {
  711. return regionEU;
  712. } else if (astrcmp_n(server, "Asia:", 5) == 0) {
  713. return regionAsia;
  714. } else if (regionOther) {
  715. return true;
  716. }
  717. } else if (service == Service::Smashcast) {
  718. if (strcmp(server, "Default") == 0) {
  719. return true;
  720. } else if (astrcmp_n(server, "US-West:", 8) == 0 ||
  721. astrcmp_n(server, "US-East:", 8) == 0) {
  722. return regionUS;
  723. } else if (astrcmp_n(server, "EU-", 3) == 0) {
  724. return regionEU;
  725. } else if (astrcmp_n(server, "South Korea:", 12) == 0 ||
  726. astrcmp_n(server, "Asia:", 5) == 0 ||
  727. astrcmp_n(server, "China:", 6) == 0) {
  728. return regionAsia;
  729. } else if (regionOther) {
  730. return true;
  731. }
  732. } else {
  733. return true;
  734. }
  735. return false;
  736. }
  737. void AutoConfig::done(int result)
  738. {
  739. QWizard::done(result);
  740. if (result == QDialog::Accepted) {
  741. if (type == Type::Streaming)
  742. SaveStreamSettings();
  743. SaveSettings();
  744. }
  745. }
  746. inline const char *AutoConfig::GetEncoderId(Encoder enc)
  747. {
  748. switch (enc) {
  749. case Encoder::NVENC:
  750. return SIMPLE_ENCODER_NVENC;
  751. case Encoder::QSV:
  752. return SIMPLE_ENCODER_QSV;
  753. case Encoder::AMD:
  754. return SIMPLE_ENCODER_AMD;
  755. default:
  756. return SIMPLE_ENCODER_X264;
  757. }
  758. };
  759. void AutoConfig::SaveStreamSettings()
  760. {
  761. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  762. /* ---------------------------------- */
  763. /* save service */
  764. const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
  765. obs_service_t *oldService = main->GetService();
  766. OBSData hotkeyData = obs_hotkeys_save_service(oldService);
  767. obs_data_release(hotkeyData);
  768. OBSData settings = obs_data_create();
  769. obs_data_release(settings);
  770. if (!customServer)
  771. obs_data_set_string(settings, "service", serviceName.c_str());
  772. obs_data_set_string(settings, "server", server.c_str());
  773. obs_data_set_string(settings, "key", key.c_str());
  774. OBSService newService = obs_service_create(
  775. service_id, "default_service", settings, hotkeyData);
  776. obs_service_release(newService);
  777. if (!newService)
  778. return;
  779. main->SetService(newService);
  780. main->SaveService();
  781. main->auth = streamPage->auth;
  782. if (!!main->auth)
  783. main->auth->LoadUI();
  784. /* ---------------------------------- */
  785. /* save stream settings */
  786. config_set_int(main->Config(), "SimpleOutput", "VBitrate",
  787. idealBitrate);
  788. config_set_string(main->Config(), "SimpleOutput", "StreamEncoder",
  789. GetEncoderId(streamingEncoder));
  790. config_remove_value(main->Config(), "SimpleOutput", "UseAdvanced");
  791. }
  792. void AutoConfig::SaveSettings()
  793. {
  794. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  795. if (recordingEncoder != Encoder::Stream)
  796. config_set_string(main->Config(), "SimpleOutput", "RecEncoder",
  797. GetEncoderId(recordingEncoder));
  798. const char *quality = recordingQuality == Quality::High ? "Small"
  799. : "Stream";
  800. config_set_string(main->Config(), "Output", "Mode", "Simple");
  801. config_set_string(main->Config(), "SimpleOutput", "RecQuality",
  802. quality);
  803. config_set_int(main->Config(), "Video", "BaseCX", baseResolutionCX);
  804. config_set_int(main->Config(), "Video", "BaseCY", baseResolutionCY);
  805. config_set_int(main->Config(), "Video", "OutputCX", idealResolutionCX);
  806. config_set_int(main->Config(), "Video", "OutputCY", idealResolutionCY);
  807. if (fpsType != FPSType::UseCurrent) {
  808. config_set_uint(main->Config(), "Video", "FPSType", 0);
  809. config_set_string(main->Config(), "Video", "FPSCommon",
  810. std::to_string(idealFPSNum).c_str());
  811. }
  812. main->ResetVideo();
  813. main->ResetOutputs();
  814. config_save_safe(main->Config(), "tmp", nullptr);
  815. }