pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 12 | #include "webrtc/modules/bitrate_controller/bitrate_controller_impl.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 13 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 14 | #include <algorithm> |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 21 | class BitrateControllerImpl::RtcpBandwidthObserverImpl |
| 22 | : public RtcpBandwidthObserver { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 23 | public: |
| 24 | explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner) |
| 25 | : owner_(owner) { |
| 26 | } |
| 27 | virtual ~RtcpBandwidthObserverImpl() { |
| 28 | } |
| 29 | // Received RTCP REMB or TMMBR. |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 30 | virtual void OnReceivedEstimatedBitrate(uint32_t bitrate) OVERRIDE { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 31 | owner_->OnReceivedEstimatedBitrate(bitrate); |
| 32 | } |
| 33 | // Received RTCP receiver block. |
| 34 | virtual void OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 35 | const ReportBlockList& report_blocks, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 36 | int64_t rtt, |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 37 | int64_t now_ms) OVERRIDE { |
| 38 | if (report_blocks.empty()) |
| 39 | return; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 40 | |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 41 | int fraction_lost_aggregate = 0; |
| 42 | int total_number_of_packets = 0; |
| 43 | |
| 44 | // Compute the a weighted average of the fraction loss from all report |
| 45 | // blocks. |
| 46 | for (ReportBlockList::const_iterator it = report_blocks.begin(); |
| 47 | it != report_blocks.end(); ++it) { |
| 48 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 49 | ssrc_to_last_received_extended_high_seq_num_.find(it->sourceSSRC); |
| 50 | |
| 51 | int number_of_packets = 0; |
| 52 | if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) |
| 53 | number_of_packets = it->extendedHighSeqNum - |
| 54 | seq_num_it->second; |
| 55 | |
| 56 | fraction_lost_aggregate += number_of_packets * it->fractionLost; |
| 57 | total_number_of_packets += number_of_packets; |
| 58 | |
| 59 | // Update last received for this SSRC. |
| 60 | ssrc_to_last_received_extended_high_seq_num_[it->sourceSSRC] = |
| 61 | it->extendedHighSeqNum; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 62 | } |
stefan@webrtc.org | 28a331e | 2013-09-17 07:49:56 +0000 | [diff] [blame] | 63 | if (total_number_of_packets == 0) |
| 64 | fraction_lost_aggregate = 0; |
| 65 | else |
| 66 | fraction_lost_aggregate = (fraction_lost_aggregate + |
| 67 | total_number_of_packets / 2) / total_number_of_packets; |
| 68 | if (fraction_lost_aggregate > 255) |
| 69 | return; |
| 70 | |
| 71 | owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt, |
| 72 | total_number_of_packets, now_ms); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 73 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 74 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 75 | private: |
| 76 | std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_; |
| 77 | BitrateControllerImpl* owner_; |
| 78 | }; |
| 79 | |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 80 | BitrateController* BitrateController::CreateBitrateController( |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 81 | Clock* clock, |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 82 | BitrateObserver* observer) { |
| 83 | return new BitrateControllerImpl(clock, observer); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 86 | BitrateControllerImpl::BitrateControllerImpl(Clock* clock, |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 87 | BitrateObserver* observer) |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 88 | : clock_(clock), |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 89 | observer_(observer), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 90 | last_bitrate_update_ms_(clock_->TimeInMilliseconds()), |
| 91 | critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 92 | bandwidth_estimation_(), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 93 | reserved_bitrate_bps_(0), |
| 94 | last_bitrate_bps_(0), |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 95 | last_fraction_loss_(0), |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 96 | last_rtt_ms_(0), |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 97 | last_reserved_bitrate_bps_(0), |
| 98 | remb_suppressor_(new RembSuppressor(clock)) { |
| 99 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 100 | |
| 101 | BitrateControllerImpl::~BitrateControllerImpl() { |
| 102 | delete critsect_; |
| 103 | } |
| 104 | |
| 105 | RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() { |
| 106 | return new RtcpBandwidthObserverImpl(this); |
| 107 | } |
| 108 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 109 | void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 110 | CriticalSectionScoped cs(critsect_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 111 | bandwidth_estimation_.SetSendBitrate(start_bitrate_bps); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 114 | void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps, |
| 115 | int max_bitrate_bps) { |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 116 | CriticalSectionScoped cs(critsect_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 117 | bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps); |
henrik.lundin@webrtc.org | 845862f | 2014-03-06 07:19:28 +0000 | [diff] [blame] | 118 | } |
| 119 | |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 120 | void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 121 | { |
| 122 | CriticalSectionScoped cs(critsect_); |
| 123 | reserved_bitrate_bps_ = reserved_bitrate_bps; |
| 124 | } |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 125 | MaybeTriggerOnNetworkChanged(); |
| 126 | } |
| 127 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 128 | void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 129 | { |
| 130 | CriticalSectionScoped cs(critsect_); |
| 131 | if (remb_suppressor_->SuppresNewRemb(bitrate)) { |
| 132 | return; |
| 133 | } |
| 134 | bandwidth_estimation_.UpdateReceiverEstimate(bitrate); |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 135 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 136 | MaybeTriggerOnNetworkChanged(); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 137 | } |
| 138 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 139 | int64_t BitrateControllerImpl::TimeUntilNextProcess() { |
| 140 | const int64_t kBitrateControllerUpdateIntervalMs = 25; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 141 | CriticalSectionScoped cs(critsect_); |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 142 | int64_t time_since_update_ms = |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 143 | clock_->TimeInMilliseconds() - last_bitrate_update_ms_; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 144 | return std::max<int64_t>( |
| 145 | kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int32_t BitrateControllerImpl::Process() { |
| 149 | if (TimeUntilNextProcess() > 0) |
| 150 | return 0; |
| 151 | { |
| 152 | CriticalSectionScoped cs(critsect_); |
| 153 | bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds()); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 154 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 155 | MaybeTriggerOnNetworkChanged(); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 +0000 | [diff] [blame] | 156 | last_bitrate_update_ms_ = clock_->TimeInMilliseconds(); |
| 157 | return 0; |
| 158 | } |
| 159 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 160 | void BitrateControllerImpl::OnReceivedRtcpReceiverReport( |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 161 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 162 | int64_t rtt, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 163 | int number_of_packets, |
| 164 | int64_t now_ms) { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 165 | { |
| 166 | CriticalSectionScoped cs(critsect_); |
| 167 | bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt, |
| 168 | number_of_packets, now_ms); |
| 169 | } |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 170 | MaybeTriggerOnNetworkChanged(); |
| 171 | } |
| 172 | |
| 173 | void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() { |
| 174 | uint32_t bitrate; |
| 175 | uint8_t fraction_loss; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 176 | int64_t rtt; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 177 | bool new_bitrate = false; |
| 178 | { |
| 179 | CriticalSectionScoped cs(critsect_); |
| 180 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
| 181 | bitrate -= std::min(bitrate, reserved_bitrate_bps_); |
| 182 | bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate()); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 183 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 184 | if (bitrate != last_bitrate_bps_ || fraction_loss != last_fraction_loss_ || |
| 185 | rtt != last_rtt_ms_ || |
| 186 | last_reserved_bitrate_bps_ != reserved_bitrate_bps_) { |
| 187 | last_bitrate_bps_ = bitrate; |
| 188 | last_fraction_loss_ = fraction_loss; |
| 189 | last_rtt_ms_ = rtt; |
| 190 | last_reserved_bitrate_bps_ = reserved_bitrate_bps_; |
| 191 | new_bitrate = true; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 192 | } |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 193 | } |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 194 | if (new_bitrate) |
| 195 | observer_->OnNetworkChanged(bitrate, fraction_loss, rtt); |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 198 | bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const { |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 +0000 | [diff] [blame] | 199 | CriticalSectionScoped cs(critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 200 | uint32_t bitrate; |
| 201 | uint8_t fraction_loss; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 202 | int64_t rtt; |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 203 | bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt); |
| 204 | if (bitrate) { |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 +0000 | [diff] [blame] | 205 | *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame^] | 206 | *bandwidth = std::max(*bandwidth, bandwidth_estimation_.GetMinBitrate()); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | return false; |
pwestin@webrtc.org | a2cd732 | 2012-04-23 08:32:47 +0000 | [diff] [blame] | 210 | } |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 +0000 | [diff] [blame] | 211 | |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 +0000 | [diff] [blame] | 212 | void BitrateControllerImpl::SetBitrateSent(uint32_t bitrate_sent_bps) { |
| 213 | CriticalSectionScoped cs(critsect_); |
| 214 | remb_suppressor_->SetBitrateSent(bitrate_sent_bps); |
| 215 | } |
| 216 | |
| 217 | void BitrateControllerImpl::SetCodecMode(webrtc::VideoCodecMode mode) { |
| 218 | CriticalSectionScoped cs(critsect_); |
| 219 | remb_suppressor_->SetEnabled(mode == kScreensharing); |
| 220 | } |
| 221 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 +0000 | [diff] [blame] | 222 | } // namespace webrtc |