tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

overuse_detector.cc (2961B)


      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 <algorithm>
     14 #include <cmath>
     15 #include <cstdint>
     16 
     17 #include "api/transport/bandwidth_usage.h"
     18 #include "rtc_base/numerics/safe_minmax.h"
     19 
     20 namespace webrtc {
     21 namespace {
     22 
     23 constexpr double kMaxAdaptOffsetMs = 15.0;
     24 constexpr double kOverUsingTimeThreshold = 10;
     25 constexpr int kMaxNumDeltas = 60;
     26 constexpr double kUp = 0.0087;
     27 constexpr double kDown = 0.039;
     28 
     29 }  // namespace
     30 
     31 OveruseDetector::OveruseDetector() = default;
     32 
     33 BandwidthUsage OveruseDetector::State() const {
     34  return hypothesis_;
     35 }
     36 
     37 BandwidthUsage OveruseDetector::Detect(double offset,
     38                                       double ts_delta,
     39                                       int num_of_deltas,
     40                                       int64_t now_ms) {
     41  if (num_of_deltas < 2) {
     42    return BandwidthUsage::kBwNormal;
     43  }
     44  const double T = std::min(num_of_deltas, kMaxNumDeltas) * offset;
     45  if (T > threshold_) {
     46    if (time_over_using_ == -1) {
     47      // Initialize the timer. Assume that we've been
     48      // over-using half of the time since the previous
     49      // sample.
     50      time_over_using_ = ts_delta / 2;
     51    } else {
     52      // Increment timer
     53      time_over_using_ += ts_delta;
     54    }
     55    overuse_counter_++;
     56    if (time_over_using_ > kOverUsingTimeThreshold && overuse_counter_ > 1) {
     57      if (offset >= prev_offset_) {
     58        time_over_using_ = 0;
     59        overuse_counter_ = 0;
     60        hypothesis_ = BandwidthUsage::kBwOverusing;
     61      }
     62    }
     63  } else if (T < -threshold_) {
     64    time_over_using_ = -1;
     65    overuse_counter_ = 0;
     66    hypothesis_ = BandwidthUsage::kBwUnderusing;
     67  } else {
     68    time_over_using_ = -1;
     69    overuse_counter_ = 0;
     70    hypothesis_ = BandwidthUsage::kBwNormal;
     71  }
     72  prev_offset_ = offset;
     73 
     74  UpdateThreshold(T, now_ms);
     75 
     76  return hypothesis_;
     77 }
     78 
     79 void OveruseDetector::UpdateThreshold(double modified_offset, int64_t now_ms) {
     80  if (last_update_ms_ == -1)
     81    last_update_ms_ = now_ms;
     82 
     83  if (fabs(modified_offset) > threshold_ + kMaxAdaptOffsetMs) {
     84    // Avoid adapting the threshold to big latency spikes, caused e.g.,
     85    // by a sudden capacity drop.
     86    last_update_ms_ = now_ms;
     87    return;
     88  }
     89 
     90  const double k = fabs(modified_offset) < threshold_ ? kDown : kUp;
     91  const int64_t kMaxTimeDeltaMs = 100;
     92  int64_t time_delta_ms = std::min(now_ms - last_update_ms_, kMaxTimeDeltaMs);
     93  threshold_ += k * (fabs(modified_offset) - threshold_) * time_delta_ms;
     94  threshold_ = SafeClamp(threshold_, 6.f, 600.f);
     95  last_update_ms_ = now_ms;
     96 }
     97 
     98 }  // namespace webrtc