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