1 /* 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_UNITTEST_HELPER_H_ 12 #define MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_UNITTEST_HELPER_H_ 13 14 #include <list> 15 #include <map> 16 #include <memory> 17 #include <utility> 18 #include <vector> 19 20 #include "modules/congestion_controller/acknowledged_bitrate_estimator.h" 21 #include "modules/congestion_controller/delay_based_bwe.h" 22 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" 23 #include "rtc_base/constructormagic.h" 24 #include "system_wrappers/include/clock.h" 25 #include "test/gtest.h" 26 27 namespace webrtc { 28 namespace test { 29 30 class TestBitrateObserver : public RemoteBitrateObserver { 31 public: TestBitrateObserver()32 TestBitrateObserver() : updated_(false), latest_bitrate_(0) {} ~TestBitrateObserver()33 virtual ~TestBitrateObserver() {} 34 35 void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs, 36 uint32_t bitrate) override; 37 Reset()38 void Reset() { updated_ = false; } 39 updated()40 bool updated() const { return updated_; } 41 latest_bitrate()42 uint32_t latest_bitrate() const { return latest_bitrate_; } 43 44 private: 45 bool updated_; 46 uint32_t latest_bitrate_; 47 }; 48 49 class RtpStream { 50 public: 51 enum { kSendSideOffsetUs = 1000000 }; 52 53 RtpStream(int fps, int bitrate_bps); 54 55 // Generates a new frame for this stream. If called too soon after the 56 // previous frame, no frame will be generated. The frame is split into 57 // packets. 58 int64_t GenerateFrame(int64_t time_now_us, 59 std::vector<PacketFeedback>* packets); 60 61 // The send-side time when the next frame can be generated. 62 int64_t next_rtp_time() const; 63 64 void set_bitrate_bps(int bitrate_bps); 65 66 int bitrate_bps() const; 67 68 static bool Compare(const std::unique_ptr<RtpStream>& lhs, 69 const std::unique_ptr<RtpStream>& rhs); 70 71 private: 72 int fps_; 73 int bitrate_bps_; 74 int64_t next_rtp_time_; 75 uint16_t sequence_number_; 76 77 RTC_DISALLOW_COPY_AND_ASSIGN(RtpStream); 78 }; 79 80 class StreamGenerator { 81 public: 82 StreamGenerator(int capacity, int64_t time_now); 83 84 // Add a new stream. 85 void AddStream(RtpStream* stream); 86 87 // Set the link capacity. 88 void set_capacity_bps(int capacity_bps); 89 90 // Divides |bitrate_bps| among all streams. The allocated bitrate per stream 91 // is decided by the initial allocation ratios. 92 void SetBitrateBps(int bitrate_bps); 93 94 // Set the RTP timestamp offset for the stream identified by |ssrc|. 95 void set_rtp_timestamp_offset(uint32_t ssrc, uint32_t offset); 96 97 // TODO(holmer): Break out the channel simulation part from this class to make 98 // it possible to simulate different types of channels. 99 int64_t GenerateFrame(std::vector<PacketFeedback>* packets, 100 int64_t time_now_us); 101 102 private: 103 // Capacity of the simulated channel in bits per second. 104 int capacity_; 105 // The time when the last packet arrived. 106 int64_t prev_arrival_time_us_; 107 // All streams being transmitted on this simulated channel. 108 std::vector<std::unique_ptr<RtpStream>> streams_; 109 110 RTC_DISALLOW_COPY_AND_ASSIGN(StreamGenerator); 111 }; 112 } // namespace test 113 114 class DelayBasedBweTest : public ::testing::Test { 115 public: 116 DelayBasedBweTest(); 117 virtual ~DelayBasedBweTest(); 118 119 protected: 120 void AddDefaultStream(); 121 122 // Helpers to insert a single packet into the delay-based BWE. 123 void IncomingFeedback(int64_t arrival_time_ms, 124 int64_t send_time_ms, 125 uint16_t sequence_number, 126 size_t payload_size); 127 void IncomingFeedback(int64_t arrival_time_ms, 128 int64_t send_time_ms, 129 uint16_t sequence_number, 130 size_t payload_size, 131 const PacedPacketInfo& pacing_info); 132 133 // Generates a frame of packets belonging to a stream at a given bitrate and 134 // with a given ssrc. The stream is pushed through a very simple simulated 135 // network, and is then given to the receive-side bandwidth estimator. 136 // Returns true if an over-use was seen, false otherwise. 137 // The StreamGenerator::updated() should be used to check for any changes in 138 // target bitrate after the call to this function. 139 bool GenerateAndProcessFrame(uint32_t ssrc, uint32_t bitrate_bps); 140 141 // Run the bandwidth estimator with a stream of |number_of_frames| frames, or 142 // until it reaches |target_bitrate|. 143 // Can for instance be used to run the estimator for some time to get it 144 // into a steady state. 145 uint32_t SteadyStateRun(uint32_t ssrc, 146 int number_of_frames, 147 uint32_t start_bitrate, 148 uint32_t min_bitrate, 149 uint32_t max_bitrate, 150 uint32_t target_bitrate); 151 152 void TestTimestampGroupingTestHelper(); 153 154 void TestWrappingHelper(int silence_time_s); 155 156 void InitialBehaviorTestHelper(uint32_t expected_converge_bitrate); 157 void RateIncreaseReorderingTestHelper(uint32_t expected_bitrate); 158 void RateIncreaseRtpTimestampsTestHelper(int expected_iterations); 159 void CapacityDropTestHelper(int number_of_streams, 160 bool wrap_time_stamp, 161 uint32_t expected_bitrate_drop_delta, 162 int64_t receiver_clock_offset_change_ms); 163 164 static const uint32_t kDefaultSsrc; 165 166 SimulatedClock clock_; // Time at the receiver. 167 test::TestBitrateObserver bitrate_observer_; 168 std::unique_ptr<AcknowledgedBitrateEstimator> acknowledged_bitrate_estimator_; 169 std::unique_ptr<DelayBasedBwe> bitrate_estimator_; 170 std::unique_ptr<test::StreamGenerator> stream_generator_; 171 int64_t arrival_time_offset_ms_; 172 bool first_update_; 173 174 RTC_DISALLOW_COPY_AND_ASSIGN(DelayBasedBweTest); 175 }; 176 } // namespace webrtc 177 178 #endif // MODULES_CONGESTION_CONTROLLER_DELAY_BASED_BWE_UNITTEST_HELPER_H_ 179