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 // Joint encoder and decoder testing.
6 // These tests operate directly on the VP8 encoder and decoder, not the
7 // transport layer, and are targeted at validating the bit stream.
8
9 #include <gtest/gtest.h>
10
11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "media/base/video_frame.h"
14 #include "media/cast/cast_environment.h"
15 #include "media/cast/test/fake_task_runner.h"
16 #include "media/cast/test/video_utility.h"
17 #include "media/cast/video_receiver/codecs/vp8/vp8_decoder.h"
18 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h"
19
20 namespace media {
21 namespace cast {
22
23 static const int64 kStartMillisecond = GG_INT64_C(1245);
24 static const int kWidth = 1280;
25 static const int kHeight = 720;
26 static const int kStartbitrate = 4000000;
27 static const int kMaxQp = 54;
28 static const int kMinQp = 4;
29 static const int kMaxFrameRate = 30;
30
31 namespace {
32 class EncodeDecodeTestFrameCallback :
33 public base::RefCountedThreadSafe<EncodeDecodeTestFrameCallback> {
34 public:
EncodeDecodeTestFrameCallback()35 EncodeDecodeTestFrameCallback()
36 : num_called_(0) {
37 gfx::Size size(kWidth, kHeight);
38 original_frame_ = media::VideoFrame::CreateFrame(
39 VideoFrame::I420, size, gfx::Rect(size), size, base::TimeDelta());
40 }
41
SetFrameStartValue(int start_value)42 void SetFrameStartValue(int start_value) {
43 PopulateVideoFrame(original_frame_.get(), start_value);
44 }
45
DecodeComplete(const scoped_refptr<media::VideoFrame> & decoded_frame,const base::TimeTicks & render_time)46 void DecodeComplete(const scoped_refptr<media::VideoFrame>& decoded_frame,
47 const base::TimeTicks& render_time) {
48 ++num_called_;
49 // Compare resolution.
50 EXPECT_EQ(original_frame_->coded_size().width(),
51 decoded_frame->coded_size().width());
52 EXPECT_EQ(original_frame_->coded_size().height(),
53 decoded_frame->coded_size().height());
54 // Compare data.
55 EXPECT_GT(I420PSNR(original_frame_, decoded_frame), 40.0);
56 }
57
num_called() const58 int num_called() const {
59 return num_called_;
60 }
61
62 protected:
~EncodeDecodeTestFrameCallback()63 virtual ~EncodeDecodeTestFrameCallback() {}
64
65 private:
66 friend class base::RefCountedThreadSafe<EncodeDecodeTestFrameCallback>;
67
68 int num_called_;
69 scoped_refptr<media::VideoFrame> original_frame_;
70 };
71 } // namespace
72
73 class EncodeDecodeTest : public ::testing::Test {
74 protected:
EncodeDecodeTest()75 EncodeDecodeTest()
76 : task_runner_(new test::FakeTaskRunner(&testing_clock_)),
77 // CastEnvironment will only be used by the vp8 decoder; Enable only the
78 // video decoder and main threads.
79 cast_environment_(new CastEnvironment(&testing_clock_, task_runner_,
80 NULL, NULL, NULL, task_runner_, GetDefaultCastLoggingConfig())),
81 test_callback_(new EncodeDecodeTestFrameCallback()) {
82 testing_clock_.Advance(
83 base::TimeDelta::FromMilliseconds(kStartMillisecond));
84 encoder_config_.max_number_of_video_buffers_used = 1;
85 encoder_config_.number_of_cores = 1;
86 encoder_config_.width = kWidth;
87 encoder_config_.height = kHeight;
88 encoder_config_.start_bitrate = kStartbitrate;
89 encoder_config_.min_qp = kMaxQp;
90 encoder_config_.min_qp = kMinQp;
91 encoder_config_.max_frame_rate = kMaxFrameRate;
92 int max_unacked_frames = 1;
93 encoder_.reset(new Vp8Encoder(encoder_config_, max_unacked_frames));
94 // Initialize to use one core.
95 decoder_.reset(new Vp8Decoder(cast_environment_));
96 }
97
~EncodeDecodeTest()98 virtual ~EncodeDecodeTest() {}
99
SetUp()100 virtual void SetUp() OVERRIDE {
101 // Create test frame.
102 int start_value = 10; // Random value to start from.
103 gfx::Size size(encoder_config_.width, encoder_config_.height);
104 video_frame_ = media::VideoFrame::CreateFrame(VideoFrame::I420,
105 size, gfx::Rect(size), size, base::TimeDelta());
106 PopulateVideoFrame(video_frame_, start_value);
107 test_callback_->SetFrameStartValue(start_value);
108 }
109
110 VideoSenderConfig encoder_config_;
111 scoped_ptr<Vp8Encoder> encoder_;
112 scoped_ptr<Vp8Decoder> decoder_;
113 scoped_refptr<media::VideoFrame> video_frame_;
114 base::SimpleTestTickClock testing_clock_;
115 scoped_refptr<test::FakeTaskRunner> task_runner_;
116 scoped_refptr<CastEnvironment> cast_environment_;
117 scoped_refptr<EncodeDecodeTestFrameCallback> test_callback_;
118 };
119
TEST_F(EncodeDecodeTest,BasicEncodeDecode)120 TEST_F(EncodeDecodeTest, BasicEncodeDecode) {
121 EncodedVideoFrame encoded_frame;
122 // Encode frame.
123 encoder_->Encode(video_frame_, &encoded_frame);
124 EXPECT_GT(encoded_frame.data.size(), GG_UINT64_C(0));
125 // Decode frame.
126 decoder_->Decode(&encoded_frame, base::TimeTicks(), base::Bind(
127 &EncodeDecodeTestFrameCallback::DecodeComplete, test_callback_));
128 task_runner_->RunTasks();
129 }
130
131 } // namespace cast
132 } // namespace media
133