blob: 5e1fd46c27c14d2c7b2774b8e251939275243be1 [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.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000030 virtual void OnReceivedEstimatedBitrate(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(
andresp@webrtc.org44caf012014-03-26 21:00:21 +000081 Clock* clock,
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000082 bool enforce_min_bitrate) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +000083 return new BitrateControllerImpl(clock, enforce_min_bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000084}
85
andresp@webrtc.org44caf012014-03-26 21:00:21 +000086BitrateControllerImpl::BitrateControllerImpl(Clock* clock, bool enforce_min_bitrate)
87 : clock_(clock),
88 last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
89 critsect_(CriticalSectionWrapper::CreateCriticalSection()),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000090 bandwidth_estimation_(),
91 bitrate_observers_(),
92 enforce_min_bitrate_(enforce_min_bitrate),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000093 reserved_bitrate_bps_(0),
94 last_bitrate_bps_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000095 last_fraction_loss_(0),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000096 last_rtt_ms_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000097 last_enforce_min_bitrate_(!enforce_min_bitrate_),
98 bitrate_observers_modified_(false),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000099 last_reserved_bitrate_bps_(0) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000100
101BitrateControllerImpl::~BitrateControllerImpl() {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000102 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
pwestin@webrtc.orge9727cd2012-05-03 11:32:25 +0000103 while (it != bitrate_observers_.end()) {
104 delete it->second;
105 bitrate_observers_.erase(it);
106 it = bitrate_observers_.begin();
107 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000108 delete critsect_;
109}
110
111RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
112 return new RtcpBandwidthObserverImpl(this);
113}
114
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000115BitrateControllerImpl::BitrateObserverConfList::iterator
116BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver*
117 observer) {
118 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
119 for (; it != bitrate_observers_.end(); ++it) {
120 if (it->first == observer) {
121 return it;
122 }
123 }
124 return bitrate_observers_.end();
125}
126
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000127void BitrateControllerImpl::SetBitrateObserver(
128 BitrateObserver* observer,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000129 uint32_t start_bitrate,
130 uint32_t min_bitrate,
131 uint32_t max_bitrate) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000132 CriticalSectionScoped cs(critsect_);
133
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000134 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
135 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000136
137 if (it != bitrate_observers_.end()) {
138 // Update current configuration.
139 it->second->start_bitrate_ = start_bitrate;
140 it->second->min_bitrate_ = min_bitrate;
141 it->second->max_bitrate_ = max_bitrate;
stefan@webrtc.org077593b2014-06-19 12:13:00 +0000142 // Set the send-side bandwidth to the max of the sum of start bitrates and
143 // the current estimate, so that if the user wants to immediately use more
144 // bandwidth, that can be enforced.
145 uint32_t sum_start_bitrate = 0;
146 BitrateObserverConfList::iterator it;
147 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
148 ++it) {
149 sum_start_bitrate += it->second->start_bitrate_;
150 }
151 uint32_t current_estimate;
152 uint8_t loss;
153 uint32_t rtt;
154 bandwidth_estimation_.CurrentEstimate(&current_estimate, &loss, &rtt);
155 bandwidth_estimation_.SetSendBitrate(std::max(sum_start_bitrate,
156 current_estimate));
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000157 } else {
158 // Add new settings.
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000159 bitrate_observers_.push_back(BitrateObserverConfiguration(observer,
160 new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate)));
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000161 bitrate_observers_modified_ = true;
162
163 // TODO(andresp): This is a ugly way to set start bitrate.
164 //
165 // Only change start bitrate if we have exactly one observer. By definition
166 // you can only have one start bitrate, once we have our first estimate we
167 // will adapt from there.
168 if (bitrate_observers_.size() == 1) {
169 bandwidth_estimation_.SetSendBitrate(start_bitrate);
170 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000171 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000172
173 UpdateMinMaxBitrate();
174}
175
176void BitrateControllerImpl::UpdateMinMaxBitrate() {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 uint32_t sum_min_bitrate = 0;
178 uint32_t sum_max_bitrate = 0;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000179 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000180 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000181 sum_min_bitrate += it->second->min_bitrate_;
182 sum_max_bitrate += it->second->max_bitrate_;
183 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000184 if (sum_max_bitrate == 0) {
185 // No max configured use 1Gbit/s.
186 sum_max_bitrate = 1000000000;
187 }
188 if (enforce_min_bitrate_ == false) {
189 // If not enforcing min bitrate, allow the bandwidth estimation to
190 // go as low as 10 kbps.
191 sum_min_bitrate = std::min(sum_min_bitrate, 10000u);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000192 }
193 bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate,
194 sum_max_bitrate);
195}
196
197void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) {
198 CriticalSectionScoped cs(critsect_);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000199 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
200 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000201 if (it != bitrate_observers_.end()) {
202 delete it->second;
203 bitrate_observers_.erase(it);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000204 bitrate_observers_modified_ = true;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000205 }
206}
207
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +0000208void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) {
209 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000210 enforce_min_bitrate_ = enforce_min_bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000211 UpdateMinMaxBitrate();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000212}
213
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000214void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
215 CriticalSectionScoped cs(critsect_);
216 reserved_bitrate_bps_ = reserved_bitrate_bps;
217 MaybeTriggerOnNetworkChanged();
218}
219
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000220void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000222 bandwidth_estimation_.UpdateReceiverEstimate(bitrate);
223 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000224}
225
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000226int64_t BitrateControllerImpl::TimeUntilNextProcess() {
227 const int64_t kBitrateControllerUpdateIntervalMs = 25;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000228 CriticalSectionScoped cs(critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000229 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000230 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000231 return std::max<int64_t>(
232 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000233}
234
235int32_t BitrateControllerImpl::Process() {
236 if (TimeUntilNextProcess() > 0)
237 return 0;
238 {
239 CriticalSectionScoped cs(critsect_);
240 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
241 MaybeTriggerOnNetworkChanged();
242 }
243 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
244 return 0;
245}
246
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000247void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000248 uint8_t fraction_loss,
249 uint32_t rtt,
250 int number_of_packets,
251 int64_t now_ms) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000252 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000253 bandwidth_estimation_.UpdateReceiverBlock(
254 fraction_loss, rtt, number_of_packets, now_ms);
255 MaybeTriggerOnNetworkChanged();
256}
257
258void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
259 uint32_t bitrate;
260 uint8_t fraction_loss;
261 uint32_t rtt;
262 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000263 bitrate -= std::min(bitrate, reserved_bitrate_bps_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000264
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000265 if (bitrate_observers_modified_ ||
266 bitrate != last_bitrate_bps_ ||
267 fraction_loss != last_fraction_loss_ ||
268 rtt != last_rtt_ms_ ||
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000269 last_enforce_min_bitrate_ != enforce_min_bitrate_ ||
270 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000271 last_bitrate_bps_ = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000272 last_fraction_loss_ = fraction_loss;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000273 last_rtt_ms_ = rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000274 last_enforce_min_bitrate_ = enforce_min_bitrate_;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000275 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000276 bitrate_observers_modified_ = false;
277 OnNetworkChanged(bitrate, fraction_loss, rtt);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000278 }
279}
280
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000281void BitrateControllerImpl::OnNetworkChanged(uint32_t bitrate,
282 uint8_t fraction_loss,
283 uint32_t rtt) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000284 // Sanity check.
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000285 if (bitrate_observers_.empty())
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000286 return;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000287
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000288 uint32_t sum_min_bitrates = 0;
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000289 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000290 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
291 sum_min_bitrates += it->second->min_bitrate_;
292 }
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000293 if (bitrate <= sum_min_bitrates)
294 return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
295 else
296 return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
297}
298
299void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate,
300 uint8_t fraction_loss,
301 uint32_t rtt,
302 uint32_t sum_min_bitrates) {
303 uint32_t number_of_observers = bitrate_observers_.size();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000304 uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) /
305 number_of_observers;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000306 // Use map to sort list based on max bitrate.
307 ObserverSortingMap list_max_bitrates;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000308 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000309 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
310 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>(
311 it->second->max_bitrate_,
312 new ObserverConfiguration(it->first, it->second->min_bitrate_)));
313 }
314 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
315 while (max_it != list_max_bitrates.end()) {
316 number_of_observers--;
317 uint32_t observer_allowance = max_it->second->min_bitrate_ +
318 bitrate_per_observer;
319 if (max_it->first < observer_allowance) {
320 // We have more than enough for this observer.
321 // Carry the remainder forward.
322 uint32_t remainder = observer_allowance - max_it->first;
323 if (number_of_observers != 0) {
324 bitrate_per_observer += remainder / number_of_observers;
325 }
326 max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss,
327 rtt);
328 } else {
329 max_it->second->observer_->OnNetworkChanged(observer_allowance,
330 fraction_loss, rtt);
331 }
332 delete max_it->second;
333 list_max_bitrates.erase(max_it);
334 // Prepare next iteration.
335 max_it = list_max_bitrates.begin();
336 }
337}
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000338
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000339void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate,
340 uint8_t fraction_loss,
341 uint32_t rtt,
342 uint32_t sum_min_bitrates) {
343 if (enforce_min_bitrate_) {
344 // Min bitrate to all observers.
345 BitrateControllerImpl::BitrateObserverConfList::iterator it;
346 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
347 ++it) {
348 it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt);
349 }
350 // Set sum of min to current send bitrate.
351 bandwidth_estimation_.SetSendBitrate(sum_min_bitrates);
352 } else {
353 // Allocate up to |min_bitrate_| to one observer at a time, until
354 // |bitrate| is depleted.
355 uint32_t remainder = bitrate;
356 BitrateControllerImpl::BitrateObserverConfList::iterator it;
357 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
358 ++it) {
359 uint32_t allocation = std::min(remainder, it->second->min_bitrate_);
360 it->first->OnNetworkChanged(allocation, fraction_loss, rtt);
361 remainder -= allocation;
362 }
363 // Set |bitrate| to current send bitrate.
364 bandwidth_estimation_.SetSendBitrate(bitrate);
365 }
366}
367
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000368bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000369 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000370 uint32_t bitrate;
371 uint8_t fraction_loss;
372 uint32_t rtt;
373 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
374 if (bitrate) {
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000375 *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000376 return true;
377 }
378 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000379}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000380
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000381} // namespace webrtc