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