AwesomePlayer.cpp revision 4f6eed0d1c7972a983c075bdcf03089569e13fe1
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#undef DEBUG_HDCP 18 19//#define LOG_NDEBUG 0 20#define LOG_TAG "AwesomePlayer" 21#include <utils/Log.h> 22 23#include <dlfcn.h> 24 25#include "include/AwesomePlayer.h" 26#include "include/DRMExtractor.h" 27#include "include/SoftwareRenderer.h" 28#include "include/NuCachedSource2.h" 29#include "include/ThrottledSource.h" 30#include "include/MPEG2TSExtractor.h" 31#include "include/WVMExtractor.h" 32 33#include <binder/IPCThreadState.h> 34#include <binder/IServiceManager.h> 35#include <media/IMediaPlayerService.h> 36#include <media/stagefright/foundation/hexdump.h> 37#include <media/stagefright/foundation/ADebug.h> 38#include <media/stagefright/timedtext/TimedTextDriver.h> 39#include <media/stagefright/AudioPlayer.h> 40#include <media/stagefright/DataSource.h> 41#include <media/stagefright/FileSource.h> 42#include <media/stagefright/MediaBuffer.h> 43#include <media/stagefright/MediaDefs.h> 44#include <media/stagefright/MediaExtractor.h> 45#include <media/stagefright/MediaSource.h> 46#include <media/stagefright/MetaData.h> 47#include <media/stagefright/OMXCodec.h> 48 49#include <gui/ISurfaceTexture.h> 50#include <gui/SurfaceTextureClient.h> 51 52#include <media/stagefright/foundation/AMessage.h> 53 54#include <cutils/properties.h> 55 56#define USE_SURFACE_ALLOC 1 57#define FRAME_DROP_FREQ 0 58 59namespace android { 60 61static int64_t kLowWaterMarkUs = 2000000ll; // 2secs 62static int64_t kHighWaterMarkUs = 5000000ll; // 5secs 63static const size_t kLowWaterMarkBytes = 40000; 64static const size_t kHighWaterMarkBytes = 200000; 65 66struct AwesomeEvent : public TimedEventQueue::Event { 67 AwesomeEvent( 68 AwesomePlayer *player, 69 void (AwesomePlayer::*method)()) 70 : mPlayer(player), 71 mMethod(method) { 72 } 73 74protected: 75 virtual ~AwesomeEvent() {} 76 77 virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) { 78 (mPlayer->*mMethod)(); 79 } 80 81private: 82 AwesomePlayer *mPlayer; 83 void (AwesomePlayer::*mMethod)(); 84 85 AwesomeEvent(const AwesomeEvent &); 86 AwesomeEvent &operator=(const AwesomeEvent &); 87}; 88 89struct AwesomeLocalRenderer : public AwesomeRenderer { 90 AwesomeLocalRenderer( 91 const sp<ANativeWindow> &nativeWindow, const sp<MetaData> &meta) 92 : mTarget(new SoftwareRenderer(nativeWindow, meta)) { 93 } 94 95 virtual void render(MediaBuffer *buffer) { 96 render((const uint8_t *)buffer->data() + buffer->range_offset(), 97 buffer->range_length()); 98 } 99 100 void render(const void *data, size_t size) { 101 mTarget->render(data, size, NULL); 102 } 103 104protected: 105 virtual ~AwesomeLocalRenderer() { 106 delete mTarget; 107 mTarget = NULL; 108 } 109 110private: 111 SoftwareRenderer *mTarget; 112 113 AwesomeLocalRenderer(const AwesomeLocalRenderer &); 114 AwesomeLocalRenderer &operator=(const AwesomeLocalRenderer &);; 115}; 116 117struct AwesomeNativeWindowRenderer : public AwesomeRenderer { 118 AwesomeNativeWindowRenderer( 119 const sp<ANativeWindow> &nativeWindow, 120 int32_t rotationDegrees) 121 : mNativeWindow(nativeWindow) { 122 applyRotation(rotationDegrees); 123 } 124 125 virtual void render(MediaBuffer *buffer) { 126 int64_t timeUs; 127 CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs)); 128 native_window_set_buffers_timestamp(mNativeWindow.get(), timeUs * 1000); 129 status_t err = mNativeWindow->queueBuffer( 130 mNativeWindow.get(), buffer->graphicBuffer().get()); 131 if (err != 0) { 132 ALOGE("queueBuffer failed with error %s (%d)", strerror(-err), 133 -err); 134 return; 135 } 136 137 sp<MetaData> metaData = buffer->meta_data(); 138 metaData->setInt32(kKeyRendered, 1); 139 } 140 141protected: 142 virtual ~AwesomeNativeWindowRenderer() {} 143 144private: 145 sp<ANativeWindow> mNativeWindow; 146 147 void applyRotation(int32_t rotationDegrees) { 148 uint32_t transform; 149 switch (rotationDegrees) { 150 case 0: transform = 0; break; 151 case 90: transform = HAL_TRANSFORM_ROT_90; break; 152 case 180: transform = HAL_TRANSFORM_ROT_180; break; 153 case 270: transform = HAL_TRANSFORM_ROT_270; break; 154 default: transform = 0; break; 155 } 156 157 if (transform) { 158 CHECK_EQ(0, native_window_set_buffers_transform( 159 mNativeWindow.get(), transform)); 160 } 161 } 162 163 AwesomeNativeWindowRenderer(const AwesomeNativeWindowRenderer &); 164 AwesomeNativeWindowRenderer &operator=( 165 const AwesomeNativeWindowRenderer &); 166}; 167 168// To collect the decoder usage 169void addBatteryData(uint32_t params) { 170 sp<IBinder> binder = 171 defaultServiceManager()->getService(String16("media.player")); 172 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 173 CHECK(service.get() != NULL); 174 175 service->addBatteryData(params); 176} 177 178//////////////////////////////////////////////////////////////////////////////// 179AwesomePlayer::AwesomePlayer() 180 : mQueueStarted(false), 181 mUIDValid(false), 182 mTimeSource(NULL), 183 mVideoRendererIsPreview(false), 184 mAudioPlayer(NULL), 185 mDisplayWidth(0), 186 mDisplayHeight(0), 187 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 188 mFlags(0), 189 mExtractorFlags(0), 190 mVideoBuffer(NULL), 191 mDecryptHandle(NULL), 192 mLastVideoTimeUs(-1), 193 mTextDriver(NULL) { 194 CHECK_EQ(mClient.connect(), (status_t)OK); 195 196 DataSource::RegisterDefaultSniffers(); 197 198 mVideoEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoEvent); 199 mVideoEventPending = false; 200 mStreamDoneEvent = new AwesomeEvent(this, &AwesomePlayer::onStreamDone); 201 mStreamDoneEventPending = false; 202 mBufferingEvent = new AwesomeEvent(this, &AwesomePlayer::onBufferingUpdate); 203 mBufferingEventPending = false; 204 mVideoLagEvent = new AwesomeEvent(this, &AwesomePlayer::onVideoLagUpdate); 205 mVideoEventPending = false; 206 207 mCheckAudioStatusEvent = new AwesomeEvent( 208 this, &AwesomePlayer::onCheckAudioStatus); 209 210 mAudioStatusEventPending = false; 211 212 reset(); 213} 214 215AwesomePlayer::~AwesomePlayer() { 216 if (mQueueStarted) { 217 mQueue.stop(); 218 } 219 220 reset(); 221 222 mClient.disconnect(); 223} 224 225void AwesomePlayer::cancelPlayerEvents(bool keepNotifications) { 226 mQueue.cancelEvent(mVideoEvent->eventID()); 227 mVideoEventPending = false; 228 mQueue.cancelEvent(mVideoLagEvent->eventID()); 229 mVideoLagEventPending = false; 230 231 if (!keepNotifications) { 232 mQueue.cancelEvent(mStreamDoneEvent->eventID()); 233 mStreamDoneEventPending = false; 234 mQueue.cancelEvent(mCheckAudioStatusEvent->eventID()); 235 mAudioStatusEventPending = false; 236 237 mQueue.cancelEvent(mBufferingEvent->eventID()); 238 mBufferingEventPending = false; 239 } 240} 241 242void AwesomePlayer::setListener(const wp<MediaPlayerBase> &listener) { 243 Mutex::Autolock autoLock(mLock); 244 mListener = listener; 245} 246 247void AwesomePlayer::setUID(uid_t uid) { 248 ALOGV("AwesomePlayer running on behalf of uid %d", uid); 249 250 mUID = uid; 251 mUIDValid = true; 252} 253 254status_t AwesomePlayer::setDataSource( 255 const char *uri, const KeyedVector<String8, String8> *headers) { 256 Mutex::Autolock autoLock(mLock); 257 return setDataSource_l(uri, headers); 258} 259 260status_t AwesomePlayer::setDataSource_l( 261 const char *uri, const KeyedVector<String8, String8> *headers) { 262 reset_l(); 263 264 mUri = uri; 265 266 if (headers) { 267 mUriHeaders = *headers; 268 269 ssize_t index = mUriHeaders.indexOfKey(String8("x-hide-urls-from-log")); 270 if (index >= 0) { 271 // Browser is in "incognito" mode, suppress logging URLs. 272 273 // This isn't something that should be passed to the server. 274 mUriHeaders.removeItemsAt(index); 275 276 modifyFlags(INCOGNITO, SET); 277 } 278 } 279 280 if (!(mFlags & INCOGNITO)) { 281 ALOGI("setDataSource_l('%s')", mUri.string()); 282 } else { 283 ALOGI("setDataSource_l(URL suppressed)"); 284 } 285 286 // The actual work will be done during preparation in the call to 287 // ::finishSetDataSource_l to avoid blocking the calling thread in 288 // setDataSource for any significant time. 289 290 { 291 Mutex::Autolock autoLock(mStatsLock); 292 mStats.mFd = -1; 293 mStats.mURI = mUri; 294 } 295 296 return OK; 297} 298 299status_t AwesomePlayer::setDataSource( 300 int fd, int64_t offset, int64_t length) { 301 Mutex::Autolock autoLock(mLock); 302 303 reset_l(); 304 305 sp<DataSource> dataSource = new FileSource(fd, offset, length); 306 307 status_t err = dataSource->initCheck(); 308 309 if (err != OK) { 310 return err; 311 } 312 313 mFileSource = dataSource; 314 315 { 316 Mutex::Autolock autoLock(mStatsLock); 317 mStats.mFd = fd; 318 mStats.mURI = String8(); 319 } 320 321 return setDataSource_l(dataSource); 322} 323 324status_t AwesomePlayer::setDataSource(const sp<IStreamSource> &source) { 325 return INVALID_OPERATION; 326} 327 328status_t AwesomePlayer::setDataSource_l( 329 const sp<DataSource> &dataSource) { 330 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 331 332 if (extractor == NULL) { 333 return UNKNOWN_ERROR; 334 } 335 336 if (extractor->getDrmFlag()) { 337 checkDrmStatus(dataSource); 338 } 339 340 return setDataSource_l(extractor); 341} 342 343void AwesomePlayer::checkDrmStatus(const sp<DataSource>& dataSource) { 344 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient); 345 if (mDecryptHandle != NULL) { 346 CHECK(mDrmManagerClient); 347 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) { 348 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 349 } 350 } 351} 352 353status_t AwesomePlayer::setDataSource_l(const sp<MediaExtractor> &extractor) { 354 // Attempt to approximate overall stream bitrate by summing all 355 // tracks' individual bitrates, if not all of them advertise bitrate, 356 // we have to fail. 357 358 int64_t totalBitRate = 0; 359 360 mExtractor = extractor; 361 for (size_t i = 0; i < extractor->countTracks(); ++i) { 362 sp<MetaData> meta = extractor->getTrackMetaData(i); 363 364 int32_t bitrate; 365 if (!meta->findInt32(kKeyBitRate, &bitrate)) { 366 const char *mime; 367 CHECK(meta->findCString(kKeyMIMEType, &mime)); 368 ALOGV("track of type '%s' does not publish bitrate", mime); 369 370 totalBitRate = -1; 371 break; 372 } 373 374 totalBitRate += bitrate; 375 } 376 377 mBitrate = totalBitRate; 378 379 ALOGV("mBitrate = %lld bits/sec", mBitrate); 380 381 { 382 Mutex::Autolock autoLock(mStatsLock); 383 mStats.mBitrate = mBitrate; 384 mStats.mTracks.clear(); 385 mStats.mAudioTrackIndex = -1; 386 mStats.mVideoTrackIndex = -1; 387 } 388 389 bool haveAudio = false; 390 bool haveVideo = false; 391 for (size_t i = 0; i < extractor->countTracks(); ++i) { 392 sp<MetaData> meta = extractor->getTrackMetaData(i); 393 394 const char *_mime; 395 CHECK(meta->findCString(kKeyMIMEType, &_mime)); 396 397 String8 mime = String8(_mime); 398 399 if (!haveVideo && !strncasecmp(mime.string(), "video/", 6)) { 400 setVideoSource(extractor->getTrack(i)); 401 haveVideo = true; 402 403 // Set the presentation/display size 404 int32_t displayWidth, displayHeight; 405 bool success = meta->findInt32(kKeyDisplayWidth, &displayWidth); 406 if (success) { 407 success = meta->findInt32(kKeyDisplayHeight, &displayHeight); 408 } 409 if (success) { 410 mDisplayWidth = displayWidth; 411 mDisplayHeight = displayHeight; 412 } 413 414 { 415 Mutex::Autolock autoLock(mStatsLock); 416 mStats.mVideoTrackIndex = mStats.mTracks.size(); 417 mStats.mTracks.push(); 418 TrackStat *stat = 419 &mStats.mTracks.editItemAt(mStats.mVideoTrackIndex); 420 stat->mMIME = mime.string(); 421 } 422 } else if (!haveAudio && !strncasecmp(mime.string(), "audio/", 6)) { 423 setAudioSource(extractor->getTrack(i)); 424 haveAudio = true; 425 426 { 427 Mutex::Autolock autoLock(mStatsLock); 428 mStats.mAudioTrackIndex = mStats.mTracks.size(); 429 mStats.mTracks.push(); 430 TrackStat *stat = 431 &mStats.mTracks.editItemAt(mStats.mAudioTrackIndex); 432 stat->mMIME = mime.string(); 433 } 434 435 if (!strcasecmp(mime.string(), MEDIA_MIMETYPE_AUDIO_VORBIS)) { 436 // Only do this for vorbis audio, none of the other audio 437 // formats even support this ringtone specific hack and 438 // retrieving the metadata on some extractors may turn out 439 // to be very expensive. 440 sp<MetaData> fileMeta = extractor->getMetaData(); 441 int32_t loop; 442 if (fileMeta != NULL 443 && fileMeta->findInt32(kKeyAutoLoop, &loop) && loop != 0) { 444 modifyFlags(AUTO_LOOPING, SET); 445 } 446 } 447 } else if (!strcasecmp(mime.string(), MEDIA_MIMETYPE_TEXT_3GPP)) { 448 addTextSource(i, extractor->getTrack(i)); 449 } 450 } 451 452 if (!haveAudio && !haveVideo) { 453 return UNKNOWN_ERROR; 454 } 455 456 mExtractorFlags = extractor->flags(); 457 458 return OK; 459} 460 461void AwesomePlayer::reset() { 462 Mutex::Autolock autoLock(mLock); 463 reset_l(); 464} 465 466void AwesomePlayer::reset_l() { 467 mDisplayWidth = 0; 468 mDisplayHeight = 0; 469 470 if (mDecryptHandle != NULL) { 471 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 472 Playback::STOP, 0); 473 mDecryptHandle = NULL; 474 mDrmManagerClient = NULL; 475 } 476 477 if (mFlags & PLAYING) { 478 uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder; 479 if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) { 480 params |= IMediaPlayerService::kBatteryDataTrackAudio; 481 } 482 if (mVideoSource != NULL) { 483 params |= IMediaPlayerService::kBatteryDataTrackVideo; 484 } 485 addBatteryData(params); 486 } 487 488 if (mFlags & PREPARING) { 489 modifyFlags(PREPARE_CANCELLED, SET); 490 if (mConnectingDataSource != NULL) { 491 ALOGI("interrupting the connection process"); 492 mConnectingDataSource->disconnect(); 493 } 494 495 if (mFlags & PREPARING_CONNECTED) { 496 // We are basically done preparing, we're just buffering 497 // enough data to start playback, we can safely interrupt that. 498 finishAsyncPrepare_l(); 499 } 500 } 501 502 while (mFlags & PREPARING) { 503 mPreparedCondition.wait(mLock); 504 } 505 506 cancelPlayerEvents(); 507 508 mWVMExtractor.clear(); 509 mCachedSource.clear(); 510 mAudioTrack.clear(); 511 mVideoTrack.clear(); 512 mExtractor.clear(); 513 514 // Shutdown audio first, so that the respone to the reset request 515 // appears to happen instantaneously as far as the user is concerned 516 // If we did this later, audio would continue playing while we 517 // shutdown the video-related resources and the player appear to 518 // not be as responsive to a reset request. 519 if ((mAudioPlayer == NULL || !(mFlags & AUDIOPLAYER_STARTED)) 520 && mAudioSource != NULL) { 521 // If we had an audio player, it would have effectively 522 // taken possession of the audio source and stopped it when 523 // _it_ is stopped. Otherwise this is still our responsibility. 524 mAudioSource->stop(); 525 } 526 mAudioSource.clear(); 527 528 mTimeSource = NULL; 529 530 delete mAudioPlayer; 531 mAudioPlayer = NULL; 532 533 if (mTextDriver != NULL) { 534 delete mTextDriver; 535 mTextDriver = NULL; 536 } 537 538 mVideoRenderer.clear(); 539 540 if (mVideoSource != NULL) { 541 shutdownVideoDecoder_l(); 542 } 543 544 mDurationUs = -1; 545 modifyFlags(0, ASSIGN); 546 mExtractorFlags = 0; 547 mTimeSourceDeltaUs = 0; 548 mVideoTimeUs = 0; 549 550 mSeeking = NO_SEEK; 551 mSeekNotificationSent = true; 552 mSeekTimeUs = 0; 553 554 mUri.setTo(""); 555 mUriHeaders.clear(); 556 557 mFileSource.clear(); 558 559 mBitrate = -1; 560 mLastVideoTimeUs = -1; 561 562 { 563 Mutex::Autolock autoLock(mStatsLock); 564 mStats.mFd = -1; 565 mStats.mURI = String8(); 566 mStats.mBitrate = -1; 567 mStats.mAudioTrackIndex = -1; 568 mStats.mVideoTrackIndex = -1; 569 mStats.mNumVideoFramesDecoded = 0; 570 mStats.mNumVideoFramesDropped = 0; 571 mStats.mVideoWidth = -1; 572 mStats.mVideoHeight = -1; 573 mStats.mFlags = 0; 574 mStats.mTracks.clear(); 575 } 576 577 mWatchForAudioSeekComplete = false; 578 mWatchForAudioEOS = false; 579} 580 581void AwesomePlayer::notifyListener_l(int msg, int ext1, int ext2) { 582 if (mListener != NULL) { 583 sp<MediaPlayerBase> listener = mListener.promote(); 584 585 if (listener != NULL) { 586 listener->sendEvent(msg, ext1, ext2); 587 } 588 } 589} 590 591bool AwesomePlayer::getBitrate(int64_t *bitrate) { 592 off64_t size; 593 if (mDurationUs >= 0 && mCachedSource != NULL 594 && mCachedSource->getSize(&size) == OK) { 595 *bitrate = size * 8000000ll / mDurationUs; // in bits/sec 596 return true; 597 } 598 599 if (mBitrate >= 0) { 600 *bitrate = mBitrate; 601 return true; 602 } 603 604 *bitrate = 0; 605 606 return false; 607} 608 609// Returns true iff cached duration is available/applicable. 610bool AwesomePlayer::getCachedDuration_l(int64_t *durationUs, bool *eos) { 611 int64_t bitrate; 612 613 if (mCachedSource != NULL && getBitrate(&bitrate)) { 614 status_t finalStatus; 615 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); 616 *durationUs = cachedDataRemaining * 8000000ll / bitrate; 617 *eos = (finalStatus != OK); 618 return true; 619 } else if (mWVMExtractor != NULL) { 620 status_t finalStatus; 621 *durationUs = mWVMExtractor->getCachedDurationUs(&finalStatus); 622 *eos = (finalStatus != OK); 623 return true; 624 } 625 626 return false; 627} 628 629void AwesomePlayer::ensureCacheIsFetching_l() { 630 if (mCachedSource != NULL) { 631 mCachedSource->resumeFetchingIfNecessary(); 632 } 633} 634 635void AwesomePlayer::onVideoLagUpdate() { 636 Mutex::Autolock autoLock(mLock); 637 if (!mVideoLagEventPending) { 638 return; 639 } 640 mVideoLagEventPending = false; 641 642 int64_t audioTimeUs = mAudioPlayer->getMediaTimeUs(); 643 int64_t videoLateByUs = audioTimeUs - mVideoTimeUs; 644 645 if (!(mFlags & VIDEO_AT_EOS) && videoLateByUs > 300000ll) { 646 ALOGV("video late by %lld ms.", videoLateByUs / 1000ll); 647 648 notifyListener_l( 649 MEDIA_INFO, 650 MEDIA_INFO_VIDEO_TRACK_LAGGING, 651 videoLateByUs / 1000ll); 652 } 653 654 postVideoLagEvent_l(); 655} 656 657void AwesomePlayer::onBufferingUpdate() { 658 Mutex::Autolock autoLock(mLock); 659 if (!mBufferingEventPending) { 660 return; 661 } 662 mBufferingEventPending = false; 663 664 if (mCachedSource != NULL) { 665 status_t finalStatus; 666 size_t cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); 667 bool eos = (finalStatus != OK); 668 669 if (eos) { 670 if (finalStatus == ERROR_END_OF_STREAM) { 671 notifyListener_l(MEDIA_BUFFERING_UPDATE, 100); 672 } 673 if (mFlags & PREPARING) { 674 ALOGV("cache has reached EOS, prepare is done."); 675 finishAsyncPrepare_l(); 676 } 677 } else { 678 int64_t bitrate; 679 if (getBitrate(&bitrate)) { 680 size_t cachedSize = mCachedSource->cachedSize(); 681 int64_t cachedDurationUs = cachedSize * 8000000ll / bitrate; 682 683 int percentage = 100.0 * (double)cachedDurationUs / mDurationUs; 684 if (percentage > 100) { 685 percentage = 100; 686 } 687 688 notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage); 689 } else { 690 // We don't know the bitrate of the stream, use absolute size 691 // limits to maintain the cache. 692 693 if ((mFlags & PLAYING) && !eos 694 && (cachedDataRemaining < kLowWaterMarkBytes)) { 695 ALOGI("cache is running low (< %d) , pausing.", 696 kLowWaterMarkBytes); 697 modifyFlags(CACHE_UNDERRUN, SET); 698 pause_l(); 699 ensureCacheIsFetching_l(); 700 sendCacheStats(); 701 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); 702 } else if (eos || cachedDataRemaining > kHighWaterMarkBytes) { 703 if (mFlags & CACHE_UNDERRUN) { 704 ALOGI("cache has filled up (> %d), resuming.", 705 kHighWaterMarkBytes); 706 modifyFlags(CACHE_UNDERRUN, CLEAR); 707 play_l(); 708 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); 709 } else if (mFlags & PREPARING) { 710 ALOGV("cache has filled up (> %d), prepare is done", 711 kHighWaterMarkBytes); 712 finishAsyncPrepare_l(); 713 } 714 } 715 } 716 } 717 } else if (mWVMExtractor != NULL) { 718 status_t finalStatus; 719 720 int64_t cachedDurationUs 721 = mWVMExtractor->getCachedDurationUs(&finalStatus); 722 723 bool eos = (finalStatus != OK); 724 725 if (eos) { 726 if (finalStatus == ERROR_END_OF_STREAM) { 727 notifyListener_l(MEDIA_BUFFERING_UPDATE, 100); 728 } 729 if (mFlags & PREPARING) { 730 ALOGV("cache has reached EOS, prepare is done."); 731 finishAsyncPrepare_l(); 732 } 733 } else { 734 int percentage = 100.0 * (double)cachedDurationUs / mDurationUs; 735 if (percentage > 100) { 736 percentage = 100; 737 } 738 739 notifyListener_l(MEDIA_BUFFERING_UPDATE, percentage); 740 } 741 } 742 743 int64_t cachedDurationUs; 744 bool eos; 745 if (getCachedDuration_l(&cachedDurationUs, &eos)) { 746 ALOGV("cachedDurationUs = %.2f secs, eos=%d", 747 cachedDurationUs / 1E6, eos); 748 749 if ((mFlags & PLAYING) && !eos 750 && (cachedDurationUs < kLowWaterMarkUs)) { 751 ALOGI("cache is running low (%.2f secs) , pausing.", 752 cachedDurationUs / 1E6); 753 modifyFlags(CACHE_UNDERRUN, SET); 754 pause_l(); 755 ensureCacheIsFetching_l(); 756 sendCacheStats(); 757 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_START); 758 } else if (eos || cachedDurationUs > kHighWaterMarkUs) { 759 if (mFlags & CACHE_UNDERRUN) { 760 ALOGI("cache has filled up (%.2f secs), resuming.", 761 cachedDurationUs / 1E6); 762 modifyFlags(CACHE_UNDERRUN, CLEAR); 763 play_l(); 764 notifyListener_l(MEDIA_INFO, MEDIA_INFO_BUFFERING_END); 765 } else if (mFlags & PREPARING) { 766 ALOGV("cache has filled up (%.2f secs), prepare is done", 767 cachedDurationUs / 1E6); 768 finishAsyncPrepare_l(); 769 } 770 } 771 } 772 773 postBufferingEvent_l(); 774} 775 776void AwesomePlayer::sendCacheStats() { 777 sp<MediaPlayerBase> listener = mListener.promote(); 778 if (listener != NULL && mCachedSource != NULL) { 779 int32_t kbps = 0; 780 status_t err = mCachedSource->getEstimatedBandwidthKbps(&kbps); 781 if (err == OK) { 782 listener->sendEvent( 783 MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 784 } 785 } 786} 787 788void AwesomePlayer::onStreamDone() { 789 // Posted whenever any stream finishes playing. 790 791 Mutex::Autolock autoLock(mLock); 792 if (!mStreamDoneEventPending) { 793 return; 794 } 795 mStreamDoneEventPending = false; 796 797 if (mStreamDoneStatus != ERROR_END_OF_STREAM) { 798 ALOGV("MEDIA_ERROR %d", mStreamDoneStatus); 799 800 notifyListener_l( 801 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus); 802 803 pause_l(true /* at eos */); 804 805 modifyFlags(AT_EOS, SET); 806 return; 807 } 808 809 const bool allDone = 810 (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS)) 811 && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS)); 812 813 if (!allDone) { 814 return; 815 } 816 817 if ((mFlags & LOOPING) 818 || ((mFlags & AUTO_LOOPING) 819 && (mAudioSink == NULL || mAudioSink->realtime()))) { 820 // Don't AUTO_LOOP if we're being recorded, since that cannot be 821 // turned off and recording would go on indefinitely. 822 823 seekTo_l(0); 824 825 if (mVideoSource != NULL) { 826 postVideoEvent_l(); 827 } 828 } else { 829 ALOGV("MEDIA_PLAYBACK_COMPLETE"); 830 notifyListener_l(MEDIA_PLAYBACK_COMPLETE); 831 832 pause_l(true /* at eos */); 833 834 modifyFlags(AT_EOS, SET); 835 } 836} 837 838status_t AwesomePlayer::play() { 839 Mutex::Autolock autoLock(mLock); 840 841 modifyFlags(CACHE_UNDERRUN, CLEAR); 842 843 return play_l(); 844} 845 846status_t AwesomePlayer::play_l() { 847 modifyFlags(SEEK_PREVIEW, CLEAR); 848 849 if (mFlags & PLAYING) { 850 return OK; 851 } 852 853 if (!(mFlags & PREPARED)) { 854 status_t err = prepare_l(); 855 856 if (err != OK) { 857 return err; 858 } 859 } 860 861 modifyFlags(PLAYING, SET); 862 modifyFlags(FIRST_FRAME, SET); 863 864 if (mDecryptHandle != NULL) { 865 int64_t position; 866 getPosition(&position); 867 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 868 Playback::START, position / 1000); 869 } 870 871 if (mAudioSource != NULL) { 872 if (mAudioPlayer == NULL) { 873 if (mAudioSink != NULL) { 874 bool allowDeepBuffering; 875 int64_t cachedDurationUs; 876 bool eos; 877 if (mVideoSource == NULL && (mDurationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US || 878 getCachedDuration_l(&cachedDurationUs, &eos) && 879 cachedDurationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) { 880 allowDeepBuffering = true; 881 } else { 882 allowDeepBuffering = false; 883 } 884 885 mAudioPlayer = new AudioPlayer(mAudioSink, allowDeepBuffering, this); 886 mAudioPlayer->setSource(mAudioSource); 887 888 mTimeSource = mAudioPlayer; 889 890 // If there was a seek request before we ever started, 891 // honor the request now. 892 // Make sure to do this before starting the audio player 893 // to avoid a race condition. 894 seekAudioIfNecessary_l(); 895 } 896 } 897 898 CHECK(!(mFlags & AUDIO_RUNNING)); 899 900 if (mVideoSource == NULL) { 901 // We don't want to post an error notification at this point, 902 // the error returned from MediaPlayer::start() will suffice. 903 904 status_t err = startAudioPlayer_l( 905 false /* sendErrorNotification */); 906 907 if (err != OK) { 908 delete mAudioPlayer; 909 mAudioPlayer = NULL; 910 911 modifyFlags((PLAYING | FIRST_FRAME), CLEAR); 912 913 if (mDecryptHandle != NULL) { 914 mDrmManagerClient->setPlaybackStatus( 915 mDecryptHandle, Playback::STOP, 0); 916 } 917 918 return err; 919 } 920 } 921 } 922 923 if (mTimeSource == NULL && mAudioPlayer == NULL) { 924 mTimeSource = &mSystemTimeSource; 925 } 926 927 if (mVideoSource != NULL) { 928 // Kick off video playback 929 postVideoEvent_l(); 930 931 if (mAudioSource != NULL && mVideoSource != NULL) { 932 postVideoLagEvent_l(); 933 } 934 } 935 936 if (mFlags & AT_EOS) { 937 // Legacy behaviour, if a stream finishes playing and then 938 // is started again, we play from the start... 939 seekTo_l(0); 940 } 941 942 uint32_t params = IMediaPlayerService::kBatteryDataCodecStarted 943 | IMediaPlayerService::kBatteryDataTrackDecoder; 944 if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) { 945 params |= IMediaPlayerService::kBatteryDataTrackAudio; 946 } 947 if (mVideoSource != NULL) { 948 params |= IMediaPlayerService::kBatteryDataTrackVideo; 949 } 950 addBatteryData(params); 951 952 return OK; 953} 954 955status_t AwesomePlayer::startAudioPlayer_l(bool sendErrorNotification) { 956 CHECK(!(mFlags & AUDIO_RUNNING)); 957 958 if (mAudioSource == NULL || mAudioPlayer == NULL) { 959 return OK; 960 } 961 962 if (!(mFlags & AUDIOPLAYER_STARTED)) { 963 modifyFlags(AUDIOPLAYER_STARTED, SET); 964 965 bool wasSeeking = mAudioPlayer->isSeeking(); 966 967 // We've already started the MediaSource in order to enable 968 // the prefetcher to read its data. 969 status_t err = mAudioPlayer->start( 970 true /* sourceAlreadyStarted */); 971 972 if (err != OK) { 973 if (sendErrorNotification) { 974 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 975 } 976 977 return err; 978 } 979 980 if (wasSeeking) { 981 CHECK(!mAudioPlayer->isSeeking()); 982 983 // We will have finished the seek while starting the audio player. 984 postAudioSeekComplete(); 985 } 986 } else { 987 mAudioPlayer->resume(); 988 } 989 990 modifyFlags(AUDIO_RUNNING, SET); 991 992 mWatchForAudioEOS = true; 993 994 return OK; 995} 996 997void AwesomePlayer::notifyVideoSize_l() { 998 sp<MetaData> meta = mVideoSource->getFormat(); 999 1000 int32_t cropLeft, cropTop, cropRight, cropBottom; 1001 if (!meta->findRect( 1002 kKeyCropRect, &cropLeft, &cropTop, &cropRight, &cropBottom)) { 1003 int32_t width, height; 1004 CHECK(meta->findInt32(kKeyWidth, &width)); 1005 CHECK(meta->findInt32(kKeyHeight, &height)); 1006 1007 cropLeft = cropTop = 0; 1008 cropRight = width - 1; 1009 cropBottom = height - 1; 1010 1011 ALOGV("got dimensions only %d x %d", width, height); 1012 } else { 1013 ALOGV("got crop rect %d, %d, %d, %d", 1014 cropLeft, cropTop, cropRight, cropBottom); 1015 } 1016 1017 int32_t displayWidth; 1018 if (meta->findInt32(kKeyDisplayWidth, &displayWidth)) { 1019 ALOGV("Display width changed (%d=>%d)", mDisplayWidth, displayWidth); 1020 mDisplayWidth = displayWidth; 1021 } 1022 int32_t displayHeight; 1023 if (meta->findInt32(kKeyDisplayHeight, &displayHeight)) { 1024 ALOGV("Display height changed (%d=>%d)", mDisplayHeight, displayHeight); 1025 mDisplayHeight = displayHeight; 1026 } 1027 1028 int32_t usableWidth = cropRight - cropLeft + 1; 1029 int32_t usableHeight = cropBottom - cropTop + 1; 1030 if (mDisplayWidth != 0) { 1031 usableWidth = mDisplayWidth; 1032 } 1033 if (mDisplayHeight != 0) { 1034 usableHeight = mDisplayHeight; 1035 } 1036 1037 { 1038 Mutex::Autolock autoLock(mStatsLock); 1039 mStats.mVideoWidth = usableWidth; 1040 mStats.mVideoHeight = usableHeight; 1041 } 1042 1043 int32_t rotationDegrees; 1044 if (!mVideoTrack->getFormat()->findInt32( 1045 kKeyRotation, &rotationDegrees)) { 1046 rotationDegrees = 0; 1047 } 1048 1049 if (rotationDegrees == 90 || rotationDegrees == 270) { 1050 notifyListener_l( 1051 MEDIA_SET_VIDEO_SIZE, usableHeight, usableWidth); 1052 } else { 1053 notifyListener_l( 1054 MEDIA_SET_VIDEO_SIZE, usableWidth, usableHeight); 1055 } 1056} 1057 1058void AwesomePlayer::initRenderer_l() { 1059 if (mNativeWindow == NULL) { 1060 return; 1061 } 1062 1063 sp<MetaData> meta = mVideoSource->getFormat(); 1064 1065 int32_t format; 1066 const char *component; 1067 int32_t decodedWidth, decodedHeight; 1068 CHECK(meta->findInt32(kKeyColorFormat, &format)); 1069 CHECK(meta->findCString(kKeyDecoderComponent, &component)); 1070 CHECK(meta->findInt32(kKeyWidth, &decodedWidth)); 1071 CHECK(meta->findInt32(kKeyHeight, &decodedHeight)); 1072 1073 int32_t rotationDegrees; 1074 if (!mVideoTrack->getFormat()->findInt32( 1075 kKeyRotation, &rotationDegrees)) { 1076 rotationDegrees = 0; 1077 } 1078 1079 mVideoRenderer.clear(); 1080 1081 // Must ensure that mVideoRenderer's destructor is actually executed 1082 // before creating a new one. 1083 IPCThreadState::self()->flushCommands(); 1084 1085 // Even if set scaling mode fails, we will continue anyway 1086 setVideoScalingMode_l(mVideoScalingMode); 1087 if (USE_SURFACE_ALLOC 1088 && !strncmp(component, "OMX.", 4) 1089 && strncmp(component, "OMX.google.", 11) 1090 && strcmp(component, "OMX.Nvidia.mpeg2v.decode")) { 1091 // Hardware decoders avoid the CPU color conversion by decoding 1092 // directly to ANativeBuffers, so we must use a renderer that 1093 // just pushes those buffers to the ANativeWindow. 1094 mVideoRenderer = 1095 new AwesomeNativeWindowRenderer(mNativeWindow, rotationDegrees); 1096 } else { 1097 // Other decoders are instantiated locally and as a consequence 1098 // allocate their buffers in local address space. This renderer 1099 // then performs a color conversion and copy to get the data 1100 // into the ANativeBuffer. 1101 mVideoRenderer = new AwesomeLocalRenderer(mNativeWindow, meta); 1102 } 1103} 1104 1105status_t AwesomePlayer::pause() { 1106 Mutex::Autolock autoLock(mLock); 1107 1108 modifyFlags(CACHE_UNDERRUN, CLEAR); 1109 1110 return pause_l(); 1111} 1112 1113status_t AwesomePlayer::pause_l(bool at_eos) { 1114 if (!(mFlags & PLAYING)) { 1115 return OK; 1116 } 1117 1118 cancelPlayerEvents(true /* keepNotifications */); 1119 1120 if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { 1121 if (at_eos) { 1122 // If we played the audio stream to completion we 1123 // want to make sure that all samples remaining in the audio 1124 // track's queue are played out. 1125 mAudioPlayer->pause(true /* playPendingSamples */); 1126 } else { 1127 mAudioPlayer->pause(); 1128 } 1129 1130 modifyFlags(AUDIO_RUNNING, CLEAR); 1131 } 1132 1133 if (mFlags & TEXTPLAYER_INITIALIZED) { 1134 mTextDriver->pause(); 1135 modifyFlags(TEXT_RUNNING, CLEAR); 1136 } 1137 1138 modifyFlags(PLAYING, CLEAR); 1139 1140 if (mDecryptHandle != NULL) { 1141 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1142 Playback::PAUSE, 0); 1143 } 1144 1145 uint32_t params = IMediaPlayerService::kBatteryDataTrackDecoder; 1146 if ((mAudioSource != NULL) && (mAudioSource != mAudioTrack)) { 1147 params |= IMediaPlayerService::kBatteryDataTrackAudio; 1148 } 1149 if (mVideoSource != NULL) { 1150 params |= IMediaPlayerService::kBatteryDataTrackVideo; 1151 } 1152 1153 addBatteryData(params); 1154 1155 return OK; 1156} 1157 1158bool AwesomePlayer::isPlaying() const { 1159 return (mFlags & PLAYING) || (mFlags & CACHE_UNDERRUN); 1160} 1161 1162status_t AwesomePlayer::setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) { 1163 Mutex::Autolock autoLock(mLock); 1164 1165 status_t err; 1166 if (surfaceTexture != NULL) { 1167 err = setNativeWindow_l(new SurfaceTextureClient(surfaceTexture)); 1168 } else { 1169 err = setNativeWindow_l(NULL); 1170 } 1171 1172 return err; 1173} 1174 1175void AwesomePlayer::shutdownVideoDecoder_l() { 1176 if (mVideoBuffer) { 1177 mVideoBuffer->release(); 1178 mVideoBuffer = NULL; 1179 } 1180 1181 mVideoSource->stop(); 1182 1183 // The following hack is necessary to ensure that the OMX 1184 // component is completely released by the time we may try 1185 // to instantiate it again. 1186 wp<MediaSource> tmp = mVideoSource; 1187 mVideoSource.clear(); 1188 while (tmp.promote() != NULL) { 1189 usleep(1000); 1190 } 1191 IPCThreadState::self()->flushCommands(); 1192 ALOGV("video decoder shutdown completed"); 1193} 1194 1195status_t AwesomePlayer::setNativeWindow_l(const sp<ANativeWindow> &native) { 1196 mNativeWindow = native; 1197 1198 if (mVideoSource == NULL) { 1199 return OK; 1200 } 1201 1202 ALOGV("attempting to reconfigure to use new surface"); 1203 1204 bool wasPlaying = (mFlags & PLAYING) != 0; 1205 1206 pause_l(); 1207 mVideoRenderer.clear(); 1208 1209 shutdownVideoDecoder_l(); 1210 1211 status_t err = initVideoDecoder(); 1212 1213 if (err != OK) { 1214 ALOGE("failed to reinstantiate video decoder after surface change."); 1215 return err; 1216 } 1217 1218 if (mLastVideoTimeUs >= 0) { 1219 mSeeking = SEEK; 1220 mSeekTimeUs = mLastVideoTimeUs; 1221 modifyFlags((AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS), CLEAR); 1222 } 1223 1224 if (wasPlaying) { 1225 play_l(); 1226 } 1227 1228 return OK; 1229} 1230 1231void AwesomePlayer::setAudioSink( 1232 const sp<MediaPlayerBase::AudioSink> &audioSink) { 1233 Mutex::Autolock autoLock(mLock); 1234 1235 mAudioSink = audioSink; 1236} 1237 1238status_t AwesomePlayer::setLooping(bool shouldLoop) { 1239 Mutex::Autolock autoLock(mLock); 1240 1241 modifyFlags(LOOPING, CLEAR); 1242 1243 if (shouldLoop) { 1244 modifyFlags(LOOPING, SET); 1245 } 1246 1247 return OK; 1248} 1249 1250status_t AwesomePlayer::getDuration(int64_t *durationUs) { 1251 Mutex::Autolock autoLock(mMiscStateLock); 1252 1253 if (mDurationUs < 0) { 1254 return UNKNOWN_ERROR; 1255 } 1256 1257 *durationUs = mDurationUs; 1258 1259 return OK; 1260} 1261 1262status_t AwesomePlayer::getPosition(int64_t *positionUs) { 1263 if (mSeeking != NO_SEEK) { 1264 *positionUs = mSeekTimeUs; 1265 } else if (mVideoSource != NULL 1266 && (mAudioPlayer == NULL || !(mFlags & VIDEO_AT_EOS))) { 1267 Mutex::Autolock autoLock(mMiscStateLock); 1268 *positionUs = mVideoTimeUs; 1269 } else if (mAudioPlayer != NULL) { 1270 *positionUs = mAudioPlayer->getMediaTimeUs(); 1271 } else { 1272 *positionUs = 0; 1273 } 1274 1275 return OK; 1276} 1277 1278status_t AwesomePlayer::seekTo(int64_t timeUs) { 1279 if (mExtractorFlags & MediaExtractor::CAN_SEEK) { 1280 Mutex::Autolock autoLock(mLock); 1281 return seekTo_l(timeUs); 1282 } 1283 1284 return OK; 1285} 1286 1287status_t AwesomePlayer::seekTo_l(int64_t timeUs) { 1288 if (mFlags & CACHE_UNDERRUN) { 1289 modifyFlags(CACHE_UNDERRUN, CLEAR); 1290 play_l(); 1291 } 1292 1293 if ((mFlags & PLAYING) && mVideoSource != NULL && (mFlags & VIDEO_AT_EOS)) { 1294 // Video playback completed before, there's no pending 1295 // video event right now. In order for this new seek 1296 // to be honored, we need to post one. 1297 1298 postVideoEvent_l(); 1299 } 1300 1301 mSeeking = SEEK; 1302 mSeekNotificationSent = false; 1303 mSeekTimeUs = timeUs; 1304 modifyFlags((AT_EOS | AUDIO_AT_EOS | VIDEO_AT_EOS), CLEAR); 1305 1306 seekAudioIfNecessary_l(); 1307 1308 if (mFlags & TEXTPLAYER_INITIALIZED) { 1309 mTextDriver->seekToAsync(mSeekTimeUs); 1310 } 1311 1312 if (!(mFlags & PLAYING)) { 1313 ALOGV("seeking while paused, sending SEEK_COMPLETE notification" 1314 " immediately."); 1315 1316 notifyListener_l(MEDIA_SEEK_COMPLETE); 1317 mSeekNotificationSent = true; 1318 1319 if ((mFlags & PREPARED) && mVideoSource != NULL) { 1320 modifyFlags(SEEK_PREVIEW, SET); 1321 postVideoEvent_l(); 1322 } 1323 } 1324 1325 return OK; 1326} 1327 1328void AwesomePlayer::seekAudioIfNecessary_l() { 1329 if (mSeeking != NO_SEEK && mVideoSource == NULL && mAudioPlayer != NULL) { 1330 mAudioPlayer->seekTo(mSeekTimeUs); 1331 1332 mWatchForAudioSeekComplete = true; 1333 mWatchForAudioEOS = true; 1334 1335 if (mDecryptHandle != NULL) { 1336 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1337 Playback::PAUSE, 0); 1338 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1339 Playback::START, mSeekTimeUs / 1000); 1340 } 1341 } 1342} 1343 1344void AwesomePlayer::setAudioSource(sp<MediaSource> source) { 1345 CHECK(source != NULL); 1346 1347 mAudioTrack = source; 1348} 1349 1350void AwesomePlayer::addTextSource(size_t trackIndex, const sp<MediaSource>& source) { 1351 Mutex::Autolock autoLock(mLock); 1352 CHECK(source != NULL); 1353 1354 if (mTextDriver == NULL) { 1355 mTextDriver = new TimedTextDriver(mListener); 1356 } 1357 1358 mTextDriver->addInBandTextSource(trackIndex, source); 1359} 1360 1361status_t AwesomePlayer::initAudioDecoder() { 1362 sp<MetaData> meta = mAudioTrack->getFormat(); 1363 1364 const char *mime; 1365 CHECK(meta->findCString(kKeyMIMEType, &mime)); 1366 1367 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { 1368 mAudioSource = mAudioTrack; 1369 } else { 1370 mAudioSource = OMXCodec::Create( 1371 mClient.interface(), mAudioTrack->getFormat(), 1372 false, // createEncoder 1373 mAudioTrack); 1374 } 1375 1376 if (mAudioSource != NULL) { 1377 int64_t durationUs; 1378 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 1379 Mutex::Autolock autoLock(mMiscStateLock); 1380 if (mDurationUs < 0 || durationUs > mDurationUs) { 1381 mDurationUs = durationUs; 1382 } 1383 } 1384 1385 status_t err = mAudioSource->start(); 1386 1387 if (err != OK) { 1388 mAudioSource.clear(); 1389 return err; 1390 } 1391 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) { 1392 // For legacy reasons we're simply going to ignore the absence 1393 // of an audio decoder for QCELP instead of aborting playback 1394 // altogether. 1395 return OK; 1396 } 1397 1398 if (mAudioSource != NULL) { 1399 Mutex::Autolock autoLock(mStatsLock); 1400 TrackStat *stat = &mStats.mTracks.editItemAt(mStats.mAudioTrackIndex); 1401 const char *component; 1402 if (!mAudioSource->getFormat() 1403 ->findCString(kKeyDecoderComponent, &component)) { 1404 component = "none"; 1405 } 1406 1407 stat->mDecoderName = component; 1408 } 1409 1410 return mAudioSource != NULL ? OK : UNKNOWN_ERROR; 1411} 1412 1413void AwesomePlayer::setVideoSource(sp<MediaSource> source) { 1414 CHECK(source != NULL); 1415 1416 mVideoTrack = source; 1417} 1418 1419status_t AwesomePlayer::initVideoDecoder(uint32_t flags) { 1420 1421 // Either the application or the DRM system can independently say 1422 // that there must be a hardware-protected path to an external video sink. 1423 // For now we always require a hardware-protected path to external video sink 1424 // if content is DRMed, but eventually this could be optional per DRM agent. 1425 // When the application wants protection, then 1426 // (USE_SURFACE_ALLOC && (mSurface != 0) && 1427 // (mSurface->getFlags() & ISurfaceComposer::eProtectedByApp)) 1428 // will be true, but that part is already handled by SurfaceFlinger. 1429 1430#ifdef DEBUG_HDCP 1431 // For debugging, we allow a system property to control the protected usage. 1432 // In case of uninitialized or unexpected property, we default to "DRM only". 1433 bool setProtectionBit = false; 1434 char value[PROPERTY_VALUE_MAX]; 1435 if (property_get("persist.sys.hdcp_checking", value, NULL)) { 1436 if (!strcmp(value, "never")) { 1437 // nop 1438 } else if (!strcmp(value, "always")) { 1439 setProtectionBit = true; 1440 } else if (!strcmp(value, "drm-only")) { 1441 if (mDecryptHandle != NULL) { 1442 setProtectionBit = true; 1443 } 1444 // property value is empty, or unexpected value 1445 } else { 1446 if (mDecryptHandle != NULL) { 1447 setProtectionBit = true; 1448 } 1449 } 1450 // can' read property value 1451 } else { 1452 if (mDecryptHandle != NULL) { 1453 setProtectionBit = true; 1454 } 1455 } 1456 // note that usage bit is already cleared, so no need to clear it in the "else" case 1457 if (setProtectionBit) { 1458 flags |= OMXCodec::kEnableGrallocUsageProtected; 1459 } 1460#else 1461 if (mDecryptHandle != NULL) { 1462 flags |= OMXCodec::kEnableGrallocUsageProtected; 1463 } 1464#endif 1465 ALOGV("initVideoDecoder flags=0x%x", flags); 1466 mVideoSource = OMXCodec::Create( 1467 mClient.interface(), mVideoTrack->getFormat(), 1468 false, // createEncoder 1469 mVideoTrack, 1470 NULL, flags, USE_SURFACE_ALLOC ? mNativeWindow : NULL); 1471 1472 if (mVideoSource != NULL) { 1473 int64_t durationUs; 1474 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 1475 Mutex::Autolock autoLock(mMiscStateLock); 1476 if (mDurationUs < 0 || durationUs > mDurationUs) { 1477 mDurationUs = durationUs; 1478 } 1479 } 1480 1481 status_t err = mVideoSource->start(); 1482 1483 if (err != OK) { 1484 mVideoSource.clear(); 1485 return err; 1486 } 1487 } 1488 1489 if (mVideoSource != NULL) { 1490 const char *componentName; 1491 CHECK(mVideoSource->getFormat() 1492 ->findCString(kKeyDecoderComponent, &componentName)); 1493 1494 { 1495 Mutex::Autolock autoLock(mStatsLock); 1496 TrackStat *stat = &mStats.mTracks.editItemAt(mStats.mVideoTrackIndex); 1497 1498 stat->mDecoderName = componentName; 1499 } 1500 1501 static const char *kPrefix = "OMX.Nvidia."; 1502 static const char *kSuffix = ".decode"; 1503 static const size_t kSuffixLength = strlen(kSuffix); 1504 1505 size_t componentNameLength = strlen(componentName); 1506 1507 if (!strncmp(componentName, kPrefix, strlen(kPrefix)) 1508 && componentNameLength >= kSuffixLength 1509 && !strcmp(&componentName[ 1510 componentNameLength - kSuffixLength], kSuffix)) { 1511 modifyFlags(SLOW_DECODER_HACK, SET); 1512 } 1513 } 1514 1515 return mVideoSource != NULL ? OK : UNKNOWN_ERROR; 1516} 1517 1518void AwesomePlayer::finishSeekIfNecessary(int64_t videoTimeUs) { 1519 if (mSeeking == SEEK_VIDEO_ONLY) { 1520 mSeeking = NO_SEEK; 1521 return; 1522 } 1523 1524 if (mSeeking == NO_SEEK || (mFlags & SEEK_PREVIEW)) { 1525 return; 1526 } 1527 1528 if (mAudioPlayer != NULL) { 1529 ALOGV("seeking audio to %lld us (%.2f secs).", videoTimeUs, videoTimeUs / 1E6); 1530 1531 // If we don't have a video time, seek audio to the originally 1532 // requested seek time instead. 1533 1534 mAudioPlayer->seekTo(videoTimeUs < 0 ? mSeekTimeUs : videoTimeUs); 1535 mWatchForAudioSeekComplete = true; 1536 mWatchForAudioEOS = true; 1537 } else if (!mSeekNotificationSent) { 1538 // If we're playing video only, report seek complete now, 1539 // otherwise audio player will notify us later. 1540 notifyListener_l(MEDIA_SEEK_COMPLETE); 1541 mSeekNotificationSent = true; 1542 } 1543 1544 modifyFlags(FIRST_FRAME, SET); 1545 mSeeking = NO_SEEK; 1546 1547 if (mDecryptHandle != NULL) { 1548 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1549 Playback::PAUSE, 0); 1550 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, 1551 Playback::START, videoTimeUs / 1000); 1552 } 1553} 1554 1555void AwesomePlayer::onVideoEvent() { 1556 Mutex::Autolock autoLock(mLock); 1557 if (!mVideoEventPending) { 1558 // The event has been cancelled in reset_l() but had already 1559 // been scheduled for execution at that time. 1560 return; 1561 } 1562 mVideoEventPending = false; 1563 1564 if (mSeeking != NO_SEEK) { 1565 if (mVideoBuffer) { 1566 mVideoBuffer->release(); 1567 mVideoBuffer = NULL; 1568 } 1569 1570 if (mSeeking == SEEK && isStreamingHTTP() && mAudioSource != NULL 1571 && !(mFlags & SEEK_PREVIEW)) { 1572 // We're going to seek the video source first, followed by 1573 // the audio source. 1574 // In order to avoid jumps in the DataSource offset caused by 1575 // the audio codec prefetching data from the old locations 1576 // while the video codec is already reading data from the new 1577 // locations, we'll "pause" the audio source, causing it to 1578 // stop reading input data until a subsequent seek. 1579 1580 if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { 1581 mAudioPlayer->pause(); 1582 1583 modifyFlags(AUDIO_RUNNING, CLEAR); 1584 } 1585 mAudioSource->pause(); 1586 } 1587 } 1588 1589 if (!mVideoBuffer) { 1590 MediaSource::ReadOptions options; 1591 if (mSeeking != NO_SEEK) { 1592 ALOGV("seeking to %lld us (%.2f secs)", mSeekTimeUs, mSeekTimeUs / 1E6); 1593 1594 options.setSeekTo( 1595 mSeekTimeUs, 1596 mSeeking == SEEK_VIDEO_ONLY 1597 ? MediaSource::ReadOptions::SEEK_NEXT_SYNC 1598 : MediaSource::ReadOptions::SEEK_CLOSEST); 1599 } 1600 for (;;) { 1601 status_t err = mVideoSource->read(&mVideoBuffer, &options); 1602 options.clearSeekTo(); 1603 1604 if (err != OK) { 1605 CHECK(mVideoBuffer == NULL); 1606 1607 if (err == INFO_FORMAT_CHANGED) { 1608 ALOGV("VideoSource signalled format change."); 1609 1610 notifyVideoSize_l(); 1611 1612 if (mVideoRenderer != NULL) { 1613 mVideoRendererIsPreview = false; 1614 initRenderer_l(); 1615 } 1616 continue; 1617 } 1618 1619 // So video playback is complete, but we may still have 1620 // a seek request pending that needs to be applied 1621 // to the audio track. 1622 if (mSeeking != NO_SEEK) { 1623 ALOGV("video stream ended while seeking!"); 1624 } 1625 finishSeekIfNecessary(-1); 1626 1627 if (mAudioPlayer != NULL 1628 && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) { 1629 startAudioPlayer_l(); 1630 } 1631 1632 modifyFlags(VIDEO_AT_EOS, SET); 1633 postStreamDoneEvent_l(err); 1634 return; 1635 } 1636 1637 if (mVideoBuffer->range_length() == 0) { 1638 // Some decoders, notably the PV AVC software decoder 1639 // return spurious empty buffers that we just want to ignore. 1640 1641 mVideoBuffer->release(); 1642 mVideoBuffer = NULL; 1643 continue; 1644 } 1645 1646 break; 1647 } 1648 1649 { 1650 Mutex::Autolock autoLock(mStatsLock); 1651 ++mStats.mNumVideoFramesDecoded; 1652 } 1653 } 1654 1655 int64_t timeUs; 1656 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 1657 1658 mLastVideoTimeUs = timeUs; 1659 1660 if (mSeeking == SEEK_VIDEO_ONLY) { 1661 if (mSeekTimeUs > timeUs) { 1662 ALOGI("XXX mSeekTimeUs = %lld us, timeUs = %lld us", 1663 mSeekTimeUs, timeUs); 1664 } 1665 } 1666 1667 { 1668 Mutex::Autolock autoLock(mMiscStateLock); 1669 mVideoTimeUs = timeUs; 1670 } 1671 1672 SeekType wasSeeking = mSeeking; 1673 finishSeekIfNecessary(timeUs); 1674 1675 if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING | SEEK_PREVIEW))) { 1676 status_t err = startAudioPlayer_l(); 1677 if (err != OK) { 1678 ALOGE("Starting the audio player failed w/ err %d", err); 1679 return; 1680 } 1681 } 1682 1683 if ((mFlags & TEXTPLAYER_INITIALIZED) && !(mFlags & (TEXT_RUNNING | SEEK_PREVIEW))) { 1684 mTextDriver->start(); 1685 modifyFlags(TEXT_RUNNING, SET); 1686 } 1687 1688 TimeSource *ts = 1689 ((mFlags & AUDIO_AT_EOS) || !(mFlags & AUDIOPLAYER_STARTED)) 1690 ? &mSystemTimeSource : mTimeSource; 1691 1692 if (mFlags & FIRST_FRAME) { 1693 modifyFlags(FIRST_FRAME, CLEAR); 1694 mSinceLastDropped = 0; 1695 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs; 1696 } 1697 1698 int64_t realTimeUs, mediaTimeUs; 1699 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL 1700 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) { 1701 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs; 1702 } 1703 1704 if (wasSeeking == SEEK_VIDEO_ONLY) { 1705 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; 1706 1707 int64_t latenessUs = nowUs - timeUs; 1708 1709 if (latenessUs > 0) { 1710 ALOGI("after SEEK_VIDEO_ONLY we're late by %.2f secs", latenessUs / 1E6); 1711 } 1712 } 1713 1714 if (wasSeeking == NO_SEEK) { 1715 // Let's display the first frame after seeking right away. 1716 1717 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; 1718 1719 int64_t latenessUs = nowUs - timeUs; 1720 1721 if (latenessUs > 500000ll 1722 && mAudioPlayer != NULL 1723 && mAudioPlayer->getMediaTimeMapping( 1724 &realTimeUs, &mediaTimeUs)) { 1725 ALOGI("we're much too late (%.2f secs), video skipping ahead", 1726 latenessUs / 1E6); 1727 1728 mVideoBuffer->release(); 1729 mVideoBuffer = NULL; 1730 1731 mSeeking = SEEK_VIDEO_ONLY; 1732 mSeekTimeUs = mediaTimeUs; 1733 1734 postVideoEvent_l(); 1735 return; 1736 } 1737 1738 if (latenessUs > 40000) { 1739 // We're more than 40ms late. 1740 ALOGV("we're late by %lld us (%.2f secs)", 1741 latenessUs, latenessUs / 1E6); 1742 1743 if (!(mFlags & SLOW_DECODER_HACK) 1744 || mSinceLastDropped > FRAME_DROP_FREQ) 1745 { 1746 ALOGV("we're late by %lld us (%.2f secs) dropping " 1747 "one after %d frames", 1748 latenessUs, latenessUs / 1E6, mSinceLastDropped); 1749 1750 mSinceLastDropped = 0; 1751 mVideoBuffer->release(); 1752 mVideoBuffer = NULL; 1753 1754 { 1755 Mutex::Autolock autoLock(mStatsLock); 1756 ++mStats.mNumVideoFramesDropped; 1757 } 1758 1759 postVideoEvent_l(); 1760 return; 1761 } 1762 } 1763 1764 if (latenessUs < -10000) { 1765 // We're more than 10ms early. 1766 1767 postVideoEvent_l(10000); 1768 return; 1769 } 1770 } 1771 1772 if ((mNativeWindow != NULL) 1773 && (mVideoRendererIsPreview || mVideoRenderer == NULL)) { 1774 mVideoRendererIsPreview = false; 1775 1776 initRenderer_l(); 1777 } 1778 1779 if (mVideoRenderer != NULL) { 1780 mSinceLastDropped++; 1781 mVideoRenderer->render(mVideoBuffer); 1782 } 1783 1784 mVideoBuffer->release(); 1785 mVideoBuffer = NULL; 1786 1787 if (wasSeeking != NO_SEEK && (mFlags & SEEK_PREVIEW)) { 1788 modifyFlags(SEEK_PREVIEW, CLEAR); 1789 return; 1790 } 1791 1792 postVideoEvent_l(); 1793} 1794 1795void AwesomePlayer::postVideoEvent_l(int64_t delayUs) { 1796 if (mVideoEventPending) { 1797 return; 1798 } 1799 1800 mVideoEventPending = true; 1801 mQueue.postEventWithDelay(mVideoEvent, delayUs < 0 ? 10000 : delayUs); 1802} 1803 1804void AwesomePlayer::postStreamDoneEvent_l(status_t status) { 1805 if (mStreamDoneEventPending) { 1806 return; 1807 } 1808 mStreamDoneEventPending = true; 1809 1810 mStreamDoneStatus = status; 1811 mQueue.postEvent(mStreamDoneEvent); 1812} 1813 1814void AwesomePlayer::postBufferingEvent_l() { 1815 if (mBufferingEventPending) { 1816 return; 1817 } 1818 mBufferingEventPending = true; 1819 mQueue.postEventWithDelay(mBufferingEvent, 1000000ll); 1820} 1821 1822void AwesomePlayer::postVideoLagEvent_l() { 1823 if (mVideoLagEventPending) { 1824 return; 1825 } 1826 mVideoLagEventPending = true; 1827 mQueue.postEventWithDelay(mVideoLagEvent, 1000000ll); 1828} 1829 1830void AwesomePlayer::postCheckAudioStatusEvent(int64_t delayUs) { 1831 Mutex::Autolock autoLock(mAudioLock); 1832 if (mAudioStatusEventPending) { 1833 return; 1834 } 1835 mAudioStatusEventPending = true; 1836 // Do not honor delay when looping in order to limit audio gap 1837 if (mFlags & (LOOPING | AUTO_LOOPING)) { 1838 delayUs = 0; 1839 } 1840 mQueue.postEventWithDelay(mCheckAudioStatusEvent, delayUs); 1841} 1842 1843void AwesomePlayer::onCheckAudioStatus() { 1844 { 1845 Mutex::Autolock autoLock(mAudioLock); 1846 if (!mAudioStatusEventPending) { 1847 // Event was dispatched and while we were blocking on the mutex, 1848 // has already been cancelled. 1849 return; 1850 } 1851 1852 mAudioStatusEventPending = false; 1853 } 1854 1855 Mutex::Autolock autoLock(mLock); 1856 1857 if (mWatchForAudioSeekComplete && !mAudioPlayer->isSeeking()) { 1858 mWatchForAudioSeekComplete = false; 1859 1860 if (!mSeekNotificationSent) { 1861 notifyListener_l(MEDIA_SEEK_COMPLETE); 1862 mSeekNotificationSent = true; 1863 } 1864 1865 mSeeking = NO_SEEK; 1866 } 1867 1868 status_t finalStatus; 1869 if (mWatchForAudioEOS && mAudioPlayer->reachedEOS(&finalStatus)) { 1870 mWatchForAudioEOS = false; 1871 modifyFlags(AUDIO_AT_EOS, SET); 1872 modifyFlags(FIRST_FRAME, SET); 1873 postStreamDoneEvent_l(finalStatus); 1874 } 1875} 1876 1877status_t AwesomePlayer::prepare() { 1878 Mutex::Autolock autoLock(mLock); 1879 return prepare_l(); 1880} 1881 1882status_t AwesomePlayer::prepare_l() { 1883 if (mFlags & PREPARED) { 1884 return OK; 1885 } 1886 1887 if (mFlags & PREPARING) { 1888 return UNKNOWN_ERROR; 1889 } 1890 1891 mIsAsyncPrepare = false; 1892 status_t err = prepareAsync_l(); 1893 1894 if (err != OK) { 1895 return err; 1896 } 1897 1898 while (mFlags & PREPARING) { 1899 mPreparedCondition.wait(mLock); 1900 } 1901 1902 return mPrepareResult; 1903} 1904 1905status_t AwesomePlayer::prepareAsync() { 1906 Mutex::Autolock autoLock(mLock); 1907 1908 if (mFlags & PREPARING) { 1909 return UNKNOWN_ERROR; // async prepare already pending 1910 } 1911 1912 mIsAsyncPrepare = true; 1913 return prepareAsync_l(); 1914} 1915 1916status_t AwesomePlayer::prepareAsync_l() { 1917 if (mFlags & PREPARING) { 1918 return UNKNOWN_ERROR; // async prepare already pending 1919 } 1920 1921 if (!mQueueStarted) { 1922 mQueue.start(); 1923 mQueueStarted = true; 1924 } 1925 1926 modifyFlags(PREPARING, SET); 1927 mAsyncPrepareEvent = new AwesomeEvent( 1928 this, &AwesomePlayer::onPrepareAsyncEvent); 1929 1930 mQueue.postEvent(mAsyncPrepareEvent); 1931 1932 return OK; 1933} 1934 1935status_t AwesomePlayer::finishSetDataSource_l() { 1936 sp<DataSource> dataSource; 1937 1938 bool isWidevineStreaming = false; 1939 if (!strncasecmp("widevine://", mUri.string(), 11)) { 1940 isWidevineStreaming = true; 1941 1942 String8 newURI = String8("http://"); 1943 newURI.append(mUri.string() + 11); 1944 1945 mUri = newURI; 1946 } 1947 1948 AString sniffedMIME; 1949 1950 if (!strncasecmp("http://", mUri.string(), 7) 1951 || !strncasecmp("https://", mUri.string(), 8) 1952 || isWidevineStreaming) { 1953 mConnectingDataSource = HTTPBase::Create( 1954 (mFlags & INCOGNITO) 1955 ? HTTPBase::kFlagIncognito 1956 : 0); 1957 1958 if (mUIDValid) { 1959 mConnectingDataSource->setUID(mUID); 1960 } 1961 1962 String8 cacheConfig; 1963 bool disconnectAtHighwatermark; 1964 NuCachedSource2::RemoveCacheSpecificHeaders( 1965 &mUriHeaders, &cacheConfig, &disconnectAtHighwatermark); 1966 1967 mLock.unlock(); 1968 status_t err = mConnectingDataSource->connect(mUri, &mUriHeaders); 1969 mLock.lock(); 1970 1971 if (err != OK) { 1972 mConnectingDataSource.clear(); 1973 1974 ALOGI("mConnectingDataSource->connect() returned %d", err); 1975 return err; 1976 } 1977 1978 if (!isWidevineStreaming) { 1979 // The widevine extractor does its own caching. 1980 1981#if 0 1982 mCachedSource = new NuCachedSource2( 1983 new ThrottledSource( 1984 mConnectingDataSource, 50 * 1024 /* bytes/sec */)); 1985#else 1986 mCachedSource = new NuCachedSource2( 1987 mConnectingDataSource, 1988 cacheConfig.isEmpty() ? NULL : cacheConfig.string(), 1989 disconnectAtHighwatermark); 1990#endif 1991 1992 dataSource = mCachedSource; 1993 } else { 1994 dataSource = mConnectingDataSource; 1995 } 1996 1997 mConnectingDataSource.clear(); 1998 1999 String8 contentType = dataSource->getMIMEType(); 2000 2001 if (strncasecmp(contentType.string(), "audio/", 6)) { 2002 // We're not doing this for streams that appear to be audio-only 2003 // streams to ensure that even low bandwidth streams start 2004 // playing back fairly instantly. 2005 2006 // We're going to prefill the cache before trying to instantiate 2007 // the extractor below, as the latter is an operation that otherwise 2008 // could block on the datasource for a significant amount of time. 2009 // During that time we'd be unable to abort the preparation phase 2010 // without this prefill. 2011 if (mCachedSource != NULL) { 2012 // We're going to prefill the cache before trying to instantiate 2013 // the extractor below, as the latter is an operation that otherwise 2014 // could block on the datasource for a significant amount of time. 2015 // During that time we'd be unable to abort the preparation phase 2016 // without this prefill. 2017 2018 mLock.unlock(); 2019 2020 // Initially make sure we have at least 192 KB for the sniff 2021 // to complete without blocking. 2022 static const size_t kMinBytesForSniffing = 192 * 1024; 2023 2024 off64_t metaDataSize = -1ll; 2025 for (;;) { 2026 status_t finalStatus; 2027 size_t cachedDataRemaining = 2028 mCachedSource->approxDataRemaining(&finalStatus); 2029 2030 if (finalStatus != OK 2031 || (metaDataSize >= 0 2032 && cachedDataRemaining >= metaDataSize) 2033 || (mFlags & PREPARE_CANCELLED)) { 2034 break; 2035 } 2036 2037 ALOGV("now cached %d bytes of data", cachedDataRemaining); 2038 2039 if (metaDataSize < 0 2040 && cachedDataRemaining >= kMinBytesForSniffing) { 2041 String8 tmp; 2042 float confidence; 2043 sp<AMessage> meta; 2044 if (!dataSource->sniff(&tmp, &confidence, &meta)) { 2045 mLock.lock(); 2046 return UNKNOWN_ERROR; 2047 } 2048 2049 // We successfully identified the file's extractor to 2050 // be, remember this mime type so we don't have to 2051 // sniff it again when we call MediaExtractor::Create() 2052 // below. 2053 sniffedMIME = tmp.string(); 2054 2055 if (meta == NULL 2056 || !meta->findInt64( 2057 "meta-data-size", &metaDataSize)) { 2058 metaDataSize = kHighWaterMarkBytes; 2059 } 2060 2061 CHECK_GE(metaDataSize, 0ll); 2062 ALOGV("metaDataSize = %lld bytes", metaDataSize); 2063 } 2064 2065 usleep(200000); 2066 } 2067 2068 mLock.lock(); 2069 } 2070 2071 if (mFlags & PREPARE_CANCELLED) { 2072 ALOGI("Prepare cancelled while waiting for initial cache fill."); 2073 return UNKNOWN_ERROR; 2074 } 2075 } 2076 } else { 2077 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders); 2078 } 2079 2080 if (dataSource == NULL) { 2081 return UNKNOWN_ERROR; 2082 } 2083 2084 sp<MediaExtractor> extractor; 2085 2086 if (isWidevineStreaming) { 2087 String8 mimeType; 2088 float confidence; 2089 sp<AMessage> dummy; 2090 bool success = SniffWVM(dataSource, &mimeType, &confidence, &dummy); 2091 2092 if (!success 2093 || strcasecmp( 2094 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) { 2095 return ERROR_UNSUPPORTED; 2096 } 2097 2098 mWVMExtractor = new WVMExtractor(dataSource); 2099 mWVMExtractor->setAdaptiveStreamingMode(true); 2100 if (mUIDValid) 2101 mWVMExtractor->setUID(mUID); 2102 extractor = mWVMExtractor; 2103 } else { 2104 extractor = MediaExtractor::Create( 2105 dataSource, sniffedMIME.empty() ? NULL : sniffedMIME.c_str()); 2106 2107 if (extractor == NULL) { 2108 return UNKNOWN_ERROR; 2109 } 2110 } 2111 2112 if (extractor->getDrmFlag()) { 2113 checkDrmStatus(dataSource); 2114 } 2115 2116 status_t err = setDataSource_l(extractor); 2117 2118 if (err != OK) { 2119 mWVMExtractor.clear(); 2120 2121 return err; 2122 } 2123 2124 return OK; 2125} 2126 2127void AwesomePlayer::abortPrepare(status_t err) { 2128 CHECK(err != OK); 2129 2130 if (mIsAsyncPrepare) { 2131 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 2132 } 2133 2134 mPrepareResult = err; 2135 modifyFlags((PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED), CLEAR); 2136 mAsyncPrepareEvent = NULL; 2137 mPreparedCondition.broadcast(); 2138} 2139 2140// static 2141bool AwesomePlayer::ContinuePreparation(void *cookie) { 2142 AwesomePlayer *me = static_cast<AwesomePlayer *>(cookie); 2143 2144 return (me->mFlags & PREPARE_CANCELLED) == 0; 2145} 2146 2147void AwesomePlayer::onPrepareAsyncEvent() { 2148 Mutex::Autolock autoLock(mLock); 2149 2150 if (mFlags & PREPARE_CANCELLED) { 2151 ALOGI("prepare was cancelled before doing anything"); 2152 abortPrepare(UNKNOWN_ERROR); 2153 return; 2154 } 2155 2156 if (mUri.size() > 0) { 2157 status_t err = finishSetDataSource_l(); 2158 2159 if (err != OK) { 2160 abortPrepare(err); 2161 return; 2162 } 2163 } 2164 2165 if (mVideoTrack != NULL && mVideoSource == NULL) { 2166 status_t err = initVideoDecoder(); 2167 2168 if (err != OK) { 2169 abortPrepare(err); 2170 return; 2171 } 2172 } 2173 2174 if (mAudioTrack != NULL && mAudioSource == NULL) { 2175 status_t err = initAudioDecoder(); 2176 2177 if (err != OK) { 2178 abortPrepare(err); 2179 return; 2180 } 2181 } 2182 2183 modifyFlags(PREPARING_CONNECTED, SET); 2184 2185 if (isStreamingHTTP()) { 2186 postBufferingEvent_l(); 2187 } else { 2188 finishAsyncPrepare_l(); 2189 } 2190} 2191 2192void AwesomePlayer::finishAsyncPrepare_l() { 2193 if (mIsAsyncPrepare) { 2194 if (mVideoSource == NULL) { 2195 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0); 2196 } else { 2197 notifyVideoSize_l(); 2198 } 2199 2200 notifyListener_l(MEDIA_PREPARED); 2201 } 2202 2203 mPrepareResult = OK; 2204 modifyFlags((PREPARING|PREPARE_CANCELLED|PREPARING_CONNECTED), CLEAR); 2205 modifyFlags(PREPARED, SET); 2206 mAsyncPrepareEvent = NULL; 2207 mPreparedCondition.broadcast(); 2208} 2209 2210uint32_t AwesomePlayer::flags() const { 2211 return mExtractorFlags; 2212} 2213 2214void AwesomePlayer::postAudioEOS(int64_t delayUs) { 2215 postCheckAudioStatusEvent(delayUs); 2216} 2217 2218void AwesomePlayer::postAudioSeekComplete() { 2219 postCheckAudioStatusEvent(0); 2220} 2221 2222status_t AwesomePlayer::setParameter(int key, const Parcel &request) { 2223 switch (key) { 2224 case KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS: 2225 { 2226 return setCacheStatCollectFreq(request); 2227 } 2228 case KEY_PARAMETER_PLAYBACK_RATE_PERMILLE: 2229 { 2230 if (mAudioPlayer != NULL) { 2231 return mAudioPlayer->setPlaybackRatePermille(request.readInt32()); 2232 } else { 2233 return NO_INIT; 2234 } 2235 } 2236 default: 2237 { 2238 return ERROR_UNSUPPORTED; 2239 } 2240 } 2241} 2242 2243status_t AwesomePlayer::setCacheStatCollectFreq(const Parcel &request) { 2244 if (mCachedSource != NULL) { 2245 int32_t freqMs = request.readInt32(); 2246 ALOGD("Request to keep cache stats in the past %d ms", 2247 freqMs); 2248 return mCachedSource->setCacheStatCollectFreq(freqMs); 2249 } 2250 return ERROR_UNSUPPORTED; 2251} 2252 2253status_t AwesomePlayer::getParameter(int key, Parcel *reply) { 2254 switch (key) { 2255 case KEY_PARAMETER_AUDIO_CHANNEL_COUNT: 2256 { 2257 int32_t channelCount; 2258 if (mAudioTrack == 0 || 2259 !mAudioTrack->getFormat()->findInt32(kKeyChannelCount, &channelCount)) { 2260 channelCount = 0; 2261 } 2262 reply->writeInt32(channelCount); 2263 } 2264 return OK; 2265 default: 2266 { 2267 return ERROR_UNSUPPORTED; 2268 } 2269 } 2270} 2271 2272status_t AwesomePlayer::getTrackInfo(Parcel *reply) const { 2273 Mutex::Autolock autoLock(mLock); 2274 size_t trackCount = mExtractor->countTracks(); 2275 if (mTextDriver != NULL) { 2276 trackCount += mTextDriver->countExternalTracks(); 2277 } 2278 2279 reply->writeInt32(trackCount); 2280 for (size_t i = 0; i < mExtractor->countTracks(); ++i) { 2281 sp<MetaData> meta = mExtractor->getTrackMetaData(i); 2282 2283 const char *_mime; 2284 CHECK(meta->findCString(kKeyMIMEType, &_mime)); 2285 2286 String8 mime = String8(_mime); 2287 2288 reply->writeInt32(2); // 2 fields 2289 2290 if (!strncasecmp(mime.string(), "video/", 6)) { 2291 reply->writeInt32(MEDIA_TRACK_TYPE_VIDEO); 2292 } else if (!strncasecmp(mime.string(), "audio/", 6)) { 2293 reply->writeInt32(MEDIA_TRACK_TYPE_AUDIO); 2294 } else if (!strcasecmp(mime.string(), MEDIA_MIMETYPE_TEXT_3GPP)) { 2295 reply->writeInt32(MEDIA_TRACK_TYPE_TIMEDTEXT); 2296 } else { 2297 reply->writeInt32(MEDIA_TRACK_TYPE_UNKNOWN); 2298 } 2299 2300 const char *lang; 2301 if (!meta->findCString(kKeyMediaLanguage, &lang)) { 2302 lang = "und"; 2303 } 2304 reply->writeString16(String16(lang)); 2305 } 2306 2307 if (mTextDriver != NULL) { 2308 mTextDriver->getExternalTrackInfo(reply); 2309 } 2310 return OK; 2311} 2312 2313// FIXME: 2314// At present, only timed text track is able to be selected or unselected. 2315status_t AwesomePlayer::selectTrack(size_t trackIndex, bool select) { 2316 ALOGV("selectTrack: trackIndex = %d and select=%d", trackIndex, select); 2317 Mutex::Autolock autoLock(mLock); 2318 size_t trackCount = mExtractor->countTracks(); 2319 if (mTextDriver != NULL) { 2320 trackCount += mTextDriver->countExternalTracks(); 2321 } 2322 2323 if (trackIndex >= trackCount) { 2324 ALOGE("Track index (%d) is out of range [0, %d)", trackIndex, trackCount); 2325 return ERROR_OUT_OF_RANGE; 2326 } 2327 2328 if (trackIndex < mExtractor->countTracks()) { 2329 sp<MetaData> meta = mExtractor->getTrackMetaData(trackIndex); 2330 const char *_mime; 2331 CHECK(meta->findCString(kKeyMIMEType, &_mime)); 2332 String8 mime = String8(_mime); 2333 2334 if (strcasecmp(mime.string(), MEDIA_MIMETYPE_TEXT_3GPP)) { 2335 return ERROR_UNSUPPORTED; 2336 } 2337 } 2338 2339 // Timed text track handling 2340 if (mTextDriver == NULL) { 2341 return INVALID_OPERATION; 2342 } 2343 2344 status_t err = OK; 2345 if (select) { 2346 err = mTextDriver->selectTrack(trackIndex); 2347 if (err == OK) { 2348 modifyFlags(TEXTPLAYER_INITIALIZED, SET); 2349 if (mFlags & PLAYING && !(mFlags & TEXT_RUNNING)) { 2350 mTextDriver->start(); 2351 modifyFlags(TEXT_RUNNING, SET); 2352 } 2353 } 2354 } else { 2355 err = mTextDriver->unselectTrack(trackIndex); 2356 if (err == OK) { 2357 modifyFlags(TEXTPLAYER_INITIALIZED, CLEAR); 2358 modifyFlags(TEXT_RUNNING, CLEAR); 2359 } 2360 } 2361 return err; 2362} 2363 2364size_t AwesomePlayer::countTracks() const { 2365 return mExtractor->countTracks() + mTextDriver->countExternalTracks(); 2366} 2367 2368status_t AwesomePlayer::setVideoScalingMode(int32_t mode) { 2369 Mutex::Autolock lock(mLock); 2370 return setVideoScalingMode_l(mode); 2371} 2372 2373status_t AwesomePlayer::setVideoScalingMode_l(int32_t mode) { 2374 mVideoScalingMode = mode; 2375 if (mNativeWindow != NULL) { 2376 status_t err = native_window_set_scaling_mode( 2377 mNativeWindow.get(), mVideoScalingMode); 2378 if (err != OK) { 2379 ALOGW("Failed to set scaling mode: %d", err); 2380 } 2381 } 2382 return OK; 2383} 2384 2385status_t AwesomePlayer::invoke(const Parcel &request, Parcel *reply) { 2386 if (NULL == reply) { 2387 return android::BAD_VALUE; 2388 } 2389 int32_t methodId; 2390 status_t ret = request.readInt32(&methodId); 2391 if (ret != android::OK) { 2392 return ret; 2393 } 2394 switch(methodId) { 2395 case INVOKE_ID_SET_VIDEO_SCALING_MODE: 2396 { 2397 int mode = request.readInt32(); 2398 return setVideoScalingMode(mode); 2399 } 2400 2401 case INVOKE_ID_GET_TRACK_INFO: 2402 { 2403 return getTrackInfo(reply); 2404 } 2405 case INVOKE_ID_ADD_EXTERNAL_SOURCE: 2406 { 2407 Mutex::Autolock autoLock(mLock); 2408 if (mTextDriver == NULL) { 2409 mTextDriver = new TimedTextDriver(mListener); 2410 } 2411 // String values written in Parcel are UTF-16 values. 2412 String8 uri(request.readString16()); 2413 String8 mimeType(request.readString16()); 2414 size_t nTracks = countTracks(); 2415 return mTextDriver->addOutOfBandTextSource(nTracks, uri, mimeType); 2416 } 2417 case INVOKE_ID_ADD_EXTERNAL_SOURCE_FD: 2418 { 2419 Mutex::Autolock autoLock(mLock); 2420 if (mTextDriver == NULL) { 2421 mTextDriver = new TimedTextDriver(mListener); 2422 } 2423 int fd = request.readFileDescriptor(); 2424 off64_t offset = request.readInt64(); 2425 off64_t length = request.readInt64(); 2426 String8 mimeType(request.readString16()); 2427 size_t nTracks = countTracks(); 2428 return mTextDriver->addOutOfBandTextSource( 2429 nTracks, fd, offset, length, mimeType); 2430 } 2431 case INVOKE_ID_SELECT_TRACK: 2432 { 2433 int trackIndex = request.readInt32(); 2434 return selectTrack(trackIndex, true /* select */); 2435 } 2436 case INVOKE_ID_UNSELECT_TRACK: 2437 { 2438 int trackIndex = request.readInt32(); 2439 return selectTrack(trackIndex, false /* select */); 2440 } 2441 default: 2442 { 2443 return ERROR_UNSUPPORTED; 2444 } 2445 } 2446 // It will not reach here. 2447 return OK; 2448} 2449 2450bool AwesomePlayer::isStreamingHTTP() const { 2451 return mCachedSource != NULL || mWVMExtractor != NULL; 2452} 2453 2454status_t AwesomePlayer::dump(int fd, const Vector<String16> &args) const { 2455 Mutex::Autolock autoLock(mStatsLock); 2456 2457 FILE *out = fdopen(dup(fd), "w"); 2458 2459 fprintf(out, " AwesomePlayer\n"); 2460 if (mStats.mFd < 0) { 2461 fprintf(out, " URI(%s)", mStats.mURI.string()); 2462 } else { 2463 fprintf(out, " fd(%d)", mStats.mFd); 2464 } 2465 2466 fprintf(out, ", flags(0x%08x)", mStats.mFlags); 2467 2468 if (mStats.mBitrate >= 0) { 2469 fprintf(out, ", bitrate(%lld bps)", mStats.mBitrate); 2470 } 2471 2472 fprintf(out, "\n"); 2473 2474 for (size_t i = 0; i < mStats.mTracks.size(); ++i) { 2475 const TrackStat &stat = mStats.mTracks.itemAt(i); 2476 2477 fprintf(out, " Track %d\n", i + 1); 2478 fprintf(out, " MIME(%s)", stat.mMIME.string()); 2479 2480 if (!stat.mDecoderName.isEmpty()) { 2481 fprintf(out, ", decoder(%s)", stat.mDecoderName.string()); 2482 } 2483 2484 fprintf(out, "\n"); 2485 2486 if ((ssize_t)i == mStats.mVideoTrackIndex) { 2487 fprintf(out, 2488 " videoDimensions(%d x %d), " 2489 "numVideoFramesDecoded(%lld), " 2490 "numVideoFramesDropped(%lld)\n", 2491 mStats.mVideoWidth, 2492 mStats.mVideoHeight, 2493 mStats.mNumVideoFramesDecoded, 2494 mStats.mNumVideoFramesDropped); 2495 } 2496 } 2497 2498 fclose(out); 2499 out = NULL; 2500 2501 return OK; 2502} 2503 2504void AwesomePlayer::modifyFlags(unsigned value, FlagMode mode) { 2505 switch (mode) { 2506 case SET: 2507 mFlags |= value; 2508 break; 2509 case CLEAR: 2510 mFlags &= ~value; 2511 break; 2512 case ASSIGN: 2513 mFlags = value; 2514 break; 2515 default: 2516 TRESPASS(); 2517 } 2518 2519 { 2520 Mutex::Autolock autoLock(mStatsLock); 2521 mStats.mFlags = mFlags; 2522 } 2523} 2524 2525} // namespace android 2526