diff --git a/components/atoms/static_atoms.txt b/components/atoms/static_atoms.txt index 74f24a23e30..7a6022cb64c 100644 --- a/components/atoms/static_atoms.txt +++ b/components/atoms/static_atoms.txt @@ -37,6 +37,8 @@ gattserverdisconnected hashchange hidden icecandidate +iceconnectionstatechange +icegatheringstatechange image input invalid @@ -95,6 +97,7 @@ seeked seeking select serif +signalingstatechange srclang statechange storage diff --git a/components/script/dom/rtcpeerconnection.rs b/components/script/dom/rtcpeerconnection.rs index 0a1c41cf3f9..e4948e08d8e 100644 --- a/components/script/dom/rtcpeerconnection.rs +++ b/components/script/dom/rtcpeerconnection.rs @@ -7,7 +7,8 @@ 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, RTCIceConnectionState, + RTCIceGatheringState, RTCOfferOptions, RTCSignalingState, }; use crate::dom::bindings::codegen::Bindings::RTCSessionDescriptionBinding::{ RTCSdpType, RTCSessionDescriptionInit, @@ -36,7 +37,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, IceConnectionState, SdpType, SessionDescription, + SignalingState, WebRtcController, WebRtcSignaller, }; use servo_media::ServoMedia; use servo_media_auto::Backend; @@ -59,6 +61,9 @@ pub struct RTCPeerConnection { answer_promises: DomRefCell>>, local_description: MutNullableDom, remote_description: MutNullableDom, + gathering_state: Cell, + ice_connection_state: Cell, + signaling_state: Cell, } struct RTCSignaller { @@ -90,6 +95,39 @@ 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 update_ice_connection_state(&self, state: IceConnectionState) { + let this = self.trusted.clone(); + let _ = self.task_source.queue_with_canceller( + task!(update_ice_connection_state: move || { + let this = this.root(); + this.update_ice_connection_state(state); + }), + &self.canceller, + ); + } + + fn update_signaling_state(&self, state: SignalingState) { + let this = self.trusted.clone(); + let _ = self.task_source.queue_with_canceller( + task!(update_signaling_state: move || { + let this = this.root(); + this.update_signaling_state(state); + }), + &self.canceller, + ); + } + fn on_add_stream(&self, _: Box) {} fn close(&self) { @@ -108,6 +146,9 @@ impl RTCPeerConnection { answer_promises: DomRefCell::new(vec![]), local_description: Default::default(), remote_description: Default::default(), + gathering_state: Cell::new(RTCIceGatheringState::New), + ice_connection_state: Cell::new(RTCIceConnectionState::New), + signaling_state: Cell::new(RTCSignalingState::Stable), } } @@ -198,6 +239,96 @@ impl RTCPeerConnection { event.upcast::().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::().fire(self.upcast()); + + // step 6 + if state == RTCIceGatheringState::Complete { + let event = RTCPeerConnectionIceEvent::new( + &self.global(), + atom!("icecandidate"), + None, + None, + true, + ); + event.upcast::().fire(self.upcast()); + } + } + + /// https://www.w3.org/TR/webrtc/#update-ice-connection-state + fn update_ice_connection_state(&self, state: IceConnectionState) { + // step 1 + if self.closed.get() { + return; + } + + // step 2 (state derivation already done by gstreamer) + let state: RTCIceConnectionState = state.into(); + + // step 3 + if state == self.ice_connection_state.get() { + return; + } + + // step 4 + self.ice_connection_state.set(state); + + // step 5 + let event = Event::new( + &self.global(), + atom!("iceconnectionstatechange"), + EventBubbles::DoesNotBubble, + EventCancelable::NotCancelable, + ); + event.upcast::().fire(self.upcast()); + } + + fn update_signaling_state(&self, state: SignalingState) { + if self.closed.get() { + return; + } + + let state: RTCSignalingState = state.into(); + + if state == self.signaling_state.get() { + return; + } + + self.signaling_state.set(state); + + let event = Event::new( + &self.global(), + atom!("signalingstatechange"), + EventBubbles::DoesNotBubble, + EventCancelable::NotCancelable, + ); + event.upcast::().fire(self.upcast()); + } + fn create_offer(&self) { let generation = self.offer_answer_generation.get(); let (task_source, canceller) = self @@ -269,6 +400,20 @@ 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-iceconnectionstatechange + event_handler!( + iceconnectionstatechange, + GetOniceconnectionstatechange, + SetOniceconnectionstatechange + ); + + /// 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, @@ -276,6 +421,13 @@ impl RTCPeerConnectionMethods for RTCPeerConnection { SetOnnegotiationneeded ); + /// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-signalingstatechange + event_handler!( + signalingstatechange, + GetOnsignalingstatechange, + SetOnsignalingstatechange + ); + /// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addicecandidate fn AddIceCandidate(&self, candidate: &RTCIceCandidateInit) -> Rc { let p = Promise::new(&self.global()); @@ -419,6 +571,46 @@ 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() + } + + /// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-iceconnectionstate + fn IceConnectionState(&self) -> RTCIceConnectionState { + self.ice_connection_state.get() + } + + /// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-signalingstate + fn SignalingState(&self) -> RTCSignalingState { + self.signaling_state.get() + } + + /// https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close + fn Close(&self) { + // Step 1 + if self.closed.get() { + return; + } + // Step 2 + self.closed.set(true); + + // Step 4 + self.signaling_state.set(RTCSignalingState::Closed); + + // Step 5 handled by backend + self.controller.borrow_mut().as_ref().unwrap().quit(); + + // Step 6-10 + // (no current support for data channels, transports, etc) + + // Step 11 + self.ice_connection_state.set(RTCIceConnectionState::Closed); + + // Step 11 + // (no current support for connection state) + } } impl From for RTCSessionDescriptionInit { @@ -450,3 +642,40 @@ impl<'a> From<&'a RTCSessionDescriptionInit> for SessionDescription { } } } + +impl From for RTCIceGatheringState { + fn from(state: GatheringState) -> Self { + match state { + GatheringState::New => RTCIceGatheringState::New, + GatheringState::Gathering => RTCIceGatheringState::Gathering, + GatheringState::Complete => RTCIceGatheringState::Complete, + } + } +} + +impl From for RTCIceConnectionState { + fn from(state: IceConnectionState) -> Self { + match state { + IceConnectionState::New => RTCIceConnectionState::New, + IceConnectionState::Checking => RTCIceConnectionState::Checking, + IceConnectionState::Connected => RTCIceConnectionState::Connected, + IceConnectionState::Completed => RTCIceConnectionState::Completed, + IceConnectionState::Disconnected => RTCIceConnectionState::Disconnected, + IceConnectionState::Failed => RTCIceConnectionState::Failed, + IceConnectionState::Closed => RTCIceConnectionState::Closed, + } + } +} + +impl From for RTCSignalingState { + fn from(state: SignalingState) -> Self { + match state { + SignalingState::Stable => RTCSignalingState::Stable, + SignalingState::HaveLocalOffer => RTCSignalingState::Have_local_offer, + SignalingState::HaveRemoteOffer => RTCSignalingState::Have_remote_offer, + SignalingState::HaveLocalPranswer => RTCSignalingState::Have_local_pranswer, + SignalingState::HaveRemotePranswer => RTCSignalingState::Have_remote_pranswer, + SignalingState::Closed => RTCSignalingState::Closed, + } + } +} diff --git a/components/script/dom/webidls/RTCPeerConnection.webidl b/components/script/dom/webidls/RTCPeerConnection.webidl index c79dba097bf..0f00f3b5b95 100644 --- a/components/script/dom/webidls/RTCPeerConnection.webidl +++ b/components/script/dom/webidls/RTCPeerConnection.webidl @@ -18,21 +18,21 @@ interface RTCPeerConnection : EventTarget { // readonly attribute RTCSessionDescription? currentRemoteDescription; // readonly attribute RTCSessionDescription? pendingRemoteDescription; Promise addIceCandidate(optional RTCIceCandidateInit candidate); - // readonly attribute RTCSignalingState signalingState; - // readonly attribute RTCIceGatheringState iceGatheringState; - // readonly attribute RTCIceConnectionState iceConnectionState; + readonly attribute RTCSignalingState signalingState; + readonly attribute RTCIceGatheringState iceGatheringState; + readonly attribute RTCIceConnectionState iceConnectionState; // readonly attribute RTCPeerConnectionState connectionState; // readonly attribute boolean? canTrickleIceCandidates; // static sequence getDefaultIceServers(); // RTCConfiguration getConfiguration(); // void setConfiguration(RTCConfiguration configuration); - // void close(); + void close(); attribute EventHandler onnegotiationneeded; attribute EventHandler onicecandidate; // attribute EventHandler onicecandidateerror; - // attribute EventHandler onsignalingstatechange; - // attribute EventHandler oniceconnectionstatechange; - // attribute EventHandler onicegatheringstatechange; + attribute EventHandler onsignalingstatechange; + attribute EventHandler oniceconnectionstatechange; + attribute EventHandler onicegatheringstatechange; // attribute EventHandler onconnectionstatechange; // removed from spec, but still shipped by browsers @@ -89,3 +89,28 @@ dictionary RTCOfferOptions : RTCOfferAnswerOptions { dictionary RTCAnswerOptions : RTCOfferAnswerOptions { }; + +enum RTCIceGatheringState { + "new", + "gathering", + "complete" +}; + +enum RTCIceConnectionState { + "new", + "checking", + "connected", + "completed", + "disconnected", + "failed", + "closed" +}; + +enum RTCSignalingState { + "stable", + "have-local-offer", + "have-remote-offer", + "have-local-pranswer", + "have-remote-pranswer", + "closed" +};