window-basic-main-profiles.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /******************************************************************************
  2. Copyright (C) 2015 by Hugh 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 ProfileExists(const char *findName)
  64. {
  65. bool found = false;
  66. auto func = [&](const char *name, const char *) {
  67. if (strcmp(name, findName) == 0) {
  68. found = true;
  69. return false;
  70. }
  71. return true;
  72. };
  73. EnumProfiles(func);
  74. return found;
  75. }
  76. static bool GetProfileName(QWidget *parent, std::string &name,
  77. std::string &file, const char *title,
  78. const char *text, const bool showWizard,
  79. bool &wizardChecked, const char *oldName = nullptr)
  80. {
  81. char path[512];
  82. int ret;
  83. for (;;) {
  84. bool success = false;
  85. if (showWizard) {
  86. success = NameDialog::AskForNameWithOption(
  87. parent, title, text, name,
  88. QTStr("AddProfile.WizardCheckbox"),
  89. wizardChecked, QT_UTF8(oldName));
  90. } else {
  91. success = NameDialog::AskForName(
  92. parent, title, text, name, QT_UTF8(oldName));
  93. }
  94. if (!success) {
  95. return false;
  96. }
  97. if (name.empty()) {
  98. OBSMessageBox::warning(parent,
  99. QTStr("NoNameEntered.Title"),
  100. QTStr("NoNameEntered.Text"));
  101. continue;
  102. }
  103. if (ProfileExists(name.c_str())) {
  104. OBSMessageBox::warning(parent,
  105. QTStr("NameExists.Title"),
  106. QTStr("NameExists.Text"));
  107. continue;
  108. }
  109. break;
  110. }
  111. if (!GetFileSafeName(name.c_str(), file)) {
  112. blog(LOG_WARNING, "Failed to create safe file name for '%s'",
  113. name.c_str());
  114. return false;
  115. }
  116. ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
  117. if (ret <= 0) {
  118. blog(LOG_WARNING, "Failed to get profiles config path");
  119. return false;
  120. }
  121. file.insert(0, path);
  122. if (!GetClosestUnusedFileName(file, nullptr)) {
  123. blog(LOG_WARNING, "Failed to get closest file name for %s",
  124. file.c_str());
  125. return false;
  126. }
  127. file.erase(0, ret);
  128. return true;
  129. }
  130. static bool CopyProfile(const char *fromPartial, const char *to)
  131. {
  132. os_glob_t *glob;
  133. char path[514];
  134. char dir[512];
  135. int ret;
  136. ret = GetConfigPath(dir, sizeof(dir), "obs-studio/basic/profiles/");
  137. if (ret <= 0) {
  138. blog(LOG_WARNING, "Failed to get profiles config path");
  139. return false;
  140. }
  141. snprintf(path, sizeof(path), "%s%s/*", dir, fromPartial);
  142. if (os_glob(path, 0, &glob) != 0) {
  143. blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
  144. return false;
  145. }
  146. for (size_t i = 0; i < glob->gl_pathc; i++) {
  147. const char *filePath = glob->gl_pathv[i].path;
  148. if (glob->gl_pathv[i].directory)
  149. continue;
  150. ret = snprintf(path, sizeof(path), "%s/%s", to,
  151. strrchr(filePath, '/') + 1);
  152. if (ret > 0) {
  153. if (os_copyfile(filePath, path) != 0) {
  154. blog(LOG_WARNING,
  155. "CopyProfile: Failed to "
  156. "copy file %s to %s",
  157. filePath, path);
  158. }
  159. }
  160. }
  161. os_globfree(glob);
  162. return true;
  163. }
  164. bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
  165. const char *init_text, bool rename)
  166. {
  167. std::string newName;
  168. std::string newDir;
  169. std::string newPath;
  170. ConfigFile config;
  171. bool showWizardChecked = config_get_bool(App()->GlobalConfig(), "Basic",
  172. "ConfigOnNewProfile");
  173. if (!GetProfileName(this, newName, newDir, title, text, create_new,
  174. showWizardChecked, init_text))
  175. return false;
  176. if (create_new) {
  177. config_set_bool(App()->GlobalConfig(), "Basic",
  178. "ConfigOnNewProfile", showWizardChecked);
  179. }
  180. std::string curDir =
  181. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  182. char baseDir[512];
  183. int ret = GetConfigPath(baseDir, sizeof(baseDir),
  184. "obs-studio/basic/profiles/");
  185. if (ret <= 0) {
  186. blog(LOG_WARNING, "Failed to get profiles config path");
  187. return false;
  188. }
  189. newPath = baseDir;
  190. newPath += newDir;
  191. if (os_mkdir(newPath.c_str()) < 0) {
  192. blog(LOG_WARNING, "Failed to create profile directory '%s'",
  193. newDir.c_str());
  194. return false;
  195. }
  196. if (!create_new)
  197. CopyProfile(curDir.c_str(), newPath.c_str());
  198. newPath += "/basic.ini";
  199. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  200. blog(LOG_ERROR, "Failed to open new config file '%s'",
  201. newDir.c_str());
  202. return false;
  203. }
  204. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  205. newName.c_str());
  206. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
  207. newDir.c_str());
  208. Auth::Save();
  209. if (create_new) {
  210. auth.reset();
  211. DestroyPanelCookieManager();
  212. } else if (!rename) {
  213. DuplicateCurrentCookieProfile(config);
  214. }
  215. config_set_string(config, "General", "Name", newName.c_str());
  216. basicConfig.SaveSafe("tmp");
  217. config.SaveSafe("tmp");
  218. config.Swap(basicConfig);
  219. InitBasicConfigDefaults();
  220. InitBasicConfigDefaults2();
  221. RefreshProfiles();
  222. if (create_new)
  223. ResetProfileData();
  224. blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
  225. create_new ? "clean" : "duplicate", newDir.c_str());
  226. blog(LOG_INFO, "------------------------------------------------");
  227. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  228. UpdateTitleBar();
  229. // Run auto configuration setup wizard when a new profile is made to assist
  230. // setting up blank settings
  231. if (create_new && showWizardChecked) {
  232. AutoConfig wizard(this);
  233. wizard.setModal(true);
  234. wizard.show();
  235. wizard.exec();
  236. }
  237. if (api) {
  238. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  239. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  240. }
  241. return true;
  242. }
  243. void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
  244. {
  245. char profilePath[512];
  246. char basePath[512];
  247. int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
  248. if (ret <= 0) {
  249. blog(LOG_WARNING, "Failed to get profiles config path");
  250. return;
  251. }
  252. ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
  253. if (ret <= 0) {
  254. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  255. profileDir);
  256. return;
  257. }
  258. os_glob_t *glob;
  259. if (os_glob(profilePath, 0, &glob) != 0) {
  260. blog(LOG_WARNING, "Failed to glob profile dir '%s'",
  261. profileDir);
  262. return;
  263. }
  264. for (size_t i = 0; i < glob->gl_pathc; i++) {
  265. const char *filePath = glob->gl_pathv[i].path;
  266. if (glob->gl_pathv[i].directory)
  267. continue;
  268. os_unlink(filePath);
  269. }
  270. os_globfree(glob);
  271. ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
  272. if (ret <= 0) {
  273. blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
  274. profileDir);
  275. return;
  276. }
  277. os_rmdir(profilePath);
  278. blog(LOG_INFO, "------------------------------------------------");
  279. blog(LOG_INFO, "Removed profile '%s' (%s)", profileName, profileDir);
  280. blog(LOG_INFO, "------------------------------------------------");
  281. }
  282. void OBSBasic::RefreshProfiles()
  283. {
  284. QList<QAction *> menuActions = ui->profileMenu->actions();
  285. int count = 0;
  286. for (int i = 0; i < menuActions.count(); i++) {
  287. QVariant v = menuActions[i]->property("file_name");
  288. if (v.typeName() != nullptr)
  289. delete menuActions[i];
  290. }
  291. const char *curName =
  292. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  293. auto addProfile = [&](const char *name, const char *path) {
  294. std::string file = strrchr(path, '/') + 1;
  295. QAction *action = new QAction(QT_UTF8(name), this);
  296. action->setProperty("file_name", QT_UTF8(path));
  297. connect(action, &QAction::triggered, this,
  298. &OBSBasic::ChangeProfile);
  299. action->setCheckable(true);
  300. action->setChecked(strcmp(name, curName) == 0);
  301. ui->profileMenu->addAction(action);
  302. count++;
  303. return true;
  304. };
  305. EnumProfiles(addProfile);
  306. ui->actionRemoveProfile->setEnabled(count > 1);
  307. }
  308. void OBSBasic::ResetProfileData()
  309. {
  310. ResetVideo();
  311. service = nullptr;
  312. InitService();
  313. ResetOutputs();
  314. ClearHotkeys();
  315. CreateHotkeys();
  316. /* load audio monitoring */
  317. #if defined(_WIN32) || defined(__APPLE__) || HAVE_PULSEAUDIO
  318. const char *device_name =
  319. config_get_string(basicConfig, "Audio", "MonitoringDeviceName");
  320. const char *device_id =
  321. config_get_string(basicConfig, "Audio", "MonitoringDeviceId");
  322. obs_set_audio_monitoring_device(device_name, device_id);
  323. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
  324. device_name, device_id);
  325. #endif
  326. }
  327. void OBSBasic::on_actionNewProfile_triggered()
  328. {
  329. AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
  330. }
  331. void OBSBasic::on_actionDupProfile_triggered()
  332. {
  333. AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
  334. }
  335. void OBSBasic::on_actionRenameProfile_triggered()
  336. {
  337. std::string curDir =
  338. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  339. std::string curName =
  340. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  341. /* Duplicate and delete in case there are any issues in the process */
  342. bool success = AddProfile(false, Str("RenameProfile.Title"),
  343. Str("AddProfile.Text"), curName.c_str(),
  344. true);
  345. if (success) {
  346. DeleteProfile(curName.c_str(), curDir.c_str());
  347. RefreshProfiles();
  348. }
  349. if (api) {
  350. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  351. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  352. }
  353. }
  354. void OBSBasic::on_actionRemoveProfile_triggered()
  355. {
  356. std::string newName;
  357. std::string newPath;
  358. ConfigFile config;
  359. std::string oldDir =
  360. config_get_string(App()->GlobalConfig(), "Basic", "ProfileDir");
  361. std::string oldName =
  362. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  363. auto cb = [&](const char *name, const char *filePath) {
  364. if (strcmp(oldName.c_str(), name) != 0) {
  365. newName = name;
  366. newPath = filePath;
  367. return false;
  368. }
  369. return true;
  370. };
  371. EnumProfiles(cb);
  372. /* this should never be true due to menu item being grayed out */
  373. if (newPath.empty())
  374. return;
  375. QString text = QTStr("ConfirmRemove.Text");
  376. text.replace("$1", QT_UTF8(oldName.c_str()));
  377. QMessageBox::StandardButton button = OBSMessageBox::question(
  378. this, QTStr("ConfirmRemove.Title"), text);
  379. if (button == QMessageBox::No)
  380. return;
  381. size_t newPath_len = newPath.size();
  382. newPath += "/basic.ini";
  383. if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  384. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  385. newPath.c_str());
  386. return;
  387. }
  388. newPath.resize(newPath_len);
  389. const char *newDir = strrchr(newPath.c_str(), '/') + 1;
  390. config_set_string(App()->GlobalConfig(), "Basic", "Profile",
  391. newName.c_str());
  392. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  393. Auth::Save();
  394. auth.reset();
  395. DeleteCookies();
  396. DestroyPanelCookieManager();
  397. config.Swap(basicConfig);
  398. InitBasicConfigDefaults();
  399. InitBasicConfigDefaults2();
  400. ResetProfileData();
  401. DeleteProfile(oldName.c_str(), oldDir.c_str());
  402. RefreshProfiles();
  403. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  404. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName.c_str(),
  405. newDir);
  406. blog(LOG_INFO, "------------------------------------------------");
  407. UpdateTitleBar();
  408. Auth::Load();
  409. if (api) {
  410. api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
  411. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  412. }
  413. }
  414. void OBSBasic::on_actionImportProfile_triggered()
  415. {
  416. char path[512];
  417. QString home = QDir::homePath();
  418. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  419. if (ret <= 0) {
  420. blog(LOG_WARNING, "Failed to get profile config path");
  421. return;
  422. }
  423. QString dir = SelectDirectory(
  424. this, QTStr("Basic.MainMenu.Profile.Import"), home);
  425. if (!dir.isEmpty() && !dir.isNull()) {
  426. QString inputPath = QString::fromUtf8(path);
  427. QFileInfo finfo(dir);
  428. QString directory = finfo.fileName();
  429. QString profileDir = inputPath + directory;
  430. if (ProfileExists(directory.toStdString().c_str())) {
  431. OBSMessageBox::warning(
  432. this, QTStr("Basic.MainMenu.Profile.Import"),
  433. QTStr("Basic.MainMenu.Profile.Exists"));
  434. } else if (os_mkdir(profileDir.toStdString().c_str()) < 0) {
  435. blog(LOG_WARNING,
  436. "Failed to create profile directory '%s'",
  437. directory.toStdString().c_str());
  438. } else {
  439. QFile::copy(dir + "/basic.ini",
  440. profileDir + "/basic.ini");
  441. QFile::copy(dir + "/service.json",
  442. profileDir + "/service.json");
  443. QFile::copy(dir + "/streamEncoder.json",
  444. profileDir + "/streamEncoder.json");
  445. QFile::copy(dir + "/recordEncoder.json",
  446. profileDir + "/recordEncoder.json");
  447. RefreshProfiles();
  448. }
  449. }
  450. }
  451. void OBSBasic::on_actionExportProfile_triggered()
  452. {
  453. char path[512];
  454. QString home = QDir::homePath();
  455. QString currentProfile = QString::fromUtf8(config_get_string(
  456. App()->GlobalConfig(), "Basic", "ProfileDir"));
  457. int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
  458. if (ret <= 0) {
  459. blog(LOG_WARNING, "Failed to get profile config path");
  460. return;
  461. }
  462. QString dir = SelectDirectory(
  463. this, QTStr("Basic.MainMenu.Profile.Export"), home);
  464. if (!dir.isEmpty() && !dir.isNull()) {
  465. QString outputDir = dir + "/" + currentProfile;
  466. QString inputPath = QString::fromUtf8(path);
  467. QDir folder(outputDir);
  468. if (!folder.exists()) {
  469. folder.mkpath(outputDir);
  470. } else {
  471. if (QFile::exists(outputDir + "/basic.ini"))
  472. QFile::remove(outputDir + "/basic.ini");
  473. if (QFile::exists(outputDir + "/service.json"))
  474. QFile::remove(outputDir + "/service.json");
  475. if (QFile::exists(outputDir + "/streamEncoder.json"))
  476. QFile::remove(outputDir +
  477. "/streamEncoder.json");
  478. if (QFile::exists(outputDir + "/recordEncoder.json"))
  479. QFile::remove(outputDir +
  480. "/recordEncoder.json");
  481. }
  482. QFile::copy(inputPath + currentProfile + "/basic.ini",
  483. outputDir + "/basic.ini");
  484. QFile::copy(inputPath + currentProfile + "/service.json",
  485. outputDir + "/service.json");
  486. QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
  487. outputDir + "/streamEncoder.json");
  488. QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
  489. outputDir + "/recordEncoder.json");
  490. }
  491. }
  492. void OBSBasic::ChangeProfile()
  493. {
  494. QAction *action = reinterpret_cast<QAction *>(sender());
  495. ConfigFile config;
  496. std::string path;
  497. if (!action)
  498. return;
  499. path = QT_TO_UTF8(action->property("file_name").value<QString>());
  500. if (path.empty())
  501. return;
  502. const char *oldName =
  503. config_get_string(App()->GlobalConfig(), "Basic", "Profile");
  504. if (action->text().compare(QT_UTF8(oldName)) == 0) {
  505. action->setChecked(true);
  506. return;
  507. }
  508. size_t path_len = path.size();
  509. path += "/basic.ini";
  510. if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
  511. blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
  512. path.c_str());
  513. return;
  514. }
  515. path.resize(path_len);
  516. const char *newName = config_get_string(config, "General", "Name");
  517. const char *newDir = strrchr(path.c_str(), '/') + 1;
  518. config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
  519. config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir", newDir);
  520. Auth::Save();
  521. auth.reset();
  522. DestroyPanelCookieManager();
  523. config.Swap(basicConfig);
  524. InitBasicConfigDefaults();
  525. InitBasicConfigDefaults2();
  526. ResetProfileData();
  527. RefreshProfiles();
  528. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  529. UpdateTitleBar();
  530. Auth::Load();
  531. CheckForSimpleModeX264Fallback();
  532. blog(LOG_INFO, "Switched to profile '%s' (%s)", newName, newDir);
  533. blog(LOG_INFO, "------------------------------------------------");
  534. if (api)
  535. api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
  536. }
  537. void OBSBasic::CheckForSimpleModeX264Fallback()
  538. {
  539. const char *curStreamEncoder =
  540. config_get_string(basicConfig, "SimpleOutput", "StreamEncoder");
  541. const char *curRecEncoder =
  542. config_get_string(basicConfig, "SimpleOutput", "RecEncoder");
  543. bool qsv_supported = false;
  544. bool amd_supported = false;
  545. bool nve_supported = false;
  546. bool changed = false;
  547. size_t idx = 0;
  548. const char *id;
  549. while (obs_enum_encoder_types(idx++, &id)) {
  550. if (strcmp(id, "amd_amf_h264") == 0)
  551. amd_supported = true;
  552. else if (strcmp(id, "obs_qsv11") == 0)
  553. qsv_supported = true;
  554. else if (strcmp(id, "ffmpeg_nvenc") == 0)
  555. nve_supported = true;
  556. }
  557. auto CheckEncoder = [&](const char *&name) {
  558. if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
  559. if (!qsv_supported) {
  560. changed = true;
  561. name = SIMPLE_ENCODER_X264;
  562. return false;
  563. }
  564. } else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
  565. if (!nve_supported) {
  566. changed = true;
  567. name = SIMPLE_ENCODER_X264;
  568. return false;
  569. }
  570. } else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
  571. if (!amd_supported) {
  572. changed = true;
  573. name = SIMPLE_ENCODER_X264;
  574. return false;
  575. }
  576. }
  577. return true;
  578. };
  579. if (!CheckEncoder(curStreamEncoder))
  580. config_set_string(basicConfig, "SimpleOutput", "StreamEncoder",
  581. curStreamEncoder);
  582. if (!CheckEncoder(curRecEncoder))
  583. config_set_string(basicConfig, "SimpleOutput", "RecEncoder",
  584. curRecEncoder);
  585. if (changed)
  586. config_save_safe(basicConfig, "tmp", nullptr);
  587. }