xref: /aosp_15_r20/external/webrtc/modules/remote_bitrate_estimator/overuse_detector.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <math.h>
14 #include <stdio.h>
15 
16 #include <algorithm>
17 #include <string>
18 
19 #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/numerics/safe_minmax.h"
22 
23 namespace webrtc {
24 
25 const double kMaxAdaptOffsetMs = 15.0;
26 const double kOverUsingTimeThreshold = 10;
27 const int kMaxNumDeltas = 60;
28 
OveruseDetector(const FieldTrialsView * key_value_config)29 OveruseDetector::OveruseDetector(const FieldTrialsView* key_value_config)
30     // Experiment is on by default, but can be disabled with finch by setting
31     // the field trial string to "WebRTC-AdaptiveBweThreshold/Disabled/".
32     : k_up_(0.0087),
33       k_down_(0.039),
34       overusing_time_threshold_(kOverUsingTimeThreshold),
35       threshold_(12.5),
36       last_update_ms_(-1),
37       prev_offset_(0.0),
38       time_over_using_(-1),
39       overuse_counter_(0),
40       hypothesis_(BandwidthUsage::kBwNormal) {}
41 
~OveruseDetector()42 OveruseDetector::~OveruseDetector() {}
43 
State() const44 BandwidthUsage OveruseDetector::State() const {
45   return hypothesis_;
46 }
47 
Detect(double offset,double ts_delta,int num_of_deltas,int64_t now_ms)48 BandwidthUsage OveruseDetector::Detect(double offset,
49                                        double ts_delta,
50                                        int num_of_deltas,
51                                        int64_t now_ms) {
52   if (num_of_deltas < 2) {
53     return BandwidthUsage::kBwNormal;
54   }
55   const double T = std::min(num_of_deltas, kMaxNumDeltas) * offset;
56   BWE_TEST_LOGGING_PLOT(1, "T", now_ms, T);
57   BWE_TEST_LOGGING_PLOT(1, "threshold", now_ms, threshold_);
58   if (T > threshold_) {
59     if (time_over_using_ == -1) {
60       // Initialize the timer. Assume that we've been
61       // over-using half of the time since the previous
62       // sample.
63       time_over_using_ = ts_delta / 2;
64     } else {
65       // Increment timer
66       time_over_using_ += ts_delta;
67     }
68     overuse_counter_++;
69     if (time_over_using_ > overusing_time_threshold_ && overuse_counter_ > 1) {
70       if (offset >= prev_offset_) {
71         time_over_using_ = 0;
72         overuse_counter_ = 0;
73         hypothesis_ = BandwidthUsage::kBwOverusing;
74       }
75     }
76   } else if (T < -threshold_) {
77     time_over_using_ = -1;
78     overuse_counter_ = 0;
79     hypothesis_ = BandwidthUsage::kBwUnderusing;
80   } else {
81     time_over_using_ = -1;
82     overuse_counter_ = 0;
83     hypothesis_ = BandwidthUsage::kBwNormal;
84   }
85   prev_offset_ = offset;
86 
87   UpdateThreshold(T, now_ms);
88 
89   return hypothesis_;
90 }
91 
UpdateThreshold(double modified_offset,int64_t now_ms)92 void OveruseDetector::UpdateThreshold(double modified_offset, int64_t now_ms) {
93   if (last_update_ms_ == -1)
94     last_update_ms_ = now_ms;
95 
96   if (fabs(modified_offset) > threshold_ + kMaxAdaptOffsetMs) {
97     // Avoid adapting the threshold to big latency spikes, caused e.g.,
98     // by a sudden capacity drop.
99     last_update_ms_ = now_ms;
100     return;
101   }
102 
103   const double k = fabs(modified_offset) < threshold_ ? k_down_ : k_up_;
104   const int64_t kMaxTimeDeltaMs = 100;
105   int64_t time_delta_ms = std::min(now_ms - last_update_ms_, kMaxTimeDeltaMs);
106   threshold_ += k * (fabs(modified_offset) - threshold_) * time_delta_ms;
107   threshold_ = rtc::SafeClamp(threshold_, 6.f, 600.f);
108   last_update_ms_ = now_ms;
109 }
110 
111 }  // namespace webrtc
112