Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1333)

Side by Side Diff: media/base/android/media_codec_player_unittest.cc

Issue 2276343005: Delete MediaCodecPlayer, it's time! (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/android/media_codec_player.cc ('k') | media/base/android/media_task_runner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "media/base/android/media_codec_player.h"
6
7 #include <stdint.h>
8 #include <memory>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/timer/timer.h"
16 #include "media/base/android/demuxer_android.h"
17 #include "media/base/android/media_codec_util.h"
18 #include "media/base/android/media_player_manager.h"
19 #include "media/base/android/media_task_runner.h"
20 #include "media/base/android/sdk_media_codec_bridge.h"
21 #include "media/base/android/test_data_factory.h"
22 #include "media/base/android/test_statistics.h"
23 #include "media/base/timestamp_constants.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/gl/android/surface_texture.h"
26
27 namespace media {
28
29 #define RUN_ON_MEDIA_THREAD(CLASS, METHOD, ...) \
30 do { \
31 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \
32 GetMediaTaskRunner()->PostTask( \
33 FROM_HERE, \
34 base::Bind(&CLASS::METHOD, base::Unretained(this), ##__VA_ARGS__)); \
35 return; \
36 } \
37 } while (0)
38
39 namespace {
40
41 const base::TimeDelta kDefaultTimeout = base::TimeDelta::FromMilliseconds(200);
42 const base::TimeDelta kAudioFramePeriod =
43 base::TimeDelta::FromSecondsD(1024.0 / 44100); // 1024 samples @ 44100 Hz
44 const base::TimeDelta kVideoFramePeriod = base::TimeDelta::FromMilliseconds(20);
45
46 enum Flags {
47 kAlwaysReconfigAudio = 0x1,
48 kAlwaysReconfigVideo = 0x2,
49 };
50
51 // The predicate that always returns false, used for WaitForDelay implementation
52 bool AlwaysFalse() {
53 return false;
54 }
55
56 // The method used to compare two time values of type T in expectations.
57 // Type T requires that a difference of type base::TimeDelta is defined.
58 template <typename T>
59 bool AlmostEqual(T a, T b, double tolerance_ms) {
60 return (a - b).magnitude().InMilliseconds() <= tolerance_ms;
61 }
62
63 // A helper function to calculate the expected number of frames.
64 int GetFrameCount(base::TimeDelta duration,
65 base::TimeDelta frame_period,
66 int num_reconfigs) {
67 // A chunk has 4 access units. The last unit timestamp must exceed the
68 // duration. Last chunk has 3 regular access units and one stand-alone EOS
69 // unit that we do not count.
70
71 // Number of time intervals to exceed duration.
72 int num_intervals = duration / frame_period + 1.0;
73
74 // To cover these intervals we need one extra unit at the beginning and a one
75 // for each reconfiguration.
76 int num_units = num_intervals + 1 + num_reconfigs;
77
78 // Number of 4-unit chunks that hold these units:
79 int num_chunks = (num_units + 3) / 4;
80
81 // Altogether these chunks hold 4*num_chunks units, but we do not count
82 // reconfiguration units and last EOS as frames.
83 return 4 * num_chunks - 1 - num_reconfigs;
84 }
85
86 // Mock of MediaPlayerManager for testing purpose.
87
88 class MockMediaPlayerManager : public MediaPlayerManager {
89 public:
90 MockMediaPlayerManager()
91 : playback_allowed_(true),
92 playback_completed_(false),
93 num_seeks_completed_(0),
94 num_audio_codecs_created_(0),
95 num_video_codecs_created_(0),
96 weak_ptr_factory_(this) {}
97 ~MockMediaPlayerManager() override {}
98
99 MediaResourceGetter* GetMediaResourceGetter() override { return nullptr; }
100 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return nullptr; }
101
102 // Regular time update callback, reports current playback time to
103 // MediaPlayerManager.
104 void OnTimeUpdate(int player_id,
105 base::TimeDelta current_timestamp,
106 base::TimeTicks current_time_ticks) override {
107 pts_stat_.AddValue(current_timestamp);
108 }
109
110 void OnMediaMetadataChanged(int player_id,
111 base::TimeDelta duration,
112 int width,
113 int height,
114 bool success) override {
115 media_metadata_.duration = duration;
116 media_metadata_.width = width;
117 media_metadata_.height = height;
118 media_metadata_.modified = true;
119 }
120
121 void OnPlaybackComplete(int player_id) override {
122 playback_completed_ = true;
123 }
124
125 void OnMediaInterrupted(int player_id) override {}
126 void OnBufferingUpdate(int player_id, int percentage) override {}
127 void OnSeekComplete(int player_id,
128 const base::TimeDelta& current_time) override {
129 ++num_seeks_completed_;
130 }
131 void OnError(int player_id, int error) override {}
132 void OnVideoSizeChanged(int player_id, int width, int height) override {}
133 void OnWaitingForDecryptionKey(int player_id) override {}
134 MediaPlayerAndroid* GetFullscreenPlayer() override { return nullptr; }
135 MediaPlayerAndroid* GetPlayer(int player_id) override { return nullptr; }
136 bool RequestPlay(int player_id,
137 base::TimeDelta duration,
138 bool has_audio) override {
139 return playback_allowed_;
140 }
141
142 void OnMediaResourcesRequested(int player_id) {}
143
144 // Time update callback that reports the internal progress of the stream.
145 // Implementation dependent, used for testing only.
146 void OnDecodersTimeUpdate(DemuxerStream::Type stream_type,
147 base::TimeDelta now_playing,
148 base::TimeDelta last_buffered) {
149 render_stat_[stream_type].AddValue(
150 PTSTime(now_playing, base::TimeTicks::Now()));
151 }
152
153 // Notification called on MediaCodec creation.
154 // Implementation dependent, used for testing only.
155 void OnMediaCodecCreated(DemuxerStream::Type stream_type) {
156 if (stream_type == DemuxerStream::AUDIO)
157 ++num_audio_codecs_created_;
158 else if (stream_type == DemuxerStream::VIDEO)
159 ++num_video_codecs_created_;
160 }
161
162 // First frame information
163 base::TimeDelta FirstFramePTS(DemuxerStream::Type stream_type) const {
164 return render_stat_[stream_type].min().pts;
165 }
166 base::TimeTicks FirstFrameTime(DemuxerStream::Type stream_type) const {
167 return render_stat_[stream_type].min().time;
168 }
169
170 base::WeakPtr<MockMediaPlayerManager> GetWeakPtr() {
171 return weak_ptr_factory_.GetWeakPtr();
172 }
173
174 void SetPlaybackAllowed(bool value) { playback_allowed_ = value; }
175
176 // Conditions to wait for.
177 bool IsMetadataChanged() const { return media_metadata_.modified; }
178 bool IsPlaybackCompleted() const { return playback_completed_; }
179 bool IsPlaybackStarted() const { return pts_stat_.num_values() > 0; }
180 bool IsPlaybackBeyondPosition(const base::TimeDelta& pts) const {
181 return pts_stat_.max() > pts;
182 }
183 bool IsSeekCompleted() const { return num_seeks_completed_ > 0; }
184 bool HasFirstFrame(DemuxerStream::Type stream_type) const {
185 return render_stat_[stream_type].num_values() != 0;
186 }
187
188 int num_audio_codecs_created() const { return num_audio_codecs_created_; }
189 int num_video_codecs_created() const { return num_video_codecs_created_; }
190
191 struct MediaMetadata {
192 base::TimeDelta duration;
193 int width;
194 int height;
195 bool modified;
196 MediaMetadata() : width(0), height(0), modified(false) {}
197 };
198 MediaMetadata media_metadata_;
199
200 struct PTSTime {
201 base::TimeDelta pts;
202 base::TimeTicks time;
203
204 PTSTime() : pts(), time() {}
205 PTSTime(base::TimeDelta p, base::TimeTicks t) : pts(p), time(t) {}
206 bool is_null() const { return time.is_null(); }
207 bool operator<(const PTSTime& rhs) const { return time < rhs.time; }
208 };
209 Minimax<PTSTime> render_stat_[DemuxerStream::NUM_TYPES];
210
211 Minimax<base::TimeDelta> pts_stat_;
212
213 private:
214 bool playback_allowed_;
215 bool playback_completed_;
216 int num_seeks_completed_;
217 int num_audio_codecs_created_;
218 int num_video_codecs_created_;
219
220 base::WeakPtrFactory<MockMediaPlayerManager> weak_ptr_factory_;
221
222 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
223 };
224
225 // Helper method that creates demuxer configuration.
226
227 DemuxerConfigs CreateAudioVideoConfigs(const base::TimeDelta& duration,
228 const gfx::Size& video_size) {
229 DemuxerConfigs configs =
230 TestDataFactory::CreateAudioConfigs(kCodecAAC, duration);
231 configs.video_codec = kCodecVP8;
232 configs.video_size = video_size;
233 configs.is_video_encrypted = false;
234 return configs;
235 }
236
237 DemuxerConfigs CreateAudioVideoConfigs(const TestDataFactory* audio,
238 const TestDataFactory* video) {
239 DemuxerConfigs result = audio->GetConfigs();
240 DemuxerConfigs vconf = video->GetConfigs();
241
242 result.video_codec = vconf.video_codec;
243 result.video_size = vconf.video_size;
244 result.is_video_encrypted = vconf.is_video_encrypted;
245 result.duration = std::max(result.duration, vconf.duration);
246 return result;
247 }
248
249 // AudioFactory creates data chunks that simulate audio stream from demuxer.
250
251 class AudioFactory : public TestDataFactory {
252 public:
253 AudioFactory(base::TimeDelta duration)
254 : TestDataFactory("aac-44100-packet-%d", duration, kAudioFramePeriod) {}
255
256 DemuxerConfigs GetConfigs() const override {
257 return TestDataFactory::CreateAudioConfigs(kCodecAAC, duration_);
258 }
259
260 protected:
261 void ModifyChunk(DemuxerData* chunk) override {
262 DCHECK(chunk);
263 for (AccessUnit& unit : chunk->access_units) {
264 if (!unit.data.empty())
265 unit.is_key_frame = true;
266 }
267 }
268 };
269
270 // VideoFactory creates a video stream from demuxer.
271
272 class VideoFactory : public TestDataFactory {
273 public:
274 VideoFactory(base::TimeDelta duration)
275 : TestDataFactory("h264-320x180-frame-%d", duration, kVideoFramePeriod),
276 key_frame_requested_(true) {}
277
278 DemuxerConfigs GetConfigs() const override {
279 return TestDataFactory::CreateVideoConfigs(kCodecH264, duration_,
280 gfx::Size(320, 180));
281 }
282
283 void RequestKeyFrame() { key_frame_requested_ = true; }
284
285 protected:
286 void ModifyChunk(DemuxerData* chunk) override {
287 // The frames are taken from High profile and some are B-frames.
288 // The first 4 frames appear in the file in the following order:
289 //
290 // Frames: I P B P
291 // Decoding order: 0 1 2 3
292 // Presentation order: 0 2 1 4(3)
293 //
294 // I keep the last PTS to be 3 for simplicity.
295
296 // If the chunk contains EOS, it should not break the presentation order.
297 // For instance, the following chunk is ok:
298 //
299 // Frames: I P B EOS
300 // Decoding order: 0 1 2 -
301 // Presentation order: 0 2 1 -
302 //
303 // while this might cause decoder to block:
304 //
305 // Frames: I P EOS
306 // Decoding order: 0 1 -
307 // Presentation order: 0 2 - <------- might wait for the B frame forever
308 //
309 // With current base class implementation that always has EOS at the 4th
310 // place we are covered (http://crbug.com/526755)
311
312 DCHECK(chunk);
313 DCHECK(chunk->access_units.size() == 4);
314
315 // Swap pts for second and third frames.
316 base::TimeDelta tmp = chunk->access_units[1].timestamp;
317 chunk->access_units[1].timestamp = chunk->access_units[2].timestamp;
318 chunk->access_units[2].timestamp = tmp;
319
320 // Make first frame a key frame.
321 if (key_frame_requested_) {
322 chunk->access_units[0].is_key_frame = true;
323 key_frame_requested_ = false;
324 }
325 }
326
327 private:
328 bool key_frame_requested_;
329 };
330
331 // Mock of DemuxerAndroid for testing purpose.
332
333 class MockDemuxerAndroid : public DemuxerAndroid {
334 public:
335 MockDemuxerAndroid(base::MessageLoop* ui_message_loop);
336 ~MockDemuxerAndroid() override;
337
338 // DemuxerAndroid implementation
339 void Initialize(DemuxerAndroidClient* client) override;
340 void RequestDemuxerData(DemuxerStream::Type type) override;
341 void RequestDemuxerSeek(const base::TimeDelta& seek_request,
342 bool is_browser_seek) override;
343
344 // Helper methods that enable using a weak pointer when posting to the player.
345 void OnDemuxerDataAvailable(const DemuxerData& chunk);
346 void OnDemuxerSeekDone(base::TimeDelta reported_seek_time);
347
348 // Sets the callback that is fired when demuxer is deleted (deletion
349 // happens on the Media thread).
350 void SetDemuxerDeletedCallback(base::Closure cb) { demuxer_deleted_cb_ = cb; }
351
352 // Sets the audio data factory.
353 void SetAudioFactory(std::unique_ptr<AudioFactory> factory) {
354 audio_factory_ = std::move(factory);
355 }
356
357 // Sets the video data factory.
358 void SetVideoFactory(std::unique_ptr<VideoFactory> factory) {
359 video_factory_ = std::move(factory);
360 }
361
362 // Accessors for data factories.
363 AudioFactory* audio_factory() const { return audio_factory_.get(); }
364 VideoFactory* video_factory() const { return video_factory_.get(); }
365
366 // Set the preroll interval after seek for audio stream.
367 void SetAudioPrerollInterval(base::TimeDelta value) {
368 audio_preroll_interval_ = value;
369 }
370
371 // Set the preroll interval after seek for video stream.
372 void SetVideoPrerollInterval(base::TimeDelta value) {
373 video_preroll_interval_ = value;
374 }
375
376 // Sets the delay in OnDemuxerSeekDone response.
377 void SetSeekDoneDelay(base::TimeDelta delay) { seek_done_delay_ = delay; }
378
379 // Post DemuxerConfigs to the client (i.e. the player) on correct thread.
380 void PostConfigs(const DemuxerConfigs& configs);
381
382 // Post DemuxerConfigs derived from data factories that has been set.
383 void PostInternalConfigs();
384
385 // Conditions to wait for.
386 bool IsInitialized() const { return client_; }
387 bool HasPendingConfigs() const { return !!pending_configs_; }
388 bool ReceivedSeekRequest() const { return num_seeks_ > 0; }
389 bool ReceivedBrowserSeekRequest() const { return num_browser_seeks_ > 0; }
390
391 private:
392 base::MessageLoop* ui_message_loop_;
393 DemuxerAndroidClient* client_;
394
395 std::unique_ptr<DemuxerConfigs> pending_configs_;
396 std::unique_ptr<AudioFactory> audio_factory_;
397 std::unique_ptr<VideoFactory> video_factory_;
398
399 base::TimeDelta audio_preroll_interval_;
400 base::TimeDelta video_preroll_interval_;
401 base::TimeDelta seek_done_delay_;
402
403 int num_seeks_;
404 int num_browser_seeks_;
405
406 base::Closure demuxer_deleted_cb_;
407
408 // NOTE: WeakPtrFactory must be the last data member to be destroyed first.
409 base::WeakPtrFactory<MockDemuxerAndroid> weak_factory_;
410
411 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid);
412 };
413
414 MockDemuxerAndroid::MockDemuxerAndroid(base::MessageLoop* ui_message_loop)
415 : ui_message_loop_(ui_message_loop),
416 client_(nullptr),
417 num_seeks_(0),
418 num_browser_seeks_(0),
419 weak_factory_(this) {}
420
421 MockDemuxerAndroid::~MockDemuxerAndroid() {
422 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__;
423 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
424
425 if (!demuxer_deleted_cb_.is_null())
426 ui_message_loop_->PostTask(FROM_HERE, demuxer_deleted_cb_);
427 }
428
429 void MockDemuxerAndroid::Initialize(DemuxerAndroidClient* client) {
430 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__;
431 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
432
433 client_ = client;
434 if (pending_configs_)
435 client_->OnDemuxerConfigsAvailable(*pending_configs_);
436 }
437
438 void MockDemuxerAndroid::RequestDemuxerData(DemuxerStream::Type type) {
439 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
440
441 DemuxerData chunk;
442 base::TimeDelta delay;
443
444 bool created = false;
445 if (type == DemuxerStream::AUDIO && audio_factory_)
446 created = audio_factory_->CreateChunk(&chunk, &delay);
447 else if (type == DemuxerStream::VIDEO && video_factory_)
448 created = video_factory_->CreateChunk(&chunk, &delay);
449
450 if (!created)
451 return;
452
453 // Request key frame after |kConfigChanged|
454 if (type == DemuxerStream::VIDEO && !chunk.demuxer_configs.empty())
455 video_factory_->RequestKeyFrame();
456
457 chunk.type = type;
458
459 // Post to the Media thread. Use the weak pointer to prevent the data arrival
460 // after the player has been deleted.
461 GetMediaTaskRunner()->PostDelayedTask(
462 FROM_HERE, base::Bind(&MockDemuxerAndroid::OnDemuxerDataAvailable,
463 weak_factory_.GetWeakPtr(), chunk),
464 delay);
465 }
466
467 void MockDemuxerAndroid::RequestDemuxerSeek(const base::TimeDelta& seek_request,
468 bool is_browser_seek) {
469 // Tell data factories to start next chunk with the new timestamp.
470 if (audio_factory_) {
471 base::TimeDelta time_to_seek =
472 std::max(base::TimeDelta(), seek_request - audio_preroll_interval_);
473 audio_factory_->SeekTo(time_to_seek);
474 }
475 if (video_factory_) {
476 base::TimeDelta time_to_seek =
477 std::max(base::TimeDelta(), seek_request - video_preroll_interval_);
478 video_factory_->SeekTo(time_to_seek);
479 video_factory_->RequestKeyFrame();
480 }
481
482 ++num_seeks_;
483 if (is_browser_seek)
484 ++num_browser_seeks_;
485
486 // Post OnDemuxerSeekDone() to the player.
487 DCHECK(client_);
488 base::TimeDelta reported_seek_time =
489 is_browser_seek ? seek_request : kNoTimestamp;
490 GetMediaTaskRunner()->PostDelayedTask(
491 FROM_HERE, base::Bind(&MockDemuxerAndroid::OnDemuxerSeekDone,
492 weak_factory_.GetWeakPtr(), reported_seek_time),
493 seek_done_delay_);
494 }
495
496 void MockDemuxerAndroid::OnDemuxerDataAvailable(const DemuxerData& chunk) {
497 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
498 DCHECK(client_);
499 client_->OnDemuxerDataAvailable(chunk);
500 }
501
502 void MockDemuxerAndroid::OnDemuxerSeekDone(base::TimeDelta reported_seek_time) {
503 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
504 DCHECK(client_);
505 client_->OnDemuxerSeekDone(reported_seek_time);
506 }
507
508 void MockDemuxerAndroid::PostConfigs(const DemuxerConfigs& configs) {
509 RUN_ON_MEDIA_THREAD(MockDemuxerAndroid, PostConfigs, configs);
510
511 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__;
512
513 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
514
515 if (client_)
516 client_->OnDemuxerConfigsAvailable(configs);
517 else
518 pending_configs_ = base::MakeUnique<DemuxerConfigs>(configs);
519 }
520
521 void MockDemuxerAndroid::PostInternalConfigs() {
522 ASSERT_TRUE(audio_factory_ || video_factory_);
523
524 if (audio_factory_ && video_factory_) {
525 PostConfigs(
526 CreateAudioVideoConfigs(audio_factory_.get(), video_factory_.get()));
527 } else if (audio_factory_) {
528 PostConfigs(audio_factory_->GetConfigs());
529 } else if (video_factory_) {
530 PostConfigs(video_factory_->GetConfigs());
531 }
532 }
533
534 } // namespace (anonymous)
535
536 // The test fixture for MediaCodecPlayer
537
538 class MediaCodecPlayerTest : public testing::Test {
539 public:
540 MediaCodecPlayerTest();
541
542 // Conditions to wait for.
543 bool IsPaused() const { return !(player_ && player_->IsPlaying()); }
544
545 protected:
546 typedef base::Callback<bool()> Predicate;
547
548 void TearDown() override;
549
550 void CreatePlayer();
551 void SetVideoSurface();
552 void SetVideoSurfaceB();
553 void RemoveVideoSurface();
554
555 // Waits for condition to become true or for timeout to expire.
556 // Returns true if the condition becomes true.
557 bool WaitForCondition(const Predicate& condition,
558 const base::TimeDelta& timeout = kDefaultTimeout);
559
560 // Waits for timeout to expire.
561 void WaitForDelay(const base::TimeDelta& timeout);
562
563 // Waits till playback position as determined by maximal reported pts
564 // reaches the given value or for timeout to expire. Returns true if the
565 // playback has passed the given position.
566 bool WaitForPlaybackBeyondPosition(
567 const base::TimeDelta& pts,
568 const base::TimeDelta& timeout = kDefaultTimeout);
569
570 // Helper method that starts video only stream. Waits till it actually
571 // started.
572 bool StartVideoPlayback(base::TimeDelta duration, const char* test_name);
573
574 // Helper method that starts audio and video streams.
575 bool StartAVPlayback(std::unique_ptr<AudioFactory> audio_factory,
576 std::unique_ptr<VideoFactory> video_factory,
577 uint32_t flags,
578 const char* test_name);
579
580 // Helper method that starts audio and video streams with preroll.
581 // The preroll is achieved by setting significant video preroll interval
582 // so video will have to catch up with audio. To make room for this interval
583 // the Start() command is preceded by SeekTo().
584 bool StartAVSeekAndPreroll(std::unique_ptr<AudioFactory> audio_factory,
585 std::unique_ptr<VideoFactory> video_factory,
586 base::TimeDelta seek_position,
587 uint32_t flags,
588 const char* test_name);
589
590 // Callback sent when demuxer is being deleted.
591 void OnDemuxerDeleted() { demuxer_ = nullptr; }
592
593 bool IsDemuxerDeleted() const { return !demuxer_; }
594
595 base::MessageLoop message_loop_;
596 MockMediaPlayerManager manager_;
597 MockDemuxerAndroid* demuxer_; // owned by player_
598 scoped_refptr<gl::SurfaceTexture> surface_texture_a_;
599 scoped_refptr<gl::SurfaceTexture> surface_texture_b_;
600 MediaCodecPlayer* player_; // raw pointer due to DeleteOnCorrectThread()
601
602 private:
603 bool is_timeout_expired() const { return is_timeout_expired_; }
604 void SetTimeoutExpired(bool value) { is_timeout_expired_ = value; }
605
606 bool is_timeout_expired_;
607
608 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayerTest);
609 };
610
611 MediaCodecPlayerTest::MediaCodecPlayerTest()
612 : demuxer_(new MockDemuxerAndroid(&message_loop_)),
613 player_(nullptr),
614 is_timeout_expired_(false) {}
615
616 void MediaCodecPlayerTest::TearDown() {
617 DVLOG(1) << __FUNCTION__;
618
619 // Wait till the player is destroyed on the Media thread.
620
621 if (player_) {
622 // The player deletes the demuxer on the Media thread. The demuxer's
623 // destructor sends a notification to the UI thread. When this notification
624 // arrives we can conclude that player started destroying its member
625 // variables. By that time the media codecs should have been released.
626
627 DCHECK(demuxer_);
628 demuxer_->SetDemuxerDeletedCallback(base::Bind(
629 &MediaCodecPlayerTest::OnDemuxerDeleted, base::Unretained(this)));
630
631 player_->DeleteOnCorrectThread();
632
633 EXPECT_TRUE(
634 WaitForCondition(base::Bind(&MediaCodecPlayerTest::IsDemuxerDeleted,
635 base::Unretained(this)),
636 base::TimeDelta::FromMilliseconds(500)));
637
638 player_ = nullptr;
639 }
640 }
641
642 void MediaCodecPlayerTest::CreatePlayer() {
643 DCHECK(demuxer_);
644 player_ = new MediaCodecPlayer(
645 0, // player_id
646 manager_.GetWeakPtr(),
647 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested,
648 base::Unretained(&manager_)),
649 base::WrapUnique(demuxer_), GURL(), kDefaultMediaSessionId);
650
651 DCHECK(player_);
652 }
653
654 void MediaCodecPlayerTest::SetVideoSurface() {
655 surface_texture_a_ = gl::SurfaceTexture::Create(0);
656 gl::ScopedJavaSurface surface(surface_texture_a_.get());
657
658 ASSERT_NE(nullptr, player_);
659 player_->SetVideoSurface(std::move(surface));
660 }
661
662 void MediaCodecPlayerTest::SetVideoSurfaceB() {
663 surface_texture_b_ = gl::SurfaceTexture::Create(1);
664 gl::ScopedJavaSurface surface(surface_texture_b_.get());
665
666 ASSERT_NE(nullptr, player_);
667 player_->SetVideoSurface(std::move(surface));
668 }
669
670 void MediaCodecPlayerTest::RemoveVideoSurface() {
671 player_->SetVideoSurface(gl::ScopedJavaSurface());
672 surface_texture_a_ = NULL;
673 }
674
675 bool MediaCodecPlayerTest::WaitForCondition(const Predicate& condition,
676 const base::TimeDelta& timeout) {
677 // Let the message_loop_ process events.
678 // We start the timer and RunUntilIdle() until it signals.
679
680 SetTimeoutExpired(false);
681
682 base::Timer timer(false, false);
683 timer.Start(FROM_HERE, timeout,
684 base::Bind(&MediaCodecPlayerTest::SetTimeoutExpired,
685 base::Unretained(this), true));
686
687 do {
688 if (condition.Run()) {
689 timer.Stop();
690 return true;
691 }
692 message_loop_.RunUntilIdle();
693 } while (!is_timeout_expired());
694
695 DCHECK(!timer.IsRunning());
696 return false;
697 }
698
699 void MediaCodecPlayerTest::WaitForDelay(const base::TimeDelta& timeout) {
700 WaitForCondition(base::Bind(&AlwaysFalse), timeout);
701 }
702
703 bool MediaCodecPlayerTest::WaitForPlaybackBeyondPosition(
704 const base::TimeDelta& pts,
705 const base::TimeDelta& timeout) {
706 return WaitForCondition(
707 base::Bind(&MockMediaPlayerManager::IsPlaybackBeyondPosition,
708 base::Unretained(&manager_), pts),
709 timeout);
710 }
711
712 bool MediaCodecPlayerTest::StartVideoPlayback(base::TimeDelta duration,
713 const char* test_name) {
714 const base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
715
716 demuxer_->SetVideoFactory(base::MakeUnique<VideoFactory>(duration));
717
718 CreatePlayer();
719
720 // Wait till the player is initialized on media thread.
721 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
722 base::Unretained(demuxer_))));
723
724 if (!demuxer_->IsInitialized()) {
725 DVLOG(0) << test_name << ": demuxer is not initialized";
726 return false;
727 }
728
729 SetVideoSurface();
730
731 // Post configuration after the player has been initialized.
732 demuxer_->PostInternalConfigs();
733
734 // Start the player.
735 EXPECT_FALSE(manager_.IsPlaybackStarted());
736 player_->Start();
737
738 // Wait for playback to start.
739 EXPECT_TRUE(
740 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
741 base::Unretained(&manager_)),
742 start_timeout));
743
744 if (!manager_.IsPlaybackStarted()) {
745 DVLOG(0) << test_name << ": playback did not start";
746 return false;
747 }
748
749 return true;
750 }
751
752 bool MediaCodecPlayerTest::StartAVPlayback(
753 std::unique_ptr<AudioFactory> audio_factory,
754 std::unique_ptr<VideoFactory> video_factory,
755 uint32_t flags,
756 const char* test_name) {
757 demuxer_->SetAudioFactory(std::move(audio_factory));
758 demuxer_->SetVideoFactory(std::move(video_factory));
759
760 CreatePlayer();
761 SetVideoSurface();
762
763 // Wait till the player is initialized on media thread.
764 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
765 base::Unretained(demuxer_))));
766
767 if (!demuxer_->IsInitialized()) {
768 DVLOG(0) << test_name << ": demuxer is not initialized";
769 return false;
770 }
771
772 // Ask decoders to always reconfigure after the player has been initialized.
773 if (flags & kAlwaysReconfigAudio)
774 player_->SetAlwaysReconfigureForTests(DemuxerStream::AUDIO);
775 if (flags & kAlwaysReconfigVideo)
776 player_->SetAlwaysReconfigureForTests(DemuxerStream::VIDEO);
777
778 // Set a testing callback to receive PTS from decoders.
779 player_->SetDecodersTimeCallbackForTests(
780 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
781 base::Unretained(&manager_)));
782
783 // Set a testing callback to receive MediaCodec creation events from decoders.
784 player_->SetCodecCreatedCallbackForTests(
785 base::Bind(&MockMediaPlayerManager::OnMediaCodecCreated,
786 base::Unretained(&manager_)));
787
788 // Post configuration after the player has been initialized.
789 demuxer_->PostInternalConfigs();
790
791 // Start and wait for playback.
792 player_->Start();
793
794 // Wait till we start to play.
795 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(2000);
796
797 EXPECT_TRUE(
798 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
799 base::Unretained(&manager_)),
800 start_timeout));
801
802 if (!manager_.IsPlaybackStarted()) {
803 DVLOG(0) << test_name << ": playback did not start";
804 return false;
805 }
806
807 return true;
808 }
809
810 bool MediaCodecPlayerTest::StartAVSeekAndPreroll(
811 std::unique_ptr<AudioFactory> audio_factory,
812 std::unique_ptr<VideoFactory> video_factory,
813 base::TimeDelta seek_position,
814 uint32_t flags,
815 const char* test_name) {
816 // Initialize A/V playback
817
818 demuxer_->SetAudioFactory(std::move(audio_factory));
819 demuxer_->SetVideoFactory(std::move(video_factory));
820
821 CreatePlayer();
822 SetVideoSurface();
823
824 // Wait till the player is initialized on media thread.
825 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
826 base::Unretained(demuxer_))));
827
828 if (!demuxer_->IsInitialized()) {
829 DVLOG(0) << test_name << ": demuxer is not initialized";
830 return false;
831 }
832
833 // Ask decoders to always reconfigure after the player has been initialized.
834 if (flags & kAlwaysReconfigAudio)
835 player_->SetAlwaysReconfigureForTests(DemuxerStream::AUDIO);
836 if (flags & kAlwaysReconfigVideo)
837 player_->SetAlwaysReconfigureForTests(DemuxerStream::VIDEO);
838
839 // Set a testing callback to receive PTS from decoders.
840 player_->SetDecodersTimeCallbackForTests(
841 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
842 base::Unretained(&manager_)));
843
844 // Set a testing callback to receive MediaCodec creation events from decoders.
845 player_->SetCodecCreatedCallbackForTests(
846 base::Bind(&MockMediaPlayerManager::OnMediaCodecCreated,
847 base::Unretained(&manager_)));
848
849 // Post configuration after the player has been initialized.
850 demuxer_->PostInternalConfigs();
851
852 // Issue SeekTo().
853 player_->SeekTo(seek_position);
854
855 // Start the playback.
856 player_->Start();
857
858 // Wait till preroll starts.
859 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(2000);
860 EXPECT_TRUE(WaitForCondition(
861 base::Bind(&MediaCodecPlayer::IsPrerollingForTests,
862 base::Unretained(player_), DemuxerStream::VIDEO),
863 start_timeout));
864
865 if (!player_->IsPrerollingForTests(DemuxerStream::VIDEO)) {
866 DVLOG(0) << test_name << ": preroll did not happen for video";
867 return false;
868 }
869
870 return true;
871 }
872
873 TEST_F(MediaCodecPlayerTest, SetAudioConfigsBeforePlayerCreation) {
874 // Post configuration when there is no player yet.
875 EXPECT_EQ(nullptr, player_);
876
877 base::TimeDelta duration = base::TimeDelta::FromSeconds(10);
878
879 demuxer_->PostConfigs(
880 TestDataFactory::CreateAudioConfigs(kCodecAAC, duration));
881
882 // Wait until the configuration gets to the media thread.
883 EXPECT_TRUE(WaitForCondition(base::Bind(
884 &MockDemuxerAndroid::HasPendingConfigs, base::Unretained(demuxer_))));
885
886 // Then create the player.
887 CreatePlayer();
888
889 // Configuration should propagate through the player and to the manager.
890 EXPECT_TRUE(
891 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged,
892 base::Unretained(&manager_))));
893
894 EXPECT_EQ(duration, manager_.media_metadata_.duration);
895 EXPECT_EQ(0, manager_.media_metadata_.width);
896 EXPECT_EQ(0, manager_.media_metadata_.height);
897 }
898
899 TEST_F(MediaCodecPlayerTest, SetAudioConfigsAfterPlayerCreation) {
900 CreatePlayer();
901
902 // Wait till the player is initialized on media thread.
903 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
904 base::Unretained(demuxer_))));
905
906 // Post configuration after the player has been initialized.
907 base::TimeDelta duration = base::TimeDelta::FromSeconds(10);
908 demuxer_->PostConfigs(
909 TestDataFactory::CreateAudioConfigs(kCodecAAC, duration));
910
911 // Configuration should propagate through the player and to the manager.
912 EXPECT_TRUE(
913 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged,
914 base::Unretained(&manager_))));
915
916 EXPECT_EQ(duration, manager_.media_metadata_.duration);
917 EXPECT_EQ(0, manager_.media_metadata_.width);
918 EXPECT_EQ(0, manager_.media_metadata_.height);
919 }
920
921 TEST_F(MediaCodecPlayerTest, SetAudioVideoConfigsAfterPlayerCreation) {
922 CreatePlayer();
923
924 // Wait till the player is initialized on media thread.
925 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
926 base::Unretained(demuxer_))));
927
928 // Post configuration after the player has been initialized.
929 base::TimeDelta duration = base::TimeDelta::FromSeconds(10);
930 demuxer_->PostConfigs(CreateAudioVideoConfigs(duration, gfx::Size(320, 240)));
931
932 // Configuration should propagate through the player and to the manager.
933 EXPECT_TRUE(
934 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged,
935 base::Unretained(&manager_))));
936
937 EXPECT_EQ(duration, manager_.media_metadata_.duration);
938 EXPECT_EQ(320, manager_.media_metadata_.width);
939 EXPECT_EQ(240, manager_.media_metadata_.height);
940 }
941
942 TEST_F(MediaCodecPlayerTest, DISABLED_AudioPlayTillCompletion) {
943 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
944
945 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000);
946 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(2000);
947
948 demuxer_->SetAudioFactory(base::MakeUnique<AudioFactory>(duration));
949
950 CreatePlayer();
951
952 // Wait till the player is initialized on media thread.
953 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
954 base::Unretained(demuxer_))));
955
956 // Post configuration after the player has been initialized.
957 demuxer_->PostInternalConfigs();
958
959 EXPECT_FALSE(manager_.IsPlaybackCompleted());
960
961 player_->Start();
962
963 EXPECT_TRUE(
964 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
965 base::Unretained(&manager_)),
966 timeout));
967
968 // Current timestamp reflects "now playing" time. It might come with delay
969 // relative to the frame's PTS. Allow for 100 ms delay here.
970 base::TimeDelta audio_pts_delay = base::TimeDelta::FromMilliseconds(100);
971 EXPECT_LT(duration - audio_pts_delay, manager_.pts_stat_.max());
972 }
973
974 TEST_F(MediaCodecPlayerTest, AudioNoPermission) {
975 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
976
977 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000);
978 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
979
980 manager_.SetPlaybackAllowed(false);
981
982 demuxer_->SetAudioFactory(base::MakeUnique<AudioFactory>(duration));
983
984 CreatePlayer();
985
986 // Wait till the player is initialized on media thread.
987 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
988 base::Unretained(demuxer_))));
989
990 // Post configuration after the player has been initialized.
991 demuxer_->PostInternalConfigs();
992
993 EXPECT_FALSE(manager_.IsPlaybackCompleted());
994
995 player_->Start();
996
997 // Playback should not start.
998 EXPECT_FALSE(
999 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1000 base::Unretained(&manager_)),
1001 start_timeout));
1002 }
1003
1004 // crbug.com/618274
1005 #if defined(OS_ANDROID)
1006 #define MAYBE_VideoPlayTillCompletion DISABLED_VideoPlayTillCompletion
1007 #else
1008 #define MAYBE_VideoPlayTillCompletion VideoPlayTillCompletion
1009 #endif
1010 TEST_F(MediaCodecPlayerTest, MAYBE_VideoPlayTillCompletion) {
1011 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1012
1013 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(500);
1014 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(2000);
1015
1016 ASSERT_TRUE(StartVideoPlayback(duration, "VideoPlayTillCompletion"));
1017
1018 // Wait till completion.
1019 EXPECT_TRUE(
1020 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1021 base::Unretained(&manager_)),
1022 timeout));
1023
1024 EXPECT_LE(duration, manager_.pts_stat_.max());
1025 }
1026
1027 TEST_F(MediaCodecPlayerTest, VideoNoPermission) {
1028 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1029
1030 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(500);
1031 const base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
1032
1033 manager_.SetPlaybackAllowed(false);
1034
1035 demuxer_->SetVideoFactory(base::MakeUnique<VideoFactory>(duration));
1036
1037 CreatePlayer();
1038
1039 // Wait till the player is initialized on media thread.
1040 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1041 base::Unretained(demuxer_))));
1042
1043 SetVideoSurface();
1044
1045 // Post configuration after the player has been initialized.
1046 demuxer_->PostInternalConfigs();
1047
1048 // Start the player.
1049 EXPECT_FALSE(manager_.IsPlaybackStarted());
1050 player_->Start();
1051
1052 // Playback should not start.
1053 EXPECT_FALSE(
1054 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1055 base::Unretained(&manager_)),
1056 start_timeout));
1057 }
1058
1059 // http://crbug.com/518900
1060 TEST_F(MediaCodecPlayerTest, DISABLED_AudioSeekAfterStop) {
1061 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1062
1063 // Play for 300 ms, then Pause, then Seek to beginning. The playback should
1064 // start from the beginning.
1065
1066 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1067
1068 demuxer_->SetAudioFactory(base::WrapUnique(new AudioFactory(duration)));
1069
1070 CreatePlayer();
1071
1072 // Post configuration.
1073 demuxer_->PostInternalConfigs();
1074
1075 // Start the player.
1076 player_->Start();
1077
1078 // Wait for playback to start.
1079 EXPECT_TRUE(
1080 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1081 base::Unretained(&manager_))));
1082
1083 // Wait for 300 ms and stop. The 300 ms interval takes into account potential
1084 // audio delay: audio takes time reconfiguring after the first several packets
1085 // get written to the audio track.
1086 WaitForDelay(base::TimeDelta::FromMilliseconds(300));
1087
1088 player_->Pause(true);
1089
1090 // Make sure we played at least 100 ms.
1091 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1092
1093 // Wait till the Pause is completed.
1094 EXPECT_TRUE(WaitForCondition(
1095 base::Bind(&MediaCodecPlayerTest::IsPaused, base::Unretained(this))));
1096
1097 // Clear statistics.
1098 manager_.pts_stat_.Clear();
1099
1100 // Now we can seek to the beginning and start the playback.
1101 player_->SeekTo(base::TimeDelta());
1102
1103 player_->Start();
1104
1105 // Wait for playback to start.
1106 EXPECT_TRUE(
1107 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1108 base::Unretained(&manager_))));
1109
1110 // Make sure we started from the beginninig
1111 EXPECT_GT(base::TimeDelta::FromMilliseconds(40), manager_.pts_stat_.min());
1112
1113 // The player should have reported the seek completion to the manager.
1114 EXPECT_TRUE(WaitForCondition(base::Bind(
1115 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1116 }
1117
1118 TEST_F(MediaCodecPlayerTest, DISABLED_AudioSeekThenPlay) {
1119 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1120
1121 // Issue Seek command immediately followed by Start. The playback should
1122 // start at the seek position.
1123
1124 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1125 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(500);
1126
1127 demuxer_->SetAudioFactory(base::MakeUnique<AudioFactory>(duration));
1128
1129 CreatePlayer();
1130
1131 // Post configuration.
1132 demuxer_->PostInternalConfigs();
1133
1134 // Seek and immediately start.
1135 player_->SeekTo(seek_position);
1136 player_->Start();
1137
1138 // Wait for playback to start.
1139 EXPECT_TRUE(
1140 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1141 base::Unretained(&manager_))));
1142
1143 // The playback should start at |seek_position|
1144 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
1145
1146 // The player should have reported the seek completion to the manager.
1147 EXPECT_TRUE(WaitForCondition(base::Bind(
1148 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1149 }
1150
1151 TEST_F(MediaCodecPlayerTest, DISABLED_AudioSeekThenPlayThenConfig) {
1152 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1153
1154 // Issue Seek command immediately followed by Start but without prior demuxer
1155 // configuration. Start should wait for configuration. After it has been
1156 // posted the playback should start at the seek position.
1157
1158 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1159 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(500);
1160
1161 demuxer_->SetAudioFactory(base::MakeUnique<AudioFactory>(duration));
1162
1163 CreatePlayer();
1164
1165 // Seek and immediately start.
1166 player_->SeekTo(seek_position);
1167 player_->Start();
1168
1169 // Make sure the player is waiting.
1170 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1171 EXPECT_FALSE(player_->IsPlaying());
1172
1173 // Post configuration.
1174 demuxer_->PostInternalConfigs();
1175
1176 // Wait for playback to start.
1177 EXPECT_TRUE(
1178 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1179 base::Unretained(&manager_))));
1180
1181 // The playback should start at |seek_position|
1182 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
1183
1184 // The player should have reported the seek completion to the manager.
1185 EXPECT_TRUE(WaitForCondition(base::Bind(
1186 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1187 }
1188
1189 // http://crbug.com/518900
1190 TEST_F(MediaCodecPlayerTest, DISABLED_AudioSeekWhilePlaying) {
1191 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1192
1193 // Play for 300 ms, then issue several Seek commands in the row.
1194 // The playback should continue at the last seek position.
1195
1196 // To test this condition without analyzing the reported time details
1197 // and without introducing dependency on implementation I make a long (10s)
1198 // duration and test that the playback resumes after big time jump (5s) in a
1199 // short period of time (200 ms).
1200 base::TimeDelta duration = base::TimeDelta::FromSeconds(10);
1201
1202 demuxer_->SetAudioFactory(base::WrapUnique(new AudioFactory(duration)));
1203
1204 CreatePlayer();
1205
1206 // Post configuration.
1207 demuxer_->PostInternalConfigs();
1208
1209 // Start the player.
1210 player_->Start();
1211
1212 // Wait for playback to start.
1213 EXPECT_TRUE(
1214 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1215 base::Unretained(&manager_))));
1216
1217 // Wait for 300 ms.
1218 WaitForDelay(base::TimeDelta::FromMilliseconds(300));
1219
1220 // Make sure we played at least 100 ms.
1221 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1222
1223 // Seek forward several times.
1224 player_->SeekTo(base::TimeDelta::FromSeconds(3));
1225 player_->SeekTo(base::TimeDelta::FromSeconds(4));
1226 player_->SeekTo(base::TimeDelta::FromSeconds(5));
1227
1228 // Make sure that we reached the last timestamp within default timeout,
1229 // i.e. 200 ms.
1230 EXPECT_TRUE(WaitForPlaybackBeyondPosition(base::TimeDelta::FromSeconds(5)));
1231 EXPECT_TRUE(player_->IsPlaying());
1232
1233 // The player should have reported the seek completion to the manager.
1234 EXPECT_TRUE(WaitForCondition(base::Bind(
1235 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1236 }
1237
1238 // Disabled per http://crbug.com/611489.
1239 TEST_F(MediaCodecPlayerTest, DISABLED_VideoReplaceSurface) {
1240 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1241
1242 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000);
1243 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(1500);
1244
1245 ASSERT_TRUE(StartVideoPlayback(duration, "VideoReplaceSurface"));
1246
1247 // Wait for some time and check statistics.
1248 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1249
1250 // Make sure we played at least 100 ms.
1251 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1252
1253 // Set new video surface without removing the old one.
1254 SetVideoSurfaceB();
1255
1256 // We should receive a browser seek request.
1257 EXPECT_TRUE(WaitForCondition(
1258 base::Bind(&MockDemuxerAndroid::ReceivedBrowserSeekRequest,
1259 base::Unretained(demuxer_))));
1260
1261 // Playback should continue with a new surface. Wait till completion.
1262 EXPECT_TRUE(
1263 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1264 base::Unretained(&manager_)),
1265 timeout));
1266 EXPECT_LE(duration, manager_.pts_stat_.max());
1267 }
1268
1269 TEST_F(MediaCodecPlayerTest, DISABLED_VideoRemoveAndSetSurface) {
1270 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1271
1272 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000);
1273
1274 ASSERT_TRUE(StartVideoPlayback(duration, "VideoRemoveAndSetSurface"));
1275
1276 // Wait for some time and check statistics.
1277 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1278
1279 // Make sure we played at least 100 ms.
1280 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1281
1282 // Remove video surface.
1283 RemoveVideoSurface();
1284
1285 // We should be stuck waiting for the new surface.
1286 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1287 EXPECT_FALSE(player_->IsPlaying());
1288
1289 // Save last PTS and clear statistics.
1290 base::TimeDelta max_pts_before_removal = manager_.pts_stat_.max();
1291 manager_.pts_stat_.Clear();
1292
1293 // After clearing statistics we are ready to wait for IsPlaybackStarted again.
1294 EXPECT_FALSE(manager_.IsPlaybackStarted());
1295
1296 // Extra RemoveVideoSurface() should not change anything.
1297 RemoveVideoSurface();
1298
1299 // Set another video surface.
1300 SetVideoSurfaceB();
1301
1302 // We should receive a browser seek request.
1303 EXPECT_TRUE(WaitForCondition(
1304 base::Bind(&MockDemuxerAndroid::ReceivedBrowserSeekRequest,
1305 base::Unretained(demuxer_))));
1306
1307 // Playback should continue with a new surface. Wait till it starts again.
1308 base::TimeDelta reconfigure_timeout = base::TimeDelta::FromMilliseconds(800);
1309 EXPECT_TRUE(
1310 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1311 base::Unretained(&manager_)),
1312 reconfigure_timeout));
1313
1314 // Timestamps should not go back.
1315 EXPECT_LE(max_pts_before_removal, manager_.pts_stat_.max());
1316 }
1317
1318 // http://crbug.com/518900
1319 TEST_F(MediaCodecPlayerTest, DISABLED_VideoReleaseAndStart) {
1320 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1321
1322 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000);
1323
1324 ASSERT_TRUE(StartVideoPlayback(duration, "VideoReleaseAndStart"));
1325
1326 // Wait for some time and check statistics.
1327 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1328
1329 // Make sure we played at least 100 ms.
1330 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1331
1332 // When the user presses Tasks button Chrome calls Pause() and Release().
1333 player_->Pause(true);
1334 player_->Release();
1335
1336 // Make sure we are not playing any more.
1337 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1338 EXPECT_FALSE(player_->IsPlaying());
1339
1340 // Save last PTS and clear statistics.
1341 base::TimeDelta max_pts_before_backgrounding = manager_.pts_stat_.max();
1342 manager_.pts_stat_.Clear();
1343
1344 // After clearing statistics we are ready to wait for IsPlaybackStarted again.
1345 EXPECT_FALSE(manager_.IsPlaybackStarted());
1346
1347 // Restart.
1348 SetVideoSurface();
1349 player_->Start();
1350
1351 // We should receive a browser seek request.
1352 EXPECT_TRUE(WaitForCondition(
1353 base::Bind(&MockDemuxerAndroid::ReceivedBrowserSeekRequest,
1354 base::Unretained(demuxer_))));
1355
1356 // Wait for playback to start again.
1357 base::TimeDelta reconfigure_timeout = base::TimeDelta::FromMilliseconds(800);
1358 EXPECT_TRUE(
1359 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1360 base::Unretained(&manager_)),
1361 reconfigure_timeout));
1362
1363 // Timestamps should not go back.
1364 EXPECT_LE(max_pts_before_backgrounding, manager_.pts_stat_.max());
1365 }
1366
1367 TEST_F(MediaCodecPlayerTest, DISABLED_VideoSeekAndRelease) {
1368 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1369
1370 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1371 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1372
1373 ASSERT_TRUE(StartVideoPlayback(duration, "VideoSeekAndRelease"));
1374
1375 // Wait for some time and check statistics.
1376 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1377
1378 // Make sure we played at least 100 ms.
1379 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1380
1381 // Issue SeekTo() immediately followed by Release().
1382 player_->SeekTo(seek_position);
1383 player_->Release();
1384
1385 // Make sure we are not playing any more.
1386 WaitForDelay(base::TimeDelta::FromMilliseconds(400));
1387 EXPECT_FALSE(player_->IsPlaying());
1388
1389 // The Release() should not cancel the SeekTo() and we should have received
1390 // the seek request by this time.
1391 EXPECT_TRUE(demuxer_->ReceivedSeekRequest());
1392
1393 // The player should have reported the seek completion to the manager.
1394 EXPECT_TRUE(WaitForCondition(base::Bind(
1395 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1396
1397 // Clear statistics.
1398 manager_.pts_stat_.Clear();
1399
1400 // After clearing statistics we are ready to wait for IsPlaybackStarted again.
1401 EXPECT_FALSE(manager_.IsPlaybackStarted());
1402
1403 // Restart.
1404 SetVideoSurface();
1405 player_->Start();
1406
1407 // Wait for playback to start again.
1408 base::TimeDelta reconfigure_timeout = base::TimeDelta::FromMilliseconds(800);
1409 EXPECT_TRUE(
1410 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1411 base::Unretained(&manager_)),
1412 reconfigure_timeout));
1413
1414 // Timestamps should start at the new seek position
1415 EXPECT_LE(seek_position, manager_.pts_stat_.min());
1416 }
1417
1418 // Flaky test: https://crbug.com/609444
1419 TEST_F(MediaCodecPlayerTest, DISABLED_VideoReleaseWhileWaitingForSeek) {
1420 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1421
1422 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1423 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1424
1425 ASSERT_TRUE(StartVideoPlayback(duration, "VideoReleaseWhileWaitingForSeek"));
1426
1427 // Wait for some time and check statistics.
1428 WaitForDelay(base::TimeDelta::FromMilliseconds(200));
1429
1430 // Make sure we played at least 100 ms.
1431 EXPECT_LT(base::TimeDelta::FromMilliseconds(100), manager_.pts_stat_.max());
1432
1433 // Set artificial delay in the OnDemuxerSeekDone response so we can
1434 // issue commands while the player is in the STATE_WAITING_FOR_SEEK.
1435 demuxer_->SetSeekDoneDelay(base::TimeDelta::FromMilliseconds(100));
1436
1437 // Issue SeekTo().
1438 player_->SeekTo(seek_position);
1439
1440 // Wait for the seek request to demuxer.
1441 EXPECT_TRUE(WaitForCondition(base::Bind(
1442 &MockDemuxerAndroid::ReceivedSeekRequest, base::Unretained(demuxer_))));
1443
1444 // The player is supposed to be in STATE_WAITING_FOR_SEEK. Issue Release().
1445 player_->Release();
1446
1447 // Make sure we are not playing any more.
1448 WaitForDelay(base::TimeDelta::FromMilliseconds(400));
1449 EXPECT_FALSE(player_->IsPlaying());
1450
1451 // Clear statistics.
1452 manager_.pts_stat_.Clear();
1453
1454 // After clearing statistics we are ready to wait for IsPlaybackStarted again.
1455 EXPECT_FALSE(manager_.IsPlaybackStarted());
1456
1457 // Restart.
1458 SetVideoSurface();
1459 player_->Start();
1460
1461 // Wait for playback to start again.
1462 base::TimeDelta reconfigure_timeout = base::TimeDelta::FromMilliseconds(1000);
1463 EXPECT_TRUE(
1464 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1465 base::Unretained(&manager_)),
1466 reconfigure_timeout));
1467
1468 // Timestamps should start at the new seek position
1469 EXPECT_LE(seek_position, manager_.pts_stat_.min());
1470
1471 // The player should have reported the seek completion to the manager.
1472 EXPECT_TRUE(WaitForCondition(base::Bind(
1473 &MockMediaPlayerManager::IsSeekCompleted, base::Unretained(&manager_))));
1474 }
1475
1476 // Disabled per http://crbug.com/611489.
1477 TEST_F(MediaCodecPlayerTest, DISABLED_VideoPrerollAfterSeek) {
1478 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1479
1480 // A simple test for preroll for video stream only. After the seek is done
1481 // the data factory generates the frames with pts before the seek time, and
1482 // they should not be rendered. We deduce which frame is rendered by looking
1483 // at the reported time progress.
1484
1485 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(600);
1486 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(500);
1487 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
1488
1489 // Tell demuxer to make the first frame 100ms earlier than the seek request.
1490 demuxer_->SetVideoPrerollInterval(base::TimeDelta::FromMilliseconds(100));
1491
1492 demuxer_->SetVideoFactory(base::WrapUnique(new VideoFactory(duration)));
1493
1494 CreatePlayer();
1495 SetVideoSurface();
1496
1497 // Wait till the player is initialized on media thread.
1498 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1499 base::Unretained(demuxer_))));
1500 if (!demuxer_->IsInitialized()) {
1501 DVLOG(0) << "VideoPrerollAfterSeek: demuxer is not initialized";
1502 return;
1503 }
1504
1505 // Post configuration after the player has been initialized.
1506 demuxer_->PostInternalConfigs();
1507
1508 // Issue SeekTo().
1509 player_->SeekTo(seek_position);
1510
1511 // Start the playback and make sure it is started.
1512 player_->Start();
1513
1514 EXPECT_TRUE(
1515 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1516 base::Unretained(&manager_)),
1517 start_timeout));
1518
1519 // Wait for completion.
1520 EXPECT_TRUE(
1521 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1522 base::Unretained(&manager_))));
1523
1524 // The first pts should be equal than seek position even if video frames
1525 // started 100 ms eralier than the seek request.
1526 EXPECT_EQ(seek_position, manager_.pts_stat_.min());
1527
1528 EXPECT_EQ(6, manager_.pts_stat_.num_values());
1529 }
1530
1531 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollAudioWaitsForVideo) {
1532 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1533
1534 // Test that during prerolling neither audio nor video plays and that both
1535 // resume simultaneously after preroll is finished. In other words, test
1536 // that preroll works.
1537 // We put the video into the long preroll and intercept the time when first
1538 // rendering happens in each stream. The moment of rendering is approximated
1539 // with a decoder PTS that is delivered by a test-only callback.
1540
1541 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1542
1543 // Set significant preroll interval. 500 ms means 25 frames, at 10 ms
1544 // per frame it would take 250 ms to preroll.
1545 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1546 base::TimeDelta preroll_intvl = base::TimeDelta::FromMilliseconds(500);
1547 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(1000);
1548
1549 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
1550 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
1551
1552 demuxer_->SetVideoPrerollInterval(preroll_intvl);
1553
1554 ASSERT_TRUE(StartAVSeekAndPreroll(std::move(audio_factory),
1555 std::move(video_factory), seek_position, 0,
1556 "AVPrerollAudioWaitsForVideo"));
1557
1558 // Wait till preroll finishes and the real playback starts.
1559 EXPECT_TRUE(
1560 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1561 base::Unretained(&manager_)),
1562 preroll_timeout));
1563
1564 // Ensure that the first audio and video pts are close to each other and are
1565 // reported at the close moments in time.
1566
1567 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1568 EXPECT_TRUE(WaitForCondition(
1569 base::Bind(&MockMediaPlayerManager::HasFirstFrame,
1570 base::Unretained(&manager_), DemuxerStream::VIDEO)));
1571
1572 EXPECT_TRUE(AlmostEqual(manager_.FirstFramePTS(DemuxerStream::AUDIO),
1573 manager_.FirstFramePTS(DemuxerStream::VIDEO), 25));
1574
1575 EXPECT_TRUE(AlmostEqual(manager_.FirstFrameTime(DemuxerStream::AUDIO),
1576 manager_.FirstFrameTime(DemuxerStream::VIDEO), 50));
1577
1578 // The playback should start at |seek_position|
1579 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
1580 }
1581
1582 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollReleaseAndRestart) {
1583 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1584
1585 // Test that player will resume prerolling if prerolling is interrupted by
1586 // Release() and Start().
1587
1588 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(2000);
1589
1590 // Set significant preroll interval. 500 ms means 25 frames, at 10 ms
1591 // per frame it would take 250 ms to preroll.
1592 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1593 base::TimeDelta preroll_intvl = base::TimeDelta::FromMilliseconds(500);
1594
1595 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
1596 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(1000);
1597
1598 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
1599 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
1600
1601 demuxer_->SetVideoPrerollInterval(preroll_intvl);
1602
1603 ASSERT_TRUE(StartAVSeekAndPreroll(std::move(audio_factory),
1604 std::move(video_factory), seek_position, 0,
1605 "AVPrerollReleaseAndRestart"));
1606
1607 // Issue Release().
1608 player_->Release();
1609
1610 // Make sure we have not been playing.
1611 WaitForDelay(base::TimeDelta::FromMilliseconds(400));
1612
1613 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1614 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO));
1615
1616 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::AUDIO));
1617 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::VIDEO));
1618 EXPECT_EQ(0, manager_.pts_stat_.num_values());
1619
1620 // Restart. Release() removed the video surface, we need to set it again.
1621 SetVideoSurface();
1622 player_->Start();
1623
1624 // The playback should pass through prerolling phase.
1625 EXPECT_TRUE(WaitForCondition(
1626 base::Bind(&MediaCodecPlayer::IsPrerollingForTests,
1627 base::Unretained(player_), DemuxerStream::VIDEO),
1628 start_timeout));
1629
1630 // Wait till preroll finishes and the real playback starts.
1631 EXPECT_TRUE(
1632 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1633 base::Unretained(&manager_)),
1634 preroll_timeout));
1635
1636 // Ensure that the first audio and video pts are close to each other and are
1637 // reported at the close moments in time.
1638
1639 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1640 EXPECT_TRUE(WaitForCondition(
1641 base::Bind(&MockMediaPlayerManager::HasFirstFrame,
1642 base::Unretained(&manager_), DemuxerStream::VIDEO)));
1643
1644 // Release() might disacrd first audio frame.
1645 EXPECT_TRUE(AlmostEqual(manager_.FirstFramePTS(DemuxerStream::AUDIO),
1646 manager_.FirstFramePTS(DemuxerStream::VIDEO), 50));
1647
1648 EXPECT_TRUE(AlmostEqual(manager_.FirstFrameTime(DemuxerStream::AUDIO),
1649 manager_.FirstFrameTime(DemuxerStream::VIDEO), 50));
1650
1651 // The playback should start at |seek_position|, but Release() might discard
1652 // the first audio frame.
1653 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 50));
1654 }
1655
1656 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollStopAndRestart) {
1657 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1658
1659 // Test that if Pause() happens during the preroll phase,
1660 // we continue to do preroll after restart.
1661
1662 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
1663
1664 // Set significant preroll interval. 500 ms means 25 frames, at 10 ms
1665 // per frame it would take 250 ms to preroll.
1666 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1667 base::TimeDelta preroll_intvl = base::TimeDelta::FromMilliseconds(500);
1668
1669 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
1670 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(1000);
1671
1672 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
1673 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
1674
1675 demuxer_->SetVideoPrerollInterval(preroll_intvl);
1676
1677 ASSERT_TRUE(StartAVSeekAndPreroll(std::move(audio_factory),
1678 std::move(video_factory), seek_position, 0,
1679 "AVPrerollStopAndRestart"));
1680
1681 // Video stream should be prerolling. Request to stop.
1682 EXPECT_FALSE(IsPaused());
1683 player_->Pause(true);
1684
1685 EXPECT_TRUE(WaitForCondition(
1686 base::Bind(&MediaCodecPlayerTest::IsPaused, base::Unretained(this))));
1687
1688 // Test that we have not been playing.
1689 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1690 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO));
1691
1692 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::AUDIO));
1693 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::VIDEO));
1694 EXPECT_EQ(0, manager_.pts_stat_.num_values());
1695
1696 // Restart.
1697 player_->Start();
1698
1699 // There should be preroll after the start.
1700 EXPECT_TRUE(WaitForCondition(
1701 base::Bind(&MediaCodecPlayer::IsPrerollingForTests,
1702 base::Unretained(player_), DemuxerStream::VIDEO),
1703 start_timeout));
1704
1705 // Wait for a short period of time, so that preroll is still ongoing,
1706 // and pause again.
1707 WaitForDelay(base::TimeDelta::FromMilliseconds(100));
1708
1709 EXPECT_FALSE(IsPaused());
1710 player_->Pause(true);
1711
1712 EXPECT_TRUE(WaitForCondition(
1713 base::Bind(&MediaCodecPlayerTest::IsPaused, base::Unretained(this))));
1714
1715 // Check that we still haven't started rendering.
1716 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1717 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO));
1718
1719 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::AUDIO));
1720 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::VIDEO));
1721 EXPECT_EQ(0, manager_.pts_stat_.num_values());
1722
1723 // Restart again.
1724 player_->Start();
1725
1726 // Wait till we start to play.
1727 EXPECT_TRUE(
1728 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1729 base::Unretained(&manager_)),
1730 preroll_timeout));
1731
1732 // Check that we did prerolling, i.e. audio did wait for video.
1733 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1734 EXPECT_TRUE(WaitForCondition(
1735 base::Bind(&MockMediaPlayerManager::HasFirstFrame,
1736 base::Unretained(&manager_), DemuxerStream::VIDEO)));
1737
1738 EXPECT_TRUE(AlmostEqual(manager_.FirstFramePTS(DemuxerStream::AUDIO),
1739 manager_.FirstFramePTS(DemuxerStream::VIDEO), 25));
1740
1741 EXPECT_TRUE(AlmostEqual(manager_.FirstFrameTime(DemuxerStream::AUDIO),
1742 manager_.FirstFrameTime(DemuxerStream::VIDEO), 50));
1743
1744 // The playback should start at |seek_position|
1745 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
1746 }
1747
1748 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollVideoEndsWhilePrerolling) {
1749 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1750
1751 // Test that when one stream ends in the preroll phase and another is not
1752 // the preroll finishes and playback continues after it.
1753
1754 // http://crbug.com/526755
1755 // TODO(timav): remove these logs after verifying that the bug is fixed.
1756 DVLOG(0) << "AVPrerollVideoEndsWhilePrerolling: begin";
1757
1758 base::TimeDelta audio_duration = base::TimeDelta::FromMilliseconds(1100);
1759 base::TimeDelta video_duration = base::TimeDelta::FromMilliseconds(900);
1760 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
1761 base::TimeDelta video_preroll_intvl = base::TimeDelta::FromMilliseconds(200);
1762
1763 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(800);
1764 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(400);
1765
1766 demuxer_->SetVideoPrerollInterval(video_preroll_intvl);
1767
1768 demuxer_->SetAudioFactory(base::MakeUnique<AudioFactory>(audio_duration));
1769 demuxer_->SetVideoFactory(base::MakeUnique<VideoFactory>(video_duration));
1770
1771 CreatePlayer();
1772 SetVideoSurface();
1773
1774 // Set special testing callback to receive PTS from decoders.
1775 player_->SetDecodersTimeCallbackForTests(
1776 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
1777 base::Unretained(&manager_)));
1778
1779 // Wait till the player is initialized on media thread.
1780 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1781 base::Unretained(demuxer_))));
1782
1783 if (!demuxer_->IsInitialized()) {
1784 DVLOG(0) << "AVPrerollVideoEndsWhilePrerolling: demuxer is not initialized";
1785 return;
1786 }
1787
1788 // Post configuration after the player has been initialized.
1789 demuxer_->PostInternalConfigs();
1790
1791 // Issue SeekTo().
1792 player_->SeekTo(seek_position);
1793
1794 // Start the playback.
1795 player_->Start();
1796
1797 // The video decoder should start prerolling.
1798 // Wait till preroll starts.
1799 EXPECT_TRUE(WaitForCondition(
1800 base::Bind(&MediaCodecPlayer::IsPrerollingForTests,
1801 base::Unretained(player_), DemuxerStream::VIDEO),
1802 start_timeout));
1803
1804 // Wait for playback to start.
1805 bool playback_started =
1806 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1807 base::Unretained(&manager_)),
1808 preroll_timeout);
1809
1810 // http://crbug.com/526755
1811 if (!playback_started) {
1812 DVLOG(0) << "AVPrerollVideoEndsWhilePrerolling: playback did not start for "
1813 << preroll_timeout;
1814 }
1815 ASSERT_TRUE(playback_started);
1816
1817 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1818
1819 // Play till completion.
1820 EXPECT_TRUE(
1821 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1822 base::Unretained(&manager_))));
1823
1824 // There should not be any video frames.
1825 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO));
1826
1827 // http://crbug.com/526755
1828 DVLOG(0) << "AVPrerollVideoEndsWhilePrerolling: end";
1829 }
1830
1831 TEST_F(MediaCodecPlayerTest, DISABLED_VideoConfigChangeWhilePlaying) {
1832 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1833
1834 // Test that video only playback continues after video config change.
1835
1836 // Initialize video playback
1837 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
1838 base::TimeDelta config_change_position =
1839 base::TimeDelta::FromMilliseconds(1000);
1840
1841 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(2000);
1842 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
1843
1844 demuxer_->SetVideoFactory(base::MakeUnique<VideoFactory>(duration));
1845
1846 demuxer_->video_factory()->RequestConfigChange(config_change_position);
1847
1848 CreatePlayer();
1849 SetVideoSurface();
1850
1851 // Wait till the player is initialized on media thread.
1852 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1853 base::Unretained(demuxer_))));
1854
1855 if (!demuxer_->IsInitialized()) {
1856 DVLOG(0) << "VideoConfigChangeWhilePlaying: demuxer is not initialized";
1857 return;
1858 }
1859
1860 // Ask decoders to always reconfigure after the player has been initialized.
1861 player_->SetAlwaysReconfigureForTests(DemuxerStream::VIDEO);
1862
1863 // Set a testing callback to receive PTS from decoders.
1864 player_->SetDecodersTimeCallbackForTests(
1865 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
1866 base::Unretained(&manager_)));
1867
1868 // Set a testing callback to receive MediaCodec creation events from decoders.
1869 player_->SetCodecCreatedCallbackForTests(
1870 base::Bind(&MockMediaPlayerManager::OnMediaCodecCreated,
1871 base::Unretained(&manager_)));
1872
1873 // Post configuration after the player has been initialized.
1874 demuxer_->PostInternalConfigs();
1875
1876 // Start and wait for playback.
1877 player_->Start();
1878
1879 // Wait till we start to play.
1880 EXPECT_TRUE(
1881 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1882 base::Unretained(&manager_)),
1883 start_timeout));
1884
1885 // Wait till completion
1886 EXPECT_TRUE(
1887 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1888 base::Unretained(&manager_)),
1889 completion_timeout));
1890
1891 // The video codec should be recreated upon config changes.
1892 EXPECT_EQ(2, manager_.num_video_codecs_created());
1893
1894 // Check that we did not miss video frames
1895 int expected_video_frames = GetFrameCount(duration, kVideoFramePeriod, 1);
1896 EXPECT_EQ(expected_video_frames,
1897 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
1898 }
1899
1900 // https://crbug.com/587195
1901 TEST_F(MediaCodecPlayerTest, DISABLED_AVVideoConfigChangeWhilePlaying) {
1902 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1903
1904 // Test that A/V playback continues after video config change.
1905
1906 // Initialize A/V playback
1907 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
1908 base::TimeDelta config_change_position =
1909 base::TimeDelta::FromMilliseconds(1000);
1910
1911 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
1912
1913 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
1914 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
1915
1916 video_factory->RequestConfigChange(config_change_position);
1917
1918 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
1919 std::move(video_factory), kAlwaysReconfigVideo,
1920 "AVVideoConfigChangeWhilePlaying"));
1921
1922 // Wait till completion
1923 EXPECT_TRUE(
1924 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1925 base::Unretained(&manager_)),
1926 completion_timeout));
1927
1928 // The audio codec should be kept.
1929 EXPECT_EQ(1, manager_.num_audio_codecs_created());
1930
1931 // The video codec should be recreated upon config changes.
1932 EXPECT_EQ(2, manager_.num_video_codecs_created());
1933
1934 // Check that we did not miss video frames
1935 int expected_video_frames = GetFrameCount(duration, kVideoFramePeriod, 1);
1936 EXPECT_EQ(expected_video_frames,
1937 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
1938
1939 // Check that we did not miss audio frames. We expect one postponed frames
1940 // that are not reported.
1941 // For Nexus 4 KitKat the AAC decoder seems to swallow the first frame
1942 // but reports the last pts twice, maybe it just shifts the reported PTS.
1943 int expected_audio_frames = GetFrameCount(duration, kAudioFramePeriod, 0) - 1;
1944 EXPECT_EQ(expected_audio_frames,
1945 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
1946 }
1947
1948 TEST_F(MediaCodecPlayerTest, DISABLED_AVAudioConfigChangeWhilePlaying) {
1949 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1950
1951 // Test that A/V playback continues after audio config change.
1952
1953 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
1954 base::TimeDelta config_change_position =
1955 base::TimeDelta::FromMilliseconds(1000);
1956
1957 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
1958
1959 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
1960 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
1961
1962 audio_factory->RequestConfigChange(config_change_position);
1963
1964 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
1965 std::move(video_factory), kAlwaysReconfigAudio,
1966 "AVAudioConfigChangeWhilePlaying"));
1967
1968 // Wait till completion
1969 EXPECT_TRUE(
1970 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1971 base::Unretained(&manager_)),
1972 completion_timeout));
1973
1974 // The audio codec should be recreated upon config changes.
1975 EXPECT_EQ(2, manager_.num_audio_codecs_created());
1976
1977 // The video codec should be kept.
1978 EXPECT_EQ(1, manager_.num_video_codecs_created());
1979
1980 // Check that we did not miss video frames.
1981 int expected_video_frames = GetFrameCount(duration, kVideoFramePeriod, 0);
1982 EXPECT_EQ(expected_video_frames,
1983 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
1984
1985 // Check that we did not miss audio frames. We expect two postponed frames
1986 // that are not reported.
1987 int expected_audio_frames = GetFrameCount(duration, kAudioFramePeriod, 1) - 2;
1988 EXPECT_EQ(expected_audio_frames,
1989 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
1990 }
1991
1992 TEST_F(MediaCodecPlayerTest, DISABLED_AVSimultaneousConfigChange_1) {
1993 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1994
1995 // Test that the playback continues if audio and video config changes happen
1996 // at the same time.
1997
1998 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
1999 base::TimeDelta config_change_audio = base::TimeDelta::FromMilliseconds(1000);
2000 base::TimeDelta config_change_video = base::TimeDelta::FromMilliseconds(1000);
2001
2002 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
2003
2004 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
2005 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
2006
2007 audio_factory->RequestConfigChange(config_change_audio);
2008 video_factory->RequestConfigChange(config_change_video);
2009
2010 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
2011 std::move(video_factory),
2012 kAlwaysReconfigAudio | kAlwaysReconfigVideo,
2013 "AVSimultaneousConfigChange_1"));
2014
2015 // Wait till completion
2016 EXPECT_TRUE(
2017 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
2018 base::Unretained(&manager_)),
2019 completion_timeout));
2020
2021 // The audio codec should be recreated upon config changes.
2022 EXPECT_EQ(2, manager_.num_audio_codecs_created());
2023
2024 // The video codec should be recreated upon config changes.
2025 EXPECT_EQ(2, manager_.num_video_codecs_created());
2026
2027 // Check that we did not miss video frames.
2028 int expected_video_frames = GetFrameCount(duration, kVideoFramePeriod, 1);
2029 EXPECT_EQ(expected_video_frames,
2030 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
2031
2032 // Check that we did not miss audio frames. We expect two postponed frames
2033 // that are not reported.
2034 int expected_audio_frames = GetFrameCount(duration, kAudioFramePeriod, 1) - 2;
2035 EXPECT_EQ(expected_audio_frames,
2036 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
2037 }
2038
2039 TEST_F(MediaCodecPlayerTest, DISABLED_AVSimultaneousConfigChange_2) {
2040 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2041
2042 // Test that the playback continues if audio and video config changes happen
2043 // at the same time. Move audio change moment slightly to make it drained
2044 // after video.
2045
2046 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
2047 base::TimeDelta config_change_audio = base::TimeDelta::FromMilliseconds(1020);
2048 base::TimeDelta config_change_video = base::TimeDelta::FromMilliseconds(1000);
2049
2050 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
2051
2052 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
2053 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
2054
2055 audio_factory->RequestConfigChange(config_change_audio);
2056 video_factory->RequestConfigChange(config_change_video);
2057
2058 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
2059 std::move(video_factory),
2060 kAlwaysReconfigAudio | kAlwaysReconfigVideo,
2061 "AVSimultaneousConfigChange_2"));
2062
2063 // Wait till completion
2064 EXPECT_TRUE(
2065 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
2066 base::Unretained(&manager_)),
2067 completion_timeout));
2068
2069 // The audio codec should be recreated upon config changes.
2070 EXPECT_EQ(2, manager_.num_audio_codecs_created());
2071
2072 // The video codec should be recreated upon config changes.
2073 EXPECT_EQ(2, manager_.num_video_codecs_created());
2074
2075 // Check that we did not miss video frames.
2076 int expected_video_frames = GetFrameCount(duration, kVideoFramePeriod, 1);
2077 EXPECT_EQ(expected_video_frames,
2078 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
2079
2080 // Check that we did not miss audio frames. We expect two postponed frames
2081 // that are not reported.
2082 int expected_audio_frames = GetFrameCount(duration, kAudioFramePeriod, 1) - 2;
2083 EXPECT_EQ(expected_audio_frames,
2084 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
2085 }
2086
2087 TEST_F(MediaCodecPlayerTest, DISABLED_AVAudioEndsAcrossVideoConfigChange) {
2088 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2089
2090 // Test that audio can end while video config change processing.
2091
2092 base::TimeDelta audio_duration = base::TimeDelta::FromMilliseconds(1000);
2093 base::TimeDelta video_duration = base::TimeDelta::FromMilliseconds(1200);
2094 base::TimeDelta config_change_video = base::TimeDelta::FromMilliseconds(1000);
2095
2096 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
2097
2098 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(audio_duration));
2099 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(video_duration));
2100
2101 video_factory->RequestConfigChange(config_change_video);
2102
2103 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
2104 std::move(video_factory), kAlwaysReconfigVideo,
2105 "AVAudioEndsAcrossVideoConfigChange"));
2106
2107 // Wait till completion
2108 EXPECT_TRUE(
2109 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
2110 base::Unretained(&manager_)),
2111 completion_timeout));
2112
2113 // The audio codec should not be recreated.
2114 EXPECT_EQ(1, manager_.num_audio_codecs_created());
2115
2116 // The video codec should be recreated upon config changes.
2117 EXPECT_EQ(2, manager_.num_video_codecs_created());
2118
2119 // Check that we did not miss video frames.
2120 int expected_video_frames =
2121 GetFrameCount(video_duration, kVideoFramePeriod, 1);
2122 EXPECT_EQ(expected_video_frames,
2123 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
2124
2125 // Check the last video frame timestamp. The maximum render pts may differ
2126 // from |video_duration| because of the testing artefact: if the last video
2127 // chunk is incomplete if will have different last pts due to B-frames
2128 // rearrangements.
2129 EXPECT_LE(video_duration,
2130 manager_.render_stat_[DemuxerStream::VIDEO].max().pts);
2131
2132 // Check that the playback time reported by the player goes past
2133 // the audio time and corresponds to video after the audio ended.
2134 EXPECT_EQ(video_duration, manager_.pts_stat_.max());
2135 }
2136
2137 // https://crbug.com/587195
2138 TEST_F(MediaCodecPlayerTest, DISABLED_AVVideoEndsAcrossAudioConfigChange) {
2139 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2140
2141 // Test that video can end while audio config change processing.
2142 base::TimeDelta audio_duration = base::TimeDelta::FromMilliseconds(1200);
2143 base::TimeDelta video_duration = base::TimeDelta::FromMilliseconds(1000);
2144 base::TimeDelta config_change_audio = base::TimeDelta::FromMilliseconds(1000);
2145
2146 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
2147
2148 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(audio_duration));
2149 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(video_duration));
2150
2151 audio_factory->RequestConfigChange(config_change_audio);
2152
2153 ASSERT_TRUE(StartAVPlayback(std::move(audio_factory),
2154 std::move(video_factory), kAlwaysReconfigAudio,
2155 "AVVideoEndsAcrossAudioConfigChange"));
2156
2157 // Wait till completion
2158 EXPECT_TRUE(
2159 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
2160 base::Unretained(&manager_)),
2161 completion_timeout));
2162
2163 // The audio codec should be recreated upon config changes.
2164 EXPECT_EQ(2, manager_.num_audio_codecs_created());
2165
2166 // The video codec should not be recreated.
2167 EXPECT_EQ(1, manager_.num_video_codecs_created());
2168
2169 // Check that we did not miss audio frames. We expect two postponed frames
2170 // that are not reported.
2171 int expected_audio_frames =
2172 GetFrameCount(audio_duration, kAudioFramePeriod, 1) - 2;
2173 EXPECT_EQ(expected_audio_frames,
2174 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
2175 }
2176
2177 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollAcrossVideoConfigChange) {
2178 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2179
2180 // Test that preroll continues if interrupted by video config change.
2181
2182 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
2183 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
2184 base::TimeDelta config_change_position =
2185 base::TimeDelta::FromMilliseconds(800);
2186 base::TimeDelta video_preroll_intvl = base::TimeDelta::FromMilliseconds(500);
2187 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(3000);
2188
2189 demuxer_->SetVideoPrerollInterval(video_preroll_intvl);
2190
2191 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
2192
2193 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
2194 video_factory->RequestConfigChange(config_change_position);
2195
2196 ASSERT_TRUE(StartAVSeekAndPreroll(
2197 std::move(audio_factory), std::move(video_factory), seek_position,
2198 kAlwaysReconfigVideo, "AVPrerollAcrossVideoConfigChange"));
2199
2200 // Wait till preroll finishes and the real playback starts.
2201 EXPECT_TRUE(
2202 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
2203 base::Unretained(&manager_)),
2204 preroll_timeout));
2205
2206 // The presense of config change should not affect preroll behavior:
2207
2208 // Ensure that the first audio and video pts are close to each other and are
2209 // reported at the close moments in time.
2210
2211 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
2212 EXPECT_TRUE(WaitForCondition(
2213 base::Bind(&MockMediaPlayerManager::HasFirstFrame,
2214 base::Unretained(&manager_), DemuxerStream::VIDEO)));
2215
2216 EXPECT_TRUE(AlmostEqual(manager_.FirstFramePTS(DemuxerStream::AUDIO),
2217 manager_.FirstFramePTS(DemuxerStream::VIDEO), 25));
2218
2219 EXPECT_TRUE(AlmostEqual(manager_.FirstFrameTime(DemuxerStream::AUDIO),
2220 manager_.FirstFrameTime(DemuxerStream::VIDEO), 50));
2221
2222 // The playback should start at |seek_position|
2223 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
2224 }
2225
2226 TEST_F(MediaCodecPlayerTest, DISABLED_AVPrerollAcrossAudioConfigChange) {
2227 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
2228
2229 // Test that preroll continues if interrupted by video config change.
2230
2231 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1200);
2232 base::TimeDelta seek_position = base::TimeDelta::FromMilliseconds(1000);
2233 base::TimeDelta config_change_position =
2234 base::TimeDelta::FromMilliseconds(800);
2235 base::TimeDelta audio_preroll_intvl = base::TimeDelta::FromMilliseconds(400);
2236 base::TimeDelta preroll_timeout = base::TimeDelta::FromMilliseconds(3000);
2237
2238 demuxer_->SetAudioPrerollInterval(audio_preroll_intvl);
2239
2240 std::unique_ptr<AudioFactory> audio_factory(new AudioFactory(duration));
2241 audio_factory->RequestConfigChange(config_change_position);
2242
2243 std::unique_ptr<VideoFactory> video_factory(new VideoFactory(duration));
2244
2245 ASSERT_TRUE(StartAVSeekAndPreroll(
2246 std::move(audio_factory), std::move(video_factory), seek_position,
2247 kAlwaysReconfigAudio, "AVPrerollAcrossAudioConfigChange"));
2248
2249 // Wait till preroll finishes and the real playback starts.
2250 EXPECT_TRUE(
2251 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
2252 base::Unretained(&manager_)),
2253 preroll_timeout));
2254
2255 // The presense of config change should not affect preroll behavior:
2256
2257 // Ensure that the first audio and video pts are close to each other and are
2258 // reported at the close moments in time.
2259
2260 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
2261 EXPECT_TRUE(WaitForCondition(
2262 base::Bind(&MockMediaPlayerManager::HasFirstFrame,
2263 base::Unretained(&manager_), DemuxerStream::VIDEO)));
2264
2265 // Wait for some more video
2266 WaitForDelay(base::TimeDelta::FromMilliseconds(100));
2267
2268 EXPECT_TRUE(AlmostEqual(manager_.FirstFramePTS(DemuxerStream::AUDIO),
2269 manager_.FirstFramePTS(DemuxerStream::VIDEO), 25));
2270
2271 // Because for video preroll the first frame after preroll renders during the
2272 // preroll stage (and not after the preroll is done) we cannot guarantee the
2273 // proper video timimg in this test.
2274 // TODO(timav): maybe we should not call the testing callback for
2275 // kRenderAfterPreroll for video (for audio we already do not call).
2276 // EXPECT_TRUE(AlmostEqual(manager_.FirstFrameTime(DemuxerStream::AUDIO),
2277 // manager_.FirstFrameTime(DemuxerStream::VIDEO), 50));
2278
2279 // The playback should start at |seek_position|
2280 EXPECT_TRUE(AlmostEqual(seek_position, manager_.pts_stat_.min(), 25));
2281 }
2282
2283 } // namespace media
OLDNEW
« no previous file with comments | « media/base/android/media_codec_player.cc ('k') | media/base/android/media_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698