Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(978)

Side by Side Diff: webrtc/modules/video_coding/main/source/video_sender_unittest.cc

Issue 1417283007: modules/video_coding refactorings (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix the other copy of the mock include header Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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"
14 #include "webrtc/base/scoped_ptr.h"
15 #include "webrtc/common.h"
16 #include "webrtc/modules/video_coding/codecs/interface/mock/mock_video_codec_int erface.h"
17 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
18 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
19 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
20 #include "webrtc/modules/video_coding/main/interface/mock/mock_vcm_callbacks.h"
21 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
22 #include "webrtc/modules/video_coding/main/source/video_coding_impl.h"
23 #include "webrtc/modules/video_coding/main/test/test_util.h"
24 #include "webrtc/system_wrappers/include/clock.h"
25 #include "webrtc/test/frame_generator.h"
26 #include "webrtc/test/testsupport/fileutils.h"
27 #include "webrtc/test/testsupport/gtest_disable.h"
28
29 using ::testing::_;
30 using ::testing::AllOf;
31 using ::testing::ElementsAre;
32 using ::testing::ElementsAreArray;
33 using ::testing::Field;
34 using ::testing::NiceMock;
35 using ::testing::Pointee;
36 using ::testing::Return;
37 using ::testing::FloatEq;
38 using std::vector;
39 using webrtc::test::FrameGenerator;
40
41 namespace webrtc {
42 namespace vcm {
43 namespace {
44 enum {
45 kMaxNumberOfTemporalLayers = 3
46 };
47
48 struct Vp8StreamInfo {
49 float framerate_fps[kMaxNumberOfTemporalLayers];
50 int bitrate_kbps[kMaxNumberOfTemporalLayers];
51 };
52
53 MATCHER_P(MatchesVp8StreamInfo, expected, "") {
54 bool res = true;
55 for (int tl = 0; tl < kMaxNumberOfTemporalLayers; ++tl) {
56 if (fabs(expected.framerate_fps[tl] - arg.framerate_fps[tl]) > 0.5) {
57 *result_listener << " framerate_fps[" << tl
58 << "] = " << arg.framerate_fps[tl] << " (expected "
59 << expected.framerate_fps[tl] << ") ";
60 res = false;
61 }
62 if (abs(expected.bitrate_kbps[tl] - arg.bitrate_kbps[tl]) > 10) {
63 *result_listener << " bitrate_kbps[" << tl
64 << "] = " << arg.bitrate_kbps[tl] << " (expected "
65 << expected.bitrate_kbps[tl] << ") ";
66 res = false;
67 }
68 }
69 return res;
70 }
71
72 class EmptyFrameGenerator : public FrameGenerator {
73 public:
74 EmptyFrameGenerator(int width, int height) : width_(width), height_(height) {}
75 VideoFrame* NextFrame() override {
76 frame_.reset(new VideoFrame());
77 frame_->CreateEmptyFrame(width_, height_, width_, (width_ + 1) / 2,
78 (width_ + 1) / 2);
79 return frame_.get();
80 }
81
82 private:
83 const int width_;
84 const int height_;
85 rtc::scoped_ptr<VideoFrame> frame_;
86 };
87
88 class PacketizationCallback : public VCMPacketizationCallback {
89 public:
90 PacketizationCallback(Clock* clock)
91 : clock_(clock), start_time_ms_(clock_->TimeInMilliseconds()) {}
92
93 virtual ~PacketizationCallback() {}
94
95 int32_t SendData(uint8_t payload_type,
96 const EncodedImage& encoded_image,
97 const RTPFragmentationHeader& fragmentation_header,
98 const RTPVideoHeader* rtp_video_header) override {
99 assert(rtp_video_header);
100 frame_data_.push_back(FrameData(encoded_image._length, *rtp_video_header));
101 return 0;
102 }
103
104 void Reset() {
105 frame_data_.clear();
106 start_time_ms_ = clock_->TimeInMilliseconds();
107 }
108
109 float FramerateFpsWithinTemporalLayer(int temporal_layer) {
110 return CountFramesWithinTemporalLayer(temporal_layer) *
111 (1000.0 / interval_ms());
112 }
113
114 float BitrateKbpsWithinTemporalLayer(int temporal_layer) {
115 return SumPayloadBytesWithinTemporalLayer(temporal_layer) * 8.0 /
116 interval_ms();
117 }
118
119 Vp8StreamInfo CalculateVp8StreamInfo() {
120 Vp8StreamInfo info;
121 for (int tl = 0; tl < 3; ++tl) {
122 info.framerate_fps[tl] = FramerateFpsWithinTemporalLayer(tl);
123 info.bitrate_kbps[tl] = BitrateKbpsWithinTemporalLayer(tl);
124 }
125 return info;
126 }
127
128 private:
129 struct FrameData {
130 FrameData() {}
131
132 FrameData(size_t payload_size, const RTPVideoHeader& rtp_video_header)
133 : payload_size(payload_size), rtp_video_header(rtp_video_header) {}
134
135 size_t payload_size;
136 RTPVideoHeader rtp_video_header;
137 };
138
139 int64_t interval_ms() {
140 int64_t diff = (clock_->TimeInMilliseconds() - start_time_ms_);
141 EXPECT_GT(diff, 0);
142 return diff;
143 }
144
145 int CountFramesWithinTemporalLayer(int temporal_layer) {
146 int frames = 0;
147 for (size_t i = 0; i < frame_data_.size(); ++i) {
148 EXPECT_EQ(kRtpVideoVp8, frame_data_[i].rtp_video_header.codec);
149 const uint8_t temporal_idx =
150 frame_data_[i].rtp_video_header.codecHeader.VP8.temporalIdx;
151 if (temporal_idx <= temporal_layer || temporal_idx == kNoTemporalIdx)
152 frames++;
153 }
154 return frames;
155 }
156
157 size_t SumPayloadBytesWithinTemporalLayer(int temporal_layer) {
158 size_t payload_size = 0;
159 for (size_t i = 0; i < frame_data_.size(); ++i) {
160 EXPECT_EQ(kRtpVideoVp8, frame_data_[i].rtp_video_header.codec);
161 const uint8_t temporal_idx =
162 frame_data_[i].rtp_video_header.codecHeader.VP8.temporalIdx;
163 if (temporal_idx <= temporal_layer || temporal_idx == kNoTemporalIdx)
164 payload_size += frame_data_[i].payload_size;
165 }
166 return payload_size;
167 }
168
169 Clock* clock_;
170 int64_t start_time_ms_;
171 vector<FrameData> frame_data_;
172 };
173
174 class TestVideoSender : public ::testing::Test {
175 protected:
176 // Note: simulated clock starts at 1 seconds, since parts of webrtc use 0 as
177 // a special case (e.g. frame rate in media optimization).
178 TestVideoSender() : clock_(1000), packetization_callback_(&clock_) {}
179
180 void SetUp() override {
181 sender_.reset(
182 new VideoSender(&clock_, &post_encode_callback_, nullptr, nullptr));
183 EXPECT_EQ(0, sender_->RegisterTransportCallback(&packetization_callback_));
184 }
185
186 void AddFrame() {
187 assert(generator_.get());
188 sender_->AddVideoFrame(*generator_->NextFrame(), NULL, NULL);
189 }
190
191 SimulatedClock clock_;
192 PacketizationCallback packetization_callback_;
193 MockEncodedImageCallback post_encode_callback_;
194 // Used by subclassing tests, need to outlive sender_.
195 rtc::scoped_ptr<VideoEncoder> encoder_;
196 rtc::scoped_ptr<VideoSender> sender_;
197 rtc::scoped_ptr<FrameGenerator> generator_;
198 };
199
200 class TestVideoSenderWithMockEncoder : public TestVideoSender {
201 protected:
202 static const int kDefaultWidth = 1280;
203 static const int kDefaultHeight = 720;
204 static const int kNumberOfStreams = 3;
205 static const int kNumberOfLayers = 3;
206 static const int kUnusedPayloadType = 10;
207
208 void SetUp() override {
209 TestVideoSender::SetUp();
210 EXPECT_EQ(
211 0,
212 sender_->RegisterExternalEncoder(&encoder_, kUnusedPayloadType, false));
213 memset(&settings_, 0, sizeof(settings_));
214 EXPECT_EQ(0, VideoCodingModule::Codec(kVideoCodecVP8, &settings_));
215 settings_.numberOfSimulcastStreams = kNumberOfStreams;
216 ConfigureStream(kDefaultWidth / 4,
217 kDefaultHeight / 4,
218 100,
219 &settings_.simulcastStream[0]);
220 ConfigureStream(kDefaultWidth / 2,
221 kDefaultHeight / 2,
222 500,
223 &settings_.simulcastStream[1]);
224 ConfigureStream(
225 kDefaultWidth, kDefaultHeight, 1200, &settings_.simulcastStream[2]);
226 settings_.plType = kUnusedPayloadType; // Use the mocked encoder.
227 generator_.reset(
228 new EmptyFrameGenerator(settings_.width, settings_.height));
229 EXPECT_EQ(0, sender_->RegisterSendCodec(&settings_, 1, 1200));
230 }
231
232 void TearDown() override { sender_.reset(); }
233
234 void ExpectIntraRequest(int stream) {
235 if (stream == -1) {
236 // No intra request expected.
237 EXPECT_CALL(
238 encoder_,
239 Encode(_, _, Pointee(ElementsAre(kVideoFrameDelta, kVideoFrameDelta,
240 kVideoFrameDelta))))
241 .Times(1)
242 .WillRepeatedly(Return(0));
243 return;
244 }
245 assert(stream >= 0);
246 assert(stream < kNumberOfStreams);
247 std::vector<FrameType> frame_types(kNumberOfStreams, kVideoFrameDelta);
248 frame_types[stream] = kVideoFrameKey;
249 EXPECT_CALL(
250 encoder_,
251 Encode(_,
252 _,
253 Pointee(ElementsAreArray(&frame_types[0], frame_types.size()))))
254 .Times(1).WillRepeatedly(Return(0));
255 }
256
257 static void ConfigureStream(int width,
258 int height,
259 int max_bitrate,
260 SimulcastStream* stream) {
261 assert(stream);
262 stream->width = width;
263 stream->height = height;
264 stream->maxBitrate = max_bitrate;
265 stream->numberOfTemporalLayers = kNumberOfLayers;
266 stream->qpMax = 45;
267 }
268
269 VideoCodec settings_;
270 NiceMock<MockVideoEncoder> encoder_;
271 };
272
273 TEST_F(TestVideoSenderWithMockEncoder, TestIntraRequests) {
274 EXPECT_EQ(0, sender_->IntraFrameRequest(0));
275 ExpectIntraRequest(0);
276 AddFrame();
277 ExpectIntraRequest(-1);
278 AddFrame();
279
280 EXPECT_EQ(0, sender_->IntraFrameRequest(1));
281 ExpectIntraRequest(1);
282 AddFrame();
283 ExpectIntraRequest(-1);
284 AddFrame();
285
286 EXPECT_EQ(0, sender_->IntraFrameRequest(2));
287 ExpectIntraRequest(2);
288 AddFrame();
289 ExpectIntraRequest(-1);
290 AddFrame();
291
292 EXPECT_EQ(-1, sender_->IntraFrameRequest(3));
293 ExpectIntraRequest(-1);
294 AddFrame();
295
296 EXPECT_EQ(-1, sender_->IntraFrameRequest(-1));
297 ExpectIntraRequest(-1);
298 AddFrame();
299 }
300
301 TEST_F(TestVideoSenderWithMockEncoder, TestIntraRequestsInternalCapture) {
302 // De-register current external encoder.
303 EXPECT_EQ(0,
304 sender_->RegisterExternalEncoder(NULL, kUnusedPayloadType, false));
305 // Register encoder with internal capture.
306 EXPECT_EQ(
307 0, sender_->RegisterExternalEncoder(&encoder_, kUnusedPayloadType, true));
308 EXPECT_EQ(0, sender_->RegisterSendCodec(&settings_, 1, 1200));
309 ExpectIntraRequest(0);
310 EXPECT_EQ(0, sender_->IntraFrameRequest(0));
311 ExpectIntraRequest(1);
312 EXPECT_EQ(0, sender_->IntraFrameRequest(1));
313 ExpectIntraRequest(2);
314 EXPECT_EQ(0, sender_->IntraFrameRequest(2));
315 // No requests expected since these indices are out of bounds.
316 EXPECT_EQ(-1, sender_->IntraFrameRequest(3));
317 EXPECT_EQ(-1, sender_->IntraFrameRequest(-1));
318 }
319
320 TEST_F(TestVideoSenderWithMockEncoder, EncoderFramerateUpdatedViaProcess) {
321 sender_->SetChannelParameters(settings_.startBitrate * 1000, 0, 200);
322 const int64_t kRateStatsWindowMs = 2000;
323 const uint32_t kInputFps = 20;
324 int64_t start_time = clock_.TimeInMilliseconds();
325 while (clock_.TimeInMilliseconds() < start_time + kRateStatsWindowMs) {
326 AddFrame();
327 clock_.AdvanceTimeMilliseconds(1000 / kInputFps);
328 }
329 EXPECT_CALL(encoder_, SetRates(_, kInputFps)).Times(1).WillOnce(Return(0));
330 sender_->Process();
331 AddFrame();
332 }
333
334 TEST_F(TestVideoSenderWithMockEncoder,
335 NoRedundantSetChannelParameterOrSetRatesCalls) {
336 const uint8_t kLossRate = 4;
337 const uint8_t kRtt = 200;
338 const int64_t kRateStatsWindowMs = 2000;
339 const uint32_t kInputFps = 20;
340 int64_t start_time = clock_.TimeInMilliseconds();
341 // Expect initial call to SetChannelParameters. Rates are initialized through
342 // InitEncode and expects no additional call before the framerate (or bitrate)
343 // updates.
344 EXPECT_CALL(encoder_, SetChannelParameters(kLossRate, kRtt))
345 .Times(1)
346 .WillOnce(Return(0));
347 sender_->SetChannelParameters(settings_.startBitrate * 1000, kLossRate, kRtt);
348 while (clock_.TimeInMilliseconds() < start_time + kRateStatsWindowMs) {
349 AddFrame();
350 clock_.AdvanceTimeMilliseconds(1000 / kInputFps);
351 }
352 // After process, input framerate should be updated but not ChannelParameters
353 // as they are the same as before.
354 EXPECT_CALL(encoder_, SetRates(_, kInputFps)).Times(1).WillOnce(Return(0));
355 sender_->Process();
356 AddFrame();
357 // Call to SetChannelParameters with changed bitrate should call encoder
358 // SetRates but not encoder SetChannelParameters (that are unchanged).
359 EXPECT_CALL(encoder_, SetRates(2 * settings_.startBitrate, kInputFps))
360 .Times(1)
361 .WillOnce(Return(0));
362 sender_->SetChannelParameters(2 * settings_.startBitrate * 1000, kLossRate,
363 kRtt);
364 AddFrame();
365 }
366
367 class TestVideoSenderWithVp8 : public TestVideoSender {
368 public:
369 TestVideoSenderWithVp8()
370 : codec_bitrate_kbps_(300), available_bitrate_kbps_(1000) {}
371
372 void SetUp() override {
373 TestVideoSender::SetUp();
374
375 const char* input_video = "foreman_cif";
376 const int width = 352;
377 const int height = 288;
378 generator_.reset(FrameGenerator::CreateFromYuvFile(
379 std::vector<std::string>(1, test::ResourcePath(input_video, "yuv")),
380 width, height, 1));
381
382 codec_ = MakeVp8VideoCodec(width, height, 3);
383 codec_.minBitrate = 10;
384 codec_.startBitrate = codec_bitrate_kbps_;
385 codec_.maxBitrate = codec_bitrate_kbps_;
386 encoder_.reset(VP8Encoder::Create());
387 ASSERT_EQ(0, sender_->RegisterExternalEncoder(encoder_.get(), codec_.plType,
388 false));
389 EXPECT_EQ(0, sender_->RegisterSendCodec(&codec_, 1, 1200));
390 }
391
392 static VideoCodec MakeVp8VideoCodec(int width,
393 int height,
394 int temporal_layers) {
395 VideoCodec codec;
396 memset(&codec, 0, sizeof(codec));
397 EXPECT_EQ(0, VideoCodingModule::Codec(kVideoCodecVP8, &codec));
398 codec.width = width;
399 codec.height = height;
400 codec.codecSpecific.VP8.numberOfTemporalLayers = temporal_layers;
401 return codec;
402 }
403
404 void InsertFrames(float framerate, float seconds) {
405 for (int i = 0; i < seconds * framerate; ++i) {
406 clock_.AdvanceTimeMilliseconds(1000.0f / framerate);
407 EXPECT_CALL(post_encode_callback_, Encoded(_, NULL, NULL))
408 .WillOnce(Return(0));
409 AddFrame();
410 // SetChannelParameters needs to be called frequently to propagate
411 // framerate from the media optimization into the encoder.
412 // Note: SetChannelParameters fails if less than 2 frames are in the
413 // buffer since it will fail to calculate the framerate.
414 if (i != 0) {
415 EXPECT_EQ(VCM_OK, sender_->SetChannelParameters(
416 available_bitrate_kbps_ * 1000, 0, 200));
417 }
418 }
419 }
420
421 Vp8StreamInfo SimulateWithFramerate(float framerate) {
422 const float short_simulation_interval = 5.0;
423 const float long_simulation_interval = 10.0;
424 // It appears that this 5 seconds simulation is needed to allow
425 // bitrate and framerate to stabilize.
426 InsertFrames(framerate, short_simulation_interval);
427 packetization_callback_.Reset();
428
429 InsertFrames(framerate, long_simulation_interval);
430 return packetization_callback_.CalculateVp8StreamInfo();
431 }
432
433 protected:
434 VideoCodec codec_;
435 int codec_bitrate_kbps_;
436 int available_bitrate_kbps_;
437 };
438
439 TEST_F(TestVideoSenderWithVp8,
440 DISABLED_ON_IOS(DISABLED_ON_ANDROID(FixedTemporalLayersStrategy))) {
441 const int low_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][0];
442 const int mid_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][1];
443 const int high_b = codec_bitrate_kbps_ * kVp8LayerRateAlloction[2][2];
444 {
445 Vp8StreamInfo expected = {{7.5, 15.0, 30.0}, {low_b, mid_b, high_b}};
446 EXPECT_THAT(SimulateWithFramerate(30.0), MatchesVp8StreamInfo(expected));
447 }
448 {
449 Vp8StreamInfo expected = {{3.75, 7.5, 15.0}, {low_b, mid_b, high_b}};
450 EXPECT_THAT(SimulateWithFramerate(15.0), MatchesVp8StreamInfo(expected));
451 }
452 }
453
454 TEST_F(TestVideoSenderWithVp8,
455 DISABLED_ON_IOS(DISABLED_ON_ANDROID(RealTimeTemporalLayersStrategy))) {
456 Config extra_options;
457 extra_options.Set<TemporalLayers::Factory>(
458 new RealTimeTemporalLayersFactory());
459 VideoCodec codec = MakeVp8VideoCodec(352, 288, 3);
460 codec.extra_options = &extra_options;
461 codec.minBitrate = 10;
462 codec.startBitrate = codec_bitrate_kbps_;
463 codec.maxBitrate = codec_bitrate_kbps_;
464 EXPECT_EQ(0, sender_->RegisterSendCodec(&codec, 1, 1200));
465
466 const int low_b = codec_bitrate_kbps_ * 0.4;
467 const int mid_b = codec_bitrate_kbps_ * 0.6;
468 const int high_b = codec_bitrate_kbps_;
469
470 {
471 Vp8StreamInfo expected = {{7.5, 15.0, 30.0}, {low_b, mid_b, high_b}};
472 EXPECT_THAT(SimulateWithFramerate(30.0), MatchesVp8StreamInfo(expected));
473 }
474 {
475 Vp8StreamInfo expected = {{5.0, 10.0, 20.0}, {low_b, mid_b, high_b}};
476 EXPECT_THAT(SimulateWithFramerate(20.0), MatchesVp8StreamInfo(expected));
477 }
478 {
479 Vp8StreamInfo expected = {{7.5, 15.0, 15.0}, {mid_b, high_b, high_b}};
480 EXPECT_THAT(SimulateWithFramerate(15.0), MatchesVp8StreamInfo(expected));
481 }
482 {
483 Vp8StreamInfo expected = {{5.0, 10.0, 10.0}, {mid_b, high_b, high_b}};
484 EXPECT_THAT(SimulateWithFramerate(10.0), MatchesVp8StreamInfo(expected));
485 }
486 {
487 // TODO(andresp): Find out why this fails with framerate = 7.5
488 Vp8StreamInfo expected = {{7.0, 7.0, 7.0}, {high_b, high_b, high_b}};
489 EXPECT_THAT(SimulateWithFramerate(7.0), MatchesVp8StreamInfo(expected));
490 }
491 }
492 } // namespace
493 } // namespace vcm
494 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/main/source/video_sender.cc ('k') | webrtc/modules/video_coding/main/test/plotJitterEstimate.m » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698