window-basic-settings-stream.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. #include <QMessageBox>
  2. #include <QUrl>
  3. #include "window-basic-settings.hpp"
  4. #include "obs-frontend-api.h"
  5. #include "obs-app.hpp"
  6. #include "window-basic-main.hpp"
  7. #include "qt-wrappers.hpp"
  8. #include "url-push-button.hpp"
  9. #ifdef BROWSER_AVAILABLE
  10. #include <browser-panel.hpp>
  11. #endif
  12. #include "auth-oauth.hpp"
  13. #include "ui-config.h"
  14. #if YOUTUBE_ENABLED
  15. #include "youtube-api-wrappers.hpp"
  16. #endif
  17. struct QCef;
  18. struct QCefCookieManager;
  19. extern QCef *cef;
  20. extern QCefCookieManager *panel_cookies;
  21. enum class ListOpt : int {
  22. ShowAll = 1,
  23. Custom,
  24. WHIP,
  25. };
  26. enum class Section : int {
  27. Connect,
  28. StreamKey,
  29. };
  30. inline bool OBSBasicSettings::IsCustomService() const
  31. {
  32. return ui->service->currentData().toInt() == (int)ListOpt::Custom;
  33. }
  34. inline bool OBSBasicSettings::IsWHIP() const
  35. {
  36. return ui->service->currentData().toInt() == (int)ListOpt::WHIP;
  37. }
  38. void OBSBasicSettings::InitStreamPage()
  39. {
  40. ui->connectAccount2->setVisible(false);
  41. ui->disconnectAccount->setVisible(false);
  42. ui->bandwidthTestEnable->setVisible(false);
  43. ui->twitchAddonDropdown->setVisible(false);
  44. ui->twitchAddonLabel->setVisible(false);
  45. ui->connectedAccountLabel->setVisible(false);
  46. ui->connectedAccountText->setVisible(false);
  47. int vertSpacing = ui->topStreamLayout->verticalSpacing();
  48. QMargins m = ui->topStreamLayout->contentsMargins();
  49. m.setBottom(vertSpacing / 2);
  50. ui->topStreamLayout->setContentsMargins(m);
  51. m = ui->loginPageLayout->contentsMargins();
  52. m.setTop(vertSpacing / 2);
  53. ui->loginPageLayout->setContentsMargins(m);
  54. m = ui->streamkeyPageLayout->contentsMargins();
  55. m.setTop(vertSpacing / 2);
  56. ui->streamkeyPageLayout->setContentsMargins(m);
  57. LoadServices(false);
  58. ui->twitchAddonDropdown->addItem(
  59. QTStr("Basic.Settings.Stream.TTVAddon.None"));
  60. ui->twitchAddonDropdown->addItem(
  61. QTStr("Basic.Settings.Stream.TTVAddon.BTTV"));
  62. ui->twitchAddonDropdown->addItem(
  63. QTStr("Basic.Settings.Stream.TTVAddon.FFZ"));
  64. ui->twitchAddonDropdown->addItem(
  65. QTStr("Basic.Settings.Stream.TTVAddon.Both"));
  66. connect(ui->ignoreRecommended, SIGNAL(clicked(bool)), this,
  67. SLOT(DisplayEnforceWarning(bool)));
  68. connect(ui->ignoreRecommended, SIGNAL(toggled(bool)), this,
  69. SLOT(UpdateResFPSLimits()));
  70. }
  71. void OBSBasicSettings::LoadStream1Settings()
  72. {
  73. bool ignoreRecommended =
  74. config_get_bool(main->Config(), "Stream1", "IgnoreRecommended");
  75. obs_service_t *service_obj = main->GetService();
  76. const char *type = obs_service_get_type(service_obj);
  77. bool is_rtmp_custom = (strcmp(type, "rtmp_custom") == 0);
  78. bool is_rtmp_common = (strcmp(type, "rtmp_common") == 0);
  79. bool is_whip = (strcmp(type, "whip_custom") == 0);
  80. loading = true;
  81. OBSDataAutoRelease settings = obs_service_get_settings(service_obj);
  82. const char *service = obs_data_get_string(settings, "service");
  83. const char *server = obs_data_get_string(settings, "server");
  84. const char *key = obs_data_get_string(settings, "key");
  85. protocol = QT_UTF8(obs_service_get_protocol(service_obj));
  86. const char *bearer_token =
  87. obs_data_get_string(settings, "bearer_token");
  88. if (is_rtmp_custom || is_whip)
  89. ui->customServer->setText(server);
  90. if (is_rtmp_custom) {
  91. ui->service->setCurrentIndex(0);
  92. lastServiceIdx = 0;
  93. lastCustomServer = ui->customServer->text();
  94. bool use_auth = obs_data_get_bool(settings, "use_auth");
  95. const char *username =
  96. obs_data_get_string(settings, "username");
  97. const char *password =
  98. obs_data_get_string(settings, "password");
  99. ui->authUsername->setText(QT_UTF8(username));
  100. ui->authPw->setText(QT_UTF8(password));
  101. ui->useAuth->setChecked(use_auth);
  102. /* add tooltips for stream key, user, password fields */
  103. QString file = !App()->IsThemeDark()
  104. ? ":/res/images/help.svg"
  105. : ":/res/images/help_light.svg";
  106. QString lStr = "<html>%1 <img src='%2' style=' \
  107. vertical-align: bottom; \
  108. ' /></html>";
  109. ui->streamKeyLabel->setText(
  110. lStr.arg(ui->streamKeyLabel->text(), file));
  111. ui->streamKeyLabel->setToolTip(
  112. QTStr("Basic.AutoConfig.StreamPage.StreamKey.ToolTip"));
  113. ui->authUsernameLabel->setText(
  114. lStr.arg(ui->authUsernameLabel->text(), file));
  115. ui->authUsernameLabel->setToolTip(
  116. QTStr("Basic.Settings.Stream.Custom.Username.ToolTip"));
  117. ui->authPwLabel->setText(
  118. lStr.arg(ui->authPwLabel->text(), file));
  119. ui->authPwLabel->setToolTip(
  120. QTStr("Basic.Settings.Stream.Custom.Password.ToolTip"));
  121. } else {
  122. int idx = ui->service->findText(service);
  123. if (idx == -1) {
  124. if (service && *service)
  125. ui->service->insertItem(1, service);
  126. idx = 1;
  127. }
  128. ui->service->setCurrentIndex(idx);
  129. lastServiceIdx = idx;
  130. bool bw_test = obs_data_get_bool(settings, "bwtest");
  131. ui->bandwidthTestEnable->setChecked(bw_test);
  132. idx = config_get_int(main->Config(), "Twitch", "AddonChoice");
  133. ui->twitchAddonDropdown->setCurrentIndex(idx);
  134. }
  135. UpdateServerList();
  136. if (is_rtmp_common) {
  137. int idx = ui->server->findData(server);
  138. if (idx == -1) {
  139. if (server && *server)
  140. ui->server->insertItem(0, server, server);
  141. idx = 0;
  142. }
  143. ui->server->setCurrentIndex(idx);
  144. }
  145. if (is_whip)
  146. ui->key->setText(bearer_token);
  147. else
  148. ui->key->setText(key);
  149. lastService.clear();
  150. ServiceChanged();
  151. UpdateKeyLink();
  152. UpdateMoreInfoLink();
  153. UpdateVodTrackSetting();
  154. UpdateServiceRecommendations();
  155. bool streamActive = obs_frontend_streaming_active();
  156. ui->streamPage->setEnabled(!streamActive);
  157. ui->ignoreRecommended->setChecked(ignoreRecommended);
  158. loading = false;
  159. QMetaObject::invokeMethod(this, "UpdateResFPSLimits",
  160. Qt::QueuedConnection);
  161. }
  162. void OBSBasicSettings::SaveStream1Settings()
  163. {
  164. bool customServer = IsCustomService();
  165. bool whip = IsWHIP();
  166. const char *service_id = "rtmp_common";
  167. if (customServer) {
  168. service_id = "rtmp_custom";
  169. } else if (whip) {
  170. service_id = "whip_custom";
  171. }
  172. obs_service_t *oldService = main->GetService();
  173. OBSDataAutoRelease hotkeyData = obs_hotkeys_save_service(oldService);
  174. OBSDataAutoRelease settings = obs_data_create();
  175. if (!customServer && !whip) {
  176. obs_data_set_string(settings, "service",
  177. QT_TO_UTF8(ui->service->currentText()));
  178. obs_data_set_string(settings, "protocol", QT_TO_UTF8(protocol));
  179. obs_data_set_string(
  180. settings, "server",
  181. QT_TO_UTF8(ui->server->currentData().toString()));
  182. } else {
  183. obs_data_set_string(
  184. settings, "server",
  185. QT_TO_UTF8(ui->customServer->text().trimmed()));
  186. obs_data_set_bool(settings, "use_auth",
  187. ui->useAuth->isChecked());
  188. if (ui->useAuth->isChecked()) {
  189. obs_data_set_string(
  190. settings, "username",
  191. QT_TO_UTF8(ui->authUsername->text()));
  192. obs_data_set_string(settings, "password",
  193. QT_TO_UTF8(ui->authPw->text()));
  194. }
  195. }
  196. if (!!auth && strcmp(auth->service(), "Twitch") == 0) {
  197. bool choiceExists = config_has_user_value(
  198. main->Config(), "Twitch", "AddonChoice");
  199. int currentChoice =
  200. config_get_int(main->Config(), "Twitch", "AddonChoice");
  201. int newChoice = ui->twitchAddonDropdown->currentIndex();
  202. config_set_int(main->Config(), "Twitch", "AddonChoice",
  203. newChoice);
  204. if (choiceExists && currentChoice != newChoice)
  205. forceAuthReload = true;
  206. obs_data_set_bool(settings, "bwtest",
  207. ui->bandwidthTestEnable->isChecked());
  208. } else {
  209. obs_data_set_bool(settings, "bwtest", false);
  210. }
  211. if (whip)
  212. obs_data_set_string(settings, "bearer_token",
  213. QT_TO_UTF8(ui->key->text()));
  214. else
  215. obs_data_set_string(settings, "key",
  216. QT_TO_UTF8(ui->key->text()));
  217. OBSServiceAutoRelease newService = obs_service_create(
  218. service_id, "default_service", settings, hotkeyData);
  219. if (!newService)
  220. return;
  221. main->SetService(newService);
  222. main->SaveService();
  223. main->auth = auth;
  224. if (!!main->auth) {
  225. main->auth->LoadUI();
  226. main->SetBroadcastFlowEnabled(main->auth->broadcastFlow());
  227. } else {
  228. main->SetBroadcastFlowEnabled(false);
  229. }
  230. SaveCheckBox(ui->ignoreRecommended, "Stream1", "IgnoreRecommended");
  231. }
  232. void OBSBasicSettings::UpdateMoreInfoLink()
  233. {
  234. if (IsCustomService() || IsWHIP()) {
  235. ui->moreInfoButton->hide();
  236. return;
  237. }
  238. QString serviceName = ui->service->currentText();
  239. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  240. obs_property_t *services = obs_properties_get(props, "service");
  241. OBSDataAutoRelease settings = obs_data_create();
  242. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  243. obs_property_modified(services, settings);
  244. const char *more_info_link =
  245. obs_data_get_string(settings, "more_info_link");
  246. if (!more_info_link || (*more_info_link == '\0')) {
  247. ui->moreInfoButton->hide();
  248. } else {
  249. ui->moreInfoButton->setTargetUrl(QUrl(more_info_link));
  250. ui->moreInfoButton->show();
  251. }
  252. obs_properties_destroy(props);
  253. }
  254. void OBSBasicSettings::UpdateKeyLink()
  255. {
  256. QString serviceName = ui->service->currentText();
  257. QString customServer = ui->customServer->text().trimmed();
  258. QString streamKeyLink;
  259. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  260. obs_property_t *services = obs_properties_get(props, "service");
  261. OBSDataAutoRelease settings = obs_data_create();
  262. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  263. obs_property_modified(services, settings);
  264. streamKeyLink = obs_data_get_string(settings, "stream_key_link");
  265. if (customServer.contains("fbcdn.net") && IsCustomService()) {
  266. streamKeyLink =
  267. "https://www.facebook.com/live/producer?ref=OBS";
  268. }
  269. if (serviceName == "Dacast") {
  270. ui->streamKeyLabel->setText(
  271. QTStr("Basic.AutoConfig.StreamPage.EncoderKey"));
  272. } else if (IsWHIP()) {
  273. ui->streamKeyLabel->setText(
  274. QTStr("Basic.AutoConfig.StreamPage.BearerToken"));
  275. } else if (!IsCustomService()) {
  276. ui->streamKeyLabel->setText(
  277. QTStr("Basic.AutoConfig.StreamPage.StreamKey"));
  278. }
  279. if (QString(streamKeyLink).isNull() ||
  280. QString(streamKeyLink).isEmpty()) {
  281. ui->getStreamKeyButton->hide();
  282. } else {
  283. ui->getStreamKeyButton->setTargetUrl(QUrl(streamKeyLink));
  284. ui->getStreamKeyButton->show();
  285. }
  286. obs_properties_destroy(props);
  287. }
  288. void OBSBasicSettings::LoadServices(bool showAll)
  289. {
  290. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  291. OBSDataAutoRelease settings = obs_data_create();
  292. obs_data_set_bool(settings, "show_all", showAll);
  293. obs_property_t *prop = obs_properties_get(props, "show_all");
  294. obs_property_modified(prop, settings);
  295. ui->service->blockSignals(true);
  296. ui->service->clear();
  297. QStringList names;
  298. obs_property_t *services = obs_properties_get(props, "service");
  299. size_t services_count = obs_property_list_item_count(services);
  300. for (size_t i = 0; i < services_count; i++) {
  301. const char *name = obs_property_list_item_string(services, i);
  302. names.push_back(name);
  303. }
  304. if (showAll)
  305. names.sort(Qt::CaseInsensitive);
  306. for (QString &name : names)
  307. ui->service->addItem(name);
  308. ui->service->insertItem(0, QTStr("WHIP"), QVariant((int)ListOpt::WHIP));
  309. if (!showAll) {
  310. ui->service->addItem(
  311. QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
  312. QVariant((int)ListOpt::ShowAll));
  313. }
  314. ui->service->insertItem(
  315. 0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"),
  316. QVariant((int)ListOpt::Custom));
  317. if (!lastService.isEmpty()) {
  318. int idx = ui->service->findText(lastService);
  319. if (idx != -1)
  320. ui->service->setCurrentIndex(idx);
  321. }
  322. obs_properties_destroy(props);
  323. ui->service->blockSignals(false);
  324. }
  325. static inline bool is_auth_service(const std::string &service)
  326. {
  327. return Auth::AuthType(service) != Auth::Type::None;
  328. }
  329. static inline bool is_external_oauth(const std::string &service)
  330. {
  331. return Auth::External(service);
  332. }
  333. static void reset_service_ui_fields(Ui::OBSBasicSettings *ui,
  334. std::string &service, bool loading)
  335. {
  336. bool external_oauth = is_external_oauth(service);
  337. if (external_oauth) {
  338. ui->streamKeyWidget->setVisible(false);
  339. ui->streamKeyLabel->setVisible(false);
  340. ui->connectAccount2->setVisible(true);
  341. ui->useStreamKeyAdv->setVisible(true);
  342. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  343. } else if (cef) {
  344. QString key = ui->key->text();
  345. bool can_auth = is_auth_service(service);
  346. int page = can_auth && (!loading || key.isEmpty())
  347. ? (int)Section::Connect
  348. : (int)Section::StreamKey;
  349. ui->streamStackWidget->setCurrentIndex(page);
  350. ui->streamKeyWidget->setVisible(true);
  351. ui->streamKeyLabel->setVisible(true);
  352. ui->connectAccount2->setVisible(can_auth);
  353. ui->useStreamKeyAdv->setVisible(false);
  354. } else {
  355. ui->connectAccount2->setVisible(false);
  356. ui->useStreamKeyAdv->setVisible(false);
  357. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  358. }
  359. ui->connectedAccountLabel->setVisible(false);
  360. ui->connectedAccountText->setVisible(false);
  361. ui->disconnectAccount->setVisible(false);
  362. }
  363. #if YOUTUBE_ENABLED
  364. static void get_yt_ch_title(Ui::OBSBasicSettings *ui)
  365. {
  366. const char *name = config_get_string(OBSBasic::Get()->Config(),
  367. "YouTube", "ChannelName");
  368. if (name) {
  369. ui->connectedAccountText->setText(name);
  370. } else {
  371. // if we still not changed the service page
  372. if (IsYouTubeService(QT_TO_UTF8(ui->service->currentText()))) {
  373. ui->connectedAccountText->setText(
  374. QTStr("Auth.LoadingChannel.Error"));
  375. }
  376. }
  377. }
  378. #endif
  379. void OBSBasicSettings::UseStreamKeyAdvClicked()
  380. {
  381. ui->streamKeyWidget->setVisible(true);
  382. }
  383. void OBSBasicSettings::on_service_currentIndexChanged(int idx)
  384. {
  385. if (ui->service->currentData().toInt() == (int)ListOpt::ShowAll) {
  386. LoadServices(true);
  387. ui->service->showPopup();
  388. return;
  389. }
  390. ServiceChanged();
  391. UpdateMoreInfoLink();
  392. UpdateServerList();
  393. UpdateKeyLink();
  394. UpdateServiceRecommendations();
  395. UpdateVodTrackSetting();
  396. protocol = FindProtocol();
  397. UpdateAdvNetworkGroup();
  398. if (ServiceSupportsCodecCheck() && UpdateResFPSLimits()) {
  399. lastServiceIdx = idx;
  400. if (idx == 0)
  401. lastCustomServer = ui->customServer->text();
  402. }
  403. }
  404. void OBSBasicSettings::on_customServer_textChanged(const QString &)
  405. {
  406. UpdateKeyLink();
  407. protocol = FindProtocol();
  408. UpdateAdvNetworkGroup();
  409. if (ServiceSupportsCodecCheck())
  410. lastCustomServer = ui->customServer->text();
  411. }
  412. void OBSBasicSettings::ServiceChanged()
  413. {
  414. std::string service = QT_TO_UTF8(ui->service->currentText());
  415. bool custom = IsCustomService();
  416. bool whip = IsWHIP();
  417. ui->disconnectAccount->setVisible(false);
  418. ui->bandwidthTestEnable->setVisible(false);
  419. ui->twitchAddonDropdown->setVisible(false);
  420. ui->twitchAddonLabel->setVisible(false);
  421. if (lastService != service.c_str()) {
  422. reset_service_ui_fields(ui.get(), service, loading);
  423. }
  424. ui->useAuth->setVisible(custom);
  425. ui->authUsernameLabel->setVisible(custom);
  426. ui->authUsername->setVisible(custom);
  427. ui->authPwLabel->setVisible(custom);
  428. ui->authPwWidget->setVisible(custom);
  429. if (custom || whip) {
  430. ui->streamkeyPageLayout->insertRow(1, ui->serverLabel,
  431. ui->serverStackedWidget);
  432. ui->serverStackedWidget->setCurrentIndex(1);
  433. ui->serverStackedWidget->setVisible(true);
  434. ui->serverLabel->setVisible(true);
  435. on_useAuth_toggled();
  436. } else {
  437. ui->serverStackedWidget->setCurrentIndex(0);
  438. }
  439. auth.reset();
  440. if (!main->auth) {
  441. return;
  442. }
  443. auto system_auth_service = main->auth->service();
  444. bool service_check = service.find(system_auth_service) !=
  445. std::string::npos;
  446. #if YOUTUBE_ENABLED
  447. service_check = service_check ? service_check
  448. : IsYouTubeService(system_auth_service) &&
  449. IsYouTubeService(service);
  450. #endif
  451. if (service_check) {
  452. auth = main->auth;
  453. OnAuthConnected();
  454. }
  455. }
  456. QString OBSBasicSettings::FindProtocol()
  457. {
  458. if (IsCustomService()) {
  459. if (ui->customServer->text().isEmpty())
  460. return QString("RTMP");
  461. QString server = ui->customServer->text();
  462. if (obs_is_output_protocol_registered("RTMPS") &&
  463. server.startsWith("rtmps://"))
  464. return QString("RTMPS");
  465. if (server.startsWith("ftl://"))
  466. return QString("FTL");
  467. if (server.startsWith("srt://"))
  468. return QString("SRT");
  469. if (server.startsWith("rist://"))
  470. return QString("RIST");
  471. } else {
  472. obs_properties_t *props =
  473. obs_get_service_properties("rtmp_common");
  474. obs_property_t *services = obs_properties_get(props, "service");
  475. OBSDataAutoRelease settings = obs_data_create();
  476. obs_data_set_string(settings, "service",
  477. QT_TO_UTF8(ui->service->currentText()));
  478. obs_property_modified(services, settings);
  479. obs_properties_destroy(props);
  480. return QT_UTF8(obs_data_get_string(settings, "protocol"));
  481. }
  482. return QString("RTMP");
  483. }
  484. void OBSBasicSettings::UpdateServerList()
  485. {
  486. QString serviceName = ui->service->currentText();
  487. lastService = serviceName;
  488. obs_properties_t *props = obs_get_service_properties("rtmp_common");
  489. obs_property_t *services = obs_properties_get(props, "service");
  490. OBSDataAutoRelease settings = obs_data_create();
  491. obs_data_set_string(settings, "service", QT_TO_UTF8(serviceName));
  492. obs_property_modified(services, settings);
  493. obs_property_t *servers = obs_properties_get(props, "server");
  494. ui->server->clear();
  495. size_t servers_count = obs_property_list_item_count(servers);
  496. for (size_t i = 0; i < servers_count; i++) {
  497. const char *name = obs_property_list_item_name(servers, i);
  498. const char *server = obs_property_list_item_string(servers, i);
  499. ui->server->addItem(name, server);
  500. }
  501. obs_properties_destroy(props);
  502. }
  503. void OBSBasicSettings::on_show_clicked()
  504. {
  505. if (ui->key->echoMode() == QLineEdit::Password) {
  506. ui->key->setEchoMode(QLineEdit::Normal);
  507. ui->show->setText(QTStr("Hide"));
  508. } else {
  509. ui->key->setEchoMode(QLineEdit::Password);
  510. ui->show->setText(QTStr("Show"));
  511. }
  512. }
  513. void OBSBasicSettings::on_authPwShow_clicked()
  514. {
  515. if (ui->authPw->echoMode() == QLineEdit::Password) {
  516. ui->authPw->setEchoMode(QLineEdit::Normal);
  517. ui->authPwShow->setText(QTStr("Hide"));
  518. } else {
  519. ui->authPw->setEchoMode(QLineEdit::Password);
  520. ui->authPwShow->setText(QTStr("Show"));
  521. }
  522. }
  523. OBSService OBSBasicSettings::SpawnTempService()
  524. {
  525. bool custom = IsCustomService();
  526. bool whip = IsWHIP();
  527. const char *service_id = "rtmp_common";
  528. if (custom) {
  529. service_id = "rtmp_custom";
  530. } else if (whip) {
  531. service_id = "whip_custom";
  532. }
  533. OBSDataAutoRelease settings = obs_data_create();
  534. if (!custom && !whip) {
  535. obs_data_set_string(settings, "service",
  536. QT_TO_UTF8(ui->service->currentText()));
  537. obs_data_set_string(
  538. settings, "server",
  539. QT_TO_UTF8(ui->server->currentData().toString()));
  540. } else {
  541. obs_data_set_string(
  542. settings, "server",
  543. QT_TO_UTF8(ui->customServer->text().trimmed()));
  544. }
  545. if (whip)
  546. obs_data_set_string(settings, "bearer_token",
  547. QT_TO_UTF8(ui->key->text()));
  548. else
  549. obs_data_set_string(settings, "key",
  550. QT_TO_UTF8(ui->key->text()));
  551. OBSServiceAutoRelease newService = obs_service_create(
  552. service_id, "temp_service", settings, nullptr);
  553. return newService.Get();
  554. }
  555. void OBSBasicSettings::OnOAuthStreamKeyConnected()
  556. {
  557. OAuthStreamKey *a = reinterpret_cast<OAuthStreamKey *>(auth.get());
  558. if (a) {
  559. bool validKey = !a->key().empty();
  560. if (validKey)
  561. ui->key->setText(QT_UTF8(a->key().c_str()));
  562. ui->streamKeyWidget->setVisible(false);
  563. ui->streamKeyLabel->setVisible(false);
  564. ui->connectAccount2->setVisible(false);
  565. ui->disconnectAccount->setVisible(true);
  566. ui->useStreamKeyAdv->setVisible(false);
  567. ui->connectedAccountLabel->setVisible(false);
  568. ui->connectedAccountText->setVisible(false);
  569. if (strcmp(a->service(), "Twitch") == 0) {
  570. ui->bandwidthTestEnable->setVisible(true);
  571. ui->twitchAddonLabel->setVisible(true);
  572. ui->twitchAddonDropdown->setVisible(true);
  573. } else {
  574. ui->bandwidthTestEnable->setChecked(false);
  575. }
  576. #if YOUTUBE_ENABLED
  577. if (IsYouTubeService(a->service())) {
  578. ui->key->clear();
  579. ui->connectedAccountLabel->setVisible(true);
  580. ui->connectedAccountText->setVisible(true);
  581. ui->connectedAccountText->setText(
  582. QTStr("Auth.LoadingChannel.Title"));
  583. get_yt_ch_title(ui.get());
  584. }
  585. #endif
  586. }
  587. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  588. }
  589. void OBSBasicSettings::OnAuthConnected()
  590. {
  591. std::string service = QT_TO_UTF8(ui->service->currentText());
  592. Auth::Type type = Auth::AuthType(service);
  593. if (type == Auth::Type::OAuth_StreamKey ||
  594. type == Auth::Type::OAuth_LinkedAccount) {
  595. OnOAuthStreamKeyConnected();
  596. }
  597. if (!loading) {
  598. stream1Changed = true;
  599. EnableApplyButton(true);
  600. }
  601. }
  602. void OBSBasicSettings::on_connectAccount_clicked()
  603. {
  604. std::string service = QT_TO_UTF8(ui->service->currentText());
  605. OAuth::DeleteCookies(service);
  606. auth = OAuthStreamKey::Login(this, service);
  607. if (!!auth) {
  608. OnAuthConnected();
  609. ui->useStreamKeyAdv->setVisible(false);
  610. }
  611. }
  612. #define DISCONNECT_COMFIRM_TITLE \
  613. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Title"
  614. #define DISCONNECT_COMFIRM_TEXT \
  615. "Basic.AutoConfig.StreamPage.DisconnectAccount.Confirm.Text"
  616. void OBSBasicSettings::on_disconnectAccount_clicked()
  617. {
  618. QMessageBox::StandardButton button;
  619. button = OBSMessageBox::question(this, QTStr(DISCONNECT_COMFIRM_TITLE),
  620. QTStr(DISCONNECT_COMFIRM_TEXT));
  621. if (button == QMessageBox::No) {
  622. return;
  623. }
  624. main->auth.reset();
  625. auth.reset();
  626. main->SetBroadcastFlowEnabled(false);
  627. std::string service = QT_TO_UTF8(ui->service->currentText());
  628. #ifdef BROWSER_AVAILABLE
  629. OAuth::DeleteCookies(service);
  630. #endif
  631. ui->bandwidthTestEnable->setChecked(false);
  632. reset_service_ui_fields(ui.get(), service, loading);
  633. ui->bandwidthTestEnable->setVisible(false);
  634. ui->twitchAddonDropdown->setVisible(false);
  635. ui->twitchAddonLabel->setVisible(false);
  636. ui->key->setText("");
  637. ui->connectedAccountLabel->setVisible(false);
  638. ui->connectedAccountText->setVisible(false);
  639. }
  640. void OBSBasicSettings::on_useStreamKey_clicked()
  641. {
  642. ui->streamStackWidget->setCurrentIndex((int)Section::StreamKey);
  643. }
  644. void OBSBasicSettings::on_useAuth_toggled()
  645. {
  646. if (!IsCustomService())
  647. return;
  648. bool use_auth = ui->useAuth->isChecked();
  649. ui->authUsernameLabel->setVisible(use_auth);
  650. ui->authUsername->setVisible(use_auth);
  651. ui->authPwLabel->setVisible(use_auth);
  652. ui->authPwWidget->setVisible(use_auth);
  653. }
  654. void OBSBasicSettings::UpdateVodTrackSetting()
  655. {
  656. bool enableForCustomServer = config_get_bool(
  657. GetGlobalConfig(), "General", "EnableCustomServerVodTrack");
  658. bool enableVodTrack = ui->service->currentText() == "Twitch";
  659. bool wasEnabled = !!vodTrackCheckbox;
  660. if (enableForCustomServer && IsCustomService())
  661. enableVodTrack = true;
  662. if (enableVodTrack == wasEnabled)
  663. return;
  664. if (!enableVodTrack) {
  665. delete vodTrackCheckbox;
  666. delete vodTrackContainer;
  667. delete simpleVodTrack;
  668. return;
  669. }
  670. /* -------------------------------------- */
  671. /* simple output mode vod track widgets */
  672. bool simpleAdv = ui->simpleOutAdvanced->isChecked();
  673. bool vodTrackEnabled = config_get_bool(main->Config(), "SimpleOutput",
  674. "VodTrackEnabled");
  675. simpleVodTrack = new QCheckBox(this);
  676. simpleVodTrack->setText(
  677. QTStr("Basic.Settings.Output.Simple.TwitchVodTrack"));
  678. simpleVodTrack->setVisible(simpleAdv);
  679. simpleVodTrack->setChecked(vodTrackEnabled);
  680. int pos;
  681. ui->simpleStreamingLayout->getWidgetPosition(ui->simpleOutAdvanced,
  682. &pos, nullptr);
  683. ui->simpleStreamingLayout->insertRow(pos + 1, nullptr, simpleVodTrack);
  684. HookWidget(simpleVodTrack, SIGNAL(clicked(bool)),
  685. SLOT(OutputsChanged()));
  686. connect(ui->simpleOutAdvanced, SIGNAL(toggled(bool)),
  687. simpleVodTrack.data(), SLOT(setVisible(bool)));
  688. /* -------------------------------------- */
  689. /* advanced output mode vod track widgets */
  690. vodTrackCheckbox = new QCheckBox(this);
  691. vodTrackCheckbox->setText(
  692. QTStr("Basic.Settings.Output.Adv.TwitchVodTrack"));
  693. vodTrackCheckbox->setLayoutDirection(Qt::RightToLeft);
  694. vodTrackContainer = new QWidget(this);
  695. QHBoxLayout *vodTrackLayout = new QHBoxLayout();
  696. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  697. vodTrack[i] = new QRadioButton(QString::number(i + 1));
  698. vodTrackLayout->addWidget(vodTrack[i]);
  699. HookWidget(vodTrack[i], SIGNAL(clicked(bool)),
  700. SLOT(OutputsChanged()));
  701. }
  702. HookWidget(vodTrackCheckbox, SIGNAL(clicked(bool)),
  703. SLOT(OutputsChanged()));
  704. vodTrackLayout->addStretch();
  705. vodTrackLayout->setContentsMargins(0, 0, 0, 0);
  706. vodTrackContainer->setLayout(vodTrackLayout);
  707. ui->advOutTopLayout->insertRow(2, vodTrackCheckbox, vodTrackContainer);
  708. vodTrackEnabled =
  709. config_get_bool(main->Config(), "AdvOut", "VodTrackEnabled");
  710. vodTrackCheckbox->setChecked(vodTrackEnabled);
  711. vodTrackContainer->setEnabled(vodTrackEnabled);
  712. connect(vodTrackCheckbox, SIGNAL(clicked(bool)), vodTrackContainer,
  713. SLOT(setEnabled(bool)));
  714. int trackIndex =
  715. config_get_int(main->Config(), "AdvOut", "VodTrackIndex");
  716. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  717. vodTrack[i]->setChecked((i + 1) == trackIndex);
  718. }
  719. }
  720. OBSService OBSBasicSettings::GetStream1Service()
  721. {
  722. return stream1Changed ? SpawnTempService()
  723. : OBSService(main->GetService());
  724. }
  725. void OBSBasicSettings::UpdateServiceRecommendations()
  726. {
  727. bool customServer = IsCustomService();
  728. ui->ignoreRecommended->setVisible(!customServer);
  729. ui->enforceSettingsLabel->setVisible(!customServer);
  730. OBSService service = GetStream1Service();
  731. int vbitrate, abitrate;
  732. BPtr<obs_service_resolution> res_list;
  733. size_t res_count;
  734. int fps;
  735. obs_service_get_max_bitrate(service, &vbitrate, &abitrate);
  736. obs_service_get_supported_resolutions(service, &res_list, &res_count);
  737. obs_service_get_max_fps(service, &fps);
  738. QString text;
  739. #define ENFORCE_TEXT(x) QTStr("Basic.Settings.Stream.Recommended." x)
  740. if (vbitrate)
  741. text += ENFORCE_TEXT("MaxVideoBitrate")
  742. .arg(QString::number(vbitrate));
  743. if (abitrate) {
  744. if (!text.isEmpty())
  745. text += "<br>";
  746. text += ENFORCE_TEXT("MaxAudioBitrate")
  747. .arg(QString::number(abitrate));
  748. }
  749. if (res_count) {
  750. if (!text.isEmpty())
  751. text += "<br>";
  752. obs_service_resolution best_res = {};
  753. int best_res_pixels = 0;
  754. for (size_t i = 0; i < res_count; i++) {
  755. obs_service_resolution res = res_list[i];
  756. int res_pixels = res.cx + res.cy;
  757. if (res_pixels > best_res_pixels) {
  758. best_res = res;
  759. best_res_pixels = res_pixels;
  760. }
  761. }
  762. QString res_str =
  763. QString("%1x%2").arg(QString::number(best_res.cx),
  764. QString::number(best_res.cy));
  765. text += ENFORCE_TEXT("MaxResolution").arg(res_str);
  766. }
  767. if (fps) {
  768. if (!text.isEmpty())
  769. text += "<br>";
  770. text += ENFORCE_TEXT("MaxFPS").arg(QString::number(fps));
  771. }
  772. #undef ENFORCE_TEXT
  773. #if YOUTUBE_ENABLED
  774. if (IsYouTubeService(QT_TO_UTF8(ui->service->currentText()))) {
  775. if (!text.isEmpty())
  776. text += "<br><br>";
  777. text += "<a href=\"https://www.youtube.com/t/terms\">"
  778. "YouTube Terms of Service</a><br>"
  779. "<a href=\"http://www.google.com/policies/privacy\">"
  780. "Google Privacy Policy</a><br>"
  781. "<a href=\"https://security.google.com/settings/security/permissions\">"
  782. "Google Third-Party Permissions</a>";
  783. }
  784. #endif
  785. ui->enforceSettingsLabel->setText(text);
  786. }
  787. void OBSBasicSettings::DisplayEnforceWarning(bool checked)
  788. {
  789. if (IsCustomService())
  790. return;
  791. if (!checked) {
  792. SimpleRecordingEncoderChanged();
  793. return;
  794. }
  795. QMessageBox::StandardButton button;
  796. #define ENFORCE_WARNING(x) \
  797. QTStr("Basic.Settings.Stream.IgnoreRecommended.Warn." x)
  798. button = OBSMessageBox::question(this, ENFORCE_WARNING("Title"),
  799. ENFORCE_WARNING("Text"));
  800. #undef ENFORCE_WARNING
  801. if (button == QMessageBox::No) {
  802. QMetaObject::invokeMethod(ui->ignoreRecommended, "setChecked",
  803. Qt::QueuedConnection,
  804. Q_ARG(bool, false));
  805. return;
  806. }
  807. SimpleRecordingEncoderChanged();
  808. }
  809. bool OBSBasicSettings::ResFPSValid(obs_service_resolution *res_list,
  810. size_t res_count, int max_fps)
  811. {
  812. if (!res_count && !max_fps)
  813. return true;
  814. if (res_count) {
  815. QString res = ui->outputResolution->currentText();
  816. bool found_res = false;
  817. int cx, cy;
  818. if (sscanf(QT_TO_UTF8(res), "%dx%d", &cx, &cy) != 2)
  819. return false;
  820. for (size_t i = 0; i < res_count; i++) {
  821. if (res_list[i].cx == cx && res_list[i].cy == cy) {
  822. found_res = true;
  823. break;
  824. }
  825. }
  826. if (!found_res)
  827. return false;
  828. }
  829. if (max_fps) {
  830. int fpsType = ui->fpsType->currentIndex();
  831. if (fpsType != 0)
  832. return false;
  833. std::string fps_str = QT_TO_UTF8(ui->fpsCommon->currentText());
  834. float fps;
  835. sscanf(fps_str.c_str(), "%f", &fps);
  836. if (fps > (float)max_fps)
  837. return false;
  838. }
  839. return true;
  840. }
  841. extern void set_closest_res(int &cx, int &cy,
  842. struct obs_service_resolution *res_list,
  843. size_t count);
  844. /* Checks for and updates the resolution and FPS limits of a service, if any.
  845. *
  846. * If the service has a resolution and/or FPS limit, this will enforce those
  847. * limitations in the UI itself, preventing the user from selecting a
  848. * resolution or FPS that's not supported.
  849. *
  850. * This is an unpleasant thing to have to do to users, but there is no other
  851. * way to ensure that a service's restricted resolution/framerate values are
  852. * properly enforced, otherwise users will just be confused when things aren't
  853. * working correctly. The user can turn it off if they're partner (or if they
  854. * want to risk getting in trouble with their service) by selecting the "Ignore
  855. * recommended settings" option in the stream section of settings.
  856. *
  857. * This only affects services that have a resolution and/or framerate limit, of
  858. * which as of this writing, and hopefully for the foreseeable future, there is
  859. * only one.
  860. */
  861. bool OBSBasicSettings::UpdateResFPSLimits()
  862. {
  863. if (loading)
  864. return false;
  865. int idx = ui->service->currentIndex();
  866. if (idx == -1)
  867. return false;
  868. bool ignoreRecommended = ui->ignoreRecommended->isChecked();
  869. BPtr<obs_service_resolution> res_list;
  870. size_t res_count = 0;
  871. int max_fps = 0;
  872. if (!IsCustomService() && !ignoreRecommended) {
  873. OBSService service = GetStream1Service();
  874. obs_service_get_supported_resolutions(service, &res_list,
  875. &res_count);
  876. obs_service_get_max_fps(service, &max_fps);
  877. }
  878. /* ------------------------------------ */
  879. /* Check for enforced res/FPS */
  880. QString res = ui->outputResolution->currentText();
  881. QString fps_str;
  882. int cx = 0, cy = 0;
  883. double max_fpsd = (double)max_fps;
  884. int closest_fps_index = -1;
  885. double fpsd;
  886. sscanf(QT_TO_UTF8(res), "%dx%d", &cx, &cy);
  887. if (res_count)
  888. set_closest_res(cx, cy, res_list, res_count);
  889. if (max_fps) {
  890. int fpsType = ui->fpsType->currentIndex();
  891. if (fpsType == 1) { //Integer
  892. fpsd = (double)ui->fpsInteger->value();
  893. } else if (fpsType == 2) { //Fractional
  894. fpsd = (double)ui->fpsNumerator->value() /
  895. (double)ui->fpsDenominator->value();
  896. } else { //Common
  897. sscanf(QT_TO_UTF8(ui->fpsCommon->currentText()), "%lf",
  898. &fpsd);
  899. }
  900. double closest_diff = 1000000000000.0;
  901. for (int i = 0; i < ui->fpsCommon->count(); i++) {
  902. double com_fpsd;
  903. sscanf(QT_TO_UTF8(ui->fpsCommon->itemText(i)), "%lf",
  904. &com_fpsd);
  905. if (com_fpsd > max_fpsd) {
  906. continue;
  907. }
  908. double diff = fabs(com_fpsd - fpsd);
  909. if (diff < closest_diff) {
  910. closest_diff = diff;
  911. closest_fps_index = i;
  912. fps_str = ui->fpsCommon->itemText(i);
  913. }
  914. }
  915. }
  916. QString res_str =
  917. QString("%1x%2").arg(QString::number(cx), QString::number(cy));
  918. /* ------------------------------------ */
  919. /* Display message box if res/FPS bad */
  920. bool valid = ResFPSValid(res_list, res_count, max_fps);
  921. if (!valid) {
  922. /* if the user was already on facebook with an incompatible
  923. * resolution, assume it's an upgrade */
  924. if (lastServiceIdx == -1 && lastIgnoreRecommended == -1) {
  925. ui->ignoreRecommended->setChecked(true);
  926. ui->ignoreRecommended->setProperty("changed", true);
  927. stream1Changed = true;
  928. EnableApplyButton(true);
  929. return UpdateResFPSLimits();
  930. }
  931. QMessageBox::StandardButton button;
  932. #define WARNING_VAL(x) \
  933. QTStr("Basic.Settings.Output.Warn.EnforceResolutionFPS." x)
  934. QString str;
  935. if (res_count)
  936. str += WARNING_VAL("Resolution").arg(res_str);
  937. if (max_fps) {
  938. if (!str.isEmpty())
  939. str += "\n";
  940. str += WARNING_VAL("FPS").arg(fps_str);
  941. }
  942. button = OBSMessageBox::question(this, WARNING_VAL("Title"),
  943. WARNING_VAL("Msg").arg(str));
  944. #undef WARNING_VAL
  945. if (button == QMessageBox::No) {
  946. if (idx != lastServiceIdx)
  947. QMetaObject::invokeMethod(
  948. ui->service, "setCurrentIndex",
  949. Qt::QueuedConnection,
  950. Q_ARG(int, lastServiceIdx));
  951. else
  952. QMetaObject::invokeMethod(ui->ignoreRecommended,
  953. "setChecked",
  954. Qt::QueuedConnection,
  955. Q_ARG(bool, true));
  956. return false;
  957. }
  958. }
  959. /* ------------------------------------ */
  960. /* Update widgets/values if switching */
  961. /* to/from enforced resolution/FPS */
  962. ui->outputResolution->blockSignals(true);
  963. if (res_count) {
  964. ui->outputResolution->clear();
  965. ui->outputResolution->setEditable(false);
  966. HookWidget(ui->outputResolution,
  967. SIGNAL(currentIndexChanged(int)),
  968. SLOT(VideoChangedResolution()));
  969. int new_res_index = -1;
  970. for (size_t i = 0; i < res_count; i++) {
  971. obs_service_resolution val = res_list[i];
  972. QString str =
  973. QString("%1x%2").arg(QString::number(val.cx),
  974. QString::number(val.cy));
  975. ui->outputResolution->addItem(str);
  976. if (val.cx == cx && val.cy == cy)
  977. new_res_index = (int)i;
  978. }
  979. ui->outputResolution->setCurrentIndex(new_res_index);
  980. if (!valid) {
  981. ui->outputResolution->setProperty("changed", true);
  982. videoChanged = true;
  983. EnableApplyButton(true);
  984. }
  985. } else {
  986. QString baseRes = ui->baseResolution->currentText();
  987. int baseCX, baseCY;
  988. sscanf(QT_TO_UTF8(baseRes), "%dx%d", &baseCX, &baseCY);
  989. if (!ui->outputResolution->isEditable()) {
  990. RecreateOutputResolutionWidget();
  991. ui->outputResolution->blockSignals(true);
  992. ResetDownscales((uint32_t)baseCX, (uint32_t)baseCY,
  993. true);
  994. ui->outputResolution->setCurrentText(res);
  995. }
  996. }
  997. ui->outputResolution->blockSignals(false);
  998. if (max_fps) {
  999. for (int i = 0; i < ui->fpsCommon->count(); i++) {
  1000. double com_fpsd;
  1001. sscanf(QT_TO_UTF8(ui->fpsCommon->itemText(i)), "%lf",
  1002. &com_fpsd);
  1003. if (com_fpsd > max_fpsd) {
  1004. SetComboItemEnabled(ui->fpsCommon, i, false);
  1005. continue;
  1006. }
  1007. }
  1008. ui->fpsType->setCurrentIndex(0);
  1009. ui->fpsCommon->setCurrentIndex(closest_fps_index);
  1010. if (!valid) {
  1011. ui->fpsType->setProperty("changed", true);
  1012. ui->fpsCommon->setProperty("changed", true);
  1013. videoChanged = true;
  1014. EnableApplyButton(true);
  1015. }
  1016. } else {
  1017. for (int i = 0; i < ui->fpsCommon->count(); i++)
  1018. SetComboItemEnabled(ui->fpsCommon, i, true);
  1019. }
  1020. SetComboItemEnabled(ui->fpsType, 1, !max_fps);
  1021. SetComboItemEnabled(ui->fpsType, 2, !max_fps);
  1022. /* ------------------------------------ */
  1023. lastIgnoreRecommended = (int)ignoreRecommended;
  1024. return true;
  1025. }
  1026. static bool service_supports_codec(const char **codecs, const char *codec)
  1027. {
  1028. if (!codecs)
  1029. return true;
  1030. while (*codecs) {
  1031. if (strcmp(*codecs, codec) == 0)
  1032. return true;
  1033. codecs++;
  1034. }
  1035. return false;
  1036. }
  1037. extern bool EncoderAvailable(const char *encoder);
  1038. extern const char *get_simple_output_encoder(const char *name);
  1039. static inline bool service_supports_encoder(const char **codecs,
  1040. const char *encoder)
  1041. {
  1042. if (!EncoderAvailable(encoder))
  1043. return false;
  1044. const char *codec = obs_get_encoder_codec(encoder);
  1045. return service_supports_codec(codecs, codec);
  1046. }
  1047. static bool return_first_id(void *data, const char *id)
  1048. {
  1049. const char **output = (const char **)data;
  1050. *output = id;
  1051. return false;
  1052. }
  1053. bool OBSBasicSettings::ServiceAndVCodecCompatible()
  1054. {
  1055. bool simple = (ui->outputMode->currentIndex() == 0);
  1056. bool ret;
  1057. const char *codec;
  1058. if (simple) {
  1059. QString encoder =
  1060. ui->simpleOutStrEncoder->currentData().toString();
  1061. const char *id = get_simple_output_encoder(QT_TO_UTF8(encoder));
  1062. codec = obs_get_encoder_codec(id);
  1063. } else {
  1064. QString encoder = ui->advOutEncoder->currentData().toString();
  1065. codec = obs_get_encoder_codec(QT_TO_UTF8(encoder));
  1066. }
  1067. OBSService service = SpawnTempService();
  1068. const char **codecs = obs_service_get_supported_video_codecs(service);
  1069. if (!codecs || IsCustomService()) {
  1070. const char *output;
  1071. char **output_codecs;
  1072. obs_enum_output_types_with_protocol(QT_TO_UTF8(protocol),
  1073. &output, return_first_id);
  1074. output_codecs = strlist_split(
  1075. obs_get_output_supported_video_codecs(output), ';',
  1076. false);
  1077. ret = service_supports_codec((const char **)output_codecs,
  1078. codec);
  1079. strlist_free(output_codecs);
  1080. } else {
  1081. ret = service_supports_codec(codecs, codec);
  1082. }
  1083. return ret;
  1084. }
  1085. bool OBSBasicSettings::ServiceAndACodecCompatible()
  1086. {
  1087. bool simple = (ui->outputMode->currentIndex() == 0);
  1088. bool ret;
  1089. QString codec;
  1090. if (simple) {
  1091. codec = ui->simpleOutStrAEncoder->currentData().toString();
  1092. } else {
  1093. QString encoder = ui->advOutAEncoder->currentData().toString();
  1094. codec = obs_get_encoder_codec(QT_TO_UTF8(encoder));
  1095. }
  1096. OBSService service = SpawnTempService();
  1097. const char **codecs = obs_service_get_supported_audio_codecs(service);
  1098. if (!codecs || IsCustomService()) {
  1099. const char *output;
  1100. char **output_codecs;
  1101. obs_enum_output_types_with_protocol(QT_TO_UTF8(protocol),
  1102. &output, return_first_id);
  1103. output_codecs = strlist_split(
  1104. obs_get_output_supported_audio_codecs(output), ';',
  1105. false);
  1106. ret = service_supports_codec((const char **)output_codecs,
  1107. QT_TO_UTF8(codec));
  1108. strlist_free(output_codecs);
  1109. } else {
  1110. ret = service_supports_codec(codecs, QT_TO_UTF8(codec));
  1111. }
  1112. return ret;
  1113. }
  1114. /* we really need a way to find fallbacks in a less hardcoded way. maybe. */
  1115. static QString get_adv_fallback(const QString &enc)
  1116. {
  1117. if (enc == "jim_hevc_nvenc" || enc == "jim_av1_nvenc")
  1118. return "jim_nvenc";
  1119. if (enc == "h265_texture_amf" || enc == "av1_texture_amf")
  1120. return "h264_texture_amf";
  1121. if (enc == "com.apple.videotoolbox.videoencoder.ave.hevc")
  1122. return "com.apple.videotoolbox.videoencoder.ave.avc";
  1123. if (enc == "obs_qsv11_av1")
  1124. return "obs_qsv11";
  1125. return "obs_x264";
  1126. }
  1127. static QString get_adv_audio_fallback(const QString &enc)
  1128. {
  1129. const char *codec = obs_get_encoder_codec(QT_TO_UTF8(enc));
  1130. if (strcmp(codec, "aac") == 0)
  1131. return "ffmpeg_opus";
  1132. QString aac_default = "ffmpeg_aac";
  1133. if (EncoderAvailable("CoreAudio_AAC"))
  1134. aac_default = "CoreAudio_AAC";
  1135. else if (EncoderAvailable("libfdk_aac"))
  1136. aac_default = "libfdk_aac";
  1137. return aac_default;
  1138. }
  1139. static QString get_simple_fallback(const QString &enc)
  1140. {
  1141. if (enc == SIMPLE_ENCODER_NVENC_HEVC || enc == SIMPLE_ENCODER_NVENC_AV1)
  1142. return SIMPLE_ENCODER_NVENC;
  1143. if (enc == SIMPLE_ENCODER_AMD_HEVC || enc == SIMPLE_ENCODER_AMD_AV1)
  1144. return SIMPLE_ENCODER_AMD;
  1145. if (enc == SIMPLE_ENCODER_APPLE_HEVC)
  1146. return SIMPLE_ENCODER_APPLE_H264;
  1147. if (enc == SIMPLE_ENCODER_QSV_AV1)
  1148. return SIMPLE_ENCODER_QSV;
  1149. return SIMPLE_ENCODER_X264;
  1150. }
  1151. bool OBSBasicSettings::ServiceSupportsCodecCheck()
  1152. {
  1153. if (loading)
  1154. return false;
  1155. bool vcodec_compat = ServiceAndVCodecCompatible();
  1156. bool acodec_compat = ServiceAndACodecCompatible();
  1157. if (vcodec_compat && acodec_compat) {
  1158. if (lastServiceIdx != ui->service->currentIndex() ||
  1159. IsCustomService())
  1160. ResetEncoders(true);
  1161. return true;
  1162. }
  1163. QString service = ui->service->currentText();
  1164. QString cur_video_name;
  1165. QString fb_video_name;
  1166. QString cur_audio_name;
  1167. QString fb_audio_name;
  1168. bool simple = (ui->outputMode->currentIndex() == 0);
  1169. /* ------------------------------------------------- */
  1170. /* get current codec */
  1171. if (simple) {
  1172. QString cur_enc =
  1173. ui->simpleOutStrEncoder->currentData().toString();
  1174. QString fb_enc = get_simple_fallback(cur_enc);
  1175. int cur_idx = ui->simpleOutStrEncoder->findData(cur_enc);
  1176. int fb_idx = ui->simpleOutStrEncoder->findData(fb_enc);
  1177. cur_video_name = ui->simpleOutStrEncoder->itemText(cur_idx);
  1178. fb_video_name = ui->simpleOutStrEncoder->itemText(fb_idx);
  1179. cur_enc = ui->simpleOutStrAEncoder->currentData().toString();
  1180. fb_enc = (cur_enc == "opus") ? "aac" : "opus";
  1181. cur_audio_name = ui->simpleOutStrAEncoder->itemText(
  1182. ui->simpleOutStrAEncoder->findData(cur_enc));
  1183. fb_audio_name =
  1184. (cur_enc == "opus")
  1185. ? QTStr("Basic.Settings.Output.Simple.Codec.AAC")
  1186. : QTStr("Basic.Settings.Output.Simple.Codec.Opus");
  1187. } else {
  1188. QString cur_enc = ui->advOutEncoder->currentData().toString();
  1189. QString fb_enc = get_adv_fallback(cur_enc);
  1190. cur_video_name =
  1191. obs_encoder_get_display_name(QT_TO_UTF8(cur_enc));
  1192. fb_video_name =
  1193. obs_encoder_get_display_name(QT_TO_UTF8(fb_enc));
  1194. cur_enc = ui->advOutAEncoder->currentData().toString();
  1195. fb_enc = get_adv_audio_fallback(cur_enc);
  1196. cur_audio_name =
  1197. obs_encoder_get_display_name(QT_TO_UTF8(cur_enc));
  1198. fb_audio_name =
  1199. obs_encoder_get_display_name(QT_TO_UTF8(fb_enc));
  1200. }
  1201. #define WARNING_VAL(x) \
  1202. QTStr("Basic.Settings.Output.Warn.ServiceCodecCompatibility." x)
  1203. QString msg = WARNING_VAL("Msg").arg(
  1204. service, vcodec_compat ? cur_audio_name : cur_video_name,
  1205. vcodec_compat ? fb_audio_name : fb_video_name);
  1206. if (!vcodec_compat && !acodec_compat)
  1207. msg = WARNING_VAL("Msg2").arg(service, cur_video_name,
  1208. cur_audio_name, fb_video_name,
  1209. fb_audio_name);
  1210. auto button = OBSMessageBox::question(this, WARNING_VAL("Title"), msg);
  1211. #undef WARNING_VAL
  1212. if (button == QMessageBox::No) {
  1213. if (lastServiceIdx == 0 &&
  1214. lastServiceIdx == ui->service->currentIndex())
  1215. QMetaObject::invokeMethod(ui->customServer, "setText",
  1216. Qt::QueuedConnection,
  1217. Q_ARG(QString,
  1218. lastCustomServer));
  1219. else
  1220. QMetaObject::invokeMethod(ui->service,
  1221. "setCurrentIndex",
  1222. Qt::QueuedConnection,
  1223. Q_ARG(int, lastServiceIdx));
  1224. return false;
  1225. }
  1226. ResetEncoders(true);
  1227. return true;
  1228. }
  1229. #define TEXT_USE_STREAM_ENC \
  1230. QTStr("Basic.Settings.Output.Adv.Recording.UseStreamEncoder")
  1231. void OBSBasicSettings::ResetEncoders(bool streamOnly)
  1232. {
  1233. QString lastAdvVideoEnc = ui->advOutEncoder->currentData().toString();
  1234. QString lastVideoEnc =
  1235. ui->simpleOutStrEncoder->currentData().toString();
  1236. QString lastAdvAudioEnc = ui->advOutAEncoder->currentData().toString();
  1237. QString lastAudioEnc =
  1238. ui->simpleOutStrAEncoder->currentData().toString();
  1239. OBSService service = SpawnTempService();
  1240. const char **vcodecs = obs_service_get_supported_video_codecs(service);
  1241. const char **acodecs = obs_service_get_supported_audio_codecs(service);
  1242. const char *type;
  1243. BPtr<char *> output_vcodecs;
  1244. BPtr<char *> output_acodecs;
  1245. size_t idx = 0;
  1246. if (!vcodecs || IsCustomService()) {
  1247. const char *output;
  1248. obs_enum_output_types_with_protocol(QT_TO_UTF8(protocol),
  1249. &output, return_first_id);
  1250. output_vcodecs = strlist_split(
  1251. obs_get_output_supported_video_codecs(output), ';',
  1252. false);
  1253. vcodecs = (const char **)output_vcodecs.Get();
  1254. }
  1255. if (!acodecs || IsCustomService()) {
  1256. const char *output;
  1257. obs_enum_output_types_with_protocol(QT_TO_UTF8(protocol),
  1258. &output, return_first_id);
  1259. output_acodecs = strlist_split(
  1260. obs_get_output_supported_audio_codecs(output), ';',
  1261. false);
  1262. acodecs = (const char **)output_acodecs.Get();
  1263. }
  1264. QSignalBlocker s1(ui->simpleOutStrEncoder);
  1265. QSignalBlocker s2(ui->advOutEncoder);
  1266. QSignalBlocker s3(ui->simpleOutStrAEncoder);
  1267. QSignalBlocker s4(ui->advOutAEncoder);
  1268. /* ------------------------------------------------- */
  1269. /* clear encoder lists */
  1270. ui->simpleOutStrEncoder->clear();
  1271. ui->advOutEncoder->clear();
  1272. ui->simpleOutStrAEncoder->clear();
  1273. ui->advOutAEncoder->clear();
  1274. if (!streamOnly) {
  1275. ui->advOutRecEncoder->clear();
  1276. ui->advOutRecAEncoder->clear();
  1277. }
  1278. /* ------------------------------------------------- */
  1279. /* load advanced stream/recording encoders */
  1280. while (obs_enum_encoder_types(idx++, &type)) {
  1281. const char *name = obs_encoder_get_display_name(type);
  1282. const char *codec = obs_get_encoder_codec(type);
  1283. uint32_t caps = obs_get_encoder_caps(type);
  1284. QString qName = QT_UTF8(name);
  1285. QString qType = QT_UTF8(type);
  1286. if (obs_get_encoder_type(type) == OBS_ENCODER_VIDEO) {
  1287. if ((caps & ENCODER_HIDE_FLAGS) != 0)
  1288. continue;
  1289. if (service_supports_codec(vcodecs, codec))
  1290. ui->advOutEncoder->addItem(qName, qType);
  1291. if (!streamOnly)
  1292. ui->advOutRecEncoder->addItem(qName, qType);
  1293. }
  1294. if (obs_get_encoder_type(type) == OBS_ENCODER_AUDIO) {
  1295. if (service_supports_codec(acodecs, codec))
  1296. ui->advOutAEncoder->addItem(qName, qType);
  1297. if (!streamOnly)
  1298. ui->advOutRecAEncoder->addItem(qName, qType);
  1299. }
  1300. }
  1301. ui->advOutEncoder->model()->sort(0);
  1302. ui->advOutAEncoder->model()->sort(0);
  1303. if (!streamOnly) {
  1304. ui->advOutRecEncoder->model()->sort(0);
  1305. ui->advOutRecEncoder->insertItem(0, TEXT_USE_STREAM_ENC,
  1306. "none");
  1307. ui->advOutRecAEncoder->model()->sort(0);
  1308. ui->advOutRecAEncoder->insertItem(0, TEXT_USE_STREAM_ENC,
  1309. "none");
  1310. }
  1311. /* ------------------------------------------------- */
  1312. /* load simple stream encoders */
  1313. #define ENCODER_STR(str) QTStr("Basic.Settings.Output.Simple.Encoder." str)
  1314. ui->simpleOutStrEncoder->addItem(ENCODER_STR("Software"),
  1315. QString(SIMPLE_ENCODER_X264));
  1316. if (service_supports_encoder(vcodecs, "obs_qsv11"))
  1317. ui->simpleOutStrEncoder->addItem(
  1318. ENCODER_STR("Hardware.QSV.H264"),
  1319. QString(SIMPLE_ENCODER_QSV));
  1320. if (service_supports_encoder(vcodecs, "obs_qsv11_av1"))
  1321. ui->simpleOutStrEncoder->addItem(
  1322. ENCODER_STR("Hardware.QSV.AV1"),
  1323. QString(SIMPLE_ENCODER_QSV_AV1));
  1324. if (service_supports_encoder(vcodecs, "ffmpeg_nvenc"))
  1325. ui->simpleOutStrEncoder->addItem(
  1326. ENCODER_STR("Hardware.NVENC.H264"),
  1327. QString(SIMPLE_ENCODER_NVENC));
  1328. if (service_supports_encoder(vcodecs, "jim_av1_nvenc"))
  1329. ui->simpleOutStrEncoder->addItem(
  1330. ENCODER_STR("Hardware.NVENC.AV1"),
  1331. QString(SIMPLE_ENCODER_NVENC_AV1));
  1332. #ifdef ENABLE_HEVC
  1333. if (service_supports_encoder(vcodecs, "h265_texture_amf"))
  1334. ui->simpleOutStrEncoder->addItem(
  1335. ENCODER_STR("Hardware.AMD.HEVC"),
  1336. QString(SIMPLE_ENCODER_AMD_HEVC));
  1337. if (service_supports_encoder(vcodecs, "ffmpeg_hevc_nvenc"))
  1338. ui->simpleOutStrEncoder->addItem(
  1339. ENCODER_STR("Hardware.NVENC.HEVC"),
  1340. QString(SIMPLE_ENCODER_NVENC_HEVC));
  1341. #endif
  1342. if (service_supports_encoder(vcodecs, "h264_texture_amf"))
  1343. ui->simpleOutStrEncoder->addItem(
  1344. ENCODER_STR("Hardware.AMD.H264"),
  1345. QString(SIMPLE_ENCODER_AMD));
  1346. if (service_supports_encoder(vcodecs, "av1_texture_amf"))
  1347. ui->simpleOutStrEncoder->addItem(
  1348. ENCODER_STR("Hardware.AMD.AV1"),
  1349. QString(SIMPLE_ENCODER_AMD_AV1));
  1350. /* Preprocessor guard required for the macOS version check */
  1351. #ifdef __APPLE__
  1352. if (service_supports_encoder(
  1353. vcodecs, "com.apple.videotoolbox.videoencoder.ave.avc")
  1354. #ifndef __aarch64__
  1355. && os_get_emulation_status() == true
  1356. #endif
  1357. ) {
  1358. if (__builtin_available(macOS 13.0, *)) {
  1359. ui->simpleOutStrEncoder->addItem(
  1360. ENCODER_STR("Hardware.Apple.H264"),
  1361. QString(SIMPLE_ENCODER_APPLE_H264));
  1362. }
  1363. }
  1364. #ifdef ENABLE_HEVC
  1365. if (service_supports_encoder(
  1366. vcodecs, "com.apple.videotoolbox.videoencoder.ave.hevc")
  1367. #ifndef __aarch64__
  1368. && os_get_emulation_status() == true
  1369. #endif
  1370. ) {
  1371. if (__builtin_available(macOS 13.0, *)) {
  1372. ui->simpleOutStrEncoder->addItem(
  1373. ENCODER_STR("Hardware.Apple.HEVC"),
  1374. QString(SIMPLE_ENCODER_APPLE_HEVC));
  1375. }
  1376. }
  1377. #endif
  1378. #endif
  1379. if (service_supports_encoder(acodecs, "CoreAudio_AAC") ||
  1380. service_supports_encoder(acodecs, "libfdk_aac") ||
  1381. service_supports_encoder(acodecs, "ffmpeg_aac"))
  1382. ui->simpleOutStrAEncoder->addItem(
  1383. QTStr("Basic.Settings.Output.Simple.Codec.AAC.Default"),
  1384. "aac");
  1385. if (service_supports_encoder(acodecs, "ffmpeg_opus"))
  1386. ui->simpleOutStrAEncoder->addItem(
  1387. QTStr("Basic.Settings.Output.Simple.Codec.Opus"),
  1388. "opus");
  1389. #undef ENCODER_STR
  1390. /* ------------------------------------------------- */
  1391. /* Find fallback encoders */
  1392. if (!lastAdvVideoEnc.isEmpty()) {
  1393. int idx = ui->advOutEncoder->findData(lastAdvVideoEnc);
  1394. if (idx == -1) {
  1395. lastAdvVideoEnc = get_adv_fallback(lastAdvVideoEnc);
  1396. ui->advOutEncoder->setProperty("changed",
  1397. QVariant(true));
  1398. OutputsChanged();
  1399. }
  1400. idx = ui->advOutEncoder->findData(lastAdvVideoEnc);
  1401. s2.unblock();
  1402. ui->advOutEncoder->setCurrentIndex(idx);
  1403. }
  1404. if (!lastAdvAudioEnc.isEmpty()) {
  1405. int idx = ui->advOutAEncoder->findData(lastAdvAudioEnc);
  1406. if (idx == -1) {
  1407. lastAdvAudioEnc =
  1408. get_adv_audio_fallback(lastAdvAudioEnc);
  1409. ui->advOutAEncoder->setProperty("changed",
  1410. QVariant(true));
  1411. OutputsChanged();
  1412. }
  1413. idx = ui->advOutAEncoder->findData(lastAdvAudioEnc);
  1414. s4.unblock();
  1415. ui->advOutAEncoder->setCurrentIndex(idx);
  1416. }
  1417. if (!lastVideoEnc.isEmpty()) {
  1418. int idx = ui->simpleOutStrEncoder->findData(lastVideoEnc);
  1419. if (idx == -1) {
  1420. lastVideoEnc = get_simple_fallback(lastVideoEnc);
  1421. ui->simpleOutStrEncoder->setProperty("changed",
  1422. QVariant(true));
  1423. OutputsChanged();
  1424. }
  1425. idx = ui->simpleOutStrEncoder->findData(lastVideoEnc);
  1426. s1.unblock();
  1427. ui->simpleOutStrEncoder->setCurrentIndex(idx);
  1428. }
  1429. if (!lastAudioEnc.isEmpty()) {
  1430. int idx = ui->simpleOutStrAEncoder->findData(lastAudioEnc);
  1431. if (idx == -1) {
  1432. lastAudioEnc = (lastAudioEnc == "opus") ? "aac"
  1433. : "opus";
  1434. ui->simpleOutStrAEncoder->setProperty("changed",
  1435. QVariant(true));
  1436. OutputsChanged();
  1437. }
  1438. idx = ui->simpleOutStrAEncoder->findData(lastAudioEnc);
  1439. s3.unblock();
  1440. ui->simpleOutStrAEncoder->setCurrentIndex(idx);
  1441. }
  1442. }