servo_media_webrtc/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5extern 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
37/// This trait is implemented by backends and should never be used directly by
38/// the client. Use WebRtcController instead
39pub 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/// <https://www.w3.org/TR/webrtc/#rtcsdptype>
97#[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/// <https://www.w3.org/TR/webrtc/#rtcsessiondescription-class>
136///
137/// <https://developer.mozilla.org/en-US/docs/Web/API/RTCSessionDescription>
138#[derive(Clone, Hash, Debug, PartialEq, Eq)]
139pub struct SessionDescription {
140    pub type_: SdpType,
141    pub sdp: String,
142}
143
144/// <https://www.w3.org/TR/webrtc/#rtcicecandidate-interface>
145///
146/// <https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidate>
147#[derive(Clone, Hash, Debug, PartialEq, Eq)]
148pub struct IceCandidate {
149    pub sdp_mline_index: u32,
150    pub candidate: String,
151    // XXXManishearth this is missing a bunch
152}
153
154/// <https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection#RTCBundlePolicy_enum>
155#[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/// <https://www.w3.org/TR/webrtc/#rtcsignalingstate-enum>
173#[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/// <https://www.w3.org/TR/webrtc/#rtcicegatheringstate-enum>
184#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
185pub enum GatheringState {
186    New,
187    Gathering,
188    Complete,
189}
190
191/// <https://www.w3.org/TR/webrtc/#rtciceconnectionstate-enum>
192#[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}