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

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

Issue 2429503002: Simplifying audio network adaptor by moving receiver frame length range to ctor. (Closed)
Patch Set: nicer solution Created 4 years, 1 month 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 | « webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc ('k') | 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
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/audio_network_adaptor/mock/mock_audio_netw ork_adaptor.h"
16 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" 16 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h"
17 #include "webrtc/test/gmock.h"
17 #include "webrtc/test/gtest.h" 18 #include "webrtc/test/gtest.h"
18 #include "webrtc/system_wrappers/include/clock.h" 19 #include "webrtc/system_wrappers/include/clock.h"
19 20
20 namespace webrtc { 21 namespace webrtc {
21 using ::testing::NiceMock; 22 using ::testing::NiceMock;
22 using ::testing::Return; 23 using ::testing::Return;
23 24
24 namespace { 25 namespace {
25 26
26 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000}; 27 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000};
27 constexpr int64_t kInitialTimeUs = 12345678; 28 constexpr int64_t kInitialTimeUs = 12345678;
28 29
29 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { 30 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) {
30 AudioEncoderOpus::Config config; 31 AudioEncoderOpus::Config config;
31 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); 32 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48);
32 config.num_channels = codec_inst.channels; 33 config.num_channels = codec_inst.channels;
33 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); 34 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate);
34 config.payload_type = codec_inst.pltype; 35 config.payload_type = codec_inst.pltype;
35 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip 36 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip
36 : AudioEncoderOpus::kAudio; 37 : AudioEncoderOpus::kAudio;
38 config.supported_frame_lengths_ms.push_back(config.frame_size_ms);
37 return config; 39 return config;
38 } 40 }
39 41
40 struct AudioEncoderOpusStates { 42 struct AudioEncoderOpusStates {
41 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor; 43 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor;
42 std::unique_ptr<AudioEncoderOpus> encoder; 44 std::unique_ptr<AudioEncoderOpus> encoder;
43 std::unique_ptr<SimulatedClock> simulated_clock; 45 std::unique_ptr<SimulatedClock> simulated_clock;
44 }; 46 };
45 47
46 AudioEncoderOpusStates CreateCodec(size_t num_channels) { 48 AudioEncoderOpusStates CreateCodec(size_t num_channels) {
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 TestSetPacketLossRate(states.encoder.get(), I(0.22 + eps, 1.00 ), 0.20); 217 TestSetPacketLossRate(states.encoder.get(), I(0.22 + eps, 1.00 ), 0.20);
216 218
217 TestSetPacketLossRate(states.encoder.get(), I(1.00 , 0.18 + eps), 0.20); 219 TestSetPacketLossRate(states.encoder.get(), I(1.00 , 0.18 + eps), 0.20);
218 TestSetPacketLossRate(states.encoder.get(), I(0.18 - eps, 0.09 + eps), 0.10); 220 TestSetPacketLossRate(states.encoder.get(), I(0.18 - eps, 0.09 + eps), 0.10);
219 TestSetPacketLossRate(states.encoder.get(), I(0.09 - eps, 0.04 + eps), 0.05); 221 TestSetPacketLossRate(states.encoder.get(), I(0.09 - eps, 0.04 + eps), 0.05);
220 TestSetPacketLossRate(states.encoder.get(), I(0.04 - eps, 0.01 + eps), 0.01); 222 TestSetPacketLossRate(states.encoder.get(), I(0.04 - eps, 0.01 + eps), 0.01);
221 TestSetPacketLossRate(states.encoder.get(), I(0.01 - eps, 0.00 ), 0.00); 223 TestSetPacketLossRate(states.encoder.get(), I(0.01 - eps, 0.00 ), 0.00);
222 // clang-format on 224 // clang-format on
223 } 225 }
224 226
227 TEST(AudioEncoderOpusTest, SetReceiverFrameLengthRange) {
228 auto states = CreateCodec(2);
229 // Before calling to |SetReceiverFrameLengthRange|,
230 // |supported_frame_lengths_ms| should contain only the frame length being
231 // used.
232 using ::testing::ElementsAre;
233 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(),
234 ElementsAre(states.encoder->next_frame_length_ms()));
235 states.encoder->SetReceiverFrameLengthRange(0, 12345);
236 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(),
237 ElementsAre(20, 60));
238 states.encoder->SetReceiverFrameLengthRange(21, 60);
239 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(), ElementsAre(60));
240 states.encoder->SetReceiverFrameLengthRange(20, 59);
241 EXPECT_THAT(states.encoder->supported_frame_lengths_ms(), ElementsAre(20));
242 }
243
225 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetUplinkBandwidth) { 244 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetUplinkBandwidth) {
226 auto states = CreateCodec(2); 245 auto states = CreateCodec(2);
227 printf("passed!\n");
228 states.encoder->EnableAudioNetworkAdaptor("", nullptr); 246 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
229 247
230 auto config = CreateEncoderRuntimeConfig(); 248 auto config = CreateEncoderRuntimeConfig();
231 EXPECT_CALL(**states.mock_audio_network_adaptor, GetEncoderRuntimeConfig()) 249 EXPECT_CALL(**states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
232 .WillOnce(Return(config)); 250 .WillOnce(Return(config));
233 251
234 // Since using mock audio network adaptor, any bandwidth value is fine. 252 // Since using mock audio network adaptor, any bandwidth value is fine.
235 constexpr int kUplinkBandwidth = 50000; 253 constexpr int kUplinkBandwidth = 50000;
236 EXPECT_CALL(**states.mock_audio_network_adaptor, 254 EXPECT_CALL(**states.mock_audio_network_adaptor,
237 SetUplinkBandwidth(kUplinkBandwidth)); 255 SetUplinkBandwidth(kUplinkBandwidth));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 303
286 // Since using mock audio network adaptor, any rtt is fine. 304 // Since using mock audio network adaptor, any rtt is fine.
287 constexpr int kRtt = 30; 305 constexpr int kRtt = 30;
288 EXPECT_CALL(**states.mock_audio_network_adaptor, SetRtt(kRtt)); 306 EXPECT_CALL(**states.mock_audio_network_adaptor, SetRtt(kRtt));
289 states.encoder->OnReceivedRtt(kRtt); 307 states.encoder->OnReceivedRtt(kRtt);
290 308
291 CheckEncoderRuntimeConfig(states.encoder.get(), config); 309 CheckEncoderRuntimeConfig(states.encoder.get(), config);
292 } 310 }
293 311
294 TEST(AudioEncoderOpusTest, 312 TEST(AudioEncoderOpusTest,
295 InvokeAudioNetworkAdaptorOnSetReceiverFrameLengthRange) {
296 auto states = CreateCodec(2);
297 states.encoder->EnableAudioNetworkAdaptor("", nullptr);
298
299 auto config = CreateEncoderRuntimeConfig();
300 EXPECT_CALL(**states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
301 .WillOnce(Return(config));
302
303 constexpr int kMinFrameLength = 10;
304 constexpr int kMaxFrameLength = 60;
305 EXPECT_CALL(**states.mock_audio_network_adaptor,
306 SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength));
307 states.encoder->SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength);
308
309 CheckEncoderRuntimeConfig(states.encoder.get(), config);
310 }
311
312 TEST(AudioEncoderOpusTest,
313 PacketLossFractionSmoothedOnSetUplinkPacketLossFraction) { 313 PacketLossFractionSmoothedOnSetUplinkPacketLossFraction) {
314 auto states = CreateCodec(2); 314 auto states = CreateCodec(2);
315 315
316 // 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
317 // will fail. 317 // will fail.
318 constexpr float kPacketLossFraction_1 = 0.02f; 318 constexpr float kPacketLossFraction_1 = 0.02f;
319 constexpr float kPacketLossFraction_2 = 0.198f; 319 constexpr float kPacketLossFraction_2 = 0.198f;
320 // |kSecondSampleTimeMs| is chose to ease the calculation since 320 // |kSecondSampleTimeMs| is chose to ease the calculation since
321 // 0.9999 ^ 6931 = 0.5. 321 // 0.9999 ^ 6931 = 0.5.
322 constexpr float kSecondSampleTimeMs = 6931; 322 constexpr float kSecondSampleTimeMs = 6931;
323 323
324 // First time, no filtering. 324 // First time, no filtering.
325 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1); 325 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1);
326 EXPECT_DOUBLE_EQ(0.01, states.encoder->packet_loss_rate()); 326 EXPECT_DOUBLE_EQ(0.01, states.encoder->packet_loss_rate());
327 327
328 states.simulated_clock->AdvanceTimeMilliseconds(kSecondSampleTimeMs); 328 states.simulated_clock->AdvanceTimeMilliseconds(kSecondSampleTimeMs);
329 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2); 329 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2);
330 330
331 // Now the output of packet loss fraction smoother should be 331 // 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 332 // (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 333 // 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. 334 // optimized packet loss rate should have been increase to 0.1.
335 EXPECT_DOUBLE_EQ(0.05, states.encoder->packet_loss_rate()); 335 EXPECT_DOUBLE_EQ(0.05, states.encoder->packet_loss_rate());
336 } 336 }
337 337
338 } // namespace webrtc 338 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698