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