webrtc 0.20.0

Async-friendly WebRTC implementation in Rust
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
//! Integration tests for PeerConnection configuration options

use rtc::media_stream::MediaStreamTrack;
use std::sync::Arc;
use std::time::Duration;
use webrtc::media_stream::track_local::static_rtp::TrackLocalStaticRTP;
use webrtc::peer_connection::*;
use webrtc::peer_connection::{
    MediaEngine, RTCBundlePolicy, RTCConfigurationBuilder, RTCIceServer, RTCIceTransportPolicy,
    RTCRtcpMuxPolicy, SettingEngine,
};

mod common;
use common::block_on;

#[derive(Clone)]
struct ConfigTestHandler;

#[async_trait::async_trait]
impl PeerConnectionEventHandler for ConfigTestHandler {}

#[test]
fn test_media_engine_configuration() {
    block_on(async {
        // Test MediaEngine with default codecs
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register default codecs");

        let config = RTCConfigurationBuilder::new().build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Add a track first (needed to create offer with media)
        let track = Arc::new(TrackLocalStaticRTP::new(MediaStreamTrack::new(
            "stream".to_string(),
            "video".to_string(),
            "track".to_string(),
            rtc::rtp_transceiver::rtp_sender::RtpCodecKind::Video,
            vec![],
        )));
        pc.add_track(track).await.expect("Failed to add track");

        // Create offer should work with registered codecs
        let offer = pc.create_offer(None).await.expect("Failed to create offer");
        assert!(!offer.sdp.is_empty(), "Offer SDP should not be empty");
    });
}

#[test]
fn test_setting_engine_ice_timeouts() {
    block_on(async {
        // Test SettingEngine with custom ICE timeouts
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register codecs");

        let mut setting_engine = SettingEngine::default();

        // Configure ICE timeouts
        setting_engine.set_ice_timeouts(
            Some(Duration::from_secs(5)),  // disconnect timeout
            Some(Duration::from_secs(10)), // failed timeout
            Some(Duration::from_secs(1)),  // keepalive interval
        );

        let config = RTCConfigurationBuilder::new().build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_setting_engine(setting_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Should be able to create offer with custom settings
        let offer = pc.create_offer(None).await.expect("Failed to create offer");
        assert!(!offer.sdp.is_empty());
    });
}

#[test]
fn test_setting_engine_replay_protection() {
    block_on(async {
        // Test SettingEngine with custom replay protection windows
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register codecs");

        let mut setting_engine = SettingEngine::default();

        // Configure replay protection window sizes
        setting_engine.set_srtp_replay_protection_window(128);
        setting_engine.set_srtcp_replay_protection_window(64);

        let config = RTCConfigurationBuilder::new().build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_setting_engine(setting_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        let offer = pc.create_offer(None).await.expect("Failed to create offer");
        assert!(!offer.sdp.is_empty());
    });
}

#[test]
fn test_combined_configuration() {
    block_on(async {
        // Test combining MediaEngine and SettingEngine configuration
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register codecs");

        let mut setting_engine = SettingEngine::default();
        setting_engine.set_ice_timeouts(
            Some(Duration::from_secs(7)),
            Some(Duration::from_secs(15)),
            Some(Duration::from_secs(2)),
        );
        setting_engine.set_srtp_replay_protection_window(256);

        let config = RTCConfigurationBuilder::new().build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_setting_engine(setting_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Create and set local description
        let offer = pc.create_offer(None).await.expect("Failed to create offer");
        pc.set_local_description(offer.clone())
            .await
            .expect("Failed to set local description");

        // Verify local description was set
        let local_desc = pc
            .local_description()
            .await
            .expect("Local description should be set");
        assert_eq!(local_desc.sdp, offer.sdp);
    });
}

#[test]
fn test_peer_connection_with_full_configuration() {
    block_on(async {
        // End-to-end test with full configuration
        let mut media_engine_a = MediaEngine::default();
        media_engine_a
            .register_default_codecs()
            .expect("Failed to register codecs");
        let mut media_engine_b = MediaEngine::default();
        media_engine_b
            .register_default_codecs()
            .expect("Failed to register codecs");

        let mut setting_engine_a = SettingEngine::default();
        setting_engine_a.set_ice_timeouts(
            Some(Duration::from_secs(5)),
            Some(Duration::from_secs(10)),
            Some(Duration::from_secs(1)),
        );

        let mut setting_engine_b = SettingEngine::default();
        setting_engine_b.set_ice_timeouts(
            Some(Duration::from_secs(5)),
            Some(Duration::from_secs(10)),
            Some(Duration::from_secs(1)),
        );

        let config_a = RTCConfigurationBuilder::new().build();

        let config_b = RTCConfigurationBuilder::new().build();

        let handler_a = Arc::new(ConfigTestHandler);
        let handler_b = Arc::new(ConfigTestHandler);

        let pc_a = PeerConnectionBuilder::new()
            .with_configuration(config_a)
            .with_media_engine(media_engine_a)
            .with_setting_engine(setting_engine_a)
            .with_handler(handler_a)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();
        let pc_b = PeerConnectionBuilder::new()
            .with_configuration(config_b)
            .with_media_engine(media_engine_b)
            .with_setting_engine(setting_engine_b)
            .with_handler(handler_b)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        let _ = pc_a.create_data_channel("channel1", None).await.unwrap();

        // Perform offer/answer exchange
        let offer = pc_a
            .create_offer(None)
            .await
            .expect("Failed to create offer");
        pc_a.set_local_description(offer.clone())
            .await
            .expect("Failed to set local description");
        pc_b.set_remote_description(offer)
            .await
            .expect("Failed to set remote description");

        let answer = pc_b
            .create_answer(None)
            .await
            .expect("Failed to create answer");
        pc_b.set_local_description(answer.clone())
            .await
            .expect("Failed to set local description");
        pc_a.set_remote_description(answer)
            .await
            .expect("Failed to set remote description");

        // Both peers should have local and remote descriptions set
        assert!(pc_a.local_description().await.is_some());
        assert!(pc_a.remote_description().await.is_some());
        assert!(pc_b.local_description().await.is_some());
        assert!(pc_b.remote_description().await.is_some());
    });
}

#[test]
fn test_media_engine_required_for_tracks() {
    block_on(async {
        // Verify that MediaEngine with codecs is required for adding tracks
        let handler = Arc::new(ConfigTestHandler);

        // Create peer connection with MediaEngine
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register codecs");

        let config = RTCConfigurationBuilder::new().build();

        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Add track
        let track = Arc::new(TrackLocalStaticRTP::new(MediaStreamTrack::new(
            "stream".to_string(),
            "video".to_string(),
            "track".to_string(),
            rtc::rtp_transceiver::rtp_sender::RtpCodecKind::Video,
            vec![],
        )));

        // Should succeed
        let result = pc.add_track(track).await;
        assert!(
            result.is_ok(),
            "Adding track with MediaEngine should succeed"
        );

        // Creating offer should also succeed
        let offer_result = pc.create_offer(None);
        assert!(
            offer_result.await.is_ok(),
            "Creating offer with track should succeed"
        );
    });
}

#[test]
fn test_ice_servers_configuration() {
    block_on(async {
        // Test configuring ICE servers (STUN/TURN)
        let ice_servers = vec![
            RTCIceServer {
                urls: vec!["stun:stun.l.google.com:19302".to_string()],
                username: "".to_string(),
                credential: "".to_string(),
            },
            RTCIceServer {
                urls: vec!["turn:turn.example.com:3478".to_string()],
                username: "user".to_string(),
                credential: "pass".to_string(),
            },
        ];

        let config = RTCConfigurationBuilder::new()
            .with_ice_servers(ice_servers)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Should be able to create offers/answers with ICE servers configured
        let offer = pc.create_offer(None);
        assert!(offer.await.is_ok(), "Should create offer with ICE servers");
    });
}

#[test]
fn test_ice_transport_policy() {
    block_on(async {
        // Test ICE transport policy (all, relay-only, etc.)
        let config_all = RTCConfigurationBuilder::new()
            .with_ice_transport_policy(RTCIceTransportPolicy::All)
            .build();

        let config_relay = RTCConfigurationBuilder::new()
            .with_ice_transport_policy(RTCIceTransportPolicy::Relay)
            .with_ice_servers(vec![RTCIceServer {
                urls: vec!["turn:turn.example.com:3478".to_string()],
                username: "user".to_string(),
                credential: "pass".to_string(),
            }])
            .build();

        let handler_all = Arc::new(ConfigTestHandler);
        let handler_relay = Arc::new(ConfigTestHandler);

        let pc_all = PeerConnectionBuilder::new()
            .with_configuration(config_all)
            .with_handler(handler_all)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();
        let pc_relay = PeerConnectionBuilder::new()
            .with_configuration(config_relay)
            .with_handler(handler_relay)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Both should succeed
        assert!(pc_all.create_offer(None).await.is_ok());
        assert!(pc_relay.create_offer(None).await.is_ok());
    });
}

#[test]
fn test_bundle_policy() {
    block_on(async {
        // Test RTP bundle policy
        let config_balanced = RTCConfigurationBuilder::new()
            .with_bundle_policy(RTCBundlePolicy::Balanced)
            .build();

        let config_max_compat = RTCConfigurationBuilder::new()
            .with_bundle_policy(RTCBundlePolicy::MaxCompat)
            .build();

        let config_max_bundle = RTCConfigurationBuilder::new()
            .with_bundle_policy(RTCBundlePolicy::MaxBundle)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc_balanced = PeerConnectionBuilder::new()
            .with_configuration(config_balanced)
            .with_handler(handler.clone())
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();
        let pc_compat = PeerConnectionBuilder::new()
            .with_configuration(config_max_compat)
            .with_handler(handler.clone())
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();
        let pc_bundle = PeerConnectionBuilder::new()
            .with_configuration(config_max_bundle)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // All should create offers successfully
        assert!(pc_balanced.create_offer(None).await.is_ok());
        assert!(pc_compat.create_offer(None).await.is_ok());
        assert!(pc_bundle.create_offer(None).await.is_ok());
    });
}

#[test]
fn test_rtcp_mux_policy() {
    block_on(async {
        // Test RTCP multiplexing policy
        let config_require = RTCConfigurationBuilder::new()
            .with_rtcp_mux_policy(RTCRtcpMuxPolicy::Require)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config_require)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        let offer = pc.create_offer(None);
        assert!(offer.await.is_ok(), "Should create offer with RTCP mux");
    });
}

#[test]
fn test_peer_identity() {
    block_on(async {
        // Test peer identity configuration
        let config = RTCConfigurationBuilder::new()
            .with_peer_identitys("test-peer-identity".to_string())
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        assert!(pc.create_offer(None).await.is_ok());
    });
}

#[test]
fn test_certificates() {
    block_on(async {
        // Test with custom certificates
        // Note: RTCCertificate::from_pem or generate would be used in real scenarios
        let certificates = vec![]; // Empty for now - real usage would load certs

        let config = RTCConfigurationBuilder::new()
            .with_certificates(certificates)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        assert!(pc.create_offer(None).await.is_ok());
    });
}

#[test]
fn test_ice_candidate_pool_size() {
    block_on(async {
        // Test ICE candidate pool size
        let config = RTCConfigurationBuilder::new()
            .with_ice_candidate_pool_size(5)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        assert!(pc.create_offer(None).await.is_ok());
    });
}

#[test]
fn test_all_configuration_options_combined() {
    block_on(async {
        // Test using all configuration options together
        let mut media_engine = MediaEngine::default();
        media_engine
            .register_default_codecs()
            .expect("Failed to register codecs");

        let mut setting_engine = SettingEngine::default();
        setting_engine.set_ice_timeouts(
            Some(Duration::from_secs(5)),
            Some(Duration::from_secs(10)),
            Some(Duration::from_secs(1)),
        );

        let ice_servers = vec![RTCIceServer {
            urls: vec!["stun:stun.l.google.com:19302".to_string()],
            username: "".to_string(),
            credential: "".to_string(),
        }];

        let config = RTCConfigurationBuilder::new()
            .with_ice_servers(ice_servers)
            .with_ice_transport_policy(RTCIceTransportPolicy::All)
            .with_bundle_policy(RTCBundlePolicy::MaxBundle)
            .with_rtcp_mux_policy(RTCRtcpMuxPolicy::Require)
            .with_peer_identitys("full-config-peer".to_string())
            .with_ice_candidate_pool_size(3)
            .build();

        let handler = Arc::new(ConfigTestHandler);
        let pc = PeerConnectionBuilder::new()
            .with_configuration(config)
            .with_media_engine(media_engine)
            .with_setting_engine(setting_engine)
            .with_handler(handler)
            .with_udp_addrs(vec!["127.0.0.1:0"])
            .build()
            .await
            .unwrap();

        // Add track
        let track = Arc::new(TrackLocalStaticRTP::new(MediaStreamTrack::new(
            "stream".to_string(),
            "video".to_string(),
            "track".to_string(),
            rtc::rtp_transceiver::rtp_sender::RtpCodecKind::Video,
            vec![],
        )));
        pc.add_track(track).await.expect("Failed to add track");

        // Should create offer successfully with all options
        let offer = pc.create_offer(None).await.expect("Failed to create offer");
        assert!(!offer.sdp.is_empty(), "Offer should have SDP content");
    });
}