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

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

Issue 1287423004: MediaCodecPlayer implementation (stage 5 - reconfiguration) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mtplayer-cleanuptest
Patch Set: Avoid potential frame skipping after decoder drain with a new prerolling mode Created 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/timer/timer.h" 7 #include "base/timer/timer.h"
8 #include "media/base/android/demuxer_android.h" 8 #include "media/base/android/demuxer_android.h"
9 #include "media/base/android/media_codec_bridge.h" 9 #include "media/base/android/media_codec_bridge.h"
10 #include "media/base/android/media_codec_player.h" 10 #include "media/base/android/media_codec_player.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 return (a - b).magnitude().InMilliseconds() <= tolerance_ms; 55 return (a - b).magnitude().InMilliseconds() <= tolerance_ms;
56 } 56 }
57 57
58 // Mock of MediaPlayerManager for testing purpose. 58 // Mock of MediaPlayerManager for testing purpose.
59 59
60 class MockMediaPlayerManager : public MediaPlayerManager { 60 class MockMediaPlayerManager : public MediaPlayerManager {
61 public: 61 public:
62 MockMediaPlayerManager() 62 MockMediaPlayerManager()
63 : playback_completed_(false), 63 : playback_completed_(false),
64 num_seeks_completed_(0), 64 num_seeks_completed_(0),
65 num_audio_codecs_created_(0),
66 num_video_codecs_created_(0),
65 weak_ptr_factory_(this) {} 67 weak_ptr_factory_(this) {}
66 ~MockMediaPlayerManager() override {} 68 ~MockMediaPlayerManager() override {}
67 69
68 MediaResourceGetter* GetMediaResourceGetter() override { return nullptr; } 70 MediaResourceGetter* GetMediaResourceGetter() override { return nullptr; }
69 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return nullptr; } 71 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return nullptr; }
70 72
71 // Regular time update callback, reports current playback time to 73 // Regular time update callback, reports current playback time to
72 // MediaPlayerManager. 74 // MediaPlayerManager.
73 void OnTimeUpdate(int player_id, 75 void OnTimeUpdate(int player_id,
74 base::TimeDelta current_timestamp, 76 base::TimeDelta current_timestamp,
(...skipping 30 matching lines...) Expand all
105 MediaPlayerAndroid* GetPlayer(int player_id) override { return nullptr; } 107 MediaPlayerAndroid* GetPlayer(int player_id) override { return nullptr; }
106 bool RequestPlay(int player_id) override { return true; } 108 bool RequestPlay(int player_id) override { return true; }
107 109
108 void OnMediaResourcesRequested(int player_id) {} 110 void OnMediaResourcesRequested(int player_id) {}
109 111
110 // Time update callback that reports the internal progress of the stream. 112 // Time update callback that reports the internal progress of the stream.
111 // Implementation dependent, used for testing only. 113 // Implementation dependent, used for testing only.
112 void OnDecodersTimeUpdate(DemuxerStream::Type stream_type, 114 void OnDecodersTimeUpdate(DemuxerStream::Type stream_type,
113 base::TimeDelta now_playing, 115 base::TimeDelta now_playing,
114 base::TimeDelta last_buffered) { 116 base::TimeDelta last_buffered) {
115 PTSTime& hit = first_frame_hit_[stream_type]; 117 render_stat_[stream_type].AddValue(
116 if (hit.is_null()) 118 PTSTime(now_playing, base::TimeTicks::Now()));
117 hit = PTSTime(now_playing, base::TimeTicks::Now()); 119 }
120
121 // Notification called on MediaCodec creation.
122 // Implementation dependent, used for testing only.
123 void OnMediaCodecCreated(DemuxerStream::Type stream_type) {
124 if (stream_type == DemuxerStream::AUDIO)
125 ++num_audio_codecs_created_;
126 else if (stream_type == DemuxerStream::VIDEO)
127 ++num_video_codecs_created_;
118 } 128 }
119 129
120 // First frame information 130 // First frame information
121 base::TimeDelta FirstFramePTS(DemuxerStream::Type stream_type) const { 131 base::TimeDelta FirstFramePTS(DemuxerStream::Type stream_type) const {
122 return first_frame_hit_[stream_type].pts; 132 return render_stat_[stream_type].min().pts;
123 } 133 }
124 base::TimeTicks FirstFrameTime(DemuxerStream::Type stream_type) const { 134 base::TimeTicks FirstFrameTime(DemuxerStream::Type stream_type) const {
125 return first_frame_hit_[stream_type].time; 135 return render_stat_[stream_type].min().time;
126 } 136 }
127 137
128 base::WeakPtr<MockMediaPlayerManager> GetWeakPtr() { 138 base::WeakPtr<MockMediaPlayerManager> GetWeakPtr() {
129 return weak_ptr_factory_.GetWeakPtr(); 139 return weak_ptr_factory_.GetWeakPtr();
130 } 140 }
131 141
132 // Conditions to wait for. 142 // Conditions to wait for.
133 bool IsMetadataChanged() const { return media_metadata_.modified; } 143 bool IsMetadataChanged() const { return media_metadata_.modified; }
134 bool IsPlaybackCompleted() const { return playback_completed_; } 144 bool IsPlaybackCompleted() const { return playback_completed_; }
135 bool IsPlaybackStarted() const { return pts_stat_.num_values() > 0; } 145 bool IsPlaybackStarted() const { return pts_stat_.num_values() > 0; }
136 bool IsPlaybackBeyondPosition(const base::TimeDelta& pts) const { 146 bool IsPlaybackBeyondPosition(const base::TimeDelta& pts) const {
137 return pts_stat_.max() > pts; 147 return pts_stat_.max() > pts;
138 } 148 }
139 bool IsSeekCompleted() const { return num_seeks_completed_ > 0; } 149 bool IsSeekCompleted() const { return num_seeks_completed_ > 0; }
140 bool HasFirstFrame(DemuxerStream::Type stream_type) const { 150 bool HasFirstFrame(DemuxerStream::Type stream_type) const {
141 return !first_frame_hit_[stream_type].is_null(); 151 return render_stat_[stream_type].num_values() != 0;
142 } 152 }
143 153
154 int num_audio_codecs_created() const { return num_audio_codecs_created_; }
155 int num_video_codecs_created() const { return num_video_codecs_created_; }
156
144 struct MediaMetadata { 157 struct MediaMetadata {
145 base::TimeDelta duration; 158 base::TimeDelta duration;
146 int width; 159 int width;
147 int height; 160 int height;
148 bool modified; 161 bool modified;
149 MediaMetadata() : width(0), height(0), modified(false) {} 162 MediaMetadata() : width(0), height(0), modified(false) {}
150 }; 163 };
151 MediaMetadata media_metadata_; 164 MediaMetadata media_metadata_;
152 165
166 struct PTSTime {
167 base::TimeDelta pts;
168 base::TimeTicks time;
169
170 PTSTime() : pts(), time() {}
171 PTSTime(base::TimeDelta p, base::TimeTicks t) : pts(p), time(t) {}
172 bool is_null() const { return time.is_null(); }
173 bool operator<(const PTSTime& rhs) const { return time < rhs.time; }
174 };
175 Minimax<PTSTime> render_stat_[DemuxerStream::NUM_TYPES];
176
153 Minimax<base::TimeDelta> pts_stat_; 177 Minimax<base::TimeDelta> pts_stat_;
154 178
155 private: 179 private:
156 bool playback_completed_; 180 bool playback_completed_;
157 int num_seeks_completed_; 181 int num_seeks_completed_;
158 182 int num_audio_codecs_created_;
159 struct PTSTime { 183 int num_video_codecs_created_;
160 base::TimeDelta pts;
161 base::TimeTicks time;
162 PTSTime() : pts(), time() {}
163 PTSTime(base::TimeDelta p, base::TimeTicks t) : pts(p), time(t) {}
164 bool is_null() const { return time.is_null(); }
165 };
166 PTSTime first_frame_hit_[DemuxerStream::NUM_TYPES];
167 184
168 base::WeakPtrFactory<MockMediaPlayerManager> weak_ptr_factory_; 185 base::WeakPtrFactory<MockMediaPlayerManager> weak_ptr_factory_;
169 186
170 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager); 187 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
171 }; 188 };
172 189
173 // Helper method that creates demuxer configuration. 190 // Helper method that creates demuxer configuration.
174 191
175 DemuxerConfigs CreateAudioVideoConfigs(const base::TimeDelta& duration, 192 DemuxerConfigs CreateAudioVideoConfigs(const base::TimeDelta& duration,
176 const gfx::Size& video_size) { 193 const gfx::Size& video_size) {
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 392
376 bool created = false; 393 bool created = false;
377 if (type == DemuxerStream::AUDIO && audio_factory_) 394 if (type == DemuxerStream::AUDIO && audio_factory_)
378 created = audio_factory_->CreateChunk(&chunk, &delay); 395 created = audio_factory_->CreateChunk(&chunk, &delay);
379 else if (type == DemuxerStream::VIDEO && video_factory_) 396 else if (type == DemuxerStream::VIDEO && video_factory_)
380 created = video_factory_->CreateChunk(&chunk, &delay); 397 created = video_factory_->CreateChunk(&chunk, &delay);
381 398
382 if (!created) 399 if (!created)
383 return; 400 return;
384 401
402 // Request key frame after |kConfigChanged|
403 if (type == DemuxerStream::VIDEO && !chunk.demuxer_configs.empty())
404 video_factory_->RequestKeyFrame();
405
385 chunk.type = type; 406 chunk.type = type;
386 407
387 // Post to the Media thread. Use the weak pointer to prevent the data arrival 408 // Post to the Media thread. Use the weak pointer to prevent the data arrival
388 // after the player has been deleted. 409 // after the player has been deleted.
389 GetMediaTaskRunner()->PostDelayedTask( 410 GetMediaTaskRunner()->PostDelayedTask(
390 FROM_HERE, base::Bind(&MockDemuxerAndroid::OnDemuxerDataAvailable, 411 FROM_HERE, base::Bind(&MockDemuxerAndroid::OnDemuxerDataAvailable,
391 weak_factory_.GetWeakPtr(), chunk), 412 weak_factory_.GetWeakPtr(), chunk),
392 delay); 413 delay);
393 } 414 }
394 415
(...skipping 1162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 1578
1558 // Post configuration after the player has been initialized. 1579 // Post configuration after the player has been initialized.
1559 demuxer_->PostInternalConfigs(); 1580 demuxer_->PostInternalConfigs();
1560 1581
1561 // Issue SeekTo(). 1582 // Issue SeekTo().
1562 player_->SeekTo(seek_position); 1583 player_->SeekTo(seek_position);
1563 1584
1564 // Start the playback. 1585 // Start the playback.
1565 player_->Start(); 1586 player_->Start();
1566 1587
1567 // The video decoder should start prerolling 1588 // The video decoder should start prerolling.
1568 // Wait till preroll starts. 1589 // Wait till preroll starts.
1569 EXPECT_TRUE(WaitForCondition( 1590 EXPECT_TRUE(WaitForCondition(
1570 base::Bind(&MediaCodecPlayer::IsPrerollingForTests, 1591 base::Bind(&MediaCodecPlayer::IsPrerollingForTests,
1571 base::Unretained(player_), DemuxerStream::VIDEO), 1592 base::Unretained(player_), DemuxerStream::VIDEO),
1572 start_timeout)); 1593 start_timeout));
1573 1594
1574 // Wait for playback to start. 1595 // Wait for playback to start.
1575 EXPECT_TRUE( 1596 EXPECT_TRUE(
1576 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted, 1597 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1577 base::Unretained(&manager_)), 1598 base::Unretained(&manager_)),
1578 preroll_timeout)); 1599 preroll_timeout));
1579 1600
1580 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO)); 1601 EXPECT_TRUE(manager_.HasFirstFrame(DemuxerStream::AUDIO));
1581 1602
1582 // Play till completion. 1603 // Play till completion.
1583 EXPECT_TRUE( 1604 EXPECT_TRUE(
1584 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted, 1605 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1585 base::Unretained(&manager_)))); 1606 base::Unretained(&manager_))));
1586 1607
1587 // There should not be any video frames. 1608 // There should not be any video frames.
1588 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO)); 1609 EXPECT_FALSE(manager_.HasFirstFrame(DemuxerStream::VIDEO));
1589 } 1610 }
1590 1611
1612 TEST_F(MediaCodecPlayerTest, AVVideoConfigChangeWhilePlaying) {
1613 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1614
1615 // Test that playback continues after video config change.
1616
1617 // Initialize A/V playback
1618 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1600);
1619 base::TimeDelta config_change_position =
1620 base::TimeDelta::FromMilliseconds(1000);
1621
1622 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(2000);
1623 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
1624
1625 demuxer_->SetAudioFactory(
1626 scoped_ptr<AudioFactory>(new AudioFactory(duration)));
1627 demuxer_->SetVideoFactory(
1628 scoped_ptr<VideoFactory>(new VideoFactory(duration)));
1629
1630 demuxer_->video_factory()->RequestConfigChange(config_change_position);
1631
1632 CreatePlayer();
1633 SetVideoSurface();
1634
1635 // Wait till the player is initialized on media thread.
1636 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1637 base::Unretained(demuxer_))));
1638
1639 if (!demuxer_->IsInitialized()) {
1640 DVLOG(0) << "AVConfigChangeWhilePlaying: demuxer is not initialized";
1641 return;
1642 }
1643
1644 // Ask decoders to always reconfigure after the player has been initialized.
1645 player_->SetAlwaysReconfigureForTests(DemuxerStream::VIDEO);
1646
1647 // Set a testing callback to receive PTS from decoders.
1648 player_->SetDecodersTimeCallbackForTests(
1649 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
1650 base::Unretained(&manager_)));
1651
1652 // Set a testing callback to receive MediaCodec creation events from decoders.
1653 player_->SetCodecCreatedCallbackForTests(
1654 base::Bind(&MockMediaPlayerManager::OnMediaCodecCreated,
1655 base::Unretained(&manager_)));
1656
1657 // Post configuration after the player has been initialized.
1658 demuxer_->PostInternalConfigs();
1659
1660 // Start and wait for playback.
1661 player_->Start();
1662
1663 // Wait till we start to play.
1664 EXPECT_TRUE(
1665 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1666 base::Unretained(&manager_)),
1667 start_timeout));
1668
1669 // Wait another 100 ms to make sure we are done with initial preroll.
1670 WaitForDelay(base::TimeDelta::FromMilliseconds(100));
1671
1672 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::AUDIO));
1673 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::VIDEO));
1674
1675 // Wait till completion
1676 EXPECT_TRUE(
1677 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1678 base::Unretained(&manager_)),
1679 completion_timeout));
1680
1681 // The audio codec should be kept.
1682 EXPECT_EQ(1, manager_.num_audio_codecs_created());
1683
1684 // The video codec should be recreated upon config changes.
1685 EXPECT_EQ(2, manager_.num_video_codecs_created());
1686
1687 // Check that we did not miss video frames
1688 int expected_video_frames = duration / kVideoFramePeriod + 1;
1689 EXPECT_EQ(expected_video_frames,
1690 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
1691
1692 // Check that we did not miss audio frames. We expect two postponed frames
1693 // that are not reported.
1694 // For Nexus 4 KitKat the AAC decoder seems to swallow the first frame
1695 // but reports the last pts twice, maybe it just shifts the reported PTS.
1696 int expected_audio_frames = duration / kAudioFramePeriod + 1 - 2;
1697 EXPECT_EQ(expected_audio_frames,
1698 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
1699 }
1700
1701 TEST_F(MediaCodecPlayerTest, AVAudioConfigChangeWhilePlaying) {
1702 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1703
1704 // Test that playback continues after audio config change.
1705
1706 // Initialize A/V playback
1707 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1600);
1708 base::TimeDelta config_change_position =
1709 base::TimeDelta::FromMilliseconds(1000);
1710
1711 base::TimeDelta start_timeout = base::TimeDelta::FromMilliseconds(2000);
1712 base::TimeDelta completion_timeout = base::TimeDelta::FromMilliseconds(3000);
1713
1714 demuxer_->SetAudioFactory(
1715 scoped_ptr<AudioFactory>(new AudioFactory(duration)));
1716 demuxer_->SetVideoFactory(
1717 scoped_ptr<VideoFactory>(new VideoFactory(duration)));
1718
1719 demuxer_->audio_factory()->RequestConfigChange(config_change_position);
1720
1721 CreatePlayer();
1722 SetVideoSurface();
1723
1724 // Wait till the player is initialized on media thread.
1725 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized,
1726 base::Unretained(demuxer_))));
1727
1728 if (!demuxer_->IsInitialized()) {
1729 DVLOG(0) << "AVConfigChangeWhilePlaying: demuxer is not initialized";
1730 return;
1731 }
1732
1733 // Ask decoders to always reconfigure after the player has been initialized.
1734 player_->SetAlwaysReconfigureForTests(DemuxerStream::AUDIO);
1735
1736 // Set a testing callback to receive PTS from decoders.
1737 player_->SetDecodersTimeCallbackForTests(
1738 base::Bind(&MockMediaPlayerManager::OnDecodersTimeUpdate,
1739 base::Unretained(&manager_)));
1740
1741 // Set a testing callback to receive MediaCodec creation events from decoders.
1742 player_->SetCodecCreatedCallbackForTests(
1743 base::Bind(&MockMediaPlayerManager::OnMediaCodecCreated,
1744 base::Unretained(&manager_)));
1745
1746 // Post configuration after the player has been initialized.
1747 demuxer_->PostInternalConfigs();
1748
1749 // Start and wait for playback.
1750 player_->Start();
1751
1752 // Wait till we start to play.
1753 EXPECT_TRUE(
1754 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackStarted,
1755 base::Unretained(&manager_)),
1756 start_timeout));
1757
1758 // Wait another 100 ms to make sure we are done with initial preroll.
1759 WaitForDelay(base::TimeDelta::FromMilliseconds(100));
liberato (no reviews please) 2015/08/27 20:42:26 can this be converted to a WaitForCondition or cal
Tima Vaisburd 2015/08/27 22:04:19 IsPlaybackStarted() comes when the first audio fra
Tima Vaisburd 2015/08/28 00:16:48 I removed the wait and the subsequent check for Is
1760
1761 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::AUDIO));
1762 EXPECT_FALSE(player_->IsPrerollingForTests(DemuxerStream::VIDEO));
1763
1764 // Wait till completion
1765 EXPECT_TRUE(
1766 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted,
1767 base::Unretained(&manager_)),
1768 completion_timeout));
1769
1770 // The audio codec should be recreated upon config changes.
1771 EXPECT_EQ(2, manager_.num_audio_codecs_created());
1772
1773 // The video codec should be kept.
1774 EXPECT_EQ(1, manager_.num_video_codecs_created());
1775
1776 // Check that we did not miss video frames.
1777 int expected_video_frames = duration / kVideoFramePeriod + 1;
1778 EXPECT_EQ(expected_video_frames,
1779 manager_.render_stat_[DemuxerStream::VIDEO].num_values());
1780
1781 // Check that we did not miss audio frames. We expect two postponed frames
1782 // that are not reported.
1783 int expected_audio_frames = duration / kAudioFramePeriod + 1 - 2;
1784 EXPECT_EQ(expected_audio_frames,
1785 manager_.render_stat_[DemuxerStream::AUDIO].num_values());
1786 }
1787
1591 } // namespace media 1788 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698