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