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 { |
| 22 | static const char* kVp8PayloadName = "VP8"; |
| 23 | static const int kVp8PayloadType = 100; |
| 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; |
| 32 | static const uint32_t kScreenshareTl0BitrateBps = 100000; |
| 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 | |
| 40 | /* |
| 41 | * static bool SetupCodec( |
| 42 | const VideoEncoderConfig& config, |
| 43 | const VideoSendStream::Config::EncoderSettings settings, |
| 44 | const std::vector<VideoStream>& streams, |
| 45 | bool nack_enabled, |
| 46 | VideoCodec* codec, |
| 47 | std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator); |
| 48 | |
| 49 | // Create a bitrate allocator for the specified codec. |tl_factory| is |
| 50 | // optional, if it is populated, ownership of that instance will be |
| 51 | // transferred to the VideoBitrateAllocator instance. |
| 52 | static std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator( |
| 53 | const VideoCodec& codec, |
| 54 | std::unique_ptr<TemporalLayersFactory> tl_factory); |
| 55 | */ |
| 56 | |
| 57 | // TODO(sprang): Extend coverage to handle the rest of the codec initializer. |
| 58 | class VideoCodecInitializerTest : public ::testing::Test { |
| 59 | public: |
| 60 | VideoCodecInitializerTest() : nack_enabled_(false) {} |
| 61 | virtual ~VideoCodecInitializerTest() {} |
| 62 | |
| 63 | protected: |
| 64 | void SetUpFor(VideoCodecType type, |
| 65 | int num_spatial_streams, |
| 66 | int num_temporal_streams, |
| 67 | bool screenshare) { |
| 68 | config_ = VideoEncoderConfig(); |
| 69 | if (screenshare) { |
| 70 | config_.min_transmit_bitrate_bps = kDefaultMinTransmitBitrateBps; |
| 71 | config_.content_type = VideoEncoderConfig::ContentType::kScreen; |
| 72 | } |
| 73 | |
| 74 | if (type == VideoCodecType::kVideoCodecVP8) { |
| 75 | config_.number_of_streams = num_spatial_streams; |
| 76 | VideoCodecVP8 vp8_settings = VideoEncoder::GetDefaultVp8Settings(); |
| 77 | vp8_settings.numberOfTemporalLayers = num_temporal_streams; |
| 78 | config_.encoder_specific_settings = new rtc::RefCountedObject< |
| 79 | webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); |
| 80 | settings_.payload_name = kVp8PayloadName; |
| 81 | settings_.payload_type = kVp8PayloadType; |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 82 | } else if (type == VideoCodecType::kVideoCodecStereo) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 83 | } else { |
| 84 | ADD_FAILURE() << "Unexpected codec type: " << type; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | bool InitializeCodec() { |
| 89 | codec_out_ = VideoCodec(); |
| 90 | bitrate_allocator_out_.reset(); |
| 91 | temporal_layers_.clear(); |
| 92 | if (!VideoCodecInitializer::SetupCodec(config_, settings_, streams_, |
| 93 | nack_enabled_, &codec_out_, |
| 94 | &bitrate_allocator_out_)) { |
| 95 | return false; |
| 96 | } |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 97 | if (codec_out_.codecType == VideoCodecType::kVideoCodecStereo) |
| 98 | return true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 99 | // Make sure temporal layers instances have been created. |
| 100 | if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) { |
| 101 | if (!codec_out_.VP8()->tl_factory) |
| 102 | return false; |
| 103 | |
| 104 | for (int i = 0; i < codec_out_.numberOfSimulcastStreams; ++i) { |
| 105 | temporal_layers_.emplace_back(codec_out_.VP8()->tl_factory->Create( |
| 106 | i, streams_[i].temporal_layer_thresholds_bps.size() + 1, 0)); |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | VideoStream DefaultStream() { |
| 113 | VideoStream stream; |
| 114 | stream.width = kDefaultWidth; |
| 115 | stream.height = kDefaultHeight; |
| 116 | stream.max_framerate = kDefaultFrameRate; |
| 117 | stream.min_bitrate_bps = kDefaultMinBitrateBps; |
| 118 | stream.target_bitrate_bps = kDefaultTargetBitrateBps; |
| 119 | stream.max_bitrate_bps = kDefaultMaxBitrateBps; |
| 120 | stream.max_qp = kDefaultMaxQp; |
Seth Hampson | 18c4261 | 2018-01-12 11:33:12 -0800 | [diff] [blame^] | 121 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 122 | return stream; |
| 123 | } |
| 124 | |
| 125 | VideoStream DefaultScreenshareStream() { |
| 126 | VideoStream stream = DefaultStream(); |
| 127 | stream.min_bitrate_bps = 30000; |
| 128 | stream.target_bitrate_bps = kScreenshareTl0BitrateBps; |
| 129 | stream.max_bitrate_bps = 1000000; |
| 130 | stream.max_framerate = kScreenshareDefaultFramerate; |
| 131 | stream.temporal_layer_thresholds_bps.push_back(kScreenshareTl0BitrateBps); |
Seth Hampson | 18c4261 | 2018-01-12 11:33:12 -0800 | [diff] [blame^] | 132 | stream.active = true; |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 133 | return stream; |
| 134 | } |
| 135 | |
| 136 | // Input settings. |
| 137 | VideoEncoderConfig config_; |
| 138 | VideoSendStream::Config::EncoderSettings settings_; |
| 139 | std::vector<VideoStream> streams_; |
| 140 | bool nack_enabled_; |
| 141 | |
| 142 | // Output. |
| 143 | VideoCodec codec_out_; |
| 144 | std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_out_; |
| 145 | std::vector<std::unique_ptr<TemporalLayers>> temporal_layers_; |
| 146 | }; |
| 147 | |
| 148 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) { |
| 149 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 150 | streams_.push_back(DefaultStream()); |
| 151 | EXPECT_TRUE(InitializeCodec()); |
| 152 | |
| 153 | BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation( |
| 154 | kDefaultTargetBitrateBps, kDefaultFrameRate); |
| 155 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 156 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 157 | EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps()); |
| 158 | } |
| 159 | |
Seth Hampson | 18c4261 | 2018-01-12 11:33:12 -0800 | [diff] [blame^] | 160 | TEST_F(VideoCodecInitializerTest, SingleStreamVp8ScreenshareInactive) { |
| 161 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true); |
| 162 | VideoStream inactive_stream = DefaultStream(); |
| 163 | inactive_stream.active = false; |
| 164 | streams_.push_back(inactive_stream); |
| 165 | EXPECT_TRUE(InitializeCodec()); |
| 166 | |
| 167 | BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation( |
| 168 | kDefaultTargetBitrateBps, kDefaultFrameRate); |
| 169 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 170 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 171 | EXPECT_EQ(0U, bitrate_allocation.get_sum_bps()); |
| 172 | } |
| 173 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 174 | TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) { |
| 175 | SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true); |
| 176 | streams_.push_back(DefaultScreenshareStream()); |
| 177 | EXPECT_TRUE(InitializeCodec()); |
| 178 | |
| 179 | EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams); |
| 180 | EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers); |
| 181 | BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation( |
| 182 | kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate); |
| 183 | EXPECT_EQ(kScreenshareCodecTargetBitrateBps, |
| 184 | bitrate_allocation.get_sum_bps()); |
| 185 | EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0)); |
| 186 | } |
| 187 | |
Seth Hampson | 18c4261 | 2018-01-12 11:33:12 -0800 | [diff] [blame^] | 188 | TEST_F(VideoCodecInitializerTest, SimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 189 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 190 | streams_.push_back(DefaultScreenshareStream()); |
| 191 | VideoStream video_stream = DefaultStream(); |
| 192 | video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 193 | streams_.push_back(video_stream); |
| 194 | EXPECT_TRUE(InitializeCodec()); |
| 195 | |
| 196 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 197 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 198 | const uint32_t max_bitrate_bps = |
| 199 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
| 200 | BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation( |
| 201 | max_bitrate_bps, kScreenshareDefaultFramerate); |
| 202 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 203 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 204 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 205 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 206 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 207 | } |
| 208 | |
Seth Hampson | 18c4261 | 2018-01-12 11:33:12 -0800 | [diff] [blame^] | 209 | // Tests that when a video stream is inactive, then the bitrate allocation will |
| 210 | // be 0 for that stream. |
| 211 | TEST_F(VideoCodecInitializerTest, SimulcastVp8ScreenshareInactive) { |
| 212 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true); |
| 213 | streams_.push_back(DefaultScreenshareStream()); |
| 214 | VideoStream inactive_video_stream = DefaultStream(); |
| 215 | inactive_video_stream.active = false; |
| 216 | inactive_video_stream.max_framerate = kScreenshareDefaultFramerate; |
| 217 | streams_.push_back(inactive_video_stream); |
| 218 | EXPECT_TRUE(InitializeCodec()); |
| 219 | |
| 220 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 221 | EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers); |
| 222 | const uint32_t target_bitrate = |
| 223 | streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps; |
| 224 | BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation( |
| 225 | target_bitrate, kScreenshareDefaultFramerate); |
| 226 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 227 | bitrate_allocation.get_sum_bps()); |
| 228 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps), |
| 229 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 230 | EXPECT_EQ(0U, bitrate_allocation.GetSpatialLayerSum(1)); |
| 231 | } |
| 232 | |
| 233 | TEST_F(VideoCodecInitializerTest, HighFpsSimulcastVp8Screenshare) { |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 234 | // Two simulcast streams, the lower one using legacy settings (two temporal |
| 235 | // streams, 5fps), the higher one using 3 temporal streams and 30fps. |
| 236 | SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true); |
| 237 | streams_.push_back(DefaultScreenshareStream()); |
| 238 | VideoStream video_stream = DefaultStream(); |
| 239 | video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl0Bps); |
| 240 | video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl1Bps); |
| 241 | streams_.push_back(video_stream); |
| 242 | EXPECT_TRUE(InitializeCodec()); |
| 243 | |
| 244 | EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams); |
| 245 | EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers); |
| 246 | const uint32_t max_bitrate_bps = |
| 247 | streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps; |
| 248 | BitrateAllocation bitrate_allocation = |
| 249 | bitrate_allocator_out_->GetAllocation(max_bitrate_bps, kDefaultFrameRate); |
| 250 | EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps()); |
| 251 | EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps), |
| 252 | bitrate_allocation.GetSpatialLayerSum(0)); |
| 253 | EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps), |
| 254 | bitrate_allocation.GetSpatialLayerSum(1)); |
| 255 | EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0)); |
| 256 | EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps, |
| 257 | bitrate_allocation.GetBitrate(1, 1)); |
| 258 | } |
| 259 | |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 260 | TEST_F(VideoCodecInitializerTest, SingleStreamStereoCodec) { |
| 261 | SetUpFor(VideoCodecType::kVideoCodecStereo, 1, 1, true); |
| 262 | streams_.push_back(DefaultStream()); |
| 263 | EXPECT_TRUE(InitializeCodec()); |
| 264 | } |
| 265 | |
sprang | 429600d | 2017-01-26 06:12:26 -0800 | [diff] [blame] | 266 | } // namespace webrtc |