Bug 1296531 - Let waitForAnalysisSuccess take a cancelPromise. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 29 May 2017 15:52:51 +0200
changeset 670324 2231e20902d32c4fe6837671bc6dbaf4f1e4dffb
parent 670323 5a122dc13fa034fa13a3b22183d604141be838e8
child 670325 8a23d15e1d274f6c580ae1c9c0669bb274f9fecc
push id81598
push userbmo:apehrson@mozilla.com
push dateTue, 26 Sep 2017 09:13:19 +0000
reviewersjib
bugs1296531
milestone58.0a1
Bug 1296531 - Let waitForAnalysisSuccess take a cancelPromise. r?jib MozReview-Commit-ID: 3UHUXbg2laL
dom/media/tests/mochitest/head.js
dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
--- a/dom/media/tests/mochitest/head.js
+++ b/dom/media/tests/mochitest/head.js
@@ -114,37 +114,39 @@ AudioStreamAnalyser.prototype = {
     this.sourceNodes.forEach(n => n.disconnect());
     this.sourceNodes = [];
     this.stream.removeEventListener("addtrack", this.onaddtrack);
   },
 
   /**
    * Return a Promise, that will be resolved when the function passed as
    * argument, when called, returns true (meaning the analysis was a
-   * success).
+   * success). The promise is rejected if the cancel promise resolves first.
    *
    * @param {function} analysisFunction
-   *        A fonction that performs an analysis, and returns true if the
+   *        A function that performs an analysis, and resolves with true if the
    *        analysis was a success (i.e. it found what it was looking for)
+   * @param {promise} cancel
+   *        A promise that on resolving will reject the promise we returned.
    */
-  waitForAnalysisSuccess: function(analysisFunction) {
-    var self = this;
-    return new Promise((resolve, reject) => {
-      function analysisLoop() {
-        var success = analysisFunction(self.getByteFrequencyData());
-        if (success) {
-          resolve();
-          return;
-        }
-        // else, we need more time
-        requestAnimationFrame(analysisLoop);
+  waitForAnalysisSuccess: async function(analysisFunction,
+                                         cancel = wait(60000, new Error("Audio analysis timed out"))) {
+    let aborted = false;
+    cancel.then(() => aborted = true);
+
+    // We need to give the Analyser some time to start gathering data.
+    await wait(200);
+
+    do {
+      await new Promise(resolve => requestAnimationFrame(resolve));
+      if (aborted) {
+        throw error;
       }
-      // We need to give the Analyser some time to start gathering data.
-      wait(200).then(analysisLoop);
-    });
+    }
+    while (!analysisFunction(this.getByteFrequencyData()));
   },
 
   /**
    * Return the FFT bin index for a given frequency.
    *
    * @param {double} frequency
    *        The frequency for whicht to return the bin number.
    * @returns {integer} the index of the bin in the FFT array.
--- a/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
+++ b/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
@@ -92,17 +92,17 @@ scriptsReady
         // energy in between, not just a flat white noise.
         return (array[analyser.binIndexForFrequency(50)]    < 50 &&
                 array[analyser.binIndexForFrequency(1000)]  > 200 &&
                 array[analyser.binIndexForFrequency(2500)]  < 50 &&
                 array[analyser.binIndexForFrequency(5000)]  > 200 &&
                 array[analyser.binIndexForFrequency(7500)]  < 50 &&
                 array[analyser.binIndexForFrequency(10000)] > 200);
       }).then(finish);
-    }).catch(finish);
+    });
   });
 });
 
 
 
 </script>
 </pre>
 </body>