| 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/base/optional.h" |
| 17 #include "webrtc/typedefs.h" | 17 #include "webrtc/typedefs.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 class RateStatistics { | 21 class RateStatistics { |
| 22 public: | 22 public: |
| 23 static constexpr float kBpsScale = 8000.0f; | |
| 24 | |
| 25 // max_window_size_ms = Maximum window size in ms for the rate estimation. | 23 // max_window_size_ms = Maximum window size in ms for the rate estimation. |
| 26 // Initial window size is set to this, but may be changed | 24 // Initial window size is set to this, but may be changed |
| 27 // to something lower by calling SetWindowSize(). | 25 // to something lower by calling SetWindowSize(). |
| 28 // scale = coefficient to convert counts/ms to desired unit | 26 // scale = coefficient to convert counts/ms to desired units, |
| 29 // ex: kBpsScale (8000) for bits/s if count represents bytes. | 27 // ex: if counts represents bytes, use 8*1000 to go to bits/s |
| 30 RateStatistics(int64_t max_window_size_ms, float scale); | 28 RateStatistics(int64_t max_window_size_ms, float scale); |
| 31 ~RateStatistics(); | 29 ~RateStatistics(); |
| 32 | 30 |
| 33 // Reset instance to original state. | |
| 34 void Reset(); | 31 void Reset(); |
| 35 | |
| 36 // Update rate with a new data point, moving averaging window as needed. | |
| 37 void Update(size_t count, int64_t now_ms); | 32 void Update(size_t count, int64_t now_ms); |
| 38 | 33 rtc::Optional<uint32_t> Rate(int64_t now_ms); |
| 39 // Note that despite this being a const method, it still updates the internal | |
| 40 // state (moves averaging window), but it doesn't make any alterations that | |
| 41 // are observable from the other methods, as long as supplied timestamps are | |
| 42 // from a monotonic clock. Ie, it doesn't matter if this call moves the | |
| 43 // window, since any subsequent call to Update or Rate would still have moved | |
| 44 // the window as much or more. | |
| 45 rtc::Optional<uint32_t> Rate(int64_t now_ms) const; | |
| 46 | |
| 47 // Update the size of the averaging window. The maximum allowed value for | |
| 48 // window_size_ms is max_window_size_ms as supplied in the constructor. | |
| 49 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms); | 34 bool SetWindowSize(int64_t window_size_ms, int64_t now_ms); |
| 50 | 35 |
| 51 private: | 36 private: |
| 52 void EraseOld(int64_t now_ms); | 37 void EraseOld(int64_t now_ms); |
| 53 bool IsInitialized() const; | 38 bool IsInitialized(); |
| 54 | 39 |
| 55 // Counters are kept in buckets (circular buffer), with one bucket | 40 // Counters are kept in buckets (circular buffer), with one bucket |
| 56 // per millisecond. | 41 // per millisecond. |
| 57 struct Bucket { | 42 struct Bucket { |
| 58 size_t sum; // Sum of all samples in this bucket. | 43 size_t sum; // Sum of all samples in this bucket. |
| 59 size_t samples; // Number of samples in this bucket. | 44 size_t samples; // Number of samples in this bucket. |
| 60 }; | 45 }; |
| 61 std::unique_ptr<Bucket[]> buckets_; | 46 std::unique_ptr<Bucket[]> buckets_; |
| 62 | 47 |
| 63 // Total count recorded in buckets. | 48 // Total count recorded in buckets. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 // To convert counts/ms to desired units | 60 // To convert counts/ms to desired units |
| 76 const float scale_; | 61 const float scale_; |
| 77 | 62 |
| 78 // The window sizes, in ms, over which the rate is calculated. | 63 // The window sizes, in ms, over which the rate is calculated. |
| 79 const int64_t max_window_size_ms_; | 64 const int64_t max_window_size_ms_; |
| 80 int64_t current_window_size_ms_; | 65 int64_t current_window_size_ms_; |
| 81 }; | 66 }; |
| 82 } // namespace webrtc | 67 } // namespace webrtc |
| 83 | 68 |
| 84 #endif // WEBRTC_BASE_RATE_STATISTICS_H_ | 69 #endif // WEBRTC_BASE_RATE_STATISTICS_H_ |
| OLD | NEW |