Bug 1523563 - Modernize test_gUM_audioCapture.html somewhat. r=jib
authorAndreas Pehrson <apehrson@mozilla.com>
Thu, 20 Jun 2019 08:03:35 +0000
changeset 543924 d9f2bc3dd281e95f1711f327e8d45649aa2f8077
parent 543923 6c52ecd21b3033b903d1689eecd6134ab28d5aed
child 543927 580ca9880dfa748c2dc92c3dbe4cb3e546c4ed83
push id2131
push userffxbld-merge
push dateMon, 26 Aug 2019 18:30:20 +0000
treeherdermozilla-release@b19ffb3ca153 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1523563
milestone69.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 1523563 - Modernize test_gUM_audioCapture.html somewhat. r=jib Differential Revision: https://phabricator.services.mozilla.com/D35174
dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
--- a/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
+++ b/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
@@ -9,41 +9,34 @@
 <script>
 
 createHTML({
   bug: "1156472",
   title: "Test AudioCapture with regular HTMLMediaElement, AudioContext, and HTMLMediaElement playing a MediaStream",
   visible: true
 });
 
-scriptsReady
-.then(() => FAKE_ENABLED = false)
-.then(() => {
-  runTestWhenReady(function() {
-    // Get an opus file containing a sine wave at maximum amplitude, of duration
-    // `lengthSeconds`, and of frequency `frequency`.
-    function getSineWaveFile(frequency, lengthSeconds, callback) {
-      var chunks = [];
-      var off = new OfflineAudioContext(1, lengthSeconds * 48000, 48000);
-      var osc = off.createOscillator();
-      var rec = new MediaRecorder(osc);
-      rec.ondataavailable = function(e) {
-        chunks.push(e.data);
-      };
-      rec.onstop = function(e) {
-        var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
-        callback(blob);
-      }
-      osc.frequency.value = frequency;
-      osc.start();
-      rec.start();
-      off.startRendering().then(function(buffer) {
-        rec.stop();
-      });
-    }
+// Get an opus file containing a sine wave at maximum amplitude, of duration
+// `lengthSeconds`, and of frequency `frequency`.
+async function getSineWaveFile(frequency, lengthSeconds) {
+  const off = new OfflineAudioContext(1, lengthSeconds * 48000, 48000);
+  const osc = off.createOscillator();
+  const rec = new MediaRecorder(osc, {mimeType: "audio/ogg; codecs=opus"});
+  osc.frequency.value = frequency;
+  osc.start();
+  rec.start();
+  off.startRendering();
+  const {data} = await new Promise(r => rec.ondataavailable = r);
+  return data;
+}
+
+(async function() {
+  await scriptsReady;
+  FAKE_ENABLED = false;
+  await runTestWhenReady(async function() {
     /**
      * Get two HTMLMediaElements:
      * - One playing a sine tone from a blob (of an opus file created on the fly)
      * - One being the output for an AudioContext's OscillatorNode, connected to
      *   a MediaSourceDestinationNode.
      *
      * Also, use the AudioContext playing through its AudioDestinationNode another
      * tone, using another OscillatorNode.
@@ -51,59 +44,58 @@ scriptsReady
      * Capture the output of the document, feed that back into the AudioContext,
      * with an AnalyserNode, and check the frequency content to make sure we
      * have recorded the three sources.
      *
      * The three sine tones have frequencies far apart from each other, so that we
      * can check that the spectrum of the capture stream contains three
      * components with a high magnitude.
      */
-    var wavtone = createMediaElement("audio", "WaveTone");
-    var acTone = createMediaElement("audio", "audioContextTone");
-    var ac = new AudioContext();
+    const wavtone = createMediaElement("audio", "WaveTone");
+    const acTone = createMediaElement("audio", "audioContextTone");
+    const ac = new AudioContext();
 
-    var oscThroughMediaElement = ac.createOscillator();
+    const oscThroughMediaElement = ac.createOscillator();
     oscThroughMediaElement.frequency.value = 1000;
-    var oscThroughAudioDestinationNode = ac.createOscillator();
+    const oscThroughAudioDestinationNode = ac.createOscillator();
     oscThroughAudioDestinationNode.frequency.value = 5000;
-    var msDest = ac.createMediaStreamDestination();
+    const msDest = ac.createMediaStreamDestination();
 
     oscThroughMediaElement.connect(msDest);
     oscThroughAudioDestinationNode.connect(ac.destination);
 
     acTone.srcObject = msDest.stream;
 
-    getSineWaveFile(10000, 10, function(blob) {
-      wavtone.src = URL.createObjectURL(blob);
-      oscThroughMediaElement.start();
-      oscThroughAudioDestinationNode.start();
-      wavtone.loop = true;
-      wavtone.play();
-      acTone.play();
-    });
+    const blob = await getSineWaveFile(10000, 10);
+    wavtone.src = URL.createObjectURL(blob);
+    oscThroughMediaElement.start();
+    oscThroughAudioDestinationNode.start();
+    wavtone.loop = true;
+    wavtone.play();
+    acTone.play();
 
-    var constraints = {audio: {mediaSource: "audioCapture"}};
+    const constraints = {audio: {mediaSource: "audioCapture"}};
 
-    return getUserMedia(constraints).then((stream) => {
-      window.grip = stream;
-      var analyser = new AudioStreamAnalyser(ac, stream);
-      analyser.enableDebugCanvas();
-      return analyser.waitForAnalysisSuccess(function(array) {
-        // We want to find three frequency components here, around 1000, 5000
-        // and 10000Hz. Frequency are logarithmic. Also make sure we have low
-        // 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);
+    const stream = await getUserMedia(constraints);
+    window.grip = stream;
+    const analyser = new AudioStreamAnalyser(ac, stream);
+    analyser.enableDebugCanvas();
+    await analyser.waitForAnalysisSuccess(array => {
+      // We want to find three frequency components here, around 1000, 5000
+      // and 10000Hz. Frequency are logarithmic. Also make sure we have low
+      // 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);
     });
+    finish();
   });
-});
+})();
 
 
 
 </script>
 </pre>
 </body>
 </html>