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