OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_RATE_STATISTICS_H_ | 11 #ifndef WEBRTC_BASE_RATE_STATISTICS_H_ |
12 #define WEBRTC_BASE_RATE_STATISTICS_H_ | 12 #define WEBRTC_BASE_RATE_STATISTICS_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 | 15 |
| 16 #include "webrtc/base/optional.h" |
16 #include "webrtc/typedefs.h" | 17 #include "webrtc/typedefs.h" |
17 | 18 |
18 namespace webrtc { | 19 namespace webrtc { |
19 | 20 |
20 class RateStatistics { | 21 class RateStatistics { |
21 public: | 22 public: |
22 // window_size = window size in ms for the rate estimation | 23 // 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(). |
23 // scale = coefficient to convert counts/ms to desired units, | 26 // scale = coefficient to convert counts/ms to desired units, |
24 // ex: if counts represents bytes, use 8*1000 to go to bits/s | 27 // ex: if counts represents bytes, use 8*1000 to go to bits/s |
25 RateStatistics(uint32_t window_size_ms, float scale); | 28 RateStatistics(int64_t max_window_size_ms, float scale); |
26 ~RateStatistics(); | 29 ~RateStatistics(); |
27 | 30 |
28 void Reset(); | 31 void Reset(); |
29 void Update(size_t count, int64_t now_ms); | 32 void Update(size_t count, int64_t now_ms); |
30 uint32_t Rate(int64_t now_ms); | 33 rtc::Optional<uint32_t> Rate(int64_t now_ms); |
| 34 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms); |
31 | 35 |
32 private: | 36 private: |
33 void EraseOld(int64_t now_ms); | 37 void EraseOld(int64_t now_ms); |
34 | 38 |
35 // Counters are kept in buckets (circular buffer), with one bucket | 39 // Counters are kept in buckets (circular buffer), with one bucket |
36 // per millisecond. | 40 // per millisecond. |
37 const int num_buckets_; | 41 struct Bucket { |
38 std::unique_ptr<size_t[]> buckets_; | 42 size_t sum; // Sum of all samples in this bucket. |
| 43 size_t samples; // Number of samples in this bucket. |
| 44 }; |
| 45 std::unique_ptr<Bucket[]> buckets_; |
39 | 46 |
40 // Total count recorded in buckets. | 47 // Total count recorded in buckets. |
41 size_t accumulated_count_; | 48 size_t accumulated_count_; |
42 | 49 |
| 50 // The total number of samples in the buckets. |
| 51 size_t num_samples_; |
| 52 |
43 // Oldest time recorded in buckets. | 53 // Oldest time recorded in buckets. |
44 int64_t oldest_time_; | 54 int64_t oldest_time_; |
45 | 55 |
46 // Bucket index of oldest counter recorded in buckets. | 56 // Bucket index of oldest counter recorded in buckets. |
47 int oldest_index_; | 57 uint32_t oldest_index_; |
48 | 58 |
49 // To convert counts/ms to desired units | 59 // To convert counts/ms to desired units |
50 const float scale_; | 60 const float scale_; |
| 61 |
| 62 // The window sizes, in ms, over which the rate is calculated. |
| 63 const int64_t max_window_size_ms_; |
| 64 int64_t current_window_size_ms_; |
51 }; | 65 }; |
52 } // namespace webrtc | 66 } // namespace webrtc |
53 | 67 |
54 #endif // WEBRTC_BASE_RATE_STATISTICS_H_ | 68 #endif // WEBRTC_BASE_RATE_STATISTICS_H_ |
OLD | NEW |