sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_BASE_RATE_STATISTICS_H_ |
| 12 | #define WEBRTC_BASE_RATE_STATISTICS_H_ |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 13 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame^] | 14 | #include <memory> |
| 15 | |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 16 | #include "webrtc/typedefs.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class 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.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 29 | void Update(size_t count, int64_t now_ms); |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 30 | 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_; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame^] | 38 | std::unique_ptr<size_t[]> buckets_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 39 | |
| 40 | // Total count recorded in buckets. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 41 | size_t accumulated_count_; |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 42 | |
| 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 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 54 | #endif // WEBRTC_BASE_RATE_STATISTICS_H_ |