Bug 1395853 - Add convenience methods regarding codec ids to sdpUtils. r=drno
authorAndreas Pehrson <pehrsons@mozilla.com>
Wed, 13 Sep 2017 11:38:14 +0200
changeset 385433 807225b4dc5bfeafec2725011f4bb7dba7493c87
parent 385432 5eb3882e6de1d8f6ab76bf44ef85b99fc22488ab
child 385434 37ca45926c5b428f5d0894f6e9d4845c63ea9768
push id32656
push userarchaeopteryx@coole-files.de
push dateWed, 11 Oct 2017 09:50:40 +0000
treeherdermozilla-central@20d9ad08dd36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdrno
bugs1395853
milestone58.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 1395853 - Add convenience methods regarding codec ids to sdpUtils. r=drno MozReview-Commit-ID: J4bZhZ6gm7O
dom/media/tests/mochitest/sdpUtils.js
dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
--- a/dom/media/tests/mochitest/sdpUtils.js
+++ b/dom/media/tests/mochitest/sdpUtils.js
@@ -1,14 +1,32 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var sdputils = {
 
+// Finds the codec id / payload type given a codec format
+// (e.g., "VP8", "VP9/90000"). `offset` tells us which one to use in case of
+// multiple matches.
+findCodecId: function(sdp, format, offset = 0) {
+  let regex = new RegExp("rtpmap:\([0-9]+\) " + format, "gi");
+  let match;
+  for (let i = 0; i <= offset; ++i) {
+    match = regex.exec(sdp);
+    if (!match) {
+      throw new Error("Couldn't find offset " + i + " of codec " + format
+        + " while looking for offset " + offset + " in sdp:\n" + sdp);
+    }
+  }
+  // match[0] is the full matched string
+  // match[1] is the first parenthesis group
+  return match[1];
+},
+
 checkSdpAfterEndOfTrickle: function(sdp, testOptions, label) {
   info("EOC-SDP: " + JSON.stringify(sdp));
 
   ok(sdp.sdp.includes("a=end-of-candidates"), label + ": SDP contains end-of-candidates");
   sdputils.checkSdpCLineNotDefault(sdp.sdp, label);
 
   if (testOptions.rtcpmux) {
     ok(sdp.sdp.includes("a=rtcp-mux"), label + ": SDP contains rtcp-mux");
@@ -25,26 +43,35 @@ checkSdpAfterEndOfTrickle: function(sdp,
 // takes sdp in string form (or possibly a fragment, say an m-section), and
 // verifies that the default 0.0.0.0 addr is not present.
 checkSdpCLineNotDefault: function(sdpStr, label) {
   info("CLINE-NO-DEFAULT-ADDR-SDP: " + JSON.stringify(sdpStr));
   ok(!sdpStr.includes("c=IN IP4 0.0.0.0"), label + ": SDP contains non-zero IP c line");
 },
 
 // Note, we don't bother removing the fmtp lines, which makes a good test
-// for some SDP parsing issues.  It would be good to have a "removeAllButCodec(sdp, codec)" too.
+// for some SDP parsing issues. 
 removeCodec: function(sdp, codec) {
     var updated_sdp = sdp.replace(new RegExp("a=rtpmap:" + codec + ".*\\/90000\\r\\n",""),"");
     updated_sdp = updated_sdp.replace(new RegExp("(RTP\\/SAVPF.*)( " + codec + ")(.*\\r\\n)",""),"$1$3");
     updated_sdp = updated_sdp.replace(new RegExp("a=rtcp-fb:" + codec + " nack\\r\\n",""),"");
     updated_sdp = updated_sdp.replace(new RegExp("a=rtcp-fb:" + codec + " nack pli\\r\\n",""),"");
     updated_sdp = updated_sdp.replace(new RegExp("a=rtcp-fb:" + codec + " ccm fir\\r\\n",""),"");
   return updated_sdp;
 },
 
+removeAllButPayloadType: function(sdp, pt) {
+  return sdp.replace(new RegExp("m=(\\w+ \\w+) UDP/TLS/RTP/SAVPF .*" + pt + ".*\\r\\n", "gi"),
+                     "m=$1 UDP/TLS/RTP/SAVPF " + pt + "\r\n");
+},
+
+removeRtpMapForPayloadType: function(sdp, pt) {
+  return sdp.replace(new RegExp("a=rtpmap:" + pt + ".*\\r\\n", "gi"), "");
+},
+
 removeRtcpMux: function(sdp) {
   return sdp.replace(/a=rtcp-mux\r\n/g,"");
 },
 
 removeSSRCs: function(sdp) {
   return sdp.replace(/a=ssrc.*\r\n/g,"");
 },
 
--- a/dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
+++ b/dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
@@ -13,33 +13,31 @@
   });
 
   const pushPrefs = (...p) => SpecialPowers.pushPrefEnv({set: p});
 
   var mustRejectWith = (msg, reason, f) =>
     f().then(() => ok(false, msg),
              e => is(e.name, reason, msg));
 
-  var removeAllButCodec = (d, codec) =>
-    (d.sdp = d.sdp.replace(/m=video (\w) UDP\/TLS\/RTP\/SAVPF \w.*\r\n/,
-                           "m=video $1 UDP/TLS/RTP/SAVPF " + codec + "\r\n"), d);
-
   function testScale(codec) {
     var pc1 = new RTCPeerConnection();
     var pc2 = new RTCPeerConnection();
 
     var add = (pc, can, failed) => can && pc.addIceCandidate(can).catch(failed);
     pc1.onicecandidate = e => add(pc2, e.candidate, generateErrorCallback());
     pc2.onicecandidate = e => add(pc1, e.candidate, generateErrorCallback());
 
     info("testing scaling with " + codec);
 
     pc1.onnegotiationneeded = e =>
       pc1.createOffer()
-      .then(d => pc1.setLocalDescription(codec == "VP8" ? d : removeAllButCodec(d, 126)))
+      .then(d => pc1.setLocalDescription(codec == "VP8"
+        ? d
+        : (d.sdp = sdputils.removeAllButPayloadType(d.sdp, 126), d)))
       .then(() => pc2.setRemoteDescription(pc1.localDescription))
       .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
       .then(() => pc1.setRemoteDescription(pc2.localDescription))
       .catch(generateErrorCallback());
 
     return navigator.mediaDevices.getUserMedia({ video: true })
     .then(stream => {
       var v1 = createMediaElement('video', 'v1');