rtp/srtpstack/src/srtpstream.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2005 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 "srtpstream.h"
       
    23 #include "srtpsession.h"
       
    24 #include "srtpcryptohandler.h"
       
    25 #include "srtpcryptohandlersrtp.h"
       
    26 #include "srtpcryptohandlersrtcp.h"
       
    27 #include "msrtprekeyingobserver.h"
       
    28 #include "srtpcryptocontext.h"
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt CSRTPStream::iStreamOffset = _FOFF( CSRTPStream, iStreamLink );
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CSRTPStream::SetROC()
       
    35 // 
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 EXPORT_C void CSRTPStream::SetROC(  TUint32 aROC )
       
    39     {
       
    40     iROC = aROC;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CSRTPStream::CSRTPStream
       
    45 // 
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
    49                                       TUint aSSRC,
       
    50                                       TBool aIsStreamInType)
       
    51     :   iSession(aSession),
       
    52         iContext(NULL),
       
    53         iObserver(NULL),
       
    54         iStrmSpecificCrypto(EFalse), 
       
    55         iHandlerRTP(NULL),       
       
    56         iHandlerRTCP(NULL),       
       
    57         iROC(0),
       
    58         iSSRC(aSSRC),
       
    59         iIsStreamInType(aIsStreamInType),
       
    60         iRekey(EFalse)
       
    61     {
       
    62     iSession.AddStreamToList(this);    
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CSRTPStream::CSRTPStream
       
    67 // 
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
    71                                       TUint aSSRC,
       
    72                                       CSRTPCryptoContext* aCon,
       
    73                                       MSRTPReKeyingObserver& aObs ,
       
    74                                       TBool aIsStreamInType)
       
    75       : iSession(aSession),
       
    76         iContext(aCon),
       
    77         iObserver(&aObs),
       
    78         iStrmSpecificCrypto(ETrue),
       
    79         iHandlerRTP(NULL),
       
    80         iHandlerRTCP(NULL),
       
    81         iROC(0),
       
    82         iSSRC(aSSRC),
       
    83         iIsStreamInType(aIsStreamInType),
       
    84         iRekey(EFalse)    
       
    85     {
       
    86     iSession.AddStreamToList(this);
       
    87     }
       
    88 	
       
    89 // ---------------------------------------------------------------------------
       
    90 // CSRTPStream::CSRTPStream
       
    91 // 
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
    95                                       TUint aSSRC,
       
    96                                       MSRTPReKeyingObserver& aObs ,
       
    97                                       TBool aIsStreamInType)
       
    98       : iSession(aSession),
       
    99         iContext(NULL),
       
   100         iObserver(&aObs),
       
   101         iStrmSpecificCrypto(EFalse),
       
   102         iHandlerRTP(NULL),
       
   103         iHandlerRTCP(NULL),
       
   104         iROC(0),
       
   105         iSSRC(aSSRC),
       
   106         iIsStreamInType(aIsStreamInType),
       
   107         iRekey(EFalse)      
       
   108     {
       
   109     iSession.AddStreamToList(this);
       
   110     }
       
   111 // ---------------------------------------------------------------------------
       
   112 // CSRTPStream::CSRTPStream
       
   113 // 
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
   117                           TBool aIsStreamInType)
       
   118     :   iSession(aSession),
       
   119         iContext(NULL),
       
   120         iObserver(NULL),
       
   121         iStrmSpecificCrypto(EFalse), 
       
   122         iHandlerRTP(NULL),       
       
   123         iHandlerRTCP(NULL),       
       
   124         iROC(0),
       
   125         iSSRC(0),                // set dummy 0 for SSRC in "late binding" mode
       
   126         iIsStreamInType(aIsStreamInType),
       
   127         iRekey(EFalse)
       
   128     {
       
   129     iSession.AddStreamToList(this);    
       
   130     }
       
   131 
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CSRTPStream::CSRTPStream
       
   135 // 
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
   139                                       CSRTPCryptoContext* aCon,
       
   140                                       MSRTPReKeyingObserver& aObs,
       
   141                                       TBool aIsStreamInType)
       
   142       : iSession(aSession),
       
   143         iContext(aCon),
       
   144         iObserver(&aObs),
       
   145         iStrmSpecificCrypto(ETrue),
       
   146         iHandlerRTP(NULL),       
       
   147         iHandlerRTCP(NULL),       
       
   148         iROC(0),
       
   149         iSSRC(0),          // set dummy 0 for SSRC in "late binding" mode
       
   150         iIsStreamInType(aIsStreamInType),
       
   151         iRekey(EFalse)
       
   152     {
       
   153     iSession.AddStreamToList(this);
       
   154     }
       
   155 	
       
   156 // ---------------------------------------------------------------------------
       
   157 // CSRTPStream::CSRTPStream
       
   158 // 
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 CSRTPStream::CSRTPStream( CSRTPSession& aSession,
       
   162                                       MSRTPReKeyingObserver& aObs,
       
   163                                       TBool aIsStreamInType)
       
   164       : iSession(aSession),
       
   165         iContext(NULL),
       
   166         iObserver(&aObs),
       
   167         iStrmSpecificCrypto(EFalse),
       
   168         iHandlerRTP(NULL),       
       
   169         iHandlerRTCP(NULL),       
       
   170         iROC(0),
       
   171         iSSRC(0),          // set dummy 0 for SSRC in "late binding" mode
       
   172         iIsStreamInType(aIsStreamInType),
       
   173         iRekey(EFalse)
       
   174     {
       
   175     iSession.AddStreamToList(this);
       
   176     }
       
   177 // ---------------------------------------------------------------------------
       
   178 // CSRTPStream::ROC
       
   179 // 
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 EXPORT_C TUint32 CSRTPStream::ROC() const
       
   183     {
       
   184     return iROC;
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CSRTPStream::SSRC
       
   189 // 
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 EXPORT_C TUint CSRTPStream::SSRC() const
       
   193     {
       
   194     return iSSRC;
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CSRTPStream::SSRC
       
   199 // 
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CSRTPStream::SetSSRC( const TUint aSSRC )
       
   203 	{
       
   204 	//should only be set when it is in latebinding state with In-Stream
       
   205 	iSSRC=aSSRC;	
       
   206 	}
       
   207 // ---------------------------------------------------------------------------
       
   208 // CSRTPStream::GetCryptoContext()
       
   209 // 
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 CSRTPCryptoContext& CSRTPStream::GetCryptoContext()
       
   213     {
       
   214     if (iStrmSpecificCrypto || iContext) 
       
   215     	{
       
   216     	return *iContext;		
       
   217     	}
       
   218     else
       
   219     	{
       
   220     	return iSession.GetCryptoContext();    
       
   221     	}	
       
   222     }
       
   223 
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // CSRTPStream::GetCryptoContext()
       
   227 // 
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 TBool CSRTPStream::IsContextSet()
       
   231     {
       
   232     if (iStrmSpecificCrypto && iHandlerRTP && iHandlerRTCP) 
       
   233     	{
       
   234     	return ETrue;		
       
   235     	}
       
   236     if (iSession.iSesssionCrypto && iHandlerRTP && iHandlerRTCP )
       
   237     	{
       
   238     	return ETrue;    
       
   239     	}	
       
   240     return EFalse;	
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CSRTPStream::~CSRTPStream
       
   245 // 
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 CSRTPStream::~CSRTPStream()
       
   249     {
       
   250 	iSession.RemoveStreamFromList(this);   
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // CSRTPStream::CreateCryptoHandlerSRTPL
       
   255 // 
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CSRTPStream::CreateCryptoHandlerSRTPL()
       
   259     {
       
   260     if (!iHandlerRTP)
       
   261         {
       
   262         iHandlerRTP = CSRTPCryptoHandlerSRTP::NewL(*this); 
       
   263         }    
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // CSRTPStream::CreateCryptoHandlerSRTCPL
       
   268 // 
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 void CSRTPStream::CreateCryptoHandlerSRTCPL()
       
   272     {
       
   273     if (!iHandlerRTCP)
       
   274         {
       
   275         iHandlerRTCP = CSRTPCryptoHandlerSRTCP::NewL(*this); 	
       
   276         }    
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CSRTPStream::ReKeyNeeded
       
   281 // 
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CSRTPStream::ReKeyNeeded()
       
   285 	{
       
   286 	if (iObserver)
       
   287 		{
       
   288 		iObserver->SRTPMasterKeyStaleEvent(*this);
       
   289 		}
       
   290 	else
       
   291 		{
       
   292 		iSession.ReKeyNeeded(*this,iStrmSpecificCrypto );
       
   293 		}	
       
   294 	}
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CSRTPStream::KeyExpired
       
   298 // 
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CSRTPStream::KeyExpired()
       
   302 	{
       
   303 	iRekey = ETrue;
       
   304 	iSession.iRekey = ETrue;
       
   305 	}
       
   306 // -----------------------------------------------------------------------------
       
   307 // CSRTPStream::UpdateCryptoAndStatesL()
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CSRTPStream::UpdateCryptoHandlerL()
       
   311 	{
       
   312 	//Note ROC is not updating
       
   313 	
       
   314     delete iHandlerRTP; iHandlerRTP=NULL;    
       
   315 	delete iHandlerRTCP;iHandlerRTCP=NULL;
       
   316 
       
   317   	//should create handler and initial state
       
   318 	CreateCryptoHandlerSRTPL();
       
   319 	CreateCryptoHandlerSRTCPL();
       
   320 	}	
       
   321 	
       
   322 // ---------------------------------------------------------------------------
       
   323 // CSRTPStream::CreateCryptoHandlerSRTCPL
       
   324 // 
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 EXPORT_C TBool CSRTPStream::StreamType()
       
   328 	{
       
   329 	return iIsStreamInType;
       
   330 	}