blob: b3789d3bb6830994fd2ca867ff93771ac0b93e5a [file] [log] [blame]
stefan@webrtc.org792f1a12015-03-04 12:24:26 +00001/*
2 * Copyright (c) 2015 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
mflodman0e7e2592015-11-12 21:02:42 -080012#include "webrtc/call/bitrate_allocator.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000013
14#include <algorithm>
15#include <utility>
16
17#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
18
19namespace webrtc {
20
Stefan Holmere5904162015-03-26 11:11:06 +010021// Allow packets to be transmitted in up to 2 times max video bitrate if the
22// bandwidth estimate allows it.
23const int kTransmissionMaxBitrateMultiplier = 2;
24const int kDefaultBitrateBps = 300000;
25
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000026BitrateAllocator::BitrateAllocator()
27 : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
28 bitrate_observers_(),
sprang2f48d942015-11-05 04:25:49 -080029 bitrate_observers_modified_(false),
Stefan Holmere5904162015-03-26 11:11:06 +010030 enforce_min_bitrate_(true),
31 last_bitrate_bps_(kDefaultBitrateBps),
32 last_fraction_loss_(0),
sprang2f48d942015-11-05 04:25:49 -080033 last_rtt_(0) {}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000034
sprang2f48d942015-11-05 04:25:49 -080035uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate,
36 uint8_t fraction_loss,
37 int64_t rtt) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000038 CriticalSectionScoped lock(crit_sect_.get());
Stefan Holmere5904162015-03-26 11:11:06 +010039 last_bitrate_bps_ = bitrate;
40 last_fraction_loss_ = fraction_loss;
41 last_rtt_ = rtt;
sprang2f48d942015-11-05 04:25:49 -080042 uint32_t allocated_bitrate_bps = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010043 ObserverBitrateMap allocation = AllocateBitrates();
sprang2f48d942015-11-05 04:25:49 -080044 for (const auto& kv : allocation) {
Stefan Holmere5904162015-03-26 11:11:06 +010045 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_);
sprang2f48d942015-11-05 04:25:49 -080046 allocated_bitrate_bps += kv.second;
47 }
48 return allocated_bitrate_bps;
Stefan Holmere5904162015-03-26 11:11:06 +010049}
50
51BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000052 if (bitrate_observers_.empty())
Stefan Holmere5904162015-03-26 11:11:06 +010053 return ObserverBitrateMap();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000054
55 uint32_t sum_min_bitrates = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010056 for (const auto& observer : bitrate_observers_)
Peter Boström8e4e8b02015-09-15 15:08:03 +020057 sum_min_bitrates += observer.second.min_bitrate;
Stefan Holmere5904162015-03-26 11:11:06 +010058 if (last_bitrate_bps_ <= sum_min_bitrates)
59 return LowRateAllocation(last_bitrate_bps_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000060 else
Stefan Holmere5904162015-03-26 11:11:06 +010061 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000062}
63
64int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer,
Stefan Holmere5904162015-03-26 11:11:06 +010065 uint32_t min_bitrate_bps,
Peter Boström8e4e8b02015-09-15 15:08:03 +020066 uint32_t max_bitrate_bps) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000067 CriticalSectionScoped lock(crit_sect_.get());
68
69 BitrateObserverConfList::iterator it =
70 FindObserverConfigurationPair(observer);
71
Stefan Holmere5904162015-03-26 11:11:06 +010072 // Allow the max bitrate to be exceeded for FEC and retransmissions.
73 // TODO(holmer): We have to get rid of this hack as it makes it difficult to
74 // properly allocate bitrate. The allocator should instead distribute any
75 // extra bitrate after all streams have maxed out.
76 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000077 if (it != bitrate_observers_.end()) {
78 // Update current configuration.
Peter Boström8e4e8b02015-09-15 15:08:03 +020079 it->second.min_bitrate = min_bitrate_bps;
80 it->second.max_bitrate = max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000081 } else {
82 // Add new settings.
83 bitrate_observers_.push_back(BitrateObserverConfiguration(
Peter Boström8e4e8b02015-09-15 15:08:03 +020084 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps)));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000085 bitrate_observers_modified_ = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000086 }
Stefan Holmere5904162015-03-26 11:11:06 +010087
Stefan Holmere5904162015-03-26 11:11:06 +010088 ObserverBitrateMap allocation = AllocateBitrates();
Peter Boström8e4e8b02015-09-15 15:08:03 +020089 int new_observer_bitrate_bps = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010090 for (auto& kv : allocation) {
91 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_);
92 if (kv.first == observer)
Peter Boström8e4e8b02015-09-15 15:08:03 +020093 new_observer_bitrate_bps = kv.second;
Stefan Holmere5904162015-03-26 11:11:06 +010094 }
Peter Boström8e4e8b02015-09-15 15:08:03 +020095 return new_observer_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000096}
97
98void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) {
99 CriticalSectionScoped lock(crit_sect_.get());
100 BitrateObserverConfList::iterator it =
101 FindObserverConfigurationPair(observer);
102 if (it != bitrate_observers_.end()) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000103 bitrate_observers_.erase(it);
104 bitrate_observers_modified_ = true;
105 }
106}
107
108void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps,
109 int* max_bitrate_sum_bps) const {
110 *min_bitrate_sum_bps = 0;
111 *max_bitrate_sum_bps = 0;
112
113 CriticalSectionScoped lock(crit_sect_.get());
Stefan Holmere5904162015-03-26 11:11:06 +0100114 for (const auto& observer : bitrate_observers_) {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200115 *min_bitrate_sum_bps += observer.second.min_bitrate;
116 *max_bitrate_sum_bps += observer.second.max_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000117 }
118}
119
120BitrateAllocator::BitrateObserverConfList::iterator
121BitrateAllocator::FindObserverConfigurationPair(
122 const BitrateObserver* observer) {
Stefan Holmere5904162015-03-26 11:11:06 +0100123 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end();
124 ++it) {
125 if (it->first == observer)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000126 return it;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127 }
128 return bitrate_observers_.end();
129}
130
131void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
132 CriticalSectionScoped lock(crit_sect_.get());
133 enforce_min_bitrate_ = enforce_min_bitrate;
134}
135
Stefan Holmere5904162015-03-26 11:11:06 +0100136BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation(
137 uint32_t bitrate,
138 uint32_t sum_min_bitrates) {
mflodman0e7e2592015-11-12 21:02:42 -0800139 uint32_t number_of_observers =
140 static_cast<uint32_t>(bitrate_observers_.size());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000141 uint32_t bitrate_per_observer =
142 (bitrate - sum_min_bitrates) / number_of_observers;
143 // Use map to sort list based on max bitrate.
144 ObserverSortingMap list_max_bitrates;
Stefan Holmere5904162015-03-26 11:11:06 +0100145 for (const auto& observer : bitrate_observers_) {
146 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>(
Peter Boström8e4e8b02015-09-15 15:08:03 +0200147 observer.second.max_bitrate,
148 ObserverConfiguration(observer.first, observer.second.min_bitrate)));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000149 }
Stefan Holmere5904162015-03-26 11:11:06 +0100150 ObserverBitrateMap allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000151 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
152 while (max_it != list_max_bitrates.end()) {
153 number_of_observers--;
154 uint32_t observer_allowance =
Peter Boström8e4e8b02015-09-15 15:08:03 +0200155 max_it->second.min_bitrate + bitrate_per_observer;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000156 if (max_it->first < observer_allowance) {
157 // We have more than enough for this observer.
158 // Carry the remainder forward.
159 uint32_t remainder = observer_allowance - max_it->first;
160 if (number_of_observers != 0) {
161 bitrate_per_observer += remainder / number_of_observers;
162 }
Peter Boström8e4e8b02015-09-15 15:08:03 +0200163 allocation[max_it->second.observer] = max_it->first;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000164 } else {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200165 allocation[max_it->second.observer] = observer_allowance;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000166 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167 list_max_bitrates.erase(max_it);
168 // Prepare next iteration.
169 max_it = list_max_bitrates.begin();
170 }
Stefan Holmere5904162015-03-26 11:11:06 +0100171 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000172}
173
Stefan Holmere5904162015-03-26 11:11:06 +0100174BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation(
175 uint32_t bitrate) {
176 ObserverBitrateMap allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000177 if (enforce_min_bitrate_) {
178 // Min bitrate to all observers.
Stefan Holmere5904162015-03-26 11:11:06 +0100179 for (const auto& observer : bitrate_observers_)
Peter Boström8e4e8b02015-09-15 15:08:03 +0200180 allocation[observer.first] = observer.second.min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000181 } else {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200182 // Allocate up to |min_bitrate| to one observer at a time, until
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000183 // |bitrate| is depleted.
184 uint32_t remainder = bitrate;
Stefan Holmere5904162015-03-26 11:11:06 +0100185 for (const auto& observer : bitrate_observers_) {
186 uint32_t allocated_bitrate =
Peter Boström8e4e8b02015-09-15 15:08:03 +0200187 std::min(remainder, observer.second.min_bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100188 allocation[observer.first] = allocated_bitrate;
189 remainder -= allocated_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000190 }
191 }
Stefan Holmere5904162015-03-26 11:11:06 +0100192 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000193}
194} // namespace webrtc