Bug 1423842 - Part 2: Fire onaddstream after track additions, but before SRD resolves. r=jib draft
authorByron Campen [:bwc] <docfaraday@gmail.com>
Mon, 18 Dec 2017 13:41:18 -0600
changeset 713614 f5f01cb5e0a7bfa7b0d97f6332f2c1ea4438c06a
parent 713599 aba7da5ab6be4f9f97b0c15a4d0816670e619822
child 714079 bee557f078c402c7893c19ca1dc3bc2164261e65
push id93699
push userbcampen@mozilla.com
push dateWed, 20 Dec 2017 19:10:43 +0000
reviewersjib
bugs1423842
milestone59.0a1
Bug 1423842 - Part 2: Fire onaddstream after track additions, but before SRD resolves. r=jib MozReview-Commit-ID: BOJv560nKyw
dom/media/PeerConnection.js
--- a/dom/media/PeerConnection.js
+++ b/dom/media/PeerConnection.js
@@ -361,16 +361,18 @@ class RTCRtpSourceCache {
     // Has a cache wipe already been scheduled
     this.scheduledClear = null;
   }
 }
 
 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._localType = null;
     this._remoteType = null;
     // http://rtcweb-wg.github.io/jsep/#rfc.section.4.1.9
@@ -1317,23 +1319,30 @@ class RTCPeerConnection {
 
     this._queueTaskWithClosedCheck(() => {
       if (this._negotiationNeeded) {
         this.dispatchEvent(new this._win.Event("negotiationneeded"));
       }
     });
   }
 
+  // TODO(Bug 1241291): Legacy event, remove eventually
+  _fireLegacyAddStreamEvents() {
+    for (let stream of this._newStreams) {
+      let ev = new this._win.MediaStreamEvent("addstream", { stream });
+      this.dispatchEvent(ev);
+    }
+    this._newStreams = [];
+  }
+
   _getOrCreateStream(id) {
     if (!this._receiveStreams.has(id)) {
       let stream = new this._win.MediaStream();
       stream.assignId(id);
-      // Legacy event, remove eventually
-      let ev = new this._win.MediaStreamEvent("addstream", { stream });
-      this.dispatchEvent(ev);
+      this._newStreams.push(stream);
       this._receiveStreams.set(id, stream);
     }
 
     return this._receiveStreams.get(id);
   }
 
   _insertDTMF(transceiverImpl, tones, duration, interToneGap) {
     return this._impl.insertDTMF(transceiverImpl, tones, duration, interToneGap);
@@ -1642,16 +1651,17 @@ class PeerConnectionObserver {
 
   onSetLocalDescriptionSuccess() {
     this._dompc._syncTransceivers();
     this._dompc._onSetLocalDescriptionSuccess();
   }
 
   onSetRemoteDescriptionSuccess() {
     this._dompc._syncTransceivers();
+    this._dompc._fireLegacyAddStreamEvents();
     this._dompc._onSetRemoteDescriptionSuccess();
   }
 
   onSetLocalDescriptionError(code, message) {
     this._localType = null;
     this._dompc._onSetLocalDescriptionFailure(this.newError(message, code));
   }