1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Audio test utilities. 6 7// Gathers |numSamples| samples at |frequency| number of times per second and 8// calls back |callback| with an array with numbers in the [0, 32768] range. 9function gatherAudioLevelSamples(peerConnection, numSamples, frequency, 10 callback) { 11 var audioLevelSamples = [] 12 var gatherSamples = setInterval(function() { 13 peerConnection.getStats(function(response) { 14 audioLevelSamples.push(getAudioLevelFromStats_(response)); 15 if (audioLevelSamples.length == numSamples) { 16 clearInterval(gatherSamples); 17 callback(audioLevelSamples); 18 } 19 }); 20 }, 1000 / frequency); 21} 22 23// Tries to identify the beep-every-second signal generated by the fake audio 24// media/audio/fake_audio_input_stream.cc. Fails the test if we can't see a 25// signal. 26function verifyAudioIsPlaying(samples) { 27 var average = 0; 28 for (var i = 0; i < samples.length; ++i) 29 average += samples[i] / samples.length; 30 31 var largest = 0; 32 for (var i = 0; i < samples.length; ++i) 33 largest = Math.max(largest, samples[i]); 34 35 console.log('Average audio level: ' + average + ', largest: ' + largest); 36 37 // TODO(phoglund): Make a more sophisticated curve-fitting algorithm. We want 38 // to see a number of peaks with relative silence between them. The following 39 // seems to work fine on a nexus 7. 40 if (average < 3000 || average > 8000) 41 throw 'Unexpected avg audio level: got ' + average + ', expected it ' + 42 'to be 4000 < avg < 8000.' 43 if (largest < 30000) 44 throw 'Too low max audio level: got ' + largest + ', expected > 30000.'; 45}; 46 47// If silent (like when muted), we should get very near zero audio level. 48function verifyIsSilent(samples) { 49 var average = 0; 50 for (var i = 0; i < samples.length; ++i) 51 average += samples[i] / samples.length; 52 53 console.log('Average audio level: ' + average); 54 if (average > 10) 55 throw 'Expected silence, but avg audio level was ' + average; 56} 57 58/** 59 * @private 60 */ 61function getAudioLevelFromStats_(response) { 62 var reports = response.result(); 63 var audioOutputLevels = []; 64 for (var i = 0; i < reports.length; ++i) { 65 var report = reports[i]; 66 if (report.names().indexOf('audioOutputLevel') != -1) { 67 audioOutputLevels.push(report.stat('audioOutputLevel')); 68 } 69 } 70 // Should only be one audio level reported, otherwise we get confused. 71 expectEquals(1, audioOutputLevels.length); 72 73 return audioOutputLevels[0]; 74}