window-basic-auto-config.cpp 27 KB

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