OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 "webrtc/modules/congestion_controller/include/congestion_controller.h" |
11 #include "webrtc/logging/rtc_event_log/mock/mock_rtc_event_log.h" | 12 #include "webrtc/logging/rtc_event_log/mock/mock_rtc_event_log.h" |
12 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 13 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
13 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 14 #include "webrtc/modules/congestion_controller/congestion_controller_unittests_h
elper.h" |
14 #include "webrtc/modules/congestion_controller/include/mock/mock_congestion_cont
roller.h" | 15 #include "webrtc/modules/congestion_controller/include/mock/mock_congestion_cont
roller.h" |
15 #include "webrtc/modules/pacing/mock/mock_paced_sender.h" | 16 #include "webrtc/modules/pacing/mock/mock_paced_sender.h" |
16 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" | 17 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
17 #include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitra
te_observer.h" | 18 #include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitra
te_observer.h" |
| 19 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
18 #include "webrtc/system_wrappers/include/clock.h" | 20 #include "webrtc/system_wrappers/include/clock.h" |
19 #include "webrtc/test/gmock.h" | 21 #include "webrtc/test/gmock.h" |
20 #include "webrtc/test/gtest.h" | 22 #include "webrtc/test/gtest.h" |
21 | 23 |
22 using testing::_; | 24 using testing::_; |
23 using testing::AtLeast; | 25 using testing::AtLeast; |
24 using testing::NiceMock; | 26 using testing::NiceMock; |
25 using testing::Return; | 27 using testing::Return; |
26 using testing::SaveArg; | 28 using testing::SaveArg; |
27 using testing::StrictMock; | 29 using testing::StrictMock; |
28 | 30 |
29 namespace { | 31 namespace { |
| 32 const webrtc::PacedPacketInfo kPacingInfo0(0, 5, 2000); |
| 33 const webrtc::PacedPacketInfo kPacingInfo1(1, 8, 4000); |
30 | 34 |
31 // Helper to convert some time format to resolution used in absolute send time | 35 // Helper to convert some time format to resolution used in absolute send time |
32 // header extension, rounded upwards. |t| is the time to convert, in some | 36 // header extension, rounded upwards. |t| is the time to convert, in some |
33 // resolution. |denom| is the value to divide |t| by to get whole seconds, | 37 // resolution. |denom| is the value to divide |t| by to get whole seconds, |
34 // e.g. |denom| = 1000 if |t| is in milliseconds. | 38 // e.g. |denom| = 1000 if |t| is in milliseconds. |
35 uint32_t AbsSendTime(int64_t t, int64_t denom) { | 39 uint32_t AbsSendTime(int64_t t, int64_t denom) { |
36 return (((t << 18) + (denom >> 1)) / denom) & 0x00fffffful; | 40 return (((t << 18) + (denom >> 1)) / denom) & 0x00fffffful; |
37 } | 41 } |
38 | 42 |
39 } // namespace | 43 } // namespace |
40 | 44 |
41 namespace webrtc { | 45 namespace webrtc { |
42 namespace test { | 46 namespace test { |
43 | 47 |
44 class CongestionControllerTest : public ::testing::Test { | 48 class CongestionControllerTest : public ::testing::Test { |
45 protected: | 49 protected: |
46 CongestionControllerTest() : clock_(123456) {} | 50 CongestionControllerTest() : clock_(123456), target_bitrate_observer_(this) {} |
47 ~CongestionControllerTest() override {} | 51 ~CongestionControllerTest() override {} |
48 | 52 |
49 void SetUp() override { | 53 void SetUp() override { |
50 pacer_ = new NiceMock<MockPacedSender>(); | 54 pacer_ = new NiceMock<MockPacedSender>(); |
51 std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership. | 55 std::unique_ptr<PacedSender> pacer(pacer_); // Passes ownership. |
52 controller_.reset(new CongestionController( | 56 controller_.reset(new CongestionController( |
53 &clock_, &observer_, &remote_bitrate_observer_, &event_log_, | 57 &clock_, &observer_, &remote_bitrate_observer_, &event_log_, |
54 &packet_router_, std::move(pacer))); | 58 &packet_router_, std::move(pacer))); |
55 bandwidth_observer_.reset( | 59 bandwidth_observer_.reset( |
56 controller_->GetBitrateController()->CreateRtcpBandwidthObserver()); | 60 controller_->GetBitrateController()->CreateRtcpBandwidthObserver()); |
57 | 61 |
58 // Set the initial bitrate estimate and expect the |observer| and |pacer_| | 62 // Set the initial bitrate estimate and expect the |observer| and |pacer_| |
59 // to be updated. | 63 // to be updated. |
60 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); | 64 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps, _, _, _)); |
61 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps)); | 65 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps)); |
62 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 3)); | 66 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 3)); |
63 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 5)); | 67 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 5)); |
64 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps); | 68 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps); |
65 } | 69 } |
66 | 70 |
| 71 // Custom setup - use an observer that tracks the target bitrate, without |
| 72 // prescribing on which iterations it must change (like a mock would). |
| 73 void TargetBitrateTrackingSetup() { |
| 74 std::unique_ptr<PacedSender> pacer(new NiceMock<MockPacedSender>()); |
| 75 controller_.reset(new CongestionController( |
| 76 &clock_, &target_bitrate_observer_, &remote_bitrate_observer_, |
| 77 &event_log_, &packet_router_, std::move(pacer))); |
| 78 controller_->SetBweBitrates(0, kInitialBitrateBps, 5 * kInitialBitrateBps); |
| 79 } |
| 80 |
| 81 void OnSentPacket(const PacketFeedback& packet_feedback) { |
| 82 controller_->AddPacket(packet_feedback.sequence_number, |
| 83 packet_feedback.payload_size, |
| 84 packet_feedback.pacing_info); |
| 85 controller_->OnSentPacket(rtc::SentPacket(packet_feedback.sequence_number, |
| 86 packet_feedback.send_time_ms)); |
| 87 } |
| 88 |
| 89 // Allows us to track the target bitrate, without prescribing the exact |
| 90 // iterations when this would hapen, like a mock would. |
| 91 class TargetBitrateObserver : public CongestionController::Observer { |
| 92 public: |
| 93 explicit TargetBitrateObserver(CongestionControllerTest* owner) |
| 94 : owner_(owner) {} |
| 95 ~TargetBitrateObserver() override = default; |
| 96 void OnNetworkChanged(uint32_t bitrate_bps, |
| 97 uint8_t fraction_loss, // 0 - 255. |
| 98 int64_t rtt_ms, |
| 99 int64_t probing_interval_ms) override { |
| 100 owner_->target_bitrate_bps_ = rtc::Optional<uint32_t>(bitrate_bps); |
| 101 } |
| 102 |
| 103 private: |
| 104 CongestionControllerTest* owner_; |
| 105 }; |
| 106 |
| 107 void PacketTransmissionAndFeedbackBlock(uint16_t* seq_num, |
| 108 int64_t runtime_ms, |
| 109 int64_t delay) { |
| 110 int64_t delay_buildup = 0; |
| 111 int64_t start_time_ms = clock_.TimeInMilliseconds(); |
| 112 while (clock_.TimeInMilliseconds() - start_time_ms < runtime_ms) { |
| 113 constexpr size_t kPayloadSize = 1000; |
| 114 PacketFeedback packet(clock_.TimeInMilliseconds() + delay_buildup, |
| 115 clock_.TimeInMilliseconds(), *seq_num, kPayloadSize, |
| 116 PacedPacketInfo()); |
| 117 delay_buildup += delay; // Delay has to increase, or it's just RTT. |
| 118 OnSentPacket(packet); |
| 119 // Create expected feedback and send into adapter. |
| 120 std::unique_ptr<rtcp::TransportFeedback> feedback( |
| 121 new rtcp::TransportFeedback()); |
| 122 feedback->SetBase(packet.sequence_number, packet.arrival_time_ms * 1000); |
| 123 EXPECT_TRUE(feedback->AddReceivedPacket(packet.sequence_number, |
| 124 packet.arrival_time_ms * 1000)); |
| 125 rtc::Buffer raw_packet = feedback->Build(); |
| 126 feedback = rtcp::TransportFeedback::ParseFrom(raw_packet.data(), |
| 127 raw_packet.size()); |
| 128 EXPECT_TRUE(feedback.get() != nullptr); |
| 129 controller_->OnTransportFeedback(*feedback.get()); |
| 130 clock_.AdvanceTimeMilliseconds(50); |
| 131 controller_->Process(); |
| 132 ++(*seq_num); |
| 133 } |
| 134 } |
| 135 |
67 SimulatedClock clock_; | 136 SimulatedClock clock_; |
68 StrictMock<MockCongestionObserver> observer_; | 137 StrictMock<MockCongestionObserver> observer_; |
| 138 TargetBitrateObserver target_bitrate_observer_; |
69 NiceMock<MockPacedSender>* pacer_; | 139 NiceMock<MockPacedSender>* pacer_; |
70 NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_; | 140 NiceMock<MockRemoteBitrateObserver> remote_bitrate_observer_; |
71 NiceMock<MockRtcEventLog> event_log_; | 141 NiceMock<MockRtcEventLog> event_log_; |
72 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; | 142 std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_; |
73 PacketRouter packet_router_; | 143 PacketRouter packet_router_; |
74 std::unique_ptr<CongestionController> controller_; | 144 std::unique_ptr<CongestionController> controller_; |
75 const uint32_t kInitialBitrateBps = 60000; | 145 const uint32_t kInitialBitrateBps = 60000; |
| 146 |
| 147 rtc::Optional<uint32_t> target_bitrate_bps_; |
76 }; | 148 }; |
77 | 149 |
78 TEST_F(CongestionControllerTest, OnNetworkChanged) { | 150 TEST_F(CongestionControllerTest, OnNetworkChanged) { |
79 // Test no change. | 151 // Test no change. |
80 clock_.AdvanceTimeMilliseconds(25); | 152 clock_.AdvanceTimeMilliseconds(25); |
81 controller_->Process(); | 153 controller_->Process(); |
82 | 154 |
83 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); | 155 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); |
84 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); | 156 EXPECT_CALL(*pacer_, SetEstimatedBitrate(kInitialBitrateBps * 2)); |
85 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); | 157 bandwidth_observer_->OnReceivedEstimatedBitrate(kInitialBitrateBps * 2); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 318 |
247 TEST_F(CongestionControllerTest, ProbeOnBweReset) { | 319 TEST_F(CongestionControllerTest, ProbeOnBweReset) { |
248 testing::Mock::VerifyAndClearExpectations(pacer_); | 320 testing::Mock::VerifyAndClearExpectations(pacer_); |
249 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 6)); | 321 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 6)); |
250 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 12)); | 322 EXPECT_CALL(*pacer_, CreateProbeCluster(kInitialBitrateBps * 12)); |
251 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); | 323 EXPECT_CALL(observer_, OnNetworkChanged(kInitialBitrateBps * 2, _, _, _)); |
252 controller_->ResetBweAndBitrates(2 * kInitialBitrateBps, 0, | 324 controller_->ResetBweAndBitrates(2 * kInitialBitrateBps, 0, |
253 20 * kInitialBitrateBps); | 325 20 * kInitialBitrateBps); |
254 } | 326 } |
255 | 327 |
| 328 // Estimated bitrate reduced when the feedbacks arrive with such a long delay, |
| 329 // that the send-time-history no longer holds the feedbacked packets. |
| 330 TEST_F(CongestionControllerTest, LongFeedbackDelays) { |
| 331 TargetBitrateTrackingSetup(); |
| 332 |
| 333 const int64_t kFeedbackTimeoutMs = 60001; |
| 334 const int kMaxConsecutiveFailedLookups = 5; |
| 335 for (int i = 0; i < kMaxConsecutiveFailedLookups; ++i) { |
| 336 std::vector<PacketFeedback> packets; |
| 337 packets.push_back( |
| 338 PacketFeedback(i * 100, 2 * i * 100, 0, 1500, kPacingInfo0)); |
| 339 packets.push_back( |
| 340 PacketFeedback(i * 100 + 10, 2 * i * 100 + 10, 1, 1500, kPacingInfo0)); |
| 341 packets.push_back( |
| 342 PacketFeedback(i * 100 + 20, 2 * i * 100 + 20, 2, 1500, kPacingInfo0)); |
| 343 packets.push_back( |
| 344 PacketFeedback(i * 100 + 30, 2 * i * 100 + 30, 3, 1500, kPacingInfo1)); |
| 345 packets.push_back( |
| 346 PacketFeedback(i * 100 + 40, 2 * i * 100 + 40, 4, 1500, kPacingInfo1)); |
| 347 |
| 348 for (const PacketFeedback& packet : packets) |
| 349 OnSentPacket(packet); |
| 350 |
| 351 rtcp::TransportFeedback feedback; |
| 352 feedback.SetBase(packets[0].sequence_number, |
| 353 packets[0].arrival_time_ms * 1000); |
| 354 |
| 355 for (const PacketFeedback& packet : packets) { |
| 356 EXPECT_TRUE(feedback.AddReceivedPacket(packet.sequence_number, |
| 357 packet.arrival_time_ms * 1000)); |
| 358 } |
| 359 |
| 360 feedback.Build(); |
| 361 |
| 362 clock_.AdvanceTimeMilliseconds(kFeedbackTimeoutMs); |
| 363 PacketFeedback later_packet(kFeedbackTimeoutMs + i * 100 + 40, |
| 364 kFeedbackTimeoutMs + i * 200 + 40, 5, 1500, |
| 365 kPacingInfo1); |
| 366 OnSentPacket(later_packet); |
| 367 |
| 368 controller_->OnTransportFeedback(feedback); |
| 369 |
| 370 // Check that packets have timed out. |
| 371 for (PacketFeedback& packet : packets) { |
| 372 packet.send_time_ms = -1; |
| 373 packet.payload_size = 0; |
| 374 packet.pacing_info = PacedPacketInfo(); |
| 375 } |
| 376 ComparePacketFeedbackVectors(packets, |
| 377 controller_->GetTransportFeedbackVector()); |
| 378 } |
| 379 |
| 380 controller_->Process(); |
| 381 |
| 382 EXPECT_EQ(kInitialBitrateBps / 2, target_bitrate_bps_); |
| 383 |
| 384 // Test with feedback that isn't late enough to time out. |
| 385 { |
| 386 std::vector<PacketFeedback> packets; |
| 387 packets.push_back(PacketFeedback(100, 200, 0, 1500, kPacingInfo0)); |
| 388 packets.push_back(PacketFeedback(110, 210, 1, 1500, kPacingInfo0)); |
| 389 packets.push_back(PacketFeedback(120, 220, 2, 1500, kPacingInfo0)); |
| 390 packets.push_back(PacketFeedback(130, 230, 3, 1500, kPacingInfo1)); |
| 391 packets.push_back(PacketFeedback(140, 240, 4, 1500, kPacingInfo1)); |
| 392 |
| 393 for (const PacketFeedback& packet : packets) |
| 394 OnSentPacket(packet); |
| 395 |
| 396 rtcp::TransportFeedback feedback; |
| 397 feedback.SetBase(packets[0].sequence_number, |
| 398 packets[0].arrival_time_ms * 1000); |
| 399 |
| 400 for (const PacketFeedback& packet : packets) { |
| 401 EXPECT_TRUE(feedback.AddReceivedPacket(packet.sequence_number, |
| 402 packet.arrival_time_ms * 1000)); |
| 403 } |
| 404 |
| 405 feedback.Build(); |
| 406 |
| 407 clock_.AdvanceTimeMilliseconds(kFeedbackTimeoutMs - 1); |
| 408 PacketFeedback later_packet(kFeedbackTimeoutMs + 140, |
| 409 kFeedbackTimeoutMs + 240, 5, 1500, |
| 410 kPacingInfo1); |
| 411 OnSentPacket(later_packet); |
| 412 |
| 413 controller_->OnTransportFeedback(feedback); |
| 414 ComparePacketFeedbackVectors(packets, |
| 415 controller_->GetTransportFeedbackVector()); |
| 416 } |
| 417 } |
| 418 |
| 419 // Bandwidth estimation is updated when feedbacks are received. |
| 420 // Feedbacks which show an increasing delay cause the estimation to be reduced. |
| 421 TEST_F(CongestionControllerTest, UpdatesDelayBasedEstimate) { |
| 422 TargetBitrateTrackingSetup(); |
| 423 |
| 424 const int64_t kRunTimeMs = 6000; |
| 425 uint16_t seq_num = 0; |
| 426 |
| 427 // The test must run and insert packets/feedback long enough that the |
| 428 // BWE computes a valid estimate. This is first done in an environment which |
| 429 // simulates no bandwidth limitation, and therefore not built-up delay. |
| 430 PacketTransmissionAndFeedbackBlock(&seq_num, kRunTimeMs, 0); |
| 431 ASSERT_TRUE(target_bitrate_bps_); |
| 432 |
| 433 // Repeat, but this time with a building delay, and make sure that the |
| 434 // estimation is adjusted downwards. |
| 435 uint32_t bitrate_before_delay = *target_bitrate_bps_; |
| 436 PacketTransmissionAndFeedbackBlock(&seq_num, kRunTimeMs, 50); |
| 437 EXPECT_LT(*target_bitrate_bps_, bitrate_before_delay); |
| 438 } |
256 } // namespace test | 439 } // namespace test |
257 } // namespace webrtc | 440 } // namespace webrtc |
OLD | NEW |