OLD | NEW |
(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 "base/bind.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/timer/timer.h" |
| 8 #include "media/base/android/demuxer_android.h" |
| 9 #include "media/base/android/media_codec_bridge.h" |
| 10 #include "media/base/android/media_codec_player.h" |
| 11 #include "media/base/android/media_player_manager.h" |
| 12 #include "media/base/android/test_data_factory.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 // Helper macro to skip the test if MediaCodecBridge isn't available. |
| 18 #define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE() \ |
| 19 do { \ |
| 20 if (!MediaCodecBridge::IsAvailable()) { \ |
| 21 VLOG(0) << "Could not run test - not supported on device."; \ |
| 22 return; \ |
| 23 } \ |
| 24 } while (0) |
| 25 |
| 26 #define RUN_ON_MEDIA_THREAD(CLASS, METHOD, ...) \ |
| 27 do { \ |
| 28 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ |
| 29 GetMediaTaskRunner()->PostTask( \ |
| 30 FROM_HERE, \ |
| 31 base::Bind(&CLASS::METHOD, base::Unretained(this), ##__VA_ARGS__)); \ |
| 32 return; \ |
| 33 } \ |
| 34 } while (0) |
| 35 |
| 36 namespace { |
| 37 |
| 38 const base::TimeDelta kDefaultTimeout = base::TimeDelta::FromMilliseconds(200); |
| 39 const base::TimeDelta kAudioFramePeriod = base::TimeDelta::FromMilliseconds(20); |
| 40 |
| 41 // Mock of MediaPlayerManager for testing purpose. |
| 42 |
| 43 class MockMediaPlayerManager : public MediaPlayerManager { |
| 44 public: |
| 45 MockMediaPlayerManager() |
| 46 : playback_completed_(false), weak_ptr_factory_(this) {} |
| 47 ~MockMediaPlayerManager() override {} |
| 48 |
| 49 MediaResourceGetter* GetMediaResourceGetter() override { return nullptr; } |
| 50 MediaUrlInterceptor* GetMediaUrlInterceptor() override { return nullptr; } |
| 51 void OnTimeUpdate(int player_id, |
| 52 base::TimeDelta current_timestamp, |
| 53 base::TimeTicks current_time_ticks) override {} |
| 54 void OnMediaMetadataChanged(int player_id, |
| 55 base::TimeDelta duration, |
| 56 int width, |
| 57 int height, |
| 58 bool success) override { |
| 59 media_metadata_.duration = duration; |
| 60 media_metadata_.width = width; |
| 61 media_metadata_.height = height; |
| 62 media_metadata_.modified = true; |
| 63 } |
| 64 |
| 65 void OnPlaybackComplete(int player_id) override { |
| 66 playback_completed_ = true; |
| 67 } |
| 68 void OnMediaInterrupted(int player_id) override {} |
| 69 void OnBufferingUpdate(int player_id, int percentage) override {} |
| 70 void OnSeekComplete(int player_id, |
| 71 const base::TimeDelta& current_time) override {} |
| 72 void OnError(int player_id, int error) override {} |
| 73 void OnVideoSizeChanged(int player_id, int width, int height) override {} |
| 74 void OnAudibleStateChanged(int player_id, bool is_audible_now) override {} |
| 75 void OnWaitingForDecryptionKey(int player_id) override {} |
| 76 MediaPlayerAndroid* GetFullscreenPlayer() override { return nullptr; } |
| 77 MediaPlayerAndroid* GetPlayer(int player_id) override { return nullptr; } |
| 78 bool RequestPlay(int player_id) override { return true; } |
| 79 |
| 80 void OnMediaResourcesRequested(int player_id) {} |
| 81 |
| 82 base::WeakPtr<MockMediaPlayerManager> GetWeakPtr() { |
| 83 return weak_ptr_factory_.GetWeakPtr(); |
| 84 } |
| 85 |
| 86 // Conditions to wait for. |
| 87 bool IsMetadataChanged() const { return media_metadata_.modified; } |
| 88 bool IsPlaybackCompleted() const { return playback_completed_; } |
| 89 |
| 90 struct MediaMetadata { |
| 91 base::TimeDelta duration; |
| 92 int width; |
| 93 int height; |
| 94 bool modified; |
| 95 MediaMetadata() : width(0), height(0), modified(false) {} |
| 96 }; |
| 97 MediaMetadata media_metadata_; |
| 98 |
| 99 private: |
| 100 bool playback_completed_; |
| 101 |
| 102 base::WeakPtrFactory<MockMediaPlayerManager> weak_ptr_factory_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager); |
| 105 }; |
| 106 |
| 107 // Helper method that creates demuxer configuration. |
| 108 |
| 109 DemuxerConfigs CreateAudioVideoConfigs(const base::TimeDelta& duration, |
| 110 const gfx::Size& video_size) { |
| 111 DemuxerConfigs configs = |
| 112 TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration); |
| 113 configs.video_codec = kCodecVP8; |
| 114 configs.video_size = video_size; |
| 115 configs.is_video_encrypted = false; |
| 116 return configs; |
| 117 } |
| 118 |
| 119 // AudioFactory creates data chunks that simulate audio stream from demuxer. |
| 120 |
| 121 class AudioFactory : public TestDataFactory { |
| 122 public: |
| 123 AudioFactory(const base::TimeDelta& duration) |
| 124 : TestDataFactory("vorbis-packet-%d", duration, kAudioFramePeriod) {} |
| 125 |
| 126 DemuxerConfigs GetConfigs() { |
| 127 return TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration_); |
| 128 } |
| 129 |
| 130 protected: |
| 131 void ModifyAccessUnit(int index_in_chunk, AccessUnit* unit) override { |
| 132 // Vorbis needs 4 extra bytes padding on Android to decode properly. |
| 133 // Check NuMediaExtractor.cpp in Android source code. |
| 134 uint8 padding[4] = {0xff, 0xff, 0xff, 0xff}; |
| 135 unit->data.insert(unit->data.end(), padding, padding + 4); |
| 136 } |
| 137 }; |
| 138 |
| 139 // Mock of DemuxerAndroid for testing purpose. |
| 140 |
| 141 class MockDemuxerAndroid : public DemuxerAndroid { |
| 142 public: |
| 143 MockDemuxerAndroid() : client_(nullptr) {} |
| 144 ~MockDemuxerAndroid() override {} |
| 145 |
| 146 // DemuxerAndroid implementation |
| 147 void Initialize(DemuxerAndroidClient* client) override; |
| 148 void RequestDemuxerData(DemuxerStream::Type type) override; |
| 149 void RequestDemuxerSeek(const base::TimeDelta& time_to_seek, |
| 150 bool is_browser_seek) override {} |
| 151 |
| 152 // Post DemuxerConfigs to the client (i.e. the player) on correct thread. |
| 153 void PostConfigs(const DemuxerConfigs& configs); |
| 154 |
| 155 // Sets the audio data factory. |
| 156 void SetAudioFactory(scoped_refptr<TestDataFactory> factory) { |
| 157 audio_factory_ = factory; |
| 158 } |
| 159 |
| 160 // Sets the video data factory. |
| 161 void SetVideoFactory(scoped_refptr<TestDataFactory> factory) { |
| 162 video_factory_ = factory; |
| 163 } |
| 164 |
| 165 // Conditions to wait for. |
| 166 bool IsInitialized() const { return client_; } |
| 167 bool HasPendingConfigs() const { return pending_configs_; } |
| 168 |
| 169 private: |
| 170 DemuxerAndroidClient* client_; |
| 171 scoped_ptr<DemuxerConfigs> pending_configs_; |
| 172 scoped_refptr<TestDataFactory> audio_factory_; |
| 173 scoped_refptr<TestDataFactory> video_factory_; |
| 174 |
| 175 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid); |
| 176 }; |
| 177 |
| 178 void MockDemuxerAndroid::Initialize(DemuxerAndroidClient* client) { |
| 179 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| 180 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 181 |
| 182 client_ = client; |
| 183 if (pending_configs_) |
| 184 client_->OnDemuxerConfigsAvailable(*pending_configs_); |
| 185 } |
| 186 |
| 187 void MockDemuxerAndroid::RequestDemuxerData(DemuxerStream::Type type) { |
| 188 DemuxerData chunk; |
| 189 base::TimeDelta delay; |
| 190 |
| 191 if (type == DemuxerStream::AUDIO && audio_factory_) |
| 192 audio_factory_->CreateChunk(&chunk, &delay); |
| 193 else if (type == DemuxerStream::VIDEO && audio_factory_) |
| 194 video_factory_->CreateChunk(&chunk, &delay); |
| 195 else |
| 196 return; // skip request for not supported stream type |
| 197 |
| 198 chunk.type = type; |
| 199 |
| 200 // Post to Media thread. |
| 201 DCHECK(client_); |
| 202 GetMediaTaskRunner()->PostDelayedTask( |
| 203 FROM_HERE, base::Bind(&DemuxerAndroidClient::OnDemuxerDataAvailable, |
| 204 base::Unretained(client_), chunk), |
| 205 delay); |
| 206 } |
| 207 |
| 208 void MockDemuxerAndroid::PostConfigs(const DemuxerConfigs& configs) { |
| 209 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| 210 RUN_ON_MEDIA_THREAD(MockDemuxerAndroid, PostConfigs, configs); |
| 211 |
| 212 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 213 |
| 214 if (client_) |
| 215 client_->OnDemuxerConfigsAvailable(configs); |
| 216 else |
| 217 pending_configs_ = scoped_ptr<DemuxerConfigs>(new DemuxerConfigs(configs)); |
| 218 } |
| 219 |
| 220 } // namespace (anonymous) |
| 221 |
| 222 // The test fixture for MediaCodecPlayer |
| 223 |
| 224 class MediaCodecPlayerTest : public testing::Test { |
| 225 public: |
| 226 MediaCodecPlayerTest(); |
| 227 ~MediaCodecPlayerTest() override; |
| 228 |
| 229 protected: |
| 230 typedef base::Callback<bool()> Predicate; |
| 231 |
| 232 void CreatePlayer(); |
| 233 |
| 234 // Waits for condition to become true or for timeout to expire. |
| 235 // Returns true if the condition becomes true. |
| 236 bool WaitForCondition(const Predicate& condition, |
| 237 const base::TimeDelta& timeout = kDefaultTimeout); |
| 238 |
| 239 base::MessageLoop message_loop_; |
| 240 MockMediaPlayerManager manager_; |
| 241 MockDemuxerAndroid* demuxer_; // owned by player_ |
| 242 MediaCodecPlayer* player_; // raw pointer due to DeleteOnCorrectThread() |
| 243 |
| 244 private: |
| 245 bool is_timeout_expired() const { return is_timeout_expired_; } |
| 246 void SetTimeoutExpired(bool value) { is_timeout_expired_ = value; } |
| 247 |
| 248 bool is_timeout_expired_; |
| 249 |
| 250 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayerTest); |
| 251 }; |
| 252 |
| 253 MediaCodecPlayerTest::MediaCodecPlayerTest() |
| 254 : demuxer_(new MockDemuxerAndroid()), player_(nullptr) { |
| 255 } |
| 256 |
| 257 void MediaCodecPlayerTest::CreatePlayer() { |
| 258 DCHECK(demuxer_); |
| 259 player_ = new MediaCodecPlayer( |
| 260 0, // player_id |
| 261 manager_.GetWeakPtr(), |
| 262 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested, |
| 263 base::Unretained(&manager_)), |
| 264 scoped_ptr<MockDemuxerAndroid>(demuxer_), GURL()); |
| 265 |
| 266 DCHECK(player_); |
| 267 } |
| 268 |
| 269 MediaCodecPlayerTest::~MediaCodecPlayerTest() { |
| 270 if (player_) |
| 271 player_->DeleteOnCorrectThread(); |
| 272 } |
| 273 |
| 274 bool MediaCodecPlayerTest::WaitForCondition(const Predicate& condition, |
| 275 const base::TimeDelta& timeout) { |
| 276 // Let the message_loop_ process events. |
| 277 // We start the timer and RunUntilIdle() until it signals. |
| 278 |
| 279 SetTimeoutExpired(false); |
| 280 |
| 281 base::Timer timer(false, false); |
| 282 timer.Start(FROM_HERE, timeout, |
| 283 base::Bind(&MediaCodecPlayerTest::SetTimeoutExpired, |
| 284 base::Unretained(this), true)); |
| 285 |
| 286 do { |
| 287 if (condition.Run()) { |
| 288 timer.Stop(); |
| 289 return true; |
| 290 } |
| 291 message_loop_.RunUntilIdle(); |
| 292 } while (!is_timeout_expired()); |
| 293 |
| 294 DCHECK(!timer.IsRunning()); |
| 295 return false; |
| 296 } |
| 297 |
| 298 TEST_F(MediaCodecPlayerTest, SetAudioConfigsBeforePlayerCreation) { |
| 299 // Post configuration when there is no player yet. |
| 300 EXPECT_EQ(nullptr, player_); |
| 301 |
| 302 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 303 |
| 304 demuxer_->PostConfigs( |
| 305 TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration)); |
| 306 |
| 307 // Wait until the configuration gets to the media thread. |
| 308 WaitForCondition(base::Bind(&MockDemuxerAndroid::HasPendingConfigs, |
| 309 base::Unretained(demuxer_))); |
| 310 |
| 311 // Then create the player. |
| 312 CreatePlayer(); |
| 313 |
| 314 // Configuration should propagate through the player and to the manager. |
| 315 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 316 base::Unretained(&manager_))); |
| 317 |
| 318 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 319 EXPECT_EQ(0, manager_.media_metadata_.width); |
| 320 EXPECT_EQ(0, manager_.media_metadata_.height); |
| 321 } |
| 322 |
| 323 TEST_F(MediaCodecPlayerTest, SetAudioConfigsAfterPlayerCreation) { |
| 324 CreatePlayer(); |
| 325 |
| 326 // Wait till the player is initialized on media thread. |
| 327 WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 328 base::Unretained(demuxer_))); |
| 329 |
| 330 // Post configuration after the player has been initialized. |
| 331 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 332 demuxer_->PostConfigs( |
| 333 TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration)); |
| 334 |
| 335 // Configuration should propagate through the player and to the manager. |
| 336 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 337 base::Unretained(&manager_))); |
| 338 |
| 339 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 340 EXPECT_EQ(0, manager_.media_metadata_.width); |
| 341 EXPECT_EQ(0, manager_.media_metadata_.height); |
| 342 } |
| 343 |
| 344 TEST_F(MediaCodecPlayerTest, SetAudioVideoConfigsAfterPlayerCreation) { |
| 345 CreatePlayer(); |
| 346 |
| 347 // Wait till the player is initialized on media thread. |
| 348 WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 349 base::Unretained(demuxer_))); |
| 350 |
| 351 // Post configuration after the player has been initialized. |
| 352 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 353 demuxer_->PostConfigs(CreateAudioVideoConfigs(duration, gfx::Size(320, 240))); |
| 354 |
| 355 // Configuration should propagate through the player and to the manager. |
| 356 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 357 base::Unretained(&manager_))); |
| 358 |
| 359 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 360 EXPECT_EQ(320, manager_.media_metadata_.width); |
| 361 EXPECT_EQ(240, manager_.media_metadata_.height); |
| 362 } |
| 363 |
| 364 TEST_F(MediaCodecPlayerTest, PlayAudioTillCompletion) { |
| 365 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); |
| 366 |
| 367 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000); |
| 368 scoped_refptr<AudioFactory> factory(new AudioFactory(duration)); |
| 369 demuxer_->SetAudioFactory(factory); |
| 370 |
| 371 CreatePlayer(); |
| 372 |
| 373 // Wait till the player is initialized on media thread. |
| 374 WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 375 base::Unretained(demuxer_))); |
| 376 |
| 377 // Post configuration after the player has been initialized. |
| 378 demuxer_->PostConfigs(factory->GetConfigs()); |
| 379 |
| 380 EXPECT_FALSE(manager_.IsPlaybackCompleted()); |
| 381 |
| 382 player_->Start(); |
| 383 |
| 384 bool playback_completed = |
| 385 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted, |
| 386 base::Unretained(&manager_)), |
| 387 base::TimeDelta::FromMilliseconds(1100)); |
| 388 EXPECT_TRUE(playback_completed); |
| 389 } |
| 390 |
| 391 } // namespace media |
OLD | NEW |