servo_media_webrtc/
lib.rs

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
33/// This trait is implemented by backends and should never be used directly by
34/// the client. Use WebRtcController instead
35pub 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(&mut self, cb: Box<dyn FnOnce(SessionDescription,) + Send + 'static>) -> WebRtcResult;
49    fn create_answer(&mut self, cb: Box<dyn FnOnce(SessionDescription,) + Send + 'static>) -> WebRtcResult;
50    fn add_stream(&mut self, stream: &MediaStreamId) -> WebRtcResult;
51
52    fn create_data_channel(&mut self, init: &DataChannelInit) -> WebRtcDataChannelResult;
53    fn close_data_channel(&mut self, channel: &DataChannelId) -> WebRtcResult;
54    fn send_data_channel_message(
55        &mut self,
56        channel: &DataChannelId,
57        message: &DataChannelMessage,
58    ) -> WebRtcResult;
59
60    fn internal_event(&mut self, event: thread::InternalEvent) -> WebRtcResult;
61    fn quit(&mut self);
62}
63
64pub trait WebRtcSignaller: Send {
65    fn on_ice_candidate(&self, controller: &WebRtcController, candidate: IceCandidate);
66    fn on_negotiation_needed(&self, controller: &WebRtcController);
67    fn close(&self);
68    fn on_add_stream(&self, stream: &MediaStreamId, ty: MediaStreamType);
69
70    fn update_signaling_state(&self, _: SignalingState) {}
71    fn update_gathering_state(&self, _: GatheringState) {}
72    fn update_ice_connection_state(&self, _: IceConnectionState) {}
73
74    fn on_data_channel_event(&self, _: DataChannelId, _: DataChannelEvent, _: &WebRtcController) {}
75}
76
77pub trait WebRtcBackend {
78    type Controller: WebRtcControllerBackend + 'static;
79
80    fn construct_webrtc_controller(
81        signaller: Box<dyn WebRtcSignaller>,
82        thread: WebRtcController,
83    ) -> Self::Controller;
84}
85
86/// https://www.w3.org/TR/webrtc/#rtcsdptype
87#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
88pub enum SdpType {
89    Answer,
90    Offer,
91    Pranswer,
92    Rollback,
93}
94
95#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
96pub enum DescriptionType {
97    Local,
98    Remote,
99}
100
101impl SdpType {
102    pub fn as_str(self) -> &'static str {
103        match self {
104            SdpType::Answer => "answer",
105            SdpType::Offer => "offer",
106            SdpType::Pranswer => "pranswer",
107            SdpType::Rollback => "rollback",
108        }
109    }
110}
111
112impl FromStr for SdpType {
113    type Err = ();
114    fn from_str(s: &str) -> Result<Self, ()> {
115        Ok(match s {
116            "answer" => SdpType::Answer,
117            "offer" => SdpType::Offer,
118            "pranswer" => SdpType::Pranswer,
119            "rollback" => SdpType::Rollback,
120            _ => return Err(()),
121        })
122    }
123}
124
125/// https://www.w3.org/TR/webrtc/#rtcsessiondescription-class
126///
127/// https://developer.mozilla.org/en-US/docs/Web/API/RTCSessionDescription
128#[derive(Clone, Hash, Debug, PartialEq, Eq)]
129pub struct SessionDescription {
130    pub type_: SdpType,
131    pub sdp: String,
132}
133
134/// https://www.w3.org/TR/webrtc/#rtcicecandidate-interface
135///
136/// https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidate
137#[derive(Clone, Hash, Debug, PartialEq, Eq)]
138pub struct IceCandidate {
139    pub sdp_mline_index: u32,
140    pub candidate: String,
141    // XXXManishearth this is missing a bunch
142}
143
144/// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection#RTCBundlePolicy_enum
145#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
146pub enum BundlePolicy {
147    Balanced,
148    MaxCompat,
149    MaxBundle,
150}
151
152impl BundlePolicy {
153    pub fn as_str(self) -> &'static str {
154        match self {
155            BundlePolicy::Balanced => "balanced",
156            BundlePolicy::MaxCompat => "max-compat",
157            BundlePolicy::MaxBundle => "max-bundle",
158        }
159    }
160}
161
162/// https://www.w3.org/TR/webrtc/#rtcsignalingstate-enum
163#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
164pub enum SignalingState {
165    Stable,
166    HaveLocalOffer,
167    HaveRemoteOffer,
168    HaveLocalPranswer,
169    HaveRemotePranswer,
170    Closed,
171}
172
173/// https://www.w3.org/TR/webrtc/#rtcicegatheringstate-enum
174#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
175pub enum GatheringState {
176    New,
177    Gathering,
178    Complete,
179}
180
181/// https://www.w3.org/TR/webrtc/#rtciceconnectionstate-enum
182#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
183pub enum IceConnectionState {
184    New,
185    Checking,
186    Connected,
187    Completed,
188    Disconnected,
189    Failed,
190    Closed,
191}