Skip to content

Commit

Permalink
Add ICEGatheringState to RTCPeerConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Mar 25, 2019
1 parent 5ae562b commit 209caa4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/atoms/static_atoms.txt
Expand Up @@ -37,6 +37,7 @@ gattserverdisconnected
hashchange
hidden
icecandidate
icegatheringstatechange
image
input
invalid
Expand Down
90 changes: 88 additions & 2 deletions components/script/dom/rtcpeerconnection.rs
Expand Up @@ -7,7 +7,7 @@ use crate::dom::bindings::codegen::Bindings::RTCIceCandidateBinding::RTCIceCandi
use crate::dom::bindings::codegen::Bindings::RTCPeerConnectionBinding;
use crate::dom::bindings::codegen::Bindings::RTCPeerConnectionBinding::RTCPeerConnectionMethods;
use crate::dom::bindings::codegen::Bindings::RTCPeerConnectionBinding::{
RTCAnswerOptions, RTCBundlePolicy, RTCConfiguration, RTCOfferOptions,
RTCAnswerOptions, RTCBundlePolicy, RTCConfiguration, RTCIceGatheringState, RTCOfferOptions,
};
use crate::dom::bindings::codegen::Bindings::RTCSessionDescriptionBinding::{
RTCSdpType, RTCSessionDescriptionInit,
Expand Down Expand Up @@ -36,7 +36,8 @@ use dom_struct::dom_struct;

use servo_media::streams::MediaStream as BackendMediaStream;
use servo_media::webrtc::{
BundlePolicy, IceCandidate, SdpType, SessionDescription, WebRtcController, WebRtcSignaller,
BundlePolicy, GatheringState, IceCandidate, SdpType, SessionDescription, WebRtcController,
WebRtcSignaller,
};
use servo_media::ServoMedia;
use servo_media_auto::Backend;
Expand All @@ -59,6 +60,7 @@ pub struct RTCPeerConnection {
answer_promises: DomRefCell<Vec<Rc<Promise>>>,
local_description: MutNullableDom<RTCSessionDescription>,
remote_description: MutNullableDom<RTCSessionDescription>,
gathering_state: Cell<RTCIceGatheringState>,
}

struct RTCSignaller {
Expand Down Expand Up @@ -90,6 +92,17 @@ impl WebRtcSignaller for RTCSignaller {
);
}

fn update_gathering_state(&self, state: GatheringState) {
let this = self.trusted.clone();
let _ = self.task_source.queue_with_canceller(
task!(update_gathering_state: move || {
let this = this.root();
this.update_gathering_state(state);
}),
&self.canceller,
);
}

fn on_add_stream(&self, _: Box<BackendMediaStream>) {}

fn close(&self) {
Expand All @@ -108,6 +121,7 @@ impl RTCPeerConnection {
answer_promises: DomRefCell::new(vec![]),
local_description: Default::default(),
remote_description: Default::default(),
gathering_state: Cell::new(RTCIceGatheringState::New),
}
}

Expand Down Expand Up @@ -198,6 +212,55 @@ impl RTCPeerConnection {
event.upcast::<Event>().fire(self.upcast());
}

/// https://www.w3.org/TR/webrtc/#update-ice-gathering-state
fn update_gathering_state(&self, state: GatheringState) {
// step 1
if self.closed.get() {
return;
}

// step 2 (state derivation already done by gstreamer)
let state: RTCIceGatheringState = state.into();

// step 3
if state == self.gathering_state.get() {
return;
}

// step 4
self.gathering_state.set(state);

// step 5
let event = Event::new(
&self.global(),
atom!("icegatheringstatechange"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
);
event.upcast::<Event>().fire(self.upcast());

// step 6
if state == RTCIceGatheringState::Complete {
let event = RTCPeerConnectionIceEvent::new(
&self.global(),
atom!("icecandidate"),
None,
None,
true,
);
event.upcast::<Event>().fire(self.upcast());
}

// step 5
let event = Event::new(
&self.global(),
atom!("icegatheringstatechange"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable,
);
event.upcast::<Event>().fire(self.upcast());
}

fn create_offer(&self) {
let generation = self.offer_answer_generation.get();
let (task_source, canceller) = self
Expand Down Expand Up @@ -269,6 +332,13 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {
/// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-icecandidate
event_handler!(icecandidate, GetOnicecandidate, SetOnicecandidate);

/// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-icegatheringstatechange
event_handler!(
icegatheringstatechange,
GetOnicegatheringstatechange,
SetOnicegatheringstatechange
);

/// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-onnegotiationneeded
event_handler!(
negotiationneeded,
Expand Down Expand Up @@ -419,6 +489,11 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {
self.controller.borrow().as_ref().unwrap().add_stream(track);
}
}

/// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-icegatheringstate
fn IceGatheringState(&self) -> RTCIceGatheringState {
self.gathering_state.get()
}
}

impl From<SessionDescription> for RTCSessionDescriptionInit {
Expand Down Expand Up @@ -450,3 +525,14 @@ impl<'a> From<&'a RTCSessionDescriptionInit> for SessionDescription {
}
}
}


impl From<GatheringState> for RTCIceGatheringState {
fn from(state: GatheringState) -> Self {
match state {
GatheringState::New => RTCIceGatheringState::New,
GatheringState::Gathering => RTCIceGatheringState::Gathering,
GatheringState::Complete => RTCIceGatheringState::Complete,
}
}
}
10 changes: 8 additions & 2 deletions components/script/dom/webidls/RTCPeerConnection.webidl
Expand Up @@ -19,7 +19,7 @@ interface RTCPeerConnection : EventTarget {
// readonly attribute RTCSessionDescription? pendingRemoteDescription;
Promise<void> addIceCandidate(optional RTCIceCandidateInit candidate);
// readonly attribute RTCSignalingState signalingState;
// readonly attribute RTCIceGatheringState iceGatheringState;
readonly attribute RTCIceGatheringState iceGatheringState;
// readonly attribute RTCIceConnectionState iceConnectionState;
// readonly attribute RTCPeerConnectionState connectionState;
// readonly attribute boolean? canTrickleIceCandidates;
Expand All @@ -32,7 +32,7 @@ interface RTCPeerConnection : EventTarget {
// attribute EventHandler onicecandidateerror;
// attribute EventHandler onsignalingstatechange;
// attribute EventHandler oniceconnectionstatechange;
// attribute EventHandler onicegatheringstatechange;
attribute EventHandler onicegatheringstatechange;
// attribute EventHandler onconnectionstatechange;

// removed from spec, but still shipped by browsers
Expand Down Expand Up @@ -89,3 +89,9 @@ dictionary RTCOfferOptions : RTCOfferAnswerOptions {

dictionary RTCAnswerOptions : RTCOfferAnswerOptions {
};

enum RTCIceGatheringState {
"new",
"gathering",
"complete"
};

0 comments on commit 209caa4

Please sign in to comment.