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