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.

68 lines
2.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 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. #include "modules/pacing/interval_budget.h"
  11. #include <algorithm>
  12. #include "rtc_base/numerics/safe_conversions.h"
  13. namespace webrtc {
  14. namespace {
  15. constexpr int64_t kWindowMs = 500;
  16. }
  17. IntervalBudget::IntervalBudget(int initial_target_rate_kbps)
  18. : IntervalBudget(initial_target_rate_kbps, false) {}
  19. IntervalBudget::IntervalBudget(int initial_target_rate_kbps,
  20. bool can_build_up_underuse)
  21. : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) {
  22. set_target_rate_kbps(initial_target_rate_kbps);
  23. }
  24. void IntervalBudget::set_target_rate_kbps(int target_rate_kbps) {
  25. target_rate_kbps_ = target_rate_kbps;
  26. max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) / 8;
  27. bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_),
  28. max_bytes_in_budget_);
  29. }
  30. void IntervalBudget::IncreaseBudget(int64_t delta_time_ms) {
  31. int64_t bytes = target_rate_kbps_ * delta_time_ms / 8;
  32. if (bytes_remaining_ < 0 || can_build_up_underuse_) {
  33. // We overused last interval, compensate this interval.
  34. bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_);
  35. } else {
  36. // If we underused last interval we can't use it this interval.
  37. bytes_remaining_ = std::min(bytes, max_bytes_in_budget_);
  38. }
  39. }
  40. void IntervalBudget::UseBudget(size_t bytes) {
  41. bytes_remaining_ = std::max(bytes_remaining_ - static_cast<int>(bytes),
  42. -max_bytes_in_budget_);
  43. }
  44. size_t IntervalBudget::bytes_remaining() const {
  45. return rtc::saturated_cast<size_t>(std::max<int64_t>(0, bytes_remaining_));
  46. }
  47. double IntervalBudget::budget_ratio() const {
  48. if (max_bytes_in_budget_ == 0)
  49. return 0.0;
  50. return static_cast<double>(bytes_remaining_) / max_bytes_in_budget_;
  51. }
  52. int IntervalBudget::target_rate_kbps() const {
  53. return target_rate_kbps_;
  54. }
  55. } // namespace webrtc