blob: eae565ead3763b54ced0b777e1c7ad82721da94a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
17namespace 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].
31class BandwidthSmoother {
32 public:
33 BandwidthSmoother(int initial_bandwidth_guess,
Peter Boström0c4e06b2015-10-07 12:23:21 +020034 uint32_t time_between_increase,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 double percent_increase,
36 size_t samples_count_to_average,
37 double min_sample_count_percent);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000038 ~BandwidthSmoother();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
40 // Samples a new bandwidth measurement.
41 // bandwidth is expected to be non-negative.
42 // returns true if the bandwidth estimation changed
Peter Boström0c4e06b2015-10-07 12:23:21 +020043 bool Sample(uint32_t sample_time, int bandwidth);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
45 int get_bandwidth_estimation() const {
46 return bandwidth_estimation_;
47 }
48
49 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +020050 uint32_t time_between_increase_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 double percent_increase_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020052 uint32_t time_at_last_change_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 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_