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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/pacing/packet_router.h" |
| 14 | #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h" |
| 15 | #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h" |
| 16 | #include "rtc_base/logging.h" |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | static const uint32_t kTimeOffsetSwitchThreshold = 30; |
| 22 | } // namespace |
| 23 | |
| 24 | ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 25 | WrappingBitrateEstimator(RemoteBitrateObserver* observer, |
| 26 | const Clock* clock) |
| 27 | : observer_(observer), |
| 28 | clock_(clock), |
| 29 | rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)), |
| 30 | using_absolute_send_time_(false), |
| 31 | packets_since_absolute_send_time_(0), |
| 32 | min_bitrate_bps_(congestion_controller::GetMinBitrateBps()) {} |
| 33 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame^] | 34 | ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 35 | ~WrappingBitrateEstimator() = default; |
| 36 | |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 37 | void ReceiveSideCongestionController::WrappingBitrateEstimator::IncomingPacket( |
| 38 | int64_t arrival_time_ms, |
| 39 | size_t payload_size, |
| 40 | const RTPHeader& header) { |
| 41 | rtc::CritScope cs(&crit_sect_); |
| 42 | PickEstimatorFromHeader(header); |
| 43 | rbe_->IncomingPacket(arrival_time_ms, payload_size, header); |
| 44 | } |
| 45 | |
| 46 | void ReceiveSideCongestionController::WrappingBitrateEstimator::Process() { |
| 47 | rtc::CritScope cs(&crit_sect_); |
| 48 | rbe_->Process(); |
| 49 | } |
| 50 | |
| 51 | int64_t ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 52 | TimeUntilNextProcess() { |
| 53 | rtc::CritScope cs(&crit_sect_); |
| 54 | return rbe_->TimeUntilNextProcess(); |
| 55 | } |
| 56 | |
| 57 | void ReceiveSideCongestionController::WrappingBitrateEstimator::OnRttUpdate( |
| 58 | int64_t avg_rtt_ms, |
| 59 | int64_t max_rtt_ms) { |
| 60 | rtc::CritScope cs(&crit_sect_); |
| 61 | rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 62 | } |
| 63 | |
| 64 | void ReceiveSideCongestionController::WrappingBitrateEstimator::RemoveStream( |
| 65 | unsigned int ssrc) { |
| 66 | rtc::CritScope cs(&crit_sect_); |
| 67 | rbe_->RemoveStream(ssrc); |
| 68 | } |
| 69 | |
| 70 | bool ReceiveSideCongestionController::WrappingBitrateEstimator::LatestEstimate( |
| 71 | std::vector<unsigned int>* ssrcs, |
| 72 | unsigned int* bitrate_bps) const { |
| 73 | rtc::CritScope cs(&crit_sect_); |
| 74 | return rbe_->LatestEstimate(ssrcs, bitrate_bps); |
| 75 | } |
| 76 | |
| 77 | void ReceiveSideCongestionController::WrappingBitrateEstimator::SetMinBitrate( |
| 78 | int min_bitrate_bps) { |
| 79 | rtc::CritScope cs(&crit_sect_); |
| 80 | rbe_->SetMinBitrate(min_bitrate_bps); |
| 81 | min_bitrate_bps_ = min_bitrate_bps; |
| 82 | } |
| 83 | |
| 84 | void ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 85 | PickEstimatorFromHeader(const RTPHeader& header) { |
| 86 | if (header.extension.hasAbsoluteSendTime) { |
| 87 | // If we see AST in header, switch RBE strategy immediately. |
| 88 | if (!using_absolute_send_time_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 89 | RTC_LOG(LS_INFO) |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 90 | << "WrappingBitrateEstimator: Switching to absolute send time RBE."; |
| 91 | using_absolute_send_time_ = true; |
| 92 | PickEstimator(); |
| 93 | } |
| 94 | packets_since_absolute_send_time_ = 0; |
| 95 | } else { |
| 96 | // When we don't see AST, wait for a few packets before going back to TOF. |
| 97 | if (using_absolute_send_time_) { |
| 98 | ++packets_since_absolute_send_time_; |
| 99 | if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 100 | RTC_LOG(LS_INFO) |
| 101 | << "WrappingBitrateEstimator: Switching to transmission " |
| 102 | << "time offset RBE."; |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 103 | using_absolute_send_time_ = false; |
| 104 | PickEstimator(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Instantiate RBE for Time Offset or Absolute Send Time extensions. |
| 111 | void ReceiveSideCongestionController::WrappingBitrateEstimator:: |
| 112 | PickEstimator() { |
| 113 | if (using_absolute_send_time_) { |
| 114 | rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_)); |
| 115 | } else { |
| 116 | rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_)); |
| 117 | } |
| 118 | rbe_->SetMinBitrate(min_bitrate_bps_); |
| 119 | } |
| 120 | |
| 121 | ReceiveSideCongestionController::ReceiveSideCongestionController( |
| 122 | const Clock* clock, |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 123 | PacketRouter* packet_router) |
nisse | 0584331 | 2017-04-18 23:38:35 -0700 | [diff] [blame] | 124 | : remote_bitrate_estimator_(packet_router, clock), |
nisse | 559af38 | 2017-03-21 06:41:12 -0700 | [diff] [blame] | 125 | remote_estimator_proxy_(clock, packet_router) {} |
| 126 | |
| 127 | void ReceiveSideCongestionController::OnReceivedPacket( |
| 128 | int64_t arrival_time_ms, |
| 129 | size_t payload_size, |
| 130 | const RTPHeader& header) { |
| 131 | // Send-side BWE. |
| 132 | if (header.extension.hasTransportSequenceNumber) { |
| 133 | remote_estimator_proxy_.IncomingPacket(arrival_time_ms, payload_size, |
| 134 | header); |
| 135 | } else { |
| 136 | // Receive-side BWE. |
| 137 | remote_bitrate_estimator_.IncomingPacket(arrival_time_ms, payload_size, |
| 138 | header); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | RemoteBitrateEstimator* |
| 143 | ReceiveSideCongestionController::GetRemoteBitrateEstimator(bool send_side_bwe) { |
| 144 | if (send_side_bwe) { |
| 145 | return &remote_estimator_proxy_; |
| 146 | } else { |
| 147 | return &remote_bitrate_estimator_; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | const RemoteBitrateEstimator* |
| 152 | ReceiveSideCongestionController::GetRemoteBitrateEstimator( |
| 153 | bool send_side_bwe) const { |
| 154 | if (send_side_bwe) { |
| 155 | return &remote_estimator_proxy_; |
| 156 | } else { |
| 157 | return &remote_bitrate_estimator_; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ReceiveSideCongestionController::OnRttUpdate(int64_t avg_rtt_ms, |
| 162 | int64_t max_rtt_ms) { |
| 163 | remote_bitrate_estimator_.OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 164 | } |
| 165 | |
| 166 | void ReceiveSideCongestionController::OnBitrateChanged(int bitrate_bps) { |
| 167 | remote_estimator_proxy_.OnBitrateChanged(bitrate_bps); |
| 168 | } |
| 169 | |
| 170 | int64_t ReceiveSideCongestionController::TimeUntilNextProcess() { |
| 171 | return remote_bitrate_estimator_.TimeUntilNextProcess(); |
| 172 | } |
| 173 | |
| 174 | void ReceiveSideCongestionController::Process() { |
| 175 | remote_bitrate_estimator_.Process(); |
| 176 | } |
| 177 | |
| 178 | } // namespace webrtc |