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

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

Issue 2362703002: Adding audio network adaptor to AudioEncoderOpus. (Closed)
Patch Set: repolishing Created 4 years, 2 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
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
11 #include <memory> 11 #include <memory>
12 12
13 #include "webrtc/base/checks.h" 13 #include "webrtc/base/checks.h"
14 #include "webrtc/common_types.h" 14 #include "webrtc/common_types.h"
15 #include "webrtc/modules/audio_coding/audio_network_adaptor/mock/mock_audio_netw ork_adaptor.h"
15 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" 16 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h"
16 #include "webrtc/test/gtest.h" 17 #include "webrtc/test/gtest.h"
17 18
18 namespace webrtc { 19 namespace webrtc {
20 using ::testing::NiceMock;
21 using ::testing::Return;
19 22
20 namespace { 23 namespace {
21 const CodecInst kOpusSettings = {105, "opus", 48000, 960, 1, 32000}; 24
25 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000};
26
27 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) {
28 AudioEncoderOpus::Config config;
29 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48);
30 config.num_channels = codec_inst.channels;
31 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate);
32 config.payload_type = codec_inst.pltype;
33 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip
34 : AudioEncoderOpus::kAudio;
35 return config;
36 }
37
38 struct AudioEncoderOpusStates {
39 MockAudioNetworkAdaptor* mock_audio_network_adaptor = nullptr;
40 std::unique_ptr<AudioEncoderOpus> encoder;
41
42 explicit AudioEncoderOpusStates(size_t num_channels) {
43 CodecInst codec_inst = kDefaultOpusSettings;
44 codec_inst.channels = num_channels;
45 auto config = CreateConfig(codec_inst);
46 auto creator = [this](const std::string&, const Clock*) {
47 return this->CreateMockAudioNetworkAdaptor();
48 };
49 encoder.reset(new AudioEncoderOpus(config, creator));
50 }
51
52 std::unique_ptr<AudioNetworkAdaptor> CreateMockAudioNetworkAdaptor() {
53 std::unique_ptr<MockAudioNetworkAdaptor> adaptor(
54 new NiceMock<MockAudioNetworkAdaptor>());
55 EXPECT_CALL(*adaptor, Die());
56 mock_audio_network_adaptor = adaptor.get();
57 return adaptor;
58 }
kwiberg-webrtc 2016/10/04 17:40:37 Much better! However, you could have kept the old
minyue-webrtc 2016/10/04 20:31:03 I prefer keeping it this way. I now can put states
kwiberg-webrtc 2016/10/04 20:47:47 You'd still get precisely that with my suggestion,
59 };
60
61 AudioNetworkAdaptor::EncoderRuntimeConfig CreateEncoderRuntimeConfig() {
62 constexpr int kBitrate = 40000;
63 constexpr int kFrameLength = 60;
64 constexpr bool kEnableFec = true;
65 constexpr bool kEnableDtx = false;
66 constexpr size_t kNumChannels = 1;
67 constexpr float kPacketLossFraction = 0.1f;
68 AudioNetworkAdaptor::EncoderRuntimeConfig config;
69 config.bitrate_bps = rtc::Optional<int>(kBitrate);
70 config.frame_length_ms = rtc::Optional<int>(kFrameLength);
71 config.enable_fec = rtc::Optional<bool>(kEnableFec);
72 config.enable_dtx = rtc::Optional<bool>(kEnableDtx);
73 config.num_channels = rtc::Optional<size_t>(kNumChannels);
74 config.uplink_packet_loss_fraction =
75 rtc::Optional<float>(kPacketLossFraction);
76 return config;
77 }
78
79 void CheckEncoderRuntimeConfig(
80 const AudioEncoderOpus* encoder,
81 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) {
82 EXPECT_EQ(*config.bitrate_bps, encoder->GetTargetBitrate());
83 EXPECT_EQ(*config.frame_length_ms, encoder->next_frame_length_ms());
84 EXPECT_EQ(*config.enable_fec, encoder->fec_enabled());
85 EXPECT_EQ(*config.enable_dtx, encoder->GetDtx());
86 EXPECT_EQ(*config.num_channels, encoder->num_channels_to_encode());
87 }
88
22 } // namespace 89 } // namespace
23 90
24 class AudioEncoderOpusTest : public ::testing::Test { 91 TEST(AudioEncoderOpusTest, DefaultApplicationModeMono) {
25 protected: 92 AudioEncoderOpusStates states(1);
26 void CreateCodec(int num_channels) { 93 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
27 codec_inst_.channels = num_channels;
28 encoder_.reset(new AudioEncoderOpus(codec_inst_));
29 auto expected_app =
30 num_channels == 1 ? AudioEncoderOpus::kVoip : AudioEncoderOpus::kAudio;
31 EXPECT_EQ(expected_app, encoder_->application());
32 }
33
34 CodecInst codec_inst_ = kOpusSettings;
35 std::unique_ptr<AudioEncoderOpus> encoder_;
36 };
37
38 TEST_F(AudioEncoderOpusTest, DefaultApplicationModeMono) {
39 CreateCodec(1);
40 } 94 }
41 95
42 TEST_F(AudioEncoderOpusTest, DefaultApplicationModeStereo) { 96 TEST(AudioEncoderOpusTest, DefaultApplicationModeStereo) {
43 CreateCodec(2); 97 AudioEncoderOpusStates states(2);
98 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
44 } 99 }
45 100
46 TEST_F(AudioEncoderOpusTest, ChangeApplicationMode) { 101 TEST(AudioEncoderOpusTest, ChangeApplicationMode) {
47 CreateCodec(2); 102 AudioEncoderOpusStates states(2);
48 EXPECT_TRUE(encoder_->SetApplication(AudioEncoder::Application::kSpeech)); 103 EXPECT_TRUE(
49 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 104 states.encoder->SetApplication(AudioEncoder::Application::kSpeech));
105 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
50 } 106 }
51 107
52 TEST_F(AudioEncoderOpusTest, ResetWontChangeApplicationMode) { 108 TEST(AudioEncoderOpusTest, ResetWontChangeApplicationMode) {
53 CreateCodec(2); 109 AudioEncoderOpusStates states(2);
54 110
55 // Trigger a reset. 111 // Trigger a reset.
56 encoder_->Reset(); 112 states.encoder->Reset();
57 // Verify that the mode is still kAudio. 113 // Verify that the mode is still kAudio.
58 EXPECT_EQ(AudioEncoderOpus::kAudio, encoder_->application()); 114 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
59 115
60 // Now change to kVoip. 116 // Now change to kVoip.
61 EXPECT_TRUE(encoder_->SetApplication(AudioEncoder::Application::kSpeech)); 117 EXPECT_TRUE(
62 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 118 states.encoder->SetApplication(AudioEncoder::Application::kSpeech));
119 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
63 120
64 // Trigger a reset again. 121 // Trigger a reset again.
65 encoder_->Reset(); 122 states.encoder->Reset();
66 // Verify that the mode is still kVoip. 123 // Verify that the mode is still kVoip.
67 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 124 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
68 } 125 }
69 126
70 TEST_F(AudioEncoderOpusTest, ToggleDtx) { 127 TEST(AudioEncoderOpusTest, ToggleDtx) {
71 CreateCodec(2); 128 AudioEncoderOpusStates states(2);
72 // Enable DTX 129 // Enable DTX
73 EXPECT_TRUE(encoder_->SetDtx(true)); 130 EXPECT_TRUE(states.encoder->SetDtx(true));
74 // Verify that the mode is still kAudio. 131 // Verify that the mode is still kAudio.
75 EXPECT_EQ(AudioEncoderOpus::kAudio, encoder_->application()); 132 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
76 // Turn off DTX. 133 // Turn off DTX.
77 EXPECT_TRUE(encoder_->SetDtx(false)); 134 EXPECT_TRUE(states.encoder->SetDtx(false));
78 } 135 }
79 136
80 TEST_F(AudioEncoderOpusTest, SetBitrate) { 137 TEST(AudioEncoderOpusTest, SetBitrate) {
81 CreateCodec(1); 138 AudioEncoderOpusStates states(1);
82 // Constants are replicated from audio_encoder_opus.cc. 139 // Constants are replicated from audio_states.encoderopus.cc.
83 const int kMinBitrateBps = 500; 140 const int kMinBitrateBps = 500;
84 const int kMaxBitrateBps = 512000; 141 const int kMaxBitrateBps = 512000;
85 // Set a too low bitrate. 142 // Set a too low bitrate.
86 encoder_->SetTargetBitrate(kMinBitrateBps - 1); 143 states.encoder->SetTargetBitrate(kMinBitrateBps - 1);
87 EXPECT_EQ(kMinBitrateBps, encoder_->GetTargetBitrate()); 144 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
88 // Set a too high bitrate. 145 // Set a too high bitrate.
89 encoder_->SetTargetBitrate(kMaxBitrateBps + 1); 146 states.encoder->SetTargetBitrate(kMaxBitrateBps + 1);
90 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate()); 147 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
91 // Set the minimum rate. 148 // Set the minimum rate.
92 encoder_->SetTargetBitrate(kMinBitrateBps); 149 states.encoder->SetTargetBitrate(kMinBitrateBps);
93 EXPECT_EQ(kMinBitrateBps, encoder_->GetTargetBitrate()); 150 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
94 // Set the maximum rate. 151 // Set the maximum rate.
95 encoder_->SetTargetBitrate(kMaxBitrateBps); 152 states.encoder->SetTargetBitrate(kMaxBitrateBps);
96 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate()); 153 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
97 // Set rates from 1000 up to 32000 bps. 154 // Set rates from 1000 up to 32000 bps.
98 for (int rate = 1000; rate <= 32000; rate += 1000) { 155 for (int rate = 1000; rate <= 32000; rate += 1000) {
99 encoder_->SetTargetBitrate(rate); 156 states.encoder->SetTargetBitrate(rate);
100 EXPECT_EQ(rate, encoder_->GetTargetBitrate()); 157 EXPECT_EQ(rate, states.encoder->GetTargetBitrate());
101 } 158 }
102 } 159 }
103 160
104 namespace { 161 namespace {
105 162
106 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1), 163 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1),
107 // ..., b. 164 // ..., b.
108 std::vector<double> IntervalSteps(double a, double b, size_t n) { 165 std::vector<double> IntervalSteps(double a, double b, size_t n) {
109 RTC_DCHECK_GT(n, 1u); 166 RTC_DCHECK_GT(n, 1u);
110 const double step = (b - a) / (n - 1); 167 const double step = (b - a) / (n - 1);
(...skipping 10 matching lines...) Expand all
121 const std::vector<double>& losses, 178 const std::vector<double>& losses,
122 double expected_return) { 179 double expected_return) {
123 for (double loss : losses) { 180 for (double loss : losses) {
124 encoder->SetProjectedPacketLossRate(loss); 181 encoder->SetProjectedPacketLossRate(loss);
125 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate()); 182 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate());
126 } 183 }
127 } 184 }
128 185
129 } // namespace 186 } // namespace
130 187
131 TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) { 188 TEST(AudioEncoderOpusTest, PacketLossRateOptimized) {
132 CreateCodec(1); 189 AudioEncoderOpusStates states(1);
133 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); }; 190 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); };
134 const double eps = 1e-15; 191 const double eps = 1e-15;
135 192
136 // Note that the order of the following calls is critical. 193 // Note that the order of the following calls is critical.
137 194
138 // clang-format off 195 // clang-format off
139 TestSetPacketLossRate(encoder_.get(), I(0.00 , 0.01 - eps), 0.00); 196 TestSetPacketLossRate(states.encoder.get(), I(0.00 , 0.01 - eps), 0.00);
140 TestSetPacketLossRate(encoder_.get(), I(0.01 + eps, 0.06 - eps), 0.01); 197 TestSetPacketLossRate(states.encoder.get(), I(0.01 + eps, 0.06 - eps), 0.01);
141 TestSetPacketLossRate(encoder_.get(), I(0.06 + eps, 0.11 - eps), 0.05); 198 TestSetPacketLossRate(states.encoder.get(), I(0.06 + eps, 0.11 - eps), 0.05);
142 TestSetPacketLossRate(encoder_.get(), I(0.11 + eps, 0.22 - eps), 0.10); 199 TestSetPacketLossRate(states.encoder.get(), I(0.11 + eps, 0.22 - eps), 0.10);
143 TestSetPacketLossRate(encoder_.get(), I(0.22 + eps, 1.00 ), 0.20); 200 TestSetPacketLossRate(states.encoder.get(), I(0.22 + eps, 1.00 ), 0.20);
144 201
145 TestSetPacketLossRate(encoder_.get(), I(1.00 , 0.18 + eps), 0.20); 202 TestSetPacketLossRate(states.encoder.get(), I(1.00 , 0.18 + eps), 0.20);
146 TestSetPacketLossRate(encoder_.get(), I(0.18 - eps, 0.09 + eps), 0.10); 203 TestSetPacketLossRate(states.encoder.get(), I(0.18 - eps, 0.09 + eps), 0.10);
147 TestSetPacketLossRate(encoder_.get(), I(0.09 - eps, 0.04 + eps), 0.05); 204 TestSetPacketLossRate(states.encoder.get(), I(0.09 - eps, 0.04 + eps), 0.05);
148 TestSetPacketLossRate(encoder_.get(), I(0.04 - eps, 0.01 + eps), 0.01); 205 TestSetPacketLossRate(states.encoder.get(), I(0.04 - eps, 0.01 + eps), 0.01);
149 TestSetPacketLossRate(encoder_.get(), I(0.01 - eps, 0.00 ), 0.00); 206 TestSetPacketLossRate(states.encoder.get(), I(0.01 - eps, 0.00 ), 0.00);
150 // clang-format on 207 // clang-format on
151 } 208 }
152 209
210 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetUplinkBandwidth) {
211 AudioEncoderOpusStates states(2);
212 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
213
214 auto config = CreateEncoderRuntimeConfig();
215 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
216 .WillOnce(Return(config));
217
218 // Since using mock audio network adaptor, any bandwidth value is fine.
219 constexpr int kUplinkBandwidth = 50000;
220 EXPECT_CALL(*states.mock_audio_network_adaptor,
221 SetUplinkBandwidth(kUplinkBandwidth));
222 states.encoder->OnReceivedUplinkBandwidth(kUplinkBandwidth);
223
224 CheckEncoderRuntimeConfig(states.encoder.get(), config);
225 }
226
227 TEST(AudioEncoderOpusTest,
228 InvokeAudioNetworkAdaptorOnSetUplinkPacketLossFraction) {
229 AudioEncoderOpusStates states(2);
230 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
231
232 auto config = CreateEncoderRuntimeConfig();
233 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
234 .WillOnce(Return(config));
235
236 // Since using mock audio network adaptor, any packet loss fraction is fine.
237 constexpr float kUplinkPacketLoss = 0.1f;
238 EXPECT_CALL(*states.mock_audio_network_adaptor,
239 SetUplinkPacketLossFraction(kUplinkPacketLoss));
240 states.encoder->OnReceivedUplinkPacketLossFraction(kUplinkPacketLoss);
241
242 CheckEncoderRuntimeConfig(states.encoder.get(), config);
243 }
244
245 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetTargetAudioBitrate) {
246 AudioEncoderOpusStates states(2);
247 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
248
249 auto config = CreateEncoderRuntimeConfig();
250 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
251 .WillOnce(Return(config));
252
253 // Since using mock audio network adaptor, any target audio bitrate is fine.
254 constexpr int kTargetAudioBitrate = 30000;
255 EXPECT_CALL(*states.mock_audio_network_adaptor,
256 SetTargetAudioBitrate(kTargetAudioBitrate));
257 states.encoder->OnReceivedTargetAudioBitrate(kTargetAudioBitrate);
258
259 CheckEncoderRuntimeConfig(states.encoder.get(), config);
260 }
261
262 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetRtt) {
263 AudioEncoderOpusStates states(2);
264 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
265
266 auto config = CreateEncoderRuntimeConfig();
267 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
268 .WillOnce(Return(config));
269
270 // Since using mock audio network adaptor, any rtt is fine.
271 constexpr int kRtt = 30;
272 EXPECT_CALL(*states.mock_audio_network_adaptor, SetRtt(kRtt));
273 states.encoder->OnReceivedRtt(kRtt);
274
275 CheckEncoderRuntimeConfig(states.encoder.get(), config);
276 }
277
278 TEST(AudioEncoderOpusTest,
279 InvokeAudioNetworkAdaptorOnSetReceiverFrameLengthRange) {
280 AudioEncoderOpusStates states(2);
281 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
282
283 auto config = CreateEncoderRuntimeConfig();
284
285 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
286 .WillOnce(Return(config));
287
288 constexpr int kMinFrameLength = 10;
289 constexpr int kMaxFrameLength = 60;
290 EXPECT_CALL(*states.mock_audio_network_adaptor,
291 SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength));
292 states.encoder->SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength);
293
294 CheckEncoderRuntimeConfig(states.encoder.get(), config);
295 }
296
153 } // namespace webrtc 297 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc ('k') | webrtc/modules/audio_coding/codecs/opus/opus.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698