window-basic-main-profiles.cpp 17 KB

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