1 /*
2  *  Copyright (c) 2013 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 #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
12 
13 #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 
18 class RemoteBitrateEstimatorAbsSendTimeTest
19     : public RemoteBitrateEstimatorTest {
20  public:
RemoteBitrateEstimatorAbsSendTimeTest()21   RemoteBitrateEstimatorAbsSendTimeTest() {}
22 
23   RemoteBitrateEstimatorAbsSendTimeTest(
24       const RemoteBitrateEstimatorAbsSendTimeTest&) = delete;
25   RemoteBitrateEstimatorAbsSendTimeTest& operator=(
26       const RemoteBitrateEstimatorAbsSendTimeTest&) = delete;
27 
SetUp()28   virtual void SetUp() {
29     bitrate_estimator_.reset(new RemoteBitrateEstimatorAbsSendTime(
30         bitrate_observer_.get(), &clock_));
31   }
32 
33  protected:
34 };
35 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,InitialBehavior)36 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, InitialBehavior) {
37   InitialBehaviorTestHelper(674840);
38 }
39 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,RateIncreaseReordering)40 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, RateIncreaseReordering) {
41   RateIncreaseReorderingTestHelper(674840);
42 }
43 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,RateIncreaseRtpTimestamps)44 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, RateIncreaseRtpTimestamps) {
45   RateIncreaseRtpTimestampsTestHelper(1237);
46 }
47 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropOneStream)48 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropOneStream) {
49   CapacityDropTestHelper(1, false, 633, 0);
50 }
51 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropPosOffsetChange)52 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropPosOffsetChange) {
53   CapacityDropTestHelper(1, false, 267, 30000);
54 }
55 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropNegOffsetChange)56 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropNegOffsetChange) {
57   CapacityDropTestHelper(1, false, 267, -30000);
58 }
59 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropOneStreamWrap)60 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropOneStreamWrap) {
61   CapacityDropTestHelper(1, true, 633, 0);
62 }
63 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropTwoStreamsWrap)64 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropTwoStreamsWrap) {
65   CapacityDropTestHelper(2, true, 700, 0);
66 }
67 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropThreeStreamsWrap)68 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropThreeStreamsWrap) {
69   CapacityDropTestHelper(3, true, 633, 0);
70 }
71 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropThirteenStreamsWrap)72 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropThirteenStreamsWrap) {
73   CapacityDropTestHelper(13, true, 667, 0);
74 }
75 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropNineteenStreamsWrap)76 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropNineteenStreamsWrap) {
77   CapacityDropTestHelper(19, true, 667, 0);
78 }
79 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,CapacityDropThirtyStreamsWrap)80 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, CapacityDropThirtyStreamsWrap) {
81   CapacityDropTestHelper(30, true, 667, 0);
82 }
83 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestTimestampGrouping)84 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestTimestampGrouping) {
85   TestTimestampGroupingTestHelper();
86 }
87 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestShortTimeoutAndWrap)88 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestShortTimeoutAndWrap) {
89   // Simulate a client leaving and rejoining the call after 35 seconds. This
90   // will make abs send time wrap, so if streams aren't timed out properly
91   // the next 30 seconds of packets will be out of order.
92   TestWrappingHelper(35);
93 }
94 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestLongTimeoutAndWrap)95 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestLongTimeoutAndWrap) {
96   // Simulate a client leaving and rejoining the call after some multiple of
97   // 64 seconds later. This will cause a zero difference in abs send times due
98   // to the wrap, but a big difference in arrival time, if streams aren't
99   // properly timed out.
100   TestWrappingHelper(10 * 64);
101 }
102 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProcessAfterTimeout)103 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestProcessAfterTimeout) {
104   // This time constant must be equal to the ones defined for the
105   // RemoteBitrateEstimator.
106   const int64_t kStreamTimeOutMs = 2000;
107   const int64_t kProcessIntervalMs = 1000;
108   IncomingPacket(0, 1000, clock_.TimeInMilliseconds(), 0, 0);
109   clock_.AdvanceTimeMilliseconds(kStreamTimeOutMs + 1);
110   // Trigger timeout.
111   bitrate_estimator_->Process();
112   clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
113   // This shouldn't crash.
114   bitrate_estimator_->Process();
115 }
116 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetection)117 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestProbeDetection) {
118   const int kProbeLength = 5;
119   int64_t now_ms = clock_.TimeInMilliseconds();
120   // First burst sent at 8 * 1000 / 10 = 800 kbps.
121   for (int i = 0; i < kProbeLength; ++i) {
122     clock_.AdvanceTimeMilliseconds(10);
123     now_ms = clock_.TimeInMilliseconds();
124     IncomingPacket(0, 1000, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
125   }
126 
127   // Second burst sent at 8 * 1000 / 5 = 1600 kbps.
128   for (int i = 0; i < kProbeLength; ++i) {
129     clock_.AdvanceTimeMilliseconds(5);
130     now_ms = clock_.TimeInMilliseconds();
131     IncomingPacket(0, 1000, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
132   }
133 
134   bitrate_estimator_->Process();
135   EXPECT_TRUE(bitrate_observer_->updated());
136   EXPECT_GT(bitrate_observer_->latest_bitrate(), 1500000u);
137 }
138 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionNonPacedPackets)139 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,
140        TestProbeDetectionNonPacedPackets) {
141   const int kProbeLength = 5;
142   int64_t now_ms = clock_.TimeInMilliseconds();
143   // First burst sent at 8 * 1000 / 10 = 800 kbps, but with every other packet
144   // not being paced which could mess things up.
145   for (int i = 0; i < kProbeLength; ++i) {
146     clock_.AdvanceTimeMilliseconds(5);
147     now_ms = clock_.TimeInMilliseconds();
148     IncomingPacket(0, 1000, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
149     // Non-paced packet, arriving 5 ms after.
150     clock_.AdvanceTimeMilliseconds(5);
151     IncomingPacket(0, 100, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
152   }
153 
154   bitrate_estimator_->Process();
155   EXPECT_TRUE(bitrate_observer_->updated());
156   EXPECT_GT(bitrate_observer_->latest_bitrate(), 800000u);
157 }
158 
159 // Packets will require 5 ms to be transmitted to the receiver, causing packets
160 // of the second probe to be dispersed.
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionTooHighBitrate)161 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,
162        TestProbeDetectionTooHighBitrate) {
163   const int kProbeLength = 5;
164   int64_t now_ms = clock_.TimeInMilliseconds();
165   int64_t send_time_ms = 0;
166   // First burst sent at 8 * 1000 / 10 = 800 kbps.
167   for (int i = 0; i < kProbeLength; ++i) {
168     clock_.AdvanceTimeMilliseconds(10);
169     now_ms = clock_.TimeInMilliseconds();
170     send_time_ms += 10;
171     IncomingPacket(0, 1000, now_ms, 90 * send_time_ms,
172                    AbsSendTime(send_time_ms, 1000));
173   }
174 
175   // Second burst sent at 8 * 1000 / 5 = 1600 kbps, arriving at 8 * 1000 / 8 =
176   // 1000 kbps.
177   for (int i = 0; i < kProbeLength; ++i) {
178     clock_.AdvanceTimeMilliseconds(8);
179     now_ms = clock_.TimeInMilliseconds();
180     send_time_ms += 5;
181     IncomingPacket(0, 1000, now_ms, send_time_ms,
182                    AbsSendTime(send_time_ms, 1000));
183   }
184 
185   bitrate_estimator_->Process();
186   EXPECT_TRUE(bitrate_observer_->updated());
187   EXPECT_NEAR(bitrate_observer_->latest_bitrate(), 800000u, 10000);
188 }
189 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionSlightlyFasterArrival)190 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,
191        TestProbeDetectionSlightlyFasterArrival) {
192   const int kProbeLength = 5;
193   int64_t now_ms = clock_.TimeInMilliseconds();
194   // First burst sent at 8 * 1000 / 10 = 800 kbps.
195   // Arriving at 8 * 1000 / 5 = 1600 kbps.
196   int64_t send_time_ms = 0;
197   for (int i = 0; i < kProbeLength; ++i) {
198     clock_.AdvanceTimeMilliseconds(5);
199     send_time_ms += 10;
200     now_ms = clock_.TimeInMilliseconds();
201     IncomingPacket(0, 1000, now_ms, 90 * send_time_ms,
202                    AbsSendTime(send_time_ms, 1000));
203   }
204 
205   bitrate_estimator_->Process();
206   EXPECT_TRUE(bitrate_observer_->updated());
207   EXPECT_GT(bitrate_observer_->latest_bitrate(), 800000u);
208 }
209 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionFasterArrival)210 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestProbeDetectionFasterArrival) {
211   const int kProbeLength = 5;
212   int64_t now_ms = clock_.TimeInMilliseconds();
213   // First burst sent at 8 * 1000 / 10 = 800 kbps.
214   // Arriving at 8 * 1000 / 5 = 1600 kbps.
215   int64_t send_time_ms = 0;
216   for (int i = 0; i < kProbeLength; ++i) {
217     clock_.AdvanceTimeMilliseconds(1);
218     send_time_ms += 10;
219     now_ms = clock_.TimeInMilliseconds();
220     IncomingPacket(0, 1000, now_ms, 90 * send_time_ms,
221                    AbsSendTime(send_time_ms, 1000));
222   }
223 
224   bitrate_estimator_->Process();
225   EXPECT_FALSE(bitrate_observer_->updated());
226 }
227 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionSlowerArrival)228 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, TestProbeDetectionSlowerArrival) {
229   const int kProbeLength = 5;
230   int64_t now_ms = clock_.TimeInMilliseconds();
231   // First burst sent at 8 * 1000 / 5 = 1600 kbps.
232   // Arriving at 8 * 1000 / 7 = 1142 kbps.
233   int64_t send_time_ms = 0;
234   for (int i = 0; i < kProbeLength; ++i) {
235     clock_.AdvanceTimeMilliseconds(7);
236     send_time_ms += 5;
237     now_ms = clock_.TimeInMilliseconds();
238     IncomingPacket(0, 1000, now_ms, 90 * send_time_ms,
239                    AbsSendTime(send_time_ms, 1000));
240   }
241 
242   bitrate_estimator_->Process();
243   EXPECT_TRUE(bitrate_observer_->updated());
244   EXPECT_NEAR(bitrate_observer_->latest_bitrate(), 1140000, 10000);
245 }
246 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,TestProbeDetectionSlowerArrivalHighBitrate)247 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,
248        TestProbeDetectionSlowerArrivalHighBitrate) {
249   const int kProbeLength = 5;
250   int64_t now_ms = clock_.TimeInMilliseconds();
251   // Burst sent at 8 * 1000 / 1 = 8000 kbps.
252   // Arriving at 8 * 1000 / 2 = 4000 kbps.
253   int64_t send_time_ms = 0;
254   for (int i = 0; i < kProbeLength; ++i) {
255     clock_.AdvanceTimeMilliseconds(2);
256     send_time_ms += 1;
257     now_ms = clock_.TimeInMilliseconds();
258     IncomingPacket(0, 1000, now_ms, 90 * send_time_ms,
259                    AbsSendTime(send_time_ms, 1000));
260   }
261 
262   bitrate_estimator_->Process();
263   EXPECT_TRUE(bitrate_observer_->updated());
264   EXPECT_NEAR(bitrate_observer_->latest_bitrate(), 4000000u, 10000);
265 }
266 
TEST_F(RemoteBitrateEstimatorAbsSendTimeTest,ProbingIgnoresSmallPackets)267 TEST_F(RemoteBitrateEstimatorAbsSendTimeTest, ProbingIgnoresSmallPackets) {
268   const int kProbeLength = 5;
269   int64_t now_ms = clock_.TimeInMilliseconds();
270   // Probing with 200 bytes every 10 ms, should be ignored by the probe
271   // detection.
272   for (int i = 0; i < kProbeLength; ++i) {
273     clock_.AdvanceTimeMilliseconds(10);
274     now_ms = clock_.TimeInMilliseconds();
275     IncomingPacket(0, 200, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
276   }
277 
278   bitrate_estimator_->Process();
279   EXPECT_FALSE(bitrate_observer_->updated());
280 
281   // Followed by a probe with 1000 bytes packets, should be detected as a
282   // probe.
283   for (int i = 0; i < kProbeLength; ++i) {
284     clock_.AdvanceTimeMilliseconds(10);
285     now_ms = clock_.TimeInMilliseconds();
286     IncomingPacket(0, 1000, now_ms, 90 * now_ms, AbsSendTime(now_ms, 1000));
287   }
288 
289   // Wait long enough so that we can call Process again.
290   clock_.AdvanceTimeMilliseconds(1000);
291 
292   bitrate_estimator_->Process();
293   EXPECT_TRUE(bitrate_observer_->updated());
294   EXPECT_NEAR(bitrate_observer_->latest_bitrate(), 800000u, 10000);
295 }
296 }  // namespace webrtc
297