sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/include/video_codec_initializer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
| 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
| 18 | #include "absl/types/optional.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Elad Alon | 45befc5 | 2019-07-02 11:20:09 +0200 | [diff] [blame] | 20 | #include "api/test/mock_fec_controller_override.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 21 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "api/video/video_bitrate_allocation.h" |
| 23 | #include "api/video/video_bitrate_allocator.h" |
| 24 | #include "api/video/video_bitrate_allocator_factory.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 25 | #include "api/video_codecs/video_encoder.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 26 | #include "api/video_codecs/vp8_temporal_layers.h" |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 27 | #include "api/video_codecs/vp8_temporal_layers_factory.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 28 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 29 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "rtc_base/ref_counted_object.h" |
Elad Alon | 45befc5 | 2019-07-02 11:20:09 +0200 | [diff] [blame] | 31 | #include "test/gmock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "test/gtest.h" |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | namespace { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 37 | static const int kDefaultWidth = 1280; |
| 38 | static const int kDefaultHeight = 720; |
| 39 | static const int kDefaultFrameRate = 30; |
| 40 | static const uint32_t kDefaultMinBitrateBps = 60000; |
| 41 | static const uint32_t kDefaultTargetBitrateBps = 2000000; |
| 42 | static const uint32_t kDefaultMaxBitrateBps = 2000000; |
| 43 | static const uint32_t kDefaultMinTransmitBitrateBps = 400000; |
| 44 | static const int kDefaultMaxQp = 48; |
Florent Castelli | d351101 | 2020-08-04 11:40:23 +0200 | [diff] [blame] | 45 | static const uint32_t kScreenshareTl0BitrateBps = 120000; |
| 46 | static const uint32_t kScreenshareConferenceTl0BitrateBps = 200000; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 47 | static const uint32_t kScreenshareCodecTargetBitrateBps = 200000; |
| 48 | static const uint32_t kScreenshareDefaultFramerate = 5; |
| 49 | // Bitrates for the temporal layers of the higher screenshare simulcast stream. |
| 50 | static const uint32_t kHighScreenshareTl0Bps = 800000; |
| 51 | static const uint32_t kHighScreenshareTl1Bps = 1200000; |
| 52 | } // namespace |
| 53 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 54 | // TODO(sprang): Extend coverage to handle the rest of the codec initializer. |
| 55 | class VideoCodecInitializerTest : public ::testing::Test { |
| 56 | public: |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 57 | VideoCodecInitializerTest() {} |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 58 | virtual ~VideoCodecInitializerTest() {} |
| 59 | |
| 60 | protected: |
| 61 | void SetUpFor(VideoCodecType type, |
| 62 | int num_spatial_streams, |
| 63 | int num_temporal_streams, |
| 64 | bool screenshare) { |
| 65 | config_ = VideoEncoderConfig(); |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 66 | config_.codec_type = type; |
| 67 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 68 | if (screenshare) { |
| 69 | config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps; |
| 70 | config_.content_type = VideoEncoderConfig::ContentType::kScreen; |
| 71 | } |
| 72 | |
| 73 | if (type == VideoCodecType::kVideoCodecVP8) { |
| 74 | config_.number_of_streams = num_spatial_streams; |
| 75 | VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings(); |
| 76 | vp8_settings.numberOfTemporalLayers = num_temporal_streams; |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 77 | config_.encoder_specific_settings = rtc::make_ref_counted< |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 78 | webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 79 | } else if (type == VideoCodecType::kVideoCodecVP9) { |
| 80 | VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings(); |
| 81 | vp9_settings.numberOfSpatialLayers = num_spatial_streams; |
| 82 | vp9_settings.numberOfTemporalLayers = num_temporal_streams; |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 83 | config_.encoder_specific_settings = rtc::make_ref_counted< |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 84 | webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
| 85 | } else if (type != VideoCodecType::kVideoCodecMultiplex) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 86 | ADD_FAILURE() << "Unexpected codec type: " << type; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | bool InitializeCodec() { |
| 91 | codec_out_ = VideoCodec(); |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 92 | frame_buffer_controller_.reset(); |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 93 | if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_)) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 94 | return false; |
| 95 | } |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 96 | bitrate_allocator_ = CreateBuiltinVideoBitrateAllocatorFactory() |
| 97 | ->CreateVideoBitrateAllocator(codec_out_); |
| 98 | RTC_CHECK(bitrate_allocator_); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 99 | if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex) |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 100 | return true; |
Erik Språng | 82fad3d | 2018-03-21 09:57:23 +0100 | [diff] [blame] | 101 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 102 | // Make sure temporal layers instances have been created. |
| 103 | if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) { |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 104 | Vp8TemporalLayersFactory factory; |
Elad Alon | a279584 | 2019-06-07 23:10:00 +0200 | [diff] [blame] | 105 | const VideoEncoder::Settings settings(VideoEncoder::Capabilities(false), |
| 106 | 1, 1000); |
Elad Alon | 45befc5 | 2019-07-02 11:20:09 +0200 | [diff] [blame] | 107 | frame_buffer_controller_ = |
| 108 | factory.Create(codec_out_, settings, &fec_controller_override_); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | VideoStream DefaultStream() { |
| 114 | VideoStream stream; |
| 115 | stream.width = kDefaultWidth; |
| 116 | stream.height = kDefaultHeight; |
| 117 | stream.max_framerate = kDefaultFrameRate; |
| 118 | stream.min_bitrate_bps = kDefaultMinBitrateBps; |
| 119 | stream.target_bitrate_bps = kDefaultTargetBitrateBps; |
| 120 | stream.max_bitrate_bps = kDefaultMaxBitrateBps; |
| 121 | stream.max_qp = kDefaultMaxQp; |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 122 | stream.num_temporal_layers = 1; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 123 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 124 | return stream; |
| 125 | } |
| 126 | |
| 127 | VideoStream DefaultScreenshareStream() { |
| 128 | VideoStream stream = DefaultStream(); |
| 129 | stream.min_bitrate_bps = 30000; |
Florent Castelli | d351101 | 2020-08-04 11:40:23 +0200 | [diff] [blame] | 130 | stream.target_bitrate_bps = kScreenshareCodecTargetBitrateBps; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 131 | stream.max_bitrate_bps = 1000000; |
| 132 | stream.max_framerate = kScreenshareDefaultFramerate; |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 133 | stream.num_temporal_layers = 2; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 134 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 135 | return stream; |
| 136 | } |
| 137 | |
Elad Alon | 45befc5 | 2019-07-02 11:20:09 +0200 | [diff] [blame] | 138 | MockFecControllerOverride fec_controller_override_; |
| 139 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 140 | // Input settings. |
| 141 | VideoEncoderConfig config_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 142 | std::vector<VideoStream> streams_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 143 | |
| 144 | // Output. |
| 145 | VideoCodec codec_out_; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 146 | std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_; |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 147 | std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) { |
| 151 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 152 | streams_.push_back(DefaultStream()); |
| 153 | EXPECT_TRUE(InitializeCodec()); |
| 154 | |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 155 | VideoBitrateAllocation bitrate_allocation = |
| 156 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 157 | kDefaultTargetBitrateBps, kDefaultFrameRate)); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 158 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 159 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 160 | EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps()); |
| 161 | } |
| 162 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 163 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) { |
| 164 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 165 | VideoStream inactive_stream = DefaultStream(); |
| 166 | inactive_stream.active = false; |
| 167 | streams_.push_back(inactive_stream); |
| 168 | EXPECT_TRUE(InitializeCodec()); |
| 169 | |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 170 | VideoBitrateAllocation bitrate_allocation = |
| 171 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 172 | kDefaultTargetBitrateBps, kDefaultFrameRate)); |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 173 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 174 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 175 | EXPECT_EQ(0U, bitrate_allocation.get_sum_bps()); |
| 176 | } |
| 177 | |
Florent Castelli | d351101 | 2020-08-04 11:40:23 +0200 | [diff] [blame] | 178 | TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8ScreenshareConference) { |
| 179 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true); |
| 180 | streams_.push_back(DefaultScreenshareStream()); |
| 181 | EXPECT_TRUE(InitializeCodec()); |
| 182 | bitrate_allocator_->SetLegacyConferenceMode(true); |
| 183 | |
| 184 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 185 | EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers); |
| 186 | VideoBitrateAllocation bitrate_allocation = |
| 187 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 188 | kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate)); |
| 189 | EXPECT_EQ(kScreenshareCodecTargetBitrateBps, |
| 190 | bitrate_allocation.get_sum_bps()); |
| 191 | EXPECT_EQ(kScreenshareConferenceTl0BitrateBps, |
| 192 | bitrate_allocation.GetBitrate(0, 0)); |
| 193 | } |
| 194 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 195 | TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) { |
| 196 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true); |
| 197 | streams_.push_back(DefaultScreenshareStream()); |
| 198 | EXPECT_TRUE(InitializeCodec()); |
| 199 | |
| 200 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 201 | EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers); |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 202 | VideoBitrateAllocation bitrate_allocation = |
| 203 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 204 | kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate)); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 205 | EXPECT_EQ(kScreenshareCodecTargetBitrateBps, |
| 206 | bitrate_allocation.get_sum_bps()); |
| 207 | EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0)); |
| 208 | } |
| 209 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 210 | TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 211 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 212 | streams_.push_back(DefaultScreenshareStream()); |
| 213 | VideoStream video_stream = DefaultStream(); |
| 214 | video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 215 | streams_.push_back(video_stream); |
| 216 | EXPECT_TRUE(InitializeCodec()); |
| 217 | |
| 218 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 219 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 220 | const uint32_t max_bitrate_bps = |
| 221 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 222 | VideoBitrateAllocation bitrate_allocation = |
| 223 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 224 | max_bitrate_bps, kScreenshareDefaultFramerate)); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 225 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 226 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 227 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 228 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 229 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 230 | } |
| 231 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 232 | // Tests that when a video stream is inactive, then the bitrate allocation will |
| 233 | // be 0 for that stream. |
| 234 | TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) { |
| 235 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 236 | streams_.push_back(DefaultScreenshareStream()); |
| 237 | VideoStream inactive_video_stream = DefaultStream(); |
| 238 | inactive_video_stream.active = false; |
| 239 | inactive_video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 240 | streams_.push_back(inactive_video_stream); |
| 241 | EXPECT_TRUE(InitializeCodec()); |
| 242 | |
| 243 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 244 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 245 | const uint32_t target_bitrate = |
| 246 | streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps; |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 247 | VideoBitrateAllocation bitrate_allocation = |
| 248 | bitrate_allocator_->Allocate(VideoBitrateAllocationParameters( |
| 249 | target_bitrate, kScreenshareDefaultFramerate)); |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 250 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 251 | bitrate_allocation.get_sum_bps()); |
| 252 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 253 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 254 | EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1)); |
| 255 | } |
| 256 | |
| 257 | TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 258 | // Two simulcast streams, the lower one using legacy settings (two temporal |
| 259 | // streams, 5fps), the higher one using 3 temporal streams and 30fps. |
| 260 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true); |
| 261 | streams_.push_back(DefaultScreenshareStream()); |
| 262 | VideoStream video_stream = DefaultStream(); |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 263 | video_stream.num_temporal_layers = 3; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 264 | streams_.push_back(video_stream); |
| 265 | EXPECT_TRUE(InitializeCodec()); |
| 266 | |
| 267 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 268 | EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers); |
| 269 | const uint32_t max_bitrate_bps = |
| 270 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
Florent Castelli | 8bbdb5b | 2019-08-02 15:16:28 +0200 | [diff] [blame] | 271 | VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->Allocate( |
| 272 | VideoBitrateAllocationParameters(max_bitrate_bps, kDefaultFrameRate)); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 273 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 274 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 275 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 276 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 277 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 278 | EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0)); |
| 279 | EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps, |
| 280 | bitrate_allocation.GetBitrate(1, 1)); |
| 281 | } |
| 282 | |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 283 | TEST_F(VideoCodecInitializerTest, SingleStreamMultiplexCodec) { |
| 284 | SetUpFor(VideoCodecType::kVideoCodecMultiplex, 1, 1, true); |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 285 | streams_.push_back(DefaultStream()); |
| 286 | EXPECT_TRUE(InitializeCodec()); |
| 287 | } |
| 288 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 289 | TEST_F(VideoCodecInitializerTest, Vp9SvcDefaultLayering) { |
| 290 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false); |
| 291 | VideoStream stream = DefaultStream(); |
| 292 | stream.num_temporal_layers = 3; |
| 293 | streams_.push_back(stream); |
| 294 | |
| 295 | EXPECT_TRUE(InitializeCodec()); |
| 296 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3u); |
| 297 | EXPECT_EQ(codec_out_.VP9()->numberOfTemporalLayers, 3u); |
| 298 | } |
| 299 | |
| 300 | TEST_F(VideoCodecInitializerTest, Vp9SvcAdjustedLayering) { |
| 301 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false); |
| 302 | VideoStream stream = DefaultStream(); |
| 303 | stream.num_temporal_layers = 3; |
| 304 | // Set resolution which is only enough to produce 2 spatial layers. |
Konrad Hofbauer | d2cd872 | 2021-12-09 09:43:56 +0100 | [diff] [blame] | 305 | stream.width = kMinVp9SpatialLayerLongSideLength * 2; |
| 306 | stream.height = kMinVp9SpatialLayerShortSideLength * 2; |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 307 | |
| 308 | streams_.push_back(stream); |
| 309 | |
| 310 | EXPECT_TRUE(InitializeCodec()); |
| 311 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2u); |
| 312 | } |
| 313 | |
Sergey Silkin | fafeac3 | 2018-04-13 16:36:39 +0200 | [diff] [blame] | 314 | TEST_F(VideoCodecInitializerTest, |
| 315 | Vp9SingleSpatialLayerMaxBitrateIsEqualToCodecMaxBitrate) { |
| 316 | SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 3, false); |
| 317 | VideoStream stream = DefaultStream(); |
| 318 | stream.num_temporal_layers = 3; |
| 319 | streams_.push_back(stream); |
| 320 | |
| 321 | EXPECT_TRUE(InitializeCodec()); |
| 322 | EXPECT_EQ(codec_out_.spatialLayers[0].maxBitrate, |
| 323 | kDefaultMaxBitrateBps / 1000); |
| 324 | } |
| 325 | |
Sergey Silkin | 3312092 | 2018-11-28 13:32:13 +0100 | [diff] [blame] | 326 | TEST_F(VideoCodecInitializerTest, |
Ilya Nikolaevskiy | 9ef5e05 | 2019-03-05 10:08:35 +0100 | [diff] [blame] | 327 | Vp9SingleSpatialLayerTargetBitrateIsEqualToCodecMaxBitrate) { |
| 328 | SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 1, true); |
| 329 | VideoStream stream = DefaultStream(); |
| 330 | stream.num_temporal_layers = 1; |
| 331 | streams_.push_back(stream); |
| 332 | |
| 333 | EXPECT_TRUE(InitializeCodec()); |
| 334 | EXPECT_EQ(codec_out_.spatialLayers[0].targetBitrate, |
| 335 | kDefaultMaxBitrateBps / 1000); |
| 336 | } |
| 337 | |
| 338 | TEST_F(VideoCodecInitializerTest, |
Sergey Silkin | 3312092 | 2018-11-28 13:32:13 +0100 | [diff] [blame] | 339 | Vp9KeepBitrateLimitsIfNumberOfSpatialLayersIsReducedToOne) { |
| 340 | // Request 3 spatial layers for 320x180 input. Actual number of layers will be |
| 341 | // reduced to 1 due to low input resolution but SVC bitrate limits should be |
| 342 | // applied. |
| 343 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false); |
| 344 | VideoStream stream = DefaultStream(); |
| 345 | stream.width = 320; |
| 346 | stream.height = 180; |
| 347 | stream.num_temporal_layers = 3; |
| 348 | streams_.push_back(stream); |
| 349 | |
| 350 | EXPECT_TRUE(InitializeCodec()); |
| 351 | EXPECT_LT(codec_out_.spatialLayers[0].maxBitrate, |
| 352 | kDefaultMaxBitrateBps / 1000); |
| 353 | } |
| 354 | |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 355 | TEST_F(VideoCodecInitializerTest, Vp9DeactivateLayers) { |
| 356 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 1, false); |
| 357 | VideoStream stream = DefaultStream(); |
| 358 | streams_.push_back(stream); |
| 359 | |
| 360 | config_.simulcast_layers.resize(3); |
| 361 | |
| 362 | // Activate all layers. |
| 363 | config_.simulcast_layers[0].active = true; |
| 364 | config_.simulcast_layers[1].active = true; |
| 365 | config_.simulcast_layers[2].active = true; |
| 366 | EXPECT_TRUE(InitializeCodec()); |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 367 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3); |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 368 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 369 | EXPECT_TRUE(codec_out_.spatialLayers[1].active); |
| 370 | EXPECT_TRUE(codec_out_.spatialLayers[2].active); |
| 371 | |
| 372 | // Deactivate top layer. |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 373 | config_.simulcast_layers[0].active = true; |
| 374 | config_.simulcast_layers[1].active = true; |
Ilya Nikolaevskiy | 72859e5 | 2020-02-05 17:31:00 +0100 | [diff] [blame] | 375 | config_.simulcast_layers[2].active = false; |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 376 | EXPECT_TRUE(InitializeCodec()); |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 377 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3); |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 378 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 379 | EXPECT_TRUE(codec_out_.spatialLayers[1].active); |
| 380 | EXPECT_FALSE(codec_out_.spatialLayers[2].active); |
| 381 | |
| 382 | // Deactivate middle layer. |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 383 | config_.simulcast_layers[0].active = true; |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 384 | config_.simulcast_layers[1].active = false; |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 385 | config_.simulcast_layers[2].active = true; |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 386 | EXPECT_TRUE(InitializeCodec()); |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 387 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3); |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 388 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 389 | EXPECT_FALSE(codec_out_.spatialLayers[1].active); |
| 390 | EXPECT_TRUE(codec_out_.spatialLayers[2].active); |
Ilya Nikolaevskiy | a945cda | 2020-09-22 15:55:23 +0200 | [diff] [blame] | 391 | |
| 392 | // Deactivate first layer. |
| 393 | config_.simulcast_layers[0].active = false; |
| 394 | config_.simulcast_layers[1].active = true; |
| 395 | config_.simulcast_layers[2].active = true; |
| 396 | EXPECT_TRUE(InitializeCodec()); |
| 397 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2); |
| 398 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 399 | EXPECT_TRUE(codec_out_.spatialLayers[1].active); |
| 400 | |
| 401 | // HD singlecast. |
| 402 | config_.simulcast_layers[0].active = false; |
| 403 | config_.simulcast_layers[1].active = false; |
| 404 | config_.simulcast_layers[2].active = true; |
| 405 | EXPECT_TRUE(InitializeCodec()); |
| 406 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 1); |
| 407 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 408 | |
| 409 | // VGA singlecast. |
| 410 | config_.simulcast_layers[0].active = false; |
| 411 | config_.simulcast_layers[1].active = true; |
| 412 | config_.simulcast_layers[2].active = false; |
| 413 | EXPECT_TRUE(InitializeCodec()); |
| 414 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2); |
| 415 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 416 | EXPECT_FALSE(codec_out_.spatialLayers[1].active); |
| 417 | |
| 418 | // QVGA singlecast. |
| 419 | config_.simulcast_layers[0].active = true; |
| 420 | config_.simulcast_layers[1].active = false; |
| 421 | config_.simulcast_layers[2].active = false; |
| 422 | EXPECT_TRUE(InitializeCodec()); |
| 423 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3); |
| 424 | EXPECT_TRUE(codec_out_.spatialLayers[0].active); |
| 425 | EXPECT_FALSE(codec_out_.spatialLayers[1].active); |
| 426 | EXPECT_FALSE(codec_out_.spatialLayers[2].active); |
Sergey Silkin | 8b9b5f9 | 2018-12-10 09:28:53 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Danil Chapovalov | c27c047 | 2021-05-04 13:06:29 +0200 | [diff] [blame] | 429 | TEST_F(VideoCodecInitializerTest, Av1SingleSpatialLayerBitratesAreConsistent) { |
| 430 | VideoEncoderConfig config; |
| 431 | config.codec_type = VideoCodecType::kVideoCodecAV1; |
| 432 | std::vector<VideoStream> streams = {DefaultStream()}; |
| 433 | streams[0].scalability_mode = "L1T2"; |
| 434 | |
| 435 | VideoCodec codec; |
| 436 | EXPECT_TRUE(VideoCodecInitializer::SetupCodec(config, streams, &codec)); |
| 437 | |
| 438 | EXPECT_GE(codec.spatialLayers[0].targetBitrate, |
| 439 | codec.spatialLayers[0].minBitrate); |
| 440 | EXPECT_LE(codec.spatialLayers[0].targetBitrate, |
| 441 | codec.spatialLayers[0].maxBitrate); |
| 442 | } |
| 443 | |
| 444 | TEST_F(VideoCodecInitializerTest, Av1TwoSpatialLayersBitratesAreConsistent) { |
| 445 | VideoEncoderConfig config; |
| 446 | config.codec_type = VideoCodecType::kVideoCodecAV1; |
| 447 | std::vector<VideoStream> streams = {DefaultStream()}; |
| 448 | streams[0].scalability_mode = "L2T2"; |
| 449 | |
| 450 | VideoCodec codec; |
| 451 | EXPECT_TRUE(VideoCodecInitializer::SetupCodec(config, streams, &codec)); |
| 452 | |
| 453 | EXPECT_GE(codec.spatialLayers[0].targetBitrate, |
| 454 | codec.spatialLayers[0].minBitrate); |
| 455 | EXPECT_LE(codec.spatialLayers[0].targetBitrate, |
| 456 | codec.spatialLayers[0].maxBitrate); |
| 457 | |
| 458 | EXPECT_GE(codec.spatialLayers[1].targetBitrate, |
| 459 | codec.spatialLayers[1].minBitrate); |
| 460 | EXPECT_LE(codec.spatialLayers[1].targetBitrate, |
| 461 | codec.spatialLayers[1].maxBitrate); |
| 462 | } |
| 463 | |
Erik Språng | f865444 | 2021-06-01 16:52:24 +0200 | [diff] [blame] | 464 | TEST_F(VideoCodecInitializerTest, Av1TwoSpatialLayersActiveByDefault) { |
| 465 | VideoEncoderConfig config; |
| 466 | config.codec_type = VideoCodecType::kVideoCodecAV1; |
| 467 | std::vector<VideoStream> streams = {DefaultStream()}; |
| 468 | streams[0].scalability_mode = "L2T2"; |
| 469 | config.spatial_layers = {}; |
| 470 | |
| 471 | VideoCodec codec; |
| 472 | EXPECT_TRUE(VideoCodecInitializer::SetupCodec(config, streams, &codec)); |
| 473 | |
| 474 | EXPECT_TRUE(codec.spatialLayers[0].active); |
| 475 | EXPECT_TRUE(codec.spatialLayers[1].active); |
| 476 | } |
| 477 | |
| 478 | TEST_F(VideoCodecInitializerTest, Av1TwoSpatialLayersOneDeactivated) { |
| 479 | VideoEncoderConfig config; |
| 480 | config.codec_type = VideoCodecType::kVideoCodecAV1; |
| 481 | std::vector<VideoStream> streams = {DefaultStream()}; |
| 482 | streams[0].scalability_mode = "L2T2"; |
| 483 | config.spatial_layers.resize(2); |
| 484 | config.spatial_layers[0].active = true; |
| 485 | config.spatial_layers[1].active = false; |
| 486 | |
| 487 | VideoCodec codec; |
| 488 | EXPECT_TRUE(VideoCodecInitializer::SetupCodec(config, streams, &codec)); |
| 489 | |
| 490 | EXPECT_TRUE(codec.spatialLayers[0].active); |
| 491 | EXPECT_FALSE(codec.spatialLayers[1].active); |
| 492 | } |
| 493 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 494 | } // namespace webrtc |