window-basic-auto-config.cpp 29 KB

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