blob: 8fa22e7dcd6edfc9f379d5df0c0c225537eac1b5 [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
11#include "webrtc/modules/congestion_controller/probe_bitrate_estimator.h"
12
13#include <algorithm>
14
philipelf4238f92017-03-28 04:18:02 -070015#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020016#include "webrtc/rtc_base/checks.h"
17#include "webrtc/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_) {
106 event_log_->LogProbeResultFailure(cluster_id,
107 kInvalidSendReceiveInterval);
108 }
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_)
138 event_log_->LogProbeResultFailure(cluster_id, kInvalidSendReceiveRatio);
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700139 return -1;
philipel233c4ba2016-08-01 08:49:04 -0700140 }
philipel233c4ba2016-08-01 08:49:04 -0700141 LOG(LS_INFO) << "Probing successful"
philipel8aadd502017-02-23 02:56:13 -0800142 << " [cluster id: " << cluster_id << "] [send: " << send_size
143 << " bytes / " << send_interval_ms << " ms = " << send_bps / 1000
144 << " kb/s]"
philipel0561bdf2016-08-24 03:43:53 -0700145 << " [receive: " << receive_size << " bytes / "
philipel233c4ba2016-08-01 08:49:04 -0700146 << receive_interval_ms << " ms = " << receive_bps / 1000
147 << " kb/s]";
michaelt8490f8a2017-04-20 10:10:10 -0700148
philipelf4238f92017-03-28 04:18:02 -0700149 float res = std::min(send_bps, receive_bps);
terelius37647302017-06-27 07:50:31 -0700150 // 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 }
philipelf4238f92017-03-28 04:18:02 -0700157 if (event_log_)
158 event_log_->LogProbeResultSuccess(cluster_id, res);
terelius91047e52017-06-19 06:07:30 -0700159 estimated_bitrate_bps_ = rtc::Optional<int>(res);
michaelt8490f8a2017-04-20 10:10:10 -0700160 return *estimated_bitrate_bps_;
161}
162
163rtc::Optional<int>
164ProbeBitrateEstimator::FetchAndResetLastEstimatedBitrateBps() {
165 rtc::Optional<int> estimated_bitrate_bps = estimated_bitrate_bps_;
166 estimated_bitrate_bps_.reset();
167 return estimated_bitrate_bps;
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700168}
philipel233c4ba2016-08-01 08:49:04 -0700169
Irfan Sherifff99a9de2016-08-23 14:23:03 -0700170void 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 }
philipel233c4ba2016-08-01 08:49:04 -0700178}
179} // namespace webrtc