rustrtc 0.3.46

A high-performance implementation of WebRTC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
use crate::media::depacketizer::{DefaultDepacketizerFactory, DepacketizerFactory};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::sync::Arc;

/// Describes how credentials are conveyed for a given ICE server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum IceCredentialType {
    #[default]
    Password,
    Oauth,
}

/// Mirrors the W3C `RTCIceServer` dictionary.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IceServer {
    pub urls: Vec<String>,
    pub username: Option<String>,
    pub credential: Option<String>,
    #[serde(default)]
    pub credential_type: IceCredentialType,
}

impl IceServer {
    pub fn new<T: Into<Vec<String>>>(urls: T) -> Self {
        Self {
            urls: urls.into(),
            username: None,
            credential: None,
            credential_type: IceCredentialType::default(),
        }
    }

    pub fn with_credential(
        mut self,
        username: impl Into<String>,
        credential: impl Into<String>,
    ) -> Self {
        self.username = Some(username.into());
        self.credential = Some(credential.into());
        self
    }

    pub fn credential_type(mut self, kind: IceCredentialType) -> Self {
        self.credential_type = kind;
        self
    }
}

impl Default for IceServer {
    fn default() -> Self {
        Self::new(Vec::new())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum IceTransportPolicy {
    #[default]
    All,
    Relay,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum BundlePolicy {
    #[default]
    Balanced,
    MaxCompat,
    MaxBundle,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum RtcpMuxPolicy {
    #[default]
    Require,
    Negotiate,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum TransportMode {
    #[default]
    WebRtc,
    Srtp,
    Rtp,
}

/// Strategy for dropping packets when buffer is full.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum BufferDropStrategy {
    #[default]
    DropNew,
    DropOldest,
}

/// Tracks user-supplied certificate material.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct CertificateConfig {
    pub pem_chain: Vec<String>,
    pub private_key_pem: Option<String>,
}

/// Configuration for audio/video codecs and parameters.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AudioCapability {
    pub payload_type: u8,
    pub codec_name: String,
    pub clock_rate: u32,
    pub channels: u8,
    pub fmtp: Option<String>,
    pub rtcp_fbs: Vec<String>,
}

impl Default for AudioCapability {
    fn default() -> Self {
        Self {
            payload_type: 111,
            codec_name: "opus".to_string(),
            clock_rate: 48000,
            channels: 2,
            fmtp: Some("minptime=10;useinbandfec=1".to_string()),
            rtcp_fbs: vec![],
        }
    }
}

impl AudioCapability {
    pub fn opus() -> Self {
        Self::default()
    }

    pub fn pcmu() -> Self {
        Self {
            payload_type: 0,
            codec_name: "PCMU".to_string(),
            clock_rate: 8000,
            channels: 1,
            fmtp: None,
            rtcp_fbs: vec![],
        }
    }

    pub fn pcma() -> Self {
        Self {
            payload_type: 8,
            codec_name: "PCMA".to_string(),
            clock_rate: 8000,
            channels: 1,
            fmtp: None,
            rtcp_fbs: vec![],
        }
    }

    pub fn g722() -> Self {
        Self {
            payload_type: 9,
            codec_name: "G722".to_string(),
            clock_rate: 8000,
            channels: 1,
            fmtp: None,
            rtcp_fbs: vec![],
        }
    }

    pub fn g729() -> Self {
        Self {
            payload_type: 18,
            codec_name: "G729".to_string(),
            clock_rate: 8000,
            channels: 1,
            fmtp: None,
            rtcp_fbs: vec![],
        }
    }

    pub fn telephone_event() -> Self {
        Self {
            payload_type: 101,
            codec_name: "telephone-event".to_string(),
            clock_rate: 8000,
            channels: 1,
            fmtp: Some("0-16".to_string()),
            rtcp_fbs: vec![],
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct VideoCapability {
    pub payload_type: u8,
    pub codec_name: String,
    pub clock_rate: u32,
    pub fmtp: Option<String>,
    pub rtcp_fbs: Vec<String>,
}

impl Default for VideoCapability {
    fn default() -> Self {
        Self {
            payload_type: 96,
            codec_name: "VP8".to_string(),
            clock_rate: 90000,
            fmtp: None,
            rtcp_fbs: vec![
                "nack".to_string(),
                "nack pli".to_string(),
                "ccm fir".to_string(),
                "goog-remb".to_string(),
                "transport-cc".to_string(),
            ],
        }
    }
}

impl VideoCapability {
    pub fn h264() -> Self {
        Self {
            payload_type: 96,
            codec_name: "H264".to_string(),
            clock_rate: 90000,
            fmtp: Some("packetization-mode=1;profile-level-id=42e01f".to_string()),
            rtcp_fbs: vec!["nack pli".to_string(), "ccm fir".to_string()],
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ApplicationCapability {
    pub sctp_port: u16,
}

impl Default for ApplicationCapability {
    fn default() -> Self {
        Self { sctp_port: 5000 }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MediaCapabilities {
    pub audio: Vec<AudioCapability>,
    pub video: Vec<VideoCapability>,
    pub application: Option<ApplicationCapability>,
}

impl Default for MediaCapabilities {
    fn default() -> Self {
        Self {
            audio: vec![AudioCapability::opus(), AudioCapability::pcmu()],
            video: vec![VideoCapability::default()],
            application: Some(ApplicationCapability::default()),
        }
    }
}

#[derive(Clone)]
pub struct DepacketizerStrategy {
    pub factory: Arc<dyn DepacketizerFactory>,
}

impl Default for DepacketizerStrategy {
    fn default() -> Self {
        Self {
            factory: Arc::new(DefaultDepacketizerFactory),
        }
    }
}

impl Debug for DepacketizerStrategy {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.factory.fmt(f)
    }
}

impl PartialEq for DepacketizerStrategy {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.factory, &other.factory)
    }
}

impl Eq for DepacketizerStrategy {}

fn default_rtp_buffer_capacity() -> usize {
    100
}

fn default_buffer_stats_log_interval() -> std::time::Duration {
    std::time::Duration::from_secs(10)
}

/// Controls SDP generation compatibility for interoperability with legacy SIP endpoints.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum SdpCompatibilityMode {
    /// Standard WebRTC / RFC-compliant SDP output (default).
    #[default]
    Standard,
    /// Compatibility mode for legacy SIP endpoints (e.g. Linphone):
    /// omits `a=mid` unless BUNDLE is active, omits `a=rtcp-mux`.
    LegacySip,
}

fn default_enable_upnp() -> bool {
    false
}

fn default_upnp_lease_duration() -> u32 {
    3600
}

/// Primary configuration for a `PeerConnection`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RtcConfiguration {
    pub ice_servers: Vec<IceServer>,
    pub ice_transport_policy: IceTransportPolicy,
    pub bundle_policy: BundlePolicy,
    pub rtcp_mux_policy: RtcpMuxPolicy,
    pub certificates: Vec<CertificateConfig>,
    pub transport_mode: TransportMode,
    pub nack_buffer_size: usize,
    pub media_capabilities: Option<MediaCapabilities>,
    pub external_ip: Option<String>,
    pub bind_ip: Option<String>,
    pub disable_ipv6: bool,
    pub ssrc_start: u32,
    pub stun_timeout: std::time::Duration,
    /// Timeout for the ICE nomination binding check (USE-CANDIDATE).
    /// This should be larger than `stun_timeout` to allow more retransmissions
    /// and reduce the probability of nomination failures under packet loss.
    pub nomination_timeout: std::time::Duration,
    pub ice_connection_timeout: std::time::Duration,
    pub sctp_rto_initial: std::time::Duration,
    pub sctp_rto_min: std::time::Duration,
    pub sctp_rto_max: std::time::Duration,
    pub sctp_max_association_retransmits: u32,
    pub sctp_receive_window: usize,
    pub sctp_heartbeat_interval: std::time::Duration,
    pub sctp_max_heartbeat_failures: u32,
    pub sctp_max_burst: usize,
    pub sctp_max_cwnd: usize,
    pub dtls_buffer_size: usize,
    pub rtp_start_port: Option<u16>,
    pub rtp_end_port: Option<u16>,
    pub enable_latching: bool,
    pub enable_ice_lite: bool,
    /// Enable UPnP IGD for automatic port mapping
    #[serde(default = "default_enable_upnp")]
    pub enable_upnp: bool,
    /// UPnP port mapping lease duration in seconds
    #[serde(default = "default_upnp_lease_duration")]
    pub upnp_lease_duration: u32,
    #[serde(skip, default)]
    pub depacketizer_strategy: DepacketizerStrategy,
    #[serde(default = "default_rtp_buffer_capacity")]
    pub rtp_buffer_capacity: usize,
    #[serde(default)]
    pub buffer_drop_strategy: BufferDropStrategy,
    #[serde(default = "default_buffer_stats_log_interval")]
    pub buffer_stats_log_interval: std::time::Duration,
    /// SDP generation compatibility mode.
    #[serde(default)]
    pub sdp_compatibility: SdpCompatibilityMode,
}

impl Default for RtcConfiguration {
    fn default() -> Self {
        Self {
            ice_servers: Vec::new(),
            ice_transport_policy: IceTransportPolicy::default(),
            bundle_policy: BundlePolicy::default(),
            rtcp_mux_policy: RtcpMuxPolicy::default(),
            certificates: Vec::new(),
            transport_mode: TransportMode::default(),
            nack_buffer_size: 200,
            media_capabilities: None,
            external_ip: None,
            bind_ip: None,
            disable_ipv6: false,
            ssrc_start: 10000,
            stun_timeout: std::time::Duration::from_secs(5),
            nomination_timeout: std::time::Duration::from_secs(10),
            ice_connection_timeout: std::time::Duration::from_secs(30),
            sctp_rto_initial: std::time::Duration::from_secs(3),
            sctp_rto_min: std::time::Duration::from_secs(1),
            sctp_rto_max: std::time::Duration::from_secs(60),
            sctp_max_association_retransmits: 20,
            sctp_receive_window: 128 * 1024, // 128KB - reduced for lower memory footprint
            sctp_heartbeat_interval: std::time::Duration::from_secs(15),
            sctp_max_heartbeat_failures: 4,
            sctp_max_burst: 0,         // 0 = use default heuristic
            sctp_max_cwnd: 256 * 1024, // 256 KB
            dtls_buffer_size: 2048,
            rtp_start_port: None,
            rtp_end_port: None,
            enable_latching: false,
            enable_ice_lite: false,
            enable_upnp: default_enable_upnp(),
            upnp_lease_duration: default_upnp_lease_duration(),
            depacketizer_strategy: DepacketizerStrategy::default(),
            rtp_buffer_capacity: default_rtp_buffer_capacity(),
            buffer_drop_strategy: BufferDropStrategy::default(),
            buffer_stats_log_interval: default_buffer_stats_log_interval(),
            sdp_compatibility: SdpCompatibilityMode::default(),
        }
    }
}

pub struct RtcConfigurationBuilder {
    inner: RtcConfiguration,
}

impl Default for RtcConfigurationBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl RtcConfigurationBuilder {
    pub fn new() -> Self {
        Self {
            inner: RtcConfiguration::default(),
        }
    }

    pub fn enable_latching(mut self, enable: bool) -> Self {
        self.inner.enable_latching = enable;
        self
    }

    pub fn enable_ice_lite(mut self, enable: bool) -> Self {
        self.inner.enable_ice_lite = enable;
        self
    }

    pub fn enable_upnp(mut self, enable: bool) -> Self {
        self.inner.enable_upnp = enable;
        self
    }

    pub fn upnp_lease_duration(mut self, duration_secs: u32) -> Self {
        self.inner.upnp_lease_duration = duration_secs;
        self
    }

    pub fn ice_server(mut self, server: IceServer) -> Self {
        self.inner.ice_servers.push(server);
        self
    }

    pub fn ice_transport_policy(mut self, policy: IceTransportPolicy) -> Self {
        self.inner.ice_transport_policy = policy;
        self
    }

    pub fn bundle_policy(mut self, policy: BundlePolicy) -> Self {
        self.inner.bundle_policy = policy;
        self
    }

    pub fn rtcp_mux_policy(mut self, policy: RtcpMuxPolicy) -> Self {
        self.inner.rtcp_mux_policy = policy;
        self
    }

    pub fn certificate(mut self, cert: CertificateConfig) -> Self {
        self.inner.certificates.push(cert);
        self
    }

    pub fn transport_mode(mut self, mode: TransportMode) -> Self {
        self.inner.transport_mode = mode;
        self
    }

    pub fn media_capabilities(mut self, capabilities: MediaCapabilities) -> Self {
        self.inner.media_capabilities = Some(capabilities);
        self
    }

    pub fn external_ip(mut self, ip: String) -> Self {
        self.inner.external_ip = Some(ip);
        self
    }

    pub fn bind_ip(mut self, ip: String) -> Self {
        self.inner.bind_ip = Some(ip);
        self
    }

    pub fn disable_ipv6(mut self, disable: bool) -> Self {
        self.inner.disable_ipv6 = disable;
        self
    }

    pub fn ssrc_start(mut self, start: u32) -> Self {
        self.inner.ssrc_start = start;
        self
    }

    pub fn stun_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.inner.stun_timeout = timeout;
        self
    }

    pub fn nomination_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.inner.nomination_timeout = timeout;
        self
    }

    pub fn rtp_port_range(mut self, start: u16, end: u16) -> Self {
        self.inner.rtp_start_port = Some(start);
        self.inner.rtp_end_port = Some(end);
        self
    }

    pub fn dtls_buffer_size(mut self, size: usize) -> Self {
        self.inner.dtls_buffer_size = size;
        self
    }

    pub fn sctp_rto_initial(mut self, duration: std::time::Duration) -> Self {
        self.inner.sctp_rto_initial = duration;
        self
    }

    pub fn sctp_rto_min(mut self, duration: std::time::Duration) -> Self {
        self.inner.sctp_rto_min = duration;
        self
    }

    pub fn sctp_rto_max(mut self, duration: std::time::Duration) -> Self {
        self.inner.sctp_rto_max = duration;
        self
    }

    pub fn sctp_max_association_retransmits(mut self, count: u32) -> Self {
        self.inner.sctp_max_association_retransmits = count;
        self
    }

    pub fn sctp_receive_window(mut self, size: usize) -> Self {
        self.inner.sctp_receive_window = size;
        self
    }

    pub fn sctp_heartbeat_interval(mut self, duration: std::time::Duration) -> Self {
        self.inner.sctp_heartbeat_interval = duration;
        self
    }

    pub fn sctp_max_heartbeat_failures(mut self, count: u32) -> Self {
        self.inner.sctp_max_heartbeat_failures = count;
        self
    }

    /// Set the maximum burst size for SCTP in number of MTU-sized packets.
    /// 0 means use the default heuristic (16 packets normal, 4 in recovery).
    /// For rate-limited TURN relays, a value of 2-4 can reduce burst-induced
    /// packet loss.
    pub fn sctp_max_burst(mut self, packets: usize) -> Self {
        self.inner.sctp_max_burst = packets;
        self
    }

    /// Set the maximum congestion window size in bytes.
    /// Default is 256 KB. For high-latency TURN relays, consider 512KB-1MB.
    pub fn sctp_max_cwnd(mut self, size: usize) -> Self {
        self.inner.sctp_max_cwnd = size;
        self
    }

    pub fn ice_connection_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.inner.ice_connection_timeout = timeout;
        self
    }

    pub fn rtp_buffer_capacity(mut self, capacity: usize) -> Self {
        self.inner.rtp_buffer_capacity = capacity;
        self
    }

    pub fn buffer_drop_strategy(mut self, strategy: BufferDropStrategy) -> Self {
        self.inner.buffer_drop_strategy = strategy;
        self
    }

    pub fn buffer_stats_log_interval(mut self, interval: std::time::Duration) -> Self {
        self.inner.buffer_stats_log_interval = interval;
        self
    }

    pub fn sdp_compatibility(mut self, mode: SdpCompatibilityMode) -> Self {
        self.inner.sdp_compatibility = mode;
        self
    }

    pub fn build(self) -> RtcConfiguration {
        self.inner
    }
}

impl From<RtcConfigurationBuilder> for RtcConfiguration {
    fn from(builder: RtcConfigurationBuilder) -> Self {
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_rtc_configuration_defaults() {
        let config = RtcConfiguration::default();
        assert_eq!(config.ice_connection_timeout, Duration::from_secs(30));
        assert_eq!(config.sctp_rto_initial, Duration::from_secs(3));
        assert_eq!(config.sctp_rto_min, Duration::from_secs(1));
        assert_eq!(config.sctp_rto_max, Duration::from_secs(60));
        assert_eq!(config.sctp_max_association_retransmits, 20);
        assert_eq!(config.sctp_heartbeat_interval, Duration::from_secs(15));
        assert_eq!(config.sctp_max_heartbeat_failures, 4);
        assert_eq!(config.sctp_max_burst, 0);
        assert_eq!(config.sctp_max_cwnd, 256 * 1024);
        assert_eq!(config.rtp_buffer_capacity, 100);
        assert_eq!(config.buffer_drop_strategy, BufferDropStrategy::DropNew);
        assert_eq!(config.buffer_stats_log_interval, Duration::from_secs(10));
    }

    #[test]
    fn test_rtc_configuration_builder() {
        let config = RtcConfigurationBuilder::new()
            .stun_timeout(Duration::from_secs(10))
            .build();
        assert_eq!(config.stun_timeout, Duration::from_secs(10));
        // Verify other defaults are still there
        assert_eq!(config.ice_connection_timeout, Duration::from_secs(30));
    }

    #[test]
    fn test_buffer_config_builder() {
        let config = RtcConfigurationBuilder::new()
            .rtp_buffer_capacity(200)
            .buffer_drop_strategy(BufferDropStrategy::DropOldest)
            .buffer_stats_log_interval(Duration::from_secs(5))
            .build();
        assert_eq!(config.rtp_buffer_capacity, 200);
        assert_eq!(config.buffer_drop_strategy, BufferDropStrategy::DropOldest);
        assert_eq!(config.buffer_stats_log_interval, Duration::from_secs(5));
    }

    #[test]
    fn test_sctp_builder_methods() {
        let config = RtcConfigurationBuilder::new()
            .sctp_rto_initial(Duration::from_millis(500))
            .sctp_rto_min(Duration::from_millis(200))
            .sctp_rto_max(Duration::from_secs(10))
            .sctp_max_association_retransmits(30)
            .sctp_receive_window(512 * 1024)
            .sctp_heartbeat_interval(Duration::from_secs(10))
            .sctp_max_heartbeat_failures(8)
            .sctp_max_burst(4)
            .sctp_max_cwnd(512 * 1024)
            .ice_connection_timeout(Duration::from_secs(60))
            .build();

        assert_eq!(config.sctp_rto_initial, Duration::from_millis(500));
        assert_eq!(config.sctp_rto_min, Duration::from_millis(200));
        assert_eq!(config.sctp_rto_max, Duration::from_secs(10));
        assert_eq!(config.sctp_max_association_retransmits, 30);
        assert_eq!(config.sctp_receive_window, 512 * 1024);
        assert_eq!(config.sctp_heartbeat_interval, Duration::from_secs(10));
        assert_eq!(config.sctp_max_heartbeat_failures, 8);
        assert_eq!(config.sctp_max_burst, 4);
        assert_eq!(config.sctp_max_cwnd, 512 * 1024);
        assert_eq!(config.ice_connection_timeout, Duration::from_secs(60));
    }

    #[test]
    fn test_turn_optimized_config() {
        // Verify a TURN-optimized configuration can be expressed cleanly
        let config = RtcConfigurationBuilder::new()
            .sctp_rto_initial(Duration::from_millis(500))
            .sctp_rto_min(Duration::from_millis(200))
            .sctp_rto_max(Duration::from_secs(10))
            .sctp_max_association_retransmits(30)
            .sctp_max_heartbeat_failures(8)
            .sctp_max_burst(4)
            .stun_timeout(Duration::from_secs(10))
            .nomination_timeout(Duration::from_secs(20))
            .build();

        // Verify the TURN-optimized values are more aggressive than defaults
        let defaults = RtcConfiguration::default();
        assert!(config.sctp_rto_initial < defaults.sctp_rto_initial);
        assert!(config.sctp_rto_min < defaults.sctp_rto_min);
        assert!(config.sctp_rto_max < defaults.sctp_rto_max);
        assert!(
            config.sctp_max_association_retransmits > defaults.sctp_max_association_retransmits
        );
        assert!(config.sctp_max_heartbeat_failures > defaults.sctp_max_heartbeat_failures);
        assert!(config.sctp_max_burst > 0); // Explicit burst limit vs. heuristic
    }

    #[test]
    fn test_upnp_defaults() {
        let config = RtcConfiguration::default();
        assert!(!config.enable_upnp, "UPnP should be disabled by default");
        assert_eq!(config.upnp_lease_duration, 3600);
    }

    #[test]
    fn test_upnp_builder_methods() {
        let config = RtcConfigurationBuilder::new()
            .enable_upnp(false)
            .upnp_lease_duration(7200)
            .build();
        assert!(!config.enable_upnp);
        assert_eq!(config.upnp_lease_duration, 7200);
    }

    #[test]
    fn test_upnp_optimized_config() {
        let config = RtcConfigurationBuilder::new()
            .enable_upnp(true)
            .upnp_lease_duration(1800)
            .build();

        assert!(config.enable_upnp);
        assert_eq!(config.upnp_lease_duration, 1800);

        // Verify defaults remain for other options
        let defaults = RtcConfiguration::default();
        assert_eq!(
            config.ice_connection_timeout,
            defaults.ice_connection_timeout
        );
    }
}