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/run_loop.h"
12 #include "base/test/test_timeouts.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "media/base/audio_buffer.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/decoder_buffer.h"
18 #include "ui/gfx/geometry/rect.h"
21 using ::testing::StrictMock
;
25 // Utility mock for testing methods expecting Closures and PipelineStatusCBs.
26 class MockCallback
: public base::RefCountedThreadSafe
<MockCallback
> {
29 MOCK_METHOD0(Run
, void());
30 MOCK_METHOD1(RunWithBool
, void(bool));
31 MOCK_METHOD1(RunWithStatus
, void(PipelineStatus
));
34 friend class base::RefCountedThreadSafe
<MockCallback
>;
35 virtual ~MockCallback();
38 DISALLOW_COPY_AND_ASSIGN(MockCallback
);
41 MockCallback::MockCallback() {}
42 MockCallback::~MockCallback() {}
44 base::Closure
NewExpectedClosure() {
45 StrictMock
<MockCallback
>* callback
= new StrictMock
<MockCallback
>();
46 EXPECT_CALL(*callback
, Run());
47 return base::Bind(&MockCallback::Run
, callback
);
50 base::Callback
<void(bool)> NewExpectedBoolCB(bool success
) {
51 StrictMock
<MockCallback
>* callback
= new StrictMock
<MockCallback
>();
52 EXPECT_CALL(*callback
, RunWithBool(success
));
53 return base::Bind(&MockCallback::RunWithBool
, callback
);
56 PipelineStatusCB
NewExpectedStatusCB(PipelineStatus status
) {
57 StrictMock
<MockCallback
>* callback
= new StrictMock
<MockCallback
>();
58 EXPECT_CALL(*callback
, RunWithStatus(status
));
59 return base::Bind(&MockCallback::RunWithStatus
, callback
);
62 WaitableMessageLoopEvent::WaitableMessageLoopEvent()
63 : message_loop_(base::MessageLoop::current()),
65 status_(PIPELINE_OK
) {
66 DCHECK(message_loop_
);
69 WaitableMessageLoopEvent::~WaitableMessageLoopEvent() {}
71 base::Closure
WaitableMessageLoopEvent::GetClosure() {
72 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
73 return BindToCurrentLoop(base::Bind(
74 &WaitableMessageLoopEvent::OnCallback
, base::Unretained(this),
78 PipelineStatusCB
WaitableMessageLoopEvent::GetPipelineStatusCB() {
79 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
80 return BindToCurrentLoop(base::Bind(
81 &WaitableMessageLoopEvent::OnCallback
, base::Unretained(this)));
84 void WaitableMessageLoopEvent::RunAndWait() {
85 RunAndWaitForStatus(PIPELINE_OK
);
88 void WaitableMessageLoopEvent::RunAndWaitForStatus(PipelineStatus expected
) {
89 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
91 EXPECT_EQ(expected
, status_
);
95 run_loop_
.reset(new base::RunLoop());
96 base::Timer
timer(false, false);
97 timer
.Start(FROM_HERE
, TestTimeouts::action_timeout(), base::Bind(
98 &WaitableMessageLoopEvent::OnTimeout
, base::Unretained(this)));
101 EXPECT_TRUE(signaled_
);
102 EXPECT_EQ(expected
, status_
);
106 void WaitableMessageLoopEvent::OnCallback(PipelineStatus status
) {
107 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
111 // |run_loop_| may be null if the callback fires before RunAndWaitForStatus().
116 void WaitableMessageLoopEvent::OnTimeout() {
117 DCHECK_EQ(message_loop_
, base::MessageLoop::current());
118 ADD_FAILURE() << "Timed out waiting for message loop to quit";
122 static VideoDecoderConfig
GetTestConfig(VideoCodec codec
,
123 gfx::Size coded_size
,
125 gfx::Rect
visible_rect(coded_size
.width(), coded_size
.height());
126 gfx::Size natural_size
= coded_size
;
128 return VideoDecoderConfig(codec
, VIDEO_CODEC_PROFILE_UNKNOWN
,
129 VideoFrame::YV12
, coded_size
, visible_rect
, natural_size
,
130 NULL
, 0, is_encrypted
);
133 static const gfx::Size
kNormalSize(320, 240);
134 static const gfx::Size
kLargeSize(640, 480);
136 VideoDecoderConfig
TestVideoConfig::Invalid() {
137 return GetTestConfig(kUnknownVideoCodec
, kNormalSize
, false);
140 VideoDecoderConfig
TestVideoConfig::Normal() {
141 return GetTestConfig(kCodecVP8
, kNormalSize
, false);
144 VideoDecoderConfig
TestVideoConfig::NormalEncrypted() {
145 return GetTestConfig(kCodecVP8
, kNormalSize
, true);
148 VideoDecoderConfig
TestVideoConfig::Large() {
149 return GetTestConfig(kCodecVP8
, kLargeSize
, false);
152 VideoDecoderConfig
TestVideoConfig::LargeEncrypted() {
153 return GetTestConfig(kCodecVP8
, kLargeSize
, true);
156 gfx::Size
TestVideoConfig::NormalCodedSize() {
160 gfx::Size
TestVideoConfig::LargeCodedSize() {
165 scoped_refptr
<AudioBuffer
> MakeAudioBuffer(SampleFormat format
,
166 ChannelLayout channel_layout
,
167 size_t channel_count
,
172 base::TimeDelta timestamp
) {
173 const size_t channels
= ChannelLayoutToChannelCount(channel_layout
);
174 scoped_refptr
<AudioBuffer
> output
=
175 AudioBuffer::CreateBuffer(format
,
177 static_cast<int>(channel_count
),
179 static_cast<int>(frames
));
180 output
->set_timestamp(timestamp
);
182 const bool is_planar
=
183 format
== kSampleFormatPlanarS16
|| format
== kSampleFormatPlanarF32
;
185 // Values in channel 0 will be:
188 // start + 2 * increment, ...
189 // While, values in channel 1 will be:
190 // start + frames * increment
191 // start + (frames + 1) * increment
192 // start + (frames + 2) * increment, ...
193 for (size_t ch
= 0; ch
< channels
; ++ch
) {
195 reinterpret_cast<T
*>(output
->channel_data()[is_planar
? ch
: 0]);
196 const T v
= static_cast<T
>(start
+ ch
* frames
* increment
);
197 for (size_t i
= 0; i
< frames
; ++i
) {
198 buffer
[is_planar
? i
: ch
+ i
* channels
] =
199 static_cast<T
>(v
+ i
* increment
);
205 // Instantiate all the types of MakeAudioBuffer() and
206 // MakeAudioBuffer() needed.
207 #define DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(type) \
208 template scoped_refptr<AudioBuffer> MakeAudioBuffer<type>( \
209 SampleFormat format, \
210 ChannelLayout channel_layout, \
211 size_t channel_count, \
216 base::TimeDelta start_time)
217 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(uint8
);
218 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int16
);
219 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(int32
);
220 DEFINE_MAKE_AUDIO_BUFFER_INSTANCE(float);
222 static const char kFakeVideoBufferHeader
[] = "FakeVideoBufferForTest";
224 scoped_refptr
<DecoderBuffer
> CreateFakeVideoBufferForTest(
225 const VideoDecoderConfig
& config
,
226 base::TimeDelta timestamp
, base::TimeDelta duration
) {
228 pickle
.WriteString(kFakeVideoBufferHeader
);
229 pickle
.WriteInt(config
.coded_size().width());
230 pickle
.WriteInt(config
.coded_size().height());
231 pickle
.WriteInt64(timestamp
.InMilliseconds());
233 scoped_refptr
<DecoderBuffer
> buffer
= DecoderBuffer::CopyFrom(
234 static_cast<const uint8
*>(pickle
.data()),
235 static_cast<int>(pickle
.size()));
236 buffer
->set_timestamp(timestamp
);
237 buffer
->set_duration(duration
);
238 buffer
->set_is_key_frame(true);
243 bool VerifyFakeVideoBufferForTest(
244 const scoped_refptr
<DecoderBuffer
>& buffer
,
245 const VideoDecoderConfig
& config
) {
246 // Check if the input |buffer| matches the |config|.
247 base::PickleIterator
pickle(base::Pickle(
248 reinterpret_cast<const char*>(buffer
->data()), buffer
->data_size()));
252 bool success
= pickle
.ReadString(&header
) && pickle
.ReadInt(&width
) &&
253 pickle
.ReadInt(&height
);
254 return (success
&& header
== kFakeVideoBufferHeader
&&
255 width
== config
.coded_size().width() &&
256 height
== config
.coded_size().height());
259 CallbackPairChecker::CallbackPairChecker() : expecting_b_(false) {
262 CallbackPairChecker::~CallbackPairChecker() {
263 EXPECT_FALSE(expecting_b_
);
266 void CallbackPairChecker::RecordACalled() {
267 EXPECT_FALSE(expecting_b_
);
271 void CallbackPairChecker::RecordBCalled() {
272 EXPECT_TRUE(expecting_b_
);
273 expecting_b_
= false;
276 void AddLogEntryForTest(MediaLog::MediaLogLevel level
,
277 const std::string
& message
) {
278 DVLOG(1) << "Media log (" << MediaLog::MediaLogLevelToString(level
)