window-basic-settings-stream.cpp 47 KB

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