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.
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/vp8_encoder.h"
16 #include "media/cast/test/utility/default_config.h"
17 #include "media/cast/test/utility/standalone_cast_environment.h"
18 #include "media/cast/test/utility/video_utility.h"
19 #include "testing/gtest/include/gtest/gtest.h"
26 const int kStartingWidth
= 360;
27 const int kStartingHeight
= 240;
28 const int kFrameRate
= 10;
30 VideoSenderConfig
GetVideoSenderConfigForTest() {
31 VideoSenderConfig config
= GetDefaultVideoSenderConfig();
32 config
.max_frame_rate
= kFrameRate
;
38 class VideoDecoderTest
: public ::testing::TestWithParam
<Codec
> {
41 : cast_environment_(new StandaloneCastEnvironment()),
42 vp8_encoder_(GetVideoSenderConfigForTest()),
44 vp8_encoder_
.Initialize();
47 virtual ~VideoDecoderTest() {
48 // Make sure all threads have stopped before the environment goes away.
49 cast_environment_
->Shutdown();
54 video_decoder_
.reset(new VideoDecoder(cast_environment_
, GetParam()));
55 CHECK_EQ(STATUS_INITIALIZED
, video_decoder_
->InitializationResult());
57 next_frame_size_
= gfx::Size(kStartingWidth
, kStartingHeight
);
58 next_frame_timestamp_
= base::TimeDelta();
60 seen_a_decoded_frame_
= false;
62 total_video_frames_feed_in_
= 0;
63 total_video_frames_decoded_
= 0;
66 void SetNextFrameSize(const gfx::Size
& size
) {
67 next_frame_size_
= size
;
70 // Called from the unit test thread to create another EncodedFrame and push it
71 // into the decoding pipeline.
72 void FeedMoreVideo(int num_dropped_frames
) {
73 // Prepare a simulated EncodedFrame to feed into the VideoDecoder.
75 const scoped_refptr
<VideoFrame
> video_frame
=
76 VideoFrame::CreateFrame(VideoFrame::YV12
,
78 gfx::Rect(next_frame_size_
),
80 next_frame_timestamp_
);
81 const base::TimeTicks reference_time
=
82 base::TimeTicks::UnixEpoch() + next_frame_timestamp_
;
83 next_frame_timestamp_
+= base::TimeDelta::FromSeconds(1) / kFrameRate
;
84 PopulateVideoFrame(video_frame
.get(), 0);
86 // Encode |frame| into |encoded_frame->data|.
87 scoped_ptr
<EncodedFrame
> encoded_frame(new EncodedFrame());
88 // Test only supports VP8, currently.
89 CHECK_EQ(CODEC_VIDEO_VP8
, GetParam());
90 vp8_encoder_
.Encode(video_frame
, reference_time
, encoded_frame
.get());
91 // Rewrite frame IDs for testing purposes.
92 encoded_frame
->frame_id
= last_frame_id_
+ 1 + num_dropped_frames
;
93 if (encoded_frame
->dependency
== EncodedFrame::KEY
)
94 encoded_frame
->referenced_frame_id
= encoded_frame
->frame_id
;
96 encoded_frame
->referenced_frame_id
= encoded_frame
->frame_id
- 1;
97 last_frame_id_
= encoded_frame
->frame_id
;
98 ASSERT_EQ(reference_time
, encoded_frame
->reference_time
);
101 base::AutoLock
auto_lock(lock_
);
102 ++total_video_frames_feed_in_
;
105 cast_environment_
->PostTask(
106 CastEnvironment::MAIN
,
108 base::Bind(&VideoDecoder::DecodeFrame
,
109 base::Unretained(video_decoder_
.get()),
110 base::Passed(&encoded_frame
),
111 base::Bind(&VideoDecoderTest::OnDecodedFrame
,
112 base::Unretained(this),
114 num_dropped_frames
== 0)));
117 // Blocks the caller until all video that has been feed in has been decoded.
118 void WaitForAllVideoToBeDecoded() {
119 DCHECK(!cast_environment_
->CurrentlyOn(CastEnvironment::MAIN
));
120 base::AutoLock
auto_lock(lock_
);
121 while (total_video_frames_decoded_
< total_video_frames_feed_in_
)
123 EXPECT_EQ(total_video_frames_feed_in_
, total_video_frames_decoded_
);
127 // Called by |vp8_decoder_| to deliver each frame of decoded video.
128 void OnDecodedFrame(const scoped_refptr
<VideoFrame
>& expected_video_frame
,
129 bool should_be_continuous
,
130 const scoped_refptr
<VideoFrame
>& video_frame
,
131 bool is_continuous
) {
132 DCHECK(cast_environment_
->CurrentlyOn(CastEnvironment::MAIN
));
134 // A NULL |video_frame| indicates a decode error, which we don't expect.
135 ASSERT_FALSE(!video_frame
.get());
137 // Did the decoder detect whether frames were dropped?
138 EXPECT_EQ(should_be_continuous
, is_continuous
);
140 // Does the video data seem to be intact?
141 EXPECT_EQ(expected_video_frame
->coded_size().width(),
142 video_frame
->coded_size().width());
143 EXPECT_EQ(expected_video_frame
->coded_size().height(),
144 video_frame
->coded_size().height());
145 EXPECT_LT(40.0, I420PSNR(expected_video_frame
, video_frame
));
146 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here.
148 // Signal the main test thread that more video was decoded.
149 base::AutoLock
auto_lock(lock_
);
150 ++total_video_frames_decoded_
;
154 const scoped_refptr
<StandaloneCastEnvironment
> cast_environment_
;
155 scoped_ptr
<VideoDecoder
> video_decoder_
;
156 gfx::Size next_frame_size_
;
157 base::TimeDelta next_frame_timestamp_
;
158 uint32 last_frame_id_
;
159 bool seen_a_decoded_frame_
;
161 Vp8Encoder vp8_encoder_
;
164 base::ConditionVariable cond_
;
165 int total_video_frames_feed_in_
;
166 int total_video_frames_decoded_
;
168 DISALLOW_COPY_AND_ASSIGN(VideoDecoderTest
);
171 TEST_P(VideoDecoderTest
, DecodesFrames
) {
172 const int kNumFrames
= 10;
173 for (int i
= 0; i
< kNumFrames
; ++i
)
175 WaitForAllVideoToBeDecoded();
178 TEST_P(VideoDecoderTest
, RecoversFromDroppedFrames
) {
179 const int kNumFrames
= 100;
180 int next_drop_at
= 3;
181 int next_num_dropped
= 1;
182 for (int i
= 0; i
< kNumFrames
; ++i
) {
183 if (i
== next_drop_at
) {
184 const int num_dropped
= next_num_dropped
++;
187 FeedMoreVideo(num_dropped
);
192 WaitForAllVideoToBeDecoded();
195 TEST_P(VideoDecoderTest
, DecodesFramesOfVaryingSizes
) {
196 std::vector
<gfx::Size
> frame_sizes
;
197 frame_sizes
.push_back(gfx::Size(1280, 720));
198 frame_sizes
.push_back(gfx::Size(640, 360)); // Shrink both dimensions.
199 frame_sizes
.push_back(gfx::Size(300, 200)); // Shrink both dimensions again.
200 frame_sizes
.push_back(gfx::Size(200, 300)); // Same area.
201 frame_sizes
.push_back(gfx::Size(600, 400)); // Grow both dimensions.
202 frame_sizes
.push_back(gfx::Size(638, 400)); // Shrink only one dimension.
203 frame_sizes
.push_back(gfx::Size(638, 398)); // Shrink the other dimension.
204 frame_sizes
.push_back(gfx::Size(320, 180)); // Shrink both dimensions again.
205 frame_sizes
.push_back(gfx::Size(322, 180)); // Grow only one dimension.
206 frame_sizes
.push_back(gfx::Size(322, 182)); // Grow the other dimension.
207 frame_sizes
.push_back(gfx::Size(1920, 1080)); // Grow both dimensions again.
209 // Encode one frame at each size.
210 for (const auto& frame_size
: frame_sizes
) {
211 SetNextFrameSize(frame_size
);
215 // Encode 10 frames at each size.
216 for (const auto& frame_size
: frame_sizes
) {
217 SetNextFrameSize(frame_size
);
218 const int kNumFrames
= 10;
219 for (int i
= 0; i
< kNumFrames
; ++i
)
223 WaitForAllVideoToBeDecoded();
226 INSTANTIATE_TEST_CASE_P(,
228 ::testing::Values(CODEC_VIDEO_VP8
));