web-datachannel 0.2.0

Rust wrapper for WASM 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
use wasm_bindgen::JsCast as _;

pub struct PeerConnection {
    pc: web_sys::RtcPeerConnection,
}

#[derive(thiserror::Error, Debug)]
pub struct Error(js_sys::Error);

unsafe impl Send for Error {}
unsafe impl Sync for Error {}

impl From<wasm_bindgen::JsValue> for Error {
    fn from(value: wasm_bindgen::JsValue) -> Self {
        Self(value.into())
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s: String = self.0.to_string().into();
        write!(f, "{s}")
    }
}

pub type SdpType = web_sys::RtcSdpType;
pub type IceGatheringState = web_sys::RtcIceGatheringState;
pub type PeerConnectionState = web_sys::RtcPeerConnectionState;
pub type IceTransportPolicy = web_sys::RtcIceTransportPolicy;

#[derive(Debug, Clone)]
pub struct Description {
    pub type_: SdpType,
    pub sdp: String,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct IceServer {
    pub urls: Vec<String>,
    pub username: Option<String>,
    pub credential: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Configuration {
    pub ice_servers: Vec<IceServer>,
    pub ice_transport_policy: IceTransportPolicy,
}

impl Default for Configuration {
    fn default() -> Self {
        Self {
            ice_servers: Default::default(),
            ice_transport_policy: IceTransportPolicy::All,
        }
    }
}

unsafe impl Send for PeerConnection {}
unsafe impl Sync for PeerConnection {}

impl PeerConnection {
    pub fn new(configuration: Configuration) -> Result<Self, Error> {
        let mut raw = web_sys::RtcConfiguration::new();
        raw.ice_servers(&serde_wasm_bindgen::to_value(&configuration.ice_servers).unwrap());
        raw.ice_transport_policy(configuration.ice_transport_policy);
        Ok(Self {
            pc: web_sys::RtcPeerConnection::new_with_configuration(&raw)?,
        })
    }

    pub fn close(&self) {
        self.pc.close()
    }

    pub async fn create_offer(&self) -> Result<Description, Error> {
        let raw = web_sys::RtcSessionDescription::from(
            wasm_bindgen_futures::JsFuture::from(self.pc.create_offer()).await?,
        );

        Ok(Description {
            type_: raw.type_(),
            sdp: raw.sdp(),
        })
    }

    pub async fn create_answer(&self) -> Result<Description, Error> {
        let raw = web_sys::RtcSessionDescription::from(
            wasm_bindgen_futures::JsFuture::from(self.pc.create_answer()).await?,
        );

        Ok(Description {
            type_: raw.type_(),
            sdp: raw.sdp(),
        })
    }

    pub async fn set_local_description(&self, description: &Description) -> Result<(), Error> {
        let mut raw = web_sys::RtcSessionDescriptionInit::new(description.type_);
        raw.sdp(&description.sdp);
        wasm_bindgen_futures::JsFuture::from(self.pc.set_local_description(&raw)).await?;
        Ok(())
    }

    pub async fn set_remote_description(&self, description: &Description) -> Result<(), Error> {
        let mut raw = web_sys::RtcSessionDescriptionInit::new(description.type_);
        raw.sdp(&description.sdp);
        wasm_bindgen_futures::JsFuture::from(self.pc.set_remote_description(&raw)).await?;
        Ok(())
    }

    pub fn create_data_channel(
        &self,
        label: &str,
        options: DataChannelOptions,
    ) -> Result<DataChannel, Error> {
        let mut raw = web_sys::RtcDataChannelInit::new();
        raw.ordered(options.ordered);
        if let Some(v) = options.max_packet_life_time {
            raw.max_packet_life_time(v);
        }
        if let Some(v) = options.max_retransmits {
            raw.max_retransmits(v);
        }
        raw.protocol(&options.protocol);
        raw.negotiated(options.negotiated);
        if let Some(v) = options.id {
            raw.id(v);
        }
        Ok(DataChannel {
            dc: self
                .pc
                .create_data_channel_with_data_channel_dict(label, &raw),
        })
    }

    pub fn set_on_ice_candidate(&self, cb: Option<impl Fn(Option<&str>) + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(
                move |ev: web_sys::RtcPeerConnectionIceEvent| {
                    cb(ev
                        .candidate()
                        .map(|cand| cand.candidate())
                        .as_ref()
                        .map(|v| v.as_str()));
                },
            )
        });
        self.pc
            .set_onicecandidate(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_ice_gathering_state_change(
        &self,
        cb: Option<impl Fn(IceGatheringState) + Send + Sync + 'static>,
    ) {
        let pc = self.pc.clone();
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_ev: web_sys::Event| {
                cb(pc.ice_gathering_state());
            })
        });
        self.pc
            .set_onicegatheringstatechange(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_connection_state_change(
        &self,
        cb: Option<impl Fn(PeerConnectionState) + Send + Sync + 'static>,
    ) {
        let pc = self.pc.clone();
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_ev: web_sys::Event| {
                cb(pc.connection_state());
            })
        });
        self.pc
            .set_onconnectionstatechange(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_data_channel(&self, cb: Option<impl Fn(DataChannel) + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(
                move |ev: web_sys::RtcDataChannelEvent| {
                    cb(DataChannel { dc: ev.channel() });
                },
            )
        });
        self.pc
            .set_ondatachannel(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn local_description(&self) -> Option<Description> {
        self.pc.local_description().map(|v| Description {
            type_: v.type_(),
            sdp: v.sdp(),
        })
    }

    pub fn remote_description(&self) -> Option<Description> {
        self.pc.remote_description().map(|v| Description {
            type_: v.type_(),
            sdp: v.sdp(),
        })
    }

    pub async fn add_ice_candidate(&self, cand: Option<&str>) -> Result<(), crate::Error> {
        wasm_bindgen_futures::JsFuture::from(
            self.pc.add_ice_candidate_with_opt_rtc_ice_candidate(
                cand.map(|cand| {
                    let raw = web_sys::RtcIceCandidateInit::new(cand);
                    web_sys::RtcIceCandidate::new(&raw).unwrap()
                })
                .as_ref(),
            ),
        )
        .await?;
        Ok(())
    }
}

impl Drop for PeerConnection {
    fn drop(&mut self) {
        self.pc.close();
    }
}

pub struct DataChannel {
    dc: web_sys::RtcDataChannel,
}

unsafe impl Send for DataChannel {}
unsafe impl Sync for DataChannel {}

impl DataChannel {
    pub fn set_on_open(&self, cb: Option<impl Fn() + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(
                move |_: web_sys::RtcDataChannelEvent| {
                    cb();
                },
            )
        });
        self.dc
            .set_onopen(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_close(&self, cb: Option<impl Fn() + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(
                move |_: web_sys::RtcDataChannelEvent| {
                    cb();
                },
            )
        });
        self.dc
            .set_onclose(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_buffered_amount_low(&self, cb: Option<impl Fn() + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |_: web_sys::Event| {
                cb();
            })
        });
        self.dc
            .set_onbufferedamountlow(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_error(&self, cb: Option<impl Fn(Error) + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |ev: web_sys::ErrorEvent| {
                cb(ev.error().into());
            })
        });
        self.dc
            .set_onerror(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_on_message(&self, cb: Option<impl Fn(&[u8]) + Send + Sync + 'static>) {
        let cb = cb.map(|cb| {
            wasm_bindgen::closure::Closure::<dyn FnMut(_)>::new(move |ev: web_sys::MessageEvent| {
                let arr = match ev.data().dyn_into::<js_sys::ArrayBuffer>() {
                    Ok(arr) => arr,
                    Err(e) => {
                        log::error!("unsupported message: {:?}", e);
                        return;
                    }
                };
                cb(js_sys::Uint8Array::new(&arr).to_vec().as_slice());
            })
        });
        self.dc
            .set_onmessage(cb.as_ref().map(|cb| cb.as_ref().unchecked_ref()));
        if let Some(cb) = cb {
            cb.forget();
        }
    }

    pub fn set_buffered_amount_low_threshold(&self, value: u32) {
        self.dc.set_buffered_amount_low_threshold(value);
    }

    pub fn buffered_amount(&self) -> u32 {
        self.dc.buffered_amount()
    }

    pub fn close(&self) {
        self.dc.close();
    }

    pub fn send(&self, buf: &[u8]) -> Result<(), Error> {
        self.dc.send_with_u8_array(buf)?;
        Ok(())
    }
}

impl Drop for DataChannel {
    fn drop(&mut self) {
        self.dc.close();
    }
}

pub struct DataChannelOptions {
    pub ordered: bool,
    pub max_packet_life_time: Option<u16>,
    pub max_retransmits: Option<u16>,
    pub protocol: String,
    pub negotiated: bool,
    pub id: Option<u16>,
}

impl Default for DataChannelOptions {
    fn default() -> Self {
        Self {
            ordered: true,
            max_packet_life_time: None,
            max_retransmits: None,
            protocol: "".to_string(),
            negotiated: false,
            id: None,
        }
    }
}

#[cfg(test)]
mod test {
    wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

    use super::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    pub async fn test_peer_connection_new() {
        let pc = PeerConnection::new(Default::default()).unwrap();
        pc.create_data_channel("test", Default::default()).unwrap();
    }

    #[wasm_bindgen_test]
    pub async fn test_peer_connection_communicate() {
        std::panic::set_hook(Box::new(console_error_panic_hook::hook));

        let pc1 = PeerConnection::new(Default::default()).unwrap();
        let pc1_gathered = std::sync::Arc::new(async_notify::Notify::new());
        pc1.set_on_ice_gathering_state_change(Some({
            let pc1_gathered = std::sync::Arc::clone(&pc1_gathered);
            move |ice_gathering_state| {
                if ice_gathering_state == IceGatheringState::Complete {
                    pc1_gathered.notify();
                }
            }
        }));

        let dc1 = pc1
            .create_data_channel(
                "test",
                DataChannelOptions {
                    negotiated: true,
                    id: Some(1),
                    ..Default::default()
                },
            )
            .unwrap();
        let dc1_open = std::sync::Arc::new(async_notify::Notify::new());
        dc1.set_on_open(Some({
            let dc1_open = std::sync::Arc::clone(&dc1_open);
            move || {
                dc1_open.notify();
            }
        }));
        pc1.set_local_description(&pc1.create_offer().await.unwrap())
            .await
            .unwrap();
        pc1_gathered.notified().await;

        let pc2 = PeerConnection::new(Default::default()).unwrap();
        let pc2_gathered = std::sync::Arc::new(async_notify::Notify::new());
        pc2.set_on_ice_gathering_state_change(Some({
            let pc2_gathered = std::sync::Arc::clone(&pc2_gathered);
            move |ice_gathering_state| {
                if ice_gathering_state == IceGatheringState::Complete {
                    pc2_gathered.notify();
                }
            }
        }));

        let dc2 = pc2
            .create_data_channel(
                "test",
                DataChannelOptions {
                    negotiated: true,
                    id: Some(1),
                    ..Default::default()
                },
            )
            .unwrap();
        let dc2_open = std::sync::Arc::new(async_notify::Notify::new());
        dc2.set_on_open(Some({
            let dc2_open = std::sync::Arc::clone(&dc2_open);
            move || {
                dc2_open.notify();
            }
        }));

        let (tx1, rx1) = async_channel::bounded(1);
        dc1.set_on_message(Some(move |msg: &[u8]| {
            tx1.try_send(msg.to_vec()).unwrap();
        }));

        let (tx2, rx2) = async_channel::bounded(1);
        dc2.set_on_message(Some(move |msg: &[u8]| {
            tx2.try_send(msg.to_vec()).unwrap();
        }));

        pc2.set_remote_description(&pc1.local_description().unwrap())
            .await
            .unwrap();

        pc2.set_local_description(&pc2.create_answer().await.unwrap())
            .await
            .unwrap();
        pc2_gathered.notified().await;

        pc1.set_remote_description(&pc2.local_description().unwrap())
            .await
            .unwrap();

        dc1_open.notified().await;
        dc2_open.notified().await;

        dc1.send(b"hello world!").unwrap();
        assert_eq!(rx2.recv().await.unwrap(), b"hello world!");

        dc2.send(b"goodbye world!").unwrap();
        assert_eq!(rx1.recv().await.unwrap(), b"goodbye world!");
    }
}