Reland of BWE allocation strategy

TBR=stefan@webrtc.org,alexnarest@webrtc.org

Bug: webrtc:8243
Change-Id: Ie68e4f414b2ac32ba4e64877cb250fabcb089a07
Reviewed-on: https://webrtc-review.googlesource.com/13940
Commit-Queue: Alex Narest <alexnarest@webrtc.org>
Reviewed-by: Alex Narest <alexnarest@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20369}
diff --git a/call/bitrate_allocator.cc b/call/bitrate_allocator.cc
index b379f7f..e186380 100644
--- a/call/bitrate_allocator.cc
+++ b/call/bitrate_allocator.cc
@@ -12,6 +12,7 @@
 #include "call/bitrate_allocator.h"
 
 #include <algorithm>
+#include <memory>
 #include <utility>
 
 #include "modules/bitrate_controller/include/bitrate_controller.h"
@@ -56,7 +57,8 @@
       clock_(Clock::GetRealTimeClock()),
       last_bwe_log_time_(0),
       total_requested_padding_bitrate_(0),
-      total_requested_min_bitrate_(0) {
+      total_requested_min_bitrate_(0),
+      bitrate_allocation_strategy_(nullptr) {
   sequenced_checker_.Detach();
 }
 
@@ -199,6 +201,7 @@
 
 void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
   RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
+
   auto it = FindObserverConfig(observer);
   if (it != bitrate_observer_configs_.end()) {
     bitrate_observer_configs_.erase(it);
@@ -224,6 +227,13 @@
   }
 }
 
+void BitrateAllocator::SetBitrateAllocationStrategy(
+    std::unique_ptr<rtc::BitrateAllocationStrategy>
+        bitrate_allocation_strategy) {
+  RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
+  bitrate_allocation_strategy_ = std::move(bitrate_allocation_strategy);
+}
+
 BitrateAllocator::ObserverConfigs::iterator
 BitrateAllocator::FindObserverConfig(const BitrateAllocatorObserver* observer) {
   RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_);
@@ -241,6 +251,25 @@
   if (bitrate_observer_configs_.empty())
     return ObserverAllocation();
 
+  if (bitrate_allocation_strategy_ != nullptr) {
+    std::vector<const rtc::BitrateAllocationStrategy::TrackConfig*>
+        track_configs(bitrate_observer_configs_.size());
+    int i = 0;
+    for (const auto& c : bitrate_observer_configs_) {
+      track_configs[i++] = &c;
+    }
+    std::vector<uint32_t> track_allocations =
+        bitrate_allocation_strategy_->AllocateBitrates(bitrate, track_configs);
+    // The strategy should return allocation for all tracks.
+    RTC_CHECK(track_allocations.size() == bitrate_observer_configs_.size());
+    ObserverAllocation allocation;
+    auto track_allocations_it = track_allocations.begin();
+    for (const auto& observer_config : bitrate_observer_configs_) {
+      allocation[observer_config.observer] = *track_allocations_it++;
+    }
+    return allocation;
+  }
+
   if (bitrate == 0)
     return ZeroRateAllocation();