blob: 212ac194f7fc054b08bec8cff2fdbcab59a7d6ae [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
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.org2e10b8e2013-07-16 12:54:53 +000012#include "webrtc/modules/bitrate_controller/bitrate_controller_impl.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000013
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000014#include <algorithm>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000015#include <utility>
16
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000017#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000018
19namespace webrtc {
20
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000021class BitrateControllerImpl::RtcpBandwidthObserverImpl
22 : public RtcpBandwidthObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000023 public:
24 explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner)
25 : owner_(owner) {
26 }
27 virtual ~RtcpBandwidthObserverImpl() {
28 }
29 // Received RTCP REMB or TMMBR.
pbos@webrtc.org4fac8a42013-07-31 15:16:20 +000030 virtual void OnReceivedEstimatedBitrate(const uint32_t bitrate) OVERRIDE {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000031 owner_->OnReceivedEstimatedBitrate(bitrate);
32 }
33 // Received RTCP receiver block.
34 virtual void OnReceivedRtcpReceiverReport(
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000035 const ReportBlockList& report_blocks,
36 uint16_t rtt,
37 int64_t now_ms) OVERRIDE {
38 if (report_blocks.empty())
39 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000040
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000041 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.org1cd11622012-04-19 12:13:52 +000062 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000063 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.org1cd11622012-04-19 12:13:52 +000073 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000074
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000075 private:
76 std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_;
77 BitrateControllerImpl* owner_;
78};
79
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000080BitrateController* BitrateController::CreateBitrateController(
81 bool enforce_min_bitrate) {
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +000082 return new BitrateControllerImpl(enforce_min_bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000083}
84
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +000085BitrateControllerImpl::BitrateControllerImpl(bool enforce_min_bitrate)
86 : critsect_(CriticalSectionWrapper::CreateCriticalSection()),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000087 bandwidth_estimation_(),
88 bitrate_observers_(),
89 enforce_min_bitrate_(enforce_min_bitrate),
90 last_bitrate_(0),
91 last_fraction_loss_(0),
92 last_rtt_(0),
93 last_enforce_min_bitrate_(!enforce_min_bitrate_),
94 bitrate_observers_modified_(false),
95 last_reserved_bitrate_bps_(0),
96 reserved_bitrate_bps_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000097
98BitrateControllerImpl::~BitrateControllerImpl() {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000099 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
pwestin@webrtc.orge9727cd2012-05-03 11:32:25 +0000100 while (it != bitrate_observers_.end()) {
101 delete it->second;
102 bitrate_observers_.erase(it);
103 it = bitrate_observers_.begin();
104 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000105 delete critsect_;
106}
107
108RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
109 return new RtcpBandwidthObserverImpl(this);
110}
111
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000112BitrateControllerImpl::BitrateObserverConfList::iterator
113BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver*
114 observer) {
115 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
116 for (; it != bitrate_observers_.end(); ++it) {
117 if (it->first == observer) {
118 return it;
119 }
120 }
121 return bitrate_observers_.end();
122}
123
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000124void BitrateControllerImpl::SetBitrateObserver(
125 BitrateObserver* observer,
126 const uint32_t start_bitrate,
127 const uint32_t min_bitrate,
128 const uint32_t max_bitrate) {
129 CriticalSectionScoped cs(critsect_);
130
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000131 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
132 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000133
134 if (it != bitrate_observers_.end()) {
135 // Update current configuration.
136 it->second->start_bitrate_ = start_bitrate;
137 it->second->min_bitrate_ = min_bitrate;
138 it->second->max_bitrate_ = max_bitrate;
139 } else {
140 // Add new settings.
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000141 bitrate_observers_.push_back(BitrateObserverConfiguration(observer,
142 new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate)));
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000143 bitrate_observers_modified_ = true;
144
145 // TODO(andresp): This is a ugly way to set start bitrate.
146 //
147 // Only change start bitrate if we have exactly one observer. By definition
148 // you can only have one start bitrate, once we have our first estimate we
149 // will adapt from there.
150 if (bitrate_observers_.size() == 1) {
151 bandwidth_estimation_.SetSendBitrate(start_bitrate);
152 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000153 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000154
155 UpdateMinMaxBitrate();
156}
157
158void BitrateControllerImpl::UpdateMinMaxBitrate() {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000159 uint32_t sum_start_bitrate = 0;
160 uint32_t sum_min_bitrate = 0;
161 uint32_t sum_max_bitrate = 0;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000162 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000163 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
164 sum_start_bitrate += it->second->start_bitrate_;
165 sum_min_bitrate += it->second->min_bitrate_;
166 sum_max_bitrate += it->second->max_bitrate_;
167 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000168 if (sum_max_bitrate == 0) {
169 // No max configured use 1Gbit/s.
170 sum_max_bitrate = 1000000000;
171 }
172 if (enforce_min_bitrate_ == false) {
173 // If not enforcing min bitrate, allow the bandwidth estimation to
174 // go as low as 10 kbps.
175 sum_min_bitrate = std::min(sum_min_bitrate, 10000u);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000176 }
177 bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate,
178 sum_max_bitrate);
179}
180
181void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) {
182 CriticalSectionScoped cs(critsect_);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000183 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
184 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000185 if (it != bitrate_observers_.end()) {
186 delete it->second;
187 bitrate_observers_.erase(it);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000188 bitrate_observers_modified_ = true;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000189 }
190}
191
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +0000192void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) {
193 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000194 enforce_min_bitrate_ = enforce_min_bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000195 UpdateMinMaxBitrate();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000196}
197
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000198void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
199 CriticalSectionScoped cs(critsect_);
200 reserved_bitrate_bps_ = reserved_bitrate_bps;
201 MaybeTriggerOnNetworkChanged();
202}
203
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000204void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000205 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000206 bandwidth_estimation_.UpdateReceiverEstimate(bitrate);
207 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000208}
209
210void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
211 const uint8_t fraction_loss,
212 const uint32_t rtt,
213 const int number_of_packets,
214 const uint32_t now_ms) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000215 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000216 bandwidth_estimation_.UpdateReceiverBlock(
217 fraction_loss, rtt, number_of_packets, now_ms);
218 MaybeTriggerOnNetworkChanged();
219}
220
221void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
222 uint32_t bitrate;
223 uint8_t fraction_loss;
224 uint32_t rtt;
225 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000226 bitrate -= std::min(bitrate, reserved_bitrate_bps_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000227
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000228 if (bitrate_observers_modified_ || bitrate != last_bitrate_ ||
229 fraction_loss != last_fraction_loss_ || rtt != last_rtt_ ||
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000230 last_enforce_min_bitrate_ != enforce_min_bitrate_ ||
231 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000232 last_bitrate_ = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000233 last_fraction_loss_ = fraction_loss;
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000234 last_rtt_ = rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000235 last_enforce_min_bitrate_ = enforce_min_bitrate_;
236 bitrate_observers_modified_ = false;
237 OnNetworkChanged(bitrate, fraction_loss, rtt);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000238 }
239}
240
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000241void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate,
242 const uint8_t fraction_loss,
243 const uint32_t rtt) {
244 // Sanity check.
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000245 if (bitrate_observers_.empty())
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000246 return;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000247
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000248 uint32_t sum_min_bitrates = 0;
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000249 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000250 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
251 sum_min_bitrates += it->second->min_bitrate_;
252 }
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000253 if (bitrate <= sum_min_bitrates)
254 return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
255 else
256 return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
257}
258
259void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate,
260 uint8_t fraction_loss,
261 uint32_t rtt,
262 uint32_t sum_min_bitrates) {
263 uint32_t number_of_observers = bitrate_observers_.size();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000264 uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) /
265 number_of_observers;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000266 // Use map to sort list based on max bitrate.
267 ObserverSortingMap list_max_bitrates;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000268 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000269 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
270 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>(
271 it->second->max_bitrate_,
272 new ObserverConfiguration(it->first, it->second->min_bitrate_)));
273 }
274 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
275 while (max_it != list_max_bitrates.end()) {
276 number_of_observers--;
277 uint32_t observer_allowance = max_it->second->min_bitrate_ +
278 bitrate_per_observer;
279 if (max_it->first < observer_allowance) {
280 // We have more than enough for this observer.
281 // Carry the remainder forward.
282 uint32_t remainder = observer_allowance - max_it->first;
283 if (number_of_observers != 0) {
284 bitrate_per_observer += remainder / number_of_observers;
285 }
286 max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss,
287 rtt);
288 } else {
289 max_it->second->observer_->OnNetworkChanged(observer_allowance,
290 fraction_loss, rtt);
291 }
292 delete max_it->second;
293 list_max_bitrates.erase(max_it);
294 // Prepare next iteration.
295 max_it = list_max_bitrates.begin();
296 }
297}
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000298
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000299void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate,
300 uint8_t fraction_loss,
301 uint32_t rtt,
302 uint32_t sum_min_bitrates) {
303 if (enforce_min_bitrate_) {
304 // Min bitrate to all observers.
305 BitrateControllerImpl::BitrateObserverConfList::iterator it;
306 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
307 ++it) {
308 it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt);
309 }
310 // Set sum of min to current send bitrate.
311 bandwidth_estimation_.SetSendBitrate(sum_min_bitrates);
312 } else {
313 // Allocate up to |min_bitrate_| to one observer at a time, until
314 // |bitrate| is depleted.
315 uint32_t remainder = bitrate;
316 BitrateControllerImpl::BitrateObserverConfList::iterator it;
317 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
318 ++it) {
319 uint32_t allocation = std::min(remainder, it->second->min_bitrate_);
320 it->first->OnNetworkChanged(allocation, fraction_loss, rtt);
321 remainder -= allocation;
322 }
323 // Set |bitrate| to current send bitrate.
324 bandwidth_estimation_.SetSendBitrate(bitrate);
325 }
326}
327
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000328bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000329 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000330 uint32_t bitrate;
331 uint8_t fraction_loss;
332 uint32_t rtt;
333 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
334 if (bitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000335 *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000336 return true;
337 }
338 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000339}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000340
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000341} // namespace webrtc