multimediacommscontroller/mmccamrpayloadformat/src/amrpayloadformatread.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004-2006 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:    PayloadFormat plugin capable to read RTP payload containing
       
    15 *                AMR audio.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include <e32base.h>
       
    24 #include <mmf/common/mmffourcc.h>
       
    25 #include "amrpayloadformatread.h"
       
    26 #include "amrpayloadencoder.h"
       
    27 #include "amrpayloaddecoder.h"
       
    28 #include "amrpayloaddecoderoa.h"
       
    29 #include "amrpayloadformatutil.h"
       
    30 #include "amrcommonutil.h"
       
    31 #include "mccrtpdatasource.h"
       
    32 #include "mccuids.hrh"
       
    33 #include "mccinternaldef.h"
       
    34 #include "amrpayloadformatter.h"
       
    35 #include "mccredpayloadread.h"
       
    36 
       
    37 #ifdef VOIP_TRACE_ENABLED
       
    38 #include <voip_trace.h>
       
    39 #endif
       
    40 
       
    41 // CONSTANTS
       
    42 const TReal KTimeMultiplier = 0.001;
       
    43 
       
    44 // ============================= LOCAL FUNCTIONS ===============================
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CAmrPayloadFormatRead::CAmrPayloadFormatRead
       
    50 // C++ default constructor can NOT contain any code, that
       
    51 // might leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CAmrPayloadFormatRead::CAmrPayloadFormatRead ( ) :
       
    55     iIsNb( ETrue ), iSamplingRate( KAmrNbSampleRate ), iChannels( 1 ),
       
    56     iMode( EAmrFrame12_2 ), iCmr ( EAmrModeReq7 ), iStreamEncoder( TStreamEncoder() )
       
    57     {
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CAmrPayloadFormatRead::ConstructL
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CAmrPayloadFormatRead::ConstructL ( MDataSource* aSource )
       
    66     {
       
    67     User::LeaveIfNull( aSource );
       
    68 
       
    69     iRtpDataSource = aSource;
       
    70     iClip = aSource;
       
    71     iBufferToReadExists = EFalse;
       
    72 
       
    73     iFourCC.Set( TFourCC( ' ','A','M','R' ) );
       
    74 
       
    75     // Set default value of iChannels and iFramesPerPacket
       
    76     iFramesPerPacket = 1;
       
    77     iChannels = 1;
       
    78     
       
    79     // Initialize decoding state machine
       
    80     iStateMachine = CFormatDecodeStateMachine::NewL( this );
       
    81     iStateMachine->ChangeState( EDecodeIdle );
       
    82 
       
    83     iCurrentBuffer = EBufferOne;
       
    84     
       
    85     #ifdef FTD_ENABLED
       
    86     
       
    87     User::LeaveIfError( iJBufStatsQueue.OpenGlobal( KMccJBufferStats, 
       
    88         EOwnerProcess ) );    
       
    89     
       
    90     #endif        
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CAmrPayloadFormatRead::NewL
       
    95 //
       
    96 // Two-phased constructor.
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 CAmrPayloadFormatRead* CAmrPayloadFormatRead::NewL ( MDataSource* aSource )
       
   100     {
       
   101     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
   102 
       
   103     AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::NewL" );
       
   104     
       
   105     CAmrPayloadFormatRead* self = new ( ELeave ) CAmrPayloadFormatRead;
       
   106     CleanupStack::PushL ( self );
       
   107     self->ConstructL ( aSource );
       
   108     CleanupStack::Pop( self );
       
   109     return self;
       
   110     }
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CAmrPayloadFormatRead::~CAmrPayloadFormatRead
       
   114 //
       
   115 // Destructor.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CAmrPayloadFormatRead::~CAmrPayloadFormatRead ( )
       
   119     {
       
   120     // commented out CreateSourceBufferL now sets aReference to ETrue.
       
   121     // datapath doesn't clean this buffer any longer.
       
   122     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   123     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead: 0x%x", this );
       
   124     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iFrameBufferOne: 0x%x", iFrameBufferOne );
       
   125     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iFrameBufferTwo: 0x%x", iFrameBufferTwo );
       
   126     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iSourceBuffer: 0x%x", iSourceBuffer );
       
   127     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iStateMachine: 0x%x", iStateMachine );
       
   128     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iPayloadDecoder: 0x%x", iPayloadDecoder );
       
   129     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iRtpDataSource: 0x%x", iRtpDataSource );
       
   130     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iClip: 0x%x", iClip );
       
   131     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iDataPath: 0x%x", iDataPath );
       
   132     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::~CAmrPayloadFormatRead iBufferToRead: 0x%x", iBufferToRead );
       
   133     #endif
       
   134     
       
   135 
       
   136     delete iFrameBufferOne;
       
   137     delete iFrameBufferTwo;
       
   138     if ( iSourceBufOwnership )
       
   139         {
       
   140         delete iSourceBuffer;
       
   141         }
       
   142     iSourceBuffer = NULL;
       
   143 
       
   144     if ( iStateMachine )
       
   145         {
       
   146         iStateMachine->Cancel( );
       
   147         delete iStateMachine;
       
   148         }
       
   149     
       
   150     delete iPayloadDecoder;
       
   151     iRtpDataSource = NULL;
       
   152     iClip = NULL;
       
   153     iDataPath = NULL;
       
   154     iBufferToRead = NULL;
       
   155     iEventHandler = NULL;
       
   156 
       
   157     #ifdef FTD_ENABLED
       
   158     iJBufStatsQueue.Close();
       
   159     #endif
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CAmrPayloadFormatRead::Streams
       
   164 // Return number of audio streams for the given media
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 TUint CAmrPayloadFormatRead::Streams( TUid aMediaType ) const
       
   168     {
       
   169     if ( KUidMediaTypeAudio == aMediaType )
       
   170         {
       
   171         return 1;
       
   172         }
       
   173     else
       
   174         {
       
   175         return 0;
       
   176         }
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CAmrPayloadFormatRead::FrameTimeInterval
       
   181 // Return the frame time interval for the given media
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 TTimeIntervalMicroSeconds CAmrPayloadFormatRead::FrameTimeInterval( 
       
   185         TMediaId aMediaId ) const
       
   186     {
       
   187     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   188         {
       
   189         TInt hwFrametime = static_cast<TInt>( iCInfo.iHwFrameTime );
       
   190         return TTimeIntervalMicroSeconds( TInt64( hwFrametime ) );
       
   191         }
       
   192     else
       
   193         {
       
   194         return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CAmrPayloadFormatRead::FillBufferL
       
   200 // Fill Buffer
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CAmrPayloadFormatRead::FillBufferL( CMMFBuffer* aBuffer, 
       
   204         MDataSink* aConsumer, TMediaId aMediaId )
       
   205     {
       
   206     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   207     	AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillBufferL( )" );
       
   208     #endif
       
   209     
       
   210     User::LeaveIfNull( aBuffer );
       
   211     User::LeaveIfNull( aConsumer );
       
   212 
       
   213     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   214     	AMR_PAYLOAD_FORMAT_READ3 ( "CAmrPayloadFormatRead::FillBufferL( ) buffer 0x%x passed in with length %d bytes \n", aBuffer, aBuffer->BufferSize( ) );
       
   215     #endif
       
   216     #ifdef VOIP_TRACE_ENABLED
       
   217         VoipTrace( "%x %x %x %x", MCC_TRACE, MCC_AMR_PLF_READ_FILLBUFFERL,
       
   218             aBuffer, aConsumer );
       
   219     #endif
       
   220     
       
   221     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   222         {
       
   223         User::Leave( KErrNotSupported );
       
   224         }
       
   225 
       
   226     if ( KUidMmfDataBuffer != aBuffer->Type( ) )
       
   227         {
       
   228         User::Leave( KErrNotSupported );
       
   229         }
       
   230 
       
   231     iDataPath = aConsumer;
       
   232     iMediaId = aMediaId;
       
   233 
       
   234     // aBuffer is a reference to those frame buffers that AmrPayloadFormatRead owns
       
   235     aBuffer->SetLastBuffer( EFalse );
       
   236 
       
   237     iFillRequested = ETrue;
       
   238 
       
   239     if ( iCurrentBuffer == EBufferOne )
       
   240         {
       
   241         iFrameBufferTwo->SetStatus( EAvailable );
       
   242         }
       
   243     else
       
   244         {
       
   245         iFrameBufferOne->SetStatus( EAvailable );
       
   246         }
       
   247 
       
   248     if ( iBufferToReadExists ) // Payload has some frames not decoded
       
   249         {
       
   250         iStateMachine->ChangeState( ESourceDataReady );
       
   251         }
       
   252     else
       
   253         {
       
   254         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ 
       
   255         	AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillBufferL( ), Request data from source\n" );
       
   256         #endif
       
   257 
       
   258         FillSourceBufferL(); // No payload ask for it
       
   259         } 
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // CAmrPayloadFormatRead::DoRead
       
   264 // Reads data starting from the given position into the source buffer 
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 TBool CAmrPayloadFormatRead::DoRead( CMMFBuffer* aBuffer )
       
   268     {
       
   269     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   270         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::DoRead" );
       
   271     #endif
       
   272     #ifdef VOIP_MCC_DL_TRACE_ENABLED
       
   273         VoipTrace( "%x %x %x", MCC_TRACE, MCC_AMR_PLF_READ_DOREAD, aBuffer );
       
   274     #endif
       
   275     
       
   276     if ( !aBuffer )
       
   277         {
       
   278         return EFalse;
       
   279         }
       
   280 
       
   281     if ( EFull == aBuffer->Status( ) )
       
   282         {
       
   283         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   284         RDebug::Print ( _L ( "CAmrPayloadFormatRead::DoRead - Buffer already full" ) );
       
   285         #endif
       
   286 
       
   287         return EFalse;
       
   288         }
       
   289 
       
   290     if ( iNumOfFrames == 0 )
       
   291         {
       
   292         return EFalse;
       
   293         }
       
   294         
       
   295     TAmrFrame* frames = iPayloadDecoder->Frames( );
       
   296     TDes8& bufferDes = static_cast<CMMFDataBuffer*>( aBuffer )->Data();
       
   297     TInt byteIndex = bufferDes.Length( );
       
   298     
       
   299     TUint8* desPtr = const_cast<TUint8*>( bufferDes.Ptr( ) );
       
   300     iStreamEncoder.Initialize( desPtr, byteIndex, 0 );
       
   301 
       
   302    	#ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   303     	AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::DoRead - iCurrentFrame = %d",
       
   304     	iCurrentFrame );
       
   305     #endif
       
   306     
       
   307     TInt bitCount = iPayloadDecoder->SpeechBitCount( frames[iCurrentFrame].iFrameType );
       
   308     // Calculate byte count
       
   309     TInt nextFrameLen =
       
   310     	bitCount % TInt(KNumValue8) == 0 ? bitCount / TInt(KNumValue8) : bitCount / TInt(KNumValue8) + 1;
       
   311     
       
   312     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   313     	AMR_PAYLOAD_FORMAT_READ4 ( "CAmrPayloadFormatRead::DoRead - iCurrentFrame = %d iNumOfFrames = %d byteIndex = %d",
       
   314         	iCurrentFrame, iNumOfFrames, byteIndex );
       
   315     #endif
       
   316 
       
   317     // Construct 8-bit frame header.
       
   318     // +-+-+-+-+-+-+-+-+
       
   319     // |P|  FT   |Q|P|P|
       
   320     // +-+-+-+-+-+-+-+-+
       
   321     iStreamEncoder.Encode( 0, 1 );
       
   322     iStreamEncoder.Encode( TInt( frames[iCurrentFrame].iFrameType ), KNumValue4 );
       
   323     iStreamEncoder.Encode( frames[iCurrentFrame].iFrameQualityInd, 1 );
       
   324     iStreamEncoder.Encode( 0, KNumValue2 );
       
   325     
       
   326     if ( bitCount > 0 )
       
   327         {
       
   328         // Construct core frame part ( speech data )
       
   329         iStreamEncoder.Encode( frames[iCurrentFrame].iFrameData, 0, 
       
   330                                frames[iCurrentFrame].iBitIndex, bitCount );
       
   331         if ( iStreamEncoder.BitIndex( ) != 0 )
       
   332             {
       
   333             // Need padding to byte boundray
       
   334             iStreamEncoder.Encode( 0, KNumValue8 - iStreamEncoder.BitIndex( ) );
       
   335             }
       
   336         }
       
   337 
       
   338     // Update iCurrentFrame
       
   339     iCurrentFrame++;
       
   340 
       
   341     byteIndex = byteIndex + nextFrameLen + 1;       // Add 1 for frame header byte
       
   342 
       
   343     // Get next frame length
       
   344     if ( iCurrentFrame < iNumOfFrames )
       
   345         {
       
   346         bitCount = iPayloadDecoder->SpeechBitCount( frames[iCurrentFrame].iFrameType );
       
   347         // Calculate byte count
       
   348         nextFrameLen =
       
   349         	bitCount % (TInt) KNumValue8 == 0 ? bitCount / (TInt) KNumValue8 : bitCount / (TInt) KNumValue8 + 1;
       
   350         }
       
   351 
       
   352     bufferDes.SetLength( byteIndex );
       
   353     
       
   354     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   355     	AMR_PAYLOAD_FORMAT_READ4 ( "CAmrPayloadFormatRead::DoRead - after decoding iCurrentFrame = %d iNumOfFrames = %d byteIndex = %d",
       
   356         iCurrentFrame, iNumOfFrames, byteIndex );
       
   357     #endif
       
   358 
       
   359     if ( iCurrentFrame == iNumOfFrames )
       
   360         {
       
   361         // Packet is fully decoded.
       
   362         // If it is the last buffer from RtpSourceSink, then we are not
       
   363         // expecting more packets. Also SetLastBuffer to notify datapath.
       
   364         if( iBufferToRead->LastBuffer( ) )
       
   365             {
       
   366             aBuffer->SetLastBuffer( ETrue );
       
   367             aBuffer->SetStatus( EFull );
       
   368             }
       
   369         return ETrue;
       
   370         }
       
   371 
       
   372     return EFalse;
       
   373     }
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CAmrPayloadFormatRead::FillSinkBufferL
       
   377 // Read RTP payload and convert it into AMR frames.
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 void CAmrPayloadFormatRead::FillSinkBufferL( )
       
   381     {
       
   382     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   383         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSinkBuffer( )" );
       
   384     #endif
       
   385     #ifdef VOIP_MCC_DL_TRACE_ENABLED
       
   386         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_FILLSINKBUFFER );
       
   387     #endif    
       
   388 
       
   389     CMMFBuffer* curBuffer;
       
   390     if ( EBufferOne == iCurrentBuffer )
       
   391         {
       
   392         curBuffer = iFrameBufferOne;
       
   393         
       
   394         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   395         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSinkBuffer( ) - Current Buffer One" );
       
   396         #endif
       
   397         }
       
   398     else
       
   399         {
       
   400         curBuffer = iFrameBufferTwo;
       
   401         
       
   402         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   403         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSinkBuffer( ) - Current Buffer Two" );
       
   404         #endif
       
   405         }
       
   406 
       
   407 	curBuffer->SetStatus( EAvailable );
       
   408     TBool allFrmBuffered = DoRead( curBuffer );
       
   409 	curBuffer->SetFrameNumber(
       
   410 		iRecvHeader.iTimestamp + ( ( iCurrentFrame - 1 )
       
   411 	    * TUint( iCInfo.iHwFrameTime * iSamplingRate * KTimeMultiplier ) ) );
       
   412 
       
   413     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   414     AMR_PAYLOAD_FORMAT_READ2 ( "CAmrPayloadFormatRead::FillSinkBuffer - FRAMENUM: %u",
       
   415         curBuffer->FrameNumber() );
       
   416     #endif	                           
       
   417 
       
   418     // Do not wait for the buffer to be filled up. If we do, this will cause jitters in
       
   419     // comfort-noise ( SID frame ) playing since SID-frame packets take considerably less
       
   420     // data bytes and it will delay waiting for many SID-frame packets before forwarding them to 
       
   421     // DataPath for further play-out processing.
       
   422     // Instead, forward the data as soon as we receive and decode one packet
       
   423     // to avoid jitters in play-out.
       
   424     if ( allFrmBuffered )
       
   425         {   
       
   426         iBufferToReadExists = EFalse;
       
   427         }
       
   428 
       
   429 
       
   430     // Do not forward zero length buffers
       
   431     if ( curBuffer->BufferSize() > 0 )
       
   432         {
       
   433         iStateMachine->ChangeState( EEmptyDataToSink );
       
   434         }
       
   435     else
       
   436         {
       
   437         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ 
       
   438         	AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSinkBufferL( ), Request data from source\n" );
       
   439         #endif
       
   440         
       
   441         FillSourceBufferL(); // No payload ask for it                           
       
   442         }
       
   443  
       
   444     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   445    	AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSinkBuffer - DONE" );
       
   446     #endif
       
   447     }
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CAmrPayloadFormatRead::FillSourceBufferL
       
   451 // Send fill buffer request to RTP Data Source
       
   452 // -----------------------------------------------------------------------------
       
   453 //
       
   454 void CAmrPayloadFormatRead::FillSourceBufferL( )
       
   455     {
       
   456     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   457         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::FillSourceBufferL( )" );
       
   458     #endif 
       
   459     
       
   460     iClip->FillBufferL(iSourceBuffer, this, iMediaId );     
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // CAmrPayloadFormatRead::SendDataToSinkL
       
   465 // Send full frame buffer to Data Path
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CAmrPayloadFormatRead::SendDataToSinkL( )
       
   469     {
       
   470     #ifdef VOIP_MCC_DL_TRACE_ENABLED
       
   471         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SENDDATATOSINKL );
       
   472     #endif
       
   473 
       
   474     iFillRequested = EFalse;
       
   475           
       
   476     if ( EBufferOne == iCurrentBuffer )
       
   477         {
       
   478         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   479         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::SendDataToSinkL( ) - BufferOne"  );
       
   480         #endif
       
   481 
       
   482         iDataPath->BufferFilledL( iFrameBufferOne );
       
   483         iCurrentBuffer = EBufferTwo;
       
   484         if ( iBufferToReadExists && !iFrameBufferOne->LastBuffer( ) )
       
   485             {
       
   486             // More payload buffer is needed
       
   487             iStateMachine->ChangeState( ESourceDataReady );
       
   488             }
       
   489         }
       
   490     else
       
   491         {
       
   492         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   493         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::SendDataToSinkL( ) - BufferTwo" );
       
   494         #endif
       
   495 
       
   496         iDataPath->BufferFilledL( iFrameBufferTwo );
       
   497         iCurrentBuffer = EBufferOne;
       
   498         if ( iBufferToReadExists && !iFrameBufferTwo->LastBuffer( ) )
       
   499             {
       
   500             // More payload buffer is needed
       
   501             iStateMachine->ChangeState( ESourceDataReady );
       
   502             }
       
   503        }
       
   504 
       
   505     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   506     AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::SendDataToSinkL( ) - DONE" );
       
   507     #endif
       
   508     }
       
   509 
       
   510 // -----------------------------------------------------------------------------
       
   511 // CAmrPayloadFormatRead::CreateSourceBufferL
       
   512 // Create a source buffer for the given media and indicate in aReference if 
       
   513 // buffer is created.
       
   514 // -----------------------------------------------------------------------------
       
   515 //
       
   516 CMMFBuffer* CAmrPayloadFormatRead::CreateSourceBufferL( TMediaId aMediaId, 
       
   517         TBool &aReference )
       
   518     {
       
   519     #ifdef VOIP_TRACE_ENABLED
       
   520         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_CREATESOURCEBUFFERL );
       
   521     #endif
       
   522 
       
   523     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   524         {
       
   525         User::Leave( KErrNotSupported );
       
   526         }
       
   527 
       
   528     // the source buffers belong to AmrPayloadFormatRead not to datapath
       
   529     // aference should be set to ETrue and destried by AmrPayloadFormatRead itself.
       
   530     aReference = ETrue;
       
   531     return iFrameBufferOne; 
       
   532     }
       
   533 
       
   534 // -----------------------------------------------------------------------------
       
   535 // CAmrPayloadFormatRead::CreateSourceBufferL
       
   536 // Create a source buffer for the given media, setting frame size to match
       
   537 // the given sink buffer
       
   538 // -----------------------------------------------------------------------------
       
   539 //
       
   540 CMMFBuffer* CAmrPayloadFormatRead::CreateSourceBufferL( TMediaId aMediaId,
       
   541         CMMFBuffer& /*aSinkBuffer*/, TBool &aReference )
       
   542     {
       
   543     #ifdef VOIP_TRACE_ENABLED
       
   544         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_CREATESOURCEBUFFERL );
       
   545     #endif
       
   546 
       
   547     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   548         {
       
   549         User::Leave( KErrNotSupported );
       
   550         }
       
   551     
       
   552     return CreateSourceBufferL( aMediaId, aReference );
       
   553     }
       
   554 
       
   555 // -----------------------------------------------------------------------------
       
   556 // CAmrPayloadFormatRead::SourceDataTypeCode
       
   557 // Return the source data type ( four CC code ) for the given media
       
   558 // -----------------------------------------------------------------------------
       
   559 //
       
   560 TFourCC CAmrPayloadFormatRead::SourceDataTypeCode( TMediaId aMediaId )
       
   561     {
       
   562     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   563         {
       
   564         #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   565         	AMR_PAYLOAD_FORMAT_READ2( 
       
   566         		"CAmrPayloadFormatRead::SourceDataTypeCode: 0x%x", iFourCC.FourCC() );
       
   567         #endif
       
   568         return iFourCC;
       
   569         }
       
   570     else
       
   571         {
       
   572         return TFourCC( ); //defaults to 'NULL' fourCC
       
   573         }
       
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CAmrPayloadFormatRead::SetSourceDataTypeCode
       
   578 // Set the source data type to the given four CC code for the given media
       
   579 // -----------------------------------------------------------------------------
       
   580 //
       
   581 TInt CAmrPayloadFormatRead::SetSourceDataTypeCode( TFourCC aSourceFourCC, 
       
   582         TMediaId aMediaId )
       
   583     {
       
   584     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   585         	AMR_PAYLOAD_FORMAT_READ2( 
       
   586         		"CAmrPayloadFormatRead::SetSourceDataTypeCode: 0x%x", 
       
   587         		aSourceFourCC.FourCC() );
       
   588     #endif
       
   589     #ifdef VOIP_TRACE_ENABLED
       
   590         VoipTrace( "%x %x %d", MCC_TRACE, MCC_AMR_PLF_READ_SETSOURCEDATATYPECODE,
       
   591             aSourceFourCC.FourCC() );
       
   592     #endif    
       
   593     
       
   594     if ( KUidMediaTypeAudio != aMediaId.iMediaType ) 
       
   595         {
       
   596         return KErrNotSupported;
       
   597         }
       
   598 
       
   599     iFourCC = aSourceFourCC;
       
   600     iMediaId = aMediaId;
       
   601 
       
   602     if ( aSourceFourCC == KMccFourCCIdAMRWB )
       
   603         {
       
   604         // Nb is set true in the constructor by default.
       
   605         iIsNb = EFalse; 
       
   606         SetSampleRate( KAmrWbSampleRate );
       
   607         }
       
   608         
       
   609     iClip->SetSourceDataTypeCode( iFourCC, iMediaId );
       
   610 
       
   611     return KErrNone;
       
   612     }
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // CAmrPayloadFormatRead::SourceThreadLogon
       
   616 // Log in to the source thread
       
   617 // -----------------------------------------------------------------------------
       
   618 //
       
   619 TInt CAmrPayloadFormatRead::SourceThreadLogon( 
       
   620         MAsyncEventHandler& aEventHandler )
       
   621     {
       
   622     #ifdef VOIP_TRACE_ENABLED
       
   623         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCETHREADLOGON );
       
   624     #endif
       
   625     
       
   626     iEventHandler = &aEventHandler;
       
   627     iClip->SourceThreadLogon( aEventHandler );
       
   628     return KErrNone;
       
   629     }
       
   630 
       
   631 // -----------------------------------------------------------------------------
       
   632 // CAmrPayloadFormatRead::NegotiateSourceL( MDataSink& aDataSink )
       
   633 // Negotiate source settings to match data sink object.
       
   634 // Re-size frame buffers if needed
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CAmrPayloadFormatRead::NegotiateSourceL( MDataSink& /*aDataSink*/ )
       
   638     {
       
   639     #ifdef VOIP_TRACE_ENABLED
       
   640         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_NEGOTIATESOURCEL );
       
   641     #endif
       
   642 
       
   643     iClip->NegotiateSourceL( *this );
       
   644     }
       
   645 
       
   646 // -----------------------------------------------------------------------------
       
   647 // CAmrPayloadFormatRead::SourceThreadLogoff
       
   648 // Log out of the source thread.
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CAmrPayloadFormatRead::SourceThreadLogoff( )
       
   652     {
       
   653     #ifdef VOIP_TRACE_ENABLED
       
   654         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCETHREADLOGOFF );
       
   655     #endif
       
   656 
       
   657     iClip->SourceThreadLogoff( );
       
   658     }
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CAmrPayloadFormatRead::DataBufferFilledL
       
   662 // Called after the data buffer is filled. Update the number of bytes read
       
   663 // and the current read position for the next read operation.
       
   664 // -----------------------------------------------------------------------------
       
   665 //
       
   666 void CAmrPayloadFormatRead::DataBufferFilledL( CMMFBuffer* aBuffer, 
       
   667                                            const TRtpRecvHeader& aRtpHeader )
       
   668     {
       
   669     AMR_PAYLOAD_FORMAT_READ2( "CAmrPayloadFormatRead::DataBufferFilledL TSTAMP: %u",
       
   670         aRtpHeader.iTimestamp )
       
   671     
       
   672     __ASSERT_ALWAYS( aBuffer, User::Leave( KErrArgument ) );
       
   673     __ASSERT_ALWAYS( KUidMmfDataBuffer == aBuffer->Type(), 
       
   674         User::Leave( KErrNotSupported) );
       
   675     __ASSERT_ALWAYS( iSourceBuffer == aBuffer, User::Leave( KErrArgument ) );
       
   676     
       
   677     AMR_PAYLOAD_FORMAT_READ2( "CAmrPayloadFormatRead::DataBufferFilledL LEN:%d",
       
   678         iSourceBuffer->Data().Length() )
       
   679     
       
   680     if ( !iSourceBuffer->Data().Size() )
       
   681         {
       
   682         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::BufferFilledL NO DATA" )
       
   683         
       
   684         // Request for a new buffer or playback is stopped
       
   685         FillSourceBufferL();
       
   686         return;
       
   687         }
       
   688         
       
   689     iBufferToRead = iSourceBuffer;
       
   690 	iRecvHeader = aRtpHeader;
       
   691     iBufferToReadExists = ETrue;
       
   692 
       
   693     // Get a reference to the data
       
   694     TDes8& srcData = iSourceBuffer->Data();
       
   695 
       
   696 	iPayloadDecoder->SetPayloadBuffer( srcData );
       
   697 	iPayloadDecoder->SetChannelCount( iChannels );
       
   698 	iPayloadDecoder->SetFrameBlockCount( iFramesPerPacket );
       
   699 
       
   700 	// Decoder rips off redundant frames already received and increases
       
   701 	// timestamp accordingly. This prevents frame placing too early at
       
   702 	// jitter buffer's playout buffer.
       
   703 	const TUint32 timeStampIncrement = 
       
   704 	    TUint32( iCInfo.iHwFrameTime * iChannels * iSamplingRate * KTimeMultiplier );
       
   705 
       
   706 	iNumOfFrames = iPayloadDecoder->DecodePayload( iRecvHeader.iTimestamp,
       
   707 	                                               timeStampIncrement );
       
   708     
       
   709 	iBufferToRead->SetFrameNumber( iRecvHeader.iTimestamp );
       
   710 
       
   711 	// If the mode request has changed and it is a speech mode, then report
       
   712 	// it to subcontroller. Otherwise it can be ignored (RFC3267 4.3.1)
       
   713 	const TAmrModeRequest updateCmr = iPayloadDecoder->ModeRequest();
       
   714 	if ( updateCmr != iCmr && updateCmr != EAmrModeReqNone )
       
   715 	    {
       
   716         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::BufferFilledL MODE CHANGE" )
       
   717         
       
   718         iCmr = updateCmr;
       
   719         SendAmrEventToClient( EMccInternalAmrEventCmr );
       
   720 	    }
       
   721 	
       
   722     AMR_PAYLOAD_FORMAT_READ2( "CAmrPayloadFormatRead::BufferFilledL MODE_REQ: %u", updateCmr )
       
   723 
       
   724     // Read frame data and forward it to data sink
       
   725     iCurrentFrame = 0;
       
   726 
       
   727     // Whenever BufferFilledL is called from RtpSourceSink
       
   728     // Set the state machine to fill sinkbuffer
       
   729     if ( iNumOfFrames )
       
   730         {
       
   731         iStateMachine->ChangeState( ESourceDataReady );
       
   732         }
       
   733     else
       
   734         {
       
   735         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::BufferFilledL NO ACCEPTED FRAMES" )
       
   736         
       
   737         // Request for a new buffer or playback is stopped
       
   738         FillSourceBufferL();
       
   739         }
       
   740     }
       
   741 
       
   742 // -----------------------------------------------------------------------------
       
   743 // CAmrPayloadFormatRead::NumChannels
       
   744 // Gets the number of channels
       
   745 // -----------------------------------------------------------------------------
       
   746 //
       
   747 TUint CAmrPayloadFormatRead::NumChannels( )
       
   748     {
       
   749     return KMono;
       
   750     }
       
   751 
       
   752 // -----------------------------------------------------------------------------
       
   753 // CAmrPayloadFormatRead::SampleRate
       
   754 // Returns the samplerate
       
   755 // -----------------------------------------------------------------------------
       
   756 //
       
   757 TUint CAmrPayloadFormatRead::SampleRate( )
       
   758     {
       
   759     return iSamplingRate;
       
   760     }
       
   761 
       
   762 // -----------------------------------------------------------------------------
       
   763 // CAmrPayloadFormatRead::SetSampleRate
       
   764 // Only supported sample rate for AMR-NB is 8000Hz
       
   765 // -----------------------------------------------------------------------------
       
   766 //
       
   767 TInt CAmrPayloadFormatRead::SetSampleRate ( TUint aSampleRate )
       
   768     {
       
   769     #ifdef VOIP_TRACE_ENABLED
       
   770         VoipTrace( "%x %x %d", MCC_TRACE, MCC_AMR_PLF_READ_SETSAMPLERATE,
       
   771             aSampleRate );
       
   772     #endif
       
   773     
       
   774     if( (iIsNb && KAmrNbSampleRate != aSampleRate) ||
       
   775         (!iIsNb && KAmrWbSampleRate != aSampleRate) )
       
   776         {
       
   777         return KErrNotSupported;
       
   778         }
       
   779     else
       
   780         {
       
   781         iSamplingRate = aSampleRate;
       
   782         return KErrNone;
       
   783         }
       
   784     }
       
   785 
       
   786 // -----------------------------------------------------------------------------
       
   787 // CAmrPayloadFormatRead::Duration
       
   788 // Return the clip duration for the given media.
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 TTimeIntervalMicroSeconds CAmrPayloadFormatRead::Duration( TMediaId /*aMediaType*/ ) const
       
   792     {
       
   793     return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   794     }
       
   795 
       
   796 // -----------------------------------------------------------------------------
       
   797 // CAmrPayloadFormatRead::CancelDlRequest( )
       
   798 // Cancels the statemachine and resets the buffers
       
   799 // -----------------------------------------------------------------------------
       
   800 //
       
   801 void CAmrPayloadFormatRead::CancelDlRequest()
       
   802     {
       
   803     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   804         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::CancelDlRequest" );
       
   805     #endif
       
   806 
       
   807     iStateMachine->Cancel();
       
   808     iStateMachine->ChangeState( EDecodeIdle );
       
   809     
       
   810     if ( iFrameBufferOne && iFrameBufferTwo )
       
   811         {
       
   812         iFrameBufferOne->SetLastBuffer( EFalse );
       
   813         iFrameBufferTwo->SetLastBuffer( EFalse );
       
   814 
       
   815         iFrameBufferOne->SetStatus( EAvailable );
       
   816         iFrameBufferTwo->SetStatus( EAvailable );
       
   817         }
       
   818 
       
   819     iBufferToReadExists = EFalse;
       
   820     iCurrentBuffer = EBufferOne;
       
   821     iFillRequested = EFalse;
       
   822     }
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // CAmrPayloadFormatRead::SourcePrimeL()
       
   826 // 
       
   827 // -----------------------------------------------------------------------------
       
   828 //
       
   829 void CAmrPayloadFormatRead::SourcePrimeL()
       
   830     {
       
   831     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   832         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::SourcePrimeL" );
       
   833     #endif
       
   834     #ifdef VOIP_TRACE_ENABLED
       
   835         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCEPRIMEL );
       
   836     #endif
       
   837 
       
   838     iClip->SourcePrimeL();
       
   839     }
       
   840 
       
   841 // -----------------------------------------------------------------------------
       
   842 // CAmrPayloadFormatRead::SourcePlayL()
       
   843 // Starts the source
       
   844 // -----------------------------------------------------------------------------
       
   845 //
       
   846 void CAmrPayloadFormatRead::SourcePlayL()
       
   847     {
       
   848     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   849         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::SourcePlayL" );
       
   850     #endif
       
   851     #ifdef VOIP_TRACE_ENABLED
       
   852         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCEPLAYL );
       
   853     #endif
       
   854     
       
   855     #ifdef FTD_ENABLED
       
   856     iLatestSeqNum = 0;
       
   857     iTotalFrames = 0;
       
   858     iLostFrames = 0;
       
   859     #endif
       
   860     
       
   861     // Initialize payload decoder
       
   862     iPayloadDecoder->Initialize();
       
   863     
       
   864     iClip->SourcePlayL();
       
   865     }
       
   866 
       
   867 // -----------------------------------------------------------------------------
       
   868 // CAmrPayloadFormatRead::SourcePauseL()
       
   869 // Pauses the source
       
   870 // -----------------------------------------------------------------------------
       
   871 //
       
   872 void CAmrPayloadFormatRead::SourcePauseL()
       
   873     {
       
   874     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   875         AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::SourcePauseL" );
       
   876     #endif
       
   877     #ifdef VOIP_TRACE_ENABLED
       
   878         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCEPAUSEL );
       
   879     #endif
       
   880 
       
   881     this->CancelDlRequest();
       
   882     iClip->SourcePauseL();
       
   883     }
       
   884 
       
   885 // -----------------------------------------------------------------------------
       
   886 // CAmrPayloadFormatRead::SourceStopL( )
       
   887 // Stops the source
       
   888 // -----------------------------------------------------------------------------
       
   889 //
       
   890 void CAmrPayloadFormatRead::SourceStopL()
       
   891     {
       
   892     #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   893         AMR_PAYLOAD_FORMAT_READ(  "CAmrPayloadFormatRead::SourceStopL" );
       
   894     #endif
       
   895     #ifdef VOIP_TRACE_ENABLED
       
   896         VoipTrace( "%x %x", MCC_TRACE, MCC_AMR_PLF_READ_SOURCESTOPL );
       
   897     #endif
       
   898 
       
   899     this->CancelDlRequest();
       
   900     iClip->SourceStopL();
       
   901     }
       
   902 
       
   903 // -----------------------------------------------------------------------------
       
   904 // CAmrPayloadFormatRead::SinkDataTypeCode()
       
   905 // Returns the datatype code for this Format Decoder
       
   906 // -----------------------------------------------------------------------------
       
   907 //
       
   908 TFourCC CAmrPayloadFormatRead::SinkDataTypeCode( TMediaId aMediaId )
       
   909     {
       
   910     if( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   911         {
       
   912         return iFourCC;
       
   913         }
       
   914     else
       
   915         {
       
   916         return TFourCC();
       
   917         }
       
   918     }
       
   919     
       
   920 // -----------------------------------------------------------------------------
       
   921 // CAmrPayloadFormatRead::ConfigurePayloadFormatL
       
   922 // Configure payload decoding parameters.
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 void CAmrPayloadFormatRead::ConfigurePayloadFormatL( const TDesC8& aConfigParams )
       
   926     {
       
   927     AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::ConfigurePayloadFormatL (NB/WB)" );
       
   928     __ASSERT_ALWAYS( aConfigParams.Size() == sizeof( TMccCodecInfo ),
       
   929         User::Leave( KErrArgument ) );
       
   930         
       
   931     TMccCodecInfoBuffer infoBuffer;
       
   932     infoBuffer.Copy( aConfigParams );
       
   933     
       
   934     if ( !infoBuffer().iIsUpdate )
       
   935         {
       
   936         __ASSERT_ALWAYS( infoBuffer().iHwFrameTime, User::Leave( KErrArgument ) );
       
   937         
       
   938         iCInfo = infoBuffer();
       
   939         
       
   940         AMR_PAYLOAD_FORMAT_READ5 ( "CAmrPayloadFormatRead::ConfigurePayloadFormatL \
       
   941             MaxPT: %d HWFS: %d FS: %d RED: %d" , iCInfo.iMaxPtime, iCInfo.iHwFrameTime, 
       
   942             iCInfo.iFrameSize, iCInfo.iRedundancyCount );
       
   943         
       
   944         if ( !iPayloadDecoder )
       
   945             {
       
   946             if ( KAmrCodecModeBandwidthEfficient == iCInfo.iCodecMode )
       
   947                 {
       
   948                 iPayloadDecoder = CAmrPayloadDecoder::NewL( iIsNb );
       
   949                 }
       
   950             else if ( KAmrCodecModeOctetAlign == iCInfo.iCodecMode )
       
   951                 {
       
   952                 iPayloadDecoder = CAmrPayloadDecoderOA::NewL( iIsNb );
       
   953                 }
       
   954             else
       
   955                 {
       
   956                 AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::ConfigurePayloadFormatL KErrArgument" );
       
   957                 User::Leave( KErrArgument );
       
   958                 }
       
   959             }
       
   960 
       
   961         iFramesPerPacket = iCInfo.iMaxPtime / iCInfo.iHwFrameTime;
       
   962         iPayloadDecoder->SetFrameBlockCount( iFramesPerPacket );
       
   963 
       
   964         if ( !iFrameBufferOne )
       
   965             {
       
   966             iFrameBufferOne = CMMFDataBuffer::NewL( iCInfo.iFrameSize );
       
   967             }
       
   968         if ( !iFrameBufferTwo )
       
   969             {
       
   970             iFrameBufferTwo = CMMFDataBuffer::NewL( iCInfo.iFrameSize );
       
   971             }
       
   972             
       
   973         if ( iSourceBufOwnership )	
       
   974             {
       
   975             delete iSourceBuffer;
       
   976             }
       
   977         iSourceBuffer = NULL;
       
   978         
       
   979         TInt plSize = iCInfo.iFrameSize * iFramesPerPacket;
       
   980         
       
   981         if ( EAmrFecUsed == iCInfo.iAlgoUsed )
       
   982             {
       
   983             AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::ConfigurePayloadFormatL - Using AMR FEC" );
       
   984             
       
   985             plSize 
       
   986                 = ( 0 <= iCInfo.iMaxRed )
       
   987                 ? iCInfo.iFrameSize * ( iFramesPerPacket + ( iCInfo.iMaxRed / iCInfo.iHwFrameTime ) )
       
   988                 : iCInfo.iFrameSize * ( iFramesPerPacket + KMaxAmrRedCount );
       
   989             }
       
   990         else if ( EGenRedUsed == iCInfo.iAlgoUsed )
       
   991             {
       
   992             AMR_PAYLOAD_FORMAT_READ2( "CAmrPayloadFormatRead::ConfigurePayloadFormatL, RED LEVEL: %d",
       
   993                 iCInfo.iRedundancyCount );
       
   994             
       
   995             if ( iCInfo.iRedundancyCount )
       
   996                 {
       
   997                 plSize *= iCInfo.iRedundancyCount;
       
   998                 }
       
   999             
       
  1000             CPayloadFormatRead* redDecoder 
       
  1001                 = static_cast<CPayloadFormatRead*>( iClip );
       
  1002             
       
  1003             TMccRedPayloadReadConfig config;
       
  1004             config.iRedBlockCount = iCInfo.iRedundancyCount;
       
  1005             config.iMaxPayloadSize = iCInfo.iFrameSize * iFramesPerPacket;
       
  1006             config.iNumOfEncodings = 1;
       
  1007             config.iRedPayloadType = iCInfo.iRedundantPayload;
       
  1008             config.InitPayloadTypes();
       
  1009             config.iEncPayloadTypes[0] = iCInfo.iPayloadType;
       
  1010             TMccRedPayloadReadPckg pckg( config );
       
  1011             redDecoder->ConfigurePayloadFormatL( pckg );
       
  1012             }
       
  1013         else
       
  1014             {
       
  1015             // NOP
       
  1016             }
       
  1017         
       
  1018         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
  1019         }
       
  1020     else
       
  1021         {
       
  1022         UpdateConfigurationL( infoBuffer() );
       
  1023         }            
       
  1024     }
       
  1025 
       
  1026 // -----------------------------------------------------------------------------
       
  1027 // CAmrPayloadFormatRead::UpdateConfigurationL
       
  1028 // Update payload decoder parameters
       
  1029 // -----------------------------------------------------------------------------
       
  1030 //
       
  1031 void CAmrPayloadFormatRead::UpdateConfigurationL( const TMccCodecInfo& aCodecInfo )
       
  1032     {
       
  1033     AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::UpdateConfigurationL" );
       
  1034     
       
  1035     if ( iCInfo.iMaxPtime != aCodecInfo.iMaxPtime )
       
  1036         {       
       
  1037         iFramesPerPacket = aCodecInfo.iMaxPtime / iCInfo.iHwFrameTime;
       
  1038         iPayloadDecoder->SetFrameBlockCount( iFramesPerPacket );
       
  1039               
       
  1040         if ( iSourceBufOwnership )
       
  1041             {
       
  1042             delete iSourceBuffer;
       
  1043             }
       
  1044         iSourceBuffer = NULL;
       
  1045         
       
  1046         TInt plSize = iCInfo.iFrameSize * iFramesPerPacket;
       
  1047         
       
  1048         if ( EAmrFecUsed == iCInfo.iAlgoUsed )
       
  1049             {
       
  1050             plSize 
       
  1051                 = ( 0 <= iCInfo.iMaxRed )
       
  1052                 ? iCInfo.iFrameSize * ( iFramesPerPacket + ( iCInfo.iMaxRed / iCInfo.iHwFrameTime ) )
       
  1053                 : iCInfo.iFrameSize * ( iFramesPerPacket + KMaxAmrRedCount );
       
  1054             }
       
  1055         else
       
  1056             {
       
  1057             AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::ConfigurePayloadFormatL Not using AMR FEC" )
       
  1058             }           
       
  1059  
       
  1060         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
  1061         
       
  1062         // Initialize payload decoder
       
  1063         iPayloadDecoder->Initialize();
       
  1064         
       
  1065         iCInfo.iMaxPtime = aCodecInfo.iMaxPtime;
       
  1066         }
       
  1067     
       
  1068     AMR_PAYLOAD_FORMAT_READ( "CAmrPayloadFormatRead::UpdateConfigurationL OUT" )
       
  1069     }
       
  1070 
       
  1071 // -----------------------------------------------------------------------------
       
  1072 // CAmrPayloadFormatRead::SendAmrEventToClient
       
  1073 // -----------------------------------------------------------------------------
       
  1074 //    
       
  1075 void CAmrPayloadFormatRead::SendAmrEventToClient( 
       
  1076     TMccInternalEventType aEventType )
       
  1077     {
       
  1078     TMccAmrEventData eventData;
       
  1079     TInt bitrate = ConvertModeToBitrate( iCmr );
       
  1080     eventData.iModeRequestBitrate = bitrate;
       
  1081     
       
  1082     if ( iEventHandler )
       
  1083 	    {
       
  1084 	    TMccEvent event;
       
  1085 	    event.iEventData = TMccAmrEventDataPackage( eventData );
       
  1086 	    
       
  1087 	    TMccInternalEvent internalEvent( KMccAmrFormatterUid, 
       
  1088 	                                     aEventType,
       
  1089 	                                     event );
       
  1090 	                                     
       
  1091 	    iEventHandler->SendEventToClient( internalEvent );	
       
  1092 	    }
       
  1093 #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
  1094     else
       
  1095 	    {
       
  1096         AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::SendAmrEventToClient, \
       
  1097 iEventHandler=NULL" )
       
  1098 	    }
       
  1099 #endif
       
  1100     }
       
  1101 
       
  1102 // -----------------------------------------------------------------------------
       
  1103 // CAmrPayloadFormatRead::CreateClipBufferL
       
  1104 // Creates buffer needed in data transfer with format readers clip.
       
  1105 // -----------------------------------------------------------------------------
       
  1106 //
       
  1107 CMMFDataBuffer* CAmrPayloadFormatRead::CreateClipBufferL( 
       
  1108         TUint aSize, TBool& aIsOwnBuffer )
       
  1109     {
       
  1110     AMR_PAYLOAD_FORMAT_READ ( "CAmrPayloadFormatRead::CreateClipBufferL" )
       
  1111     CMMFDataBuffer* buffer( NULL );
       
  1112     
       
  1113     if ( iClip->CanCreateSourceBuffer() )
       
  1114         {
       
  1115         if ( iClip->DataSourceType() == KUidMmfFormatDecode || 
       
  1116              iClip->DataSourceType() == KMccMultiplexerUid )
       
  1117             {
       
  1118             static_cast<CMMFFormatDecode*>( iClip )->SuggestSourceBufferSize( aSize );
       
  1119             }
       
  1120         
       
  1121         TBool reference( EFalse );
       
  1122         CMMFBuffer* sourceBuf 
       
  1123             = iClip->CreateSourceBufferL( KUidMediaTypeAudio, reference );
       
  1124         TBool isSupportedBuf 
       
  1125             = CMMFBuffer::IsSupportedDataBuffer( sourceBuf->Type() );
       
  1126         TBool isOwnBuffer = reference ? EFalse : ETrue;
       
  1127         
       
  1128         if ( !isSupportedBuf )
       
  1129             {
       
  1130             if ( isOwnBuffer )
       
  1131                 {
       
  1132                 delete sourceBuf;
       
  1133                 }
       
  1134             
       
  1135             User::Leave( KErrNotSupported );
       
  1136             }
       
  1137         
       
  1138         aIsOwnBuffer = isOwnBuffer;
       
  1139         buffer = static_cast<CMMFDataBuffer*>( sourceBuf );
       
  1140         }
       
  1141     else
       
  1142         {
       
  1143         aIsOwnBuffer = ETrue;
       
  1144         buffer = CMMFDataBuffer::NewL( aSize );
       
  1145         }
       
  1146     
       
  1147     return buffer;
       
  1148     }
       
  1149 
       
  1150 // -----------------------------------------------------------------------------
       
  1151 // CAmrPayloadFormatRead::ConvertModeToBitrate
       
  1152 // Gives corresponding bitrate for mode request depending
       
  1153 // on formatter mode (nb/wb)
       
  1154 // -----------------------------------------------------------------------------
       
  1155 //
       
  1156 TInt CAmrPayloadFormatRead::ConvertModeToBitrate( TAmrModeRequest aModeRequest )
       
  1157     {
       
  1158     if ( iIsNb && aModeRequest < KNumberOfNbModes )
       
  1159         {
       
  1160         return KAmrNbModes[aModeRequest];
       
  1161         }
       
  1162     
       
  1163     if ( !iIsNb && aModeRequest < KNumberOfWbModes )
       
  1164         {
       
  1165         return KAmrWbModes[aModeRequest];
       
  1166         }
       
  1167    
       
  1168     return 0;
       
  1169     }
       
  1170     
       
  1171 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1172 
       
  1173 //  End of File