philipel | 863a826 | 2016-06-17 09:21:34 -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/delay_based_bwe.h" |
| 12 | |
| 13 | #include <math.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | |
| 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/constructormagic.h" |
| 19 | #include "webrtc/base/logging.h" |
| 20 | #include "webrtc/base/thread_annotations.h" |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 21 | #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 22 | #include "webrtc/modules/pacing/paced_sender.h" |
| 23 | #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/metrics.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 25 | #include "webrtc/typedefs.h" |
| 26 | |
| 27 | namespace { |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 28 | constexpr int kTimestampGroupLengthMs = 5; |
| 29 | constexpr int kAbsSendTimeFraction = 18; |
| 30 | constexpr int kAbsSendTimeInterArrivalUpshift = 8; |
| 31 | constexpr int kInterArrivalShift = |
| 32 | kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift; |
| 33 | constexpr double kTimestampToMs = |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 34 | 1000.0 / static_cast<double>(1 << kInterArrivalShift); |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 35 | // This ssrc is used to fulfill the current API but will be removed |
| 36 | // after the API has been changed. |
| 37 | constexpr uint32_t kFixedSsrc = 0; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 38 | } // namespace |
| 39 | |
| 40 | namespace webrtc { |
| 41 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 42 | DelayBasedBwe::DelayBasedBwe(Clock* clock) |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 43 | : clock_(clock), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 44 | inter_arrival_(), |
| 45 | estimator_(), |
| 46 | detector_(OverUseDetectorOptions()), |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 47 | receiver_incoming_bitrate_(kBitrateWindowMs, 8000), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 48 | last_update_ms_(-1), |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 49 | last_seen_packet_ms_(-1), |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 50 | uma_recorded_(false) { |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 51 | network_thread_.DetachFromThread(); |
| 52 | } |
| 53 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 54 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector( |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 55 | const std::vector<PacketInfo>& packet_feedback_vector) { |
| 56 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 57 | if (!uma_recorded_) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 58 | RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
| 59 | BweNames::kSendSideTransportSeqNum, |
| 60 | BweNames::kBweNamesMax); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame] | 61 | uma_recorded_ = true; |
| 62 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 63 | Result aggregated_result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 64 | for (const auto& packet_info : packet_feedback_vector) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 65 | Result result = IncomingPacketInfo(packet_info); |
| 66 | if (result.updated) |
| 67 | aggregated_result = result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 68 | } |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 69 | return aggregated_result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 72 | DelayBasedBwe::Result DelayBasedBwe::IncomingPacketInfo( |
| 73 | const PacketInfo& info) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 74 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 75 | |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 76 | receiver_incoming_bitrate_.Update(info.payload_size, info.arrival_time_ms); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 77 | Result result; |
| 78 | // Reset if the stream has timed out. |
| 79 | if (last_seen_packet_ms_ == -1 || |
| 80 | now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) { |
| 81 | inter_arrival_.reset( |
| 82 | new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
| 83 | kTimestampToMs, true)); |
| 84 | estimator_.reset(new OveruseEstimator(OverUseDetectorOptions())); |
| 85 | } |
| 86 | last_seen_packet_ms_ = now_ms; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 87 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 88 | uint32_t send_time_24bits = |
| 89 | static_cast<uint32_t>( |
| 90 | ((static_cast<uint64_t>(info.send_time_ms) << kAbsSendTimeFraction) + |
| 91 | 500) / |
| 92 | 1000) & |
| 93 | 0x00FFFFFF; |
| 94 | // Shift up send time to use the full 32 bits that inter_arrival works with, |
| 95 | // so wrapping works properly. |
| 96 | uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift; |
stefan | fd0d426 | 2016-09-29 02:44:31 -0700 | [diff] [blame] | 97 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 98 | uint32_t ts_delta = 0; |
| 99 | int64_t t_delta = 0; |
| 100 | int size_delta = 0; |
| 101 | if (inter_arrival_->ComputeDeltas(timestamp, info.arrival_time_ms, now_ms, |
| 102 | info.payload_size, &ts_delta, &t_delta, |
| 103 | &size_delta)) { |
| 104 | double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift); |
| 105 | estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State(), |
| 106 | info.arrival_time_ms); |
| 107 | detector_.Detect(estimator_->offset(), ts_delta_ms, |
| 108 | estimator_->num_of_deltas(), info.arrival_time_ms); |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 111 | int probing_bps = 0; |
| 112 | if (info.probe_cluster_id != PacketInfo::kNotAProbe) { |
| 113 | probing_bps = probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(info); |
| 114 | } |
| 115 | |
| 116 | // Currently overusing the bandwidth. |
| 117 | if (detector_.State() == kBwOverusing) { |
| 118 | rtc::Optional<uint32_t> incoming_rate = |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 119 | receiver_incoming_bitrate_.Rate(info.arrival_time_ms); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 120 | if (incoming_rate && |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 121 | rate_control_.TimeToReduceFurther(now_ms, *incoming_rate)) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 122 | result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, |
| 123 | &result.target_bitrate_bps); |
| 124 | } |
| 125 | } else if (probing_bps > 0) { |
| 126 | // No overuse, but probing measured a bitrate. |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 127 | rate_control_.SetEstimate(probing_bps, info.arrival_time_ms); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 128 | result.probe = true; |
| 129 | result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, |
| 130 | &result.target_bitrate_bps); |
| 131 | } |
| 132 | rtc::Optional<uint32_t> incoming_rate = |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 133 | receiver_incoming_bitrate_.Rate(info.arrival_time_ms); |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 134 | if (!result.updated && |
| 135 | (last_update_ms_ == -1 || |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 136 | now_ms - last_update_ms_ > rate_control_.GetFeedbackInterval())) { |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 137 | result.updated = UpdateEstimate(info.arrival_time_ms, now_ms, |
| 138 | &result.target_bitrate_bps); |
| 139 | } |
| 140 | if (result.updated) |
stefan | 5ec85fb | 2016-09-29 04:19:38 -0700 | [diff] [blame] | 141 | last_update_ms_ = now_ms; |
Stefan Holmer | 280de9e | 2016-09-30 10:06:51 +0200 | [diff] [blame] | 142 | |
| 143 | return result; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 146 | bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms, |
| 147 | int64_t now_ms, |
| 148 | uint32_t* target_bitrate_bps) { |
| 149 | // The first overuse should immediately trigger a new estimate. |
| 150 | // We also have to update the estimate immediately if we are overusing |
| 151 | // and the target bitrate is too high compared to what we are receiving. |
| 152 | const RateControlInput input(detector_.State(), |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 153 | receiver_incoming_bitrate_.Rate(arrival_time_ms), |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 154 | estimator_->var_noise()); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 155 | rate_control_.Update(&input, now_ms); |
| 156 | *target_bitrate_bps = rate_control_.UpdateBandwidthEstimate(now_ms); |
| 157 | return rate_control_.ValidEstimate(); |
Irfan Sheriff | b2540bb | 2016-09-12 12:28:54 -0700 | [diff] [blame] | 158 | } |
| 159 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 160 | void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 161 | rate_control_.SetRtt(avg_rtt_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 164 | bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs, |
| 165 | uint32_t* bitrate_bps) const { |
| 166 | // Currently accessed from both the process thread (see |
| 167 | // ModuleRtpRtcpImpl::Process()) and the configuration thread (see |
| 168 | // Call::GetStats()). Should in the future only be accessed from a single |
| 169 | // thread. |
| 170 | RTC_DCHECK(ssrcs); |
| 171 | RTC_DCHECK(bitrate_bps); |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 172 | if (!rate_control_.ValidEstimate()) |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 173 | return false; |
philipel | 7522a28 | 2016-08-16 10:59:36 +0200 | [diff] [blame] | 174 | |
| 175 | *ssrcs = {kFixedSsrc}; |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 176 | *bitrate_bps = rate_control_.LatestEstimate(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
| 181 | // Called from both the configuration thread and the network thread. Shouldn't |
| 182 | // be called from the network thread in the future. |
terelius | 6ed592d | 2016-10-18 05:55:30 -0700 | [diff] [blame^] | 183 | rate_control_.SetMinBitrate(min_bitrate_bps); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 184 | } |
| 185 | } // namespace webrtc |