multimediacommscontroller/mmccg711payloadformat/src/g711payloadformatread.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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 component capable to read RTP payload containing
       
    15 *                G711 audio.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include    "g711payloadformatread.h"
       
    24 #include    "g711payloadformatdefs.h"
       
    25 #include    "mccrtpdatasource.h"
       
    26 #include    "mccinternaldef.h"
       
    27 #include    "mccdef.h"
       
    28 #include    "mccredpayloadread.h"
       
    29 
       
    30 
       
    31 // ============================= LOCAL FUNCTIONS ===============================
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CG711PayloadFormatRead::CG711PayloadFormatRead
       
    37 // C++ default constructor can NOT contain any code, that
       
    38 // might leave.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CG711PayloadFormatRead::CG711PayloadFormatRead()
       
    42     {
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CG711PayloadFormatRead::ConstructL
       
    47 // Symbian 2nd phase constructor can leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 void CG711PayloadFormatRead::ConstructL( MDataSource* aSource )
       
    51     {
       
    52     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
    53     
       
    54     iClip = aSource;
       
    55     iBufferToReadExists = EFalse;
       
    56 
       
    57     iFourCC.Set( KMccFourCCIdG711 );
       
    58 
       
    59     iFramesPerPacket = 1;
       
    60 
       
    61     // Initialize decoding state machine
       
    62     iStateMachine = CFormatDecodeStateMachine::NewL( this );
       
    63 
       
    64     iStateMachine->ChangeState( EDecodeIdle );
       
    65 
       
    66     iCurrentBuffer = EBufferOne;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CG711PayloadFormatRead::NewL
       
    71 // Two-phased constructor.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CG711PayloadFormatRead* CG711PayloadFormatRead::NewL( MDataSource* aSource )
       
    75     {
       
    76     DP_G711_READ( "CG711PayloadFormatRead::NewL()" );
       
    77 
       
    78     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
    79 
       
    80     CG711PayloadFormatRead* self = new (ELeave) CG711PayloadFormatRead;
       
    81     CleanupStack::PushL( self );
       
    82     self->ConstructL( aSource );
       
    83     CleanupStack::Pop( self );
       
    84     return self;
       
    85     }
       
    86 
       
    87 
       
    88 // Destructor
       
    89 CG711PayloadFormatRead::~CG711PayloadFormatRead()
       
    90     {
       
    91     DP_G711_READ( "CG711PayloadFormatRead::~CG711PayloadFormatRead()" );
       
    92     
       
    93     delete iFrameBufferOne;
       
    94     delete iFrameBufferTwo;
       
    95     if ( iSourceBufOwnership )
       
    96         {
       
    97         delete iSourceBuffer;
       
    98         }
       
    99     
       
   100     delete iStateMachine;
       
   101     
       
   102     iClip = NULL;
       
   103     iFrameArray.Close();
       
   104     }
       
   105 
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CG711PayloadFormatRead::SendDataToSinkL
       
   109 // Send full frame buffer to the DataPath.
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 void CG711PayloadFormatRead::SendDataToSinkL()
       
   113     {
       
   114     if ( iCurrentBuffer == EBufferOne )
       
   115         {
       
   116         DP_G711_READ( "CG711PayloadFormatRead::SendDataToSinkL() - BufferOne" );
       
   117 
       
   118         iDataPath->BufferFilledL( iFrameBufferOne );
       
   119         iCurrentBuffer = EBufferTwo;
       
   120         if ( iBufferToReadExists && !iFrameBufferOne->LastBuffer() )
       
   121             {
       
   122             // More payload buffer is ready
       
   123             iStateMachine->ChangeState( ESourceDataReady );
       
   124             }
       
   125         }
       
   126     else
       
   127         {
       
   128         DP_G711_READ( "CG711PayloadFormatRead::SendDataToSinkL() - BufferTwo" );            
       
   129 
       
   130         iDataPath->BufferFilledL( iFrameBufferTwo );
       
   131         iCurrentBuffer = EBufferOne;
       
   132         if ( iBufferToReadExists && !iFrameBufferTwo->LastBuffer() )
       
   133             {
       
   134             // More payload buffer is ready
       
   135             iStateMachine->ChangeState( ESourceDataReady );
       
   136             }
       
   137        }
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CG711PayloadFormatRead::FillSinkBuffer
       
   142 // Fill SinkBuffer.
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CG711PayloadFormatRead::FillSinkBufferL()
       
   146     {
       
   147     DP_G711_READ( "CG711PayloadFormatRead::FillSinkBuffer()" );
       
   148     
       
   149     CMMFDataBuffer* curFrameBuffer = NULL;
       
   150     if ( EBufferOne == iCurrentBuffer )
       
   151         {
       
   152         curFrameBuffer = iFrameBufferOne;
       
   153         }
       
   154     else
       
   155         {
       
   156         curFrameBuffer = iFrameBufferTwo;
       
   157         }
       
   158         
       
   159     TDes8& curFrameData( curFrameBuffer->Data() );
       
   160     curFrameData.SetLength( KVoIPHeaderLength );
       
   161 
       
   162     // Put next frame decoded from RTP payload to the framebuffer
       
   163     iBufferToReadExists = GetNextFrame( curFrameData );
       
   164 
       
   165     // G.711 Gwc expects 2 bytes for dtx-decision
       
   166     if ( iCnFrame )
       
   167         {
       
   168 	    DP_G711_READ( "CG711PayloadFormatRead::FillSinkBufferL add dtx-header" );
       
   169 
       
   170         curFrameData[0] = KVoIPCNFrame;
       
   171         curFrameData[1] = 0;
       
   172 	    }
       
   173     else
       
   174         {
       
   175 	    DP_G711_READ( "CG711PayloadFormatRead::FillSinkBufferL add voice-header" );
       
   176 
       
   177         curFrameData[0] = KVoIPAudioFrame;
       
   178         curFrameData[1] = 0;
       
   179         }
       
   180 
       
   181 	curFrameBuffer->SetFrameNumber( iRecvHeader.iTimestamp + ( ( iFrameIndex - 1 )
       
   182 	                           * TUint( iCInfo.iHwFrameTime * KDefaultSampleRateInkHz ) ) );
       
   183 
       
   184     DP_G711_READ2( "CG711PayloadFormatRead::FillSinkBuffer - FRAMENUM: %u",
       
   185         curFrameBuffer->FrameNumber() );
       
   186     
       
   187     const TInt dataLen( ( iCInfo.iHwFrameTime * KBitsPerByte )
       
   188                             + KVoIPHeaderLength );
       
   189     curFrameData.SetLength( dataLen );          
       
   190     curFrameBuffer->SetStatus( EFull );
       
   191 
       
   192     DP_G711_READ2( "CG711PayloadFormatRead: Filled framebuffer with buf. size: %d",
       
   193         curFrameBuffer->BufferSize() );
       
   194 
       
   195     iStateMachine->ChangeState( EEmptyDataToSink );
       
   196 
       
   197     DP_G711_READ( "CG711PayloadFormatRead::FillSinkBuffer - DONE" );
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CG711PayloadFormatRead::FillSourceBufferL
       
   202 // Send fill buffer request to the RTP Data Source.
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void CG711PayloadFormatRead::FillSourceBufferL()
       
   206     {
       
   207     DP_G711_READ( "CG711PayloadFormatRead::FillSourceBufferL()");
       
   208     
       
   209     const TMediaId mediaId( KUidMediaTypeAudio );
       
   210     iClip->FillBufferL( iSourceBuffer, this, mediaId );     
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CG711PayloadFormatRead::SourcePrimeL
       
   215 // Prime source.
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CG711PayloadFormatRead::SourcePrimeL()
       
   219     {
       
   220     DP_G711_READ( "CG711PayloadFormatRead::SourcePrimeL()" );
       
   221     
       
   222     iClip->SourcePrimeL();
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CG711PayloadFormatRead::SourcePlayL
       
   227 // Start playing.
       
   228 // -----------------------------------------------------------------------------
       
   229 void CG711PayloadFormatRead::SourcePlayL()
       
   230     {
       
   231     DP_G711_READ( "CG711PayloadFormatRead::SourcePlayL()" );
       
   232     
       
   233     iClip->SourcePlayL();
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CG711PayloadFormatRead::SourcePauseL
       
   238 // Pause source.
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CG711PayloadFormatRead::SourcePauseL()
       
   242     {
       
   243     DP_G711_READ( "CG711PayloadFormatRead::SourcePauseL" );
       
   244     
       
   245     iStateMachine->Cancel();
       
   246     iStateMachine->ChangeState( EDecodeIdle );
       
   247     
       
   248     iFrameBufferOne->SetLastBuffer( EFalse );
       
   249     iFrameBufferTwo->SetLastBuffer( EFalse );
       
   250 
       
   251     iFrameBufferOne->SetStatus( EAvailable );
       
   252     iFrameBufferTwo->SetStatus( EAvailable );
       
   253 
       
   254     iBufferToReadExists = EFalse;
       
   255     iCurrentBuffer = EBufferOne;
       
   256     
       
   257     iClip->SourcePauseL();
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CG711PayloadFormatRead::SourceStopL
       
   262 // Stop source
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CG711PayloadFormatRead::SourceStopL()
       
   266     {
       
   267     DP_G711_READ( "CG711PayloadFormatRead::SourceStopL" );
       
   268     
       
   269     // DO NOT RESET PACKET COUNT BACK TO ZERO HERE
       
   270     // UPPER LAYER MAY CALL LastDlPacketCount LATER
       
   271 
       
   272     iStateMachine->Cancel();
       
   273 
       
   274     iStateMachine->ChangeState( EDecodeIdle );
       
   275     iFrameBufferOne->SetLastBuffer( EFalse );
       
   276     iFrameBufferTwo->SetLastBuffer( EFalse );
       
   277 
       
   278     iFrameBufferOne->SetStatus( EAvailable );
       
   279     iFrameBufferTwo->SetStatus( EAvailable );
       
   280 
       
   281     iBufferToReadExists = EFalse;
       
   282     iCurrentBuffer = EBufferOne;
       
   283 
       
   284     iClip->SourceStopL();
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CG711PayloadFormatRead::BufferFilledL
       
   289 // 
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CG711PayloadFormatRead::DataBufferFilledL( CMMFBuffer* aBuffer,
       
   293     const TRtpRecvHeader& aRecvHeader )
       
   294     {
       
   295     DP_G711_READ2( "CG711PayloadFormatRead::DataBufferFilledL iTimestamp: %u",
       
   296         aRecvHeader.iTimestamp )
       
   297     DP_G711_READ2( "CG711PayloadFormatRead::DataBufferFilledL this: 0x%x", this )
       
   298     
       
   299     __ASSERT_ALWAYS( aBuffer, User::Leave( KErrArgument ) );
       
   300     __ASSERT_ALWAYS( KUidMmfDataBuffer == aBuffer->Type(), 
       
   301         User::Leave( KErrNotSupported ) );
       
   302     __ASSERT_ALWAYS( iSourceBuffer == aBuffer, User::Leave( KErrArgument ) );
       
   303     
       
   304 	iRecvHeader = aRecvHeader;
       
   305     if ( KComfortNoisePT == iRecvHeader.iPayloadType
       
   306         || KOldComfortNoisePT == iRecvHeader.iPayloadType )
       
   307         {
       
   308         iCnFrame = ETrue;
       
   309         }
       
   310     else 
       
   311         {
       
   312         iCnFrame = EFalse;
       
   313         }
       
   314     
       
   315     TInt numOfFrames = DecodePayload( iSourceBuffer->Data() );
       
   316     
       
   317     // Whenever BufferFilledL is called from RtpSourceSink
       
   318     // Set the state machine to fillsinkbuffer
       
   319     if ( numOfFrames )
       
   320         {
       
   321         iBufferToReadExists = ETrue;
       
   322         iSourceBuffer->SetFrameNumber( iRecvHeader.iTimestamp );
       
   323         iStateMachine->ChangeState( ESourceDataReady );
       
   324         }
       
   325     else
       
   326         {
       
   327         DP_G711_READ( "CG711PayloadFormatRead::BufferFilledL, decode failed" )
       
   328         
       
   329         FillSourceBufferL();
       
   330         }
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CG711PayloadFormatRead::SampleRate
       
   335 // Returns samplerate.
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 TUint CG711PayloadFormatRead::SampleRate()
       
   339     {
       
   340     return KDefaultSampleRate;
       
   341     }  
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CG711PayloadFormatRead::SetSampleRate
       
   345 // Set samplerate.
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 TInt CG711PayloadFormatRead::SetSampleRate( TUint aSampleRate )
       
   349     {
       
   350     if ( KDefaultSampleRate != aSampleRate )
       
   351         {
       
   352         return KErrNotSupported;
       
   353         }
       
   354     else
       
   355         {
       
   356         return KErrNone;
       
   357         }
       
   358     }
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CG711PayloadFormatRead::NumChannels
       
   362 // Returns number of channels.
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 TUint CG711PayloadFormatRead::NumChannels()
       
   366     {
       
   367     return KMono;
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CG711PayloadFormatRead::SourceThreadLogoff
       
   372 // Logout the source thread.
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 void CG711PayloadFormatRead::SourceThreadLogoff()
       
   376     {
       
   377     iClip->SourceThreadLogoff();
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CG711PayloadFormatRead::NegotiateSourceL
       
   382 // Negotiate Source.
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 void CG711PayloadFormatRead::NegotiateSourceL( MDataSink& aDataSink )
       
   386     {
       
   387     DP_G711_READ( "CG711PayloadFormatRead::NegotiateSourceL()" ) ;
       
   388     
       
   389     iDataPath = &aDataSink;
       
   390     iClip->NegotiateSourceL( *this );
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CG711PayloadFormatRead::SourceThreadLogon
       
   395 // Logon to the source thread.
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 TInt CG711PayloadFormatRead::SourceThreadLogon( MAsyncEventHandler& aEventHandler )
       
   399     {
       
   400     if ( iClip )
       
   401         {
       
   402         iClip->SourceThreadLogon( aEventHandler );
       
   403         return KErrNone;
       
   404         }
       
   405     else
       
   406         {
       
   407         return KErrNotReady;
       
   408         }
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CG711PayloadFormatRead::SetSourceDataTypeCode
       
   413 // Sets source datatype fourCC code
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 TInt CG711PayloadFormatRead::SetSourceDataTypeCode( TFourCC aSourceFourCC,
       
   417                                                     TMediaId aMediaId )
       
   418     {
       
   419     DP_G711_READ( "CG711PayloadFormatRead::SetSourceDataTypeCode()" );
       
   420     
       
   421     if ( KUidMediaTypeAudio != aMediaId.iMediaType ) 
       
   422         {
       
   423         return KErrNotSupported;
       
   424         }
       
   425 
       
   426     iFourCC = aSourceFourCC;
       
   427 
       
   428     iClip->SetSourceDataTypeCode( iFourCC, aMediaId );
       
   429 
       
   430     return KErrNone;
       
   431     }
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CG711PayloadFormatRead::SourceDataTypeCode
       
   435 // Returns the current datatype FourCC code.
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 TFourCC CG711PayloadFormatRead::SourceDataTypeCode( TMediaId aMediaId )
       
   439     {
       
   440     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   441         {
       
   442         return iFourCC;
       
   443         }
       
   444     else
       
   445         {
       
   446         return TFourCC(); //defaults to 'NULL' fourCC
       
   447         }
       
   448     }
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // CG711PayloadFormatRead::SinkDataTypeCode
       
   452 // Returns the current datatype FourCC code.
       
   453 // -----------------------------------------------------------------------------
       
   454 //
       
   455 TFourCC CG711PayloadFormatRead::SinkDataTypeCode( TMediaId aMediaId )
       
   456     {
       
   457     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   458         {
       
   459         return iFourCC;
       
   460         }
       
   461     else
       
   462         {
       
   463         return TFourCC(); //defaults to 'NULL' fourCC
       
   464         }
       
   465     }
       
   466     
       
   467 // -----------------------------------------------------------------------------
       
   468 // CG711PayloadFormatRead::CreateSourceBufferL
       
   469 // Create a source buffer for the given media and indicate in aReference if 
       
   470 // buffer is created.
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 CMMFBuffer* CG711PayloadFormatRead::CreateSourceBufferL( TMediaId aMediaId,
       
   474                                                          TBool &aReference )
       
   475     {
       
   476     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   477         {
       
   478         User::Leave( KErrNotSupported );
       
   479         }
       
   480 
       
   481     // The source buffers belong to G711PayloadFormatRead not to datapath
       
   482     // aference should be set to ETrue and destroyed by G711PayloadFormatRead itself.
       
   483     aReference = ETrue;
       
   484     
       
   485     return iFrameBufferOne; 
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CG711PayloadFormatRead::CreateSourceBufferL
       
   490 // Create a source buffer for the given media, setting frame size to match
       
   491 // the given sink buffer.
       
   492 // -----------------------------------------------------------------------------
       
   493 CMMFBuffer* CG711PayloadFormatRead::CreateSourceBufferL( TMediaId aMediaId,
       
   494                                                         CMMFBuffer& /*aSinkBuffer*/,
       
   495                                                         TBool& aReference )
       
   496     {
       
   497     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   498         {
       
   499         User::Leave( KErrNotSupported );
       
   500         }
       
   501 
       
   502     return CreateSourceBufferL( aMediaId, aReference );
       
   503     }
       
   504 
       
   505 // -----------------------------------------------------------------------------
       
   506 // CG711PayloadFormatRead::FillBufferL
       
   507 // Fill Buffer.
       
   508 // -----------------------------------------------------------------------------
       
   509 //
       
   510 void CG711PayloadFormatRead::FillBufferL( CMMFBuffer* aBuffer, 
       
   511                                           MDataSink* aConsumer, 
       
   512                                           TMediaId aMediaId )
       
   513     {
       
   514     if ( NULL == aBuffer ) 
       
   515         {
       
   516         User::Leave( KErrGeneral );
       
   517         }
       
   518         
       
   519     DP_G711_READ3( 
       
   520     "CG711PayloadFormatRead::FillBufferL() buffer 0x%x passed in with length %d bytes",
       
   521     aBuffer, aBuffer->BufferSize() );
       
   522          
       
   523     if ( KUidMediaTypeAudio != aMediaId.iMediaType )
       
   524         {
       
   525         User::Leave( KErrNotSupported );
       
   526         }
       
   527 
       
   528     if ( KUidMmfDataBuffer != aBuffer->Type() )
       
   529         {
       
   530         User::Leave( KErrNotSupported );
       
   531         }
       
   532 
       
   533     iDataPath = aConsumer;
       
   534 
       
   535     // aBuffer is a reference to those frame buffers that G711PayloadFormatRead owns
       
   536     aBuffer->SetLastBuffer( EFalse );
       
   537 
       
   538     if ( EBufferOne == iCurrentBuffer )
       
   539         {
       
   540         iFrameBufferTwo->SetStatus( EAvailable );
       
   541         }
       
   542     else
       
   543         {
       
   544         iFrameBufferOne->SetStatus( EAvailable );
       
   545         }
       
   546 
       
   547     if ( iBufferToReadExists )
       
   548         {
       
   549         // All decoded frame are not passed to the datapath
       
   550         iStateMachine->ChangeState( ESourceDataReady );
       
   551         }
       
   552     else
       
   553         {
       
   554         FillSourceBufferL(); // No payload ask for it
       
   555         } 
       
   556     }
       
   557 
       
   558 // -----------------------------------------------------------------------------
       
   559 // CG711PayloadFormatRead::Streams
       
   560 // Return number of audio streams for the given media.
       
   561 // -----------------------------------------------------------------------------
       
   562 //
       
   563 TUint CG711PayloadFormatRead::Streams( TUid aMediaType ) const
       
   564     {
       
   565     // Need to check aMediaType for audio
       
   566     if ( KUidMediaTypeAudio == aMediaType )
       
   567         {
       
   568         return 1;
       
   569         }
       
   570     else
       
   571         {
       
   572         return 0;
       
   573         }
       
   574     }
       
   575 
       
   576 // -----------------------------------------------------------------------------
       
   577 // CG711PayloadFormatRead::FrameTimeInterval
       
   578 // Return the frame time interval for the given media
       
   579 // -----------------------------------------------------------------------------
       
   580 //
       
   581 TTimeIntervalMicroSeconds 
       
   582     CG711PayloadFormatRead::FrameTimeInterval( TMediaId aMediaId ) const
       
   583     {
       
   584     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   585         {
       
   586         return iFrameTimeInterval;
       
   587         }
       
   588     else
       
   589         {
       
   590         return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   591         }
       
   592     }
       
   593 
       
   594 // -----------------------------------------------------------------------------
       
   595 // CG711PayloadFormatRead::Duration
       
   596 // Return the frame time interval for the given media.
       
   597 // NOT SUPPORTED
       
   598 // -----------------------------------------------------------------------------
       
   599 //
       
   600 TTimeIntervalMicroSeconds 
       
   601     CG711PayloadFormatRead::Duration( TMediaId /*aMediaType*/ ) const
       
   602     {
       
   603     return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   604     }
       
   605 
       
   606 // -----------------------------------------------------------------------------
       
   607 // CG711PayloadFormatRead::DecodePayload
       
   608 // Decodes all audio frames from the received RTP payload buffer. Decoded
       
   609 // audio frames are saved to the internal array so that audio frames can be
       
   610 // requested one at a time with GetNextFrame() -method.
       
   611 // No assumption about frame count in RTP packet is done.
       
   612 // -----------------------------------------------------------------------------
       
   613 //
       
   614 TInt CG711PayloadFormatRead::DecodePayload( TDes8& aSourceBuffer )
       
   615     {
       
   616     DP_G711_READ2( "CG711PayloadFormatRead::DecodePayload SourceBufSize: %d",
       
   617         aSourceBuffer.Size() );
       
   618     DP_G711_READ2( "CG711PayloadFormatRead::DecodePayload SourceBufLength: %d",
       
   619         aSourceBuffer.Length() );
       
   620     
       
   621     iFrameIndex = 0;
       
   622     
       
   623     const TUint8* framePtr = aSourceBuffer.Ptr();
       
   624     const TUint8* endPtr = aSourceBuffer.Ptr() + aSourceBuffer.Size();
       
   625     
       
   626     TInt frames = aSourceBuffer.Size() / TInt( iCInfo.iFrameSize );
       
   627     
       
   628     DP_G711_READ2( "CG711PayloadFormatRead::DecodePayload FrameSize: %d",
       
   629         iCInfo.iFrameSize );
       
   630     DP_G711_READ2( "CG711PayloadFormatRead::DecodePayload Frames: %d",
       
   631         frames );
       
   632     
       
   633     // Construct pointers to frames in payload if not in CN mode
       
   634     if ( !iCnFrame )
       
   635         {
       
   636         while ( frames-- )
       
   637             {
       
   638             const TPtr8 bufPtr( const_cast<TUint8*>( framePtr ),
       
   639                 iCInfo.iFrameSize, iCInfo.iFrameSize );
       
   640             iFrameArray.Append( bufPtr );
       
   641             
       
   642             framePtr += iCInfo.iFrameSize;
       
   643             if ( framePtr >= endPtr )
       
   644                 {
       
   645                 frames = 0;
       
   646                 }
       
   647             }
       
   648         }
       
   649     else if ( aSourceBuffer.Size() && iCnFrame )
       
   650         {
       
   651         // If it is a CN frame, then we must do special handling. This is
       
   652         // because e.g Cisco kit sends 1 byte CN frames, thus we need to
       
   653         // expand the source buffer with zeroes.
       
   654         TInt targetLen = iCInfo.iFrameSize;
       
   655         targetLen = targetLen - aSourceBuffer.Size();
       
   656         
       
   657         DP_G711_READ2( "CG711PayloadFormatRead::DecodePayload CN frame size adjust: %d",
       
   658             targetLen );
       
   659         
       
   660         const TChar zero( '0' );
       
   661         aSourceBuffer.AppendFill( zero, targetLen );
       
   662         
       
   663         const TPtr8 bufPtr( const_cast<TUint8*>( framePtr ),
       
   664             iCInfo.iFrameSize, iCInfo.iFrameSize );
       
   665         
       
   666         iFrameArray.Append( bufPtr );
       
   667         }
       
   668     
       
   669     
       
   670     return iFrameArray.Count();
       
   671     }
       
   672 
       
   673 // -----------------------------------------------------------------------------
       
   674 // CG711PayloadFormatRead::GetNextFrameL
       
   675 // Passes next audio frame decoded with DecodePayload(). Return ETrue if decoded
       
   676 // frames are remaining.
       
   677 // -----------------------------------------------------------------------------
       
   678 //
       
   679 TBool CG711PayloadFormatRead::GetNextFrame( TDes8& aToBuffer )
       
   680     {
       
   681     iFrameIndex++;
       
   682     
       
   683     DP_G711_READ3( "CG711PayloadFormatRead::GetNextFrame - FrameCount: %d, FrameIndex: %d",
       
   684         iFrameArray.Count(), iFrameIndex );
       
   685     
       
   686     const TInt frameCount( iFrameArray.Count() );
       
   687     
       
   688     if ( iFrameIndex == frameCount )
       
   689         {
       
   690         aToBuffer.Append( iFrameArray[iFrameIndex - 1] );
       
   691         iFrameArray.Reset();
       
   692         return EFalse;
       
   693         }
       
   694     else if ( iFrameIndex < frameCount )
       
   695         {
       
   696         aToBuffer.Append( iFrameArray[iFrameIndex - 1] );
       
   697         return ETrue;
       
   698         }
       
   699     else
       
   700         {
       
   701         return EFalse;
       
   702         }        
       
   703     }
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CG711PayloadFormatRead::ConfigurePayloadFormatL
       
   707 // Configure payload decoding parameters.
       
   708 // -----------------------------------------------------------------------------
       
   709 //
       
   710 void CG711PayloadFormatRead::ConfigurePayloadFormatL( const TDesC8& aConfigParams )
       
   711     {
       
   712     DP_G711_READ( "CG711PayloadFormatRead::ConfigurePayloadFormatL" );
       
   713     
       
   714     __ASSERT_ALWAYS( aConfigParams.Size() == sizeof( TMccCodecInfo ),
       
   715         User::Leave( KErrArgument ) );
       
   716     
       
   717     TMccCodecInfoBuffer infoBuffer;
       
   718     infoBuffer.Copy( aConfigParams );
       
   719     
       
   720     if ( !infoBuffer() .iIsUpdate )
       
   721         {
       
   722         iCInfo = infoBuffer();
       
   723         // Calculates frames/packet, frame size and frame time interval
       
   724         iFramesPerPacket = TInt8( iCInfo.iMaxPtime / iCInfo.iHwFrameTime );
       
   725         iCInfo.iFrameSize =
       
   726             static_cast<TUint>( KDefaultSampleRateInkHz * iCInfo.iHwFrameTime );
       
   727         iFrameTimeInterval = TInt64( iCInfo.iHwFrameTime );
       
   728     	
       
   729         // Create two frame buffers used in data transfer with datapath.
       
   730         // Space for two byte additional header needed by HW codec is reserved.
       
   731         iFrameBufferOne =
       
   732             CMMFDataBuffer::NewL( iCInfo.iFrameSize + KVoIPHeaderLength );
       
   733         iFrameBufferTwo =
       
   734             CMMFDataBuffer::NewL( iCInfo.iFrameSize + KVoIPHeaderLength );
       
   735         
       
   736         // PayloadBuffer contains data received from network
       
   737         TInt plSize = iCInfo.iFrameSize * iFramesPerPacket;
       
   738         DP_G711_READ3( "CG711PayloadFormatRead::ConfigurePayloadFormatL \
       
   739 FramesPerPacket: %d, FrameSize: %d" , iFramesPerPacket, iCInfo.iFrameSize );
       
   740         
       
   741         if ( EGenRedUsed == iCInfo.iAlgoUsed )
       
   742             {
       
   743             DP_G711_READ2( "CG711PayloadFormatRead::ConfigurePayloadFormatL, RED LEVEL: %d",
       
   744                 iCInfo.iRedundancyCount );
       
   745             
       
   746             if ( iCInfo.iRedundancyCount )
       
   747                 {
       
   748                 plSize *= iCInfo.iRedundancyCount;
       
   749                 }
       
   750             
       
   751             CPayloadFormatRead* redDecoder 
       
   752                 = static_cast<CPayloadFormatRead*>( iClip );
       
   753             
       
   754             TMccRedPayloadReadConfig config;
       
   755             config.iRedBlockCount = iCInfo.iRedundancyCount;
       
   756             config.iMaxPayloadSize = iCInfo.iFrameSize * iFramesPerPacket;
       
   757             config.iNumOfEncodings = 1;
       
   758             config.iRedPayloadType = iCInfo.iRedundantPayload;
       
   759             config.InitPayloadTypes();
       
   760             config.iEncPayloadTypes[0] = iCInfo.iPayloadType;
       
   761             TMccRedPayloadReadPckg pckg( config );
       
   762             redDecoder->ConfigurePayloadFormatL( pckg );
       
   763             }
       
   764         
       
   765         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
   766         }
       
   767     else
       
   768         {
       
   769         DP_G711_READ( "CG711PayloadFormatRead::ConfigurePayloadFormatL - Update Configuration" );
       
   770         
       
   771         UpdateConfigurationL( infoBuffer() ); 
       
   772         }
       
   773     
       
   774     DP_G711_READ( "CG711PayloadFormatRead::ConfigurePayloadFormatL OUT" );
       
   775     }
       
   776 
       
   777 // -----------------------------------------------------------------------------
       
   778 // CG711PayloadFormatRead::UpdateConfigurationL
       
   779 // Update payload decoder parameters
       
   780 // -----------------------------------------------------------------------------
       
   781 //
       
   782 void CG711PayloadFormatRead::UpdateConfigurationL( TMccCodecInfo& aCodecInfo )
       
   783     {
       
   784     DP_G711_READ( "CG711PayloadFormatRead::UpdateConfigurationL" );
       
   785     
       
   786     DP_G711_READ2( "CG711PayloadFormatRead::UpdateConfigurationL maxptime: %d", 
       
   787                    aCodecInfo.iMaxPtime )
       
   788     DP_G711_READ2( "CG711PayloadFormatRead::UpdateConfigurationL hwframe: %d", 
       
   789                    aCodecInfo.iHwFrameTime )
       
   790     
       
   791     if ( iCInfo.iMaxPtime != aCodecInfo.iMaxPtime ||
       
   792         iCInfo.iHwFrameTime != aCodecInfo.iHwFrameTime )
       
   793         {
       
   794         DP_G711_READ( "CG711PayloadFormatRead::UpdateConfigurationL IS UPDATE" )
       
   795         
       
   796         iCInfo.iHwFrameTime = aCodecInfo.iHwFrameTime;
       
   797         iCInfo.iMaxPtime = aCodecInfo.iMaxPtime;
       
   798         
       
   799         iFrameTimeInterval = TInt64( iCInfo.iHwFrameTime );
       
   800         
       
   801         if ( iSourceBufOwnership )
       
   802             {
       
   803             delete iSourceBuffer;
       
   804             }
       
   805 
       
   806         iSourceBuffer = NULL;
       
   807         
       
   808         // Update sink buffers only if they are not big enough, extra space
       
   809         // does not matter really
       
   810         TUint updatedFrameSize = 
       
   811             static_cast<TUint>( KDefaultSampleRateInkHz * iCInfo.iHwFrameTime );
       
   812         if ( updatedFrameSize > iCInfo.iFrameSize )
       
   813             {
       
   814             DP_G711_READ( "CG711PayloadFormatRead::UpdateConfigurationL, \
       
   815 creating new sink buffers" );
       
   816             
       
   817             CMMFDataBuffer* bufferOne = 
       
   818                 CMMFDataBuffer::NewL( updatedFrameSize + KVoIPHeaderLength ); 
       
   819             CleanupStack::PushL( bufferOne ); 
       
   820             CMMFDataBuffer* bufferTwo =
       
   821                 CMMFDataBuffer::NewL( updatedFrameSize + KVoIPHeaderLength );
       
   822             CleanupStack::PushL( bufferTwo ); 
       
   823             delete iFrameBufferOne;
       
   824             iFrameBufferOne = bufferOne;
       
   825             delete iFrameBufferTwo;
       
   826             iFrameBufferTwo = bufferTwo;
       
   827             CleanupStack::Pop( bufferTwo );
       
   828             CleanupStack::Pop( bufferOne );
       
   829             }
       
   830             
       
   831         iCInfo.iFrameSize = updatedFrameSize;
       
   832         
       
   833         // PayloadBuffer contains data received from network
       
   834         const TUint pktCount = aCodecInfo.iMaxPtime / iCInfo.iHwFrameTime;
       
   835         TInt plSize = iCInfo.iFrameSize * pktCount;
       
   836         if ( iCInfo.iRedundancyCount )
       
   837             {
       
   838             plSize *= iCInfo.iRedundancyCount;
       
   839             }
       
   840         
       
   841         DP_G711_READ3( "CG711PayloadFormatRead::UpdateConfigurationL \
       
   842 pktCount: %d, iFrameSize: %d", pktCount, iCInfo.iFrameSize )
       
   843         
       
   844         iSourceBuffer = CreateClipBufferL( plSize, iSourceBufOwnership );
       
   845         iFrameArray.Reset();
       
   846         iFrameIndex = 0;
       
   847         iBufferToReadExists = EFalse;
       
   848         FillSourceBufferL();
       
   849         }
       
   850     
       
   851     DP_G711_READ( "CG711PayloadFormatRead::UpdateConfigurationL, exit" )
       
   852     }
       
   853 
       
   854 // -----------------------------------------------------------------------------
       
   855 // CG711PayloadFormatRead::CreateClipBufferL
       
   856 // Creates buffer needed in data transfer with format readers clip.
       
   857 // -----------------------------------------------------------------------------
       
   858 //
       
   859 CMMFDataBuffer* CG711PayloadFormatRead::CreateClipBufferL( 
       
   860         TUint aSize, TBool& aIsOwnBuffer )
       
   861     {
       
   862     DP_G711_READ( "CG711PayloadFormatRead::CreateClipBufferL" );
       
   863     CMMFDataBuffer* buffer( NULL );
       
   864     
       
   865     if ( iClip->CanCreateSourceBuffer() )
       
   866         {
       
   867         static_cast<CMMFFormatDecode*>( iClip )->SuggestSourceBufferSize( aSize );
       
   868         
       
   869         TBool reference( EFalse );
       
   870         CMMFBuffer* sourceBuf 
       
   871             = iClip->CreateSourceBufferL( KUidMediaTypeAudio, reference );
       
   872         TBool isSupportedBuf 
       
   873             = CMMFBuffer::IsSupportedDataBuffer( sourceBuf->Type() );
       
   874         TBool isOwnBuffer = reference ? EFalse : ETrue;
       
   875         
       
   876         if ( !isSupportedBuf )
       
   877             {
       
   878             if ( isOwnBuffer )
       
   879                 {
       
   880                 delete sourceBuf;
       
   881                 }
       
   882             
       
   883             User::Leave( KErrNotSupported );
       
   884             }
       
   885         
       
   886         aIsOwnBuffer = isOwnBuffer;
       
   887         buffer = static_cast<CMMFDataBuffer*>( sourceBuf );
       
   888         }
       
   889     else
       
   890         {
       
   891         aIsOwnBuffer = ETrue;
       
   892         buffer = CMMFDataBuffer::NewL( aSize );
       
   893         }
       
   894     
       
   895     return buffer;
       
   896     }
       
   897 
       
   898 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   899 
       
   900 //  End of File