blob: 38104064640a38699beed4665f56b9150943900a [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) {
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +000082 return new BitrateControllerImpl(enforce_min_bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000083}
84
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +000085BitrateControllerImpl::BitrateControllerImpl(bool enforce_min_bitrate)
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000086 : 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)));
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000135 }
136 uint32_t sum_start_bitrate = 0;
137 uint32_t sum_min_bitrate = 0;
138 uint32_t sum_max_bitrate = 0;
139
140 // Summarize all configurations.
141 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
142 sum_start_bitrate += it->second->start_bitrate_;
143 sum_min_bitrate += it->second->min_bitrate_;
144 sum_max_bitrate += it->second->max_bitrate_;
145 }
146 // Only change start bitrate if we have exactly one observer. By definition
147 // you can only have one start bitrate, once we have our first estimate we
148 // will adapt from there.
149 if (bitrate_observers_.size() == 1) {
150 bandwidth_estimation_.SetSendBitrate(sum_start_bitrate);
151 }
152 bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate,
153 sum_max_bitrate);
154}
155
156void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) {
157 CriticalSectionScoped cs(critsect_);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000158 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
159 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000160 if (it != bitrate_observers_.end()) {
161 delete it->second;
162 bitrate_observers_.erase(it);
163 }
164}
165
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +0000166void BitrateControllerImpl::EnforceMinBitrate(bool enforce_min_bitrate) {
167 CriticalSectionScoped cs(critsect_);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000168 enforce_min_bitrate_ = enforce_min_bitrate;
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +0000169}
170
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000171void BitrateControllerImpl::SetBweMinBitrate(uint32_t min_bitrate) {
172 CriticalSectionScoped cs(critsect_);
173 bandwidth_estimation_.SetMinBitrate(min_bitrate);
174}
175
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000176void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) {
177 uint32_t new_bitrate = 0;
178 uint8_t fraction_lost = 0;
179 uint16_t rtt = 0;
180 CriticalSectionScoped cs(critsect_);
181 if (bandwidth_estimation_.UpdateBandwidthEstimate(bitrate,
182 &new_bitrate,
183 &fraction_lost,
184 &rtt)) {
185 OnNetworkChanged(new_bitrate, fraction_lost, rtt);
186 }
187}
188
189void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
190 const uint8_t fraction_loss,
191 const uint32_t rtt,
192 const int number_of_packets,
193 const uint32_t now_ms) {
194 uint32_t new_bitrate = 0;
195 uint8_t loss = fraction_loss;
196 CriticalSectionScoped cs(critsect_);
197 if (bandwidth_estimation_.UpdatePacketLoss(number_of_packets, rtt, now_ms,
198 &loss, &new_bitrate)) {
199 OnNetworkChanged(new_bitrate, loss, rtt);
200 }
201}
202
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000203void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate,
204 const uint8_t fraction_loss,
205 const uint32_t rtt) {
206 // Sanity check.
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000207 if (bitrate_observers_.empty())
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000208 return;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000209
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000210 uint32_t sum_min_bitrates = 0;
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000211 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000212 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
213 sum_min_bitrates += it->second->min_bitrate_;
214 }
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000215 if (bitrate <= sum_min_bitrates)
216 return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
217 else
218 return NormalRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
219}
220
221void BitrateControllerImpl::NormalRateAllocation(uint32_t bitrate,
222 uint8_t fraction_loss,
223 uint32_t rtt,
224 uint32_t sum_min_bitrates) {
225 uint32_t number_of_observers = bitrate_observers_.size();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000226 uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) /
227 number_of_observers;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000228 // Use map to sort list based on max bitrate.
229 ObserverSortingMap list_max_bitrates;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000230 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000231 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
232 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>(
233 it->second->max_bitrate_,
234 new ObserverConfiguration(it->first, it->second->min_bitrate_)));
235 }
236 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
237 while (max_it != list_max_bitrates.end()) {
238 number_of_observers--;
239 uint32_t observer_allowance = max_it->second->min_bitrate_ +
240 bitrate_per_observer;
241 if (max_it->first < observer_allowance) {
242 // We have more than enough for this observer.
243 // Carry the remainder forward.
244 uint32_t remainder = observer_allowance - max_it->first;
245 if (number_of_observers != 0) {
246 bitrate_per_observer += remainder / number_of_observers;
247 }
248 max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss,
249 rtt);
250 } else {
251 max_it->second->observer_->OnNetworkChanged(observer_allowance,
252 fraction_loss, rtt);
253 }
254 delete max_it->second;
255 list_max_bitrates.erase(max_it);
256 // Prepare next iteration.
257 max_it = list_max_bitrates.begin();
258 }
259}
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000260
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000261void BitrateControllerImpl::LowRateAllocation(uint32_t bitrate,
262 uint8_t fraction_loss,
263 uint32_t rtt,
264 uint32_t sum_min_bitrates) {
265 if (enforce_min_bitrate_) {
266 // Min bitrate to all observers.
267 BitrateControllerImpl::BitrateObserverConfList::iterator it;
268 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
269 ++it) {
270 it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss, rtt);
271 }
272 // Set sum of min to current send bitrate.
273 bandwidth_estimation_.SetSendBitrate(sum_min_bitrates);
274 } else {
275 // Allocate up to |min_bitrate_| to one observer at a time, until
276 // |bitrate| is depleted.
277 uint32_t remainder = bitrate;
278 BitrateControllerImpl::BitrateObserverConfList::iterator it;
279 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
280 ++it) {
281 uint32_t allocation = std::min(remainder, it->second->min_bitrate_);
282 it->first->OnNetworkChanged(allocation, fraction_loss, rtt);
283 remainder -= allocation;
284 }
285 // Set |bitrate| to current send bitrate.
286 bandwidth_estimation_.SetSendBitrate(bitrate);
287 }
288}
289
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000290bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000291 CriticalSectionScoped cs(critsect_);
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000292 return bandwidth_estimation_.AvailableBandwidth(bandwidth);
293}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000294
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000295} // namespace webrtc