window-basic-main-profiles.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. const std::filesystem::path profileSettingsFile = std::filesystem::u8path(OBSProfileSettingsFile);
  234. if (!std::filesystem::exists(profilesPath)) {
  235. blog(LOG_WARNING, "Failed to get profiles config path");
  236. return;
  237. }
  238. for (const auto &entry : std::filesystem::directory_iterator(profilesPath)) {
  239. if (!entry.is_directory()) {
  240. continue;
  241. }
  242. const auto profileCandidate = entry.path() / profileSettingsFile;
  243. ConfigFile config;
  244. if (config.Open(profileCandidate.u8string().c_str(), CONFIG_OPEN_EXISTING) != CONFIG_SUCCESS) {
  245. continue;
  246. }
  247. std::string candidateName;
  248. const char *configName = config_get_string(config, "General", "Name");
  249. if (configName) {
  250. candidateName = configName;
  251. } else {
  252. candidateName = entry.path().filename().u8string();
  253. }
  254. foundProfiles.try_emplace(candidateName, OBSProfile{candidateName, entry.path().filename().u8string(),
  255. entry.path(), profileCandidate});
  256. }
  257. profiles.swap(foundProfiles);
  258. }
  259. const OBSProfile &OBSBasic::GetCurrentProfile() const
  260. {
  261. std::string currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  262. if (currentProfileName.empty()) {
  263. throw std::invalid_argument("No valid profile name in configuration Basic->Profile");
  264. }
  265. const auto &foundProfile = profiles.find(currentProfileName);
  266. if (foundProfile != profiles.end()) {
  267. return foundProfile->second;
  268. } else {
  269. throw std::invalid_argument("Profile not found in profile list: " + currentProfileName);
  270. }
  271. }
  272. std::optional<OBSProfile> OBSBasic::GetProfileByName(const std::string &profileName) const
  273. {
  274. auto foundProfile = profiles.find(profileName);
  275. if (foundProfile == profiles.end()) {
  276. return {};
  277. } else {
  278. return foundProfile->second;
  279. }
  280. }
  281. std::optional<OBSProfile> OBSBasic::GetProfileByDirectoryName(const std::string &directoryName) const
  282. {
  283. for (auto &[iterator, profile] : profiles) {
  284. if (profile.directoryName == directoryName) {
  285. return profile;
  286. }
  287. }
  288. return {};
  289. }
  290. // MARK: - Qt Slot Functions
  291. void OBSBasic::on_actionNewProfile_triggered()
  292. {
  293. bool useProfileWizard = config_get_bool(App()->GetUserConfig(), "Basic", "ConfigOnNewProfile");
  294. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  295. if (GetProfileByName(result.promptValue)) {
  296. return false;
  297. }
  298. return true;
  299. };
  300. const OBSPromptRequest request{Str("AddProfile.Title"), Str("AddProfile.Text"), "", true,
  301. Str("AddProfile.WizardCheckbox"), useProfileWizard};
  302. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  303. if (!result.success) {
  304. return;
  305. }
  306. try {
  307. SetupNewProfile(result.promptValue, result.optionValue);
  308. } catch (const std::invalid_argument &error) {
  309. blog(LOG_ERROR, "%s", error.what());
  310. } catch (const std::logic_error &error) {
  311. blog(LOG_ERROR, "%s", error.what());
  312. }
  313. }
  314. void OBSBasic::on_actionDupProfile_triggered()
  315. {
  316. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  317. if (GetProfileByName(result.promptValue)) {
  318. return false;
  319. }
  320. return true;
  321. };
  322. const OBSPromptRequest request{Str("AddProfile.Title"), Str("AddProfile.Text")};
  323. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  324. if (!result.success) {
  325. return;
  326. }
  327. try {
  328. SetupDuplicateProfile(result.promptValue);
  329. } catch (const std::invalid_argument &error) {
  330. blog(LOG_ERROR, "%s", error.what());
  331. } catch (const std::logic_error &error) {
  332. blog(LOG_ERROR, "%s", error.what());
  333. }
  334. }
  335. void OBSBasic::on_actionRenameProfile_triggered()
  336. {
  337. const std::string currentProfileName = config_get_string(App()->GetUserConfig(), "Basic", "Profile");
  338. const OBSPromptCallback profilePromptCallback = [this](const OBSPromptResult &result) {
  339. if (GetProfileByName(result.promptValue)) {
  340. return false;
  341. }
  342. return true;
  343. };
  344. const OBSPromptRequest request{Str("RenameProfile.Title"), Str("AddProfile.Text"), currentProfileName};
  345. OBSPromptResult result = PromptForName(request, profilePromptCallback);
  346. if (!result.success) {
  347. return;
  348. }
  349. try {
  350. SetupRenameProfile(result.promptValue);
  351. } catch (const std::invalid_argument &error) {
  352. blog(LOG_ERROR, "%s", error.what());
  353. } catch (const std::logic_error &error) {
  354. blog(LOG_ERROR, "%s", error.what());
  355. }
  356. }
  357. void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
  358. {
  359. if (profiles.size() < 2) {
  360. return;
  361. }
  362. OBSProfile currentProfile;
  363. try {
  364. currentProfile = GetCurrentProfile();
  365. if (!skipConfirmation) {
  366. const QString confirmationText =
  367. QTStr("ConfirmRemove.Text").arg(QString::fromStdString(currentProfile.name));
  368. const QMessageBox::StandardButton button =
  369. OBSMessageBox::question(this, QTStr("ConfirmRemove.Title"), confirmationText);
  370. if (button == QMessageBox::No) {
  371. return;
  372. }
  373. }
  374. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  375. profiles.erase(currentProfile.name);
  376. } catch (const std::invalid_argument &error) {
  377. blog(LOG_ERROR, "%s", error.what());
  378. } catch (const std::logic_error &error) {
  379. blog(LOG_ERROR, "%s", error.what());
  380. }
  381. const OBSProfile &newProfile = profiles.begin()->second;
  382. ActivateProfile(newProfile, true);
  383. RemoveProfile(currentProfile);
  384. #ifdef YOUTUBE_ENABLED
  385. if (YouTubeAppDock::IsYTServiceSelected() && !youtubeAppDock)
  386. NewYouTubeAppDock();
  387. #endif
  388. blog(LOG_INFO, "Switched to profile '%s' (%s)", newProfile.name.c_str(), newProfile.directoryName.c_str());
  389. blog(LOG_INFO, "------------------------------------------------");
  390. }
  391. void OBSBasic::on_actionImportProfile_triggered()
  392. {
  393. const QString home = QDir::homePath();
  394. const QString sourceDirectory = SelectDirectory(this, QTStr("Basic.MainMenu.Profile.Import"), home);
  395. if (!sourceDirectory.isEmpty() && !sourceDirectory.isNull()) {
  396. const std::filesystem::path sourcePath = std::filesystem::u8path(sourceDirectory.toStdString());
  397. const std::string directoryName = sourcePath.filename().string();
  398. if (auto profile = GetProfileByDirectoryName(directoryName)) {
  399. OBSMessageBox::warning(this, QTStr("Basic.MainMenu.Profile.Import"),
  400. QTStr("Basic.MainMenu.Profile.Exists"));
  401. return;
  402. }
  403. std::string destinationPathString;
  404. destinationPathString.reserve(App()->userProfilesLocation.u8string().size() + OBSProfilePath.size() +
  405. directoryName.size());
  406. destinationPathString.append(App()->userProfilesLocation.u8string())
  407. .append(OBSProfilePath)
  408. .append(directoryName);
  409. const std::filesystem::path destinationPath = std::filesystem::u8path(destinationPathString);
  410. try {
  411. std::filesystem::create_directory(destinationPath);
  412. } catch (const std::filesystem::filesystem_error &error) {
  413. blog(LOG_WARNING, "Failed to create profile directory '%s':\n%s", directoryName.c_str(),
  414. error.what());
  415. return;
  416. }
  417. const std::array<std::pair<std::string, bool>, 4> profileFiles{{
  418. {"basic.ini", true},
  419. {"service.json", false},
  420. {"streamEncoder.json", false},
  421. {"recordEncoder.json", false},
  422. }};
  423. for (auto &[file, isMandatory] : profileFiles) {
  424. const std::filesystem::path sourceFile = sourcePath / std::filesystem::u8path(file);
  425. if (!std::filesystem::exists(sourceFile)) {
  426. if (isMandatory) {
  427. blog(LOG_ERROR,
  428. "Failed to import profile from directory '%s' - necessary file '%s' not found",
  429. directoryName.c_str(), file.c_str());
  430. return;
  431. }
  432. continue;
  433. }
  434. const std::filesystem::path destinationFile = destinationPath / std::filesystem::u8path(file);
  435. try {
  436. std::filesystem::copy(sourceFile, destinationFile);
  437. } catch (const std::filesystem::filesystem_error &error) {
  438. blog(LOG_WARNING, "Failed to copy import file '%s' for profile '%s':\n%s", file.c_str(),
  439. directoryName.c_str(), error.what());
  440. return;
  441. }
  442. }
  443. RefreshProfiles(true);
  444. }
  445. }
  446. void OBSBasic::on_actionExportProfile_triggered()
  447. {
  448. const OBSProfile &currentProfile = GetCurrentProfile();
  449. const QString home = QDir::homePath();
  450. const QString destinationDirectory = SelectDirectory(this, QTStr("Basic.MainMenu.Profile.Export"), home);
  451. const std::array<std::string, 4> profileFiles{"basic.ini", "service.json", "streamEncoder.json",
  452. "recordEncoder.json"};
  453. if (!destinationDirectory.isEmpty() && !destinationDirectory.isNull()) {
  454. const std::filesystem::path sourcePath = currentProfile.path;
  455. const std::filesystem::path destinationPath =
  456. std::filesystem::u8path(destinationDirectory.toStdString()) /
  457. std::filesystem::u8path(currentProfile.directoryName);
  458. if (!std::filesystem::exists(destinationPath)) {
  459. std::filesystem::create_directory(destinationPath);
  460. }
  461. std::filesystem::copy_options copyOptions = std::filesystem::copy_options::overwrite_existing;
  462. for (auto &file : profileFiles) {
  463. const std::filesystem::path sourceFile = sourcePath / std::filesystem::u8path(file);
  464. if (!std::filesystem::exists(sourceFile)) {
  465. continue;
  466. }
  467. const std::filesystem::path destinationFile = destinationPath / std::filesystem::u8path(file);
  468. try {
  469. std::filesystem::copy(sourceFile, destinationFile, copyOptions);
  470. } catch (const std::filesystem::filesystem_error &error) {
  471. blog(LOG_WARNING, "Failed to copy export file '%s' for profile '%s'\n%s", file.c_str(),
  472. currentProfile.name.c_str(), error.what());
  473. return;
  474. }
  475. }
  476. }
  477. }
  478. // MARK: - Profile Management Helper Functions
  479. void OBSBasic::ActivateProfile(const OBSProfile &profile, bool reset)
  480. {
  481. ConfigFile config;
  482. if (config.Open(profile.profileFile.u8string().c_str(), CONFIG_OPEN_ALWAYS) != CONFIG_SUCCESS) {
  483. throw std::logic_error("failed to open configuration file of new profile: " +
  484. profile.profileFile.string());
  485. }
  486. config_set_string(config, "General", "Name", profile.name.c_str());
  487. config.SaveSafe("tmp");
  488. std::vector<std::string> restartRequirements;
  489. if (activeConfiguration) {
  490. Auth::Save();
  491. if (reset) {
  492. auth.reset();
  493. DestroyPanelCookieManager();
  494. #ifdef YOUTUBE_ENABLED
  495. if (youtubeAppDock) {
  496. DeleteYouTubeAppDock();
  497. }
  498. #endif
  499. }
  500. restartRequirements = GetRestartRequirements(config);
  501. activeConfiguration.SaveSafe("tmp");
  502. }
  503. activeConfiguration.Swap(config);
  504. config_set_string(App()->GetUserConfig(), "Basic", "Profile", profile.name.c_str());
  505. config_set_string(App()->GetUserConfig(), "Basic", "ProfileDir", profile.directoryName.c_str());
  506. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  507. InitBasicConfigDefaults();
  508. InitBasicConfigDefaults2();
  509. if (reset) {
  510. ResetProfileData();
  511. }
  512. CheckForSimpleModeX264Fallback();
  513. RefreshProfiles();
  514. UpdateTitleBar();
  515. UpdateVolumeControlsDecayRate();
  516. Auth::Load();
  517. OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  518. if (!restartRequirements.empty()) {
  519. std::string requirements = std::accumulate(
  520. std::next(restartRequirements.begin()), restartRequirements.end(), restartRequirements[0],
  521. [](std::string a, std::string b) { return std::move(a) + "\n" + b; });
  522. QMessageBox::StandardButton button = OBSMessageBox::question(
  523. this, QTStr("Restart"), QTStr("LoadProfileNeedsRestart").arg(requirements.c_str()));
  524. if (button == QMessageBox::Yes) {
  525. restart = true;
  526. close();
  527. }
  528. }
  529. }
  530. void OBSBasic::ResetProfileData()
  531. {
  532. ResetVideo();
  533. service = nullptr;
  534. InitService();
  535. ResetOutputs();
  536. ClearHotkeys();
  537. CreateHotkeys();
  538. /* load audio monitoring */
  539. if (obs_audio_monitoring_available()) {
  540. const char *device_name = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceName");
  541. const char *device_id = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceId");
  542. obs_set_audio_monitoring_device(device_name, device_id);
  543. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s", device_name, device_id);
  544. }
  545. }
  546. std::vector<std::string> OBSBasic::GetRestartRequirements(const ConfigFile &config) const
  547. {
  548. std::vector<std::string> result;
  549. const char *oldSpeakers = config_get_string(activeConfiguration, "Audio", "ChannelSetup");
  550. const char *newSpeakers = config_get_string(config, "Audio", "ChannelSetup");
  551. uint64_t oldSampleRate = config_get_uint(activeConfiguration, "Audio", "SampleRate");
  552. uint64_t newSampleRate = config_get_uint(config, "Audio", "SampleRate");
  553. if (oldSpeakers != NULL && newSpeakers != NULL) {
  554. if (std::string_view{oldSpeakers} != std::string_view{newSpeakers}) {
  555. result.emplace_back(Str("Basic.Settings.Audio.Channels"));
  556. }
  557. }
  558. if (oldSampleRate != 0 && newSampleRate != 0) {
  559. if (oldSampleRate != newSampleRate) {
  560. result.emplace_back(Str("Basic.Settings.Audio.SampleRate"));
  561. }
  562. }
  563. return result;
  564. }
  565. void OBSBasic::CheckForSimpleModeX264Fallback()
  566. {
  567. const char *curStreamEncoder = config_get_string(activeConfiguration, "SimpleOutput", "StreamEncoder");
  568. const char *curRecEncoder = config_get_string(activeConfiguration, "SimpleOutput", "RecEncoder");
  569. bool qsv_supported = false;
  570. bool qsv_av1_supported = false;
  571. bool amd_supported = false;
  572. bool nve_supported = false;
  573. #ifdef ENABLE_HEVC
  574. bool amd_hevc_supported = false;
  575. bool nve_hevc_supported = false;
  576. bool apple_hevc_supported = false;
  577. #endif
  578. bool amd_av1_supported = false;
  579. bool apple_supported = false;
  580. bool changed = false;
  581. size_t idx = 0;
  582. const char *id;
  583. while (obs_enum_encoder_types(idx++, &id)) {
  584. if (strcmp(id, "h264_texture_amf") == 0)
  585. amd_supported = true;
  586. else if (strcmp(id, "obs_qsv11") == 0)
  587. qsv_supported = true;
  588. else if (strcmp(id, "obs_qsv11_av1") == 0)
  589. qsv_av1_supported = true;
  590. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  591. nve_supported = true;
  592. #ifdef ENABLE_HEVC
  593. else if (strcmp(id, "h265_texture_amf") == 0)
  594. amd_hevc_supported = true;
  595. else if (strcmp(id, "ffmpeg_hevc_nvenc") == 0)
  596. nve_hevc_supported = true;
  597. #endif
  598. else if (strcmp(id, "av1_texture_amf") == 0)
  599. amd_av1_supported = true;
  600. else if (strcmp(id, "com.apple.videotoolbox.videoencoder.ave.avc") == 0)
  601. apple_supported = true;
  602. #ifdef ENABLE_HEVC
  603. else if (strcmp(id, "com.apple.videotoolbox.videoencoder.ave.hevc") == 0)
  604. apple_hevc_supported = true;
  605. #endif
  606. }
  607. auto CheckEncoder = [&](const char *&name) {
  608. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  609. if (!qsv_supported) {
  610. changed = true;
  611. name = SIMPLE_ENCODER_X264;
  612. return false;
  613. }
  614. } else if (strcmp(name, SIMPLE_ENCODER_QSV_AV1) == 0) {
  615. if (!qsv_av1_supported) {
  616. changed = true;
  617. name = SIMPLE_ENCODER_X264;
  618. return false;
  619. }
  620. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  621. if (!nve_supported) {
  622. changed = true;
  623. name = SIMPLE_ENCODER_X264;
  624. return false;
  625. }
  626. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  627. if (!nve_supported) {
  628. changed = true;
  629. name = SIMPLE_ENCODER_X264;
  630. return false;
  631. }
  632. #ifdef ENABLE_HEVC
  633. } else if (strcmp(name, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  634. if (!amd_hevc_supported) {
  635. changed = true;
  636. name = SIMPLE_ENCODER_X264;
  637. return false;
  638. }
  639. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  640. if (!nve_hevc_supported) {
  641. changed = true;
  642. name = SIMPLE_ENCODER_X264;
  643. return false;
  644. }
  645. #endif
  646. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  647. if (!amd_supported) {
  648. changed = true;
  649. name = SIMPLE_ENCODER_X264;
  650. return false;
  651. }
  652. } else if (strcmp(name, SIMPLE_ENCODER_AMD_AV1) == 0) {
  653. if (!amd_av1_supported) {
  654. changed = true;
  655. name = SIMPLE_ENCODER_X264;
  656. return false;
  657. }
  658. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_H264) == 0) {
  659. if (!apple_supported) {
  660. changed = true;
  661. name = SIMPLE_ENCODER_X264;
  662. return false;
  663. }
  664. #ifdef ENABLE_HEVC
  665. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  666. if (!apple_hevc_supported) {
  667. changed = true;
  668. name = SIMPLE_ENCODER_X264;
  669. return false;
  670. }
  671. #endif
  672. }
  673. return true;
  674. };
  675. if (!CheckEncoder(curStreamEncoder))
  676. config_set_string(activeConfiguration, "SimpleOutput", "StreamEncoder", curStreamEncoder);
  677. if (!CheckEncoder(curRecEncoder))
  678. config_set_string(activeConfiguration, "SimpleOutput", "RecEncoder", curRecEncoder);
  679. if (changed) {
  680. activeConfiguration.SaveSafe("tmp");
  681. }
  682. }