Bug 1246310 - Add a method for attaching local tracks rather than full streams. r?drno draft
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 18 Apr 2016 17:48:32 +0200
changeset 353691 f887ed052ee57eae8456432eb28a48464e8a9190
parent 350335 564b225d553547fe4aa9a1039278f695c9507db9
child 353692 7055db327f562961f89b9238b3484849e8fdcb62
push id15908
push userpehrsons@gmail.com
push dateTue, 19 Apr 2016 12:54:03 +0000
reviewersdrno
bugs1246310
milestone48.0a1
Bug 1246310 - Add a method for attaching local tracks rather than full streams. r?drno MozReview-Commit-ID: IdE7H1ItQTR
dom/media/tests/mochitest/pc.js
--- a/dom/media/tests/mochitest/pc.js
+++ b/dom/media/tests/mochitest/pc.js
@@ -707,16 +707,17 @@ function PeerConnectionWrapper(label, co
     label = label + "_" + configuration.label_suffix;
   }
   this.label = label;
   this.whenCreated = Date.now();
 
   this.constraints = [ ];
   this.offerOptions = {};
   this.streams = [ ];
+  this.streamsForLocalTracks = [ ];
   this.mediaElements = [ ];
 
   this.dataChannels = [ ];
 
   this._local_ice_candidates = [];
   this._remote_ice_candidates = [];
   this.localRequiresTrickleIce = false;
   this.remoteRequiresTrickleIce = false;
@@ -816,16 +817,68 @@ PeerConnectionWrapper.prototype = {
     return this._pc.iceConnectionState;
   },
 
   setIdentityProvider: function(provider, protocol, identity) {
     this._pc.setIdentityProvider(provider, protocol, identity);
   },
 
   /**
+   * Attaches a local track to this RTCPeerConnection using
+   * RTCPeerConnection.addTrack().
+   *
+   * Also creates a media element playing a MediaStream containing all
+   * tracks that have been added to `stream` using `attachLocalTrack()`.
+   *
+   * @param {MediaStreamTrack} track
+   *        MediaStreamTrack to handle
+   * @param {MediaStream} stream
+   *        MediaStream to use as container for `track` on remote side
+   */
+  attachLocalTrack : function(track, stream) {
+    info("Get a local " + track.kind + " track");
+
+    this.expectNegotiationNeeded();
+    var sender = this._pc.addTrack(track, stream);
+    is(sender.track, track, "addTrack returns sender");
+
+    ok(track.id, "track has id");
+    ok(track.kind, "track has kind");
+    this.expectedLocalTrackInfoById[track.id] = {
+      type: track.kind,
+      streamId: stream.id,
+    };
+
+    var mappedStream =
+      this.streamsForLocalTracks.find(o => o.fromStreamId == stream.id);
+
+    if (mappedStream) {
+      mappedStream.stream.addTrack(track);
+      return;
+    }
+
+    mappedStream = new MediaStream([track]);
+    this.streamsForLocalTracks.push(
+      { fromStreamId: stream.id
+      , stream: mappedStream
+      }
+    );
+
+    var element = createMediaElement(track.kind, this.label + '_tracks_' + this.streamsForLocalTracks.length);
+    this.mediaElements.push(element);
+    element.srcObject = mappedStream;
+    element.play();
+
+    // Store local media elements so that we can stop them when done.
+    // Don't store remote ones because they should stop when the PC does.
+    this.localMediaElements.push(element);
+    return this.observedNegotiationNeeded;
+  },
+
+  /**
    * Callback when we get media from either side. Also an appropriate
    * HTML media element will be created.
    *
    * @param {MediaStream} stream
    *        Media stream to handle
    * @param {string} type
    *        The type of media stream ('audio' or 'video')
    * @param {string} side