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/modules/video_coding/video_coding_impl.cc b/webrtc/modules/video_coding/video_coding_impl.cc
index 2f709b6..021281b 100644
--- a/webrtc/modules/video_coding/video_coding_impl.cc
+++ b/webrtc/modules/video_coding/video_coding_impl.cc
@@ -11,10 +11,14 @@
 #include "webrtc/modules/video_coding/video_coding_impl.h"
 
 #include <algorithm>
+#include <utility>
 
 #include "webrtc/common_types.h"
+#include "webrtc/common_video/include/video_bitrate_allocator.h"
 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
 #include "webrtc/base/criticalsection.h"
+#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
+#include "webrtc/modules/video_coding/include/video_codec_initializer.h"
 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
 #include "webrtc/modules/video_coding/encoded_frame.h"
 #include "webrtc/modules/video_coding/jitter_buffer.h"
@@ -104,6 +108,21 @@
   int32_t RegisterSendCodec(const VideoCodec* sendCodec,
                             uint32_t numberOfCores,
                             uint32_t maxPayloadSize) override {
+    if (sendCodec != nullptr && sendCodec->codecType == kVideoCodecVP8) {
+      // Set up a rate allocator and temporal layers factory for this vp8
+      // instance. The codec impl will have a raw pointer to the TL factory,
+      // and will call it when initializing. Since this can happen
+      // asynchronously keep the instance alive until destruction or until a
+      // new send codec is registered.
+      VideoCodec vp8_codec = *sendCodec;
+      std::unique_ptr<TemporalLayersFactory> tl_factory(
+          new TemporalLayersFactory());
+      vp8_codec.VP8()->tl_factory = tl_factory.get();
+      rate_allocator_ = VideoCodecInitializer::CreateBitrateAllocator(
+          vp8_codec, std::move(tl_factory));
+      return sender_.RegisterSendCodec(&vp8_codec, numberOfCores,
+                                       maxPayloadSize);
+    }
     return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
   }
 
@@ -126,7 +145,8 @@
   int32_t SetChannelParameters(uint32_t target_bitrate,  // bits/s.
                                uint8_t lossRate,
                                int64_t rtt) override {
-    return sender_.SetChannelParameters(target_bitrate, lossRate, rtt);
+    return sender_.SetChannelParameters(target_bitrate, lossRate, rtt,
+                                        rate_allocator_.get());
   }
 
   int32_t RegisterProtectionCallback(
@@ -256,6 +276,7 @@
  private:
   EncodedImageCallbackWrapper post_encode_callback_;
   vcm::VideoSender sender_;
+  std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
   vcm::VideoReceiver receiver_;
 };
 }  // namespace