window-basic-auto-config.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 =
  494. "https://www.twitch.tv/broadcast/dashboard/streamkey";
  495. } else if (serviceName.startsWith("YouTube")) {
  496. streamKeyLink = "https://www.youtube.com/live_dashboard";
  497. isYoutube = true;
  498. } else if (serviceName.startsWith("Restream.io")) {
  499. streamKeyLink =
  500. "https://restream.io/settings/streaming-setup?from=OBS";
  501. } else if (serviceName == "Facebook Live" ||
  502. (customServer.contains("fbcdn.net") && IsCustomService())) {
  503. streamKeyLink =
  504. "https://www.facebook.com/live/producer?ref=OBS";
  505. } else if (serviceName.startsWith("Twitter")) {
  506. streamKeyLink = "https://www.pscp.tv/account/producer";
  507. } else if (serviceName.startsWith("YouStreamer")) {
  508. streamKeyLink = "https://www.app.youstreamer.com/stream/";
  509. } else if (serviceName == "Trovo") {
  510. streamKeyLink = "https://studio.trovo.live/mychannel/stream";
  511. }
  512. if (QString(streamKeyLink).isNull()) {
  513. ui->streamKeyButton->hide();
  514. } else {
  515. ui->streamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  516. ui->streamKeyButton->show();
  517. }
  518. if (isYoutube) {
  519. ui->doBandwidthTest->setChecked(false);
  520. ui->doBandwidthTest->setEnabled(false);
  521. } else {
  522. ui->doBandwidthTest->setEnabled(true);
  523. }
  524. }
  525. void AutoConfigStreamPage::LoadServices(bool showAll)
  526. {
  527. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  528. OBSData settings = obs_data_create();
  529. obs_data_release(settings);
  530. obs_data_set_bool(settings, "show_all", showAll);
  531. obs_property_t *prop = obs_properties_get(props, "show_all");
  532. obs_property_modified(prop, settings);
  533. ui->service->blockSignals(true);
  534. ui->service->clear();
  535. QStringList names;
  536. obs_property_t *services = obs_properties_get(props, "service");
  537. size_t services_count = obs_property_list_item_count(services);
  538. for (size_t i = 0; i < services_count; i++) {
  539. const char *name = obs_property_list_item_string(services, i);
  540. names.push_back(name);
  541. }
  542. if (showAll)
  543. names.sort(Qt::CaseInsensitive);
  544. for (QString &name : names)
  545. ui->service->addItem(name);
  546. if (!showAll) {
  547. ui->service->addItem(
  548. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  549. QVariant((int)ListOpt::ShowAll));
  550. }
  551. ui->service->insertItem(
  552. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  553. QVariant((int)ListOpt::Custom));
  554. if (!lastService.isEmpty()) {
  555. int idx = ui->service->findText(lastService);
  556. if (idx != -1)
  557. ui->service->setCurrentIndex(idx);
  558. }
  559. obs_properties_destroy(props);
  560. ui->service->blockSignals(false);
  561. }
  562. void AutoConfigStreamPage::UpdateServerList()
  563. {
  564. QString serviceName = ui->service->currentText();
  565. bool showMore = ui->service->currentData().toInt() ==
  566. (int)ListOpt::ShowAll;
  567. if (showMore) {
  568. LoadServices(true);
  569. ui->service->showPopup();
  570. return;
  571. } else {
  572. lastService = serviceName;
  573. }
  574. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  575. obs_property_t *services = obs_properties_get(props, "service");
  576. OBSData settings = obs_data_create();
  577. obs_data_release(settings);
  578. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  579. obs_property_modified(services, settings);
  580. obs_property_t *servers = obs_properties_get(props, "server");
  581. ui->server->clear();
  582. size_t servers_count = obs_property_list_item_count(servers);
  583. for (size_t i = 0; i < servers_count; i++) {
  584. const char *name = obs_property_list_item_name(servers, i);
  585. const char *server = obs_property_list_item_string(servers, i);
  586. ui->server->addItem(name, server);
  587. }
  588. obs_properties_destroy(props);
  589. }
  590. void AutoConfigStreamPage::UpdateCompleted()
  591. {
  592. if (ui->stackedWidget->currentIndex() == (int)Section::Connect ||
  593. (ui->key->text().isEmpty() && !auth)) {
  594. ready = false;
  595. } else {
  596. bool custom = IsCustomService();
  597. if (custom) {
  598. ready = !ui->customServer->text().isEmpty();
  599. } else {
  600. ready = !wiz->testRegions ||
  601. ui->regionUS->isChecked() ||
  602. ui->regionEU->isChecked() ||
  603. ui->regionAsia->isChecked() ||
  604. ui->regionOther->isChecked();
  605. }
  606. }
  607. emit completeChanged();
  608. }
  609. /* ------------------------------------------------------------------------- */
  610. AutoConfig::AutoConfig(QWidget *parent) : QWizard(parent)
  611. {
  612. EnableThreadedMessageBoxes(true);
  613. calldata_t cd = {0};
  614. calldata_set_int(&cd, "seconds", 5);
  615. proc_handler_t *ph = obs_get_proc_handler();
  616. proc_handler_call(ph, "twitch_ingests_refresh", &cd);
  617. calldata_free(&cd);
  618. OBSBasic *main = reinterpret_cast<OBSBasic *>(parent);
  619. main->EnableOutputs(false);
  620. installEventFilter(CreateShortcutFilter());
  621. std::string serviceType;
  622. GetServiceInfo(serviceType, serviceName, server, key);
  623. #if defined(_WIN32) || defined(__APPLE__)
  624. setWizardStyle(QWizard::ModernStyle);
  625. #endif
  626. streamPage = new AutoConfigStreamPage();
  627. setPage(StartPage, new AutoConfigStartPage());
  628. setPage(VideoPage, new AutoConfigVideoPage());
  629. setPage(StreamPage, streamPage);
  630. setPage(TestPage, new AutoConfigTestPage());
  631. setWindowTitle(QTStr("Basic.AutoConfig"));
  632. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  633. obs_video_info ovi;
  634. obs_get_video_info(&ovi);
  635. baseResolutionCX = ovi.base_width;
  636. baseResolutionCY = ovi.base_height;
  637. /* ----------------------------------------- */
  638. /* check to see if Twitch's "auto" available */
  639. OBSData twitchSettings = obs_data_create();
  640. obs_data_release(twitchSettings);
  641. obs_data_set_string(twitchSettings, "service", "Twitch");
  642. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  643. obs_properties_apply_settings(props, twitchSettings);
  644. obs_property_t *p = obs_properties_get(props, "server");
  645. const char *first = obs_property_list_item_string(p, 0);
  646. twitchAuto = strcmp(first, "auto") == 0;
  647. obs_properties_destroy(props);
  648. /* ----------------------------------------- */
  649. /* load service/servers */
  650. customServer = serviceType == "rtmp_custom";
  651. QComboBox *serviceList = streamPage->ui->service;
  652. if (!serviceName.empty()) {
  653. serviceList->blockSignals(true);
  654. int count = serviceList->count();
  655. bool found = false;
  656. for (int i = 0; i < count; i++) {
  657. QString name = serviceList->itemText(i);
  658. if (name == serviceName.c_str()) {
  659. serviceList->setCurrentIndex(i);
  660. found = true;
  661. break;
  662. }
  663. }
  664. if (!found) {
  665. serviceList->insertItem(0, serviceName.c_str());
  666. serviceList->setCurrentIndex(0);
  667. }
  668. serviceList->blockSignals(false);
  669. }
  670. streamPage->UpdateServerList();
  671. streamPage->UpdateKeyLink();
  672. streamPage->UpdateMoreInfoLink();
  673. streamPage->lastService.clear();
  674. if (!customServer) {
  675. QComboBox *serverList = streamPage->ui->server;
  676. int idx = serverList->findData(QString(server.c_str()));
  677. if (idx == -1)
  678. idx = 0;
  679. serverList->setCurrentIndex(idx);
  680. } else {
  681. streamPage->ui->customServer->setText(server.c_str());
  682. int idx = streamPage->ui->service->findData(
  683. QVariant((int)ListOpt::Custom));
  684. streamPage->ui->service->setCurrentIndex(idx);
  685. }
  686. if (!key.empty())
  687. streamPage->ui->key->setText(key.c_str());
  688. int bitrate =
  689. config_get_int(main->Config(), "SimpleOutput", "VBitrate");
  690. streamPage->ui->bitrate->setValue(bitrate);
  691. streamPage->ServiceChanged();
  692. TestHardwareEncoding();
  693. if (!hardwareEncodingAvailable) {
  694. delete streamPage->ui->preferHardware;
  695. streamPage->ui->preferHardware = nullptr;
  696. } else {
  697. /* Newer generations of NVENC have a high enough quality to
  698. * bitrate ratio that if NVENC is available, it makes sense to
  699. * just always prefer hardware encoding by default */
  700. bool preferHardware = nvencAvailable ||
  701. os_get_physical_cores() <= 4;
  702. streamPage->ui->preferHardware->setChecked(preferHardware);
  703. }
  704. setOptions(QWizard::WizardOptions());
  705. setButtonText(QWizard::FinishButton,
  706. QTStr("Basic.AutoConfig.ApplySettings"));
  707. setButtonText(QWizard::BackButton, QTStr("Back"));
  708. setButtonText(QWizard::NextButton, QTStr("Next"));
  709. setButtonText(QWizard::CancelButton, QTStr("Cancel"));
  710. }
  711. AutoConfig::~AutoConfig()
  712. {
  713. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  714. main->EnableOutputs(true);
  715. EnableThreadedMessageBoxes(false);
  716. }
  717. void AutoConfig::TestHardwareEncoding()
  718. {
  719. size_t idx = 0;
  720. const char *id;
  721. while (obs_enum_encoder_types(idx++, &id)) {
  722. if (strcmp(id, "ffmpeg_nvenc") == 0)
  723. hardwareEncodingAvailable = nvencAvailable = true;
  724. else if (strcmp(id, "obs_qsv11") == 0)
  725. hardwareEncodingAvailable = qsvAvailable = true;
  726. else if (strcmp(id, "amd_amf_h264") == 0)
  727. hardwareEncodingAvailable = vceAvailable = true;
  728. }
  729. }
  730. bool AutoConfig::CanTestServer(const char *server)
  731. {
  732. if (!testRegions || (regionUS && regionEU && regionAsia && regionOther))
  733. return true;
  734. if (service == Service::Twitch) {
  735. if (astrcmp_n(server, "US West:", 8) == 0 ||
  736. astrcmp_n(server, "US East:", 8) == 0 ||
  737. astrcmp_n(server, "US Central:", 11) == 0) {
  738. return regionUS;
  739. } else if (astrcmp_n(server, "EU:", 3) == 0) {
  740. return regionEU;
  741. } else if (astrcmp_n(server, "Asia:", 5) == 0) {
  742. return regionAsia;
  743. } else if (regionOther) {
  744. return true;
  745. }
  746. } else if (service == Service::Smashcast) {
  747. if (strcmp(server, "Default") == 0) {
  748. return true;
  749. } else if (astrcmp_n(server, "US-West:", 8) == 0 ||
  750. astrcmp_n(server, "US-East:", 8) == 0) {
  751. return regionUS;
  752. } else if (astrcmp_n(server, "EU-", 3) == 0) {
  753. return regionEU;
  754. } else if (astrcmp_n(server, "South Korea:", 12) == 0 ||
  755. astrcmp_n(server, "Asia:", 5) == 0 ||
  756. astrcmp_n(server, "China:", 6) == 0) {
  757. return regionAsia;
  758. } else if (regionOther) {
  759. return true;
  760. }
  761. } else {
  762. return true;
  763. }
  764. return false;
  765. }
  766. void AutoConfig::done(int result)
  767. {
  768. QWizard::done(result);
  769. if (result == QDialog::Accepted) {
  770. if (type == Type::Streaming)
  771. SaveStreamSettings();
  772. SaveSettings();
  773. }
  774. }
  775. inline const char *AutoConfig::GetEncoderId(Encoder enc)
  776. {
  777. switch (enc) {
  778. case Encoder::NVENC:
  779. return SIMPLE_ENCODER_NVENC;
  780. case Encoder::QSV:
  781. return SIMPLE_ENCODER_QSV;
  782. case Encoder::AMD:
  783. return SIMPLE_ENCODER_AMD;
  784. default:
  785. return SIMPLE_ENCODER_X264;
  786. }
  787. };
  788. void AutoConfig::SaveStreamSettings()
  789. {
  790. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  791. /* ---------------------------------- */
  792. /* save service */
  793. const char *service_id = customServer ? "rtmp_custom" : "rtmp_common";
  794. obs_service_t *oldService = main->GetService();
  795. OBSData hotkeyData = obs_hotkeys_save_service(oldService);
  796. obs_data_release(hotkeyData);
  797. OBSData settings = obs_data_create();
  798. obs_data_release(settings);
  799. if (!customServer)
  800. obs_data_set_string(settings, "service", serviceName.c_str());
  801. obs_data_set_string(settings, "server", server.c_str());
  802. obs_data_set_string(settings, "key", key.c_str());
  803. OBSService newService = obs_service_create(
  804. service_id, "default_service", settings, hotkeyData);
  805. obs_service_release(newService);
  806. if (!newService)
  807. return;
  808. main->SetService(newService);
  809. main->SaveService();
  810. main->auth = streamPage->auth;
  811. if (!!main->auth)
  812. main->auth->LoadUI();
  813. /* ---------------------------------- */
  814. /* save stream settings */
  815. config_set_int(main->Config(), "SimpleOutput", "VBitrate",
  816. idealBitrate);
  817. config_set_string(main->Config(), "SimpleOutput", "StreamEncoder",
  818. GetEncoderId(streamingEncoder));
  819. config_remove_value(main->Config(), "SimpleOutput", "UseAdvanced");
  820. }
  821. void AutoConfig::SaveSettings()
  822. {
  823. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  824. if (recordingEncoder != Encoder::Stream)
  825. config_set_string(main->Config(), "SimpleOutput", "RecEncoder",
  826. GetEncoderId(recordingEncoder));
  827. const char *quality = recordingQuality == Quality::High ? "Small"
  828. : "Stream";
  829. config_set_string(main->Config(), "Output", "Mode", "Simple");
  830. config_set_string(main->Config(), "SimpleOutput", "RecQuality",
  831. quality);
  832. config_set_int(main->Config(), "Video", "BaseCX", baseResolutionCX);
  833. config_set_int(main->Config(), "Video", "BaseCY", baseResolutionCY);
  834. config_set_int(main->Config(), "Video", "OutputCX", idealResolutionCX);
  835. config_set_int(main->Config(), "Video", "OutputCY", idealResolutionCY);
  836. if (fpsType != FPSType::UseCurrent) {
  837. config_set_uint(main->Config(), "Video", "FPSType", 0);
  838. config_set_string(main->Config(), "Video", "FPSCommon",
  839. std::to_string(idealFPSNum).c_str());
  840. }
  841. main->ResetVideo();
  842. main->ResetOutputs();
  843. config_save_safe(main->Config(), "tmp", nullptr);
  844. }