blob: d777536b96d26af2bd0c434dc22614fa05b2fd5c [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()),
87 enforce_min_bitrate_(enforce_min_bitrate) {}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000088
89BitrateControllerImpl::~BitrateControllerImpl() {
stefan@webrtc.org1281dc02012-08-13 16:13:09 +000090 BitrateObserverConfList::iterator it =
pwestin@webrtc.orge9727cd2012-05-03 11:32:25 +000091 bitrate_observers_.begin();
92 while (it != bitrate_observers_.end()) {
93 delete it->second;
94 bitrate_observers_.erase(it);
95 it = bitrate_observers_.begin();
96 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000097 delete critsect_;
98}
99
100RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
101 return new RtcpBandwidthObserverImpl(this);
102}
103
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000104BitrateControllerImpl::BitrateObserverConfList::iterator
105BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver*
106 observer) {
107 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
108 for (; it != bitrate_observers_.end(); ++it) {
109 if (it->first == observer) {
110 return it;
111 }
112 }
113 return bitrate_observers_.end();
114}
115
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000116void BitrateControllerImpl::SetBitrateObserver(
117 BitrateObserver* observer,
118 const uint32_t start_bitrate,
119 const uint32_t min_bitrate,
120 const uint32_t max_bitrate) {
121 CriticalSectionScoped cs(critsect_);
122
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000123 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
124 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000125
126 if (it != bitrate_observers_.end()) {
127 // Update current configuration.
128 it->second->start_bitrate_ = start_bitrate;
129 it->second->min_bitrate_ = min_bitrate;
130 it->second->max_bitrate_ = max_bitrate;
131 } else {
132 // Add new settings.
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000133 bitrate_observers_.push_back(BitrateObserverConfiguration(observer,
134 new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate)));
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000135 bitrate_observers_modified_ = true;
136
137 // TODO(andresp): This is a ugly way to set start bitrate.
138 //
139 // Only change start bitrate if we have exactly one observer. By definition
140 // you can only have one start bitrate, once we have our first estimate we
141 // will adapt from there.
142 if (bitrate_observers_.size() == 1) {
143 bandwidth_estimation_.SetSendBitrate(start_bitrate);
144 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000145 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000146
147 UpdateMinMaxBitrate();
148}
149
150void BitrateControllerImpl::UpdateMinMaxBitrate() {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000151 uint32_t sum_start_bitrate = 0;
152 uint32_t sum_min_bitrate = 0;
153 uint32_t sum_max_bitrate = 0;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000154 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000155 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
156 sum_start_bitrate += it->second->start_bitrate_;
157 sum_min_bitrate += it->second->min_bitrate_;
158 sum_max_bitrate += it->second->max_bitrate_;
159 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000160 if (sum_max_bitrate == 0) {
161 // No max configured use 1Gbit/s.
162 sum_max_bitrate = 1000000000;
163 }
164 if (enforce_min_bitrate_ == false) {
165 // If not enforcing min bitrate, allow the bandwidth estimation to
166 // go as low as 10 kbps.
167 sum_min_bitrate = std::min(sum_min_bitrate, 10000u);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000168 }
169 bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate,
170 sum_max_bitrate);
171}
172
173void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) {
174 CriticalSectionScoped cs(critsect_);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000175 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
176 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 if (it != bitrate_observers_.end()) {
178 delete it->second;
179 bitrate_observers_.erase(it);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000180 bitrate_observers_modified_ = true;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000181 }
182}
183
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +0000184void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) {
185 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000186 enforce_min_bitrate_ = enforce_min_bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000187 UpdateMinMaxBitrate();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000188}
189
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000190void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000191 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000192 bandwidth_estimation_.UpdateReceiverEstimate(bitrate);
193 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000194}
195
196void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
197 const uint8_t fraction_loss,
198 const uint32_t rtt,
199 const int number_of_packets,
200 const uint32_t now_ms) {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000201 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000202 bandwidth_estimation_.UpdateReceiverBlock(
203 fraction_loss, rtt, number_of_packets, now_ms);
204 MaybeTriggerOnNetworkChanged();
205}
206
207void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
208 uint32_t bitrate;
209 uint8_t fraction_loss;
210 uint32_t rtt;
211 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
212
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000213 if (bitrate_observers_modified_ || bitrate != last_bitrate_ ||
214 fraction_loss != last_fraction_loss_ || rtt != last_rtt_ ||
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000215 last_enforce_min_bitrate_ != enforce_min_bitrate_) {
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000216 last_bitrate_ = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000217 last_fraction_loss_ = fraction_loss;
andrew@webrtc.org6cd201c2014-03-25 19:42:39 +0000218 last_rtt_ = rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000219 last_enforce_min_bitrate_ = enforce_min_bitrate_;
220 bitrate_observers_modified_ = false;
221 OnNetworkChanged(bitrate, fraction_loss, rtt);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000222 }
223}
224
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000225void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate,
226 const uint8_t fraction_loss,
227 const uint32_t rtt) {
228 // Sanity check.
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000229 if (bitrate_observers_.empty())
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000230 return;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000231
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000232 uint32_t sum_min_bitrates = 0;
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000233 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000234 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
235 sum_min_bitrates += it->second->min_bitrate_;
236 }
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000237 if (bitrate <= sum_min_bitrates)
238 return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
239 else
240 return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
241}
242
243void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate,
244 uint8_t fraction_loss,
245 uint32_t rtt,
246 uint32_t sum_min_bitrates) {
247 uint32_t number_of_observers = bitrate_observers_.size();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000248 uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) /
249 number_of_observers;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000250 // Use map to sort list based on max bitrate.
251 ObserverSortingMap list_max_bitrates;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000252 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000253 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
254 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>(
255 it->second->max_bitrate_,
256 new ObserverConfiguration(it->first, it->second->min_bitrate_)));
257 }
258 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
259 while (max_it != list_max_bitrates.end()) {
260 number_of_observers--;
261 uint32_t observer_allowance = max_it->second->min_bitrate_ +
262 bitrate_per_observer;
263 if (max_it->first < observer_allowance) {
264 // We have more than enough for this observer.
265 // Carry the remainder forward.
266 uint32_t remainder = observer_allowance - max_it->first;
267 if (number_of_observers != 0) {
268 bitrate_per_observer += remainder / number_of_observers;
269 }
270 max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss,
271 rtt);
272 } else {
273 max_it->second->observer_->OnNetworkChanged(observer_allowance,
274 fraction_loss, rtt);
275 }
276 delete max_it->second;
277 list_max_bitrates.erase(max_it);
278 // Prepare next iteration.
279 max_it = list_max_bitrates.begin();
280 }
281}
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000282
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000283void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate,
284 uint8_t fraction_loss,
285 uint32_t rtt,
286 uint32_t sum_min_bitrates) {
287 if (enforce_min_bitrate_) {
288 // Min bitrate to all observers.
289 BitrateControllerImpl::BitrateObserverConfList::iterator it;
290 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
291 ++it) {
292 it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt);
293 }
294 // Set sum of min to current send bitrate.
295 bandwidth_estimation_.SetSendBitrate(sum_min_bitrates);
296 } else {
297 // Allocate up to |min_bitrate_| to one observer at a time, until
298 // |bitrate| is depleted.
299 uint32_t remainder = bitrate;
300 BitrateControllerImpl::BitrateObserverConfList::iterator it;
301 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
302 ++it) {
303 uint32_t allocation = std::min(remainder, it->second->min_bitrate_);
304 it->first->OnNetworkChanged(allocation, fraction_loss, rtt);
305 remainder -= allocation;
306 }
307 // Set |bitrate| to current send bitrate.
308 bandwidth_estimation_.SetSendBitrate(bitrate);
309 }
310}
311
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000312bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000313 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000314 uint32_t bitrate;
315 uint8_t fraction_loss;
316 uint32_t rtt;
317 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
318 if (bitrate) {
319 *bandwidth = bitrate;
320 return true;
321 }
322 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000323}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000324
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000325} // namespace webrtc