| 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 |
| 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/gtest.h" | 17 #include "webrtc/test/gtest.h" |
| 18 #include "webrtc/system_wrappers/include/clock.h" |
| 18 | 19 |
| 19 namespace webrtc { | 20 namespace webrtc { |
| 20 using ::testing::NiceMock; | 21 using ::testing::NiceMock; |
| 21 using ::testing::Return; | 22 using ::testing::Return; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000}; | 26 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000}; |
| 27 constexpr int64_t kInitialTimeUs = 12345678; |
| 26 | 28 |
| 27 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { | 29 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst) { |
| 28 AudioEncoderOpus::Config config; | 30 AudioEncoderOpus::Config config; |
| 29 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | 31 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); |
| 30 config.num_channels = codec_inst.channels; | 32 config.num_channels = codec_inst.channels; |
| 31 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | 33 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); |
| 32 config.payload_type = codec_inst.pltype; | 34 config.payload_type = codec_inst.pltype; |
| 33 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | 35 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip |
| 34 : AudioEncoderOpus::kAudio; | 36 : AudioEncoderOpus::kAudio; |
| 35 return config; | 37 return config; |
| 36 } | 38 } |
| 37 | 39 |
| 38 struct AudioEncoderOpusStates { | 40 struct AudioEncoderOpusStates { |
| 39 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor; | 41 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor; |
| 40 std::unique_ptr<AudioEncoderOpus> encoder; | 42 std::unique_ptr<AudioEncoderOpus> encoder; |
| 43 std::unique_ptr<SimulatedClock> simulated_clock; |
| 41 }; | 44 }; |
| 42 | 45 |
| 43 AudioEncoderOpusStates CreateCodec(size_t num_channels) { | 46 AudioEncoderOpusStates CreateCodec(size_t num_channels) { |
| 44 AudioEncoderOpusStates states; | 47 AudioEncoderOpusStates states; |
| 45 states.mock_audio_network_adaptor = | 48 states.mock_audio_network_adaptor = |
| 46 std::make_shared<MockAudioNetworkAdaptor*>(nullptr); | 49 std::make_shared<MockAudioNetworkAdaptor*>(nullptr); |
| 47 | 50 |
| 48 std::weak_ptr<MockAudioNetworkAdaptor*> mock_ptr( | 51 std::weak_ptr<MockAudioNetworkAdaptor*> mock_ptr( |
| 49 states.mock_audio_network_adaptor); | 52 states.mock_audio_network_adaptor); |
| 50 AudioEncoderOpus::AudioNetworkAdaptorCreator creator = [mock_ptr]( | 53 AudioEncoderOpus::AudioNetworkAdaptorCreator creator = [mock_ptr]( |
| 51 const std::string&, const Clock*) { | 54 const std::string&, const Clock*) { |
| 52 std::unique_ptr<MockAudioNetworkAdaptor> adaptor( | 55 std::unique_ptr<MockAudioNetworkAdaptor> adaptor( |
| 53 new NiceMock<MockAudioNetworkAdaptor>()); | 56 new NiceMock<MockAudioNetworkAdaptor>()); |
| 54 EXPECT_CALL(*adaptor, Die()); | 57 EXPECT_CALL(*adaptor, Die()); |
| 55 if (auto sp = mock_ptr.lock()) { | 58 if (auto sp = mock_ptr.lock()) { |
| 56 *sp = adaptor.get(); | 59 *sp = adaptor.get(); |
| 57 } else { | 60 } else { |
| 58 RTC_NOTREACHED(); | 61 RTC_NOTREACHED(); |
| 59 } | 62 } |
| 60 return adaptor; | 63 return adaptor; |
| 61 }; | 64 }; |
| 62 | 65 |
| 63 CodecInst codec_inst = kDefaultOpusSettings; | 66 CodecInst codec_inst = kDefaultOpusSettings; |
| 64 codec_inst.channels = num_channels; | 67 codec_inst.channels = num_channels; |
| 65 auto config = CreateConfig(codec_inst); | 68 auto config = CreateConfig(codec_inst); |
| 69 states.simulated_clock.reset(new SimulatedClock(kInitialTimeUs)); |
| 70 config.clock = states.simulated_clock.get(); |
| 71 |
| 66 states.encoder.reset(new AudioEncoderOpus(config, std::move(creator))); | 72 states.encoder.reset(new AudioEncoderOpus(config, std::move(creator))); |
| 67 return states; | 73 return states; |
| 68 } | 74 } |
| 69 | 75 |
| 70 AudioNetworkAdaptor::EncoderRuntimeConfig CreateEncoderRuntimeConfig() { | 76 AudioNetworkAdaptor::EncoderRuntimeConfig CreateEncoderRuntimeConfig() { |
| 71 constexpr int kBitrate = 40000; | 77 constexpr int kBitrate = 40000; |
| 72 constexpr int kFrameLength = 60; | 78 constexpr int kFrameLength = 60; |
| 73 constexpr bool kEnableFec = true; | 79 constexpr bool kEnableFec = true; |
| 74 constexpr bool kEnableDtx = false; | 80 constexpr bool kEnableDtx = false; |
| 75 constexpr size_t kNumChannels = 1; | 81 constexpr size_t kNumChannels = 1; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 302 |
| 297 constexpr int kMinFrameLength = 10; | 303 constexpr int kMinFrameLength = 10; |
| 298 constexpr int kMaxFrameLength = 60; | 304 constexpr int kMaxFrameLength = 60; |
| 299 EXPECT_CALL(**states.mock_audio_network_adaptor, | 305 EXPECT_CALL(**states.mock_audio_network_adaptor, |
| 300 SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength)); | 306 SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength)); |
| 301 states.encoder->SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength); | 307 states.encoder->SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength); |
| 302 | 308 |
| 303 CheckEncoderRuntimeConfig(states.encoder.get(), config); | 309 CheckEncoderRuntimeConfig(states.encoder.get(), config); |
| 304 } | 310 } |
| 305 | 311 |
| 312 TEST(AudioEncoderOpusTest, |
| 313 PacketLossFractionSmoothedOnSetUplinkPacketLossFraction) { |
| 314 auto states = CreateCodec(2); |
| 315 |
| 316 // The values are carefully chosen so that if no smoothing is made, the test |
| 317 // will fail. |
| 318 constexpr float kPacketLossFraction_1 = 0.02f; |
| 319 constexpr float kPacketLossFraction_2 = 0.198f; |
| 320 // |kSecondSampleTimeMs| is chose to ease the calculation since |
| 321 // 0.9999 ^ 6931 = 0.5. |
| 322 constexpr float kSecondSampleTimeMs = 6931; |
| 323 |
| 324 // First time, no filtering. |
| 325 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_1); |
| 326 EXPECT_DOUBLE_EQ(0.01, states.encoder->packet_loss_rate()); |
| 327 |
| 328 states.simulated_clock->AdvanceTimeMilliseconds(kSecondSampleTimeMs); |
| 329 states.encoder->OnReceivedUplinkPacketLossFraction(kPacketLossFraction_2); |
| 330 |
| 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 |
| 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. |
| 335 EXPECT_DOUBLE_EQ(0.05, states.encoder->packet_loss_rate()); |
| 336 } |
| 337 |
| 306 } // namespace webrtc | 338 } // namespace webrtc |
| OLD | NEW |