• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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 <math.h>
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstdlib>
16 
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 #include "webrtc/base/random.h"
20 #include "webrtc/base/scoped_ptr.h"
21 #include "webrtc/common_types.h"
22 #include "webrtc/modules/remote_bitrate_estimator/inter_arrival.h"
23 #include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h"
24 #include "webrtc/modules/remote_bitrate_estimator/overuse_estimator.h"
25 #include "webrtc/modules/remote_bitrate_estimator/rate_statistics.h"
26 #include "webrtc/test/field_trial.h"
27 
28 namespace webrtc {
29 namespace testing {
30 
31 const double kRtpTimestampToMs = 1.0 / 90.0;
32 
33 class OveruseDetectorTest : public ::testing::Test {
34  public:
OveruseDetectorTest()35   OveruseDetectorTest()
36       : now_ms_(0),
37         receive_time_ms_(0),
38         rtp_timestamp_(10 * 90),
39         overuse_detector_(),
40         overuse_estimator_(new OveruseEstimator(options_)),
41         inter_arrival_(new InterArrival(5 * 90, kRtpTimestampToMs, true)),
42         random_(123456789) {}
43 
44  protected:
SetUp()45   void SetUp() override {
46     overuse_detector_.reset(new OveruseDetector(options_));
47   }
48 
Run100000Samples(int packets_per_frame,size_t packet_size,int mean_ms,int standard_deviation_ms)49   int Run100000Samples(int packets_per_frame, size_t packet_size, int mean_ms,
50                        int standard_deviation_ms) {
51     int unique_overuse = 0;
52     int last_overuse = -1;
53     for (int i = 0; i < 100000; ++i) {
54       for (int j = 0; j < packets_per_frame; ++j) {
55         UpdateDetector(rtp_timestamp_, receive_time_ms_, packet_size);
56       }
57       rtp_timestamp_ += mean_ms * 90;
58       now_ms_ += mean_ms;
59       receive_time_ms_ = std::max<int64_t>(
60           receive_time_ms_,
61           now_ms_ + static_cast<int64_t>(
62                         random_.Gaussian(0, standard_deviation_ms) + 0.5));
63       if (kBwOverusing == overuse_detector_->State()) {
64         if (last_overuse + 1 != i) {
65           unique_overuse++;
66         }
67         last_overuse = i;
68       }
69     }
70     return unique_overuse;
71   }
72 
RunUntilOveruse(int packets_per_frame,size_t packet_size,int mean_ms,int standard_deviation_ms,int drift_per_frame_ms)73   int RunUntilOveruse(int packets_per_frame, size_t packet_size, int mean_ms,
74                       int standard_deviation_ms, int drift_per_frame_ms) {
75     // Simulate a higher send pace, that is too high.
76     for (int i = 0; i < 1000; ++i) {
77       for (int j = 0; j < packets_per_frame; ++j) {
78         UpdateDetector(rtp_timestamp_, receive_time_ms_, packet_size);
79       }
80       rtp_timestamp_ += mean_ms * 90;
81       now_ms_ += mean_ms + drift_per_frame_ms;
82       receive_time_ms_ = std::max<int64_t>(
83           receive_time_ms_,
84           now_ms_ + static_cast<int64_t>(
85                         random_.Gaussian(0, standard_deviation_ms) + 0.5));
86       if (kBwOverusing == overuse_detector_->State()) {
87         return i + 1;
88       }
89     }
90     return -1;
91   }
92 
UpdateDetector(uint32_t rtp_timestamp,int64_t receive_time_ms,size_t packet_size)93   void UpdateDetector(uint32_t rtp_timestamp, int64_t receive_time_ms,
94                       size_t packet_size) {
95     uint32_t timestamp_delta;
96     int64_t time_delta;
97     int size_delta;
98     if (inter_arrival_->ComputeDeltas(rtp_timestamp,
99                                       receive_time_ms,
100                                       packet_size,
101                                       &timestamp_delta,
102                                       &time_delta,
103                                       &size_delta)) {
104       double timestamp_delta_ms = timestamp_delta / 90.0;
105       overuse_estimator_->Update(time_delta, timestamp_delta_ms, size_delta,
106                                  overuse_detector_->State());
107       overuse_detector_->Detect(
108           overuse_estimator_->offset(), timestamp_delta_ms,
109           overuse_estimator_->num_of_deltas(), receive_time_ms);
110     }
111   }
112 
113   int64_t now_ms_;
114   int64_t receive_time_ms_;
115   uint32_t rtp_timestamp_;
116   OverUseDetectorOptions options_;
117   rtc::scoped_ptr<OveruseDetector> overuse_detector_;
118   rtc::scoped_ptr<OveruseEstimator> overuse_estimator_;
119   rtc::scoped_ptr<InterArrival> inter_arrival_;
120   Random random_;
121 };
122 
TEST_F(OveruseDetectorTest,GaussianRandom)123 TEST_F(OveruseDetectorTest, GaussianRandom) {
124   int buckets[100];
125   memset(buckets, 0, sizeof(buckets));
126   for (int i = 0; i < 100000; ++i) {
127     int index = random_.Gaussian(49, 10);
128     if (index >= 0 && index < 100)
129       buckets[index]++;
130   }
131   for (int n = 0; n < 100; ++n) {
132     printf("Bucket n:%d, %d\n", n, buckets[n]);
133   }
134 }
135 
TEST_F(OveruseDetectorTest,SimpleNonOveruse30fps)136 TEST_F(OveruseDetectorTest, SimpleNonOveruse30fps) {
137   size_t packet_size = 1200;
138   uint32_t frame_duration_ms = 33;
139   uint32_t rtp_timestamp = 10 * 90;
140 
141   // No variance.
142   for (int i = 0; i < 1000; ++i) {
143     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
144     now_ms_ += frame_duration_ms;
145     rtp_timestamp += frame_duration_ms * 90;
146     EXPECT_EQ(kBwNormal, overuse_detector_->State());
147   }
148 }
149 
150 // Roughly 1 Mbit/s
TEST_F(OveruseDetectorTest,SimpleNonOveruseWithReceiveVariance)151 TEST_F(OveruseDetectorTest, SimpleNonOveruseWithReceiveVariance) {
152   uint32_t frame_duration_ms = 10;
153   uint32_t rtp_timestamp = 10 * 90;
154   size_t packet_size = 1200;
155 
156   for (int i = 0; i < 1000; ++i) {
157     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
158     rtp_timestamp += frame_duration_ms * 90;
159     if (i % 2) {
160       now_ms_ += frame_duration_ms - 5;
161     } else {
162       now_ms_ += frame_duration_ms + 5;
163     }
164     EXPECT_EQ(kBwNormal, overuse_detector_->State());
165   }
166 }
167 
TEST_F(OveruseDetectorTest,SimpleNonOveruseWithRtpTimestampVariance)168 TEST_F(OveruseDetectorTest, SimpleNonOveruseWithRtpTimestampVariance) {
169   // Roughly 1 Mbit/s.
170   uint32_t frame_duration_ms = 10;
171   uint32_t rtp_timestamp = 10 * 90;
172   size_t packet_size = 1200;
173 
174   for (int i = 0; i < 1000; ++i) {
175     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
176     now_ms_ += frame_duration_ms;
177     if (i % 2) {
178       rtp_timestamp += (frame_duration_ms - 5) * 90;
179     } else {
180       rtp_timestamp += (frame_duration_ms + 5) * 90;
181     }
182     EXPECT_EQ(kBwNormal, overuse_detector_->State());
183   }
184 }
185 
TEST_F(OveruseDetectorTest,SimpleOveruse2000Kbit30fps)186 TEST_F(OveruseDetectorTest, SimpleOveruse2000Kbit30fps) {
187   size_t packet_size = 1200;
188   int packets_per_frame = 6;
189   int frame_duration_ms = 33;
190   int drift_per_frame_ms = 1;
191   int sigma_ms = 0;  // No variance.
192   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
193                                         frame_duration_ms, sigma_ms);
194 
195   EXPECT_EQ(0, unique_overuse);
196   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
197       frame_duration_ms, sigma_ms, drift_per_frame_ms);
198   EXPECT_EQ(8, frames_until_overuse);
199 }
200 
TEST_F(OveruseDetectorTest,SimpleOveruse100kbit10fps)201 TEST_F(OveruseDetectorTest, SimpleOveruse100kbit10fps) {
202   size_t packet_size = 1200;
203   int packets_per_frame = 1;
204   int frame_duration_ms = 100;
205   int drift_per_frame_ms = 1;
206   int sigma_ms = 0;  // No variance.
207   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
208                                         frame_duration_ms, sigma_ms);
209 
210   EXPECT_EQ(0, unique_overuse);
211   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
212       frame_duration_ms, sigma_ms, drift_per_frame_ms);
213   EXPECT_EQ(6, frames_until_overuse);
214 }
215 
TEST_F(OveruseDetectorTest,DISABLED_OveruseWithHighVariance100Kbit10fps)216 TEST_F(OveruseDetectorTest, DISABLED_OveruseWithHighVariance100Kbit10fps) {
217   uint32_t frame_duration_ms = 100;
218   uint32_t drift_per_frame_ms = 10;
219   uint32_t rtp_timestamp = frame_duration_ms * 90;
220   size_t packet_size = 1200;
221   int offset = 10;
222 
223   // Run 1000 samples to reach steady state.
224   for (int i = 0; i < 1000; ++i) {
225     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
226     rtp_timestamp += frame_duration_ms * 90;
227     if (i % 2) {
228       offset = random_.Rand(0, 49);
229       now_ms_ += frame_duration_ms - offset;
230     } else {
231       now_ms_ += frame_duration_ms + offset;
232     }
233     EXPECT_EQ(kBwNormal, overuse_detector_->State());
234   }
235   // Simulate a higher send pace, that is too high.
236   // Above noise generate a standard deviation of approximately 28 ms.
237   // Total build up of 150 ms.
238   for (int j = 0; j < 15; ++j) {
239     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
240     now_ms_ += frame_duration_ms + drift_per_frame_ms;
241     rtp_timestamp += frame_duration_ms * 90;
242     EXPECT_EQ(kBwNormal, overuse_detector_->State());
243   }
244   UpdateDetector(rtp_timestamp, now_ms_, packet_size);
245   EXPECT_EQ(kBwOverusing, overuse_detector_->State());
246 }
247 
TEST_F(OveruseDetectorTest,DISABLED_OveruseWithLowVariance100Kbit10fps)248 TEST_F(OveruseDetectorTest, DISABLED_OveruseWithLowVariance100Kbit10fps) {
249   uint32_t frame_duration_ms = 100;
250   uint32_t drift_per_frame_ms = 1;
251   uint32_t rtp_timestamp = frame_duration_ms * 90;
252   size_t packet_size = 1200;
253   int offset = 10;
254 
255   // Run 1000 samples to reach steady state.
256   for (int i = 0; i < 1000; ++i) {
257     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
258     rtp_timestamp += frame_duration_ms * 90;
259     if (i % 2) {
260       offset = random_.Rand(0, 1);
261       now_ms_ += frame_duration_ms - offset;
262     } else {
263       now_ms_ += frame_duration_ms + offset;
264     }
265     EXPECT_EQ(kBwNormal, overuse_detector_->State());
266   }
267   // Simulate a higher send pace, that is too high.
268   // Total build up of 6 ms.
269   for (int j = 0; j < 6; ++j) {
270     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
271     now_ms_ += frame_duration_ms + drift_per_frame_ms;
272     rtp_timestamp += frame_duration_ms * 90;
273     EXPECT_EQ(kBwNormal, overuse_detector_->State());
274   }
275   UpdateDetector(rtp_timestamp, now_ms_, packet_size);
276   EXPECT_EQ(kBwOverusing, overuse_detector_->State());
277 }
278 
TEST_F(OveruseDetectorTest,OveruseWithLowVariance2000Kbit30fps)279 TEST_F(OveruseDetectorTest, OveruseWithLowVariance2000Kbit30fps) {
280   uint32_t frame_duration_ms = 33;
281   uint32_t drift_per_frame_ms = 1;
282   uint32_t rtp_timestamp = frame_duration_ms * 90;
283   size_t packet_size = 1200;
284   int offset = 0;
285 
286   // Run 1000 samples to reach steady state.
287   for (int i = 0; i < 1000; ++i) {
288     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
289     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
290     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
291     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
292     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
293     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
294     rtp_timestamp += frame_duration_ms * 90;
295     if (i % 2) {
296       offset = random_.Rand(0, 1);
297       now_ms_ += frame_duration_ms - offset;
298     } else {
299       now_ms_ += frame_duration_ms + offset;
300     }
301     EXPECT_EQ(kBwNormal, overuse_detector_->State());
302   }
303   // Simulate a higher send pace, that is too high.
304   // Total build up of 30 ms.
305   for (int j = 0; j < 5; ++j) {
306     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
307     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
308     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
309     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
310     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
311     UpdateDetector(rtp_timestamp, now_ms_, packet_size);
312     now_ms_ += frame_duration_ms + drift_per_frame_ms * 6;
313     rtp_timestamp += frame_duration_ms * 90;
314     EXPECT_EQ(kBwNormal, overuse_detector_->State());
315   }
316   UpdateDetector(rtp_timestamp, now_ms_, packet_size);
317   EXPECT_EQ(kBwOverusing, overuse_detector_->State());
318 }
319 
320 #if defined(WEBRTC_ANDROID)
321 #define MAYBE_LowGaussianVariance30Kbit3fps \
322   DISABLED_LowGaussianVariance30Kbit3fps
323 #else
324 #define MAYBE_LowGaussianVariance30Kbit3fps LowGaussianVariance30Kbit3fps
325 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance30Kbit3fps)326 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance30Kbit3fps) {
327   size_t packet_size = 1200;
328   int packets_per_frame = 1;
329   int frame_duration_ms = 333;
330   int drift_per_frame_ms = 1;
331   int sigma_ms = 3;
332   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
333                                         frame_duration_ms, sigma_ms);
334   EXPECT_EQ(1, unique_overuse);
335   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
336       frame_duration_ms, sigma_ms, drift_per_frame_ms);
337   EXPECT_EQ(13, frames_until_overuse);
338 }
339 
TEST_F(OveruseDetectorTest,LowGaussianVarianceFastDrift30Kbit3fps)340 TEST_F(OveruseDetectorTest, LowGaussianVarianceFastDrift30Kbit3fps) {
341   size_t packet_size = 1200;
342   int packets_per_frame = 1;
343   int frame_duration_ms = 333;
344   int drift_per_frame_ms = 100;
345   int sigma_ms = 3;
346   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
347                                         frame_duration_ms, sigma_ms);
348   EXPECT_EQ(1, unique_overuse);
349   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
350       frame_duration_ms, sigma_ms, drift_per_frame_ms);
351   EXPECT_EQ(4, frames_until_overuse);
352 }
353 
TEST_F(OveruseDetectorTest,HighGaussianVariance30Kbit3fps)354 TEST_F(OveruseDetectorTest, HighGaussianVariance30Kbit3fps) {
355   size_t packet_size = 1200;
356   int packets_per_frame = 1;
357   int frame_duration_ms = 333;
358   int drift_per_frame_ms = 1;
359   int sigma_ms = 10;
360   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
361                                         frame_duration_ms, sigma_ms);
362   EXPECT_EQ(1, unique_overuse);
363   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
364       frame_duration_ms, sigma_ms, drift_per_frame_ms);
365   EXPECT_EQ(32, frames_until_overuse);
366 }
367 
TEST_F(OveruseDetectorTest,HighGaussianVarianceFastDrift30Kbit3fps)368 TEST_F(OveruseDetectorTest, HighGaussianVarianceFastDrift30Kbit3fps) {
369   size_t packet_size = 1200;
370   int packets_per_frame = 1;
371   int frame_duration_ms = 333;
372   int drift_per_frame_ms = 100;
373   int sigma_ms = 10;
374   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
375                                         frame_duration_ms, sigma_ms);
376   EXPECT_EQ(1, unique_overuse);
377   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
378       frame_duration_ms, sigma_ms, drift_per_frame_ms);
379   EXPECT_EQ(4, frames_until_overuse);
380 }
381 
382 #if defined(WEBRTC_ANDROID)
383 #define MAYBE_LowGaussianVariance100Kbit5fps \
384   DISABLED_LowGaussianVariance100Kbit5fps
385 #else
386 #define MAYBE_LowGaussianVariance100Kbit5fps LowGaussianVariance100Kbit5fps
387 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance100Kbit5fps)388 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance100Kbit5fps) {
389   size_t packet_size = 1200;
390   int packets_per_frame = 2;
391   int frame_duration_ms = 200;
392   int drift_per_frame_ms = 1;
393   int sigma_ms = 3;
394   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
395                                         frame_duration_ms, sigma_ms);
396   EXPECT_EQ(0, unique_overuse);
397   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
398       frame_duration_ms, sigma_ms, drift_per_frame_ms);
399   EXPECT_EQ(13, frames_until_overuse);
400 }
401 
402 #if defined(WEBRTC_ANDROID)
403 #define MAYBE_HighGaussianVariance100Kbit5fps \
404   DISABLED_HighGaussianVariance100Kbit5fps
405 #else
406 #define MAYBE_HighGaussianVariance100Kbit5fps HighGaussianVariance100Kbit5fps
407 #endif
TEST_F(OveruseDetectorTest,MAYBE_HighGaussianVariance100Kbit5fps)408 TEST_F(OveruseDetectorTest, MAYBE_HighGaussianVariance100Kbit5fps) {
409   size_t packet_size = 1200;
410   int packets_per_frame = 2;
411   int frame_duration_ms = 200;
412   int drift_per_frame_ms = 1;
413   int sigma_ms = 10;
414   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
415                                         frame_duration_ms, sigma_ms);
416   EXPECT_EQ(1, unique_overuse);
417   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
418       frame_duration_ms, sigma_ms, drift_per_frame_ms);
419   EXPECT_EQ(32, frames_until_overuse);
420 }
421 
422 #if defined(WEBRTC_ANDROID)
423 #define MAYBE_LowGaussianVariance100Kbit10fps \
424   DISABLED_LowGaussianVariance100Kbit10fps
425 #else
426 #define MAYBE_LowGaussianVariance100Kbit10fps LowGaussianVariance100Kbit10fps
427 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance100Kbit10fps)428 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance100Kbit10fps) {
429   size_t packet_size = 1200;
430   int packets_per_frame = 1;
431   int frame_duration_ms = 100;
432   int drift_per_frame_ms = 1;
433   int sigma_ms = 3;
434   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
435                                         frame_duration_ms, sigma_ms);
436   EXPECT_EQ(1, unique_overuse);
437   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
438       frame_duration_ms, sigma_ms, drift_per_frame_ms);
439   EXPECT_EQ(13, frames_until_overuse);
440 }
441 
442 #if defined(WEBRTC_ANDROID)
443 #define MAYBE_HighGaussianVariance100Kbit10fps \
444   DISABLED_HighGaussianVariance100Kbit10fps
445 #else
446 #define MAYBE_HighGaussianVariance100Kbit10fps HighGaussianVariance100Kbit10fps
447 #endif
TEST_F(OveruseDetectorTest,MAYBE_HighGaussianVariance100Kbit10fps)448 TEST_F(OveruseDetectorTest, MAYBE_HighGaussianVariance100Kbit10fps) {
449   size_t packet_size = 1200;
450   int packets_per_frame = 1;
451   int frame_duration_ms = 100;
452   int drift_per_frame_ms = 1;
453   int sigma_ms = 10;
454   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
455                                         frame_duration_ms, sigma_ms);
456   EXPECT_EQ(0, unique_overuse);
457   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
458       frame_duration_ms, sigma_ms, drift_per_frame_ms);
459   EXPECT_EQ(32, frames_until_overuse);
460 }
461 
462 #if defined(WEBRTC_ANDROID)
463 #define MAYBE_LowGaussianVariance300Kbit30fps \
464   DISABLED_LowGaussianVariance300Kbit30fps
465 #else
466 #define MAYBE_LowGaussianVariance300Kbit30fps LowGaussianVariance300Kbit30fps
467 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance300Kbit30fps)468 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance300Kbit30fps) {
469   size_t packet_size = 1200;
470   int packets_per_frame = 1;
471   int frame_duration_ms = 33;
472   int drift_per_frame_ms = 1;
473   int sigma_ms = 3;
474   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
475                                         frame_duration_ms, sigma_ms);
476   EXPECT_EQ(0, unique_overuse);
477   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
478       frame_duration_ms, sigma_ms, drift_per_frame_ms);
479   EXPECT_EQ(15, frames_until_overuse);
480 }
481 
TEST_F(OveruseDetectorTest,LowGaussianVarianceFastDrift300Kbit30fps)482 TEST_F(OveruseDetectorTest, LowGaussianVarianceFastDrift300Kbit30fps) {
483   size_t packet_size = 1200;
484   int packets_per_frame = 1;
485   int frame_duration_ms = 33;
486   int drift_per_frame_ms = 10;
487   int sigma_ms = 3;
488   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
489                                         frame_duration_ms, sigma_ms);
490   EXPECT_EQ(0, unique_overuse);
491   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
492       frame_duration_ms, sigma_ms, drift_per_frame_ms);
493   EXPECT_EQ(6, frames_until_overuse);
494 }
495 
TEST_F(OveruseDetectorTest,HighGaussianVariance300Kbit30fps)496 TEST_F(OveruseDetectorTest, HighGaussianVariance300Kbit30fps) {
497   size_t packet_size = 1200;
498   int packets_per_frame = 1;
499   int frame_duration_ms = 33;
500   int drift_per_frame_ms = 1;
501   int sigma_ms = 10;
502   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
503                                         frame_duration_ms, sigma_ms);
504   EXPECT_EQ(0, unique_overuse);
505   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
506       frame_duration_ms, sigma_ms, drift_per_frame_ms);
507   EXPECT_EQ(41, frames_until_overuse);
508 }
509 
TEST_F(OveruseDetectorTest,HighGaussianVarianceFastDrift300Kbit30fps)510 TEST_F(OveruseDetectorTest, HighGaussianVarianceFastDrift300Kbit30fps) {
511   size_t packet_size = 1200;
512   int packets_per_frame = 1;
513   int frame_duration_ms = 33;
514   int drift_per_frame_ms = 10;
515   int sigma_ms = 10;
516   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
517                                         frame_duration_ms, sigma_ms);
518   EXPECT_EQ(0, unique_overuse);
519   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
520       frame_duration_ms, sigma_ms, drift_per_frame_ms);
521   EXPECT_EQ(10, frames_until_overuse);
522 }
523 
524 #if defined(WEBRTC_ANDROID)
525 #define MAYBE_LowGaussianVariance1000Kbit30fps \
526   DISABLED_LowGaussianVariance1000Kbit30fps
527 #else
528 #define MAYBE_LowGaussianVariance1000Kbit30fps LowGaussianVariance1000Kbit30fps
529 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance1000Kbit30fps)530 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance1000Kbit30fps) {
531   size_t packet_size = 1200;
532   int packets_per_frame = 3;
533   int frame_duration_ms = 33;
534   int drift_per_frame_ms = 1;
535   int sigma_ms = 3;
536   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
537                                         frame_duration_ms, sigma_ms);
538   EXPECT_EQ(0, unique_overuse);
539   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
540       frame_duration_ms, sigma_ms, drift_per_frame_ms);
541   EXPECT_EQ(15, frames_until_overuse);
542 }
543 
TEST_F(OveruseDetectorTest,LowGaussianVarianceFastDrift1000Kbit30fps)544 TEST_F(OveruseDetectorTest, LowGaussianVarianceFastDrift1000Kbit30fps) {
545   size_t packet_size = 1200;
546   int packets_per_frame = 3;
547   int frame_duration_ms = 33;
548   int drift_per_frame_ms = 10;
549   int sigma_ms = 3;
550   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
551                                         frame_duration_ms, sigma_ms);
552   EXPECT_EQ(0, unique_overuse);
553   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
554       frame_duration_ms, sigma_ms, drift_per_frame_ms);
555   EXPECT_EQ(6, frames_until_overuse);
556 }
557 
TEST_F(OveruseDetectorTest,HighGaussianVariance1000Kbit30fps)558 TEST_F(OveruseDetectorTest, HighGaussianVariance1000Kbit30fps) {
559   size_t packet_size = 1200;
560   int packets_per_frame = 3;
561   int frame_duration_ms = 33;
562   int drift_per_frame_ms = 1;
563   int sigma_ms = 10;
564   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
565                                         frame_duration_ms, sigma_ms);
566   EXPECT_EQ(0, unique_overuse);
567   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
568       frame_duration_ms, sigma_ms, drift_per_frame_ms);
569   EXPECT_EQ(41, frames_until_overuse);
570 }
571 
TEST_F(OveruseDetectorTest,HighGaussianVarianceFastDrift1000Kbit30fps)572 TEST_F(OveruseDetectorTest, HighGaussianVarianceFastDrift1000Kbit30fps) {
573   size_t packet_size = 1200;
574   int packets_per_frame = 3;
575   int frame_duration_ms = 33;
576   int drift_per_frame_ms = 10;
577   int sigma_ms = 10;
578   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
579                                         frame_duration_ms, sigma_ms);
580   EXPECT_EQ(0, unique_overuse);
581   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
582       frame_duration_ms, sigma_ms, drift_per_frame_ms);
583   EXPECT_EQ(10, frames_until_overuse);
584 }
585 
586 #if defined(WEBRTC_ANDROID)
587 #define MAYBE_LowGaussianVariance2000Kbit30fps \
588   DISABLED_LowGaussianVariance2000Kbit30fps
589 #else
590 #define MAYBE_LowGaussianVariance2000Kbit30fps LowGaussianVariance2000Kbit30fps
591 #endif
TEST_F(OveruseDetectorTest,MAYBE_LowGaussianVariance2000Kbit30fps)592 TEST_F(OveruseDetectorTest, MAYBE_LowGaussianVariance2000Kbit30fps) {
593   size_t packet_size = 1200;
594   int packets_per_frame = 6;
595   int frame_duration_ms = 33;
596   int drift_per_frame_ms = 1;
597   int sigma_ms = 3;
598   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
599                                         frame_duration_ms, sigma_ms);
600   EXPECT_EQ(0, unique_overuse);
601   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
602       frame_duration_ms, sigma_ms, drift_per_frame_ms);
603   EXPECT_EQ(15, frames_until_overuse);
604 }
605 
TEST_F(OveruseDetectorTest,LowGaussianVarianceFastDrift2000Kbit30fps)606 TEST_F(OveruseDetectorTest, LowGaussianVarianceFastDrift2000Kbit30fps) {
607   size_t packet_size = 1200;
608   int packets_per_frame = 6;
609   int frame_duration_ms = 33;
610   int drift_per_frame_ms = 10;
611   int sigma_ms = 3;
612   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
613                                         frame_duration_ms, sigma_ms);
614   EXPECT_EQ(0, unique_overuse);
615   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
616       frame_duration_ms, sigma_ms, drift_per_frame_ms);
617   EXPECT_EQ(6, frames_until_overuse);
618 }
619 
TEST_F(OveruseDetectorTest,HighGaussianVariance2000Kbit30fps)620 TEST_F(OveruseDetectorTest, HighGaussianVariance2000Kbit30fps) {
621   size_t packet_size = 1200;
622   int packets_per_frame = 6;
623   int frame_duration_ms = 33;
624   int drift_per_frame_ms = 1;
625   int sigma_ms = 10;
626   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
627                                         frame_duration_ms, sigma_ms);
628   EXPECT_EQ(0, unique_overuse);
629   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
630       frame_duration_ms, sigma_ms, drift_per_frame_ms);
631   EXPECT_EQ(41, frames_until_overuse);
632 }
633 
TEST_F(OveruseDetectorTest,HighGaussianVarianceFastDrift2000Kbit30fps)634 TEST_F(OveruseDetectorTest, HighGaussianVarianceFastDrift2000Kbit30fps) {
635   size_t packet_size = 1200;
636   int packets_per_frame = 6;
637   int frame_duration_ms = 33;
638   int drift_per_frame_ms = 10;
639   int sigma_ms = 10;
640   int unique_overuse = Run100000Samples(packets_per_frame, packet_size,
641                                         frame_duration_ms, sigma_ms);
642   EXPECT_EQ(0, unique_overuse);
643   int frames_until_overuse = RunUntilOveruse(packets_per_frame, packet_size,
644       frame_duration_ms, sigma_ms, drift_per_frame_ms);
645   EXPECT_EQ(10, frames_until_overuse);
646 }
647 
648 class OveruseDetectorExperimentTest : public OveruseDetectorTest {
649  public:
OveruseDetectorExperimentTest()650   OveruseDetectorExperimentTest()
651       : override_field_trials_(
652             "WebRTC-AdaptiveBweThreshold/Enabled-0.01,0.00018/") {}
653 
654  protected:
SetUp()655   void SetUp() override {
656     overuse_detector_.reset(new OveruseDetector(options_));
657   }
658 
659   test::ScopedFieldTrials override_field_trials_;
660 };
661 
TEST_F(OveruseDetectorExperimentTest,ThresholdAdapts)662 TEST_F(OveruseDetectorExperimentTest, ThresholdAdapts) {
663   const double kOffset = 0.21;
664   double kTsDelta = 3000.0;
665   int64_t now_ms = 0;
666   int num_deltas = 60;
667   const int kBatchLength = 10;
668 
669   // Pass in a positive offset and verify it triggers overuse.
670   bool overuse_detected = false;
671   for (int i = 0; i < kBatchLength; ++i) {
672     BandwidthUsage overuse_state =
673         overuse_detector_->Detect(kOffset, kTsDelta, num_deltas, now_ms);
674     if (overuse_state == kBwOverusing) {
675       overuse_detected = true;
676     }
677     ++num_deltas;
678     now_ms += 5;
679   }
680   EXPECT_TRUE(overuse_detected);
681 
682   // Force the threshold to increase by passing in a higher offset.
683   overuse_detected = false;
684   for (int i = 0; i < kBatchLength; ++i) {
685     BandwidthUsage overuse_state =
686         overuse_detector_->Detect(1.1 * kOffset, kTsDelta, num_deltas, now_ms);
687     if (overuse_state == kBwOverusing) {
688       overuse_detected = true;
689     }
690     ++num_deltas;
691     now_ms += 5;
692   }
693   EXPECT_TRUE(overuse_detected);
694 
695   // Verify that the same offset as before no longer triggers overuse.
696   overuse_detected = false;
697   for (int i = 0; i < kBatchLength; ++i) {
698     BandwidthUsage overuse_state =
699         overuse_detector_->Detect(kOffset, kTsDelta, num_deltas, now_ms);
700     if (overuse_state == kBwOverusing) {
701       overuse_detected = true;
702     }
703     ++num_deltas;
704     now_ms += 5;
705   }
706   EXPECT_FALSE(overuse_detected);
707 
708   // Pass in a low offset to make the threshold adapt down.
709   for (int i = 0; i < 15 * kBatchLength; ++i) {
710     BandwidthUsage overuse_state =
711         overuse_detector_->Detect(0.7 * kOffset, kTsDelta, num_deltas, now_ms);
712     if (overuse_state == kBwOverusing) {
713       overuse_detected = true;
714     }
715     ++num_deltas;
716     now_ms += 5;
717   }
718   EXPECT_FALSE(overuse_detected);
719 
720   // Make sure the original offset now again triggers overuse.
721   for (int i = 0; i < kBatchLength; ++i) {
722     BandwidthUsage overuse_state =
723         overuse_detector_->Detect(kOffset, kTsDelta, num_deltas, now_ms);
724     if (overuse_state == kBwOverusing) {
725       overuse_detected = true;
726     }
727     ++num_deltas;
728     now_ms += 5;
729   }
730   EXPECT_TRUE(overuse_detected);
731 }
732 
TEST_F(OveruseDetectorExperimentTest,DoesntAdaptToSpikes)733 TEST_F(OveruseDetectorExperimentTest, DoesntAdaptToSpikes) {
734   const double kOffset = 1.0;
735   const double kLargeOffset = 20.0;
736   double kTsDelta = 3000.0;
737   int64_t now_ms = 0;
738   int num_deltas = 60;
739   const int kBatchLength = 10;
740   const int kShortBatchLength = 3;
741 
742   // Pass in a positive offset and verify it triggers overuse.
743   bool overuse_detected = false;
744   for (int i = 0; i < kBatchLength; ++i) {
745     BandwidthUsage overuse_state =
746         overuse_detector_->Detect(kOffset, kTsDelta, num_deltas, now_ms);
747     if (overuse_state == kBwOverusing) {
748       overuse_detected = true;
749     }
750     ++num_deltas;
751     now_ms += 5;
752   }
753 
754   // Pass in a large offset. This shouldn't have a too big impact on the
755   // threshold, but still trigger an overuse.
756   now_ms += 100;
757   overuse_detected = false;
758   for (int i = 0; i < kShortBatchLength; ++i) {
759     BandwidthUsage overuse_state =
760         overuse_detector_->Detect(kLargeOffset, kTsDelta, num_deltas, now_ms);
761     if (overuse_state == kBwOverusing) {
762       overuse_detected = true;
763     }
764     ++num_deltas;
765     now_ms += 5;
766   }
767   EXPECT_TRUE(overuse_detected);
768 
769   // Pass in a positive normal offset and verify it still triggers.
770   overuse_detected = false;
771   for (int i = 0; i < kBatchLength; ++i) {
772     BandwidthUsage overuse_state =
773         overuse_detector_->Detect(kOffset, kTsDelta, num_deltas, now_ms);
774     if (overuse_state == kBwOverusing) {
775       overuse_detected = true;
776     }
777     ++num_deltas;
778     now_ms += 5;
779   }
780   EXPECT_TRUE(overuse_detected);
781 }
782 }  // namespace testing
783 }  // namespace webrtc
784