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 | |
| 13 | #include <assert.h> |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 14 | #include <algorithm> |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <sstream> |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 17 | #include <utility> |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 18 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 19 | #include "webrtc/base/checks.h" |
Peter Boström | 7c704b8 | 2015-12-04 16:13:05 +0100 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 0b9e29c | 2015-11-16 11:12:24 +0100 | [diff] [blame] | 21 | #include "webrtc/modules/pacing/paced_sender.h" |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 26 | |
| 27 | // Inactivity threshold above which probing is restarted. |
| 28 | constexpr int kInactivityThresholdMs = 5000; |
| 29 | |
Per | 28a4456 | 2016-05-04 17:12:51 +0200 | [diff] [blame] | 30 | int ComputeDeltaFromBitrate(size_t packet_size, uint32_t bitrate_bps) { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 31 | assert(bitrate_bps > 0); |
| 32 | // Compute the time delta needed to send packet_size bytes at bitrate_bps |
| 33 | // bps. Result is in milliseconds. |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 34 | return static_cast<int>((1000ll * packet_size * 8) / bitrate_bps); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 35 | } |
| 36 | } // namespace |
| 37 | |
| 38 | BitrateProber::BitrateProber() |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 39 | : probing_state_(ProbingState::kDisabled), |
| 40 | packet_size_last_sent_(0), |
| 41 | time_last_probe_sent_ms_(-1), |
| 42 | next_cluster_id_(0) { |
| 43 | SetEnabled(true); |
| 44 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 45 | |
| 46 | void BitrateProber::SetEnabled(bool enable) { |
| 47 | if (enable) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 48 | if (probing_state_ == ProbingState::kDisabled) { |
| 49 | probing_state_ = ProbingState::kInactive; |
| 50 | LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 51 | } |
| 52 | } else { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 53 | probing_state_ = ProbingState::kDisabled; |
| 54 | LOG(LS_INFO) << "Bandwidth probing disabled"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | bool BitrateProber::IsProbing() const { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 59 | return probing_state_ == ProbingState::kActive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Per | 28a4456 | 2016-05-04 17:12:51 +0200 | [diff] [blame] | 62 | void BitrateProber::OnIncomingPacket(uint32_t bitrate_bps, |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 63 | size_t packet_size, |
| 64 | int64_t now_ms) { |
| 65 | // Don't initialize probing unless we have something large enough to start |
| 66 | // probing. |
| 67 | if (packet_size < PacedSender::kMinProbePacketSize) |
| 68 | return; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 69 | if (probing_state_ != ProbingState::kInactive) |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 70 | return; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 71 | // Max number of packets used for probing. |
stefan@webrtc.org | d839e0a | 2014-11-04 19:33:55 +0000 | [diff] [blame] | 72 | const int kMaxNumProbes = 2; |
| 73 | const int kPacketsPerProbe = 5; |
| 74 | const float kProbeBitrateMultipliers[kMaxNumProbes] = {3, 6}; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 75 | std::stringstream bitrate_log; |
philipel | 29dca2c | 2016-05-13 11:13:05 +0200 | [diff] [blame] | 76 | bitrate_log << "Start probing for bandwidth, (bitrate:packets): "; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 77 | for (int i = 0; i < kMaxNumProbes; ++i) { |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 78 | ProbeCluster cluster; |
| 79 | // We need one extra to get 5 deltas for the first probe, therefore (i == 0) |
| 80 | cluster.max_probe_packets = kPacketsPerProbe + (i == 0 ? 1 : 0); |
| 81 | cluster.probe_bitrate_bps = kProbeBitrateMultipliers[i] * bitrate_bps; |
philipel | 29dca2c | 2016-05-13 11:13:05 +0200 | [diff] [blame] | 82 | cluster.id = next_cluster_id_++; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 83 | |
philipel | 29dca2c | 2016-05-13 11:13:05 +0200 | [diff] [blame] | 84 | bitrate_log << "(" << cluster.probe_bitrate_bps << ":" |
| 85 | << cluster.max_probe_packets << ") "; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 86 | |
| 87 | clusters_.push(cluster); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 88 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 89 | LOG(LS_INFO) << bitrate_log.str().c_str(); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 90 | probing_state_ = ProbingState::kActive; |
| 91 | } |
| 92 | |
| 93 | void BitrateProber::ResetState() { |
| 94 | time_last_probe_sent_ms_ = -1; |
| 95 | packet_size_last_sent_ = 0; |
| 96 | clusters_ = std::queue<ProbeCluster>(); |
| 97 | // If its enabled, reset to inactive. |
| 98 | if (probing_state_ != ProbingState::kDisabled) |
| 99 | probing_state_ = ProbingState::kInactive; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 103 | // Probing is not active or probing is already complete. |
| 104 | if (probing_state_ != ProbingState::kActive || clusters_.empty()) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 105 | return -1; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 106 | // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. |
| 107 | int64_t elapsed_time_ms; |
| 108 | if (time_last_probe_sent_ms_ == -1) { |
| 109 | elapsed_time_ms = 0; |
| 110 | } else { |
| 111 | elapsed_time_ms = now_ms - time_last_probe_sent_ms_; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 112 | } |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 113 | // If no probes have been sent for a while, abort current probing and |
| 114 | // reset. |
| 115 | if (elapsed_time_ms > kInactivityThresholdMs) { |
| 116 | ResetState(); |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 117 | return -1; |
| 118 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 119 | // We will send the first probe packet immediately if no packet has been |
| 120 | // sent before. |
| 121 | int time_until_probe_ms = 0; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 122 | if (packet_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 123 | int next_delta_ms = ComputeDeltaFromBitrate( |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 124 | packet_size_last_sent_, clusters_.front().probe_bitrate_bps); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 125 | time_until_probe_ms = next_delta_ms - elapsed_time_ms; |
| 126 | // There is no point in trying to probe with less than 1 ms between packets |
| 127 | // as it essentially means trying to probe at infinite bandwidth. |
| 128 | const int kMinProbeDeltaMs = 1; |
| 129 | // If we have waited more than 3 ms for a new packet to probe with we will |
| 130 | // consider this probing session over. |
| 131 | const int kMaxProbeDelayMs = 3; |
| 132 | if (next_delta_ms < kMinProbeDeltaMs || |
| 133 | time_until_probe_ms < -kMaxProbeDelayMs) { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 134 | probing_state_ = ProbingState::kSuspended; |
| 135 | LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend"; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 136 | time_until_probe_ms = 0; |
| 137 | } |
| 138 | } |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 139 | return std::max(time_until_probe_ms, 0); |
| 140 | } |
| 141 | |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 142 | int BitrateProber::CurrentClusterId() const { |
| 143 | RTC_DCHECK(!clusters_.empty()); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 144 | RTC_DCHECK(ProbingState::kActive == probing_state_); |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 145 | return clusters_.front().id; |
| 146 | } |
| 147 | |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 148 | size_t BitrateProber::RecommendedPacketSize() const { |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 149 | return packet_size_last_sent_; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) { |
| 153 | assert(packet_size > 0); |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 154 | if (packet_size < PacedSender::kMinProbePacketSize) |
| 155 | return; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 156 | packet_size_last_sent_ = packet_size; |
| 157 | if (probing_state_ != ProbingState::kActive) |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 158 | return; |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 159 | time_last_probe_sent_ms_ = now_ms; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 160 | if (!clusters_.empty()) { |
| 161 | ProbeCluster* cluster = &clusters_.front(); |
| 162 | ++cluster->sent_probe_packets; |
| 163 | if (cluster->sent_probe_packets == cluster->max_probe_packets) |
| 164 | clusters_.pop(); |
philipel | 1a93cde | 2016-06-03 01:41:45 -0700 | [diff] [blame] | 165 | if (clusters_.empty()) |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame^] | 166 | probing_state_ = ProbingState::kSuspended; |
philipel | dd32486 | 2016-05-06 17:06:14 +0200 | [diff] [blame] | 167 | } |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 168 | } |
| 169 | } // namespace webrtc |