window-basic-main-profiles.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <filesystem>
  15. #include <functional>
  16. #include <string>
  17. #include <map>
  18. #include <tuple>
  19. #include <obs.hpp>
  20. #include <util/platform.h>
  21. #include <util/util.hpp>
  22. #include <QMessageBox>
  23. #include <QVariant>
  24. #include <QFileDialog>
  25. #include <qt-wrappers.hpp>
  26. #include "window-basic-main.hpp"
  27. #include "window-basic-auto-config.hpp"
  28. #include "window-namedialog.hpp"
  29. // MARK: Constant Expressions
  30. constexpr std::string_view OBSProfilePath = "/obs-studio/basic/profiles/";
  31. constexpr std::string_view OBSProfileSettingsFile = "basic.ini";
  32. // MARK: Forward Declarations
  33. extern void DestroyPanelCookieManager();
  34. extern void DuplicateCurrentCookieProfile(ConfigFile &config);
  35. extern void CheckExistingCookieId();
  36. extern void DeleteCookies();
  37. // MARK: - Main Profile Management Functions
  38. void OBSBasic::SetupNewProfile(const std::string &profileName, bool useWizard)
  39. {
  40. const OBSProfile &newProfile = CreateProfile(profileName);
  41. config_set_bool(App()->GetUserConfig(), "Basic", "ConfigOnNewProfile", useWizard);
  42. ActivateProfile(newProfile, true);
  43. blog(LOG_INFO, "Created profile '%s' (clean, %s)", newProfile.name.c_str(), newProfile.directoryName.c_str());
  44. blog(LOG_INFO, "------------------------------------------------");
  45. if (useWizard) {
  46. AutoConfig wizard(this);
  47. wizard.setModal(true);
  48. wizard.show();
  49. wizard.exec();
  50. }
  51. }
  52. void OBSBasic::SetupDuplicateProfile(const std::string &profileName)
  53. {
  54. const OBSProfile &newProfile = CreateProfile(profileName);
  55. const OBSProfile &currentProfile = GetCurrentProfile();
  56. const auto copyOptions = std::filesystem::copy_options::recursive |
  57. std::filesystem::copy_options::overwrite_existing;
  58. try {
  59. std::filesystem::copy(currentProfile.path, newProfile.path, copyOptions);
  60. } catch (const std::filesystem::filesystem_error &error) {
  61. blog(LOG_DEBUG, "%s", error.what());
  62. throw std::logic_error("Failed to copy files for cloned profile: " + newProfile.name);
  63. }
  64. ActivateProfile(newProfile);
  65. blog(LOG_INFO, "Created profile '%s' (duplicate, %s)", newProfile.name.c_str(),
  66. newProfile.directoryName.c_str());
  67. blog(LOG_INFO, "------------------------------------------------");
  68. }
  69. void OBSBasic::SetupRenameProfile(const std::string &profileName)
  70. {
  71. const OBSProfile &newProfile = CreateProfile(profileName);
  72. const OBSProfile currentProfile = GetCurrentProfile();
  73. const auto copyOptions = std::filesystem::copy_options::recursive |
  74. std::filesystem::copy_options::overwrite_existing;
  75. try {
  76. std::filesystem::copy(currentProfile.path, newProfile.path, copyOptions);
  77. } catch (const std::filesystem::filesystem_error &error) {
  78. blog(LOG_DEBUG, "%s", error.what());
  79. throw std::logic_error("Failed to copy files for profile: " + currentProfile.name);
  80. }
  81. profiles.erase(currentProfile.name);
  82. ActivateProfile(newProfile);
  83. RemoveProfile(currentProfile);
  84. blog(LOG_INFO, "Renamed profile '%s' to '%s' (%s)", currentProfile.name.c_str(), newProfile.name.c_str(),
  85. newProfile.directoryName.c_str());
  86. blog(LOG_INFO, "------------------------------------------------");
  87. OnEvent(OBS_FRONTEND_EVENT_PROFILE_RENAMED);
  88. }
  89. // MARK: - Profile File Management Functions
  90. const OBSProfile &OBSBasic::CreateProfile(const std::string &profileName)
  91. {
  92. if (const auto &foundProfile = GetProfileByName(profileName)) {
  93. throw std::invalid_argument("Profile already exists: " + profileName);
  94. }
  95. std::string directoryName;
  96. if (!GetFileSafeName(profileName.c_str(), directoryName)) {
  97. throw std::invalid_argument("Failed to create safe directory for new profile: " + profileName);
  98. }
  99. std::string profileDirectory;
  100. profileDirectory.reserve(App()->userProfilesLocation.u8string().size() + OBSProfilePath.size() +
  101. directoryName.size());
  102. profileDirectory.append(App()->userProfilesLocation.u8string()).append(OBSProfilePath).append(directoryName);
  103. if (!GetClosestUnusedFileName(profileDirectory, nullptr)) {
  104. throw std::invalid_argument("Failed to get closest directory name for new profile: " + profileName);
  105. }
  106. const std::filesystem::path profileDirectoryPath = std::filesystem::u8path(profileDirectory);
  107. try {
  108. std::filesystem::create_directory(profileDirectoryPath);
  109. } catch (const std::filesystem::filesystem_error error) {
  110. throw std::logic_error("Failed to create directory for new profile: " + profileDirectory);
  111. }
  112. const std::filesystem::path profileFile =
  113. profileDirectoryPath / std::filesystem::u8path(OBSProfileSettingsFile);
  114. auto [iterator, success] =
  115. profiles.try_emplace(profileName, OBSProfile{profileName, profileDirectoryPath.filename().u8string(),
  116. profileDirectoryPath, profileFile});
  117. OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  118. return iterator->second;
  119. }
  120. void OBSBasic::RemoveProfile(OBSProfile profile)
  121. {
  122. try {
  123. std::filesystem::remove_all(profile.path);
  124. } catch (const std::filesystem::filesystem_error &error) {
  125. blog(LOG_DEBUG, "%s", error.what());
  126. throw std::logic_error("Failed to remove profile directory: " + profile.directoryName);
  127. }
  128. blog(LOG_INFO, "------------------------------------------------");
  129. blog(LOG_INFO, "Removed profile '%s' (%s)", profile.name.c_str(), profile.directoryName.c_str());
  130. blog(LOG_INFO, "------------------------------------------------");
  131. }
  132. // MARK: - Profile UI Handling Functions
  133. bool OBSBasic::CreateNewProfile(const QString &name)
  134. {
  135. try {
  136. SetupNewProfile(name.toStdString());
  137. return true;
  138. } catch (const std::invalid_argument &error) {
  139. blog(LOG_ERROR, "%s", error.what());
  140. return false;
  141. } catch (const std::logic_error &error) {
  142. blog(LOG_ERROR, "%s", error.what());
  143. return false;
  144. }
  145. }
  146. bool OBSBasic::CreateDuplicateProfile(const QString &name)
  147. {
  148. try {
  149. SetupDuplicateProfile(name.toStdString());
  150. return true;
  151. } catch (const std::invalid_argument &error) {
  152. blog(LOG_ERROR, "%s", error.what());
  153. return false;
  154. } catch (const std::logic_error &error) {
  155. blog(LOG_ERROR, "%s", error.what());
  156. return false;
  157. }
  158. }
  159. void OBSBasic::DeleteProfile(const QString &name)
  160. {
  161. const std::string_view currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  162. if (currentProfileName == name.toStdString()) {
  163. on_actionRemoveProfile_triggered();
  164. return;
  165. }
  166. auto foundProfile = GetProfileByName(name.toStdString());
  167. if (!foundProfile) {
  168. blog(LOG_ERROR, "Invalid profile name: %s", QT_TO_UTF8(name));
  169. return;
  170. }
  171. RemoveProfile(foundProfile.value());
  172. profiles.erase(name.toStdString());
  173. RefreshProfiles();
  174. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  175. OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  176. }
  177. void OBSBasic::ChangeProfile()
  178. {
  179. QAction *action = reinterpret_cast<QAction *>(sender());
  180. if (!action) {
  181. return;
  182. }
  183. const std::string_view currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  184. const std::string selectedProfileName{action->text().toStdString()};
  185. if (currentProfileName == selectedProfileName) {
  186. action->setChecked(true);
  187. return;
  188. }
  189. const std::optional<OBSProfile> foundProfile = GetProfileByName(selectedProfileName);
  190. if (!foundProfile) {
  191. const std::string errorMessage{"Selected profile not found: "};
  192. throw std::invalid_argument(errorMessage + currentProfileName.data());
  193. }
  194. const OBSProfile &selectedProfile = foundProfile.value();
  195. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  196. ActivateProfile(selectedProfile, true);
  197. blog(LOG_INFO, "Switched to profile '%s' (%s)", selectedProfile.name.c_str(),
  198. selectedProfile.directoryName.c_str());
  199. blog(LOG_INFO, "------------------------------------------------");
  200. }
  201. void OBSBasic::RefreshProfiles(bool refreshCache)
  202. {
  203. std::string_view currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  204. QList<QAction *> menuActions = ui->profileMenu->actions();
  205. for (auto &action : menuActions) {
  206. QVariant variant = action->property("file_name");
  207. if (variant.typeName() != nullptr) {
  208. delete action;
  209. }
  210. }
  211. if (refreshCache) {
  212. RefreshProfileCache();
  213. }
  214. size_t numAddedProfiles = 0;
  215. for (auto &[profileName, profile] : profiles) {
  216. QAction *action = new QAction(QString().fromStdString(profileName), this);
  217. action->setProperty("file_name", QString().fromStdString(profile.directoryName));
  218. connect(action, &QAction::triggered, this, &OBSBasic::ChangeProfile);
  219. action->setCheckable(true);
  220. action->setChecked(profileName == currentProfileName);
  221. ui->profileMenu->addAction(action);
  222. numAddedProfiles += 1;
  223. }
  224. ui->actionRemoveProfile->setEnabled(numAddedProfiles > 1);
  225. }
  226. // MARK: - Profile Cache Functions
  227. /// Refreshes profile cache data with profile state found on local file system.
  228. void OBSBasic::RefreshProfileCache()
  229. {
  230. std::map<std::string, OBSProfile> foundProfiles{};
  231. const std::filesystem::path profilesPath =
  232. App()->userProfilesLocation / std::filesystem::u8path(OBSProfilePath.substr(1));
  233. if (!std::filesystem::exists(profilesPath)) {
  234. blog(LOG_WARNING, "Failed to get profiles config path");
  235. return;
  236. }
  237. for (const auto &entry : std::filesystem::directory_iterator(profilesPath)) {
  238. if (!entry.is_directory()) {
  239. continue;
  240. }
  241. std::string profileCandidate;
  242. profileCandidate.reserve(entry.path().u8string().size() + OBSProfileSettingsFile.size() + 1);
  243. profileCandidate.append(entry.path().u8string()).append("/").append(OBSProfileSettingsFile);
  244. ConfigFile config;
  245. if (config.Open(profileCandidate.c_str(), CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
  246. continue;
  247. }
  248. std::string candidateName;
  249. const char *configName = config_get_string(config, "General", "Name");
  250. if (configName) {
  251. candidateName = configName;
  252. } else {
  253. candidateName = entry.path().filename().u8string();
  254. }
  255. foundProfiles.try_emplace(candidateName, OBSProfile{candidateName, entry.path().filename().u8string(),
  256. entry.path(), profileCandidate});
  257. }
  258. profiles.swap(foundProfiles);
  259. }
  260. const OBSProfile &OBSBasic::GetCurrentProfile() const
  261. {
  262. std::string currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  263. if (currentProfileName.empty()) {
  264. throw std::invalid_argument("No valid profile name in configuration Basic->Profile");
  265. }
  266. const auto &foundProfile = profiles.find(currentProfileName);
  267. if (foundProfile != profiles.end()) {
  268. return foundProfile->second;
  269. } else {
  270. throw std::invalid_argument("Profile not found in profile list: " + currentProfileName);
  271. }
  272. }
  273. std::optional<OBSProfile> OBSBasic::GetProfileByName(const std::string &profileName) const
  274. {
  275. auto foundProfile = profiles.find(profileName);
  276. if (foundProfile == profiles.end()) {
  277. return {};
  278. } else {
  279. return foundProfile->second;
  280. }
  281. }
  282. std::optional<OBSProfile> OBSBasic::GetProfileByDirectoryName(const std::string &directoryName) const
  283. {
  284. for (auto &[iterator, profile] : profiles) {
  285. if (profile.directoryName == directoryName) {
  286. return profile;
  287. }
  288. }
  289. return {};
  290. }
  291. // MARK: - Qt Slot Functions
  292. void OBSBasic::on_actionNewProfile_triggered()
  293. {
  294. bool useProfileWizard = config_get_bool(App()->GetUserConfig(), "Basic", "ConfigOnNewProfile");
  295. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  296. if (GetProfileByName(result.promptValue)) {
  297. return false;
  298. }
  299. return true;
  300. };
  301. const OBSPromptRequest request{Str("AddProfile.Title"), Str("AddProfile.Text"), "", true,
  302. Str("AddProfile.WizardCheckbox"), useProfileWizard};
  303. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  304. if (!result.success) {
  305. return;
  306. }
  307. try {
  308. SetupNewProfile(result.promptValue, result.optionValue);
  309. } catch (const std::invalid_argument &error) {
  310. blog(LOG_ERROR, "%s", error.what());
  311. } catch (const std::logic_error &error) {
  312. blog(LOG_ERROR, "%s", error.what());
  313. }
  314. }
  315. void OBSBasic::on_actionDupProfile_triggered()
  316. {
  317. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  318. if (GetProfileByName(result.promptValue)) {
  319. return false;
  320. }
  321. return true;
  322. };
  323. const OBSPromptRequest request{Str("AddProfile.Title"), Str("AddProfile.Text")};
  324. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  325. if (!result.success) {
  326. return;
  327. }
  328. try {
  329. SetupDuplicateProfile(result.promptValue);
  330. } catch (const std::invalid_argument &error) {
  331. blog(LOG_ERROR, "%s", error.what());
  332. } catch (const std::logic_error &error) {
  333. blog(LOG_ERROR, "%s", error.what());
  334. }
  335. }
  336. void OBSBasic::on_actionRenameProfile_triggered()
  337. {
  338. const std::string currentProfileName = config_get_string(App()->GetUserConfig(), "Basic", "Profile");
  339. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  340. if (GetProfileByName(result.promptValue)) {
  341. return false;
  342. }
  343. return true;
  344. };
  345. const OBSPromptRequest request{Str("RenameProfile.Title"), Str("RenameProfile.Text"), currentProfileName};
  346. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  347. if (!result.success) {
  348. return;
  349. }
  350. try {
  351. SetupRenameProfile(result.promptValue);
  352. } catch (const std::invalid_argument &error) {
  353. blog(LOG_ERROR, "%s", error.what());
  354. } catch (const std::logic_error &error) {
  355. blog(LOG_ERROR, "%s", error.what());
  356. }
  357. }
  358. void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
  359. {
  360. if (profiles.size() < 2) {
  361. return;
  362. }
  363. OBSProfile currentProfile;
  364. try {
  365. currentProfile = GetCurrentProfile();
  366. if (!skipConfirmation) {
  367. const QString confirmationText =
  368. QTStr("ConfirmRemove.Text").arg(QString::fromStdString(currentProfile.name));
  369. const QMessageBox::StandardButton button =
  370. OBSMessageBox::question(this, QTStr("ConfirmRemove.Title"), confirmationText);
  371. if (button == QMessageBox::No) {
  372. return;
  373. }
  374. }
  375. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  376. profiles.erase(currentProfile.name);
  377. } catch (const std::invalid_argument &error) {
  378. blog(LOG_ERROR, "%s", error.what());
  379. } catch (const std::logic_error &error) {
  380. blog(LOG_ERROR, "%s", error.what());
  381. }
  382. const OBSProfile &newProfile = profiles.rbegin()->second;
  383. ActivateProfile(newProfile, true);
  384. RemoveProfile(currentProfile);
  385. #ifdef YOUTUBE_ENABLED
  386. if (YouTubeAppDock::IsYTServiceSelected() && !youtubeAppDock)
  387. NewYouTubeAppDock();
  388. #endif
  389. blog(LOG_INFO, "Switched to profile '%s' (%s)", newProfile.name.c_str(), newProfile.directoryName.c_str());
  390. blog(LOG_INFO, "------------------------------------------------");
  391. }
  392. void OBSBasic::on_actionImportProfile_triggered()
  393. {
  394. const QString home = QDir::homePath();
  395. const QString sourceDirectory = SelectDirectory(this, QTStr("Basic.MainMenu.Profile.Import"), home);
  396. if (!sourceDirectory.isEmpty() && !sourceDirectory.isNull()) {
  397. const std::filesystem::path sourcePath = std::filesystem::u8path(sourceDirectory.toStdString());
  398. const std::string directoryName = sourcePath.filename().string();
  399. if (auto profile = GetProfileByDirectoryName(directoryName)) {
  400. OBSMessageBox::warning(this, QTStr("Basic.MainMenu.Profile.Import"),
  401. QTStr("Basic.MainMenu.Profile.Exists"));
  402. return;
  403. }
  404. std::string destinationPathString;
  405. destinationPathString.reserve(App()->userProfilesLocation.u8string().size() + OBSProfilePath.size() +
  406. directoryName.size());
  407. destinationPathString.append(App()->userProfilesLocation.u8string())
  408. .append(OBSProfilePath)
  409. .append(directoryName);
  410. const std::filesystem::path destinationPath = std::filesystem::u8path(destinationPathString);
  411. try {
  412. std::filesystem::create_directory(destinationPath);
  413. } catch (const std::filesystem::filesystem_error &error) {
  414. blog(LOG_WARNING, "Failed to create profile directory '%s':\n%s", directoryName.c_str(),
  415. error.what());
  416. return;
  417. }
  418. const std::array<std::pair<std::string, bool>, 4> profileFiles{{
  419. {"basic.ini", true},
  420. {"service.json", false},
  421. {"streamEncoder.json", false},
  422. {"recordEncoder.json", false},
  423. }};
  424. for (auto &[file, isMandatory] : profileFiles) {
  425. const std::filesystem::path sourceFile = sourcePath / std::filesystem::u8path(file);
  426. if (!std::filesystem::exists(sourceFile)) {
  427. if (isMandatory) {
  428. blog(LOG_ERROR,
  429. "Failed to import profile from directory '%s' - necessary file '%s' not found",
  430. directoryName.c_str(), file.c_str());
  431. return;
  432. }
  433. continue;
  434. }
  435. const std::filesystem::path destinationFile = destinationPath / std::filesystem::u8path(file);
  436. try {
  437. std::filesystem::copy(sourceFile, destinationFile);
  438. } catch (const std::filesystem::filesystem_error &error) {
  439. blog(LOG_WARNING, "Failed to copy import file '%s' for profile '%s':\n%s", file.c_str(),
  440. directoryName.c_str(), error.what());
  441. return;
  442. }
  443. }
  444. RefreshProfiles(true);
  445. }
  446. }
  447. void OBSBasic::on_actionExportProfile_triggered()
  448. {
  449. const OBSProfile &currentProfile = GetCurrentProfile();
  450. const QString home = QDir::homePath();
  451. const QString destinationDirectory = SelectDirectory(this, QTStr("Basic.MainMenu.Profile.Export"), home);
  452. const std::array<std::string, 4> profileFiles{"basic.ini", "service.json", "streamEncoder.json",
  453. "recordEncoder.json"};
  454. if (!destinationDirectory.isEmpty() && !destinationDirectory.isNull()) {
  455. const std::filesystem::path sourcePath = currentProfile.path;
  456. const std::filesystem::path destinationPath =
  457. std::filesystem::u8path(destinationDirectory.toStdString()) /
  458. std::filesystem::u8path(currentProfile.directoryName);
  459. if (!std::filesystem::exists(destinationPath)) {
  460. std::filesystem::create_directory(destinationPath);
  461. }
  462. std::filesystem::copy_options copyOptions = std::filesystem::copy_options::overwrite_existing;
  463. for (auto &file : profileFiles) {
  464. const std::filesystem::path sourceFile = sourcePath / std::filesystem::u8path(file);
  465. if (!std::filesystem::exists(sourceFile)) {
  466. continue;
  467. }
  468. const std::filesystem::path destinationFile = destinationPath / std::filesystem::u8path(file);
  469. try {
  470. std::filesystem::copy(sourceFile, destinationFile, copyOptions);
  471. } catch (const std::filesystem::filesystem_error &error) {
  472. blog(LOG_WARNING, "Failed to copy export file '%s' for profile '%s'\n%s", file.c_str(),
  473. currentProfile.name.c_str(), error.what());
  474. return;
  475. }
  476. }
  477. }
  478. }
  479. // MARK: - Profile Management Helper Functions
  480. void OBSBasic::ActivateProfile(const OBSProfile &profile, bool reset)
  481. {
  482. ConfigFile config;
  483. if (config.Open(profile.profileFile.u8string().c_str(), CONFIG_OPEN_ALWAYS) != CONFIG_SUCCESS) {
  484. throw std::logic_error("failed to open configuration file of new profile: " +
  485. profile.profileFile.string());
  486. }
  487. config_set_string(config, "General", "Name", profile.name.c_str());
  488. config.SaveSafe("tmp");
  489. std::vector<std::string> restartRequirements;
  490. if (activeConfiguration) {
  491. Auth::Save();
  492. if (reset) {
  493. auth.reset();
  494. DestroyPanelCookieManager();
  495. #ifdef YOUTUBE_ENABLED
  496. if (youtubeAppDock) {
  497. DeleteYouTubeAppDock();
  498. }
  499. #endif
  500. }
  501. restartRequirements = GetRestartRequirements(config);
  502. activeConfiguration.SaveSafe("tmp");
  503. }
  504. activeConfiguration.Swap(config);
  505. config_set_string(App()->GetUserConfig(), "Basic", "Profile", profile.name.c_str());
  506. config_set_string(App()->GetUserConfig(), "Basic", "ProfileDir", profile.directoryName.c_str());
  507. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  508. InitBasicConfigDefaults();
  509. InitBasicConfigDefaults2();
  510. if (reset) {
  511. ResetProfileData();
  512. }
  513. CheckForSimpleModeX264Fallback();
  514. RefreshProfiles();
  515. UpdateTitleBar();
  516. UpdateVolumeControlsDecayRate();
  517. Auth::Load();
  518. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  519. if (!restartRequirements.empty()) {
  520. std::string requirements = std::accumulate(
  521. std::next(restartRequirements.begin()), restartRequirements.end(), restartRequirements[0],
  522. [](std::string a, std::string b) { return std::move(a) + "\n" + b; });
  523. QMessageBox::StandardButton button = OBSMessageBox::question(
  524. this, QTStr("Restart"), QTStr("LoadProfileNeedsRestart").arg(requirements.c_str()));
  525. if (button == QMessageBox::Yes) {
  526. restart = true;
  527. close();
  528. }
  529. }
  530. }
  531. void OBSBasic::ResetProfileData()
  532. {
  533. ResetVideo();
  534. service = nullptr;
  535. InitService();
  536. ResetOutputs();
  537. ClearHotkeys();
  538. CreateHotkeys();
  539. /* load audio monitoring */
  540. if (obs_audio_monitoring_available()) {
  541. const char *device_name = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceName");
  542. const char *device_id = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceId");
  543. obs_set_audio_monitoring_device(device_name, device_id);
  544. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s", device_name, device_id);
  545. }
  546. }
  547. std::vector<std::string> OBSBasic::GetRestartRequirements(const ConfigFile &config) const
  548. {
  549. std::vector<std::string> result;
  550. const char *oldSpeakers = config_get_string(activeConfiguration, "Audio", "ChannelSetup");
  551. const char *newSpeakers = config_get_string(config, "Audio", "ChannelSetup");
  552. uint64_t oldSampleRate = config_get_uint(activeConfiguration, "Audio", "SampleRate");
  553. uint64_t newSampleRate = config_get_uint(config, "Audio", "SampleRate");
  554. if (oldSpeakers != NULL && newSpeakers != NULL) {
  555. if (std::string_view{oldSpeakers} != std::string_view{newSpeakers}) {
  556. result.emplace_back(Str("Basic.Settings.Audio.Channels"));
  557. }
  558. }
  559. if (oldSampleRate != 0 && newSampleRate != 0) {
  560. if (oldSampleRate != newSampleRate) {
  561. result.emplace_back(Str("Basic.Settings.Audio.SampleRate"));
  562. }
  563. }
  564. return result;
  565. }
  566. void OBSBasic::CheckForSimpleModeX264Fallback()
  567. {
  568. const char *curStreamEncoder = config_get_string(activeConfiguration, "SimpleOutput", "StreamEncoder");
  569. const char *curRecEncoder = config_get_string(activeConfiguration, "SimpleOutput", "RecEncoder");
  570. bool qsv_supported = false;
  571. bool qsv_av1_supported = false;
  572. bool amd_supported = false;
  573. bool nve_supported = false;
  574. #ifdef ENABLE_HEVC
  575. bool amd_hevc_supported = false;
  576. bool nve_hevc_supported = false;
  577. bool apple_hevc_supported = false;
  578. #endif
  579. bool amd_av1_supported = false;
  580. bool apple_supported = false;
  581. bool changed = false;
  582. size_t idx = 0;
  583. const char *id;
  584. while (obs_enum_encoder_types(idx++, &id)) {
  585. if (strcmp(id, "h264_texture_amf") == 0)
  586. amd_supported = true;
  587. else if (strcmp(id, "obs_qsv11") == 0)
  588. qsv_supported = true;
  589. else if (strcmp(id, "obs_qsv11_av1") == 0)
  590. qsv_av1_supported = true;
  591. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  592. nve_supported = true;
  593. #ifdef ENABLE_HEVC
  594. else if (strcmp(id, "h265_texture_amf") == 0)
  595. amd_hevc_supported = true;
  596. else if (strcmp(id, "ffmpeg_hevc_nvenc") == 0)
  597. nve_hevc_supported = true;
  598. #endif
  599. else if (strcmp(id, "av1_texture_amf") == 0)
  600. amd_av1_supported = true;
  601. else if (strcmp(id, "com.apple.videotoolbox.videoencoder.ave.avc") == 0)
  602. apple_supported = true;
  603. #ifdef ENABLE_HEVC
  604. else if (strcmp(id, "com.apple.videotoolbox.videoencoder.ave.hevc") == 0)
  605. apple_hevc_supported = true;
  606. #endif
  607. }
  608. auto CheckEncoder = [&](const char *&name) {
  609. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  610. if (!qsv_supported) {
  611. changed = true;
  612. name = SIMPLE_ENCODER_X264;
  613. return false;
  614. }
  615. } else if (strcmp(name, SIMPLE_ENCODER_QSV_AV1) == 0) {
  616. if (!qsv_av1_supported) {
  617. changed = true;
  618. name = SIMPLE_ENCODER_X264;
  619. return false;
  620. }
  621. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  622. if (!nve_supported) {
  623. changed = true;
  624. name = SIMPLE_ENCODER_X264;
  625. return false;
  626. }
  627. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  628. if (!nve_supported) {
  629. changed = true;
  630. name = SIMPLE_ENCODER_X264;
  631. return false;
  632. }
  633. #ifdef ENABLE_HEVC
  634. } else if (strcmp(name, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  635. if (!amd_hevc_supported) {
  636. changed = true;
  637. name = SIMPLE_ENCODER_X264;
  638. return false;
  639. }
  640. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  641. if (!nve_hevc_supported) {
  642. changed = true;
  643. name = SIMPLE_ENCODER_X264;
  644. return false;
  645. }
  646. #endif
  647. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  648. if (!amd_supported) {
  649. changed = true;
  650. name = SIMPLE_ENCODER_X264;
  651. return false;
  652. }
  653. } else if (strcmp(name, SIMPLE_ENCODER_AMD_AV1) == 0) {
  654. if (!amd_av1_supported) {
  655. changed = true;
  656. name = SIMPLE_ENCODER_X264;
  657. return false;
  658. }
  659. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_H264) == 0) {
  660. if (!apple_supported) {
  661. changed = true;
  662. name = SIMPLE_ENCODER_X264;
  663. return false;
  664. }
  665. #ifdef ENABLE_HEVC
  666. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  667. if (!apple_hevc_supported) {
  668. changed = true;
  669. name = SIMPLE_ENCODER_X264;
  670. return false;
  671. }
  672. #endif
  673. }
  674. return true;
  675. };
  676. if (!CheckEncoder(curStreamEncoder))
  677. config_set_string(activeConfiguration, "SimpleOutput", "StreamEncoder", curStreamEncoder);
  678. if (!CheckEncoder(curRecEncoder))
  679. config_set_string(activeConfiguration, "SimpleOutput", "RecEncoder", curRecEncoder);
  680. if (changed) {
  681. activeConfiguration.SaveSafe("tmp");
  682. }
  683. }