blob: aea8d793fed154df0c60c3f54c52e8f2f1eb7d12 [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
sprang@webrtc.org37968a92013-12-03 10:31:59 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20class RateStatistics {
21 public:
22 // window_size = window size in ms for the rate estimation
23 // scale = coefficient to convert counts/ms to desired units,
24 // ex: if counts represents bytes, use 8*1000 to go to bits/s
25 RateStatistics(uint32_t window_size_ms, float scale);
26 ~RateStatistics();
27
28 void Reset();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000029 void Update(size_t count, int64_t now_ms);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000030 uint32_t Rate(int64_t now_ms);
31
32 private:
33 void EraseOld(int64_t now_ms);
34
35 // Counters are kept in buckets (circular buffer), with one bucket
36 // per millisecond.
37 const int num_buckets_;
jbauch555604a2016-04-26 03:13:22 -070038 std::unique_ptr<size_t[]> buckets_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000039
40 // Total count recorded in buckets.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000041 size_t accumulated_count_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000042
43 // Oldest time recorded in buckets.
44 int64_t oldest_time_;
45
46 // Bucket index of oldest counter recorded in buckets.
47 int oldest_index_;
48
49 // To convert counts/ms to desired units
50 const float scale_;
51};
52} // namespace webrtc
53
tkchinf75d0082016-02-23 22:49:42 -080054#endif // WEBRTC_BASE_RATE_STATISTICS_H_