henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
| 11 | #include "webrtc/base/bandwidthsmoother.h" |
| 12 | |
| 13 | #include <limits.h> |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 14 | #include <algorithm> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
| 16 | namespace rtc { |
| 17 | |
| 18 | BandwidthSmoother::BandwidthSmoother(int initial_bandwidth_guess, |
| 19 | uint32 time_between_increase, |
| 20 | double percent_increase, |
| 21 | size_t samples_count_to_average, |
| 22 | double min_sample_count_percent) |
| 23 | : time_between_increase_(time_between_increase), |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 24 | percent_increase_(std::max(1.0, percent_increase)), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | time_at_last_change_(0), |
| 26 | bandwidth_estimation_(initial_bandwidth_guess), |
| 27 | accumulator_(samples_count_to_average), |
| 28 | min_sample_count_percent_( |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 29 | std::min(1.0, std::max(0.0, min_sample_count_percent))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | } |
| 31 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame^] | 32 | BandwidthSmoother::~BandwidthSmoother() = default; |
| 33 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | // Samples a new bandwidth measurement |
| 35 | // returns true if the bandwidth estimation changed |
| 36 | bool BandwidthSmoother::Sample(uint32 sample_time, int bandwidth) { |
| 37 | if (bandwidth < 0) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | accumulator_.AddSample(bandwidth); |
| 42 | |
| 43 | if (accumulator_.count() < static_cast<size_t>( |
| 44 | accumulator_.max_count() * min_sample_count_percent_)) { |
| 45 | // We have not collected enough samples yet. |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // Replace bandwidth with the mean of sampled bandwidths. |
| 50 | const int mean_bandwidth = static_cast<int>(accumulator_.ComputeMean()); |
| 51 | |
| 52 | if (mean_bandwidth < bandwidth_estimation_) { |
| 53 | time_at_last_change_ = sample_time; |
| 54 | bandwidth_estimation_ = mean_bandwidth; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | const int old_bandwidth_estimation = bandwidth_estimation_; |
| 59 | const double increase_threshold_d = percent_increase_ * bandwidth_estimation_; |
| 60 | if (increase_threshold_d > INT_MAX) { |
| 61 | // If bandwidth goes any higher we would overflow. |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | const int increase_threshold = static_cast<int>(increase_threshold_d); |
| 66 | if (mean_bandwidth < increase_threshold) { |
| 67 | time_at_last_change_ = sample_time; |
| 68 | // The value of bandwidth_estimation remains the same if we don't exceed |
| 69 | // percent_increase_ * bandwidth_estimation_ for at least |
| 70 | // time_between_increase_ time. |
| 71 | } else if (sample_time >= time_at_last_change_ + time_between_increase_) { |
| 72 | time_at_last_change_ = sample_time; |
| 73 | if (increase_threshold == 0) { |
| 74 | // Bandwidth_estimation_ must be zero. Assume a jump from zero to a |
| 75 | // positive bandwidth means we have regained connectivity. |
| 76 | bandwidth_estimation_ = mean_bandwidth; |
| 77 | } else { |
| 78 | bandwidth_estimation_ = increase_threshold; |
| 79 | } |
| 80 | } |
| 81 | // Else don't make a change. |
| 82 | |
| 83 | return old_bandwidth_estimation != bandwidth_estimation_; |
| 84 | } |
| 85 | |
| 86 | } // namespace rtc |