blob: db3ae582f1795c99b4b9b4f2f95a2dc633a1597f [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"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070021#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
philipel863a8262016-06-17 09:21:34 -070022#include "webrtc/modules/pacing/paced_sender.h"
23#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.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
stefanfd0d4262016-09-29 02:44:31 -070042DelayBasedBwe::DelayBasedBwe(Clock* clock)
stefan5e12d362016-07-11 01:44:02 -070043 : clock_(clock),
philipel863a8262016-06-17 09:21:34 -070044 inter_arrival_(),
45 estimator_(),
46 detector_(OverUseDetectorOptions()),
47 incoming_bitrate_(kBitrateWindowMs, 8000),
philipel863a8262016-06-17 09:21:34 -070048 last_update_ms_(-1),
philipel7522a282016-08-16 10:59:36 +020049 last_seen_packet_ms_(-1),
stefan64636dd2016-08-03 00:29:03 -070050 uma_recorded_(false) {
philipel863a8262016-06-17 09:21:34 -070051 network_thread_.DetachFromThread();
52}
53
stefanfd0d4262016-09-29 02:44:31 -070054DelayBasedBwe::Result DelayBasedBwe::IncomingPacketFeedbackVector(
philipel863a8262016-06-17 09:21:34 -070055 const std::vector<PacketInfo>& packet_feedback_vector) {
56 RTC_DCHECK(network_thread_.CalledOnValidThread());
stefan64636dd2016-08-03 00:29:03 -070057 if (!uma_recorded_) {
asapersson1d02d3e2016-09-09 22:40:25 -070058 RTC_HISTOGRAM_ENUMERATION(kBweTypeHistogram,
59 BweNames::kSendSideTransportSeqNum,
60 BweNames::kBweNamesMax);
stefan64636dd2016-08-03 00:29:03 -070061 uma_recorded_ = true;
62 }
stefanfd0d4262016-09-29 02:44:31 -070063 Result aggregated_result;
philipel863a8262016-06-17 09:21:34 -070064 for (const auto& packet_info : packet_feedback_vector) {
stefanfd0d4262016-09-29 02:44:31 -070065 Result result = IncomingPacketInfo(packet_info);
66 if (result.updated)
67 aggregated_result = result;
philipel863a8262016-06-17 09:21:34 -070068 }
stefanfd0d4262016-09-29 02:44:31 -070069 return aggregated_result;
philipel863a8262016-06-17 09:21:34 -070070}
71
stefanfd0d4262016-09-29 02:44:31 -070072DelayBasedBwe::Result DelayBasedBwe::IncomingPacketInfo(
73 const PacketInfo& info) {
74 // printf("Acked: %ld\n", info.payload_size);
stefan5e12d362016-07-11 01:44:02 -070075 int64_t now_ms = clock_->TimeInMilliseconds();
philipel863a8262016-06-17 09:21:34 -070076
philipel7522a282016-08-16 10:59:36 +020077 incoming_bitrate_.Update(info.payload_size, info.arrival_time_ms);
stefanfd0d4262016-09-29 02:44:31 -070078 Result result;
79 // Reset if the stream has timed out.
80 if (last_seen_packet_ms_ == -1 ||
81 now_ms - last_seen_packet_ms_ > kStreamTimeOutMs) {
82 inter_arrival_.reset(new InterArrival(
83 (kTimestampGroupLengthMs << kInterArrivalShift) / 1000,
84 kTimestampToMs, true));
85 estimator_.reset(new OveruseEstimator(OverUseDetectorOptions()));
86 }
87 last_seen_packet_ms_ = now_ms;
philipel863a8262016-06-17 09:21:34 -070088
stefanfd0d4262016-09-29 02:44:31 -070089 uint32_t send_time_24bits =
90 static_cast<uint32_t>(((static_cast<uint64_t>(info.send_time_ms)
91 << kAbsSendTimeFraction) +
92 500) /
93 1000) &
94 0x00FFFFFF;
95 // Shift up send time to use the full 32 bits that inter_arrival works with,
96 // so wrapping works properly.
97 uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift;
philipel7522a282016-08-16 10:59:36 +020098
stefanfd0d4262016-09-29 02:44:31 -070099 uint32_t ts_delta = 0;
100 int64_t t_delta = 0;
101 int size_delta = 0;
102 if (inter_arrival_->ComputeDeltas(timestamp, info.arrival_time_ms, now_ms,
103 info.payload_size, &ts_delta, &t_delta,
104 &size_delta)) {
105 double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift);
106 estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State(),
107 info.arrival_time_ms);
108 detector_.Detect(estimator_->offset(), ts_delta_ms,
109 estimator_->num_of_deltas(), info.arrival_time_ms);
philipel863a8262016-06-17 09:21:34 -0700110 }
philipel7522a282016-08-16 10:59:36 +0200111
stefanfd0d4262016-09-29 02:44:31 -0700112 int probing_bps = 0;
113 if (info.probe_cluster_id != PacketInfo::kNotAProbe) {
114 probing_bps =
115 probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(info);
116 }
117
118 // Currently overusing the bandwidth.
119 if (detector_.State() == kBwOverusing) {
120 rtc::Optional<uint32_t> incoming_rate =
121 incoming_bitrate_.Rate(info.arrival_time_ms);
122 if (incoming_rate &&
123 remote_rate_.TimeToReduceFurther(now_ms, *incoming_rate)) {
124 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms,
125 &result.target_bitrate_bps);
126 }
127 } else if (probing_bps > 0) {
128 // No overuse, but probing measured a bitrate.
129 remote_rate_.SetEstimate(probing_bps, info.arrival_time_ms);
130 result.probe = true;
131 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms,
132 &result.target_bitrate_bps);
133 }
134 rtc::Optional<uint32_t> incoming_rate =
135 incoming_bitrate_.Rate(info.arrival_time_ms);
136 if (!result.updated &&
137 (last_update_ms_ == -1 ||
138 now_ms - last_update_ms_ > remote_rate_.GetFeedbackInterval())) {
139 result.updated = UpdateEstimate(info.arrival_time_ms, now_ms,
140 &result.target_bitrate_bps);
141 }
142 if (result.updated)
philipel863a8262016-06-17 09:21:34 -0700143 last_update_ms_ = now_ms;
stefanfd0d4262016-09-29 02:44:31 -0700144
145 return result;
philipel863a8262016-06-17 09:21:34 -0700146}
147
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700148bool DelayBasedBwe::UpdateEstimate(int64_t arrival_time_ms,
149 int64_t now_ms,
150 uint32_t* target_bitrate_bps) {
151 // The first overuse should immediately trigger a new estimate.
152 // We also have to update the estimate immediately if we are overusing
153 // and the target bitrate is too high compared to what we are receiving.
154 const RateControlInput input(detector_.State(),
155 incoming_bitrate_.Rate(arrival_time_ms),
156 estimator_->var_noise());
157 remote_rate_.Update(&input, now_ms);
158 *target_bitrate_bps = remote_rate_.UpdateBandwidthEstimate(now_ms);
159 return remote_rate_.ValidEstimate();
160}
161
philipel863a8262016-06-17 09:21:34 -0700162void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
philipel863a8262016-06-17 09:21:34 -0700163 remote_rate_.SetRtt(avg_rtt_ms);
164}
165
philipel863a8262016-06-17 09:21:34 -0700166bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs,
167 uint32_t* bitrate_bps) const {
168 // Currently accessed from both the process thread (see
169 // ModuleRtpRtcpImpl::Process()) and the configuration thread (see
170 // Call::GetStats()). Should in the future only be accessed from a single
171 // thread.
172 RTC_DCHECK(ssrcs);
173 RTC_DCHECK(bitrate_bps);
philipel7522a282016-08-16 10:59:36 +0200174 if (!remote_rate_.ValidEstimate())
philipel863a8262016-06-17 09:21:34 -0700175 return false;
philipel7522a282016-08-16 10:59:36 +0200176
177 *ssrcs = {kFixedSsrc};
178 *bitrate_bps = remote_rate_.LatestEstimate();
philipel863a8262016-06-17 09:21:34 -0700179 return true;
180}
181
182void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) {
183 // Called from both the configuration thread and the network thread. Shouldn't
184 // be called from the network thread in the future.
philipel863a8262016-06-17 09:21:34 -0700185 remote_rate_.SetMinBitrate(min_bitrate_bps);
186}
187} // namespace webrtc