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. | 7 // GetStats reports audio output energy in the [0, 32768] range. |
8 var MAX_AUDIO_OUTPUT_ENERGY = 32768; | 8 var MAX_AUDIO_OUTPUT_ENERGY = 32768; |
9 | 9 |
10 // Gathers |numSamples| samples at |frequency| number of times per second and | 10 // Gathers |numSamples| samples at |frequency| number of times per second and |
(...skipping 11 matching lines...) Expand all Loading... |
22 callback(audioLevelSamples); | 22 callback(audioLevelSamples); |
23 } | 23 } |
24 }); | 24 }); |
25 }, 1000 / frequency); | 25 }, 1000 / frequency); |
26 } | 26 } |
27 | 27 |
28 // Tries to identify the beep-every-half-second signal generated by the fake | 28 // Tries to identify the beep-every-half-second signal generated by the fake |
29 // audio device in media/video/capture/fake_video_capture_device.cc. Fails the | 29 // audio device in media/video/capture/fake_video_capture_device.cc. Fails the |
30 // test if we can't see a signal. The samples should have been gathered over at | 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 | 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). We are quite | 32 // (we should see either 3 or 4 depending on how things line up). |
33 // generous in what we consider to be a peak since Android devices in particular | 33 // |
34 // seem to be flake-prone (they often don't reach anywhere near the max level | 34 // If |beLenient| is specified, we assume we're running on a slow device or |
35 // for some reason). | 35 // or under TSAN, and relax the checks quite a bit. |
36 function verifyAudioIsPlaying(samples) { | 36 function verifyAudioIsPlaying(samples, beLenient) { |
37 var numPeaks = 0; | 37 var numPeaks = 0; |
38 var threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.6; | 38 var threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.7; |
| 39 if (beLenient) |
| 40 threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.6; |
39 var currentlyOverThreshold = false; | 41 var currentlyOverThreshold = false; |
40 | 42 |
41 // Detect when we have been been over the threshold and is going back again | 43 // Detect when we have been been over the threshold and is going back again |
42 // (i.e. count peaks). We should see about one peak per second. | 44 // (i.e. count peaks). We should see about one peak per second. |
43 for (var i = 0; i < samples.length; ++i) { | 45 for (var i = 0; i < samples.length; ++i) { |
44 if (currentlyOverThreshold && samples[i] < threshold) | 46 if (currentlyOverThreshold && samples[i] < threshold) |
45 numPeaks++; | 47 numPeaks++; |
46 currentlyOverThreshold = samples[i] >= threshold; | 48 currentlyOverThreshold = samples[i] >= threshold; |
47 } | 49 } |
48 | 50 |
49 console.log('Number of peaks identified: ' + numPeaks); | 51 console.log('Number of peaks identified: ' + numPeaks); |
50 | 52 |
51 if (numPeaks < 2) | 53 var expectedPeaks = 2; |
52 failTest('Expected to see at least two peaks in audio signal, got ' + | 54 if (beLenient) |
53 numPeaks + '. Dumping samples for analysis: "' + samples + '"'); | 55 expectedPeaks = 1; |
| 56 |
| 57 if (numPeaks < expectedPeaks) |
| 58 failTest('Expected to see at least ' + expectedPeaks + ' peak(s) in ' + |
| 59 'audio signal, got ' + numPeaks + '. Dumping samples for analysis: "' + |
| 60 samples + '"'); |
54 } | 61 } |
55 | 62 |
56 // If silent (like when muted), we should get very near zero audio level. | 63 // If silent (like when muted), we should get very near zero audio level. |
57 function verifyIsSilent(samples) { | 64 function verifyIsSilent(samples) { |
58 var average = 0; | 65 var average = 0; |
59 for (var i = 0; i < samples.length; ++i) | 66 for (var i = 0; i < samples.length; ++i) |
60 average += samples[i] / samples.length; | 67 average += samples[i] / samples.length; |
61 | 68 |
62 console.log('Average audio level: ' + average); | 69 console.log('Average audio level: ' + average); |
63 if (average > 500) | 70 if (average > 500) |
(...skipping 10 matching lines...) Expand all Loading... |
74 var report = reports[i]; | 81 var report = reports[i]; |
75 if (report.names().indexOf('audioOutputLevel') != -1) { | 82 if (report.names().indexOf('audioOutputLevel') != -1) { |
76 audioOutputLevels.push(report.stat('audioOutputLevel')); | 83 audioOutputLevels.push(report.stat('audioOutputLevel')); |
77 } | 84 } |
78 } | 85 } |
79 // Should only be one audio level reported, otherwise we get confused. | 86 // Should only be one audio level reported, otherwise we get confused. |
80 assertEquals(1, audioOutputLevels.length); | 87 assertEquals(1, audioOutputLevels.length); |
81 | 88 |
82 return audioOutputLevels[0]; | 89 return audioOutputLevels[0]; |
83 } | 90 } |
OLD | NEW |