1 // Copyright 2015 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.
6 #include "base/logging.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "base/timer/timer.h"
9 #include "media/base/android/media_codec_audio_decoder.h"
10 #include "media/base/android/media_codec_bridge.h"
11 #include "media/base/android/media_codec_video_decoder.h"
12 #include "media/base/android/test_data_factory.h"
13 #include "media/base/android/test_statistics.h"
14 #include "media/base/timestamp_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gl/android/surface_texture.h"
20 // Helper macro to skip the test if MediaCodecBridge isn't available.
21 #define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE() \
23 if (!MediaCodecBridge::IsAvailable()) { \
24 VLOG(0) << "Could not run test - not supported on device."; \
31 const base::TimeDelta kDefaultTimeout
= base::TimeDelta::FromMilliseconds(200);
32 const base::TimeDelta kAudioFramePeriod
=
33 base::TimeDelta::FromSecondsD(1024.0 / 44100); // 1024 samples @ 44100 Hz
34 const base::TimeDelta kVideoFramePeriod
= base::TimeDelta::FromMilliseconds(20);
36 class AudioFactory
: public TestDataFactory
{
38 AudioFactory(base::TimeDelta duration
);
39 DemuxerConfigs
GetConfigs() const override
;
42 void ModifyAccessUnit(int index_in_chunk
, AccessUnit
* unit
) override
;
45 class VideoFactory
: public TestDataFactory
{
47 VideoFactory(base::TimeDelta duration
);
48 DemuxerConfigs
GetConfigs() const override
;
51 void ModifyAccessUnit(int index_in_chunk
, AccessUnit
* unit
) override
;
54 AudioFactory::AudioFactory(base::TimeDelta duration
)
55 : TestDataFactory("aac-44100-packet-%d", duration
, kAudioFramePeriod
) {
58 DemuxerConfigs
AudioFactory::GetConfigs() const {
59 return TestDataFactory::CreateAudioConfigs(kCodecAAC
, duration_
);
62 void AudioFactory::ModifyAccessUnit(int index_in_chunk
, AccessUnit
* unit
) {
63 unit
->is_key_frame
= true;
66 VideoFactory::VideoFactory(base::TimeDelta duration
)
67 : TestDataFactory("h264-320x180-frame-%d", duration
, kVideoFramePeriod
) {
70 DemuxerConfigs
VideoFactory::GetConfigs() const {
71 return TestDataFactory::CreateVideoConfigs(kCodecH264
, duration_
,
75 void VideoFactory::ModifyAccessUnit(int index_in_chunk
, AccessUnit
* unit
) {
76 // The frames are taken from High profile and some are B-frames.
77 // The first 4 frames appear in the file in the following order:
80 // Decoding order: 0 1 2 3
81 // Presentation order: 0 2 1 4(3)
83 // I keep the last PTS to be 3 for simplicity.
85 // Swap pts for second and third frames. Make first frame a key frame.
86 switch (index_in_chunk
) {
87 case 0: // first frame
88 unit
->is_key_frame
= true;
90 case 1: // second frame
91 unit
->timestamp
+= frame_period_
;
93 case 2: // third frame
94 unit
->timestamp
-= frame_period_
;
96 case 3: // fourth frame, do not modify
104 } // namespace (anonymous)
106 // The test fixture for MediaCodecDecoder
108 class MediaCodecDecoderTest
: public testing::Test
{
110 MediaCodecDecoderTest();
111 ~MediaCodecDecoderTest() override
;
113 // Conditions we wait for.
114 bool is_prefetched() const { return is_prefetched_
; }
115 bool is_stopped() const { return is_stopped_
; }
116 bool is_starved() const { return is_starved_
; }
118 void SetPrefetched(bool value
) { is_prefetched_
= value
; }
119 void SetStopped(bool value
) { is_stopped_
= value
; }
120 void SetStarved(bool value
) { is_starved_
= value
; }
123 typedef base::Callback
<bool()> Predicate
;
125 typedef base::Callback
<void(const DemuxerData
&)> DataAvailableCallback
;
127 // Waits for condition to become true or for timeout to expire.
128 // Returns true if the condition becomes true.
129 bool WaitForCondition(const Predicate
& condition
,
130 const base::TimeDelta
& timeout
= kDefaultTimeout
);
132 void SetDataFactory(scoped_ptr
<TestDataFactory
> factory
) {
133 data_factory_
= factory
.Pass();
136 DemuxerConfigs
GetConfigs() const {
137 // ASSERT_NE does not compile here because it expects void return value.
138 EXPECT_NE(nullptr, data_factory_
.get());
139 return data_factory_
->GetConfigs();
142 void CreateAudioDecoder();
143 void CreateVideoDecoder();
144 void SetVideoSurface();
145 void SetStopRequestAtTime(const base::TimeDelta
& time
) {
146 stop_request_time_
= time
;
149 // Decoder callbacks.
150 void OnDataRequested();
151 void OnStarvation() { is_starved_
= true; }
152 void OnDecoderDrained() {}
153 void OnStopDone() { is_stopped_
= true; }
154 void OnError() { DVLOG(0) << "MediaCodecDecoderTest::" << __FUNCTION__
; }
155 void OnUpdateCurrentTime(base::TimeDelta now_playing
,
156 base::TimeDelta last_buffered
,
158 // Add the |last_buffered| value for PTS. For video it is the same as
159 // |now_playing| and is equal to PTS, for audio |last_buffered| should
164 pts_stat_
.AddValue(last_buffered
);
166 if (stop_request_time_
!= kNoTimestamp() &&
167 now_playing
>= stop_request_time_
) {
168 stop_request_time_
= kNoTimestamp();
169 decoder_
->RequestToStop();
173 void OnVideoSizeChanged(const gfx::Size
& video_size
) {
174 video_size_
= video_size
;
177 void OnVideoCodecCreated() {}
179 scoped_ptr
<MediaCodecDecoder
> decoder_
;
180 scoped_ptr
<TestDataFactory
> data_factory_
;
181 Minimax
<base::TimeDelta
> pts_stat_
;
182 gfx::Size video_size_
;
185 bool is_timeout_expired() const { return is_timeout_expired_
; }
186 void SetTimeoutExpired(bool value
) { is_timeout_expired_
= value
; }
188 base::MessageLoop message_loop_
;
189 bool is_timeout_expired_
;
194 base::TimeDelta stop_request_time_
;
196 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
197 DataAvailableCallback data_available_cb_
;
198 scoped_refptr
<gfx::SurfaceTexture
> surface_texture_
;
200 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoderTest
);
203 MediaCodecDecoderTest::MediaCodecDecoderTest()
204 : is_timeout_expired_(false),
205 is_prefetched_(false),
208 stop_request_time_(kNoTimestamp()),
209 task_runner_(base::ThreadTaskRunnerHandle::Get()) {
212 MediaCodecDecoderTest::~MediaCodecDecoderTest() {}
214 bool MediaCodecDecoderTest::WaitForCondition(const Predicate
& condition
,
215 const base::TimeDelta
& timeout
) {
216 // Let the message_loop_ process events.
217 // We start the timer and RunUntilIdle() until it signals.
219 SetTimeoutExpired(false);
221 base::Timer
timer(false, false);
222 timer
.Start(FROM_HERE
, timeout
,
223 base::Bind(&MediaCodecDecoderTest::SetTimeoutExpired
,
224 base::Unretained(this), true));
227 if (condition
.Run()) {
231 message_loop_
.RunUntilIdle();
232 } while (!is_timeout_expired());
234 DCHECK(!timer
.IsRunning());
238 void MediaCodecDecoderTest::CreateAudioDecoder() {
239 decoder_
= scoped_ptr
<MediaCodecDecoder
>(new MediaCodecAudioDecoder(
240 task_runner_
, base::Bind(&MediaCodecDecoderTest::OnDataRequested
,
241 base::Unretained(this)),
242 base::Bind(&MediaCodecDecoderTest::OnStarvation
, base::Unretained(this)),
243 base::Bind(&MediaCodecDecoderTest::OnDecoderDrained
,
244 base::Unretained(this)),
245 base::Bind(&MediaCodecDecoderTest::OnStopDone
, base::Unretained(this)),
246 base::Bind(&MediaCodecDecoderTest::OnError
, base::Unretained(this)),
247 base::Bind(&MediaCodecDecoderTest::OnUpdateCurrentTime
,
248 base::Unretained(this))));
250 data_available_cb_
= base::Bind(&MediaCodecDecoder::OnDemuxerDataAvailable
,
251 base::Unretained(decoder_
.get()));
254 void MediaCodecDecoderTest::CreateVideoDecoder() {
255 decoder_
= scoped_ptr
<MediaCodecDecoder
>(new MediaCodecVideoDecoder(
256 task_runner_
, base::Bind(&MediaCodecDecoderTest::OnDataRequested
,
257 base::Unretained(this)),
258 base::Bind(&MediaCodecDecoderTest::OnStarvation
, base::Unretained(this)),
259 base::Bind(&MediaCodecDecoderTest::OnDecoderDrained
,
260 base::Unretained(this)),
261 base::Bind(&MediaCodecDecoderTest::OnStopDone
, base::Unretained(this)),
262 base::Bind(&MediaCodecDecoderTest::OnError
, base::Unretained(this)),
263 base::Bind(&MediaCodecDecoderTest::OnUpdateCurrentTime
,
264 base::Unretained(this)),
265 base::Bind(&MediaCodecDecoderTest::OnVideoSizeChanged
,
266 base::Unretained(this)),
267 base::Bind(&MediaCodecDecoderTest::OnVideoCodecCreated
,
268 base::Unretained(this))));
270 data_available_cb_
= base::Bind(&MediaCodecDecoder::OnDemuxerDataAvailable
,
271 base::Unretained(decoder_
.get()));
274 void MediaCodecDecoderTest::OnDataRequested() {
279 base::TimeDelta delay
;
280 if (!data_factory_
->CreateChunk(&data
, &delay
))
283 task_runner_
->PostDelayedTask(FROM_HERE
, base::Bind(data_available_cb_
, data
),
287 void MediaCodecDecoderTest::SetVideoSurface() {
288 surface_texture_
= gfx::SurfaceTexture::Create(0);
289 gfx::ScopedJavaSurface
surface(surface_texture_
.get());
290 ASSERT_NE(nullptr, decoder_
.get());
291 MediaCodecVideoDecoder
* video_decoder
=
292 static_cast<MediaCodecVideoDecoder
*>(decoder_
.get());
293 video_decoder
->SetVideoSurface(surface
.Pass());
296 TEST_F(MediaCodecDecoderTest
, AudioPrefetch
) {
297 CreateAudioDecoder();
299 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
300 SetDataFactory(scoped_ptr
<TestDataFactory
>(new AudioFactory(duration
)));
302 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
303 base::Unretained(this), true));
305 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
306 base::Unretained(this))));
309 TEST_F(MediaCodecDecoderTest
, VideoPrefetch
) {
310 CreateVideoDecoder();
312 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
313 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
315 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
316 base::Unretained(this), true));
318 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
319 base::Unretained(this))));
322 TEST_F(MediaCodecDecoderTest
, AudioConfigureNoParams
) {
323 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
325 CreateAudioDecoder();
327 // Cannot configure without config parameters.
328 EXPECT_EQ(MediaCodecDecoder::kConfigFailure
, decoder_
->Configure());
331 TEST_F(MediaCodecDecoderTest
, AudioConfigureValidParams
) {
332 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
334 CreateAudioDecoder();
336 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
337 scoped_ptr
<AudioFactory
> factory(new AudioFactory(duration
));
338 decoder_
->SetDemuxerConfigs(factory
->GetConfigs());
340 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
343 TEST_F(MediaCodecDecoderTest
, VideoConfigureNoParams
) {
344 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
346 CreateVideoDecoder();
348 // decoder_->Configure() searches back for the key frame.
349 // We have to prefetch decoder.
351 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
352 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
354 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
355 base::Unretained(this), true));
357 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
358 base::Unretained(this))));
362 // Cannot configure without config parameters.
363 EXPECT_EQ(MediaCodecDecoder::kConfigFailure
, decoder_
->Configure());
366 TEST_F(MediaCodecDecoderTest
, VideoConfigureNoSurface
) {
367 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
369 CreateVideoDecoder();
371 // decoder_->Configure() searches back for the key frame.
372 // We have to prefetch decoder.
374 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
375 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
377 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
378 base::Unretained(this), true));
380 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
381 base::Unretained(this))));
383 decoder_
->SetDemuxerConfigs(GetConfigs());
385 // Surface is not set, Configure() should fail.
387 EXPECT_EQ(MediaCodecDecoder::kConfigFailure
, decoder_
->Configure());
390 TEST_F(MediaCodecDecoderTest
, VideoConfigureInvalidSurface
) {
391 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
393 CreateVideoDecoder();
395 // decoder_->Configure() searches back for the key frame.
396 // We have to prefetch decoder.
398 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
399 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
401 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
402 base::Unretained(this), true));
404 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
405 base::Unretained(this))));
407 decoder_
->SetDemuxerConfigs(GetConfigs());
409 // Prepare the surface.
410 scoped_refptr
<gfx::SurfaceTexture
> surface_texture(
411 gfx::SurfaceTexture::Create(0));
412 gfx::ScopedJavaSurface
surface(surface_texture
.get());
414 // Release the surface texture.
415 surface_texture
= NULL
;
417 MediaCodecVideoDecoder
* video_decoder
=
418 static_cast<MediaCodecVideoDecoder
*>(decoder_
.get());
419 video_decoder
->SetVideoSurface(surface
.Pass());
421 EXPECT_EQ(MediaCodecDecoder::kConfigFailure
, decoder_
->Configure());
424 TEST_F(MediaCodecDecoderTest
, VideoConfigureValidParams
) {
425 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
427 CreateVideoDecoder();
429 // decoder_->Configure() searches back for the key frame.
430 // We have to prefetch decoder.
432 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
433 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
435 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
436 base::Unretained(this), true));
438 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
439 base::Unretained(this))));
441 decoder_
->SetDemuxerConfigs(GetConfigs());
445 // Now we can expect Configure() to succeed.
447 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
450 TEST_F(MediaCodecDecoderTest
, AudioStartWithoutConfigure
) {
451 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
453 CreateAudioDecoder();
455 // Decoder has to be prefetched and configured before the start.
457 // Wrong state: not prefetched
458 EXPECT_FALSE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
461 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
462 SetDataFactory(scoped_ptr
<AudioFactory
>(new AudioFactory(duration
)));
464 // Prefetch to avoid starvation at the beginning of playback.
465 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
466 base::Unretained(this), true));
468 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
469 base::Unretained(this))));
471 // Still, decoder is not configured.
472 EXPECT_FALSE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
475 // http://crbug.com/518900
476 TEST_F(MediaCodecDecoderTest
, AudioPlayTillCompletion
) {
477 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
479 DVLOG(0) << "AudioPlayTillCompletion started";
481 CreateAudioDecoder();
483 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
484 base::TimeDelta timeout
= base::TimeDelta::FromMilliseconds(1500);
486 SetDataFactory(scoped_ptr
<AudioFactory
>(new AudioFactory(duration
)));
488 // Prefetch to avoid starvation at the beginning of playback.
489 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
490 base::Unretained(this), true));
492 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
493 base::Unretained(this))));
495 decoder_
->SetDemuxerConfigs(GetConfigs());
497 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
499 EXPECT_TRUE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
501 EXPECT_TRUE(WaitForCondition(
502 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this)),
505 EXPECT_TRUE(decoder_
->IsStopped());
506 EXPECT_TRUE(decoder_
->IsCompleted());
508 // Last buffered timestamp should be no less than PTS.
509 // The number of hits in pts_stat_ depends on the preroll implementation.
510 // We might not report the time for the first buffer after preroll that
511 // is written to the audio track. pts_stat_.num_values() is either 21 or 22.
512 EXPECT_LE(21, pts_stat_
.num_values());
513 EXPECT_LE(data_factory_
->last_pts(), pts_stat_
.max());
515 DVLOG(0) << "AudioPlayTillCompletion stopping";
518 TEST_F(MediaCodecDecoderTest
, VideoPlayTillCompletion
) {
519 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
521 CreateVideoDecoder();
523 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
524 // The first output frame might come out with significant delay. Apparently
525 // the codec does initial configuration at this time. We increase the timeout
526 // to leave a room of 1 second for this initial configuration.
527 base::TimeDelta timeout
= base::TimeDelta::FromMilliseconds(1500);
528 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
531 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
532 base::Unretained(this), true));
534 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
535 base::Unretained(this))));
537 decoder_
->SetDemuxerConfigs(GetConfigs());
541 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
543 EXPECT_TRUE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
545 EXPECT_TRUE(WaitForCondition(
546 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this)),
549 EXPECT_TRUE(decoder_
->IsStopped());
550 EXPECT_TRUE(decoder_
->IsCompleted());
552 EXPECT_EQ(26, pts_stat_
.num_values());
553 EXPECT_EQ(data_factory_
->last_pts(), pts_stat_
.max());
556 TEST_F(MediaCodecDecoderTest
, VideoStopAndResume
) {
557 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
559 CreateVideoDecoder();
561 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(500);
562 base::TimeDelta stop_request_time
= base::TimeDelta::FromMilliseconds(200);
563 base::TimeDelta timeout
= base::TimeDelta::FromMilliseconds(1000);
565 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
568 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
569 base::Unretained(this), true));
571 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
572 base::Unretained(this))));
574 decoder_
->SetDemuxerConfigs(GetConfigs());
578 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
580 SetStopRequestAtTime(stop_request_time
);
582 // Start from the beginning.
583 EXPECT_TRUE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
585 EXPECT_TRUE(WaitForCondition(
586 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this)),
589 EXPECT_TRUE(decoder_
->IsStopped());
590 EXPECT_FALSE(decoder_
->IsCompleted());
592 base::TimeDelta last_pts
= pts_stat_
.max();
594 EXPECT_GE(last_pts
, stop_request_time
);
596 // Resume playback from last_pts:
598 SetPrefetched(false);
602 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
603 base::Unretained(this), true));
605 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
606 base::Unretained(this))));
609 EXPECT_TRUE(decoder_
->Start(last_pts
));
611 // Wait till completion.
612 EXPECT_TRUE(WaitForCondition(
613 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this)),
616 EXPECT_TRUE(decoder_
->IsStopped());
617 EXPECT_TRUE(decoder_
->IsCompleted());
619 // We should not skip frames in this process.
620 EXPECT_EQ(26, pts_stat_
.num_values());
621 EXPECT_EQ(data_factory_
->last_pts(), pts_stat_
.max());
624 // http://crbug.com/518900
625 TEST_F(MediaCodecDecoderTest
, DISABLED_AudioStarvationAndStop
) {
626 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
628 CreateAudioDecoder();
630 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(200);
631 base::TimeDelta timeout
= base::TimeDelta::FromMilliseconds(400);
633 AudioFactory
* factory
= new AudioFactory(duration
);
634 factory
->SetStarvationMode(true);
635 SetDataFactory(scoped_ptr
<AudioFactory
>(factory
));
638 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
639 base::Unretained(this), true));
641 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
642 base::Unretained(this))));
645 decoder_
->SetDemuxerConfigs(GetConfigs());
647 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
650 EXPECT_TRUE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
652 // Wait for starvation.
653 EXPECT_TRUE(WaitForCondition(
654 base::Bind(&MediaCodecDecoderTest::is_starved
, base::Unretained(this)),
657 EXPECT_FALSE(decoder_
->IsStopped());
658 EXPECT_FALSE(decoder_
->IsCompleted());
660 EXPECT_GT(pts_stat_
.num_values(), 0);
662 // After starvation we should be able to stop decoder.
663 decoder_
->RequestToStop();
665 EXPECT_TRUE(WaitForCondition(
666 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this))));
668 EXPECT_TRUE(decoder_
->IsStopped());
669 EXPECT_FALSE(decoder_
->IsCompleted());
672 TEST_F(MediaCodecDecoderTest
, VideoFirstUnitIsReconfig
) {
673 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
675 // Test that the kConfigChanged unit that comes before the first data unit
676 // gets processed, i.e. is not lost.
678 CreateVideoDecoder();
680 base::TimeDelta duration
= base::TimeDelta::FromMilliseconds(200);
681 base::TimeDelta timeout
= base::TimeDelta::FromMilliseconds(1000);
682 SetDataFactory(scoped_ptr
<VideoFactory
>(new VideoFactory(duration
)));
684 // Ask factory to produce initial configuration unit. The configuraton will
685 // be factory.GetConfigs().
686 data_factory_
->RequestInitialConfigs();
688 // Create am alternative configuration (we just alter video size).
689 DemuxerConfigs alt_configs
= data_factory_
->GetConfigs();
690 alt_configs
.video_size
= gfx::Size(100, 100);
692 // Pass the alternative configuration to decoder.
693 decoder_
->SetDemuxerConfigs(alt_configs
);
696 decoder_
->Prefetch(base::Bind(&MediaCodecDecoderTest::SetPrefetched
,
697 base::Unretained(this), true));
699 EXPECT_TRUE(WaitForCondition(base::Bind(&MediaCodecDecoderTest::is_prefetched
,
700 base::Unretained(this))));
702 // Current implementation reports the new video size after
703 // SetDemuxerConfigs(), verify that it is alt size.
704 EXPECT_EQ(alt_configs
.video_size
, video_size_
);
709 EXPECT_EQ(MediaCodecDecoder::kConfigOk
, decoder_
->Configure());
712 EXPECT_TRUE(decoder_
->Start(base::TimeDelta::FromMilliseconds(0)));
714 // Wait for completion.
715 EXPECT_TRUE(WaitForCondition(
716 base::Bind(&MediaCodecDecoderTest::is_stopped
, base::Unretained(this)),
719 EXPECT_TRUE(decoder_
->IsStopped());
720 EXPECT_TRUE(decoder_
->IsCompleted());
721 EXPECT_EQ(data_factory_
->last_pts(), pts_stat_
.max());
723 // Check that the reported video size is the one from the in-stream configs.
724 EXPECT_EQ(data_factory_
->GetConfigs().video_size
, video_size_
);