AudioOut volume changes
authorsrinivasa.mr <srinivasa.mr@samsung.com>
Tue, 18 Jun 2013 10:05:23 +0000 (19:05 +0900)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Tue, 18 Jun 2013 10:48:18 +0000 (10:48 +0000)
Change-Id: I296b43b19305ecc1e6e1efc62161ae8065aaf5ad
Signed-off-by: srinivasa.mr <srinivasa.mr@samsung.com>
src/FMedia_AudioOutImpl.cpp
src/inc/FMedia_AudioOutImpl.h

index 9e46fda..9adab57 100644 (file)
@@ -292,8 +292,10 @@ _AudioOutImpl::Prepare(AudioSampleType audioSampleType, AudioChannelType audioCh
                r = MapExceptionToResult(ret);
                SysLogException(NID_MEDIA, r, "[%s] Failed to perform audio_out_get_buffer_size operation with error code : %x", GetErrorMessage(r), ret);
        }
-
+       // this is done to get better Stop time
        __audioOutOptimalBufferSize = 1024;
+       __pFeedBuffer.reset(new (std::nothrow) ByteBuffer);
+       __pFeedBuffer->Construct(__audioOutOptimalBufferSize);
        __presentSampleType = audioSampleType;
        __presentSampleRate = audioSampleRate;
 
@@ -343,6 +345,8 @@ _AudioOutImpl::Prepare(AudioStreamType audioStreamType, AudioSampleType audioSam
        }
 
        __audioOutOptimalBufferSize = 1024;
+       __pFeedBuffer.reset(new (std::nothrow) ByteBuffer);
+       __pFeedBuffer->Construct(__audioOutOptimalBufferSize);
        __presentSampleType = audioSampleType;
        __presentSampleRate = audioSampleRate;
 
@@ -480,6 +484,8 @@ _AudioOutImpl::Run(void)
        result r = E_SUCCESS;
        int ret = 0;
        bool isSuccesful = true;
+       int feedSize = 0;
+       int index = 0;
        while (!__stop)
        {
                if (isSuccesful)
@@ -500,36 +506,53 @@ _AudioOutImpl::Run(void)
 
                        SysTryCatch(NID_MEDIA, pBuffer, r = E_INVALID_ARG, E_INVALID_ARG,
                                "[E_INVALID_ARG] Invalid argument is used. The buffer to write is null.");
-
-                       if ((__volume >= 0) && (__volume < 100))
-                       {
-                               PreProcess(pBuffer);
-                       }
                        isSuccesful = false;
                }
 
                buffer_size = pBuffer->GetLimit();
                pData = (unsigned char*) pBuffer->GetPointer();
                SysTryCatch(NID_MEDIA, pData, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The data to write is null");
-               if (!__stop)
+               index = 0;
+               while (!__stop && buffer_size)
                {
-                       ret = audio_out_write(__audioOutHandle, (void*) pData, buffer_size);
-                       if (ret != buffer_size)
+                       if (buffer_size > __audioOutOptimalBufferSize)
                        {
-                               SysLog(NID_MEDIA, "The size of actual written data is %d", ret);
+                               feedSize = __audioOutOptimalBufferSize;
+                               buffer_size = buffer_size - __audioOutOptimalBufferSize;
+                               SysLog(NID_MEDIA, "index - %d buffer_size - %d", index, feedSize);
                        }
                        else
                        {
-                               isSuccesful = true;
-                               if (!__stop)
-                               {
-                                       __pBufferSync->Enter();
-                                       __bufferQueue.Dequeue(pBuffer);
-                                       __pBufferSync->Exit();
-                                       pBuffer = NULL;
-                                       SendBufferEndReachedEvent();
-
-                               }
+                               feedSize = buffer_size;
+                               buffer_size = 0;
+                               SysLog(NID_MEDIA, "index - %d buffer_size - %d", index, feedSize);
+                       }
+                       if ((__volume >= 0) && (__volume < 100))
+                       {
+                               PreProcess(pBuffer, index, feedSize);
+                               ret = audio_out_write(__audioOutHandle, (void*) __pFeedBuffer->GetPointer(), feedSize);
+                       }
+                       else
+                       {
+                               pData = pData + index;
+                               ret = audio_out_write(__audioOutHandle, (void*) pData, feedSize);
+                       }
+                       if (ret != feedSize)
+                       {
+                               SysLog(NID_MEDIA, "The size of actual written data is %d", ret);
+                       }
+                       index = index + feedSize;
+               }
+               if (buffer_size == 0)
+               {
+                       isSuccesful = true;
+                       if (!__stop)
+                       {
+                               __pBufferSync->Enter();
+                               __bufferQueue.Dequeue(pBuffer);
+                               __pBufferSync->Exit();
+                               pBuffer = NULL;
+                               SendBufferEndReachedEvent();
                        }
                }
        }
@@ -659,12 +682,13 @@ _AudioOutImpl::ScaleDownVolume(unsigned char* pDataBuffer, int bytes)
        {
        case AUDIO_TYPE_PCM_U8:
        {
+               unsigned char* pOutput = __pFeedBuffer->GetPointer();
                unsigned char* pData = pDataBuffer;
                int noOfSamples = bytes;
                for (int i = 0; i < noOfSamples; i++)
                {
-                       int v = (((*pData - 128) * __volume256 + 128) >> 8) + 128;
-                       *pData++ = av_clip_uint8(v);
+                       int v = (((*pData++ - 128) * __volume256 + 128) >> 8) + 128;
+                       *pOutput++ = av_clip_uint8(v);
                }
        }
        break;
@@ -672,11 +696,12 @@ _AudioOutImpl::ScaleDownVolume(unsigned char* pDataBuffer, int bytes)
        case AUDIO_TYPE_PCM_S16_LE:
        {
                short* pData = (short*) pDataBuffer;
+               short* pOutput = (short*) __pFeedBuffer->GetPointer();
                int noOfSamples = bytes >> 1;
                for (int i = 0; i < noOfSamples; i++)
                {
-                       int v = ((*pData) * __volume256 + 128) >> 8;
-                       *pData++ = av_clip_int16(v);
+                       int v = ((*pData++) * __volume256 + 128) >> 8;
+                       *pOutput++ = av_clip_int16(v);
                }
        }
        break;
@@ -687,10 +712,9 @@ _AudioOutImpl::ScaleDownVolume(unsigned char* pDataBuffer, int bytes)
 }
 
 void
-_AudioOutImpl::PreProcess(ByteBuffer* pBuffer)
+_AudioOutImpl::PreProcess(ByteBuffer* pBuffer, int index, int size)
 {
        unsigned char* pDataBuffer = null;
-       int bytes = 0;
        if (pBuffer->GetLimit() > 0)
        {
                pDataBuffer = (unsigned char*) pBuffer->GetPointer();
@@ -698,7 +722,7 @@ _AudioOutImpl::PreProcess(ByteBuffer* pBuffer)
                {
                        return;
                }
-               bytes = pBuffer->GetLimit();
+               pDataBuffer = pDataBuffer + index;
        }
        else
        {
@@ -706,7 +730,7 @@ _AudioOutImpl::PreProcess(ByteBuffer* pBuffer)
        }
        if ((__volume256 < 256) && (__volume256 >= 0))
        {
-               ScaleDownVolume(pDataBuffer, bytes);
+               ScaleDownVolume(pDataBuffer, size);
        }
 }
 
index 1443fd5..8e98ecf 100644 (file)
@@ -88,7 +88,7 @@ private:
        result SendAudioFocusChanged(void);
        void DownSample(unsigned char* pDataBuffer, int bytes);
        void ScaleDownVolume(unsigned char* pDataBuffer, int bytes);
-       void PreProcess(Tizen::Base::ByteBuffer* pBuffer);
+       void PreProcess(Tizen::Base::ByteBuffer* pBuffer, int index, int size);
        result MapExceptionToResult(int reason);
        void HandleInterrupted(audio_io_interrupted_code_e code);
 
@@ -107,6 +107,7 @@ private:
        int __presentSampleRate;
        audio_out_h __audioOutHandle;
        Tizen::Base::Collection::QueueT <Tizen::Base::ByteBuffer*> __bufferQueue;
+       std::unique_ptr<Tizen::Base::ByteBuffer> __pFeedBuffer;
        int __audioOutOptimalBufferSize;
        bool __stop;
        bool __interruptFlag;