window-basic-settings-stream.cpp 46 KB

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