blob: 3e913cc1bbc310ba9d6b122bf93ea499a592ebfc [file] [log] [blame]
sprang@webrtc.org37968a92013-12-03 10:31:59 +00001/*
2 * Copyright (c) 2013 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
tkchinf75d0082016-02-23 22:49:42 -080011#ifndef WEBRTC_BASE_RATE_STATISTICS_H_
12#define WEBRTC_BASE_RATE_STATISTICS_H_
sprang@webrtc.org37968a92013-12-03 10:31:59 +000013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
15
Erik Språng51e60302016-06-10 22:13:21 +020016#include "webrtc/base/optional.h"
sprang@webrtc.org37968a92013-12-03 10:31:59 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21class RateStatistics {
22 public:
Erik Språng51e60302016-06-10 22:13:21 +020023 // max_window_size_ms = Maximum window size in ms for the rate estimation.
24 // Initial window size is set to this, but may be changed
25 // to something lower by calling SetWindowSize().
sprang@webrtc.org37968a92013-12-03 10:31:59 +000026 // scale = coefficient to convert counts/ms to desired units,
27 // ex: if counts represents bytes, use 8*1000 to go to bits/s
Erik Språng51e60302016-06-10 22:13:21 +020028 RateStatistics(int64_t max_window_size_ms, float scale);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000029 ~RateStatistics();
30
31 void Reset();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000032 void Update(size_t count, int64_t now_ms);
Erik Språng51e60302016-06-10 22:13:21 +020033 rtc::Optional<uint32_t> Rate(int64_t now_ms);
34 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000035
36 private:
37 void EraseOld(int64_t now_ms);
Erik Språng51e60302016-06-10 22:13:21 +020038 bool IsInitialized();
sprang@webrtc.org37968a92013-12-03 10:31:59 +000039
40 // Counters are kept in buckets (circular buffer), with one bucket
41 // per millisecond.
Erik Språng51e60302016-06-10 22:13:21 +020042 struct Bucket {
43 size_t sum; // Sum of all samples in this bucket.
44 size_t samples; // Number of samples in this bucket.
45 };
46 std::unique_ptr<Bucket[]> buckets_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000047
48 // Total count recorded in buckets.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000049 size_t accumulated_count_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000050
Erik Språng51e60302016-06-10 22:13:21 +020051 // The total number of samples in the buckets.
52 size_t num_samples_;
53
sprang@webrtc.org37968a92013-12-03 10:31:59 +000054 // Oldest time recorded in buckets.
55 int64_t oldest_time_;
56
57 // Bucket index of oldest counter recorded in buckets.
Erik Språng51e60302016-06-10 22:13:21 +020058 uint32_t oldest_index_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000059
60 // To convert counts/ms to desired units
61 const float scale_;
Erik Språng51e60302016-06-10 22:13:21 +020062
63 // The window sizes, in ms, over which the rate is calculated.
64 const int64_t max_window_size_ms_;
65 int64_t current_window_size_ms_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000066};
67} // namespace webrtc
68
tkchinf75d0082016-02-23 22:49:42 -080069#endif // WEBRTC_BASE_RATE_STATISTICS_H_