stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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/pacing/bitrate_prober.h" |
| 12 | |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 13 | #include <algorithm> |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 14 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
Peter Boström | 7c704b8 | 2015-12-04 16:13:05 +0100 | [diff] [blame] | 16 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 0b9e29c | 2015-11-16 11:12:24 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/pacing/paced_sender.h" |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 22 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 23 | // A minimum interval between probes to allow scheduling to be feasible. |
| 24 | constexpr int kMinProbeDeltaMs = 1; |
| 25 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 26 | // The minimum number probing packets used. |
| 27 | constexpr int kMinProbePacketsSent = 5; |
| 28 | |
| 29 | // The minimum probing duration in ms. |
| 30 | constexpr int kMinProbeDurationMs = 15; |
| 31 | |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 32 | // Maximum amount of time each probe can be delayed. Probe cluster is reset and |
| 33 | // retried from the start when this limit is reached. |
| 34 | constexpr int kMaxProbeDelayMs = 3; |
| 35 | |
| 36 | // Number of times probing is retried before the cluster is dropped. |
| 37 | constexpr int kMaxRetryAttempts = 3; |
| 38 | |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 39 | // The min probe packet size is scaled with the bitrate we're probing at. |
| 40 | // This defines the max min probe packet size, meaning that on high bitrates |
| 41 | // we have a min probe packet size of 200 bytes. |
| 42 | constexpr size_t kMinProbePacketSize = 200; |
| 43 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | BitrateProber::BitrateProber() |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 47 | : probing_state_(ProbingState::kDisabled), |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 48 | next_probe_time_ms_(-1), |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 49 | next_cluster_id_(0) { |
| 50 | SetEnabled(true); |
| 51 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 52 | |
| 53 | void BitrateProber::SetEnabled(bool enable) { |
| 54 | if (enable) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 55 | if (probing_state_ == ProbingState::kDisabled) { |
| 56 | probing_state_ = ProbingState::kInactive; |
| 57 | LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 58 | } |
| 59 | } else { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 60 | probing_state_ = ProbingState::kDisabled; |
| 61 | LOG(LS_INFO) << "Bandwidth probing disabled"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | bool BitrateProber::IsProbing() const { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 66 | return probing_state_ == ProbingState::kActive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 67 | } |
| 68 | |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 69 | void BitrateProber::OnIncomingPacket(size_t packet_size) { |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 70 | // Don't initialize probing unless we have something large enough to start |
| 71 | // probing. |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 72 | if (probing_state_ == ProbingState::kInactive && !clusters_.empty() && |
| 73 | packet_size >= |
| 74 | std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) { |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 75 | // Send next probe right away. |
| 76 | next_probe_time_ms_ = -1; |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 77 | probing_state_ = ProbingState::kActive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 78 | } |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 81 | void BitrateProber::CreateProbeCluster(int bitrate_bps) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 82 | RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 83 | ProbeCluster cluster; |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 84 | cluster.min_probes = kMinProbePacketsSent; |
| 85 | cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; |
| 86 | cluster.bitrate_bps = bitrate_bps; |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 87 | cluster.id = next_cluster_id_++; |
| 88 | clusters_.push(cluster); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 89 | |
| 90 | LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" |
| 91 | << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" |
| 92 | << cluster.min_probes << ")"; |
| 93 | // If we are already probing, continue to do so. Otherwise set it to |
| 94 | // kInactive and wait for OnIncomingPacket to start the probing. |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 95 | if (probing_state_ != ProbingState::kActive) |
| 96 | probing_state_ = ProbingState::kInactive; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void BitrateProber::ResetState() { |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 100 | RTC_DCHECK(probing_state_ == ProbingState::kActive); |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 101 | |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 102 | // Recreate all probing clusters. |
| 103 | std::queue<ProbeCluster> clusters; |
| 104 | clusters.swap(clusters_); |
| 105 | while (!clusters.empty()) { |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 106 | if (clusters.front().retries < kMaxRetryAttempts) { |
| 107 | CreateProbeCluster(clusters.front().bitrate_bps); |
| 108 | clusters_.back().retries = clusters.front().retries + 1; |
| 109 | } |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 110 | clusters.pop(); |
| 111 | } |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 112 | |
| 113 | probing_state_ = ProbingState::kInactive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 117 | // Probing is not active or probing is already complete. |
| 118 | if (probing_state_ != ProbingState::kActive || clusters_.empty()) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 119 | return -1; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 120 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 121 | int time_until_probe_ms = 0; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 122 | if (next_probe_time_ms_ >= 0) { |
| 123 | time_until_probe_ms = next_probe_time_ms_ - now_ms; |
| 124 | if (time_until_probe_ms < -kMaxProbeDelayMs) { |
| 125 | ResetState(); |
| 126 | return -1; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 129 | |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 130 | return std::max(time_until_probe_ms, 0); |
| 131 | } |
| 132 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 133 | int BitrateProber::CurrentClusterId() const { |
| 134 | RTC_DCHECK(!clusters_.empty()); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 135 | RTC_DCHECK(ProbingState::kActive == probing_state_); |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 136 | return clusters_.front().id; |
| 137 | } |
| 138 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 139 | // Probe size is recommended based on the probe bitrate required. We choose |
| 140 | // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be |
| 141 | // feasible. |
| 142 | size_t BitrateProber::RecommendedMinProbeSize() const { |
| 143 | RTC_DCHECK(!clusters_.empty()); |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 144 | return clusters_.front().bitrate_bps * 3 * kMinProbeDeltaMs / (8 * 1000); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 147 | void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { |
| 148 | RTC_DCHECK(probing_state_ == ProbingState::kActive); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 149 | RTC_DCHECK_GT(bytes, 0); |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 150 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 151 | if (!clusters_.empty()) { |
| 152 | ProbeCluster* cluster = &clusters_.front(); |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 153 | if (cluster->sent_probes == 0) { |
| 154 | RTC_DCHECK_EQ(cluster->time_started_ms, -1); |
| 155 | cluster->time_started_ms = now_ms; |
| 156 | } |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 157 | cluster->sent_bytes += static_cast<int>(bytes); |
| 158 | cluster->sent_probes += 1; |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 159 | next_probe_time_ms_ = GetNextProbeTime(clusters_.front()); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 160 | if (cluster->sent_bytes >= cluster->min_bytes && |
| 161 | cluster->sent_probes >= cluster->min_probes) { |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 162 | clusters_.pop(); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 163 | } |
philipel | 1a93cde | 2016-06-03 01:41:45 -0700 | [diff] [blame] | 164 | if (clusters_.empty()) |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 165 | probing_state_ = ProbingState::kSuspended; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 166 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 167 | } |
sergeyu | 6dbbd89 | 2017-01-17 15:07:59 -0800 | [diff] [blame] | 168 | |
| 169 | int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) { |
| 170 | RTC_CHECK_GT(cluster.bitrate_bps, 0); |
| 171 | RTC_CHECK_GE(cluster.time_started_ms, 0); |
| 172 | |
| 173 | // Compute the time delta from the cluster start to ensure probe bitrate stays |
| 174 | // close to the target bitrate. Result is in milliseconds. |
| 175 | int64_t delta_ms = (8000ll * cluster.sent_bytes + cluster.bitrate_bps / 2) / |
| 176 | cluster.bitrate_bps; |
| 177 | return cluster.time_started_ms + delta_ms; |
| 178 | } |
| 179 | |
| 180 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 181 | } // namespace webrtc |