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 // GetStats reports audio output energy in the [0, 32768] range. | |
8 var MAX_AUDIO_OUTPUT_ENERGY = 32768; | |
9 | |
10 // Gathers |numSamples| samples at |frequency| number of times per second and | 7 // Gathers |numSamples| samples at |frequency| number of times per second and |
11 // 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. |
12 function gatherAudioLevelSamples(peerConnection, numSamples, frequency, | 9 function gatherAudioLevelSamples(peerConnection, numSamples, frequency, |
13 callback) { | 10 callback) { |
14 console.log('Gathering ' + numSamples + ' audio samples...'); | 11 console.log('Gathering ' + numSamples + ' audio samples...'); |
15 var audioLevelSamples = [] | 12 var audioLevelSamples = [] |
16 var gatherSamples = setInterval(function() { | 13 var gatherSamples = setInterval(function() { |
17 peerConnection.getStats(function(response) { | 14 peerConnection.getStats(function(response) { |
18 audioLevelSamples.push(getAudioLevelFromStats_(response)); | 15 audioLevelSamples.push(getAudioLevelFromStats_(response)); |
19 if (audioLevelSamples.length == numSamples) { | 16 if (audioLevelSamples.length == numSamples) { |
20 console.log('Gathered all samples.'); | 17 console.log('Gathered all samples.'); |
21 clearInterval(gatherSamples); | 18 clearInterval(gatherSamples); |
22 callback(audioLevelSamples); | 19 callback(audioLevelSamples); |
23 } | 20 } |
24 }); | 21 }); |
25 }, 1000 / frequency); | 22 }, 1000 / frequency); |
26 } | 23 } |
27 | 24 |
28 // Tries to identify the beep-every-half-second signal generated by the fake | 25 // Tries to identify the beep-every-second signal generated by the fake audio |
29 // audio device in media/video/capture/fake_video_capture_device.cc. Fails the | 26 // media/audio/fake_audio_input_stream.cc. Fails the test if we can't see a |
30 // test if we can't see a signal. The samples should have been gathered over at | 27 // signal. |
31 // least two seconds since we expect to see at least three "peaks" in there | |
32 // (we should see either 3 or 4 depending on how things line up). | |
33 function verifyAudioIsPlaying(samples) { | 28 function verifyAudioIsPlaying(samples) { |
34 var numPeaks = 0; | 29 var average = 0; |
35 var threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.7; | 30 for (var i = 0; i < samples.length; ++i) |
36 var currentlyOverThreshold = false; | 31 average += samples[i] / samples.length; |
37 | 32 |
38 // Detect when we have been been over the threshold and is going back again | 33 var largest = 0; |
39 // (i.e. count peaks). We should see about one peak per second. | 34 for (var i = 0; i < samples.length; ++i) |
40 for (var i = 0; i < samples.length; ++i) { | 35 largest = Math.max(largest, samples[i]); |
41 if (currentlyOverThreshold && samples[i] < threshold) | |
42 numPeaks++; | |
43 currentlyOverThreshold = samples[i] >= threshold; | |
44 } | |
45 | 36 |
46 console.log('Number of peaks identified: ' + numPeaks); | 37 console.log('Average audio level: ' + average + ', largest: ' + largest); |
47 | 38 |
48 if (numPeaks < 2) | 39 // TODO(phoglund): Make a more sophisticated curve-fitting algorithm. We want |
49 failTest('Expected to see at least two peaks in audio signal, got ' + | 40 // to see a number of peaks with relative silence between them. The following |
50 numPeaks + '. Dumping samples for analysis: "' + samples + '"'); | 41 // seems to work fine on a nexus 7. |
| 42 if (average < 3000 || average > 8000) |
| 43 failTest('Unexpected avg audio level: got ' + average + ', expected it ' + |
| 44 'to be 4000 < avg < 8000.'); |
| 45 if (largest < 25000) |
| 46 failTest('Too low max audio level: got ' + largest + ', expected > 30000.'); |
51 } | 47 } |
52 | 48 |
53 // 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. |
54 function verifyIsSilent(samples) { | 50 function verifyIsSilent(samples) { |
55 var average = 0; | 51 var average = 0; |
56 for (var i = 0; i < samples.length; ++i) | 52 for (var i = 0; i < samples.length; ++i) |
57 average += samples[i] / samples.length; | 53 average += samples[i] / samples.length; |
58 | 54 |
59 console.log('Average audio level: ' + average); | 55 console.log('Average audio level: ' + average); |
60 if (average > 500) | 56 if (average > 500) |
(...skipping 10 matching lines...) Expand all Loading... |
71 var report = reports[i]; | 67 var report = reports[i]; |
72 if (report.names().indexOf('audioOutputLevel') != -1) { | 68 if (report.names().indexOf('audioOutputLevel') != -1) { |
73 audioOutputLevels.push(report.stat('audioOutputLevel')); | 69 audioOutputLevels.push(report.stat('audioOutputLevel')); |
74 } | 70 } |
75 } | 71 } |
76 // Should only be one audio level reported, otherwise we get confused. | 72 // Should only be one audio level reported, otherwise we get confused. |
77 assertEquals(1, audioOutputLevels.length); | 73 assertEquals(1, audioOutputLevels.length); |
78 | 74 |
79 return audioOutputLevels[0]; | 75 return audioOutputLevels[0]; |
80 } | 76 } |
OLD | NEW |