Bug 1406910 - Break out getSineWaveFile to head.js. r?jib
MozReview-Commit-ID: 9jtLAKvWM4n
--- 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) {