rvoip_rtp_core/api/server/
config.rs

1//! Server configuration
2//!
3//! This module defines server-specific configuration types.
4
5use std::net::SocketAddr;
6use crate::api::server::security::ServerSecurityConfig;
7use crate::api::common::extension::ExtensionFormat;
8use crate::buffer::{TransmitBufferConfig, BufferLimits};
9
10/// Server configuration
11#[derive(Debug, Clone)]
12pub struct ServerConfig {
13    /// Local address to bind to
14    pub local_address: SocketAddr,
15    /// Default payload type
16    pub default_payload_type: u8,
17    /// Clock rate in Hz
18    pub clock_rate: u32,
19    /// Security configuration
20    pub security_config: ServerSecurityConfig,
21    /// Jitter buffer size in packets
22    pub jitter_buffer_size: u32,
23    /// Maximum packet age in milliseconds
24    pub jitter_max_packet_age_ms: u32,
25    /// Enable jitter buffer
26    pub enable_jitter_buffer: bool,
27    /// Maximum number of clients
28    pub max_clients: usize,
29    /// Enable RTCP multiplexing (RFC 5761)
30    pub rtcp_mux: bool,
31    /// Enable media synchronization features (optional)
32    pub media_sync_enabled: Option<bool>,
33    /// Enable SSRC demultiplexing for handling multiple streams
34    pub ssrc_demultiplexing_enabled: Option<bool>,
35    /// Enable CSRC management for conferencing scenarios
36    pub csrc_management_enabled: bool,
37    /// Enable header extensions support (RFC 8285)
38    pub header_extensions_enabled: bool,
39    /// Header extension format (One-byte or Two-byte)
40    pub header_extension_format: ExtensionFormat,
41    /// Transmit buffer configuration
42    pub transmit_buffer_config: TransmitBufferConfig,
43    /// Buffer limits
44    pub buffer_limits: BufferLimits,
45    /// Enable high-performance buffers
46    pub high_performance_buffers_enabled: bool,
47}
48
49/// Builder for ServerConfig
50#[derive(Debug, Clone)]
51pub struct ServerConfigBuilder {
52    /// Server configuration being built
53    config: ServerConfig,
54}
55
56impl ServerConfigBuilder {
57    /// Create a new server config builder with default values
58    pub fn new() -> Self {
59        Self {
60            config: ServerConfig {
61                local_address: "0.0.0.0:0".parse().unwrap(),
62                default_payload_type: 0,
63                clock_rate: 8000,
64                security_config: ServerSecurityConfig::default(),
65                jitter_buffer_size: 100,
66                jitter_max_packet_age_ms: 500,
67                enable_jitter_buffer: true,
68                max_clients: 100,
69                rtcp_mux: false, // Disabled by default
70                media_sync_enabled: None, // Optional, defaults to None
71                ssrc_demultiplexing_enabled: None, // Optional, defaults to None
72                csrc_management_enabled: false, // Disabled by default
73                header_extensions_enabled: false, // Disabled by default
74                header_extension_format: ExtensionFormat::OneByte, // One-byte header is standard
75                transmit_buffer_config: TransmitBufferConfig::default(),
76                buffer_limits: BufferLimits {
77                    max_packets_per_stream: 500,
78                    max_packet_size: 1500,
79                    max_memory: 100 * 1024 * 1024, // 100 MB default for server (more than client)
80                },
81                high_performance_buffers_enabled: false,
82            },
83        }
84    }
85    
86    /// Create a builder with WebRTC-optimized defaults
87    pub fn webrtc() -> Self {
88        let mut builder = Self::new();
89        builder.config.security_config.security_mode = crate::api::common::config::SecurityMode::DtlsSrtp;
90        builder.config.rtcp_mux = true; // WebRTC typically uses RTCP-MUX
91        builder.config.header_extensions_enabled = true; // WebRTC makes extensive use of header extensions
92        builder
93    }
94    
95    /// Create a builder with SIP-optimized defaults
96    pub fn sip() -> Self {
97        let mut builder = Self::new();
98        builder.config.security_config.security_mode = crate::api::common::config::SecurityMode::Srtp;
99        builder.config.rtcp_mux = false; // Traditional SIP doesn't use RTCP-MUX by default
100        builder
101    }
102    
103    /// Set the local address
104    pub fn local_address(mut self, addr: SocketAddr) -> Self {
105        self.config.local_address = addr;
106        self
107    }
108    
109    /// Set the default payload type
110    pub fn default_payload_type(mut self, pt: u8) -> Self {
111        self.config.default_payload_type = pt;
112        self
113    }
114    
115    /// Set the clock rate
116    pub fn clock_rate(mut self, rate: u32) -> Self {
117        self.config.clock_rate = rate;
118        self
119    }
120    
121    /// Set the security configuration
122    pub fn security_config(mut self, config: ServerSecurityConfig) -> Self {
123        self.config.security_config = config;
124        self
125    }
126    
127    /// Set the jitter buffer size
128    pub fn jitter_buffer_size(mut self, size: u32) -> Self {
129        self.config.jitter_buffer_size = size;
130        self
131    }
132    
133    /// Set the maximum packet age
134    pub fn jitter_max_packet_age_ms(mut self, age: u32) -> Self {
135        self.config.jitter_max_packet_age_ms = age;
136        self
137    }
138    
139    /// Enable or disable the jitter buffer
140    pub fn enable_jitter_buffer(mut self, enable: bool) -> Self {
141        self.config.enable_jitter_buffer = enable;
142        self
143    }
144    
145    /// Set the maximum number of clients
146    pub fn max_clients(mut self, max: usize) -> Self {
147        self.config.max_clients = max;
148        self
149    }
150    
151    /// Enable or disable RTCP multiplexing (RFC 5761)
152    pub fn rtcp_mux(mut self, enable: bool) -> Self {
153        self.config.rtcp_mux = enable;
154        self
155    }
156    
157    /// Enable or disable media synchronization features
158    pub fn media_sync_enabled(mut self, enable: bool) -> Self {
159        self.config.media_sync_enabled = Some(enable);
160        self
161    }
162    
163    /// Enable or disable SSRC demultiplexing for handling multiple streams
164    pub fn ssrc_demultiplexing_enabled(mut self, enable: bool) -> Self {
165        self.config.ssrc_demultiplexing_enabled = Some(enable);
166        self
167    }
168    
169    /// Enable or disable CSRC management for conferencing scenarios
170    pub fn csrc_management_enabled(mut self, enable: bool) -> Self {
171        self.config.csrc_management_enabled = enable;
172        self
173    }
174    
175    /// Enable or disable header extensions support (RFC 8285)
176    pub fn header_extensions_enabled(mut self, enable: bool) -> Self {
177        self.config.header_extensions_enabled = enable;
178        self
179    }
180    
181    /// Set the header extension format (One-byte or Two-byte)
182    pub fn header_extension_format(mut self, format: ExtensionFormat) -> Self {
183        self.config.header_extension_format = format;
184        self
185    }
186    
187    /// Set the transmit buffer configuration
188    pub fn transmit_buffer_config(mut self, config: TransmitBufferConfig) -> Self {
189        self.config.transmit_buffer_config = config;
190        self
191    }
192    
193    /// Set the buffer limits
194    pub fn buffer_limits(mut self, limits: BufferLimits) -> Self {
195        self.config.buffer_limits = limits;
196        self
197    }
198    
199    /// Enable or disable high-performance buffers
200    pub fn high_performance_buffers_enabled(mut self, enabled: bool) -> Self {
201        self.config.high_performance_buffers_enabled = enabled;
202        self
203    }
204    
205    /// Build the server configuration
206    pub fn build(self) -> Result<ServerConfig, crate::api::common::error::MediaTransportError> {
207        // Validate configuration
208        if self.config.max_clients == 0 {
209            return Err(crate::api::common::error::MediaTransportError::ConfigError(
210                "Maximum number of clients cannot be zero".to_string(),
211            ));
212        }
213        
214        Ok(self.config)
215    }
216}