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.

101 lines
4.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/bandwidthsmoother.h"
  28. #include <limits.h>
  29. namespace talk_base {
  30. BandwidthSmoother::BandwidthSmoother(int initial_bandwidth_guess,
  31. uint32 time_between_increase,
  32. double percent_increase,
  33. size_t samples_count_to_average,
  34. double min_sample_count_percent)
  35. : time_between_increase_(time_between_increase),
  36. percent_increase_(talk_base::_max(1.0, percent_increase)),
  37. time_at_last_change_(0),
  38. bandwidth_estimation_(initial_bandwidth_guess),
  39. accumulator_(samples_count_to_average),
  40. min_sample_count_percent_(
  41. talk_base::_min(1.0,
  42. talk_base::_max(0.0, min_sample_count_percent))) {
  43. }
  44. // Samples a new bandwidth measurement
  45. // returns true if the bandwidth estimation changed
  46. bool BandwidthSmoother::Sample(uint32 sample_time, int bandwidth) {
  47. if (bandwidth < 0) {
  48. return false;
  49. }
  50. accumulator_.AddSample(bandwidth);
  51. if (accumulator_.count() < static_cast<size_t>(
  52. accumulator_.max_count() * min_sample_count_percent_)) {
  53. // We have not collected enough samples yet.
  54. return false;
  55. }
  56. // Replace bandwidth with the mean of sampled bandwidths.
  57. const int mean_bandwidth = accumulator_.ComputeMean();
  58. if (mean_bandwidth < bandwidth_estimation_) {
  59. time_at_last_change_ = sample_time;
  60. bandwidth_estimation_ = mean_bandwidth;
  61. return true;
  62. }
  63. const int old_bandwidth_estimation = bandwidth_estimation_;
  64. const double increase_threshold_d = percent_increase_ * bandwidth_estimation_;
  65. if (increase_threshold_d > INT_MAX) {
  66. // If bandwidth goes any higher we would overflow.
  67. return false;
  68. }
  69. const int increase_threshold = static_cast<int>(increase_threshold_d);
  70. if (mean_bandwidth < increase_threshold) {
  71. time_at_last_change_ = sample_time;
  72. // The value of bandwidth_estimation remains the same if we don't exceed
  73. // percent_increase_ * bandwidth_estimation_ for at least
  74. // time_between_increase_ time.
  75. } else if (sample_time >= time_at_last_change_ + time_between_increase_) {
  76. time_at_last_change_ = sample_time;
  77. if (increase_threshold == 0) {
  78. // Bandwidth_estimation_ must be zero. Assume a jump from zero to a
  79. // positive bandwidth means we have regained connectivity.
  80. bandwidth_estimation_ = mean_bandwidth;
  81. } else {
  82. bandwidth_estimation_ = increase_threshold;
  83. }
  84. }
  85. // Else don't make a change.
  86. return old_bandwidth_estimation != bandwidth_estimation_;
  87. }
  88. } // namespace talk_base