-
Notifications
You must be signed in to change notification settings - Fork 50
/
data-channel-direct.rs
101 lines (77 loc) · 2.66 KB
/
data-channel-direct.rs
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
use std::net::Ipv4Addr;
use std::time::Duration;
use str0m::channel::ChannelConfig;
use str0m::{Candidate, Event, RtcConfig, RtcError};
use tracing::info_span;
mod common;
use common::{init_log, progress, TestRtc};
#[test]
pub fn data_channel_direct() -> Result<(), RtcError> {
init_log();
let mut l = TestRtc::new(info_span!("L"));
let rtc_r = RtcConfig::new().set_ice_lite(true).build();
let mut r = TestRtc::new_with_rtc(info_span!("R"), rtc_r);
let host1 = Candidate::host((Ipv4Addr::new(1, 1, 1, 1), 1000).into(), "udp")?;
let host2 = Candidate::host((Ipv4Addr::new(2, 2, 2, 2), 2000).into(), "udp")?;
l.add_local_candidate(host1.clone());
l.add_remote_candidate(host2.clone());
r.add_local_candidate(host2);
r.add_remote_candidate(host1);
let finger_l = l.direct_api().local_dtls_fingerprint();
let finger_r = r.direct_api().local_dtls_fingerprint();
l.direct_api().set_remote_fingerprint(finger_r);
r.direct_api().set_remote_fingerprint(finger_l);
let creds_l = l.direct_api().local_ice_credentials();
let creds_r = r.direct_api().local_ice_credentials();
l.direct_api().set_remote_ice_credentials(creds_r);
r.direct_api().set_remote_ice_credentials(creds_l);
l.direct_api().set_ice_controlling(true);
r.direct_api().set_ice_controlling(false);
l.direct_api().start_dtls(true).unwrap();
r.direct_api().start_dtls(false).unwrap();
l.direct_api().start_sctp(true);
r.direct_api().start_sctp(false);
let config = ChannelConfig {
negotiated: Some(1),
label: "my-chan".into(),
..Default::default()
};
let cid = l.direct_api().create_data_channel(config.clone());
r.direct_api().create_data_channel(config);
loop {
if l.is_connected() || r.is_connected() {
break;
}
progress(&mut l, &mut r)?;
}
let max = l.last.max(r.last);
l.last = max;
r.last = max;
loop {
if let Some(mut chan) = l.channel(cid) {
chan.write(false, "Hello world! ".as_bytes())
.expect("to write string");
}
progress(&mut l, &mut r)?;
if l.duration() > Duration::from_secs(10) {
break;
}
}
l.direct_api().close_data_channel(cid);
loop {
progress(&mut l, &mut r)?;
if l.duration() > Duration::from_secs(12) {
break;
}
}
assert!(l
.events
.iter()
.any(|(_, event)| event == &Event::ChannelOpen(cid, "my-chan".into())));
assert!(r.events.len() > 120);
assert!(l
.events
.iter()
.any(|(_, event)| event == &Event::ChannelClose(cid)));
Ok(())
}