multimediacommscontroller/mmccredpayloadformat/src/mccreddecoder.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2005-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:    Decoder capable to handle redundancy RTP payload.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include    "mccreddecoder.h"
       
    23 #include    "streamformatter.h"
       
    24 #include    "mccredpayloadformatdefs.h"
       
    25 #include    "mccdef.h"
       
    26 
       
    27 const TInt KNumValue3 = 3;
       
    28 const TInt KNumValue4 = 4;
       
    29 
       
    30 // ============================= LOCAL FUNCTIONS ===============================
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CMccRedDecoder::CMccRedDecoder
       
    36 // C++ default constructor can NOT contain any code, that
       
    37 // might leave.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CMccRedDecoder::CMccRedDecoder()
       
    41     {
       
    42     DP_RED_DECODE( "CMccRedDecoder::CMccRedDecoder" )
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CMccRedDecoder::NewL
       
    47 // Two-phased constructor.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CMccRedDecoder* CMccRedDecoder::NewL()
       
    51     {
       
    52     DP_RED_DECODE( "CMccRedDecoder::NewL" )
       
    53     
       
    54     CMccRedDecoder* self = new( ELeave ) CMccRedDecoder;
       
    55     return self;
       
    56     }
       
    57 
       
    58 // Destructor
       
    59 CMccRedDecoder::~CMccRedDecoder()
       
    60     {
       
    61     DP_RED_DECODE( "CMccRedDecoder::~CMccRedDecoder" )
       
    62     
       
    63     iRedHeaderInfo.Close();
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CMCCRedDecoder::DoInitializeL
       
    68 // Initialize decoder after encoding parameters are changed or decoding is 
       
    69 // started again after a pause.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void CMccRedDecoder::DoInitializeL( TInt aRedBlockCount,
       
    73                                     TInt /*aMaxPayloadSize*/,
       
    74                                     TInt /*aNumOfEncodings*/ )
       
    75     {
       
    76     DP_RED_DECODE( "CMccRedDecoder::DoInitializeL" )
       
    77 
       
    78     if ( KMaxRedCount >= aRedBlockCount )
       
    79         {
       
    80         iRedBlockCount = aRedBlockCount;
       
    81         iCurTimeStamp = 0;
       
    82         iLatestTimeStamp = -1;
       
    83         }
       
    84     else
       
    85         {
       
    86         User::Leave( KErrArgument );
       
    87         }
       
    88     }
       
    89     
       
    90 // -----------------------------------------------------------------------------
       
    91 // CMccRedDecoder::DecodePayload
       
    92 // Decode encoding(s) from RTP payload given.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 TInt CMccRedDecoder::DecodePayload( TUint aCurTimeStamp )
       
    96     {
       
    97     DP_RED_DECODE( "CMccRedDecoder::DecodePayload" )
       
    98     
       
    99     iCurTimeStamp = aCurTimeStamp;
       
   100     iRedHeaderInfo.Reset();
       
   101     iEncodings[EMccPrimaryEncoding]->Data().Zero();
       
   102     
       
   103     TInt error = GetBlockInfos( iRedHeaderInfo, iRTPPayload->Data() );
       
   104     if ( KErrNone == error )
       
   105         {
       
   106         // Ignore already received blocks
       
   107         while ( iRedHeaderInfo.Count() 
       
   108                 && static_cast<TInt>( iRedHeaderInfo[0].iTimeStamp ) <= iLatestTimeStamp )
       
   109             {
       
   110             DP_RED_ENCODE2( "BLOCK with TIMESTAMP %d REMOVED",
       
   111                 iRedHeaderInfo[0].iTimeStamp )
       
   112             iRedHeaderInfo.Remove( 0 );
       
   113             }
       
   114             
       
   115         // Separate encodings
       
   116         for ( TInt i = 0; i < iRedHeaderInfo.Count(); i++ )
       
   117             {
       
   118             if ( iRedHeaderInfo[i].iBlockPT 
       
   119                 == iPayloadTypes[EMccPrimaryEncoding] )
       
   120                 {
       
   121                 TDes8& primaryEnc = iEncodings[EMccPrimaryEncoding]->Data();
       
   122                 primaryEnc.Append( iRedHeaderInfo[i].iBlockIndex,
       
   123                                    iRedHeaderInfo[i].iBlockLength );
       
   124                 }
       
   125             else if ( iRedHeaderInfo[i].iBlockPT 
       
   126                         == iPayloadTypes[EMccSecondaryEncoding] )
       
   127                 {
       
   128                 TDes8& secondaryEnc = iEncodings[EMccSecondaryEncoding]->Data();
       
   129                 secondaryEnc.Append( iRedHeaderInfo[i].iBlockIndex,
       
   130                                      iRedHeaderInfo[i].iBlockLength );
       
   131                 }
       
   132             else if ( iRedHeaderInfo[i].iBlockPT == KComfortNoisePT )                
       
   133                 {
       
   134                 DP_RED_DECODE( "CMccRedDecoder::DecodePayload() - CN" )
       
   135                 TDes8& primaryEnc = iEncodings[EMccPrimaryEncoding]->Data();
       
   136                 primaryEnc.Append( iRedHeaderInfo[i].iBlockIndex,
       
   137                                    iRedHeaderInfo[i].iBlockLength );
       
   138                 }
       
   139             else
       
   140                 {
       
   141                 DP_RED_ENCODE( "NOT SUPPORTED ENCODING, SKIPPED" )
       
   142                 }                
       
   143             }
       
   144             
       
   145         iLatestTimeStamp = aCurTimeStamp;
       
   146         return iRedHeaderInfo.Count();
       
   147         }
       
   148     else
       
   149         {
       
   150         DP_RED_DECODE( "CMccRedDecoder::DecodePayload() - \
       
   151             ERROR with GetBlockInfos" )
       
   152         
       
   153         iEncodings[EMccPrimaryEncoding]->Data().Zero();
       
   154         return error;
       
   155         }        
       
   156     }
       
   157 
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CMccRedDecoder::GetBlockInfos
       
   161 // Get block indexes. Redundant blocks are ignored based on timestamp.
       
   162 // Timestamp of the primary data block is saved. Data validity is verified by
       
   163 // checking that decoded block indexes are inside bounds and overall length of
       
   164 // decoded data blocks is not greater than RPT payload length. Time stamps
       
   165 // are also checked that they are in increasing order.
       
   166 // -----------------------------------------------------------------------------
       
   167 //
       
   168 TInt CMccRedDecoder::GetBlockInfos( RArray<TRedHeaderInfo>& aBlockInfos,
       
   169                                     const TDesC8& aRTPPayload )
       
   170     {
       
   171     DP_RED_DECODE( "CMccRedDecoder::GetBlockInfos" )
       
   172     
       
   173     if ( 0 == aRTPPayload.Size() )
       
   174         {
       
   175         return KErrArgument;
       
   176         }
       
   177     
       
   178     const TUint8* seekPtr = aRTPPayload.Ptr();
       
   179     
       
   180     TStreamDecoder streamDecoder;
       
   181     streamDecoder.Initialize( const_cast<TUint8*>( seekPtr ), 0, 0 );
       
   182     
       
   183     TBool FBitSet( ETrue );
       
   184     TInt allBlockLength(0); // For data validity check
       
   185     
       
   186     // Go through redundancy headers and count audio frame blocks
       
   187     // Determine number of redundant frame blocks to ignore based on timestamp
       
   188     TUint frameBlockCount(0);
       
   189     
       
   190     while ( FBitSet )
       
   191         {
       
   192         frameBlockCount++;
       
   193         TRedHeaderInfo curInfo;
       
   194         
       
   195         FBitSet = static_cast<TBool>( streamDecoder.Decode( KFBitLength ) );
       
   196         curInfo.iBlockPT = static_cast<TUint8>( streamDecoder.Decode( KPTFieldBits ) );
       
   197 
       
   198         if ( FBitSet )
       
   199             {
       
   200             TUint offset = streamDecoder.Decode( KTSOffsetBits );
       
   201             curInfo.iTimeStamp = iCurTimeStamp - offset;
       
   202             curInfo.iBlockLength = streamDecoder.Decode( KBlockLengthBits );
       
   203             
       
   204             allBlockLength += curInfo.iBlockLength;
       
   205                     
       
   206             DP_RED_DECODE6( "DEC RED_HEADER NUM %d: F:%d PT:%d \
       
   207             TIMESTAMP:%d, BL:%d", frameBlockCount, ETrue, curInfo.iBlockPT,
       
   208                 curInfo.iTimeStamp, curInfo.iBlockLength )
       
   209             
       
   210             // Jump to the beginning of next redundancy header
       
   211             streamDecoder.Initialize( const_cast<TUint8*>( seekPtr ), 
       
   212                 frameBlockCount * KNumValue4, 0 );
       
   213             }
       
   214         else
       
   215             {
       
   216             // Primary data block's time stamp
       
   217             curInfo.iTimeStamp = iCurTimeStamp;
       
   218             DP_RED_DECODE5( "DEC FINAL RED_HEADER %d: F:%d PT:%d \
       
   219             #TIMESTAMP#:%d", frameBlockCount, 0, curInfo.iBlockPT,
       
   220                 curInfo.iTimeStamp )
       
   221             }
       
   222         
       
   223         TInt err = aBlockInfos.Append( curInfo );
       
   224         if ( err )
       
   225             {
       
   226             return err;
       
   227             }
       
   228         }
       
   229         
       
   230     // Calculate frame block indexes
       
   231     frameBlockCount = aBlockInfos.Count();
       
   232     TUint8* firstBlockIndex = ( const_cast<TUint8*>( seekPtr ) 
       
   233         + frameBlockCount * KRedHeaderSize - KNumValue3 );
       
   234     aBlockInfos[0].iBlockIndex = firstBlockIndex;
       
   235 
       
   236     for( TUint i = 1; i < frameBlockCount; i++ )
       
   237         {
       
   238         aBlockInfos[i].iBlockIndex 
       
   239             = firstBlockIndex + aBlockInfos[i - 1].iBlockLength;
       
   240         }
       
   241 
       
   242     // Length of the last block
       
   243     const TUint8* endPtr = aRTPPayload.Ptr() + aRTPPayload.Length();
       
   244     TInt lastIndex( aBlockInfos.Count() - 1 );
       
   245     aBlockInfos[lastIndex].iBlockLength = 
       
   246         endPtr - aBlockInfos[lastIndex].iBlockIndex;
       
   247     
       
   248     allBlockLength += aBlockInfos[lastIndex].iBlockLength;
       
   249     
       
   250     // Check data validity
       
   251     for ( TUint i = 0; i < frameBlockCount - 1; i++ )
       
   252         {
       
   253         // Check time stamps contiguousness
       
   254         if ( aBlockInfos[i].iTimeStamp > aBlockInfos[i+1].iTimeStamp )
       
   255             {
       
   256             return KErrCorrupt;
       
   257             }
       
   258         
       
   259         // Check index bounds
       
   260         if ( aBlockInfos[i].iBlockIndex < firstBlockIndex
       
   261             || aBlockInfos[i].iBlockIndex > endPtr )
       
   262             {
       
   263             return KErrCorrupt;
       
   264             }      
       
   265         
       
   266         TUint plLen = static_cast<TUint>( aRTPPayload.Length() );
       
   267         if ( aBlockInfos[i].iBlockLength < 1 || aBlockInfos[i+1].iBlockLength < 1
       
   268             || plLen < aBlockInfos[i].iBlockLength )  
       
   269             {
       
   270             return KErrCorrupt;
       
   271             }
       
   272         }
       
   273     
       
   274     // Check overall block lengths
       
   275     if ( aRTPPayload.Length() < allBlockLength || 0 > allBlockLength )
       
   276         {
       
   277         return KErrCorrupt;
       
   278         }
       
   279     else
       
   280         {
       
   281         return KErrNone;
       
   282         }
       
   283     }
       
   284 
       
   285 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   286 
       
   287 //  End of File