blob: bd9f3eef31c84a3d067dba5e8f337a9105f02dd2 [file] [log] [blame]
philipel863a8262016-06-17 09:21:34 -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/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"
stefan64636dd2016-08-03 00:29:03 -070024#include "webrtc/system_wrappers/include/metrics.h"
philipel863a8262016-06-17 09:21:34 -070025#include "webrtc/typedefs.h"
26
27namespace {
philipel7522a282016-08-16 10:59:36 +020028constexpr int kTimestampGroupLengthMs = 5;
29constexpr int kAbsSendTimeFraction = 18;
30constexpr int kAbsSendTimeInterArrivalUpshift = 8;
31constexpr int kInterArrivalShift =
32 kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift;
33constexpr double kTimestampToMs =
philipel863a8262016-06-17 09:21:34 -070034 1000.0 / static_cast<double>(1 << kInterArrivalShift);
philipel7522a282016-08-16 10:59:36 +020035// This ssrc is used to fulfill the current API but will be removed
36// after the API has been changed.
37constexpr uint32_t kFixedSsrc = 0;
philipel863a8262016-06-17 09:21:34 -070038} // namespace
39
40namespace webrtc {
41
stefan5e12d362016-07-11 01:44:02 -070042DelayBasedBwe::DelayBasedBwe(RemoteBitrateObserver* observer, Clock* clock)
43 : clock_(clock),
44 observer_(observer),
philipel863a8262016-06-17 09:21:34 -070045 inter_arrival_(),
46 estimator_(),
47 detector_(OverUseDetectorOptions()),
48 incoming_bitrate_(kBitrateWindowMs, 8000),
philipel863a8262016-06-17 09:21:34 -070049 first_packet_time_ms_(-1),
50 last_update_ms_(-1),
philipel7522a282016-08-16 10:59:36 +020051 last_seen_packet_ms_(-1),
stefan64636dd2016-08-03 00:29:03 -070052 uma_recorded_(false) {
philipel863a8262016-06-17 09:21:34 -070053 RTC_DCHECK(observer_);
philipel863a8262016-06-17 09:21:34 -070054 network_thread_.DetachFromThread();
55}
56
philipel863a8262016-06-17 09:21:34 -070057void DelayBasedBwe::IncomingPacketFeedbackVector(
58 const std::vector<PacketInfo>& packet_feedback_vector) {
59 RTC_DCHECK(network_thread_.CalledOnValidThread());
stefan64636dd2016-08-03 00:29:03 -070060 if (!uma_recorded_) {
asapersson1d02d3e2016-09-09 22:40:25 -070061 RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram,
62 BweNames::kSendSideTransportSeqNum,
63 BweNames::kBweNamesMax);
stefan64636dd2016-08-03 00:29:03 -070064 uma_recorded_ = true;
65 }
philipel863a8262016-06-17 09:21:34 -070066 for (const auto& packet_info : packet_feedback_vector) {
philipel7522a282016-08-16 10:59:36 +020067 IncomingPacketInfo(packet_info);
philipel863a8262016-06-17 09:21:34 -070068 }
69}
70
philipel7522a282016-08-16 10:59:36 +020071void DelayBasedBwe::IncomingPacketInfo(const PacketInfo& info) {
stefan5e12d362016-07-11 01:44:02 -070072 int64_t now_ms = clock_->TimeInMilliseconds();
philipel863a8262016-06-17 09:21:34 -070073
74 if (first_packet_time_ms_ == -1)
stefan5e12d362016-07-11 01:44:02 -070075 first_packet_time_ms_ = now_ms;
philipel863a8262016-06-17 09:21:34 -070076
philipel7522a282016-08-16 10:59:36 +020077 incoming_bitrate_.Update(info.payload_size, info.arrival_time_ms);
philipel863a8262016-06-17 09:21:34 -070078 bool update_estimate = false;
79 uint32_t target_bitrate_bps = 0;
philipel863a8262016-06-17 09:21:34 -070080 {
81 rtc::CritScope lock(&crit_);
82
philipel7522a282016-08-16 10:59:36 +020083 // Reset if the stream has timed out.
84 if (last_seen_packet_ms_ == -1 ||
85 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) {
86 inter_arrival_.reset(new InterArrival(
87 (kTimestampGroupLengthMs << kInterArrivalShift) / 1000,
88 kTimestampToMs, true));
89 estimator_.reset(new OveruseEstimator(OverUseDetectorOptions()));
philipel863a8262016-06-17 09:21:34 -070090 }
philipel7522a282016-08-16 10:59:36 +020091 last_seen_packet_ms_ = now_ms;
92
93 if (info.probe_cluster_id != PacketInfo::kNotAProbe) {
Irfan Sherifff99a9de2016-08-23 14:23:03 -070094 int bps = probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(info);
95 if (bps > 0) {
96 remote_rate_.SetEstimate(bps, info.arrival_time_ms);
philipel0aa9d182016-08-24 02:45:35 -070097 observer_->OnProbeBitrate(bps);
philipel7522a282016-08-16 10:59:36 +020098 update_estimate = true;
99 }
100 }
101
102 uint32_t send_time_24bits =
103 static_cast<uint32_t>(((static_cast<uint64_t>(info.send_time_ms)
104 << kAbsSendTimeFraction) +
105 500) /
106 1000) &
107 0x00FFFFFF;
108 // Shift up send time to use the full 32 bits that inter_arrival works with,
109 // so wrapping works properly.
110 uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift;
111
112 uint32_t ts_delta = 0;
113 int64_t t_delta = 0;
114 int size_delta = 0;
115 if (inter_arrival_->ComputeDeltas(timestamp, info.arrival_time_ms, now_ms,
116 info.payload_size, &ts_delta, &t_delta,
stefan5e12d362016-07-11 01:44:02 -0700117 &size_delta)) {
philipel863a8262016-06-17 09:21:34 -0700118 double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift);
119 estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State());
120 detector_.Detect(estimator_->offset(), ts_delta_ms,
philipel7522a282016-08-16 10:59:36 +0200121 estimator_->num_of_deltas(), info.arrival_time_ms);
philipel863a8262016-06-17 09:21:34 -0700122 }
123
124 if (!update_estimate) {
125 // Check if it's time for a periodic update or if we should update because
126 // of an over-use.
127 if (last_update_ms_ == -1 ||
128 now_ms - last_update_ms_ > remote_rate_.GetFeedbackInterval()) {
129 update_estimate = true;
130 } else if (detector_.State() == kBwOverusing) {
stefan5e12d362016-07-11 01:44:02 -0700131 rtc::Optional<uint32_t> incoming_rate =
philipel7522a282016-08-16 10:59:36 +0200132 incoming_bitrate_.Rate(info.arrival_time_ms);
philipel863a8262016-06-17 09:21:34 -0700133 if (incoming_rate &&
134 remote_rate_.TimeToReduceFurther(now_ms, *incoming_rate)) {
135 update_estimate = true;
136 }
137 }
138 }
139
140 if (update_estimate) {
141 // The first overuse should immediately trigger a new estimate.
142 // We also have to update the estimate immediately if we are overusing
143 // and the target bitrate is too high compared to what we are receiving.
144 const RateControlInput input(detector_.State(),
philipel7522a282016-08-16 10:59:36 +0200145 incoming_bitrate_.Rate(info.arrival_time_ms),
philipel863a8262016-06-17 09:21:34 -0700146 estimator_->var_noise());
147 remote_rate_.Update(&input, now_ms);
148 target_bitrate_bps = remote_rate_.UpdateBandwidthEstimate(now_ms);
149 update_estimate = remote_rate_.ValidEstimate();
philipel863a8262016-06-17 09:21:34 -0700150 }
151 }
philipel7522a282016-08-16 10:59:36 +0200152
philipel863a8262016-06-17 09:21:34 -0700153 if (update_estimate) {
154 last_update_ms_ = now_ms;
philipel7522a282016-08-16 10:59:36 +0200155 observer_->OnReceiveBitrateChanged({kFixedSsrc}, target_bitrate_bps);
philipel863a8262016-06-17 09:21:34 -0700156 }
157}
158
159void DelayBasedBwe::Process() {}
160
161int64_t DelayBasedBwe::TimeUntilNextProcess() {
162 const int64_t kDisabledModuleTime = 1000;
163 return kDisabledModuleTime;
164}
165
philipel863a8262016-06-17 09:21:34 -0700166void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
167 rtc::CritScope lock(&crit_);
168 remote_rate_.SetRtt(avg_rtt_ms);
169}
170
philipel7522a282016-08-16 10:59:36 +0200171void DelayBasedBwe::RemoveStream(uint32_t ssrc) {}
philipel863a8262016-06-17 09:21:34 -0700172
173bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs,
174 uint32_t* bitrate_bps) const {
175 // Currently accessed from both the process thread (see
176 // ModuleRtpRtcpImpl::Process()) and the configuration thread (see
177 // Call::GetStats()). Should in the future only be accessed from a single
178 // thread.
179 RTC_DCHECK(ssrcs);
180 RTC_DCHECK(bitrate_bps);
181 rtc::CritScope lock(&crit_);
philipel7522a282016-08-16 10:59:36 +0200182 if (!remote_rate_.ValidEstimate())
philipel863a8262016-06-17 09:21:34 -0700183 return false;
philipel7522a282016-08-16 10:59:36 +0200184
185 *ssrcs = {kFixedSsrc};
186 *bitrate_bps = remote_rate_.LatestEstimate();
philipel863a8262016-06-17 09:21:34 -0700187 return true;
188}
189
190void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) {
191 // Called from both the configuration thread and the network thread. Shouldn't
192 // be called from the network thread in the future.
193 rtc::CritScope lock(&crit_);
194 remote_rate_.SetMinBitrate(min_bitrate_bps);
195}
196} // namespace webrtc