Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / test / encode_decode_test.cc
blob67edbc89d0886d2de98646aa909617d8fdd61761
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.
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.
9 #include <gtest/gtest.h>
10 #include <stdint.h>
12 #include "base/bind.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "media/base/video_frame.h"
15 #include "media/cast/cast_environment.h"
16 #include "media/cast/test/fake_single_thread_task_runner.h"
17 #include "media/cast/test/utility/video_utility.h"
18 #include "media/cast/video_receiver/codecs/vp8/vp8_decoder.h"
19 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h"
21 namespace media {
22 namespace cast {
24 static const int64 kStartMillisecond = INT64_C(1245);
25 static const int kWidth = 1280;
26 static const int kHeight = 720;
27 static const int kStartbitrate = 4000000;
28 static const int kMaxQp = 54;
29 static const int kMinQp = 4;
30 static const int kMaxFrameRate = 30;
32 namespace {
33 class EncodeDecodeTestFrameCallback
34 : public base::RefCountedThreadSafe<EncodeDecodeTestFrameCallback> {
35 public:
36 EncodeDecodeTestFrameCallback() : 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());
42 void SetFrameStartValue(int start_value) {
43 PopulateVideoFrame(original_frame_.get(), start_value);
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);
58 int num_called() const { return num_called_; }
60 protected:
61 virtual ~EncodeDecodeTestFrameCallback() {}
63 private:
64 friend class base::RefCountedThreadSafe<EncodeDecodeTestFrameCallback>;
66 int num_called_;
67 scoped_refptr<media::VideoFrame> original_frame_;
69 } // namespace
71 class EncodeDecodeTest : public ::testing::Test {
72 protected:
73 EncodeDecodeTest()
74 : testing_clock_(new base::SimpleTestTickClock()),
75 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
76 // CastEnvironment will only be used by the vp8 decoder; Enable only the
77 // video and main threads.
78 cast_environment_(new CastEnvironment(
79 scoped_ptr<base::TickClock>(testing_clock_).Pass(),
80 task_runner_,
81 NULL,
82 task_runner_)),
83 test_callback_(new EncodeDecodeTestFrameCallback()) {
84 testing_clock_->Advance(
85 base::TimeDelta::FromMilliseconds(kStartMillisecond));
86 encoder_config_.max_number_of_video_buffers_used = 1;
87 encoder_config_.width = kWidth;
88 encoder_config_.height = kHeight;
89 encoder_config_.start_bitrate = kStartbitrate;
90 encoder_config_.min_qp = kMaxQp;
91 encoder_config_.min_qp = kMinQp;
92 encoder_config_.max_frame_rate = kMaxFrameRate;
93 int max_unacked_frames = 1;
94 encoder_.reset(new Vp8Encoder(encoder_config_, max_unacked_frames));
95 // Initialize to use one core.
96 decoder_.reset(new Vp8Decoder(cast_environment_));
99 virtual ~EncodeDecodeTest() {}
101 virtual void SetUp() OVERRIDE {
102 // Create test frame.
103 int start_value = 10; // Random value to start from.
104 gfx::Size size(encoder_config_.width, encoder_config_.height);
105 video_frame_ = media::VideoFrame::CreateFrame(
106 VideoFrame::I420, size, gfx::Rect(size), size, base::TimeDelta());
107 PopulateVideoFrame(video_frame_, start_value);
108 test_callback_->SetFrameStartValue(start_value);
111 VideoSenderConfig encoder_config_;
112 scoped_ptr<Vp8Encoder> encoder_;
113 scoped_ptr<Vp8Decoder> decoder_;
114 scoped_refptr<media::VideoFrame> video_frame_;
115 base::SimpleTestTickClock* testing_clock_; // Owned by CastEnvironment.
116 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
117 scoped_refptr<CastEnvironment> cast_environment_;
118 scoped_refptr<EncodeDecodeTestFrameCallback> test_callback_;
121 TEST_F(EncodeDecodeTest, BasicEncodeDecode) {
122 transport::EncodedVideoFrame encoded_frame;
123 encoder_->Initialize();
124 // Encode frame.
125 encoder_->Encode(video_frame_, &encoded_frame);
126 EXPECT_GT(encoded_frame.data.size(), UINT64_C(0));
127 // Decode frame.
128 decoder_->Decode(&encoded_frame,
129 base::TimeTicks(),
130 base::Bind(&EncodeDecodeTestFrameCallback::DecodeComplete,
131 test_callback_));
132 task_runner_->RunTasks();
135 } // namespace cast
136 } // namespace media