window-basic-auto-config.cpp 29 KB

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