Use single FrameBufferController in VP8, created by a factory.

This CL paves the way to making FrameBufferController injectable.

LibvpxVp8Encoder can manage multiple streams. Prior to this CL,
each stream had its own frame buffer controller, all of them held
in a vector by LibvpxVp8Encoder. This complicated the code and
produced some code duplication (cf. SetupTemporalLayers).

This CL:
1. Replaces CreateVp8TemporalLayers() by a factory. (Later CLs
   will make this factory injectable.)
2. Makes LibvpxVp8Encoder use a single controller. This single
   controller will, in the case of multiple streams, delegate
   its work to multiple controllers, but that fact is not visible
   to LibvpxVp8Encoder.

This CL also squashes CL #126046 (Send notifications of RTT and
PLR changes to Vp8FrameBufferController) into it.

Bug: webrtc:10382
Change-Id: Id9b55734bebb457acc276f34a7a9e52cc19c8eb9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/126483
Commit-Queue: Elad Alon <eladalon@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27206}
diff --git a/api/video_codecs/BUILD.gn b/api/video_codecs/BUILD.gn
index 50e3d02..24b1298 100644
--- a/api/video_codecs/BUILD.gn
+++ b/api/video_codecs/BUILD.gn
@@ -31,6 +31,7 @@
     "vp8_frame_buffer_controller.h",
     "vp8_frame_config.cc",
     "vp8_frame_config.h",
+    "vp8_temporal_layers.cc",
     "vp8_temporal_layers.h",
   ]
 
@@ -95,20 +96,19 @@
   ]
 }
 
-rtc_static_library("create_vp8_temporal_layers") {
+rtc_static_library("vp8_temporal_layers_factory") {
   visibility = [ "*" ]
   allow_poison = [ "software_video_codecs" ]
   sources = [
-    "create_vp8_temporal_layers.cc",
-    "create_vp8_temporal_layers.h",
+    "vp8_temporal_layers_factory.cc",
+    "vp8_temporal_layers_factory.h",
   ]
 
   deps = [
     ":video_codecs_api",
-    "../..:webrtc_common",
-    "../../modules/video_coding:video_codec_interface",
+    "../../modules/video_coding:video_coding_utility",
     "../../modules/video_coding:webrtc_vp8_temporal_layers",
-    "../../system_wrappers:system_wrappers",
+    "../../rtc_base:checks",
     "//third_party/abseil-cpp/absl/memory",
   ]
 }
diff --git a/api/video_codecs/create_vp8_temporal_layers.cc b/api/video_codecs/create_vp8_temporal_layers.cc
deleted file mode 100644
index 26f734a..0000000
--- a/api/video_codecs/create_vp8_temporal_layers.cc
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "api/video_codecs/create_vp8_temporal_layers.h"
-#include "absl/memory/memory.h"
-#include "api/video_codecs/vp8_temporal_layers.h"
-#include "modules/video_coding/codecs/vp8/default_temporal_layers.h"
-#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
-
-namespace webrtc {
-
-std::unique_ptr<Vp8TemporalLayers> CreateVp8TemporalLayers(
-    Vp8TemporalLayersType type,
-    int num_temporal_layers) {
-  switch (type) {
-    case Vp8TemporalLayersType::kFixedPattern:
-      return absl::make_unique<DefaultTemporalLayers>(num_temporal_layers);
-    case Vp8TemporalLayersType::kBitrateDynamic:
-      // Conference mode temporal layering for screen content in base stream.
-      return absl::make_unique<ScreenshareLayers>(num_temporal_layers);
-  }
-}
-
-}  // namespace webrtc
diff --git a/api/video_codecs/create_vp8_temporal_layers.h b/api/video_codecs/create_vp8_temporal_layers.h
deleted file mode 100644
index 52f14c8..0000000
--- a/api/video_codecs/create_vp8_temporal_layers.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef API_VIDEO_CODECS_CREATE_VP8_TEMPORAL_LAYERS_H_
-#define API_VIDEO_CODECS_CREATE_VP8_TEMPORAL_LAYERS_H_
-
-#include <memory>
-
-#include "api/video_codecs/vp8_temporal_layers.h"
-
-namespace webrtc {
-
-std::unique_ptr<Vp8TemporalLayers> CreateVp8TemporalLayers(
-    Vp8TemporalLayersType type,
-    int num_temporal_layers);
-
-}  // namespace webrtc
-
-#endif  // API_VIDEO_CODECS_CREATE_VP8_TEMPORAL_LAYERS_H_
diff --git a/api/video_codecs/video_encoder.cc b/api/video_codecs/video_encoder.cc
index a8b6f42..233ca24 100644
--- a/api/video_codecs/video_encoder.cc
+++ b/api/video_codecs/video_encoder.cc
@@ -125,6 +125,10 @@
   return SetRates(allocation.get_sum_kbps(), framerate);
 }
 
+void VideoEncoder::OnPacketLossRateUpdate(float packet_loss_rate) {}
+
+void VideoEncoder::OnRttUpdate(int64_t rtt_ms) {}
+
 // TODO(webrtc:9722): Remove and make pure virtual.
 VideoEncoder::EncoderInfo VideoEncoder::GetEncoderInfo() const {
   return EncoderInfo();
diff --git a/api/video_codecs/video_encoder.h b/api/video_codecs/video_encoder.h
index dc68928..9ff8faa 100644
--- a/api/video_codecs/video_encoder.h
+++ b/api/video_codecs/video_encoder.h
@@ -263,6 +263,16 @@
   virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
                                     uint32_t framerate);
 
+  // Inform the encoder when the packet loss rate changes.
+  //
+  // Input:   - packet_loss_rate  : The packet loss rate (0.0 to 1.0).
+  virtual void OnPacketLossRateUpdate(float packet_loss_rate);
+
+  // Inform the encoder when the round trip time changes.
+  //
+  // Input:   - rtt_ms            : The new RTT, in milliseconds.
+  virtual void OnRttUpdate(int64_t rtt_ms);
+
   // Returns meta-data about the encoder, such as implementation name.
   // The output of this method may change during runtime. For instance if a
   // hardware encoder fails, it may fall back to doing software encoding using
diff --git a/api/video_codecs/vp8_frame_buffer_controller.h b/api/video_codecs/vp8_frame_buffer_controller.h
index 93ed6da..7f4c282 100644
--- a/api/video_codecs/vp8_frame_buffer_controller.h
+++ b/api/video_codecs/vp8_frame_buffer_controller.h
@@ -11,8 +11,10 @@
 #ifndef API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_
 #define API_VIDEO_CODECS_VP8_FRAME_BUFFER_CONTROLLER_H_
 
+#include <memory>
 #include <vector>
 
+#include "api/video_codecs/video_codec.h"
 #include "api/video_codecs/vp8_frame_config.h"
 
 namespace webrtc {
@@ -66,10 +68,15 @@
 };
 
 // This interface defines a way of delegating the logic of buffer management.
+// Multiple streams may be controlled by a single controller, demuxing between
+// them using stream_index.
 class Vp8FrameBufferController {
  public:
   virtual ~Vp8FrameBufferController() = default;
 
+  // Number of streamed controlled by |this|.
+  virtual size_t StreamCount() const = 0;
+
   // If this method returns true, the encoder is free to drop frames for
   // instance in an effort to uphold encoding bitrate.
   // If this return false, the encoder must not drop any frames unless:
@@ -79,10 +86,11 @@
   //     re-encode the image at a low bitrate. In this case the encoder should
   //     call OnEncodeDone() once with size = 0 to indicate drop, and then call
   //     OnEncodeDone() again when the frame has actually been encoded.
-  virtual bool SupportsEncoderFrameDropping() const = 0;
+  virtual bool SupportsEncoderFrameDropping(size_t stream_index) const = 0;
 
   // New target bitrate, per temporal layer.
-  virtual void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
+  virtual void OnRatesUpdated(size_t stream_index,
+                              const std::vector<uint32_t>& bitrates_bps,
                               int framerate_fps) = 0;
 
   // Called by the encoder before encoding a frame. |cfg| contains the current
@@ -90,7 +98,8 @@
   // to be changed before the encode step, |cfg| should be changed and then
   // return true. If false is returned, the encoder will proceed without
   // updating the configuration.
-  virtual bool UpdateConfiguration(Vp8EncoderConfig* cfg) = 0;
+  virtual bool UpdateConfiguration(size_t stream_index,
+                                   Vp8EncoderConfig* cfg) = 0;
 
   // Returns the recommended VP8 encode flags needed, and moves the temporal
   // pattern to the next frame.
@@ -99,7 +108,8 @@
   // The timestamp uses a 90kHz RTP clock.
   // After calling this method, first call the actual encoder with the provided
   // frame configuration, and then OnEncodeDone() below.
-  virtual Vp8FrameConfig UpdateLayerConfig(uint32_t rtp_timestamp) = 0;
+  virtual Vp8FrameConfig UpdateLayerConfig(size_t stream_index,
+                                           uint32_t rtp_timestamp) = 0;
 
   // Called after the encode step is done. |rtp_timestamp| must match the
   // parameter use in the UpdateLayerConfig() call.
@@ -114,11 +124,28 @@
   // If |size_bytes| > 0, |qp| should indicate the frame-level QP this frame was
   // encoded at. If the encoder does not support extracting this, |qp| should be
   // set to 0.
-  virtual void OnEncodeDone(uint32_t rtp_timestamp,
+  virtual void OnEncodeDone(size_t stream_index,
+                            uint32_t rtp_timestamp,
                             size_t size_bytes,
                             bool is_keyframe,
                             int qp,
                             CodecSpecificInfo* info) = 0;
+
+  // Called by the encoder when the packet loss rate changes.
+  // |packet_loss_rate| runs between 0.0 (no loss) and 1.0 (everything lost).
+  virtual void OnPacketLossRateUpdate(float packet_loss_rate) = 0;
+
+  // Called by the encoder when the round trip time changes.
+  virtual void OnRttUpdate(int64_t rtt_ms) = 0;
+};
+
+// Interface for a factory of Vp8FrameBufferController instances.
+class Vp8FrameBufferControllerFactory {
+ public:
+  virtual ~Vp8FrameBufferControllerFactory() = default;
+
+  virtual std::unique_ptr<Vp8FrameBufferController> Create(
+      const VideoCodec& codec) = 0;
 };
 
 }  // namespace webrtc
diff --git a/api/video_codecs/vp8_temporal_layers.cc b/api/video_codecs/vp8_temporal_layers.cc
new file mode 100644
index 0000000..a3c0db1
--- /dev/null
+++ b/api/video_codecs/vp8_temporal_layers.cc
@@ -0,0 +1,84 @@
+/*
+ *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "api/video_codecs/vp8_temporal_layers.h"
+
+#include <utility>
+
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+Vp8TemporalLayers::Vp8TemporalLayers(
+    std::vector<std::unique_ptr<Vp8FrameBufferController>>&& controllers)
+    : controllers_(std::move(controllers)) {
+  RTC_DCHECK(!controllers_.empty());
+  RTC_DCHECK(std::none_of(
+      controllers_.begin(), controllers_.end(),
+      [](const std::unique_ptr<Vp8FrameBufferController>& controller) {
+        return controller.get() == nullptr;
+      }));
+}
+
+size_t Vp8TemporalLayers::StreamCount() const {
+  return controllers_.size();
+}
+
+bool Vp8TemporalLayers::SupportsEncoderFrameDropping(
+    size_t stream_index) const {
+  RTC_DCHECK_LT(stream_index, controllers_.size());
+  return controllers_[stream_index]->SupportsEncoderFrameDropping(0);
+}
+
+void Vp8TemporalLayers::OnRatesUpdated(
+    size_t stream_index,
+    const std::vector<uint32_t>& bitrates_bps,
+    int framerate_fps) {
+  RTC_DCHECK_LT(stream_index, controllers_.size());
+  return controllers_[stream_index]->OnRatesUpdated(0, bitrates_bps,
+                                                    framerate_fps);
+}
+
+bool Vp8TemporalLayers::UpdateConfiguration(size_t stream_index,
+                                            Vp8EncoderConfig* cfg) {
+  RTC_DCHECK_LT(stream_index, controllers_.size());
+  return controllers_[stream_index]->UpdateConfiguration(0, cfg);
+}
+
+Vp8FrameConfig Vp8TemporalLayers::UpdateLayerConfig(size_t stream_index,
+                                                    uint32_t rtp_timestamp) {
+  RTC_DCHECK_LT(stream_index, controllers_.size());
+  return controllers_[stream_index]->UpdateLayerConfig(0, rtp_timestamp);
+}
+
+void Vp8TemporalLayers::OnEncodeDone(size_t stream_index,
+                                     uint32_t rtp_timestamp,
+                                     size_t size_bytes,
+                                     bool is_keyframe,
+                                     int qp,
+                                     CodecSpecificInfo* info) {
+  RTC_DCHECK_LT(stream_index, controllers_.size());
+  return controllers_[stream_index]->OnEncodeDone(0, rtp_timestamp, size_bytes,
+                                                  is_keyframe, qp, info);
+}
+
+void Vp8TemporalLayers::OnPacketLossRateUpdate(float packet_loss_rate) {
+  for (auto& controller : controllers_) {
+    controller->OnPacketLossRateUpdate(packet_loss_rate);
+  }
+}
+
+void Vp8TemporalLayers::OnRttUpdate(int64_t rtt_ms) {
+  for (auto& controller : controllers_) {
+    controller->OnRttUpdate(rtt_ms);
+  }
+}
+
+}  // namespace webrtc
diff --git a/api/video_codecs/vp8_temporal_layers.h b/api/video_codecs/vp8_temporal_layers.h
index 61a9def..d5d29cd 100644
--- a/api/video_codecs/vp8_temporal_layers.h
+++ b/api/video_codecs/vp8_temporal_layers.h
@@ -11,8 +11,10 @@
 #ifndef API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_H_
 #define API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_H_
 
+#include <memory>
 #include <vector>
 
+#include "api/video_codecs/video_codec.h"
 #include "api/video_codecs/vp8_frame_buffer_controller.h"
 #include "api/video_codecs/vp8_frame_config.h"
 
@@ -22,28 +24,43 @@
 // kFixedPattern uses a fixed repeating pattern of 1-4 layers.
 // kBitrateDynamic can allocate frames dynamically to 1 or 2 layers, based on
 // the bitrate produced.
+// TODO(eladalon): Remove this enum.
 enum class Vp8TemporalLayersType { kFixedPattern, kBitrateDynamic };
 
 // This interface defines a way of getting the encoder settings needed to
 // realize a temporal layer structure.
-class Vp8TemporalLayers : public Vp8FrameBufferController {
+class Vp8TemporalLayers final : public Vp8FrameBufferController {
  public:
+  explicit Vp8TemporalLayers(
+      std::vector<std::unique_ptr<Vp8FrameBufferController>>&& controllers);
   ~Vp8TemporalLayers() override = default;
 
-  bool SupportsEncoderFrameDropping() const override = 0;
+  size_t StreamCount() const override;
 
-  void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
-                      int framerate_fps) override = 0;
+  bool SupportsEncoderFrameDropping(size_t stream_index) const override;
 
-  bool UpdateConfiguration(Vp8EncoderConfig* cfg) override = 0;
+  void OnRatesUpdated(size_t stream_index,
+                      const std::vector<uint32_t>& bitrates_bps,
+                      int framerate_fps) override;
 
-  Vp8FrameConfig UpdateLayerConfig(uint32_t rtp_timestamp) override = 0;
+  bool UpdateConfiguration(size_t stream_index, Vp8EncoderConfig* cfg) override;
 
-  void OnEncodeDone(uint32_t rtp_timestamp,
+  Vp8FrameConfig UpdateLayerConfig(size_t stream_index,
+                                   uint32_t rtp_timestamp) override;
+
+  void OnEncodeDone(size_t stream_index,
+                    uint32_t rtp_timestamp,
                     size_t size_bytes,
                     bool is_keyframe,
                     int qp,
-                    CodecSpecificInfo* info) override = 0;
+                    CodecSpecificInfo* info) override;
+
+  void OnPacketLossRateUpdate(float packet_loss_rate) override;
+
+  void OnRttUpdate(int64_t rtt_ms) override;
+
+ private:
+  std::vector<std::unique_ptr<Vp8FrameBufferController>> controllers_;
 };
 
 }  // namespace webrtc
diff --git a/api/video_codecs/vp8_temporal_layers_factory.cc b/api/video_codecs/vp8_temporal_layers_factory.cc
new file mode 100644
index 0000000..2eb7bb9
--- /dev/null
+++ b/api/video_codecs/vp8_temporal_layers_factory.cc
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "api/video_codecs/vp8_temporal_layers_factory.h"
+
+#include <algorithm>
+#include <utility>
+#include <vector>
+
+#include "absl/memory/memory.h"
+#include "modules/video_coding/codecs/vp8/default_temporal_layers.h"
+#include "modules/video_coding/codecs/vp8/screenshare_layers.h"
+#include "modules/video_coding/utility/simulcast_utility.h"
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+std::unique_ptr<Vp8FrameBufferController> Vp8TemporalLayersFactory::Create(
+    const VideoCodec& codec) {
+  std::vector<std::unique_ptr<Vp8FrameBufferController>> controllers;
+  const int num_streams = SimulcastUtility::NumberOfSimulcastStreams(codec);
+  RTC_DCHECK_GE(num_streams, 1);
+  controllers.reserve(num_streams);
+
+  for (int i = 0; i < num_streams; ++i) {
+    int num_temporal_layers =
+        SimulcastUtility::NumberOfTemporalLayers(codec, i);
+    if (SimulcastUtility::IsConferenceModeScreenshare(codec) && i == 0) {
+      // Legacy screenshare layers supports max 2 layers.
+      num_temporal_layers = std::max(2, num_temporal_layers);
+      controllers.push_back(
+          absl::make_unique<ScreenshareLayers>(num_temporal_layers));
+    } else {
+      controllers.push_back(
+          absl::make_unique<DefaultTemporalLayers>(num_temporal_layers));
+    }
+  }
+
+  return absl::make_unique<Vp8TemporalLayers>(std::move(controllers));
+}
+
+}  // namespace webrtc
diff --git a/api/video_codecs/vp8_temporal_layers_factory.h b/api/video_codecs/vp8_temporal_layers_factory.h
new file mode 100644
index 0000000..9c65d4c
--- /dev/null
+++ b/api/video_codecs/vp8_temporal_layers_factory.h
@@ -0,0 +1,30 @@
+/*
+ *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_FACTORY_H_
+#define API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_FACTORY_H_
+
+#include <memory>
+
+#include "api/video_codecs/vp8_temporal_layers.h"
+
+namespace webrtc {
+
+class Vp8TemporalLayersFactory : public Vp8FrameBufferControllerFactory {
+ public:
+  ~Vp8TemporalLayersFactory() override = default;
+
+  std::unique_ptr<Vp8FrameBufferController> Create(
+      const VideoCodec& codec) override;
+};
+
+}  // namespace webrtc
+
+#endif  // API_VIDEO_CODECS_VP8_TEMPORAL_LAYERS_FACTORY_H_