blob: 07f830610c6c2b853d7e4d64fe72d8973f8cecbb [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()
tommi63cb4342016-01-20 02:32:54 -080027 : bitrate_observers_(),
sprang2f48d942015-11-05 04:25:49 -080028 bitrate_observers_modified_(false),
Stefan Holmere5904162015-03-26 11:11:06 +010029 enforce_min_bitrate_(true),
30 last_bitrate_bps_(kDefaultBitrateBps),
31 last_fraction_loss_(0),
sprang2f48d942015-11-05 04:25:49 -080032 last_rtt_(0) {}
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000033
sprang2f48d942015-11-05 04:25:49 -080034uint32_t BitrateAllocator::OnNetworkChanged(uint32_t bitrate,
35 uint8_t fraction_loss,
36 int64_t rtt) {
tommi63cb4342016-01-20 02:32:54 -080037 rtc::CritScope lock(&crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +010038 last_bitrate_bps_ = bitrate;
39 last_fraction_loss_ = fraction_loss;
40 last_rtt_ = rtt;
sprang2f48d942015-11-05 04:25:49 -080041 uint32_t allocated_bitrate_bps = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010042 ObserverBitrateMap allocation = AllocateBitrates();
sprang2f48d942015-11-05 04:25:49 -080043 for (const auto& kv : allocation) {
Stefan Holmere5904162015-03-26 11:11:06 +010044 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_);
sprang2f48d942015-11-05 04:25:49 -080045 allocated_bitrate_bps += kv.second;
46 }
47 return allocated_bitrate_bps;
Stefan Holmere5904162015-03-26 11:11:06 +010048}
49
50BitrateAllocator::ObserverBitrateMap BitrateAllocator::AllocateBitrates() {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000051 if (bitrate_observers_.empty())
Stefan Holmere5904162015-03-26 11:11:06 +010052 return ObserverBitrateMap();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000053
54 uint32_t sum_min_bitrates = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010055 for (const auto& observer : bitrate_observers_)
Peter Boström8e4e8b02015-09-15 15:08:03 +020056 sum_min_bitrates += observer.second.min_bitrate;
Stefan Holmere5904162015-03-26 11:11:06 +010057 if (last_bitrate_bps_ <= sum_min_bitrates)
58 return LowRateAllocation(last_bitrate_bps_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000059 else
Stefan Holmere5904162015-03-26 11:11:06 +010060 return NormalRateAllocation(last_bitrate_bps_, sum_min_bitrates);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000061}
62
63int BitrateAllocator::AddBitrateObserver(BitrateObserver* observer,
Stefan Holmere5904162015-03-26 11:11:06 +010064 uint32_t min_bitrate_bps,
Peter Boström8e4e8b02015-09-15 15:08:03 +020065 uint32_t max_bitrate_bps) {
tommi63cb4342016-01-20 02:32:54 -080066 rtc::CritScope lock(&crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000067
68 BitrateObserverConfList::iterator it =
69 FindObserverConfigurationPair(observer);
70
Stefan Holmere5904162015-03-26 11:11:06 +010071 // Allow the max bitrate to be exceeded for FEC and retransmissions.
72 // TODO(holmer): We have to get rid of this hack as it makes it difficult to
73 // properly allocate bitrate. The allocator should instead distribute any
74 // extra bitrate after all streams have maxed out.
75 max_bitrate_bps *= kTransmissionMaxBitrateMultiplier;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000076 if (it != bitrate_observers_.end()) {
77 // Update current configuration.
Peter Boström8e4e8b02015-09-15 15:08:03 +020078 it->second.min_bitrate = min_bitrate_bps;
79 it->second.max_bitrate = max_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000080 } else {
81 // Add new settings.
82 bitrate_observers_.push_back(BitrateObserverConfiguration(
Peter Boström8e4e8b02015-09-15 15:08:03 +020083 observer, BitrateConfiguration(min_bitrate_bps, max_bitrate_bps)));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000084 bitrate_observers_modified_ = true;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000085 }
Stefan Holmere5904162015-03-26 11:11:06 +010086
Stefan Holmere5904162015-03-26 11:11:06 +010087 ObserverBitrateMap allocation = AllocateBitrates();
Peter Boström8e4e8b02015-09-15 15:08:03 +020088 int new_observer_bitrate_bps = 0;
Stefan Holmere5904162015-03-26 11:11:06 +010089 for (auto& kv : allocation) {
90 kv.first->OnNetworkChanged(kv.second, last_fraction_loss_, last_rtt_);
91 if (kv.first == observer)
Peter Boström8e4e8b02015-09-15 15:08:03 +020092 new_observer_bitrate_bps = kv.second;
Stefan Holmere5904162015-03-26 11:11:06 +010093 }
Peter Boström8e4e8b02015-09-15 15:08:03 +020094 return new_observer_bitrate_bps;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000095}
96
97void BitrateAllocator::RemoveBitrateObserver(BitrateObserver* observer) {
tommi63cb4342016-01-20 02:32:54 -080098 rtc::CritScope lock(&crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000099 BitrateObserverConfList::iterator it =
100 FindObserverConfigurationPair(observer);
101 if (it != bitrate_observers_.end()) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000102 bitrate_observers_.erase(it);
103 bitrate_observers_modified_ = true;
104 }
105}
106
107void BitrateAllocator::GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps,
108 int* max_bitrate_sum_bps) const {
109 *min_bitrate_sum_bps = 0;
110 *max_bitrate_sum_bps = 0;
111
tommi63cb4342016-01-20 02:32:54 -0800112 rtc::CritScope lock(&crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100113 for (const auto& observer : bitrate_observers_) {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200114 *min_bitrate_sum_bps += observer.second.min_bitrate;
115 *max_bitrate_sum_bps += observer.second.max_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000116 }
117}
118
119BitrateAllocator::BitrateObserverConfList::iterator
120BitrateAllocator::FindObserverConfigurationPair(
121 const BitrateObserver* observer) {
Stefan Holmere5904162015-03-26 11:11:06 +0100122 for (auto it = bitrate_observers_.begin(); it != bitrate_observers_.end();
123 ++it) {
124 if (it->first == observer)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000125 return it;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000126 }
127 return bitrate_observers_.end();
128}
129
130void BitrateAllocator::EnforceMinBitrate(bool enforce_min_bitrate) {
tommi63cb4342016-01-20 02:32:54 -0800131 rtc::CritScope lock(&crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000132 enforce_min_bitrate_ = enforce_min_bitrate;
133}
134
Stefan Holmere5904162015-03-26 11:11:06 +0100135BitrateAllocator::ObserverBitrateMap BitrateAllocator::NormalRateAllocation(
136 uint32_t bitrate,
137 uint32_t sum_min_bitrates) {
mflodman0e7e2592015-11-12 21:02:42 -0800138 uint32_t number_of_observers =
139 static_cast<uint32_t>(bitrate_observers_.size());
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000140 uint32_t bitrate_per_observer =
141 (bitrate - sum_min_bitrates) / number_of_observers;
142 // Use map to sort list based on max bitrate.
143 ObserverSortingMap list_max_bitrates;
Stefan Holmere5904162015-03-26 11:11:06 +0100144 for (const auto& observer : bitrate_observers_) {
145 list_max_bitrates.insert(std::pair<uint32_t, ObserverConfiguration>(
Peter Boström8e4e8b02015-09-15 15:08:03 +0200146 observer.second.max_bitrate,
147 ObserverConfiguration(observer.first, observer.second.min_bitrate)));
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000148 }
Stefan Holmere5904162015-03-26 11:11:06 +0100149 ObserverBitrateMap allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000150 ObserverSortingMap::iterator max_it = list_max_bitrates.begin();
151 while (max_it != list_max_bitrates.end()) {
152 number_of_observers--;
153 uint32_t observer_allowance =
Peter Boström8e4e8b02015-09-15 15:08:03 +0200154 max_it->second.min_bitrate + bitrate_per_observer;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000155 if (max_it->first < observer_allowance) {
156 // We have more than enough for this observer.
157 // Carry the remainder forward.
158 uint32_t remainder = observer_allowance - max_it->first;
159 if (number_of_observers != 0) {
160 bitrate_per_observer += remainder / number_of_observers;
161 }
Peter Boström8e4e8b02015-09-15 15:08:03 +0200162 allocation[max_it->second.observer] = max_it->first;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000163 } else {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200164 allocation[max_it->second.observer] = observer_allowance;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000165 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000166 list_max_bitrates.erase(max_it);
167 // Prepare next iteration.
168 max_it = list_max_bitrates.begin();
169 }
Stefan Holmere5904162015-03-26 11:11:06 +0100170 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000171}
172
Stefan Holmere5904162015-03-26 11:11:06 +0100173BitrateAllocator::ObserverBitrateMap BitrateAllocator::LowRateAllocation(
174 uint32_t bitrate) {
175 ObserverBitrateMap allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000176 if (enforce_min_bitrate_) {
177 // Min bitrate to all observers.
Stefan Holmere5904162015-03-26 11:11:06 +0100178 for (const auto& observer : bitrate_observers_)
Peter Boström8e4e8b02015-09-15 15:08:03 +0200179 allocation[observer.first] = observer.second.min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000180 } else {
Peter Boström8e4e8b02015-09-15 15:08:03 +0200181 // Allocate up to |min_bitrate| to one observer at a time, until
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182 // |bitrate| is depleted.
183 uint32_t remainder = bitrate;
Stefan Holmere5904162015-03-26 11:11:06 +0100184 for (const auto& observer : bitrate_observers_) {
185 uint32_t allocated_bitrate =
Peter Boström8e4e8b02015-09-15 15:08:03 +0200186 std::min(remainder, observer.second.min_bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100187 allocation[observer.first] = allocated_bitrate;
188 remainder -= allocated_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000189 }
190 }
Stefan Holmere5904162015-03-26 11:11:06 +0100191 return allocation;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192}
193} // namespace webrtc