dvrengine/CommonRecordingEngine/DvrRtpUtils/src/CRtpTsConverter.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 provide TS conversion for an RTP-stream based on*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CRtpTsConverter.h"
       
    22 #include <ipvideo/CRtpUtil.h>
       
    23 #include "videoserviceutilsLogger.h"
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt KValidRtcpType( 200 );
       
    27 const TInt KTypeBytesPoint( 1 );
       
    28 const TInt KNtpSecBytesPoint( 8 );
       
    29 const TInt KNtpFracBytesPoint( 12 );
       
    30 const TInt KRtcpTsBytesPoint( 16 );
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CRtpTsConverter::NewL
       
    36 // Two-phased constructor.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 EXPORT_C CRtpTsConverter* CRtpTsConverter::NewL( const TUint aClockRate )
       
    40     {
       
    41     CRtpTsConverter* self = new( ELeave ) CRtpTsConverter( aClockRate );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47     
       
    48 // -----------------------------------------------------------------------------
       
    49 // CRtpTsConverter::CRtpTsConverter
       
    50 // C++ default constructor can NOT contain any code, that might leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CRtpTsConverter::CRtpTsConverter( const TUint aClockRate )
       
    54   : iClockRate( aClockRate ),
       
    55     iOffset( 0 ),
       
    56     iInitiated( EFalse )
       
    57     {
       
    58     // None
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CRtpTsConverter::ConstructL
       
    63 // Symbian 2nd phase constructor can leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CRtpTsConverter::ConstructL()
       
    67     {
       
    68     // None
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CRtpTsConverter::CRtpTsConverter
       
    73 // Destructor.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 EXPORT_C CRtpTsConverter::~CRtpTsConverter()
       
    77     {
       
    78     // None
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CRtpTsConverter::Init
       
    83 // 
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 EXPORT_C void CRtpTsConverter::Init( const TDesC8& aRtcpPkt )
       
    87     {
       
    88     // Verify packet type?
       
    89     if ( aRtcpPkt.Length() > ( KRtcpTsBytesPoint + KIntegerBytes ) && 
       
    90          aRtcpPkt[KTypeBytesPoint] == KValidRtcpType && iClockRate > 0 )
       
    91         {
       
    92         // Handle RTCP packet
       
    93         TUint ntp_sec( 0 );
       
    94         TUint ntp_frac( 0 );
       
    95         TUint rtcp_ts( 0 );
       
    96         TInt err( CRtpUtil::GetValue( 
       
    97              aRtcpPkt.Mid( KNtpSecBytesPoint, KIntegerBytes ), ntp_sec ) );
       
    98         if ( !err )
       
    99             {
       
   100             LOG1( "CRtpTsConverter::Init(), NTP Timestamp, MSW: %u", ntp_sec );
       
   101             err = CRtpUtil::GetValue( 
       
   102                   aRtcpPkt.Mid( KNtpFracBytesPoint, KIntegerBytes ), ntp_frac );
       
   103             }
       
   104         if ( !err )
       
   105             {
       
   106             LOG1( "CRtpTsConverter::Init(), NTP Timestamp, LSW: %u", ntp_frac );
       
   107             err = CRtpUtil::GetValue( 
       
   108                   aRtcpPkt.Mid( KRtcpTsBytesPoint, KIntegerBytes ), rtcp_ts );
       
   109             }
       
   110         
       
   111         if ( !err )
       
   112             {
       
   113             LOG1( "CRtpTsConverter::Init(), RTP Timestamp: %u", rtcp_ts );
       
   114             // Calculate the wallclock time when this RTCP packet is generated
       
   115             TUint wallClock( ( ntp_sec & 0x00FF ) * 1000 );
       
   116             wallClock += ( ( ( ntp_frac >> 16 ) & 0x0000FFFF ) * 1000 ) >> 16;
       
   117             
       
   118             // Compute the proper time offset
       
   119             iOffset = ComputeOffset( rtcp_ts, wallClock, iClockRate );
       
   120             }
       
   121         }
       
   122     }
       
   123 // -----------------------------------------------------------------------------
       
   124 // CRtpTsConverter::Init
       
   125 // Compute the proper time offset. No wallclok time available.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C void CRtpTsConverter::Init( const TUint& aTs )
       
   129     {
       
   130     iOffset = ComputeOffset( aTs, 0, iClockRate );
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CRtpTsConverter::Initiated
       
   135 // Returns: initiated status of converter.
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 EXPORT_C TBool CRtpTsConverter::Initiated()
       
   139     {
       
   140     return iInitiated;
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CRtpTsConverter::UnInitiate
       
   145 //
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 EXPORT_C void CRtpTsConverter::UnInitiate()
       
   149     {
       
   150     iInitiated = EFalse;
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CRtpTsConverter::ConvertTs
       
   155 // Returns: converted unsigned integer value of timestamp.
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C TUint CRtpTsConverter::ConvertTs( 
       
   159     const TUint aTimestamp,
       
   160     TBool aUseWallClock )
       
   161     {
       
   162     TUint ret( KMaxTUint );
       
   163 
       
   164     if ( iClockRate == KPipelineClockRate )
       
   165         {
       
   166         ret = aTimestamp - iOffset;
       
   167         }
       
   168     else
       
   169         {
       
   170         if ( iClockRate > 0 )
       
   171             {
       
   172             ret = aTimestamp - iOffset;
       
   173             
       
   174             if ( aUseWallClock )
       
   175                 {
       
   176                 TUint sec( ret / iClockRate );
       
   177                 TUint subSec( ret % iClockRate );
       
   178                 ret = ( ( sec * KPipelineClockRate ) + 
       
   179                         ( subSec * KPipelineClockRate ) / iClockRate );
       
   180                 }
       
   181             }
       
   182         }
       
   183 
       
   184     return ret;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CRtpTsConverter::ComputeOffset
       
   189 // Returns: offset of the RTP timestamp.
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TUint CRtpTsConverter::ComputeOffset(
       
   193     TUint aRtpTime,
       
   194     TUint aSeekTime,
       
   195     TInt aClockRate )
       
   196     {
       
   197     TUint ret( KMaxTUint );
       
   198 
       
   199     if ( aClockRate )
       
   200         {
       
   201         TUint rtpSec( aRtpTime / aClockRate );
       
   202         TUint rtpFracSecTU( aRtpTime % aClockRate ); // transport units
       
   203         TUint seekSec( aSeekTime / 1000 );
       
   204         TUint seekFracSecTU( ( ( aSeekTime % 1000) * aClockRate ) / 1000 );
       
   205 
       
   206         TUint offsetSec( rtpSec - seekSec );
       
   207         TUint offsetFracSecTU( rtpFracSecTU - seekFracSecTU );
       
   208 
       
   209         if ( rtpFracSecTU < seekFracSecTU )
       
   210             {
       
   211             offsetSec--;
       
   212             offsetFracSecTU = aClockRate + rtpFracSecTU - seekFracSecTU;
       
   213             }
       
   214 
       
   215         ret = ( offsetSec * aClockRate ) + offsetFracSecTU;
       
   216         iInitiated = ETrue;
       
   217         }
       
   218     
       
   219     return ret;
       
   220     }
       
   221 
       
   222 // End of File