OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <cmath> | 11 #include <cmath> |
12 #include <memory> | 12 #include <memory> |
13 | 13 |
14 #include "webrtc/base/fakeclock.h" | |
14 #include "webrtc/common_audio/smoothing_filter.h" | 15 #include "webrtc/common_audio/smoothing_filter.h" |
15 #include "webrtc/test/gtest.h" | 16 #include "webrtc/test/gtest.h" |
16 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 constexpr float kMaxAbsError = 1e-5f; | 22 constexpr float kMaxAbsError = 1e-5f; |
22 constexpr int64_t kClockInitialTime = 123456; | 23 constexpr int64_t kClockInitialTime = 123456; |
23 | 24 |
24 struct SmoothingFilterStates { | 25 struct SmoothingFilterStates { |
25 std::unique_ptr<SimulatedClock> simulated_clock; | 26 std::unique_ptr<rtc::ScopedFakeClock> fake_clock; |
kwiberg-webrtc
2017/04/11 12:18:38
This doesn't look quite right. IIUC the intention
michaelt
2017/04/11 14:11:13
I think you are right that this class was meant to
kwiberg-webrtc
2017/04/11 16:46:57
OK. But could you at least get rid of the unique_p
michaelt
2017/04/12 07:17:57
Done
| |
26 std::unique_ptr<SmoothingFilterImpl> smoothing_filter; | 27 std::unique_ptr<SmoothingFilterImpl> smoothing_filter; |
27 }; | 28 }; |
28 | 29 |
29 SmoothingFilterStates CreateSmoothingFilter(int init_time_ms) { | 30 SmoothingFilterStates CreateSmoothingFilter(int init_time_ms) { |
30 SmoothingFilterStates states; | 31 SmoothingFilterStates states; |
31 states.simulated_clock.reset(new SimulatedClock(kClockInitialTime)); | 32 states.fake_clock.reset(new rtc::ScopedFakeClock()); |
32 states.smoothing_filter.reset( | 33 states.fake_clock->AdvanceTime( |
33 new SmoothingFilterImpl(init_time_ms, states.simulated_clock.get())); | 34 rtc::TimeDelta::FromMilliseconds(kClockInitialTime)); |
35 states.smoothing_filter.reset(new SmoothingFilterImpl(init_time_ms)); | |
34 return states; | 36 return states; |
35 } | 37 } |
36 | 38 |
37 // This function does the following: | 39 // This function does the following: |
38 // 1. Add a sample to filter at current clock, | 40 // 1. Add a sample to filter at current clock, |
39 // 2. Advance the clock by |advance_time_ms|, | 41 // 2. Advance the clock by |advance_time_ms|, |
40 // 3. Get the output of both SmoothingFilter and verify that it equals to an | 42 // 3. Get the output of both SmoothingFilter and verify that it equals to an |
41 // expected value. | 43 // expected value. |
42 void CheckOutput(SmoothingFilterStates* states, | 44 void CheckOutput(SmoothingFilterStates* states, |
43 float sample, | 45 float sample, |
44 int advance_time_ms, | 46 int advance_time_ms, |
45 float expected_ouput) { | 47 float expected_ouput) { |
46 states->smoothing_filter->AddSample(sample); | 48 states->smoothing_filter->AddSample(sample); |
47 states->simulated_clock->AdvanceTimeMilliseconds(advance_time_ms); | 49 states->fake_clock->AdvanceTime( |
50 rtc::TimeDelta::FromMilliseconds(advance_time_ms)); | |
48 auto output = states->smoothing_filter->GetAverage(); | 51 auto output = states->smoothing_filter->GetAverage(); |
49 EXPECT_TRUE(output); | 52 EXPECT_TRUE(output); |
50 EXPECT_NEAR(expected_ouput, *output, kMaxAbsError); | 53 EXPECT_NEAR(expected_ouput, *output, kMaxAbsError); |
51 } | 54 } |
52 | 55 |
53 } // namespace | 56 } // namespace |
54 | 57 |
55 TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { | 58 TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { |
56 constexpr int kInitTimeMs = 100; | 59 constexpr int kInitTimeMs = 100; |
57 auto states = CreateSmoothingFilter(kInitTimeMs); | 60 auto states = CreateSmoothingFilter(kInitTimeMs); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 EXPECT_EQ(rtc::Optional<float>(kFirstSample), | 140 EXPECT_EQ(rtc::Optional<float>(kFirstSample), |
138 states.smoothing_filter->GetAverage()); | 141 states.smoothing_filter->GetAverage()); |
139 } | 142 } |
140 | 143 |
141 TEST(SmoothingFilterTest, CannotChangeTimeConstantDuringInitialization) { | 144 TEST(SmoothingFilterTest, CannotChangeTimeConstantDuringInitialization) { |
142 constexpr int kInitTimeMs = 100; | 145 constexpr int kInitTimeMs = 100; |
143 auto states = CreateSmoothingFilter(kInitTimeMs); | 146 auto states = CreateSmoothingFilter(kInitTimeMs); |
144 states.smoothing_filter->AddSample(0.0); | 147 states.smoothing_filter->AddSample(0.0); |
145 | 148 |
146 // During initialization, |SetTimeConstantMs| does not take effect. | 149 // During initialization, |SetTimeConstantMs| does not take effect. |
147 states.simulated_clock->AdvanceTimeMilliseconds(kInitTimeMs - 1); | 150 states.fake_clock->AdvanceTime( |
151 rtc::TimeDelta::FromMilliseconds(kInitTimeMs - 1)); | |
148 states.smoothing_filter->AddSample(0.0); | 152 states.smoothing_filter->AddSample(0.0); |
149 | 153 |
150 EXPECT_FALSE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); | 154 EXPECT_FALSE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); |
151 EXPECT_NE(exp(-1.0f / (kInitTimeMs * 2)), states.smoothing_filter->alpha()); | 155 EXPECT_NE(exp(-1.0f / (kInitTimeMs * 2)), states.smoothing_filter->alpha()); |
152 | 156 |
153 states.simulated_clock->AdvanceTimeMilliseconds(1); | 157 states.fake_clock->AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); |
154 states.smoothing_filter->AddSample(0.0); | 158 states.smoothing_filter->AddSample(0.0); |
155 // When initialization finishes, the time constant should be come | 159 // When initialization finishes, the time constant should be come |
156 // |kInitTimeConstantMs|. | 160 // |kInitTimeConstantMs|. |
157 EXPECT_FLOAT_EQ(exp(-1.0f / kInitTimeMs), states.smoothing_filter->alpha()); | 161 EXPECT_FLOAT_EQ(exp(-1.0f / kInitTimeMs), states.smoothing_filter->alpha()); |
158 | 162 |
159 // After initialization, |SetTimeConstantMs| takes effect. | 163 // After initialization, |SetTimeConstantMs| takes effect. |
160 EXPECT_TRUE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); | 164 EXPECT_TRUE(states.smoothing_filter->SetTimeConstantMs(kInitTimeMs * 2)); |
161 EXPECT_FLOAT_EQ(exp(-1.0f / (kInitTimeMs * 2)), | 165 EXPECT_FLOAT_EQ(exp(-1.0f / (kInitTimeMs * 2)), |
162 states.smoothing_filter->alpha()); | 166 states.smoothing_filter->alpha()); |
163 } | 167 } |
164 | 168 |
165 } // namespace webrtc | 169 } // namespace webrtc |
OLD | NEW |