blob: 61a126fbe331bd8167e50e86dd5ba171ae45fc89 [file] [log] [blame]
nisse559af382017-03-21 06:41:12 -07001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
nisse559af382017-03-21 06:41:12 -070012
Per Kjellander898f0912021-04-21 11:56:32 +020013#include "api/units/data_rate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/pacing/packet_router.h"
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
17#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
18#include "rtc_base/logging.h"
nisse559af382017-03-21 06:41:12 -070019
20namespace webrtc {
21
22namespace {
23static const uint32_t kTimeOffsetSwitchThreshold = 30;
24} // namespace
25
26ReceiveSideCongestionController::WrappingBitrateEstimator::
Sebastian Janssonaa01f272019-01-30 11:28:59 +010027 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
nisse559af382017-03-21 06:41:12 -070028 : observer_(observer),
29 clock_(clock),
30 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
31 using_absolute_send_time_(false),
32 packets_since_absolute_send_time_(0),
33 min_bitrate_bps_(congestion_controller::GetMinBitrateBps()) {}
34
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020035ReceiveSideCongestionController::WrappingBitrateEstimator::
36 ~WrappingBitrateEstimator() = default;
37
nisse559af382017-03-21 06:41:12 -070038void ReceiveSideCongestionController::WrappingBitrateEstimator::IncomingPacket(
39 int64_t arrival_time_ms,
40 size_t payload_size,
41 const RTPHeader& header) {
Markus Handell9c962502020-07-07 22:03:26 +020042 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070043 PickEstimatorFromHeader(header);
44 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
45}
46
47void ReceiveSideCongestionController::WrappingBitrateEstimator::Process() {
Markus Handell9c962502020-07-07 22:03:26 +020048 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070049 rbe_->Process();
50}
51
52int64_t ReceiveSideCongestionController::WrappingBitrateEstimator::
53 TimeUntilNextProcess() {
Markus Handell9c962502020-07-07 22:03:26 +020054 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070055 return rbe_->TimeUntilNextProcess();
56}
57
58void ReceiveSideCongestionController::WrappingBitrateEstimator::OnRttUpdate(
59 int64_t avg_rtt_ms,
60 int64_t max_rtt_ms) {
Markus Handell9c962502020-07-07 22:03:26 +020061 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070062 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
63}
64
65void ReceiveSideCongestionController::WrappingBitrateEstimator::RemoveStream(
66 unsigned int ssrc) {
Markus Handell9c962502020-07-07 22:03:26 +020067 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070068 rbe_->RemoveStream(ssrc);
69}
70
71bool ReceiveSideCongestionController::WrappingBitrateEstimator::LatestEstimate(
72 std::vector<unsigned int>* ssrcs,
73 unsigned int* bitrate_bps) const {
Markus Handell9c962502020-07-07 22:03:26 +020074 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070075 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
76}
77
78void ReceiveSideCongestionController::WrappingBitrateEstimator::SetMinBitrate(
79 int min_bitrate_bps) {
Markus Handell9c962502020-07-07 22:03:26 +020080 MutexLock lock(&mutex_);
nisse559af382017-03-21 06:41:12 -070081 rbe_->SetMinBitrate(min_bitrate_bps);
82 min_bitrate_bps_ = min_bitrate_bps;
83}
84
85void ReceiveSideCongestionController::WrappingBitrateEstimator::
86 PickEstimatorFromHeader(const RTPHeader& header) {
87 if (header.extension.hasAbsoluteSendTime) {
88 // If we see AST in header, switch RBE strategy immediately.
89 if (!using_absolute_send_time_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010090 RTC_LOG(LS_INFO)
nisse559af382017-03-21 06:41:12 -070091 << "WrappingBitrateEstimator: Switching to absolute send time RBE.";
92 using_absolute_send_time_ = true;
93 PickEstimator();
94 }
95 packets_since_absolute_send_time_ = 0;
96 } else {
97 // When we don't see AST, wait for a few packets before going back to TOF.
98 if (using_absolute_send_time_) {
99 ++packets_since_absolute_send_time_;
100 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100101 RTC_LOG(LS_INFO)
102 << "WrappingBitrateEstimator: Switching to transmission "
Jonas Olssonb2b20312020-01-14 12:11:31 +0100103 "time offset RBE.";
nisse559af382017-03-21 06:41:12 -0700104 using_absolute_send_time_ = false;
105 PickEstimator();
106 }
107 }
108 }
109}
110
111// Instantiate RBE for Time Offset or Absolute Send Time extensions.
112void ReceiveSideCongestionController::WrappingBitrateEstimator::
113 PickEstimator() {
114 if (using_absolute_send_time_) {
115 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
116 } else {
117 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
118 }
119 rbe_->SetMinBitrate(min_bitrate_bps_);
120}
121
122ReceiveSideCongestionController::ReceiveSideCongestionController(
Sebastian Janssonaa01f272019-01-30 11:28:59 +0100123 Clock* clock,
Per Kjellander898f0912021-04-21 11:56:32 +0200124 RemoteEstimatorProxy::TransportFeedbackSender feedback_sender,
125 RembThrottler::RembSender remb_sender,
126 NetworkStateEstimator* network_state_estimator)
127 : remb_throttler_(std::move(remb_sender), clock),
128 remote_bitrate_estimator_(&remb_throttler_, clock),
Per Kjellander52f7ae72019-09-10 19:28:06 +0200129 remote_estimator_proxy_(clock,
Per Kjellander898f0912021-04-21 11:56:32 +0200130 std::move(feedback_sender),
Per Kjellander52f7ae72019-09-10 19:28:06 +0200131 &field_trial_config_,
132 network_state_estimator) {}
nisse559af382017-03-21 06:41:12 -0700133
134void ReceiveSideCongestionController::OnReceivedPacket(
135 int64_t arrival_time_ms,
136 size_t payload_size,
137 const RTPHeader& header) {
Per Kjellander52f7ae72019-09-10 19:28:06 +0200138 remote_estimator_proxy_.IncomingPacket(arrival_time_ms, payload_size, header);
139 if (!header.extension.hasTransportSequenceNumber) {
nisse559af382017-03-21 06:41:12 -0700140 // Receive-side BWE.
141 remote_bitrate_estimator_.IncomingPacket(arrival_time_ms, payload_size,
142 header);
143 }
144}
145
Johannes Kronf59666b2019-04-08 12:57:06 +0200146void ReceiveSideCongestionController::SetSendPeriodicFeedback(
147 bool send_periodic_feedback) {
148 remote_estimator_proxy_.SetSendPeriodicFeedback(send_periodic_feedback);
Johannes Kron7ff164e2019-02-07 12:50:18 +0100149}
150
nisse559af382017-03-21 06:41:12 -0700151RemoteBitrateEstimator*
152ReceiveSideCongestionController::GetRemoteBitrateEstimator(bool send_side_bwe) {
153 if (send_side_bwe) {
154 return &remote_estimator_proxy_;
155 } else {
156 return &remote_bitrate_estimator_;
157 }
158}
159
160const RemoteBitrateEstimator*
161ReceiveSideCongestionController::GetRemoteBitrateEstimator(
162 bool send_side_bwe) const {
163 if (send_side_bwe) {
164 return &remote_estimator_proxy_;
165 } else {
166 return &remote_bitrate_estimator_;
167 }
168}
169
170void ReceiveSideCongestionController::OnRttUpdate(int64_t avg_rtt_ms,
171 int64_t max_rtt_ms) {
172 remote_bitrate_estimator_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
173}
174
175void ReceiveSideCongestionController::OnBitrateChanged(int bitrate_bps) {
176 remote_estimator_proxy_.OnBitrateChanged(bitrate_bps);
177}
178
179int64_t ReceiveSideCongestionController::TimeUntilNextProcess() {
180 return remote_bitrate_estimator_.TimeUntilNextProcess();
181}
182
183void ReceiveSideCongestionController::Process() {
184 remote_bitrate_estimator_.Process();
185}
186
Per Kjellander898f0912021-04-21 11:56:32 +0200187void ReceiveSideCongestionController::SetMaxDesiredReceiveBitrate(
188 DataRate bitrate) {
189 remb_throttler_.SetMaxDesiredReceiveBitrate(bitrate);
190}
191
nisse559af382017-03-21 06:41:12 -0700192} // namespace webrtc