Chromium Code Reviews| Index: media/base/android/media_codec_player_unittest.cc |
| diff --git a/media/base/android/media_codec_player_unittest.cc b/media/base/android/media_codec_player_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c1b0d1d27a72aeda024ac9328b0e5e33a4209a38 |
| --- /dev/null |
| +++ b/media/base/android/media_codec_player_unittest.cc |
| @@ -0,0 +1,488 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
|
watk
2015/06/05 01:00:33
s/2013/2015
Tima Vaisburd
2015/06/05 04:17:47
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/timer/timer.h" |
| +#include "media/base/android/demuxer_android.h" |
| +#include "media/base/android/media_codec_bridge.h" |
| +#include "media/base/android/media_codec_player.h" |
| +#include "media/base/android/media_player_manager.h" |
| +#include "media/base/decoder_buffer.h" |
| +#include "media/base/test_data_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace media { |
| + |
| +// Helper macro to skip the test if MediaCodecBridge isn't available. |
| +#define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE() \ |
| + do { \ |
| + if (!MediaCodecBridge::IsAvailable()) { \ |
| + VLOG(0) << "Could not run test - not supported on device."; \ |
| + return; \ |
| + } \ |
| + } while (0) |
| + |
| + |
| +#define RUN_ON_MEDIA_THREAD(CLASS, METHOD, ...) \ |
| + do { \ |
| + if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ |
| + GetMediaTaskRunner()->PostTask( \ |
| + FROM_HERE, \ |
| + base::Bind(&CLASS :: METHOD, base::Unretained(this), ##__VA_ARGS__));\ |
| + return; \ |
| + } \ |
| + } while(0) |
| + |
| +namespace { |
| +const base::TimeDelta kDefaultDelay = base::TimeDelta::FromMilliseconds(200); |
| +} |
| + |
| +// Mock of MediaPlayerManager for testing purpose. |
| + |
| +class MockMediaPlayerManager : public MediaPlayerManager { |
| + public: |
| + MockMediaPlayerManager() |
| + : playback_completed_(false), |
| + weak_ptr_factory_(this) {} |
| + ~MockMediaPlayerManager() override {} |
| + |
| + MediaResourceGetter* GetMediaResourceGetter() override { return nullptr; } |
| + MediaUrlInterceptor* GetMediaUrlInterceptor() override { return nullptr; } |
| + void OnTimeUpdate(int player_id, |
| + base::TimeDelta current_timestamp, |
| + base::TimeTicks current_time_ticks) override {} |
| + void OnMediaMetadataChanged( |
| + int player_id, |
| + base::TimeDelta duration, |
| + int width, |
| + int height, |
| + bool success) override { |
| + media_metadata_.duration = duration; |
| + media_metadata_.width = width; |
| + media_metadata_.height = height; |
| + media_metadata_.modified = true; |
| + } |
| + |
| + void OnPlaybackComplete(int player_id) override { |
| + playback_completed_ = true; |
| + } |
| + void OnMediaInterrupted(int player_id) override {} |
| + void OnBufferingUpdate(int player_id, int percentage) override {} |
| + void OnSeekComplete( |
| + int player_id, |
| + const base::TimeDelta& current_time) override {} |
| + void OnError(int player_id, int error) override {} |
| + void OnVideoSizeChanged(int player_id, int width, int height) override {} |
| + void OnAudibleStateChanged(int player_id, bool is_audible_now) override {} |
| + void OnWaitingForDecryptionKey(int player_id) override {} |
| + MediaPlayerAndroid* GetFullscreenPlayer() override { return nullptr; } |
| + MediaPlayerAndroid* GetPlayer(int player_id) override { return nullptr; } |
| + void RequestFullScreen(int player_id) override {} |
| + |
| + void OnMediaResourcesRequested(int player_id) {} |
| + |
| + base::WeakPtr<MockMediaPlayerManager> GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| + } |
| + |
| + // Conditions to wait for. |
| + bool IsMetadataChanged() const { return media_metadata_.modified; } |
| + bool IsPlaybackCompleted() const { return playback_completed_; } |
| + |
| + struct MediaMetadata { |
| + base::TimeDelta duration; |
| + int width; |
| + int height; |
| + bool modified; |
| + MediaMetadata() : width(0), height(0), modified(false) {} |
| + }; |
| + MediaMetadata media_metadata_; |
| + |
| + private: |
| + bool playback_completed_; |
| + |
| + base::WeakPtrFactory<MockMediaPlayerManager> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager); |
| +}; |
| + |
| +// ChunkFactory defines how the data will be generated. |
| + |
| +struct ChunkFactory { |
| + virtual ~ChunkFactory() {} |
| + // Returns true if data is created. |
| + virtual bool CreateChunk(DemuxerStream::Type stream_type, |
| + DemuxerData* chunk, base::TimeDelta* delay) = 0; |
| +}; |
| + |
| +class AudioFactory : public ChunkFactory { |
| + public: |
| + AudioFactory(const base::TimeDelta& duration, |
| + const base::TimeDelta& frame_period); |
| + |
| + bool CreateChunk(DemuxerStream::Type stream_type, |
| + DemuxerData* chunk, base::TimeDelta* delay) override; |
| + |
| + private: |
| + base::TimeDelta duration_; |
| + base::TimeDelta frame_period_; |
| + std::vector<uint8> packet_[4]; |
| + base::TimeDelta current_pts_; |
| +}; |
| + |
| +AudioFactory::AudioFactory(const base::TimeDelta& duration, |
| + const base::TimeDelta& frame_period) |
| + : duration_(duration), |
| + frame_period_(frame_period) { |
| + // Load packets |
| + for (int i = 0; i < 4; ++i) { |
| + scoped_refptr<DecoderBuffer> buffer = |
| + ReadTestDataFile(base::StringPrintf("vorbis-packet-%d", i)); |
| + packet_[i] = std::vector<uint8>( |
| + buffer->data(), buffer->data() + buffer->data_size()); |
| + } |
| +} |
| + |
| +bool AudioFactory::CreateChunk(DemuxerStream::Type stream_type, |
| + DemuxerData* chunk, base::TimeDelta* delay) { |
| + if (stream_type != DemuxerStream::AUDIO) |
| + return false; |
| + |
| + DCHECK(chunk); |
| + DCHECK(delay); |
| + |
| + *delay = base::TimeDelta(); |
| + |
| + chunk->type = stream_type; |
| + for (int i = 0; i < 4; ++i) { |
| + chunk->access_units.push_back(AccessUnit()); |
| + AccessUnit& unit = chunk->access_units.back(); |
| + unit.status = DemuxerStream::kOk; |
| + |
| + unit.timestamp = current_pts_; |
| + current_pts_ += frame_period_; |
| + |
| + if (unit.timestamp > duration_) { |
| + unit.is_end_of_stream = true; |
| + break; // EOS units have no data |
| + } |
| + |
| + unit.data = packet_[i]; |
| + // Vorbis needs 4 extra bytes padding on Android to decode properly. Check |
| + // NuMediaExtractor.cpp in Android source code. |
| + uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff }; |
| + unit.data.insert(unit.data.end(), padding, padding + 4); |
| + } |
| + return true; |
| +} |
| + |
| +// Mock of DemuxerAndroid for testing purpose. |
| + |
| +class MockDemuxerAndroid : public DemuxerAndroid { |
| + public: |
| + MockDemuxerAndroid() : client_(nullptr) {} |
| + ~MockDemuxerAndroid() override {} |
| + |
| + // DemuxerAndroid implementation |
| + void Initialize(DemuxerAndroidClient* client) override; |
| + void RequestDemuxerData(DemuxerStream::Type type) override; |
| + void RequestDemuxerSeek(const base::TimeDelta& time_to_seek, |
| + bool is_browser_seek) override {} |
| + |
| + // Post DemuxerConfigs to the client (i.e. the player) on correct thread. |
| + void PostConfigs(const DemuxerConfigs& configs); |
| + |
| + // Sets the data rule that governs the process of packet generation. |
| + void SetChunkFactory(scoped_ptr<ChunkFactory> factory) { |
| + chunk_factory_ = factory.Pass(); |
| + } |
| + |
| + // Conditions to wait for. |
| + bool IsInitialized() const { return client_; } |
| + bool HasPendingConfigs() const { return pending_configs_; } |
| + |
| + private: |
| + DemuxerAndroidClient* client_; |
| + scoped_ptr<DemuxerConfigs> pending_configs_; |
| + scoped_ptr<ChunkFactory> chunk_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid); |
| +}; |
| + |
| +void MockDemuxerAndroid::Initialize(DemuxerAndroidClient* client) { |
| + DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + |
| + client_ = client; |
| + if (pending_configs_) |
| + client_->OnDemuxerConfigsAvailable(*pending_configs_); |
| +} |
| + |
| +void MockDemuxerAndroid::RequestDemuxerData(DemuxerStream::Type type) { |
| + DCHECK(chunk_factory_); |
| + DCHECK(client_); |
| + |
| + DemuxerData chunk; |
| + base::TimeDelta delay; |
| + if (chunk_factory_->CreateChunk(type, &chunk, &delay)) { |
| + // Post to Media thread. |
| + GetMediaTaskRunner()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&DemuxerAndroidClient::OnDemuxerDataAvailable, |
| + base::Unretained(client_), chunk), |
| + delay); |
| + } |
| +} |
| + |
| +void MockDemuxerAndroid::PostConfigs(const DemuxerConfigs& configs) { |
| + DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| + RUN_ON_MEDIA_THREAD(MockDemuxerAndroid, PostConfigs, configs); |
| + |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + |
| + if (client_) |
| + client_->OnDemuxerConfigsAvailable(configs); |
| + else |
| + pending_configs_ = scoped_ptr<DemuxerConfigs>(new DemuxerConfigs(configs)); |
| +} |
| + |
| +// The test fixture for MediaCodecPlayer |
| + |
| +class MediaCodecPlayerTest : public testing::Test { |
| + public: |
| + MediaCodecPlayerTest(); |
| + ~MediaCodecPlayerTest() override; |
| + |
| + protected: |
| + typedef base::Callback<bool ()> Predicate; |
| + |
| + void CreatePlayer(); |
| + |
| + // Waits for condition to become true or for delay to expire. |
| + // Returns true if the condition becomes true. |
| + bool WaitForCondition(const Predicate& condition, |
| + const base::TimeDelta& delay = kDefaultDelay); |
| + |
| + DemuxerConfigs CreateAudioConfigs( |
| + AudioCodec audio_codec, const base::TimeDelta& duration); |
| + DemuxerConfigs CreateVideoConfigs(const base::TimeDelta& duration); |
| + DemuxerConfigs CreateAudioVideoConfigs(const base::TimeDelta& duration); |
| + |
| + base::MessageLoop message_loop_; |
| + MockMediaPlayerManager manager_; |
| + MockDemuxerAndroid* demuxer_; // owned by player_ |
| + MediaCodecPlayer* player_; // raw pointer due to DeleteOnCorrectThread() |
| + |
| + private: |
| + bool is_delay_expired() const { return is_delay_expired_; } |
| + void SetDelayExpired(bool value) { |
| + is_delay_expired_ = value; |
| + } |
| + |
| + bool is_delay_expired_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayerTest); |
| +}; |
| + |
| +MediaCodecPlayerTest::MediaCodecPlayerTest() |
| + : demuxer_(new MockDemuxerAndroid()), |
| + player_(nullptr) { |
| +} |
| + |
| +void MediaCodecPlayerTest::CreatePlayer() { |
| + DCHECK(demuxer_); |
| + player_ = new MediaCodecPlayer( |
| + 0, // player_id |
| + manager_.GetWeakPtr(), |
| + base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested, |
| + base::Unretained(&manager_)), |
| + scoped_ptr<MockDemuxerAndroid>(demuxer_), |
| + GURL()); |
| + |
| + DCHECK(player_); |
| +} |
| + |
| +MediaCodecPlayerTest::~MediaCodecPlayerTest() { |
| + if (player_) |
| + player_->DeleteOnCorrectThread(); |
| +} |
| + |
| +bool MediaCodecPlayerTest::WaitForCondition(const Predicate& condition, |
| + const base::TimeDelta& delay) { |
| + // Let the message_loop_ process events. |
| + // We post delayed task and RunUntilIdle() until it signals. |
| + |
| + SetDelayExpired(false); |
| + |
| + base::Timer timer(false, false); |
| + timer.Start(FROM_HERE, delay, |
| + base::Bind(&MediaCodecPlayerTest::SetDelayExpired, |
| + base::Unretained(this), true)); |
| + |
| + do { |
| + if (condition.Run()) { |
| + timer.Stop(); |
| + return true; |
| + } |
| + message_loop_.RunUntilIdle(); |
| + } while (!is_delay_expired()); |
| + |
| + DCHECK(!timer.IsRunning()); |
| + return false; |
| +} |
| + |
| +DemuxerConfigs MediaCodecPlayerTest::CreateAudioConfigs( |
| + AudioCodec audio_codec, const base::TimeDelta& duration) { |
| + |
| + DemuxerConfigs configs; |
| + configs.audio_codec = audio_codec; |
| + configs.audio_channels = 2; |
| + configs.is_audio_encrypted = false; |
| + configs.duration = duration; |
| + |
| + // Other codecs are not yet supported by this helper. |
| + EXPECT_TRUE(audio_codec == kCodecAAC || audio_codec == kCodecVorbis); |
| + |
| + switch (audio_codec) { |
| + case kCodecVorbis: |
| + { |
| + configs.audio_sampling_rate = 44100; |
| + scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile( |
| + "vorbis-extradata"); |
| + configs.audio_extra_data = std::vector<uint8>( |
| + buffer->data(), |
| + buffer->data() + buffer->data_size()); |
| + } |
| + break; |
| + |
| + case kCodecAAC: |
| + { |
| + configs.audio_sampling_rate = 48000; |
| + uint8 aac_extra_data[] = { 0x13, 0x10 }; |
| + configs.audio_extra_data = std::vector<uint8>( |
| + aac_extra_data, |
| + aac_extra_data + 2); |
| + } |
| + break; |
| + |
| + default: |
| + NOTREACHED(); |
| + break; |
| + } |
| + |
| + return configs; |
| +} |
| + |
| +DemuxerConfigs MediaCodecPlayerTest::CreateVideoConfigs( |
| + const base::TimeDelta& duration) { |
| + DemuxerConfigs configs; |
| + configs.video_codec = kCodecVP8; |
| + configs.video_size = gfx::Size(320, 240); |
| + configs.is_video_encrypted = false; |
| + return configs; |
| +} |
| + |
| +DemuxerConfigs MediaCodecPlayerTest::CreateAudioVideoConfigs( |
| + const base::TimeDelta& duration) { |
| + DemuxerConfigs configs = CreateAudioConfigs(kCodecVorbis, duration); |
| + configs.video_codec = kCodecVP8; |
| + configs.video_size = gfx::Size(320, 240); |
| + configs.is_video_encrypted = false; |
| + return configs; |
| +} |
| + |
| +TEST_F(MediaCodecPlayerTest, SetAudioConfigsBeforePlayerCreation) { |
| + // Post configuration when there is no player yet. |
| + EXPECT_EQ(nullptr, player_); |
| + |
| + base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| + demuxer_->PostConfigs(CreateAudioConfigs(kCodecVorbis, duration)); |
| + |
| + // Wait until the configuration gets to the media thread. |
| + WaitForCondition(base::Bind(&MockDemuxerAndroid::HasPendingConfigs, |
| + base::Unretained(demuxer_))); |
| + |
| + // Then create the player. |
| + CreatePlayer(); |
| + |
| + // Configuration should propagate through the player and to the manager. |
| + WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| + base::Unretained(&manager_))); |
| + |
| + EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| + EXPECT_EQ(0, manager_.media_metadata_.width); |
| + EXPECT_EQ(0, manager_.media_metadata_.height); |
| +} |
| + |
| +TEST_F(MediaCodecPlayerTest, SetAudioConfigsAfterPlayerCreation) { |
| + CreatePlayer(); |
| + |
| + // Wait till the player is initialized on media thread. |
| + WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| + base::Unretained(demuxer_))); |
| + |
| + // Post configuration after the player has been initialized. |
| + base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| + demuxer_->PostConfigs(CreateAudioConfigs(kCodecVorbis, duration)); |
| + |
| + // Configuration should propagate through the player and to the manager. |
| + WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| + base::Unretained(&manager_))); |
| + |
| + EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| + EXPECT_EQ(0, manager_.media_metadata_.width); |
| + EXPECT_EQ(0, manager_.media_metadata_.height); |
| +} |
| + |
| +TEST_F(MediaCodecPlayerTest, SetAudioVideoConfigsAfterPlayerCreation) { |
| + CreatePlayer(); |
| + |
| + // Wait till the player is initialized on media thread. |
| + WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| + base::Unretained(demuxer_))); |
| + |
| + // Post configuration after the player has been initialized. |
| + base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| + demuxer_->PostConfigs(CreateAudioVideoConfigs(duration)); |
| + |
| + // Configuration should propagate through the player and to the manager. |
| + WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| + base::Unretained(&manager_))); |
| + |
| + EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| + EXPECT_EQ(320, manager_.media_metadata_.width); |
| + EXPECT_EQ(240, manager_.media_metadata_.height); |
| +} |
| + |
| +TEST_F(MediaCodecPlayerTest, PlayAudioTillCompletion) { |
| + SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); |
| + |
| + base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000); |
| + base::TimeDelta frame_period = base::TimeDelta::FromMilliseconds(20); |
| + demuxer_->SetChunkFactory(scoped_ptr<ChunkFactory>( |
| + new AudioFactory(duration, frame_period))); |
| + |
| + CreatePlayer(); |
| + |
| + // Wait till the player is initialized on media thread. |
| + WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| + base::Unretained(demuxer_))); |
| + |
| + // Post configuration after the player has been initialized. |
| + demuxer_->PostConfigs(CreateAudioConfigs(kCodecVorbis, duration)); |
| + |
| + EXPECT_FALSE(manager_.IsPlaybackCompleted()); |
| + |
| + player_->Start(); |
| + |
| + bool playback_completed = |
| + WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted, |
| + base::Unretained(&manager_)), |
| + base::TimeDelta::FromMilliseconds(1100)); |
| + EXPECT_TRUE(playback_completed); |
| +} |
| + |
| +} // namespace media |