2013-11-07 16:45:03 -07:00
|
|
|
/******************************************************************************
|
2023-05-18 17:37:26 -07:00
|
|
|
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
|
|
|
|
|
Zachary Lund <admin@computerquip.com>
|
|
|
|
|
Philippe Groarke <philippe.groarke@gmail.com>
|
2013-11-07 16:45:03 -07:00
|
|
|
|
|
|
|
|
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
|
2013-12-02 22:24:38 -07:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-11-07 16:45:03 -07:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
2013-11-09 00:19:38 -07:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-11-07 16:45:03 -07:00
|
|
|
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/>.
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
#include "OBSBasic.hpp"
|
2013-12-29 09:17:00 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
constexpr std::string_view OBSServiceFileName = "service.json";
|
2019-07-07 15:47:29 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
void OBSBasic::SaveService()
|
2019-07-07 15:47:29 -07:00
|
|
|
{
|
2024-12-11 17:43:35 +01:00
|
|
|
if (!service)
|
2019-07-07 15:47:29 -07:00
|
|
|
return;
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
const OBSProfile ¤tProfile = GetCurrentProfile();
|
2019-10-02 19:44:47 -04:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
const std::filesystem::path jsonFilePath = currentProfile.path / std::filesystem::u8path(OBSServiceFileName);
|
2020-04-14 20:16:33 +02:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
OBSDataAutoRelease data = obs_data_create();
|
|
|
|
|
OBSDataAutoRelease settings = obs_service_get_settings(service);
|
2019-10-02 19:44:47 -04:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
obs_data_set_string(data, "type", obs_service_get_type(service));
|
|
|
|
|
obs_data_set_obj(data, "settings", settings);
|
2019-07-07 15:47:29 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
if (!obs_data_save_json_safe(data, jsonFilePath.u8string().c_str(), "tmp", "bak")) {
|
|
|
|
|
blog(LOG_WARNING, "Failed to save service");
|
2019-07-07 15:47:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
bool OBSBasic::LoadService()
|
2019-07-07 15:47:29 -07:00
|
|
|
{
|
2024-12-11 17:43:35 +01:00
|
|
|
OBSDataAutoRelease data;
|
2019-07-07 15:47:29 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
try {
|
|
|
|
|
const OBSProfile ¤tProfile = GetCurrentProfile();
|
2024-02-29 20:46:07 +00:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
const std::filesystem::path jsonFilePath =
|
|
|
|
|
currentProfile.path / std::filesystem::u8path(OBSServiceFileName);
|
2019-10-02 19:44:47 -04:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
data = obs_data_create_from_json_file_safe(jsonFilePath.u8string().c_str(), "bak");
|
2020-04-14 20:16:33 +02:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
if (!data) {
|
|
|
|
|
return false;
|
2020-11-12 23:39:32 +01:00
|
|
|
}
|
2024-12-11 17:43:35 +01:00
|
|
|
} catch (const std::invalid_argument &error) {
|
|
|
|
|
blog(LOG_ERROR, "%s", error.what());
|
|
|
|
|
return false;
|
2019-07-07 15:47:29 -07:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
const char *type;
|
|
|
|
|
obs_data_set_default_string(data, "type", "rtmp_common");
|
|
|
|
|
type = obs_data_get_string(data, "type");
|
2019-07-07 15:47:29 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
OBSDataAutoRelease settings = obs_data_get_obj(data, "settings");
|
|
|
|
|
OBSDataAutoRelease hotkey_data = obs_data_get_obj(data, "hotkeys");
|
2019-07-07 15:47:29 -07:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
service = obs_service_create(type, "default_service", settings, hotkey_data);
|
|
|
|
|
obs_service_release(service);
|
2024-04-03 17:08:14 +02:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
if (!service)
|
|
|
|
|
return false;
|
2019-06-12 04:56:00 -05:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
/* Enforce Opus on WHIP if needed */
|
|
|
|
|
if (strcmp(obs_service_get_protocol(service), "WHIP") == 0) {
|
|
|
|
|
const char *option = config_get_string(activeConfiguration, "SimpleOutput", "StreamAudioEncoder");
|
|
|
|
|
if (strcmp(option, "opus") != 0)
|
|
|
|
|
config_set_string(activeConfiguration, "SimpleOutput", "StreamAudioEncoder", "opus");
|
2019-09-27 15:04:07 +02:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
option = config_get_string(activeConfiguration, "AdvOut", "AudioEncoder");
|
2019-09-27 15:04:07 +02:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
const char *encoder_codec = obs_get_encoder_codec(option);
|
|
|
|
|
if (!encoder_codec || strcmp(encoder_codec, "opus") != 0)
|
|
|
|
|
config_set_string(activeConfiguration, "AdvOut", "AudioEncoder", "ffmpeg_opus");
|
2019-09-27 15:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
return true;
|
2019-12-27 17:12:31 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
bool OBSBasic::InitService()
|
2019-12-27 17:12:31 +08:00
|
|
|
{
|
2024-12-11 17:43:35 +01:00
|
|
|
ProfileScope("OBSBasic::InitService");
|
2024-03-28 16:43:12 -04:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
if (LoadService())
|
2024-03-28 16:43:12 -04:00
|
|
|
return true;
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
service = obs_service_create("rtmp_common", "default_service", nullptr, nullptr);
|
|
|
|
|
if (!service)
|
2019-06-12 04:56:00 -05:00
|
|
|
return false;
|
2024-12-11 17:43:35 +01:00
|
|
|
obs_service_release(service);
|
2024-06-18 14:18:22 -05:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
return true;
|
2024-06-18 14:18:22 -05:00
|
|
|
}
|
2024-07-31 10:53:59 -04:00
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
obs_service_t *OBSBasic::GetService()
|
2024-07-31 10:53:59 -04:00
|
|
|
{
|
2024-12-11 17:43:35 +01:00
|
|
|
if (!service) {
|
|
|
|
|
service = obs_service_create("rtmp_common", NULL, NULL, nullptr);
|
|
|
|
|
obs_service_release(service);
|
2024-07-31 10:53:59 -04:00
|
|
|
}
|
2024-12-11 17:43:35 +01:00
|
|
|
return service;
|
2024-07-31 10:53:59 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 17:43:35 +01:00
|
|
|
void OBSBasic::SetService(obs_service_t *newService)
|
2024-09-03 16:29:30 +02:00
|
|
|
{
|
2024-12-11 17:43:35 +01:00
|
|
|
if (newService) {
|
|
|
|
|
service = newService;
|
2024-09-03 16:29:30 +02:00
|
|
|
}
|
|
|
|
|
}
|