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