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

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

Issue 1316673010: AudioEncoderOpusTest.PacketLossRateOptimized: Fix bug and make prettier (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@opus-test
Patch Set: looks better Created 5 years, 3 months 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate()); 90 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate());
91 // Set rates from 1000 up to 32000 bps. 91 // Set rates from 1000 up to 32000 bps.
92 for (int rate = 1000; rate <= 32000; rate += 1000) { 92 for (int rate = 1000; rate <= 32000; rate += 1000) {
93 encoder_->SetTargetBitrate(rate); 93 encoder_->SetTargetBitrate(rate);
94 EXPECT_EQ(rate, encoder_->GetTargetBitrate()); 94 EXPECT_EQ(rate, encoder_->GetTargetBitrate());
95 } 95 }
96 } 96 }
97 97
98 namespace { 98 namespace {
99 99
100 // These constants correspond to those used in 100 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1),
101 // AudioEncoderOpus::SetProjectedPacketLossRate. 101 // ..., b.
102 const double kPacketLossRate20 = 0.20; 102 std::vector<double> IntervalSteps(double a, double b, size_t n) {
103 const double kPacketLossRate10 = 0.10; 103 DCHECK_GT(n, 1u);
104 const double kPacketLossRate5 = 0.05; 104 const double step = (b - a) / (n - 1);
105 const double kPacketLossRate1 = 0.01; 105 std::vector<double> points;
106 const double kLossRate20Margin = 0.02; 106 for (size_t i = 0; i < n; ++i)
107 const double kLossRate10Margin = 0.01; 107 points.push_back(a + i * step);
108 const double kLossRate5Margin = 0.01; 108 return points;
109 }
109 110
110 // Repeatedly sets packet loss rates in the range [from, to], increasing by 111 // Sets the packet loss rate to each number in the vector in turn, and verifies
111 // 0.01 in each step. The function verifies that the actual loss rate is 112 // that the loss rate as reported by the encoder is |expected_return| for all
112 // |expected_return|. 113 // of them.
113 void TestSetPacketLossRate(AudioEncoderOpus* encoder, 114 void TestSetPacketLossRate(AudioEncoderOpus* encoder,
114 double from, 115 const std::vector<double>& losses,
115 double to,
116 double expected_return) { 116 double expected_return) {
117 for (double loss = from; loss <= to; 117 for (double loss : losses) {
118 (to >= from) ? loss += 0.01 : loss -= 0.01) {
119 encoder->SetProjectedPacketLossRate(loss); 118 encoder->SetProjectedPacketLossRate(loss);
120 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate()); 119 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate());
121 } 120 }
122 } 121 }
123 122
124 } // namespace 123 } // namespace
125 124
126 TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) { 125 TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) {
127 CreateCodec(1); 126 CreateCodec(1);
127 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); };
128 const double eps = 1e-15;
128 129
129 // Note that the order of the following calls is critical. 130 // Note that the order of the following calls is critical.
130 TestSetPacketLossRate(encoder_.get(), 0.0, 0.0, 0.0); 131
131 TestSetPacketLossRate(encoder_.get(), kPacketLossRate1, 132 // clang-format off
132 kPacketLossRate5 + kLossRate5Margin - 0.01, 133 TestSetPacketLossRate(encoder_.get(), I(0.00 , 0.01 - eps), 0.00);
133 kPacketLossRate1); 134 TestSetPacketLossRate(encoder_.get(), I(0.01 + eps, 0.06 - eps), 0.01);
134 TestSetPacketLossRate(encoder_.get(), kPacketLossRate5 + kLossRate5Margin, 135 TestSetPacketLossRate(encoder_.get(), I(0.06 + eps, 0.11 - eps), 0.05);
135 kPacketLossRate10 + kLossRate10Margin - 0.01, 136 TestSetPacketLossRate(encoder_.get(), I(0.11 + eps, 0.22 - eps), 0.10);
136 kPacketLossRate5); 137 TestSetPacketLossRate(encoder_.get(), I(0.22 + eps, 1.00 ), 0.20);
137 TestSetPacketLossRate(encoder_.get(), kPacketLossRate10 + kLossRate10Margin, 138
138 kPacketLossRate20 + kLossRate20Margin - 0.01, 139 TestSetPacketLossRate(encoder_.get(), I(1.00 , 0.18 + eps), 0.20);
139 kPacketLossRate10); 140 TestSetPacketLossRate(encoder_.get(), I(0.18 - eps, 0.09 + eps), 0.10);
140 TestSetPacketLossRate(encoder_.get(), kPacketLossRate20 + kLossRate20Margin, 141 TestSetPacketLossRate(encoder_.get(), I(0.09 - eps, 0.04 + eps), 0.05);
141 1.0, kPacketLossRate20); 142 TestSetPacketLossRate(encoder_.get(), I(0.04 - eps, 0.01 + eps), 0.01);
142 TestSetPacketLossRate(encoder_.get(), kPacketLossRate20 + kLossRate20Margin, 143 TestSetPacketLossRate(encoder_.get(), I(0.01 - eps, 0.00 ), 0.00);
143 kPacketLossRate20 - kLossRate20Margin, 144 // clang-format on
144 kPacketLossRate20);
145 TestSetPacketLossRate(
146 encoder_.get(), kPacketLossRate20 - kLossRate20Margin - 0.01,
147 kPacketLossRate10 - kLossRate10Margin, kPacketLossRate10);
148 TestSetPacketLossRate(encoder_.get(),
149 kPacketLossRate10 - kLossRate10Margin - 0.01,
150 kPacketLossRate5 - kLossRate5Margin, kPacketLossRate5);
151 TestSetPacketLossRate(encoder_.get(),
152 kPacketLossRate5 - kLossRate5Margin - 0.01,
153 kPacketLossRate1, kPacketLossRate1);
154 TestSetPacketLossRate(encoder_.get(), 0.0, 0.0, 0.0);
155 } 145 }
156 146
157 } // namespace webrtc 147 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698