rtp/srtpstack/src/srtpstreamin.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2004 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:    Represents an SRTP stream.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "msrtpstreamincontext.h"
       
    23 #include "srtpstreamin.h"
       
    24 #include "tsrtpstreaminstatebase.h"
       
    25 #include "tsrtpstreaminstateuninit.h"
       
    26 #include "tsrtpstreaminstatelatebind.h"
       
    27 #include "tsrtpstreaminstatenormal.h"
       
    28 #include "srtpsession.h"
       
    29 #include "srtpcryptohandler.h"
       
    30 #include "srtpcryptohandlersrtp.h"
       
    31 #include "srtpcryptohandlersrtcp.h"
       
    32 #include "srtputils.h"
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Constructor. Used when stream uses default cryptographic context
       
    36 // 
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession,
       
    40                                       TUint aSSRC)
       
    41     {
       
    42     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession, aSSRC);
       
    43     CleanupStack::PushL( self );    
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop( self );        
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // Constructor. Used when stream uses its own cryptographic context
       
    51 // 
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession,
       
    55                                       TUint aSSRC,
       
    56                                       CSRTPCryptoContext* aCon,
       
    57                                       MSRTPReKeyingObserver& aObs )
       
    58     {
       
    59     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession, aSSRC, 
       
    60                                                       aCon, aObs);
       
    61     CleanupStack::PushL( self );    
       
    62     TRAPD( err, self->ConstructL() );
       
    63     if ( err )
       
    64         {
       
    65         self->iStrmSpecificCrypto = EFalse; // Cannot take ownership
       
    66         User::Leave( err );
       
    67         }
       
    68     CleanupStack::Pop( self );        
       
    69     return self;
       
    70     }
       
    71 		
       
    72 // ---------------------------------------------------------------------------
       
    73 // Constructor. Used when stream uses default cryptographic context
       
    74 // and "late binding". No SSRC is provided by the user.
       
    75 // 
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession)
       
    79     {
       
    80     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession);
       
    81     CleanupStack::PushL( self );    
       
    82     self->ConstructL();
       
    83     CleanupStack::Pop( self );        
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // Constructor. Used when stream uses its own cryptographic context
       
    89 // and "late binding". No SSRC is provided by the user.
       
    90 // 
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession,
       
    94                                       CSRTPCryptoContext* aCon,
       
    95                                       MSRTPReKeyingObserver& aObs )
       
    96     {
       
    97     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession, 
       
    98                                                       aCon, aObs);
       
    99     CleanupStack::PushL( self );    
       
   100     TRAPD( err, self->ConstructL() );
       
   101     if ( err )
       
   102         {
       
   103         self->iStrmSpecificCrypto = EFalse; // Cannot take ownership
       
   104         User::Leave( err );
       
   105         }
       
   106     CleanupStack::Pop( self );        
       
   107     return self;
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Constructor. Used when stream uses its own cryptographic context
       
   112 // 
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession,
       
   116                                       TUint aSSRC,
       
   117                                       MSRTPReKeyingObserver& aObs )
       
   118     {
       
   119     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession, aSSRC,aObs);
       
   120     return self;
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // Constructor. Used when stream uses its own cryptographic context
       
   125 // and "late binding". No SSRC is provided by the user.
       
   126 // 
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 EXPORT_C CSRTPStreamIn* CSRTPStreamIn::NewL( CSRTPSession& aSession,
       
   130                                       MSRTPReKeyingObserver& aObs )
       
   131     {
       
   132     CSRTPStreamIn* self = new( ELeave )CSRTPStreamIn( aSession, aObs);
       
   133     return self;
       
   134     }    
       
   135 // -----------------------------------------------------------------------------
       
   136 // CSRTPStreamIn::ConstructL
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CSRTPStreamIn::ConstructL()
       
   140 	{
       
   141 	CreateCryptoHandlerSRTPL();
       
   142     CreateCryptoHandlerSRTCPL();
       
   143     InitializeStatesL();
       
   144 	}
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CSRTPStreamIn::SetCryptoInL()
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 EXPORT_C void CSRTPStreamIn::SetCryptoInL(CSRTPCryptoContext* aCon)
       
   151 	{
       
   152 	//checking if there is one crytpocontext existed
       
   153 	//construct only when stream has its own cryto handler
       
   154 	 if( !aCon)
       
   155 	    {
       
   156 	    User::Leave( KErrArgument );
       
   157 	    }
       
   158 	 if (!iRekey ) 
       
   159     	{
       
   160     	delete iHandlerRTP; iHandlerRTP=NULL;    
       
   161     	delete iHandlerRTCP;iHandlerRTCP=NULL;
       
   162     	delete iContext; iContext=NULL;
       
   163 		iContext= aCon; 
       
   164 		iStrmSpecificCrypto= ETrue;
       
   165     	iStates.Reset(); 
       
   166     	
       
   167       	//should create handler and initial state
       
   168       	TRAPD( err, 
       
   169       	    {
       
   170       	    CreateCryptoHandlerSRTPL();
       
   171     	    CreateCryptoHandlerSRTCPL();
       
   172     	    InitializeStatesL();
       
   173       	    } );
       
   174       	    
       
   175         if ( err )
       
   176             {
       
   177             iContext = NULL; // Cannot take ownership if leave occurs
       
   178             User::Leave( err );
       
   179             }
       
   180             
       
   181     	if (SSRC())
       
   182     		{
       
   183     		ChangeRTPState(MSRTPStreamInContext::ESRTPStreamInUninitialized);
       
   184     		ChangeRTCPState(MSRTPStreamInContext::ESRTPStreamInUninitialized);
       
   185     		}
       
   186     	else
       
   187     		{
       
   188     		ChangeRTPState(MSRTPStreamInContext::ESRTPStreamInLateBinding);
       
   189     		ChangeRTCPState(MSRTPStreamInContext::ESRTPStreamInLateBinding);
       
   190     		}	
       
   191 		}
       
   192 	}
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // CSRTPStreamIn::ReInitialInStates
       
   196 // 
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CSRTPStreamIn::UpdateCryptoAndStatesL()
       
   200 	{
       
   201 	if( !iStrmSpecificCrypto)
       
   202 	    {
       
   203 	    UpdateCryptoHandlerL();
       
   204 		iStates.Reset(); 
       
   205 		InitializeStatesL();
       
   206 		if (SSRC())
       
   207 			{
       
   208 			ChangeRTPState(MSRTPStreamInContext::ESRTPStreamInUninitialized);
       
   209 			ChangeRTCPState(MSRTPStreamInContext::ESRTPStreamInUninitialized);
       
   210 			}
       
   211 		else
       
   212 			{
       
   213 			ChangeRTPState(MSRTPStreamInContext::ESRTPStreamInLateBinding);
       
   214 			ChangeRTCPState(MSRTPStreamInContext::ESRTPStreamInLateBinding);
       
   215 			}	
       
   216 		}
       
   217 	}
       
   218 // ---------------------------------------------------------------------------
       
   219 // CSRTPStreamIn::CSRTPStreamIn
       
   220 // 
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 CSRTPStreamIn::CSRTPStreamIn(  CSRTPSession& aSession,
       
   224                                     TUint aSSRC)
       
   225     :   CSRTPStream(aSession, aSSRC, ETrue),
       
   226         iStates(MSRTPStreamInContext::EMaxStates),
       
   227         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInUninitialized),      
       
   228         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInUninitialized)
       
   229            
       
   230     {
       
   231     
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CSRTPStreamIn::~CSRTPStreamIn
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 CSRTPStreamIn::~CSRTPStreamIn( )
       
   239     {
       
   240     delete iHandlerRTP;    
       
   241     delete iHandlerRTCP;
       
   242     if (iStrmSpecificCrypto ) 
       
   243     	{
       
   244     	delete iContext; iContext=NULL;
       
   245     	}  
       
   246 	iStates.Reset();
       
   247 	iHandlerRTP=NULL;
       
   248 	iHandlerRTCP=NULL;  
       
   249     }
       
   250 // ---------------------------------------------------------------------------
       
   251 // CSRTPStreamIn::CSRTPStreamIn
       
   252 // 
       
   253 // ---------------------------------------------------------------------------
       
   254 //
       
   255 CSRTPStreamIn::CSRTPStreamIn( CSRTPSession& aSession,
       
   256                                       TUint aSSRC,
       
   257                                       CSRTPCryptoContext* aCon,
       
   258                                       MSRTPReKeyingObserver& aObs )
       
   259     :   CSRTPStream(aSession, aSSRC, aCon, aObs, ETrue),
       
   260         iStates(MSRTPStreamInContext::EMaxStates),
       
   261         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInUninitialized),      
       
   262         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInUninitialized)
       
   263             
       
   264     {
       
   265     
       
   266     }
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // CSRTPStreamIn::CSRTPStreamIn
       
   270 // 
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 CSRTPStreamIn::CSRTPStreamIn( CSRTPSession& aSession,
       
   274                                       TUint aSSRC,
       
   275                                       MSRTPReKeyingObserver& aObs )
       
   276     :   CSRTPStream(aSession, aSSRC, aObs, ETrue),
       
   277         iStates(MSRTPStreamInContext::EMaxStates),
       
   278         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInUninitialized),      
       
   279         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInUninitialized)
       
   280             
       
   281     {
       
   282     
       
   283     }
       
   284 		
       
   285 // ---------------------------------------------------------------------------
       
   286 // CSRTPStreamIn::CSRTPStreamIn
       
   287 // 
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 CSRTPStreamIn::CSRTPStreamIn(  CSRTPSession& aSession)
       
   291     :   CSRTPStream(aSession,  ETrue),
       
   292         iStates(MSRTPStreamInContext::EMaxStates),
       
   293         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInLateBinding),     
       
   294         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInLateBinding)
       
   295        
       
   296     {
       
   297     
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CSRTPStreamIn::CSRTPStreamIn
       
   302 // 
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 CSRTPStreamIn::CSRTPStreamIn( CSRTPSession& aSession,
       
   306                                       CSRTPCryptoContext* aCon,
       
   307                                       MSRTPReKeyingObserver& aObs )
       
   308     :   CSRTPStream(aSession, aCon, aObs,  ETrue),
       
   309         iStates(MSRTPStreamInContext::EMaxStates),
       
   310         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInLateBinding),     
       
   311         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInLateBinding)
       
   312        
       
   313     {
       
   314     
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // CSRTPStreamIn::CSRTPStreamIn
       
   319 // 
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 CSRTPStreamIn::CSRTPStreamIn( CSRTPSession& aSession,
       
   323                                       MSRTPReKeyingObserver& aObs )
       
   324     :   CSRTPStream(aSession, aObs,  ETrue),
       
   325         iStates(MSRTPStreamInContext::EMaxStates),
       
   326         iCurrentRTPState(MSRTPStreamInContext::ESRTPStreamInLateBinding),     
       
   327         iCurrentRTCPState(MSRTPStreamInContext::ESRTPStreamInLateBinding)
       
   328        
       
   329     {
       
   330     
       
   331     }
       
   332 		
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // HBufC8* CSRTPStreamIn::UnprotectSrtpL()
       
   336 // 
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 EXPORT_C HBufC8* CSRTPStreamIn::UnprotectSrtpL( const TDesC8& aPacket )
       
   340     {
       
   341  	SRTP_DEBUG_DETAIL( "CSRTPStreamIn::UnprotectSrtpL Entry" );
       
   342      
       
   343     if ( IsContextSet() )
       
   344     	{
       
   345     	SRTP_DEBUG_DETAIL( "CSRTPStreamIn::UnprotectSrtpL Exit" );
       
   346     	return (CurrentRTPState().DoUnprotectSrtpL(aPacket) );
       
   347     	}
       
   348     SRTP_DEBUG_DETAIL( "CSRTPStreamIn::UnprotectSrtpL Exit" );
       
   349     
       
   350     User::Leave (KErrArgument);
       
   351     return NULL;
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // HBufC8* CSRTPStreamIn::UnprotectSrtcpL()
       
   356 // 
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 EXPORT_C HBufC8* CSRTPStreamIn::UnprotectSrtcpL( const TDesC8& aPacket )
       
   360     {
       
   361     if (IsContextSet())
       
   362     	return (CurrentRTCPState().DoUnprotectSrtcpL(aPacket));
       
   363     User::Leave (KErrArgument);
       
   364     return NULL;
       
   365     }
       
   366 
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CSRTPStreamIn::InitializeStatesL
       
   370 // -----------------------------------------------------------------------------
       
   371 //	
       
   372 void CSRTPStreamIn::InitializeStatesL()
       
   373     {
       
   374 	iStates.AppendL(TSRTPStreamInStateUninit(*this, *iHandlerRTP, *iHandlerRTCP),
       
   375 					sizeof(TSRTPStreamInStateUninit));
       
   376 	iStates.AppendL(TSRTPStreamInStateLateBind(*this, *iHandlerRTP, *iHandlerRTCP),
       
   377 					sizeof(TSRTPStreamInStateLateBind));
       
   378 	iStates.AppendL(TSRTPStreamInStateNormal(*this, *iHandlerRTP, *iHandlerRTCP),
       
   379 					sizeof(TSRTPStreamInStateNormal));
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CSRTPStreamIn::ChangeRTPState
       
   384 // -----------------------------------------------------------------------------
       
   385 //      
       
   386 void CSRTPStreamIn::ChangeRTPState(MSRTPStreamInContext::TContextInState aNewState)
       
   387     {
       
   388     SRTP_DEBUG_TINT_VALUE( "CSRTPStreamIn::ChangeRTPState", aNewState);
       
   389     
       
   390     iCurrentRTPState = aNewState;
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CSRTPStreamIn::ChangeRTCPState
       
   395 // -----------------------------------------------------------------------------
       
   396 //      
       
   397 void CSRTPStreamIn::ChangeRTCPState(MSRTPStreamInContext::TContextInState aNewState)
       
   398     {
       
   399     iCurrentRTCPState = aNewState;
       
   400     }
       
   401 
       
   402 // ----------------------------------------------------------------------------
       
   403 // CSRTPStreamIn::CurrentRTPState
       
   404 // ----------------------------------------------------------------------------
       
   405 //
       
   406 TSRTPStreamInStateBase& CSRTPStreamIn::CurrentRTPState()
       
   407 	{
       
   408 	SRTP_DEBUG_TINT_VALUE( "CSRTPStreamIN::CurrentRTPState()", iCurrentRTPState);
       
   409     SRTP_DEBUG_TINT_VALUE( "CSRTPStreamIN::RTP State Count", iStates.Count() );    
       
   410     
       
   411 	return iStates.At(iCurrentRTPState);
       
   412 	}
       
   413 
       
   414 // ----------------------------------------------------------------------------
       
   415 // CSRTPStreamIn::CurrentRTCPState
       
   416 // ----------------------------------------------------------------------------
       
   417 //
       
   418 TSRTPStreamInStateBase& CSRTPStreamIn::CurrentRTCPState()
       
   419 	{
       
   420 	SRTP_DEBUG_TINT_VALUE( "CSRTPStreamIN::CurrentRTCPState()", iCurrentRTPState );
       
   421     SRTP_DEBUG_TINT_VALUE( "CSRTPStreamIN::RTCP State Count", iStates.Count() );    
       
   422      	
       
   423 	return iStates.At(iCurrentRTCPState);
       
   424 	}
       
   425