window-basic-settings-stream.cpp 48 KB

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