Bug 1531156 - Part 1: Remember our role from the previous negotiation so we can use the right type for getCurrentRemote/LocalDescription. r=jib
authorByron Campen [:bwc] <docfaraday@gmail.com>
Mon, 11 Mar 2019 21:01:40 +0000
changeset 521518 41c4e3fc6f9d
parent 521517 52fd3c1c9615
child 521519 c1e7e2cb32cd
push id10867
push userdvarga@mozilla.com
push dateThu, 14 Mar 2019 15:20:45 +0000
treeherdermozilla-beta@abad13547875 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1531156
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1531156 - Part 1: Remember our role from the previous negotiation so we can use the right type for getCurrentRemote/LocalDescription. r=jib Differential Revision: https://phabricator.services.mozilla.com/D22207
dom/media/PeerConnection.jsm
--- a/dom/media/PeerConnection.jsm
+++ b/dom/media/PeerConnection.jsm
@@ -345,19 +345,19 @@ class RTCPeerConnection {
   constructor() {
     this._receiveStreams = new Map();
     // Used to fire onaddstream, remove when we don't do that anymore.
     this._newStreams = [];
     this._transceivers = [];
 
     this._pc = null;
     this._closed = false;
+    this._currentRole = null;
+    this._pendingRole = null;
 
-    this._localType = null;
-    this._remoteType = null;
     // http://rtcweb-wg.github.io/jsep/#rfc.section.4.1.9
     // canTrickle == null means unknown; when a remote description is received it
     // is set to true or false based on the presence of the "trickle" ice-option
     this._canTrickle = null;
 
     // States
     this._iceGatheringState = this._iceConnectionState = "new";
 
@@ -942,30 +942,34 @@ class RTCPeerConnection {
 
   setLocalDescription(desc, onSucc, onErr) {
     return this._auto(onSucc, onErr, () => this._setLocalDescription(desc));
   }
 
   async _setLocalDescription({ type, sdp }) {
     this._checkClosed();
 
-    this._localType = type;
-
     let action = this._actions[type];
 
     this._sanityCheckSdp(action, type, sdp);
 
     return this._chain(async () => {
       await this._getPermission();
       await new Promise((resolve, reject) => {
         this._onSetLocalDescriptionSuccess = resolve;
         this._onSetLocalDescriptionFailure = reject;
         this._impl.setLocalDescription(action, sdp);
       });
       this._negotiationNeeded = false;
+      if (type == "answer") {
+        this._currentRole = "answerer";
+        this._pendingRole = null;
+      } else {
+        this._pendingRole = "offerer";
+      }
       this.updateNegotiationNeeded();
     });
   }
 
   async _validateIdentity(sdp, origin) {
     let expectedIdentity;
 
     // Only run a single identity verification at a time.  We have to do this to
@@ -1011,17 +1015,16 @@ class RTCPeerConnection {
   }
 
   setRemoteDescription(desc, onSucc, onErr) {
     return this._auto(onSucc, onErr, () => this._setRemoteDescription(desc));
   }
 
   async _setRemoteDescription({ type, sdp }) {
     this._checkClosed();
-    this._remoteType = type;
 
     let action = this._actions[type];
 
     this._sanityCheckSdp(action, type, sdp);
 
     // Get caller's origin before hitting the promise chain
     let origin = Cu.getWebIDLCallerPrincipal().origin;
 
@@ -1037,16 +1040,22 @@ class RTCPeerConnection {
       })();
 
       if (action != Ci.IPeerConnection.kActionRollback) {
         // Do setRemoteDescription and identity validation in parallel
         await this._validateIdentity(sdp, origin);
       }
       await haveSetRemote;
       this._negotiationNeeded = false;
+      if (type == "answer") {
+        this._currentRole = "offerer";
+        this._pendingRole = null;
+      } else {
+        this._pendingRole = "answerer";
+      }
       this.updateNegotiationNeeded();
     });
   }
 
   setIdentityProvider(provider,
                       {protocol, usernameHint, peerIdentity} = {}) {
     this._checkClosed();
     this._localIdp.setIdentityProvider(provider,
@@ -1436,67 +1445,62 @@ class RTCPeerConnection {
     this._impl.disablePacketDump(level, type, sending);
   }
 
   getTransceivers() {
     return this._transceivers;
   }
 
   get localDescription() {
-    this._checkClosed();
-    let sdp = this._impl.localDescription;
-    if (sdp.length == 0) {
-      return null;
-    }
-    return new this._win.RTCSessionDescription({ type: this._localType, sdp });
+    return this.pendingLocalDescription || this.currentLocalDescription;
   }
 
   get currentLocalDescription() {
     this._checkClosed();
     let sdp = this._impl.currentLocalDescription;
     if (sdp.length == 0) {
       return null;
     }
-    return new this._win.RTCSessionDescription({ type: this._localType, sdp });
+    const type = this._currentRole == "answerer" ? "answer" : "offer";
+    return new this._win.RTCSessionDescription({ type, sdp });
   }
 
   get pendingLocalDescription() {
     this._checkClosed();
     let sdp = this._impl.pendingLocalDescription;
     if (sdp.length == 0) {
       return null;
     }
-    return new this._win.RTCSessionDescription({ type: this._localType, sdp });
+    const type = this._pendingRole == "answerer" ? "answer" : "offer";
+    return new this._win.RTCSessionDescription({ type, sdp });
   }
 
+
   get remoteDescription() {
-    this._checkClosed();
-    let sdp = this._impl.remoteDescription;
-    if (sdp.length == 0) {
-      return null;
-    }
-    return new this._win.RTCSessionDescription({ type: this._remoteType, sdp });
+    return this.pendingRemoteDescription || this.currentRemoteDescription;
   }
 
   get currentRemoteDescription() {
     this._checkClosed();
     let sdp = this._impl.currentRemoteDescription;
     if (sdp.length == 0) {
       return null;
     }
-    return new this._win.RTCSessionDescription({ type: this._remoteType, sdp });
+    const type = this._currentRole == "offerer" ? "answer" : "offer";
+    return new this._win.RTCSessionDescription({ type, sdp });
   }
 
   get pendingRemoteDescription() {
     this._checkClosed();
     let sdp = this._impl.pendingRemoteDescription;
     if (sdp.length == 0) {
       return null;
     }
-    return new this._win.RTCSessionDescription({ type: this._remoteType, sdp });
+    const type = this._pendingRole == "offerer" ? "answer" : "offer";
+    return new this._win.RTCSessionDescription({ type, sdp });
   }
 
   get peerIdentity() { return this._peerIdentity; }
   get idpLoginUrl() { return this._localIdp.idpLoginUrl; }
   get id() { return this._impl.id; }
   set id(s) { this._impl.id = s; }
   get iceGatheringState() { return this._iceGatheringState; }
   get iceConnectionState() { return this._iceConnectionState; }
@@ -1659,22 +1663,20 @@ class PeerConnectionObserver {
     this._dompc._syncTransceivers();
     this._dompc._processTrackAdditionsAndRemovals();
     this._dompc._fireLegacyAddStreamEvents();
     this._dompc._transceivers = this._dompc._transceivers.filter(t => !t.shouldRemove);
     this._dompc._onSetRemoteDescriptionSuccess();
   }
 
   onSetLocalDescriptionError(code, message) {
-    this._localType = null;
     this._dompc._onSetLocalDescriptionFailure(this.newError(message, code));
   }
 
   onSetRemoteDescriptionError(code, message) {
-    this._remoteType = null;
     this._dompc._onSetRemoteDescriptionFailure(this.newError(message, code));
   }
 
   onAddIceCandidateSuccess() {
     this._dompc._onAddIceCandidateSuccess();
   }
 
   onAddIceCandidateError(code, message) {