blob: e83d1e08cd2ac7f42059047fb4411a9ec177640e [file] [log] [blame]
philipel233c4ba2016-08-01 08:49:04 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/probe_bitrate_estimator.h"
philipel233c4ba2016-08-01 08:49:04 -070012
13#include <algorithm>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "logging/rtc_event_log/rtc_event_log.h"
16#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
philipel233c4ba2016-08-01 08:49:04 -070018
19namespace {
philipelb5feb2e2017-03-09 07:01:58 -080020// The minumum number of probes we need to receive feedback about in percent
21// in order to have a valid estimate.
22constexpr 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.
26constexpr int kMinReceivedBytesPercent = 80;
philipel233c4ba2016-08-01 08:49:04 -070027
terelius37647302017-06-27 07:50:31 -070028// The maximum |receive rate| / |send rate| ratio for a valid estimate.
29constexpr 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.
35constexpr 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.
39constexpr float kTargetUtilizationFraction = 0.95f;
Irfan Sherifff99a9de2016-08-23 14:23:03 -070040
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.
44constexpr 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.
48constexpr int kMaxProbeIntervalMs = 1000;
49} // namespace
philipel233c4ba2016-08-01 08:49:04 -070050
51namespace webrtc {
52
philipelf4238f92017-03-28 04:18:02 -070053ProbeBitrateEstimator::ProbeBitrateEstimator(RtcEventLog* event_log)
54 : event_log_(event_log) {}
philipel233c4ba2016-08-01 08:49:04 -070055
nisse76e62b02017-05-31 02:24:52 -070056ProbeBitrateEstimator::~ProbeBitrateEstimator() = default;
57
Irfan Sherifff99a9de2016-08-23 14:23:03 -070058int ProbeBitrateEstimator::HandleProbeAndEstimateBitrate(
elad.alonf9490002017-03-06 05:32:21 -080059 const PacketFeedback& packet_feedback) {
60 int cluster_id = packet_feedback.pacing_info.probe_cluster_id;
philipel8aadd502017-02-23 02:56:13 -080061 RTC_DCHECK_NE(cluster_id, PacedPacketInfo::kNotAProbe);
Irfan Sherifff99a9de2016-08-23 14:23:03 -070062
elad.alonf9490002017-03-06 05:32:21 -080063 EraseOldClusters(packet_feedback.arrival_time_ms - kMaxClusterHistoryMs);
philipel233c4ba2016-08-01 08:49:04 -070064
elad.alonf9490002017-03-06 05:32:21 -080065 int payload_size_bits = packet_feedback.payload_size * 8;
philipel8aadd502017-02-23 02:56:13 -080066 AggregatedCluster* cluster = &clusters_[cluster_id];
philipel0561bdf2016-08-24 03:43:53 -070067
elad.alonf9490002017-03-06 05:32:21 -080068 if (packet_feedback.send_time_ms < cluster->first_send_ms) {
69 cluster->first_send_ms = packet_feedback.send_time_ms;
philipel0561bdf2016-08-24 03:43:53 -070070 }
elad.alonf9490002017-03-06 05:32:21 -080071 if (packet_feedback.send_time_ms > cluster->last_send_ms) {
72 cluster->last_send_ms = packet_feedback.send_time_ms;
philipel0561bdf2016-08-24 03:43:53 -070073 cluster->size_last_send = payload_size_bits;
74 }
elad.alonf9490002017-03-06 05:32:21 -080075 if (packet_feedback.arrival_time_ms < cluster->first_receive_ms) {
76 cluster->first_receive_ms = packet_feedback.arrival_time_ms;
philipel0561bdf2016-08-24 03:43:53 -070077 cluster->size_first_receive = payload_size_bits;
78 }
elad.alonf9490002017-03-06 05:32:21 -080079 if (packet_feedback.arrival_time_ms > cluster->last_receive_ms) {
80 cluster->last_receive_ms = packet_feedback.arrival_time_ms;
philipel0561bdf2016-08-24 03:43:53 -070081 }
82 cluster->size_total += payload_size_bits;
83 cluster->num_probes += 1;
philipel233c4ba2016-08-01 08:49:04 -070084
philipelb5feb2e2017-03-09 07:01:58 -080085 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 Sherifff99a9de2016-08-23 14:23:03 -070093 return -1;
philipel233c4ba2016-08-01 08:49:04 -070094
philipelbf8a2c92016-08-10 19:00:39 +020095 float send_interval_ms = cluster->last_send_ms - cluster->first_send_ms;
96 float receive_interval_ms =
philipel233c4ba2016-08-01 08:49:04 -070097 cluster->last_receive_ms - cluster->first_receive_ms;
98
Irfan Sherifff99a9de2016-08-23 14:23:03 -070099 if (send_interval_ms <= 0 || send_interval_ms > kMaxProbeIntervalMs ||
100 receive_interval_ms <= 0 || receive_interval_ms > kMaxProbeIntervalMs) {
philipel233c4ba2016-08-01 08:49:04 -0700101 LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval"
philipel8aadd502017-02-23 02:56:13 -0800102 << " [cluster id: " << cluster_id
philipel233c4ba2016-08-01 08:49:04 -0700103 << "] [send interval: " << send_interval_ms << " ms]"
104 << " [receive interval: " << receive_interval_ms << " ms]";
philipelf4238f92017-03-28 04:18:02 -0700105 if (event_log_) {
Elad Alon1d87b0e2017-10-03 15:01:03 +0200106 event_log_->LogProbeResultFailure(
107 cluster_id, ProbeFailureReason::kInvalidSendReceiveInterval);
philipelf4238f92017-03-28 04:18:02 -0700108 }
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700109 return -1;
philipel233c4ba2016-08-01 08:49:04 -0700110 }
philipel0561bdf2016-08-24 03:43:53 -0700111 // 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
philipel233c4ba2016-08-01 08:49:04 -0700125 float ratio = receive_bps / send_bps;
terelius37647302017-06-27 07:50:31 -0700126 if (ratio > kMaxValidRatio) {
philipel233c4ba2016-08-01 08:49:04 -0700127 LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high"
philipel8aadd502017-02-23 02:56:13 -0800128 << " [cluster id: " << cluster_id << "] [send: " << send_size
129 << " bytes / " << send_interval_ms
philipel0561bdf2016-08-24 03:43:53 -0700130 << " ms = " << send_bps / 1000 << " kb/s]"
131 << " [receive: " << receive_size << " bytes / "
philipel233c4ba2016-08-01 08:49:04 -0700132 << receive_interval_ms << " ms = " << receive_bps / 1000
133 << " kb/s]"
134 << " [ratio: " << receive_bps / 1000 << " / "
terelius37647302017-06-27 07:50:31 -0700135 << send_bps / 1000 << " = " << ratio << " > kMaxValidRatio ("
136 << kMaxValidRatio << ")]";
philipelf4238f92017-03-28 04:18:02 -0700137 if (event_log_)
Elad Alon1d87b0e2017-10-03 15:01:03 +0200138 event_log_->LogProbeResultFailure(
139 cluster_id, ProbeFailureReason::kInvalidSendReceiveRatio);
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700140 return -1;
philipel233c4ba2016-08-01 08:49:04 -0700141 }
philipel233c4ba2016-08-01 08:49:04 -0700142 LOG(LS_INFO) << "Probing successful"
philipel8aadd502017-02-23 02:56:13 -0800143 << " [cluster id: " << cluster_id << "] [send: " << send_size
144 << " bytes / " << send_interval_ms << " ms = " << send_bps / 1000
145 << " kb/s]"
philipel0561bdf2016-08-24 03:43:53 -0700146 << " [receive: " << receive_size << " bytes / "
philipel233c4ba2016-08-01 08:49:04 -0700147 << receive_interval_ms << " ms = " << receive_bps / 1000
148 << " kb/s]";
michaelt8490f8a2017-04-20 10:10:10 -0700149
philipelf4238f92017-03-28 04:18:02 -0700150 float res = std::min(send_bps, receive_bps);
terelius37647302017-06-27 07:50:31 -0700151 // If we're receiving at significantly lower bitrate than we were sending at,
152 // it suggests that we've found the true capacity of the link. In this case,
153 // set the target bitrate slightly lower to not immediately overuse.
154 if (receive_bps < kMinRatioForUnsaturatedLink * send_bps) {
155 RTC_DCHECK_GT(send_bps, receive_bps);
156 res = kTargetUtilizationFraction * receive_bps;
157 }
philipelf4238f92017-03-28 04:18:02 -0700158 if (event_log_)
159 event_log_->LogProbeResultSuccess(cluster_id, res);
terelius91047e52017-06-19 06:07:30 -0700160 estimated_bitrate_bps_ = rtc::Optional<int>(res);
michaelt8490f8a2017-04-20 10:10:10 -0700161 return *estimated_bitrate_bps_;
162}
163
164rtc::Optional<int>
165ProbeBitrateEstimator::FetchAndResetLastEstimatedBitrateBps() {
166 rtc::Optional<int> estimated_bitrate_bps = estimated_bitrate_bps_;
167 estimated_bitrate_bps_.reset();
168 return estimated_bitrate_bps;
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700169}
philipel233c4ba2016-08-01 08:49:04 -0700170
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700171void ProbeBitrateEstimator::EraseOldClusters(int64_t timestamp_ms) {
172 for (auto it = clusters_.begin(); it != clusters_.end();) {
173 if (it->second.last_receive_ms < timestamp_ms) {
174 it = clusters_.erase(it);
175 } else {
176 ++it;
177 }
178 }
philipel233c4ba2016-08-01 08:49:04 -0700179}
180} // namespace webrtc