blob: 0862b7282e628d249dbe720b023adb39289b5af7 [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;
Emircan Uysaler0a375472017-12-11 12:21:02 +053082 } else if (type == VideoCodecType::kVideoCodecStereo) {
sprang429600d2017-01-26 06:12:26 -080083 } 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 Uysaler0a375472017-12-11 12:21:02 +053097 if (codec_out_.codecType == VideoCodecType::kVideoCodecStereo)
98 return true;
sprang429600d2017-01-26 06:12:26 -080099 // 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;
121 return stream;
122 }
123
124 VideoStream DefaultScreenshareStream() {
125 VideoStream stream = DefaultStream();
126 stream.min_bitrate_bps = 30000;
127 stream.target_bitrate_bps = kScreenshareTl0BitrateBps;
128 stream.max_bitrate_bps = 1000000;
129 stream.max_framerate = kScreenshareDefaultFramerate;
130 stream.temporal_layer_thresholds_bps.push_back(kScreenshareTl0BitrateBps);
131 return stream;
132 }
133
134 // Input settings.
135 VideoEncoderConfig config_;
136 VideoSendStream::Config::EncoderSettings settings_;
137 std::vector<VideoStream> streams_;
138 bool nack_enabled_;
139
140 // Output.
141 VideoCodec codec_out_;
142 std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_out_;
143 std::vector<std::unique_ptr<TemporalLayers>> temporal_layers_;
144};
145
146TEST_F(VideoCodecInitializerTest, SingleStreamVp8Screenshare) {
147 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 1, true);
148 streams_.push_back(DefaultStream());
149 EXPECT_TRUE(InitializeCodec());
150
151 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
152 kDefaultTargetBitrateBps, kDefaultFrameRate);
153 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
154 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
155 EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
156}
157
158TEST_F(VideoCodecInitializerTest, TemporalLayeredVp8Screenshare) {
159 SetUpFor(VideoCodecType::kVideoCodecVP8, 1, 2, true);
160 streams_.push_back(DefaultScreenshareStream());
161 EXPECT_TRUE(InitializeCodec());
162
163 EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
164 EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
165 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
166 kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
167 EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
168 bitrate_allocation.get_sum_bps());
169 EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
170}
171
172TEST_F(VideoCodecInitializerTest, SimlucastVp8Screenshare) {
173 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 1, true);
174 streams_.push_back(DefaultScreenshareStream());
175 VideoStream video_stream = DefaultStream();
176 video_stream.max_framerate = kScreenshareDefaultFramerate;
177 streams_.push_back(video_stream);
178 EXPECT_TRUE(InitializeCodec());
179
180 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
181 EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
182 const uint32_t max_bitrate_bps =
183 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
184 BitrateAllocation bitrate_allocation = bitrate_allocator_out_->GetAllocation(
185 max_bitrate_bps, kScreenshareDefaultFramerate);
186 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
187 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
188 bitrate_allocation.GetSpatialLayerSum(0));
189 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
190 bitrate_allocation.GetSpatialLayerSum(1));
191}
192
193TEST_F(VideoCodecInitializerTest, HighFpsSimlucastVp8Screenshare) {
194 // Two simulcast streams, the lower one using legacy settings (two temporal
195 // streams, 5fps), the higher one using 3 temporal streams and 30fps.
196 SetUpFor(VideoCodecType::kVideoCodecVP8, 2, 3, true);
197 streams_.push_back(DefaultScreenshareStream());
198 VideoStream video_stream = DefaultStream();
199 video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl0Bps);
200 video_stream.temporal_layer_thresholds_bps.push_back(kHighScreenshareTl1Bps);
201 streams_.push_back(video_stream);
202 EXPECT_TRUE(InitializeCodec());
203
204 EXPECT_EQ(2u, codec_out_.numberOfSimulcastStreams);
205 EXPECT_EQ(3u, codec_out_.VP8()->numberOfTemporalLayers);
206 const uint32_t max_bitrate_bps =
207 streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
208 BitrateAllocation bitrate_allocation =
209 bitrate_allocator_out_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
210 EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
211 EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
212 bitrate_allocation.GetSpatialLayerSum(0));
213 EXPECT_EQ(static_cast<uint32_t>(streams_[1].max_bitrate_bps),
214 bitrate_allocation.GetSpatialLayerSum(1));
215 EXPECT_EQ(kHighScreenshareTl0Bps, bitrate_allocation.GetBitrate(1, 0));
216 EXPECT_EQ(kHighScreenshareTl1Bps - kHighScreenshareTl0Bps,
217 bitrate_allocation.GetBitrate(1, 1));
218}
219
Emircan Uysaler0a375472017-12-11 12:21:02 +0530220TEST_F(VideoCodecInitializerTest, SingleStreamStereoCodec) {
221 SetUpFor(VideoCodecType::kVideoCodecStereo, 1, 1, true);
222 streams_.push_back(DefaultStream());
223 EXPECT_TRUE(InitializeCodec());
224}
225
sprang429600d2017-01-26 06:12:26 -0800226} // namespace webrtc