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" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 12 | #include "api/video/video_bitrate_allocator.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame^] | 13 | #include "api/video_codecs/create_vp8_temporal_layers.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 14 | #include "api/video_codecs/video_encoder.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame^] | 15 | #include "api/video_codecs/vp8_temporal_layers.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 16 | #include "common_types.h" // NOLINT(build/include) |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 17 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 18 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gtest.h" |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 24 | static const int kDefaultWidth = 1280; |
| 25 | static const int kDefaultHeight = 720; |
| 26 | static const int kDefaultFrameRate = 30; |
| 27 | static const uint32_t kDefaultMinBitrateBps = 60000; |
| 28 | static const uint32_t kDefaultTargetBitrateBps = 2000000; |
| 29 | static const uint32_t kDefaultMaxBitrateBps = 2000000; |
| 30 | static const uint32_t kDefaultMinTransmitBitrateBps = 400000; |
| 31 | static const int kDefaultMaxQp = 48; |
Erik Språng | 5e898d6 | 2018-07-06 16:32:20 +0200 | [diff] [blame] | 32 | static const uint32_t kScreenshareTl0BitrateBps = 200000; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 33 | static const uint32_t kScreenshareCodecTargetBitrateBps = 200000; |
| 34 | static const uint32_t kScreenshareDefaultFramerate = 5; |
| 35 | // Bitrates for the temporal layers of the higher screenshare simulcast stream. |
| 36 | static const uint32_t kHighScreenshareTl0Bps = 800000; |
| 37 | static const uint32_t kHighScreenshareTl1Bps = 1200000; |
| 38 | } // namespace |
| 39 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 40 | // TODO(sprang): Extend coverage to handle the rest of the codec initializer. |
| 41 | class VideoCodecInitializerTest : public ::testing::Test { |
| 42 | public: |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 43 | VideoCodecInitializerTest() {} |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 44 | virtual ~VideoCodecInitializerTest() {} |
| 45 | |
| 46 | protected: |
| 47 | void SetUpFor(VideoCodecType type, |
| 48 | int num_spatial_streams, |
| 49 | int num_temporal_streams, |
| 50 | bool screenshare) { |
| 51 | config_ = VideoEncoderConfig(); |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 52 | config_.codec_type = type; |
| 53 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 54 | if (screenshare) { |
| 55 | config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps; |
| 56 | config_.content_type = VideoEncoderConfig::ContentType::kScreen; |
| 57 | } |
| 58 | |
| 59 | if (type == VideoCodecType::kVideoCodecVP8) { |
| 60 | config_.number_of_streams = num_spatial_streams; |
| 61 | VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings(); |
| 62 | vp8_settings.numberOfTemporalLayers = num_temporal_streams; |
| 63 | config_.encoder_specific_settings = new rtc::RefCountedObject< |
| 64 | webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 65 | } else if (type == VideoCodecType::kVideoCodecVP9) { |
| 66 | VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings(); |
| 67 | vp9_settings.numberOfSpatialLayers = num_spatial_streams; |
| 68 | vp9_settings.numberOfTemporalLayers = num_temporal_streams; |
| 69 | config_.encoder_specific_settings = new rtc::RefCountedObject< |
| 70 | webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
| 71 | } else if (type != VideoCodecType::kVideoCodecMultiplex) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 72 | ADD_FAILURE() << "Unexpected codec type: " << type; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool InitializeCodec() { |
| 77 | codec_out_ = VideoCodec(); |
| 78 | bitrate_allocator_out_.reset(); |
| 79 | temporal_layers_.clear(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_, |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 81 | &bitrate_allocator_out_)) { |
| 82 | return false; |
| 83 | } |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 84 | if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex) |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 85 | return true; |
Erik Språng | 82fad3d | 2018-03-21 09:57:23 +0100 | [diff] [blame] | 86 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 87 | // Make sure temporal layers instances have been created. |
| 88 | if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 89 | for (int i = 0; i < codec_out_.numberOfSimulcastStreams; ++i) { |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame^] | 90 | temporal_layers_.emplace_back( |
| 91 | CreateVp8TemporalLayers(Vp8TemporalLayersType::kFixedPattern, |
| 92 | codec_out_.VP8()->numberOfTemporalLayers)); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | VideoStream DefaultStream() { |
| 99 | VideoStream stream; |
| 100 | stream.width = kDefaultWidth; |
| 101 | stream.height = kDefaultHeight; |
| 102 | stream.max_framerate = kDefaultFrameRate; |
| 103 | stream.min_bitrate_bps = kDefaultMinBitrateBps; |
| 104 | stream.target_bitrate_bps = kDefaultTargetBitrateBps; |
| 105 | stream.max_bitrate_bps = kDefaultMaxBitrateBps; |
| 106 | stream.max_qp = kDefaultMaxQp; |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 107 | stream.num_temporal_layers = 1; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 108 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 109 | return stream; |
| 110 | } |
| 111 | |
| 112 | VideoStream DefaultScreenshareStream() { |
| 113 | VideoStream stream = DefaultStream(); |
| 114 | stream.min_bitrate_bps = 30000; |
| 115 | stream.target_bitrate_bps = kScreenshareTl0BitrateBps; |
| 116 | stream.max_bitrate_bps = 1000000; |
| 117 | stream.max_framerate = kScreenshareDefaultFramerate; |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 118 | stream.num_temporal_layers = 2; |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 119 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 120 | return stream; |
| 121 | } |
| 122 | |
| 123 | // Input settings. |
| 124 | VideoEncoderConfig config_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 125 | std::vector<VideoStream> streams_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 126 | |
| 127 | // Output. |
| 128 | VideoCodec codec_out_; |
| 129 | std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_out_; |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame^] | 130 | std::vector<std::unique_ptr<Vp8TemporalLayers>> temporal_layers_; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) { |
| 134 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 135 | streams_.push_back(DefaultStream()); |
| 136 | EXPECT_TRUE(InitializeCodec()); |
| 137 | |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 138 | VideoBitrateAllocation bitrate_allocation = |
| 139 | bitrate_allocator_out_->GetAllocation(kDefaultTargetBitrateBps, |
| 140 | kDefaultFrameRate); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 141 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 142 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 143 | EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps()); |
| 144 | } |
| 145 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 146 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) { |
| 147 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 148 | VideoStream inactive_stream = DefaultStream(); |
| 149 | inactive_stream.active = false; |
| 150 | streams_.push_back(inactive_stream); |
| 151 | EXPECT_TRUE(InitializeCodec()); |
| 152 | |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 153 | VideoBitrateAllocation bitrate_allocation = |
| 154 | bitrate_allocator_out_->GetAllocation(kDefaultTargetBitrateBps, |
| 155 | kDefaultFrameRate); |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 156 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 157 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 158 | EXPECT_EQ(0U, bitrate_allocation.get_sum_bps()); |
| 159 | } |
| 160 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 161 | TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) { |
| 162 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true); |
| 163 | streams_.push_back(DefaultScreenshareStream()); |
| 164 | EXPECT_TRUE(InitializeCodec()); |
| 165 | |
| 166 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 167 | EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers); |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 168 | VideoBitrateAllocation bitrate_allocation = |
| 169 | bitrate_allocator_out_->GetAllocation(kScreenshareCodecTargetBitrateBps, |
| 170 | kScreenshareDefaultFramerate); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 171 | EXPECT_EQ(kScreenshareCodecTargetBitrateBps, |
| 172 | bitrate_allocation.get_sum_bps()); |
| 173 | EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0)); |
| 174 | } |
| 175 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 176 | TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 177 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 178 | streams_.push_back(DefaultScreenshareStream()); |
| 179 | VideoStream video_stream = DefaultStream(); |
| 180 | video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 181 | streams_.push_back(video_stream); |
| 182 | EXPECT_TRUE(InitializeCodec()); |
| 183 | |
| 184 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 185 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 186 | const uint32_t max_bitrate_bps = |
| 187 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 188 | VideoBitrateAllocation bitrate_allocation = |
| 189 | bitrate_allocator_out_->GetAllocation(max_bitrate_bps, |
| 190 | kScreenshareDefaultFramerate); |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 191 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 192 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 193 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 194 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 195 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 196 | } |
| 197 | |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 198 | // Tests that when a video stream is inactive, then the bitrate allocation will |
| 199 | // be 0 for that stream. |
| 200 | TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) { |
| 201 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 202 | streams_.push_back(DefaultScreenshareStream()); |
| 203 | VideoStream inactive_video_stream = DefaultStream(); |
| 204 | inactive_video_stream.active = false; |
| 205 | inactive_video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 206 | streams_.push_back(inactive_video_stream); |
| 207 | EXPECT_TRUE(InitializeCodec()); |
| 208 | |
| 209 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 210 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 211 | const uint32_t target_bitrate = |
| 212 | streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps; |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 213 | VideoBitrateAllocation bitrate_allocation = |
| 214 | bitrate_allocator_out_->GetAllocation(target_bitrate, |
| 215 | kScreenshareDefaultFramerate); |
Seth Hampson | 46e31ba | 2018-01-18 10:39:54 -0800 | [diff] [blame] | 216 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 217 | bitrate_allocation.get_sum_bps()); |
| 218 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 219 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 220 | EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1)); |
| 221 | } |
| 222 | |
| 223 | TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 224 | // Two simulcast streams, the lower one using legacy settings (two temporal |
| 225 | // streams, 5fps), the higher one using 3 temporal streams and 30fps. |
| 226 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true); |
| 227 | streams_.push_back(DefaultScreenshareStream()); |
| 228 | VideoStream video_stream = DefaultStream(); |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 229 | video_stream.num_temporal_layers = 3; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 230 | streams_.push_back(video_stream); |
| 231 | EXPECT_TRUE(InitializeCodec()); |
| 232 | |
| 233 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 234 | EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers); |
| 235 | const uint32_t max_bitrate_bps = |
| 236 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 237 | VideoBitrateAllocation bitrate_allocation = |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 238 | bitrate_allocator_out_->GetAllocation(max_bitrate_bps, kDefaultFrameRate); |
| 239 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 240 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 241 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 242 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 243 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 244 | EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0)); |
| 245 | EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps, |
| 246 | bitrate_allocation.GetBitrate(1, 1)); |
| 247 | } |
| 248 | |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 249 | TEST_F(VideoCodecInitializerTest, SingleStreamMultiplexCodec) { |
| 250 | SetUpFor(VideoCodecType::kVideoCodecMultiplex, 1, 1, true); |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 251 | streams_.push_back(DefaultStream()); |
| 252 | EXPECT_TRUE(InitializeCodec()); |
| 253 | } |
| 254 | |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 255 | TEST_F(VideoCodecInitializerTest, Vp9SvcDefaultLayering) { |
| 256 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false); |
| 257 | VideoStream stream = DefaultStream(); |
| 258 | stream.num_temporal_layers = 3; |
| 259 | streams_.push_back(stream); |
| 260 | |
| 261 | EXPECT_TRUE(InitializeCodec()); |
| 262 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 3u); |
| 263 | EXPECT_EQ(codec_out_.VP9()->numberOfTemporalLayers, 3u); |
| 264 | } |
| 265 | |
| 266 | TEST_F(VideoCodecInitializerTest, Vp9SvcAdjustedLayering) { |
| 267 | SetUpFor(VideoCodecType::kVideoCodecVP9, 3, 3, false); |
| 268 | VideoStream stream = DefaultStream(); |
| 269 | stream.num_temporal_layers = 3; |
| 270 | // Set resolution which is only enough to produce 2 spatial layers. |
| 271 | stream.width = kMinVp9SpatialLayerWidth * 2; |
| 272 | stream.height = kMinVp9SpatialLayerHeight * 2; |
| 273 | |
| 274 | streams_.push_back(stream); |
| 275 | |
| 276 | EXPECT_TRUE(InitializeCodec()); |
| 277 | EXPECT_EQ(codec_out_.VP9()->numberOfSpatialLayers, 2u); |
| 278 | } |
| 279 | |
Sergey Silkin | fafeac3 | 2018-04-13 16:36:39 +0200 | [diff] [blame] | 280 | TEST_F(VideoCodecInitializerTest, |
| 281 | Vp9SingleSpatialLayerMaxBitrateIsEqualToCodecMaxBitrate) { |
| 282 | SetUpFor(VideoCodecType::kVideoCodecVP9, 1, 3, false); |
| 283 | VideoStream stream = DefaultStream(); |
| 284 | stream.num_temporal_layers = 3; |
| 285 | streams_.push_back(stream); |
| 286 | |
| 287 | EXPECT_TRUE(InitializeCodec()); |
| 288 | EXPECT_EQ(codec_out_.spatialLayers[0].maxBitrate, |
| 289 | kDefaultMaxBitrateBps / 1000); |
| 290 | } |
| 291 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 292 | } // namespace webrtc |