window-basic-auto-config.cpp 29 KB

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