window-basic-auto-config.cpp 28 KB

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