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