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