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