Merge "Fixed issue 3443078 overlay flicker on transition in exported video"
diff --git a/libvideoeditor/lvpp/PreviewPlayer.cpp b/libvideoeditor/lvpp/PreviewPlayer.cpp
index 0892ce7..d0b84d8 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.cpp
+++ b/libvideoeditor/lvpp/PreviewPlayer.cpp
@@ -182,6 +182,7 @@
     mLastVideoBuffer = NULL;
     mSuspensionState = NULL;
     mEffectsSettings = NULL;
+    mVeAudioPlayer = NULL;
     mAudioMixStoryBoardTS = 0;
     mCurrentMediaBeginCutTime = 0;
     mCurrentMediaVolumeValue = 0;
@@ -471,8 +472,38 @@
     return play_l();
 }
 
+status_t PreviewPlayer::startAudioPlayer_l() {
+    CHECK(!(mFlags & AUDIO_RUNNING));
+
+    if (mAudioSource == NULL || mAudioPlayer == NULL) {
+        return OK;
+    }
+
+    if (!(mFlags & AUDIOPLAYER_STARTED)) {
+        mFlags |= AUDIOPLAYER_STARTED;
+
+        // We've already started the MediaSource in order to enable
+        // the prefetcher to read its data.
+        status_t err = mVeAudioPlayer->start(
+                true /* sourceAlreadyStarted */);
+
+        if (err != OK) {
+            notifyListener_l(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
+            return err;
+        }
+    } else {
+        mVeAudioPlayer->resume();
+    }
+
+    mFlags |= AUDIO_RUNNING;
+
+    mWatchForAudioEOS = true;
+
+    return OK;
+}
+
 status_t PreviewPlayer::play_l() {
-VideoEditorAudioPlayer  *mVePlayer;
+
     if (mFlags & PLAYING) {
         return OK;
     }
@@ -496,44 +527,41 @@
             if (mAudioSink != NULL) {
 
                 mAudioPlayer = new VideoEditorAudioPlayer(mAudioSink, this);
-                mVePlayer =
+                mVeAudioPlayer =
                           (VideoEditorAudioPlayer*)mAudioPlayer;
 
                 mAudioPlayer->setSource(mAudioSource);
 
-                mVePlayer->setAudioMixSettings(
+                mVeAudioPlayer->setAudioMixSettings(
                  mPreviewPlayerAudioMixSettings);
 
-                mVePlayer->setAudioMixPCMFileHandle(
+                mVeAudioPlayer->setAudioMixPCMFileHandle(
                  mAudioMixPCMFileHandle);
 
-                mVePlayer->setAudioMixStoryBoardSkimTimeStamp(
+                mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
                  mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
                  mCurrentMediaVolumeValue);
 
-                // We've already started the MediaSource in order to enable
-                // the prefetcher to read its data.
-                status_t err = mVePlayer->start(
-                        true /* sourceAlreadyStarted */);
-
-                if (err != OK) {
-                    delete mAudioPlayer;
-                    mAudioPlayer = NULL;
-
-                    mFlags &= ~(PLAYING | FIRST_FRAME);
-                    return err;
-                }
-
-                mTimeSource = mVePlayer; //mAudioPlayer;
+                mTimeSource = mVeAudioPlayer; //mAudioPlayer;
 
                 deferredAudioSeek = true;
                 mWatchForAudioSeekComplete = false;
                 mWatchForAudioEOS = true;
             }
-        } else {
-            mVePlayer->resume();
-        }
+         }
 
+        CHECK(!(mFlags & AUDIO_RUNNING));
+
+        if (mVideoSource == NULL) {
+            status_t err = startAudioPlayer_l();
+
+            if (err != OK) {
+                delete mAudioPlayer;
+                mAudioPlayer = NULL;
+                mFlags &= ~(PLAYING | FIRST_FRAME);
+                return err;
+            }
+        }
     }
 
     if (mTimeSource == NULL && mAudioPlayer == NULL) {
@@ -547,6 +575,7 @@
         MediaBuffer *aLocalBuffer;
         options.setSeekTo(mSeekTimeUs);
         mVideoSource->read(&aLocalBuffer, &options);
+        aLocalBuffer->release();
     }
 
     if (mVideoSource != NULL) {
@@ -761,8 +790,9 @@
             // locations, we'll "pause" the audio source, causing it to
             // stop reading input data until a subsequent seek.
 
-            if (mAudioPlayer != NULL) {
+            if (mAudioPlayer != NULL && (mFlags & AUDIO_RUNNING)) {
                 mAudioPlayer->pause();
+                mFlags &= ~AUDIO_RUNNING;
             }
             mAudioSource->pause();
         }
@@ -794,8 +824,9 @@
                     if (mVideoRenderer != NULL) {
                         mVideoRendererIsPreview = false;
                         err = initRenderer_l();
-                           if ( err != OK )
-                        postStreamDoneEvent_l(err); // santosh
+                        if (err != OK) {
+                            postStreamDoneEvent_l(err);
+                        }
 
                     }
                     continue;
@@ -862,6 +893,13 @@
 
     bool wasSeeking = mSeeking;
     finishSeekIfNecessary(timeUs);
+    if (mAudioPlayer != NULL && !(mFlags & (AUDIO_RUNNING))) {
+        status_t err = startAudioPlayer_l();
+        if (err != OK) {
+            LOGE("Starting the audio player failed w/ err %d", err);
+            return;
+        }
+    }
 
     TimeSource *ts = (mFlags & AUDIO_AT_EOS) ? &mSystemTimeSource : mTimeSource;
 
@@ -919,8 +957,9 @@
         mVideoRendererIsPreview = false;
 
         status_t err = initRenderer_l();
-        if ( err != OK )
-        postStreamDoneEvent_l(err); // santosh
+        if (err != OK) {
+            postStreamDoneEvent_l(err);
+        }
     }
 
     // If timestamp exceeds endCutTime of clip, donot render
@@ -1743,6 +1782,11 @@
 status_t PreviewPlayer::readFirstVideoFrame() {
     LOGV("PreviewPlayer::readFirstVideoFrame");
 
+    if (mFlags & SEEK_PREVIEW) {
+        mFlags &= ~SEEK_PREVIEW;
+        return OK;
+    }
+
     if (!mVideoBuffer) {
         MediaSource::ReadOptions options;
         if (mSeeking) {
@@ -1770,8 +1814,9 @@
                     if (mVideoRenderer != NULL) {
                         mVideoRendererIsPreview = false;
                         err = initRenderer_l();
-                        if ( err != OK )
-                                postStreamDoneEvent_l(err); // santosh
+                        if (err != OK) {
+                            postStreamDoneEvent_l(err);
+                        }
                     }
                     continue;
                 }
diff --git a/libvideoeditor/lvpp/PreviewPlayer.h b/libvideoeditor/lvpp/PreviewPlayer.h
index 564c014..e06836e 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.h
+++ b/libvideoeditor/lvpp/PreviewPlayer.h
@@ -128,6 +128,7 @@
     static bool ContinuePreparation(void *cookie);
     void onPrepareAsyncEvent();
     void finishAsyncPrepare_l();
+    status_t startAudioPlayer_l();
 
     sp<PreviewPlayerRenderer> mVideoRenderer;
 
@@ -218,6 +219,8 @@
     status_t prepare_l();
     status_t prepareAsync_l();
 
+    VideoEditorAudioPlayer  *mVeAudioPlayer;
+
     PreviewPlayer(const PreviewPlayer &);
     PreviewPlayer &operator=(const PreviewPlayer &);
 };
diff --git a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
index e699fbe..48d43b4 100755
--- a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
@@ -38,6 +38,7 @@
 
     LOGV("VideoEditorAudioPlayer");
     mBGAudioPCMFileHandle = NULL;
+    mAudioProcess = NULL;
     mBGAudioPCMFileLength = 0;
     mBGAudioPCMFileTrimmedLength = 0;
     mBGAudioPCMFileDuration = 0;
diff --git a/libvideoeditor/lvpp/VideoEditorPlayer.cpp b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
index 46b18d9..7df6669 100755
--- a/libvideoeditor/lvpp/VideoEditorPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
@@ -304,7 +304,7 @@
 void VideoEditorPlayer::VeAudioOutput::setMinBufferCount() {

 

     mIsOnEmulator = false;

-    mMinBufferCount =12;

+    mMinBufferCount = 4;

 }

 

 bool VideoEditorPlayer::VeAudioOutput::isOnEmulator() {

@@ -368,8 +368,8 @@
 

     // Check argument "bufferCount" against the mininum buffer count

     if (bufferCount < mMinBufferCount) {

-        LOGD("bufferCount (%d) is too small and increased to %d",

-         bufferCount, mMinBufferCount);

+        LOGV("bufferCount (%d) is too small and increased to %d",

+            bufferCount, mMinBufferCount);

         bufferCount = mMinBufferCount;

 

     }

diff --git a/libvideoeditor/vss/stagefrightshells/src/VideoEditorBuffer.c b/libvideoeditor/vss/stagefrightshells/src/VideoEditorBuffer.c
index 9f50a58..e4de140 100755
--- a/libvideoeditor/vss/stagefrightshells/src/VideoEditorBuffer.c
+++ b/libvideoeditor/vss/stagefrightshells/src/VideoEditorBuffer.c
@@ -208,7 +208,7 @@
 
     /**
      * Initialize all the buffers in the pool */
-    for(index = 0; index< pool->NB; index++)
+    for(index = 0; index < pool->NB; index++)
     {
         pool->pNXPBuffer[index].pData = M4OSA_NULL;
         pool->pNXPBuffer[index].pData = (M4OSA_Void*)M4OSA_malloc(
@@ -218,10 +218,10 @@
         {
             for (j = 0; j < index; j++)
             {
-                if(M4OSA_NULL != pool->pNXPBuffer[index].pData)
+                if(M4OSA_NULL != pool->pNXPBuffer[j].pData)
                 {
-                    M4OSA_free((M4OSA_MemAddr32)pool->pNXPBuffer[index].pData);
-                    pool->pNXPBuffer[index].pData = M4OSA_NULL;
+                    M4OSA_free((M4OSA_MemAddr32)pool->pNXPBuffer[j].pData);
+                    pool->pNXPBuffer[j].pData = M4OSA_NULL;
                 }
             }
             err = M4ERR_ALLOC;
diff --git a/libvideoeditor/vss/stagefrightshells/src/VideoEditorVideoDecoder.cpp b/libvideoeditor/vss/stagefrightshells/src/VideoEditorVideoDecoder.cpp
index a8b9c0c..5f6b90c 100755
--- a/libvideoeditor/vss/stagefrightshells/src/VideoEditorVideoDecoder.cpp
+++ b/libvideoeditor/vss/stagefrightshells/src/VideoEditorVideoDecoder.cpp
@@ -745,9 +745,9 @@
         cropRight = vWidth - 1;
         cropBottom = vHeight - 1;
 
-        LOGI("got dimensions only %d x %d", width, height);
+        LOGV("got dimensions only %d x %d", width, height);
     } else {
-        LOGI("got crop rect %d, %d, %d, %d",
+        LOGV("got crop rect %d, %d, %d, %d",
              cropLeft, cropTop, cropRight, cropBottom);
     }