[feat] add configurable connection starting bitrates

This commit is contained in:
dijunkun committed 2026-11-14 01:13:41 +08:00
1 parent 390eb26337
commit a369ba8a3a
7 files changed
+93 -1

No files matched your search

@@ -1,6 +1,7 @@
#include "config_center.h"
#include <algorithm>
#include <charconv>
#include <chrono>
#include <cstring>
#include <filesystem>
@@ -37,6 +38,21 @@ bool IsValidTurnModeValue(long value) {
value <= static_cast<long>(ConfigCenter::TURN_MODE::FORCE_TCP);
}
int ReadStartingBitrate(const CSimpleIniA& ini, const char* section,
const char* key, int fallback) {
const char* value = ini.GetValue(section, key, nullptr);
if (!value) return fallback;
const char* end = value + std::strlen(value);
int bps = 0;
const auto parsed = std::from_chars(value, end, bps);
if (parsed.ec != std::errc{} || parsed.ptr != end ||
bps < 300'000 || bps > 20'000'000) {
LOG_WARN("Invalid {} configuration, using {} bps", key, fallback);
return fallback;
}
return bps;
}
} // namespace
ConfigCenter::ConfigCenter(const std::string& config_path)
@@ -129,6 +145,20 @@ int ConfigCenter::Load() {
ini_->SetBoolValue(section_, "enable_srtp", true);
persist_config_migration = true;
}
direct_start_bitrate_bps_ = ReadStartingBitrate(
*ini_, section_, "direct_start_bitrate_bps", kDefaultDirectStartBitrateBps);
relay_start_bitrate_bps_ = ReadStartingBitrate(
*ini_, section_, "relay_start_bitrate_bps", kDefaultRelayStartBitrateBps);
// Populate old configurations and repair invalid values so the file shows
// the estimates that will actually be used for subsequent connections.
for (const auto& entry :
{std::make_pair("direct_start_bitrate_bps", direct_start_bitrate_bps_),
std::make_pair("relay_start_bitrate_bps", relay_start_bitrate_bps_)}) {
if (std::to_string(entry.second) != ini_->GetValue(section_, entry.first, "")) {
ini_->SetLongValue(section_, entry.first, entry.second);
persist_config_migration = true;
}
}
enable_self_hosted_ =
ini_->GetBoolValue(section_, "enable_self_hosted", enable_self_hosted_);
@@ -244,6 +274,8 @@ int ConfigCenter::Save() {
ini_->SetLongValue(section_, "video_encode_format",
static_cast<long>(video_encode_format_));
ini_->SetLongValue(section_, "turn_mode", static_cast<long>(turn_mode_));
ini_->SetLongValue(section_, "direct_start_bitrate_bps", direct_start_bitrate_bps_);
ini_->SetLongValue(section_, "relay_start_bitrate_bps", relay_start_bitrate_bps_);
ini_->SetBoolValue(section_, "enable_turn",
turn_mode_ != TURN_MODE::DISABLED);
ini_->SetBoolValue(section_, "enable_srtp", true);
@@ -292,6 +324,8 @@ std::string ConfigCenter::ExportSharedHostConfig() const {
host.SetValue(section_, "signal_server_host", signal_server_host_.c_str());
host.SetLongValue(section_, "signal_server_port", signal_server_port_);
host.SetLongValue(section_, "turn_mode", static_cast<long>(turn_mode_));
host.SetLongValue(section_, "direct_start_bitrate_bps", direct_start_bitrate_bps_);
host.SetLongValue(section_, "relay_start_bitrate_bps", relay_start_bitrate_bps_);
host.SetLongValue(section_, "video_encode_format", static_cast<long>(video_encode_format_));
host.SetBoolValue(section_, "trusted_devices_only", trusted_devices_only_.load());
host.SetBoolValue(section_, "lock_on_disconnect", lock_on_disconnect_.load());
@@ -313,6 +347,10 @@ int ConfigCenter::ApplySharedHostConfig(const std::string& contents) {
const long port = host.GetLongValue(section_, "signal_server_port", 0);
const long turn = host.GetLongValue(section_, "turn_mode", static_cast<long>(turn_mode_));
const long codec = host.GetLongValue(section_, "video_encode_format", static_cast<long>(video_encode_format_));
const int direct_bps = ReadStartingBitrate(
host, section_, "direct_start_bitrate_bps", kDefaultDirectStartBitrateBps);
const int relay_bps = ReadStartingBitrate(
host, section_, "relay_start_bitrate_bps", kDefaultRelayStartBitrateBps);
const bool trusted = host.GetBoolValue(section_, "trusted_devices_only", false);
const bool lock = host.GetBoolValue(section_, "lock_on_disconnect", true);
MiniRtcSignalServerAddress address{};
@@ -322,12 +360,16 @@ int ConfigCenter::ApplySharedHostConfig(const std::string& contents) {
if (StoreValues({{"enable_self_hosted", self_hosted ? "true" : "false"},
{"signal_server_host", server}, {"signal_server_port", std::to_string(port)},
{"turn_mode", std::to_string(turn)}, {"video_encode_format", std::to_string(codec)},
{"direct_start_bitrate_bps", std::to_string(direct_bps)},
{"relay_start_bitrate_bps", std::to_string(relay_bps)},
{"trusted_devices_only", trusted ? "true" : "false"},
{"lock_on_disconnect", lock ? "true" : "false"}}) != 0) return -1;
enable_self_hosted_ = self_hosted;
signal_server_host_ = server;
signal_server_port_ = static_cast<int>(port);
turn_mode_ = static_cast<TURN_MODE>(turn);
direct_start_bitrate_bps_ = direct_bps;
relay_start_bitrate_bps_ = relay_bps;
video_encode_format_ = static_cast<VIDEO_ENCODE_FORMAT>(codec);
trusted_devices_only_.store(trusted);
lock_on_disconnect_.store(lock);
@@ -614,6 +656,14 @@ ConfigCenter::TURN_MODE ConfigCenter::GetTurnMode() const {
return turn_mode_;
}
int ConfigCenter::GetDirectStartBitrateBps() const {
return direct_start_bitrate_bps_;
}
int ConfigCenter::GetRelayStartBitrateBps() const {
return relay_start_bitrate_bps_;
}
bool ConfigCenter::IsForceRelay() const {
return turn_mode_ == TURN_MODE::FORCE_UDP || turn_mode_ == TURN_MODE::FORCE_TCP;
}
@@ -67,6 +67,8 @@ class ConfigCenter {
bool IsIpDirectEnabled() const;
VIDEO_ENCODE_FORMAT GetVideoEncodeFormat() const;
TURN_MODE GetTurnMode() const;
int GetDirectStartBitrateBps() const;
int GetRelayStartBitrateBps() const;
bool IsForceRelay() const;
bool IsEnableSrtp() const;
std::string GetSignalServerHost() const;
@@ -106,6 +108,10 @@ class ConfigCenter {
bool enable_ip_direct_ = true;
VIDEO_ENCODE_FORMAT video_encode_format_ = VIDEO_ENCODE_FORMAT::H264;
TURN_MODE turn_mode_ = TURN_MODE::AUTO_UDP_TCP;
static constexpr int kDefaultDirectStartBitrateBps = 1'500'000;
static constexpr int kDefaultRelayStartBitrateBps = 500'000;
int direct_start_bitrate_bps_ = kDefaultDirectStartBitrateBps;
int relay_start_bitrate_bps_ = kDefaultRelayStartBitrateBps;
std::string signal_server_host_ = "";
std::string signal_server_host_default_ = "api.crossdesk.cn";
int signal_server_port_ = 0;
@@ -316,6 +316,8 @@ int GuiRuntime::CreateConnectionPeer() {
? true
: false;
params_.turn_mode = static_cast<MiniRtcTurnMode>(config_center_->GetTurnMode());
params_.direct_start_bitrate_bps = config_center_->GetDirectStartBitrateBps();
params_.relay_start_bitrate_bps = config_center_->GetRelayStartBitrateBps();
params_.enable_srtp = config_center_->IsEnableSrtp();
params_.video_content_type = MiniRtcVideoContentType::ScreenContent;
// Bootstrap outgoing media with fixed defaults. Live overrides come only
@@ -47,6 +47,8 @@ void GuiRuntime::InitializeLan() {
ConfigCenter::VIDEO_ENCODE_FORMAT::AV1;
params.enable_srtp = true;
params.turn_mode = MiniRtcTurnMode::MiniRtcTurnDisabled;
params.direct_start_bitrate_bps = config_center_->GetDirectStartBitrateBps();
params.relay_start_bitrate_bps = config_center_->GetRelayStartBitrateBps();
params.video_content_type = MiniRtcVideoContentType::ScreenContent;
params.video_quality = MiniRtcVideoQuality::MiniRtcVideoQualityHigh;
params.video_frame_rate = 60;