You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.0 KiB

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