blob: 48bf466863ce505c24be8a9927941b12d43301d9 [file] [log] [blame]
sprang429600d2017-01-26 06:12:26 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/video_codecs/video_encoder.h"
12#include "common_video/include/video_bitrate_allocator.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020013#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/codecs/vp8/temporal_layers.h"
15#include "modules/video_coding/include/video_codec_initializer.h"
Niels Möller84255bb2017-10-06 13:43:23 +020016#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/gtest.h"
sprang429600d2017-01-26 06:12:26 -080018
19namespace webrtc {
20
21namespace {
22static const char* kVp8PayloadName = "VP8";
23static const int kVp8PayloadType = 100;
24static const int kDefaultWidth = 1280;
25static const int kDefaultHeight = 720;
26static const int kDefaultFrameRate = 30;
27static const uint32_t kDefaultMinBitrateBps = 60000;
28static const uint32_t kDefaultTargetBitrateBps = 2000000;
29static const uint32_t kDefaultMaxBitrateBps = 2000000;
30static const uint32_t kDefaultMinTransmitBitrateBps = 400000;
31static const int kDefaultMaxQp = 48;
32static const uint32_t kScreenshareTl0BitrateBps = 100000;
33static const uint32_t kScreenshareCodecTargetBitrateBps = 200000;
34static const uint32_t kScreenshareDefaultFramerate = 5;
35// Bitrates for the temporal layers of the higher screenshare simulcast stream.
36static const uint32_t kHighScreenshareTl0Bps = 800000;
37static 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.
58class 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;
82 } else {
83 ADD_FAILURE() << "Unexpected codec type: " << type;
84 }
85 }
86
87 bool InitializeCodec() {
88 codec_out_ = VideoCodec();
89 bitrate_allocator_out_.reset();
90 temporal_layers_.clear();
91 if (!VideoCodecInitializer::SetupCodec(config_, settings_, streams_,
92 nack_enabled_, &codec_out_,
93 &bitrate_allocator_out_)) {
94 return false;
95 }
96
97 // Make sure temporal layers instances have been created.
98 if (codec_out_.codecType == VideoCodecType::kVideoCodecVP8) {
99 if (!codec_out_.VP8()->tl_factory)
100 return false;
101
102 for (int i = 0; i < codec_out_.numberOfSimulcastStreams; ++i) {
103 temporal_layers_.emplace_back(codec_out_.VP8()->tl_factory->Create(
104 i, streams_[i].temporal_layer_thresholds_bps.size() + 1, 0));
105 }
106 }
107 return true;
108 }
109
110 VideoStream DefaultStream() {
111 VideoStream stream;
112 stream.width = kDefaultWidth;
113 stream.height = kDefaultHeight;
114 stream.max_framerate = kDefaultFrameRate;
115 stream.min_bitrate_bps = kDefaultMinBitrateBps;
116 stream.target_bitrate_bps = kDefaultTargetBitrateBps;
117 stream.max_bitrate_bps = kDefaultMaxBitrateBps;
118 stream.max_qp = kDefaultMaxQp;
119 return stream;
120 }
121
122 VideoStream DefaultScreenshareStream() {
123 VideoStream stream = DefaultStream();
124 stream.min_bitrate_bps = 30000;
125 stream.target_bitrate_bps = kScreenshareTl0BitrateBps;
126 stream.max_bitrate_bps = 1000000;
127 stream.max_framerate = kScreenshareDefaultFramerate;
128 stream.temporal_layer_thresholds_bps.push_back(kScreenshareTl0BitrateBps);
129 return stream;
130 }
131
132 // Input settings.
133 VideoEncoderConfig config_;
134 VideoSendStream::Config::EncoderSettings settings_;
135 std::vector<VideoStream> streams_;
136 bool nack_enabled_;
137
138 // Output.
139 VideoCodec codec_out_;
140 std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_out_;
141 std::vector<std::unique_ptr<TemporalLayers>> temporal_layers_;
142};
143
144TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) {
145 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
146 streams_.push_back(DefaultStream());
147 EXPECT_TRUE(InitializeCodec());
148
149 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
150 kDefaultTargetBitrateBps, kDefaultFrameRate);
151 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
152 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
153 EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
154}
155
156TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) {
157 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true);
158 streams_.push_back(DefaultScreenshareStream());
159 EXPECT_TRUE(InitializeCodec());
160
161 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
162 EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
163 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
164 kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
165 EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
166 bitrate_allocation.get_sum_bps());
167 EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
168}
169
170TEST_F(VideoCodecInitializerTest, SimlucastVp8Screenshare) {
171 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
172 streams_.push_back(DefaultScreenshareStream());
173 VideoStream video_stream = DefaultStream();
174 video_stream.max_framerate = kScreenshareDefaultFramerate;
175 streams_.push_back(video_stream);
176 EXPECT_TRUE(InitializeCodec());
177
178 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
179 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
180 const uint32_t max_bitrate_bps =
181 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
182 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
183 max_bitrate_bps, kScreenshareDefaultFramerate);
184 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
185 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
186 bitrate_allocation.GetSpatialLayerSum(0));
187 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
188 bitrate_allocation.GetSpatialLayerSum(1));
189}
190
191TEST_F(VideoCodecInitializerTest, HighFpsSimlucastVp8Screenshare) {
192 // Two simulcast streams, the lower one using legacy settings (two temporal
193 // streams, 5fps), the higher one using 3 temporal streams and 30fps.
194 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true);
195 streams_.push_back(DefaultScreenshareStream());
196 VideoStream video_stream = DefaultStream();
197 video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl0Bps);
198 video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl1Bps);
199 streams_.push_back(video_stream);
200 EXPECT_TRUE(InitializeCodec());
201
202 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
203 EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers);
204 const uint32_t max_bitrate_bps =
205 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
206 BitrateAllocation bitrate_allocation =
207 bitrate_allocator_out_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
208 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
209 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
210 bitrate_allocation.GetSpatialLayerSum(0));
211 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
212 bitrate_allocation.GetSpatialLayerSum(1));
213 EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0));
214 EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps,
215 bitrate_allocation.GetBitrate(1, 1));
216}
217
218} // namespace webrtc