Bug 1406910 - Break out getSineWaveFile to head.js. r?jib draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Fri, 13 Oct 2017 15:29:47 +0200
changeset 766752 929e65c8e0e599c3c4f6e627be595f3a50c0a42c
parent 764177 cb3b2b090314e6b1c4bc0bc7ebe7814ba474d974
child 766753 9725938897541e366cece0af4be2e47574536817
push id102394
push userbmo:apehrson@mozilla.com
push dateTue, 13 Mar 2018 09:57:43 +0000
reviewersjib
bugs1406910
milestone60.0a1
Bug 1406910 - Break out getSineWaveFile to head.js. r?jib MozReview-Commit-ID: 9jtLAKvWM4n
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
@@ -18,16 +18,32 @@ try {
   FAKE_ENABLED = false;
   // It will be updated to 440 when/if DefaultLoopbackTone is instantiated.
   TEST_AUDIO_FREQ = -1;
 } catch (e) {
   dump('TEST DEVICES: No test devices found (in media.{audio,video}_loopback_dev, using fake streams.\n');
   FAKE_ENABLED = true;
 }
 
+// Get an opus file containing a sine wave at maximum amplitude, of duration
+// `lengthSeconds`, and of frequency `frequency`.
+async function getSineWaveFile(frequency, lengthSeconds) {
+  let off = new OfflineAudioContext(1, lengthSeconds * 48000, 48000);
+  let osc = off.createOscillator();
+  let rec = new MediaRecorder(osc);
+
+  osc.frequency.value = frequency;
+  osc.start();
+  rec.start();
+
+  off.startRendering().then(() => rec.stop());
+
+  return (await haveEvent(rec, "dataavailable")).data;
+}
+
 /**
  *  Global flag to skip LoopbackTone
  */
 var DISABLE_LOOPBACK_TONE = false
 /**
  * Helper class to setup a sine tone of a given frequency.
  */
 class LoopbackTone {
--- a/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
+++ b/dom/media/tests/mochitest/test_getUserMedia_audioCapture.html
@@ -13,30 +13,16 @@ createHTML({
   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();
       });
     }
     /**
@@ -66,24 +52,24 @@ scriptsReady
     oscThroughAudioDestinationNode.frequency.value = 5000;
     var msDest = ac.createMediaStreamDestination();
 
     oscThroughMediaElement.connect(msDest);
     oscThroughAudioDestinationNode.connect(ac.destination);
 
     acTone.srcObject = msDest.stream;
 
-    getSineWaveFile(10000, 10, function(blob) {
+    getSineWaveFile(10000, 10).then(blob => {
       wavtone.src = URL.createObjectURL(blob);
       oscThroughMediaElement.start();
       oscThroughAudioDestinationNode.start();
       wavtone.loop = true;
       wavtone.play();
       acTone.play();
-    });
+    }).catch(e => ok(false, `getSineWaveFile() failed with ${e.name}`));
 
     var 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) {