Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / video_receiver / video_decoder_unittest.cc
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.
4
5 #include <cstdlib>
6
7 #include "base/bind.h"
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"
18
19 namespace media {
20 namespace cast {
21
22 namespace {
23
24 const int kWidth = 360;
25 const int kHeight = 240;
26 const int kFrameRate = 10;
27
28 VideoSenderConfig GetVideoSenderConfigForTest() {
29   VideoSenderConfig config;
30   config.width = kWidth;
31   config.height = kHeight;
32   config.max_frame_rate = kFrameRate;
33   return config;
34 }
35
36 }  // namespace
37
38 class VideoDecoderTest
39     : public ::testing::TestWithParam<transport::VideoCodec> {
40  public:
41   VideoDecoderTest()
42       : cast_environment_(new StandaloneCastEnvironment()),
43         vp8_encoder_(GetVideoSenderConfigForTest(), 0),
44         cond_(&lock_) {
45     vp8_encoder_.Initialize();
46   }
47
48  protected:
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());
55
56     next_frame_timestamp_ = base::TimeDelta();
57     last_frame_id_ = 0;
58     seen_a_decoded_frame_ = false;
59
60     total_video_frames_feed_in_ = 0;
61     total_video_frames_decoded_ = 0;
62   }
63
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.
68
69     const gfx::Size frame_size(kWidth, kHeight);
70     const scoped_refptr<VideoFrame> video_frame =
71         VideoFrame::CreateFrame(VideoFrame::YV12,
72                                 frame_size,
73                                 gfx::Rect(frame_size),
74                                 frame_size,
75                                 next_frame_timestamp_);
76     next_frame_timestamp_ += base::TimeDelta::FromSeconds(1) / kFrameRate;
77     PopulateVideoFrame(video_frame, 0);
78
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;
87
88     {
89       base::AutoLock auto_lock(lock_);
90       ++total_video_frames_feed_in_;
91     }
92
93     cast_environment_->PostTask(
94         CastEnvironment::MAIN,
95         FROM_HERE,
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),
101                               video_frame,
102                               num_dropped_frames == 0)));
103   }
104
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_)
110       cond_.Wait();
111     EXPECT_EQ(total_video_frames_feed_in_, total_video_frames_decoded_);
112   }
113
114  private:
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));
121
122     // A NULL |video_frame| indicates a decode error, which we don't expect.
123     ASSERT_FALSE(!video_frame);
124
125     // Did the decoder detect whether frames were dropped?
126     EXPECT_EQ(should_be_continuous, is_continuous);
127
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.
135
136     // Signal the main test thread that more video was decoded.
137     base::AutoLock auto_lock(lock_);
138     ++total_video_frames_decoded_;
139     cond_.Signal();
140   }
141
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_;
147
148   Vp8Encoder vp8_encoder_;
149
150   base::Lock lock_;
151   base::ConditionVariable cond_;
152   int total_video_frames_feed_in_;
153   int total_video_frames_decoded_;
154
155   DISALLOW_COPY_AND_ASSIGN(VideoDecoderTest);
156 };
157
158 TEST_P(VideoDecoderTest, DecodesFrames) {
159   const int kNumFrames = 10;
160   for (int i = 0; i < kNumFrames; ++i)
161     FeedMoreVideo(0);
162   WaitForAllVideoToBeDecoded();
163 }
164
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++;
172       next_drop_at *= 2;
173       i += num_dropped;
174       FeedMoreVideo(num_dropped);
175     } else {
176       FeedMoreVideo(0);
177     }
178   }
179   WaitForAllVideoToBeDecoded();
180 }
181
182 INSTANTIATE_TEST_CASE_P(VideoDecoderTestScenarios,
183                         VideoDecoderTest,
184                         ::testing::Values(transport::kVp8));
185
186 }  // namespace cast
187 }  // namespace media