Reland #2 of Issue 2434073003: Extract bitrate allocation ...

This is yet another reland of https://codereview.webrtc.org/2434073003/
including two fixes:

1. SimulcastRateAllocator did not handle the screenshare settings properly for numSimulcastStreams = 1. Additional test case was added for that.
2. In VideoSender, when rate allocation is updated after setting a new VideoCodec config, only update the state of the EncoderParameters, but don't actually run SetRateAllocation on the encoder itself. This caused some problems upstreams.

Please review only the changes after patch set 1.

Original description:

Extract bitrate allocation of spatial/temporal layers out of codec impl.

This CL makes a number of intervowen changes:

* Add BitrateAllocation struct, that contains a codec independent view
  of how the target bitrate is distributed over spatial and temporal
  layers.

* Adds the BitrateAllocator interface, which takes a bitrate and frame
  rate and produces a BitrateAllocation.

* A default (non layered) implementation is added, and
  SimulcastRateAllocator is extended to fully handle VP8 allocation.
  This includes capturing TemporalLayer instances created by the
  encoder.

* ViEEncoder now owns both the bitrate allocator and the temporal layer
  factories for VP8. This allows allocation to happen fully outside of
  the encoder implementation.

This refactoring will make it possible for ViEEncoder to signal the
full picture of target bitrates to the RTCP module.

BUG=webrtc:6301
R=stefan@webrtc.org

Review URL: https://codereview.webrtc.org/2510583002 .

Cr-Commit-Position: refs/heads/master@{#15105}
diff --git a/webrtc/common_types.cc b/webrtc/common_types.cc
index e78a4aa..3f8d877 100644
--- a/webrtc/common_types.cc
+++ b/webrtc/common_types.cc
@@ -11,6 +11,7 @@
 #include "webrtc/base/checks.h"
 #include "webrtc/common_types.h"
 
+#include <limits>
 #include <string.h>
 
 namespace webrtc {
@@ -59,6 +60,7 @@
       simulcastStream(),
       spatialLayers(),
       mode(kRealtimeVideo),
+      expect_encode_from_texture(false),
       codecSpecific() {}
 
 VideoCodecVP8* VideoCodec::VP8() {
@@ -91,4 +93,89 @@
   return codecSpecific.H264;
 }
 
+static const char* kPayloadNameVp8 = "VP8";
+static const char* kPayloadNameVp9 = "VP9";
+static const char* kPayloadNameH264 = "H264";
+static const char* kPayloadNameI420 = "I420";
+static const char* kPayloadNameRED = "RED";
+static const char* kPayloadNameULPFEC = "ULPFEC";
+static const char* kPayloadNameGeneric = "Generic";
+
+rtc::Optional<std::string> CodecTypeToPayloadName(VideoCodecType type) {
+  switch (type) {
+    case kVideoCodecVP8:
+      return rtc::Optional<std::string>(kPayloadNameVp8);
+    case kVideoCodecVP9:
+      return rtc::Optional<std::string>(kPayloadNameVp9);
+    case kVideoCodecH264:
+      return rtc::Optional<std::string>(kPayloadNameH264);
+    case kVideoCodecI420:
+      return rtc::Optional<std::string>(kPayloadNameI420);
+    case kVideoCodecRED:
+      return rtc::Optional<std::string>(kPayloadNameRED);
+    case kVideoCodecULPFEC:
+      return rtc::Optional<std::string>(kPayloadNameULPFEC);
+    case kVideoCodecGeneric:
+      return rtc::Optional<std::string>(kPayloadNameGeneric);
+    default:
+      return rtc::Optional<std::string>();
+  }
+}
+
+rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
+  if (name == kPayloadNameVp8)
+    return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
+  if (name == kPayloadNameVp9)
+    return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
+  if (name == kPayloadNameH264)
+    return rtc::Optional<VideoCodecType>(kVideoCodecH264);
+  if (name == kPayloadNameI420)
+    return rtc::Optional<VideoCodecType>(kVideoCodecI420);
+  if (name == kPayloadNameRED)
+    return rtc::Optional<VideoCodecType>(kVideoCodecRED);
+  if (name == kPayloadNameULPFEC)
+    return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
+  if (name == kPayloadNameGeneric)
+    return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
+  return rtc::Optional<VideoCodecType>();
+}
+
+const uint32_t BitrateAllocation::kMaxBitrateBps =
+    std::numeric_limits<uint32_t>::max();
+
+BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{} {}
+
+bool BitrateAllocation::SetBitrate(size_t spatial_index,
+                                   size_t temporal_index,
+                                   uint32_t bitrate_bps) {
+  RTC_DCHECK_LT(spatial_index, static_cast<size_t>(kMaxSpatialLayers));
+  RTC_DCHECK_LT(temporal_index, static_cast<size_t>(kMaxTemporalStreams));
+  RTC_DCHECK_LE(bitrates_[spatial_index][temporal_index], sum_);
+  uint64_t new_bitrate_sum_bps = sum_;
+  new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index];
+  new_bitrate_sum_bps += bitrate_bps;
+  if (new_bitrate_sum_bps > kMaxBitrateBps)
+    return false;
+
+  bitrates_[spatial_index][temporal_index] = bitrate_bps;
+  sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
+  return true;
+}
+
+uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
+                                       size_t temporal_index) const {
+  RTC_DCHECK_LT(spatial_index, static_cast<size_t>(kMaxSpatialLayers));
+  RTC_DCHECK_LT(temporal_index, static_cast<size_t>(kMaxTemporalStreams));
+  return bitrates_[spatial_index][temporal_index];
+}
+
+// Get the sum of all the temporal layer for a specific spatial layer.
+uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
+  RTC_DCHECK_LT(spatial_index, static_cast<size_t>(kMaxSpatialLayers));
+  uint32_t sum = 0;
+  for (int i = 0; i < kMaxTemporalStreams; ++i)
+    sum += bitrates_[spatial_index][i];
+  return sum;
+}
+
 }  // namespace webrtc