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