1/*
2 *  Copyright 2011 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/bandwidthsmoother.h"
12
13#include <limits.h>
14
15namespace rtc {
16
17BandwidthSmoother::BandwidthSmoother(int initial_bandwidth_guess,
18                                     uint32 time_between_increase,
19                                     double percent_increase,
20                                     size_t samples_count_to_average,
21                                     double min_sample_count_percent)
22    : time_between_increase_(time_between_increase),
23      percent_increase_(rtc::_max(1.0, percent_increase)),
24      time_at_last_change_(0),
25      bandwidth_estimation_(initial_bandwidth_guess),
26      accumulator_(samples_count_to_average),
27      min_sample_count_percent_(
28          rtc::_min(1.0,
29                          rtc::_max(0.0, min_sample_count_percent))) {
30}
31
32// Samples a new bandwidth measurement
33// returns true if the bandwidth estimation changed
34bool BandwidthSmoother::Sample(uint32 sample_time, int bandwidth) {
35  if (bandwidth < 0) {
36    return false;
37  }
38
39  accumulator_.AddSample(bandwidth);
40
41  if (accumulator_.count() < static_cast<size_t>(
42          accumulator_.max_count() * min_sample_count_percent_)) {
43    // We have not collected enough samples yet.
44    return false;
45  }
46
47  // Replace bandwidth with the mean of sampled bandwidths.
48  const int mean_bandwidth = static_cast<int>(accumulator_.ComputeMean());
49
50  if (mean_bandwidth < bandwidth_estimation_) {
51    time_at_last_change_ = sample_time;
52    bandwidth_estimation_ = mean_bandwidth;
53    return true;
54  }
55
56  const int old_bandwidth_estimation = bandwidth_estimation_;
57  const double increase_threshold_d = percent_increase_ * bandwidth_estimation_;
58  if (increase_threshold_d > INT_MAX) {
59    // If bandwidth goes any higher we would overflow.
60    return false;
61  }
62
63  const int increase_threshold = static_cast<int>(increase_threshold_d);
64  if (mean_bandwidth < increase_threshold) {
65    time_at_last_change_ = sample_time;
66    // The value of bandwidth_estimation remains the same if we don't exceed
67    // percent_increase_ * bandwidth_estimation_ for at least
68    // time_between_increase_ time.
69  } else if (sample_time >= time_at_last_change_ + time_between_increase_) {
70    time_at_last_change_ = sample_time;
71    if (increase_threshold == 0) {
72      // Bandwidth_estimation_ must be zero. Assume a jump from zero to a
73      // positive bandwidth means we have regained connectivity.
74      bandwidth_estimation_ = mean_bandwidth;
75    } else {
76      bandwidth_estimation_ = increase_threshold;
77    }
78  }
79  // Else don't make a change.
80
81  return old_bandwidth_estimation != bandwidth_estimation_;
82}
83
84}  // namespace rtc
85