Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / test / pipeline_integration_test_base.cc
blobc17431a5759f5a68c338bc35184e712cb411960a
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/test/pipeline_integration_test_base.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_vector.h"
9 #include "media/base/cdm_context.h"
10 #include "media/base/media_log.h"
11 #include "media/base/test_data_util.h"
12 #include "media/filters/chunk_demuxer.h"
13 #if !defined(MEDIA_DISABLE_FFMPEG)
14 #include "media/filters/ffmpeg_audio_decoder.h"
15 #include "media/filters/ffmpeg_demuxer.h"
16 #include "media/filters/ffmpeg_video_decoder.h"
17 #endif
18 #include "media/filters/file_data_source.h"
19 #include "media/filters/opus_audio_decoder.h"
20 #include "media/renderers/audio_renderer_impl.h"
21 #include "media/renderers/renderer_impl.h"
22 #if !defined(MEDIA_DISABLE_LIBVPX)
23 #include "media/filters/vpx_video_decoder.h"
24 #endif
26 using ::testing::_;
27 using ::testing::AnyNumber;
28 using ::testing::AtMost;
29 using ::testing::Invoke;
30 using ::testing::InvokeWithoutArgs;
31 using ::testing::SaveArg;
33 namespace media {
35 const char kNullVideoHash[] = "d41d8cd98f00b204e9800998ecf8427e";
36 const char kNullAudioHash[] = "0.00,0.00,0.00,0.00,0.00,0.00,";
38 MockVideoRendererSink::MockVideoRendererSink() {}
39 MockVideoRendererSink::~MockVideoRendererSink() {}
41 PipelineIntegrationTestBase::PipelineIntegrationTestBase()
42 : hashing_enabled_(false),
43 clockless_playback_(false),
44 pipeline_(
45 new Pipeline(message_loop_.message_loop_proxy(), new MediaLog())),
46 ended_(false),
47 pipeline_status_(PIPELINE_OK),
48 last_video_frame_format_(VideoFrame::UNKNOWN),
49 hardware_config_(AudioParameters(), AudioParameters()) {
50 base::MD5Init(&md5_context_);
53 PipelineIntegrationTestBase::~PipelineIntegrationTestBase() {
54 if (!pipeline_->IsRunning())
55 return;
57 Stop();
60 void PipelineIntegrationTestBase::OnSeeked(base::TimeDelta seek_time,
61 PipelineStatus status) {
62 EXPECT_EQ(seek_time, pipeline_->GetMediaTime());
63 pipeline_status_ = status;
66 void PipelineIntegrationTestBase::OnStatusCallback(
67 PipelineStatus status) {
68 pipeline_status_ = status;
69 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
72 void PipelineIntegrationTestBase::DemuxerEncryptedMediaInitDataCB(
73 EmeInitDataType type,
74 const std::vector<uint8>& init_data) {
75 DCHECK(!init_data.empty());
76 CHECK(!encrypted_media_init_data_cb_.is_null());
77 encrypted_media_init_data_cb_.Run(type, init_data);
80 void PipelineIntegrationTestBase::OnEnded() {
81 DCHECK(!ended_);
82 ended_ = true;
83 pipeline_status_ = PIPELINE_OK;
84 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
87 bool PipelineIntegrationTestBase::WaitUntilOnEnded() {
88 if (ended_)
89 return (pipeline_status_ == PIPELINE_OK);
90 message_loop_.Run();
91 EXPECT_TRUE(ended_);
92 return ended_ && (pipeline_status_ == PIPELINE_OK);
95 PipelineStatus PipelineIntegrationTestBase::WaitUntilEndedOrError() {
96 if (ended_ || pipeline_status_ != PIPELINE_OK)
97 return pipeline_status_;
98 message_loop_.Run();
99 return pipeline_status_;
102 void PipelineIntegrationTestBase::OnError(PipelineStatus status) {
103 DCHECK_NE(status, PIPELINE_OK);
104 pipeline_status_ = status;
105 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
108 PipelineStatus PipelineIntegrationTestBase::Start(const std::string& filename) {
109 return Start(filename, nullptr);
112 PipelineStatus PipelineIntegrationTestBase::Start(const std::string& filename,
113 CdmContext* cdm_context) {
114 EXPECT_CALL(*this, OnMetadata(_))
115 .Times(AtMost(1))
116 .WillRepeatedly(SaveArg<0>(&metadata_));
117 EXPECT_CALL(*this, OnBufferingStateChanged(BUFFERING_HAVE_ENOUGH))
118 .Times(AtMost(1));
119 CreateDemuxer(filename);
121 if (cdm_context) {
122 EXPECT_CALL(*this, DecryptorAttached(true));
123 pipeline_->SetCdm(
124 cdm_context, base::Bind(&PipelineIntegrationTestBase::DecryptorAttached,
125 base::Unretained(this)));
128 // Should never be called as the required decryption keys for the encrypted
129 // media files are provided in advance.
130 EXPECT_CALL(*this, OnWaitingForDecryptionKey()).Times(0);
132 pipeline_->Start(
133 demuxer_.get(), CreateRenderer(),
134 base::Bind(&PipelineIntegrationTestBase::OnEnded, base::Unretained(this)),
135 base::Bind(&PipelineIntegrationTestBase::OnError, base::Unretained(this)),
136 base::Bind(&PipelineIntegrationTestBase::OnStatusCallback,
137 base::Unretained(this)),
138 base::Bind(&PipelineIntegrationTestBase::OnMetadata,
139 base::Unretained(this)),
140 base::Bind(&PipelineIntegrationTestBase::OnBufferingStateChanged,
141 base::Unretained(this)),
142 base::Closure(), base::Bind(&PipelineIntegrationTestBase::OnAddTextTrack,
143 base::Unretained(this)),
144 base::Bind(&PipelineIntegrationTestBase::OnWaitingForDecryptionKey,
145 base::Unretained(this)));
146 message_loop_.Run();
147 return pipeline_status_;
150 PipelineStatus PipelineIntegrationTestBase::Start(const std::string& filename,
151 kTestType test_type) {
152 hashing_enabled_ = test_type == kHashed;
153 clockless_playback_ = test_type == kClockless;
154 return Start(filename);
157 void PipelineIntegrationTestBase::Play() {
158 pipeline_->SetPlaybackRate(1);
161 void PipelineIntegrationTestBase::Pause() {
162 pipeline_->SetPlaybackRate(0);
165 bool PipelineIntegrationTestBase::Seek(base::TimeDelta seek_time) {
166 ended_ = false;
168 EXPECT_CALL(*this, OnBufferingStateChanged(BUFFERING_HAVE_ENOUGH))
169 .WillOnce(InvokeWithoutArgs(&message_loop_, &base::MessageLoop::QuitNow));
170 pipeline_->Seek(seek_time, base::Bind(&PipelineIntegrationTestBase::OnSeeked,
171 base::Unretained(this), seek_time));
172 message_loop_.Run();
173 return (pipeline_status_ == PIPELINE_OK);
176 void PipelineIntegrationTestBase::Stop() {
177 DCHECK(pipeline_->IsRunning());
178 pipeline_->Stop(base::MessageLoop::QuitClosure());
179 message_loop_.Run();
182 void PipelineIntegrationTestBase::QuitAfterCurrentTimeTask(
183 const base::TimeDelta& quit_time) {
184 if (pipeline_->GetMediaTime() >= quit_time ||
185 pipeline_status_ != PIPELINE_OK) {
186 message_loop_.Quit();
187 return;
190 message_loop_.PostDelayedTask(
191 FROM_HERE,
192 base::Bind(&PipelineIntegrationTestBase::QuitAfterCurrentTimeTask,
193 base::Unretained(this), quit_time),
194 base::TimeDelta::FromMilliseconds(10));
197 bool PipelineIntegrationTestBase::WaitUntilCurrentTimeIsAfter(
198 const base::TimeDelta& wait_time) {
199 DCHECK(pipeline_->IsRunning());
200 DCHECK_GT(pipeline_->GetPlaybackRate(), 0);
201 DCHECK(wait_time <= pipeline_->GetMediaDuration());
203 message_loop_.PostDelayedTask(
204 FROM_HERE,
205 base::Bind(&PipelineIntegrationTestBase::QuitAfterCurrentTimeTask,
206 base::Unretained(this),
207 wait_time),
208 base::TimeDelta::FromMilliseconds(10));
209 message_loop_.Run();
210 return (pipeline_status_ == PIPELINE_OK);
213 void PipelineIntegrationTestBase::CreateDemuxer(const std::string& filename) {
214 FileDataSource* file_data_source = new FileDataSource();
215 base::FilePath file_path(GetTestDataFilePath(filename));
216 CHECK(file_data_source->Initialize(file_path)) << "Is " << file_path.value()
217 << " missing?";
218 data_source_.reset(file_data_source);
220 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
221 base::Bind(&PipelineIntegrationTestBase::DemuxerEncryptedMediaInitDataCB,
222 base::Unretained(this));
224 #if !defined(MEDIA_DISABLE_FFMPEG)
225 demuxer_ = scoped_ptr<Demuxer>(
226 new FFmpegDemuxer(message_loop_.message_loop_proxy(), data_source_.get(),
227 encrypted_media_init_data_cb, new MediaLog()));
228 #endif
231 scoped_ptr<Renderer> PipelineIntegrationTestBase::CreateRenderer() {
232 ScopedVector<VideoDecoder> video_decoders;
233 #if !defined(MEDIA_DISABLE_LIBVPX)
234 video_decoders.push_back(
235 new VpxVideoDecoder(message_loop_.message_loop_proxy()));
236 #endif // !defined(MEDIA_DISABLE_LIBVPX)
238 #if !defined(MEDIA_DISABLE_FFMPEG)
239 video_decoders.push_back(
240 new FFmpegVideoDecoder(message_loop_.message_loop_proxy()));
241 #endif
243 EXPECT_CALL(video_sink_, PaintFrameUsingOldRenderingPath(_))
244 .WillRepeatedly(
245 Invoke(this, &PipelineIntegrationTestBase::OnVideoFramePaint));
247 // Disable frame dropping if hashing is enabled.
248 scoped_ptr<VideoRenderer> video_renderer(
249 new VideoRendererImpl(message_loop_.message_loop_proxy(), &video_sink_,
250 video_decoders.Pass(), false, new MediaLog()));
252 if (!clockless_playback_) {
253 audio_sink_ = new NullAudioSink(message_loop_.message_loop_proxy());
254 } else {
255 clockless_audio_sink_ = new ClocklessAudioSink();
258 ScopedVector<AudioDecoder> audio_decoders;
260 #if !defined(MEDIA_DISABLE_FFMPEG)
261 audio_decoders.push_back(
262 new FFmpegAudioDecoder(message_loop_.message_loop_proxy(), LogCB()));
263 #endif
265 audio_decoders.push_back(
266 new OpusAudioDecoder(message_loop_.message_loop_proxy()));
268 AudioParameters out_params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
269 CHANNEL_LAYOUT_STEREO,
270 44100,
272 512);
273 hardware_config_.UpdateOutputConfig(out_params);
275 scoped_ptr<AudioRenderer> audio_renderer(new AudioRendererImpl(
276 message_loop_.message_loop_proxy(),
277 (clockless_playback_)
278 ? static_cast<AudioRendererSink*>(clockless_audio_sink_.get())
279 : audio_sink_.get(),
280 audio_decoders.Pass(), hardware_config_, new MediaLog()));
281 if (hashing_enabled_)
282 audio_sink_->StartAudioHashForTesting();
284 scoped_ptr<RendererImpl> renderer_impl(
285 new RendererImpl(message_loop_.message_loop_proxy(),
286 audio_renderer.Pass(),
287 video_renderer.Pass()));
289 // Prevent non-deterministic buffering state callbacks from firing (e.g., slow
290 // machine, valgrind).
291 renderer_impl->DisableUnderflowForTesting();
293 if (clockless_playback_)
294 renderer_impl->EnableClocklessVideoPlaybackForTesting();
296 return renderer_impl.Pass();
299 void PipelineIntegrationTestBase::OnVideoFramePaint(
300 const scoped_refptr<VideoFrame>& frame) {
301 last_video_frame_format_ = frame->format();
302 if (!hashing_enabled_)
303 return;
304 frame->HashFrameForTesting(&md5_context_);
307 std::string PipelineIntegrationTestBase::GetVideoHash() {
308 DCHECK(hashing_enabled_);
309 base::MD5Digest digest;
310 base::MD5Final(&digest, &md5_context_);
311 return base::MD5DigestToBase16(digest);
314 std::string PipelineIntegrationTestBase::GetAudioHash() {
315 DCHECK(hashing_enabled_);
316 return audio_sink_->GetAudioHashForTesting();
319 base::TimeDelta PipelineIntegrationTestBase::GetAudioTime() {
320 DCHECK(clockless_playback_);
321 return clockless_audio_sink_->render_time();
324 base::TimeTicks DummyTickClock::NowTicks() {
325 now_ += base::TimeDelta::FromSeconds(60);
326 return now_;
329 } // namespace media