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