Fix for 3369917 Audio skips at clip edit points

Change-Id: Iba66585cc2e679475d8db529d6113586b58e626e
diff --git a/libvideoeditor/lvpp/DummyAudioSource.cpp b/libvideoeditor/lvpp/DummyAudioSource.cpp
index 04f8dc9..70f5944 100755
--- a/libvideoeditor/lvpp/DummyAudioSource.cpp
+++ b/libvideoeditor/lvpp/DummyAudioSource.cpp
@@ -97,7 +97,12 @@
     LOG2("DummyAudioSource::~DummyAudioSource");
 }
 
-
+void DummyAudioSource::setDuration (int64_t audioDurationUs) {
+    Mutex::Autolock autoLock(mLock);
+    LOG2("SetDuration %lld", mAudioDurationUs);
+    mAudioDurationUs += audioDurationUs;
+    LOG2("SetDuration %lld", mAudioDurationUs);
+}
 
 status_t DummyAudioSource::start(MetaData *params) {
     status_t err = OK;
@@ -143,7 +148,7 @@
     meta->setInt32(kKeySampleRate, mSamplingRate);
     meta->setInt64(kKeyDuration, mFrameDurationUs);
 
-     meta->setCString(kKeyDecoderComponent, "DummyAudioSource");
+    meta->setCString(kKeyDecoderComponent, "DummyAudioSource");
 
     return meta;
 }
@@ -159,11 +164,14 @@
     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
         CHECK(seekTimeUs >= 0);
         mTimeStampUs = seekTimeUs;
-     }
-
-    if (mTimeStampUs >= mAudioDurationUs) {
-        *out = NULL;
-        return ERROR_END_OF_STREAM;
+    }
+    {
+        Mutex::Autolock autoLock(mLock);
+        if (mTimeStampUs >= mAudioDurationUs) {
+            *out = NULL;
+            LOGI("EOS reached");
+            return ERROR_END_OF_STREAM;
+        }
     }
 
     err = mBufferGroup->acquire_buffer(&buffer);
diff --git a/libvideoeditor/lvpp/DummyAudioSource.h b/libvideoeditor/lvpp/DummyAudioSource.h
index a562bc1..6e6ead4 100755
--- a/libvideoeditor/lvpp/DummyAudioSource.h
+++ b/libvideoeditor/lvpp/DummyAudioSource.h
@@ -45,8 +45,9 @@
     virtual status_t start(MetaData *params = NULL);

     virtual status_t stop();

     virtual sp<MetaData> getFormat();

-    virtual status_t read (MediaBuffer **buffer, 

+    virtual status_t read (MediaBuffer **buffer,

                             const MediaSource::ReadOptions *options = NULL);

+    void setDuration (int64_t audioDurationUs);

 

 protected:

     DummyAudioSource (int32_t samplingRate,

@@ -63,6 +64,8 @@
     int64_t mAudioDurationUs;

     int64_t mTimeStampUs;

     int32_t mNbBuffer;

+    Mutex mLock;

+

     MediaBufferGroup *mBufferGroup;

 

     DummyAudioSource(const DummyAudioSource &);

diff --git a/libvideoeditor/lvpp/PreviewPlayer.cpp b/libvideoeditor/lvpp/PreviewPlayer.cpp
index d6233fa..617a1b6 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.cpp
+++ b/libvideoeditor/lvpp/PreviewPlayer.cpp
@@ -192,11 +192,12 @@
     mProgressCbInterval = 0;
     mNumberDecVideoFrames = 0;
     mOverlayUpdateEventPosted = false;
+    mIsChangeSourceRequired = true;
 
     mVideoEvent = new PreviewPlayerEvent(this, &PreviewPlayer::onVideoEvent);
     mVideoEventPending = false;
     mStreamDoneEvent = new PreviewPlayerEvent(this,
-         &AwesomePlayer::onStreamDone);
+         &PreviewPlayer::onStreamDone);
 
     mStreamDoneEventPending = false;
 
@@ -396,7 +397,10 @@
 
     mTimeSource = NULL;
 
-    delete mAudioPlayer;
+    //Single audio player instance used
+    //So donot delete it here
+    //It is deleted from PreviewController class
+    //delete mAudioPlayer;
     mAudioPlayer = NULL;
 
     if (mLastVideoBuffer) {
@@ -489,6 +493,93 @@
     return OK;
 }
 
+status_t PreviewPlayer::setAudioPlayer(AudioPlayer *audioPlayer) {
+    Mutex::Autolock autoLock(mLock);
+    CHECK(!(mFlags & PLAYING));
+    mAudioPlayer = audioPlayer;
+
+    LOGV("SetAudioPlayer");
+    mIsChangeSourceRequired = true;
+    mVeAudioPlayer =
+            (VideoEditorAudioPlayer*)mAudioPlayer;
+
+    // check if the new and old source are dummy
+    sp<MediaSource> anAudioSource = mVeAudioPlayer->getSource();
+    if (anAudioSource == NULL) {
+        // Audio player does not have any source set.
+        LOGV("setAudioPlayer: Audio player does not have any source set");
+        return OK;
+    }
+
+    const char *pSrcType1;
+    const char *pSrcType2;
+    sp<MetaData> meta = anAudioSource->getFormat();
+
+    if (meta->findCString(kKeyDecoderComponent, &pSrcType1)) {
+        if (strcmp(pSrcType1, "DummyAudioSource") == 0) {
+            meta = mAudioSource->getFormat();
+            if (meta->findCString(kKeyDecoderComponent, &pSrcType2)) {
+                if (strcmp(pSrcType2, "DummyAudioSource") == 0) {
+                    mIsChangeSourceRequired = false;
+                    // Just set the new play duration for the existing source
+                    MediaSource *pMediaSrc = anAudioSource.get();
+                    DummyAudioSource *pDummyAudioSource = (DummyAudioSource*)pMediaSrc;
+                    //Increment the duration of audio source
+                    pDummyAudioSource->setDuration((int64_t)((mPlayEndTimeMsec)*1000));
+                }
+            }
+        }
+    }
+
+    return OK;
+}
+
+void PreviewPlayer::onStreamDone() {
+    // Posted whenever any stream finishes playing.
+
+    Mutex::Autolock autoLock(mLock);
+    if (!mStreamDoneEventPending) {
+        return;
+    }
+    mStreamDoneEventPending = false;
+
+    if (mStreamDoneStatus != ERROR_END_OF_STREAM) {
+        LOGV("MEDIA_ERROR %d", mStreamDoneStatus);
+
+        notifyListener_l(
+                MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, mStreamDoneStatus);
+
+        pause_l(true /* at eos */);
+
+        mFlags |= AT_EOS;
+        return;
+    }
+
+    const bool allDone =
+        (mVideoSource == NULL || (mFlags & VIDEO_AT_EOS))
+            && (mAudioSource == NULL || (mFlags & AUDIO_AT_EOS));
+
+    if (!allDone) {
+        return;
+    }
+
+    if (mFlags & (LOOPING | AUTO_LOOPING)) {
+        seekTo_l(0);
+
+        if (mVideoSource != NULL) {
+            postVideoEvent_l();
+        }
+    } else {
+        LOGV("MEDIA_PLAYBACK_COMPLETE");
+        //pause before sending event
+        pause_l(true /* at eos */);
+        notifyListener_l(MEDIA_PLAYBACK_COMPLETE);
+
+        mFlags |= AT_EOS;
+    }
+}
+
+
 status_t PreviewPlayer::play_l() {
 
     mFlags &= ~SEEK_PREVIEW;
@@ -531,25 +622,75 @@
                  mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
                  mCurrentMediaVolumeValue);
 
-                mTimeSource = mVeAudioPlayer; //mAudioPlayer;
+                 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) {
+                    //delete mAudioPlayer;
+                    mAudioPlayer = NULL;
+
+                    mFlags &= ~(PLAYING | FIRST_FRAME);
+                    return err;
+                }
+
+                mTimeSource = mVeAudioPlayer;
+                mFlags |= AUDIO_RUNNING;
                 deferredAudioSeek = true;
                 mWatchForAudioSeekComplete = false;
                 mWatchForAudioEOS = true;
             }
-         }
+        } else {
+            mVeAudioPlayer = (VideoEditorAudioPlayer*)mAudioPlayer;
+            bool isAudioPlayerStarted = mVeAudioPlayer->isStarted();
 
-        CHECK(!(mFlags & AUDIO_RUNNING));
+            if (mIsChangeSourceRequired == true) {
+                LOGV("play_l: Change audio source required");
 
-        if (mVideoSource == NULL) {
-            status_t err = startAudioPlayer_l();
+                if (isAudioPlayerStarted == true) {
+                    mVeAudioPlayer->pause();
+                }
 
-            if (err != OK) {
-                delete mAudioPlayer;
-                mAudioPlayer = NULL;
-                mFlags &= ~(PLAYING | FIRST_FRAME);
-                return err;
+                mVeAudioPlayer->setSource(mAudioSource);
+                mVeAudioPlayer->setObserver(this);
+
+                mVeAudioPlayer->setAudioMixSettings(
+                 mPreviewPlayerAudioMixSettings);
+
+                mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
+                    mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
+                    mCurrentMediaVolumeValue);
+
+                if (isAudioPlayerStarted == true) {
+                    mVeAudioPlayer->resume();
+                } else {
+                    status_t err = OK;
+                    err = mVeAudioPlayer->start(true);
+                    if (err != OK) {
+                        mAudioPlayer = NULL;
+                        mVeAudioPlayer = NULL;
+
+                        mFlags &= ~(PLAYING | FIRST_FRAME);
+                        return err;
+                    }
+                }
+            } else {
+                LOGV("play_l: No Source change required");
+                mVeAudioPlayer->setAudioMixStoryBoardSkimTimeStamp(
+                    mAudioMixStoryBoardTS, mCurrentMediaBeginCutTime,
+                    mCurrentMediaVolumeValue);
+
+                mVeAudioPlayer->resume();
             }
+
+            mFlags |= AUDIOPLAYER_STARTED;
+            mFlags |= AUDIO_RUNNING;
+            mTimeSource = mVeAudioPlayer;
+            deferredAudioSeek = true;
+            mWatchForAudioSeekComplete = false;
+            mWatchForAudioEOS = true;
         }
     }
 
@@ -828,6 +969,7 @@
                 finishSeekIfNecessary(-1);
                 LOGV("PreviewPlayer: onVideoEvent EOS reached.");
                 mFlags |= VIDEO_AT_EOS;
+                mFlags |= AUDIO_AT_EOS;
                 mOverlayUpdateEventPosted = false;
                 postStreamDoneEvent_l(err);
                 // Set the last decoded timestamp to duration
@@ -1094,6 +1236,11 @@
         postStreamDoneEvent_l(ERROR_END_OF_STREAM);
     }
     else {
+        if (wasSeeking && (mFlags & SEEK_PREVIEW)) {
+            mFlags &= ~SEEK_PREVIEW;
+            return;
+        }
+
         if(!mIsVideoSourceJpg) {
             postVideoEvent_l(0);
         }
@@ -1818,6 +1965,7 @@
                 }
                 LOGV("PreviewPlayer: onVideoEvent EOS reached.");
                 mFlags |= VIDEO_AT_EOS;
+                mFlags |= AUDIO_AT_EOS;
                 postStreamDoneEvent_l(err);
                 return OK;
             }
diff --git a/libvideoeditor/lvpp/PreviewPlayer.h b/libvideoeditor/lvpp/PreviewPlayer.h
index 4d3a312..0800115 100644
--- a/libvideoeditor/lvpp/PreviewPlayer.h
+++ b/libvideoeditor/lvpp/PreviewPlayer.h
@@ -93,6 +93,7 @@
     status_t setImageClipProperties(uint32_t width, uint32_t height);
     status_t readFirstVideoFrame();
     status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
+    status_t setAudioPlayer(AudioPlayer *audioPlayer);
 
 private:
     friend struct PreviewPlayerEvent;
@@ -123,11 +124,13 @@
     status_t initAudioDecoder();
     status_t initVideoDecoder(uint32_t flags = 0);
     void onVideoEvent();
+    void onStreamDone();
     status_t finishSetDataSource_l();
     static bool ContinuePreparation(void *cookie);
     void onPrepareAsyncEvent();
     void finishAsyncPrepare_l();
     status_t startAudioPlayer_l();
+    bool mIsChangeSourceRequired;
 
     sp<PreviewPlayerRenderer> mVideoRenderer;
 
diff --git a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
index 48d43b4..f33bd0a 100755
--- a/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorAudioPlayer.cpp
@@ -48,6 +48,7 @@
     mBGAudioStoryBoardCurrentMediaBeginCutTS = 0;
     mBGAudioStoryBoardCurrentMediaVolumeVal = 0;
     mSeekTimeUs = 0;
+    mSource = NULL;
 }
 
 VideoEditorAudioPlayer::~VideoEditorAudioPlayer() {
@@ -61,9 +62,30 @@
         mAudioProcess = NULL;
     }
 }
+void VideoEditorAudioPlayer::setSource(const sp<MediaSource> &source) {
+    Mutex::Autolock autoLock(mLock);
+    mSource = source;
+    mReachedEOS = false;
+}
+
+sp<MediaSource> VideoEditorAudioPlayer::getSource() {
+    Mutex::Autolock autoLock(mLock);
+    return mSource;
+}
+
+void VideoEditorAudioPlayer::setObserver(AwesomePlayer *observer) {
+    LOGV("setObserver");
+    //CHECK(!mStarted);
+    mObserver = observer;
+}
+
+
+bool VideoEditorAudioPlayer::isStarted() {
+    return mStarted;
+}
 
 status_t VideoEditorAudioPlayer::start(bool sourceAlreadyStarted) {
-
+    Mutex::Autolock autoLock(mLock);
     CHECK(!mStarted);
     CHECK(mSource != NULL);
     LOGV("Start");
@@ -85,11 +107,16 @@
     veAudMixSettings audioMixSettings;
 
     // Pass on the audio ducking parameters
-    audioMixSettings.lvInDucking_threshold = mAudioMixSettings->uiInDucking_threshold;
-    audioMixSettings.lvInDucking_lowVolume = ((M4OSA_Float)mAudioMixSettings->uiInDucking_lowVolume) / 100.0;
-    audioMixSettings.lvInDucking_enable = mAudioMixSettings->bInDucking_enable;
-    audioMixSettings.lvPTVolLevel = ((M4OSA_Float)mBGAudioStoryBoardCurrentMediaVolumeVal) / 100.0;
-    audioMixSettings.lvBTVolLevel = ((M4OSA_Float)mAudioMixSettings->uiAddVolume) /100.0;
+    audioMixSettings.lvInDucking_threshold =
+        mAudioMixSettings->uiInDucking_threshold;
+    audioMixSettings.lvInDucking_lowVolume =
+        ((M4OSA_Float)mAudioMixSettings->uiInDucking_lowVolume) / 100.0;
+    audioMixSettings.lvInDucking_enable =
+        mAudioMixSettings->bInDucking_enable;
+    audioMixSettings.lvPTVolLevel =
+        ((M4OSA_Float)mBGAudioStoryBoardCurrentMediaVolumeVal) / 100.0;
+    audioMixSettings.lvBTVolLevel =
+        ((M4OSA_Float)mAudioMixSettings->uiAddVolume) / 100.0;
     audioMixSettings.lvBTChannelCount = mAudioMixSettings->uiBTChannelCount;
     audioMixSettings.lvPTChannelCount = mAudioMixSettings->uiNbChannels;
 
@@ -306,6 +333,33 @@
     return OK;
 }
 
+void VideoEditorAudioPlayer::resume() {
+
+    veAudMixSettings audioMixSettings;
+
+    // Single audio player is used;
+    // Pass on the audio ducking parameters
+    // which might have changed with new audio source
+    audioMixSettings.lvInDucking_threshold =
+        mAudioMixSettings->uiInDucking_threshold;
+    audioMixSettings.lvInDucking_lowVolume =
+        ((M4OSA_Float)mAudioMixSettings->uiInDucking_lowVolume) / 100.0;
+    audioMixSettings.lvInDucking_enable =
+        mAudioMixSettings->bInDucking_enable;
+    audioMixSettings.lvPTVolLevel =
+        ((M4OSA_Float)mBGAudioStoryBoardCurrentMediaVolumeVal) / 100.0;
+    audioMixSettings.lvBTVolLevel =
+        ((M4OSA_Float)mAudioMixSettings->uiAddVolume) / 100.0;
+    audioMixSettings.lvBTChannelCount = mAudioMixSettings->uiBTChannelCount;
+    audioMixSettings.lvPTChannelCount = mAudioMixSettings->uiNbChannels;
+
+    // Call to Audio mix param setting
+    mAudioProcess->veSetAudioProcessingParams(audioMixSettings);
+
+    //Call the base class
+    AudioPlayer::resume();
+}
+
 void VideoEditorAudioPlayer::reset() {
 
     LOGV("reset");
@@ -336,9 +390,9 @@
     size_t size_remaining = size;
 
     M4OSA_ERR err = M4NO_ERROR;
-    M4AM_Buffer bgFrame = {NULL, 0};
-    M4AM_Buffer mixFrame = {NULL, 0};
-    M4AM_Buffer ptFrame = {NULL, 0};
+    M4AM_Buffer16 bgFrame = {NULL, 0};
+    M4AM_Buffer16 mixFrame = {NULL, 0};
+    M4AM_Buffer16 ptFrame = {NULL, 0};
     int64_t currentSteamTS = 0;
     int64_t startTimeForBT = 0;
     M4OSA_Float fPTVolLevel =
@@ -384,7 +438,11 @@
 
                 mIsFirstBuffer = false;
             } else {
-                status = mSource->read(&mInputBuffer, &options);
+
+                {
+                    Mutex::Autolock autoLock(mLock);
+                    status = mSource->read(&mInputBuffer, &options);
+                }
                 // Data is Primary Track, mix with background track
                 // after reading same size from Background track PCM file
                 if (status == OK)
@@ -395,9 +453,9 @@
                           (int64_t)(mAudioMixSettings->uiAddCts * 1000)) {
 
                         LOGV("VideoEditorAudioPlayer::INSIDE MIXING");
-                        LOGV("Checking %lld <= %lld - %d",
+                        LOGV("Checking %lld <= %lld",
                             mBGAudioPCMFileSeekPoint-mBGAudioPCMFileOriginalSeekPoint,
-                            mBGAudioPCMFileTrimmedLength, len);
+                            mBGAudioPCMFileTrimmedLength);
 
 
                         M4OSA_Void* ptr;
diff --git a/libvideoeditor/lvpp/VideoEditorAudioPlayer.h b/libvideoeditor/lvpp/VideoEditorAudioPlayer.h
index 92b77b6..f5232cf 100755
--- a/libvideoeditor/lvpp/VideoEditorAudioPlayer.h
+++ b/libvideoeditor/lvpp/VideoEditorAudioPlayer.h
@@ -49,6 +49,7 @@
     virtual ~VideoEditorAudioPlayer();

 

     status_t start(bool sourceAlreadyStarted = false);

+    void resume();

 

     void setAudioMixSettings(M4xVSS_AudioMixingSettings* pAudioMixSettings);

     void setAudioMixPCMFileHandle(M4OSA_Context pBGAudioPCMFileHandle);

@@ -57,6 +58,11 @@
         M4OSA_UInt32 pBGAudioCurrentMediaBeginCutTS,

         M4OSA_UInt32 pBGAudioCurrentMediaVolumeVal);

 

+    void setObserver(AwesomePlayer *observer);

+    void setSource(const sp<MediaSource> &source);

+    sp<MediaSource> getSource();

+

+    bool isStarted();

 private:

 

     M4xVSS_AudioMixingSettings *mAudioMixSettings;

diff --git a/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp b/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp
index 2049f08..28208d1 100755
--- a/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp
+++ b/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp
@@ -50,9 +50,9 @@
 M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(

         void *pPTBuffer, void *pBTBuffer, void *pOutBuffer) {

 

-    M4AM_Buffer* pPrimaryTrack   = (M4AM_Buffer*)pPTBuffer;

-    M4AM_Buffer* pBackgroundTrack = (M4AM_Buffer*)pBTBuffer;

-    M4AM_Buffer* pMixedOutBuffer  = (M4AM_Buffer*)pOutBuffer;

+    M4AM_Buffer16* pPrimaryTrack   = (M4AM_Buffer16*)pPTBuffer;

+    M4AM_Buffer16* pBackgroundTrack = (M4AM_Buffer16*)pBTBuffer;

+    M4AM_Buffer16* pMixedOutBuffer  = (M4AM_Buffer16*)pOutBuffer;

 

     LOGV("VideoEditorBGAudioProcessing::lvProcessAudioMixNDuck \

      pPTBuffer 0x%x pBTBuffer 0x%x pOutBuffer 0x%x", pPTBuffer,

diff --git a/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.h b/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.h
index 8c54e15..24586a7 100755
--- a/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.h
+++ b/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.h
@@ -31,7 +31,7 @@
 typedef struct {

     M4OSA_UInt16*   m_dataAddress; // Android SRC needs a Int16 pointer

     M4OSA_UInt32    m_bufferSize;

-} M4AM_Buffer;

+} M4AM_Buffer16;    // Structure contains Int16_t pointer

 

 // Following struct will be used by app to supply the PT and BT properties

 // along with ducking values

@@ -78,9 +78,9 @@
     M4OSA_Float mPTVolLevel;

     M4OSA_Float mBTVolLevel;

 

-    M4AM_Buffer mPTBuffer;

-    M4AM_Buffer mBTBuffer;

-    M4AM_Buffer mOutMixBuffer;

+    M4AM_Buffer16 mPTBuffer;

+    M4AM_Buffer16 mBTBuffer;

+    M4AM_Buffer16 mOutMixBuffer;

     M4OSA_Int16 *mTempBuffer;

     M4OSA_Int32 mTempFrameCount;

 

diff --git a/libvideoeditor/lvpp/VideoEditorPlayer.cpp b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
index ab2bb67..3e1743e 100755
--- a/libvideoeditor/lvpp/VideoEditorPlayer.cpp
+++ b/libvideoeditor/lvpp/VideoEditorPlayer.cpp
@@ -49,13 +49,16 @@
     return OK;

 }

 

+

+status_t VideoEditorPlayer::setAudioPlayer(VideoEditorAudioPlayer *audioPlayer) {

+    return mPlayer->setAudioPlayer(audioPlayer);

+}

+

+

 status_t VideoEditorPlayer::setDataSource(

         const char *url, const KeyedVector<String8, String8> *headers) {

     LOGI("setDataSource('%s')", url);

 

-    mVeAudioSink = new VeAudioOutput();

-    mPlayer->setAudioSink(mVeAudioSink);

-

     return mPlayer->setDataSource(url, headers);

 }

 

diff --git a/libvideoeditor/lvpp/VideoEditorPlayer.h b/libvideoeditor/lvpp/VideoEditorPlayer.h
index c61e33a..da00ca5 100755
--- a/libvideoeditor/lvpp/VideoEditorPlayer.h
+++ b/libvideoeditor/lvpp/VideoEditorPlayer.h
@@ -23,14 +23,14 @@
 #include "M4xVSS_API.h"

 #include "VideoEditorMain.h"

 #include "VideoEditorTools.h"

-

+#include "VideoEditorAudioPlayer.h"

 

 namespace android {

 

 struct PreviewPlayer;

 

 class VideoEditorPlayer : public MediaPlayerInterface {

-

+    public:

     class VeAudioOutput: public MediaPlayerBase::AudioSink

     {

     public:

@@ -144,6 +144,7 @@
     virtual status_t readFirstVideoFrame();

     virtual status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);

 

+    status_t setAudioPlayer(VideoEditorAudioPlayer *audioPlayer);

 private:

     PreviewPlayer       *mPlayer;

     sp<VeAudioOutput>    mVeAudioSink;

diff --git a/libvideoeditor/lvpp/VideoEditorPreviewController.cpp b/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
index 830648a..180f75e 100755
--- a/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
+++ b/libvideoeditor/lvpp/VideoEditorPreviewController.cpp
@@ -461,6 +461,13 @@
         mTarget = NULL;

     }

 

+    // Create Audio player to be used for entire

+    // storyboard duration

+    mVEAudioSink = new VideoEditorPlayer::VeAudioOutput();

+    mVEAudioPlayer = new VideoEditorAudioPlayer(mVEAudioSink);

+    mVEAudioPlayer->setAudioMixSettings(mBackgroundAudioSetting);

+    mVEAudioPlayer->setAudioMixPCMFileHandle(mAudioMixPCMFileHandle);

+

     LOGV("startPreview: loop = %d", loop);

     mPreviewLooping = loop;

 

@@ -671,6 +678,12 @@
             mVePlayer[playerInst] = NULL;

         }

     }

+    LOGV("stopPreview: clear audioSink and audioPlayer");

+    mVEAudioSink.clear();

+    if (mVEAudioPlayer) {

+        delete mVEAudioPlayer;

+        mVEAudioPlayer = NULL;

+    }

 

     // If image file playing, then free the buffer pointer

     if(mFrameStr.pBuffer != M4OSA_NULL) {

@@ -972,6 +985,7 @@
         LOGV("preparePlayer: seekTo(%d)",

          pController->mClipList[index]->uiBeginCutTime);

     }

+    pController->mVePlayer[pController->mCurrentPlayer]->setAudioPlayer(pController->mVEAudioPlayer);

 

     pController->mVePlayer[playerInstance]->readFirstVideoFrame();

     LOGV("preparePlayer: readFirstVideoFrame of clip");

diff --git a/libvideoeditor/lvpp/VideoEditorPreviewController.h b/libvideoeditor/lvpp/VideoEditorPreviewController.h
index 216b077..e9e4e84 100755
--- a/libvideoeditor/lvpp/VideoEditorPreviewController.h
+++ b/libvideoeditor/lvpp/VideoEditorPreviewController.h
@@ -20,6 +20,7 @@
 

 #include <utils/Log.h>

 #include "VideoEditorPlayer.h"

+#include "VideoEditorAudioPlayer.h"

 #include "M4OSA_Semaphore.h"

 #include "M4OSA_Thread.h"

 #include "M4OSA_Clock.h"

@@ -131,6 +132,9 @@
     M4OSA_Context mSemThreadWait;

     bool mIsFiftiesEffectStarted;

 

+    sp<VideoEditorPlayer::VeAudioOutput> mVEAudioSink;

+    VideoEditorAudioPlayer *mVEAudioPlayer;

+

     M4VIFI_UInt8*  mFrameRGBBuffer;

     M4VIFI_UInt8*  mFrameYUVBuffer;

     static M4OSA_ERR preparePlayer(void* param, int playerInstance, int index);