Fixed the SRC interface

bug - 3369860

Change-Id: I6b866d334af9c9aea1db0295bf19edbc4123293d
diff --git a/libvideoeditor/vss/common/inc/VideoEditorResampler.h b/libvideoeditor/vss/common/inc/VideoEditorResampler.h
index 2686212..fe9876c 100755
--- a/libvideoeditor/vss/common/inc/VideoEditorResampler.h
+++ b/libvideoeditor/vss/common/inc/VideoEditorResampler.h
@@ -29,6 +29,7 @@
 void LVAudiosetVolume(M4OSA_Int32 resamplerContext, M4OSA_Int16 left, M4OSA_Int16 right) ;
 void LVAudioresample_LowQuality(M4OSA_Int16* out, M4OSA_Int16* input,
                                      M4OSA_Int32 outFrameCount, M4OSA_Int32 resamplerContext);
+void LVDestroy(M4OSA_Int32 resamplerContext);
 
 void MonoTo2I_16( const M4OSA_Int16 *src,
                         M4OSA_Int16 *dst,
diff --git a/libvideoeditor/vss/mcs/src/M4MCS_API.c b/libvideoeditor/vss/mcs/src/M4MCS_API.c
index b2b341e..d04befa 100755
--- a/libvideoeditor/vss/mcs/src/M4MCS_API.c
+++ b/libvideoeditor/vss/mcs/src/M4MCS_API.c
@@ -2212,7 +2212,7 @@
     pC->iSsrcNbSamplIn = 0;
     pC->iSsrcNbSamplOut = 0;
     pC->SsrcScratch = M4OSA_NULL;
-
+    pC->pLVAudioResampler = M4OSA_NULL;
     /**
     * Audio encoder */
     pC->pAudioEncCtxt = M4OSA_NULL;
@@ -3378,6 +3378,12 @@
         pC->pSsrcBufferOut = M4OSA_NULL;
     }
 
+    if (pC->pLVAudioResampler != M4OSA_NULL)
+    {
+        LVDestroy((M4OSA_Int32)pC->pLVAudioResampler);
+        pC->pLVAudioResampler = M4OSA_NULL;
+    }
+
     /* ----- Free the audio encoder stuff ----- */
 
     if( M4OSA_NULL != pC->pAudioEncCtxt )
diff --git a/libvideoeditor/vss/src/M4VSS3GPP_AudioMixing.c b/libvideoeditor/vss/src/M4VSS3GPP_AudioMixing.c
index 157f200..c943513 100755
--- a/libvideoeditor/vss/src/M4VSS3GPP_AudioMixing.c
+++ b/libvideoeditor/vss/src/M4VSS3GPP_AudioMixing.c
@@ -189,6 +189,7 @@
     pC->ewc.pEncContext = M4OSA_NULL;
     pC->ewc.pDummyAuBuffer = M4OSA_NULL;
     pC->ewc.p3gpWriterContext = M4OSA_NULL;
+    pC->pLVAudioResampler = M4OSA_NULL;
     /**
     * Set the OSAL filesystem function set */
     pC->pOsaFileReadPtr = pFileReadPtrFct;
@@ -592,6 +593,12 @@
         pC->pTempBuffer = M4OSA_NULL;
     }
 
+    if (pC->pLVAudioResampler != M4OSA_NULL)
+    {
+        LVDestroy(pC->pLVAudioResampler);
+        pC->pLVAudioResampler = M4OSA_NULL;
+    }
+
     /**
     * Free the shells interfaces */
     M4VSS3GPP_unRegisterAllWriters(&pC->ShellAPI);
diff --git a/libvideoeditor/vss/src/VideoEditorResampler.cpp b/libvideoeditor/vss/src/VideoEditorResampler.cpp
index 82a2423..c4d1a8a 100755
--- a/libvideoeditor/vss/src/VideoEditorResampler.cpp
+++ b/libvideoeditor/vss/src/VideoEditorResampler.cpp
@@ -45,19 +45,27 @@
     int16_t* mInput;
     int nbChannels;
     int nbSamples;
+    M4OSA_Int32 outSamplingRate;
+    M4OSA_Int32 inSamplingRate;
 
 };
 
+#define MAX_SAMPLEDURATION_FOR_CONVERTION 40 //ms
 
 status_t VideoEditorResampler::getNextBuffer(AudioBufferProvider::Buffer *pBuffer) {
 
-    pBuffer->raw = (void*)(this->mInput);
+    uint32_t dataSize = pBuffer->frameCount * this->nbChannels * sizeof(int16_t);
+    int16_t *pTmpInBuffer = (int16_t*)malloc(dataSize);
+    memcpy(pTmpInBuffer, this->mInput, dataSize);
+    pBuffer->raw = (void*)pTmpInBuffer;
+
     return OK;
 }
 
 void VideoEditorResampler::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
 
     if(pBuffer->raw != NULL) {
+        free(pBuffer->raw);
         pBuffer->raw = NULL;
     }
     pBuffer->frameCount = 0;
@@ -74,9 +82,11 @@
     if (context->mResampler == NULL) {
         return NO_MEMORY;
     }
-    context->mResampler->setSampleRate(32000);
+    context->mResampler->setSampleRate(android::VideoEditorResampler::kFreq32000Hz);
     context->mResampler->setVolume(0x1000, 0x1000);
     context->nbChannels = inChannelCount;
+    context->outSamplingRate = sampleRate;
+    context->mInput = NULL;
 
     return ((M4OSA_Int32)context);
 }
@@ -91,9 +101,10 @@
      * nbSamples is calculated for 40ms worth of data;hence sample rate
      * is used to calculate the nbSamples
      */
-    context->nbSamples = inSampleRate / 25;
-    context->mInput = (int16_t*)malloc(context->nbSamples *
-                                   context->nbChannels * sizeof(int16_t));
+    context->inSamplingRate = inSampleRate;
+    // Allocate buffer for maximum allowed number of samples.
+    context->mInput = (int16_t*)malloc( (inSampleRate * MAX_SAMPLEDURATION_FOR_CONVERTION *
+                                   context->nbChannels * sizeof(int16_t)) / 1000);
 }
 
 void LVAudiosetVolume(M4OSA_Int32 resamplerContext, M4OSA_Int16 left, M4OSA_Int16 right) {
@@ -103,6 +114,26 @@
     context->mResampler->setVolume(left,right);
 }
 
+void LVDestroy(M4OSA_Int32 resamplerContext) {
+
+    VideoEditorResampler *context =
+       (VideoEditorResampler *)resamplerContext;
+
+    if (context->mInput != NULL) {
+        free(context->mInput);
+        context->mInput = NULL;
+    }
+
+    if (context->mResampler != NULL) {
+        delete context->mResampler;
+        context->mResampler = NULL;
+    }
+
+    if (context != NULL) {
+        delete context;
+        context = NULL;
+    }
+}
 
 void LVAudioresample_LowQuality(M4OSA_Int16* out, M4OSA_Int16* input,
                                      M4OSA_Int32 outFrameCount, M4OSA_Int32 resamplerContext) {
@@ -110,7 +141,10 @@
     VideoEditorResampler *context =
       (VideoEditorResampler *)resamplerContext;
     int32_t *pTmpBuffer = NULL;
+
+    context->nbSamples = (context->inSamplingRate * outFrameCount) / context->outSamplingRate;
     memcpy(context->mInput,input,(context->nbSamples * context->nbChannels * sizeof(int16_t)));
+
     /*
      SRC module always gives stereo output, hence 2 for stereo audio
     */