AwesomePlayer.cpp revision f03034408506051f2f836e59305fcd5f662bf19a
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "AwesomePlayer" 19#include <utils/Log.h> 20 21#include <dlfcn.h> 22 23#include "include/ARTSPController.h" 24#include "include/AwesomePlayer.h" 25#include "include/SoftwareRenderer.h" 26#include "include/NuCachedSource2.h" 27#include "include/ThrottledSource.h" 28#include "include/MPEG2TSExtractor.h" 29 30#include <binder/IPCThreadState.h> 31#include <media/stagefright/foundation/hexdump.h> 32#include <media/stagefright/foundation/ADebug.h> 33#include <media/stagefright/AudioPlayer.h> 34#include <media/stagefright/DataSource.h> 35#include <media/stagefright/FileSource.h> 36#include <media/stagefright/MediaBuffer.h> 37#include <media/stagefright/MediaDefs.h> 38#include <media/stagefright/MediaExtractor.h> 39#include <media/stagefright/MediaSource.h> 40#include <media/stagefright/MetaData.h> 41#include <media/stagefright/OMXCodec.h> 42 43#include <surfaceflinger/Surface.h> 44 45#include <media/stagefright/foundation/ALooper.h> 46#include <media/stagefright/foundation/AMessage.h> 47#include "include/LiveSession.h" 48 49#define USE_SURFACE_ALLOC 1 50#define FRAME_DROP_FREQ 7 51 52namespace android { 53 54static int64_t kLowWaterMarkUs = 2000000ll; // 2secs 55static int64_t kHighWaterMarkUs = 10000000ll; // 10secs 56static int64_t kHighWaterMarkRTSPUs = 4000000ll; // 4secs 57static const size_t kLowWaterMarkBytes = 40000; 58static const size_t kHighWaterMarkBytes = 200000; 59 60struct AwesomeEvent : public TimedEventQueue::Event { 61 AwesomeEvent( 62 AwesomePlayer *player, 63 void (AwesomePlayer::*method)()) 64 : mPlayer(player), 65 mMethod(method) { 66 } 67 68protected: 69 virtual ~AwesomeEvent() {} 70 71 virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) { 72 (mPlayer->*mMethod)(); 73 } 74 75private: 76 AwesomePlayer *mPlayer; 77 void (AwesomePlayer::*mMethod)(); 78 79 AwesomeEvent(const AwesomeEvent &); 80 AwesomeEvent &operator=(const AwesomeEvent &); 81}; 82 83struct AwesomeLocalRenderer : public AwesomeRenderer { 84 AwesomeLocalRenderer( 85 const sp<Surface> &surface, const sp<MetaData> &meta) 86 : mTarget(new SoftwareRenderer(surface, meta)) { 87 } 88 89 virtual void render(MediaBuffer *buffer) { 90 render((const uint8_t *)buffer->data() + buffer->range_offset(), 91 buffer->range_length()); 92 } 93 94 void render(const void *data, size_t size) { 95 mTarget->render(data, size, NULL); 96 } 97 98protected: 99 virtual ~AwesomeLocalRenderer() { 100 delete mTarget; 101 mTarget = NULL; 102 } 103 104private: 105 SoftwareRenderer *mTarget; 106 107 AwesomeLocalRenderer(const AwesomeLocalRenderer &); 108 AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);; 109}; 110 111struct AwesomeNativeWindowRenderer : public AwesomeRenderer { 112 AwesomeNativeWindowRenderer( 113 const sp<ANativeWindow> &nativeWindow, 114 int32_t rotationDegrees) 115 : mNativeWindow(nativeWindow) { 116 applyRotation(rotationDegrees); 117 } 118 119 virtual void render(MediaBuffer *buffer) { 120 status_t err = mNativeWindow->queueBuffer( 121 mNativeWindow.get(), buffer->graphicBuffer().get()); 122 if (err != 0) { 123 LOGE("queueBuffer failed with error %s (%d)", strerror(-err), 124 -err); 125 return; 126 } 127 128 sp<MetaData> metaData = buffer->meta_data(); 129 metaData->setInt32(kKeyRendered, 1); 130 } 131 132protected: 133 virtual ~AwesomeNativeWindowRenderer() {} 134 135private: 136 sp<ANativeWindow> mNativeWindow; 137 138 void applyRotation(int32_t rotationDegrees) { 139 uint32_t transform; 140 switch (rotationDegrees) { 141 case 0: transform = 0; break; 142 case 90: transform = HAL_TRANSFORM_ROT_90; break; 143 case 180: transform = HAL_TRANSFORM_ROT_180; break; 144 case 270: transform = HAL_TRANSFORM_ROT_270; break; 145 default: transform = 0; break; 146 } 147 148 if (transform) { 149 CHECK_EQ(0, native_window_set_buffers_transform( 150 mNativeWindow.get(), transform)); 151 } 152 } 153 154 AwesomeNativeWindowRenderer(const AwesomeNativeWindowRenderer &); 155 AwesomeNativeWindowRenderer &operator=( 156 const AwesomeNativeWindowRenderer &); 157}; 158 159//////////////////////////////////////////////////////////////////////////////// 160 161AwesomePlayer::AwesomePlayer() 162 : mQueueStarted(false), 163 mTimeSource(NULL), 164 mVideoRendererIsPreview(false), 165 mAudioPlayer(NULL), 166 mDisplayWidth(0), 167 mDisplayHeight(0), 168 mFlags(0), 169 mExtractorFlags(0), 170 mVideoBuffer(NULL), 171 mDecryptHandle(NULL) { 172 CHECK_EQ(mClient.connect(), (status_t)OK); 173 174 DataSource::RegisterDefaultSniffers(); 175 176 mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent); 177 mVideoEventPending = false; 178 mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone); 179 mStreamDoneEventPending = false; 180 mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate); 181 mBufferingEventPending = false; 182 mVideoLagEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoLagUpdate); 183 mVideoEventPending = false; 184 185 mCheckAudioStatusEvent = new AwesomeEvent( 186 this, &AwesomePlayer::onCheckAudioStatus); 187 188 mAudioStatusEventPending = false; 189 190 reset(); 191} 192 193AwesomePlayer::~AwesomePlayer() { 194 if (mQueueStarted) { 195 mQueue.stop(); 196 } 197 198 reset(); 199 200 mClient.disconnect(); 201} 202 203void AwesomePlayer::cancelPlayerEvents(bool keepBufferingGoing) { 204 mQueue.cancelEvent(mVideoEvent->eventID()); 205 mVideoEventPending = false; 206 mQueue.cancelEvent(mStreamDoneEvent->eventID()); 207 mStreamDoneEventPending = false; 208 mQueue.cancelEvent(mCheckAudioStatusEvent->eventID()); 209 mAudioStatusEventPending = false; 210 mQueue.cancelEvent(mVideoLagEvent->eventID()); 211 mVideoLagEventPending = false; 212 213 if (!keepBufferingGoing) { 214 mQueue.cancelEvent(mBufferingEvent->eventID()); 215 mBufferingEventPending = false; 216 } 217} 218 219void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) { 220 Mutex::Autolock autoLock(mLock); 221 mListener = listener; 222} 223 224status_t AwesomePlayer::setDataSource( 225 const char *uri, const KeyedVector<String8, String8> *headers) { 226 Mutex::Autolock autoLock(mLock); 227 return setDataSource_l(uri, headers); 228} 229 230status_t AwesomePlayer::setDataSource_l( 231 const char *uri, const KeyedVector<String8, String8> *headers) { 232 reset_l(); 233 234 mUri = uri; 235 236 if (!strncmp("http://", uri, 7)) { 237 // Hack to support http live. 238 239 size_t len = strlen(uri); 240 if (!strcasecmp(&uri[len - 5], ".m3u8") 241 || strstr(&uri[7], "m3u8") != NULL) { 242 mUri = "httplive://"; 243 mUri.append(&uri[7]); 244 } 245 } 246 247 if (headers) { 248 mUriHeaders = *headers; 249 } 250 251 // The actual work will be done during preparation in the call to 252 // ::finishSetDataSource_l to avoid blocking the calling thread in 253 // setDataSource for any significant time. 254 255 return OK; 256} 257 258status_t AwesomePlayer::setDataSource( 259 int fd, int64_t offset, int64_t length) { 260 Mutex::Autolock autoLock(mLock); 261 262 reset_l(); 263 264 sp<DataSource> dataSource = new FileSource(fd, offset, length); 265 266 status_t err = dataSource->initCheck(); 267 268 if (err != OK) { 269 return err; 270 } 271 272 mFileSource = dataSource; 273 274 return setDataSource_l(dataSource); 275} 276 277status_t AwesomePlayer::setDataSource(const sp<IStreamSource> &source) { 278 return INVALID_OPERATION; 279} 280 281status_t AwesomePlayer::setDataSource_l( 282 const sp<DataSource> &dataSource) { 283 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 284 285 if (extractor == NULL) { 286 return UNKNOWN_ERROR; 287 } 288 289 dataSource->getDrmInfo(&mDecryptHandle, &mDrmManagerClient); 290 if (mDecryptHandle != NULL) { 291 CHECK(mDrmManagerClient); 292 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) { 293 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_NO_LICENSE); 294 } 295 } 296 297 return setDataSource_l(extractor); 298} 299 300status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) { 301 // Attempt to approximate overall stream bitrate by summing all 302 // tracks' individual bitrates, if not all of them advertise bitrate, 303 // we have to fail. 304 305 int64_t totalBitRate = 0; 306 307 for (size_t i = 0; i < extractor->countTracks(); ++i) { 308 sp<MetaData> meta = extractor->getTrackMetaData(i); 309 310 int32_t bitrate; 311 if (!meta->findInt32(kKeyBitRate, &bitrate)) { 312 totalBitRate = -1; 313 break; 314 } 315 316 totalBitRate += bitrate; 317 } 318 319 mBitrate = totalBitRate; 320 321 LOGV("mBitrate = %lld bits/sec", mBitrate); 322 323 bool haveAudio = false; 324 bool haveVideo = false; 325 for (size_t i = 0; i < extractor->countTracks(); ++i) { 326 sp<MetaData> meta = extractor->getTrackMetaData(i); 327 328 const char *mime; 329 CHECK(meta->findCString(kKeyMIMEType, &mime)); 330 331 if (!haveVideo && !strncasecmp(mime, "video/", 6)) { 332 setVideoSource(extractor->getTrack(i)); 333 haveVideo = true; 334 335 // Set the presentation/display size 336 int32_t displayWidth, displayHeight; 337 bool success = meta->findInt32(kKeyDisplayWidth, &displayWidth); 338 if (success) { 339 success = meta->findInt32(kKeyDisplayHeight, &displayHeight); 340 } 341 if (success) { 342 mDisplayWidth = displayWidth; 343 mDisplayHeight = displayHeight; 344 } 345 346 } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) { 347 setAudioSource(extractor->getTrack(i)); 348 haveAudio = true; 349 350 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { 351 // Only do this for vorbis audio, none of the other audio 352 // formats even support this ringtone specific hack and 353 // retrieving the metadata on some extractors may turn out 354 // to be very expensive. 355 sp<MetaData> fileMeta = extractor->getMetaData(); 356 int32_t loop; 357 if (fileMeta != NULL 358 && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) { 359 mFlags |= AUTO_LOOPING; 360 } 361 } 362 } 363 364 if (haveAudio && haveVideo) { 365 break; 366 } 367 } 368 369 if (!haveAudio && !haveVideo) { 370 return UNKNOWN_ERROR; 371 } 372 373 mExtractorFlags = extractor->flags(); 374 375 return OK; 376} 377 378void AwesomePlayer::reset() { 379 LOGI("reset"); 380 381 Mutex::Autolock autoLock(mLock); 382 reset_l(); 383} 384 385void AwesomePlayer::reset_l() { 386 LOGI("reset_l"); 387 mDisplayWidth = 0; 388 mDisplayHeight = 0; 389 390 if (mDecryptHandle != NULL) { 391 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 392 Playback::STOP, 0); 393 mDecryptHandle = NULL; 394 mDrmManagerClient = NULL; 395 } 396 397 if (mFlags & PREPARING) { 398 mFlags |= PREPARE_CANCELLED; 399 if (mConnectingDataSource != NULL) { 400 LOGI("interrupting the connection process"); 401 mConnectingDataSource->disconnect(); 402 } 403 404 if (mFlags & PREPARING_CONNECTED) { 405 // We are basically done preparing, we're just buffering 406 // enough data to start playback, we can safely interrupt that. 407 finishAsyncPrepare_l(); 408 } 409 } 410 411 if (mFlags & PREPARING) { 412 LOGI("waiting until preparation is completes."); 413 } 414 415 while (mFlags & PREPARING) { 416 mPreparedCondition.wait(mLock); 417 } 418 419 cancelPlayerEvents(); 420 421 mCachedSource.clear(); 422 mAudioTrack.clear(); 423 mVideoTrack.clear(); 424 425 // Shutdown audio first, so that the respone to the reset request 426 // appears to happen instantaneously as far as the user is concerned 427 // If we did this later, audio would continue playing while we 428 // shutdown the video-related resources and the player appear to 429 // not be as responsive to a reset request. 430 if (mAudioPlayer == NULL && mAudioSource != NULL) { 431 // If we had an audio player, it would have effectively 432 // taken possession of the audio source and stopped it when 433 // _it_ is stopped. Otherwise this is still our responsibility. 434 mAudioSource->stop(); 435 } 436 mAudioSource.clear(); 437 438 LOGI("audio source cleared"); 439 440 mTimeSource = NULL; 441 442 delete mAudioPlayer; 443 mAudioPlayer = NULL; 444 445 mVideoRenderer.clear(); 446 447 if (mVideoBuffer) { 448 mVideoBuffer->release(); 449 mVideoBuffer = NULL; 450 } 451 452 if (mRTSPController != NULL) { 453 mRTSPController->disconnect(); 454 mRTSPController.clear(); 455 } 456 457 if (mLiveSession != NULL) { 458 mLiveSession->disconnect(); 459 mLiveSession.clear(); 460 } 461 462 if (mVideoSource != NULL) { 463 mVideoSource->stop(); 464 465 // The following hack is necessary to ensure that the OMX 466 // component is completely released by the time we may try 467 // to instantiate it again. 468 wp<MediaSource> tmp = mVideoSource; 469 mVideoSource.clear(); 470 while (tmp.promote() != NULL) { 471 usleep(1000); 472 } 473 IPCThreadState::self()->flushCommands(); 474 } 475 476 LOGI("video source cleared"); 477 478 mDurationUs = -1; 479 mFlags = 0; 480 mExtractorFlags = 0; 481 mTimeSourceDeltaUs = 0; 482 mVideoTimeUs = 0; 483 484 mSeeking = false; 485 mSeekNotificationSent = false; 486 mSeekTimeUs = 0; 487 488 mUri.setTo(""); 489 mUriHeaders.clear(); 490 491 mFileSource.clear(); 492 493 mBitrate = -1; 494 495 LOGI("reset_l completed"); 496} 497 498void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) { 499 if (mListener != NULL) { 500 sp<MediaPlayerBase> listener = mListener.promote(); 501 502 if (listener != NULL) { 503 listener->sendEvent(msg, ext1, ext2); 504 } 505 } 506} 507 508bool AwesomePlayer::getBitrate(int64_t *bitrate) { 509 off64_t size; 510 if (mDurationUs >= 0 && mCachedSource != NULL 511 && mCachedSource->getSize(&size) == OK) { 512 *bitrate = size * 8000000ll / mDurationUs; // in bits/sec 513 return true; 514 } 515 516 if (mBitrate >= 0) { 517 *bitrate = mBitrate; 518 return true; 519 } 520 521 *bitrate = 0; 522 523 return false; 524} 525 526// Returns true iff cached duration is available/applicable. 527bool AwesomePlayer::getCachedDuration_l(int64_t *durationUs, bool *eos) { 528 int64_t bitrate; 529 530 if (mRTSPController != NULL) { 531 *durationUs = mRTSPController->getQueueDurationUs(eos); 532 return true; 533 } else if (mCachedSource != NULL && getBitrate(&bitrate)) { 534 status_t finalStatus; 535 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); 536 *durationUs = cachedDataRemaining * 8000000ll / bitrate; 537 *eos = (finalStatus != OK); 538 return true; 539 } 540 541 return false; 542} 543 544void AwesomePlayer::ensureCacheIsFetching_l() { 545 if (mCachedSource != NULL) { 546 mCachedSource->resumeFetchingIfNecessary(); 547 } 548} 549 550void AwesomePlayer::onVideoLagUpdate() { 551 Mutex::Autolock autoLock(mLock); 552 if (!mVideoLagEventPending) { 553 return; 554 } 555 mVideoLagEventPending = false; 556 557 int64_t audioTimeUs = mAudioPlayer->getMediaTimeUs(); 558 int64_t videoLateByUs = audioTimeUs - mVideoTimeUs; 559 560 if (videoLateByUs > 300000ll) { 561 LOGV("video late by %lld ms.", videoLateByUs / 1000ll); 562 563 notifyListener_l( 564 MEDIA_INFO, 565 MEDIA_INFO_VIDEO_TRACK_LAGGING, 566 videoLateByUs / 1000ll); 567 } 568 569 postVideoLagEvent_l(); 570} 571 572void AwesomePlayer::onBufferingUpdate() { 573 Mutex::Autolock autoLock(mLock); 574 if (!mBufferingEventPending) { 575 return; 576 } 577 mBufferingEventPending = false; 578 579 if (mCachedSource != NULL) { 580 status_t finalStatus; 581 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); 582 bool eos = (finalStatus != OK); 583 584 if (eos) { 585 if (finalStatus == ERROR_END_OF_STREAM) { 586 notifyListener_l(MEDIA_BUFFERING_UPDATE, 100); 587 } 588 if (mFlags & PREPARING) { 589 LOGV("cache has reached EOS, prepare is done."); 590 finishAsyncPrepare_l(); 591 } 592 } else { 593 int64_t bitrate; 594 if (getBitrate(&bitrate)) { 595 size_t cachedSize = mCachedSource->cachedSize(); 596 int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate; 597 598 int percentage = 100.0 * (double)cachedDurationUs / mDurationUs; 599 if (percentage > 100) { 600 percentage = 100; 601 } 602 603 notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage); 604 } else { 605 // We don't know the bitrate of the stream, use absolute size 606 // limits to maintain the cache. 607 608 if ((mFlags & PLAYING) && !eos 609 && (cachedDataRemaining < kLowWaterMarkBytes)) { 610 LOGI("cache is running low (< %d) , pausing.", 611 kLowWaterMarkBytes); 612 mFlags |= CACHE_UNDERRUN; 613 pause_l(); 614 ensureCacheIsFetching_l(); 615 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); 616 } else if (eos || cachedDataRemaining > kHighWaterMarkBytes) { 617 if (mFlags & CACHE_UNDERRUN) { 618 LOGI("cache has filled up (> %d), resuming.", 619 kHighWaterMarkBytes); 620 mFlags &= ~CACHE_UNDERRUN; 621 play_l(); 622 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); 623 } else if (mFlags & PREPARING) { 624 LOGV("cache has filled up (> %d), prepare is done", 625 kHighWaterMarkBytes); 626 finishAsyncPrepare_l(); 627 } 628 } 629 } 630 } 631 } 632 633 int64_t cachedDurationUs; 634 bool eos; 635 if (getCachedDuration_l(&cachedDurationUs, &eos)) { 636 LOGV("cachedDurationUs = %.2f secs, eos=%d", 637 cachedDurationUs / 1E6, eos); 638 639 int64_t highWaterMarkUs = 640 (mRTSPController != NULL) ? kHighWaterMarkRTSPUs : kHighWaterMarkUs; 641 642 if ((mFlags & PLAYING) && !eos 643 && (cachedDurationUs < kLowWaterMarkUs)) { 644 LOGI("cache is running low (%.2f secs) , pausing.", 645 cachedDurationUs / 1E6); 646 mFlags |= CACHE_UNDERRUN; 647 pause_l(); 648 ensureCacheIsFetching_l(); 649 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); 650 } else if (eos || cachedDurationUs > highWaterMarkUs) { 651 if (mFlags & CACHE_UNDERRUN) { 652 LOGI("cache has filled up (%.2f secs), resuming.", 653 cachedDurationUs / 1E6); 654 mFlags &= ~CACHE_UNDERRUN; 655 play_l(); 656 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); 657 } else if (mFlags & PREPARING) { 658 LOGV("cache has filled up (%.2f secs), prepare is done", 659 cachedDurationUs / 1E6); 660 finishAsyncPrepare_l(); 661 } 662 } 663 } 664 665 postBufferingEvent_l(); 666} 667 668void AwesomePlayer::partial_reset_l() { 669 // Only reset the video renderer and shut down the video decoder. 670 // Then instantiate a new video decoder and resume video playback. 671 672 mVideoRenderer.clear(); 673 674 if (mVideoBuffer) { 675 mVideoBuffer->release(); 676 mVideoBuffer = NULL; 677 } 678 679 { 680 mVideoSource->stop(); 681 682 // The following hack is necessary to ensure that the OMX 683 // component is completely released by the time we may try 684 // to instantiate it again. 685 wp<MediaSource> tmp = mVideoSource; 686 mVideoSource.clear(); 687 while (tmp.promote() != NULL) { 688 usleep(1000); 689 } 690 IPCThreadState::self()->flushCommands(); 691 } 692 693 CHECK_EQ((status_t)OK, 694 initVideoDecoder(OMXCodec::kIgnoreCodecSpecificData)); 695} 696 697void AwesomePlayer::onStreamDone() { 698 // Posted whenever any stream finishes playing. 699 700 Mutex::Autolock autoLock(mLock); 701 if (!mStreamDoneEventPending) { 702 return; 703 } 704 mStreamDoneEventPending = false; 705 706 if (mStreamDoneStatus == INFO_DISCONTINUITY) { 707 // This special status is returned because an http live stream's 708 // video stream switched to a different bandwidth at this point 709 // and future data may have been encoded using different parameters. 710 // This requires us to shutdown the video decoder and reinstantiate 711 // a fresh one. 712 713 LOGV("INFO_DISCONTINUITY"); 714 715 CHECK(mVideoSource != NULL); 716 717 partial_reset_l(); 718 postVideoEvent_l(); 719 return; 720 } else if (mStreamDoneStatus != ERROR_END_OF_STREAM) { 721 LOGV("MEDIA_ERROR %d", mStreamDoneStatus); 722 723 notifyListener_l( 724 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus); 725 726 pause_l(true /* at eos */); 727 728 mFlags |= AT_EOS; 729 return; 730 } 731 732 const bool allDone = 733 (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS)) 734 && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS)); 735 736 if (!allDone) { 737 return; 738 } 739 740 if (mFlags & (LOOPING | AUTO_LOOPING)) { 741 seekTo_l(0); 742 743 if (mVideoSource != NULL) { 744 postVideoEvent_l(); 745 } 746 } else { 747 LOGV("MEDIA_PLAYBACK_COMPLETE"); 748 notifyListener_l(MEDIA_PLAYBACK_COMPLETE); 749 750 pause_l(true /* at eos */); 751 752 mFlags |= AT_EOS; 753 } 754} 755 756status_t AwesomePlayer::play() { 757 Mutex::Autolock autoLock(mLock); 758 759 mFlags &= ~CACHE_UNDERRUN; 760 761 return play_l(); 762} 763 764status_t AwesomePlayer::play_l() { 765 mFlags &= ~SEEK_PREVIEW; 766 767 if (mFlags & PLAYING) { 768 return OK; 769 } 770 771 if (!(mFlags & PREPARED)) { 772 status_t err = prepare_l(); 773 774 if (err != OK) { 775 return err; 776 } 777 } 778 779 mFlags |= PLAYING; 780 mFlags |= FIRST_FRAME; 781 782 bool deferredAudioSeek = false; 783 784 if (mDecryptHandle != NULL) { 785 int64_t position; 786 getPosition(&position); 787 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 788 Playback::START, position / 1000); 789 } 790 791 if (mAudioSource != NULL) { 792 if (mAudioPlayer == NULL) { 793 if (mAudioSink != NULL) { 794 mAudioPlayer = new AudioPlayer(mAudioSink, this); 795 mAudioPlayer->setSource(mAudioSource); 796 797 mTimeSource = mAudioPlayer; 798 799 deferredAudioSeek = true; 800 801 mWatchForAudioSeekComplete = false; 802 mWatchForAudioEOS = true; 803 } 804 } 805 806 CHECK(!(mFlags & AUDIO_RUNNING)); 807 808 if (mVideoSource == NULL) { 809 status_t err = startAudioPlayer_l(); 810 811 if (err != OK) { 812 delete mAudioPlayer; 813 mAudioPlayer = NULL; 814 815 mFlags &= ~(PLAYING | FIRST_FRAME); 816 817 if (mDecryptHandle != NULL) { 818 mDrmManagerClient->setPlaybackStatus( 819 mDecryptHandle, Playback::STOP, 0); 820 } 821 822 return err; 823 } 824 } 825 } 826 827 if (mTimeSource == NULL && mAudioPlayer == NULL) { 828 mTimeSource = &mSystemTimeSource; 829 } 830 831 if (mVideoSource != NULL) { 832 // Kick off video playback 833 postVideoEvent_l(); 834 835 if (mAudioSource != NULL && mVideoSource != NULL) { 836 postVideoLagEvent_l(); 837 } 838 } 839 840 if (deferredAudioSeek) { 841 // If there was a seek request while we were paused 842 // and we're just starting up again, honor the request now. 843 seekAudioIfNecessary_l(); 844 } 845 846 if (mFlags & AT_EOS) { 847 // Legacy behaviour, if a stream finishes playing and then 848 // is started again, we play from the start... 849 seekTo_l(0); 850 } 851 852 return OK; 853} 854 855status_t AwesomePlayer::startAudioPlayer_l() { 856 CHECK(!(mFlags & AUDIO_RUNNING)); 857 858 if (mAudioSource == NULL || mAudioPlayer == NULL) { 859 return OK; 860 } 861 862 if (!(mFlags & AUDIOPLAYER_STARTED)) { 863 mFlags |= AUDIOPLAYER_STARTED; 864 865 // We've already started the MediaSource in order to enable 866 // the prefetcher to read its data. 867 status_t err = mAudioPlayer->start( 868 true /* sourceAlreadyStarted */); 869 870 if (err != OK) { 871 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 872 return err; 873 } 874 } else { 875 mAudioPlayer->resume(); 876 } 877 878 mFlags |= AUDIO_RUNNING; 879 880 mWatchForAudioEOS = true; 881 882 return OK; 883} 884 885void AwesomePlayer::notifyVideoSize_l() { 886 sp<MetaData> meta = mVideoSource->getFormat(); 887 888 int32_t cropLeft, cropTop, cropRight, cropBottom; 889 if (!meta->findRect( 890 kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom)) { 891 int32_t width, height; 892 CHECK(meta->findInt32(kKeyWidth, &width)); 893 CHECK(meta->findInt32(kKeyHeight, &height)); 894 895 cropLeft = cropTop = 0; 896 cropRight = width - 1; 897 cropBottom = height - 1; 898 899 LOGV("got dimensions only %d x %d", width, height); 900 } else { 901 LOGV("got crop rect %d, %d, %d, %d", 902 cropLeft, cropTop, cropRight, cropBottom); 903 } 904 905 int32_t usableWidth = cropRight - cropLeft + 1; 906 int32_t usableHeight = cropBottom - cropTop + 1; 907 if (mDisplayWidth != 0) { 908 usableWidth = mDisplayWidth; 909 } 910 if (mDisplayHeight != 0) { 911 usableHeight = mDisplayHeight; 912 } 913 914 int32_t rotationDegrees; 915 if (!mVideoTrack->getFormat()->findInt32( 916 kKeyRotation, &rotationDegrees)) { 917 rotationDegrees = 0; 918 } 919 920 if (rotationDegrees == 90 || rotationDegrees == 270) { 921 notifyListener_l( 922 MEDIA_SET_VIDEO_SIZE, usableHeight, usableWidth); 923 } else { 924 notifyListener_l( 925 MEDIA_SET_VIDEO_SIZE, usableWidth, usableHeight); 926 } 927} 928 929void AwesomePlayer::initRenderer_l() { 930 if (mSurface == NULL) { 931 return; 932 } 933 934 sp<MetaData> meta = mVideoSource->getFormat(); 935 936 int32_t format; 937 const char *component; 938 int32_t decodedWidth, decodedHeight; 939 CHECK(meta->findInt32(kKeyColorFormat, &format)); 940 CHECK(meta->findCString(kKeyDecoderComponent, &component)); 941 CHECK(meta->findInt32(kKeyWidth, &decodedWidth)); 942 CHECK(meta->findInt32(kKeyHeight, &decodedHeight)); 943 944 int32_t rotationDegrees; 945 if (!mVideoTrack->getFormat()->findInt32( 946 kKeyRotation, &rotationDegrees)) { 947 rotationDegrees = 0; 948 } 949 950 mVideoRenderer.clear(); 951 952 // Must ensure that mVideoRenderer's destructor is actually executed 953 // before creating a new one. 954 IPCThreadState::self()->flushCommands(); 955 956 if (USE_SURFACE_ALLOC && strncmp(component, "OMX.", 4) == 0) { 957 // Hardware decoders avoid the CPU color conversion by decoding 958 // directly to ANativeBuffers, so we must use a renderer that 959 // just pushes those buffers to the ANativeWindow. 960 mVideoRenderer = 961 new AwesomeNativeWindowRenderer(mSurface, rotationDegrees); 962 } else { 963 // Other decoders are instantiated locally and as a consequence 964 // allocate their buffers in local address space. This renderer 965 // then performs a color conversion and copy to get the data 966 // into the ANativeBuffer. 967 mVideoRenderer = new AwesomeLocalRenderer(mSurface, meta); 968 } 969} 970 971status_t AwesomePlayer::pause() { 972 Mutex::Autolock autoLock(mLock); 973 974 mFlags &= ~CACHE_UNDERRUN; 975 976 return pause_l(); 977} 978 979status_t AwesomePlayer::pause_l(bool at_eos) { 980 if (!(mFlags & PLAYING)) { 981 return OK; 982 } 983 984 cancelPlayerEvents(true /* keepBufferingGoing */); 985 986 if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { 987 if (at_eos) { 988 // If we played the audio stream to completion we 989 // want to make sure that all samples remaining in the audio 990 // track's queue are played out. 991 mAudioPlayer->pause(true /* playPendingSamples */); 992 } else { 993 mAudioPlayer->pause(); 994 } 995 996 mFlags &= ~AUDIO_RUNNING; 997 } 998 999 mFlags &= ~PLAYING; 1000 1001 if (mDecryptHandle != NULL) { 1002 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1003 Playback::PAUSE, 0); 1004 } 1005 1006 return OK; 1007} 1008 1009bool AwesomePlayer::isPlaying() const { 1010 return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN); 1011} 1012 1013void AwesomePlayer::setSurface(const sp<Surface> &surface) { 1014 Mutex::Autolock autoLock(mLock); 1015 1016 mSurface = surface; 1017} 1018 1019void AwesomePlayer::setAudioSink( 1020 const sp<MediaPlayerBase::AudioSink> &audioSink) { 1021 Mutex::Autolock autoLock(mLock); 1022 1023 mAudioSink = audioSink; 1024} 1025 1026status_t AwesomePlayer::setLooping(bool shouldLoop) { 1027 Mutex::Autolock autoLock(mLock); 1028 1029 mFlags = mFlags & ~LOOPING; 1030 1031 if (shouldLoop) { 1032 mFlags |= LOOPING; 1033 } 1034 1035 return OK; 1036} 1037 1038status_t AwesomePlayer::getDuration(int64_t *durationUs) { 1039 Mutex::Autolock autoLock(mMiscStateLock); 1040 1041 if (mDurationUs < 0) { 1042 return UNKNOWN_ERROR; 1043 } 1044 1045 *durationUs = mDurationUs; 1046 1047 return OK; 1048} 1049 1050status_t AwesomePlayer::getPosition(int64_t *positionUs) { 1051 if (mRTSPController != NULL) { 1052 *positionUs = mRTSPController->getNormalPlayTimeUs(); 1053 } 1054 else if (mSeeking) { 1055 *positionUs = mSeekTimeUs; 1056 } else if (mVideoSource != NULL) { 1057 Mutex::Autolock autoLock(mMiscStateLock); 1058 *positionUs = mVideoTimeUs; 1059 } else if (mAudioPlayer != NULL) { 1060 *positionUs = mAudioPlayer->getMediaTimeUs(); 1061 } else { 1062 *positionUs = 0; 1063 } 1064 1065 return OK; 1066} 1067 1068status_t AwesomePlayer::seekTo(int64_t timeUs) { 1069 if (mExtractorFlags & MediaExtractor::CAN_SEEK) { 1070 Mutex::Autolock autoLock(mLock); 1071 return seekTo_l(timeUs); 1072 } 1073 1074 return OK; 1075} 1076 1077// static 1078void AwesomePlayer::OnRTSPSeekDoneWrapper(void *cookie) { 1079 static_cast<AwesomePlayer *>(cookie)->onRTSPSeekDone(); 1080} 1081 1082void AwesomePlayer::onRTSPSeekDone() { 1083 notifyListener_l(MEDIA_SEEK_COMPLETE); 1084 mSeekNotificationSent = true; 1085} 1086 1087status_t AwesomePlayer::seekTo_l(int64_t timeUs) { 1088 if (mRTSPController != NULL) { 1089 mRTSPController->seekAsync(timeUs, OnRTSPSeekDoneWrapper, this); 1090 return OK; 1091 } 1092 1093 if (mFlags & CACHE_UNDERRUN) { 1094 mFlags &= ~CACHE_UNDERRUN; 1095 play_l(); 1096 } 1097 1098 mSeeking = true; 1099 mSeekNotificationSent = false; 1100 mSeekTimeUs = timeUs; 1101 mFlags &= ~(AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS); 1102 1103 seekAudioIfNecessary_l(); 1104 1105 if (!(mFlags & PLAYING)) { 1106 LOGV("seeking while paused, sending SEEK_COMPLETE notification" 1107 " immediately."); 1108 1109 notifyListener_l(MEDIA_SEEK_COMPLETE); 1110 mSeekNotificationSent = true; 1111 1112 if ((mFlags & PREPARED) && mVideoSource != NULL) { 1113 mFlags |= SEEK_PREVIEW; 1114 postVideoEvent_l(); 1115 } 1116 } 1117 1118 return OK; 1119} 1120 1121void AwesomePlayer::seekAudioIfNecessary_l() { 1122 if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) { 1123 mAudioPlayer->seekTo(mSeekTimeUs); 1124 1125 mWatchForAudioSeekComplete = true; 1126 mWatchForAudioEOS = true; 1127 mSeekNotificationSent = false; 1128 1129 if (mDecryptHandle != NULL) { 1130 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1131 Playback::PAUSE, 0); 1132 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1133 Playback::START, mSeekTimeUs / 1000); 1134 } 1135 } 1136} 1137 1138void AwesomePlayer::setAudioSource(sp<MediaSource> source) { 1139 CHECK(source != NULL); 1140 1141 mAudioTrack = source; 1142} 1143 1144status_t AwesomePlayer::initAudioDecoder() { 1145 sp<MetaData> meta = mAudioTrack->getFormat(); 1146 1147 const char *mime; 1148 CHECK(meta->findCString(kKeyMIMEType, &mime)); 1149 1150 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { 1151 mAudioSource = mAudioTrack; 1152 } else { 1153 mAudioSource = OMXCodec::Create( 1154 mClient.interface(), mAudioTrack->getFormat(), 1155 false, // createEncoder 1156 mAudioTrack); 1157 } 1158 1159 if (mAudioSource != NULL) { 1160 int64_t durationUs; 1161 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 1162 Mutex::Autolock autoLock(mMiscStateLock); 1163 if (mDurationUs < 0 || durationUs > mDurationUs) { 1164 mDurationUs = durationUs; 1165 } 1166 } 1167 1168 status_t err = mAudioSource->start(); 1169 1170 if (err != OK) { 1171 mAudioSource.clear(); 1172 return err; 1173 } 1174 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) { 1175 // For legacy reasons we're simply going to ignore the absence 1176 // of an audio decoder for QCELP instead of aborting playback 1177 // altogether. 1178 return OK; 1179 } 1180 1181 return mAudioSource != NULL ? OK : UNKNOWN_ERROR; 1182} 1183 1184void AwesomePlayer::setVideoSource(sp<MediaSource> source) { 1185 CHECK(source != NULL); 1186 1187 mVideoTrack = source; 1188} 1189 1190status_t AwesomePlayer::initVideoDecoder(uint32_t flags) { 1191 mVideoSource = OMXCodec::Create( 1192 mClient.interface(), mVideoTrack->getFormat(), 1193 false, // createEncoder 1194 mVideoTrack, 1195 NULL, flags, USE_SURFACE_ALLOC ? mSurface : NULL); 1196 1197 if (mVideoSource != NULL) { 1198 int64_t durationUs; 1199 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 1200 Mutex::Autolock autoLock(mMiscStateLock); 1201 if (mDurationUs < 0 || durationUs > mDurationUs) { 1202 mDurationUs = durationUs; 1203 } 1204 } 1205 1206 status_t err = mVideoSource->start(); 1207 1208 if (err != OK) { 1209 mVideoSource.clear(); 1210 return err; 1211 } 1212 } 1213 1214 return mVideoSource != NULL ? OK : UNKNOWN_ERROR; 1215} 1216 1217void AwesomePlayer::finishSeekIfNecessary(int64_t videoTimeUs) { 1218 if (!mSeeking || (mFlags & SEEK_PREVIEW)) { 1219 return; 1220 } 1221 1222 if (mAudioPlayer != NULL) { 1223 LOGV("seeking audio to %lld us (%.2f secs).", videoTimeUs, videoTimeUs / 1E6); 1224 1225 // If we don't have a video time, seek audio to the originally 1226 // requested seek time instead. 1227 1228 mAudioPlayer->seekTo(videoTimeUs < 0 ? mSeekTimeUs : videoTimeUs); 1229 mWatchForAudioSeekComplete = true; 1230 } else if (!mSeekNotificationSent) { 1231 // If we're playing video only, report seek complete now, 1232 // otherwise audio player will notify us later. 1233 notifyListener_l(MEDIA_SEEK_COMPLETE); 1234 } 1235 1236 mFlags |= FIRST_FRAME; 1237 mSeeking = false; 1238 mSeekNotificationSent = false; 1239 1240 if (mDecryptHandle != NULL) { 1241 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1242 Playback::PAUSE, 0); 1243 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1244 Playback::START, videoTimeUs / 1000); 1245 } 1246} 1247 1248void AwesomePlayer::onVideoEvent() { 1249 Mutex::Autolock autoLock(mLock); 1250 if (!mVideoEventPending) { 1251 // The event has been cancelled in reset_l() but had already 1252 // been scheduled for execution at that time. 1253 return; 1254 } 1255 mVideoEventPending = false; 1256 1257 if (mSeeking) { 1258 if (mVideoBuffer) { 1259 mVideoBuffer->release(); 1260 mVideoBuffer = NULL; 1261 } 1262 1263 if (mCachedSource != NULL && mAudioSource != NULL 1264 && !(mFlags & SEEK_PREVIEW)) { 1265 // We're going to seek the video source first, followed by 1266 // the audio source. 1267 // In order to avoid jumps in the DataSource offset caused by 1268 // the audio codec prefetching data from the old locations 1269 // while the video codec is already reading data from the new 1270 // locations, we'll "pause" the audio source, causing it to 1271 // stop reading input data until a subsequent seek. 1272 1273 if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { 1274 mAudioPlayer->pause(); 1275 1276 mFlags &= ~AUDIO_RUNNING; 1277 } 1278 mAudioSource->pause(); 1279 } 1280 } 1281 1282 if (!mVideoBuffer) { 1283 MediaSource::ReadOptions options; 1284 if (mSeeking) { 1285 LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6); 1286 1287 options.setSeekTo( 1288 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST_SYNC); 1289 } 1290 for (;;) { 1291 status_t err = mVideoSource->read(&mVideoBuffer, &options); 1292 options.clearSeekTo(); 1293 1294 if (err != OK) { 1295 CHECK(mVideoBuffer == NULL); 1296 1297 if (err == INFO_FORMAT_CHANGED) { 1298 LOGV("VideoSource signalled format change."); 1299 1300 notifyVideoSize_l(); 1301 1302 if (mVideoRenderer != NULL) { 1303 mVideoRendererIsPreview = false; 1304 initRenderer_l(); 1305 } 1306 continue; 1307 } 1308 1309 // So video playback is complete, but we may still have 1310 // a seek request pending that needs to be applied 1311 // to the audio track. 1312 if (mSeeking) { 1313 LOGV("video stream ended while seeking!"); 1314 } 1315 finishSeekIfNecessary(-1); 1316 1317 mFlags |= VIDEO_AT_EOS; 1318 postStreamDoneEvent_l(err); 1319 return; 1320 } 1321 1322 if (mVideoBuffer->range_length() == 0) { 1323 // Some decoders, notably the PV AVC software decoder 1324 // return spurious empty buffers that we just want to ignore. 1325 1326 mVideoBuffer->release(); 1327 mVideoBuffer = NULL; 1328 continue; 1329 } 1330 1331 break; 1332 } 1333 } 1334 1335 int64_t timeUs; 1336 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 1337 1338 { 1339 Mutex::Autolock autoLock(mMiscStateLock); 1340 mVideoTimeUs = timeUs; 1341 } 1342 1343 bool wasSeeking = mSeeking; 1344 finishSeekIfNecessary(timeUs); 1345 1346 if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) { 1347 status_t err = startAudioPlayer_l(); 1348 if (err != OK) { 1349 LOGE("Startung the audio player failed w/ err %d", err); 1350 return; 1351 } 1352 } 1353 1354 TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource; 1355 1356 if (mFlags & FIRST_FRAME) { 1357 mFlags &= ~FIRST_FRAME; 1358 mSinceLastDropped = 0; 1359 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs; 1360 } 1361 1362 int64_t realTimeUs, mediaTimeUs; 1363 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL 1364 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) { 1365 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs; 1366 } 1367 1368 if (!wasSeeking) { 1369 // Let's display the first frame after seeking right away. 1370 1371 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; 1372 1373 int64_t latenessUs = nowUs - timeUs; 1374 1375 if (latenessUs > 40000) { 1376 // We're more than 40ms late. 1377 LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6); 1378 if ( mSinceLastDropped > FRAME_DROP_FREQ) 1379 { 1380 LOGV("we're late by %lld us (%.2f secs) dropping one after %d frames", latenessUs, latenessUs / 1E6, mSinceLastDropped); 1381 mSinceLastDropped = 0; 1382 mVideoBuffer->release(); 1383 mVideoBuffer = NULL; 1384 1385 postVideoEvent_l(); 1386 return; 1387 } 1388 } 1389 1390 if (latenessUs < -10000) { 1391 // We're more than 10ms early. 1392 1393 postVideoEvent_l(10000); 1394 return; 1395 } 1396 } 1397 1398 if (mVideoRendererIsPreview || mVideoRenderer == NULL) { 1399 mVideoRendererIsPreview = false; 1400 1401 initRenderer_l(); 1402 } 1403 1404 if (mVideoRenderer != NULL) { 1405 mSinceLastDropped++; 1406 mVideoRenderer->render(mVideoBuffer); 1407 } 1408 1409 mVideoBuffer->release(); 1410 mVideoBuffer = NULL; 1411 1412 if (wasSeeking && (mFlags & SEEK_PREVIEW)) { 1413 mFlags &= ~SEEK_PREVIEW; 1414 return; 1415 } 1416 1417 postVideoEvent_l(); 1418} 1419 1420void AwesomePlayer::postVideoEvent_l(int64_t delayUs) { 1421 if (mVideoEventPending) { 1422 return; 1423 } 1424 1425 mVideoEventPending = true; 1426 mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs); 1427} 1428 1429void AwesomePlayer::postStreamDoneEvent_l(status_t status) { 1430 if (mStreamDoneEventPending) { 1431 return; 1432 } 1433 mStreamDoneEventPending = true; 1434 1435 mStreamDoneStatus = status; 1436 mQueue.postEvent(mStreamDoneEvent); 1437} 1438 1439void AwesomePlayer::postBufferingEvent_l() { 1440 if (mBufferingEventPending) { 1441 return; 1442 } 1443 mBufferingEventPending = true; 1444 mQueue.postEventWithDelay(mBufferingEvent, 1000000ll); 1445} 1446 1447void AwesomePlayer::postVideoLagEvent_l() { 1448 if (mVideoLagEventPending) { 1449 return; 1450 } 1451 mVideoLagEventPending = true; 1452 mQueue.postEventWithDelay(mVideoLagEvent, 1000000ll); 1453} 1454 1455void AwesomePlayer::postCheckAudioStatusEvent_l() { 1456 if (mAudioStatusEventPending) { 1457 return; 1458 } 1459 mAudioStatusEventPending = true; 1460 mQueue.postEvent(mCheckAudioStatusEvent); 1461} 1462 1463void AwesomePlayer::onCheckAudioStatus() { 1464 Mutex::Autolock autoLock(mLock); 1465 if (!mAudioStatusEventPending) { 1466 // Event was dispatched and while we were blocking on the mutex, 1467 // has already been cancelled. 1468 return; 1469 } 1470 1471 mAudioStatusEventPending = false; 1472 1473 if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) { 1474 mWatchForAudioSeekComplete = false; 1475 1476 if (!mSeekNotificationSent) { 1477 notifyListener_l(MEDIA_SEEK_COMPLETE); 1478 mSeekNotificationSent = true; 1479 } 1480 1481 mSeeking = false; 1482 } 1483 1484 status_t finalStatus; 1485 if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) { 1486 mWatchForAudioEOS = false; 1487 mFlags |= AUDIO_AT_EOS; 1488 mFlags |= FIRST_FRAME; 1489 postStreamDoneEvent_l(finalStatus); 1490 } 1491} 1492 1493status_t AwesomePlayer::prepare() { 1494 Mutex::Autolock autoLock(mLock); 1495 return prepare_l(); 1496} 1497 1498status_t AwesomePlayer::prepare_l() { 1499 if (mFlags & PREPARED) { 1500 return OK; 1501 } 1502 1503 if (mFlags & PREPARING) { 1504 return UNKNOWN_ERROR; 1505 } 1506 1507 mIsAsyncPrepare = false; 1508 status_t err = prepareAsync_l(); 1509 1510 if (err != OK) { 1511 return err; 1512 } 1513 1514 while (mFlags & PREPARING) { 1515 mPreparedCondition.wait(mLock); 1516 } 1517 1518 return mPrepareResult; 1519} 1520 1521status_t AwesomePlayer::prepareAsync() { 1522 Mutex::Autolock autoLock(mLock); 1523 1524 if (mFlags & PREPARING) { 1525 return UNKNOWN_ERROR; // async prepare already pending 1526 } 1527 1528 mIsAsyncPrepare = true; 1529 return prepareAsync_l(); 1530} 1531 1532status_t AwesomePlayer::prepareAsync_l() { 1533 if (mFlags & PREPARING) { 1534 return UNKNOWN_ERROR; // async prepare already pending 1535 } 1536 1537 if (!mQueueStarted) { 1538 mQueue.start(); 1539 mQueueStarted = true; 1540 } 1541 1542 mFlags |= PREPARING; 1543 mAsyncPrepareEvent = new AwesomeEvent( 1544 this, &AwesomePlayer::onPrepareAsyncEvent); 1545 1546 mQueue.postEvent(mAsyncPrepareEvent); 1547 1548 return OK; 1549} 1550 1551status_t AwesomePlayer::finishSetDataSource_l() { 1552 sp<DataSource> dataSource; 1553 1554 if (!strncasecmp("http://", mUri.string(), 7)) { 1555 mConnectingDataSource = new NuHTTPDataSource; 1556 1557 mLock.unlock(); 1558 status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders); 1559 mLock.lock(); 1560 1561 if (err != OK) { 1562 mConnectingDataSource.clear(); 1563 1564 LOGI("mConnectingDataSource->connect() returned %d", err); 1565 return err; 1566 } 1567 1568#if 0 1569 mCachedSource = new NuCachedSource2( 1570 new ThrottledSource( 1571 mConnectingDataSource, 50 * 1024 /* bytes/sec */)); 1572#else 1573 mCachedSource = new NuCachedSource2(mConnectingDataSource); 1574#endif 1575 mConnectingDataSource.clear(); 1576 1577 dataSource = mCachedSource; 1578 1579 // We're going to prefill the cache before trying to instantiate 1580 // the extractor below, as the latter is an operation that otherwise 1581 // could block on the datasource for a significant amount of time. 1582 // During that time we'd be unable to abort the preparation phase 1583 // without this prefill. 1584 1585 mLock.unlock(); 1586 1587 for (;;) { 1588 status_t finalStatus; 1589 size_t cachedDataRemaining = 1590 mCachedSource->approxDataRemaining(&finalStatus); 1591 1592 if (finalStatus != OK || cachedDataRemaining >= kHighWaterMarkBytes 1593 || (mFlags & PREPARE_CANCELLED)) { 1594 break; 1595 } 1596 1597 usleep(200000); 1598 } 1599 1600 mLock.lock(); 1601 1602 if (mFlags & PREPARE_CANCELLED) { 1603 LOGI("Prepare cancelled while waiting for initial cache fill."); 1604 return UNKNOWN_ERROR; 1605 } 1606 } else if (!strncasecmp(mUri.string(), "httplive://", 11)) { 1607 String8 uri("http://"); 1608 uri.append(mUri.string() + 11); 1609 1610 if (mLooper == NULL) { 1611 mLooper = new ALooper; 1612 mLooper->setName("httplive"); 1613 mLooper->start(); 1614 } 1615 1616 mLiveSession = new LiveSession; 1617 mLooper->registerHandler(mLiveSession); 1618 1619 mLiveSession->connect(uri.string()); 1620 dataSource = mLiveSession->getDataSource(); 1621 1622 sp<MediaExtractor> extractor = 1623 MediaExtractor::Create(dataSource, MEDIA_MIMETYPE_CONTAINER_MPEG2TS); 1624 1625 static_cast<MPEG2TSExtractor *>(extractor.get()) 1626 ->setLiveSession(mLiveSession); 1627 1628 return setDataSource_l(extractor); 1629 } else if (!strncasecmp("rtsp://", mUri.string(), 7)) { 1630 if (mLooper == NULL) { 1631 mLooper = new ALooper; 1632 mLooper->setName("rtsp"); 1633 mLooper->start(); 1634 } 1635 mRTSPController = new ARTSPController(mLooper); 1636 status_t err = mRTSPController->connect(mUri.string()); 1637 1638 LOGI("ARTSPController::connect returned %d", err); 1639 1640 if (err != OK) { 1641 mRTSPController.clear(); 1642 return err; 1643 } 1644 1645 sp<MediaExtractor> extractor = mRTSPController.get(); 1646 return setDataSource_l(extractor); 1647 } else { 1648 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders); 1649 } 1650 1651 if (dataSource == NULL) { 1652 return UNKNOWN_ERROR; 1653 } 1654 1655 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 1656 1657 if (extractor == NULL) { 1658 return UNKNOWN_ERROR; 1659 } 1660 1661 dataSource->getDrmInfo(&mDecryptHandle, &mDrmManagerClient); 1662 if (mDecryptHandle != NULL) { 1663 CHECK(mDrmManagerClient); 1664 if (RightsStatus::RIGHTS_VALID == mDecryptHandle->status) { 1665 if (DecryptApiType::WV_BASED == mDecryptHandle->decryptApiType) { 1666 LOGD("Setting mCachedSource to NULL for WVM\n"); 1667 mCachedSource.clear(); 1668 } 1669 } else { 1670 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_NO_LICENSE); 1671 } 1672 } 1673 1674 return setDataSource_l(extractor); 1675} 1676 1677void AwesomePlayer::abortPrepare(status_t err) { 1678 CHECK(err != OK); 1679 1680 if (mIsAsyncPrepare) { 1681 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1682 } 1683 1684 mPrepareResult = err; 1685 mFlags &= ~(PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED); 1686 mAsyncPrepareEvent = NULL; 1687 mPreparedCondition.broadcast(); 1688} 1689 1690// static 1691bool AwesomePlayer::ContinuePreparation(void *cookie) { 1692 AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie); 1693 1694 return (me->mFlags & PREPARE_CANCELLED) == 0; 1695} 1696 1697void AwesomePlayer::onPrepareAsyncEvent() { 1698 Mutex::Autolock autoLock(mLock); 1699 1700 if (mFlags & PREPARE_CANCELLED) { 1701 LOGI("prepare was cancelled before doing anything"); 1702 abortPrepare(UNKNOWN_ERROR); 1703 return; 1704 } 1705 1706 if (mUri.size() > 0) { 1707 status_t err = finishSetDataSource_l(); 1708 1709 if (err != OK) { 1710 abortPrepare(err); 1711 return; 1712 } 1713 } 1714 1715 if (mVideoTrack != NULL && mVideoSource == NULL) { 1716 status_t err = initVideoDecoder(); 1717 1718 if (err != OK) { 1719 abortPrepare(err); 1720 return; 1721 } 1722 } 1723 1724 if (mAudioTrack != NULL && mAudioSource == NULL) { 1725 status_t err = initAudioDecoder(); 1726 1727 if (err != OK) { 1728 abortPrepare(err); 1729 return; 1730 } 1731 } 1732 1733 mFlags |= PREPARING_CONNECTED; 1734 1735 if (mCachedSource != NULL || mRTSPController != NULL) { 1736 postBufferingEvent_l(); 1737 } else { 1738 finishAsyncPrepare_l(); 1739 } 1740} 1741 1742void AwesomePlayer::finishAsyncPrepare_l() { 1743 if (mIsAsyncPrepare) { 1744 if (mVideoSource == NULL) { 1745 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0); 1746 } else { 1747 notifyVideoSize_l(); 1748 } 1749 1750 notifyListener_l(MEDIA_PREPARED); 1751 } 1752 1753 mPrepareResult = OK; 1754 mFlags &= ~(PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED); 1755 mFlags |= PREPARED; 1756 mAsyncPrepareEvent = NULL; 1757 mPreparedCondition.broadcast(); 1758} 1759 1760uint32_t AwesomePlayer::flags() const { 1761 return mExtractorFlags; 1762} 1763 1764void AwesomePlayer::postAudioEOS() { 1765 postCheckAudioStatusEvent_l(); 1766} 1767 1768void AwesomePlayer::postAudioSeekComplete() { 1769 postCheckAudioStatusEvent_l(); 1770} 1771 1772} // namespace android 1773