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 | |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 23 | // Inactivity threshold above which probing is restarted. |
| 24 | constexpr int kInactivityThresholdMs = 5000; |
| 25 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 26 | // A minimum interval between probes to allow scheduling to be feasible. |
| 27 | constexpr int kMinProbeDeltaMs = 1; |
| 28 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 29 | // The minimum number probing packets used. |
| 30 | constexpr int kMinProbePacketsSent = 5; |
| 31 | |
| 32 | // The minimum probing duration in ms. |
| 33 | constexpr int kMinProbeDurationMs = 15; |
| 34 | |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 35 | int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) { |
| 36 | RTC_CHECK_GT(bitrate_bps, 0); |
| 37 | // Compute the time delta needed to send probe_size bytes at bitrate_bps |
| 38 | // bps. Result is in milliseconds. |
| 39 | return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps); |
| 40 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | BitrateProber::BitrateProber() |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 44 | : probing_state_(ProbingState::kDisabled), |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 45 | probe_size_last_sent_(0), |
| 46 | time_last_probe_sent_ms_(-1), |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 47 | next_cluster_id_(0) { |
| 48 | SetEnabled(true); |
| 49 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 50 | |
| 51 | void BitrateProber::SetEnabled(bool enable) { |
| 52 | if (enable) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 53 | if (probing_state_ == ProbingState::kDisabled) { |
| 54 | probing_state_ = ProbingState::kInactive; |
| 55 | LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 56 | } |
| 57 | } else { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 58 | probing_state_ = ProbingState::kDisabled; |
| 59 | LOG(LS_INFO) << "Bandwidth probing disabled"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | bool BitrateProber::IsProbing() const { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 64 | return probing_state_ == ProbingState::kActive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 65 | } |
| 66 | |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 67 | void BitrateProber::OnIncomingPacket(size_t packet_size) { |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 68 | // Don't initialize probing unless we have something large enough to start |
| 69 | // probing. |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 70 | if (probing_state_ == ProbingState::kInactive && |
philipel | eb680ea | 2016-08-17 11:11:59 +0200 | [diff] [blame] | 71 | !clusters_.empty() && |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 72 | packet_size >= PacedSender::kMinProbePacketSize) { |
| 73 | probing_state_ = ProbingState::kActive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 74 | } |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 75 | } |
| 76 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 77 | void BitrateProber::CreateProbeCluster(int bitrate_bps) { |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 78 | RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 79 | ProbeCluster cluster; |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 80 | cluster.min_probes = kMinProbePacketsSent; |
| 81 | cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; |
| 82 | cluster.bitrate_bps = bitrate_bps; |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 83 | cluster.id = next_cluster_id_++; |
| 84 | clusters_.push(cluster); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 85 | |
| 86 | LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" |
| 87 | << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" |
| 88 | << cluster.min_probes << ")"; |
| 89 | // If we are already probing, continue to do so. Otherwise set it to |
| 90 | // kInactive and wait for OnIncomingPacket to start the probing. |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 91 | if (probing_state_ != ProbingState::kActive) |
| 92 | probing_state_ = ProbingState::kInactive; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void BitrateProber::ResetState() { |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 96 | time_last_probe_sent_ms_ = -1; |
| 97 | probe_size_last_sent_ = 0; |
| 98 | |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 99 | // Recreate all probing clusters. |
| 100 | std::queue<ProbeCluster> clusters; |
| 101 | clusters.swap(clusters_); |
| 102 | while (!clusters.empty()) { |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 103 | CreateProbeCluster(clusters.front().bitrate_bps); |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 104 | clusters.pop(); |
| 105 | } |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 106 | // If its enabled, reset to inactive. |
| 107 | if (probing_state_ != ProbingState::kDisabled) |
| 108 | probing_state_ = ProbingState::kInactive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 112 | // Probing is not active or probing is already complete. |
| 113 | if (probing_state_ != ProbingState::kActive || clusters_.empty()) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 114 | return -1; |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 115 | // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. |
| 116 | int64_t elapsed_time_ms; |
| 117 | if (time_last_probe_sent_ms_ == -1) { |
| 118 | elapsed_time_ms = 0; |
| 119 | } else { |
| 120 | elapsed_time_ms = now_ms - time_last_probe_sent_ms_; |
| 121 | } |
| 122 | // If no probes have been sent for a while, abort current probing and |
| 123 | // reset. |
| 124 | if (elapsed_time_ms > kInactivityThresholdMs) { |
| 125 | ResetState(); |
| 126 | return -1; |
| 127 | } |
| 128 | // We will send the first probe packet immediately if no packet has been |
| 129 | // sent before. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 130 | int time_until_probe_ms = 0; |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 131 | if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { |
| 132 | int next_delta_ms = ComputeDeltaFromBitrate(probe_size_last_sent_, |
| 133 | clusters_.front().bitrate_bps); |
| 134 | time_until_probe_ms = next_delta_ms - elapsed_time_ms; |
| 135 | // If we have waited more than 3 ms for a new packet to probe with we will |
| 136 | // consider this probing session over. |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 137 | const int kMaxProbeDelayMs = 3; |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 138 | if (next_delta_ms < kMinProbeDeltaMs || |
| 139 | time_until_probe_ms < -kMaxProbeDelayMs) { |
| 140 | probing_state_ = ProbingState::kSuspended; |
| 141 | LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend"; |
| 142 | time_until_probe_ms = 0; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 145 | return std::max(time_until_probe_ms, 0); |
| 146 | } |
| 147 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 148 | int BitrateProber::CurrentClusterId() const { |
| 149 | RTC_DCHECK(!clusters_.empty()); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 150 | RTC_DCHECK(ProbingState::kActive == probing_state_); |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 151 | return clusters_.front().id; |
| 152 | } |
| 153 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 154 | // Probe size is recommended based on the probe bitrate required. We choose |
| 155 | // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be |
| 156 | // feasible. |
| 157 | size_t BitrateProber::RecommendedMinProbeSize() const { |
| 158 | RTC_DCHECK(!clusters_.empty()); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 159 | return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 160 | } |
| 161 | |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 162 | void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { |
| 163 | RTC_DCHECK(probing_state_ == ProbingState::kActive); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 164 | RTC_DCHECK_GT(bytes, 0); |
sprang | ebfbc8e | 2017-01-10 01:27:28 -0800 | [diff] [blame] | 165 | probe_size_last_sent_ = bytes; |
| 166 | time_last_probe_sent_ms_ = now_ms; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 167 | if (!clusters_.empty()) { |
| 168 | ProbeCluster* cluster = &clusters_.front(); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 169 | cluster->sent_bytes += static_cast<int>(bytes); |
| 170 | cluster->sent_probes += 1; |
| 171 | if (cluster->sent_bytes >= cluster->min_bytes && |
| 172 | cluster->sent_probes >= cluster->min_probes) { |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 173 | clusters_.pop(); |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 174 | } |
philipel | 1a93cde | 2016-06-03 01:41:45 -0700 | [diff] [blame] | 175 | if (clusters_.empty()) |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 176 | probing_state_ = ProbingState::kSuspended; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 177 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 178 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 179 | } // namespace webrtc |