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