1extern crate log;
2extern crate servo_media_streams;
3extern crate uuid;
4
5use servo_media_streams::registry::MediaStreamId;
6use servo_media_streams::MediaStreamType;
7
8use std::fmt::Display;
9use std::str::FromStr;
10
11pub mod datachannel;
12pub mod thread;
13
14pub use datachannel::{
15 DataChannelEvent, DataChannelId, DataChannelInit, DataChannelMessage, DataChannelState,
16};
17pub use thread::WebRtcController;
18
19#[derive(Debug)]
20pub enum WebRtcError {
21 Backend(String),
22}
23
24pub type WebRtcResult = Result<(), WebRtcError>;
25pub type WebRtcDataChannelResult = Result<DataChannelId, WebRtcError>;
26
27impl<T: Display> From<T> for WebRtcError {
28 fn from(x: T) -> Self {
29 WebRtcError::Backend(x.to_string())
30 }
31}
32
33pub trait WebRtcControllerBackend: Send {
36 fn configure(&mut self, stun_server: &str, policy: BundlePolicy) -> WebRtcResult;
37 fn set_remote_description(
38 &mut self,
39 _: SessionDescription,
40 cb: Box<dyn FnOnce() + Send + 'static>,
41 ) -> WebRtcResult;
42 fn set_local_description(
43 &mut self,
44 _: SessionDescription,
45 cb: Box<dyn FnOnce() + Send + 'static>,
46 ) -> WebRtcResult;
47 fn add_ice_candidate(&mut self, candidate: IceCandidate) -> WebRtcResult;
48 fn create_offer(
49 &mut self,
50 cb: Box<dyn FnOnce(SessionDescription) + Send + 'static>,
51 ) -> WebRtcResult;
52 fn create_answer(
53 &mut self,
54 cb: Box<dyn FnOnce(SessionDescription) + Send + 'static>,
55 ) -> WebRtcResult;
56 fn add_stream(&mut self, stream: &MediaStreamId) -> WebRtcResult;
57
58 fn create_data_channel(&mut self, init: &DataChannelInit) -> WebRtcDataChannelResult;
59 fn close_data_channel(&mut self, channel: &DataChannelId) -> WebRtcResult;
60 fn send_data_channel_message(
61 &mut self,
62 channel: &DataChannelId,
63 message: &DataChannelMessage,
64 ) -> WebRtcResult;
65
66 fn internal_event(&mut self, event: thread::InternalEvent) -> WebRtcResult;
67 fn quit(&mut self);
68}
69
70pub trait WebRtcSignaller: Send {
71 fn on_ice_candidate(&self, controller: &WebRtcController, candidate: IceCandidate);
72 fn on_negotiation_needed(&self, controller: &WebRtcController);
73 fn close(&self);
74 fn on_add_stream(&self, stream: &MediaStreamId, ty: MediaStreamType);
75
76 fn update_signaling_state(&self, _: SignalingState) {}
77 fn update_gathering_state(&self, _: GatheringState) {}
78 fn update_ice_connection_state(&self, _: IceConnectionState) {}
79
80 fn on_data_channel_event(&self, _: DataChannelId, _: DataChannelEvent, _: &WebRtcController) {}
81}
82
83pub trait WebRtcBackend {
84 type Controller: WebRtcControllerBackend + 'static;
85
86 fn construct_webrtc_controller(
87 signaller: Box<dyn WebRtcSignaller>,
88 thread: WebRtcController,
89 ) -> Self::Controller;
90}
91
92#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
94pub enum SdpType {
95 Answer,
96 Offer,
97 Pranswer,
98 Rollback,
99}
100
101#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
102pub enum DescriptionType {
103 Local,
104 Remote,
105}
106
107impl SdpType {
108 pub fn as_str(self) -> &'static str {
109 match self {
110 SdpType::Answer => "answer",
111 SdpType::Offer => "offer",
112 SdpType::Pranswer => "pranswer",
113 SdpType::Rollback => "rollback",
114 }
115 }
116}
117
118impl FromStr for SdpType {
119 type Err = ();
120 fn from_str(s: &str) -> Result<Self, ()> {
121 Ok(match s {
122 "answer" => SdpType::Answer,
123 "offer" => SdpType::Offer,
124 "pranswer" => SdpType::Pranswer,
125 "rollback" => SdpType::Rollback,
126 _ => return Err(()),
127 })
128 }
129}
130
131#[derive(Clone, Hash, Debug, PartialEq, Eq)]
135pub struct SessionDescription {
136 pub type_: SdpType,
137 pub sdp: String,
138}
139
140#[derive(Clone, Hash, Debug, PartialEq, Eq)]
144pub struct IceCandidate {
145 pub sdp_mline_index: u32,
146 pub candidate: String,
147 }
149
150#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
152pub enum BundlePolicy {
153 Balanced,
154 MaxCompat,
155 MaxBundle,
156}
157
158impl BundlePolicy {
159 pub fn as_str(self) -> &'static str {
160 match self {
161 BundlePolicy::Balanced => "balanced",
162 BundlePolicy::MaxCompat => "max-compat",
163 BundlePolicy::MaxBundle => "max-bundle",
164 }
165 }
166}
167
168#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
170pub enum SignalingState {
171 Stable,
172 HaveLocalOffer,
173 HaveRemoteOffer,
174 HaveLocalPranswer,
175 HaveRemotePranswer,
176 Closed,
177}
178
179#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
181pub enum GatheringState {
182 New,
183 Gathering,
184 Complete,
185}
186
187#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
189pub enum IceConnectionState {
190 New,
191 Checking,
192 Connected,
193 Completed,
194 Disconnected,
195 Failed,
196 Closed,
197}