AwesomePlayer.cpp revision 2b82e9652ba049e754c2cc74e381282f231d5fbf
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(true /* at eos */); 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(true /* at eos */); 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(bool at_eos) { 742 if (!(mFlags & PLAYING)) { 743 return OK; 744 } 745 746 cancelPlayerEvents(true /* keepBufferingGoing */); 747 748 if (mAudioPlayer != NULL) { 749 if (at_eos) { 750 // If we played the audio stream to completion we 751 // want to make sure that all samples remaining in the audio 752 // track's queue are played out. 753 mAudioPlayer->pause(true /* playPendingSamples */); 754 } else { 755 mAudioPlayer->pause(); 756 } 757 } 758 759 mFlags &= ~PLAYING; 760 761 return OK; 762} 763 764bool AwesomePlayer::isPlaying() const { 765 return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN); 766} 767 768void AwesomePlayer::setISurface(const sp<ISurface> &isurface) { 769 Mutex::Autolock autoLock(mLock); 770 771 mISurface = isurface; 772} 773 774void AwesomePlayer::setAudioSink( 775 const sp<MediaPlayerBase::AudioSink> &audioSink) { 776 Mutex::Autolock autoLock(mLock); 777 778 mAudioSink = audioSink; 779} 780 781status_t AwesomePlayer::setLooping(bool shouldLoop) { 782 Mutex::Autolock autoLock(mLock); 783 784 mFlags = mFlags & ~LOOPING; 785 786 if (shouldLoop) { 787 mFlags |= LOOPING; 788 } 789 790 return OK; 791} 792 793status_t AwesomePlayer::getDuration(int64_t *durationUs) { 794 Mutex::Autolock autoLock(mMiscStateLock); 795 796 if (mDurationUs < 0) { 797 return UNKNOWN_ERROR; 798 } 799 800 *durationUs = mDurationUs; 801 802 return OK; 803} 804 805status_t AwesomePlayer::getPosition(int64_t *positionUs) { 806 if (mRTSPController != NULL) { 807 *positionUs = mRTSPController->getNormalPlayTimeUs(); 808 } 809 else if (mSeeking) { 810 *positionUs = mSeekTimeUs; 811 } else if (mVideoSource != NULL) { 812 Mutex::Autolock autoLock(mMiscStateLock); 813 *positionUs = mVideoTimeUs; 814 } else if (mAudioPlayer != NULL) { 815 *positionUs = mAudioPlayer->getMediaTimeUs(); 816 } else { 817 *positionUs = 0; 818 } 819 820 return OK; 821} 822 823status_t AwesomePlayer::seekTo(int64_t timeUs) { 824 if (mExtractorFlags 825 & (MediaExtractor::CAN_SEEK_FORWARD 826 | MediaExtractor::CAN_SEEK_BACKWARD)) { 827 Mutex::Autolock autoLock(mLock); 828 return seekTo_l(timeUs); 829 } 830 831 return OK; 832} 833 834status_t AwesomePlayer::seekTo_l(int64_t timeUs) { 835 if (mRTSPController != NULL) { 836 mRTSPController->seek(timeUs); 837 838 notifyListener_l(MEDIA_SEEK_COMPLETE); 839 mSeekNotificationSent = true; 840 return OK; 841 } 842 843 if (mFlags & CACHE_UNDERRUN) { 844 mFlags &= ~CACHE_UNDERRUN; 845 play_l(); 846 } 847 848 mSeeking = true; 849 mSeekNotificationSent = false; 850 mSeekTimeUs = timeUs; 851 mFlags &= ~(AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS); 852 853 seekAudioIfNecessary_l(); 854 855 if (!(mFlags & PLAYING)) { 856 LOGV("seeking while paused, sending SEEK_COMPLETE notification" 857 " immediately."); 858 859 notifyListener_l(MEDIA_SEEK_COMPLETE); 860 mSeekNotificationSent = true; 861 } 862 863 return OK; 864} 865 866void AwesomePlayer::seekAudioIfNecessary_l() { 867 if (mSeeking && mVideoSource == NULL && mAudioPlayer != NULL) { 868 mAudioPlayer->seekTo(mSeekTimeUs); 869 870 mWatchForAudioSeekComplete = true; 871 mWatchForAudioEOS = true; 872 mSeekNotificationSent = false; 873 } 874} 875 876status_t AwesomePlayer::getVideoDimensions( 877 int32_t *width, int32_t *height) const { 878 Mutex::Autolock autoLock(mLock); 879 880 if (mVideoWidth < 0 || mVideoHeight < 0) { 881 return UNKNOWN_ERROR; 882 } 883 884 *width = mVideoWidth; 885 *height = mVideoHeight; 886 887 return OK; 888} 889 890void AwesomePlayer::setAudioSource(sp<MediaSource> source) { 891 CHECK(source != NULL); 892 893 mAudioTrack = source; 894} 895 896status_t AwesomePlayer::initAudioDecoder() { 897 sp<MetaData> meta = mAudioTrack->getFormat(); 898 899 const char *mime; 900 CHECK(meta->findCString(kKeyMIMEType, &mime)); 901 902 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { 903 mAudioSource = mAudioTrack; 904 } else { 905 mAudioSource = OMXCodec::Create( 906 mClient.interface(), mAudioTrack->getFormat(), 907 false, // createEncoder 908 mAudioTrack); 909 } 910 911 if (mAudioSource != NULL) { 912 int64_t durationUs; 913 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 914 Mutex::Autolock autoLock(mMiscStateLock); 915 if (mDurationUs < 0 || durationUs > mDurationUs) { 916 mDurationUs = durationUs; 917 } 918 } 919 920 status_t err = mAudioSource->start(); 921 922 if (err != OK) { 923 mAudioSource.clear(); 924 return err; 925 } 926 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) { 927 // For legacy reasons we're simply going to ignore the absence 928 // of an audio decoder for QCELP instead of aborting playback 929 // altogether. 930 return OK; 931 } 932 933 return mAudioSource != NULL ? OK : UNKNOWN_ERROR; 934} 935 936void AwesomePlayer::setVideoSource(sp<MediaSource> source) { 937 CHECK(source != NULL); 938 939 mVideoTrack = source; 940} 941 942status_t AwesomePlayer::initVideoDecoder() { 943 uint32_t flags = 0; 944 mVideoSource = OMXCodec::Create( 945 mClient.interface(), mVideoTrack->getFormat(), 946 false, // createEncoder 947 mVideoTrack, 948 NULL, flags); 949 950 if (mVideoSource != NULL) { 951 int64_t durationUs; 952 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 953 Mutex::Autolock autoLock(mMiscStateLock); 954 if (mDurationUs < 0 || durationUs > mDurationUs) { 955 mDurationUs = durationUs; 956 } 957 } 958 959 CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth)); 960 CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight)); 961 962 status_t err = mVideoSource->start(); 963 964 if (err != OK) { 965 mVideoSource.clear(); 966 return err; 967 } 968 } 969 970 return mVideoSource != NULL ? OK : UNKNOWN_ERROR; 971} 972 973void AwesomePlayer::onVideoEvent() { 974 Mutex::Autolock autoLock(mLock); 975 if (!mVideoEventPending) { 976 // The event has been cancelled in reset_l() but had already 977 // been scheduled for execution at that time. 978 return; 979 } 980 mVideoEventPending = false; 981 982 if (mSeeking) { 983 if (mLastVideoBuffer) { 984 mLastVideoBuffer->release(); 985 mLastVideoBuffer = NULL; 986 } 987 988 if (mVideoBuffer) { 989 mVideoBuffer->release(); 990 mVideoBuffer = NULL; 991 } 992 993 if (mCachedSource != NULL && mAudioSource != NULL) { 994 // We're going to seek the video source first, followed by 995 // the audio source. 996 // In order to avoid jumps in the DataSource offset caused by 997 // the audio codec prefetching data from the old locations 998 // while the video codec is already reading data from the new 999 // locations, we'll "pause" the audio source, causing it to 1000 // stop reading input data until a subsequent seek. 1001 1002 if (mAudioPlayer != NULL) { 1003 mAudioPlayer->pause(); 1004 } 1005 mAudioSource->pause(); 1006 } 1007 } 1008 1009 if (!mVideoBuffer) { 1010 MediaSource::ReadOptions options; 1011 if (mSeeking) { 1012 LOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6); 1013 1014 options.setSeekTo( 1015 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST_SYNC); 1016 } 1017 for (;;) { 1018 status_t err = mVideoSource->read(&mVideoBuffer, &options); 1019 options.clearSeekTo(); 1020 1021 if (err != OK) { 1022 CHECK_EQ(mVideoBuffer, NULL); 1023 1024 if (err == INFO_FORMAT_CHANGED) { 1025 LOGV("VideoSource signalled format change."); 1026 1027 if (mVideoRenderer != NULL) { 1028 mVideoRendererIsPreview = false; 1029 initRenderer_l(); 1030 } 1031 continue; 1032 } 1033 1034 mFlags |= VIDEO_AT_EOS; 1035 postStreamDoneEvent_l(err); 1036 return; 1037 } 1038 1039 if (mVideoBuffer->range_length() == 0) { 1040 // Some decoders, notably the PV AVC software decoder 1041 // return spurious empty buffers that we just want to ignore. 1042 1043 mVideoBuffer->release(); 1044 mVideoBuffer = NULL; 1045 continue; 1046 } 1047 1048 break; 1049 } 1050 } 1051 1052 int64_t timeUs; 1053 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 1054 1055 { 1056 Mutex::Autolock autoLock(mMiscStateLock); 1057 mVideoTimeUs = timeUs; 1058 } 1059 1060 if (mSeeking) { 1061 if (mAudioPlayer != NULL) { 1062 LOGV("seeking audio to %lld us (%.2f secs).", timeUs, timeUs / 1E6); 1063 1064 mAudioPlayer->seekTo(timeUs); 1065 mAudioPlayer->resume(); 1066 mWatchForAudioSeekComplete = true; 1067 mWatchForAudioEOS = true; 1068 } else if (!mSeekNotificationSent) { 1069 // If we're playing video only, report seek complete now, 1070 // otherwise audio player will notify us later. 1071 notifyListener_l(MEDIA_SEEK_COMPLETE); 1072 } 1073 1074 mFlags |= FIRST_FRAME; 1075 mSeeking = false; 1076 mSeekNotificationSent = false; 1077 } 1078 1079 TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource; 1080 1081 if (mFlags & FIRST_FRAME) { 1082 mFlags &= ~FIRST_FRAME; 1083 1084 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs; 1085 } 1086 1087 int64_t realTimeUs, mediaTimeUs; 1088 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL 1089 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) { 1090 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs; 1091 } 1092 1093 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; 1094 1095 int64_t latenessUs = nowUs - timeUs; 1096 1097 if (mRTPSession != NULL) { 1098 // We'll completely ignore timestamps for gtalk videochat 1099 // and we'll play incoming video as fast as we get it. 1100 latenessUs = 0; 1101 } 1102 1103 if (latenessUs > 40000) { 1104 // We're more than 40ms late. 1105 LOGV("we're late by %lld us (%.2f secs)", latenessUs, latenessUs / 1E6); 1106 1107 mVideoBuffer->release(); 1108 mVideoBuffer = NULL; 1109 1110 postVideoEvent_l(); 1111 return; 1112 } 1113 1114 if (latenessUs < -10000) { 1115 // We're more than 10ms early. 1116 1117 postVideoEvent_l(10000); 1118 return; 1119 } 1120 1121 if (mVideoRendererIsPreview || mVideoRenderer == NULL) { 1122 mVideoRendererIsPreview = false; 1123 1124 initRenderer_l(); 1125 } 1126 1127 if (mVideoRenderer != NULL) { 1128 mVideoRenderer->render(mVideoBuffer); 1129 } 1130 1131 if (mLastVideoBuffer) { 1132 mLastVideoBuffer->release(); 1133 mLastVideoBuffer = NULL; 1134 } 1135 mLastVideoBuffer = mVideoBuffer; 1136 mVideoBuffer = NULL; 1137 1138 postVideoEvent_l(); 1139} 1140 1141void AwesomePlayer::postVideoEvent_l(int64_t delayUs) { 1142 if (mVideoEventPending) { 1143 return; 1144 } 1145 1146 mVideoEventPending = true; 1147 mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs); 1148} 1149 1150void AwesomePlayer::postStreamDoneEvent_l(status_t status) { 1151 if (mStreamDoneEventPending) { 1152 return; 1153 } 1154 mStreamDoneEventPending = true; 1155 1156 mStreamDoneStatus = status; 1157 mQueue.postEvent(mStreamDoneEvent); 1158} 1159 1160void AwesomePlayer::postBufferingEvent_l() { 1161 if (mBufferingEventPending) { 1162 return; 1163 } 1164 mBufferingEventPending = true; 1165 mQueue.postEventWithDelay(mBufferingEvent, 1000000ll); 1166} 1167 1168void AwesomePlayer::postCheckAudioStatusEvent_l() { 1169 if (mAudioStatusEventPending) { 1170 return; 1171 } 1172 mAudioStatusEventPending = true; 1173 mQueue.postEvent(mCheckAudioStatusEvent); 1174} 1175 1176void AwesomePlayer::onCheckAudioStatus() { 1177 Mutex::Autolock autoLock(mLock); 1178 if (!mAudioStatusEventPending) { 1179 // Event was dispatched and while we were blocking on the mutex, 1180 // has already been cancelled. 1181 return; 1182 } 1183 1184 mAudioStatusEventPending = false; 1185 1186 if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) { 1187 mWatchForAudioSeekComplete = false; 1188 1189 if (!mSeekNotificationSent) { 1190 notifyListener_l(MEDIA_SEEK_COMPLETE); 1191 mSeekNotificationSent = true; 1192 } 1193 1194 mSeeking = false; 1195 } 1196 1197 status_t finalStatus; 1198 if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) { 1199 mWatchForAudioEOS = false; 1200 mFlags |= AUDIO_AT_EOS; 1201 mFlags |= FIRST_FRAME; 1202 postStreamDoneEvent_l(finalStatus); 1203 } 1204} 1205 1206status_t AwesomePlayer::prepare() { 1207 Mutex::Autolock autoLock(mLock); 1208 return prepare_l(); 1209} 1210 1211status_t AwesomePlayer::prepare_l() { 1212 if (mFlags & PREPARED) { 1213 return OK; 1214 } 1215 1216 if (mFlags & PREPARING) { 1217 return UNKNOWN_ERROR; 1218 } 1219 1220 mIsAsyncPrepare = false; 1221 status_t err = prepareAsync_l(); 1222 1223 if (err != OK) { 1224 return err; 1225 } 1226 1227 while (mFlags & PREPARING) { 1228 mPreparedCondition.wait(mLock); 1229 } 1230 1231 return mPrepareResult; 1232} 1233 1234status_t AwesomePlayer::prepareAsync() { 1235 Mutex::Autolock autoLock(mLock); 1236 1237 if (mFlags & PREPARING) { 1238 return UNKNOWN_ERROR; // async prepare already pending 1239 } 1240 1241 mIsAsyncPrepare = true; 1242 return prepareAsync_l(); 1243} 1244 1245status_t AwesomePlayer::prepareAsync_l() { 1246 if (mFlags & PREPARING) { 1247 return UNKNOWN_ERROR; // async prepare already pending 1248 } 1249 1250 if (!mQueueStarted) { 1251 mQueue.start(); 1252 mQueueStarted = true; 1253 } 1254 1255 mFlags |= PREPARING; 1256 mAsyncPrepareEvent = new AwesomeEvent( 1257 this, &AwesomePlayer::onPrepareAsyncEvent); 1258 1259 mQueue.postEvent(mAsyncPrepareEvent); 1260 1261 return OK; 1262} 1263 1264status_t AwesomePlayer::finishSetDataSource_l() { 1265 sp<DataSource> dataSource; 1266 1267 if (!strncasecmp("http://", mUri.string(), 7)) { 1268 mConnectingDataSource = new NuHTTPDataSource; 1269 1270 mLock.unlock(); 1271 status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders); 1272 mLock.lock(); 1273 1274 if (err != OK) { 1275 mConnectingDataSource.clear(); 1276 1277 LOGI("mConnectingDataSource->connect() returned %d", err); 1278 return err; 1279 } 1280 1281#if 0 1282 mCachedSource = new NuCachedSource2( 1283 new ThrottledSource( 1284 mConnectingDataSource, 50 * 1024 /* bytes/sec */)); 1285#else 1286 mCachedSource = new NuCachedSource2(mConnectingDataSource); 1287#endif 1288 mConnectingDataSource.clear(); 1289 1290 dataSource = mCachedSource; 1291 } else if (!strncasecmp(mUri.string(), "httplive://", 11)) { 1292 String8 uri("http://"); 1293 uri.append(mUri.string() + 11); 1294 1295 dataSource = new LiveSource(uri.string()); 1296 1297 mCachedSource = new NuCachedSource2(dataSource); 1298 dataSource = mCachedSource; 1299 1300 sp<MediaExtractor> extractor = 1301 MediaExtractor::Create(dataSource, MEDIA_MIMETYPE_CONTAINER_MPEG2TS); 1302 1303 return setDataSource_l(extractor); 1304 } else if (!strncmp("rtsp://gtalk/", mUri.string(), 13)) { 1305 if (mLooper == NULL) { 1306 mLooper = new ALooper; 1307 mLooper->setName("gtalk rtp"); 1308 mLooper->start( 1309 false /* runOnCallingThread */, 1310 false /* canCallJava */, 1311 PRIORITY_HIGHEST); 1312 } 1313 1314 const char *startOfCodecString = &mUri.string()[13]; 1315 const char *startOfSlash1 = strchr(startOfCodecString, '/'); 1316 if (startOfSlash1 == NULL) { 1317 return BAD_VALUE; 1318 } 1319 const char *startOfWidthString = &startOfSlash1[1]; 1320 const char *startOfSlash2 = strchr(startOfWidthString, '/'); 1321 if (startOfSlash2 == NULL) { 1322 return BAD_VALUE; 1323 } 1324 const char *startOfHeightString = &startOfSlash2[1]; 1325 1326 String8 codecString(startOfCodecString, startOfSlash1 - startOfCodecString); 1327 String8 widthString(startOfWidthString, startOfSlash2 - startOfWidthString); 1328 String8 heightString(startOfHeightString); 1329 1330#if 0 1331 mRTPPusher = new UDPPusher("/data/misc/rtpout.bin", 5434); 1332 mLooper->registerHandler(mRTPPusher); 1333 1334 mRTCPPusher = new UDPPusher("/data/misc/rtcpout.bin", 5435); 1335 mLooper->registerHandler(mRTCPPusher); 1336#endif 1337 1338 mRTPSession = new ARTPSession; 1339 mLooper->registerHandler(mRTPSession); 1340 1341#if 0 1342 // My AMR SDP 1343 static const char *raw = 1344 "v=0\r\n" 1345 "o=- 64 233572944 IN IP4 127.0.0.0\r\n" 1346 "s=QuickTime\r\n" 1347 "t=0 0\r\n" 1348 "a=range:npt=0-315\r\n" 1349 "a=isma-compliance:2,2.0,2\r\n" 1350 "m=audio 5434 RTP/AVP 97\r\n" 1351 "c=IN IP4 127.0.0.1\r\n" 1352 "b=AS:30\r\n" 1353 "a=rtpmap:97 AMR/8000/1\r\n" 1354 "a=fmtp:97 octet-align\r\n"; 1355#elif 1 1356 String8 sdp; 1357 sdp.appendFormat( 1358 "v=0\r\n" 1359 "o=- 64 233572944 IN IP4 127.0.0.0\r\n" 1360 "s=QuickTime\r\n" 1361 "t=0 0\r\n" 1362 "a=range:npt=0-315\r\n" 1363 "a=isma-compliance:2,2.0,2\r\n" 1364 "m=video 5434 RTP/AVP 97\r\n" 1365 "c=IN IP4 127.0.0.1\r\n" 1366 "b=AS:30\r\n" 1367 "a=rtpmap:97 %s/90000\r\n" 1368 "a=cliprect:0,0,%s,%s\r\n" 1369 "a=framesize:97 %s-%s\r\n", 1370 1371 codecString.string(), 1372 heightString.string(), widthString.string(), 1373 widthString.string(), heightString.string() 1374 ); 1375 const char *raw = sdp.string(); 1376 1377#endif 1378 1379 sp<ASessionDescription> desc = new ASessionDescription; 1380 CHECK(desc->setTo(raw, strlen(raw))); 1381 1382 CHECK_EQ(mRTPSession->setup(desc), (status_t)OK); 1383 1384 if (mRTPPusher != NULL) { 1385 mRTPPusher->start(); 1386 } 1387 1388 if (mRTCPPusher != NULL) { 1389 mRTCPPusher->start(); 1390 } 1391 1392 CHECK_EQ(mRTPSession->countTracks(), 1u); 1393 sp<MediaSource> source = mRTPSession->trackAt(0); 1394 1395#if 0 1396 bool eos; 1397 while (((APacketSource *)source.get()) 1398 ->getQueuedDuration(&eos) < 5000000ll && !eos) { 1399 usleep(100000ll); 1400 } 1401#endif 1402 1403 const char *mime; 1404 CHECK(source->getFormat()->findCString(kKeyMIMEType, &mime)); 1405 1406 if (!strncasecmp("video/", mime, 6)) { 1407 setVideoSource(source); 1408 } else { 1409 CHECK(!strncasecmp("audio/", mime, 6)); 1410 setAudioSource(source); 1411 } 1412 1413 mExtractorFlags = MediaExtractor::CAN_PAUSE; 1414 1415 return OK; 1416 } else if (!strncasecmp("rtsp://", mUri.string(), 7)) { 1417 if (mLooper == NULL) { 1418 mLooper = new ALooper; 1419 mLooper->setName("rtsp"); 1420 mLooper->start(); 1421 } 1422 mRTSPController = new ARTSPController(mLooper); 1423 status_t err = mRTSPController->connect(mUri.string()); 1424 1425 LOGI("ARTSPController::connect returned %d", err); 1426 1427 if (err != OK) { 1428 mRTSPController.clear(); 1429 return err; 1430 } 1431 1432 sp<MediaExtractor> extractor = mRTSPController.get(); 1433 return setDataSource_l(extractor); 1434 } else { 1435 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders); 1436 } 1437 1438 if (dataSource == NULL) { 1439 return UNKNOWN_ERROR; 1440 } 1441 1442 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 1443 1444 if (extractor == NULL) { 1445 return UNKNOWN_ERROR; 1446 } 1447 1448 return setDataSource_l(extractor); 1449} 1450 1451void AwesomePlayer::abortPrepare(status_t err) { 1452 CHECK(err != OK); 1453 1454 if (mIsAsyncPrepare) { 1455 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1456 } 1457 1458 mPrepareResult = err; 1459 mFlags &= ~(PREPARING|PREPARE_CANCELLED); 1460 mAsyncPrepareEvent = NULL; 1461 mPreparedCondition.broadcast(); 1462} 1463 1464// static 1465bool AwesomePlayer::ContinuePreparation(void *cookie) { 1466 AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie); 1467 1468 return (me->mFlags & PREPARE_CANCELLED) == 0; 1469} 1470 1471void AwesomePlayer::onPrepareAsyncEvent() { 1472 Mutex::Autolock autoLock(mLock); 1473 1474 if (mFlags & PREPARE_CANCELLED) { 1475 LOGI("prepare was cancelled before doing anything"); 1476 abortPrepare(UNKNOWN_ERROR); 1477 return; 1478 } 1479 1480 if (mUri.size() > 0) { 1481 status_t err = finishSetDataSource_l(); 1482 1483 if (err != OK) { 1484 abortPrepare(err); 1485 return; 1486 } 1487 } 1488 1489 if (mVideoTrack != NULL && mVideoSource == NULL) { 1490 status_t err = initVideoDecoder(); 1491 1492 if (err != OK) { 1493 abortPrepare(err); 1494 return; 1495 } 1496 } 1497 1498 if (mAudioTrack != NULL && mAudioSource == NULL) { 1499 status_t err = initAudioDecoder(); 1500 1501 if (err != OK) { 1502 abortPrepare(err); 1503 return; 1504 } 1505 } 1506 1507 if (mCachedSource != NULL || mRTSPController != NULL) { 1508 postBufferingEvent_l(); 1509 } else { 1510 finishAsyncPrepare_l(); 1511 } 1512} 1513 1514void AwesomePlayer::finishAsyncPrepare_l() { 1515 if (mIsAsyncPrepare) { 1516 if (mVideoWidth < 0 || mVideoHeight < 0) { 1517 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0); 1518 } else { 1519 notifyListener_l(MEDIA_SET_VIDEO_SIZE, mVideoWidth, mVideoHeight); 1520 } 1521 1522 notifyListener_l(MEDIA_PREPARED); 1523 } 1524 1525 mPrepareResult = OK; 1526 mFlags &= ~(PREPARING|PREPARE_CANCELLED); 1527 mFlags |= PREPARED; 1528 mAsyncPrepareEvent = NULL; 1529 mPreparedCondition.broadcast(); 1530} 1531 1532status_t AwesomePlayer::suspend() { 1533 LOGV("suspend"); 1534 Mutex::Autolock autoLock(mLock); 1535 1536 if (mSuspensionState != NULL) { 1537 if (mLastVideoBuffer == NULL) { 1538 //go into here if video is suspended again 1539 //after resuming without being played between 1540 //them 1541 SuspensionState *state = mSuspensionState; 1542 mSuspensionState = NULL; 1543 reset_l(); 1544 mSuspensionState = state; 1545 return OK; 1546 } 1547 1548 delete mSuspensionState; 1549 mSuspensionState = NULL; 1550 } 1551 1552 if (mFlags & PREPARING) { 1553 mFlags |= PREPARE_CANCELLED; 1554 if (mConnectingDataSource != NULL) { 1555 LOGI("interrupting the connection process"); 1556 mConnectingDataSource->disconnect(); 1557 } 1558 } 1559 1560 while (mFlags & PREPARING) { 1561 mPreparedCondition.wait(mLock); 1562 } 1563 1564 SuspensionState *state = new SuspensionState; 1565 state->mUri = mUri; 1566 state->mUriHeaders = mUriHeaders; 1567 state->mFileSource = mFileSource; 1568 1569 state->mFlags = mFlags & (PLAYING | AUTO_LOOPING | LOOPING | AT_EOS); 1570 getPosition(&state->mPositionUs); 1571 1572 if (mLastVideoBuffer) { 1573 size_t size = mLastVideoBuffer->range_length(); 1574 1575 if (size) { 1576 int32_t unreadable; 1577 if (!mLastVideoBuffer->meta_data()->findInt32( 1578 kKeyIsUnreadable, &unreadable) 1579 || unreadable == 0) { 1580 state->mLastVideoFrameSize = size; 1581 state->mLastVideoFrame = malloc(size); 1582 memcpy(state->mLastVideoFrame, 1583 (const uint8_t *)mLastVideoBuffer->data() 1584 + mLastVideoBuffer->range_offset(), 1585 size); 1586 1587 state->mVideoWidth = mVideoWidth; 1588 state->mVideoHeight = mVideoHeight; 1589 1590 sp<MetaData> meta = mVideoSource->getFormat(); 1591 CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat)); 1592 CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth)); 1593 CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight)); 1594 } else { 1595 LOGV("Unable to save last video frame, we have no access to " 1596 "the decoded video data."); 1597 } 1598 } 1599 } 1600 1601 reset_l(); 1602 1603 mSuspensionState = state; 1604 1605 return OK; 1606} 1607 1608status_t AwesomePlayer::resume() { 1609 LOGV("resume"); 1610 Mutex::Autolock autoLock(mLock); 1611 1612 if (mSuspensionState == NULL) { 1613 return INVALID_OPERATION; 1614 } 1615 1616 SuspensionState *state = mSuspensionState; 1617 mSuspensionState = NULL; 1618 1619 status_t err; 1620 if (state->mFileSource != NULL) { 1621 err = setDataSource_l(state->mFileSource); 1622 1623 if (err == OK) { 1624 mFileSource = state->mFileSource; 1625 } 1626 } else { 1627 err = setDataSource_l(state->mUri, &state->mUriHeaders); 1628 } 1629 1630 if (err != OK) { 1631 delete state; 1632 state = NULL; 1633 1634 return err; 1635 } 1636 1637 seekTo_l(state->mPositionUs); 1638 1639 mFlags = state->mFlags & (AUTO_LOOPING | LOOPING | AT_EOS); 1640 1641 if (state->mLastVideoFrame && mISurface != NULL) { 1642 mVideoRenderer = 1643 new AwesomeLocalRenderer( 1644 true, // previewOnly 1645 "", 1646 (OMX_COLOR_FORMATTYPE)state->mColorFormat, 1647 mISurface, 1648 state->mVideoWidth, 1649 state->mVideoHeight, 1650 state->mDecodedWidth, 1651 state->mDecodedHeight); 1652 1653 mVideoRendererIsPreview = true; 1654 1655 ((AwesomeLocalRenderer *)mVideoRenderer.get())->render( 1656 state->mLastVideoFrame, state->mLastVideoFrameSize); 1657 } 1658 1659 if (state->mFlags & PLAYING) { 1660 play_l(); 1661 } 1662 1663 mSuspensionState = state; 1664 state = NULL; 1665 1666 return OK; 1667} 1668 1669uint32_t AwesomePlayer::flags() const { 1670 return mExtractorFlags; 1671} 1672 1673void AwesomePlayer::postAudioEOS() { 1674 postCheckAudioStatusEvent_l(); 1675} 1676 1677void AwesomePlayer::postAudioSeekComplete() { 1678 postCheckAudioStatusEvent_l(); 1679} 1680 1681} // namespace android 1682 1683