Bug 1247622 - Make RTCRtpSender.setParameters return a promise. r=jesup,smaug
authorJan-Ivar Bruaroey <jib@mozilla.com>
Wed, 24 Feb 2016 19:14:57 -0500
changeset 321940 ec4f62653a7e801ba3f0c7dda21a4e7931721fce
parent 321939 de49494c8da78694b56627a8ccdaa79835263a08
child 321941 4f9b57948d0aeac51fe162d1059cdadd607f6983
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup, smaug
bugs1247622
milestone47.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 1247622 - Make RTCRtpSender.setParameters return a promise. r=jesup,smaug MozReview-Commit-ID: 6lfmqpKevQl
dom/media/PeerConnection.js
dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
dom/media/tests/mochitest/test_peerConnection_setParameters.html
dom/media/tests/mochitest/test_peerConnection_simulcastOffer.html
dom/webidl/RTCRtpSender.webidl
--- a/dom/media/PeerConnection.js
+++ b/dom/media/PeerConnection.js
@@ -1588,17 +1588,18 @@ RTCRtpSender.prototype = {
   contractID: PC_SENDER_CONTRACT,
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports]),
 
   replaceTrack: function(withTrack) {
     return this._pc._chain(() => this._pc._replaceTrack(this, withTrack));
   },
 
   setParameters: function(parameters) {
-    return this._pc._setParameters(this, parameters);
+    return this._pc._win.Promise.resolve()
+      .then(() => this._pc._setParameters(this, parameters));
   },
 
   getParameters: function() {
     return this._pc._getParameters(this);
   }
 };
 
 function RTCRtpReceiver(pc, track) {
--- a/dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
+++ b/dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
@@ -21,43 +21,37 @@
 
   pc1.onnegotiationneeded = e =>
     pc1.createOffer().then(d => pc1.setLocalDescription(d))
     .then(() => pc2.setRemoteDescription(pc1.localDescription))
     .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
     .then(() => pc1.setRemoteDescription(pc2.localDescription))
     .catch(generateErrorCallback());
 
-  function mustThrowWith(msg, reason, f) {
-    try {
-      f();
-      ok(false, msg + " must throw");
-    } catch (e) {
-      is(e.name, reason, msg + " must throw: " + e.message);
-    }
-  }
-
+  var mustRejectWith = (msg, reason, f) =>
+    f().then(() => ok(false, msg),
+             e => is(e.name, reason, msg));
   var v1, v2;
 
   runNetworkTest(function() {
     v1 = createMediaElement('video', 'v1');
     v2 = createMediaElement('video', 'v2');
 
     is(v2.currentTime, 0, "v2.currentTime is zero at outset");
 
     navigator.mediaDevices.getUserMedia({ video: true })
     .then(stream => {
       v1.srcObject = stream;
       var sender = pc1.addTrack(stream.getVideoTracks()[0], stream);
 
-      mustThrowWith("scaleResolutionDownBy must be valid", "RangeError",
-        () => sender.setParameters({ encodings: [{ scaleResolutionDownBy: 0.5 } ] }));
-
-      sender.setParameters({ encodings: [{ maxBitrate: 60000,
-                                           scaleResolutionDownBy: 2 }] });
+      return mustRejectWith("Invalid scaleResolutionDownBy must reject", "RangeError",
+                            () => sender.setParameters({ encodings:
+                                                       [{ scaleResolutionDownBy: 0.5 } ] }))
+      .then(() => sender.setParameters({ encodings: [{ maxBitrate: 60000,
+                                                     scaleResolutionDownBy: 2 }] }))
     })
     .then(() => new Promise(resolve => pc2.ontrack = e => resolve(e)))
     .then(e => v2.srcObject = e.streams[0])
     .then(() => new Promise(resolve => v2.onloadedmetadata = resolve))
     .then(() => waitUntil(() => v2.currentTime > 0 && v2.srcObject.currentTime > 0))
     .then(() => ok(v2.currentTime > 0, "v2.currentTime is moving (" + v2.currentTime + ")"))
     .then(() => wait(1000)) // TODO: Bug 1248154
     .then(() => {
--- a/dom/media/tests/mochitest/test_peerConnection_setParameters.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setParameters.html
@@ -7,79 +7,74 @@
 <pre id="test">
 <script type="application/javascript;version=1.8">
 createHTML({
   bug: "1230184",
   title: "Set parameters on sender",
   visible: true
 });
 
-function parameterstest(wrapper) {
-  var pc = wrapper._pc;
+function parameterstest(pc) {
   ok(pc.getSenders().length > 0, "have senders");
   var sender = pc.getSenders()[0];
 
   var testParameters = (params, errorName, errorMsg) => {
-    var compareParameters = (a, b) => {
-      var compareEncoding = (a, b) => {
+
+    var validateParameters = (a, b) => {
+      var validateEncoding = (a, b) => {
         is(a.rid, b.rid || "", "same rid");
         is(a.maxBitrate, b.maxBitrate, "same maxBitrate");
         is(a.scaleResolutionDownBy, b.scaleResolutionDownBy,
            "same scaleResolutionDownBy");
       };
       is(a.encodings.length, (b.encodings || []).length, "same encodings");
-      a.encodings.forEach((en, i) => compareEncoding(en, b.encodings[i]));
+      a.encodings.forEach((en, i) => validateEncoding(en, b.encodings[i]));
     };
-    try {
-      sender.setParameters(params);
-      compareParameters(sender.getParameters(), params);
+
+    var before = JSON.stringify(sender.getParameters());
+    isnot(JSON.stringify(params), before, "starting condition");
+
+    var p = sender.setParameters(params)
+    .then(() => {
+      isnot(JSON.stringify(sender.getParameters()), before, "parameters changed");
+      validateParameters(sender.getParameters(), params);
       is(null, errorName || null, "is success expected");
-    } catch (e) {
+    }, e => {
       is(e.name, errorName, "correct error name");
       is(e.message, errorMsg, "correct error message");
-    }
+    });
+    is(JSON.stringify(sender.getParameters()), before, "parameters not set yet");
+    return p;
   };
 
-  testParameters({
-    encodings: [
-      { rid: "foo", maxBitrate: 40000, scaleResolutionDownBy: 2 },
-      { rid: "bar", maxBitrate: 10000, scaleResolutionDownBy: 4 },
-    ]
-  });
-  testParameters({
-    encodings: [
-      { maxBitrate: 10000, scaleResolutionDownBy: 4 },
-    ]
-  });
-  testParameters({
-    encodings: [
-      { maxBitrate: 40000 },
-      { rid: "bar", maxBitrate: 10000 },
-    ]
-  }, "TypeError", "Missing rid");
-  testParameters({
-    encodings: [
-      { rid: "foo", maxBitrate: 40000 },
-      { rid: "bar", maxBitrate: 10000 },
-      { rid: "bar", maxBitrate: 20000 },
-    ]
-  }, "TypeError", "Duplicate rid");
-  testParameters({});
+  return [
+    [{ encodings: [ { rid: "foo", maxBitrate: 40000, scaleResolutionDownBy: 2 },
+                    { rid: "bar", maxBitrate: 10000, scaleResolutionDownBy: 4 }]
+    }],
+    [{ encodings: [{ maxBitrate: 10000, scaleResolutionDownBy: 4 }]}],
+    [{ encodings: [{ maxBitrate: 40000 },
+                   { rid: "bar", maxBitrate: 10000 }] }, "TypeError", "Missing rid"],
+    [{ encodings: [{ rid: "foo", maxBitrate: 40000 },
+                   { rid: "bar", maxBitrate: 10000 },
+                   { rid: "bar", maxBitrate: 20000 }] }, "TypeError", "Duplicate rid"],
+    [{}]
+  ].reduce((p, args) => p.then(() => testParameters.apply(this, args)),
+           Promise.resolve());
 }
 
 runNetworkTest(() => {
 
   test = new PeerConnectionTest();
   test.setMediaConstraints([{video: true}], [{video: true}]);
   test.chain.removeAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW");
 
   // Test sender parameters.
   test.chain.append([
     function PC_LOCAL_SET_PARAMETERS(test) {
-      return parameterstest(test.pcLocal);
+      return parameterstest(test.pcLocal._pc);
     }
   ]);
 
   return test.run()
     .catch(e => ok(false, "unexpected failure: " + e));
 });
 
 </script>
--- a/dom/media/tests/mochitest/test_peerConnection_simulcastOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_simulcastOffer.html
@@ -75,21 +75,19 @@
 
       test.chain.insertBefore('PC_LOCAL_CREATE_OFFER', [
         function PC_LOCAL_SET_RIDS(test) {
           var senders = test.pcLocal._pc.getSenders();
           is(senders.length, 1, "We have exactly one RTP sender");
           var sender = senders[0];
           ok(sender.track, "Sender has a track");
 
-          sender.setParameters({
-            encodings: [
-            { rid: "foo", maxBitrate: 40000 },
-            { rid: "bar", maxBitrate: 40000, scaleResolutionDownBy: 2 },
-            ]
+          return sender.setParameters({
+            encodings: [{ rid: "foo", maxBitrate: 40000 },
+                        { rid: "bar", maxBitrate: 40000, scaleResolutionDownBy: 2 }]
           });
         }
       ]);
 
       test.chain.insertAfter('PC_LOCAL_GET_ANSWER', [
         function PC_LOCAL_ADD_RIDS_TO_ANSWER(test) {
           test._remote_answer.sdp = sdputils.transferSimulcastProperties(
             test.originalOffer.sdp, test._remote_answer.sdp);
--- a/dom/webidl/RTCRtpSender.webidl
+++ b/dom/webidl/RTCRtpSender.webidl
@@ -65,12 +65,12 @@ dictionary RTCRtpParameters {
   RTCRtcpParameters                         rtcp;
   sequence<RTCRtpCodecParameters>           codecs;
 };
 
 [Pref="media.peerconnection.enabled",
  JSImplementation="@mozilla.org/dom/rtpsender;1"]
 interface RTCRtpSender {
   readonly attribute MediaStreamTrack track;
-  void setParameters (optional RTCRtpParameters parameters);
+  Promise<void> setParameters (optional RTCRtpParameters parameters);
   RTCRtpParameters getParameters();
   Promise<void> replaceTrack(MediaStreamTrack track);
 };