1 // Copyright 2013 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/test/utility/standalone_cast_environment.h"
14 #include "media/cast/test/utility/video_utility.h"
15 #include "media/cast/video_receiver/video_decoder.h"
16 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.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
39 : public ::testing::TestWithParam<transport::VideoCodec> {
42 : cast_environment_(new StandaloneCastEnvironment()),
43 vp8_encoder_(GetVideoSenderConfigForTest(), 0),
45 vp8_encoder_.Initialize();
49 virtual void SetUp() OVERRIDE {
50 VideoReceiverConfig decoder_config;
51 decoder_config.use_external_decoder = false;
52 decoder_config.codec = GetParam();
53 video_decoder_.reset(new VideoDecoder(cast_environment_, decoder_config));
54 CHECK_EQ(STATUS_VIDEO_INITIALIZED, video_decoder_->InitializationResult());
56 next_frame_timestamp_ = base::TimeDelta();
58 seen_a_decoded_frame_ = false;
60 total_video_frames_feed_in_ = 0;
61 total_video_frames_decoded_ = 0;
64 // Called from the unit test thread to create another EncodedVideoFrame and
65 // push it into the decoding pipeline.
66 void FeedMoreVideo(int num_dropped_frames) {
67 // Prepare a simulated EncodedVideoFrame to feed into the VideoDecoder.
69 const gfx::Size frame_size(kWidth, kHeight);
70 const scoped_refptr<VideoFrame> video_frame =
71 VideoFrame::CreateFrame(VideoFrame::YV12,
73 gfx::Rect(frame_size),
75 next_frame_timestamp_);
76 next_frame_timestamp_ += base::TimeDelta::FromSeconds(1) / kFrameRate;
77 PopulateVideoFrame(video_frame, 0);
79 // Encode |frame| into |encoded_frame->data|.
80 scoped_ptr<transport::EncodedVideoFrame> encoded_frame(
81 new transport::EncodedVideoFrame());
82 CHECK_EQ(transport::kVp8, GetParam()); // Only support VP8 test currently.
83 vp8_encoder_.Encode(video_frame, encoded_frame.get());
84 encoded_frame->codec = GetParam();
85 encoded_frame->frame_id = last_frame_id_ + 1 + num_dropped_frames;
86 last_frame_id_ = encoded_frame->frame_id;
89 base::AutoLock auto_lock(lock_);
90 ++total_video_frames_feed_in_;
93 cast_environment_->PostTask(
94 CastEnvironment::MAIN,
96 base::Bind(&VideoDecoder::DecodeFrame,
97 base::Unretained(video_decoder_.get()),
98 base::Passed(&encoded_frame),
99 base::Bind(&VideoDecoderTest::OnDecodedFrame,
100 base::Unretained(this),
102 num_dropped_frames == 0)));
105 // Blocks the caller until all video that has been feed in has been decoded.
106 void WaitForAllVideoToBeDecoded() {
107 DCHECK(!cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
108 base::AutoLock auto_lock(lock_);
109 while (total_video_frames_decoded_ < total_video_frames_feed_in_)
111 EXPECT_EQ(total_video_frames_feed_in_, total_video_frames_decoded_);
115 // Called by |vp8_decoder_| to deliver each frame of decoded video.
116 void OnDecodedFrame(const scoped_refptr<VideoFrame>& expected_video_frame,
117 bool should_be_continuous,
118 const scoped_refptr<VideoFrame>& video_frame,
119 bool is_continuous) {
120 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
122 // A NULL |video_frame| indicates a decode error, which we don't expect.
123 ASSERT_FALSE(!video_frame);
125 // Did the decoder detect whether frames were dropped?
126 EXPECT_EQ(should_be_continuous, is_continuous);
128 // Does the video data seem to be intact?
129 EXPECT_EQ(expected_video_frame->coded_size().width(),
130 video_frame->coded_size().width());
131 EXPECT_EQ(expected_video_frame->coded_size().height(),
132 video_frame->coded_size().height());
133 EXPECT_LT(40.0, I420PSNR(expected_video_frame, video_frame));
134 // TODO(miu): Once we start using VideoFrame::timestamp_, check that here.
136 // Signal the main test thread that more video was decoded.
137 base::AutoLock auto_lock(lock_);
138 ++total_video_frames_decoded_;
142 const scoped_refptr<StandaloneCastEnvironment> cast_environment_;
143 scoped_ptr<VideoDecoder> video_decoder_;
144 base::TimeDelta next_frame_timestamp_;
145 uint32 last_frame_id_;
146 bool seen_a_decoded_frame_;
148 Vp8Encoder vp8_encoder_;
151 base::ConditionVariable cond_;
152 int total_video_frames_feed_in_;
153 int total_video_frames_decoded_;
155 DISALLOW_COPY_AND_ASSIGN(VideoDecoderTest);
158 TEST_P(VideoDecoderTest, DecodesFrames) {
159 const int kNumFrames = 10;
160 for (int i = 0; i < kNumFrames; ++i)
162 WaitForAllVideoToBeDecoded();
165 TEST_P(VideoDecoderTest, RecoversFromDroppedFrames) {
166 const int kNumFrames = 100;
167 int next_drop_at = 3;
168 int next_num_dropped = 1;
169 for (int i = 0; i < kNumFrames; ++i) {
170 if (i == next_drop_at) {
171 const int num_dropped = next_num_dropped++;
174 FeedMoreVideo(num_dropped);
179 WaitForAllVideoToBeDecoded();
182 INSTANTIATE_TEST_CASE_P(VideoDecoderTestScenarios,
184 ::testing::Values(transport::kVp8));