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.
8 #include "base/bind_helpers.h"
9 #include "base/synchronization/condition_variable.h"
10 #include "base/synchronization/lock.h"
11 #include "base/time/time.h"
12 #include "media/cast/cast_config.h"
13 #include "media/cast/receiver/video_decoder.h"
14 #include "media/cast/sender/vp8_encoder.h"
15 #include "media/cast/test/utility/standalone_cast_environment.h"
16 #include "media/cast/test/utility/video_utility.h"
17 #include "testing/gtest/include/gtest/gtest.h"
24 const int kWidth
= 360;
25 const int kHeight
= 240;
26 const int kFrameRate
= 10;
28 VideoSenderConfig
GetVideoSenderConfigForTest() {
29 VideoSenderConfig config
;
30 config
.width
= kWidth
;
31 config
.height
= kHeight
;
32 config
.max_frame_rate
= kFrameRate
;
38 class VideoDecoderTest
: public ::testing::TestWithParam
<Codec
> {
41 : cast_environment_(new StandaloneCastEnvironment()),
42 vp8_encoder_(GetVideoSenderConfigForTest(), 0),
44 vp8_encoder_
.Initialize();
47 virtual ~VideoDecoderTest() {
48 // Make sure all threads have stopped before the environment goes away.
49 cast_environment_
->Shutdown();
53 virtual void SetUp() OVERRIDE
{
54 video_decoder_
.reset(new VideoDecoder(cast_environment_
, GetParam()));
55 CHECK_EQ(STATUS_VIDEO_INITIALIZED
, video_decoder_
->InitializationResult());
57 next_frame_timestamp_
= base::TimeDelta();
59 seen_a_decoded_frame_
= false;
61 total_video_frames_feed_in_
= 0;
62 total_video_frames_decoded_
= 0;
65 // Called from the unit test thread to create another EncodedFrame and push it
66 // into the decoding pipeline.
67 void FeedMoreVideo(int num_dropped_frames
) {
68 // Prepare a simulated EncodedFrame to feed into the VideoDecoder.
70 const gfx::Size
frame_size(kWidth
, kHeight
);
71 const scoped_refptr
<VideoFrame
> video_frame
=
72 VideoFrame::CreateFrame(VideoFrame::YV12
,
74 gfx::Rect(frame_size
),
76 next_frame_timestamp_
);
77 next_frame_timestamp_
+= base::TimeDelta::FromSeconds(1) / kFrameRate
;
78 PopulateVideoFrame(video_frame
.get(), 0);
80 // Encode |frame| into |encoded_frame->data|.
81 scoped_ptr
<EncodedFrame
> encoded_frame(
83 // Test only supports VP8, currently.
84 CHECK_EQ(CODEC_VIDEO_VP8
, GetParam());
85 vp8_encoder_
.Encode(video_frame
, encoded_frame
.get());
86 encoded_frame
->frame_id
= last_frame_id_
+ 1 + num_dropped_frames
;
87 last_frame_id_
= encoded_frame
->frame_id
;
90 base::AutoLock
auto_lock(lock_
);
91 ++total_video_frames_feed_in_
;
94 cast_environment_
->PostTask(
95 CastEnvironment::MAIN
,
97 base::Bind(&VideoDecoder::DecodeFrame
,
98 base::Unretained(video_decoder_
.get()),
99 base::Passed(&encoded_frame
),
100 base::Bind(&VideoDecoderTest::OnDecodedFrame
,
101 base::Unretained(this),
103 num_dropped_frames
== 0)));
106 // Blocks the caller until all video that has been feed in has been decoded.
107 void WaitForAllVideoToBeDecoded() {
108 DCHECK(!cast_environment_
->CurrentlyOn(CastEnvironment::MAIN
));
109 base::AutoLock
auto_lock(lock_
);
110 while (total_video_frames_decoded_
< total_video_frames_feed_in_
)
112 EXPECT_EQ(total_video_frames_feed_in_
, total_video_frames_decoded_
);
116 // Called by |vp8_decoder_| to deliver each frame of decoded video.
117 void OnDecodedFrame(const scoped_refptr
<VideoFrame
>& expected_video_frame
,
118 bool should_be_continuous
,
119 const scoped_refptr
<VideoFrame
>& video_frame
,
120 bool is_continuous
) {
121 DCHECK(cast_environment_
->CurrentlyOn(CastEnvironment::MAIN
));
123 // A NULL |video_frame| indicates a decode error, which we don't expect.
124 ASSERT_FALSE(!video_frame
.get());
126 // Did the decoder detect whether frames were dropped?
127 EXPECT_EQ(should_be_continuous
, is_continuous
);
129 // Does the video data seem to be intact?
130 EXPECT_EQ(expected_video_frame
->coded_size().width(),
131 video_frame
->coded_size().width());
132 EXPECT_EQ(expected_video_frame
->coded_size().height(),
133 video_frame
->coded_size().height());
134 EXPECT_LT(40.0, I420PSNR(expected_video_frame
, video_frame
));
135 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here.
137 // Signal the main test thread that more video was decoded.
138 base::AutoLock
auto_lock(lock_
);
139 ++total_video_frames_decoded_
;
143 const scoped_refptr
<StandaloneCastEnvironment
> cast_environment_
;
144 scoped_ptr
<VideoDecoder
> video_decoder_
;
145 base::TimeDelta next_frame_timestamp_
;
146 uint32 last_frame_id_
;
147 bool seen_a_decoded_frame_
;
149 Vp8Encoder vp8_encoder_
;
152 base::ConditionVariable cond_
;
153 int total_video_frames_feed_in_
;
154 int total_video_frames_decoded_
;
156 DISALLOW_COPY_AND_ASSIGN(VideoDecoderTest
);
159 TEST_P(VideoDecoderTest
, DecodesFrames
) {
160 const int kNumFrames
= 10;
161 for (int i
= 0; i
< kNumFrames
; ++i
)
163 WaitForAllVideoToBeDecoded();
166 TEST_P(VideoDecoderTest
, RecoversFromDroppedFrames
) {
167 const int kNumFrames
= 100;
168 int next_drop_at
= 3;
169 int next_num_dropped
= 1;
170 for (int i
= 0; i
< kNumFrames
; ++i
) {
171 if (i
== next_drop_at
) {
172 const int num_dropped
= next_num_dropped
++;
175 FeedMoreVideo(num_dropped
);
180 WaitForAllVideoToBeDecoded();
183 INSTANTIATE_TEST_CASE_P(VideoDecoderTestScenarios
,
185 ::testing::Values(CODEC_VIDEO_VP8
));