Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / cast / receiver / video_decoder_unittest.cc
blobfad2db54f8258a952fc10619b7b7a7b8d912c901
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 <cstdlib>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/synchronization/condition_variable.h"
11 #include "base/synchronization/lock.h"
12 #include "base/time/time.h"
13 #include "media/cast/cast_config.h"
14 #include "media/cast/receiver/video_decoder.h"
15 #include "media/cast/sender/sender_encoded_frame.h"
16 #include "media/cast/sender/vp8_encoder.h"
17 #include "media/cast/test/utility/default_config.h"
18 #include "media/cast/test/utility/standalone_cast_environment.h"
19 #include "media/cast/test/utility/video_utility.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace media {
23 namespace cast {
25 namespace {
27 const int kStartingWidth = 360;
28 const int kStartingHeight = 240;
29 const int kFrameRate = 10;
31 VideoSenderConfig GetVideoSenderConfigForTest() {
32 VideoSenderConfig config = GetDefaultVideoSenderConfig();
33 config.max_frame_rate = kFrameRate;
34 return config;
37 } // namespace
39 class VideoDecoderTest : public ::testing::TestWithParam<Codec> {
40 public:
41 VideoDecoderTest()
42 : cast_environment_(new StandaloneCastEnvironment()),
43 vp8_encoder_(GetVideoSenderConfigForTest()),
44 cond_(&lock_) {
45 vp8_encoder_.Initialize();
48 virtual ~VideoDecoderTest() {
49 // Make sure all threads have stopped before the environment goes away.
50 cast_environment_->Shutdown();
53 protected:
54 void SetUp() final {
55 video_decoder_.reset(new VideoDecoder(cast_environment_, GetParam()));
56 CHECK_EQ(STATUS_INITIALIZED, video_decoder_->InitializationResult());
58 next_frame_size_ = gfx::Size(kStartingWidth, kStartingHeight);
59 next_frame_timestamp_ = base::TimeDelta();
60 last_frame_id_ = 0;
61 seen_a_decoded_frame_ = false;
63 total_video_frames_feed_in_ = 0;
64 total_video_frames_decoded_ = 0;
67 void SetNextFrameSize(const gfx::Size& size) {
68 next_frame_size_ = size;
71 // Called from the unit test thread to create another EncodedFrame and push it
72 // into the decoding pipeline.
73 void FeedMoreVideo(int num_dropped_frames) {
74 // Prepare a simulated EncodedFrame to feed into the VideoDecoder.
76 const scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateFrame(
77 PIXEL_FORMAT_YV12, next_frame_size_, gfx::Rect(next_frame_size_),
78 next_frame_size_, next_frame_timestamp_);
79 const base::TimeTicks reference_time =
80 base::TimeTicks::UnixEpoch() + next_frame_timestamp_;
81 next_frame_timestamp_ += base::TimeDelta::FromSeconds(1) / kFrameRate;
82 PopulateVideoFrame(video_frame.get(), 0);
84 // Encode |frame| into |encoded_frame->data|.
85 scoped_ptr<SenderEncodedFrame> encoded_frame(new SenderEncodedFrame());
86 // Test only supports VP8, currently.
87 CHECK_EQ(CODEC_VIDEO_VP8, GetParam());
88 vp8_encoder_.Encode(video_frame, reference_time, encoded_frame.get());
89 // Rewrite frame IDs for testing purposes.
90 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames;
91 if (encoded_frame->dependency == EncodedFrame::KEY)
92 encoded_frame->referenced_frame_id = encoded_frame->frame_id;
93 else
94 encoded_frame->referenced_frame_id = encoded_frame->frame_id - 1;
95 last_frame_id_ = encoded_frame->frame_id;
96 ASSERT_EQ(reference_time, encoded_frame->reference_time);
99 base::AutoLock auto_lock(lock_);
100 ++total_video_frames_feed_in_;
103 cast_environment_->PostTask(
104 CastEnvironment::MAIN,
105 FROM_HERE,
106 base::Bind(&VideoDecoder::DecodeFrame,
107 base::Unretained(video_decoder_.get()),
108 base::Passed(&encoded_frame),
109 base::Bind(&VideoDecoderTest::OnDecodedFrame,
110 base::Unretained(this),
111 video_frame,
112 num_dropped_frames == 0)));
115 // Blocks the caller until all video that has been feed in has been decoded.
116 void WaitForAllVideoToBeDecoded() {
117 DCHECK(!cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
118 base::AutoLock auto_lock(lock_);
119 while (total_video_frames_decoded_ < total_video_frames_feed_in_)
120 cond_.Wait();
121 EXPECT_EQ(total_video_frames_feed_in_, total_video_frames_decoded_);
124 private:
125 // Called by |vp8_decoder_| to deliver each frame of decoded video.
126 void OnDecodedFrame(const scoped_refptr<VideoFrame>& expected_video_frame,
127 bool should_be_continuous,
128 const scoped_refptr<VideoFrame>& video_frame,
129 bool is_continuous) {
130 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
132 // A NULL |video_frame| indicates a decode error, which we don't expect.
133 ASSERT_TRUE(video_frame.get());
135 // Did the decoder detect whether frames were dropped?
136 EXPECT_EQ(should_be_continuous, is_continuous);
138 // Does the video data seem to be intact?
139 EXPECT_EQ(expected_video_frame->coded_size().width(),
140 video_frame->coded_size().width());
141 EXPECT_EQ(expected_video_frame->coded_size().height(),
142 video_frame->coded_size().height());
143 EXPECT_LT(40.0, I420PSNR(expected_video_frame, video_frame));
144 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here.
146 // Signal the main test thread that more video was decoded.
147 base::AutoLock auto_lock(lock_);
148 ++total_video_frames_decoded_;
149 cond_.Signal();
152 const scoped_refptr<StandaloneCastEnvironment> cast_environment_;
153 scoped_ptr<VideoDecoder> video_decoder_;
154 gfx::Size next_frame_size_;
155 base::TimeDelta next_frame_timestamp_;
156 uint32 last_frame_id_;
157 bool seen_a_decoded_frame_;
159 Vp8Encoder vp8_encoder_;
161 base::Lock lock_;
162 base::ConditionVariable cond_;
163 int total_video_frames_feed_in_;
164 int total_video_frames_decoded_;
166 DISALLOW_COPY_AND_ASSIGN(VideoDecoderTest);
169 TEST_P(VideoDecoderTest, DecodesFrames) {
170 const int kNumFrames = 10;
171 for (int i = 0; i < kNumFrames; ++i)
172 FeedMoreVideo(0);
173 WaitForAllVideoToBeDecoded();
176 TEST_P(VideoDecoderTest, RecoversFromDroppedFrames) {
177 const int kNumFrames = 100;
178 int next_drop_at = 3;
179 int next_num_dropped = 1;
180 for (int i = 0; i < kNumFrames; ++i) {
181 if (i == next_drop_at) {
182 const int num_dropped = next_num_dropped++;
183 next_drop_at *= 2;
184 i += num_dropped;
185 FeedMoreVideo(num_dropped);
186 } else {
187 FeedMoreVideo(0);
190 WaitForAllVideoToBeDecoded();
193 TEST_P(VideoDecoderTest, DecodesFramesOfVaryingSizes) {
194 std::vector<gfx::Size> frame_sizes;
195 frame_sizes.push_back(gfx::Size(1280, 720));
196 frame_sizes.push_back(gfx::Size(640, 360)); // Shrink both dimensions.
197 frame_sizes.push_back(gfx::Size(300, 200)); // Shrink both dimensions again.
198 frame_sizes.push_back(gfx::Size(200, 300)); // Same area.
199 frame_sizes.push_back(gfx::Size(600, 400)); // Grow both dimensions.
200 frame_sizes.push_back(gfx::Size(638, 400)); // Shrink only one dimension.
201 frame_sizes.push_back(gfx::Size(638, 398)); // Shrink the other dimension.
202 frame_sizes.push_back(gfx::Size(320, 180)); // Shrink both dimensions again.
203 frame_sizes.push_back(gfx::Size(322, 180)); // Grow only one dimension.
204 frame_sizes.push_back(gfx::Size(322, 182)); // Grow the other dimension.
205 frame_sizes.push_back(gfx::Size(1920, 1080)); // Grow both dimensions again.
207 // Encode one frame at each size.
208 for (const auto& frame_size : frame_sizes) {
209 SetNextFrameSize(frame_size);
210 FeedMoreVideo(0);
213 // Encode 10 frames at each size.
214 for (const auto& frame_size : frame_sizes) {
215 SetNextFrameSize(frame_size);
216 const int kNumFrames = 10;
217 for (int i = 0; i < kNumFrames; ++i)
218 FeedMoreVideo(0);
221 WaitForAllVideoToBeDecoded();
224 INSTANTIATE_TEST_CASE_P(,
225 VideoDecoderTest,
226 ::testing::Values(CODEC_VIDEO_VP8));
228 } // namespace cast
229 } // namespace media