Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698