Bug 1120266 - Factor some machinery out of test_BufferingWait into mediasource.js and make it Promise-friendly. r=jya, a=sledru
authorBobby Holley <bobbyholley@gmail.com>
Fri, 16 Jan 2015 10:57:59 -0800
changeset 242998 ff1b74ec9f19
parent 242997 9b565733c680
child 242999 53f55825252a
push id4358
push userryanvm@gmail.com
push date2015-01-22 19:57 +0000
treeherdermozilla-beta@62f7b8ea571f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya, sledru
bugs1120266
milestone36.0
Bug 1120266 - Factor some machinery out of test_BufferingWait into mediasource.js and make it Promise-friendly. r=jya, a=sledru
dom/media/mediasource/test/mediasource.js
dom/media/mediasource/test/test_BufferingWait.html
--- a/dom/media/mediasource/test/mediasource.js
+++ b/dom/media/mediasource/test/mediasource.js
@@ -21,25 +21,69 @@ function runWithMSE(testFunction) {
 	[ "media.mediasource.enabled", true ],
 	[ "media.mediasource.youtubeonly", false ],
     ]},
                               bootstrapTest);
   });
 }
 
 function fetchWithXHR(uri, onLoadFunction) {
-  var xhr = new XMLHttpRequest();
-  xhr.open("GET", uri, true);
-  xhr.responseType = "arraybuffer";
-  xhr.addEventListener("load", function () {
-    is(xhr.status, 200, "fetchWithXHR load uri='" + uri + "' status=" + xhr.status);
-    onLoadFunction(xhr.response);
+  var p = new Promise(function(resolve, reject) {
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", uri, true);
+    xhr.responseType = "arraybuffer";
+    xhr.addEventListener("load", function () {
+      is(xhr.status, 200, "fetchWithXHR load uri='" + uri + "' status=" + xhr.status);
+      resolve(xhr.response);
+    });
+    xhr.send();
+  });
+
+  if (onLoadFunction) {
+    p.then(onLoadFunction);
+  }
+
+  return p;
+};
+
+function once(target, name, cb) {
+  var p = new Promise(function(resolve, reject) {
+    target.addEventListener(name, function() {
+      target.removeEventListener(name, cb);
+      resolve();
+    });
   });
-  xhr.send();
-};
+  if (cb) {
+    p.then(cb);
+  }
+  return p;
+}
+
+function timeRangeToString(r) {
+  var str = "TimeRanges: ";
+  for (var i = 0; i < r.length; i++) {
+    str += "[" + r.start(i) + ", " + r.end(i) + ")";
+  }
+  return str;
+}
+
+function loadSegment(sb, typedArrayOrArrayBuffer) {
+  var typedArray = (typedArrayOrArrayBuffer instanceof ArrayBuffer) ? new Uint8Array(typedArrayOrArrayBuffer)
+                                                                    : typedArrayOrArrayBuffer;
+  info(`Loading buffer: [${typedArray.byteOffset}, ${typedArray.byteOffset + typedArray.byteLength})`);
+  var beforeBuffered = timeRangeToString(sb.buffered);
+  return new Promise(function(resolve, reject) {
+    once(sb, 'update').then(function() {
+      var afterBuffered = timeRangeToString(sb.buffered);
+      info(`SourceBuffer buffered ranges grew from ${beforeBuffered} to ${afterBuffered}`);
+      resolve();
+    });
+    sb.appendBuffer(typedArray);
+  });
+}
 
 function fetchAndLoad(sb, prefix, chunks, suffix) {
 
   // Fetch the buffers in parallel.
   var buffers = {};
   var fetches = [];
   for (var chunk of chunks) {
     fetches.push(fetchWithXHR(prefix + chunk + suffix).then(((c, x) => buffers[c] = x).bind(null, chunk)));
--- a/dom/media/mediasource/test/test_BufferingWait.html
+++ b/dom/media/mediasource/test/test_BufferingWait.html
@@ -15,67 +15,53 @@ var receivedSourceOpen = false;
 runWithMSE(function(ms, v) {
   ms.addEventListener("sourceopen", function() {
     ok(true, "Receive a sourceopen event");
     ok(!receivedSourceOpen, "Should only receive one sourceopen for this test");
     receivedSourceOpen = true;
     var sb = ms.addSourceBuffer("video/webm");
     ok(sb, "Create a SourceBuffer");
 
-    function once(target, name, cb) {
-      target.addEventListener(name, function() {
-        target.removeEventListener(name, cb);
-        cb();
-      });
-    }
-    function loadSegment(typedArray) {
-      info(`Loading buffer: [${typedArray.byteOffset}, ${typedArray.byteOffset + typedArray.byteLength})`);
-      return new Promise(function(resolve, reject) {
-        once(sb, 'update', resolve);
-        sb.appendBuffer(typedArray);
-      });
-    }
-
     function waitUntilTime(targetTime) {
       return new Promise(function(resolve, reject) {
         v.addEventListener("waiting", function onwaiting() {
           info("Got a waiting event at " + v.currentTime);
           if (v.currentTime >= targetTime) {
             ok(true, "Reached target time of: " + targetTime);
             v.removeEventListener("waiting", onwaiting);
             resolve();
           }
         });
       });
     }
 
     fetchWithXHR("seek.webm", function(arrayBuffer) {
       sb.addEventListener('error', (e) => { ok(false, "Got Error: " + e); SimpleTest.finish(); });
-      loadSegment.bind(null, new Uint8Array(arrayBuffer, 0, 318))().then(
-      loadSegment.bind(null, new Uint8Array(arrayBuffer, 318, 25223-318))).then(
-      loadSegment.bind(null, new Uint8Array(arrayBuffer, 25223, 46712-25223))).then(
+      loadSegment.bind(null, sb, new Uint8Array(arrayBuffer, 0, 318))().then(
+      loadSegment.bind(null, sb, new Uint8Array(arrayBuffer, 318, 25223-318))).then(
+      loadSegment.bind(null, sb, new Uint8Array(arrayBuffer, 25223, 46712-25223))).then(
       /* Note - Missing |46712, 67833 - 46712| segment here corresponding to (0.8, 1.2] */
       /* Note - Missing |67833, 88966 - 67833| segment here corresponding to (1.2, 1.6]  */
-      loadSegment.bind(null, new Uint8Array(arrayBuffer, 88966))).then(function() {
+      loadSegment.bind(null, sb, new Uint8Array(arrayBuffer, 88966))).then(function() {
         var promise = waitUntilTime(0.7);
         info("Playing video. It should play for a bit, then fire 'waiting'");
         v.play();
         return promise;
       }).then(function() {
         window.firstStop = Date.now();
-        loadSegment(new Uint8Array(arrayBuffer, 46712, 67833 - 46712));
+        loadSegment(sb, new Uint8Array(arrayBuffer, 46712, 67833 - 46712));
         return waitUntilTime(1.0);
       }).then(function() {
         var waitDuration = (Date.now() - window.firstStop) / 1000;
         ok(waitDuration < 15, "Should not spend an inordinate amount of time buffering: " + waitDuration);
         SimpleTest.finish();
         /* If we allow the rest of the stream to be played, we get stuck at
            around 2s. See bug 1093133.
         once(v, 'ended', SimpleTest.finish.bind(SimpleTest));
-        return loadSegment(new Uint8Array(arrayBuffer, 67833, 88966 - 67833));
+        return loadSegment(sb, new Uint8Array(arrayBuffer, 67833, 88966 - 67833));
         */
       });
     });
   });
 });
 </script>
 </pre>
 </body>