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