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 "modules/congestion_controller/goog_cc/probe_bitrate_estimator.h"
12
13 #include <algorithm>
14 #include <memory>
15
16 #include "api/rtc_event_log/rtc_event_log.h"
17 #include "logging/rtc_event_log/events/rtc_event_probe_result_failure.h"
18 #include "logging/rtc_event_log/events/rtc_event_probe_result_success.h"
19 #include "rtc_base/checks.h"
20 #include "rtc_base/logging.h"
21 #include "rtc_base/numerics/safe_conversions.h"
22
23 namespace webrtc {
24 namespace {
25 // The minumum number of probes we need to receive feedback about in percent
26 // in order to have a valid estimate.
27 constexpr double kMinReceivedProbesRatio = .80;
28
29 // The minumum number of bytes we need to receive feedback about in percent
30 // in order to have a valid estimate.
31 constexpr double kMinReceivedBytesRatio = .80;
32
33 // The maximum |receive rate| / |send rate| ratio for a valid estimate.
34 constexpr float kMaxValidRatio = 2.0f;
35
36 // The minimum |receive rate| / |send rate| ratio assuming that the link is
37 // not saturated, i.e. we assume that we will receive at least
38 // kMinRatioForUnsaturatedLink * |send rate| if |send rate| is less than the
39 // link capacity.
40 constexpr float kMinRatioForUnsaturatedLink = 0.9f;
41
42 // The target utilization of the link. If we know true link capacity
43 // we'd like to send at 95% of that rate.
44 constexpr float kTargetUtilizationFraction = 0.95f;
45
46 // The maximum time period over which the cluster history is retained.
47 // This is also the maximum time period beyond which a probing burst is not
48 // expected to last.
49 constexpr TimeDelta kMaxClusterHistory = TimeDelta::Seconds(1);
50
51 // The maximum time interval between first and the last probe on a cluster
52 // on the sender side as well as the receive side.
53 constexpr TimeDelta kMaxProbeInterval = TimeDelta::Seconds(1);
54
55 } // namespace
56
ProbeBitrateEstimator(RtcEventLog * event_log)57 ProbeBitrateEstimator::ProbeBitrateEstimator(RtcEventLog* event_log)
58 : event_log_(event_log) {}
59
60 ProbeBitrateEstimator::~ProbeBitrateEstimator() = default;
61
HandleProbeAndEstimateBitrate(const PacketResult & packet_feedback)62 absl::optional<DataRate> ProbeBitrateEstimator::HandleProbeAndEstimateBitrate(
63 const PacketResult& packet_feedback) {
64 int cluster_id = packet_feedback.sent_packet.pacing_info.probe_cluster_id;
65 RTC_DCHECK_NE(cluster_id, PacedPacketInfo::kNotAProbe);
66
67 EraseOldClusters(packet_feedback.receive_time);
68
69 AggregatedCluster* cluster = &clusters_[cluster_id];
70
71 if (packet_feedback.sent_packet.send_time < cluster->first_send) {
72 cluster->first_send = packet_feedback.sent_packet.send_time;
73 }
74 if (packet_feedback.sent_packet.send_time > cluster->last_send) {
75 cluster->last_send = packet_feedback.sent_packet.send_time;
76 cluster->size_last_send = packet_feedback.sent_packet.size;
77 }
78 if (packet_feedback.receive_time < cluster->first_receive) {
79 cluster->first_receive = packet_feedback.receive_time;
80 cluster->size_first_receive = packet_feedback.sent_packet.size;
81 }
82 if (packet_feedback.receive_time > cluster->last_receive) {
83 cluster->last_receive = packet_feedback.receive_time;
84 }
85 cluster->size_total += packet_feedback.sent_packet.size;
86 cluster->num_probes += 1;
87
88 RTC_DCHECK_GT(
89 packet_feedback.sent_packet.pacing_info.probe_cluster_min_probes, 0);
90 RTC_DCHECK_GT(packet_feedback.sent_packet.pacing_info.probe_cluster_min_bytes,
91 0);
92
93 int min_probes =
94 packet_feedback.sent_packet.pacing_info.probe_cluster_min_probes *
95 kMinReceivedProbesRatio;
96 DataSize min_size =
97 DataSize::Bytes(
98 packet_feedback.sent_packet.pacing_info.probe_cluster_min_bytes) *
99 kMinReceivedBytesRatio;
100 if (cluster->num_probes < min_probes || cluster->size_total < min_size)
101 return absl::nullopt;
102
103 TimeDelta send_interval = cluster->last_send - cluster->first_send;
104 TimeDelta receive_interval = cluster->last_receive - cluster->first_receive;
105
106 if (send_interval <= TimeDelta::Zero() || send_interval > kMaxProbeInterval ||
107 receive_interval <= TimeDelta::Zero() ||
108 receive_interval > kMaxProbeInterval) {
109 RTC_LOG(LS_INFO) << "Probing unsuccessful, invalid send/receive interval"
110 " [cluster id: "
111 << cluster_id
112 << "] [send interval: " << ToString(send_interval)
113 << "]"
114 " [receive interval: "
115 << ToString(receive_interval) << "]";
116 if (event_log_) {
117 event_log_->Log(std::make_unique<RtcEventProbeResultFailure>(
118 cluster_id, ProbeFailureReason::kInvalidSendReceiveInterval));
119 }
120 return absl::nullopt;
121 }
122 // Since the |send_interval| does not include the time it takes to actually
123 // send the last packet the size of the last sent packet should not be
124 // included when calculating the send bitrate.
125 RTC_DCHECK_GT(cluster->size_total, cluster->size_last_send);
126 DataSize send_size = cluster->size_total - cluster->size_last_send;
127 DataRate send_rate = send_size / send_interval;
128
129 // Since the |receive_interval| does not include the time it takes to
130 // actually receive the first packet the size of the first received packet
131 // should not be included when calculating the receive bitrate.
132 RTC_DCHECK_GT(cluster->size_total, cluster->size_first_receive);
133 DataSize receive_size = cluster->size_total - cluster->size_first_receive;
134 DataRate receive_rate = receive_size / receive_interval;
135
136 double ratio = receive_rate / send_rate;
137 if (ratio > kMaxValidRatio) {
138 RTC_LOG(LS_INFO) << "Probing unsuccessful, receive/send ratio too high"
139 " [cluster id: "
140 << cluster_id << "] [send: " << ToString(send_size)
141 << " / " << ToString(send_interval) << " = "
142 << ToString(send_rate)
143 << "]"
144 " [receive: "
145 << ToString(receive_size) << " / "
146 << ToString(receive_interval) << " = "
147 << ToString(receive_rate)
148 << " ]"
149 " [ratio: "
150 << ToString(receive_rate) << " / " << ToString(send_rate)
151 << " = " << ratio << " > kMaxValidRatio ("
152 << kMaxValidRatio << ")]";
153 if (event_log_) {
154 event_log_->Log(std::make_unique<RtcEventProbeResultFailure>(
155 cluster_id, ProbeFailureReason::kInvalidSendReceiveRatio));
156 }
157 return absl::nullopt;
158 }
159 RTC_LOG(LS_INFO) << "Probing successful"
160 " [cluster id: "
161 << cluster_id << "] [send: " << ToString(send_size) << " / "
162 << ToString(send_interval) << " = " << ToString(send_rate)
163 << " ]"
164 " [receive: "
165 << ToString(receive_size) << " / "
166 << ToString(receive_interval) << " = "
167 << ToString(receive_rate) << "]";
168
169 DataRate res = std::min(send_rate, receive_rate);
170 // If we're receiving at significantly lower bitrate than we were sending at,
171 // it suggests that we've found the true capacity of the link. In this case,
172 // set the target bitrate slightly lower to not immediately overuse.
173 if (receive_rate < kMinRatioForUnsaturatedLink * send_rate) {
174 RTC_DCHECK_GT(send_rate, receive_rate);
175 res = kTargetUtilizationFraction * receive_rate;
176 }
177 if (event_log_) {
178 event_log_->Log(
179 std::make_unique<RtcEventProbeResultSuccess>(cluster_id, res.bps()));
180 }
181 estimated_data_rate_ = res;
182 return estimated_data_rate_;
183 }
184
185 absl::optional<DataRate>
FetchAndResetLastEstimatedBitrate()186 ProbeBitrateEstimator::FetchAndResetLastEstimatedBitrate() {
187 absl::optional<DataRate> estimated_data_rate = estimated_data_rate_;
188 estimated_data_rate_.reset();
189 return estimated_data_rate;
190 }
191
EraseOldClusters(Timestamp timestamp)192 void ProbeBitrateEstimator::EraseOldClusters(Timestamp timestamp) {
193 for (auto it = clusters_.begin(); it != clusters_.end();) {
194 if (it->second.last_receive + kMaxClusterHistory < timestamp) {
195 it = clusters_.erase(it);
196 } else {
197 ++it;
198 }
199 }
200 }
201 } // namespace webrtc
202