mmfenh/advancedaudiocontroller/audiocontrollerpluginsvariant/AdvancedAudioController/Src/AdvancedAudioEncoder.cpp
changeset 0 71ca22bcf22a
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  The functions in this module implements the common behavior
       
    15 *                for the audio encoder base class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include	"AdvancedAudioEncoder.h"
       
    23 #include 	<mmfdatabuffer.h>
       
    24 #include    "DebugMacros.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KEmptyBuffer        = 1;
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CAdvancedAudioEncoder::CAdvancedAudioEncoder(TInt aPriority)
       
    37     :   CActive(aPriority),
       
    38     	iState(EIdle),
       
    39         iSharedBuffers(NULL),
       
    40         iMMFDevSound(NULL),
       
    41         iObserver(NULL)
       
    42     {
       
    43 #ifdef _DEBUG
       
    44     RDebug::Print(_L("CAdvancedAudioEncoder::CAdvancedAudioEncoder()"));
       
    45 #endif
       
    46     }
       
    47 
       
    48 // Destructor
       
    49 EXPORT_C CAdvancedAudioEncoder::~CAdvancedAudioEncoder()
       
    50 	{
       
    51 #ifdef _DEBUG
       
    52     RDebug::Print(_L("CAdvancedAudioEncoder::~CAdvancedAudioEncoder()"));
       
    53 #endif
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CAdvancedAudioEncoder::SetDevSound
       
    58 // Sets the DevSound instance.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 EXPORT_C void CAdvancedAudioEncoder::SetDevSound(
       
    62 	CMMFDevSound& aDevSound )
       
    63 	{
       
    64 	iMMFDevSound = &aDevSound;
       
    65 	}
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CAdvancedAudioEncoder::SetObserver
       
    69 // Sets the observer instance.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 EXPORT_C void CAdvancedAudioEncoder::SetObserver(
       
    73 	MAdvancedAudioEncoderObserver& aObserver )
       
    74 	{
       
    75 	iObserver = &aObserver;
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CAdvancedAudioEncoder::SetSinkBuffers
       
    80 // Sets the sink buffer for encoding operations.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C void CAdvancedAudioEncoder::SetSinkBuffers(
       
    84 	RPointerArray<CMMFDataBuffer>* aBuffers )
       
    85 	{
       
    86 	iSharedBuffers = aBuffers;
       
    87 	}
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CAdvancedAudioEncoder::EmptyBuffer
       
    91 // Request to fill the specified buffer with converted data.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 EXPORT_C void CAdvancedAudioEncoder::EmptyBuffer(
       
    95 	CMMFBuffer* aBuffer)
       
    96 	{
       
    97 #ifdef _DEBUG
       
    98 //    RDebug::Print(_L("CAdvancedAudioEncoder::EmptyBuffer"));
       
    99 #endif
       
   100 	iBufferToEmpty = aBuffer; // store away the buffer
       
   101 	iState = EEncoding;
       
   102 
       
   103 	iRequest = KEmptyBuffer;
       
   104 	Ready(KErrNone);
       
   105     SetActive();
       
   106 	}
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CAdvancedAudioEncoder::Stop
       
   110 // Stops encoding process.
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C void CAdvancedAudioEncoder::Stop()
       
   114 	{
       
   115 	DP1(_L("CAdvancedAudioEncoder::Stop start iNextBuffer[%d]"), iNextBuffer);
       
   116 
       
   117     // There are 2 situations in which this function is called.
       
   118     // First when Devsound has already given a buffer to Encoder to be handled.
       
   119     // In which case, one needs to finish handling it by calling HandleEmptyBufferL().
       
   120     // Secondly, in a situation where the current shared buffer is being filled (partially full)
       
   121     // in which case, the buffer needs to be flushed
       
   122 	if (iBufferToEmpty && (iState == EEncoding))
       
   123 		{
       
   124 	    DP1(_L("CAdvancedAudioEncoder::Stop setting last buffer[%d]"), iBufferToEmpty);
       
   125 		iBufferToEmpty->SetLastBuffer(ETrue);
       
   126 		TRAPD(err, HandleEmptyBufferL());
       
   127 		if ( err )
       
   128 			{
       
   129 			DP1(_L("CAdvancedAudioEncoder::Stop err[%d]"), err);
       
   130 			iState = EIdle;
       
   131 			iObserver->SendEvent(TMMFEvent(KMMFEventCategoryPlaybackComplete, err));
       
   132 			}		
       
   133         }
       
   134 	else if (iNextBuffer)
       
   135 	    { // a record controller may call stop before buffers are initialized
       
   136         if (iNextBuffer->Status() == EBeingFilled)
       
   137             {
       
   138             DP1(_L("CAdvancedAudioEncoder::Stop setting buffer[%d] to EFull"), iNextBuffer);
       
   139             iNextBuffer->SetStatus(EFull);
       
   140             DP1(_L("CAdvancedAudioEncoder::Stop emptying buffer[%d]"), iNextBuffer);
       
   141             iObserver->EmptyBuffer(iNextBuffer);
       
   142             }
       
   143 	    }
       
   144 		
       
   145 	iState = EIdle;
       
   146     DP0(_L("CAdvancedAudioEncoder::Stop end"));
       
   147 	}
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CAdvancedAudioEncoder::Ready
       
   151 // Utility function to post a request complete
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C void CAdvancedAudioEncoder::Ready(
       
   155     const TInt aStatus)
       
   156     {
       
   157     TRequestStatus* stat = &iStatus;
       
   158     User::RequestComplete(stat, aStatus);
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CDevSoundAudioInput::NextSharedBufferL
       
   163 // Determine the next available shared buffer from the pool of shared buffers
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 EXPORT_C void CAdvancedAudioEncoder::NextSharedBufferL()
       
   167 	{
       
   168 	if ( (*iSharedBuffers)[iSharedBufferIndex]->Status() == EAvailable )
       
   169 		{
       
   170 		iNextBuffer = (*iSharedBuffers)[iSharedBufferIndex];
       
   171 		iSharedBufferIndex++;
       
   172 		if ( iSharedBufferIndex == iSharedBuffers->Count() )
       
   173 			{
       
   174 			iSharedBufferIndex = 0;
       
   175 			}
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 #ifdef _DEBUG
       
   180 	    RDebug::Print(_L("CAdvancedAudioEncoder::NextSharedBufferL - KErrOverflow"));
       
   181 #endif
       
   182 		User::Leave(KErrOverflow);
       
   183 		}
       
   184 	}
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CAdvancedAudioEncoder::RunL
       
   188 // Invoke by the active scheduler when a request completes
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 EXPORT_C void CAdvancedAudioEncoder::RunL()
       
   192 	{
       
   193 #ifdef _DEBUG
       
   194 //    RDebug::Print(_L("CAdvancedAudioEncoder::RunL"));
       
   195 #endif
       
   196     switch(iRequest)
       
   197 		{
       
   198 		case KEmptyBuffer:
       
   199 			if ( iState == EEncoding )
       
   200 				{
       
   201 				TRAPD(err, HandleEmptyBufferL());
       
   202 				if ( err )
       
   203 					{
       
   204 #ifdef _DEBUG
       
   205 					RDebug::Print(_L("CAdvancedAudioEncoder::RunL err[%d]"), err);
       
   206 #endif
       
   207 					// if error is encountered, the state is reset and event is generated and the recording is stopped.
       
   208 					//err = KErrDied;
       
   209 					iState = EIdle;
       
   210 					iObserver->SendEvent(TMMFEvent(KMMFEventCategoryPlaybackComplete, err));
       
   211 					}
       
   212 				}
       
   213 			break;
       
   214 
       
   215 		default:
       
   216 			break;
       
   217 		};
       
   218 	}
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CAdvancedAudioEncoder::DoCancel
       
   222 // Cancels the current and any on going requests/tasks.
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 EXPORT_C void CAdvancedAudioEncoder::DoCancel()
       
   226     {
       
   227     }
       
   228 
       
   229 // End of file