nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/congestion_controller/include/receive_side_congestion_controller.h" |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 12 | |
Per Kjellander | 898f091 | 2021-04-21 11:56:32 +0200 | [diff] [blame] | 13 | #include "api/units/data_rate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/pacing/packet_router.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "modules/remote_bitrate_estimator/include/bwe_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #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" |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | namespace { |
| 23 | static const uint32_t kTimeOffsetSwitchThreshold = 30; |
| 24 | } // namespace |
| 25 | |
| 26 | ReceiveSideCongestionController::WrappingBitrateEstimator:: |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 27 | WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 28 | : 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 Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 35 | ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 36 | ~WrappingBitrateEstimator() = default; |
| 37 | |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 38 | void ReceiveSideCongestionController::WrappingBitrateEstimator::IncomingPacket( |
| 39 | int64_t arrival_time_ms, |
| 40 | size_t payload_size, |
| 41 | const RTPHeader& header) { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 42 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 43 | PickEstimatorFromHeader(header); |
| 44 | rbe_->IncomingPacket(arrival_time_ms, payload_size, header); |
| 45 | } |
| 46 | |
| 47 | void ReceiveSideCongestionController::WrappingBitrateEstimator::Process() { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 48 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 49 | rbe_->Process(); |
| 50 | } |
| 51 | |
| 52 | int64_t ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 53 | TimeUntilNextProcess() { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 54 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 55 | return rbe_->TimeUntilNextProcess(); |
| 56 | } |
| 57 | |
| 58 | void ReceiveSideCongestionController::WrappingBitrateEstimator::OnRttUpdate( |
| 59 | int64_t avg_rtt_ms, |
| 60 | int64_t max_rtt_ms) { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 61 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 62 | rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 63 | } |
| 64 | |
| 65 | void ReceiveSideCongestionController::WrappingBitrateEstimator::RemoveStream( |
| 66 | unsigned int ssrc) { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 67 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 68 | rbe_->RemoveStream(ssrc); |
| 69 | } |
| 70 | |
| 71 | bool ReceiveSideCongestionController::WrappingBitrateEstimator::LatestEstimate( |
| 72 | std::vector<unsigned int>* ssrcs, |
| 73 | unsigned int* bitrate_bps) const { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 74 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 75 | return rbe_->LatestEstimate(ssrcs, bitrate_bps); |
| 76 | } |
| 77 | |
| 78 | void ReceiveSideCongestionController::WrappingBitrateEstimator::SetMinBitrate( |
| 79 | int min_bitrate_bps) { |
Markus Handell | 9c96250 | 2020-07-07 22:03:26 +0200 | [diff] [blame] | 80 | MutexLock lock(&mutex_); |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 81 | rbe_->SetMinBitrate(min_bitrate_bps); |
| 82 | min_bitrate_bps_ = min_bitrate_bps; |
| 83 | } |
| 84 | |
| 85 | void 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 90 | RTC_LOG(LS_INFO) |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 91 | << "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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 101 | RTC_LOG(LS_INFO) |
| 102 | << "WrappingBitrateEstimator: Switching to transmission " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 103 | "time offset RBE."; |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 104 | using_absolute_send_time_ = false; |
| 105 | PickEstimator(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Instantiate RBE for Time Offset or Absolute Send Time extensions. |
| 112 | void 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 | |
| 122 | ReceiveSideCongestionController::ReceiveSideCongestionController( |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 123 | Clock* clock, |
Per Kjellander | 898f091 | 2021-04-21 11:56:32 +0200 | [diff] [blame] | 124 | 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 Kjellander | 52f7ae7 | 2019-09-10 19:28:06 +0200 | [diff] [blame] | 129 | remote_estimator_proxy_(clock, |
Per Kjellander | 898f091 | 2021-04-21 11:56:32 +0200 | [diff] [blame] | 130 | std::move(feedback_sender), |
Per Kjellander | 52f7ae7 | 2019-09-10 19:28:06 +0200 | [diff] [blame] | 131 | &field_trial_config_, |
| 132 | network_state_estimator) {} |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 133 | |
| 134 | void ReceiveSideCongestionController::OnReceivedPacket( |
| 135 | int64_t arrival_time_ms, |
| 136 | size_t payload_size, |
| 137 | const RTPHeader& header) { |
Per Kjellander | 52f7ae7 | 2019-09-10 19:28:06 +0200 | [diff] [blame] | 138 | remote_estimator_proxy_.IncomingPacket(arrival_time_ms, payload_size, header); |
| 139 | if (!header.extension.hasTransportSequenceNumber) { |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 140 | // Receive-side BWE. |
| 141 | remote_bitrate_estimator_.IncomingPacket(arrival_time_ms, payload_size, |
| 142 | header); |
| 143 | } |
| 144 | } |
| 145 | |
Johannes Kron | f59666b | 2019-04-08 12:57:06 +0200 | [diff] [blame] | 146 | void ReceiveSideCongestionController::SetSendPeriodicFeedback( |
| 147 | bool send_periodic_feedback) { |
| 148 | remote_estimator_proxy_.SetSendPeriodicFeedback(send_periodic_feedback); |
Johannes Kron | 7ff164e | 2019-02-07 12:50:18 +0100 | [diff] [blame] | 149 | } |
| 150 | |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 151 | RemoteBitrateEstimator* |
| 152 | ReceiveSideCongestionController::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 | |
| 160 | const RemoteBitrateEstimator* |
| 161 | ReceiveSideCongestionController::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 | |
| 170 | void 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 | |
| 175 | void ReceiveSideCongestionController::OnBitrateChanged(int bitrate_bps) { |
| 176 | remote_estimator_proxy_.OnBitrateChanged(bitrate_bps); |
| 177 | } |
| 178 | |
| 179 | int64_t ReceiveSideCongestionController::TimeUntilNextProcess() { |
| 180 | return remote_bitrate_estimator_.TimeUntilNextProcess(); |
| 181 | } |
| 182 | |
| 183 | void ReceiveSideCongestionController::Process() { |
| 184 | remote_bitrate_estimator_.Process(); |
| 185 | } |
| 186 | |
Per Kjellander | 898f091 | 2021-04-21 11:56:32 +0200 | [diff] [blame] | 187 | void ReceiveSideCongestionController::SetMaxDesiredReceiveBitrate( |
| 188 | DataRate bitrate) { |
| 189 | remb_throttler_.SetMaxDesiredReceiveBitrate(bitrate); |
| 190 | } |
| 191 | |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 192 | } // namespace webrtc |