Bug 1120266 - Factor some machinery out of test_BufferingWait into mediasource.js and make it Promise-friendly. r=jya
authorBobby Holley <bobbyholley@gmail.com>
Fri, 16 Jan 2015 10:57:59 -0800
changeset 224304 4c33a6636cc4de825bc39c0e2d587d6bbaebf890
parent 224303 6f0f1a79b3790a240ba66736ebbca682f24700f7
child 224305 a4f7c77bec123f2585644662865516ff9c79fef4
push id28122
push userkwierso@gmail.com
push dateSat, 17 Jan 2015 01:33:15 +0000
treeherdermozilla-central@369a8f14ccf8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1120266
milestone38.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 1120266 - Factor some machinery out of test_BufferingWait into mediasource.js and make it Promise-friendly. r=jya
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,17 +21,61 @@ 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);
+  });
+}
--- 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>