multimediacommscontroller/mmccg729payloadformat/src/g729payloadformatread.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 *                G729 audio.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include    "g729payloadformatread.h"
       
    25 #include    "g729payloadformatutil.h"
       
    26 #include    "mccrtpdatasource.h"
       
    27 #include    "formatstatemachine.h"
       
    28 #include    "streamformatter.h"
       
    29 #include    "mccinternaldef.h"
       
    30 #include    "mccredpayloadread.h"
       
    31 
       
    32 #ifdef VOIP_TRACE_ENABLED
       
    33 #include <voip_trace.h>
       
    34 #endif
       
    35 
       
    36 // ============================= LOCAL FUNCTIONS ===============================
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CG729PayloadFormatRead::CG729PayloadFormatRead
       
    43 // C++ default constructor can NOT contain any code, that
       
    44 // might leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CG729PayloadFormatRead::CG729PayloadFormatRead()
       
    48     {
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CG729PayloadFormatRead::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CG729PayloadFormatRead::ConstructL( MDataSource* aSource )
       
    57     {
       
    58     #ifdef VOIP_TRACE_ENABLED
       
    59         VoipTrace( "%x %x %x", MCC_TRACE, MCC_G711_PLF_READ_CONSTRUCTL, aSource );
       
    60     #endif
       
    61     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
    62     
       
    63     iClip = aSource;
       
    64     iBufferToReadExists = EFalse;
       
    65 
       
    66     iFourCC.Set( KMccFourCCIdG729 );
       
    67     
       
    68     // Initialize decoding state machine
       
    69     iStateMachine = CFormatDecodeStateMachine::NewL( this );
       
    70     iCurrentBuffer = EBufferOne;
       
    71     }
       
    72     
       
    73 // -----------------------------------------------------------------------------
       
    74 // CG729PayloadFormatRead::NewL
       
    75 // Two-phased constructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CG729PayloadFormatRead* CG729PayloadFormatRead::NewL( MDataSource* aSource )
       
    79     {
       
    80     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
    81         RDebug::Print (_L ("CG729PayloadFormatRead::NewL()"));
       
    82     #endif
       
    83     #ifdef VOIP_TRACE_ENABLED
       
    84         VoipTrace( "%x %x %x", MCC_TRACE, MCC_G711_PLF_READ_NEWL, aSource );
       
    85     #endif
       
    86 
       
    87     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
    88 
       
    89     CG729PayloadFormatRead* self = new (ELeave) CG729PayloadFormatRead;
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL( aSource );
       
    92     CleanupStack::Pop( self );
       
    93     return self;
       
    94     }    
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CG729PayloadFormatRead::~CG729PayloadFormatRead()
       
    98 // Destructor.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CG729PayloadFormatRead::~CG729PayloadFormatRead()
       
   102     {
       
   103     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   104         RDebug::Print (_L ("CG729PayloadFormatRead::\
       
   105             ~CG729PayloadFormatRead()"));
       
   106     #endif
       
   107     #ifdef VOIP_TRACE_ENABLED
       
   108         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_DESTRUCTOR );
       
   109     #endif
       
   110 
       
   111     delete iFrameBufferOne;
       
   112     delete iFrameBufferTwo;
       
   113     if ( iSourceBufOwnership )
       
   114         {
       
   115         delete iSourceBuffer;
       
   116         }
       
   117     else
       
   118         {
       
   119         iSourceBuffer = NULL;
       
   120         }
       
   121     
       
   122     if ( iStateMachine )
       
   123         {
       
   124         iStateMachine->Cancel();
       
   125         delete iStateMachine;
       
   126         }
       
   127 
       
   128     iClip = NULL;
       
   129     iDataPath = NULL;
       
   130     iClip = NULL;    
       
   131     
       
   132     iFrameArray.Close();
       
   133     }
       
   134 
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CG729PayloadFormatRead::SendDataToSinkL
       
   138 // Send full frame buffer to the DataPath.
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CG729PayloadFormatRead::SendDataToSinkL()
       
   142     {
       
   143     #ifdef VOIP_TRACE_ENABLED
       
   144         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SENDDATATOSINKL );
       
   145     #endif
       
   146           
       
   147     if ( EBufferOne == iCurrentBuffer )
       
   148         {
       
   149         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   150             RDebug::Print (_L ("CG729PayloadFormatRead::\
       
   151                 SendDataToSinkL() - BufferOne"));
       
   152         #endif
       
   153 
       
   154         iDataPath->BufferFilledL( iFrameBufferOne);
       
   155         iCurrentBuffer = EBufferTwo;
       
   156         // More payload buffers needed
       
   157         if ( iBufferToReadExists && !iFrameBufferOne->LastBuffer() )  
       
   158             {
       
   159             iStateMachine->ChangeState( ESourceDataReady );
       
   160             }
       
   161         }
       
   162     else
       
   163         {
       
   164         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   165             RDebug::Print (_L ("CG729PayloadFormatRead::\
       
   166                 SendDataToSinkL() - BufferTwo"));
       
   167         #endif
       
   168 
       
   169         iDataPath->BufferFilledL( iFrameBufferTwo );
       
   170         iCurrentBuffer = EBufferOne;
       
   171         // More payload buffers needed
       
   172         if ( iBufferToReadExists && !iFrameBufferTwo->LastBuffer() )
       
   173             {
       
   174             iStateMachine->ChangeState( ESourceDataReady );
       
   175             }
       
   176         }
       
   177            
       
   178     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   179     RDebug::Print (_L ("CG729PayloadFormatRead::SendDataToSinkL() - DONE") );
       
   180     #endif
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CG729PayloadFormatRead::FillSinkBuffer
       
   185 // Fill SinkBuffer.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CG729PayloadFormatRead::FillSinkBufferL()
       
   189     {
       
   190     #ifdef VOIP_TRACE_ENABLED
       
   191         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_FILLSINKBUFFER );
       
   192     #endif  
       
   193 
       
   194     CMMFDataBuffer* curFrameBuffer;
       
   195     if ( EBufferOne == iCurrentBuffer )
       
   196         {
       
   197         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   198             RDebug::Print( _L("G729PayloadFormatRead:FillSinkBuffer() EBufferOne \
       
   199             reqsize: %d"), iFrameBufferOne->RequestSize() );
       
   200         #endif
       
   201             
       
   202         curFrameBuffer = iFrameBufferOne;
       
   203         }
       
   204     else
       
   205         {
       
   206         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   207             RDebug::Print( _L("G729PayloadFormatRead:FillSinkBuffer() EBufferOne \
       
   208             reqsize: %d"), iFrameBufferTwo->RequestSize() );
       
   209         #endif
       
   210 
       
   211         curFrameBuffer = iFrameBufferTwo;
       
   212         }
       
   213 
       
   214     TDes8& curFrameData = curFrameBuffer->Data();
       
   215     curFrameData.Zero();
       
   216                         
       
   217     // Put next frame decoded from RTP payload to the framebuffer
       
   218     this->GetNextFrame( curFrameData );
       
   219     
       
   220     HBufC8* buffer = NULL;
       
   221     buffer = curFrameData.Alloc();
       
   222     curFrameData.SetLength( KG729NumOfHeaderBytes );
       
   223     TBool sidFrame = EFalse;
       
   224     
       
   225     // Check the audio frame length, if we have one
       
   226     TInt frameLen = 0;
       
   227     if( buffer )
       
   228         {
       
   229         frameLen = buffer->Length();
       
   230         }
       
   231     
       
   232     if ( KG729FrameSize10ms == frameLen )
       
   233         {
       
   234         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   235             RDebug::Print( _L ("CG729PayloadFormatRead::FillSinkBuffer ADD AUDIO HEADER") );
       
   236         #endif
       
   237         
       
   238         // Add 1st G.729 header byte for audio
       
   239         curFrameData[0] = KAudioFrameHeaderByte;
       
   240         sidFrame = EFalse;
       
   241         }
       
   242     else if( KG729CNFrameSize == frameLen )
       
   243         {
       
   244         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   245             RDebug::Print( _L ("CG729PayloadFormatRead::FillSinkBuffer ADD SID HEADER") );
       
   246         #endif
       
   247         
       
   248         // Add 1st G.729 header byte for SID frame
       
   249         curFrameData[0] = KCNoiseFrameHeaderByte;
       
   250         sidFrame = ETrue;
       
   251         }
       
   252     else
       
   253         {
       
   254         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   255             RDebug::Print( _L ("CG729PayloadFormatRead::FillSinkBuffer UNKNOWN FRAMELEN: %d"), frameLen );
       
   256         #endif
       
   257         
       
   258         // delete the buffer as something is badly wrong, this will be handled
       
   259         // a bit later on
       
   260         delete buffer;
       
   261         buffer = NULL;
       
   262         }
       
   263 
       
   264     if( buffer )
       
   265         {
       
   266         // Add 2nd G.729 header byte to the frame going to the decoder,
       
   267         // always zero
       
   268         curFrameData[1] = 0;
       
   269         
       
   270         this->DoBitUnPacking( *buffer, curFrameData, sidFrame );
       
   271         delete buffer;
       
   272         buffer = NULL;
       
   273         }
       
   274     else
       
   275         {
       
   276         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   277             RDebug::Print( _L ("CG729PayloadFormatRead::FillSinkBuffer NO AUDIOBUFFER") );
       
   278         #endif
       
   279         
       
   280         // May be that the allocation failed or GetNextFrame() returned a
       
   281         // unknown size frame. In this case, fill the reset of the curFrameData
       
   282         // with zeroes like it would be a normal G.729 frame.
       
   283         curFrameData.SetLength( KG729CodecDecBufSize );
       
   284         
       
   285         // This will set the header bytes to zero meaning a 0bit/s frame, which
       
   286         // is fine with the decoder.
       
   287         curFrameData.FillZ();
       
   288         }
       
   289         
       
   290     curFrameBuffer->SetFrameNumber( iRecvHeader.iTimestamp + ( ( iFrameIndex - 1 )
       
   291         * TUint( iCInfo.iHwFrameTime * KG729SampleRate * 0.001 ) ) );
       
   292         
       
   293     curFrameBuffer->SetStatus( EFull );
       
   294 
       
   295     iStateMachine->ChangeState( EEmptyDataToSink );
       
   296 
       
   297     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   298         RDebug::Print( _L("G729PayloadFormatRead::FillSinkBuffer() \
       
   299         BUFSZ: %d"), curFrameBuffer->BufferSize() );
       
   300     #endif
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CG729PayloadFormatRead::FillSourceBufferL
       
   305 // Send fill buffer request to the RTP Data Source.
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CG729PayloadFormatRead::FillSourceBufferL()
       
   309     {
       
   310     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   311         RDebug::Print (_L ("CG729PayloadFormatRead::FillSourceBufferL()"));
       
   312     #endif
       
   313     #ifdef VOIP_TRACE_ENABLED
       
   314         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_FILLSOURCEBUFFERL );
       
   315     #endif 
       
   316 
       
   317     // RtpSourceSink doesn't really need the Media Id.        
       
   318     iClip->FillBufferL( iSourceBuffer, this, iMediaId );
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CG729PayloadFormatRead::SourcePrimeL
       
   323 // Prime source.
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 void CG729PayloadFormatRead::SourcePrimeL()
       
   327     {
       
   328     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   329         RDebug::Print( _L("CG729PayloadFormatRead::SourcePrimeL") );
       
   330     #endif
       
   331     #ifdef VOIP_TRACE_ENABLED
       
   332         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SOURCEPRIMEL );
       
   333     #endif
       
   334     
       
   335     iClip->SourcePrimeL();
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CG729PayloadFormatRead::SourcePlayL
       
   340 // Start playing.
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 void CG729PayloadFormatRead::SourcePlayL()
       
   344     {
       
   345     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   346         RDebug::Print( _L("CG729PayloadFormatRead::SourcePlayL") );
       
   347     #endif
       
   348     #ifdef VOIP_TRACE_ENABLED
       
   349         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SOURCEPLAYL );
       
   350     #endif
       
   351 
       
   352     iClip->SourcePlayL();
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CG729PayloadFormatRead::SourcePauseL
       
   357 // Pause source.
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 void CG729PayloadFormatRead::SourcePauseL()
       
   361     {
       
   362     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   363         RDebug::Print( _L("CG729PayloadFormatRead::SourcePauseL") );
       
   364     #endif
       
   365     #ifdef VOIP_TRACE_ENABLED
       
   366         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SOURCEPAUSEL );
       
   367     #endif
       
   368 
       
   369     iStateMachine->Cancel();
       
   370     iStateMachine->ChangeState( EDecodeIdle );
       
   371     
       
   372     iFrameBufferOne->SetLastBuffer( EFalse );
       
   373     iFrameBufferTwo->SetLastBuffer( EFalse );
       
   374 
       
   375     iFrameBufferOne->SetStatus( EAvailable );
       
   376     iFrameBufferTwo->SetStatus( EAvailable );
       
   377 
       
   378     iBufferToReadExists = EFalse;
       
   379     iCurrentBuffer = EBufferOne;
       
   380     
       
   381     iClip->SourcePauseL();
       
   382     }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CG729PayloadFormatRead::SourceStopL
       
   386 // Stop source
       
   387 // -----------------------------------------------------------------------------
       
   388 //
       
   389 void CG729PayloadFormatRead::SourceStopL()
       
   390     {
       
   391     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   392         RDebug::Print( _L("CG729PayloadFormatRead::SourceStopL") );
       
   393     #endif
       
   394     #ifdef VOIP_TRACE_ENABLED
       
   395         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SOURCESTOPL );
       
   396     #endif
       
   397 
       
   398     // DO NOT RESET PACKET COUNT BACK TO ZERO HERE
       
   399     // UPPER LAYER MAY CALL LastDlPacketCount LATER
       
   400 
       
   401     iStateMachine->Cancel();
       
   402 
       
   403     iStateMachine->ChangeState( EDecodeIdle );
       
   404     
       
   405     iFrameBufferOne->SetLastBuffer( EFalse );
       
   406     iFrameBufferTwo->SetLastBuffer( EFalse );
       
   407 
       
   408     iFrameBufferOne->SetStatus( EAvailable );
       
   409     iFrameBufferTwo->SetStatus( EAvailable );
       
   410 
       
   411     iBufferToReadExists = EFalse;
       
   412     iCurrentBuffer = EBufferOne;
       
   413 
       
   414     #ifdef DEBUG_G729_INTERVAL
       
   415         RDebug::Print( _L("Processed %d G729 frames"), iFrameCount ); 
       
   416         iFrameCount = 0;
       
   417     #endif
       
   418 
       
   419     iClip->SourceStopL();
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CG729PayloadFormatRead::BufferFilledL
       
   424 // RTP data source has filled buffer.
       
   425 // NOTE: Although redundancy is negotiated, sender may deside to send audio 
       
   426 // packets without redundancy (RFC2198, ch5).
       
   427 // -----------------------------------------------------------------------------
       
   428 //
       
   429 void CG729PayloadFormatRead::DataBufferFilledL( CMMFBuffer* aBuffer, const TRtpRecvHeader &aRecvHeader)
       
   430     {
       
   431     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   432         RDebug::Print( _L("CG729PayloadFormatRead::DataBufferFilledL - TSTAMP: %u"),
       
   433         aRecvHeader.iTimestamp );
       
   434     #endif
       
   435     __ASSERT_ALWAYS( aBuffer, User::Leave( KErrArgument ) );
       
   436     __ASSERT_ALWAYS( KUidMmfDataBuffer == aBuffer->Type(), 
       
   437         User::Leave( KErrNotSupported ) );
       
   438     __ASSERT_ALWAYS( iSourceBuffer == aBuffer, User::Leave( KErrArgument ) );
       
   439     
       
   440     // Copy received buffer
       
   441     TDes8& destDes = iSourceBuffer->Data();
       
   442     
       
   443     iRecvHeader = aRecvHeader;
       
   444     
       
   445     // Decode data blocks
       
   446     DecodePayload( destDes );
       
   447     
       
   448     // Whenever BufferFilledL is called from RtpSourceSink
       
   449     // Set the state machine to fillsinkbuffer
       
   450     if ( iFrameArray.Count() )
       
   451         {
       
   452         iBufferToReadExists = ETrue;
       
   453         iSourceBuffer->SetFrameNumber( iRecvHeader.iTimestamp );
       
   454         iStateMachine->ChangeState( ESourceDataReady );
       
   455         }
       
   456     else
       
   457         {
       
   458         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   459         RDebug::Print( _L("CG729PayloadFormatRead::BufferFilledL, decode failed" ) );
       
   460         #endif
       
   461         
       
   462         FillSourceBufferL();
       
   463         }
       
   464     }
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CG729PayloadFormatRead::SampleRate
       
   468 // Returns samplerate.
       
   469 // -----------------------------------------------------------------------------
       
   470 //
       
   471 TUint CG729PayloadFormatRead::SampleRate()
       
   472     {
       
   473     return KG729SampleRate;
       
   474     }  
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // CG729PayloadFormatRead::SetSampleRate
       
   478 // Set samplerate.
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 TInt CG729PayloadFormatRead::SetSampleRate( TUint aSampleRate )
       
   482     {
       
   483     #ifdef VOIP_TRACE_ENABLED
       
   484         VoipTrace( "%x %x %d", MCC_TRACE, MCC_G711_PLF_READ_SETSAMPLERATE,
       
   485             aSampleRate );
       
   486     #endif
       
   487 
       
   488     return ( KG729SampleRate == aSampleRate ) ? KErrNone : KErrNotSupported;
       
   489     }
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CG729PayloadFormatRead::NumChannels
       
   493 // Returns number of channels.
       
   494 // -----------------------------------------------------------------------------
       
   495 //
       
   496 TUint CG729PayloadFormatRead::NumChannels()
       
   497     {
       
   498     return KMono;
       
   499     }
       
   500 
       
   501 // -----------------------------------------------------------------------------
       
   502 // CG729PayloadFormatRead::SourceThreadLogon
       
   503 // Logon to the source thread.
       
   504 // -----------------------------------------------------------------------------
       
   505 //
       
   506 TInt CG729PayloadFormatRead::SourceThreadLogon(
       
   507     MAsyncEventHandler& aEventHandler )
       
   508     {
       
   509     #ifdef VOIP_TRACE_ENABLED
       
   510         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_SOURCETHREADLOGOFF );
       
   511     #endif
       
   512 
       
   513     if ( iClip )
       
   514         {
       
   515         iClip->SourceThreadLogon( aEventHandler );
       
   516         return KErrNone;
       
   517         }
       
   518     else
       
   519         {
       
   520         return KErrNotReady;
       
   521         }
       
   522     }
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CG729PayloadFormatRead::SourceThreadLogoff
       
   526 // Logout the source thread.
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 void CG729PayloadFormatRead::SourceThreadLogoff()
       
   530     {
       
   531     #ifdef VOIP_TRACE_ENABLED
       
   532         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_NEGOTIATESOURCEL );
       
   533     #endif
       
   534     iClip->SourceThreadLogoff();
       
   535     }
       
   536     
       
   537 // -----------------------------------------------------------------------------
       
   538 // CG729PayloadFormatRead::NegotiateSourceL
       
   539 // Negotiate Source.
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 void CG729PayloadFormatRead::NegotiateSourceL(MDataSink& aDataSink)
       
   543     {
       
   544     #ifdef VOIP_TRACE_ENABLED
       
   545         VoipTrace( "%x %x %d", MCC_TRACE, MCC_G711_PLF_READ_SOURCETHREADLOGON,
       
   546             iCInfo.iRedundancyCount );
       
   547     #endif
       
   548 
       
   549     iDataPath = &aDataSink;
       
   550     iClip->NegotiateSourceL( *this );
       
   551     }
       
   552 
       
   553 // -----------------------------------------------------------------------------
       
   554 // CG729PayloadFormatRead::SetSourceDataTypeCode
       
   555 // Sets source datatype fourCC code
       
   556 // -----------------------------------------------------------------------------
       
   557 //
       
   558 TInt CG729PayloadFormatRead::SetSourceDataTypeCode( TFourCC aSourceFourCC,
       
   559                                                     TMediaId aMediaId )
       
   560     {
       
   561     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   562         RDebug::Print (_L ("CG729PayloadFormatRead::SetSourceDataTypeCode()"));
       
   563     #endif
       
   564     #ifdef VOIP_TRACE_ENABLED
       
   565         VoipTrace( "%x %x %d", MCC_TRACE, MCC_G711_PLF_READ_SETSOURCEDATATYPECODE,
       
   566             aSourceFourCC.FourCC() );
       
   567     #endif
       
   568 
       
   569     if ( KUidMediaTypeAudio != aMediaId.iMediaType ) 
       
   570         {
       
   571         return KErrNotSupported;
       
   572         }
       
   573 
       
   574     iFourCC = aSourceFourCC;
       
   575     iMediaId = aMediaId;
       
   576 
       
   577     iClip->SetSourceDataTypeCode( iFourCC, iMediaId );
       
   578 
       
   579     return KErrNone;
       
   580     }
       
   581 
       
   582 // -----------------------------------------------------------------------------
       
   583 // CG729PayloadFormatRead::SourceDataTypeCode
       
   584 // Returns the current datatype FourCC code.
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 TFourCC CG729PayloadFormatRead::SourceDataTypeCode( TMediaId aMediaId )
       
   588     {
       
   589     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   590         {
       
   591         return iFourCC;
       
   592         }
       
   593     else
       
   594         {
       
   595         return TFourCC(); //defaults to 'NULL' fourCC
       
   596         }
       
   597     }
       
   598 
       
   599 // -----------------------------------------------------------------------------
       
   600 // CG729PayloadFormatRead::SinkDataTypeCode
       
   601 // Returns the current datatype FourCC code.
       
   602 // -----------------------------------------------------------------------------
       
   603 //
       
   604 TFourCC CG729PayloadFormatRead::SinkDataTypeCode( TMediaId aMediaId )
       
   605     {
       
   606     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   607         {
       
   608         return iFourCC;
       
   609         }
       
   610     else
       
   611         {
       
   612         return TFourCC(); //defaults to 'NULL' fourCC
       
   613         }
       
   614     }
       
   615     
       
   616 // -----------------------------------------------------------------------------
       
   617 // CG729PayloadFormatRead::CreateSourceBufferL
       
   618 // Create a source buffer for the given media and indicate in aReference if 
       
   619 // buffer is created.
       
   620 // -----------------------------------------------------------------------------
       
   621 //
       
   622 CMMFBuffer* CG729PayloadFormatRead::CreateSourceBufferL( TMediaId aMediaId,
       
   623                                                          TBool &aReference )
       
   624     {
       
   625     #ifdef VOIP_TRACE_ENABLED
       
   626         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_CREATESOURCEBUFFERL );
       
   627     #endif
       
   628 
       
   629     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   630         {
       
   631         User::Leave( KErrNotSupported );
       
   632         }
       
   633 
       
   634     // The source buffers belong to G729PayloadFormatRead, not to datapath
       
   635     // reference should be set to ETrue and destroyed by G729PayloadFormatRead
       
   636     // itself.
       
   637     aReference = ETrue;
       
   638     return iFrameBufferOne; 
       
   639     }
       
   640 
       
   641 // -----------------------------------------------------------------------------
       
   642 // CG729PayloadFormatRead::CreateSourceBufferL
       
   643 // Create a source buffer for the given media, setting frame size to match
       
   644 // the given sink buffer.
       
   645 // -----------------------------------------------------------------------------
       
   646 //
       
   647 CMMFBuffer* CG729PayloadFormatRead::CreateSourceBufferL(TMediaId aMediaId,
       
   648                                                         CMMFBuffer& 
       
   649                                                             /*aSinkBuffer*/,
       
   650                                                         TBool& aReference)
       
   651     {
       
   652     #ifdef VOIP_TRACE_ENABLED
       
   653         VoipTrace( "%x %x", MCC_TRACE, MCC_G711_PLF_READ_CREATESOURCEBUFFERL );
       
   654     #endif
       
   655 
       
   656     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   657         {
       
   658         User::Leave( KErrNotSupported );
       
   659         }
       
   660         
       
   661     return CreateSourceBufferL( aMediaId, aReference );
       
   662     }
       
   663 
       
   664 // -----------------------------------------------------------------------------
       
   665 // CG729PayloadFormatRead::FillBufferL
       
   666 // Fill Buffer.
       
   667 // -----------------------------------------------------------------------------
       
   668 //
       
   669 void CG729PayloadFormatRead::FillBufferL( CMMFBuffer* aBuffer, 
       
   670     MDataSink* aConsumer, TMediaId aMediaId )
       
   671     {
       
   672     if ( !aBuffer ) 
       
   673         {
       
   674         User::Leave( KErrGeneral );
       
   675         }
       
   676 
       
   677     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   678     RDebug::Print (_L ("CG729PayloadFormatRead::FillBufferL() \
       
   679     buffer 0x%x passed in with length %d bytes \n"),
       
   680         aBuffer, aBuffer->BufferSize() );
       
   681     #endif
       
   682 
       
   683     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   684         {
       
   685         User::Leave( KErrNotSupported );
       
   686         }
       
   687 
       
   688     if ( KUidMmfDataBuffer != aBuffer->Type() )
       
   689         {
       
   690         User::Leave( KErrNotSupported );
       
   691         }
       
   692 
       
   693     iDataPath = aConsumer;
       
   694     iMediaId = aMediaId;
       
   695 
       
   696     // aBuffer is a reference to those frame buffers that 
       
   697     // G729PayloadFormatRead owns
       
   698     aBuffer->SetLastBuffer( EFalse );
       
   699 
       
   700     if ( EBufferOne == iCurrentBuffer )
       
   701         {
       
   702         iFrameBufferTwo->SetStatus( EAvailable );
       
   703         }
       
   704     else
       
   705         {
       
   706         iFrameBufferOne->SetStatus( EAvailable );
       
   707         }
       
   708 
       
   709     if ( iBufferToReadExists ) // Payload has some frames not decoded
       
   710         {
       
   711         iStateMachine->ChangeState( ESourceDataReady );
       
   712         }
       
   713     else
       
   714         {
       
   715         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   716             RDebug::Print( _L("CG729PayloadFormatRead::FillBufferL -> FillSourceBufferL") );
       
   717         #endif
       
   718         
       
   719         // No payload, ask for it
       
   720         FillSourceBufferL();
       
   721         } 
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CG729PayloadFormatRead::Streams
       
   726 // Return number of audio streams for the given media.
       
   727 // -----------------------------------------------------------------------------
       
   728 //
       
   729 TUint CG729PayloadFormatRead::Streams(TUid aMediaType) const
       
   730     {
       
   731     // Need to check aMediaType for audio
       
   732     if ( KUidMediaTypeAudio == aMediaType )
       
   733         {
       
   734         return 1;
       
   735         }
       
   736     else
       
   737         {
       
   738         return 0;
       
   739         }
       
   740     }
       
   741 
       
   742 // -----------------------------------------------------------------------------
       
   743 // CG729PayloadFormatRead::FrameTimeInterval
       
   744 // Return the frame time interval for the given media
       
   745 // -----------------------------------------------------------------------------
       
   746 //
       
   747 TTimeIntervalMicroSeconds CG729PayloadFormatRead::FrameTimeInterval(
       
   748                                                     TMediaId aMediaId) const
       
   749     {
       
   750     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   751         {
       
   752         return iFrameTimeInterval;
       
   753         }
       
   754     else
       
   755         {
       
   756         return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   757         }
       
   758     }
       
   759 
       
   760 // -----------------------------------------------------------------------------
       
   761 // CG729PayloadFormatRead::Duration
       
   762 // Return the frame time interval for the given media.
       
   763 // NOT SUPPORTED
       
   764 // -----------------------------------------------------------------------------
       
   765 //
       
   766 TTimeIntervalMicroSeconds CG729PayloadFormatRead::Duration(
       
   767                                                TMediaId /*aMediaType*/) const
       
   768     {
       
   769     return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   770     }
       
   771 
       
   772 // -----------------------------------------------------------------------------
       
   773 // CG729PayloadFormatRead::DecodePayload
       
   774 // Decodes all audio frames from the received RTP payload buffer. Decoded
       
   775 // audio frames are saved to the internal array so that audio frames can be
       
   776 // requested one at a time with GetNextFrame() -method.
       
   777 // No assumption about frame count in RTP packet is done.
       
   778 // -----------------------------------------------------------------------------
       
   779 //
       
   780 void CG729PayloadFormatRead::DecodePayload( const TDesC8& aSourceBuffer )
       
   781     {
       
   782     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   783         RDebug::Print ( _L ( "CG729PayloadFormatRead::DecodePayload - SourceBufSize: %d" ), aSourceBuffer.Length() );
       
   784     #endif    
       
   785     #ifdef VOIP_TRACE_ENABLED
       
   786         VoipTrace( "%x %x %d", MCC_TRACE, MCC_G711_PLF_READ_DECODEPAYLOAD,
       
   787             aSourceBuffer.Size() );
       
   788     #endif
       
   789     
       
   790     const TUint8* framePtr = aSourceBuffer.Ptr();
       
   791     TInt payloadSize( aSourceBuffer.Length() );
       
   792     const TUint8* endPtr = aSourceBuffer.Ptr() + payloadSize;
       
   793     
       
   794     // Calculate parameters for frame ripping
       
   795     TInt frames( 0 );
       
   796     TInt frameSize( 0 );
       
   797     TInt remainder( 0 );    
       
   798     iFrameIndex = 0;
       
   799 
       
   800     if ( !( payloadSize % (TInt) iCInfo.iFrameSize ) )
       
   801         {
       
   802         // Pure audio frames
       
   803         frameSize = iCInfo.iFrameSize;
       
   804         frames = payloadSize / (TInt) iCInfo.iFrameSize;
       
   805         remainder = 0;
       
   806         
       
   807         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   808             RDebug::Print( _L( "CG729PayloadFormatRead::DecodePayload - PURE AUDIO FRAMES: %d" ), frames );
       
   809         #endif        
       
   810         }
       
   811     else if ( KG729CNFrameSize == payloadSize )
       
   812         {
       
   813         // Payload consists of one CN frame
       
   814         frameSize = KG729CNFrameSize;
       
   815         frames = payloadSize / KG729CNFrameSize;
       
   816         remainder = 0;
       
   817         
       
   818         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   819             RDebug::Print( _L( "CG729PayloadFormatRead::DecodePayload - ONE CNOISE FRAME: %d" ), frames );
       
   820         #endif
       
   821         }
       
   822     else
       
   823         {
       
   824         // Payload contains one CNF at the end of buffer
       
   825         frameSize = iCInfo.iFrameSize;
       
   826         frames = payloadSize / (TInt) iCInfo.iFrameSize;
       
   827         remainder = payloadSize % (TInt) iCInfo.iFrameSize;
       
   828         
       
   829         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   830             RDebug::Print( _L( "CG729PayloadFormatRead::DecodePayload - AUDIO FRAME(s) + ONE CNOISE FRAME, FRAMES: %d, REMAINDER: %d" ), frames, remainder );
       
   831         #endif        
       
   832         }
       
   833 
       
   834     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   835     RDebug::Print( _L( "CG729PayloadFormatRead::DecodePayload - framesize: %d, frames: %d" ), frameSize, frames );
       
   836     #endif
       
   837     
       
   838     // Construct pointers to frames in payload
       
   839     while ( frames-- )
       
   840         {
       
   841         TPtr8 bufPtr( const_cast<TUint8*>( framePtr ), frameSize, frameSize );
       
   842         iFrameArray.Append( bufPtr );
       
   843 
       
   844         framePtr += frameSize;
       
   845         
       
   846         if ( framePtr >= endPtr )
       
   847             {
       
   848             frames = 0;
       
   849             }
       
   850         }
       
   851 
       
   852     // Add possible comfort noise frame
       
   853     if ( remainder )
       
   854         {
       
   855         TPtr8 bufPtr( const_cast<TUint8*>(framePtr), KG729CNFrameSize, KG729CNFrameSize );
       
   856         iFrameArray.Append( bufPtr );
       
   857         }
       
   858     }
       
   859 
       
   860 // -----------------------------------------------------------------------------
       
   861 // CG729PayloadFormatRead::GetNextFrame
       
   862 // Passes next audio frame decoded with DecodePayload()
       
   863 // -----------------------------------------------------------------------------
       
   864 //
       
   865 void CG729PayloadFormatRead::GetNextFrame( TDes8& aToBuffer )
       
   866     {
       
   867     #ifdef VOIP_TRACE_ENABLED
       
   868         VoipTrace( "%x %x %d", MCC_TRACE, MCC_G711_PLF_READ_GETNEXTFRAME,
       
   869             iFrameArray.Count(), iFrameIndex );
       
   870     #endif
       
   871 
       
   872     iFrameIndex++;
       
   873     
       
   874     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   875         RDebug::Print( _L( "CG729PayloadFormatRead::GetNextFrame - FrameCount: %d, FrameIndex: %d" ),
       
   876             iFrameArray.Count(), iFrameIndex );
       
   877         if ( iFrameArray.Count() )
       
   878             {
       
   879             RDebug::Print( _L("CG729PayloadFormatRead::GetNextFrame SRC_LEN: %d"),
       
   880                 iFrameArray[iFrameIndex - 1].Length() );
       
   881             RDebug::Print( _L("CG729PayloadFormatRead::GetNextFrame SRC_MAX: %d"),
       
   882                 iFrameArray[iFrameIndex - 1].MaxLength() );
       
   883             }
       
   884 
       
   885         RDebug::Print( _L("CG729PayloadFormatRead::GetNextFrame DEST_LEN: %d"), aToBuffer.Length() );
       
   886         RDebug::Print( _L("CG729PayloadFormatRead::GetNextFrame DEST_MAX: %d"), aToBuffer.MaxLength() );
       
   887     #endif
       
   888     
       
   889     const TInt frmCount = iFrameArray.Count();
       
   890     
       
   891     if ( iFrameIndex < frmCount )
       
   892         {
       
   893         aToBuffer.Append( iFrameArray[iFrameIndex - 1] );
       
   894         iBufferToReadExists = ETrue;
       
   895         }
       
   896     else if ( iFrameIndex == frmCount )
       
   897         {
       
   898         aToBuffer.Append( iFrameArray[iFrameIndex - 1] );
       
   899         iFrameArray.Reset();
       
   900         iBufferToReadExists = EFalse;
       
   901         }
       
   902     else
       
   903         {
       
   904         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   905             RDebug::Print( _L ( "CG729PayloadFormatRead::GetNextFrame FALSE" ));
       
   906         #endif         
       
   907         iBufferToReadExists = EFalse;
       
   908         }        
       
   909     }
       
   910     
       
   911 // -----------------------------------------------------------------------------
       
   912 // CG729PayloadFormatRead::DoBitUnPacking
       
   913 // Does unpacking for bit-packed G729 RTP payload.
       
   914 // -----------------------------------------------------------------------------
       
   915 //
       
   916 TInt CG729PayloadFormatRead::DoBitUnPacking( const TDesC8& aSourceBuf,
       
   917     TDes8& aDestBuf, TBool aIsCNoise ) const
       
   918     {
       
   919     if ( KG729CodecDecBufSize < aDestBuf.MaxLength() )
       
   920         {
       
   921         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   922             RDebug::Print( _L("CG729PayloadFormatRead::DoBitUnPacking - DESTINATION BUF TOO SMALL!") );
       
   923         #endif
       
   924         
       
   925         return KErrArgument;
       
   926         }
       
   927     else
       
   928         {
       
   929         TStreamDecoder decoder;
       
   930         // Initialize and take into account the header bytes
       
   931         decoder.Initialize( const_cast<TUint8*>( aSourceBuf.Ptr() ), 0, 0 );
       
   932         aDestBuf.SetLength( KG729CodecDecBufSize );
       
   933         
       
   934         // Store header to temporary variables and zero the data in the
       
   935         // destination buffer. After that, restore the header bytes.
       
   936         TUint8 header1 = aDestBuf[0];
       
   937         TUint8 header2 = aDestBuf[1];
       
   938         aDestBuf.FillZ();
       
   939         aDestBuf[0] = header1;
       
   940         aDestBuf[1] = header2;
       
   941         
       
   942         TUint8 numOfParams( 0 );
       
   943         if ( aIsCNoise )
       
   944             {
       
   945             numOfParams = KG729NumOfCNoiseParams;
       
   946             }
       
   947         else
       
   948             {
       
   949             numOfParams = KG729NumOfAudioParams;
       
   950             }
       
   951             
       
   952         // Don't overwrite the header bytes
       
   953         TUint byteIndex( KG729NumOfHeaderBytes );
       
   954         TUint8 curParamBits( 0 );
       
   955         TUint8 decodedByte( 0 );
       
   956         for ( TInt i = 0; i < numOfParams; i++ )
       
   957             {
       
   958             if ( aIsCNoise )
       
   959                 {
       
   960                 curParamBits = KG729CodecBufCNoiseBits[i];
       
   961                 }
       
   962             else
       
   963                 {
       
   964                 curParamBits = KG729CodecBufAudioBits[i];
       
   965                 }
       
   966 
       
   967             if ( curParamBits <= KBitsInByte )
       
   968                 {
       
   969                 decodedByte = TUint8( decoder.Decode( curParamBits ) );
       
   970                 aDestBuf[ byteIndex ] = decodedByte;
       
   971                 aDestBuf[ byteIndex + 1 ] = 0;
       
   972                 }
       
   973             else
       
   974                 {
       
   975                 decodedByte = TUint8( decoder.Decode( curParamBits - KBitsInByte ) );
       
   976                 aDestBuf[ byteIndex + 1 ] = decodedByte;
       
   977                 
       
   978                 decodedByte = TUint8( decoder.Decode( KBitsInByte ) );
       
   979                 aDestBuf[ byteIndex ] = decodedByte;
       
   980                 }
       
   981                 
       
   982             byteIndex += 2;
       
   983             }
       
   984         
       
   985         return KErrNone;            
       
   986         }
       
   987     }
       
   988 
       
   989 // -----------------------------------------------------------------------------
       
   990 // CG729PayloadFormatRead::ConfigurePayloadFormatL
       
   991 // Configure payload decoding parameters.
       
   992 // -----------------------------------------------------------------------------
       
   993 //
       
   994 void CG729PayloadFormatRead::ConfigurePayloadFormatL( const TDesC8& aConfigParams )
       
   995     {
       
   996     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
   997         RDebug::Print ( _L ( "CG729PayloadFormatRead::ConfigurePayloadFormatL" ) );
       
   998     #endif
       
   999     __ASSERT_ALWAYS( aConfigParams.Size() == sizeof( TMccCodecInfo ),
       
  1000         User::Leave( KErrArgument ) );
       
  1001     
       
  1002     TMccCodecInfoBuffer infoBuffer;
       
  1003     infoBuffer.Copy( aConfigParams );
       
  1004     
       
  1005     if ( !infoBuffer().iIsUpdate )
       
  1006         {
       
  1007         iCInfo = infoBuffer();  
       
  1008         // Maximum number of frames in RTP payload
       
  1009         iCInfo.iHwFrameTime = KG729FrameTimeInMs; // RFC3551
       
  1010         TUint pktCount = iCInfo.iMaxPtime / iCInfo.iHwFrameTime;
       
  1011         iCInfo.iFrameSize = KG729FrameSize10ms;
       
  1012         iFrameTimeInterval = TInt64( iCInfo.iHwFrameTime * TInt8( pktCount ) );
       
  1013         
       
  1014         // Create two frame buffers used in data transfer with datapath.
       
  1015         // Space for two byte additional header needed by HW codec is reserved.
       
  1016         if ( iFrameBufferOne )
       
  1017             {
       
  1018             delete iFrameBufferOne;
       
  1019             iFrameBufferOne = NULL;   
       
  1020             }
       
  1021         iFrameBufferOne = CMMFDataBuffer::NewL( KG729CodecDecBufSize );
       
  1022         
       
  1023         if ( iFrameBufferTwo )
       
  1024             {
       
  1025             delete iFrameBufferTwo; 
       
  1026             iFrameBufferTwo = NULL;  
       
  1027             }
       
  1028         iFrameBufferTwo = CMMFDataBuffer::NewL( KG729CodecDecBufSize );
       
  1029         
       
  1030         // PayloadBuffer contains data received from network
       
  1031         TInt plSize = iCInfo.iFrameSize * pktCount + KG729CNFrameSize;
       
  1032         
       
  1033         #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1034             RDebug::Print( _L ( "CG729PayloadFormatRead::ConfigurePayloadFormatL FramesPerPacket: %d, FrameSize: %d" ),
       
  1035                 pktCount, iCInfo.iFrameSize );
       
  1036         #endif
       
  1037         if ( EGenRedUsed == iCInfo.iAlgoUsed )
       
  1038             {
       
  1039             #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1040             RDebug::Print( _L("CG729PayloadFormatRead::ConfigurePayloadFormatL, RED LEVEL: %d"),
       
  1041                 iCInfo.iRedundancyCount );
       
  1042             #endif
       
  1043             if ( iCInfo.iRedundancyCount )
       
  1044                 {
       
  1045                 plSize *= iCInfo.iRedundancyCount;
       
  1046                 }
       
  1047             
       
  1048             CPayloadFormatRead* redDecoder 
       
  1049                 = static_cast<CPayloadFormatRead*>( iClip );
       
  1050             
       
  1051             TMccRedPayloadReadConfig config;
       
  1052             config.iRedBlockCount = iCInfo.iRedundancyCount;
       
  1053             config.iMaxPayloadSize = iCInfo.iFrameSize * pktCount;
       
  1054             config.iNumOfEncodings = 1;
       
  1055             config.iRedPayloadType = iCInfo.iRedundantPayload;
       
  1056             config.InitPayloadTypes();
       
  1057             config.iEncPayloadTypes[0] = iCInfo.iPayloadType;
       
  1058             TMccRedPayloadReadPckg pckg( config );
       
  1059             redDecoder->ConfigurePayloadFormatL( pckg );
       
  1060             }
       
  1061         
       
  1062         if ( iSourceBuffer && iSourceBufOwnership )
       
  1063             {
       
  1064             delete iSourceBuffer; 
       
  1065             iSourceBuffer = NULL; 
       
  1066             iSourceBufOwnership = EFalse;
       
  1067             }
       
  1068         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
  1069         }
       
  1070     else
       
  1071         {
       
  1072         UpdateConfigurationL( infoBuffer() );
       
  1073         }    
       
  1074     
       
  1075     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1076     RDebug::Print ( _L ( "CG729PayloadFormatRead::ConfigurePayloadFormatL() OUT" ) );
       
  1077     #endif
       
  1078     }
       
  1079 
       
  1080 // -----------------------------------------------------------------------------
       
  1081 // CG729PayloadFormatRead::UpdateConfigurationL
       
  1082 // Update payload decoder parameters
       
  1083 // -----------------------------------------------------------------------------
       
  1084 //
       
  1085 void CG729PayloadFormatRead::UpdateConfigurationL( TMccCodecInfo& aCodecInfo )
       
  1086     {
       
  1087     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1088         RDebug::Print( _L("CG729PayloadFormatRead::UpdateConfigurationL IN") );
       
  1089     #endif
       
  1090     
       
  1091     if ( iCInfo.iMaxPtime != aCodecInfo.iMaxPtime )
       
  1092         {
       
  1093         // Maximum number of frames in RTP payload
       
  1094         const TUint pktCount = aCodecInfo.iMaxPtime / iCInfo.iHwFrameTime;
       
  1095         iFrameTimeInterval = TInt64( iCInfo.iHwFrameTime * TInt8( pktCount ) );
       
  1096         
       
  1097         // PayloadBuffer contains data received from network
       
  1098         if ( iSourceBufOwnership )
       
  1099             {
       
  1100             delete iSourceBuffer;
       
  1101             iSourceBufOwnership = EFalse;
       
  1102             }
       
  1103         iSourceBuffer = NULL;
       
  1104 
       
  1105         TInt plSize = iCInfo.iFrameSize * pktCount + KG729CNFrameSize;
       
  1106         
       
  1107         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
  1108         
       
  1109         iCInfo.iMaxPtime = aCodecInfo.iMaxPtime;
       
  1110         }       
       
  1111     
       
  1112     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1113     RDebug::Print( _L("CG729PayloadFormatRead::UpdateConfigurationL OUT") );
       
  1114     #endif
       
  1115     }
       
  1116 
       
  1117 // -----------------------------------------------------------------------------
       
  1118 // CG729PayloadFormatRead::CreateClipBufferL
       
  1119 // Creates buffer needed in data transfer with format readers clip.
       
  1120 // -----------------------------------------------------------------------------
       
  1121 //
       
  1122 CMMFDataBuffer* CG729PayloadFormatRead::CreateClipBufferL( 
       
  1123         TUint aSize, TBool& aIsOwnBuffer )
       
  1124     {
       
  1125     #ifdef TRACE_G729_PAYLOAD_FORMAT_READ
       
  1126     RDebug::Print( _L("CG729PayloadFormatRead::CreateClipBufferL" ) );
       
  1127     #endif
       
  1128     
       
  1129     CMMFDataBuffer* buffer( NULL );
       
  1130     
       
  1131     if ( iClip->CanCreateSourceBuffer() )
       
  1132         {
       
  1133         static_cast<CMMFFormatDecode*>( iClip )->SuggestSourceBufferSize( aSize );
       
  1134         
       
  1135         TBool reference( EFalse );
       
  1136         CMMFBuffer* sourceBuf 
       
  1137             = iClip->CreateSourceBufferL( KUidMediaTypeAudio, reference );
       
  1138         TBool isSupportedBuf 
       
  1139             = CMMFBuffer::IsSupportedDataBuffer( sourceBuf->Type() );
       
  1140         TBool isOwnBuffer = reference ? EFalse : ETrue;
       
  1141         
       
  1142         if ( !isSupportedBuf )
       
  1143             {
       
  1144             if ( isOwnBuffer )
       
  1145                 {
       
  1146                 delete sourceBuf;
       
  1147                 }
       
  1148             
       
  1149             User::Leave( KErrNotSupported );
       
  1150             }
       
  1151         
       
  1152         aIsOwnBuffer = isOwnBuffer;
       
  1153         buffer = static_cast<CMMFDataBuffer*>( sourceBuf );
       
  1154         }
       
  1155     else
       
  1156         {
       
  1157         aIsOwnBuffer = ETrue;
       
  1158         buffer = CMMFDataBuffer::NewL( aSize );
       
  1159         }
       
  1160     
       
  1161     return buffer;
       
  1162     }
       
  1163 
       
  1164 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1165 
       
  1166 //  End of File