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