henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Unit tests for DelayManager class. |
| 12 | |
| 13 | #include "webrtc/modules/audio_coding/neteq4/delay_manager.h" |
| 14 | |
| 15 | #include <math.h> |
| 16 | |
| 17 | #include "gmock/gmock.h" |
| 18 | #include "gtest/gtest.h" |
| 19 | #include "webrtc/modules/audio_coding/neteq4/mock/mock_delay_peak_detector.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | using ::testing::Return; |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 24 | using ::testing::_; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 25 | |
| 26 | class DelayManagerTest : public ::testing::Test { |
| 27 | protected: |
| 28 | static const int kMaxNumberOfPackets = 240; |
| 29 | static const int kTimeStepMs = 10; |
| 30 | static const int kFs = 8000; |
| 31 | static const int kFrameSizeMs = 20; |
| 32 | static const int kTsIncrement = kFrameSizeMs * kFs / 1000; |
| 33 | |
| 34 | DelayManagerTest(); |
| 35 | virtual void SetUp(); |
| 36 | virtual void TearDown(); |
| 37 | void SetPacketAudioLength(int lengt_ms); |
| 38 | void InsertNextPacket(); |
| 39 | void IncreaseTime(int inc_ms); |
| 40 | |
| 41 | DelayManager* dm_; |
| 42 | MockDelayPeakDetector detector_; |
| 43 | uint16_t seq_no_; |
| 44 | uint32_t ts_; |
| 45 | }; |
| 46 | |
| 47 | DelayManagerTest::DelayManagerTest() |
| 48 | : dm_(NULL), |
| 49 | seq_no_(0x1234), |
| 50 | ts_(0x12345678) { |
| 51 | } |
| 52 | |
| 53 | void DelayManagerTest::SetUp() { |
| 54 | EXPECT_CALL(detector_, Reset()) |
| 55 | .Times(1); |
| 56 | dm_ = new DelayManager(kMaxNumberOfPackets, &detector_); |
| 57 | } |
| 58 | |
| 59 | void DelayManagerTest::SetPacketAudioLength(int lengt_ms) { |
| 60 | EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms)); |
| 61 | dm_->SetPacketAudioLength(lengt_ms); |
| 62 | } |
| 63 | |
| 64 | void DelayManagerTest::InsertNextPacket() { |
| 65 | EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs)); |
| 66 | seq_no_ += 1; |
| 67 | ts_ += kTsIncrement; |
| 68 | } |
| 69 | |
| 70 | void DelayManagerTest::IncreaseTime(int inc_ms) { |
| 71 | for (int t = 0; t < inc_ms; t += kTimeStepMs) { |
| 72 | EXPECT_CALL(detector_, IncrementCounter(kTimeStepMs)) |
| 73 | .Times(1); |
| 74 | dm_->UpdateCounters(kTimeStepMs); |
| 75 | } |
| 76 | } |
| 77 | void DelayManagerTest::TearDown() { |
| 78 | EXPECT_CALL(detector_, Die()); |
| 79 | delete dm_; |
| 80 | } |
| 81 | |
| 82 | TEST_F(DelayManagerTest, CreateAndDestroy) { |
| 83 | // Nothing to do here. The test fixture creates and destroys the DelayManager |
| 84 | // object. |
| 85 | } |
| 86 | |
| 87 | TEST_F(DelayManagerTest, VectorInitialization) { |
| 88 | const DelayManager::IATVector& vec = dm_->iat_vector(); |
| 89 | double sum = 0.0; |
| 90 | for (size_t i = 0; i < vec.size(); i++) { |
| 91 | EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65536); |
| 92 | // Tolerance 65536 in Q30 corresponds to a delta of approximately 0.00006. |
| 93 | sum += vec[i]; |
| 94 | } |
| 95 | EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30. |
| 96 | } |
| 97 | |
| 98 | TEST_F(DelayManagerTest, SetPacketAudioLength) { |
| 99 | const int kLengthMs = 30; |
| 100 | // Expect DelayManager to pass on the new length to the detector object. |
| 101 | EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)) |
| 102 | .Times(1); |
| 103 | EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs)); |
| 104 | EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value. |
| 105 | } |
| 106 | |
| 107 | TEST_F(DelayManagerTest, PeakFound) { |
| 108 | // Expect DelayManager to pass on the question to the detector. |
| 109 | // Call twice, and let the detector return true the first time and false the |
| 110 | // second time. |
| 111 | EXPECT_CALL(detector_, peak_found()) |
| 112 | .WillOnce(Return(true)) |
| 113 | .WillOnce(Return(false)); |
| 114 | EXPECT_TRUE(dm_->PeakFound()); |
| 115 | EXPECT_FALSE(dm_->PeakFound()); |
| 116 | } |
| 117 | |
| 118 | TEST_F(DelayManagerTest, UpdateCounters) { |
| 119 | // Expect DelayManager to pass on the counter update to the detector. |
| 120 | EXPECT_CALL(detector_, IncrementCounter(kTimeStepMs)) |
| 121 | .Times(1); |
| 122 | dm_->UpdateCounters(kTimeStepMs); |
| 123 | } |
| 124 | |
| 125 | TEST_F(DelayManagerTest, UpdateNormal) { |
| 126 | SetPacketAudioLength(kFrameSizeMs); |
| 127 | // First packet arrival. |
| 128 | InsertNextPacket(); |
| 129 | // Advance time by one frame size. |
| 130 | IncreaseTime(kFrameSizeMs); |
| 131 | // Second packet arrival. |
| 132 | // Expect detector update method to be called once with inter-arrival time |
| 133 | // equal to 1 packet, and (base) target level equal to 1 as well. |
| 134 | // Return false to indicate no peaks found. |
| 135 | EXPECT_CALL(detector_, Update(1, 1)) |
| 136 | .WillOnce(Return(false)); |
| 137 | InsertNextPacket(); |
| 138 | EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8. |
| 139 | EXPECT_EQ(1, dm_->base_target_level()); |
| 140 | int lower, higher; |
| 141 | dm_->BufferLimits(&lower, &higher); |
| 142 | // Expect |lower| to be 75% of target level, and |higher| to be target level, |
| 143 | // but also at least 20 ms higher than |lower|, which is the limiting case |
| 144 | // here. |
| 145 | EXPECT_EQ((1 << 8) * 3 / 4, lower); |
| 146 | EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher); |
| 147 | } |
| 148 | |
| 149 | TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) { |
| 150 | SetPacketAudioLength(kFrameSizeMs); |
| 151 | // First packet arrival. |
| 152 | InsertNextPacket(); |
| 153 | // Advance time by two frame size. |
| 154 | IncreaseTime(2 * kFrameSizeMs); |
| 155 | // Second packet arrival. |
| 156 | // Expect detector update method to be called once with inter-arrival time |
| 157 | // equal to 1 packet, and (base) target level equal to 1 as well. |
| 158 | // Return false to indicate no peaks found. |
| 159 | EXPECT_CALL(detector_, Update(2, 2)) |
| 160 | .WillOnce(Return(false)); |
| 161 | InsertNextPacket(); |
| 162 | EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8. |
| 163 | EXPECT_EQ(2, dm_->base_target_level()); |
| 164 | int lower, higher; |
| 165 | dm_->BufferLimits(&lower, &higher); |
| 166 | // Expect |lower| to be 75% of target level, and |higher| to be target level, |
| 167 | // but also at least 20 ms higher than |lower|, which is the limiting case |
| 168 | // here. |
| 169 | EXPECT_EQ((2 << 8) * 3 / 4, lower); |
| 170 | EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher); |
| 171 | } |
| 172 | |
| 173 | TEST_F(DelayManagerTest, UpdatePeakFound) { |
| 174 | SetPacketAudioLength(kFrameSizeMs); |
| 175 | // First packet arrival. |
| 176 | InsertNextPacket(); |
| 177 | // Advance time by one frame size. |
| 178 | IncreaseTime(kFrameSizeMs); |
| 179 | // Second packet arrival. |
| 180 | // Expect detector update method to be called once with inter-arrival time |
| 181 | // equal to 1 packet, and (base) target level equal to 1 as well. |
| 182 | // Return true to indicate that peaks are found. Let the peak height be 5. |
| 183 | EXPECT_CALL(detector_, Update(1, 1)) |
| 184 | .WillOnce(Return(true)); |
| 185 | EXPECT_CALL(detector_, MaxPeakHeight()) |
| 186 | .WillOnce(Return(5)); |
| 187 | InsertNextPacket(); |
| 188 | EXPECT_EQ(5 << 8, dm_->TargetLevel()); |
| 189 | EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks. |
| 190 | int lower, higher; |
| 191 | dm_->BufferLimits(&lower, &higher); |
| 192 | // Expect |lower| to be 75% of target level, and |higher| to be target level. |
| 193 | EXPECT_EQ((5 << 8) * 3 / 4, lower); |
| 194 | EXPECT_EQ(5 << 8, higher); |
| 195 | } |
| 196 | |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 197 | TEST_F(DelayManagerTest, TargetDelay) { |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 198 | SetPacketAudioLength(kFrameSizeMs); |
| 199 | // First packet arrival. |
| 200 | InsertNextPacket(); |
| 201 | // Advance time by one frame size. |
| 202 | IncreaseTime(kFrameSizeMs); |
| 203 | // Second packet arrival. |
| 204 | // Expect detector update method to be called once with inter-arrival time |
| 205 | // equal to 1 packet, and (base) target level equal to 1 as well. |
| 206 | // Return false to indicate no peaks found. |
| 207 | EXPECT_CALL(detector_, Update(1, 1)) |
| 208 | .WillOnce(Return(false)); |
| 209 | InsertNextPacket(); |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 210 | const int kExpectedTarget = 1; |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 211 | EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8. |
| 212 | EXPECT_EQ(1, dm_->base_target_level()); |
| 213 | int lower, higher; |
| 214 | dm_->BufferLimits(&lower, &higher); |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 215 | // Expect |lower| to be 75% of base target level, and |higher| to be |
| 216 | // lower + 20 ms headroom. |
| 217 | EXPECT_EQ((1 << 8) * 3 / 4, lower); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 218 | EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher); |
| 219 | } |
| 220 | |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 221 | TEST_F(DelayManagerTest, MaxAndRequiredDelay) { |
| 222 | const int kExpectedTarget = 5; |
| 223 | const int kTimeIncrement = kExpectedTarget * kFrameSizeMs; |
| 224 | SetPacketAudioLength(kFrameSizeMs); |
| 225 | // First packet arrival. |
| 226 | InsertNextPacket(); |
| 227 | // Second packet arrival. |
| 228 | // Expect detector update method to be called once with inter-arrival time |
| 229 | // equal to |kExpectedTarget| packet. Return true to indicate peaks found. |
| 230 | EXPECT_CALL(detector_, Update(kExpectedTarget, _)) |
| 231 | .WillRepeatedly(Return(true)); |
| 232 | EXPECT_CALL(detector_, MaxPeakHeight()) |
| 233 | .WillRepeatedly(Return(kExpectedTarget)); |
| 234 | IncreaseTime(kTimeIncrement); |
| 235 | InsertNextPacket(); |
| 236 | |
| 237 | // No limit is set. |
| 238 | EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); |
| 239 | |
| 240 | int kMaxDelayPackets = kExpectedTarget - 2; |
| 241 | int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs; |
| 242 | EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs)); |
| 243 | IncreaseTime(kTimeIncrement); |
| 244 | InsertNextPacket(); |
| 245 | EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms()); |
| 246 | EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel()); |
| 247 | |
| 248 | // Target level at least should be one packet. |
| 249 | EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1)); |
| 250 | } |
| 251 | |
| 252 | TEST_F(DelayManagerTest, MinAndRequiredDelay) { |
| 253 | const int kExpectedTarget = 5; |
| 254 | const int kTimeIncrement = kExpectedTarget * kFrameSizeMs; |
| 255 | SetPacketAudioLength(kFrameSizeMs); |
| 256 | // First packet arrival. |
| 257 | InsertNextPacket(); |
| 258 | // Second packet arrival. |
| 259 | // Expect detector update method to be called once with inter-arrival time |
| 260 | // equal to |kExpectedTarget| packet. Return true to indicate peaks found. |
| 261 | EXPECT_CALL(detector_, Update(kExpectedTarget, _)) |
| 262 | .WillRepeatedly(Return(true)); |
| 263 | EXPECT_CALL(detector_, MaxPeakHeight()) |
| 264 | .WillRepeatedly(Return(kExpectedTarget)); |
| 265 | IncreaseTime(kTimeIncrement); |
| 266 | InsertNextPacket(); |
| 267 | |
| 268 | // No limit is applied. |
| 269 | EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); |
| 270 | |
| 271 | int kMinDelayPackets = kExpectedTarget + 2; |
| 272 | int kMinDelayMs = kMinDelayPackets * kFrameSizeMs; |
| 273 | dm_->SetMinimumDelay(kMinDelayMs); |
| 274 | IncreaseTime(kTimeIncrement); |
| 275 | InsertNextPacket(); |
| 276 | EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms()); |
| 277 | EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel()); |
| 278 | } |
| 279 | |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 280 | TEST_F(DelayManagerTest, Failures) { |
| 281 | // Wrong sample rate. |
| 282 | EXPECT_EQ(-1, dm_->Update(0, 0, -1)); |
| 283 | // Wrong packet size. |
| 284 | EXPECT_EQ(-1, dm_->SetPacketAudioLength(0)); |
| 285 | EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); |
turaj@webrtc.org | 662ded4 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 286 | |
| 287 | // Minimum delay higher than a maximum delay is not accepted. |
| 288 | EXPECT_TRUE(dm_->SetMaximumDelay(10)); |
| 289 | EXPECT_FALSE(dm_->SetMinimumDelay(20)); |
| 290 | |
| 291 | // Maximum delay less than minimum delay is not accepted. |
| 292 | EXPECT_TRUE(dm_->SetMaximumDelay(100)); |
| 293 | EXPECT_TRUE(dm_->SetMinimumDelay(80)); |
| 294 | EXPECT_FALSE(dm_->SetMaximumDelay(60)); |
henrik.lundin@webrtc.org | 9a40081 | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | } // namespace webrtc |