PreviewPlayer.cpp revision b5c7784c96a606890eb8a8b560153ef4a5d1a0d9
1/* 2 * Copyright (C) 2011 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 18#define LOG_NDEBUG 1 19#define LOG_TAG "PreviewPlayer" 20#include <utils/Log.h> 21 22#include <dlfcn.h> 23 24#include "include/ARTSPController.h" 25#include "PreviewPlayer.h" 26#include "DummyAudioSource.h" 27#include "DummyVideoSource.h" 28#include "VideoEditorSRC.h" 29#include "include/NuCachedSource2.h" 30#include "include/ThrottledSource.h" 31 32 33#include "PreviewRenderer.h" 34 35#include <binder/IPCThreadState.h> 36#include <media/stagefright/DataSource.h> 37#include <media/stagefright/FileSource.h> 38#include <media/stagefright/MediaBuffer.h> 39#include <media/stagefright/MediaDefs.h> 40#include <media/stagefright/MediaExtractor.h> 41#include <media/stagefright/MediaDebug.h> 42#include <media/stagefright/MediaSource.h> 43#include <media/stagefright/MetaData.h> 44#include <media/stagefright/OMXCodec.h> 45 46#include <surfaceflinger/Surface.h> 47#include <media/stagefright/foundation/ALooper.h> 48 49namespace android { 50 51 52struct PreviewPlayerEvent : public TimedEventQueue::Event { 53 PreviewPlayerEvent( 54 PreviewPlayer *player, 55 void (PreviewPlayer::*method)()) 56 : mPlayer(player), 57 mMethod(method) { 58 } 59 60protected: 61 virtual ~PreviewPlayerEvent() {} 62 63 virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) { 64 (mPlayer->*mMethod)(); 65 } 66 67private: 68 PreviewPlayer *mPlayer; 69 void (PreviewPlayer::*mMethod)(); 70 71 PreviewPlayerEvent(const PreviewPlayerEvent &); 72 PreviewPlayerEvent &operator=(const PreviewPlayerEvent &); 73}; 74 75 76struct PreviewLocalRenderer : public PreviewPlayerRenderer { 77 78 static PreviewLocalRenderer* initPreviewLocalRenderer ( 79 bool previewOnly, 80 OMX_COLOR_FORMATTYPE colorFormat, 81 const sp<Surface> &surface, 82 size_t displayWidth, size_t displayHeight, 83 size_t decodedWidth, size_t decodedHeight, 84 int32_t rotationDegrees = 0) 85 { 86 PreviewLocalRenderer* mLocalRenderer = new 87 PreviewLocalRenderer( 88 previewOnly, 89 colorFormat, 90 surface, 91 displayWidth, displayHeight, 92 decodedWidth, decodedHeight, 93 rotationDegrees); 94 95 if ( mLocalRenderer->init(previewOnly, 96 colorFormat, surface, 97 displayWidth, displayHeight, 98 decodedWidth, decodedHeight, 99 rotationDegrees) != OK ) 100 { 101 delete mLocalRenderer; 102 return NULL; 103 } 104 return mLocalRenderer; 105 } 106 107 virtual void render(MediaBuffer *buffer) { 108 render((const uint8_t *)buffer->data() + buffer->range_offset(), 109 buffer->range_length()); 110 } 111 112 void render(const void *data, size_t size) { 113 mTarget->render(data, size, NULL); 114 } 115 void render() { 116 mTarget->renderYV12(); 117 } 118 void getBuffer(uint8_t **data, size_t *stride) { 119 mTarget->getBufferYV12(data, stride); 120 } 121 122protected: 123 virtual ~PreviewLocalRenderer() { 124 delete mTarget; 125 mTarget = NULL; 126 } 127 128private: 129 PreviewRenderer *mTarget; 130 131 PreviewLocalRenderer( 132 bool previewOnly, 133 OMX_COLOR_FORMATTYPE colorFormat, 134 const sp<Surface> &surface, 135 size_t displayWidth, size_t displayHeight, 136 size_t decodedWidth, size_t decodedHeight, 137 int32_t rotationDegrees = 0) 138 : mTarget(NULL) { 139 } 140 141 142 int init( 143 bool previewOnly, 144 OMX_COLOR_FORMATTYPE colorFormat, 145 const sp<Surface> &surface, 146 size_t displayWidth, size_t displayHeight, 147 size_t decodedWidth, size_t decodedHeight, 148 int32_t rotationDegrees = 0); 149 150 PreviewLocalRenderer(const PreviewLocalRenderer &); 151 PreviewLocalRenderer &operator=(const PreviewLocalRenderer &);; 152}; 153 154int PreviewLocalRenderer::init( 155 bool previewOnly, 156 OMX_COLOR_FORMATTYPE colorFormat, 157 const sp<Surface> &surface, 158 size_t displayWidth, size_t displayHeight, 159 size_t decodedWidth, size_t decodedHeight, 160 int32_t rotationDegrees) { 161 162 mTarget = PreviewRenderer::CreatePreviewRenderer ( 163 colorFormat, surface, displayWidth, displayHeight, 164 decodedWidth, decodedHeight, rotationDegrees); 165 if (mTarget == M4OSA_NULL) { 166 return UNKNOWN_ERROR; 167 } 168 return OK; 169} 170 171PreviewPlayer::PreviewPlayer() 172 : PreviewPlayerBase(), 173 mCurrFramingEffectIndex(0) , 174 mReportedWidth(0), 175 mReportedHeight(0), 176 mFrameRGBBuffer(NULL), 177 mFrameYUVBuffer(NULL){ 178 179 mVideoRenderer = NULL; 180 mLastVideoBuffer = NULL; 181 mSuspensionState = NULL; 182 mEffectsSettings = NULL; 183 mVeAudioPlayer = NULL; 184 mAudioMixStoryBoardTS = 0; 185 mCurrentMediaBeginCutTime = 0; 186 mCurrentMediaVolumeValue = 0; 187 mNumberEffects = 0; 188 mDecodedVideoTs = 0; 189 mDecVideoTsStoryBoard = 0; 190 mCurrentVideoEffect = VIDEO_EFFECT_NONE; 191 mProgressCbInterval = 0; 192 mNumberDecVideoFrames = 0; 193 mOverlayUpdateEventPosted = false; 194 mIsChangeSourceRequired = true; 195 196 mVideoEvent = new PreviewPlayerEvent(this, &PreviewPlayer::onVideoEvent); 197 mVideoEventPending = false; 198 mStreamDoneEvent = new PreviewPlayerEvent(this, 199 &PreviewPlayer::onStreamDone); 200 201 mStreamDoneEventPending = false; 202 203 mCheckAudioStatusEvent = new PreviewPlayerEvent( 204 this, &PreviewPlayerBase::onCheckAudioStatus); 205 206 mAudioStatusEventPending = false; 207 208 mProgressCbEvent = new PreviewPlayerEvent(this, 209 &PreviewPlayer::onProgressCbEvent); 210 211 mOverlayUpdateEvent = new PreviewPlayerEvent(this, 212 &PreviewPlayer::onUpdateOverlayEvent); 213 mProgressCbEventPending = false; 214 215 mOverlayUpdateEventPending = false; 216 mResizedVideoBuffer = NULL; 217 mVideoResizedOrCropped = false; 218 mRenderingMode = (M4xVSS_MediaRendering)MEDIA_RENDERING_INVALID; 219 mIsFiftiesEffectStarted = false; 220 reset(); 221} 222 223PreviewPlayer::~PreviewPlayer() { 224 225 if (mQueueStarted) { 226 mQueue.stop(); 227 } 228 229 reset(); 230 231 if(mResizedVideoBuffer != NULL) { 232 free((mResizedVideoBuffer->data())); 233 mResizedVideoBuffer = NULL; 234 } 235 236 mVideoRenderer.clear(); 237 mVideoRenderer = NULL; 238} 239 240void PreviewPlayer::cancelPlayerEvents(bool keepBufferingGoing) { 241 mQueue.cancelEvent(mVideoEvent->eventID()); 242 mVideoEventPending = false; 243 mQueue.cancelEvent(mStreamDoneEvent->eventID()); 244 mStreamDoneEventPending = false; 245 mQueue.cancelEvent(mCheckAudioStatusEvent->eventID()); 246 mAudioStatusEventPending = false; 247 248 mQueue.cancelEvent(mProgressCbEvent->eventID()); 249 mProgressCbEventPending = false; 250} 251 252status_t PreviewPlayer::setDataSource( 253 const char *uri, const KeyedVector<String8, String8> *headers) { 254 Mutex::Autolock autoLock(mLock); 255 return setDataSource_l(uri, headers); 256} 257 258status_t PreviewPlayer::setDataSource_l( 259 const char *uri, const KeyedVector<String8, String8> *headers) { 260 reset_l(); 261 262 mUri = uri; 263 264 if (headers) { 265 mUriHeaders = *headers; 266 } 267 268 // The actual work will be done during preparation in the call to 269 // ::finishSetDataSource_l to avoid blocking the calling thread in 270 // setDataSource for any significant time. 271 return OK; 272} 273 274status_t PreviewPlayer::setDataSource_l(const sp<MediaExtractor> &extractor) { 275 bool haveAudio = false; 276 bool haveVideo = false; 277 for (size_t i = 0; i < extractor->countTracks(); ++i) { 278 sp<MetaData> meta = extractor->getTrackMetaData(i); 279 280 const char *mime; 281 CHECK(meta->findCString(kKeyMIMEType, &mime)); 282 283 if (!haveVideo && !strncasecmp(mime, "video/", 6)) { 284 setVideoSource(extractor->getTrack(i)); 285 haveVideo = true; 286 } else if (!haveAudio && !strncasecmp(mime, "audio/", 6)) { 287 setAudioSource(extractor->getTrack(i)); 288 haveAudio = true; 289 290 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { 291 // Only do this for vorbis audio, none of the other audio 292 // formats even support this ringtone specific hack and 293 // retrieving the metadata on some extractors may turn out 294 // to be very expensive. 295 sp<MetaData> fileMeta = extractor->getMetaData(); 296 int32_t loop; 297 if (fileMeta != NULL 298 && fileMeta->findInt32(kKeyAutoLoop, &loop) 299 && loop != 0) { 300 mFlags |= AUTO_LOOPING; 301 } 302 } 303 } 304 305 if (haveAudio && haveVideo) { 306 break; 307 } 308 } 309 310 /* Add the support for Dummy audio*/ 311 if( !haveAudio ){ 312 LOGV("PreviewPlayer: setDataSource_l Dummyaudiocreation started"); 313 314 mAudioTrack = DummyAudioSource::Create(32000, 2, 20000, 315 ((mPlayEndTimeMsec)*1000LL)); 316 LOGV("PreviewPlayer: setDataSource_l Dummyauiosource created"); 317 if(mAudioTrack != NULL) { 318 haveAudio = true; 319 } 320 } 321 322 if (!haveAudio && !haveVideo) { 323 return UNKNOWN_ERROR; 324 } 325 326 mExtractorFlags = extractor->flags(); 327 return OK; 328} 329 330status_t PreviewPlayer::setDataSource_l_jpg() { 331 M4OSA_ERR err = M4NO_ERROR; 332 LOGV("PreviewPlayer: setDataSource_l_jpg started"); 333 334 mAudioSource = DummyAudioSource::Create(32000, 2, 20000, 335 ((mPlayEndTimeMsec)*1000LL)); 336 LOGV("PreviewPlayer: setDataSource_l_jpg Dummyaudiosource created"); 337 if(mAudioSource != NULL) { 338 setAudioSource(mAudioSource); 339 } 340 status_t error = mAudioSource->start(); 341 if (error != OK) { 342 LOGV("Error starting dummy audio source"); 343 mAudioSource.clear(); 344 return err; 345 } 346 347 mDurationUs = (mPlayEndTimeMsec - mPlayBeginTimeMsec)*1000LL; 348 349 mVideoSource = DummyVideoSource::Create(mVideoWidth, mVideoHeight, 350 mDurationUs, mUri); 351 mReportedWidth = mVideoWidth; 352 mReportedHeight = mVideoHeight; 353 354 setVideoSource(mVideoSource); 355 status_t err1 = mVideoSource->start(); 356 if (err1 != OK) { 357 mVideoSource.clear(); 358 return err; 359 } 360 361 mIsVideoSourceJpg = true; 362 return OK; 363} 364 365void PreviewPlayer::reset() { 366 Mutex::Autolock autoLock(mLock); 367 reset_l(); 368} 369 370void PreviewPlayer::reset_l() { 371 372 if (mFlags & PREPARING) { 373 mFlags |= PREPARE_CANCELLED; 374 } 375 376 while (mFlags & PREPARING) { 377 mPreparedCondition.wait(mLock); 378 } 379 380 cancelPlayerEvents(); 381 mAudioTrack.clear(); 382 mVideoTrack.clear(); 383 384 // Shutdown audio first, so that the respone to the reset request 385 // appears to happen instantaneously as far as the user is concerned 386 // If we did this later, audio would continue playing while we 387 // shutdown the video-related resources and the player appear to 388 // not be as responsive to a reset request. 389 if (mAudioPlayer == NULL && mAudioSource != NULL) { 390 // If we had an audio player, it would have effectively 391 // taken possession of the audio source and stopped it when 392 // _it_ is stopped. Otherwise this is still our responsibility. 393 mAudioSource->stop(); 394 } 395 mAudioSource.clear(); 396 397 mTimeSource = NULL; 398 399 //Single audio player instance used 400 //So donot delete it here 401 //It is deleted from PreviewController class 402 //delete mAudioPlayer; 403 mAudioPlayer = NULL; 404 405 if (mLastVideoBuffer) { 406 mLastVideoBuffer->release(); 407 mLastVideoBuffer = NULL; 408 } 409 410 if (mVideoBuffer) { 411 mVideoBuffer->release(); 412 mVideoBuffer = NULL; 413 } 414 415 if (mVideoSource != NULL) { 416 mVideoSource->stop(); 417 418 // The following hack is necessary to ensure that the OMX 419 // component is completely released by the time we may try 420 // to instantiate it again. 421 wp<MediaSource> tmp = mVideoSource; 422 mVideoSource.clear(); 423 while (tmp.promote() != NULL) { 424 usleep(1000); 425 } 426 IPCThreadState::self()->flushCommands(); 427 } 428 429 mDurationUs = -1; 430 mFlags = 0; 431 mExtractorFlags = 0; 432 mVideoWidth = mVideoHeight = -1; 433 mTimeSourceDeltaUs = 0; 434 mVideoTimeUs = 0; 435 436 mSeeking = NO_SEEK; 437 mSeekNotificationSent = false; 438 mSeekTimeUs = 0; 439 440 mUri.setTo(""); 441 mUriHeaders.clear(); 442 443 mFileSource.clear(); 444 445 delete mSuspensionState; 446 mSuspensionState = NULL; 447 448 mCurrentVideoEffect = VIDEO_EFFECT_NONE; 449 mIsVideoSourceJpg = false; 450 mFrameRGBBuffer = NULL; 451 if(mFrameYUVBuffer != NULL) { 452 free(mFrameYUVBuffer); 453 mFrameYUVBuffer = NULL; 454 } 455} 456 457status_t PreviewPlayer::play() { 458 Mutex::Autolock autoLock(mLock); 459 460 mFlags &= ~CACHE_UNDERRUN; 461 462 return play_l(); 463} 464 465status_t PreviewPlayer::startAudioPlayer_l() { 466 CHECK(!(mFlags & AUDIO_RUNNING)); 467 468 if (mAudioSource == NULL || mAudioPlayer == NULL) { 469 return OK; 470 } 471 472 if (!(mFlags & AUDIOPLAYER_STARTED)) { 473 mFlags |= AUDIOPLAYER_STARTED; 474 475 // We've already started the MediaSource in order to enable 476 // the prefetcher to read its data. 477 status_t err = mVeAudioPlayer->start( 478 true /* sourceAlreadyStarted */); 479 480 if (err != OK) { 481 notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 482 return err; 483 } 484 } else { 485 mVeAudioPlayer->resume(); 486 } 487 488 mFlags |= AUDIO_RUNNING; 489 490 mWatchForAudioEOS = true; 491 492 return OK; 493} 494 495status_t PreviewPlayer::setAudioPlayer(AudioPlayerBase *audioPlayer) { 496 Mutex::Autolock autoLock(mLock); 497 CHECK(!(mFlags & PLAYING)); 498 mAudioPlayer = audioPlayer; 499 500 LOGV("SetAudioPlayer"); 501 mIsChangeSourceRequired = true; 502 mVeAudioPlayer = 503 (VideoEditorAudioPlayer*)mAudioPlayer; 504 505 // check if the new and old source are dummy 506 sp<MediaSource> anAudioSource = mVeAudioPlayer->getSource(); 507 if (anAudioSource == NULL) { 508 // Audio player does not have any source set. 509 LOGV("setAudioPlayer: Audio player does not have any source set"); 510 return OK; 511 } 512 513 // If new video source is not dummy, then always change source 514 // Else audio player continues using old audio source and there are 515 // frame drops to maintain AV sync 516 sp<MetaData> meta; 517 if (mVideoSource != NULL) { 518 meta = mVideoSource->getFormat(); 519 const char *pVidSrcType; 520 if (meta->findCString(kKeyDecoderComponent, &pVidSrcType)) { 521 if (strcmp(pVidSrcType, "DummyVideoSource") != 0) { 522 LOGV(" Video clip with silent audio; need to change source"); 523 return OK; 524 } 525 } 526 } 527 528 const char *pSrcType1; 529 const char *pSrcType2; 530 meta = anAudioSource->getFormat(); 531 532 if (meta->findCString(kKeyDecoderComponent, &pSrcType1)) { 533 if (strcmp(pSrcType1, "DummyAudioSource") == 0) { 534 meta = mAudioSource->getFormat(); 535 if (meta->findCString(kKeyDecoderComponent, &pSrcType2)) { 536 if (strcmp(pSrcType2, "DummyAudioSource") == 0) { 537 mIsChangeSourceRequired = false; 538 // Just set the new play duration for the existing source 539 MediaSource *pMediaSrc = anAudioSource.get(); 540 DummyAudioSource *pDummyAudioSource = (DummyAudioSource*)pMediaSrc; 541 //Increment the duration of audio source 542 pDummyAudioSource->setDuration( 543 (int64_t)((mPlayEndTimeMsec)*1000LL)); 544 545 // Stop the new audio source 546 // since we continue using old source 547 LOGV("setAudioPlayer: stop new audio source"); 548 mAudioSource->stop(); 549 } 550 } 551 } 552 } 553 554 return OK; 555} 556 557void PreviewPlayer::onStreamDone() { 558 // Posted whenever any stream finishes playing. 559 560 Mutex::Autolock autoLock(mLock); 561 if (!mStreamDoneEventPending) { 562 return; 563 } 564 mStreamDoneEventPending = false; 565 566 if (mStreamDoneStatus != ERROR_END_OF_STREAM) { 567 LOGV("MEDIA_ERROR %d", mStreamDoneStatus); 568 569 notifyListener_l( 570 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus); 571 572 pause_l(true /* at eos */); 573 574 mFlags |= AT_EOS; 575 return; 576 } 577 578 const bool allDone = 579 (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS)) 580 && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS)); 581 582 if (!allDone) { 583 return; 584 } 585 586 if (mFlags & (LOOPING | AUTO_LOOPING)) { 587 seekTo_l(0); 588 589 if (mVideoSource != NULL) { 590 postVideoEvent_l(); 591 } 592 } else { 593 LOGV("MEDIA_PLAYBACK_COMPLETE"); 594 //pause before sending event 595 pause_l(true /* at eos */); 596 597 //This lock is used to syncronize onStreamDone() in PreviewPlayer and 598 //stopPreview() in PreviewController 599 Mutex::Autolock autoLock(mLockControl); 600 notifyListener_l(MEDIA_PLAYBACK_COMPLETE); 601 602 mFlags |= AT_EOS; 603 LOGV("onStreamDone end"); 604 return; 605 } 606} 607 608 609status_t PreviewPlayer::play_l() { 610 611 mFlags &= ~SEEK_PREVIEW; 612 613 if (mFlags & PLAYING) { 614 return OK; 615 } 616 mStartNextPlayer = false; 617 618 if (!(mFlags & PREPARED)) { 619 status_t err = prepare_l(); 620 621 if (err != OK) { 622 return err; 623 } 624 } 625 626 mFlags |= PLAYING; 627 mFlags |= FIRST_FRAME; 628 629 bool deferredAudioSeek = false; 630 631 if (mAudioSource != NULL) { 632 if (mAudioPlayer == NULL) { 633 if (mAudioSink != NULL) { 634 635 mAudioPlayer = new VideoEditorAudioPlayer(mAudioSink, this); 636 mVeAudioPlayer = 637 (VideoEditorAudioPlayer*)mAudioPlayer; 638 639 mAudioPlayer->setSource(mAudioSource); 640 641 mVeAudioPlayer->setAudioMixSettings( 642 mPreviewPlayerAudioMixSettings); 643 644 mVeAudioPlayer->setAudioMixPCMFileHandle( 645 mAudioMixPCMFileHandle); 646 647 mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp( 648 mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime, 649 mCurrentMediaVolumeValue); 650 651 mFlags |= AUDIOPLAYER_STARTED; 652 // We've already started the MediaSource in order to enable 653 // the prefetcher to read its data. 654 status_t err = mVeAudioPlayer->start( 655 true /* sourceAlreadyStarted */); 656 657 if (err != OK) { 658 //delete mAudioPlayer; 659 mAudioPlayer = NULL; 660 661 mFlags &= ~(PLAYING | FIRST_FRAME); 662 return err; 663 } 664 665 mTimeSource = mVeAudioPlayer; 666 mFlags |= AUDIO_RUNNING; 667 deferredAudioSeek = true; 668 mWatchForAudioSeekComplete = false; 669 mWatchForAudioEOS = true; 670 } 671 } else { 672 mVeAudioPlayer = (VideoEditorAudioPlayer*)mAudioPlayer; 673 bool isAudioPlayerStarted = mVeAudioPlayer->isStarted(); 674 675 if (mIsChangeSourceRequired == true) { 676 LOGV("play_l: Change audio source required"); 677 678 if (isAudioPlayerStarted == true) { 679 mVeAudioPlayer->pause(); 680 } 681 682 mVeAudioPlayer->setSource(mAudioSource); 683 mVeAudioPlayer->setObserver(this); 684 685 mVeAudioPlayer->setAudioMixSettings( 686 mPreviewPlayerAudioMixSettings); 687 688 mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp( 689 mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime, 690 mCurrentMediaVolumeValue); 691 692 if (isAudioPlayerStarted == true) { 693 mVeAudioPlayer->resume(); 694 } else { 695 status_t err = OK; 696 err = mVeAudioPlayer->start(true); 697 if (err != OK) { 698 mAudioPlayer = NULL; 699 mVeAudioPlayer = NULL; 700 701 mFlags &= ~(PLAYING | FIRST_FRAME); 702 return err; 703 } 704 } 705 } else { 706 LOGV("play_l: No Source change required"); 707 mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp( 708 mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime, 709 mCurrentMediaVolumeValue); 710 711 mVeAudioPlayer->resume(); 712 } 713 714 mFlags |= AUDIOPLAYER_STARTED; 715 mFlags |= AUDIO_RUNNING; 716 mTimeSource = mVeAudioPlayer; 717 deferredAudioSeek = true; 718 mWatchForAudioSeekComplete = false; 719 mWatchForAudioEOS = true; 720 } 721 } 722 723 if (mTimeSource == NULL && mAudioPlayer == NULL) { 724 mTimeSource = &mSystemTimeSource; 725 } 726 727 // Set the seek option for Image source files and read. 728 // This resets the timestamping for image play 729 if (mIsVideoSourceJpg) { 730 MediaSource::ReadOptions options; 731 MediaBuffer *aLocalBuffer; 732 options.setSeekTo(mSeekTimeUs); 733 mVideoSource->read(&aLocalBuffer, &options); 734 aLocalBuffer->release(); 735 } 736 737 if (mVideoSource != NULL) { 738 // Kick off video playback 739 postVideoEvent_l(); 740 } 741 742 if (deferredAudioSeek) { 743 // If there was a seek request while we were paused 744 // and we're just starting up again, honor the request now. 745 seekAudioIfNecessary_l(); 746 } 747 748 if (mFlags & AT_EOS) { 749 // Legacy behaviour, if a stream finishes playing and then 750 // is started again, we play from the start... 751 seekTo_l(0); 752 } 753 754 return OK; 755} 756 757 758status_t PreviewPlayer::initRenderer_l() { 759 if (mSurface != NULL) { 760 sp<MetaData> meta = mVideoSource->getFormat(); 761 762 int32_t format; 763 const char *component; 764 int32_t decodedWidth, decodedHeight; 765 CHECK(meta->findInt32(kKeyColorFormat, &format)); 766 CHECK(meta->findCString(kKeyDecoderComponent, &component)); 767 CHECK(meta->findInt32(kKeyWidth, &decodedWidth)); 768 CHECK(meta->findInt32(kKeyHeight, &decodedHeight)); 769 770 // Must ensure that mVideoRenderer's destructor is actually executed 771 // before creating a new one. 772 IPCThreadState::self()->flushCommands(); 773 774 // always use localrenderer since decoded buffers are modified 775 // by postprocessing module 776 // Other decoders are instantiated locally and as a consequence 777 // allocate their buffers in local address space. 778 if(mVideoRenderer == NULL) { 779 780 mVideoRenderer = PreviewLocalRenderer:: initPreviewLocalRenderer ( 781 false, // previewOnly 782 (OMX_COLOR_FORMATTYPE)format, 783 mSurface, 784 mOutputVideoWidth, mOutputVideoHeight, 785 mOutputVideoWidth, mOutputVideoHeight); 786 787 if ( mVideoRenderer == NULL ) 788 { 789 return UNKNOWN_ERROR; 790 } 791 return OK; 792 } 793 } 794 return OK; 795} 796 797 798status_t PreviewPlayer::seekTo(int64_t timeUs) { 799 800 if ((mExtractorFlags & MediaExtractor::CAN_SEEK) || (mIsVideoSourceJpg)) { 801 Mutex::Autolock autoLock(mLock); 802 return seekTo_l(timeUs); 803 } 804 805 return OK; 806} 807 808 809status_t PreviewPlayer::getVideoDimensions( 810 int32_t *width, int32_t *height) const { 811 Mutex::Autolock autoLock(mLock); 812 813 if (mVideoWidth < 0 || mVideoHeight < 0) { 814 return UNKNOWN_ERROR; 815 } 816 817 *width = mVideoWidth; 818 *height = mVideoHeight; 819 820 return OK; 821} 822 823 824status_t PreviewPlayer::initAudioDecoder() { 825 sp<MetaData> meta = mAudioTrack->getFormat(); 826 const char *mime; 827 CHECK(meta->findCString(kKeyMIMEType, &mime)); 828 829 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) { 830 mAudioSource = mAudioTrack; 831 } else { 832 sp<MediaSource> aRawSource; 833 aRawSource = OMXCodec::Create( 834 mClient.interface(), mAudioTrack->getFormat(), 835 false, // createEncoder 836 mAudioTrack); 837 838 if(aRawSource != NULL) { 839 LOGV("initAudioDecoder: new VideoEditorSRC"); 840 mAudioSource = new VideoEditorSRC(aRawSource); 841 } 842 } 843 844 if (mAudioSource != NULL) { 845 int64_t durationUs; 846 if (mAudioTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 847 Mutex::Autolock autoLock(mMiscStateLock); 848 if (mDurationUs < 0 || durationUs > mDurationUs) { 849 mDurationUs = durationUs; 850 } 851 } 852 status_t err = mAudioSource->start(); 853 854 if (err != OK) { 855 mAudioSource.clear(); 856 return err; 857 } 858 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_QCELP)) { 859 // For legacy reasons we're simply going to ignore the absence 860 // of an audio decoder for QCELP instead of aborting playback 861 // altogether. 862 return OK; 863 } 864 865 return mAudioSource != NULL ? OK : UNKNOWN_ERROR; 866} 867 868 869status_t PreviewPlayer::initVideoDecoder(uint32_t flags) { 870 871 mVideoSource = OMXCodec::Create( 872 mClient.interface(), mVideoTrack->getFormat(), 873 false, 874 mVideoTrack, 875 NULL, flags); 876 877 if (mVideoSource != NULL) { 878 int64_t durationUs; 879 if (mVideoTrack->getFormat()->findInt64(kKeyDuration, &durationUs)) { 880 Mutex::Autolock autoLock(mMiscStateLock); 881 if (mDurationUs < 0 || durationUs > mDurationUs) { 882 mDurationUs = durationUs; 883 } 884 } 885 886 CHECK(mVideoTrack->getFormat()->findInt32(kKeyWidth, &mVideoWidth)); 887 CHECK(mVideoTrack->getFormat()->findInt32(kKeyHeight, &mVideoHeight)); 888 889 mReportedWidth = mVideoWidth; 890 mReportedHeight = mVideoHeight; 891 892 status_t err = mVideoSource->start(); 893 894 if (err != OK) { 895 mVideoSource.clear(); 896 return err; 897 } 898 } 899 900 return mVideoSource != NULL ? OK : UNKNOWN_ERROR; 901} 902 903 904void PreviewPlayer::onVideoEvent() { 905 uint32_t i=0; 906 bool bAppliedVideoEffect = false; 907 M4OSA_ERR err1 = M4NO_ERROR; 908 int64_t imageFrameTimeUs = 0; 909 910 Mutex::Autolock autoLock(mLock); 911 if (!mVideoEventPending) { 912 // The event has been cancelled in reset_l() but had already 913 // been scheduled for execution at that time. 914 return; 915 } 916 mVideoEventPending = false; 917 918 if (mFlags & SEEK_PREVIEW) { 919 mFlags &= ~SEEK_PREVIEW; 920 return; 921 } 922 923 TimeSource *ts_st = &mSystemTimeSource; 924 int64_t timeStartUs = ts_st->getRealTimeUs(); 925 926 if (mSeeking != NO_SEEK) { 927 if (mLastVideoBuffer) { 928 mLastVideoBuffer->release(); 929 mLastVideoBuffer = NULL; 930 } 931 932 933 if(mAudioSource != NULL) { 934 935 // We're going to seek the video source first, followed by 936 // the audio source. 937 // In order to avoid jumps in the DataSource offset caused by 938 // the audio codec prefetching data from the old locations 939 // while the video codec is already reading data from the new 940 // locations, we'll "pause" the audio source, causing it to 941 // stop reading input data until a subsequent seek. 942 943 if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) { 944 mAudioPlayer->pause(); 945 mFlags &= ~AUDIO_RUNNING; 946 } 947 mAudioSource->pause(); 948 } 949 } 950 951 if (!mVideoBuffer) { 952 MediaSource::ReadOptions options; 953 if (mSeeking != NO_SEEK) { 954 LOGV("LV PLAYER seeking to %lld us (%.2f secs)", mSeekTimeUs, 955 mSeekTimeUs / 1E6); 956 957 options.setSeekTo( 958 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST); 959 } 960 for (;;) { 961 status_t err = mVideoSource->read(&mVideoBuffer, &options); 962 options.clearSeekTo(); 963 964 if (err != OK) { 965 CHECK_EQ(mVideoBuffer, NULL); 966 967 if (err == INFO_FORMAT_CHANGED) { 968 LOGV("LV PLAYER VideoSource signalled format change"); 969 notifyVideoSize_l(); 970 sp<MetaData> meta = mVideoSource->getFormat(); 971 972 CHECK(meta->findInt32(kKeyWidth, &mReportedWidth)); 973 CHECK(meta->findInt32(kKeyHeight, &mReportedHeight)); 974 if (mVideoRenderer != NULL) { 975 mVideoRendererIsPreview = false; 976 err = initRenderer_l(); 977 if (err != OK) { 978 postStreamDoneEvent_l(err); 979 } 980 981 } 982 continue; 983 } 984 // So video playback is complete, but we may still have 985 // a seek request pending that needs to be applied to the audio track 986 if (mSeeking != NO_SEEK) { 987 LOGV("video stream ended while seeking!"); 988 } 989 finishSeekIfNecessary(-1); 990 LOGV("PreviewPlayer: onVideoEvent EOS reached."); 991 mFlags |= VIDEO_AT_EOS; 992 mFlags |= AUDIO_AT_EOS; 993 mOverlayUpdateEventPosted = false; 994 postStreamDoneEvent_l(err); 995 // Set the last decoded timestamp to duration 996 mDecodedVideoTs = (mPlayEndTimeMsec*1000LL); 997 return; 998 } 999 1000 if (mVideoBuffer->range_length() == 0) { 1001 // Some decoders, notably the PV AVC software decoder 1002 // return spurious empty buffers that we just want to ignore. 1003 1004 mVideoBuffer->release(); 1005 mVideoBuffer = NULL; 1006 continue; 1007 } 1008 1009 int64_t videoTimeUs; 1010 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs)); 1011 1012 if (mSeeking != NO_SEEK) { 1013 if (videoTimeUs < mSeekTimeUs) { 1014 // buffers are before seek time 1015 // ignore them 1016 mVideoBuffer->release(); 1017 mVideoBuffer = NULL; 1018 continue; 1019 } 1020 } else { 1021 if((videoTimeUs/1000) < mPlayBeginTimeMsec) { 1022 // Frames are before begin cut time 1023 // Donot render 1024 mVideoBuffer->release(); 1025 mVideoBuffer = NULL; 1026 continue; 1027 } 1028 } 1029 break; 1030 } 1031 } 1032 1033 mNumberDecVideoFrames++; 1034 1035 int64_t timeUs; 1036 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 1037 1038 { 1039 Mutex::Autolock autoLock(mMiscStateLock); 1040 mVideoTimeUs = timeUs; 1041 } 1042 1043 1044 if(!mStartNextPlayer) { 1045 int64_t playbackTimeRemaining = (mPlayEndTimeMsec*1000LL) - timeUs; 1046 if(playbackTimeRemaining <= 1500000) { 1047 //When less than 1.5 sec of playback left 1048 // send notification to start next player 1049 1050 mStartNextPlayer = true; 1051 notifyListener_l(0xAAAAAAAA); 1052 } 1053 } 1054 1055 SeekType wasSeeking = mSeeking; 1056 finishSeekIfNecessary(timeUs); 1057 if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING))) { 1058 status_t err = startAudioPlayer_l(); 1059 if (err != OK) { 1060 LOGE("Starting the audio player failed w/ err %d", err); 1061 return; 1062 } 1063 } 1064 1065 TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource; 1066 1067 if(ts == NULL) { 1068 mVideoBuffer->release(); 1069 mVideoBuffer = NULL; 1070 return; 1071 } 1072 1073 if(!mIsVideoSourceJpg) { 1074 if (mFlags & FIRST_FRAME) { 1075 mFlags &= ~FIRST_FRAME; 1076 1077 mTimeSourceDeltaUs = ts->getRealTimeUs() - timeUs; 1078 } 1079 1080 int64_t realTimeUs, mediaTimeUs; 1081 if (!(mFlags & AUDIO_AT_EOS) && mAudioPlayer != NULL 1082 && mAudioPlayer->getMediaTimeMapping(&realTimeUs, &mediaTimeUs)) { 1083 mTimeSourceDeltaUs = realTimeUs - mediaTimeUs; 1084 } 1085 1086 int64_t nowUs = ts->getRealTimeUs() - mTimeSourceDeltaUs; 1087 1088 int64_t latenessUs = nowUs - timeUs; 1089 1090 if (wasSeeking != NO_SEEK) { 1091 // Let's display the first frame after seeking right away. 1092 latenessUs = 0; 1093 } 1094 LOGV("Audio time stamp = %lld and video time stamp = %lld", 1095 ts->getRealTimeUs(),timeUs); 1096 if (latenessUs > 40000) { 1097 // We're more than 40ms late. 1098 1099 LOGV("LV PLAYER we're late by %lld us (%.2f secs)", 1100 latenessUs, latenessUs / 1E6); 1101 1102 mVideoBuffer->release(); 1103 mVideoBuffer = NULL; 1104 postVideoEvent_l(0); 1105 return; 1106 } 1107 1108 if (latenessUs < -25000) { 1109 // We're more than 25ms early. 1110 LOGV("We're more than 25ms early, lateness %lld", latenessUs); 1111 1112 postVideoEvent_l(25000); 1113 return; 1114 } 1115 } 1116 1117 if (mVideoRendererIsPreview || mVideoRenderer == NULL) { 1118 mVideoRendererIsPreview = false; 1119 1120 status_t err = initRenderer_l(); 1121 if (err != OK) { 1122 postStreamDoneEvent_l(err); 1123 } 1124 } 1125 1126 // If timestamp exceeds endCutTime of clip, donot render 1127 if((timeUs/1000) > mPlayEndTimeMsec) { 1128 if (mLastVideoBuffer) { 1129 mLastVideoBuffer->release(); 1130 mLastVideoBuffer = NULL; 1131 } 1132 mLastVideoBuffer = mVideoBuffer; 1133 mVideoBuffer = NULL; 1134 mFlags |= VIDEO_AT_EOS; 1135 mFlags |= AUDIO_AT_EOS; 1136 LOGV("PreviewPlayer: onVideoEvent timeUs > mPlayEndTime; send EOS.."); 1137 mOverlayUpdateEventPosted = false; 1138 // Set the last decoded timestamp to duration 1139 mDecodedVideoTs = (mPlayEndTimeMsec*1000LL); 1140 postStreamDoneEvent_l(ERROR_END_OF_STREAM); 1141 return; 1142 } 1143 // Capture the frame timestamp to be rendered 1144 mDecodedVideoTs = timeUs; 1145 1146 // Post processing to apply video effects 1147 for(i=0;i<mNumberEffects;i++) { 1148 // First check if effect starttime matches the clip being previewed 1149 if((mEffectsSettings[i].uiStartTime < (mDecVideoTsStoryBoard/1000)) || 1150 (mEffectsSettings[i].uiStartTime >= 1151 ((mDecVideoTsStoryBoard/1000) + mPlayEndTimeMsec - mPlayBeginTimeMsec))) 1152 { 1153 // This effect doesn't belong to this clip, check next one 1154 continue; 1155 } 1156 // Check if effect applies to this particular frame timestamp 1157 if((mEffectsSettings[i].uiStartTime <= 1158 (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec)) && 1159 ((mEffectsSettings[i].uiStartTime+mEffectsSettings[i].uiDuration) >= 1160 (((timeUs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec)) 1161 && (mEffectsSettings[i].uiDuration != 0)) { 1162 setVideoPostProcessingNode( 1163 mEffectsSettings[i].VideoEffectType, TRUE); 1164 } 1165 else { 1166 setVideoPostProcessingNode( 1167 mEffectsSettings[i].VideoEffectType, FALSE); 1168 } 1169 } 1170 1171 //Provide the overlay Update indication when there is an overlay effect 1172 if (mCurrentVideoEffect & VIDEO_EFFECT_FRAMING) { 1173 mCurrentVideoEffect &= ~VIDEO_EFFECT_FRAMING; //never apply framing here. 1174 if (!mOverlayUpdateEventPosted) { 1175 // Find the effect in effectSettings array 1176 M4OSA_UInt32 index; 1177 for (index = 0; index < mNumberEffects; index++) { 1178 M4OSA_UInt32 timeMs = mDecodedVideoTs/1000; 1179 M4OSA_UInt32 timeOffset = mDecVideoTsStoryBoard/1000; 1180 if(mEffectsSettings[index].VideoEffectType == 1181 (M4VSS3GPP_VideoEffectType)M4xVSS_kVideoEffectType_Framing) { 1182 if (((mEffectsSettings[index].uiStartTime + 1) <= 1183 timeMs + timeOffset - mPlayBeginTimeMsec) && 1184 ((mEffectsSettings[index].uiStartTime - 1 + 1185 mEffectsSettings[index].uiDuration) >= 1186 timeMs + timeOffset - mPlayBeginTimeMsec)) 1187 { 1188 break; 1189 } 1190 } 1191 } 1192 if (index < mNumberEffects) { 1193 mCurrFramingEffectIndex = index; 1194 mOverlayUpdateEventPosted = true; 1195 postOverlayUpdateEvent_l(); 1196 LOGV("Framing index = %d", mCurrFramingEffectIndex); 1197 } else { 1198 LOGV("No framing effects found"); 1199 } 1200 } 1201 1202 } else if (mOverlayUpdateEventPosted) { 1203 //Post the event when the overlay is no more valid 1204 LOGV("Overlay is Done"); 1205 mOverlayUpdateEventPosted = false; 1206 postOverlayUpdateEvent_l(); 1207 } 1208 1209 1210 if (mCurrentVideoEffect != VIDEO_EFFECT_NONE) { 1211 err1 = doVideoPostProcessing(); 1212 if(err1 != M4NO_ERROR) { 1213 LOGE("doVideoPostProcessing returned err"); 1214 bAppliedVideoEffect = false; 1215 } 1216 else { 1217 bAppliedVideoEffect = true; 1218 } 1219 } 1220 else { 1221 bAppliedVideoEffect = false; 1222 if(mRenderingMode != MEDIA_RENDERING_INVALID) { 1223 // No effects to be applied, but media rendering to be done 1224 err1 = doMediaRendering(); 1225 if(err1 != M4NO_ERROR) { 1226 LOGE("doMediaRendering returned err"); 1227 //Use original mVideoBuffer for rendering 1228 mVideoResizedOrCropped = false; 1229 } 1230 } 1231 } 1232 1233 if (mVideoRenderer != NULL) { 1234 LOGV("mVideoRenderer CALL render()"); 1235 mVideoRenderer->render(); 1236 } 1237 1238 if (mLastVideoBuffer) { 1239 mLastVideoBuffer->release(); 1240 mLastVideoBuffer = NULL; 1241 } 1242 1243 mLastVideoBuffer = mVideoBuffer; 1244 mVideoBuffer = NULL; 1245 1246 // Post progress callback based on callback interval set 1247 if(mNumberDecVideoFrames >= mProgressCbInterval) { 1248 postProgressCallbackEvent_l(); 1249 mNumberDecVideoFrames = 0; // reset counter 1250 } 1251 1252 // if reached EndCutTime of clip, post EOS event 1253 if((timeUs/1000) >= mPlayEndTimeMsec) { 1254 LOGV("PreviewPlayer: onVideoEvent EOS."); 1255 mFlags |= VIDEO_AT_EOS; 1256 mFlags |= AUDIO_AT_EOS; 1257 mOverlayUpdateEventPosted = false; 1258 // Set the last decoded timestamp to duration 1259 mDecodedVideoTs = (mPlayEndTimeMsec*1000LL); 1260 postStreamDoneEvent_l(ERROR_END_OF_STREAM); 1261 } 1262 else { 1263 if ((wasSeeking != NO_SEEK) && (mFlags & SEEK_PREVIEW)) { 1264 mFlags &= ~SEEK_PREVIEW; 1265 return; 1266 } 1267 1268 if(!mIsVideoSourceJpg) { 1269 postVideoEvent_l(0); 1270 } 1271 else { 1272 postVideoEvent_l(33000); 1273 } 1274 } 1275} 1276 1277status_t PreviewPlayer::prepare() { 1278 Mutex::Autolock autoLock(mLock); 1279 return prepare_l(); 1280} 1281 1282status_t PreviewPlayer::prepare_l() { 1283 if (mFlags & PREPARED) { 1284 return OK; 1285 } 1286 1287 if (mFlags & PREPARING) { 1288 return UNKNOWN_ERROR; 1289 } 1290 1291 mIsAsyncPrepare = false; 1292 status_t err = prepareAsync_l(); 1293 1294 if (err != OK) { 1295 return err; 1296 } 1297 1298 while (mFlags & PREPARING) { 1299 mPreparedCondition.wait(mLock); 1300 } 1301 1302 return mPrepareResult; 1303} 1304 1305status_t PreviewPlayer::prepareAsync_l() { 1306 if (mFlags & PREPARING) { 1307 return UNKNOWN_ERROR; // async prepare already pending 1308 } 1309 1310 if (!mQueueStarted) { 1311 mQueue.start(); 1312 mQueueStarted = true; 1313 } 1314 1315 mFlags |= PREPARING; 1316 mAsyncPrepareEvent = new PreviewPlayerEvent( 1317 this, &PreviewPlayer::onPrepareAsyncEvent); 1318 1319 mQueue.postEvent(mAsyncPrepareEvent); 1320 1321 return OK; 1322} 1323 1324status_t PreviewPlayer::finishSetDataSource_l() { 1325 sp<DataSource> dataSource; 1326 sp<MediaExtractor> extractor; 1327 1328 dataSource = DataSource::CreateFromURI(mUri.string(), &mUriHeaders); 1329 1330 if (dataSource == NULL) { 1331 return UNKNOWN_ERROR; 1332 } 1333 1334 //If file type is .rgb, then no need to check for Extractor 1335 int uriLen = strlen(mUri); 1336 int startOffset = uriLen - 4; 1337 if(!strncasecmp(mUri+startOffset, ".rgb", 4)) { 1338 extractor = NULL; 1339 } 1340 else { 1341 extractor = MediaExtractor::Create(dataSource, 1342 MEDIA_MIMETYPE_CONTAINER_MPEG4); 1343 } 1344 1345 if (extractor == NULL) { 1346 LOGV("PreviewPlayer::finishSetDataSource_l extractor == NULL"); 1347 return setDataSource_l_jpg(); 1348 } 1349 1350 return setDataSource_l(extractor); 1351} 1352 1353 1354// static 1355bool PreviewPlayer::ContinuePreparation(void *cookie) { 1356 PreviewPlayer *me = static_cast<PreviewPlayer *>(cookie); 1357 1358 return (me->mFlags & PREPARE_CANCELLED) == 0; 1359} 1360 1361void PreviewPlayer::onPrepareAsyncEvent() { 1362 Mutex::Autolock autoLock(mLock); 1363 LOGV("onPrepareAsyncEvent"); 1364 1365 if (mFlags & PREPARE_CANCELLED) { 1366 LOGV("LV PLAYER prepare was cancelled before doing anything"); 1367 abortPrepare(UNKNOWN_ERROR); 1368 return; 1369 } 1370 1371 if (mUri.size() > 0) { 1372 status_t err = finishSetDataSource_l(); 1373 1374 if (err != OK) { 1375 abortPrepare(err); 1376 return; 1377 } 1378 } 1379 1380 if (mVideoTrack != NULL && mVideoSource == NULL) { 1381 status_t err = initVideoDecoder(OMXCodec::kHardwareCodecsOnly); 1382 1383 if (err != OK) { 1384 abortPrepare(err); 1385 return; 1386 } 1387 } 1388 1389 if (mAudioTrack != NULL && mAudioSource == NULL) { 1390 status_t err = initAudioDecoder(); 1391 1392 if (err != OK) { 1393 abortPrepare(err); 1394 return; 1395 } 1396 } 1397 finishAsyncPrepare_l(); 1398 1399} 1400 1401void PreviewPlayer::finishAsyncPrepare_l() { 1402 if (mIsAsyncPrepare) { 1403 if (mVideoSource == NULL) { 1404 LOGV("finishAsyncPrepare_l: MEDIA_SET_VIDEO_SIZE 0 0 "); 1405 notifyListener_l(MEDIA_SET_VIDEO_SIZE, 0, 0); 1406 } else { 1407 LOGV("finishAsyncPrepare_l: MEDIA_SET_VIDEO_SIZE"); 1408 notifyVideoSize_l(); 1409 } 1410 LOGV("finishAsyncPrepare_l: MEDIA_PREPARED"); 1411 notifyListener_l(MEDIA_PREPARED); 1412 } 1413 1414 mPrepareResult = OK; 1415 mFlags &= ~(PREPARING|PREPARE_CANCELLED); 1416 mFlags |= PREPARED; 1417 mAsyncPrepareEvent = NULL; 1418 mPreparedCondition.broadcast(); 1419} 1420 1421status_t PreviewPlayer::suspend() { 1422 LOGV("suspend"); 1423 Mutex::Autolock autoLock(mLock); 1424 1425 if (mSuspensionState != NULL) { 1426 if (mLastVideoBuffer == NULL) { 1427 //go into here if video is suspended again 1428 //after resuming without being played between 1429 //them 1430 SuspensionState *state = mSuspensionState; 1431 mSuspensionState = NULL; 1432 reset_l(); 1433 mSuspensionState = state; 1434 return OK; 1435 } 1436 1437 delete mSuspensionState; 1438 mSuspensionState = NULL; 1439 } 1440 1441 if (mFlags & PREPARING) { 1442 mFlags |= PREPARE_CANCELLED; 1443 } 1444 1445 while (mFlags & PREPARING) { 1446 mPreparedCondition.wait(mLock); 1447 } 1448 1449 SuspensionState *state = new SuspensionState; 1450 state->mUri = mUri; 1451 state->mUriHeaders = mUriHeaders; 1452 state->mFileSource = mFileSource; 1453 1454 state->mFlags = mFlags & (PLAYING | AUTO_LOOPING | LOOPING | AT_EOS); 1455 getPosition(&state->mPositionUs); 1456 1457 if (mLastVideoBuffer) { 1458 size_t size = mLastVideoBuffer->range_length(); 1459 if (size) { 1460 int32_t unreadable; 1461 if (!mLastVideoBuffer->meta_data()->findInt32( 1462 kKeyIsUnreadable, &unreadable) 1463 || unreadable == 0) { 1464 state->mLastVideoFrameSize = size; 1465 state->mLastVideoFrame = malloc(size); 1466 memcpy(state->mLastVideoFrame, 1467 (const uint8_t *)mLastVideoBuffer->data() 1468 + mLastVideoBuffer->range_offset(), 1469 size); 1470 1471 state->mVideoWidth = mVideoWidth; 1472 state->mVideoHeight = mVideoHeight; 1473 1474 sp<MetaData> meta = mVideoSource->getFormat(); 1475 CHECK(meta->findInt32(kKeyColorFormat, &state->mColorFormat)); 1476 CHECK(meta->findInt32(kKeyWidth, &state->mDecodedWidth)); 1477 CHECK(meta->findInt32(kKeyHeight, &state->mDecodedHeight)); 1478 } else { 1479 LOGV("Unable to save last video frame, we have no access to " 1480 "the decoded video data."); 1481 } 1482 } 1483 } 1484 1485 reset_l(); 1486 1487 mSuspensionState = state; 1488 1489 return OK; 1490} 1491 1492void PreviewPlayer::acquireLock() { 1493 LOGV("acquireLock"); 1494 mLockControl.lock(); 1495} 1496 1497void PreviewPlayer::releaseLock() { 1498 LOGV("releaseLock"); 1499 mLockControl.unlock(); 1500} 1501 1502status_t PreviewPlayer::resume() { 1503 LOGV("resume"); 1504 Mutex::Autolock autoLock(mLock); 1505 1506 if (mSuspensionState == NULL) { 1507 return INVALID_OPERATION; 1508 } 1509 1510 SuspensionState *state = mSuspensionState; 1511 mSuspensionState = NULL; 1512 1513 status_t err; 1514 if (state->mFileSource != NULL) { 1515 err = PreviewPlayerBase::setDataSource_l(state->mFileSource); 1516 1517 if (err == OK) { 1518 mFileSource = state->mFileSource; 1519 } 1520 } else { 1521 err = PreviewPlayerBase::setDataSource_l(state->mUri, &state->mUriHeaders); 1522 } 1523 1524 if (err != OK) { 1525 delete state; 1526 state = NULL; 1527 1528 return err; 1529 } 1530 1531 seekTo_l(state->mPositionUs); 1532 1533 mFlags = state->mFlags & (AUTO_LOOPING | LOOPING | AT_EOS); 1534 1535 if (state->mLastVideoFrame && (mSurface != NULL)) { 1536 mVideoRenderer = 1537 PreviewLocalRenderer::initPreviewLocalRenderer( 1538 true, // previewOnly 1539 (OMX_COLOR_FORMATTYPE)state->mColorFormat, 1540 mSurface, 1541 state->mVideoWidth, 1542 state->mVideoHeight, 1543 state->mDecodedWidth, 1544 state->mDecodedHeight); 1545 1546 mVideoRendererIsPreview = true; 1547 1548 ((PreviewLocalRenderer *)mVideoRenderer.get())->render( 1549 state->mLastVideoFrame, state->mLastVideoFrameSize); 1550 } 1551 1552 if (state->mFlags & PLAYING) { 1553 play_l(); 1554 } 1555 1556 mSuspensionState = state; 1557 state = NULL; 1558 1559 return OK; 1560} 1561 1562 1563status_t PreviewPlayer::loadEffectsSettings( 1564 M4VSS3GPP_EffectSettings* pEffectSettings, int nEffects) { 1565 M4OSA_UInt32 i = 0, rgbSize = 0; 1566 M4VIFI_UInt8 *tmp = M4OSA_NULL; 1567 1568 mNumberEffects = nEffects; 1569 mEffectsSettings = pEffectSettings; 1570 return OK; 1571} 1572 1573status_t PreviewPlayer::loadAudioMixSettings( 1574 M4xVSS_AudioMixingSettings* pAudioMixSettings) { 1575 1576 LOGV("PreviewPlayer: loadAudioMixSettings: "); 1577 mPreviewPlayerAudioMixSettings = pAudioMixSettings; 1578 return OK; 1579} 1580 1581status_t PreviewPlayer::setAudioMixPCMFileHandle( 1582 M4OSA_Context pAudioMixPCMFileHandle) { 1583 1584 LOGV("PreviewPlayer: setAudioMixPCMFileHandle: "); 1585 mAudioMixPCMFileHandle = pAudioMixPCMFileHandle; 1586 return OK; 1587} 1588 1589status_t PreviewPlayer::setAudioMixStoryBoardParam( 1590 M4OSA_UInt32 audioMixStoryBoardTS, 1591 M4OSA_UInt32 currentMediaBeginCutTime, 1592 M4OSA_UInt32 primaryTrackVolValue ) { 1593 1594 mAudioMixStoryBoardTS = audioMixStoryBoardTS; 1595 mCurrentMediaBeginCutTime = currentMediaBeginCutTime; 1596 mCurrentMediaVolumeValue = primaryTrackVolValue; 1597 return OK; 1598} 1599 1600status_t PreviewPlayer::setPlaybackBeginTime(uint32_t msec) { 1601 1602 mPlayBeginTimeMsec = msec; 1603 return OK; 1604} 1605 1606status_t PreviewPlayer::setPlaybackEndTime(uint32_t msec) { 1607 1608 mPlayEndTimeMsec = msec; 1609 return OK; 1610} 1611 1612status_t PreviewPlayer::setStoryboardStartTime(uint32_t msec) { 1613 1614 mStoryboardStartTimeMsec = msec; 1615 mDecVideoTsStoryBoard = mStoryboardStartTimeMsec*1000LL; 1616 return OK; 1617} 1618 1619status_t PreviewPlayer::setProgressCallbackInterval(uint32_t cbInterval) { 1620 1621 mProgressCbInterval = cbInterval; 1622 return OK; 1623} 1624 1625 1626status_t PreviewPlayer::setMediaRenderingMode( 1627 M4xVSS_MediaRendering mode, 1628 M4VIDEOEDITING_VideoFrameSize outputVideoSize) { 1629 1630 mRenderingMode = mode; 1631 1632 /* reset boolean for each clip*/ 1633 mVideoResizedOrCropped = false; 1634 1635 switch(outputVideoSize) { 1636 case M4VIDEOEDITING_kSQCIF: 1637 mOutputVideoWidth = 128; 1638 mOutputVideoHeight = 96; 1639 break; 1640 1641 case M4VIDEOEDITING_kQQVGA: 1642 mOutputVideoWidth = 160; 1643 mOutputVideoHeight = 120; 1644 break; 1645 1646 case M4VIDEOEDITING_kQCIF: 1647 mOutputVideoWidth = 176; 1648 mOutputVideoHeight = 144; 1649 break; 1650 1651 case M4VIDEOEDITING_kQVGA: 1652 mOutputVideoWidth = 320; 1653 mOutputVideoHeight = 240; 1654 break; 1655 1656 case M4VIDEOEDITING_kCIF: 1657 mOutputVideoWidth = 352; 1658 mOutputVideoHeight = 288; 1659 break; 1660 1661 case M4VIDEOEDITING_kVGA: 1662 mOutputVideoWidth = 640; 1663 mOutputVideoHeight = 480; 1664 break; 1665 1666 case M4VIDEOEDITING_kWVGA: 1667 mOutputVideoWidth = 800; 1668 mOutputVideoHeight = 480; 1669 break; 1670 1671 case M4VIDEOEDITING_kNTSC: 1672 mOutputVideoWidth = 720; 1673 mOutputVideoHeight = 480; 1674 break; 1675 1676 case M4VIDEOEDITING_k640_360: 1677 mOutputVideoWidth = 640; 1678 mOutputVideoHeight = 360; 1679 break; 1680 1681 case M4VIDEOEDITING_k854_480: 1682 mOutputVideoWidth = 854; 1683 mOutputVideoHeight = 480; 1684 break; 1685 1686 case M4VIDEOEDITING_kHD1280: 1687 mOutputVideoWidth = 1280; 1688 mOutputVideoHeight = 720; 1689 break; 1690 1691 case M4VIDEOEDITING_kHD1080: 1692 mOutputVideoWidth = 1080; 1693 mOutputVideoHeight = 720; 1694 break; 1695 1696 case M4VIDEOEDITING_kHD960: 1697 mOutputVideoWidth = 960; 1698 mOutputVideoHeight = 720; 1699 break; 1700 1701 default: 1702 LOGE("unsupported output video size set"); 1703 return BAD_VALUE; 1704 } 1705 1706 return OK; 1707} 1708 1709M4OSA_ERR PreviewPlayer::doMediaRendering() { 1710 M4OSA_ERR err = M4NO_ERROR; 1711 M4VIFI_ImagePlane planeIn[3], planeOut[3]; 1712 M4VIFI_UInt8 *inBuffer = M4OSA_NULL, *finalOutputBuffer = M4OSA_NULL; 1713 M4VIFI_UInt8 *tempOutputBuffer= M4OSA_NULL; 1714 size_t videoBufferSize = 0; 1715 M4OSA_UInt32 frameSize = 0, i=0, index =0, nFrameCount =0, bufferOffset =0; 1716 int32_t colorFormat = 0; 1717 1718 if(!mIsVideoSourceJpg) { 1719 sp<MetaData> meta = mVideoSource->getFormat(); 1720 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 1721 } 1722 else { 1723 colorFormat = OMX_COLOR_FormatYUV420Planar; 1724 } 1725 1726 videoBufferSize = mVideoBuffer->size(); 1727 frameSize = (mVideoWidth*mVideoHeight*3) >> 1; 1728 1729 uint8_t* outBuffer; 1730 size_t outBufferStride = 0; 1731 1732 mVideoRenderer->getBuffer(&outBuffer, &outBufferStride); 1733 1734 bufferOffset = index*frameSize; 1735 inBuffer = (M4OSA_UInt8 *)mVideoBuffer->data()+ 1736 mVideoBuffer->range_offset()+bufferOffset; 1737 1738 1739 /* In plane*/ 1740 prepareYUV420ImagePlane(planeIn, mVideoWidth, 1741 mVideoHeight, (M4VIFI_UInt8 *)inBuffer, mReportedWidth, mReportedHeight); 1742 1743 // Set the output YUV420 plane to be compatible with YV12 format 1744 // W & H even 1745 // YVU instead of YUV 1746 // align buffers on 32 bits 1747 1748 //In YV12 format, sizes must be even 1749 M4OSA_UInt32 yv12PlaneWidth = ((mOutputVideoWidth +1)>>1)<<1; 1750 M4OSA_UInt32 yv12PlaneHeight = ((mOutputVideoHeight+1)>>1)<<1; 1751 1752 prepareYV12ImagePlane(planeOut, yv12PlaneWidth, yv12PlaneHeight, 1753 (M4OSA_UInt32)outBufferStride, (M4VIFI_UInt8 *)outBuffer); 1754 1755 1756 err = applyRenderingMode(planeIn, planeOut, mRenderingMode); 1757 1758 if(err != M4NO_ERROR) 1759 { 1760 LOGE("doMediaRendering: applyRenderingMode returned err=0x%x", (int)err); 1761 return err; 1762 } 1763 mVideoResizedOrCropped = true; 1764 1765 return err; 1766} 1767 1768status_t PreviewPlayer::resetJniCallbackTimeStamp() { 1769 1770 mDecVideoTsStoryBoard = mStoryboardStartTimeMsec*1000LL; 1771 return OK; 1772} 1773 1774void PreviewPlayer::postProgressCallbackEvent_l() { 1775 if (mProgressCbEventPending) { 1776 return; 1777 } 1778 mProgressCbEventPending = true; 1779 1780 mQueue.postEvent(mProgressCbEvent); 1781} 1782 1783 1784void PreviewPlayer::onProgressCbEvent() { 1785 Mutex::Autolock autoLock(mLock); 1786 if (!mProgressCbEventPending) { 1787 return; 1788 } 1789 mProgressCbEventPending = false; 1790 // If playback starts from previous I-frame, 1791 // then send frame storyboard duration 1792 if((mDecodedVideoTs/1000) < mPlayBeginTimeMsec) { 1793 notifyListener_l(MEDIA_INFO, 0, mDecVideoTsStoryBoard/1000); 1794 } 1795 else { 1796 notifyListener_l(MEDIA_INFO, 0, 1797 (((mDecodedVideoTs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec)); 1798 } 1799} 1800 1801void PreviewPlayer::postOverlayUpdateEvent_l() { 1802 if (mOverlayUpdateEventPending) { 1803 return; 1804 } 1805 mOverlayUpdateEventPending = true; 1806 mQueue.postEvent(mOverlayUpdateEvent); 1807} 1808 1809void PreviewPlayer::onUpdateOverlayEvent() { 1810 Mutex::Autolock autoLock(mLock); 1811 1812 if (!mOverlayUpdateEventPending) { 1813 return; 1814 } 1815 mOverlayUpdateEventPending = false; 1816 1817 int updateState; 1818 if (mOverlayUpdateEventPosted) { 1819 updateState = 1; 1820 } else { 1821 updateState = 0; 1822 } 1823 notifyListener_l(0xBBBBBBBB, updateState, mCurrFramingEffectIndex); 1824} 1825 1826 1827void PreviewPlayer::setVideoPostProcessingNode( 1828 M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable) { 1829 1830 uint32_t effect = VIDEO_EFFECT_NONE; 1831 1832 //Map M4VSS3GPP_VideoEffectType to local enum 1833 switch(type) { 1834 case M4VSS3GPP_kVideoEffectType_FadeFromBlack: 1835 effect = VIDEO_EFFECT_FADEFROMBLACK; 1836 break; 1837 1838 case M4VSS3GPP_kVideoEffectType_FadeToBlack: 1839 effect = VIDEO_EFFECT_FADETOBLACK; 1840 break; 1841 1842 case M4VSS3GPP_kVideoEffectType_CurtainOpening: 1843 effect = VIDEO_EFFECT_CURTAINOPEN; 1844 break; 1845 1846 case M4VSS3GPP_kVideoEffectType_CurtainClosing: 1847 effect = VIDEO_EFFECT_CURTAINCLOSE; 1848 break; 1849 1850 case M4xVSS_kVideoEffectType_BlackAndWhite: 1851 effect = VIDEO_EFFECT_BLACKANDWHITE; 1852 break; 1853 1854 case M4xVSS_kVideoEffectType_Pink: 1855 effect = VIDEO_EFFECT_PINK; 1856 break; 1857 1858 case M4xVSS_kVideoEffectType_Green: 1859 effect = VIDEO_EFFECT_GREEN; 1860 break; 1861 1862 case M4xVSS_kVideoEffectType_Sepia: 1863 effect = VIDEO_EFFECT_SEPIA; 1864 break; 1865 1866 case M4xVSS_kVideoEffectType_Negative: 1867 effect = VIDEO_EFFECT_NEGATIVE; 1868 break; 1869 1870 case M4xVSS_kVideoEffectType_Framing: 1871 effect = VIDEO_EFFECT_FRAMING; 1872 break; 1873 1874 case M4xVSS_kVideoEffectType_Fifties: 1875 effect = VIDEO_EFFECT_FIFTIES; 1876 break; 1877 1878 case M4xVSS_kVideoEffectType_ColorRGB16: 1879 effect = VIDEO_EFFECT_COLOR_RGB16; 1880 break; 1881 1882 case M4xVSS_kVideoEffectType_Gradient: 1883 effect = VIDEO_EFFECT_GRADIENT; 1884 break; 1885 1886 default: 1887 effect = VIDEO_EFFECT_NONE; 1888 break; 1889 } 1890 1891 if(enable == M4OSA_TRUE) { 1892 //If already set, then no need to set again 1893 if(!(mCurrentVideoEffect & effect)) { 1894 mCurrentVideoEffect |= effect; 1895 if(effect == VIDEO_EFFECT_FIFTIES) { 1896 mIsFiftiesEffectStarted = true; 1897 } 1898 } 1899 } 1900 else { 1901 //Reset only if already set 1902 if(mCurrentVideoEffect & effect) { 1903 mCurrentVideoEffect &= ~effect; 1904 } 1905 } 1906} 1907 1908status_t PreviewPlayer::setImageClipProperties(uint32_t width,uint32_t height) { 1909 mVideoWidth = width; 1910 mVideoHeight = height; 1911 return OK; 1912} 1913 1914 1915M4OSA_ERR PreviewPlayer::doVideoPostProcessing() { 1916 M4OSA_ERR err = M4NO_ERROR; 1917 vePostProcessParams postProcessParams; 1918 int32_t colorFormat = 0; 1919 1920 1921 if(!mIsVideoSourceJpg) { 1922 sp<MetaData> meta = mVideoSource->getFormat(); 1923 CHECK(meta->findInt32(kKeyColorFormat, &colorFormat)); 1924 } 1925 else { 1926 colorFormat = OMX_COLOR_FormatYUV420Planar; 1927 } 1928 1929 if((colorFormat == OMX_COLOR_FormatYUV420SemiPlanar) || 1930 (colorFormat == 0x7FA30C00)) { 1931 LOGE("doVideoPostProcessing: colorFormat YUV420Sp not supported"); 1932 return M4ERR_UNSUPPORTED_MEDIA_TYPE; 1933 } 1934 1935 postProcessParams.vidBuffer = (M4VIFI_UInt8*)mVideoBuffer->data() 1936 + mVideoBuffer->range_offset(); 1937 1938 postProcessParams.videoWidth = mVideoWidth; 1939 postProcessParams.videoHeight = mVideoHeight; 1940 postProcessParams.timeMs = mDecodedVideoTs/1000; 1941 postProcessParams.timeOffset = mDecVideoTsStoryBoard/1000; 1942 postProcessParams.effectsSettings = mEffectsSettings; 1943 postProcessParams.numberEffects = mNumberEffects; 1944 postProcessParams.outVideoWidth = mOutputVideoWidth; 1945 postProcessParams.outVideoHeight = mOutputVideoHeight; 1946 postProcessParams.currentVideoEffect = mCurrentVideoEffect; 1947 postProcessParams.renderingMode = mRenderingMode; 1948 if(mIsFiftiesEffectStarted == M4OSA_TRUE) { 1949 postProcessParams.isFiftiesEffectStarted = M4OSA_TRUE; 1950 mIsFiftiesEffectStarted = M4OSA_FALSE; 1951 } 1952 else { 1953 postProcessParams.isFiftiesEffectStarted = M4OSA_FALSE; 1954 } 1955 1956 postProcessParams.overlayFrameRGBBuffer = mFrameRGBBuffer; 1957 postProcessParams.overlayFrameYUVBuffer = mFrameYUVBuffer; 1958 mVideoRenderer->getBuffer(&(postProcessParams.pOutBuffer), &(postProcessParams.outBufferStride)); 1959 err = applyEffectsAndRenderingMode(&postProcessParams, mReportedWidth, mReportedHeight); 1960 1961 return err; 1962} 1963 1964status_t PreviewPlayer::readFirstVideoFrame() { 1965 LOGV("PreviewPlayer::readFirstVideoFrame"); 1966 1967 if (!mVideoBuffer) { 1968 MediaSource::ReadOptions options; 1969 if (mSeeking != NO_SEEK) { 1970 LOGV("LV PLAYER seeking to %lld us (%.2f secs)", mSeekTimeUs, 1971 mSeekTimeUs / 1E6); 1972 1973 options.setSeekTo( 1974 mSeekTimeUs, MediaSource::ReadOptions::SEEK_CLOSEST); 1975 } 1976 for (;;) { 1977 status_t err = mVideoSource->read(&mVideoBuffer, &options); 1978 options.clearSeekTo(); 1979 1980 if (err != OK) { 1981 CHECK_EQ(mVideoBuffer, NULL); 1982 1983 if (err == INFO_FORMAT_CHANGED) { 1984 LOGV("LV PLAYER VideoSource signalled format change"); 1985 notifyVideoSize_l(); 1986 sp<MetaData> meta = mVideoSource->getFormat(); 1987 1988 CHECK(meta->findInt32(kKeyWidth, &mReportedWidth)); 1989 CHECK(meta->findInt32(kKeyHeight, &mReportedHeight)); 1990 1991 if (mVideoRenderer != NULL) { 1992 mVideoRendererIsPreview = false; 1993 err = initRenderer_l(); 1994 if (err != OK) { 1995 postStreamDoneEvent_l(err); 1996 } 1997 } 1998 continue; 1999 } 2000 LOGV("PreviewPlayer: onVideoEvent EOS reached."); 2001 mFlags |= VIDEO_AT_EOS; 2002 mFlags |= AUDIO_AT_EOS; 2003 postStreamDoneEvent_l(err); 2004 return OK; 2005 } 2006 2007 if (mVideoBuffer->range_length() == 0) { 2008 // Some decoders, notably the PV AVC software decoder 2009 // return spurious empty buffers that we just want to ignore. 2010 2011 mVideoBuffer->release(); 2012 mVideoBuffer = NULL; 2013 continue; 2014 } 2015 2016 int64_t videoTimeUs; 2017 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs)); 2018 if (mSeeking != NO_SEEK) { 2019 if (videoTimeUs < mSeekTimeUs) { 2020 // buffers are before seek time 2021 // ignore them 2022 mVideoBuffer->release(); 2023 mVideoBuffer = NULL; 2024 continue; 2025 } 2026 } else { 2027 if((videoTimeUs/1000) < mPlayBeginTimeMsec) { 2028 // buffers are before begin cut time 2029 // ignore them 2030 mVideoBuffer->release(); 2031 mVideoBuffer = NULL; 2032 continue; 2033 } 2034 } 2035 break; 2036 } 2037 } 2038 2039 int64_t timeUs; 2040 CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &timeUs)); 2041 2042 { 2043 Mutex::Autolock autoLock(mMiscStateLock); 2044 mVideoTimeUs = timeUs; 2045 } 2046 2047 mDecodedVideoTs = timeUs; 2048 2049 return OK; 2050 2051} 2052 2053status_t PreviewPlayer::getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs) { 2054 *lastRenderedTimeMs = (((mDecodedVideoTs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec); 2055 return OK; 2056} 2057 2058} // namespace android 2059