pub const RTT_GOOD_MS: f64 = 100.0;
pub const RTT_FAIR_MS: f64 = 200.0;
pub const RTT_POOR_MS: f64 = 400.0;
pub const FPS_RATIO_GOOD: f64 = 0.90;
pub const FPS_RATIO_FAIR: f64 = 0.70;
pub const FPS_RATIO_POOR: f64 = 0.40;
pub const JITTER_GOOD_MS: f64 = 20.0;
pub const JITTER_FAIR_MS: f64 = 50.0;
pub const JITTER_POOR_MS: f64 = 100.0;
pub const RTT_AVERAGING_WINDOW_SAMPLES: usize = 10;
pub struct VideoQualityTier {
pub label: &'static str,
pub max_width: u32,
pub max_height: u32,
pub target_fps: u32,
pub ideal_bitrate_kbps: u32,
pub min_bitrate_kbps: u32,
pub max_bitrate_kbps: u32,
pub keyframe_interval_frames: u32,
}
pub const VIDEO_QUALITY_TIERS: &[VideoQualityTier] = &[
VideoQualityTier {
label: "high",
max_width: 1280,
max_height: 720,
target_fps: 30,
ideal_bitrate_kbps: 1500,
min_bitrate_kbps: 800,
max_bitrate_kbps: 2500,
keyframe_interval_frames: 150, },
VideoQualityTier {
label: "medium",
max_width: 854,
max_height: 480,
target_fps: 25,
ideal_bitrate_kbps: 600,
min_bitrate_kbps: 300,
max_bitrate_kbps: 1000,
keyframe_interval_frames: 125, },
VideoQualityTier {
label: "low",
max_width: 640,
max_height: 360,
target_fps: 15,
ideal_bitrate_kbps: 300,
min_bitrate_kbps: 150,
max_bitrate_kbps: 500,
keyframe_interval_frames: 75, },
VideoQualityTier {
label: "minimal",
max_width: 426,
max_height: 240,
target_fps: 10,
ideal_bitrate_kbps: 150,
min_bitrate_kbps: 50,
max_bitrate_kbps: 250,
keyframe_interval_frames: 50, },
];
pub const DEFAULT_VIDEO_TIER_INDEX: usize = 3;
pub const DEFAULT_SCREEN_TIER_INDEX: usize = 2;
pub const SCREEN_QUALITY_TIERS: &[VideoQualityTier] = &[
VideoQualityTier {
label: "high",
max_width: 1920,
max_height: 1080,
target_fps: 15,
ideal_bitrate_kbps: 1500,
min_bitrate_kbps: 800,
max_bitrate_kbps: 2500,
keyframe_interval_frames: 75, },
VideoQualityTier {
label: "medium",
max_width: 1280,
max_height: 720,
target_fps: 10,
ideal_bitrate_kbps: 600,
min_bitrate_kbps: 300,
max_bitrate_kbps: 1000,
keyframe_interval_frames: 50, },
VideoQualityTier {
label: "low",
max_width: 854,
max_height: 480,
target_fps: 5,
ideal_bitrate_kbps: 250,
min_bitrate_kbps: 100,
max_bitrate_kbps: 400,
keyframe_interval_frames: 25, },
];
pub struct AudioQualityTier {
pub label: &'static str,
pub bitrate_kbps: u32,
pub enable_dtx: bool,
pub enable_fec: bool,
}
pub const AUDIO_QUALITY_TIERS: &[AudioQualityTier] = &[
AudioQualityTier {
label: "high",
bitrate_kbps: 50,
enable_dtx: true,
enable_fec: false,
},
AudioQualityTier {
label: "medium",
bitrate_kbps: 32,
enable_dtx: true,
enable_fec: true, },
AudioQualityTier {
label: "low",
bitrate_kbps: 24,
enable_dtx: true,
enable_fec: true,
},
AudioQualityTier {
label: "emergency",
bitrate_kbps: 16,
enable_dtx: true,
enable_fec: true,
},
];
pub const VIDEO_TIER_DEGRADE_FPS_RATIO: f64 = 0.50;
pub const VIDEO_TIER_RECOVER_FPS_RATIO: f64 = 0.85;
pub const VIDEO_TIER_DEGRADE_BITRATE_RATIO: f64 = 0.40;
pub const VIDEO_TIER_RECOVER_BITRATE_RATIO: f64 = 0.75;
pub const AUDIO_TIER_DEGRADE_FPS_RATIO: f64 = 0.30;
pub const AUDIO_TIER_RECOVER_FPS_RATIO: f64 = 0.60;
pub const STEP_UP_STABILIZATION_WINDOW_MS: u64 = 5000;
pub const STEP_DOWN_REACTION_TIME_MS: u64 = 1500;
pub const MIN_TIER_TRANSITION_INTERVAL_MS: u64 = 3000;
pub const QUALITY_WARMUP_MS: f64 = 5000.0;
pub const PID_KP: f64 = 0.2; pub const PID_KI: f64 = 0.05; pub const PID_KD: f64 = 0.02;
pub const PID_DEADBAND_FPS: f64 = 0.5;
pub const PID_OUTPUT_MIN: f64 = 0.0;
pub const PID_OUTPUT_MAX: f64 = 50.0;
pub const PID_MAX_JITTER_PENALTY: f64 = 0.30;
pub const PID_CORRECTION_THROTTLE_MS: f64 = 1000.0;
pub const PID_FPS_HISTORY_SIZE: usize = 10;
pub const BITRATE_CHANGE_THRESHOLD: f64 = 0.20;
pub const CAMERA_KEYFRAME_INTERVAL_FRAMES: u32 = 150;
pub const SCREEN_KEYFRAME_INTERVAL_FRAMES: u32 = 150;
pub const KEYFRAME_REQUEST_TIMEOUT_MS: u64 = 1000;
pub const KEYFRAME_REQUEST_MIN_INTERVAL_MS: u64 = 500;
pub const RECONNECT_INITIAL_DELAY_MS: u64 = 500;
pub const RECONNECT_MAX_DELAY_PHASE1_MS: u64 = 2000;
pub const RECONNECT_MAX_DELAY_PHASE2_MS: u64 = 10000;
pub const RECONNECT_MAX_DELAY_PHASE3_MS: u64 = 30000;
pub const RECONNECT_PHASE1_MAX_ATTEMPTS: u32 = 5;
pub const RECONNECT_PHASE2_MAX_ATTEMPTS: u32 = 15;
pub const RECONNECT_BACKOFF_MULTIPLIER: f64 = 2.0;
pub const RECONNECT_CONSECUTIVE_ZERO_LIMIT: u32 = 10;
pub const REELECTION_RTT_MULTIPLIER: f64 = 3.0;
pub const REELECTION_RTT_MIN_THRESHOLD_MS: f64 = 50.0;
pub const REELECTION_CONSECUTIVE_SAMPLES: u32 = 5;
pub const HEARTBEAT_KEEPALIVE_INTERVAL_MS: u32 = 5000;
pub const VAD_POLL_INTERVAL_MS: u32 = 50;
pub const DIAGNOSTICS_REPORT_INTERVAL_MS: u64 = 1000;
pub const RTT_PROBE_ELECTION_INTERVAL_MS: u64 = 200;
pub const ELECTION_MIN_RTT_SAMPLES: usize = 2;
pub const ELECTION_MAX_EXTENSIONS: u32 = 2;
pub const RTT_PROBE_CONNECTED_INTERVAL_MS: u64 = 1000;
pub const DATAGRAM_MAX_SIZE: usize = 1200;
pub const AUDIO_REDUNDANCY_ENABLED: bool = false;
pub const OPUS_FRAME_DURATION_MS: u32 = 20;
pub const AUDIO_RED_FORMAT: &str = "opus-red";
pub const AUDIO_RED_SEQ_HISTORY_SIZE: usize = 64;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_video_tiers_not_empty() {
assert!(
!VIDEO_QUALITY_TIERS.is_empty(),
"VIDEO_QUALITY_TIERS must have at least one tier"
);
}
#[test]
fn test_video_tier_bitrate_ordering() {
for tier in VIDEO_QUALITY_TIERS {
assert!(
tier.min_bitrate_kbps < tier.max_bitrate_kbps,
"tier '{}': min_bitrate ({}) must be less than max_bitrate ({})",
tier.label,
tier.min_bitrate_kbps,
tier.max_bitrate_kbps,
);
assert!(
tier.ideal_bitrate_kbps >= tier.min_bitrate_kbps,
"tier '{}': ideal_bitrate ({}) must be >= min_bitrate ({})",
tier.label,
tier.ideal_bitrate_kbps,
tier.min_bitrate_kbps,
);
assert!(
tier.ideal_bitrate_kbps <= tier.max_bitrate_kbps,
"tier '{}': ideal_bitrate ({}) must be <= max_bitrate ({})",
tier.label,
tier.ideal_bitrate_kbps,
tier.max_bitrate_kbps,
);
}
}
#[test]
fn test_video_tier_resolutions_positive() {
for tier in VIDEO_QUALITY_TIERS {
assert!(
tier.max_width > 0 && tier.max_height > 0,
"tier '{}': resolution must be positive ({}x{})",
tier.label,
tier.max_width,
tier.max_height,
);
}
}
#[test]
fn test_video_tier_fps_positive() {
for tier in VIDEO_QUALITY_TIERS {
assert!(
tier.target_fps > 0,
"tier '{}': target_fps must be positive",
tier.label,
);
}
}
#[test]
fn test_video_tier_keyframe_interval_positive() {
for tier in VIDEO_QUALITY_TIERS {
assert!(
tier.keyframe_interval_frames > 0,
"tier '{}': keyframe_interval_frames must be positive",
tier.label,
);
}
}
#[test]
fn test_video_tiers_descending_resolution() {
for window in VIDEO_QUALITY_TIERS.windows(2) {
let higher = &window[0];
let lower = &window[1];
let higher_pixels = higher.max_width as u64 * higher.max_height as u64;
let lower_pixels = lower.max_width as u64 * lower.max_height as u64;
assert!(
higher_pixels >= lower_pixels,
"tier '{}' ({}px) should have >= pixels than tier '{}' ({}px)",
higher.label,
higher_pixels,
lower.label,
lower_pixels,
);
}
}
#[test]
fn test_video_tiers_descending_fps() {
for window in VIDEO_QUALITY_TIERS.windows(2) {
let higher = &window[0];
let lower = &window[1];
assert!(
higher.target_fps >= lower.target_fps,
"tier '{}' ({}fps) should have >= fps than tier '{}' ({}fps)",
higher.label,
higher.target_fps,
lower.label,
lower.target_fps,
);
}
}
#[test]
fn test_default_video_tier_index_in_bounds() {
assert!(
DEFAULT_VIDEO_TIER_INDEX < VIDEO_QUALITY_TIERS.len(),
"DEFAULT_VIDEO_TIER_INDEX ({}) out of bounds (len={})",
DEFAULT_VIDEO_TIER_INDEX,
VIDEO_QUALITY_TIERS.len(),
);
}
#[test]
fn test_default_screen_tier_index_in_bounds() {
assert!(
DEFAULT_SCREEN_TIER_INDEX < SCREEN_QUALITY_TIERS.len(),
"DEFAULT_SCREEN_TIER_INDEX ({}) out of bounds (len={})",
DEFAULT_SCREEN_TIER_INDEX,
SCREEN_QUALITY_TIERS.len(),
);
}
#[test]
fn test_screen_tiers_not_empty() {
assert!(
!SCREEN_QUALITY_TIERS.is_empty(),
"SCREEN_QUALITY_TIERS must have at least one tier"
);
}
#[test]
fn test_screen_tier_bitrate_ordering() {
for tier in SCREEN_QUALITY_TIERS {
assert!(
tier.min_bitrate_kbps < tier.max_bitrate_kbps,
"screen tier '{}': min_bitrate ({}) must be < max_bitrate ({})",
tier.label,
tier.min_bitrate_kbps,
tier.max_bitrate_kbps,
);
assert!(
tier.ideal_bitrate_kbps >= tier.min_bitrate_kbps
&& tier.ideal_bitrate_kbps <= tier.max_bitrate_kbps,
"screen tier '{}': ideal_bitrate ({}) must be within [{}, {}]",
tier.label,
tier.ideal_bitrate_kbps,
tier.min_bitrate_kbps,
tier.max_bitrate_kbps,
);
}
}
#[test]
fn test_screen_tiers_descending_resolution() {
for window in SCREEN_QUALITY_TIERS.windows(2) {
let higher = &window[0];
let lower = &window[1];
let h_px = higher.max_width as u64 * higher.max_height as u64;
let l_px = lower.max_width as u64 * lower.max_height as u64;
assert!(
h_px >= l_px,
"screen tier '{}' should have >= pixels than '{}'",
higher.label,
lower.label,
);
}
}
#[test]
fn test_audio_tiers_not_empty() {
assert!(
!AUDIO_QUALITY_TIERS.is_empty(),
"AUDIO_QUALITY_TIERS must have at least one tier"
);
}
#[test]
fn test_audio_tier_bitrate_positive() {
for tier in AUDIO_QUALITY_TIERS {
assert!(
tier.bitrate_kbps > 0,
"audio tier '{}': bitrate must be positive",
tier.label,
);
}
}
#[test]
fn test_audio_tiers_descending_bitrate() {
for window in AUDIO_QUALITY_TIERS.windows(2) {
let higher = &window[0];
let lower = &window[1];
assert!(
higher.bitrate_kbps >= lower.bitrate_kbps,
"audio tier '{}' ({}kbps) should have >= bitrate than '{}' ({}kbps)",
higher.label,
higher.bitrate_kbps,
lower.label,
lower.bitrate_kbps,
);
}
}
#[test]
fn test_hysteresis_gap_video() {
assert!(
VIDEO_TIER_RECOVER_FPS_RATIO > VIDEO_TIER_DEGRADE_FPS_RATIO,
"recover FPS ratio ({}) must be > degrade FPS ratio ({})",
VIDEO_TIER_RECOVER_FPS_RATIO,
VIDEO_TIER_DEGRADE_FPS_RATIO,
);
assert!(
VIDEO_TIER_RECOVER_BITRATE_RATIO > VIDEO_TIER_DEGRADE_BITRATE_RATIO,
"recover bitrate ratio ({}) must be > degrade bitrate ratio ({})",
VIDEO_TIER_RECOVER_BITRATE_RATIO,
VIDEO_TIER_DEGRADE_BITRATE_RATIO,
);
}
#[test]
fn test_hysteresis_gap_audio() {
assert!(
AUDIO_TIER_RECOVER_FPS_RATIO > AUDIO_TIER_DEGRADE_FPS_RATIO,
"audio recover FPS ratio ({}) must be > degrade FPS ratio ({})",
AUDIO_TIER_RECOVER_FPS_RATIO,
AUDIO_TIER_DEGRADE_FPS_RATIO,
);
}
#[test]
fn test_step_up_slower_than_step_down() {
assert!(
STEP_UP_STABILIZATION_WINDOW_MS > STEP_DOWN_REACTION_TIME_MS,
"step-up window ({}) should be > step-down reaction time ({})",
STEP_UP_STABILIZATION_WINDOW_MS,
STEP_DOWN_REACTION_TIME_MS,
);
}
#[test]
fn test_pid_gains_non_negative() {
assert!(PID_KP >= 0.0, "PID_KP must be non-negative");
assert!(PID_KI >= 0.0, "PID_KI must be non-negative");
assert!(PID_KD >= 0.0, "PID_KD must be non-negative");
}
#[test]
fn test_pid_output_limits() {
assert!(
PID_OUTPUT_MIN < PID_OUTPUT_MAX,
"PID output min ({}) must be < max ({})",
PID_OUTPUT_MIN,
PID_OUTPUT_MAX,
);
}
#[test]
fn test_congestion_constants_positive() {
assert!(CONGESTION_DROP_THRESHOLD > 0);
assert!(CONGESTION_WINDOW_MS > 0);
assert!(CONGESTION_NOTIFY_MIN_INTERVAL_MS > 0);
}
#[test]
fn test_video_tier_lookup_by_index() {
let tier = &VIDEO_QUALITY_TIERS[DEFAULT_VIDEO_TIER_INDEX];
assert_eq!(tier.label, "minimal", "default tier should be 'minimal'");
}
#[test]
fn test_all_video_tiers_have_unique_labels() {
let labels: Vec<&str> = VIDEO_QUALITY_TIERS.iter().map(|t| t.label).collect();
for (i, label) in labels.iter().enumerate() {
for (j, other) in labels.iter().enumerate() {
if i != j {
assert_ne!(label, other, "duplicate video tier label: {}", label);
}
}
}
}
#[test]
fn test_all_audio_tiers_have_unique_labels() {
let labels: Vec<&str> = AUDIO_QUALITY_TIERS.iter().map(|t| t.label).collect();
for (i, label) in labels.iter().enumerate() {
for (j, other) in labels.iter().enumerate() {
if i != j {
assert_ne!(label, other, "duplicate audio tier label: {}", label);
}
}
}
}
}
pub const CONGESTION_DROP_THRESHOLD: u32 = 5;
pub const CONGESTION_WINDOW_MS: u64 = 1000;
pub const CONGESTION_NOTIFY_MIN_INTERVAL_MS: u64 = 1000;