1 // Copyright (c) 2012 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 #include "media/base/test_helpers.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "media/base/audio_buffer.h"
15 #include "media/base/bind_to_current_loop.h"
16 #include "media/base/decoder_buffer.h"
17 #include "ui/gfx/rect.h"
20 using ::testing::StrictMock
;
24 // Utility mock for testing methods expecting Closures and PipelineStatusCBs.
25 class MockCallback
: public base::RefCountedThreadSafe
<MockCallback
> {
28 MOCK_METHOD0(Run
, void());
29 MOCK_METHOD1(RunWithStatus
, void(PipelineStatus
));
32 friend class base::RefCountedThreadSafe
<MockCallback
>;
33 virtual ~MockCallback();
36 DISALLOW_COPY_AND_ASSIGN(MockCallback
);
39 MockCallback::MockCallback() {}
40 MockCallback::~MockCallback() {}
42 base::Closure
NewExpectedClosure() {
43 StrictMock
<MockCallback
>* callback
= new StrictMock
<MockCallback
>();
44 EXPECT_CALL(*callback
, Run());
45 return base::Bind(&MockCallback::Run
, callback
);
48 PipelineStatusCB
NewExpectedStatusCB(PipelineStatus status
) {
49 StrictMock
<MockCallback
>* callback
= new StrictMock
<MockCallback
>();
50 EXPECT_CALL(*callback
, RunWithStatus(status
));
51 return base::Bind(&MockCallback::RunWithStatus
, callback
);
54 WaitableMessageLoopEvent::WaitableMessageLoopEvent()
55 : message_loop_(base::MessageLoop::current()),
57 status_(PIPELINE_OK
) {
58 DCHECK(message_loop_
);
61 WaitableMessageLoopEvent::~WaitableMessageLoopEvent() {}
63 base::Closure
WaitableMessageLoopEvent::GetClosure() {
64 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
65 return BindToCurrentLoop(base::Bind(
66 &WaitableMessageLoopEvent::OnCallback
, base::Unretained(this),
70 PipelineStatusCB
WaitableMessageLoopEvent::GetPipelineStatusCB() {
71 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
72 return BindToCurrentLoop(base::Bind(
73 &WaitableMessageLoopEvent::OnCallback
, base::Unretained(this)));
76 void WaitableMessageLoopEvent::RunAndWait() {
77 RunAndWaitForStatus(PIPELINE_OK
);
80 void WaitableMessageLoopEvent::RunAndWaitForStatus(PipelineStatus expected
) {
81 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
83 EXPECT_EQ(expected
, status_
);
87 base::Timer
timer(false, false);
88 timer
.Start(FROM_HERE
, TestTimeouts::action_timeout(), base::Bind(
89 &WaitableMessageLoopEvent::OnTimeout
, base::Unretained(this)));
92 EXPECT_TRUE(signaled_
);
93 EXPECT_EQ(expected
, status_
);
96 void WaitableMessageLoopEvent::OnCallback(PipelineStatus status
) {
97 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
100 message_loop_
->QuitWhenIdle();
103 void WaitableMessageLoopEvent::OnTimeout() {
104 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
105 ADD_FAILURE() << "Timed out waiting for message loop to quit";
106 message_loop_
->QuitWhenIdle();
109 static VideoDecoderConfig
GetTestConfig(VideoCodec codec
,
110 gfx::Size coded_size
,
112 gfx::Rect
visible_rect(coded_size
.width(), coded_size
.height());
113 gfx::Size natural_size
= coded_size
;
115 return VideoDecoderConfig(codec
, VIDEO_CODEC_PROFILE_UNKNOWN
,
116 VideoFrame::YV12
, coded_size
, visible_rect
, natural_size
,
117 NULL
, 0, is_encrypted
);
120 static const gfx::Size
kNormalSize(320, 240);
121 static const gfx::Size
kLargeSize(640, 480);
123 VideoDecoderConfig
TestVideoConfig::Invalid() {
124 return GetTestConfig(kUnknownVideoCodec
, kNormalSize
, false);
127 VideoDecoderConfig
TestVideoConfig::Normal() {
128 return GetTestConfig(kCodecVP8
, kNormalSize
, false);
131 VideoDecoderConfig
TestVideoConfig::NormalEncrypted() {
132 return GetTestConfig(kCodecVP8
, kNormalSize
, true);
135 VideoDecoderConfig
TestVideoConfig::Large() {
136 return GetTestConfig(kCodecVP8
, kLargeSize
, false);
139 VideoDecoderConfig
TestVideoConfig::LargeEncrypted() {
140 return GetTestConfig(kCodecVP8
, kLargeSize
, true);
143 gfx::Size
TestVideoConfig::NormalCodedSize() {
147 gfx::Size
TestVideoConfig::LargeCodedSize() {
152 scoped_refptr
<AudioBuffer
> MakeAudioBuffer(SampleFormat format
,
153 ChannelLayout channel_layout
,
154 size_t channel_count
,
159 base::TimeDelta timestamp
) {
160 const size_t channels
= ChannelLayoutToChannelCount(channel_layout
);
161 scoped_refptr
<AudioBuffer
> output
=
162 AudioBuffer::CreateBuffer(format
,
164 static_cast<int>(channel_count
),
166 static_cast<int>(frames
));
167 output
->set_timestamp(timestamp
);
169 const bool is_planar
=
170 format
== kSampleFormatPlanarS16
|| format
== kSampleFormatPlanarF32
;
172 // Values in channel 0 will be:
175 // start + 2 * increment, ...
176 // While, values in channel 1 will be:
177 // start + frames * increment
178 // start + (frames + 1) * increment
179 // start + (frames + 2) * increment, ...
180 for (size_t ch
= 0; ch
< channels
; ++ch
) {
182 reinterpret_cast<T
*>(output
->channel_data()[is_planar
? ch
: 0]);
183 const T v
= static_cast<T
>(start
+ ch
* frames
* increment
);
184 for (size_t i
= 0; i
< frames
; ++i
) {
185 buffer
[is_planar
? i
: ch
+ i
* channels
] =
186 static_cast<T
>(v
+ i
* increment
);
192 // Instantiate all the types of MakeAudioBuffer() and
193 // MakeAudioBuffer() needed.
194 #define DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(type) \
195 template scoped_refptr<AudioBuffer> MakeAudioBuffer<type>( \
196 SampleFormat format, \
197 ChannelLayout channel_layout, \
198 size_t channel_count, \
203 base::TimeDelta start_time)
204 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(uint8
);
205 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int16
);
206 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int32
);
207 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(float);
209 static const char kFakeVideoBufferHeader
[] = "FakeVideoBufferForTest";
211 scoped_refptr
<DecoderBuffer
> CreateFakeVideoBufferForTest(
212 const VideoDecoderConfig
& config
,
213 base::TimeDelta timestamp
, base::TimeDelta duration
) {
215 pickle
.WriteString(kFakeVideoBufferHeader
);
216 pickle
.WriteInt(config
.coded_size().width());
217 pickle
.WriteInt(config
.coded_size().height());
218 pickle
.WriteInt64(timestamp
.InMilliseconds());
220 scoped_refptr
<DecoderBuffer
> buffer
= DecoderBuffer::CopyFrom(
221 static_cast<const uint8
*>(pickle
.data()),
222 static_cast<int>(pickle
.size()));
223 buffer
->set_timestamp(timestamp
);
224 buffer
->set_duration(duration
);
225 buffer
->set_is_key_frame(true);
230 bool VerifyFakeVideoBufferForTest(
231 const scoped_refptr
<DecoderBuffer
>& buffer
,
232 const VideoDecoderConfig
& config
) {
233 // Check if the input |buffer| matches the |config|.
234 PickleIterator
pickle(Pickle(reinterpret_cast<const char*>(buffer
->data()),
235 buffer
->data_size()));
239 bool success
= pickle
.ReadString(&header
) && pickle
.ReadInt(&width
) &&
240 pickle
.ReadInt(&height
);
241 return (success
&& header
== kFakeVideoBufferHeader
&&
242 width
== config
.coded_size().width() &&
243 height
== config
.coded_size().height());
246 CallbackPairChecker::CallbackPairChecker() : expecting_b_(false) {
249 CallbackPairChecker::~CallbackPairChecker() {
250 EXPECT_FALSE(expecting_b_
);
253 void CallbackPairChecker::RecordACalled() {
254 EXPECT_FALSE(expecting_b_
);
258 void CallbackPairChecker::RecordBCalled() {
259 EXPECT_TRUE(expecting_b_
);
260 expecting_b_
= false;