1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
/******************************************************************************
Copyright (C) 2015 by Hugh Bailey <obs.jim@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <obs.hpp>
#include <util/platform.h>
#include <util/util.hpp>
#include <QMessageBox>
#include <QVariant>
#include <QFileDialog>
#include "window-basic-main.hpp"
#include "window-namedialog.hpp"
#include "qt-wrappers.hpp"
void EnumProfiles(std::function<bool (const char *, const char *)> &&cb)
{
char path[512];
os_glob_t *glob;
int ret = GetConfigPath(path, sizeof(path),
"obs-studio/basic/profiles/*");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profiles config path");
return;
}
if (os_glob(path, 0, &glob) != 0) {
blog(LOG_WARNING, "Failed to glob profiles");
return;
}
for (size_t i = 0; i < glob->gl_pathc; i++) {
const char *filePath = glob->gl_pathv[i].path;
const char *dirName = strrchr(filePath, '/') + 1;
if (!glob->gl_pathv[i].directory)
continue;
if (strcmp(dirName, ".") == 0 ||
strcmp(dirName, "..") == 0)
continue;
std::string file = filePath;
file += "/basic.ini";
ConfigFile config;
int ret = config.Open(file.c_str(), CONFIG_OPEN_EXISTING);
if (ret != CONFIG_SUCCESS)
continue;
const char *name = config_get_string(config, "General", "Name");
if (!name)
name = strrchr(filePath, '/') + 1;
if (!cb(name, filePath))
break;
}
os_globfree(glob);
}
static bool ProfileExists(const char *findName)
{
bool found = false;
auto func = [&](const char *name, const char*)
{
if (strcmp(name, findName) == 0) {
found = true;
return false;
}
return true;
};
EnumProfiles(func);
return found;
}
static bool GetProfileName(QWidget *parent, std::string &name,
std::string &file, const char *title, const char *text,
const char *oldName = nullptr)
{
char path[512];
int ret;
for (;;) {
bool success = NameDialog::AskForName(parent, title, text,
name, QT_UTF8(oldName));
if (!success) {
return false;
}
if (name.empty()) {
OBSMessageBox::information(parent,
QTStr("NoNameEntered.Title"),
QTStr("NoNameEntered.Text"));
continue;
}
if (ProfileExists(name.c_str())) {
OBSMessageBox::information(parent,
QTStr("NameExists.Title"),
QTStr("NameExists.Text"));
continue;
}
break;
}
if (!GetFileSafeName(name.c_str(), file)) {
blog(LOG_WARNING, "Failed to create safe file name for '%s'",
name.c_str());
return false;
}
ret = GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profiles config path");
return false;
}
file.insert(0, path);
if (!GetClosestUnusedFileName(file, nullptr)) {
blog(LOG_WARNING, "Failed to get closest file name for %s",
file.c_str());
return false;
}
file.erase(0, ret);
return true;
}
static bool CopyProfile(const char *fromPartial, const char *to)
{
os_glob_t *glob;
char path[512];
char dir[512];
int ret;
ret = GetConfigPath(dir, sizeof(dir), "obs-studio/basic/profiles/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profiles config path");
return false;
}
snprintf(path, sizeof(path), "%s%s/*", dir, fromPartial);
if (os_glob(path, 0, &glob) != 0) {
blog(LOG_WARNING, "Failed to glob profile '%s'", fromPartial);
return false;
}
for (size_t i = 0; i < glob->gl_pathc; i++) {
const char *filePath = glob->gl_pathv[i].path;
if (glob->gl_pathv[i].directory)
continue;
ret = snprintf(path, sizeof(path), "%s/%s",
to, strrchr(filePath, '/') + 1);
if (ret > 0) {
if (os_copyfile(filePath, path) != 0) {
blog(LOG_WARNING, "CopyProfile: Failed to "
"copy file %s to %s",
filePath, path);
}
}
}
os_globfree(glob);
return true;
}
bool OBSBasic::AddProfile(bool create_new, const char *title, const char *text,
const char *init_text)
{
std::string newName;
std::string newDir;
std::string newPath;
ConfigFile config;
if (!GetProfileName(this, newName, newDir, title, text, init_text))
return false;
std::string curDir = config_get_string(App()->GlobalConfig(),
"Basic", "ProfileDir");
char baseDir[512];
int ret = GetConfigPath(baseDir, sizeof(baseDir),
"obs-studio/basic/profiles/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profiles config path");
return false;
}
newPath = baseDir;
newPath += newDir;
if (os_mkdir(newPath.c_str()) < 0) {
blog(LOG_WARNING, "Failed to create profile directory '%s'",
newDir.c_str());
return false;
}
if (!create_new)
CopyProfile(curDir.c_str(), newPath.c_str());
newPath += "/basic.ini";
if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
blog(LOG_ERROR, "Failed to open new config file '%s'",
newDir.c_str());
return false;
}
config_set_string(App()->GlobalConfig(), "Basic", "Profile",
newName.c_str());
config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
newDir.c_str());
config_set_string(config, "General", "Name", newName.c_str());
config.SaveSafe("tmp");
config.Swap(basicConfig);
InitBasicConfigDefaults();
RefreshProfiles();
if (create_new)
ResetProfileData();
blog(LOG_INFO, "Created profile '%s' (%s, %s)", newName.c_str(),
create_new ? "clean" : "duplicate", newDir.c_str());
blog(LOG_INFO, "------------------------------------------------");
config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
UpdateTitleBar();
if (api) {
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
return true;
}
void OBSBasic::DeleteProfile(const char *profileName, const char *profileDir)
{
char profilePath[512];
char basePath[512];
int ret = GetConfigPath(basePath, 512, "obs-studio/basic/profiles");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profiles config path");
return;
}
ret = snprintf(profilePath, 512, "%s/%s/*", basePath, profileDir);
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
profileDir);
return;
}
os_glob_t *glob;
if (os_glob(profilePath, 0, &glob) != 0) {
blog(LOG_WARNING, "Failed to glob profile dir '%s'",
profileDir);
return;
}
for (size_t i = 0; i < glob->gl_pathc; i++) {
const char *filePath = glob->gl_pathv[i].path;
if (glob->gl_pathv[i].directory)
continue;
os_unlink(filePath);
}
os_globfree(glob);
ret = snprintf(profilePath, 512, "%s/%s", basePath, profileDir);
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get path for profile dir '%s'",
profileDir);
return;
}
os_rmdir(profilePath);
blog(LOG_INFO, "------------------------------------------------");
blog(LOG_INFO, "Removed profile '%s' (%s)",
profileName, profileDir);
blog(LOG_INFO, "------------------------------------------------");
}
void OBSBasic::RefreshProfiles()
{
QList<QAction*> menuActions = ui->profileMenu->actions();
int count = 0;
for (int i = 0; i < menuActions.count(); i++) {
QVariant v = menuActions[i]->property("file_name");
if (v.typeName() != nullptr)
delete menuActions[i];
}
const char *curName = config_get_string(App()->GlobalConfig(),
"Basic", "Profile");
auto addProfile = [&](const char *name, const char *path)
{
std::string file = strrchr(path, '/') + 1;
QAction *action = new QAction(QT_UTF8(name), this);
action->setProperty("file_name", QT_UTF8(path));
connect(action, &QAction::triggered,
this, &OBSBasic::ChangeProfile);
action->setCheckable(true);
action->setChecked(strcmp(name, curName) == 0);
ui->profileMenu->addAction(action);
count++;
return true;
};
EnumProfiles(addProfile);
ui->actionRemoveProfile->setEnabled(count > 1);
}
void OBSBasic::ResetProfileData()
{
ResetVideo();
service = nullptr;
InitService();
ResetOutputs();
ClearHotkeys();
CreateHotkeys();
/* load audio monitoring */
#if defined(_WIN32) || defined(__APPLE__) || HAVE_PULSEAUDIO
const char *device_name = config_get_string(basicConfig, "Audio",
"MonitoringDeviceName");
const char *device_id = config_get_string(basicConfig, "Audio",
"MonitoringDeviceId");
obs_set_audio_monitoring_device(device_name, device_id);
blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
device_name, device_id);
#endif
}
void OBSBasic::on_actionNewProfile_triggered()
{
AddProfile(true, Str("AddProfile.Title"), Str("AddProfile.Text"));
}
void OBSBasic::on_actionDupProfile_triggered()
{
AddProfile(false, Str("AddProfile.Title"), Str("AddProfile.Text"));
}
void OBSBasic::on_actionRenameProfile_triggered()
{
std::string curDir = config_get_string(App()->GlobalConfig(),
"Basic", "ProfileDir");
std::string curName = config_get_string(App()->GlobalConfig(),
"Basic", "Profile");
/* Duplicate and delete in case there are any issues in the process */
bool success = AddProfile(false, Str("RenameProfile.Title"),
Str("AddProfile.Text"), curName.c_str());
if (success) {
DeleteProfile(curName.c_str(), curDir.c_str());
RefreshProfiles();
}
if (api) {
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
}
void OBSBasic::on_actionRemoveProfile_triggered()
{
std::string newName;
std::string newPath;
ConfigFile config;
std::string oldDir = config_get_string(App()->GlobalConfig(),
"Basic", "ProfileDir");
std::string oldName = config_get_string(App()->GlobalConfig(),
"Basic", "Profile");
auto cb = [&](const char *name, const char *filePath)
{
if (strcmp(oldName.c_str(), name) != 0) {
newName = name;
newPath = filePath;
return false;
}
return true;
};
EnumProfiles(cb);
/* this should never be true due to menu item being grayed out */
if (newPath.empty())
return;
QString text = QTStr("ConfirmRemove.Text");
text.replace("$1", QT_UTF8(oldName.c_str()));
QMessageBox::StandardButton button = OBSMessageBox::question(this,
QTStr("ConfirmRemove.Title"), text);
if (button == QMessageBox::No)
return;
size_t newPath_len = newPath.size();
newPath += "/basic.ini";
if (config.Open(newPath.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
newPath.c_str());
return;
}
newPath.resize(newPath_len);
const char *newDir = strrchr(newPath.c_str(), '/') + 1;
config_set_string(App()->GlobalConfig(), "Basic", "Profile",
newName.c_str());
config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
newDir);
config.Swap(basicConfig);
InitBasicConfigDefaults();
ResetProfileData();
DeleteProfile(oldName.c_str(), oldDir.c_str());
RefreshProfiles();
config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
blog(LOG_INFO, "Switched to profile '%s' (%s)",
newName.c_str(), newDir);
blog(LOG_INFO, "------------------------------------------------");
UpdateTitleBar();
if (api) {
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
}
void OBSBasic::on_actionImportProfile_triggered()
{
char path[512];
QString home = QDir::homePath();
int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profile config path");
return;
}
QString dir = QFileDialog::getExistingDirectory(
this,
QTStr("Basic.MainMenu.Profile.Import"),
home,
QFileDialog::ShowDirsOnly |
QFileDialog::DontResolveSymlinks);
if (!dir.isEmpty() && !dir.isNull()) {
QString inputPath = QString::fromUtf8(path);
QFileInfo finfo(dir);
QString directory = finfo.fileName();
QString profileDir = inputPath + directory;
QDir folder(profileDir);
if (!folder.exists()) {
folder.mkpath(profileDir);
QFile::copy(dir + "/basic.ini",
profileDir + "/basic.ini");
QFile::copy(dir + "/service.json",
profileDir + "/service.json");
QFile::copy(dir + "/streamEncoder.json",
profileDir + "/streamEncoder.json");
QFile::copy(dir + "/recordEncoder.json",
profileDir + "/recordEncoder.json");
RefreshProfiles();
} else {
OBSMessageBox::information(this,
QTStr("Basic.MainMenu.Profile.Import"),
QTStr("Basic.MainMenu.Profile.Exists"));
}
}
}
void OBSBasic::on_actionExportProfile_triggered()
{
char path[512];
QString home = QDir::homePath();
QString currentProfile =
QString::fromUtf8(config_get_string(App()->GlobalConfig(),
"Basic", "ProfileDir"));
int ret = GetConfigPath(path, 512, "obs-studio/basic/profiles/");
if (ret <= 0) {
blog(LOG_WARNING, "Failed to get profile config path");
return;
}
QString dir = QFileDialog::getExistingDirectory(
this,
QTStr("Basic.MainMenu.Profile.Export"),
home,
QFileDialog::ShowDirsOnly |
QFileDialog::DontResolveSymlinks);
if (!dir.isEmpty() && !dir.isNull()) {
QString outputDir = dir + "/" + currentProfile;
QString inputPath = QString::fromUtf8(path);
QDir folder(outputDir);
if (!folder.exists()) {
folder.mkpath(outputDir);
} else {
if (QFile::exists(outputDir + "/basic.ini"))
QFile::remove(outputDir + "/basic.ini");
if (QFile::exists(outputDir + "/service.json"))
QFile::remove(outputDir + "/service.json");
if (QFile::exists(outputDir + "/streamEncoder.json"))
QFile::remove(outputDir + "/streamEncoder.json");
if (QFile::exists(outputDir + "/recordEncoder.json"))
QFile::remove(outputDir + "/recordEncoder.json");
}
QFile::copy(inputPath + currentProfile + "/basic.ini",
outputDir + "/basic.ini");
QFile::copy(inputPath + currentProfile + "/service.json",
outputDir + "/service.json");
QFile::copy(inputPath + currentProfile + "/streamEncoder.json",
outputDir + "/streamEncoder.json");
QFile::copy(inputPath + currentProfile + "/recordEncoder.json",
outputDir + "/recordEncoder.json");
}
}
void OBSBasic::ChangeProfile()
{
QAction *action = reinterpret_cast<QAction*>(sender());
ConfigFile config;
std::string path;
if (!action)
return;
path = QT_TO_UTF8(action->property("file_name").value<QString>());
if (path.empty())
return;
const char *oldName = config_get_string(App()->GlobalConfig(),
"Basic", "Profile");
if (action->text().compare(QT_UTF8(oldName)) == 0) {
action->setChecked(true);
return;
}
size_t path_len = path.size();
path += "/basic.ini";
if (config.Open(path.c_str(), CONFIG_OPEN_ALWAYS) != 0) {
blog(LOG_ERROR, "ChangeProfile: Failed to load file '%s'",
path.c_str());
return;
}
path.resize(path_len);
const char *newName = config_get_string(config, "General", "Name");
const char *newDir = strrchr(path.c_str(), '/') + 1;
config_set_string(App()->GlobalConfig(), "Basic", "Profile", newName);
config_set_string(App()->GlobalConfig(), "Basic", "ProfileDir",
newDir);
config.Swap(basicConfig);
InitBasicConfigDefaults();
ResetProfileData();
RefreshProfiles();
config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
UpdateTitleBar();
CheckForSimpleModeX264Fallback();
blog(LOG_INFO, "Switched to profile '%s' (%s)",
newName, newDir);
blog(LOG_INFO, "------------------------------------------------");
if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
void OBSBasic::CheckForSimpleModeX264Fallback()
{
const char *curStreamEncoder = config_get_string(basicConfig,
"SimpleOutput", "StreamEncoder");
const char *curRecEncoder = config_get_string(basicConfig,
"SimpleOutput", "RecEncoder");
bool qsv_supported = false;
bool amd_supported = false;
bool nve_supported = false;
bool changed = false;
size_t idx = 0;
const char *id;
while (obs_enum_encoder_types(idx++, &id)) {
if (strcmp(id, "amd_amf_h264") == 0)
amd_supported = true;
else if (strcmp(id, "obs_qsv11") == 0)
qsv_supported = true;
else if (strcmp(id, "ffmpeg_nvenc") == 0)
nve_supported = true;
}
auto CheckEncoder = [&] (const char *&name)
{
if (strcmp(name, SIMPLE_ENCODER_QSV) == 0) {
if (!qsv_supported) {
changed = true;
name = SIMPLE_ENCODER_X264;
return false;
}
} else if (strcmp(name, SIMPLE_ENCODER_NVENC) == 0) {
if (!nve_supported) {
changed = true;
name = SIMPLE_ENCODER_X264;
return false;
}
} else if (strcmp(name, SIMPLE_ENCODER_AMD) == 0) {
if (!amd_supported) {
changed = true;
name = SIMPLE_ENCODER_X264;
return false;
}
}
return true;
};
if (!CheckEncoder(curStreamEncoder))
config_set_string(basicConfig,
"SimpleOutput", "StreamEncoder",
curStreamEncoder);
if (!CheckEncoder(curRecEncoder))
config_set_string(basicConfig,
"SimpleOutput", "RecEncoder",
curRecEncoder);
if (changed)
config_save_safe(basicConfig, "tmp", nullptr);
}
|