Implement value printer to prevent Valgrind errors in AudioPowerMonitor unit tests.
[chromium-blink-merge.git] / media / audio / audio_power_monitor_unittest.cc
blob938837d972986f7d6eca3dc304d4360d548dd979
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 #include "media/audio/audio_power_monitor.h"
7 #include <limits>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
18 static const int kSampleRate = 48000;
19 static const int kFramesPerBuffer = 128;
21 static const int kTimeConstantMillis = 5;
22 static const int kMeasurementPeriodMillis = 20;
24 namespace {
26 // Container for each parameterized test's data (input and expected results).
27 class TestScenario {
28 public:
29 TestScenario(const float* data, int num_channels, int num_frames,
30 float expected_power, bool expected_clipped)
31 : expected_power_(expected_power), expected_clipped_(expected_clipped) {
32 CreatePopulatedBuffer(data, num_channels, num_frames);
35 // Copy constructor and assignment operator for ::testing::Values(...).
36 TestScenario(const TestScenario& other) { *this = other; }
37 TestScenario& operator=(const TestScenario& other) {
38 this->expected_power_ = other.expected_power_;
39 this->expected_clipped_ = other.expected_clipped_;
40 this->bus_ = AudioBus::Create(other.bus_->channels(), other.bus_->frames());
41 other.bus_->CopyTo(this->bus_.get());
42 return *this;
45 // Returns this TestScenario, but with a bad sample value placed in the middle
46 // of channel 0.
47 TestScenario WithABadSample(float bad_value) const {
48 TestScenario result(*this);
49 result.bus_->channel(0)[result.bus_->frames() / 2] = bad_value;
50 return result;
53 const AudioBus& data() const {
54 return *bus_;
57 float expected_power() const {
58 return expected_power_;
61 bool expected_clipped() const {
62 return expected_clipped_;
65 private:
66 // Creates an AudioBus, sized and populated with kFramesPerBuffer frames of
67 // data. The given test |data| is repeated to fill the buffer.
68 void CreatePopulatedBuffer(
69 const float* data, int num_channels, int num_frames) {
70 bus_ = AudioBus::Create(num_channels, kFramesPerBuffer);
71 for (int ch = 0; ch < num_channels; ++ch) {
72 for (int frames = 0; frames < kFramesPerBuffer; frames += num_frames) {
73 const int num_to_copy = std::min(num_frames, kFramesPerBuffer - frames);
74 memcpy(bus_->channel(ch) + frames, data + num_frames * ch,
75 sizeof(float) * num_to_copy);
80 float expected_power_;
81 bool expected_clipped_;
82 scoped_ptr<AudioBus> bus_;
85 // Value printer for TestScenario. Required to prevent Valgrind "access to
86 // uninitialized memory" errors (http://crbug.com/263315).
87 ::std::ostream& operator<<(::std::ostream& os, const TestScenario& ts) {
88 return os << "{" << ts.data().channels() << "-channel signal} --> {"
89 << ts.expected_power() << " dBFS, "
90 << (ts.expected_clipped() ? "clipped" : "not clipped")
91 << "}";
94 // An observer that receives power measurements. Each power measurement should
95 // should make progress towards the goal value.
96 class MeasurementObserver {
97 public:
98 MeasurementObserver(float goal_power_measurement, bool goal_clipped)
99 : goal_power_measurement_(goal_power_measurement),
100 goal_clipped_(goal_clipped), measurement_count_(0),
101 last_power_measurement_(AudioPowerMonitor::zero_power()),
102 last_clipped_(false) {}
104 int measurement_count() const {
105 return measurement_count_;
108 float last_power_measurement() const {
109 return last_power_measurement_;
112 bool last_clipped() const {
113 return last_clipped_;
116 void OnPowerMeasured(float cur_power_measurement, bool clipped) {
117 if (measurement_count_ == 0) {
118 measurements_should_increase_ =
119 (cur_power_measurement < goal_power_measurement_);
120 } else {
121 SCOPED_TRACE(::testing::Message()
122 << "Power: goal=" << goal_power_measurement_
123 << "; last=" << last_power_measurement_
124 << "; cur=" << cur_power_measurement);
126 if (last_power_measurement_ != goal_power_measurement_) {
127 if (measurements_should_increase_) {
128 EXPECT_LE(last_power_measurement_, cur_power_measurement)
129 << "Measurements should be monotonically increasing.";
130 } else {
131 EXPECT_GE(last_power_measurement_, cur_power_measurement)
132 << "Measurements should be monotonically decreasing.";
134 } else {
135 EXPECT_EQ(last_power_measurement_, cur_power_measurement)
136 << "Measurements are numerically unstable at goal value.";
140 last_power_measurement_ = cur_power_measurement;
141 last_clipped_ = clipped;
142 ++measurement_count_;
145 private:
146 const float goal_power_measurement_;
147 const bool goal_clipped_;
148 int measurement_count_;
149 bool measurements_should_increase_;
150 float last_power_measurement_;
151 bool last_clipped_;
153 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver);
156 } // namespace
158 class AudioPowerMonitorTest : public ::testing::TestWithParam<TestScenario> {
159 public:
160 AudioPowerMonitorTest()
161 : power_monitor_(
162 kSampleRate,
163 base::TimeDelta::FromMilliseconds(kTimeConstantMillis),
164 base::TimeDelta::FromMilliseconds(kMeasurementPeriodMillis),
165 &message_loop_,
166 base::Bind(&AudioPowerMonitorTest::OnPowerMeasured,
167 base::Unretained(this))) {}
169 void FeedAndCheckExpectedPowerIsMeasured(
170 const AudioBus& bus, float power, bool clipped) {
171 // Feed the AudioPowerMonitor. It should post tasks to |message_loop_|.
172 static const int kNumFeedIters = 100;
173 for (int i = 0; i < kNumFeedIters; ++i)
174 power_monitor_.Scan(bus, bus.frames());
176 // Set up an observer and run all the enqueued tasks.
177 MeasurementObserver observer(power, clipped);
178 current_observer_ = &observer;
179 message_loop_.RunUntilIdle();
180 current_observer_ = NULL;
182 // Check that the results recorded by the observer are the same whole-number
183 // dBFS.
184 EXPECT_EQ(static_cast<int>(power),
185 static_cast<int>(observer.last_power_measurement()));
186 EXPECT_EQ(clipped, observer.last_clipped());
189 private:
190 void OnPowerMeasured(float power, bool clipped) {
191 CHECK(current_observer_);
192 current_observer_->OnPowerMeasured(power, clipped);
195 base::MessageLoop message_loop_;
196 AudioPowerMonitor power_monitor_;
197 MeasurementObserver* current_observer_;
199 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest);
202 TEST_P(AudioPowerMonitorTest, MeasuresPowerOfSignal) {
203 const TestScenario& scenario = GetParam();
205 scoped_ptr<AudioBus> zeroed_bus =
206 AudioBus::Create(scenario.data().channels(), scenario.data().frames());
207 zeroed_bus->Zero();
209 // Send a "zero power" audio signal, then this scenario's audio signal, then
210 // the "zero power" audio signal again; testing that the power monitor
211 // measurements match expected values.
212 FeedAndCheckExpectedPowerIsMeasured(
213 *zeroed_bus, AudioPowerMonitor::zero_power(), false);
214 FeedAndCheckExpectedPowerIsMeasured(
215 scenario.data(), scenario.expected_power(), scenario.expected_clipped());
216 FeedAndCheckExpectedPowerIsMeasured(
217 *zeroed_bus, AudioPowerMonitor::zero_power(), false);
220 static const float kMonoSilentNoise[] = {
221 0.01f, -0.01f
224 static const float kMonoMaxAmplitude[] = {
225 1.0f
228 static const float kMonoMaxAmplitude2[] = {
229 -1.0f, 1.0f
232 static const float kMonoHalfMaxAmplitude[] = {
233 0.5f, -0.5f, 0.5f, -0.5f
236 static const float kMonoAmplitudeClipped[] = {
237 2.0f, -2.0f
240 static const float kMonoMaxAmplitudeWithClip[] = {
241 2.0f, 0.0, 0.0f, 0.0f
244 static const float kMonoMaxAmplitudeWithClip2[] = {
245 4.0f, 0.0, 0.0f, 0.0f
248 static const float kStereoSilentNoise[] = {
249 // left channel
250 0.005f, -0.005f,
251 // right channel
252 0.005f, -0.005f
255 static const float kStereoMaxAmplitude[] = {
256 // left channel
257 1.0f, -1.0f,
258 // right channel
259 -1.0f, 1.0f
262 static const float kRightChannelMaxAmplitude[] = {
263 // left channel
264 0.0f, 0.0f, 0.0f, 0.0f,
265 // right channel
266 -1.0f, 1.0f, -1.0f, 1.0f
269 static const float kLeftChannelHalfMaxAmplitude[] = {
270 // left channel
271 0.5f, -0.5f, 0.5f, -0.5f,
272 // right channel
273 0.0f, 0.0f, 0.0f, 0.0f,
276 static const float kStereoMixed[] = {
277 // left channel
278 0.5f, -0.5f, 0.5f, -0.5f,
279 // right channel
280 -1.0f, 1.0f, -1.0f, 1.0f
283 static const float kStereoMixed2[] = {
284 // left channel
285 1.0f, -1.0f, 0.75f, -0.75f, 0.5f, -0.5f, 0.25f, -0.25f,
286 // right channel
287 0.25f, -0.25f, 0.5f, -0.5f, 0.75f, -0.75f, 1.0f, -1.0f
290 INSTANTIATE_TEST_CASE_P(
291 Scenarios, AudioPowerMonitorTest,
292 ::testing::Values(
293 TestScenario(kMonoSilentNoise, 1, 2, -40, false),
294 TestScenario(kMonoMaxAmplitude, 1, 1,
295 AudioPowerMonitor::max_power(), false),
296 TestScenario(kMonoMaxAmplitude2, 1, 2,
297 AudioPowerMonitor::max_power(), false),
298 TestScenario(kMonoHalfMaxAmplitude, 1, 4, -6, false),
299 TestScenario(kMonoAmplitudeClipped, 1, 2,
300 AudioPowerMonitor::max_power(), true),
301 TestScenario(kMonoMaxAmplitudeWithClip, 1, 4,
302 AudioPowerMonitor::max_power(), true),
303 TestScenario(kMonoMaxAmplitudeWithClip2, 1, 4,
304 AudioPowerMonitor::max_power(), true),
305 TestScenario(kMonoSilentNoise, 1, 2,
306 AudioPowerMonitor::zero_power(), true).
307 WithABadSample(std::numeric_limits<float>::infinity()),
308 TestScenario(kMonoHalfMaxAmplitude, 1, 4,
309 AudioPowerMonitor::zero_power(), false).
310 WithABadSample(std::numeric_limits<float>::quiet_NaN()),
311 TestScenario(kStereoSilentNoise, 2, 2, -46, false),
312 TestScenario(kStereoMaxAmplitude, 2, 2,
313 AudioPowerMonitor::max_power(), false),
314 TestScenario(kRightChannelMaxAmplitude, 2, 4, -3, false),
315 TestScenario(kLeftChannelHalfMaxAmplitude, 2, 4, -9, false),
316 TestScenario(kStereoMixed, 2, 4, -2, false),
317 TestScenario(kStereoMixed2, 2, 8, -3, false)));
319 } // namespace media