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 #ifndef WEBRTC_BASE_BANDWIDTHSMOOTHER_H_
12 #define WEBRTC_BASE_BANDWIDTHSMOOTHER_H_
13 
14 #include "webrtc/base/rollingaccumulator.h"
15 #include "webrtc/base/timeutils.h"
16 
17 namespace rtc {
18 
19 // The purpose of BandwidthSmoother is to smooth out bandwidth
20 // estimations so that 'trstate' messages can be triggered when we
21 // are "sure" there is sufficient bandwidth.  To avoid frequent fluctuations,
22 // we take a slightly pessimistic view of our bandwidth.  We only increase
23 // our estimation when we have sampled bandwidth measurements of values
24 // at least as large as the current estimation * percent_increase
25 // for at least time_between_increase time.  If a sampled bandwidth
26 // is less than our current estimation we immediately decrease our estimation
27 // to that sampled value.
28 // We retain the initial bandwidth guess as our current bandwidth estimation
29 // until we have received (min_sample_count_percent * samples_count_to_average)
30 // number of samples. Min_sample_count_percent must be in range [0, 1].
31 class BandwidthSmoother {
32  public:
33   BandwidthSmoother(int initial_bandwidth_guess,
34                     uint32_t time_between_increase,
35                     double percent_increase,
36                     size_t samples_count_to_average,
37                     double min_sample_count_percent);
38   ~BandwidthSmoother();
39 
40   // Samples a new bandwidth measurement.
41   // bandwidth is expected to be non-negative.
42   // returns true if the bandwidth estimation changed
43   bool Sample(uint32_t sample_time, int bandwidth);
44 
get_bandwidth_estimation()45   int get_bandwidth_estimation() const {
46     return bandwidth_estimation_;
47   }
48 
49  private:
50   uint32_t time_between_increase_;
51   double percent_increase_;
52   uint32_t time_at_last_change_;
53   int bandwidth_estimation_;
54   RollingAccumulator<int> accumulator_;
55   double min_sample_count_percent_;
56 };
57 
58 }  // namespace rtc
59 
60 #endif  // WEBRTC_BASE_BANDWIDTHSMOOTHER_H_
61