window-basic-settings-stream.cpp 48 KB

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