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