| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 TEST(AudioEncoderOpusTest, ToggleDtx) { | 144 TEST(AudioEncoderOpusTest, ToggleDtx) { |
| 145 auto states = CreateCodec(2); | 145 auto states = CreateCodec(2); |
| 146 // Enable DTX | 146 // Enable DTX |
| 147 EXPECT_TRUE(states.encoder->SetDtx(true)); | 147 EXPECT_TRUE(states.encoder->SetDtx(true)); |
| 148 // Verify that the mode is still kAudio. | 148 // Verify that the mode is still kAudio. |
| 149 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); | 149 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); |
| 150 // Turn off DTX. | 150 // Turn off DTX. |
| 151 EXPECT_TRUE(states.encoder->SetDtx(false)); | 151 EXPECT_TRUE(states.encoder->SetDtx(false)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 TEST(AudioEncoderOpusTest, | 154 TEST(AudioEncoderOpusTest, SetBitrate) { |
| 155 OnReceivedTargetAudioBitrateWithoutAudioNetworkAdaptor) { | |
| 156 auto states = CreateCodec(1); | 155 auto states = CreateCodec(1); |
| 157 // Constants are replicated from audio_states.encoderopus.cc. | 156 // Constants are replicated from audio_states.encoderopus.cc. |
| 158 const int kMinBitrateBps = 500; | 157 const int kMinBitrateBps = 500; |
| 159 const int kMaxBitrateBps = 512000; | 158 const int kMaxBitrateBps = 512000; |
| 160 // Set a too low bitrate. | 159 // Set a too low bitrate. |
| 161 states.encoder->OnReceivedTargetAudioBitrate(kMinBitrateBps - 1); | 160 states.encoder->SetTargetBitrate(kMinBitrateBps - 1); |
| 162 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); | 161 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); |
| 163 // Set a too high bitrate. | 162 // Set a too high bitrate. |
| 164 states.encoder->OnReceivedTargetAudioBitrate(kMaxBitrateBps + 1); | 163 states.encoder->SetTargetBitrate(kMaxBitrateBps + 1); |
| 165 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); | 164 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); |
| 166 // Set the minimum rate. | 165 // Set the minimum rate. |
| 167 states.encoder->OnReceivedTargetAudioBitrate(kMinBitrateBps); | 166 states.encoder->SetTargetBitrate(kMinBitrateBps); |
| 168 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); | 167 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); |
| 169 // Set the maximum rate. | 168 // Set the maximum rate. |
| 170 states.encoder->OnReceivedTargetAudioBitrate(kMaxBitrateBps); | 169 states.encoder->SetTargetBitrate(kMaxBitrateBps); |
| 171 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); | 170 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); |
| 172 // Set rates from 1000 up to 32000 bps. | 171 // Set rates from 1000 up to 32000 bps. |
| 173 for (int rate = 1000; rate <= 32000; rate += 1000) { | 172 for (int rate = 1000; rate <= 32000; rate += 1000) { |
| 174 states.encoder->OnReceivedTargetAudioBitrate(rate); | 173 states.encoder->SetTargetBitrate(rate); |
| 175 EXPECT_EQ(rate, states.encoder->GetTargetBitrate()); | 174 EXPECT_EQ(rate, states.encoder->GetTargetBitrate()); |
| 176 } | 175 } |
| 177 } | 176 } |
| 178 | 177 |
| 179 namespace { | 178 namespace { |
| 180 | 179 |
| 181 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1), | 180 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1), |
| 182 // ..., b. | 181 // ..., b. |
| 183 std::vector<float> IntervalSteps(float a, float b, size_t n) { | 182 std::vector<double> IntervalSteps(double a, double b, size_t n) { |
| 184 RTC_DCHECK_GT(n, 1u); | 183 RTC_DCHECK_GT(n, 1); |
| 185 const float step = (b - a) / (n - 1); | 184 const double step = (b - a) / (n - 1); |
| 186 std::vector<float> points; | 185 std::vector<double> points; |
| 187 points.push_back(a); | 186 for (size_t i = 0; i < n; ++i) |
| 188 for (size_t i = 1; i < n - 1; ++i) | |
| 189 points.push_back(a + i * step); | 187 points.push_back(a + i * step); |
| 190 points.push_back(b); | |
| 191 return points; | 188 return points; |
| 192 } | 189 } |
| 193 | 190 |
| 194 // Sets the packet loss rate to each number in the vector in turn, and verifies | 191 // Sets the packet loss rate to each number in the vector in turn, and verifies |
| 195 // that the loss rate as reported by the encoder is |expected_return| for all | 192 // that the loss rate as reported by the encoder is |expected_return| for all |
| 196 // of them. | 193 // of them. |
| 197 void TestSetPacketLossRate(AudioEncoderOpusStates* states, | 194 void TestSetPacketLossRate(AudioEncoderOpus* encoder, |
| 198 const std::vector<float>& losses, | 195 const std::vector<double>& losses, |
| 199 float expected_return) { | 196 double expected_return) { |
| 200 // |kSampleIntervalMs| is chosen to ease the calculation since | 197 for (double loss : losses) { |
| 201 // 0.9999 ^ 184198 = 1e-8. Which minimizes the effect of | 198 encoder->SetProjectedPacketLossRate(loss); |
| 202 // PacketLossFractionSmoother used in AudioEncoderOpus. | 199 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate()); |
| 203 constexpr int64_t kSampleIntervalMs = 184198; | |
| 204 for (float loss : losses) { | |
| 205 states->encoder->OnReceivedUplinkPacketLossFraction(loss); | |
| 206 states->simulated_clock->AdvanceTimeMilliseconds(kSampleIntervalMs); | |
| 207 EXPECT_FLOAT_EQ(expected_return, states->encoder->packet_loss_rate()); | |
| 208 } | 200 } |
| 209 } | 201 } |
| 210 | 202 |
| 211 } // namespace | 203 } // namespace |
| 212 | 204 |
| 213 TEST(AudioEncoderOpusTest, PacketLossRateOptimized) { | 205 TEST(AudioEncoderOpusTest, PacketLossRateOptimized) { |
| 214 auto states = CreateCodec(1); | 206 auto states = CreateCodec(1); |
| 215 auto I = [](float a, float b) { return IntervalSteps(a, b, 10); }; | 207 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); }; |
| 216 constexpr float eps = 1e-8f; | 208 const double eps = 1e-15; |
| 217 | 209 |
| 218 // Note that the order of the following calls is critical. | 210 // Note that the order of the following calls is critical. |
| 219 | 211 |
| 220 // clang-format off | 212 // clang-format off |
| 221 TestSetPacketLossRate(&states, I(0.00f , 0.01f - eps), 0.00f); | 213 TestSetPacketLossRate(states.encoder.get(), I(0.00 , 0.01 - eps), 0.00); |
| 222 TestSetPacketLossRate(&states, I(0.01f + eps, 0.06f - eps), 0.01f); | 214 TestSetPacketLossRate(states.encoder.get(), I(0.01 + eps, 0.06 - eps), 0.01); |
| 223 TestSetPacketLossRate(&states, I(0.06f + eps, 0.11f - eps), 0.05f); | 215 TestSetPacketLossRate(states.encoder.get(), I(0.06 + eps, 0.11 - eps), 0.05); |
| 224 TestSetPacketLossRate(&states, I(0.11f + eps, 0.22f - eps), 0.10f); | 216 TestSetPacketLossRate(states.encoder.get(), I(0.11 + eps, 0.22 - eps), 0.10); |
| 225 TestSetPacketLossRate(&states, I(0.22f + eps, 1.00f ), 0.20f); | 217 TestSetPacketLossRate(states.encoder.get(), I(0.22 + eps, 1.00 ), 0.20); |
| 226 | 218 |
| 227 TestSetPacketLossRate(&states, I(1.00f , 0.18f + eps), 0.20f); | 219 TestSetPacketLossRate(states.encoder.get(), I(1.00 , 0.18 + eps), 0.20); |
| 228 TestSetPacketLossRate(&states, I(0.18f - eps, 0.09f + eps), 0.10f); | 220 TestSetPacketLossRate(states.encoder.get(), I(0.18 - eps, 0.09 + eps), 0.10); |
| 229 TestSetPacketLossRate(&states, I(0.09f - eps, 0.04f + eps), 0.05f); | 221 TestSetPacketLossRate(states.encoder.get(), I(0.09 - eps, 0.04 + eps), 0.05); |
| 230 TestSetPacketLossRate(&states, I(0.04f - eps, 0.01f + eps), 0.01f); | 222 TestSetPacketLossRate(states.encoder.get(), I(0.04 - eps, 0.01 + eps), 0.01); |
| 231 TestSetPacketLossRate(&states, I(0.01f - eps, 0.00f ), 0.00f); | 223 TestSetPacketLossRate(states.encoder.get(), I(0.01 - eps, 0.00 ), 0.00); |
| 232 // clang-format on | 224 // clang-format on |
| 233 } | 225 } |
| 234 | 226 |
| 235 TEST(AudioEncoderOpusTest, SetReceiverFrameLengthRange) { | 227 TEST(AudioEncoderOpusTest, SetReceiverFrameLengthRange) { |
| 236 auto states = CreateCodec(2); | 228 auto states = CreateCodec(2); |
| 237 // Before calling to |SetReceiverFrameLengthRange|, | 229 // Before calling to |SetReceiverFrameLengthRange|, |
| 238 // |supported_frame_lengths_ms| should contain only the frame length being | 230 // |supported_frame_lengths_ms| should contain only the frame length being |
| 239 // used. | 231 // used. |
| 240 using ::testing::ElementsAre; | 232 using ::testing::ElementsAre; |
| 241 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(), | 233 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(), |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 } | 310 } |
| 319 | 311 |
| 320 TEST(AudioEncoderOpusTest, | 312 TEST(AudioEncoderOpusTest, |
| 321 PacketLossFractionSmoothedOnSetUplinkPacketLossFraction) { | 313 PacketLossFractionSmoothedOnSetUplinkPacketLossFraction) { |
| 322 auto states = CreateCodec(2); | 314 auto states = CreateCodec(2); |
| 323 | 315 |
| 324 // The values are carefully chosen so that if no smoothing is made, the test | 316 // The values are carefully chosen so that if no smoothing is made, the test |
| 325 // will fail. | 317 // will fail. |
| 326 constexpr float kPacketLossFraction_1 = 0.02f; | 318 constexpr float kPacketLossFraction_1 = 0.02f; |
| 327 constexpr float kPacketLossFraction_2 = 0.198f; | 319 constexpr float kPacketLossFraction_2 = 0.198f; |
| 328 // |kSecondSampleTimeMs| is chosen to ease the calculation since | 320 // |kSecondSampleTimeMs| is chose to ease the calculation since |
| 329 // 0.9999 ^ 6931 = 0.5. | 321 // 0.9999 ^ 6931 = 0.5. |
| 330 constexpr int64_t kSecondSampleTimeMs = 6931; | 322 constexpr float kSecondSampleTimeMs = 6931; |
| 331 | 323 |
| 332 // First time, no filtering. | 324 // First time, no filtering. |
| 333 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1); | 325 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1); |
| 334 EXPECT_FLOAT_EQ(0.01f, states.encoder->packet_loss_rate()); | 326 EXPECT_DOUBLE_EQ(0.01, states.encoder->packet_loss_rate()); |
| 335 | 327 |
| 336 states.simulated_clock->AdvanceTimeMilliseconds(kSecondSampleTimeMs); | 328 states.simulated_clock->AdvanceTimeMilliseconds(kSecondSampleTimeMs); |
| 337 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2); | 329 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2); |
| 338 | 330 |
| 339 // Now the output of packet loss fraction smoother should be | 331 // Now the output of packet loss fraction smoother should be |
| 340 // (0.02 + 0.198) / 2 = 0.109, which reach the threshold for the optimized | 332 // (0.02 + 0.198) / 2 = 0.109, which reach the threshold for the optimized |
| 341 // packet loss rate to increase to 0.05. If no smoothing has been made, the | 333 // packet loss rate to increase to 0.05. If no smoothing has been made, the |
| 342 // optimized packet loss rate should have been increase to 0.1. | 334 // optimized packet loss rate should have been increase to 0.1. |
| 343 EXPECT_FLOAT_EQ(0.05f, states.encoder->packet_loss_rate()); | 335 EXPECT_DOUBLE_EQ(0.05, states.encoder->packet_loss_rate()); |
| 344 } | 336 } |
| 345 | 337 |
| 346 } // namespace webrtc | 338 } // namespace webrtc |
| OLD | NEW |