OBSBasic_Service.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. constexpr std::string_view OBSServiceFileName = "service.json";
  18. void OBSBasic::SaveService()
  19. {
  20. if (!service)
  21. return;
  22. const OBSProfile &currentProfile = GetCurrentProfile();
  23. const std::filesystem::path jsonFilePath = currentProfile.path / std::filesystem::u8path(OBSServiceFileName);
  24. OBSDataAutoRelease data = obs_data_create();
  25. OBSDataAutoRelease settings = obs_service_get_settings(service);
  26. obs_data_set_string(data, "type", obs_service_get_type(service));
  27. obs_data_set_obj(data, "settings", settings);
  28. if (!obs_data_save_json_safe(data, jsonFilePath.u8string().c_str(), "tmp", "bak")) {
  29. blog(LOG_WARNING, "Failed to save service");
  30. }
  31. }
  32. bool OBSBasic::LoadService()
  33. {
  34. OBSDataAutoRelease data;
  35. try {
  36. const OBSProfile &currentProfile = GetCurrentProfile();
  37. const std::filesystem::path jsonFilePath =
  38. currentProfile.path / std::filesystem::u8path(OBSServiceFileName);
  39. data = obs_data_create_from_json_file_safe(jsonFilePath.u8string().c_str(), "bak");
  40. if (!data) {
  41. return false;
  42. }
  43. } catch (const std::invalid_argument &error) {
  44. blog(LOG_ERROR, "%s", error.what());
  45. return false;
  46. }
  47. const char *type;
  48. obs_data_set_default_string(data, "type", "rtmp_common");
  49. type = obs_data_get_string(data, "type");
  50. OBSDataAutoRelease settings = obs_data_get_obj(data, "settings");
  51. OBSDataAutoRelease hotkey_data = obs_data_get_obj(data, "hotkeys");
  52. service = obs_service_create(type, "default_service", settings, hotkey_data);
  53. obs_service_release(service);
  54. if (!service)
  55. return false;
  56. /* Enforce Opus on WHIP if needed */
  57. if (strcmp(obs_service_get_protocol(service), "WHIP") == 0) {
  58. const char *option = config_get_string(activeConfiguration, "SimpleOutput", "StreamAudioEncoder");
  59. if (strcmp(option, "opus") != 0)
  60. config_set_string(activeConfiguration, "SimpleOutput", "StreamAudioEncoder", "opus");
  61. option = config_get_string(activeConfiguration, "AdvOut", "AudioEncoder");
  62. const char *encoder_codec = obs_get_encoder_codec(option);
  63. if (!encoder_codec || strcmp(encoder_codec, "opus") != 0)
  64. config_set_string(activeConfiguration, "AdvOut", "AudioEncoder", "ffmpeg_opus");
  65. }
  66. return true;
  67. }
  68. bool OBSBasic::InitService()
  69. {
  70. ProfileScope("OBSBasic::InitService");
  71. if (LoadService())
  72. return true;
  73. service = obs_service_create("rtmp_common", "default_service", nullptr, nullptr);
  74. if (!service)
  75. return false;
  76. obs_service_release(service);
  77. return true;
  78. }
  79. obs_service_t *OBSBasic::GetService()
  80. {
  81. if (!service) {
  82. service = obs_service_create("rtmp_common", NULL, NULL, nullptr);
  83. obs_service_release(service);
  84. }
  85. return service;
  86. }
  87. void OBSBasic::SetService(obs_service_t *newService)
  88. {
  89. if (newService) {
  90. service = newService;
  91. }
  92. }