rvoip_rtp_core/api/client/
config.rs

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