1
0

window-basic-main-profiles.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 <obs.hpp>
  15. #include <util/platform.h>
  16. #include <util/util.hpp>
  17. #include <QMessageBox>
  18. #include <QVariant>
  19. #include <QFileDialog>
  20. #include "window-basic-main.hpp"
  21. #include "window-basic-auto-config.hpp"
  22. #include "window-namedialog.hpp"
  23. #include "qt-wrappers.hpp"
  24. extern void DestroyPanelCookieManager();
  25. extern void DuplicateCurrentCookieProfile(ConfigFile &config);
  26. extern void CheckExistingCookieId();
  27. extern void DeleteCookies();
  28. void EnumProfiles(std::function<bool(const char *, const char *)> &&cb)
  29. {
  30. char path[512];
  31. os_glob_t *glob;
  32. int ret = GetConfigPath(path, sizeof(path),
  33. "obs-studio/basic/profiles/*");
  34. if (ret <= 0) {
  35. blog(LOG_WARNING, "Failed to get profiles config path");
  36. return;
  37. }
  38. if (os_glob(path, 0, &glob) != 0) {
  39. blog(LOG_WARNING, "Failed to glob profiles");
  40. return;
  41. }
  42. for (size_t i = 0; i < glob->gl_pathc; i++) {
  43. const char *filePath = glob->gl_pathv[i].path;
  44. const char *dirName = strrchr(filePath, '/') + 1;
  45. if (!glob->gl_pathv[i].directory)
  46. continue;
  47. if (strcmp(dirName, ".") == 0 || strcmp(dirName, "..") == 0)
  48. continue;
  49. std::string file = filePath;
  50. file += "/basic.ini";
  51. ConfigFile config;
  52. int ret = config.Open(file.c_str(), CONFIG_OPEN_EXISTING);
  53. if (ret != CONFIG_SUCCESS)
  54. continue;
  55. const char *name = config_get_string(config, "General", "Name");
  56. if (!name)
  57. name = strrchr(filePath, '/') + 1;
  58. if (!cb(name, filePath))
  59. break;
  60. }
  61. os_globfree(glob);
  62. }
  63. static bool GetProfileDir(const char *findName, const char *&profileDir)
  64. {
  65. bool found = false;
  66. auto func = [&](const char *name, const char *path) {
  67. if (strcmp(name, findName) == 0) {
  68. found = true;
  69. profileDir = strrchr(path, '/') + 1;
  70. return false;
  71. }
  72. return true;
  73. };
  74. EnumProfiles(func);
  75. return found;
  76. }
  77. static bool ProfileExists(const char *findName)
  78. {
  79. const char *profileDir = nullptr;
  80. return GetProfileDir(findName, profileDir);
  81. }
  82. static bool AskForProfileName(QWidget *parent, std::string &name,
  83. const char *title, const char *text,
  84. const bool showWizard, bool &wizardChecked,
  85. const char *oldName = nullptr)
  86. {
  87. for (;;) {
  88. bool success = false;
  89. if (showWizard) {
  90. success = NameDialog::AskForNameWithOption(
  91. parent, title, text, name,
  92. QTStr("AddProfile.WizardCheckbox"),
  93. wizardChecked, QT_UTF8(oldName));
  94. } else {
  95. success = NameDialog::AskForName(
  96. parent, title, text, name, QT_UTF8(oldName));
  97. }
  98. if (!success) {
  99. return false;
  100. }
  101. if (name.empty()) {
  102. OBSMessageBox::warning(parent,
  103. QTStr("NoNameEntered.Title"),
  104. QTStr("NoNameEntered.Text"));
  105. continue;
  106. }
  107. if (ProfileExists(name.c_str())) {
  108. OBSMessageBox::warning(parent,
  109. QTStr("NameExists.Title"),
  110. QTStr("NameExists.Text"));
  111. continue;
  112. }
  113. break;
  114. }
  115. return true;
  116. }
  117. static bool FindSafeProfileDirName(const std::string &profileName,
  118. std::string &dirName)
  119. {
  120. char path[512];
  121. int ret;
  122. if (ProfileExists(profileName.c_str())) {
  123. blog(LOG_WARNING, "Profile '%s' exists", profileName.c_str());
  124. return false;
  125. }
  126. if (!GetFileSafeName(profileName.c_str(), dirName)) {
  127. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  128. profileName.c_str());
  129. return false;
  130. }
  131. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  132. if (ret <= 0) {
  133. blog(LOG_WARNING, "Failed to get profiles config path");
  134. return false;
  135. }
  136. dirName.insert(0, path);
  137. if (!GetClosestUnusedFileName(dirName, nullptr)) {
  138. blog(LOG_WARNING, "Failed to get closest file name for %s",
  139. dirName.c_str());
  140. return false;
  141. }
  142. dirName.erase(0, ret);
  143. return true;
  144. }
  145. static bool CopyProfile(const char *fromPartial, const char *to)
  146. {
  147. os_glob_t *glob;
  148. char path[514];
  149. char dir[512];
  150. int ret;
  151. ret = GetConfigPath(dir, sizeof(dir), "obs-studio/basic/profiles/");
  152. if (ret <= 0) {
  153. blog(LOG_WARNING, "Failed to get profiles config path");
  154. return false;
  155. }
  156. snprintf(path, sizeof(path), "%s%s/*", dir, fromPartial);
  157. if (os_glob(path, 0, &glob) != 0) {
  158. blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
  159. return false;
  160. }
  161. for (size_t i = 0; i < glob->gl_pathc; i++) {
  162. const char *filePath = glob->gl_pathv[i].path;
  163. if (glob->gl_pathv[i].directory)
  164. continue;
  165. ret = snprintf(path, sizeof(path), "%s/%s", to,
  166. strrchr(filePath, '/') + 1);
  167. if (ret > 0) {
  168. if (os_copyfile(filePath, path) != 0) {
  169. blog(LOG_WARNING,
  170. "CopyProfile: Failed to "
  171. "copy file %s to %s",
  172. filePath, path);
  173. }
  174. }
  175. }
  176. os_globfree(glob);
  177. return true;
  178. }
  179. static bool ProfileNeedsRestart(config_t *newConfig, QString &settings)
  180. {
  181. OBSBasic *main = OBSBasic::Get();
  182. const char *oldSpeakers =
  183. config_get_string(main->Config(), "Audio", "ChannelSetup");
  184. uint oldSampleRate =
  185. config_get_uint(main->Config(), "Audio", "SampleRate");
  186. const char *newSpeakers =
  187. config_get_string(newConfig, "Audio", "ChannelSetup");
  188. uint newSampleRate = config_get_uint(newConfig, "Audio", "SampleRate");
  189. auto appendSetting = [&settings](const char *name) {
  190. settings += QStringLiteral("\n") + QTStr(name);
  191. };
  192. bool result = false;
  193. if (oldSpeakers != NULL && newSpeakers != NULL) {
  194. result = strcmp(oldSpeakers, newSpeakers) != 0;
  195. appendSetting("Basic.Settings.Audio.Channels");
  196. }
  197. if (oldSampleRate != 0 && newSampleRate != 0) {
  198. result |= oldSampleRate != newSampleRate;
  199. appendSetting("Basic.Settings.Audio.SampleRate");
  200. }
  201. return result;
  202. }
  203. bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
  204. const char *init_text, bool rename)
  205. {
  206. std::string name;
  207. bool showWizardChecked = config_get_bool(App()->GlobalConfig(), "Basic",
  208. "ConfigOnNewProfile");
  209. if (!AskForProfileName(this, name, title, text, create_new,
  210. showWizardChecked, init_text))
  211. return false;
  212. return CreateProfile(name, create_new, showWizardChecked, rename);
  213. }
  214. bool OBSBasic::CreateProfile(const std::string &newName, bool create_new,
  215. bool showWizardChecked, bool rename)
  216. {
  217. std::string newDir;
  218. std::string newPath;
  219. ConfigFile config;
  220. if (!FindSafeProfileDirName(newName, newDir))
  221. return false;
  222. if (create_new) {
  223. config_set_bool(App()->GlobalConfig(), "Basic",
  224. "ConfigOnNewProfile", showWizardChecked);
  225. }
  226. std::string curDir =
  227. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  228. char baseDir[512];
  229. int ret = GetConfigPath(baseDir, sizeof(baseDir),
  230. "obs-studio/basic/profiles/");
  231. if (ret <= 0) {
  232. blog(LOG_WARNING, "Failed to get profiles config path");
  233. return false;
  234. }
  235. newPath = baseDir;
  236. newPath += newDir;
  237. if (os_mkdir(newPath.c_str()) < 0) {
  238. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  239. newDir.c_str());
  240. return false;
  241. }
  242. if (!create_new)
  243. CopyProfile(curDir.c_str(), newPath.c_str());
  244. newPath += "/basic.ini";
  245. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  246. blog(LOG_ERROR, "Failed to open new config file '%s'",
  247. newDir.c_str());
  248. return false;
  249. }
  250. if (api && !rename)
  251. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  252. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  253. newName.c_str());
  254. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  255. newDir.c_str());
  256. Auth::Save();
  257. if (create_new) {
  258. auth.reset();
  259. DestroyPanelCookieManager();
  260. #ifdef YOUTUBE_ENABLED
  261. if (youtubeAppDock)
  262. DeleteYouTubeAppDock();
  263. #endif
  264. } else if (!rename) {
  265. DuplicateCurrentCookieProfile(config);
  266. }
  267. config_set_string(config, "General", "Name", newName.c_str());
  268. basicConfig.SaveSafe("tmp");
  269. config.SaveSafe("tmp");
  270. config.Swap(basicConfig);
  271. InitBasicConfigDefaults();
  272. InitBasicConfigDefaults2();
  273. RefreshProfiles();
  274. if (create_new)
  275. ResetProfileData();
  276. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  277. create_new ? "clean" : "duplicate", newDir.c_str());
  278. blog(LOG_INFO, "------------------------------------------------");
  279. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  280. UpdateTitleBar();
  281. UpdateVolumeControlsDecayRate();
  282. Auth::Load();
  283. // Run auto configuration setup wizard when a new profile is made to assist
  284. // setting up blank settings
  285. if (create_new && showWizardChecked) {
  286. AutoConfig wizard(this);
  287. wizard.setModal(true);
  288. wizard.show();
  289. wizard.exec();
  290. }
  291. if (api && !rename) {
  292. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  293. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  294. }
  295. return true;
  296. }
  297. bool OBSBasic::NewProfile(const QString &name)
  298. {
  299. return CreateProfile(name.toStdString(), true, false, false);
  300. }
  301. bool OBSBasic::DuplicateProfile(const QString &name)
  302. {
  303. return CreateProfile(name.toStdString(), false, false, false);
  304. }
  305. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  306. {
  307. char profilePath[512];
  308. char basePath[512];
  309. int ret = GetConfigPath(basePath, sizeof(basePath),
  310. "obs-studio/basic/profiles");
  311. if (ret <= 0) {
  312. blog(LOG_WARNING, "Failed to get profiles config path");
  313. return;
  314. }
  315. ret = snprintf(profilePath, sizeof(profilePath), "%s/%s/*", basePath,
  316. profileDir);
  317. if (ret <= 0) {
  318. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  319. profileDir);
  320. return;
  321. }
  322. os_glob_t *glob;
  323. if (os_glob(profilePath, 0, &glob) != 0) {
  324. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  325. profileDir);
  326. return;
  327. }
  328. for (size_t i = 0; i < glob->gl_pathc; i++) {
  329. const char *filePath = glob->gl_pathv[i].path;
  330. if (glob->gl_pathv[i].directory)
  331. continue;
  332. os_unlink(filePath);
  333. }
  334. os_globfree(glob);
  335. ret = snprintf(profilePath, sizeof(profilePath), "%s/%s", basePath,
  336. profileDir);
  337. if (ret <= 0) {
  338. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  339. profileDir);
  340. return;
  341. }
  342. os_rmdir(profilePath);
  343. blog(LOG_INFO, "------------------------------------------------");
  344. blog(LOG_INFO, "Removed profile '%s' (%s)", profileName, profileDir);
  345. blog(LOG_INFO, "------------------------------------------------");
  346. }
  347. void OBSBasic::DeleteProfile(const QString &profileName)
  348. {
  349. std::string name = profileName.toStdString();
  350. const char *curName =
  351. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  352. if (strcmp(curName, name.c_str()) == 0) {
  353. on_actionRemoveProfile_triggered(true);
  354. return;
  355. }
  356. const char *profileDir = nullptr;
  357. if (!GetProfileDir(name.c_str(), profileDir)) {
  358. blog(LOG_WARNING, "Profile '%s' not found", name.c_str());
  359. return;
  360. }
  361. if (!profileDir) {
  362. blog(LOG_WARNING, "Failed to get profile dir for profile '%s'",
  363. name.c_str());
  364. return;
  365. }
  366. DeleteProfile(name.c_str(), profileDir);
  367. RefreshProfiles();
  368. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  369. if (api)
  370. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  371. }
  372. void OBSBasic::RefreshProfiles()
  373. {
  374. QList<QAction *> menuActions = ui->profileMenu->actions();
  375. int count = 0;
  376. for (int i = 0; i < menuActions.count(); i++) {
  377. QVariant v = menuActions[i]->property("file_name");
  378. if (v.typeName() != nullptr)
  379. delete menuActions[i];
  380. }
  381. const char *curName =
  382. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  383. auto addProfile = [&](const char *name, const char *path) {
  384. std::string file = strrchr(path, '/') + 1;
  385. QAction *action = new QAction(QT_UTF8(name), this);
  386. action->setProperty("file_name", QT_UTF8(path));
  387. connect(action, &QAction::triggered, this,
  388. &OBSBasic::ChangeProfile);
  389. action->setCheckable(true);
  390. action->setChecked(strcmp(name, curName) == 0);
  391. ui->profileMenu->addAction(action);
  392. count++;
  393. return true;
  394. };
  395. EnumProfiles(addProfile);
  396. ui->actionRemoveProfile->setEnabled(count > 1);
  397. }
  398. void OBSBasic::ResetProfileData()
  399. {
  400. ResetVideo();
  401. service = nullptr;
  402. InitService();
  403. ResetOutputs();
  404. ClearHotkeys();
  405. CreateHotkeys();
  406. /* load audio monitoring */
  407. if (obs_audio_monitoring_available()) {
  408. const char *device_name = config_get_string(
  409. basicConfig, "Audio", "MonitoringDeviceName");
  410. const char *device_id = config_get_string(basicConfig, "Audio",
  411. "MonitoringDeviceId");
  412. obs_set_audio_monitoring_device(device_name, device_id);
  413. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
  414. device_name, device_id);
  415. }
  416. }
  417. void OBSBasic::on_actionNewProfile_triggered()
  418. {
  419. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  420. }
  421. void OBSBasic::on_actionDupProfile_triggered()
  422. {
  423. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  424. }
  425. void OBSBasic::on_actionRenameProfile_triggered()
  426. {
  427. std::string curDir =
  428. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  429. std::string curName =
  430. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  431. /* Duplicate and delete in case there are any issues in the process */
  432. bool success = AddProfile(false, Str("RenameProfile.Title"),
  433. Str("AddProfile.Text"), curName.c_str(),
  434. true);
  435. if (success) {
  436. DeleteProfile(curName.c_str(), curDir.c_str());
  437. RefreshProfiles();
  438. }
  439. if (api)
  440. api->on_event(OBS_FRONTEND_EVENT_PROFILE_RENAMED);
  441. }
  442. void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
  443. {
  444. std::string newName;
  445. std::string newPath;
  446. ConfigFile config;
  447. std::string oldDir =
  448. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  449. std::string oldName =
  450. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  451. auto cb = [&](const char *name, const char *filePath) {
  452. if (strcmp(oldName.c_str(), name) != 0) {
  453. newName = name;
  454. newPath = filePath;
  455. return false;
  456. }
  457. return true;
  458. };
  459. EnumProfiles(cb);
  460. /* this should never be true due to menu item being grayed out */
  461. if (newPath.empty())
  462. return;
  463. if (!skipConfirmation) {
  464. QString text = QTStr("ConfirmRemove.Text")
  465. .arg(QT_UTF8(oldName.c_str()));
  466. QMessageBox::StandardButton button = OBSMessageBox::question(
  467. this, QTStr("ConfirmRemove.Title"), text);
  468. if (button == QMessageBox::No)
  469. return;
  470. }
  471. size_t newPath_len = newPath.size();
  472. newPath += "/basic.ini";
  473. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  474. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  475. newPath.c_str());
  476. return;
  477. }
  478. if (api)
  479. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  480. newPath.resize(newPath_len);
  481. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  482. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  483. newName.c_str());
  484. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  485. QString settingsRequiringRestart;
  486. bool needsRestart =
  487. ProfileNeedsRestart(config, settingsRequiringRestart);
  488. Auth::Save();
  489. auth.reset();
  490. DeleteCookies();
  491. DestroyPanelCookieManager();
  492. config.Swap(basicConfig);
  493. InitBasicConfigDefaults();
  494. InitBasicConfigDefaults2();
  495. ResetProfileData();
  496. DeleteProfile(oldName.c_str(), oldDir.c_str());
  497. RefreshProfiles();
  498. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  499. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName.c_str(),
  500. newDir);
  501. blog(LOG_INFO, "------------------------------------------------");
  502. UpdateTitleBar();
  503. UpdateVolumeControlsDecayRate();
  504. Auth::Load();
  505. if (api) {
  506. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  507. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  508. }
  509. if (needsRestart) {
  510. QMessageBox::StandardButton button = OBSMessageBox::question(
  511. this, QTStr("Restart"),
  512. QTStr("LoadProfileNeedsRestart")
  513. .arg(settingsRequiringRestart));
  514. if (button == QMessageBox::Yes) {
  515. restart = true;
  516. close();
  517. }
  518. }
  519. }
  520. void OBSBasic::on_actionImportProfile_triggered()
  521. {
  522. char path[512];
  523. QString home = QDir::homePath();
  524. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  525. if (ret <= 0) {
  526. blog(LOG_WARNING, "Failed to get profile config path");
  527. return;
  528. }
  529. QString dir = SelectDirectory(
  530. this, QTStr("Basic.MainMenu.Profile.Import"), home);
  531. if (!dir.isEmpty() && !dir.isNull()) {
  532. QString inputPath = QString::fromUtf8(path);
  533. QFileInfo finfo(dir);
  534. QString directory = finfo.fileName();
  535. QString profileDir = inputPath + directory;
  536. if (ProfileExists(directory.toStdString().c_str())) {
  537. OBSMessageBox::warning(
  538. this, QTStr("Basic.MainMenu.Profile.Import"),
  539. QTStr("Basic.MainMenu.Profile.Exists"));
  540. } else if (os_mkdir(profileDir.toStdString().c_str()) < 0) {
  541. blog(LOG_WARNING,
  542. "Failed to create profile directory '%s'",
  543. directory.toStdString().c_str());
  544. } else {
  545. QFile::copy(dir + "/basic.ini",
  546. profileDir + "/basic.ini");
  547. QFile::copy(dir + "/service.json",
  548. profileDir + "/service.json");
  549. QFile::copy(dir + "/streamEncoder.json",
  550. profileDir + "/streamEncoder.json");
  551. QFile::copy(dir + "/recordEncoder.json",
  552. profileDir + "/recordEncoder.json");
  553. RefreshProfiles();
  554. }
  555. }
  556. }
  557. void OBSBasic::on_actionExportProfile_triggered()
  558. {
  559. char path[512];
  560. QString home = QDir::homePath();
  561. QString currentProfile = QString::fromUtf8(config_get_string(
  562. App()->GlobalConfig(), "Basic", "ProfileDir"));
  563. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  564. if (ret <= 0) {
  565. blog(LOG_WARNING, "Failed to get profile config path");
  566. return;
  567. }
  568. QString dir = SelectDirectory(
  569. this, QTStr("Basic.MainMenu.Profile.Export"), home);
  570. if (!dir.isEmpty() && !dir.isNull()) {
  571. QString outputDir = dir + "/" + currentProfile;
  572. QString inputPath = QString::fromUtf8(path);
  573. QDir folder(outputDir);
  574. if (!folder.exists()) {
  575. folder.mkpath(outputDir);
  576. } else {
  577. if (QFile::exists(outputDir + "/basic.ini"))
  578. QFile::remove(outputDir + "/basic.ini");
  579. if (QFile::exists(outputDir + "/service.json"))
  580. QFile::remove(outputDir + "/service.json");
  581. if (QFile::exists(outputDir + "/streamEncoder.json"))
  582. QFile::remove(outputDir +
  583. "/streamEncoder.json");
  584. if (QFile::exists(outputDir + "/recordEncoder.json"))
  585. QFile::remove(outputDir +
  586. "/recordEncoder.json");
  587. }
  588. QFile::copy(inputPath + currentProfile + "/basic.ini",
  589. outputDir + "/basic.ini");
  590. QFile::copy(inputPath + currentProfile + "/service.json",
  591. outputDir + "/service.json");
  592. QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
  593. outputDir + "/streamEncoder.json");
  594. QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
  595. outputDir + "/recordEncoder.json");
  596. }
  597. }
  598. void OBSBasic::ChangeProfile()
  599. {
  600. QAction *action = reinterpret_cast<QAction *>(sender());
  601. ConfigFile config;
  602. std::string path;
  603. if (!action)
  604. return;
  605. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  606. if (path.empty())
  607. return;
  608. const char *oldName =
  609. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  610. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  611. action->setChecked(true);
  612. return;
  613. }
  614. size_t path_len = path.size();
  615. path += "/basic.ini";
  616. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  617. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  618. path.c_str());
  619. return;
  620. }
  621. if (api)
  622. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
  623. path.resize(path_len);
  624. const char *newName = config_get_string(config, "General", "Name");
  625. const char *newDir = strrchr(path.c_str(), '/') + 1;
  626. QString settingsRequiringRestart;
  627. bool needsRestart =
  628. ProfileNeedsRestart(config, settingsRequiringRestart);
  629. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  630. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  631. Auth::Save();
  632. auth.reset();
  633. DestroyPanelCookieManager();
  634. #ifdef YOUTUBE_ENABLED
  635. if (youtubeAppDock)
  636. DeleteYouTubeAppDock();
  637. #endif
  638. config.Swap(basicConfig);
  639. InitBasicConfigDefaults();
  640. InitBasicConfigDefaults2();
  641. ResetProfileData();
  642. RefreshProfiles();
  643. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  644. UpdateTitleBar();
  645. UpdateVolumeControlsDecayRate();
  646. Auth::Load();
  647. #ifdef YOUTUBE_ENABLED
  648. if (YouTubeAppDock::IsYTServiceSelected() && !youtubeAppDock)
  649. NewYouTubeAppDock();
  650. #endif
  651. CheckForSimpleModeX264Fallback();
  652. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName, newDir);
  653. blog(LOG_INFO, "------------------------------------------------");
  654. if (api)
  655. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  656. if (needsRestart) {
  657. QMessageBox::StandardButton button = OBSMessageBox::question(
  658. this, QTStr("Restart"),
  659. QTStr("LoadProfileNeedsRestart")
  660. .arg(settingsRequiringRestart));
  661. if (button == QMessageBox::Yes) {
  662. restart = true;
  663. close();
  664. }
  665. }
  666. }
  667. void OBSBasic::CheckForSimpleModeX264Fallback()
  668. {
  669. const char *curStreamEncoder =
  670. config_get_string(basicConfig, "SimpleOutput", "StreamEncoder");
  671. const char *curRecEncoder =
  672. config_get_string(basicConfig, "SimpleOutput", "RecEncoder");
  673. bool qsv_supported = false;
  674. bool qsv_av1_supported = false;
  675. bool amd_supported = false;
  676. bool nve_supported = false;
  677. #ifdef ENABLE_HEVC
  678. bool amd_hevc_supported = false;
  679. bool nve_hevc_supported = false;
  680. bool apple_hevc_supported = false;
  681. #endif
  682. bool amd_av1_supported = false;
  683. bool apple_supported = false;
  684. bool changed = false;
  685. size_t idx = 0;
  686. const char *id;
  687. while (obs_enum_encoder_types(idx++, &id)) {
  688. if (strcmp(id, "h264_texture_amf") == 0)
  689. amd_supported = true;
  690. else if (strcmp(id, "obs_qsv11") == 0)
  691. qsv_supported = true;
  692. else if (strcmp(id, "obs_qsv11_av1") == 0)
  693. qsv_av1_supported = true;
  694. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  695. nve_supported = true;
  696. #ifdef ENABLE_HEVC
  697. else if (strcmp(id, "h265_texture_amf") == 0)
  698. amd_hevc_supported = true;
  699. else if (strcmp(id, "ffmpeg_hevc_nvenc") == 0)
  700. nve_hevc_supported = true;
  701. #endif
  702. else if (strcmp(id, "av1_texture_amf") == 0)
  703. amd_av1_supported = true;
  704. else if (strcmp(id,
  705. "com.apple.videotoolbox.videoencoder.ave.avc") ==
  706. 0)
  707. apple_supported = true;
  708. #ifdef ENABLE_HEVC
  709. else if (strcmp(id,
  710. "com.apple.videotoolbox.videoencoder.ave.hevc") ==
  711. 0)
  712. apple_hevc_supported = true;
  713. #endif
  714. }
  715. auto CheckEncoder = [&](const char *&name) {
  716. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  717. if (!qsv_supported) {
  718. changed = true;
  719. name = SIMPLE_ENCODER_X264;
  720. return false;
  721. }
  722. } else if (strcmp(name, SIMPLE_ENCODER_QSV_AV1) == 0) {
  723. if (!qsv_av1_supported) {
  724. changed = true;
  725. name = SIMPLE_ENCODER_X264;
  726. return false;
  727. }
  728. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  729. if (!nve_supported) {
  730. changed = true;
  731. name = SIMPLE_ENCODER_X264;
  732. return false;
  733. }
  734. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_AV1) == 0) {
  735. if (!nve_supported) {
  736. changed = true;
  737. name = SIMPLE_ENCODER_X264;
  738. return false;
  739. }
  740. #ifdef ENABLE_HEVC
  741. } else if (strcmp(name, SIMPLE_ENCODER_AMD_HEVC) == 0) {
  742. if (!amd_hevc_supported) {
  743. changed = true;
  744. name = SIMPLE_ENCODER_X264;
  745. return false;
  746. }
  747. } else if (strcmp(name, SIMPLE_ENCODER_NVENC_HEVC) == 0) {
  748. if (!nve_hevc_supported) {
  749. changed = true;
  750. name = SIMPLE_ENCODER_X264;
  751. return false;
  752. }
  753. #endif
  754. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  755. if (!amd_supported) {
  756. changed = true;
  757. name = SIMPLE_ENCODER_X264;
  758. return false;
  759. }
  760. } else if (strcmp(name, SIMPLE_ENCODER_AMD_AV1) == 0) {
  761. if (!amd_av1_supported) {
  762. changed = true;
  763. name = SIMPLE_ENCODER_X264;
  764. return false;
  765. }
  766. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_H264) == 0) {
  767. if (!apple_supported) {
  768. changed = true;
  769. name = SIMPLE_ENCODER_X264;
  770. return false;
  771. }
  772. #ifdef ENABLE_HEVC
  773. } else if (strcmp(name, SIMPLE_ENCODER_APPLE_HEVC) == 0) {
  774. if (!apple_hevc_supported) {
  775. changed = true;
  776. name = SIMPLE_ENCODER_X264;
  777. return false;
  778. }
  779. #endif
  780. }
  781. return true;
  782. };
  783. if (!CheckEncoder(curStreamEncoder))
  784. config_set_string(basicConfig, "SimpleOutput", "StreamEncoder",
  785. curStreamEncoder);
  786. if (!CheckEncoder(curRecEncoder))
  787. config_set_string(basicConfig, "SimpleOutput", "RecEncoder",
  788. curRecEncoder);
  789. if (changed)
  790. config_save_safe(basicConfig, "tmp", nullptr);
  791. }