BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / web / data_channel.rs

data_channel.rs

data_channel.rs 3.8 KiB Download Raw
1// Copyright 2025 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use core::str;
16use std::cell::RefCell;
17use std::rc::Rc;
18use wasm_bindgen::prelude::*;
19use web_sys::{MessageEvent, RtcDataChannelEvent, RtcDataChannelState};
20
21use crate::data_channel::{
22 DataChannelError, DataChannelTrait, DataState, OnBufferedAmountChange, OnMessage, OnStateChange,
23};
24
25impl From<RtcDataChannelState> for DataState {
26 fn from(value: RtcDataChannelState) -> Self {
27 match value {
28 RtcDataChannelState::Connecting => Self::Connecting,
29 RtcDataChannelState::Open => Self::Open,
30 RtcDataChannelState::Closing => Self::Closing,
31 RtcDataChannelState::Closed => Self::Closed,
32 _ => panic!("unknown data channel state"),
33 }
34 }
35}
36
37#[derive(Clone)]
38pub struct DataChannel {
39 sys_handle: web_sys::RtcDataChannel,
40 on_closing: Rc<RefCell<Option<JsValue>>>,
41}
42
43impl DataChannelTrait for DataChannel {
44 fn send(&self, data: &[u8], binary: bool) -> Result<(), DataChannelError> {
45 if binary {
46 self.sys_handle
47 .send_with_u8_array(data)
48 .map_err(|_| DataChannelError::Send)
49 } else {
50 let utf8 = str::from_utf8(data)?;
51 self.sys_handle
52 .send_with_str(utf8)
53 .map_err(|_| DataChannelError::Send)
54 }
55 }
56
57 fn label(&self) -> String {
58 self.sys_handle.label()
59 }
60
61 fn state(&self) -> DataState {
62 self.sys_handle.ready_state().into()
63 }
64
65 fn close(&self) {
66 self.sys_handle.close();
67 }
68
69 fn on_state_change(&self, callback: Option<OnStateChange>) {
70 if let Some(mut callback) = callback {
71 let dc = self.clone();
72 let js_callback = Closure::new(move |_: RtcDataChannelEvent| {
73 callback(dc.state());
74 });
75 let js_callback = js_callback.into_js_value();
76 self.sys_handle
77 .set_onopen(Some(js_callback.unchecked_ref()));
78 self.sys_handle
79 .set_onclose(Some(js_callback.unchecked_ref()));
80 self.sys_handle
81 .add_event_listener_with_callback("closing", js_callback.unchecked_ref())
82 .unwrap();
83
84 self.on_closing.replace(Some(js_callback));
85 } else {
86 self.sys_handle.set_onopen(None);
87 self.sys_handle.set_onclose(None);
88 if let Some(on_closing) = self.on_closing.take() {
89 self.sys_handle
90 .remove_event_listener_with_callback("closing", on_closing.unchecked_ref())
91 .unwrap();
92 }
93 self.on_closing.replace(None);
94 }
95 }
96
97 fn on_message(&self, callback: Option<OnMessage>) {
98 let js_callback = callback.map(|mut callback| {
99 Closure::new(move |event: MessageEvent| {
100 if let Some(str) = event.as_string() {
101 callback(str.as_bytes(), false);
102 }
103 })
104 .into_js_value()
105 });
106
107 self.sys_handle.set_onmessage(
108 js_callback
109 .as_ref()
110 .map(|callback| callback.unchecked_ref()),
111 );
112 }
113
114 fn on_buffered_amount_change(&self, _callback: Option<OnBufferedAmountChange>) {
115 todo!("onbufferedamountlow instead?")
116 }
117}