blob: cf5a25dfa86ecac7ab6f7aa7566b11370b030ccb [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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,
34 uint32 time_between_increase,
35 double percent_increase,
36 size_t samples_count_to_average,
37 double min_sample_count_percent);
38
39 // Samples a new bandwidth measurement.
40 // bandwidth is expected to be non-negative.
41 // returns true if the bandwidth estimation changed
42 bool Sample(uint32 sample_time, int bandwidth);
43
44 int get_bandwidth_estimation() const {
45 return bandwidth_estimation_;
46 }
47
48 private:
49 uint32 time_between_increase_;
50 double percent_increase_;
51 uint32 time_at_last_change_;
52 int bandwidth_estimation_;
53 RollingAccumulator<int> accumulator_;
54 double min_sample_count_percent_;
55};
56
57} // namespace rtc
58
59#endif // WEBRTC_BASE_BANDWIDTHSMOOTHER_H_