philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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/congestion_controller/probe_bitrate_estimator.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 15 | #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 16 | #include "webrtc/rtc_base/checks.h" |
| 17 | #include "webrtc/rtc_base/logging.h" |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 18 | |
| 19 | namespace { |
philipel | b5feb2e | 2017-03-09 07:01:58 -0800 | [diff] [blame] | 20 | // The minumum number of probes we need to receive feedback about in percent |
| 21 | // in order to have a valid estimate. |
| 22 | constexpr int kMinReceivedProbesPercent = 80; |
| 23 | |
| 24 | // The minumum number of bytes we need to receive feedback about in percent |
| 25 | // in order to have a valid estimate. |
| 26 | constexpr int kMinReceivedBytesPercent = 80; |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 27 | |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 28 | // The maximum |receive rate| / |send rate| ratio for a valid estimate. |
| 29 | constexpr float kMaxValidRatio = 2.0f; |
| 30 | |
| 31 | // The minimum |receive rate| / |send rate| ratio assuming that the link is |
| 32 | // not saturated, i.e. we assume that we will receive at least |
| 33 | // kMinRatioForUnsaturatedLink * |send rate| if |send rate| is less than the |
| 34 | // link capacity. |
| 35 | constexpr float kMinRatioForUnsaturatedLink = 0.9f; |
| 36 | |
| 37 | // The target utilization of the link. If we know true link capacity |
| 38 | // we'd like to send at 95% of that rate. |
| 39 | constexpr float kTargetUtilizationFraction = 0.95f; |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 40 | |
| 41 | // The maximum time period over which the cluster history is retained. |
| 42 | // This is also the maximum time period beyond which a probing burst is not |
| 43 | // expected to last. |
| 44 | constexpr int kMaxClusterHistoryMs = 1000; |
| 45 | |
| 46 | // The maximum time interval between first and the last probe on a cluster |
| 47 | // on the sender side as well as the receive side. |
| 48 | constexpr int kMaxProbeIntervalMs = 1000; |
| 49 | } // namespace |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 50 | |
| 51 | namespace webrtc { |
| 52 | |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 53 | ProbeBitrateEstimator::ProbeBitrateEstimator(RtcEventLog* event_log) |
| 54 | : event_log_(event_log) {} |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 55 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 56 | ProbeBitrateEstimator::~ProbeBitrateEstimator() = default; |
| 57 | |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 58 | int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate( |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 59 | const PacketFeedback& packet_feedback) { |
| 60 | int cluster_id = packet_feedback.pacing_info.probe_cluster_id; |
philipel | 8aadd50 | 2017-02-23 02:56:13 -0800 | [diff] [blame] | 61 | RTC_DCHECK_NE(cluster_id, PacedPacketInfo::kNotAProbe); |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 62 | |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 63 | EraseOldClusters(packet_feedback.arrival_time_ms - kMaxClusterHistoryMs); |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 64 | |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 65 | int payload_size_bits = packet_feedback.payload_size * 8; |
philipel | 8aadd50 | 2017-02-23 02:56:13 -0800 | [diff] [blame] | 66 | AggregatedCluster* cluster = &clusters_[cluster_id]; |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 67 | |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 68 | if (packet_feedback.send_time_ms < cluster->first_send_ms) { |
| 69 | cluster->first_send_ms = packet_feedback.send_time_ms; |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 70 | } |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 71 | if (packet_feedback.send_time_ms > cluster->last_send_ms) { |
| 72 | cluster->last_send_ms = packet_feedback.send_time_ms; |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 73 | cluster->size_last_send = payload_size_bits; |
| 74 | } |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 75 | if (packet_feedback.arrival_time_ms < cluster->first_receive_ms) { |
| 76 | cluster->first_receive_ms = packet_feedback.arrival_time_ms; |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 77 | cluster->size_first_receive = payload_size_bits; |
| 78 | } |
elad.alon | f949000 | 2017-03-06 05:32:21 -0800 | [diff] [blame] | 79 | if (packet_feedback.arrival_time_ms > cluster->last_receive_ms) { |
| 80 | cluster->last_receive_ms = packet_feedback.arrival_time_ms; |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 81 | } |
| 82 | cluster->size_total += payload_size_bits; |
| 83 | cluster->num_probes += 1; |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 84 | |
philipel | b5feb2e | 2017-03-09 07:01:58 -0800 | [diff] [blame] | 85 | RTC_DCHECK_GT(packet_feedback.pacing_info.probe_cluster_min_probes, 0); |
| 86 | RTC_DCHECK_GT(packet_feedback.pacing_info.probe_cluster_min_bytes, 0); |
| 87 | |
| 88 | int min_probes = packet_feedback.pacing_info.probe_cluster_min_probes * |
| 89 | kMinReceivedProbesPercent / 100; |
| 90 | int min_bytes = packet_feedback.pacing_info.probe_cluster_min_bytes * |
| 91 | kMinReceivedBytesPercent / 100; |
| 92 | if (cluster->num_probes < min_probes || cluster->size_total < min_bytes * 8) |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 93 | return -1; |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 94 | |
philipel | bf8a2c9 | 2016-08-10 19:00:39 +0200 | [diff] [blame] | 95 | float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms; |
| 96 | float receive_interval_ms = |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 97 | cluster->last_receive_ms - cluster->first_receive_ms; |
| 98 | |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 99 | if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs || |
| 100 | receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) { |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 101 | LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval" |
philipel | 8aadd50 | 2017-02-23 02:56:13 -0800 | [diff] [blame] | 102 | << " [cluster id: " << cluster_id |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 103 | << "] [send interval: " << send_interval_ms << " ms]" |
| 104 | << " [receive interval: " << receive_interval_ms << " ms]"; |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 105 | if (event_log_) { |
| 106 | event_log_->LogProbeResultFailure(cluster_id, |
| 107 | kInvalidSendReceiveInterval); |
| 108 | } |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 109 | return -1; |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 110 | } |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 111 | // Since the |send_interval_ms| does not include the time it takes to actually |
| 112 | // send the last packet the size of the last sent packet should not be |
| 113 | // included when calculating the send bitrate. |
| 114 | RTC_DCHECK_GT(cluster->size_total, cluster->size_last_send); |
| 115 | float send_size = cluster->size_total - cluster->size_last_send; |
| 116 | float send_bps = send_size / send_interval_ms * 1000; |
| 117 | |
| 118 | // Since the |receive_interval_ms| does not include the time it takes to |
| 119 | // actually receive the first packet the size of the first received packet |
| 120 | // should not be included when calculating the receive bitrate. |
| 121 | RTC_DCHECK_GT(cluster->size_total, cluster->size_first_receive); |
| 122 | float receive_size = cluster->size_total - cluster->size_first_receive; |
| 123 | float receive_bps = receive_size / receive_interval_ms * 1000; |
| 124 | |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 125 | float ratio = receive_bps / send_bps; |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 126 | if (ratio > kMaxValidRatio) { |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 127 | LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high" |
philipel | 8aadd50 | 2017-02-23 02:56:13 -0800 | [diff] [blame] | 128 | << " [cluster id: " << cluster_id << "] [send: " << send_size |
| 129 | << " bytes / " << send_interval_ms |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 130 | << " ms = " << send_bps / 1000 << " kb/s]" |
| 131 | << " [receive: " << receive_size << " bytes / " |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 132 | << receive_interval_ms << " ms = " << receive_bps / 1000 |
| 133 | << " kb/s]" |
| 134 | << " [ratio: " << receive_bps / 1000 << " / " |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 135 | << send_bps / 1000 << " = " << ratio << " > kMaxValidRatio (" |
| 136 | << kMaxValidRatio << ")]"; |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 137 | if (event_log_) |
| 138 | event_log_->LogProbeResultFailure(cluster_id, kInvalidSendReceiveRatio); |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 139 | return -1; |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 140 | } |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 141 | LOG(LS_INFO) << "Probing successful" |
philipel | 8aadd50 | 2017-02-23 02:56:13 -0800 | [diff] [blame] | 142 | << " [cluster id: " << cluster_id << "] [send: " << send_size |
| 143 | << " bytes / " << send_interval_ms << " ms = " << send_bps / 1000 |
| 144 | << " kb/s]" |
philipel | 0561bdf | 2016-08-24 03:43:53 -0700 | [diff] [blame] | 145 | << " [receive: " << receive_size << " bytes / " |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 146 | << receive_interval_ms << " ms = " << receive_bps / 1000 |
| 147 | << " kb/s]"; |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 148 | |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 149 | float res = std::min(send_bps, receive_bps); |
terelius | 3764730 | 2017-06-27 07:50:31 -0700 | [diff] [blame] | 150 | // If we're receiving at significantly lower bitrate than we were sending at, |
| 151 | // it suggests that we've found the true capacity of the link. In this case, |
| 152 | // set the target bitrate slightly lower to not immediately overuse. |
| 153 | if (receive_bps < kMinRatioForUnsaturatedLink * send_bps) { |
| 154 | RTC_DCHECK_GT(send_bps, receive_bps); |
| 155 | res = kTargetUtilizationFraction * receive_bps; |
| 156 | } |
philipel | f4238f9 | 2017-03-28 04:18:02 -0700 | [diff] [blame] | 157 | if (event_log_) |
| 158 | event_log_->LogProbeResultSuccess(cluster_id, res); |
terelius | 91047e5 | 2017-06-19 06:07:30 -0700 | [diff] [blame] | 159 | estimated_bitrate_bps_ = rtc::Optional<int>(res); |
michaelt | 8490f8a | 2017-04-20 10:10:10 -0700 | [diff] [blame] | 160 | return *estimated_bitrate_bps_; |
| 161 | } |
| 162 | |
| 163 | rtc::Optional<int> |
| 164 | ProbeBitrateEstimator::FetchAndResetLastEstimatedBitrateBps() { |
| 165 | rtc::Optional<int> estimated_bitrate_bps = estimated_bitrate_bps_; |
| 166 | estimated_bitrate_bps_.reset(); |
| 167 | return estimated_bitrate_bps; |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 168 | } |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 169 | |
Irfan Sheriff | f99a9de | 2016-08-23 14:23:03 -0700 | [diff] [blame] | 170 | void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) { |
| 171 | for (auto it = clusters_.begin(); it != clusters_.end();) { |
| 172 | if (it->second.last_receive_ms < timestamp_ms) { |
| 173 | it = clusters_.erase(it); |
| 174 | } else { |
| 175 | ++it; |
| 176 | } |
| 177 | } |
philipel | 233c4ba | 2016-08-01 08:49:04 -0700 | [diff] [blame] | 178 | } |
| 179 | } // namespace webrtc |