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 #include "media/audio/audio_power_monitor.h" | 5 #include "media/audio/audio_power_monitor.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
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" | 9 #include "base/time/time.h" |
13 #include "media/base/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
15 | 12 |
16 namespace media { | 13 namespace media { |
17 | 14 |
18 static const int kSampleRate = 48000; | 15 static const int kSampleRate = 48000; |
19 static const int kFramesPerBuffer = 128; | 16 static const int kFramesPerBuffer = 128; |
20 | 17 |
21 static const int kTimeConstantMillis = 5; | 18 static const int kTimeConstantMillis = 5; |
22 static const int kMeasurementPeriodMillis = 20; | |
23 | 19 |
24 namespace { | 20 namespace { |
25 | 21 |
26 // Container for each parameterized test's data (input and expected results). | 22 // Container for each parameterized test's data (input and expected results). |
27 class TestScenario { | 23 class TestScenario { |
28 public: | 24 public: |
29 TestScenario(const float* data, int num_channels, int num_frames, | 25 TestScenario(const float* data, int num_channels, int num_frames, |
30 float expected_power, bool expected_clipped) | 26 float expected_power, bool expected_clipped) |
31 : expected_power_(expected_power), expected_clipped_(expected_clipped) { | 27 : expected_power_(expected_power), expected_clipped_(expected_clipped) { |
32 CreatePopulatedBuffer(data, num_channels, num_frames); | 28 CreatePopulatedBuffer(data, num_channels, num_frames); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 bool last_clipped_; | 147 bool last_clipped_; |
152 | 148 |
153 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver); | 149 DISALLOW_COPY_AND_ASSIGN(MeasurementObserver); |
154 }; | 150 }; |
155 | 151 |
156 } // namespace | 152 } // namespace |
157 | 153 |
158 class AudioPowerMonitorTest : public ::testing::TestWithParam<TestScenario> { | 154 class AudioPowerMonitorTest : public ::testing::TestWithParam<TestScenario> { |
159 public: | 155 public: |
160 AudioPowerMonitorTest() | 156 AudioPowerMonitorTest() |
161 : power_monitor_( | 157 : power_monitor_(kSampleRate, |
162 kSampleRate, | 158 base::TimeDelta::FromMilliseconds(kTimeConstantMillis)) { |
163 base::TimeDelta::FromMilliseconds(kTimeConstantMillis), | 159 } |
164 base::TimeDelta::FromMilliseconds(kMeasurementPeriodMillis), | |
165 &message_loop_, | |
166 base::Bind(&AudioPowerMonitorTest::OnPowerMeasured, | |
167 base::Unretained(this))) {} | |
168 | 160 |
169 void FeedAndCheckExpectedPowerIsMeasured( | 161 void FeedAndCheckExpectedPowerIsMeasured( |
170 const AudioBus& bus, float power, bool clipped) { | 162 const AudioBus& bus, float power, bool clipped) { |
171 // Feed the AudioPowerMonitor. It should post tasks to |message_loop_|. | 163 // Feed the AudioPowerMonitor, read measurements from it, and record them in |
| 164 // MeasurementObserver. |
172 static const int kNumFeedIters = 100; | 165 static const int kNumFeedIters = 100; |
173 for (int i = 0; i < kNumFeedIters; ++i) | 166 MeasurementObserver observer(power, clipped); |
| 167 for (int i = 0; i < kNumFeedIters; ++i) { |
174 power_monitor_.Scan(bus, bus.frames()); | 168 power_monitor_.Scan(bus, bus.frames()); |
175 | 169 const std::pair<float, bool>& reading = |
176 // Set up an observer and run all the enqueued tasks. | 170 power_monitor_.ReadCurrentPowerAndClip(); |
177 MeasurementObserver observer(power, clipped); | 171 observer.OnPowerMeasured(reading.first, reading.second); |
178 current_observer_ = &observer; | 172 } |
179 message_loop_.RunUntilIdle(); | |
180 current_observer_ = NULL; | |
181 | 173 |
182 // Check that the results recorded by the observer are the same whole-number | 174 // Check that the results recorded by the observer are the same whole-number |
183 // dBFS. | 175 // dBFS. |
184 EXPECT_EQ(static_cast<int>(power), | 176 EXPECT_EQ(static_cast<int>(power), |
185 static_cast<int>(observer.last_power_measurement())); | 177 static_cast<int>(observer.last_power_measurement())); |
186 EXPECT_EQ(clipped, observer.last_clipped()); | 178 EXPECT_EQ(clipped, observer.last_clipped()); |
187 } | 179 } |
188 | 180 |
189 private: | 181 private: |
190 void OnPowerMeasured(float power, bool clipped) { | |
191 CHECK(current_observer_); | |
192 current_observer_->OnPowerMeasured(power, clipped); | |
193 } | |
194 | |
195 base::MessageLoop message_loop_; | |
196 AudioPowerMonitor power_monitor_; | 182 AudioPowerMonitor power_monitor_; |
197 MeasurementObserver* current_observer_; | |
198 | 183 |
199 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest); | 184 DISALLOW_COPY_AND_ASSIGN(AudioPowerMonitorTest); |
200 }; | 185 }; |
201 | 186 |
202 TEST_P(AudioPowerMonitorTest, MeasuresPowerOfSignal) { | 187 TEST_P(AudioPowerMonitorTest, MeasuresPowerOfSignal) { |
203 const TestScenario& scenario = GetParam(); | 188 const TestScenario& scenario = GetParam(); |
204 | 189 |
205 scoped_ptr<AudioBus> zeroed_bus = | 190 scoped_ptr<AudioBus> zeroed_bus = |
206 AudioBus::Create(scenario.data().channels(), scenario.data().frames()); | 191 AudioBus::Create(scenario.data().channels(), scenario.data().frames()); |
207 zeroed_bus->Zero(); | 192 zeroed_bus->Zero(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 TestScenario(kMonoMaxAmplitude2, 1, 2, | 281 TestScenario(kMonoMaxAmplitude2, 1, 2, |
297 AudioPowerMonitor::max_power(), false), | 282 AudioPowerMonitor::max_power(), false), |
298 TestScenario(kMonoHalfMaxAmplitude, 1, 4, -6, false), | 283 TestScenario(kMonoHalfMaxAmplitude, 1, 4, -6, false), |
299 TestScenario(kMonoAmplitudeClipped, 1, 2, | 284 TestScenario(kMonoAmplitudeClipped, 1, 2, |
300 AudioPowerMonitor::max_power(), true), | 285 AudioPowerMonitor::max_power(), true), |
301 TestScenario(kMonoMaxAmplitudeWithClip, 1, 4, | 286 TestScenario(kMonoMaxAmplitudeWithClip, 1, 4, |
302 AudioPowerMonitor::max_power(), true), | 287 AudioPowerMonitor::max_power(), true), |
303 TestScenario(kMonoMaxAmplitudeWithClip2, 1, 4, | 288 TestScenario(kMonoMaxAmplitudeWithClip2, 1, 4, |
304 AudioPowerMonitor::max_power(), true), | 289 AudioPowerMonitor::max_power(), true), |
305 TestScenario(kMonoSilentNoise, 1, 2, | 290 TestScenario(kMonoSilentNoise, 1, 2, |
306 AudioPowerMonitor::zero_power(), true). | 291 AudioPowerMonitor::zero_power(), false). |
307 WithABadSample(std::numeric_limits<float>::infinity()), | 292 WithABadSample(std::numeric_limits<float>::infinity()), |
308 TestScenario(kMonoHalfMaxAmplitude, 1, 4, | 293 TestScenario(kMonoHalfMaxAmplitude, 1, 4, |
309 AudioPowerMonitor::zero_power(), false). | 294 AudioPowerMonitor::zero_power(), false). |
310 WithABadSample(std::numeric_limits<float>::quiet_NaN()), | 295 WithABadSample(std::numeric_limits<float>::quiet_NaN()), |
311 TestScenario(kStereoSilentNoise, 2, 2, -46, false), | 296 TestScenario(kStereoSilentNoise, 2, 2, -46, false), |
312 TestScenario(kStereoMaxAmplitude, 2, 2, | 297 TestScenario(kStereoMaxAmplitude, 2, 2, |
313 AudioPowerMonitor::max_power(), false), | 298 AudioPowerMonitor::max_power(), false), |
314 TestScenario(kRightChannelMaxAmplitude, 2, 4, -3, false), | 299 TestScenario(kRightChannelMaxAmplitude, 2, 4, -3, false), |
315 TestScenario(kLeftChannelHalfMaxAmplitude, 2, 4, -9, false), | 300 TestScenario(kLeftChannelHalfMaxAmplitude, 2, 4, -9, false), |
316 TestScenario(kStereoMixed, 2, 4, -2, false), | 301 TestScenario(kStereoMixed, 2, 4, -2, false), |
317 TestScenario(kStereoMixed2, 2, 8, -3, false))); | 302 TestScenario(kStereoMixed2, 2, 8, -3, false))); |
318 | 303 |
319 } // namespace media | 304 } // namespace media |
OLD | NEW |