Fix for issue 3439595 : Movie studio playback previous frames

Change-Id: I0f1a6cdfa40f6c8eebe989116e01ba8c212d5872
diff --git a/libvideoeditor/lvpp/PreviewPlayer.cpp b/libvideoeditor/lvpp/PreviewPlayer.cpp
index 9bae14a..d6233fa 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.cpp
+++ b/libvideoeditor/lvpp/PreviewPlayer.cpp
@@ -830,6 +830,8 @@
                 mFlags |= VIDEO_AT_EOS;
                 mOverlayUpdateEventPosted = false;
                 postStreamDoneEvent_l(err);
+                // Set the last decoded timestamp to duration
+                mDecodedVideoTs = (mPlayEndTimeMsec*1000);
                 return;
             }
 
@@ -845,14 +847,23 @@
             int64_t videoTimeUs;
             CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs));
 
-            if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
-                // Frames are before begin cut time
-                // Donot render
-                mVideoBuffer->release();
-                mVideoBuffer = NULL;
-                continue;
+            if (mSeeking) {
+                if (videoTimeUs < mSeekTimeUs) {
+                    // buffers are before seek time
+                    // ignore them
+                    mVideoBuffer->release();
+                    mVideoBuffer = NULL;
+                    continue;
+                }
+            } else {
+                if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
+                    // Frames are before begin cut time
+                    // Donot render
+                    mVideoBuffer->release();
+                    mVideoBuffer = NULL;
+                    continue;
+                }
             }
-
             break;
         }
     }
@@ -867,7 +878,6 @@
         mVideoTimeUs = timeUs;
     }
 
-    mDecodedVideoTs = timeUs;
 
     if(!mStartNextPlayer) {
         int64_t playbackTimeRemaining = (mPlayEndTimeMsec*1000) - timeUs;
@@ -966,6 +976,8 @@
         postStreamDoneEvent_l(ERROR_END_OF_STREAM);
         return;
     }
+    // Capture the frame timestamp to be rendered
+    mDecodedVideoTs = timeUs;
 
     // Post processing to apply video effects
     for(i=0;i<mNumberEffects;i++) {
@@ -1821,15 +1833,23 @@
 
             int64_t videoTimeUs;
             CHECK(mVideoBuffer->meta_data()->findInt64(kKeyTime, &videoTimeUs));
-
-            if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
-                // buffers are before begin cut time
-                // ignore them
-                mVideoBuffer->release();
-                mVideoBuffer = NULL;
-                continue;
+            if (mSeeking) {
+                if (videoTimeUs < mSeekTimeUs) {
+                    // buffers are before seek time
+                    // ignore them
+                    mVideoBuffer->release();
+                    mVideoBuffer = NULL;
+                    continue;
+                }
+            } else {
+                if((videoTimeUs/1000) < mPlayBeginTimeMsec) {
+                    // buffers are before begin cut time
+                    // ignore them
+                    mVideoBuffer->release();
+                    mVideoBuffer = NULL;
+                    continue;
+                }
             }
-
             break;
         }
     }
@@ -1848,4 +1868,9 @@
 
 }
 
+status_t PreviewPlayer::getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs) {
+    *lastRenderedTimeMs = (((mDecodedVideoTs+mDecVideoTsStoryBoard)/1000)-mPlayBeginTimeMsec);
+    return OK;
+}
+
 }  // namespace android
diff --git a/libvideoeditor/lvpp/PreviewPlayer.h b/libvideoeditor/lvpp/PreviewPlayer.h
index dceffab..4d3a312 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.h
+++ b/libvideoeditor/lvpp/PreviewPlayer.h
@@ -92,7 +92,7 @@
     status_t resetJniCallbackTimeStamp();
     status_t setImageClipProperties(uint32_t width, uint32_t height);
     status_t readFirstVideoFrame();
-
+    status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
 
 private:
     friend struct PreviewPlayerEvent;
diff --git a/libvideoeditor/lvpp/VideoEditorPlayer.cpp b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
index 7df6669..ab2bb67 100755
--- a/libvideoeditor/lvpp/VideoEditorPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
@@ -277,6 +277,11 @@
     return mPlayer->readFirstVideoFrame();

 }

 

+status_t VideoEditorPlayer::getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs) {

+    mPlayer->getLastRenderedTimeMs(lastRenderedTimeMs);

+    return NO_ERROR;

+}

+

 /* Implementation of AudioSink interface */

 #undef LOG_TAG

 #define LOG_TAG "VeAudioSink"

diff --git a/libvideoeditor/lvpp/VideoEditorPlayer.h b/libvideoeditor/lvpp/VideoEditorPlayer.h
index 47d174d..c61e33a 100755
--- a/libvideoeditor/lvpp/VideoEditorPlayer.h
+++ b/libvideoeditor/lvpp/VideoEditorPlayer.h
@@ -142,7 +142,7 @@
     virtual status_t resetJniCallbackTimeStamp();

     virtual status_t setImageClipProperties(uint32_t width, uint32_t height);

     virtual status_t readFirstVideoFrame();

-

+    virtual status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);

 

 private:

     PreviewPlayer       *mPlayer;

diff --git a/libvideoeditor/lvpp/VideoEditorPreviewController.cpp b/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
index e6f6052..830648a 100755
--- a/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
+++ b/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
@@ -25,6 +25,7 @@
 

 VideoEditorPreviewController::VideoEditorPreviewController()

     : mCurrentPlayer(0),

+      mActivePlayerIndex(0),

       mThreadContext(NULL),

       mPlayerState(VePlayerIdle),

       mPrepareReqest(M4OSA_FALSE),

@@ -550,6 +551,7 @@
 

     // Start playing with player instance 0

     mCurrentPlayer = 0;

+    mActivePlayerIndex = 0;

 

     if(toMs == -1) {

         LOGV("startPreview: Preview till end of storyboard");

@@ -622,8 +624,9 @@
     return M4NO_ERROR;

 }

 

-M4OSA_ERR VideoEditorPreviewController::stopPreview() {

+M4OSA_UInt32 VideoEditorPreviewController::stopPreview() {

     M4OSA_ERR err = M4NO_ERROR;

+    uint32_t lastRenderedFrameTimeMs = 0;

     LOGV("stopPreview");

 

     // Stop the thread

@@ -658,6 +661,10 @@
                 LOGV("stop the player first");

                 mVePlayer[playerInst]->stop();

             }

+            if (playerInst == mActivePlayerIndex) {

+                // Return the last rendered frame time stamp

+                mVePlayer[mActivePlayerIndex]->getLastRenderedTimeMs(&lastRenderedFrameTimeMs);

+            }

 

             LOGV("stopPreview: clearing mVePlayer");

             mVePlayer[playerInst].clear();

@@ -685,7 +692,8 @@
     mOutputVideoWidth = 0;

     mOutputVideoHeight = 0;

 

-    return M4NO_ERROR;

+    LOGV("stopPreview() lastRenderedFrameTimeMs %ld", lastRenderedFrameTimeMs);

+    return lastRenderedFrameTimeMs;

 }

 

 M4OSA_ERR VideoEditorPreviewController::clearSurface(

@@ -1065,6 +1073,8 @@
              pController->mClipList[index]->uiBeginCutTime,

              pController->mClipList[index]->ClipProperties.uiClipAudioVolumePercentage);

         }

+        // Capture the active player being used

+        pController->mActivePlayerIndex = pController->mCurrentPlayer;

 

         pController->mVePlayer[pController->mCurrentPlayer]->start();

         LOGV("threadProc: started");

diff --git a/libvideoeditor/lvpp/VideoEditorPreviewController.h b/libvideoeditor/lvpp/VideoEditorPreviewController.h
index be40c7c..216b077 100755
--- a/libvideoeditor/lvpp/VideoEditorPreviewController.h
+++ b/libvideoeditor/lvpp/VideoEditorPreviewController.h
@@ -74,7 +74,7 @@
     M4OSA_ERR startPreview(M4OSA_UInt32 fromMS, M4OSA_Int32 toMs,

         M4OSA_UInt16 callBackAfterFrameCount, M4OSA_Bool loop) ;

 

-    M4OSA_ERR stopPreview();

+    M4OSA_UInt32 stopPreview();

 

     M4OSA_ERR renderPreviewFrame(const sp<Surface> &surface,

         VideoEditor_renderPreviewFrameStr* pFrameInfo,

@@ -122,6 +122,7 @@
     M4OSA_UInt32 mLastPreviewClipEndTime;

     M4OSA_UInt32 mVideoStoryBoardTimeMsUptoFirstPreviewClip;

     OverlayState mOverlayState;

+    int mActivePlayerIndex;

 

     M4xVSS_MediaRendering mRenderingMode;

     uint32_t mOutputVideoWidth;