multimediacommscontroller/mmccredpayloadformat/src/mccredencoder.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:    Encoder implementation capable to form redundancy RTP payload.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include    "mccredencoder.h"
       
    24 #include    "streamformatter.h"
       
    25 #include    "mccredpayloadformatdefs.h"
       
    26 #include    "mccdef.h"
       
    27 
       
    28 // CONSTANTS
       
    29 const TUint KPrimaryEncPTInd = 0;
       
    30 
       
    31 // ============================= LOCAL FUNCTIONS ===============================
       
    32 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CMccRedEncoder::CMccRedEncoder
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CMccRedEncoder::CMccRedEncoder()
       
    43     {
       
    44     DP_RED_ENCODE( "CMccRedEncoder::CMccRedEncoder" )
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CMccRedEncoder::ConstructL
       
    49 // Symbian 2nd phase constructor can leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CMccRedEncoder::ConstructL()
       
    53     {
       
    54     DP_RED_ENCODE( "CMccRedEncoder::ConstructL" )
       
    55     
       
    56     iRedHeaders 
       
    57         = HBufC8::NewL( KDefRedCount * KRedHeaderSize + KFinalHeaderSize );
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CMccRedEncoder::NewL
       
    62 // Two-phased constructor.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CMccRedEncoder* CMccRedEncoder::NewL()
       
    66     {
       
    67     DP_RED_ENCODE( "CMccRedEncoder::NewL" )
       
    68         
       
    69     CMccRedEncoder* self = new( ELeave ) CMccRedEncoder;
       
    70     
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     CleanupStack::Pop( self );
       
    74 
       
    75     return self;
       
    76     }
       
    77 
       
    78     
       
    79 // Destructor
       
    80 CMccRedEncoder::~CMccRedEncoder()
       
    81     {
       
    82     DP_RED_ENCODE( "CMccRedEncoder::~CMccRedEncoder" )
       
    83         
       
    84     iRedTimeStamps.Close();
       
    85     delete iRedHeaders;
       
    86     delete iRedPayload;
       
    87     }
       
    88 
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CMCCRedEncoder::DoInitializeL
       
    92 // Initialize encoder after encoding parameters are changed or encoding is
       
    93 // started again after pause.
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CMccRedEncoder::DoInitializeL( TInt aRedBlockCount, TInt aMaxPayloadSize,
       
    97     TInt /*aNumOfEncodings*/ )
       
    98     {
       
    99     DP_RED_ENCODE( "CMccRedEncoder::DoInitializeL" )
       
   100 
       
   101     if ( KMaxRedCount < aRedBlockCount || 0 > aRedBlockCount )
       
   102         {
       
   103         User::Leave( KErrArgument );
       
   104         }
       
   105         
       
   106     if ( KDefRedCount != aRedBlockCount )
       
   107         {
       
   108         delete iRedHeaders;
       
   109         iRedHeaders = NULL;
       
   110         iRedHeaders = HBufC8::NewL( 
       
   111             aRedBlockCount * KRedHeaderSize + KFinalHeaderSize );
       
   112         }
       
   113 
       
   114     // Memory for redundant payloads without redundancy headers
       
   115     TInt redPayloadSize( aRedBlockCount * aMaxPayloadSize );
       
   116     if ( iRedPayload )
       
   117         {
       
   118         if ( iRedPayload->Data().MaxSize() < redPayloadSize )
       
   119             {
       
   120             delete iRedPayload;
       
   121             iRedPayload = NULL;
       
   122             iRedPayload = CMMFDataBuffer::NewL( redPayloadSize );
       
   123             }
       
   124         }
       
   125     else
       
   126         {
       
   127         iRedPayload = CMMFDataBuffer::NewL( redPayloadSize );
       
   128         }
       
   129     
       
   130     iRedPackets = EFalse;
       
   131     iRedTimeStamps.Reset();
       
   132     iRedHeaders->Des().Zero();
       
   133     }
       
   134 
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CMccRedEncoder::EncodePayloadL
       
   138 // Encode payloads set with SetEncoding() -method.
       
   139 // -----------------------------------------------------------------------------
       
   140 //    
       
   141 void CMccRedEncoder::EncodePayloadL( TUint aCurTimeStamp )
       
   142     {
       
   143     DP_RED_ENCODE( "CMccRedEncoder::EncodePayload" )
       
   144     __ASSERT_ALWAYS( iRTPPayload, User::Leave( KErrNotReady ) );
       
   145     
       
   146     TDes8& rtpPayload = iRTPPayload->Data();
       
   147     rtpPayload.Zero();
       
   148     
       
   149     // Copy redundant payload if exist
       
   150     if ( iRedPackets )
       
   151         {
       
   152         rtpPayload.Append( iRedPayload->Data() );
       
   153         }
       
   154     
       
   155     // Append new payload to the end of redundancy RTP payload
       
   156     rtpPayload.Append( iEncodings[EMccPrimaryEncoding]->Data() );
       
   157     iRedTimeStamps.AppendL( aCurTimeStamp );
       
   158     
       
   159     ConstructRedHeaders( aCurTimeStamp );
       
   160     rtpPayload.Insert( 0, *iRedHeaders );
       
   161     
       
   162     // Save redundant data block
       
   163     iRedPayload->Data().Copy( iEncodings[EMccPrimaryEncoding]->Data() );
       
   164     iRedPackets = ETrue;
       
   165     }
       
   166 
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CMccRedEncoder::ConstructRedHeaders
       
   170 // Construct redundancy headers.
       
   171 // -----------------------------------------------------------------------------
       
   172 //        
       
   173 void CMccRedEncoder::ConstructRedHeaders( TUint aCurTimeStamp )
       
   174     {
       
   175     DP_RED_ENCODE( "CMccRedEncoder::ConstructRedHeaders" )
       
   176         
       
   177     // Construct redundancy headers, final header is one byte long
       
   178     iRedHeaders->Des().Zero();
       
   179     const TUint8* seekPtr = iRedHeaders->Ptr();
       
   180 
       
   181     TStreamEncoder streamEncoder;
       
   182     streamEncoder.Initialize( const_cast<TUint8*>( seekPtr ), 0, 0 );
       
   183     
       
   184     // Encode headers except final header
       
   185     if ( 1 < iRedTimeStamps.Count() )
       
   186         {
       
   187         iRedHeaders->Des().SetLength( 
       
   188             iRedBlockCount * KRedHeaderSize + KFinalHeaderSize );
       
   189         
       
   190         for ( TInt i = 0; i < iRedBlockCount; i++ )
       
   191             {
       
   192             // F-bit            
       
   193             streamEncoder.Encode( 1, KFBitLength );
       
   194             // PT
       
   195             streamEncoder.Encode( iPayloadTypes[KPrimaryEncPTInd], KPTFieldBits );
       
   196             // Calculate timestamp offset
       
   197             TUint offset = aCurTimeStamp - iRedTimeStamps[i];
       
   198             // Timestamp offset
       
   199             streamEncoder.Encode( offset, KTSOffsetBits );
       
   200             // Block length
       
   201             streamEncoder.Encode( iRedPayload->Data().Length(), KBlockLengthBits );
       
   202             
       
   203             DP_RED_ENCODE6( "ENC RED_HEADER NUM %d: F:%d PT:%d TIMESTAMP:%d, BL:%d", 
       
   204                 i, ETrue, iPayloadTypes[KPrimaryEncPTInd], iRedTimeStamps[i], 
       
   205                 iRedPayload->Data().Length() )
       
   206             }    
       
   207         }
       
   208     else
       
   209         {
       
   210         iRedHeaders->Des().SetLength( KFinalHeaderSize );            
       
   211         }            
       
   212 
       
   213     // Encode final header
       
   214     streamEncoder.Encode( 0, KFinalHeaderSize );
       
   215     streamEncoder.Encode( iPayloadTypes[KPrimaryEncPTInd], KPTFieldBits );
       
   216     
       
   217     DP_RED_ENCODE5( "ENC FINAL RED_HEADER %d: F:%d PT:%d #TIMESTAMP#:%d",
       
   218         iRedBlockCount, 0, iPayloadTypes[KPrimaryEncPTInd], aCurTimeStamp )
       
   219     
       
   220     // Delete oldest time stamp
       
   221     if ( iRedTimeStamps.Count() > iRedBlockCount )
       
   222         {
       
   223         iRedTimeStamps.Remove( 0 );
       
   224         }
       
   225     }
       
   226     
       
   227 
       
   228 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   229 
       
   230 
       
   231 //  End of File