blob: bdf294725afb966443bb24dec4dcede0477c2963 [file] [log] [blame]
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +00001/*
2 * Copyright (c) 2013 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
11#include <vector>
12
13#include "testing/gtest/include/gtest/gtest.h"
andresp@webrtc.org03ced522013-10-03 14:06:14 +000014#include "webrtc/common.h"
andresp@webrtc.org84afa192013-09-23 11:12:59 +000015#include "webrtc/common_video/test/frame_generator.h"
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +000016#include "webrtc/modules/video_coding/codecs/interface/mock/mock_video_codec_interface.h"
andresp@webrtc.org84afa192013-09-23 11:12:59 +000017#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
andresp@webrtc.org03ced522013-10-03 14:06:14 +000018#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +000019#include "webrtc/modules/video_coding/main/interface/mock/mock_vcm_callbacks.h"
20#include "webrtc/modules/video_coding/main/interface/video_coding.h"
21#include "webrtc/modules/video_coding/main/source/video_coding_impl.h"
22#include "webrtc/modules/video_coding/main/test/test_util.h"
23#include "webrtc/system_wrappers/interface/clock.h"
24#include "webrtc/system_wrappers/interface/scoped_ptr.h"
andresp@webrtc.org84afa192013-09-23 11:12:59 +000025#include "webrtc/test/testsupport/fileutils.h"
andrew@webrtc.org4b067da2013-09-24 18:43:28 +000026#include "webrtc/test/testsupport/gtest_disable.h"
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +000027
28using ::testing::_;
29using ::testing::AllOf;
30using ::testing::ElementsAre;
31using ::testing::ElementsAreArray;
32using ::testing::Field;
33using ::testing::NiceMock;
34using ::testing::Pointee;
35using ::testing::Return;
andresp@webrtc.org03ced522013-10-03 14:06:14 +000036using ::testing::FloatEq;
andresp@webrtc.org84afa192013-09-23 11:12:59 +000037using std::vector;
38using webrtc::test::FrameGenerator;
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +000039
40namespace webrtc {
41namespace vcm {
42namespace {
andresp@webrtc.org03ced522013-10-03 14:06:14 +000043enum {
44 kMaxNumberOfTemporalLayers = 3
45};
46
47struct Vp8StreamInfo {
48 float framerate_fps[kMaxNumberOfTemporalLayers];
49 int bitrate_kbps[kMaxNumberOfTemporalLayers];
50};
51
52MATCHER_P(MatchesVp8StreamInfo, expected, "") {
53 bool res = true;
54 for (int tl = 0; tl < kMaxNumberOfTemporalLayers; ++tl) {
55 if (abs(expected.framerate_fps[tl] - arg.framerate_fps[tl]) > 0.5) {
56 *result_listener << " framerate_fps[" << tl
57 << "] = " << arg.framerate_fps[tl] << " (expected "
58 << expected.framerate_fps[tl] << ") ";
59 res = false;
60 }
61 if (abs(expected.bitrate_kbps[tl] - arg.bitrate_kbps[tl]) > 10) {
62 *result_listener << " bitrate_kbps[" << tl
63 << "] = " << arg.bitrate_kbps[tl] << " (expected "
64 << expected.bitrate_kbps[tl] << ") ";
65 res = false;
66 }
67 }
68 return res;
69}
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +000070
andresp@webrtc.org84afa192013-09-23 11:12:59 +000071class EmptyFrameGenerator : public FrameGenerator {
72 public:
73 virtual I420VideoFrame& NextFrame() OVERRIDE { return frame_; }
74
75 private:
76 I420VideoFrame frame_;
77};
78
79class PacketizationCallback : public VCMPacketizationCallback {
80 public:
81 PacketizationCallback(Clock* clock)
82 : clock_(clock), start_time_ms_(clock_->TimeInMilliseconds()) {}
83
84 virtual ~PacketizationCallback() {}
85
86 virtual int32_t SendData(FrameType frame_type,
87 uint8_t payload_type,
88 uint32_t timestamp,
89 int64_t capture_time_ms,
90 const uint8_t* payload_data,
91 uint32_t payload_size,
92 const RTPFragmentationHeader& fragmentation_header,
93 const RTPVideoHeader* rtp_video_header) {
94 assert(rtp_video_header);
95 frame_data_.push_back(FrameData(payload_size, *rtp_video_header));
96 return 0;
97 }
98
99 void Reset() {
100 frame_data_.clear();
101 start_time_ms_ = clock_->TimeInMilliseconds();
102 }
103
104 float FramerateFpsWithinTemporalLayer(int temporal_layer) {
105 return CountFramesWithinTemporalLayer(temporal_layer) *
106 (1000.0 / interval_ms());
107 }
108
109 float BitrateKbpsWithinTemporalLayer(int temporal_layer) {
110 return SumPayloadBytesWithinTemporalLayer(temporal_layer) * 8.0 /
111 interval_ms();
112 }
113
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000114 Vp8StreamInfo CalculateVp8StreamInfo() {
115 Vp8StreamInfo info;
116 for (int tl = 0; tl < 3; ++tl) {
117 info.framerate_fps[tl] = FramerateFpsWithinTemporalLayer(tl);
118 info.bitrate_kbps[tl] = BitrateKbpsWithinTemporalLayer(tl);
119 }
120 return info;
121 }
122
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000123 private:
124 struct FrameData {
125 FrameData() {}
126
127 FrameData(uint32_t payload_size, const RTPVideoHeader& rtp_video_header)
128 : payload_size(payload_size), rtp_video_header(rtp_video_header) {}
129
130 uint32_t payload_size;
131 RTPVideoHeader rtp_video_header;
132 };
133
134 int64_t interval_ms() {
135 int64_t diff = (clock_->TimeInMilliseconds() - start_time_ms_);
136 EXPECT_GT(diff, 0);
137 return diff;
138 }
139
140 int CountFramesWithinTemporalLayer(int temporal_layer) {
141 int frames = 0;
142 for (size_t i = 0; i < frame_data_.size(); ++i) {
143 EXPECT_EQ(kRtpVideoVp8, frame_data_[i].rtp_video_header.codec);
144 if (frame_data_[i].rtp_video_header.codecHeader.VP8.temporalIdx <=
145 temporal_layer) {
146 frames++;
147 }
148 }
149 return frames;
150 }
151
152 int SumPayloadBytesWithinTemporalLayer(int temporal_layer) {
153 int payload_size = 0;
154 for (size_t i = 0; i < frame_data_.size(); ++i) {
155 EXPECT_EQ(kRtpVideoVp8, frame_data_[i].rtp_video_header.codec);
156 if (frame_data_[i].rtp_video_header.codecHeader.VP8.temporalIdx <=
157 temporal_layer) {
158 payload_size += frame_data_[i].payload_size;
159 }
160 }
161 return payload_size;
162 }
163
164 Clock* clock_;
165 int64_t start_time_ms_;
166 vector<FrameData> frame_data_;
167};
168
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000169class TestVideoSender : public ::testing::Test {
170 protected:
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000171 // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
172 // a special case (e.g. frame rate in media optimization).
173 TestVideoSender() : clock_(1000), packetization_callback_(&clock_) {}
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000174
175 virtual void SetUp() {
176 sender_.reset(new VideoSender(0, &clock_));
177 EXPECT_EQ(0, sender_->InitializeSender());
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000178 EXPECT_EQ(0, sender_->RegisterTransportCallback(&packetization_callback_));
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000179 }
180
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000181 void AddFrame() {
182 assert(generator_.get());
183 sender_->AddVideoFrame(generator_->NextFrame(), NULL, NULL);
184 }
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000185
186 SimulatedClock clock_;
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000187 PacketizationCallback packetization_callback_;
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000188 scoped_ptr<VideoSender> sender_;
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000189 scoped_ptr<FrameGenerator> generator_;
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000190};
191
192class TestVideoSenderWithMockEncoder : public TestVideoSender {
193 protected:
194 static const int kDefaultWidth = 1280;
195 static const int kDefaultHeight = 720;
196 static const int kNumberOfStreams = 3;
197 static const int kNumberOfLayers = 3;
198 static const int kUnusedPayloadType = 10;
199
200 virtual void SetUp() {
201 TestVideoSender::SetUp();
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000202 generator_.reset(new EmptyFrameGenerator());
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000203 EXPECT_EQ(
204 0,
205 sender_->RegisterExternalEncoder(&encoder_, kUnusedPayloadType, false));
206 memset(&settings_, 0, sizeof(settings_));
207 EXPECT_EQ(0, VideoCodingModule::Codec(kVideoCodecVP8, &settings_));
208 settings_.numberOfSimulcastStreams = kNumberOfStreams;
209 ConfigureStream(kDefaultWidth / 4,
210 kDefaultHeight / 4,
211 100,
212 &settings_.simulcastStream[0]);
213 ConfigureStream(kDefaultWidth / 2,
214 kDefaultHeight / 2,
215 500,
216 &settings_.simulcastStream[1]);
217 ConfigureStream(
218 kDefaultWidth, kDefaultHeight, 1200, &settings_.simulcastStream[2]);
219 settings_.plType = kUnusedPayloadType; // Use the mocked encoder.
220 EXPECT_EQ(0, sender_->RegisterSendCodec(&settings_, 1, 1200));
221 }
222
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000223 virtual void TearDown() { sender_.reset(); }
224
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000225 void ExpectIntraRequest(int stream) {
226 if (stream == -1) {
227 // No intra request expected.
228 EXPECT_CALL(
229 encoder_,
230 Encode(_,
231 _,
232 Pointee(ElementsAre(kDeltaFrame, kDeltaFrame, kDeltaFrame))))
233 .Times(1).WillRepeatedly(Return(0));
234 return;
235 }
236 assert(stream >= 0);
237 assert(stream < kNumberOfStreams);
238 std::vector<VideoFrameType> frame_types(kNumberOfStreams, kDeltaFrame);
239 frame_types[stream] = kKeyFrame;
240 EXPECT_CALL(
241 encoder_,
242 Encode(_,
243 _,
244 Pointee(ElementsAreArray(&frame_types[0], frame_types.size()))))
245 .Times(1).WillRepeatedly(Return(0));
246 }
247
248 static void ConfigureStream(int width,
249 int height,
250 int max_bitrate,
251 SimulcastStream* stream) {
252 assert(stream);
253 stream->width = width;
254 stream->height = height;
255 stream->maxBitrate = max_bitrate;
256 stream->numberOfTemporalLayers = kNumberOfLayers;
257 stream->qpMax = 45;
258 }
259
260 VideoCodec settings_;
261 NiceMock<MockVideoEncoder> encoder_;
262};
263
264TEST_F(TestVideoSenderWithMockEncoder, TestIntraRequests) {
265 EXPECT_EQ(0, sender_->IntraFrameRequest(0));
266 ExpectIntraRequest(0);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000267 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000268 ExpectIntraRequest(-1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000269 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000270
271 EXPECT_EQ(0, sender_->IntraFrameRequest(1));
272 ExpectIntraRequest(1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000273 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000274 ExpectIntraRequest(-1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000275 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000276
277 EXPECT_EQ(0, sender_->IntraFrameRequest(2));
278 ExpectIntraRequest(2);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000279 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000280 ExpectIntraRequest(-1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000281 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000282
283 EXPECT_EQ(-1, sender_->IntraFrameRequest(3));
284 ExpectIntraRequest(-1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000285 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000286
287 EXPECT_EQ(-1, sender_->IntraFrameRequest(-1));
288 ExpectIntraRequest(-1);
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000289 AddFrame();
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000290}
291
292TEST_F(TestVideoSenderWithMockEncoder, TestIntraRequestsInternalCapture) {
293 // De-register current external encoder.
294 EXPECT_EQ(0,
295 sender_->RegisterExternalEncoder(NULL, kUnusedPayloadType, false));
296 // Register encoder with internal capture.
297 EXPECT_EQ(
298 0, sender_->RegisterExternalEncoder(&encoder_, kUnusedPayloadType, true));
299 EXPECT_EQ(0, sender_->RegisterSendCodec(&settings_, 1, 1200));
300 ExpectIntraRequest(0);
301 EXPECT_EQ(0, sender_->IntraFrameRequest(0));
302 ExpectIntraRequest(1);
303 EXPECT_EQ(0, sender_->IntraFrameRequest(1));
304 ExpectIntraRequest(2);
305 EXPECT_EQ(0, sender_->IntraFrameRequest(2));
306 // No requests expected since these indices are out of bounds.
307 EXPECT_EQ(-1, sender_->IntraFrameRequest(3));
308 EXPECT_EQ(-1, sender_->IntraFrameRequest(-1));
309}
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000310
311class TestVideoSenderWithVp8 : public TestVideoSender {
312 public:
313 TestVideoSenderWithVp8()
314 : codec_bitrate_kbps_(300), available_bitrate_kbps_(1000) {}
315
316 virtual void SetUp() {
317 TestVideoSender::SetUp();
318
319 const char* input_video = "foreman_cif";
320 const int width = 352;
321 const int height = 288;
322 generator_.reset(FrameGenerator::CreateFromYuvFile(
323 test::ResourcePath(input_video, "yuv").c_str(), width, height));
324
325 codec_ = MakeVp8VideoCodec(width, height, 3);
326 codec_.minBitrate = 10;
327 codec_.startBitrate = codec_bitrate_kbps_;
328 codec_.maxBitrate = codec_bitrate_kbps_;
329 EXPECT_EQ(0, sender_->RegisterSendCodec(&codec_, 1, 1200));
330 }
331
332 static VideoCodec MakeVp8VideoCodec(int width,
333 int height,
334 int temporal_layers) {
335 VideoCodec codec;
336 memset(&codec, 0, sizeof(codec));
337 EXPECT_EQ(0, VideoCodingModule::Codec(kVideoCodecVP8, &codec));
338 codec.width = width;
339 codec.height = height;
340 codec.codecSpecific.VP8.numberOfTemporalLayers = temporal_layers;
341 return codec;
342 }
343
344 void InsertFrames(float framerate, float seconds) {
345 for (int i = 0; i < seconds * framerate; ++i) {
346 clock_.AdvanceTimeMilliseconds(1000.0f / framerate);
347 AddFrame();
348
349 // SetChannelParameters needs to be called frequently to propagate
350 // framerate from the media optimization into the encoder.
351 // Note: SetChannelParameters fails if less than 2 frames are in the
352 // buffer since it will fail to calculate the framerate.
353 if (i != 0) {
354 EXPECT_EQ(VCM_OK,
355 sender_->SetChannelParameters(
356 available_bitrate_kbps_ * 1000, 0, 200));
357 }
358 }
359 }
360
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000361 Vp8StreamInfo SimulateWithFramerate(float framerate) {
362 const float short_simulation_interval = 5.0;
363 const float long_simulation_interval = 10.0;
364 // It appears that this 5 seconds simulation is needed to allow
365 // bitrate and framerate to stabilize.
366 InsertFrames(framerate, short_simulation_interval);
367 packetization_callback_.Reset();
368
369 InsertFrames(framerate, long_simulation_interval);
370 return packetization_callback_.CalculateVp8StreamInfo();
371 }
372
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000373 protected:
374 VideoCodec codec_;
375 int codec_bitrate_kbps_;
376 int available_bitrate_kbps_;
377};
378
andrew@webrtc.org4b067da2013-09-24 18:43:28 +0000379TEST_F(TestVideoSenderWithVp8,
380 DISABLED_ON_ANDROID(FixedTemporalLayersStrategy)) {
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000381 const int low_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][0];
382 const int mid_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][1];
383 const int high_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][2];
384 {
385 Vp8StreamInfo expected = {{7.5, 15.0, 30.0}, {low_b, mid_b, high_b}};
386 EXPECT_THAT(SimulateWithFramerate(30.0), MatchesVp8StreamInfo(expected));
387 }
388 {
389 Vp8StreamInfo expected = {{3.75, 7.5, 15.0}, {low_b, mid_b, high_b}};
390 EXPECT_THAT(SimulateWithFramerate(15.0), MatchesVp8StreamInfo(expected));
391 }
392}
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000393
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000394TEST_F(TestVideoSenderWithVp8,
395 DISABLED_ON_ANDROID(RealTimeTemporalLayersStrategy)) {
396 Config extra_options;
397 extra_options.Set<TemporalLayers::Factory>(
398 new RealTimeTemporalLayersFactory());
399 VideoCodec codec = MakeVp8VideoCodec(352, 288, 3);
400 codec.extra_options = &extra_options;
401 codec.minBitrate = 10;
402 codec.startBitrate = codec_bitrate_kbps_;
403 codec.maxBitrate = codec_bitrate_kbps_;
404 EXPECT_EQ(0, sender_->RegisterSendCodec(&codec, 1, 1200));
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000405
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000406 const int low_b = codec_bitrate_kbps_ * 0.4;
407 const int mid_b = codec_bitrate_kbps_ * 0.6;
408 const int high_b = codec_bitrate_kbps_;
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000409
andresp@webrtc.org03ced522013-10-03 14:06:14 +0000410 {
411 Vp8StreamInfo expected = {{7.5, 15.0, 30.0}, {low_b, mid_b, high_b}};
412 EXPECT_THAT(SimulateWithFramerate(30.0), MatchesVp8StreamInfo(expected));
413 }
414 {
415 Vp8StreamInfo expected = {{5.0, 10.0, 20.0}, {low_b, mid_b, high_b}};
416 EXPECT_THAT(SimulateWithFramerate(20.0), MatchesVp8StreamInfo(expected));
417 }
418 {
419 Vp8StreamInfo expected = {{7.5, 15.0, 15.0}, {mid_b, high_b, high_b}};
420 EXPECT_THAT(SimulateWithFramerate(15.0), MatchesVp8StreamInfo(expected));
421 }
422 {
423 Vp8StreamInfo expected = {{5.0, 10.0, 10.0}, {mid_b, high_b, high_b}};
424 EXPECT_THAT(SimulateWithFramerate(10.0), MatchesVp8StreamInfo(expected));
425 }
426 {
427 // TODO(andresp): Find out why this fails with framerate = 7.5
428 Vp8StreamInfo expected = {{7.0, 7.0, 7.0}, {high_b, high_b, high_b}};
429 EXPECT_THAT(SimulateWithFramerate(7.0), MatchesVp8StreamInfo(expected));
430 }
andresp@webrtc.org84afa192013-09-23 11:12:59 +0000431}
andresp@webrtc.orge8fdc9d2013-09-16 20:29:13 +0000432} // namespace
433} // namespace vcm
434} // namespace webrtc