Bug 1259788 - Make AudioStreamAnalyser (test-only) work with dynamically added tracks. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Wed, 06 Jul 2016 10:33:30 +0200
changeset 404923 5e2b512bb84a12c2a6895538b564b5e86f8cb893
parent 404922 61b807c526b0cb5dd2be33fc6a809c00212741d9
child 404924 3cbbdeb4c14a5970dbf10efa61e6879466447f84
push id27359
push userpehrsons@gmail.com
push dateWed, 24 Aug 2016 14:53:56 +0000
reviewersjib
bugs1259788
milestone51.0a1
Bug 1259788 - Make AudioStreamAnalyser (test-only) work with dynamically added tracks. r?jib MozReview-Commit-ID: 1mpHKMyTHy8
dom/media/tests/mochitest/head.js
--- 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