Bug 1259788 - Make AudioStreamAnalyser (test-only) work with dynamically added tracks. r?jib
MozReview-Commit-ID: 1mpHKMyTHy8
--- a/dom/media/tests/mochitest/head.js
+++ b/dom/media/tests/mochitest/head.js
@@ -28,23 +28,29 @@ try {
*
* @constructor
* @param {object} stream
* A MediaStream object whose audio track we shall analyse.
*/
function AudioStreamAnalyser(ac, stream) {
this.audioContext = ac;
this.stream = stream;
- this.sourceNodes = this.stream.getAudioTracks().map(
- t => this.audioContext.createMediaStreamSource(new MediaStream([t])));
+ this.sourceNodes = [];
this.analyser = this.audioContext.createAnalyser();
// Setting values lower than default for speedier testing on emulators
this.analyser.smoothingTimeConstant = 0.2;
this.analyser.fftSize = 1024;
- this.sourceNodes.forEach(n => n.connect(this.analyser));
+ this.connectTrack = t => {
+ let source = this.audioContext.createMediaStreamSource(new MediaStream([t]));
+ this.sourceNodes.push(source);
+ source.connect(this.analyser);
+ };
+ this.stream.getAudioTracks().forEach(t => this.connectTrack(t));
+ this.onaddtrack = ev => this.connectTrack(ev.track);
+ this.stream.addEventListener("addtrack", this.onaddtrack);
this.data = new Uint8Array(this.analyser.frequencyBinCount);
}
AudioStreamAnalyser.prototype = {
/**
* Get an array of frequency domain data for our stream's audio track.
*
* @returns {array} A Uint8Array containing the frequency domain data.
@@ -101,16 +107,17 @@ AudioStreamAnalyser.prototype = {
* Disconnects the input stream from our internal analyser node.
* Call this to reduce main thread processing, mostly necessary on slow
* devices.
*/
disconnect: function() {
this.disableDebugCanvas();
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).
*
* @param {function} analysisFunction