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