2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_UNITTEST_HELPER_H_
12 #define WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_UNITTEST_HELPER_H_
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "webrtc/base/constructormagic.h"
20 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
21 #include "webrtc/system_wrappers/interface/clock.h"
22 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
27 class TestBitrateObserver : public RemoteBitrateObserver {
29 TestBitrateObserver() : updated_(false), latest_bitrate_(0) {}
30 virtual ~TestBitrateObserver() {}
32 virtual void OnReceiveBitrateChanged(const std::vector<unsigned int>& ssrcs,
33 unsigned int bitrate) OVERRIDE;
35 void Reset() { updated_ = false; }
37 bool updated() const { return updated_; }
39 unsigned int latest_bitrate() const { return latest_bitrate_; }
43 unsigned int latest_bitrate_;
51 uint32_t rtp_timestamp;
63 typedef std::list<RtpPacket*> PacketList;
65 enum { kSendSideOffsetUs = 1000000 };
67 RtpStream(int fps, int bitrate_bps, unsigned int ssrc, unsigned int frequency,
68 uint32_t timestamp_offset, int64_t rtcp_receive_time);
69 void set_rtp_timestamp_offset(uint32_t offset);
71 // Generates a new frame for this stream. If called too soon after the
72 // previous frame, no frame will be generated. The frame is split into
74 int64_t GenerateFrame(int64_t time_now_us, PacketList* packets);
76 // The send-side time when the next frame can be generated.
77 double next_rtp_time() const;
79 // Generates an RTCP packet.
80 RtcpPacket* Rtcp(int64_t time_now_us);
82 void set_bitrate_bps(int bitrate_bps);
84 int bitrate_bps() const;
86 unsigned int ssrc() const;
88 static bool Compare(const std::pair<unsigned int, RtpStream*>& left,
89 const std::pair<unsigned int, RtpStream*>& right);
92 enum { kRtcpIntervalUs = 1000000 };
97 unsigned int frequency_;
98 int64_t next_rtp_time_;
99 int64_t next_rtcp_time_;
100 uint32_t rtp_timestamp_offset_;
101 const double kNtpFracPerMs;
103 DISALLOW_COPY_AND_ASSIGN(RtpStream);
106 class StreamGenerator {
108 typedef std::list<RtpStream::RtcpPacket*> RtcpList;
110 StreamGenerator(int capacity, double time_now);
115 void AddStream(RtpStream* stream);
117 // Set the link capacity.
118 void set_capacity_bps(int capacity_bps);
120 // Divides |bitrate_bps| among all streams. The allocated bitrate per stream
121 // is decided by the initial allocation ratios.
122 void SetBitrateBps(int bitrate_bps);
124 // Set the RTP timestamp offset for the stream identified by |ssrc|.
125 void set_rtp_timestamp_offset(unsigned int ssrc, uint32_t offset);
127 // TODO(holmer): Break out the channel simulation part from this class to make
128 // it possible to simulate different types of channels.
129 int64_t GenerateFrame(RtpStream::PacketList* packets, int64_t time_now_us);
132 typedef std::map<unsigned int, RtpStream*> StreamMap;
134 // Capacity of the simulated channel in bits per second.
136 // The time when the last packet arrived.
137 int64_t prev_arrival_time_us_;
138 // All streams being transmitted on this simulated channel.
141 DISALLOW_COPY_AND_ASSIGN(StreamGenerator);
143 } // namespace testing
145 class RemoteBitrateEstimatorTest : public ::testing::Test {
147 RemoteBitrateEstimatorTest();
148 virtual ~RemoteBitrateEstimatorTest();
151 virtual void SetUp() = 0;
153 void AddDefaultStream();
155 // Helper to convert some time format to resolution used in absolute send time
156 // header extension, rounded upwards. |t| is the time to convert, in some
157 // resolution. |denom| is the value to divide |t| by to get whole seconds,
158 // e.g. |denom| = 1000 if |t| is in milliseconds.
159 static uint32_t AbsSendTime(int64_t t, int64_t denom);
161 // Helper to add two absolute send time values and keep it less than 1<<24.
162 static uint32_t AddAbsSendTime(uint32_t t1, uint32_t t2);
164 // Helper to create a WebRtcRTPHeader containing the relevant data for the
165 // estimator (all other fields are cleared) and call IncomingPacket on the
167 void IncomingPacket(uint32_t ssrc,
168 uint32_t payload_size,
169 int64_t arrival_time,
170 uint32_t rtp_timestamp,
171 uint32_t absolute_send_time);
173 // Generates a frame of packets belonging to a stream at a given bitrate and
174 // with a given ssrc. The stream is pushed through a very simple simulated
175 // network, and is then given to the receive-side bandwidth estimator.
176 // Returns true if an over-use was seen, false otherwise.
177 // The StreamGenerator::updated() should be used to check for any changes in
178 // target bitrate after the call to this function.
179 bool GenerateAndProcessFrame(unsigned int ssrc, unsigned int bitrate_bps);
181 // Run the bandwidth estimator with a stream of |number_of_frames| frames, or
182 // until it reaches |target_bitrate|.
183 // Can for instance be used to run the estimator for some time to get it
184 // into a steady state.
185 unsigned int SteadyStateRun(unsigned int ssrc,
186 int number_of_frames,
187 unsigned int start_bitrate,
188 unsigned int min_bitrate,
189 unsigned int max_bitrate,
190 unsigned int target_bitrate);
192 void InitialBehaviorTestHelper(unsigned int expected_converge_bitrate);
193 void RateIncreaseReorderingTestHelper(unsigned int expected_bitrate);
194 void RateIncreaseRtpTimestampsTestHelper();
195 void CapacityDropTestHelper(int number_of_streams,
196 bool wrap_time_stamp,
197 unsigned int expected_converge_bitrate,
198 unsigned int expected_bitrate_drop_delta);
200 static const unsigned int kDefaultSsrc;
201 static const int kArrivalTimeClockOffsetMs = 60000;
203 SimulatedClock clock_; // Time at the receiver.
204 scoped_ptr<testing::TestBitrateObserver> bitrate_observer_;
205 scoped_ptr<RemoteBitrateEstimator> bitrate_estimator_;
206 scoped_ptr<testing::StreamGenerator> stream_generator_;
208 DISALLOW_COPY_AND_ASSIGN(RemoteBitrateEstimatorTest);
210 } // namespace webrtc
212 #endif // WEBRTC_MODULES_REMOTE_BITRATE_ESTIMATOR_REMOTE_BITRATE_ESTIMATOR_UNITTEST_HELPER_H_