dvrengine/CommonRecordingEngine/DvrRtpUtils/src/CRtpPacket.cpp
branchRCL_3
changeset 23 13a33d82ad98
parent 0 822a42b6c3f1
equal deleted inserted replaced
22:826cea16efd9 23:13a33d82ad98
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:    Class to RTP packet parsing.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CRtpPacket.h"
       
    22 
       
    23 // CONSTANTS
       
    24 const TInt KRtpPacketVersion( 2 );
       
    25 const TInt KRtpMinHeaderLength( 12 );
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CRtpPacket::NewL
       
    31 // Two-phased constructor.
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C CRtpPacket* CRtpPacket::NewL()
       
    35     {
       
    36     CRtpPacket* self = CRtpPacket::NewLC();
       
    37     CleanupStack::Pop();
       
    38     return self;
       
    39     }
       
    40     
       
    41 // -----------------------------------------------------------------------------
       
    42 // CRtpPacket::NewLC
       
    43 // Two-phased constructor.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C CRtpPacket* CRtpPacket::NewLC()
       
    47     {
       
    48     CRtpPacket* self = new( ELeave ) CRtpPacket();
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     return self;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CRtpPacket::CRtpPacket
       
    56 // C++ default constructor can NOT contain any code, that might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CRtpPacket::CRtpPacket() : iPayload( NULL, 0 )
       
    60     {
       
    61     // None
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CRtpPacket::ConstructL
       
    66 // Symbian 2nd phase constructor can leave.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CRtpPacket::ConstructL()
       
    70     {
       
    71     // None
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CRtpPacket::CRtpPacket
       
    76 // Destructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CRtpPacket::~CRtpPacket()
       
    80     {
       
    81     if ( iRtpRecvHeader.iHeaderExtension )
       
    82         {
       
    83         // Remove iData
       
    84         delete[] ( iRtpRecvHeader.iHeaderExtension )->iData;
       
    85         delete iRtpRecvHeader.iHeaderExtension;
       
    86         }
       
    87     
       
    88     if ( iRtpRecvHeader.iCsrcList )
       
    89         {
       
    90         delete[] iRtpRecvHeader.iCsrcList;
       
    91         }
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CRtpPacket::ParseRtcp
       
    96 // 
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C TInt CRtpPacket::ParseRtp( const TDesC8& aPktBuf )
       
   100     {
       
   101     if ( aPktBuf.Length() < KRtpMinHeaderLength )
       
   102         {
       
   103         SetTimeStamp( 0 );
       
   104         iPayload.Set( NULL, 0 );
       
   105         return KErrUnderflow;
       
   106         }
       
   107     
       
   108     // 1st byte
       
   109     TInt byte( 0 );
       
   110     TUint8 version_flag( ( aPktBuf[byte] & 0xc0 ) >> 6 );
       
   111     // v=2 is mandatory 
       
   112     if ( version_flag != KRtpPacketVersion ) 
       
   113         {
       
   114         SetTimeStamp( 0 );
       
   115         iPayload.Set( NULL, 0 );
       
   116         return KErrNotSupported;
       
   117         } 
       
   118         
       
   119     iRtpRecvHeader.iPadding = ( aPktBuf[byte] >> 5 ) & 1;
       
   120     iRtpRecvHeader.iExtension = ( aPktBuf[byte] >> 4 ) & 1;
       
   121     iRtpRecvHeader.iCsrcCount = aPktBuf[byte++] & 0xf;
       
   122     
       
   123     // 2nd byte
       
   124     iRtpRecvHeader.iMarker = ( aPktBuf[byte] >> 7 ) & 1;
       
   125     iRtpRecvHeader.iPayloadType = aPktBuf[byte++] & 0x7f;
       
   126     
       
   127     // 3rd - 4th bytes
       
   128     TUint16 seq_no( aPktBuf[byte++] << 8 );
       
   129     seq_no |= aPktBuf[byte++];
       
   130     iRtpRecvHeader.iSeqNum = seq_no;
       
   131     
       
   132     // 5th - 8th bytes
       
   133     TUint32 timestamp( Read32Bits( aPktBuf, byte ) );
       
   134     iRtpRecvHeader.iTimestamp = timestamp;
       
   135 
       
   136     // 9th - 12th bytes
       
   137     TInt32 ssrc( Read32Bits( aPktBuf, byte ) );
       
   138     
       
   139     // 13th - bytes (optional)
       
   140     // total len = 4 * csrc_len (bytes)
       
   141     if ( iRtpRecvHeader.iCsrcCount > 0 )
       
   142         {
       
   143         iRtpRecvHeader.iCsrcList = new TUint32[iRtpRecvHeader.iCsrcCount];
       
   144         //TInt32* csrc = new TInt32[csrc_len];
       
   145         for ( TInt i( 0 ); i < iRtpRecvHeader.iCsrcCount; i++ )
       
   146             {
       
   147             iRtpRecvHeader.iCsrcList[i] = Read32Bits( aPktBuf, byte );
       
   148             }
       
   149         }
       
   150 
       
   151     // optional extension field
       
   152     if ( iRtpRecvHeader.iExtension == 1 )
       
   153         {
       
   154         /*
       
   155         iRtpRecvHeader.iHeaderExtension = new TRtpHeaderExtension();
       
   156         // 16 bits
       
   157         TInt16 op_code( aPktBuf[byte++] << 8 );
       
   158         op_code |= aPktBuf[byte++];
       
   159         iRtpRecvHeader.iHeaderExtension->iType = op_code;
       
   160         
       
   161         // 16 bits
       
   162         TInt16 op_code_data_length = aPktBuf[byte++] << 8;
       
   163         op_code_data_length |= aPktBuf[byte++];
       
   164         iRtpRecvHeader.iHeaderExtension->iLength = op_code_data_length;
       
   165         
       
   166         op_code_data_length * 4 bytes of extension data
       
   167         op_code_data = new TInt32[op_code_data_length];
       
   168         iRtpRecvHeader.iHeaderExtension->iData = new TInt32[op_code_data_length];
       
   169         for ( TInt i( 0 ); i < op_code_data_length; i++ )
       
   170             {
       
   171             iRtpRecvHeader.iHeaderExtension)->iData[i] = Read32Bits( aPktBuf, byte );
       
   172             }
       
   173         */
       
   174         }
       
   175     
       
   176     // The rest is payload data
       
   177     iPayload.Set( aPktBuf.Mid( byte ) );
       
   178     
       
   179     return KErrNone;
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CRtpPacket::SetTimeStamp
       
   184 // 
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 EXPORT_C void CRtpPacket::SetTimeStamp( const TUint32 aTs )
       
   188     {
       
   189     iRtpRecvHeader.iTimestamp = aTs;
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CRtpPacket::Read32Bits
       
   194 // 
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 TUint CRtpPacket::Read32Bits( const TPtrC8& aPktBuf, TInt& aByte )
       
   198     {
       
   199     TUint ret( ( ( TUint )( aPktBuf[aByte++] ) ) << 24 );
       
   200     ret |= ( ( TUint )( aPktBuf[aByte++] ) ) << 16;
       
   201     ret |= ( ( TUint )( aPktBuf[aByte++] ) ) << 8;
       
   202     ret |= ( ( TUint )( aPktBuf[aByte++] ) );
       
   203     
       
   204     return ret;
       
   205     }
       
   206 
       
   207 // End of File