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