Keep bitrate constraints.

Don't relax layer bitrate constraints if spatial layering was requested.

Bug: webrtc:10063
Change-Id: Ie572fb6c5fbc677a7dd240dc75b3d75a6e784001
Reviewed-on: https://webrtc-review.googlesource.com/c/112139
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25828}
diff --git a/modules/video_coding/video_codec_initializer.cc b/modules/video_coding/video_codec_initializer.cc
index 86a5ab2..885fa84 100644
--- a/modules/video_coding/video_codec_initializer.cc
+++ b/modules/video_coding/video_codec_initializer.cc
@@ -181,7 +181,10 @@
             video_codec.VP9()->numberOfTemporalLayers,
             video_codec.mode == VideoCodecMode::kScreensharing);
 
-        const bool no_spatial_layering = (spatial_layers.size() == 1);
+        // If there was no request for spatial layering, don't limit bitrate
+        // of single spatial layer.
+        const bool no_spatial_layering =
+            video_codec.VP9()->numberOfSpatialLayers <= 1;
         if (no_spatial_layering) {
           // Use codec's bitrate limits.
           spatial_layers.back().minBitrate = video_codec.minBitrate;
diff --git a/modules/video_coding/video_codec_initializer_unittest.cc b/modules/video_coding/video_codec_initializer_unittest.cc
index ceff1eb..9249cad 100644
--- a/modules/video_coding/video_codec_initializer_unittest.cc
+++ b/modules/video_coding/video_codec_initializer_unittest.cc
@@ -286,4 +286,21 @@
             kDefaultMaxBitrateBps / 1000);
 }
 
+TEST_F(VideoCodecInitializerTest,
+       Vp9KeepBitrateLimitsIfNumberOfSpatialLayersIsReducedToOne) {
+  // Request 3 spatial layers for 320x180 input. Actual number of layers will be
+  // reduced to 1 due to low input resolution but SVC bitrate limits should be
+  // applied.
+  SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false);
+  VideoStream stream = DefaultStream();
+  stream.width = 320;
+  stream.height = 180;
+  stream.num_temporal_layers = 3;
+  streams_.push_back(stream);
+
+  EXPECT_TRUE(InitializeCodec());
+  EXPECT_LT(codec_out_.spatialLayers[0].maxBitrate,
+            kDefaultMaxBitrateBps / 1000);
+}
+
 }  // namespace webrtc