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.
5 // Audio test utilities.
7 // GetStats reports audio output energy in the [0, 32768] range.
8 var MAX_AUDIO_OUTPUT_ENERGY = 32768;
10 // 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.
12 function gatherAudioLevelSamples(peerConnection, numSamples, frequency,
14 console.log('Gathering ' + numSamples + ' audio samples...');
15 var audioLevelSamples = []
16 var gatherSamples = setInterval(function() {
17 peerConnection.getStats(function(response) {
18 audioLevelSamples.push(getAudioLevelFromStats_(response));
19 if (audioLevelSamples.length == numSamples) {
20 console.log('Gathered all samples.');
21 clearInterval(gatherSamples);
22 callback(audioLevelSamples);
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
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).
34 // If |beLenient| is specified, we assume we're running on a slow device or
35 // or under TSAN, and relax the checks quite a bit.
36 function verifyAudioIsPlaying(samples, beLenient) {
38 var threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.7;
40 threshold = MAX_AUDIO_OUTPUT_ENERGY * 0.6;
41 var currentlyOverThreshold = false;
43 // Detect when we have been been over the threshold and is going back again
44 // (i.e. count peaks). We should see about one peak per second.
45 for (var i = 0; i < samples.length; ++i) {
46 if (currentlyOverThreshold && samples[i] < threshold)
48 currentlyOverThreshold = samples[i] >= threshold;
51 console.log('Number of peaks identified: ' + numPeaks);
53 var expectedPeaks = 2;
57 if (numPeaks < expectedPeaks)
58 failTest('Expected to see at least ' + expectedPeaks + ' peak(s) in ' +
59 'audio signal, got ' + numPeaks + '. Dumping samples for analysis: "' +
63 // If silent (like when muted), we should get very near zero audio level.
64 function verifyIsSilent(samples) {
66 for (var i = 0; i < samples.length; ++i)
67 average += samples[i] / samples.length;
69 console.log('Average audio level: ' + average);
71 failTest('Expected silence, but avg audio level was ' + average);
77 function getAudioLevelFromStats_(response) {
78 var reports = response.result();
79 var audioOutputLevels = [];
80 for (var i = 0; i < reports.length; ++i) {
81 var report = reports[i];
82 if (report.names().indexOf('audioOutputLevel') != -1) {
83 audioOutputLevels.push(report.stat('audioOutputLevel'));
86 // Should only be one audio level reported, otherwise we get confused.
87 assertEquals(1, audioOutputLevels.length);
89 return audioOutputLevels[0];