blob: 11c36c096caf3e6addaa81672dd6d6ef06ae776a [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
21class RtcpBandwidthObserverImpl : public RtcpBandwidthObserver {
22 public:
23 explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner)
24 : owner_(owner) {
25 }
26 virtual ~RtcpBandwidthObserverImpl() {
27 }
28 // Received RTCP REMB or TMMBR.
pbos@webrtc.org4fac8a42013-07-31 15:16:20 +000029 virtual void OnReceivedEstimatedBitrate(const uint32_t bitrate) OVERRIDE {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000030 owner_->OnReceivedEstimatedBitrate(bitrate);
31 }
32 // Received RTCP receiver block.
33 virtual void OnReceivedRtcpReceiverReport(
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000034 const ReportBlockList& report_blocks,
35 uint16_t rtt,
36 int64_t now_ms) OVERRIDE {
37 if (report_blocks.empty())
38 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000039
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000040 int fraction_lost_aggregate = 0;
41 int total_number_of_packets = 0;
42
43 // Compute the a weighted average of the fraction loss from all report
44 // blocks.
45 for (ReportBlockList::const_iterator it = report_blocks.begin();
46 it != report_blocks.end(); ++it) {
47 std::map<uint32_t, uint32_t>::iterator seq_num_it =
48 ssrc_to_last_received_extended_high_seq_num_.find(it->sourceSSRC);
49
50 int number_of_packets = 0;
51 if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end())
52 number_of_packets = it->extendedHighSeqNum -
53 seq_num_it->second;
54
55 fraction_lost_aggregate += number_of_packets * it->fractionLost;
56 total_number_of_packets += number_of_packets;
57
58 // Update last received for this SSRC.
59 ssrc_to_last_received_extended_high_seq_num_[it->sourceSSRC] =
60 it->extendedHighSeqNum;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000061 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000062 if (total_number_of_packets == 0)
63 fraction_lost_aggregate = 0;
64 else
65 fraction_lost_aggregate = (fraction_lost_aggregate +
66 total_number_of_packets / 2) / total_number_of_packets;
67 if (fraction_lost_aggregate > 255)
68 return;
69
70 owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt,
71 total_number_of_packets, now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000072 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000073
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000074 private:
75 std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_;
76 BitrateControllerImpl* owner_;
77};
78
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000079class BitrateControllerEnforceMinRate : public BitrateControllerImpl {
80 private:
81 void LowRateAllocation(uint32_t bitrate,
82 uint8_t fraction_loss,
83 uint32_t rtt,
84 uint32_t sum_min_bitrates) {
85 // Min bitrate to all observers.
86 BitrateObserverConfList::iterator it;
87 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
88 ++it) {
89 it->first->OnNetworkChanged(it->second->min_bitrate_, fraction_loss,
90 rtt);
91 }
92 // Set sum of min to current send bitrate.
93 bandwidth_estimation_.SetSendBitrate(sum_min_bitrates);
94 }
95};
96
97class BitrateControllerNoEnforceMinRate : public BitrateControllerImpl {
98 private:
99 void LowRateAllocation(uint32_t bitrate,
100 uint8_t fraction_loss,
101 uint32_t rtt,
102 uint32_t sum_min_bitrates) {
103 // Allocate up to |min_bitrate_| to one observer at a time, until
104 // |bitrate| is depleted.
105 uint32_t remainder = bitrate;
106 BitrateObserverConfList::iterator it;
107 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end();
108 ++it) {
109 uint32_t allocation = std::min(remainder, it->second->min_bitrate_);
110 it->first->OnNetworkChanged(allocation, fraction_loss, rtt);
111 remainder -= allocation;
112 }
113 // Set |bitrate| to current send bitrate.
114 bandwidth_estimation_.SetSendBitrate(bitrate);
115 }
116};
117
118BitrateController* BitrateController::CreateBitrateController(
119 bool enforce_min_bitrate) {
120 if (enforce_min_bitrate) {
121 return new BitrateControllerEnforceMinRate();
122 } else {
123 return new BitrateControllerNoEnforceMinRate();
124 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000125}
126
127BitrateControllerImpl::BitrateControllerImpl()
128 : critsect_(CriticalSectionWrapper::CreateCriticalSection()) {
129}
130
131BitrateControllerImpl::~BitrateControllerImpl() {
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000132 BitrateObserverConfList::iterator it =
pwestin@webrtc.orge9727cd2012-05-03 11:32:25 +0000133 bitrate_observers_.begin();
134 while (it != bitrate_observers_.end()) {
135 delete it->second;
136 bitrate_observers_.erase(it);
137 it = bitrate_observers_.begin();
138 }
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000139 delete critsect_;
140}
141
142RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
143 return new RtcpBandwidthObserverImpl(this);
144}
145
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000146BitrateControllerImpl::BitrateObserverConfList::iterator
147BitrateControllerImpl::FindObserverConfigurationPair(const BitrateObserver*
148 observer) {
149 BitrateObserverConfList::iterator it = bitrate_observers_.begin();
150 for (; it != bitrate_observers_.end(); ++it) {
151 if (it->first == observer) {
152 return it;
153 }
154 }
155 return bitrate_observers_.end();
156}
157
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000158void BitrateControllerImpl::SetBitrateObserver(
159 BitrateObserver* observer,
160 const uint32_t start_bitrate,
161 const uint32_t min_bitrate,
162 const uint32_t max_bitrate) {
163 CriticalSectionScoped cs(critsect_);
164
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000165 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
166 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000167
168 if (it != bitrate_observers_.end()) {
169 // Update current configuration.
170 it->second->start_bitrate_ = start_bitrate;
171 it->second->min_bitrate_ = min_bitrate;
172 it->second->max_bitrate_ = max_bitrate;
173 } else {
174 // Add new settings.
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000175 bitrate_observers_.push_back(BitrateObserverConfiguration(observer,
176 new BitrateConfiguration(start_bitrate, min_bitrate, max_bitrate)));
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177 }
178 uint32_t sum_start_bitrate = 0;
179 uint32_t sum_min_bitrate = 0;
180 uint32_t sum_max_bitrate = 0;
181
182 // Summarize all configurations.
183 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
184 sum_start_bitrate += it->second->start_bitrate_;
185 sum_min_bitrate += it->second->min_bitrate_;
186 sum_max_bitrate += it->second->max_bitrate_;
187 }
188 // Only change start bitrate if we have exactly one observer. By definition
189 // you can only have one start bitrate, once we have our first estimate we
190 // will adapt from there.
191 if (bitrate_observers_.size() == 1) {
192 bandwidth_estimation_.SetSendBitrate(sum_start_bitrate);
193 }
194 bandwidth_estimation_.SetMinMaxBitrate(sum_min_bitrate,
195 sum_max_bitrate);
196}
197
198void BitrateControllerImpl::RemoveBitrateObserver(BitrateObserver* observer) {
199 CriticalSectionScoped cs(critsect_);
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000200 BitrateObserverConfList::iterator it = FindObserverConfigurationPair(
201 observer);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000202 if (it != bitrate_observers_.end()) {
203 delete it->second;
204 bitrate_observers_.erase(it);
205 }
206}
207
208void BitrateControllerImpl::OnReceivedEstimatedBitrate(const uint32_t bitrate) {
209 uint32_t new_bitrate = 0;
210 uint8_t fraction_lost = 0;
211 uint16_t rtt = 0;
212 CriticalSectionScoped cs(critsect_);
213 if (bandwidth_estimation_.UpdateBandwidthEstimate(bitrate,
214 &new_bitrate,
215 &fraction_lost,
216 &rtt)) {
217 OnNetworkChanged(new_bitrate, fraction_lost, rtt);
218 }
219}
220
221void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
222 const uint8_t fraction_loss,
223 const uint32_t rtt,
224 const int number_of_packets,
225 const uint32_t now_ms) {
226 uint32_t new_bitrate = 0;
227 uint8_t loss = fraction_loss;
228 CriticalSectionScoped cs(critsect_);
229 if (bandwidth_estimation_.UpdatePacketLoss(number_of_packets, rtt, now_ms,
230 &loss, &new_bitrate)) {
231 OnNetworkChanged(new_bitrate, loss, rtt);
232 }
233}
234
235// We have the lock here.
236void BitrateControllerImpl::OnNetworkChanged(const uint32_t bitrate,
237 const uint8_t fraction_loss,
238 const uint32_t rtt) {
239 // Sanity check.
240 uint32_t number_of_observers = bitrate_observers_.size();
241 if (number_of_observers == 0) {
242 return;
243 }
244 uint32_t sum_min_bitrates = 0;
stefan@webrtc.org1281dc02012-08-13 16:13:09 +0000245 BitrateObserverConfList::iterator it;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000246 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
247 sum_min_bitrates += it->second->min_bitrate_;
248 }
249 if (bitrate <= sum_min_bitrates) {
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000250 return LowRateAllocation(bitrate, fraction_loss, rtt, sum_min_bitrates);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000251 }
252 uint32_t bitrate_per_observer = (bitrate - sum_min_bitrates) /
253 number_of_observers;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000254 // Use map to sort list based on max bitrate.
255 ObserverSortingMap list_max_bitrates;
256 for (it = bitrate_observers_.begin(); it != bitrate_observers_.end(); ++it) {
257 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration*>(
258 it->second->max_bitrate_,
259 new ObserverConfiguration(it->first, it->second->min_bitrate_)));
260 }
261 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
262 while (max_it != list_max_bitrates.end()) {
263 number_of_observers--;
264 uint32_t observer_allowance = max_it->second->min_bitrate_ +
265 bitrate_per_observer;
266 if (max_it->first < observer_allowance) {
267 // We have more than enough for this observer.
268 // Carry the remainder forward.
269 uint32_t remainder = observer_allowance - max_it->first;
270 if (number_of_observers != 0) {
271 bitrate_per_observer += remainder / number_of_observers;
272 }
273 max_it->second->observer_->OnNetworkChanged(max_it->first, fraction_loss,
274 rtt);
275 } else {
276 max_it->second->observer_->OnNetworkChanged(observer_allowance,
277 fraction_loss, rtt);
278 }
279 delete max_it->second;
280 list_max_bitrates.erase(max_it);
281 // Prepare next iteration.
282 max_it = list_max_bitrates.begin();
283 }
284}
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000285
286bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
287 return bandwidth_estimation_.AvailableBandwidth(bandwidth);
288}
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +0000289
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000290} // namespace webrtc