From 6ff2783105781150357fb8a00c2195f3d40e5d59 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Sat, 18 Jul 2026 20:22:36 +0800 Subject: [PATCH] [feat] add TURN relay mode configuration --- src/config_center/config_center.cpp | 70 ++++++++++++++++++++++++++--- src/config_center/config_center.h | 10 ++++- src/gui/render.cpp | 3 +- submodules/minirtc | 2 +- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/config_center/config_center.cpp b/src/config_center/config_center.cpp index 3e7cbd5..ee056ba 100644 --- a/src/config_center/config_center.cpp +++ b/src/config_center/config_center.cpp @@ -5,6 +5,15 @@ namespace crossdesk { +namespace { + +bool IsValidTurnModeValue(long value) { + return value >= static_cast(ConfigCenter::TURN_MODE::DISABLED) && + value <= static_cast(ConfigCenter::TURN_MODE::FORCE_TCP); +} + +} // namespace + ConfigCenter::ConfigCenter(const std::string& config_path) : config_path_(config_path) { ini_.SetUnicode(true); @@ -20,6 +29,8 @@ int ConfigCenter::Load() { return -1; } + bool persist_turn_mode_migration = false; + const long language_value = ini_.GetLongValue(section_, "language", static_cast(language_)); if (language_value < static_cast(LANGUAGE::CHINESE) || @@ -42,7 +53,25 @@ int ConfigCenter::Load() { hardware_video_codec_ = ini_.GetBoolValue(section_, "hardware_video_codec", hardware_video_codec_); - enable_turn_ = ini_.GetBoolValue(section_, "enable_turn", enable_turn_); + const char* turn_mode_value = ini_.GetValue(section_, "turn_mode", nullptr); + if (turn_mode_value != nullptr && strlen(turn_mode_value) > 0) { + const long parsed_turn_mode = ini_.GetLongValue( + section_, "turn_mode", static_cast(turn_mode_)); + if (IsValidTurnModeValue(parsed_turn_mode)) { + turn_mode_ = static_cast(parsed_turn_mode); + } else { + LOG_WARN("Invalid TURN mode [{}], using auto UDP/TCP", + parsed_turn_mode); + turn_mode_ = TURN_MODE::AUTO_UDP_TCP; + } + } else { + const bool legacy_enable_turn = ini_.GetBoolValue( + section_, "enable_turn", turn_mode_ != TURN_MODE::DISABLED); + turn_mode_ = legacy_enable_turn ? TURN_MODE::AUTO_UDP_TCP + : TURN_MODE::DISABLED; + ini_.SetLongValue(section_, "turn_mode", static_cast(turn_mode_)); + persist_turn_mode_migration = true; + } enable_srtp_ = ini_.GetBoolValue(section_, "enable_srtp", enable_srtp_); enable_self_hosted_ = ini_.GetBoolValue(section_, "enable_self_hosted", enable_self_hosted_); @@ -92,6 +121,11 @@ int ConfigCenter::Load() { file_transfer_save_path_ = ""; } + if (persist_turn_mode_migration && + ini_.SaveFile(config_path_.c_str()) < 0) { + return -1; + } + return 0; } @@ -104,7 +138,9 @@ int ConfigCenter::Save() { ini_.SetLongValue(section_, "video_encode_format", static_cast(video_encode_format_)); ini_.SetBoolValue(section_, "hardware_video_codec", hardware_video_codec_); - ini_.SetBoolValue(section_, "enable_turn", enable_turn_); + ini_.SetLongValue(section_, "turn_mode", static_cast(turn_mode_)); + ini_.SetBoolValue(section_, "enable_turn", + turn_mode_ != TURN_MODE::DISABLED); ini_.SetBoolValue(section_, "enable_srtp", enable_srtp_); ini_.SetBoolValue(section_, "enable_self_hosted", enable_self_hosted_); @@ -191,9 +227,15 @@ int ConfigCenter::SetHardwareVideoCodec(bool hardware_video_codec) { return 0; } -int ConfigCenter::SetTurn(bool enable_turn) { - enable_turn_ = enable_turn; - ini_.SetBoolValue(section_, "enable_turn", enable_turn_); +int ConfigCenter::SetTurnMode(TURN_MODE turn_mode) { + if (!IsValidTurnModeValue(static_cast(turn_mode))) { + return -1; + } + + turn_mode_ = turn_mode; + ini_.SetLongValue(section_, "turn_mode", static_cast(turn_mode_)); + ini_.SetBoolValue(section_, "enable_turn", + turn_mode_ != TURN_MODE::DISABLED); SI_Error rc = ini_.SaveFile(config_path_.c_str()); if (rc < 0) { return -1; @@ -201,6 +243,16 @@ int ConfigCenter::SetTurn(bool enable_turn) { return 0; } +int ConfigCenter::SetTurn(bool enable_turn) { + if (!enable_turn) { + return SetTurnMode(TURN_MODE::DISABLED); + } + if (turn_mode_ == TURN_MODE::DISABLED) { + return SetTurnMode(TURN_MODE::AUTO_UDP_TCP); + } + return SetTurnMode(turn_mode_); +} + int ConfigCenter::SetSrtp(bool enable_srtp) { enable_srtp_ = enable_srtp; ini_.SetBoolValue(section_, "enable_srtp", enable_srtp_); @@ -362,7 +414,13 @@ bool ConfigCenter::IsHardwareVideoCodec() const { return hardware_video_codec_; } -bool ConfigCenter::IsEnableTurn() const { return enable_turn_; } +ConfigCenter::TURN_MODE ConfigCenter::GetTurnMode() const { + return turn_mode_; +} + +bool ConfigCenter::IsEnableTurn() const { + return turn_mode_ != TURN_MODE::DISABLED; +} bool ConfigCenter::IsEnableSrtp() const { return enable_srtp_; } diff --git a/src/config_center/config_center.h b/src/config_center/config_center.h index acb2a5c..d0725cf 100644 --- a/src/config_center/config_center.h +++ b/src/config_center/config_center.h @@ -19,6 +19,12 @@ class ConfigCenter { enum class VIDEO_QUALITY { LOW = 0, MEDIUM = 1, HIGH = 2 }; enum class VIDEO_FRAME_RATE { FPS_30 = 0, FPS_60 = 1 }; enum class VIDEO_ENCODE_FORMAT { H264 = 0, AV1 = 1 }; + enum class TURN_MODE { + DISABLED = 0, + AUTO_UDP_TCP = 1, + FORCE_UDP = 2, + FORCE_TCP = 3 + }; public: explicit ConfigCenter(const std::string& config_path = "config.ini"); @@ -30,6 +36,7 @@ class ConfigCenter { int SetVideoFrameRate(VIDEO_FRAME_RATE video_frame_rate); int SetVideoEncodeFormat(VIDEO_ENCODE_FORMAT video_encode_format); int SetHardwareVideoCodec(bool hardware_video_codec); + int SetTurnMode(TURN_MODE turn_mode); int SetTurn(bool enable_turn); int SetSrtp(bool enable_srtp); int SetServerHost(const std::string& signal_server_host); @@ -49,6 +56,7 @@ class ConfigCenter { VIDEO_FRAME_RATE GetVideoFrameRate() const; VIDEO_ENCODE_FORMAT GetVideoEncodeFormat() const; bool IsHardwareVideoCodec() const; + TURN_MODE GetTurnMode() const; bool IsEnableTurn() const; bool IsEnableSrtp() const; std::string GetSignalServerHost() const; @@ -77,7 +85,7 @@ class ConfigCenter { VIDEO_FRAME_RATE video_frame_rate_ = VIDEO_FRAME_RATE::FPS_60; VIDEO_ENCODE_FORMAT video_encode_format_ = VIDEO_ENCODE_FORMAT::H264; bool hardware_video_codec_ = false; - bool enable_turn_ = true; + TURN_MODE turn_mode_ = TURN_MODE::AUTO_UDP_TCP; bool enable_srtp_ = false; std::string signal_server_host_ = ""; std::string signal_server_host_default_ = "api.crossdesk.cn"; diff --git a/src/gui/render.cpp b/src/gui/render.cpp index 94c1016..43b55d7 100644 --- a/src/gui/render.cpp +++ b/src/gui/render.cpp @@ -1151,7 +1151,8 @@ int Render::CreateConnectionPeer() { ConfigCenter::VIDEO_ENCODE_FORMAT::AV1 ? true : false; - params_.enable_turn = config_center_->IsEnableTurn(); + params_.turn_mode = + static_cast(config_center_->GetTurnMode()); params_.enable_srtp = config_center_->IsEnableSrtp(); params_.video_quality = static_cast(config_center_->GetVideoQuality()); diff --git a/submodules/minirtc b/submodules/minirtc index 9e5ce69..6587685 160000 --- a/submodules/minirtc +++ b/submodules/minirtc @@ -1 +1 @@ -Subproject commit 9e5ce698df5d141006a090698b18cb379064abb7 +Subproject commit 658768547faf8fe6c2783832bfad6ce8f5999a8e