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 DemuxerConfigs CreateAudioVideoConfigs(const TestDataFactory* audio, |
| 120 const TestDataFactory* video) { |
| 121 DemuxerConfigs result = audio->GetConfigs(); |
| 122 DemuxerConfigs vconf = video->GetConfigs(); |
| 123 |
| 124 result.video_codec = vconf.video_codec; |
| 125 result.video_size = vconf.video_size; |
| 126 result.is_video_encrypted = vconf.is_video_encrypted; |
| 127 return result; |
| 128 } |
| 129 |
| 130 // AudioFactory creates data chunks that simulate audio stream from demuxer. |
| 131 |
| 132 class AudioFactory : public TestDataFactory { |
| 133 public: |
| 134 AudioFactory(const base::TimeDelta& duration) |
| 135 : TestDataFactory("vorbis-packet-%d", duration, kAudioFramePeriod) {} |
| 136 |
| 137 DemuxerConfigs GetConfigs() const override { |
| 138 return TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration_); |
| 139 } |
| 140 |
| 141 protected: |
| 142 void ModifyAccessUnit(int index_in_chunk, AccessUnit* unit) override { |
| 143 // Vorbis needs 4 extra bytes padding on Android to decode properly. |
| 144 // Check NuMediaExtractor.cpp in Android source code. |
| 145 uint8 padding[4] = {0xff, 0xff, 0xff, 0xff}; |
| 146 unit->data.insert(unit->data.end(), padding, padding + 4); |
| 147 } |
| 148 }; |
| 149 |
| 150 // Mock of DemuxerAndroid for testing purpose. |
| 151 |
| 152 class MockDemuxerAndroid : public DemuxerAndroid { |
| 153 public: |
| 154 MockDemuxerAndroid() : client_(nullptr) {} |
| 155 ~MockDemuxerAndroid() override {} |
| 156 |
| 157 // DemuxerAndroid implementation |
| 158 void Initialize(DemuxerAndroidClient* client) override; |
| 159 void RequestDemuxerData(DemuxerStream::Type type) override; |
| 160 void RequestDemuxerSeek(const base::TimeDelta& time_to_seek, |
| 161 bool is_browser_seek) override {} |
| 162 |
| 163 // Sets the audio data factory. |
| 164 void SetAudioFactory(scoped_ptr<TestDataFactory> factory) { |
| 165 audio_factory_ = factory.Pass(); |
| 166 } |
| 167 |
| 168 // Sets the video data factory. |
| 169 void SetVideoFactory(scoped_ptr<TestDataFactory> factory) { |
| 170 video_factory_ = factory.Pass(); |
| 171 } |
| 172 |
| 173 // Post DemuxerConfigs to the client (i.e. the player) on correct thread. |
| 174 void PostConfigs(const DemuxerConfigs& configs); |
| 175 |
| 176 // Post DemuxerConfigs derived from data factories that has been set. |
| 177 void PostInternalConfigs(); |
| 178 |
| 179 // Conditions to wait for. |
| 180 bool IsInitialized() const { return client_; } |
| 181 bool HasPendingConfigs() const { return pending_configs_; } |
| 182 |
| 183 private: |
| 184 DemuxerAndroidClient* client_; |
| 185 scoped_ptr<DemuxerConfigs> pending_configs_; |
| 186 scoped_ptr<TestDataFactory> audio_factory_; |
| 187 scoped_ptr<TestDataFactory> video_factory_; |
| 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid); |
| 190 }; |
| 191 |
| 192 void MockDemuxerAndroid::Initialize(DemuxerAndroidClient* client) { |
| 193 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| 194 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 195 |
| 196 client_ = client; |
| 197 if (pending_configs_) |
| 198 client_->OnDemuxerConfigsAvailable(*pending_configs_); |
| 199 } |
| 200 |
| 201 void MockDemuxerAndroid::RequestDemuxerData(DemuxerStream::Type type) { |
| 202 DemuxerData chunk; |
| 203 base::TimeDelta delay; |
| 204 |
| 205 if (type == DemuxerStream::AUDIO && audio_factory_) |
| 206 audio_factory_->CreateChunk(&chunk, &delay); |
| 207 else if (type == DemuxerStream::VIDEO && audio_factory_) |
| 208 video_factory_->CreateChunk(&chunk, &delay); |
| 209 else |
| 210 return; // skip request for not supported stream type |
| 211 |
| 212 chunk.type = type; |
| 213 |
| 214 // Post to Media thread. |
| 215 DCHECK(client_); |
| 216 GetMediaTaskRunner()->PostDelayedTask( |
| 217 FROM_HERE, base::Bind(&DemuxerAndroidClient::OnDemuxerDataAvailable, |
| 218 base::Unretained(client_), chunk), |
| 219 delay); |
| 220 } |
| 221 |
| 222 void MockDemuxerAndroid::PostConfigs(const DemuxerConfigs& configs) { |
| 223 DVLOG(1) << "MockDemuxerAndroid::" << __FUNCTION__; |
| 224 RUN_ON_MEDIA_THREAD(MockDemuxerAndroid, PostConfigs, configs); |
| 225 |
| 226 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| 227 |
| 228 if (client_) |
| 229 client_->OnDemuxerConfigsAvailable(configs); |
| 230 else |
| 231 pending_configs_ = scoped_ptr<DemuxerConfigs>(new DemuxerConfigs(configs)); |
| 232 } |
| 233 |
| 234 void MockDemuxerAndroid::PostInternalConfigs() { |
| 235 ASSERT_TRUE(audio_factory_ || video_factory_); |
| 236 |
| 237 if (audio_factory_ && video_factory_) { |
| 238 PostConfigs( |
| 239 CreateAudioVideoConfigs(audio_factory_.get(), video_factory_.get())); |
| 240 } else if (audio_factory_) { |
| 241 PostConfigs(audio_factory_->GetConfigs()); |
| 242 } else if (video_factory_) { |
| 243 PostConfigs(video_factory_->GetConfigs()); |
| 244 } |
| 245 } |
| 246 |
| 247 } // namespace (anonymous) |
| 248 |
| 249 // The test fixture for MediaCodecPlayer |
| 250 |
| 251 class MediaCodecPlayerTest : public testing::Test { |
| 252 public: |
| 253 MediaCodecPlayerTest(); |
| 254 ~MediaCodecPlayerTest() override; |
| 255 |
| 256 protected: |
| 257 typedef base::Callback<bool()> Predicate; |
| 258 |
| 259 void CreatePlayer(); |
| 260 |
| 261 // Waits for condition to become true or for timeout to expire. |
| 262 // Returns true if the condition becomes true. |
| 263 bool WaitForCondition(const Predicate& condition, |
| 264 const base::TimeDelta& timeout = kDefaultTimeout); |
| 265 |
| 266 base::MessageLoop message_loop_; |
| 267 MockMediaPlayerManager manager_; |
| 268 MockDemuxerAndroid* demuxer_; // owned by player_ |
| 269 MediaCodecPlayer* player_; // raw pointer due to DeleteOnCorrectThread() |
| 270 |
| 271 private: |
| 272 bool is_timeout_expired() const { return is_timeout_expired_; } |
| 273 void SetTimeoutExpired(bool value) { is_timeout_expired_ = value; } |
| 274 |
| 275 bool is_timeout_expired_; |
| 276 |
| 277 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayerTest); |
| 278 }; |
| 279 |
| 280 MediaCodecPlayerTest::MediaCodecPlayerTest() |
| 281 : demuxer_(new MockDemuxerAndroid()), player_(nullptr) { |
| 282 } |
| 283 |
| 284 void MediaCodecPlayerTest::CreatePlayer() { |
| 285 DCHECK(demuxer_); |
| 286 player_ = new MediaCodecPlayer( |
| 287 0, // player_id |
| 288 manager_.GetWeakPtr(), |
| 289 base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested, |
| 290 base::Unretained(&manager_)), |
| 291 scoped_ptr<MockDemuxerAndroid>(demuxer_), GURL()); |
| 292 |
| 293 DCHECK(player_); |
| 294 } |
| 295 |
| 296 MediaCodecPlayerTest::~MediaCodecPlayerTest() { |
| 297 if (player_) |
| 298 player_->DeleteOnCorrectThread(); |
| 299 } |
| 300 |
| 301 bool MediaCodecPlayerTest::WaitForCondition(const Predicate& condition, |
| 302 const base::TimeDelta& timeout) { |
| 303 // Let the message_loop_ process events. |
| 304 // We start the timer and RunUntilIdle() until it signals. |
| 305 |
| 306 SetTimeoutExpired(false); |
| 307 |
| 308 base::Timer timer(false, false); |
| 309 timer.Start(FROM_HERE, timeout, |
| 310 base::Bind(&MediaCodecPlayerTest::SetTimeoutExpired, |
| 311 base::Unretained(this), true)); |
| 312 |
| 313 do { |
| 314 if (condition.Run()) { |
| 315 timer.Stop(); |
| 316 return true; |
| 317 } |
| 318 message_loop_.RunUntilIdle(); |
| 319 } while (!is_timeout_expired()); |
| 320 |
| 321 DCHECK(!timer.IsRunning()); |
| 322 return false; |
| 323 } |
| 324 |
| 325 TEST_F(MediaCodecPlayerTest, SetAudioConfigsBeforePlayerCreation) { |
| 326 // Post configuration when there is no player yet. |
| 327 EXPECT_EQ(nullptr, player_); |
| 328 |
| 329 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 330 |
| 331 demuxer_->PostConfigs( |
| 332 TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration)); |
| 333 |
| 334 // Wait until the configuration gets to the media thread. |
| 335 EXPECT_TRUE(WaitForCondition(base::Bind( |
| 336 &MockDemuxerAndroid::HasPendingConfigs, base::Unretained(demuxer_)))); |
| 337 |
| 338 // Then create the player. |
| 339 CreatePlayer(); |
| 340 |
| 341 // Configuration should propagate through the player and to the manager. |
| 342 EXPECT_TRUE( |
| 343 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 344 base::Unretained(&manager_)))); |
| 345 |
| 346 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 347 EXPECT_EQ(0, manager_.media_metadata_.width); |
| 348 EXPECT_EQ(0, manager_.media_metadata_.height); |
| 349 } |
| 350 |
| 351 TEST_F(MediaCodecPlayerTest, SetAudioConfigsAfterPlayerCreation) { |
| 352 CreatePlayer(); |
| 353 |
| 354 // Wait till the player is initialized on media thread. |
| 355 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 356 base::Unretained(demuxer_)))); |
| 357 |
| 358 // Post configuration after the player has been initialized. |
| 359 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 360 demuxer_->PostConfigs( |
| 361 TestDataFactory::CreateAudioConfigs(kCodecVorbis, duration)); |
| 362 |
| 363 // Configuration should propagate through the player and to the manager. |
| 364 EXPECT_TRUE( |
| 365 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 366 base::Unretained(&manager_)))); |
| 367 |
| 368 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 369 EXPECT_EQ(0, manager_.media_metadata_.width); |
| 370 EXPECT_EQ(0, manager_.media_metadata_.height); |
| 371 } |
| 372 |
| 373 TEST_F(MediaCodecPlayerTest, SetAudioVideoConfigsAfterPlayerCreation) { |
| 374 CreatePlayer(); |
| 375 |
| 376 // Wait till the player is initialized on media thread. |
| 377 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 378 base::Unretained(demuxer_)))); |
| 379 |
| 380 // Post configuration after the player has been initialized. |
| 381 base::TimeDelta duration = base::TimeDelta::FromSeconds(10); |
| 382 demuxer_->PostConfigs(CreateAudioVideoConfigs(duration, gfx::Size(320, 240))); |
| 383 |
| 384 // Configuration should propagate through the player and to the manager. |
| 385 EXPECT_TRUE( |
| 386 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsMetadataChanged, |
| 387 base::Unretained(&manager_)))); |
| 388 |
| 389 EXPECT_EQ(duration, manager_.media_metadata_.duration); |
| 390 EXPECT_EQ(320, manager_.media_metadata_.width); |
| 391 EXPECT_EQ(240, manager_.media_metadata_.height); |
| 392 } |
| 393 |
| 394 TEST_F(MediaCodecPlayerTest, PlayAudioTillCompletion) { |
| 395 SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE(); |
| 396 |
| 397 base::TimeDelta duration = base::TimeDelta::FromMilliseconds(1000); |
| 398 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(1100); |
| 399 |
| 400 demuxer_->SetAudioFactory( |
| 401 scoped_ptr<AudioFactory>(new AudioFactory(duration))); |
| 402 |
| 403 CreatePlayer(); |
| 404 |
| 405 // Wait till the player is initialized on media thread. |
| 406 EXPECT_TRUE(WaitForCondition(base::Bind(&MockDemuxerAndroid::IsInitialized, |
| 407 base::Unretained(demuxer_)))); |
| 408 |
| 409 // Post configuration after the player has been initialized. |
| 410 demuxer_->PostInternalConfigs(); |
| 411 |
| 412 EXPECT_FALSE(manager_.IsPlaybackCompleted()); |
| 413 |
| 414 player_->Start(); |
| 415 |
| 416 EXPECT_TRUE( |
| 417 WaitForCondition(base::Bind(&MockMediaPlayerManager::IsPlaybackCompleted, |
| 418 base::Unretained(&manager_)), |
| 419 timeout)); |
| 420 } |
| 421 |
| 422 } // namespace media |
OLD | NEW |