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