Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 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 | |
| 11 | #include "webrtc/modules/congestion_controller/probe_controller.h" |
| 12 | |
isheriff | 8e6a761 | 2016-09-29 05:36:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 14 | #include <initializer_list> |
| 15 | |
| 16 | #include "webrtc/base/logging.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Number of deltas between probes per cluster. On the very first cluster, |
| 23 | // we will need kProbeDeltasPerCluster + 1 probes, but on a cluster following |
| 24 | // another, we need kProbeDeltasPerCluster probes. |
| 25 | constexpr int kProbeDeltasPerCluster = 5; |
| 26 | |
| 27 | // Maximum waiting time from the time of initiating probing to getting |
| 28 | // the measured results back. |
| 29 | constexpr int64_t kMaxWaitingTimeForProbingResultMs = 1000; |
| 30 | |
| 31 | // Value of |min_bitrate_to_probe_further_bps_| that indicates |
| 32 | // further probing is disabled. |
| 33 | constexpr int kExponentialProbingDisabled = 0; |
| 34 | |
isheriff | 8e6a761 | 2016-09-29 05:36:59 -0700 | [diff] [blame] | 35 | // A limit to prevent probing at excessive bitrates. |
| 36 | constexpr int kMaxProbingBitrateBps = 10000000; |
| 37 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 38 | } // namespace |
| 39 | |
| 40 | ProbeController::ProbeController(PacedSender* pacer, Clock* clock) |
| 41 | : pacer_(pacer), |
| 42 | clock_(clock), |
| 43 | state_(State::kInit), |
| 44 | min_bitrate_to_probe_further_bps_(kExponentialProbingDisabled), |
| 45 | time_last_probing_initiated_ms_(0), |
| 46 | estimated_bitrate_bps_(0), |
| 47 | max_bitrate_bps_(0) {} |
| 48 | |
| 49 | void ProbeController::SetBitrates(int min_bitrate_bps, |
| 50 | int start_bitrate_bps, |
| 51 | int max_bitrate_bps) { |
| 52 | rtc::CritScope cs(&critsect_); |
| 53 | if (state_ == State::kInit) { |
| 54 | // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of |
| 55 | // 1.2 Mbps to continue probing. |
| 56 | InitiateProbing({3 * start_bitrate_bps, 6 * start_bitrate_bps}, |
| 57 | 4 * start_bitrate_bps); |
| 58 | } |
| 59 | |
| 60 | // Only do probing if: |
Irfan Sheriff | 9b7b753 | 2016-09-16 11:30:44 -0700 | [diff] [blame] | 61 | // we are mid-call, which we consider to be if |
| 62 | // exponential probing is not active and |
| 63 | // |estimated_bitrate_bps_| is valid (> 0) and |
| 64 | // the current bitrate is lower than the new |max_bitrate_bps|, and |
| 65 | // we actually want to increase the |max_bitrate_bps_|. |
| 66 | if (state_ != State::kWaitingForProbingResult && |
| 67 | estimated_bitrate_bps_ != 0 && estimated_bitrate_bps_ < max_bitrate_bps && |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 68 | max_bitrate_bps > max_bitrate_bps_) { |
| 69 | InitiateProbing({max_bitrate_bps}, kExponentialProbingDisabled); |
| 70 | } |
| 71 | max_bitrate_bps_ = max_bitrate_bps; |
| 72 | } |
| 73 | |
| 74 | void ProbeController::SetEstimatedBitrate(int bitrate_bps) { |
| 75 | rtc::CritScope cs(&critsect_); |
| 76 | if (state_ == State::kWaitingForProbingResult) { |
| 77 | if ((clock_->TimeInMilliseconds() - time_last_probing_initiated_ms_) > |
| 78 | kMaxWaitingTimeForProbingResultMs) { |
| 79 | LOG(LS_INFO) << "kWaitingForProbingResult: timeout"; |
| 80 | state_ = State::kProbingComplete; |
| 81 | min_bitrate_to_probe_further_bps_ = kExponentialProbingDisabled; |
| 82 | } else { |
| 83 | // Continue probing if probing results indicate channel has greater |
| 84 | // capacity. |
| 85 | LOG(LS_INFO) << "Measured bitrate: " << bitrate_bps |
| 86 | << " Minimum to probe further: " |
| 87 | << min_bitrate_to_probe_further_bps_; |
| 88 | if (min_bitrate_to_probe_further_bps_ != kExponentialProbingDisabled && |
| 89 | bitrate_bps > min_bitrate_to_probe_further_bps_) { |
| 90 | // Double the probing bitrate and expect a minimum of 25% gain to |
| 91 | // continue probing. |
| 92 | InitiateProbing({2 * bitrate_bps}, 1.25 * bitrate_bps); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | estimated_bitrate_bps_ = bitrate_bps; |
| 97 | } |
| 98 | |
| 99 | void ProbeController::InitiateProbing( |
| 100 | std::initializer_list<int> bitrates_to_probe, |
| 101 | int min_bitrate_to_probe_further_bps) { |
| 102 | bool first_cluster = true; |
| 103 | for (int bitrate : bitrates_to_probe) { |
isheriff | 8e6a761 | 2016-09-29 05:36:59 -0700 | [diff] [blame] | 104 | bitrate = std::min(bitrate, kMaxProbingBitrateBps); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 105 | if (first_cluster) { |
| 106 | pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster + 1); |
| 107 | first_cluster = false; |
| 108 | } else { |
| 109 | pacer_->CreateProbeCluster(bitrate, kProbeDeltasPerCluster); |
| 110 | } |
| 111 | } |
| 112 | min_bitrate_to_probe_further_bps_ = min_bitrate_to_probe_further_bps; |
| 113 | time_last_probing_initiated_ms_ = clock_->TimeInMilliseconds(); |
| 114 | if (min_bitrate_to_probe_further_bps == kExponentialProbingDisabled) |
| 115 | state_ = State::kProbingComplete; |
| 116 | else |
| 117 | state_ = State::kWaitingForProbingResult; |
| 118 | } |
| 119 | |
| 120 | } // namespace webrtc |