Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / media / cast / sender / video_encoder_unittest.cc
bloba528e287cf0a5e787076c42b99744d4ae997e083
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <vector>
6 #include <utility>
8 #include "base/bind.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "media/base/video_frame.h"
12 #include "media/cast/cast_defines.h"
13 #include "media/cast/cast_environment.h"
14 #include "media/cast/sender/fake_video_encode_accelerator_factory.h"
15 #include "media/cast/sender/video_frame_factory.h"
16 #include "media/cast/sender/video_encoder.h"
17 #include "media/cast/test/fake_single_thread_task_runner.h"
18 #include "media/cast/test/utility/default_config.h"
19 #include "media/cast/test/utility/video_utility.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_MACOSX)
23 #include "media/cast/sender/h264_vt_encoder.h"
24 #endif
26 namespace media {
27 namespace cast {
29 class VideoEncoderTest
30 : public ::testing::TestWithParam<std::pair<Codec, bool>> {
31 protected:
32 VideoEncoderTest()
33 : testing_clock_(new base::SimpleTestTickClock()),
34 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
35 cast_environment_(new CastEnvironment(
36 scoped_ptr<base::TickClock>(testing_clock_).Pass(),
37 task_runner_,
38 task_runner_,
39 task_runner_)),
40 video_config_(GetDefaultVideoSenderConfig()),
41 operational_status_(STATUS_UNINITIALIZED),
42 count_frames_delivered_(0) {
43 testing_clock_->Advance(base::TimeTicks::Now() - base::TimeTicks());
44 first_frame_time_ = testing_clock_->NowTicks();
47 ~VideoEncoderTest() override {}
49 void SetUp() override {
50 video_config_.codec = GetParam().first;
51 video_config_.use_external_encoder = GetParam().second;
53 if (video_config_.use_external_encoder)
54 vea_factory_.reset(new FakeVideoEncodeAcceleratorFactory(task_runner_));
57 void TearDown() override {
58 video_encoder_.reset();
59 RunTasksAndAdvanceClock();
62 void CreateEncoder(bool three_buffer_mode) {
63 ASSERT_EQ(STATUS_UNINITIALIZED, operational_status_);
64 video_config_.max_number_of_video_buffers_used =
65 (three_buffer_mode ? 3 : 1);
66 video_encoder_ = VideoEncoder::Create(
67 cast_environment_,
68 video_config_,
69 base::Bind(&VideoEncoderTest::OnOperationalStatusChange,
70 base::Unretained(this)),
71 base::Bind(
72 &FakeVideoEncodeAcceleratorFactory::CreateVideoEncodeAccelerator,
73 base::Unretained(vea_factory_.get())),
74 base::Bind(&FakeVideoEncodeAcceleratorFactory::CreateSharedMemory,
75 base::Unretained(vea_factory_.get()))).Pass();
76 RunTasksAndAdvanceClock();
77 if (is_encoder_present())
78 ASSERT_EQ(STATUS_INITIALIZED, operational_status_);
81 bool is_encoder_present() const {
82 return !!video_encoder_;
85 bool is_testing_software_vp8_encoder() const {
86 return video_config_.codec == CODEC_VIDEO_VP8 &&
87 !video_config_.use_external_encoder;
90 bool is_testing_video_toolbox_encoder() const {
91 return
92 #if defined(OS_MACOSX)
93 (!video_config_.use_external_encoder &&
94 H264VideoToolboxEncoder::IsSupported(video_config_)) ||
95 #endif
96 false;
99 bool is_testing_platform_encoder() const {
100 return video_config_.use_external_encoder ||
101 is_testing_video_toolbox_encoder();
104 VideoEncoder* video_encoder() const {
105 return video_encoder_.get();
108 void DestroyEncoder() {
109 video_encoder_.reset();
112 base::TimeTicks Now() const {
113 return testing_clock_->NowTicks();
116 void RunTasksAndAdvanceClock() const {
117 const base::TimeDelta frame_duration = base::TimeDelta::FromMicroseconds(
118 1000000.0 / video_config_.max_frame_rate);
119 #if defined(OS_MACOSX)
120 if (is_testing_video_toolbox_encoder()) {
121 // The H264VideoToolboxEncoder (on MAC_OSX and IOS) is not a faked
122 // implementation in these tests, and performs its encoding asynchronously
123 // on an unknown set of threads. Therefore, sleep the current thread for
124 // the real amount of time to avoid excessively spinning the CPU while
125 // waiting for something to happen.
126 base::PlatformThread::Sleep(frame_duration);
128 #endif
129 task_runner_->RunTasks();
130 testing_clock_->Advance(frame_duration);
133 int count_frames_delivered() const {
134 return count_frames_delivered_;
137 void WaitForAllFramesToBeDelivered(int total_expected) const {
138 video_encoder_->EmitFrames();
139 while (count_frames_delivered_ < total_expected)
140 RunTasksAndAdvanceClock();
143 // Creates a new VideoFrame of the given |size|, filled with a test pattern.
144 // When available, it attempts to use the VideoFrameFactory provided by the
145 // encoder.
146 scoped_refptr<media::VideoFrame> CreateTestVideoFrame(const gfx::Size& size) {
147 const base::TimeDelta timestamp =
148 testing_clock_->NowTicks() - first_frame_time_;
149 scoped_refptr<media::VideoFrame> frame;
150 if (video_frame_factory_)
151 frame = video_frame_factory_->MaybeCreateFrame(size, timestamp);
152 if (!frame) {
153 frame = media::VideoFrame::CreateFrame(
154 VideoFrame::I420, size, gfx::Rect(size), size, timestamp);
156 PopulateVideoFrame(frame.get(), 123);
157 return frame;
160 // Requests encoding the |video_frame| and has the resulting frame delivered
161 // via a callback that checks for expected results. Returns false if the
162 // encoder rejected the request.
163 bool EncodeAndCheckDelivery(
164 const scoped_refptr<media::VideoFrame>& video_frame,
165 uint32 frame_id,
166 uint32 reference_frame_id) {
167 return video_encoder_->EncodeVideoFrame(
168 video_frame,
169 Now(),
170 base::Bind(&VideoEncoderTest::DeliverEncodedVideoFrame,
171 base::Unretained(this),
172 frame_id,
173 reference_frame_id,
174 TimeDeltaToRtpDelta(video_frame->timestamp(),
175 kVideoFrequency),
176 Now()));
179 // If the implementation of |video_encoder_| is ExternalVideoEncoder, check
180 // that the VEA factory has responded (by running the callbacks) a specific
181 // number of times. Otherwise, check that the VEA factory is inactive.
182 void ExpectVEAResponsesForExternalVideoEncoder(
183 int vea_response_count,
184 int shm_response_count) const {
185 if (!vea_factory_)
186 return;
187 EXPECT_EQ(vea_response_count, vea_factory_->vea_response_count());
188 EXPECT_EQ(shm_response_count, vea_factory_->shm_response_count());
191 void SetVEAFactoryAutoRespond(bool auto_respond) {
192 if (vea_factory_)
193 vea_factory_->SetAutoRespond(auto_respond);
196 private:
197 void OnOperationalStatusChange(OperationalStatus status) {
198 DVLOG(1) << "OnOperationalStatusChange: from " << operational_status_
199 << " to " << status;
200 operational_status_ = status;
202 EXPECT_TRUE(operational_status_ == STATUS_CODEC_REINIT_PENDING ||
203 operational_status_ == STATUS_INITIALIZED);
205 // Create the VideoFrameFactory the first time status changes to
206 // STATUS_INITIALIZED.
207 if (operational_status_ == STATUS_INITIALIZED && !video_frame_factory_)
208 video_frame_factory_ = video_encoder_->CreateVideoFrameFactory().Pass();
211 // Checks that |encoded_frame| matches expected values. This is the method
212 // bound in the callback returned from EncodeAndCheckDelivery().
213 void DeliverEncodedVideoFrame(
214 uint32 expected_frame_id,
215 uint32 expected_last_referenced_frame_id,
216 uint32 expected_rtp_timestamp,
217 const base::TimeTicks& expected_reference_time,
218 scoped_ptr<EncodedFrame> encoded_frame) {
219 EXPECT_TRUE(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
221 EXPECT_EQ(expected_frame_id, encoded_frame->frame_id);
222 EXPECT_EQ(expected_rtp_timestamp, encoded_frame->rtp_timestamp);
223 EXPECT_EQ(expected_reference_time, encoded_frame->reference_time);
225 // The platform encoders are "black boxes" and may choose to vend key frames
226 // and/or empty data at any time. The software encoders, however, should
227 // strictly adhere to expected behavior.
228 if (is_testing_platform_encoder()) {
229 const bool expected_key_frame =
230 expected_frame_id == expected_last_referenced_frame_id;
231 const bool have_key_frame =
232 encoded_frame->dependency == EncodedFrame::KEY;
233 EXPECT_EQ(have_key_frame,
234 encoded_frame->frame_id == encoded_frame->referenced_frame_id);
235 LOG_IF(WARNING, expected_key_frame != have_key_frame)
236 << "Platform encoder chose to emit a "
237 << (have_key_frame ? "key" : "delta")
238 << " frame instead of the expected kind @ frame_id="
239 << encoded_frame->frame_id;
240 LOG_IF(WARNING, encoded_frame->data.empty())
241 << "Platform encoder returned an empty frame @ frame_id="
242 << encoded_frame->frame_id;
243 } else {
244 if (expected_frame_id != expected_last_referenced_frame_id) {
245 EXPECT_EQ(EncodedFrame::DEPENDENT, encoded_frame->dependency);
246 } else if (video_config_.max_number_of_video_buffers_used == 1) {
247 EXPECT_EQ(EncodedFrame::KEY, encoded_frame->dependency);
249 EXPECT_EQ(expected_last_referenced_frame_id,
250 encoded_frame->referenced_frame_id);
251 EXPECT_FALSE(encoded_frame->data.empty());
254 ++count_frames_delivered_;
257 base::SimpleTestTickClock* const testing_clock_; // Owned by CastEnvironment.
258 const scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
259 const scoped_refptr<CastEnvironment> cast_environment_;
260 VideoSenderConfig video_config_;
261 scoped_ptr<FakeVideoEncodeAcceleratorFactory> vea_factory_;
262 base::TimeTicks first_frame_time_;
263 OperationalStatus operational_status_;
264 scoped_ptr<VideoEncoder> video_encoder_;
265 scoped_ptr<VideoFrameFactory> video_frame_factory_;
267 int count_frames_delivered_;
269 DISALLOW_COPY_AND_ASSIGN(VideoEncoderTest);
272 // A simple test to encode ten frames of video, expecting to see one key frame
273 // followed by nine delta frames.
274 TEST_P(VideoEncoderTest, GeneratesKeyFrameThenOnlyDeltaFrames) {
275 CreateEncoder(false);
276 SetVEAFactoryAutoRespond(true);
278 EXPECT_EQ(0, count_frames_delivered());
279 ExpectVEAResponsesForExternalVideoEncoder(0, 0);
281 uint32 frame_id = 0;
282 uint32 reference_frame_id = 0;
283 const gfx::Size frame_size(1280, 720);
285 // For the platform encoders, the first one or more frames is dropped while
286 // the encoder initializes. Then, for all encoders, expect one key frame is
287 // delivered.
288 bool accepted_first_frame = false;
289 do {
290 accepted_first_frame = EncodeAndCheckDelivery(
291 CreateTestVideoFrame(frame_size), frame_id, reference_frame_id);
292 if (!is_testing_platform_encoder())
293 EXPECT_TRUE(accepted_first_frame);
294 RunTasksAndAdvanceClock();
295 } while (!accepted_first_frame);
296 ExpectVEAResponsesForExternalVideoEncoder(1, 3);
298 // Expect the remaining frames are encoded as delta frames.
299 for (++frame_id; frame_id < 10; ++frame_id, ++reference_frame_id) {
300 EXPECT_TRUE(EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size),
301 frame_id,
302 reference_frame_id));
303 RunTasksAndAdvanceClock();
306 WaitForAllFramesToBeDelivered(10);
307 ExpectVEAResponsesForExternalVideoEncoder(1, 3);
310 // Tests basic frame dependency rules when using the VP8 encoder in multi-buffer
311 // mode.
312 TEST_P(VideoEncoderTest, FramesDoNotDependOnUnackedFramesInMultiBufferMode) {
313 if (!is_testing_software_vp8_encoder())
314 return; // Only test multibuffer mode for the software VP8 encoder.
315 CreateEncoder(true);
317 EXPECT_EQ(0, count_frames_delivered());
319 const gfx::Size frame_size(1280, 720);
320 EXPECT_TRUE(EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size), 0, 0));
321 RunTasksAndAdvanceClock();
323 video_encoder()->LatestFrameIdToReference(0);
324 EXPECT_TRUE(EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size), 1, 0));
325 RunTasksAndAdvanceClock();
327 video_encoder()->LatestFrameIdToReference(1);
328 EXPECT_TRUE(EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size), 2, 1));
329 RunTasksAndAdvanceClock();
331 video_encoder()->LatestFrameIdToReference(2);
333 for (uint32 frame_id = 3; frame_id < 10; ++frame_id) {
334 EXPECT_TRUE(EncodeAndCheckDelivery(
335 CreateTestVideoFrame(frame_size), frame_id, 2));
336 RunTasksAndAdvanceClock();
339 EXPECT_EQ(10, count_frames_delivered());
342 // Tests that the encoder continues to output EncodedFrames as the frame size
343 // changes. See media/cast/receiver/video_decoder_unittest.cc for a complete
344 // encode/decode cycle of varied frame sizes that actually checks the frame
345 // content.
346 TEST_P(VideoEncoderTest, EncodesVariedFrameSizes) {
347 CreateEncoder(false);
348 SetVEAFactoryAutoRespond(true);
350 EXPECT_EQ(0, count_frames_delivered());
351 ExpectVEAResponsesForExternalVideoEncoder(0, 0);
353 std::vector<gfx::Size> frame_sizes;
354 frame_sizes.push_back(gfx::Size(1280, 720));
355 frame_sizes.push_back(gfx::Size(640, 360)); // Shrink both dimensions.
356 frame_sizes.push_back(gfx::Size(300, 200)); // Shrink both dimensions again.
357 frame_sizes.push_back(gfx::Size(200, 300)); // Same area.
358 frame_sizes.push_back(gfx::Size(600, 400)); // Grow both dimensions.
359 frame_sizes.push_back(gfx::Size(638, 400)); // Shrink only one dimension.
360 frame_sizes.push_back(gfx::Size(638, 398)); // Shrink the other dimension.
361 frame_sizes.push_back(gfx::Size(320, 180)); // Shrink both dimensions again.
362 frame_sizes.push_back(gfx::Size(322, 180)); // Grow only one dimension.
363 frame_sizes.push_back(gfx::Size(322, 182)); // Grow the other dimension.
364 frame_sizes.push_back(gfx::Size(1920, 1080)); // Grow both dimensions again.
366 uint32 frame_id = 0;
368 // Encode one frame at each size. For the platform encoders, expect no frames
369 // to be delivered since each frame size change will sprun re-initialization
370 // of the underlying encoder. Otherwise, expect all key frames to come out.
371 for (const auto& frame_size : frame_sizes) {
372 EXPECT_EQ(!is_testing_platform_encoder(),
373 EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size),
374 frame_id,
375 frame_id));
376 RunTasksAndAdvanceClock();
377 if (!is_testing_platform_encoder())
378 ++frame_id;
381 // Encode 10+ frames at each size. For the platform decoders, expect the
382 // first one or more frames are dropped while the encoder re-inits. Then, for
383 // all encoders, expect one key frame followed by all delta frames.
384 for (const auto& frame_size : frame_sizes) {
385 bool accepted_first_frame = false;
386 do {
387 accepted_first_frame = EncodeAndCheckDelivery(
388 CreateTestVideoFrame(frame_size), frame_id, frame_id);
389 if (!is_testing_platform_encoder())
390 EXPECT_TRUE(accepted_first_frame);
391 RunTasksAndAdvanceClock();
392 } while (!accepted_first_frame);
393 ++frame_id;
394 for (int i = 1; i < 10; ++i, ++frame_id) {
395 EXPECT_TRUE(EncodeAndCheckDelivery(CreateTestVideoFrame(frame_size),
396 frame_id,
397 frame_id - 1));
398 RunTasksAndAdvanceClock();
402 WaitForAllFramesToBeDelivered(10 * frame_sizes.size());
403 ExpectVEAResponsesForExternalVideoEncoder(
404 2 * frame_sizes.size(), 6 * frame_sizes.size());
407 // Verify that everything goes well even if ExternalVideoEncoder is destroyed
408 // before it has a chance to receive the VEA creation callback. For all other
409 // encoders, this tests that the encoder can be safely destroyed before the task
410 // is run that delivers the first EncodedFrame.
411 TEST_P(VideoEncoderTest, CanBeDestroyedBeforeVEAIsCreated) {
412 CreateEncoder(false);
414 // Send a frame to spawn creation of the ExternalVideoEncoder instance.
415 EncodeAndCheckDelivery(CreateTestVideoFrame(gfx::Size(1280, 720)), 0, 0);
417 // Destroy the encoder, and confirm the VEA Factory did not respond yet.
418 DestroyEncoder();
419 ExpectVEAResponsesForExternalVideoEncoder(0, 0);
421 // Allow the VEA Factory to respond by running the creation callback. When
422 // the task runs, it will be a no-op since the weak pointers to the
423 // ExternalVideoEncoder were invalidated.
424 SetVEAFactoryAutoRespond(true);
425 RunTasksAndAdvanceClock();
426 ExpectVEAResponsesForExternalVideoEncoder(1, 0);
429 namespace {
430 std::vector<std::pair<Codec, bool>> DetermineEncodersToTest() {
431 std::vector<std::pair<Codec, bool>> values;
432 // Fake encoder.
433 values.push_back(std::make_pair(CODEC_VIDEO_FAKE, false));
434 // Software VP8 encoder.
435 values.push_back(std::make_pair(CODEC_VIDEO_VP8, false));
436 // Hardware-accelerated encoder (faked).
437 values.push_back(std::make_pair(CODEC_VIDEO_VP8, true));
438 #if defined(OS_MACOSX)
439 // VideoToolbox encoder (when VideoToolbox is present).
440 VideoSenderConfig video_config = GetDefaultVideoSenderConfig();
441 video_config.use_external_encoder = false;
442 video_config.codec = CODEC_VIDEO_H264;
443 if (H264VideoToolboxEncoder::IsSupported(video_config))
444 values.push_back(std::make_pair(CODEC_VIDEO_H264, false));
445 #endif
446 return values;
448 } // namespace
450 INSTANTIATE_TEST_CASE_P(
451 , VideoEncoderTest, ::testing::ValuesIn(DetermineEncodersToTest()));
453 } // namespace cast
454 } // namespace media