rtp/rtpstack/src/rtpsession.cpp
changeset 0 307788aac0a8
child 16 43c4dec3cb1f
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2002-2003 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "rtpsession.h"
       
    23 
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt TStream::iOffset = _FOFF( TStream, iMagicKey );
       
    27 
       
    28 // Used to calculate the average size of an RTCP packet, this represents
       
    29 // the fraction by which the most recent RTCP packet influences the average.
       
    30 const TReal KRtcpSizeGain = ( 1.0 / 16.0 );
       
    31 const TReal KRtcpSizeGainRemainder = 1 - KRtcpSizeGain;
       
    32 const TInt  KRtpLimitNumberOfRecvStream=100;
       
    33 
       
    34 // ================= MEMBER FUNCTIONS =======================
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // C++ default constructor can NOT contain any code, that
       
    38 // might leave.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CRtpSession::CRtpSession( const TRtpId aSessionId,
       
    42                           const TUint32* aProfileRTPTimeRates,
       
    43                           const TBool aStandardRtp,
       
    44                           MRtpErrNotify& aErrNotify,
       
    45                           const CRtpSDES* aSdesInfo , 
       
    46                           MRtpAsignUniqueID& aAssignUniqueID) 
       
    47     :
       
    48     iStandardRtp( aStandardRtp ),
       
    49     iSessionId( aSessionId ),
       
    50     iLocalSdes( aSdesInfo ),
       
    51     iNewSdes( NULL ),
       
    52     // Send one SDES packet after every KDES_SEND_PACKET SR or RR
       
    53     iSendSdesCounter( KSDES_SEND_PACKET ),
       
    54     iRtpRecvBuf( NULL, 0 ),
       
    55     iRtcpRecvBuf( NULL, 0 ),
       
    56     iRtcpEnabled( EFalse ),
       
    57     iFirstRTCPSent( EFalse ),
       
    58     iFPortsInit( EFalse ),
       
    59     iNumOfTxStreams( 0 ),
       
    60     iNumOfRxStreams( 0 ),
       
    61     iProfileRTPTimeRates( aProfileRTPTimeRates ),
       
    62     iAverageRtcpSize( static_cast<TReal>( KAverageRtcpPacketLength ) ),
       
    63     iRtcpTimeInterval( KMinRtcpTimeInterval ),
       
    64     // default: the session has one participant
       
    65     iTotalParticipantsSession( 1 ),
       
    66     iRtpObserver( NULL ),
       
    67     iRtcpObserver( NULL ),
       
    68     iNonRTPDataObserver( NULL ),
       
    69     iErrNotify( aErrNotify ),
       
    70     iAssignUniqueID( aAssignUniqueID),
       
    71     iSessionStarted( EFalse ),
       
    72     iSSRCJumps( 0 ),
       
    73     iRtcpErrors( 0 ),
       
    74     iRtcpSendingSuspended( EFalse ),
       
    75     iSeed( 0 )
       
    76     {
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Symbian 2nd phase constructor can leave.
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void CRtpSession::ConstructL( const TCreateSessionParams& aParams,
       
    84                               TUint& aPort,
       
    85                               TBool aEnableRtcp,
       
    86                               const TRtcpParams* aRtcpParams,
       
    87                               RSocketServ& aSocketServ,
       
    88                               RConnection& aRConn,
       
    89                               const RLibrary& aLibrary,
       
    90                               MRtpErrNotify& aErrNotify )
       
    91     {
       
    92     RTP_DEBUG_DETAIL_DVALUE( "CRtpSession::ConstructL, socket size:", 
       
    93                              aParams.iSocketBufSize  );
       
    94     
       
    95     // Create at least packets of size KMaxRtpPacketSize. If client
       
    96     // wants to use bigger socket than that, also packets need to
       
    97     // have the same size.                         
       
    98     TInt packetSize = ( aParams.iSocketBufSize > KMaxRtpPacketSize ) ? 
       
    99             aParams.iSocketBufSize : KMaxRtpPacketSize;
       
   100             
       
   101     if ( iStandardRtp )
       
   102         {
       
   103         // allocate 1 packet to RTP sending 
       
   104         iPktSnd = CRtpPacket::NewL( packetSize, iProfileRTPTimeRates );
       
   105         iPktSnd->SetType( ERTP );
       
   106 
       
   107         // allocate 1 packet to RTP receiving
       
   108         iPktRcv = CRtpPacket::NewL( packetSize, iProfileRTPTimeRates );   
       
   109         iPktRcv->SetType( ERTP );
       
   110         TPtr8 aux1( iPktRcv->GetHBuf()->Des() );
       
   111         iRtpRecvBuf.Set( aux1 );
       
   112         }
       
   113     else
       
   114         {
       
   115         // Function at ordinal 1 creates new CRtpPacketExt
       
   116         TLibraryFunction entry = aLibrary.Lookup( 1 );
       
   117 
       
   118         iPktExtRcv = ( MRtpPacketExt * ) entry(); //NewL()
       
   119         iPktExtRcv->ConstructL( packetSize, iSessionId );
       
   120         TPtr8 aux1( iPktExtRcv->GetHBuf()->Des() );
       
   121         iRtpRecvBuf.Set( aux1 );
       
   122 
       
   123         iPktExtSnd = ( MRtpPacketExt * ) entry();  //NewL()
       
   124         iPktExtSnd->ConstructL( packetSize, iSessionId );
       
   125         }
       
   126     
       
   127     if ( aEnableRtcp )
       
   128         {
       
   129         iRtcpEnabled = ETrue;
       
   130         
       
   131         // allocate 1 packet for RTCP sending
       
   132         iPktRtcpSnd = CRtpPacket::NewL( KMaxRtcpPacketSize,
       
   133                                         iProfileRTPTimeRates );   
       
   134 
       
   135         // allocate 1 packet for RTCP receiving
       
   136         iPktRtcpRcv = CRtpPacket::NewL( KMaxRtcpPacketSize,
       
   137                                         iProfileRTPTimeRates );  
       
   138         TPtr8 aux2( iPktRtcpRcv->GetHBuf()->Des() );
       
   139         iRtcpRecvBuf.Set( aux2 );
       
   140 
       
   141         iSndRtcpTimer = CRtpTimer::NewL( CActive::EPriorityStandard, *this );
       
   142         
       
   143         InitialiseRtcp( aRtcpParams );
       
   144         }
       
   145 
       
   146     iCommNet = CRtpComm::NewL( aPort, aSocketServ, aRConn, aParams, aErrNotify,
       
   147                                aEnableRtcp );
       
   148     
       
   149     iCommNet->RegisterReceivedNotify( this );
       
   150 
       
   151     // instantiate RTP stream arrays
       
   152     iStreamTxArray = new ( ELeave ) CArrayFixFlat<TStream>( 2 );
       
   153     iStreamRxArray = new ( ELeave ) CArrayFixFlat<TStream>( 2 );
       
   154     iSdesArray = new ( ELeave ) CArrayPtrFlat<CRtpSDES>( 2 );
       
   155 
       
   156     // Assigned default SSRC     
       
   157     iDefaultSSRC = GenerateSSRC();
       
   158     iIsSrtp=EFalse;
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // Two-phased constructor.
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 CRtpSession* CRtpSession::NewL( const TCreateSessionParams& aParams,
       
   166                                 TUint& aPort,
       
   167                                 TBool aEnableRtcp,
       
   168                                 const TRtcpParams* aRtcpParams,
       
   169                                 RSocketServ& aSocketServ,
       
   170                                 RConnection& aRConn,
       
   171                                 const TRtpId aSessionId,
       
   172                                 const CRtpSDES* aSdes, // application's SDES
       
   173                                 const TUint32* aProfileRTPTimeRates,
       
   174                                 const TBool aStandardRtp,
       
   175                                 const RLibrary& aLibrary,
       
   176                                 MRtpErrNotify& aErrNotify, 
       
   177                                 MRtpAsignUniqueID& aAssignUniqueID )
       
   178     {
       
   179     CRtpSession* self = new ( ELeave ) CRtpSession( aSessionId, 
       
   180                                                     aProfileRTPTimeRates,
       
   181                                                     aStandardRtp, aErrNotify,
       
   182                                                     aSdes,aAssignUniqueID);
       
   183     CleanupStack::PushL( self );
       
   184     self->ConstructL( aParams, aPort, aEnableRtcp, aRtcpParams, aSocketServ, 
       
   185                       aRConn, aLibrary, aErrNotify );
       
   186     CleanupStack::Pop(); // self 
       
   187     return self;
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Destructor
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 CRtpSession::~CRtpSession()
       
   195     {
       
   196     RemoveAllStreams();
       
   197     delete iStreamTxArray;
       
   198     delete iStreamRxArray;
       
   199     if (iSdesArray)
       
   200 		{
       
   201 	    iSdesArray->ResetAndDestroy();
       
   202 	    }
       
   203     delete iSdesArray;
       
   204     delete iCommNet;
       
   205     if ( iRtcpEnabled )
       
   206         {
       
   207         delete iSndRtcpTimer;
       
   208         }
       
   209     if ( iNewSdes )
       
   210         {
       
   211         delete iNewSdes;
       
   212         }
       
   213     if ( iStandardRtp )
       
   214         {
       
   215         delete iPktSnd;
       
   216         delete iPktRcv;
       
   217         if ( iRtcpEnabled )
       
   218             {
       
   219             delete iPktRtcpSnd;
       
   220             delete iPktRtcpRcv;
       
   221             }
       
   222         }
       
   223     else
       
   224         {
       
   225         if (iPktExtSnd)
       
   226 			{
       
   227         	iPktExtSnd->Close(); // delete iPktExtSnd;
       
   228         	}
       
   229         if (iPktExtRcv)
       
   230         	{
       
   231         	iPktExtRcv->Close(); // delete iPktExtRcv;
       
   232         	}
       
   233         }
       
   234     iRxSSRCArray.Close();
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // TInt CRtpSession::SetRemoteAddress()
       
   239 // 
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 TInt CRtpSession::SetRemoteAddress( TInetAddr& aRemoteAddr )
       
   243     {
       
   244     if ( !iCommNet )
       
   245         {
       
   246         return KErrGeneral;
       
   247         }
       
   248     
       
   249     TInt error( KErrNone );
       
   250     
       
   251     if ( iSessionStarted )
       
   252         {
       
   253         ResetTxStreamStats();
       
   254         ResetRxStreamStats();
       
   255         }
       
   256         
       
   257     iRemoteAddr[ERTPPort].SetAddress( aRemoteAddr.Address() ); 
       
   258     iRemoteAddr[ERTPPort].SetPort( aRemoteAddr.Port() ); 
       
   259 
       
   260     error = iCommNet->SetToAddress( ERTPPort, aRemoteAddr );   
       
   261     if (aRemoteAddr.Family() == KAfInet6 )
       
   262     	{
       
   263     	iRemoteAddr[ERTPPort].SetAddress( aRemoteAddr.Ip6Address() ); 
       
   264     	}
       
   265     
       
   266     if ( error == KErrNone && iRtcpEnabled )
       
   267         {
       
   268         TInetAddr remoteAddr( aRemoteAddr );
       
   269         
       
   270          // Set RTCP port
       
   271         remoteAddr.SetPort( remoteAddr.Port() + 1 );
       
   272         
       
   273         iRemoteAddr[ERTCPPort].SetAddress( remoteAddr.Address() ); 
       
   274         iRemoteAddr[ERTCPPort].SetPort( remoteAddr.Port() ); 
       
   275         if (aRemoteAddr.Family() == KAfInet6 )
       
   276     		{
       
   277     		iRemoteAddr[ERTCPPort].SetAddress( aRemoteAddr.Ip6Address() ); 
       
   278     		}
       
   279         error = iCommNet->SetToAddress( ERTCPPort, remoteAddr );                                        
       
   280         }
       
   281     
       
   282     if ( error == KErrNone && !iSessionStarted )
       
   283         {
       
   284         iCommNet->RegisterReceivedNotify( this );
       
   285         }
       
   286         
       
   287     iRemoteAddrSet = ETrue;
       
   288     return error;
       
   289     } 
       
   290 
       
   291 
       
   292 // ---------------------------------------------------------------------------
       
   293 // TInt CRtpSession::SetRemoteRtcpAddress()
       
   294 // 
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 TInt CRtpSession::SetRemoteRtcpAddress( TInetAddr& aRemoteAddr )
       
   298     {
       
   299     TInt error( KErrNone );
       
   300     
       
   301     if ( !iRtcpEnabled )
       
   302         {
       
   303         return KErrNotSupported;
       
   304         }
       
   305     else
       
   306         {
       
   307 
       
   308         iRemoteAddr[ERTCPPort].SetAddress( aRemoteAddr.Address() ); 
       
   309         iRemoteAddr[ERTCPPort].SetPort( aRemoteAddr.Port() ); 
       
   310 		if (aRemoteAddr.Family() == KAfInet6 )
       
   311     		{
       
   312     		iRemoteAddr[ERTCPPort].SetAddress( aRemoteAddr.Ip6Address() ); 
       
   313     		}
       
   314         error = iCommNet->SetToAddress( ERTCPPort, aRemoteAddr );    
       
   315         }
       
   316         
       
   317     return error;
       
   318     }
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // TInt CRtpSession::CreateReceiveStreamL()
       
   322 // 
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 TInt CRtpSession::CreateReceiveStreamL( TRtpId aRcvStreamId,
       
   326                                         const TRtpPayloadType aPayloadType )
       
   327     {
       
   328     if ( iNumOfRxStreams == 0 )
       
   329         {
       
   330         // This is the first Rx stream
       
   331         iCommNet->ConstructReceiverL(iNonRTPDataObserver!=NULL);
       
   332         if (iRemoteAddrSet)
       
   333             {
       
   334             iCommNet->SetAcceptedFromAddress(iRemoteAddr[ERTPPort]);
       
   335             }
       
   336         }
       
   337 
       
   338     CRtpRecvStream* newReceiveStream =
       
   339         CRtpRecvStream::NewL( aPayloadType, iSessionId, aRcvStreamId,
       
   340                               &iRtpObserver, iRtcpObserver,
       
   341                               iProfileRTPTimeRates, this );
       
   342 
       
   343 
       
   344     // store new receive stream address in stream array 
       
   345     TStream arrayData( aRcvStreamId, ( TUint ) newReceiveStream );
       
   346 
       
   347     TInt err( AddStream( arrayData, ERxStream ) );
       
   348 
       
   349     if ( err == KErrNone )
       
   350         {
       
   351         iNumOfRxStreams++;
       
   352         }
       
   353     else
       
   354         {
       
   355         delete newReceiveStream;
       
   356         }
       
   357 
       
   358     return err;
       
   359     }
       
   360 
       
   361 // ---------------------------------------------------------------------------
       
   362 // TInt CRtpSession::GetUniqueSSRC()
       
   363 // 
       
   364 // ---------------------------------------------------------------------------
       
   365 //
       
   366 TRtpSSRC CRtpSession::GetUniqueSSRC()
       
   367     {
       
   368     TRtpSSRC ssrc( 0 );
       
   369     if ( iNumOfTxStreams != 0 )
       
   370         {
       
   371         ssrc = GenerateSSRC();
       
   372 
       
   373         // find if SSRC is already in use
       
   374         while ( FindStreamForSSRC( ssrc ) )
       
   375             {
       
   376             // if SSRC is already in use, create a new one    
       
   377             ssrc = GenerateSSRC();
       
   378             }
       
   379         }
       
   380     else // first TX stream, set transmit stream SSRC equal default SSRC
       
   381         {
       
   382         ssrc = iDefaultSSRC;
       
   383         }
       
   384     return ssrc;
       
   385     }
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // TInt CRtpSession::CreateTransmitStreamL()
       
   389 // 
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 TInt CRtpSession::CreateTransmitStreamL( TRtpId aTranStreamId, 
       
   393                                          const TRtpPayloadType aPayloadType,
       
   394                                          TRtpSSRC& aSSRC )
       
   395     {
       
   396     if ( iNumOfTxStreams == 0 )
       
   397         {
       
   398         // This is the first Tx stream
       
   399         iCommNet->ConstructSenderL(iRemoteAddr[ERTPPort], iRemoteAddr[ERTCPPort]);
       
   400         }
       
   401 
       
   402     aSSRC = GetUniqueSSRC();
       
   403 
       
   404     CRtpTranStream* newTranStream =
       
   405         CRtpTranStream::NewL( aPayloadType, iSessionId, aTranStreamId, aSSRC,
       
   406                               iRtcpObserver, iProfileRTPTimeRates );
       
   407 
       
   408     // store new transmit stream address in stream array 
       
   409     TStream arrayData( aTranStreamId, ( TUint ) newTranStream );
       
   410 
       
   411     TInt ret( AddStream( arrayData, ETxStream ) );
       
   412 
       
   413     if ( ret == KErrNone )
       
   414         {
       
   415         iNumOfTxStreams++;
       
   416         RedistributeBandwidth();
       
   417         }
       
   418     else
       
   419         {
       
   420         delete newTranStream;
       
   421         }
       
   422     
       
   423     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream with Session ID = ", iSessionId );
       
   424     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream ID = ", aTranStreamId );
       
   425     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream SSRC = ", aSSRC );
       
   426    
       
   427     return ret;
       
   428     }
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // TInt CRtpSession::CreateTransmitStreamExtL()
       
   432 // 
       
   433 // ---------------------------------------------------------------------------
       
   434 //
       
   435 TInt CRtpSession::CreateTransmitStreamExtL( TRtpId aTranStreamId, 
       
   436                                             const TRtpPayloadType aPayloadType, 
       
   437                                             const TRtpSSRC aSSRC )
       
   438     {
       
   439     if ( iNumOfTxStreams == 0 )
       
   440         {
       
   441         // This is the first Tx stream
       
   442         iCommNet->ConstructSenderL(iRemoteAddr[ERTPPort], iRemoteAddr[ERTCPPort]);
       
   443         }
       
   444 
       
   445     iDefaultSSRC = aSSRC;  // asigned this value to the default just in case
       
   446 
       
   447     CRtpTranStream* newTranStream =
       
   448         CRtpTranStream::NewL( aPayloadType, iSessionId, aTranStreamId, aSSRC,
       
   449                               iRtcpObserver, iProfileRTPTimeRates );
       
   450 
       
   451     // store new receive stream address in stream array 
       
   452     TStream arrayData( aTranStreamId, ( TUint ) newTranStream );
       
   453 
       
   454     TInt ret( AddStream( arrayData, ETxStream ) );
       
   455 
       
   456     if ( ret == KErrNone )
       
   457         {        
       
   458         iNumOfTxStreams++;
       
   459         RedistributeBandwidth();
       
   460         }
       
   461     else
       
   462         {
       
   463         delete newTranStream;
       
   464         }
       
   465     
       
   466     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream with Session ID = ", iSessionId );
       
   467     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream ID = ", aTranStreamId );
       
   468     RTP_DEBUG_DETAIL_DVALUE( "Created TX Stream SSRC = ", aSSRC );
       
   469     
       
   470     return ret;
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // CRtpSession::CloseStream()
       
   475 // 
       
   476 // ---------------------------------------------------------------------------
       
   477 //
       
   478 void CRtpSession::CloseStream( TRtpId aRcvStreamID )
       
   479     {
       
   480     TStreamType streamType;
       
   481 
       
   482     // remove stream
       
   483     TInt error( RemoveStream( aRcvStreamID, streamType ) );
       
   484 
       
   485     if ( error == KErrNone )
       
   486         {
       
   487         if ( streamType == ERxStream )
       
   488             {
       
   489             iNumOfRxStreams--;
       
   490             }
       
   491         else //ETxStream
       
   492             {
       
   493             iNumOfTxStreams--;
       
   494             RedistributeBandwidth();
       
   495             }
       
   496         }
       
   497     }
       
   498 
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 // CRtpSession::RedistributeBandwidths()
       
   502 // 
       
   503 // Calculate a new bandwidth for the TX streams.
       
   504 //
       
   505 // iFraction is the procent reserved for RTCP.
       
   506 //
       
   507 // Note, the bandwith is just the theoretical bandwidth which is used 
       
   508 // for RTCP and has nothing to do with the real bandwith. 
       
   509 // So if RTCP is not enabled, it will return (no calculation needed). 
       
   510 // 
       
   511 // ---------------------------------------------------------------------------
       
   512 //
       
   513 void CRtpSession::RedistributeBandwidth()
       
   514     {
       
   515     if ( iRtcpEnabled && ( iStreamTxArray != NULL ) )
       
   516        {
       
   517         
       
   518         CRtpTranStream* tempTxStream;
       
   519         
       
   520         TInt count = iStreamTxArray->Count();
       
   521         if ( count == 0 )
       
   522             {
       
   523             return;
       
   524             }
       
   525         TUint32 bandwidth = static_cast<TUint32>(
       
   526                                 ( iBandWidth * ( 1 - iFraction )  ) / count );
       
   527         
       
   528         for( int index = 0; index < count; index++ )
       
   529             {
       
   530             tempTxStream = reinterpret_cast<CRtpTranStream*>(
       
   531                            ( iStreamTxArray->At( index ).GetStreamAddress() )
       
   532                            );
       
   533             tempTxStream->SetBandwidth( bandwidth );
       
   534             }
       
   535        }
       
   536     }
       
   537     
       
   538 // ---------------------------------------------------------------------------
       
   539 // CRtpSession::ResetRxStreamStats()
       
   540 // 
       
   541 // ---------------------------------------------------------------------------
       
   542 //
       
   543 void CRtpSession::ResetRxStreamStats()
       
   544     {
       
   545     CRtpRecvStream* tempStream;
       
   546     
       
   547     TInt count = iStreamRxArray->Count();
       
   548     
       
   549     for(int index = 0; index < count; index++ )
       
   550         {
       
   551         tempStream = ( CRtpRecvStream* )
       
   552                      ( iStreamRxArray->At( index ).GetStreamAddress() );
       
   553         tempStream->ResetStreamStat();    
       
   554         }
       
   555     } 
       
   556 
       
   557 // ---------------------------------------------------------------------------
       
   558 // CRtpSession::ResetTxStreamStats()
       
   559 // 
       
   560 // ---------------------------------------------------------------------------
       
   561 //
       
   562 void CRtpSession::ResetTxStreamStats()
       
   563     {
       
   564     CRtpTranStream* tempStream;
       
   565     
       
   566     TInt count = iStreamTxArray->Count();    
       
   567     
       
   568     for ( TInt index( 0 ); index < count; index++ )
       
   569             {
       
   570             tempStream = ( CRtpTranStream* )
       
   571                          ( iStreamTxArray->At( index ).GetStreamAddress() );
       
   572             tempStream->ResetStreamStat();
       
   573             }
       
   574     }
       
   575 
       
   576 // ---------------------------------------------------------------------------
       
   577 // TInt CRtpSession::SendRtpPacket()
       
   578 // 
       
   579 // ---------------------------------------------------------------------------
       
   580 //
       
   581 TInt CRtpSession::SendRtpPacket( TRtpId aTranStreamId,
       
   582                                  const TRtpSendHeader& aHeaderInfo, 
       
   583                                  const TDesC8& aPayloadData )
       
   584     {
       
   585     if ( static_cast<TUint> (aPayloadData.Size()) > iCommNet->MaxSocketSize() )
       
   586     	{
       
   587     	return KErrOverflow;
       
   588     	}
       
   589 
       
   590     // find stream
       
   591     TUint streamAddress = 0;
       
   592     TInt ret( FindStream( aTranStreamId, streamAddress ) );
       
   593 
       
   594     if ( ret == KErrNone )
       
   595         {
       
   596         CRtpTranStream* tempStream = ( CRtpTranStream* ) streamAddress;
       
   597         tempStream->BuildRtpPacket( aHeaderInfo, aPayloadData, 0, EFalse,
       
   598                                     iPktSnd );
       
   599         ret = iCommNet->Send( ERTPPort, iPktSnd->Des() );
       
   600         }
       
   601     return ret;
       
   602     }
       
   603 
       
   604 // ---------------------------------------------------------------------------
       
   605 // TInt CRtpSession::SendRtpPacket()
       
   606 // 
       
   607 // ---------------------------------------------------------------------------
       
   608 //
       
   609 TInt CRtpSession::SendRtpPacket( TRtpId aTranStreamId,
       
   610                                  const TRtpSendHeader& aHeaderInfo,
       
   611                                  const TDesC8& aPayloadData,
       
   612                                  TRequestStatus& aStatus )
       
   613     {
       
   614     if ( static_cast<TUint>( aPayloadData.Size() ) > iCommNet->MaxSocketSize() )
       
   615     	{
       
   616     	return KErrOverflow;
       
   617     	}
       
   618 
       
   619     // find stream
       
   620     TUint streamAddress = 0;
       
   621     TInt ret( FindStream( aTranStreamId, streamAddress ) );
       
   622 
       
   623     if ( ret == KErrNone )
       
   624         {
       
   625         CRtpTranStream* tempStream =
       
   626             reinterpret_cast<CRtpTranStream*>( streamAddress );
       
   627         tempStream->BuildRtpPacket( aHeaderInfo, aPayloadData, 0, EFalse,
       
   628                                     iPktSnd );
       
   629         iCommNet->Send( ERTPPort, iPktSnd->Des(), aStatus );
       
   630         }
       
   631     return ret;
       
   632     }
       
   633 
       
   634 // ---------------------------------------------------------------------------
       
   635 // TInt CRtpSession::SendRtpPacket()
       
   636 // 
       
   637 // ---------------------------------------------------------------------------
       
   638 //
       
   639 TInt CRtpSession::SendRtpPacket( const TRtpSendHeader& aHeaderInfo,
       
   640                                  const TDesC8& aPayloadData )
       
   641     {
       
   642     if ( static_cast<TUint>( aPayloadData.Size() ) > iCommNet->MaxSocketSize() )
       
   643     	{
       
   644    		return KErrOverflow;
       
   645     	}
       
   646        
       
   647     TInt ret( iPktExtSnd->RtpPacketBuild( aHeaderInfo, aPayloadData ) );
       
   648 
       
   649     if( ret == KErrNone )
       
   650         {
       
   651         ret = iCommNet->Send( ERTPPort, iPktExtSnd->Des() );
       
   652         }
       
   653     
       
   654     return ret;
       
   655     }
       
   656 
       
   657 // ---------------------------------------------------------------------------
       
   658 // TInt CRtpSession::SendRtpPacket()
       
   659 // 
       
   660 // ---------------------------------------------------------------------------
       
   661 //
       
   662 TInt CRtpSession::SendRtpPacket( const TRtpSendHeader& aHeaderInfo, 
       
   663                                  const TDesC8& aPayloadData, 
       
   664                                  TRequestStatus& aStatus )
       
   665     {
       
   666     if ( static_cast<TUint>( aPayloadData.Size() ) > iCommNet->MaxSocketSize() )
       
   667     	{
       
   668     	return KErrOverflow;
       
   669     	}
       
   670     	
       
   671     TInt ret( iPktExtSnd->RtpPacketBuild( aHeaderInfo, aPayloadData ) );
       
   672     
       
   673     if( ret == KErrNone )
       
   674         {
       
   675         iCommNet->Send( ERTPPort, iPktExtSnd->Des(), aStatus );    
       
   676         }
       
   677     
       
   678 
       
   679     return ret;
       
   680     }
       
   681 
       
   682 // ---------------------------------------------------------------------------
       
   683 // void CRtpSession::SendData()
       
   684 // 
       
   685 // ---------------------------------------------------------------------------
       
   686 //
       
   687 void CRtpSession::SendData( TBool aUseRTPSocket,
       
   688                             const TDesC8& aData, 
       
   689                             TRequestStatus& aStatus )
       
   690     {        
       
   691     if (aUseRTPSocket)
       
   692         {
       
   693         iCommNet->Send( ERTPPort, aData, aStatus );    
       
   694         }
       
   695     else
       
   696         {
       
   697         iCommNet->Send( ERTCPPort, aData, aStatus );    
       
   698         }                
       
   699     }
       
   700 
       
   701 // ---------------------------------------------------------------------------
       
   702 // TInt CRtpSession::SendRtpPacket()
       
   703 // 
       
   704 // ---------------------------------------------------------------------------
       
   705 //
       
   706 TInt CRtpSession::SendRtpPacket( TRtpId aTranStreamId,
       
   707                                  TRtpSequence aSequenceNum,
       
   708                                  const TRtpSendHeader& aHeaderInfo,
       
   709                                  const TDesC8& aPayloadData,
       
   710                                  TRequestStatus& aStatus )
       
   711     {
       
   712     if ( static_cast<TUint>( aPayloadData.Size() ) > iCommNet->MaxSocketSize() )
       
   713     	{
       
   714     	return KErrOverflow;
       
   715     	}
       
   716 
       
   717     // find stream
       
   718     TUint streamAddress = 0;
       
   719     TInt ret( FindStream( aTranStreamId, streamAddress ) );
       
   720 
       
   721     if ( ret == KErrNone )
       
   722         {
       
   723         CRtpTranStream* tempStream =
       
   724             reinterpret_cast<CRtpTranStream*>( streamAddress );
       
   725         tempStream->BuildRtpPacket( aHeaderInfo, aPayloadData, aSequenceNum,
       
   726                                     ETrue, iPktSnd );
       
   727         iCommNet->Send( ERTPPort, iPktSnd->Des(), aStatus );
       
   728         }
       
   729     return ret;
       
   730     }
       
   731 
       
   732 // ---------------------------------------------------------------------------
       
   733 // CRtpSession::CancelSend()
       
   734 // 
       
   735 // ---------------------------------------------------------------------------
       
   736 //
       
   737 void CRtpSession::CancelSend()
       
   738     {
       
   739     iCommNet->CancelSend( ERTPPort );
       
   740     }
       
   741 
       
   742 // ---------------------------------------------------------------------------
       
   743 // TInt CRtpSession::SendRtcpByePacket()
       
   744 // 
       
   745 // ---------------------------------------------------------------------------
       
   746 //
       
   747 TInt CRtpSession::SendRtcpByePacketL( TRtpId aTranStreamId,
       
   748                                      const TDesC8& aReason )
       
   749     {
       
   750     if ( !iRtcpEnabled )
       
   751         {
       
   752         return KErrNotSupported;
       
   753         }
       
   754     if ( !iSessionStarted )
       
   755         {
       
   756         RTP_DEBUG_DETAIL("CRtpSession::SendRtcpByePacket, Session not started" );
       
   757         
       
   758         return KErrNotReady;
       
   759         }
       
   760     if ( aReason.Size() >= KMaxRtcpReason )
       
   761         {
       
   762         return KErrTooBig;
       
   763         }
       
   764 
       
   765     // find stream
       
   766     TUint streamAddress( 0 );
       
   767     TInt ret( FindStream( aTranStreamId, streamAddress ) );
       
   768 
       
   769     if ( ret == KErrNone )
       
   770         {
       
   771         CRtpTranStream* tempStream =
       
   772             reinterpret_cast<CRtpTranStream*>( streamAddress );
       
   773         ret = BuildRTCPReport( tempStream, NULL );
       
   774         if ( ret == KErrNone )
       
   775             {
       
   776             ret = tempStream->BuildRtcpBYEPacket( aReason, iPktRtcpSnd );
       
   777             }
       
   778         if ( ret == KErrNone )
       
   779             {
       
   780             if(IsSrtp())
       
   781             	{
       
   782             	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
   783             	}
       
   784             else	
       
   785             	{
       
   786             	SendRTCPReport();	
       
   787             	}            
       
   788             }
       
   789         }
       
   790     return ret;
       
   791     }
       
   792 
       
   793 // ---------------------------------------------------------------------------
       
   794 // TInt CRtpSession::SendRtcpAppPacketL()
       
   795 // 
       
   796 // ---------------------------------------------------------------------------
       
   797 //
       
   798 TInt CRtpSession::SendRtcpAppPacketL( TRtpId aTranStreamId,
       
   799                                      const TRtcpApp& aApp )
       
   800     {
       
   801     if ( !iRtcpEnabled )
       
   802         {
       
   803         return KErrNotSupported;
       
   804         }
       
   805     if ( !iSessionStarted )
       
   806         {
       
   807         RTP_DEBUG_DETAIL("CRtpSession::SendRtcpAppPacket, Session not started" );
       
   808       
       
   809         return KErrNotReady;
       
   810         }
       
   811     if ( aApp.iAppDataLen >= KMaxRtcpAppData )
       
   812         {
       
   813         return KErrTooBig;
       
   814         }
       
   815 
       
   816     // find stream
       
   817     TUint streamAddress = 0;
       
   818     TInt ret( FindStream( aTranStreamId, streamAddress ) );
       
   819 
       
   820     if ( ret == KErrNone )
       
   821         {
       
   822         CRtpTranStream* tempStream =
       
   823             reinterpret_cast<CRtpTranStream*>( streamAddress );
       
   824         ret = BuildRTCPReport( tempStream, NULL );
       
   825         if ( ret == KErrNone )
       
   826             {
       
   827             ret = tempStream->BuildRtcpAPPPacket( aApp, iPktRtcpSnd );
       
   828             }
       
   829         if ( ret == KErrNone )
       
   830             {
       
   831             if(IsSrtp())
       
   832             	{
       
   833             	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
   834             	}
       
   835             else	
       
   836             	{
       
   837             	SendRTCPReport();	
       
   838             	}            
       
   839             }
       
   840         }
       
   841     return ret;
       
   842     }
       
   843 
       
   844 // ---------------------------------------------------------------------------
       
   845 // TInt CRtpSession::SendRtcpSrPacketL()
       
   846 // 
       
   847 // ---------------------------------------------------------------------------
       
   848 //
       
   849 TInt CRtpSession::SendRtcpSrPacketL( TRtpId aTranStreamId )
       
   850     {
       
   851     if ( !iRtcpEnabled )
       
   852         {
       
   853         return KErrNotSupported;
       
   854         }
       
   855     if ( !iSessionStarted )
       
   856         {
       
   857        	RTP_DEBUG_DETAIL( "CRtpSession::SendRtcpSrPacket, Session not started" );
       
   858         return KErrNotReady;
       
   859         }
       
   860 
       
   861     TInt ret( KErrNone );
       
   862 
       
   863     // find stream
       
   864     TUint streamAddress = 0;
       
   865     ret = FindStream( aTranStreamId, streamAddress );
       
   866 
       
   867     if ( ret == KErrNone )
       
   868         {
       
   869         CRtpTranStream* tempStream =
       
   870             reinterpret_cast<CRtpTranStream*>( streamAddress );
       
   871         ret = BuildRTCPReport( tempStream, NULL );
       
   872         if ( ret == KErrNone )
       
   873             {
       
   874             if(IsSrtp())
       
   875             	{
       
   876             	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
   877             	}
       
   878             else	
       
   879             	{
       
   880             	SendRTCPReport();	
       
   881             	}            
       
   882             }
       
   883         }
       
   884     return ret;
       
   885     }
       
   886 
       
   887 // ---------------------------------------------------------------------------
       
   888 // TInt CRtpSession::SendRtcpRrPacketL()
       
   889 // 
       
   890 // ---------------------------------------------------------------------------
       
   891 //
       
   892 TInt CRtpSession::SendRtcpRrPacketL( TRtpId aRecvStreamId )
       
   893     {
       
   894     RTP_DEBUG_DETAIL( "CRtpSession::SendRtcpRrPacket entry" );
       
   895    
       
   896     if ( !iRtcpEnabled )
       
   897         {
       
   898         return KErrNotSupported;
       
   899         }
       
   900     if ( !iSessionStarted )
       
   901         {
       
   902         RTP_DEBUG_DETAIL( "Error: Session not started");
       
   903         
       
   904         return KErrNotReady;
       
   905         }
       
   906 
       
   907     TInt ret( KErrNone );
       
   908     CRtpRecvStream* tempStream( NULL );
       
   909     // find stream
       
   910     TUint streamAddress = 0;
       
   911     ret = FindStream( aRecvStreamId, streamAddress );
       
   912 
       
   913     if ( ret == KErrNone )
       
   914         {
       
   915         // Make sure the stream has received packets
       
   916         tempStream = 
       
   917             reinterpret_cast<CRtpRecvStream*>( streamAddress );
       
   918         if ( tempStream->FirstPkg() )
       
   919             {
       
   920            	RTP_DEBUG_DETAIL( "Error: Must send RTP packets before RTCP" );
       
   921             
       
   922             ret = KErrNotReady;
       
   923             }
       
   924         }
       
   925 
       
   926     if ( ret == KErrNone )
       
   927         {
       
   928         // Build the RR version of the RTCP header
       
   929         TRtpPacketStreamParam streamParam;
       
   930         TRtpPacketIOParam initParam;
       
   931 
       
   932         initParam.TRTCP_HEADER.pt = ERTCP_RR;
       
   933         initParam.TRTCP_HEADER.sourceCount = 1;
       
   934 
       
   935         // The length of one RR block in 32-bit units is 6
       
   936         // Added to this the source SSRC makes the total length 7
       
   937         initParam.TRTCP_HEADER.length = 7;
       
   938 
       
   939         streamParam.TRTCP_HEADER.SSRC = iDefaultSSRC;
       
   940 
       
   941         iPktRtcpSnd->SetType( ERTCP_HEADER );
       
   942         ret = iPktRtcpSnd->RtpPacketBuild( &streamParam, &initParam );
       
   943         }
       
   944 
       
   945     if ( ret == KErrNone )
       
   946         {
       
   947         // Build the RR and SDES sections, then send the packet
       
   948         tempStream->RtpStreamCreateRtcpReportSection( iPktRtcpSnd );
       
   949         BuildSdesSection( NULL );
       
   950         if(IsSrtp())
       
   951            	{
       
   952            	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
   953            	}
       
   954         else	
       
   955         	{
       
   956         	SendRTCPReport();	
       
   957         	}            
       
   958         }
       
   959     return ret;
       
   960     }
       
   961 
       
   962 // ---------------------------------------------------------------------------
       
   963 // RSocket* CRtpSession::GetRtpSocket()
       
   964 // 
       
   965 // ---------------------------------------------------------------------------
       
   966 //
       
   967 RSocket* CRtpSession::GetRtpSocket()
       
   968     {
       
   969     if ( iCommNet )
       
   970         return iCommNet->GetSocket( ERTPPort );
       
   971     else
       
   972         return NULL;
       
   973     }
       
   974 
       
   975 // ---------------------------------------------------------------------------
       
   976 // RSocket* CRtpSession::GetRtcpSocket()
       
   977 // 
       
   978 // ---------------------------------------------------------------------------
       
   979 //
       
   980 RSocket* CRtpSession::GetRtcpSocket()
       
   981     {
       
   982     if ( iCommNet && iRtcpEnabled )
       
   983         return iCommNet->GetSocket( ERTCPPort );
       
   984     else
       
   985         return NULL;
       
   986     }
       
   987 
       
   988 // ---------------------------------------------------------------------------
       
   989 // TInt CRtpSession::GetStreamStatistics()
       
   990 // 
       
   991 // ---------------------------------------------------------------------------
       
   992 //
       
   993 TInt CRtpSession::GetStreamStatistics( TRtpId aStreamId, TRtpPeerStat& aStat )
       
   994     {
       
   995     TInt ret;
       
   996 
       
   997     // find stream
       
   998     TUint streamAddress = 0;
       
   999     TStreamType aStreamType;
       
  1000 
       
  1001     ret = FindStream( aStreamId, streamAddress, aStreamType );
       
  1002 
       
  1003     if ( ret != KErrNone )
       
  1004         {
       
  1005         return ret;
       
  1006         }
       
  1007 
       
  1008     if ( aStreamType == ERxStream )
       
  1009         {
       
  1010         CRtpRecvStream* tempStream = ( CRtpRecvStream* ) streamAddress;
       
  1011         ret = tempStream->GetStreamStat( aStat );
       
  1012         }
       
  1013     else //ETxStream
       
  1014         {
       
  1015         CRtpTranStream* tempStream = ( CRtpTranStream* ) streamAddress;
       
  1016         ret = tempStream->GetStreamStat( aStat );
       
  1017         }
       
  1018 
       
  1019     return ret;
       
  1020     }
       
  1021 
       
  1022 // ---------------------------------------------------------------------------
       
  1023 // TInt CRtpSession::InitialiseRtcp()
       
  1024 // 
       
  1025 // ---------------------------------------------------------------------------
       
  1026 //
       
  1027 TInt CRtpSession::InitialiseRtcp( const TRtcpParams* aParams )
       
  1028     {
       
  1029     if ( !iRtcpEnabled )
       
  1030         {
       
  1031         return KErrGeneral;
       
  1032         }
       
  1033 
       
  1034     if ( aParams == NULL )
       
  1035         {
       
  1036         TRtcpParams params;
       
  1037         params.iSessionBWidth = KRtpDefaultBandWidth;
       
  1038         params.iRtcpFraction = KRtpDefaultRtcpFrac;
       
  1039         SetRtcpParameters( params );
       
  1040         }
       
  1041     else
       
  1042         {
       
  1043         SetRtcpParameters( *aParams );
       
  1044         }
       
  1045 
       
  1046     // 1000 - milliseconds per second
       
  1047     // 8 - bits in one byte
       
  1048     iRtcpTimeInterval = ( TUint ) ( 1000 * KAverageRtcpPacketLength * 8 /
       
  1049                                   ( iBandWidth * iFraction ) ); 
       
  1050 
       
  1051     // The first RTCP interval should be half the usual amount. Hence, we
       
  1052     // divide by two here.
       
  1053      if ( iRtcpTimeInterval < KMinRtcpTimeInterval / 2 )
       
  1054         {
       
  1055         iRtcpTimeInterval = KMinRtcpTimeInterval / 2;
       
  1056         }
       
  1057 
       
  1058     // Randomise
       
  1059     TReal rtcpTimeInterval64 = iRtcpTimeInterval * ( Random64() + 0.5 );
       
  1060     iRtcpTimeInterval = static_cast<TUint>( rtcpTimeInterval64 );
       
  1061     
       
  1062     RTCP_DEBUG_DETAIL_DVALUE( "CRtpSession::InitialiseRtcp - RTCP Interval(ms) = ", iRtcpTimeInterval );
       
  1063     return KErrNone;
       
  1064     }
       
  1065 
       
  1066 // ---------------------------------------------------------------------------
       
  1067 // TInt CRtpSession::SetRtcpParameters()
       
  1068 // 
       
  1069 // ---------------------------------------------------------------------------
       
  1070 //
       
  1071 TInt CRtpSession::SetRtcpParameters( const TRtcpParams& aRtcpParams )
       
  1072     {
       
  1073     if ( !iRtcpEnabled )
       
  1074         {
       
  1075         return KErrGeneral;
       
  1076         }
       
  1077 
       
  1078     if ( aRtcpParams.iSessionBWidth < KRtpMinimumBandWidth )
       
  1079         {
       
  1080         iBandWidth = KRtpDefaultBandWidth;
       
  1081         }
       
  1082     else
       
  1083         {
       
  1084         iBandWidth = aRtcpParams.iSessionBWidth;
       
  1085         }
       
  1086     if ( aRtcpParams.iRtcpFraction == 0 || aRtcpParams.iRtcpFraction > 1 )
       
  1087         {
       
  1088         iFraction = KRtpDefaultRtcpFrac;
       
  1089         }
       
  1090     else
       
  1091         {
       
  1092         iFraction = aRtcpParams.iRtcpFraction;
       
  1093         }
       
  1094     
       
  1095     RedistributeBandwidth();
       
  1096     
       
  1097     return KErrNone;
       
  1098     }
       
  1099 
       
  1100 // ---------------------------------------------------------------------------
       
  1101 // TInt CRtpSession::RegisterRtpObserver()
       
  1102 // 
       
  1103 // ---------------------------------------------------------------------------
       
  1104 //
       
  1105 TInt CRtpSession::RegisterRtpObserver( MRtpObserver& aObserver )
       
  1106     {
       
  1107     if ( iPktExtRcv )
       
  1108         iPktExtRcv->RegisterRtpObserver( aObserver );
       
  1109 
       
  1110     iRtpObserver = &aObserver;        
       
  1111        
       
  1112     // now update observers in receiver streams
       
  1113     CRtpRecvStream* tempStream;
       
  1114     TInt count = iStreamRxArray->Count();
       
  1115     for(int index = 0; index < count; index++ )
       
  1116         {
       
  1117         tempStream = ( CRtpRecvStream* )
       
  1118                      ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  1119         tempStream->RegisterRtpObserver(&aObserver);    
       
  1120         }
       
  1121     
       
  1122     return KErrNone;
       
  1123     }
       
  1124 
       
  1125 // ---------------------------------------------------------------------------
       
  1126 // CRtpSession::UnregisterRtpObserver()
       
  1127 // 
       
  1128 // ---------------------------------------------------------------------------
       
  1129 //
       
  1130 void CRtpSession::UnregisterRtpObserver()
       
  1131     {
       
  1132     if ( iPktExtRcv )
       
  1133         iPktExtRcv->UnregisterRtpObserver();
       
  1134 
       
  1135     iRtpObserver = NULL;
       
  1136     
       
  1137     // now unregister receiver stream RTP observer
       
  1138     CRtpRecvStream* tempStream;    
       
  1139     TInt count = iStreamRxArray->Count();
       
  1140     
       
  1141     for(int index = 0; index < count; index++ )
       
  1142         {
       
  1143         tempStream = ( CRtpRecvStream* )
       
  1144                      ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  1145         tempStream->UnRegisterRtpObserver();    
       
  1146         }                   
       
  1147     }
       
  1148 
       
  1149 // ---------------------------------------------------------------------------
       
  1150 // TInt CRtpSession::RegisterRtcpObserver()
       
  1151 // 
       
  1152 // ---------------------------------------------------------------------------
       
  1153 //
       
  1154 TInt CRtpSession::RegisterRtcpObserver( MRtcpObserver& aObserver )
       
  1155     {
       
  1156     if ( !iRtcpEnabled )
       
  1157         {
       
  1158         return KErrGeneral;
       
  1159         }
       
  1160         
       
  1161     iRtcpObserver = &aObserver;
       
  1162     
       
  1163     // now register receiver stream RTCP observers
       
  1164     CRtpRecvStream* tempRecvStream;
       
  1165     TInt count = iStreamRxArray->Count();    
       
  1166     for(int index = 0; index < count; index++ )
       
  1167         {
       
  1168         tempRecvStream = ( CRtpRecvStream* )
       
  1169                      ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  1170         tempRecvStream->RegisterRtcpObserver(aObserver);    
       
  1171         }               
       
  1172 
       
  1173     // now register transmit stream RTCP observers
       
  1174     CRtpTranStream* tempTxStream;
       
  1175     count = iStreamTxArray->Count();    
       
  1176     for(int index = 0; index < count; index++ )
       
  1177         {
       
  1178         tempTxStream = ( CRtpTranStream* )
       
  1179                      ( iStreamTxArray->At( index ).GetStreamAddress() );
       
  1180         tempTxStream->RegisterRtcpObserver(aObserver);    
       
  1181         }               
       
  1182     
       
  1183     return KErrNone;
       
  1184     }
       
  1185 
       
  1186 // ---------------------------------------------------------------------------
       
  1187 // CRtpSession::UnregisterRtcpObserver()
       
  1188 // 
       
  1189 // ---------------------------------------------------------------------------
       
  1190 //
       
  1191 void CRtpSession::UnregisterRtcpObserver()
       
  1192     {
       
  1193     iRtcpObserver = NULL;
       
  1194 
       
  1195     // now unregister receiver stream RTCP observers
       
  1196     CRtpRecvStream* tempRecvStream;   
       
  1197     TInt count = iStreamRxArray->Count();
       
  1198     
       
  1199     for(int index = 0; index < count; index++ )
       
  1200         {
       
  1201         tempRecvStream = ( CRtpRecvStream* )
       
  1202                      ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  1203         tempRecvStream->UnRegisterRtcpObserver();    
       
  1204         }               
       
  1205 
       
  1206     // now unregister transmit stream RTCP observers
       
  1207     CRtpTranStream* tempTxStream;    
       
  1208     count = iStreamTxArray->Count();
       
  1209     
       
  1210     for(int index = 0; index < count; index++ )
       
  1211         {
       
  1212         tempTxStream = ( CRtpTranStream* )
       
  1213                      ( iStreamTxArray->At( index ).GetStreamAddress() );
       
  1214         tempTxStream->UnRegisterRtcpObserver();    
       
  1215         }               
       
  1216 
       
  1217     }
       
  1218 
       
  1219 // ---------------------------------------------------------------------------
       
  1220 // TInt CRtpSession::SetNonRTPDataObserver()
       
  1221 // 
       
  1222 // ---------------------------------------------------------------------------
       
  1223 //
       
  1224 TInt CRtpSession::SetNonRTPDataObserver( MNonRTPDataObserver* aNonRTPDataObserver )
       
  1225     {
       
  1226     iNonRTPDataObserver = aNonRTPDataObserver;    
       
  1227     if (iNonRTPDataObserver)
       
  1228         {
       
  1229         iCommNet->SetNonRtpObserverFlag(ETrue);        
       
  1230         }
       
  1231     else
       
  1232         {
       
  1233         iCommNet->SetNonRtpObserverFlag(EFalse);                
       
  1234         }
       
  1235     return KErrNone;
       
  1236     }
       
  1237 
       
  1238 
       
  1239 
       
  1240 // ---------------------------------------------------------------------------
       
  1241 // CRtpSession::StartSession()
       
  1242 // 
       
  1243 // ---------------------------------------------------------------------------
       
  1244 //
       
  1245 TInt CRtpSession::StartSession()
       
  1246     {
       
  1247     TInt result( KErrNone );
       
  1248     if ( iNumOfRxStreams == 0 && iNumOfTxStreams == 0 )
       
  1249         {
       
  1250         return KErrNotReady;
       
  1251         }
       
  1252     if ( iNumOfRxStreams != 0 )
       
  1253         {
       
  1254         // Start receiving RTP streams
       
  1255         result = IssueRtpRecv();
       
  1256         // IssueRtpRecv() returns KErrNone the first time, KErrInUse if it
       
  1257         // has been called before and KErrNotReady if the receive resources
       
  1258         // are not constructed.
       
  1259         if ( iRtcpEnabled && ( result == KErrNone ) )
       
  1260             {
       
  1261             result = IssueRtcpRecv();
       
  1262             }
       
  1263         // If the sockets are already active listening for packets, this is OK
       
  1264         if ( result == KErrInUse )
       
  1265             {
       
  1266             result = KErrNone;
       
  1267             }
       
  1268         }
       
  1269     if ( iRtcpEnabled && iNumOfTxStreams != 0 && !iSndRtcpTimer->IsActive() )
       
  1270         {
       
  1271         // Start sending RTCP packets
       
  1272         iSndRtcpTimer->After( iRtcpTimeInterval * KMicrosecondPerMillSecond );
       
  1273         }
       
  1274     if ( result == KErrNone )
       
  1275         {
       
  1276         iSessionStarted = ETrue;
       
  1277         }
       
  1278     return result;
       
  1279     }
       
  1280 
       
  1281 // ---------------------------------------------------------------------------
       
  1282 // CRtpSession::IssueRtpRecv()
       
  1283 // 
       
  1284 // ---------------------------------------------------------------------------
       
  1285 //
       
  1286 TInt CRtpSession::IssueRtpRecv()
       
  1287     {
       
  1288     return iCommNet->Receive( ERTPPort, iRtpRecvBuf );
       
  1289     }
       
  1290 
       
  1291 // ---------------------------------------------------------------------------
       
  1292 // CRtpSession::IssueRtcpRecv()
       
  1293 // 
       
  1294 // ---------------------------------------------------------------------------
       
  1295 //
       
  1296 TInt CRtpSession::IssueRtcpRecv()
       
  1297     {
       
  1298     if ( !iRtcpEnabled )
       
  1299         {
       
  1300         return KErrGeneral;
       
  1301         }
       
  1302     return iCommNet->Receive( ERTCPPort, iRtcpRecvBuf );
       
  1303     }
       
  1304 
       
  1305 // ---------------------------------------------------------------------------
       
  1306 // CRtpSession::DoBuildSendRTCPReportIfNotSentL()
       
  1307 // 
       
  1308 // ---------------------------------------------------------------------------
       
  1309 //
       
  1310 TBool CRtpSession::DoBuildSendRTCPReportIfNotSentL()
       
  1311     {
       
  1312     if ( !iRtcpEnabled )
       
  1313         {
       
  1314         return EFalse;
       
  1315         }
       
  1316     TInt index( 0 );
       
  1317     TBool reportSent( EFalse );
       
  1318     TInt totalTxStream( iStreamTxArray->Count() );
       
  1319     
       
  1320     for ( index = 0; index < totalTxStream; index++ )
       
  1321         {
       
  1322         CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>
       
  1323             ( iStreamTxArray->At( index ).GetStreamAddress() );
       
  1324         if ( !tempStream->SentRtcpReport() && !reportSent )
       
  1325             {
       
  1326             // if no report has been sent, send it now
       
  1327             // totalTxStream - index - 1 makes sure method knows when this is
       
  1328             // the last RTCP report to be sent
       
  1329             if ( BuildRTCPReport( tempStream, totalTxStream - index - 1 )
       
  1330                  == KErrNone )
       
  1331                 {
       
  1332                 RTP_DEBUG_DETAIL_DVALUE( "BuildSendRTCPReport: Sending SR RTCP REPORT TX stream ID = ",
       
  1333                     iStreamTxArray->At( index ).GetMagicKey() );
       
  1334                 
       
  1335                 if(IsSrtp())
       
  1336 	            	{
       
  1337 	            	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
  1338 	            	}
       
  1339 	            else	
       
  1340 	            	{
       
  1341 	            	SendRTCPReport();	
       
  1342 	            	}            
       
  1343                 }
       
  1344             else
       
  1345                 {
       
  1346                 RTP_DEBUG_DETAIL_DVALUE( "BuildSendRTCPReport: No report sent for TX stream ID = ",
       
  1347                     iStreamTxArray->At( index ).GetMagicKey() );
       
  1348                 }
       
  1349     
       
  1350             tempStream->SetRtcpReportFlag();
       
  1351             reportSent = ETrue;
       
  1352             
       
  1353             }
       
  1354         }
       
  1355     return reportSent;
       
  1356     }
       
  1357 
       
  1358 // ---------------------------------------------------------------------------
       
  1359 // CRtpSession::DoBuildSendRTCPReportL()
       
  1360 // 
       
  1361 // ---------------------------------------------------------------------------
       
  1362 //
       
  1363 void CRtpSession::DoBuildSendRTCPReportL()
       
  1364     {
       
  1365     if ( !iRtcpEnabled )
       
  1366         {
       
  1367         return;
       
  1368         }
       
  1369 
       
  1370     TInt index( 0 );
       
  1371     TInt totalTxStream( iStreamTxArray->Count() );
       
  1372 
       
  1373     for ( index = 0; index < totalTxStream; index++ )
       
  1374         {
       
  1375         CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>(
       
  1376             ( iStreamTxArray->At( index ).GetStreamAddress() ) );
       
  1377         tempStream->ResetRtcpReportFlag();
       
  1378 
       
  1379         // totalTxStream - index - 1 makes sure method knows when this is
       
  1380         // the last RTCP report to be send
       
  1381         if ( BuildRTCPReport( tempStream, totalTxStream - index - 1 )
       
  1382              == KErrNone )
       
  1383             {
       
  1384             
       
  1385             RTP_DEBUG_DETAIL_DVALUE( "BuildSendRTCPReport: Sending (AFTER) SR RTCP REPORT TX stream ID = ",
       
  1386                 iStreamTxArray->At( index ).GetMagicKey() );
       
  1387             
       
  1388             if(IsSrtp())
       
  1389             	{
       
  1390             	SendSRTCPReportL(tempStream->GetLocalSSRC());
       
  1391             	}
       
  1392             else	
       
  1393             	{
       
  1394             	SendRTCPReport();	
       
  1395             	}            
       
  1396             }
       
  1397         else
       
  1398             {
       
  1399             RTP_DEBUG_DETAIL_DVALUE( "BuildSendRTCPReport: No report sent for TX stream ID = ",
       
  1400                 iStreamTxArray->At( index ).GetMagicKey() );
       
  1401             }
       
  1402         tempStream->SetRtcpReportFlag();
       
  1403         }
       
  1404     }
       
  1405 
       
  1406 // ---------------------------------------------------------------------------
       
  1407 // CRtpSession::BuildSendRTCPReportL()
       
  1408 // 
       
  1409 // ---------------------------------------------------------------------------
       
  1410 //
       
  1411 void CRtpSession::BuildSendRTCPReportL()
       
  1412     {
       
  1413     if ( !iRtcpEnabled )
       
  1414         {
       
  1415         return;
       
  1416         }
       
  1417 
       
  1418     TInt totalRxStream( iStreamRxArray->Count() );
       
  1419     TInt totalTxStream( iStreamTxArray->Count() );
       
  1420     TBool reportSent( ETrue );
       
  1421 
       
  1422     if ( totalTxStream > 0 )
       
  1423         {
       
  1424         reportSent = DoBuildSendRTCPReportIfNotSentL();
       
  1425         // if no reports were sent for any of the TX streams, reset flags and
       
  1426         // send an RTCP report this assures that if 2 or more Tx streams will
       
  1427         // not send at the same time
       
  1428         if ( !reportSent )
       
  1429             {
       
  1430             DoBuildSendRTCPReportL();
       
  1431             }
       
  1432         }
       
  1433     else if ( totalRxStream > 0 )
       
  1434         {
       
  1435         
       
  1436         RTCP_DEBUG_DETAIL_DVALUE( "BuildSendRTCPReport::Sending RR RTCP REPORT sender SSRC = ",
       
  1437                       iDefaultSSRC );
       
  1438        
       
  1439         if ( BuildRTCPReport( NULL, NULL ) == KErrNone )
       
  1440             {
       
  1441             if(IsSrtp())
       
  1442             	{
       
  1443             	SendSRTCPReportL(iDefaultSSRC);
       
  1444             	}
       
  1445             else	
       
  1446             	{
       
  1447             	SendRTCPReport();	
       
  1448             	}            
       
  1449             }
       
  1450         }
       
  1451     else
       
  1452         {
       
  1453         // Do Nothing
       
  1454         }
       
  1455 
       
  1456     }
       
  1457 
       
  1458 // ---------------------------------------------------------------------------
       
  1459 // TInt CRtpSession::ShouldBuildEmptyRR()
       
  1460 // 
       
  1461 // ---------------------------------------------------------------------------
       
  1462 //
       
  1463 TBool CRtpSession::ShouldBuildEmptyRR( CRtpTranStream* aTempTranStream )
       
  1464     {
       
  1465     if ( !iRtcpEnabled )
       
  1466         {
       
  1467         return EFalse;
       
  1468         }
       
  1469 
       
  1470     TBool fEmptyRR( ETrue );
       
  1471     if ( aTempTranStream )
       
  1472         {
       
  1473         // check if no data has been sent lately
       
  1474         if ( aTempTranStream->SentRTPPackets() )
       
  1475             {
       
  1476             fEmptyRR = EFalse;
       
  1477             }
       
  1478         }
       
  1479 
       
  1480     // find if any receive stream has received an RTP packet
       
  1481     if ( AnyRcvStreamReceivedRtpPacket() )
       
  1482         {
       
  1483         fEmptyRR = EFalse;
       
  1484         }
       
  1485     return fEmptyRR;
       
  1486     }
       
  1487 
       
  1488 // ---------------------------------------------------------------------------
       
  1489 // TInt CRtpSession::DetermineSourceCountAndLength()
       
  1490 // 
       
  1491 // ---------------------------------------------------------------------------
       
  1492 //
       
  1493 void CRtpSession::DetermineSourceCountAndLength(
       
  1494                       TInt& aSourceCount,
       
  1495                       TInt& aLength,
       
  1496                       TBool aRxActive[KMaxNumActiveRcvStreams] )
       
  1497     {
       
  1498     if ( !iRtcpEnabled )
       
  1499         {
       
  1500         return;
       
  1501         }
       
  1502 
       
  1503     TInt k( 0 );
       
  1504     TInt numRcvStreams( iStreamRxArray->Count() );
       
  1505     for ( k = 0; k < numRcvStreams; k++ )
       
  1506         {
       
  1507         if ( aRxActive[k] )
       
  1508             {
       
  1509             if ( RcvStreamReceivedRtpPacket( k ) )
       
  1510                 {
       
  1511                 aSourceCount++;
       
  1512                 aLength += 6; // Length of one RR block in 32-bit units
       
  1513                 
       
  1514                 
       
  1515                 RTCP_DEBUG_DETAIL_DVALUE( "SEND: RX RR ID = ",
       
  1516                           iStreamRxArray->At( k ).GetMagicKey() );
       
  1517                 RTCP_DEBUG_DETAIL_DVALUE( "SEND: RX RR SSRC = ", 
       
  1518                    ( ( CRtpRecvStream* )iStreamRxArray->At( k ).
       
  1519                       GetStreamAddress() )->GetLocalSSRC() );
       
  1520                
       
  1521                 }
       
  1522             }
       
  1523         }
       
  1524     }
       
  1525 
       
  1526    
       
  1527 // ---------------------------------------------------------------------------
       
  1528 // TInt CRtpSession::SetParamsForFullRTCPReport()
       
  1529 // 
       
  1530 // ---------------------------------------------------------------------------
       
  1531 //
       
  1532 void CRtpSession::SetParamsForFullRTCPReport(
       
  1533                        CRtpTranStream* aTranStream,
       
  1534                        TRtpPacketStreamParam* aStreamParam,
       
  1535                        TRtpPacketIOParam* aInitParam,
       
  1536                        TBool aRxActive[KMaxNumActiveRcvStreams] )
       
  1537     {
       
  1538     if ( !iRtcpEnabled )
       
  1539         {
       
  1540         return;
       
  1541         }
       
  1542 
       
  1543     TInt sourceCount = 0;
       
  1544     TInt length = 1;
       
  1545 
       
  1546     if ( aTranStream != NULL )
       
  1547         {
       
  1548         if ( aTranStream->SentRTPPackets() )
       
  1549             {
       
  1550             
       
  1551             RTCP_DEBUG_DETAIL_DVALUE( "SEND: TX SR ID = ",
       
  1552                       aTranStream->GetStreamID() );
       
  1553             RTCP_DEBUG_DETAIL_DVALUE( "SEND: TX SR SSRC = ",
       
  1554                       aTranStream->GetLocalSSRC() );
       
  1555         
       
  1556             
       
  1557             length += 5;
       
  1558             aInitParam->TRTCP_HEADER.pt = ERTCP_SR;
       
  1559             }
       
  1560         else
       
  1561             {
       
  1562             aInitParam->TRTCP_HEADER.pt = ERTCP_RR;
       
  1563             }
       
  1564         aStreamParam->TRTCP_HEADER.SSRC = aTranStream->GetLocalSSRC();
       
  1565         }   
       
  1566     else
       
  1567         {
       
  1568         aInitParam->TRTCP_HEADER.pt = ERTCP_RR;
       
  1569         aStreamParam->TRTCP_HEADER.SSRC = iDefaultSSRC;
       
  1570         }
       
  1571 
       
  1572     DetermineSourceCountAndLength( sourceCount, length, aRxActive );
       
  1573 
       
  1574     aInitParam->TRTCP_HEADER.sourceCount = sourceCount;
       
  1575     aInitParam->TRTCP_HEADER.length = length;
       
  1576     }
       
  1577 
       
  1578 // ---------------------------------------------------------------------------
       
  1579 // TInt CRtpSession::BuildRTCPReport()
       
  1580 // 
       
  1581 // ---------------------------------------------------------------------------
       
  1582 //
       
  1583 TInt CRtpSession::BuildRTCPReport( CRtpTranStream* aTempTranStream,
       
  1584                                    TInt aLastReport )
       
  1585     {
       
  1586     if ( !iRtcpEnabled )
       
  1587         {
       
  1588         return KErrGeneral;
       
  1589         }
       
  1590     
       
  1591     RTCP_DEBUG_DETAIL( "CRtpSession::BuildRTCPReport" );
       
  1592     
       
  1593 
       
  1594     TBool activeStreams( EFalse );
       
  1595     TRtpPacketStreamParam streamParam;
       
  1596     TRtpPacketIOParam initParam;
       
  1597     TBool rxActive[KMaxNumActiveRcvStreams];
       
  1598     TInt numRcvStreams( iStreamRxArray->Count() );
       
  1599     TInt k( 0 );
       
  1600     TBool streamExists( EFalse );
       
  1601 
       
  1602     iPktRtcpSnd->RtpPacketReset();
       
  1603 
       
  1604     // send an SDES packet if no Tx or Rx streams are available
       
  1605     if ( aTempTranStream != NULL || numRcvStreams != NULL )
       
  1606         {
       
  1607         streamExists = ETrue;
       
  1608         }
       
  1609     else
       
  1610         {
       
  1611         
       
  1612         RTCP_DEBUG_DETAIL( "Error: no suitable stream exists" );
       
  1613         
       
  1614         return KErrGeneral;
       
  1615         }
       
  1616 
       
  1617     Mem::FillZ( rxActive, KMaxNumActiveRcvStreams );
       
  1618 
       
  1619     // check which streams are active
       
  1620     for ( k = 0; k < numRcvStreams; k++ )
       
  1621         {
       
  1622         if ( RcvStreamActive( k ) )
       
  1623             {
       
  1624             rxActive[k] = ETrue;
       
  1625             activeStreams = ETrue;
       
  1626             }
       
  1627         else
       
  1628             {
       
  1629             rxActive[k] = EFalse;
       
  1630             }
       
  1631         }
       
  1632 
       
  1633     if ( aTempTranStream )
       
  1634         {
       
  1635         if ( aTempTranStream->FirstPkg() )
       
  1636             {
       
  1637             RTCP_DEBUG_DETAIL( "Error: must send RTP packets before RTCP" );
       
  1638             
       
  1639             return KErrGeneral;
       
  1640             }
       
  1641         }
       
  1642     else
       
  1643         {
       
  1644         if ( !activeStreams )
       
  1645             {
       
  1646             RTCP_DEBUG_DETAIL( "Error: no active receive streams found" );
       
  1647             
       
  1648             return KErrGeneral;
       
  1649             }
       
  1650         }
       
  1651 
       
  1652     if ( streamExists )
       
  1653         {
       
  1654         TBool fEmptyRR( ShouldBuildEmptyRR( aTempTranStream ) );
       
  1655         
       
  1656         if ( fEmptyRR == EFalse )
       
  1657             {
       
  1658             // find which stream are active and send report accordingly
       
  1659             SetParamsForFullRTCPReport( aTempTranStream, &streamParam,
       
  1660                                         &initParam, rxActive );
       
  1661             // first build the standard RTCP header
       
  1662             iPktRtcpSnd->SetType( ERTCP_HEADER );
       
  1663             iPktRtcpSnd->RtpPacketBuild( &streamParam, &initParam );
       
  1664 
       
  1665             // always send report about itself (stream) and all the other
       
  1666             // active receiving streams
       
  1667             BuildSrSection( aTempTranStream );
       
  1668             BuildRrSection( numRcvStreams, rxActive, aLastReport );
       
  1669             }
       
  1670         else
       
  1671             {
       
  1672             // build empty RR packet if we have inactive Tx and/or Rx streams
       
  1673             BuildEmptyRr( aTempTranStream );
       
  1674             }
       
  1675         }
       
  1676 
       
  1677     BuildSdesSection( aTempTranStream );
       
  1678 
       
  1679     return KErrNone;
       
  1680     }
       
  1681 
       
  1682 // ---------------------------------------------------------------------------
       
  1683 // CRtpSession::BuildSrSection()
       
  1684 // 
       
  1685 // ---------------------------------------------------------------------------
       
  1686 //
       
  1687 void CRtpSession::BuildSrSection( CRtpTranStream* aTempTranStream )
       
  1688     {
       
  1689     if ( !iRtcpEnabled )
       
  1690         {
       
  1691         return;
       
  1692         }
       
  1693 
       
  1694     if ( aTempTranStream )
       
  1695         {
       
  1696         if ( aTempTranStream->SentRTPPackets() )
       
  1697             {
       
  1698             RTCP_DEBUG_DETAIL( "SEND: Sent SR packet" );
       
  1699                     
       
  1700             aTempTranStream->RtpStreamCreateRtcpReportSection( iPktRtcpSnd );
       
  1701             aTempTranStream->SetSentRTPPackets( EFalse );
       
  1702             }
       
  1703         }
       
  1704     }
       
  1705 
       
  1706 // ---------------------------------------------------------------------------
       
  1707 // TInt CRtpSession::BuildRrSection()
       
  1708 // 
       
  1709 // ---------------------------------------------------------------------------
       
  1710 //
       
  1711 void CRtpSession::BuildRrSection( TInt aNumRcvStreams,
       
  1712                                   TBool aRxActive[KMaxNumActiveRcvStreams],
       
  1713                                   TInt aLastReport )
       
  1714     {
       
  1715     if ( !iRtcpEnabled )
       
  1716         {
       
  1717         return;
       
  1718         }
       
  1719 
       
  1720     for ( TInt k = 0; k < aNumRcvStreams; k++ )
       
  1721         {
       
  1722         if ( aRxActive[k] && RcvStreamReceivedRtpPacket( k ) )
       
  1723             {
       
  1724             // get stream with the indicated index value
       
  1725             CRtpRecvStream* tempRcvStream = GetRcvStreamByIndex( k );
       
  1726             
       
  1727             RTCP_DEBUG_DETAIL( "SEND: Sent RR packet" );
       
  1728             
       
  1729             
       
  1730             tempRcvStream->RtpStreamCreateRtcpReportSection( iPktRtcpSnd );
       
  1731             // do not set the receive stream until this is the last RTCP
       
  1732             // report to be sent
       
  1733             if ( !aLastReport )
       
  1734                 {
       
  1735                 tempRcvStream->SetReceivedRTPPackets( EFalse );
       
  1736                 }
       
  1737             }
       
  1738         }
       
  1739     }
       
  1740     
       
  1741 // ---------------------------------------------------------------------------
       
  1742 // CRtpSession::BuildEmptyRr()
       
  1743 // 
       
  1744 // ---------------------------------------------------------------------------
       
  1745 //
       
  1746 void CRtpSession::BuildEmptyRr( CRtpTranStream* aTempTranStream )
       
  1747     {
       
  1748     if ( !iRtcpEnabled )
       
  1749         {
       
  1750         return;
       
  1751         }
       
  1752 
       
  1753     TRtpPacketStreamParam streamParam;
       
  1754     TRtpPacketIOParam initParam;
       
  1755 
       
  1756     initParam.TRTCP_HEADER.pt = ERTCP_RR;
       
  1757     initParam.TRTCP_HEADER.sourceCount = 0;
       
  1758     initParam.TRTCP_HEADER.length = 1;
       
  1759 
       
  1760     if ( aTempTranStream == NULL ) //No Tx stream
       
  1761         {
       
  1762         streamParam.TRTCP_HEADER.SSRC = iDefaultSSRC;
       
  1763         }
       
  1764     else
       
  1765         {
       
  1766         streamParam.TRTCP_HEADER.SSRC = aTempTranStream->GetLocalSSRC();
       
  1767         }
       
  1768 
       
  1769     iPktRtcpSnd->SetType( ERTCP_HEADER );
       
  1770     iPktRtcpSnd->RtpPacketBuild( &streamParam, &initParam );
       
  1771     
       
  1772     
       
  1773     RTCP_DEBUG_DETAIL_DVALUE( "SEND: Sent EMPTY RR report sender SSRC = ", 
       
  1774                   streamParam.TRTCP_HEADER.SSRC );
       
  1775     
       
  1776     }
       
  1777 
       
  1778 // ---------------------------------------------------------------------------
       
  1779 // CRtpSession::BuildSdesSection()
       
  1780 // 
       
  1781 // ---------------------------------------------------------------------------
       
  1782 //
       
  1783 void CRtpSession::BuildSdesSection( CRtpTranStream* aTempTranStream )
       
  1784     {
       
  1785     if ( !iRtcpEnabled )
       
  1786         {
       
  1787         return;
       
  1788         }
       
  1789 
       
  1790     TBool fCNameOnly( EFalse );
       
  1791 
       
  1792     // start counter, when it equals 5, send full SDES information,
       
  1793     // other wise send CNAME only
       
  1794     iSendSdesCounter++;
       
  1795 
       
  1796     // create SDES packet
       
  1797     if ( iSendSdesCounter >= KSDES_SEND_PACKET )
       
  1798         {
       
  1799         fCNameOnly = EFalse;
       
  1800         
       
  1801         
       
  1802         RTCP_DEBUG_DETAIL( "SEND: Sent FULL SDES packet" );
       
  1803         
       
  1804         
       
  1805         iSendSdesCounter = 0;
       
  1806 
       
  1807         if ( !aTempTranStream ) //No Tx stream
       
  1808             {
       
  1809             CreateSDES( fCNameOnly, iDefaultSSRC );
       
  1810             }
       
  1811         else
       
  1812             {
       
  1813             CreateSDES( fCNameOnly, aTempTranStream->GetLocalSSRC() );
       
  1814             }
       
  1815         }
       
  1816     else
       
  1817         {
       
  1818         fCNameOnly = ETrue;
       
  1819         
       
  1820         
       
  1821         RTCP_DEBUG_DETAIL( "SEND: Sent only CNAME SDES packet" );
       
  1822         
       
  1823         
       
  1824         if ( !aTempTranStream ) //No Tx stream
       
  1825             {
       
  1826             CreateSDES( fCNameOnly, iDefaultSSRC );
       
  1827             }
       
  1828         else
       
  1829             {
       
  1830             CreateSDES( fCNameOnly, aTempTranStream->GetLocalSSRC() );
       
  1831             }
       
  1832         }
       
  1833     
       
  1834 
       
  1835     if ( !aTempTranStream ) //No Tx stream
       
  1836         {
       
  1837         RTCP_DEBUG_DETAIL( "SEND: SENDING NEW RTCP REPORT ONLY RR" );
       
  1838         
       
  1839         }
       
  1840     else
       
  1841         {
       
  1842         RTCP_DEBUG_DETAIL_DVALUE( "SEND: SENDING NEW RTCP REPORT SR and RR(?) for TX Stream ID = ", 
       
  1843                       aTempTranStream->GetStreamID() );
       
  1844         
       
  1845         }
       
  1846     }
       
  1847 
       
  1848 // ---------------------------------------------------------------------------
       
  1849 // CRtpSession::SendRTCPReport()
       
  1850 // 
       
  1851 // ---------------------------------------------------------------------------
       
  1852 //
       
  1853 void CRtpSession::SendRTCPReport()
       
  1854     {
       
  1855     if ( !iRtcpEnabled )
       
  1856         {
       
  1857         return;
       
  1858         }
       
  1859     if ( iCommNet->Send( ERTCPPort, iPktRtcpSnd->Des() ) == KErrDisconnected )
       
  1860         {
       
  1861         RTCP_DEBUG_DETAIL( "Fail to send RTCP packet due to network disconnected" );
       
  1862        
       
  1863         ( void ) StopRtcpSending();
       
  1864         }
       
  1865 
       
  1866     // update RTCP average packet size
       
  1867     AverageRtcpSize( iPktRtcpSnd->Size() );
       
  1868     iPktRtcpSnd->RtpPacketReset();
       
  1869     }
       
  1870 
       
  1871 // ---------------------------------------------------------------------------
       
  1872 // CRtpSession::CreateSDES()
       
  1873 // 
       
  1874 // ---------------------------------------------------------------------------
       
  1875 //
       
  1876 void CRtpSession::CreateSDES( TInt aFCNameOnly, TRtpSSRC aSSRC )
       
  1877     {
       
  1878     if ( !iRtcpEnabled )
       
  1879         {
       
  1880         return;
       
  1881         }
       
  1882 
       
  1883     TInt paddingSize = 0;
       
  1884 
       
  1885     // build RTCP SDES packet header
       
  1886 
       
  1887     TRtpPacketStreamParam streamParam;
       
  1888     TRtpPacketIOParam initParam;
       
  1889 
       
  1890     TInt theIndex = 0;
       
  1891     TInt sourceCount = 1;
       
  1892     TInt length = 0;
       
  1893     TInt sdesCount = 0;
       
  1894 
       
  1895     initParam.TRTCP_HEADER.pt = ERTCP_SDES;
       
  1896 
       
  1897     // get number of chunks in this SDES report
       
  1898     if ( aFCNameOnly )
       
  1899         {
       
  1900         // send only the CNAME information
       
  1901         for ( theIndex = 0; theIndex <= ERTCP_SDES_CNAME; theIndex++ )
       
  1902             {
       
  1903             if ( iLocalSdes->iSDESItemsSize[theIndex] != 0 )
       
  1904                 {
       
  1905                 sdesCount++;
       
  1906                 }
       
  1907             // calculate size of all SDES items in bytes
       
  1908             length += iLocalSdes->iSDESItemsSize[theIndex];
       
  1909             }
       
  1910         }
       
  1911     else
       
  1912         {
       
  1913         // send FULL SDES information
       
  1914         for ( theIndex = 0; theIndex < ERTCP_NUM_OF_SDES_ITEMS; theIndex++ )
       
  1915             {
       
  1916             if ( iLocalSdes->iSDESItemsSize[theIndex] != 0 )
       
  1917                 {
       
  1918                 sdesCount++;
       
  1919                 }
       
  1920             // calculate size of all SDES items in bytes
       
  1921             length += iLocalSdes->iSDESItemsSize[theIndex];
       
  1922             }
       
  1923         }
       
  1924 
       
  1925     // sdesCount*2 is the bytes for 8-bit type field and octet count
       
  1926     if ( ( sdesCount * 2 + length + 1 ) % 4 == 0 )
       
  1927     {
       
  1928         // multiples of 4 bytes, no padding is needed
       
  1929         // Calcultate total number of 32 bit words in SDES packet,
       
  1930         // including end of list null octet
       
  1931         length = ( sdesCount * 2 + length + 1 ) / 4 + sourceCount;
       
  1932         }
       
  1933     else
       
  1934         {
       
  1935         // not multiples of 4 bytes, padding is needed
       
  1936         paddingSize = 4 - ( sdesCount * 2 + length + 1 ) % 4;
       
  1937         length = ( sdesCount * 2 + length + 1 ) / 4 + 1 + sourceCount;
       
  1938         }
       
  1939 
       
  1940     initParam.TRTCP_HEADER.sourceCount = sourceCount;
       
  1941     initParam.TRTCP_HEADER.length = length;
       
  1942 
       
  1943     iPktRtcpSnd->SetType( ERTCP_HEADER );
       
  1944     iPktRtcpSnd->RtpPacketBuild( &streamParam, &initParam );
       
  1945 
       
  1946     streamParam.TRTCP_SDES.SSRC = aSSRC;
       
  1947 
       
  1948     if ( aFCNameOnly )
       
  1949         {
       
  1950         for ( theIndex = 0; theIndex <= ERTCP_SDES_CNAME; theIndex++ )
       
  1951             {
       
  1952             initParam.TRTCP_SDES.sdesItems[theIndex] =
       
  1953                 iLocalSdes->iSDESItems[theIndex];
       
  1954             initParam.TRTCP_SDES.sdesItemsSize[theIndex] =
       
  1955                 iLocalSdes->iSDESItemsSize[theIndex];
       
  1956             }
       
  1957         
       
  1958         // set other sdes item sizes to zero
       
  1959         for ( theIndex = ERTCP_SDES_CNAME + 1;
       
  1960               theIndex < ERTCP_NUM_OF_SDES_ITEMS; theIndex++ )
       
  1961             {
       
  1962             initParam.TRTCP_SDES.sdesItemsSize[theIndex] = 0;
       
  1963             }
       
  1964         }
       
  1965     else
       
  1966         {
       
  1967         for ( theIndex = 0; theIndex < ERTCP_NUM_OF_SDES_ITEMS; theIndex++ )
       
  1968             {
       
  1969             initParam.TRTCP_SDES.sdesItems[theIndex] =
       
  1970                 iLocalSdes->iSDESItems[theIndex];
       
  1971             initParam.TRTCP_SDES.sdesItemsSize[theIndex] =
       
  1972                 iLocalSdes->iSDESItemsSize[theIndex];
       
  1973             }
       
  1974         }
       
  1975 
       
  1976     initParam.TRTCP_SDES.paddingSize = paddingSize; 
       
  1977     iPktRtcpSnd->SetType( ERTCP_SDES );
       
  1978     iPktRtcpSnd->RtpPacketBuild( &streamParam, &initParam ); 
       
  1979     
       
  1980     RTCP_DEBUG_DETAIL( "SEND: Sending SDES message as" );
       
  1981     for ( TInt k = 0; k <= ERTCP_SDES_CNAME; k++ )
       
  1982         {
       
  1983         RTCP_DEBUG_DETAIL( initParam.TRTCP_SDES.sdesItems[k] );
       
  1984         }
       
  1985     
       
  1986     
       
  1987     }
       
  1988 
       
  1989 // ---------------------------------------------------------------------------
       
  1990 // TRtpRtcpEnum CRtpSession::ProcessRTCPReportSection()
       
  1991 // 
       
  1992 // ---------------------------------------------------------------------------
       
  1993 //
       
  1994 TRtpRtcpEnum CRtpSession::ProcessOneRTCPReportSectionL(
       
  1995                               const TRtpPacketIOParam& aExtractParam,
       
  1996                               TRtpSSRC aSourceSSRC )
       
  1997     {
       
  1998     if ( !iRtcpEnabled )
       
  1999         {
       
  2000         return ERTCP_PACKET_ERROR;
       
  2001         }
       
  2002 
       
  2003     TRtpRtcpEnum parseResult( ERTCP_NO_ERROR );
       
  2004     iPktRtcpRcv->SetType( aExtractParam.TRTCP_HEADER.pt );
       
  2005 
       
  2006     // handle compound packets
       
  2007     switch( iPktRtcpRcv->Type() )
       
  2008         {
       
  2009         case ERTCP_SDES:
       
  2010             parseResult = ProcessSDESSectionL( aSourceSSRC );
       
  2011             
       
  2012             
       
  2013             RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Parsed SDES Info source SSRC ", aSourceSSRC );
       
  2014             
       
  2015             
       
  2016             break;
       
  2017 
       
  2018         case ERTCP_BYE:
       
  2019             parseResult = ProcessBYESectionL();
       
  2020             
       
  2021             RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Parsed BYE Info source SSRC ", aSourceSSRC );
       
  2022             
       
  2023             
       
  2024             if ( parseResult == ERTCP_FOUND_RXSTREAM )
       
  2025                 {
       
  2026                 return ERTCP_FOUND_RXSTREAM;
       
  2027                 }
       
  2028             break;
       
  2029         
       
  2030         case ERTCP_APP:
       
  2031             parseResult = ProcessAPPSectionL( aExtractParam.TRTCP_HEADER.length,
       
  2032                                     aExtractParam.TRTCP_HEADER.sourceCount );
       
  2033             
       
  2034             RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Parsed APP Info source SSRC ", aSourceSSRC );
       
  2035             
       
  2036             
       
  2037             if ( parseResult == ERTCP_FOUND_RXSTREAM )
       
  2038                 {
       
  2039                 return ERTCP_FOUND_RXSTREAM;
       
  2040                 }
       
  2041             break;
       
  2042 
       
  2043         case ERTCP_SR:
       
  2044             
       
  2045              RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Received RTCP_SR, sender SSRC ", aSourceSSRC );
       
  2046             
       
  2047             
       
  2048             parseResult = ProcessSRSectionL(
       
  2049                               aExtractParam.TRTCP_HEADER.sourceCount );
       
  2050             break;
       
  2051 
       
  2052         case ERTCP_RR:
       
  2053             
       
  2054              RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Received RTCP_RR, sender SSRC ", aSourceSSRC );
       
  2055             
       
  2056             
       
  2057             // update average RTCP report size
       
  2058             AverageRtcpSize( iPktRtcpRcv->Size() );
       
  2059 
       
  2060             if ( aExtractParam.TRTCP_HEADER.sourceCount == 0 )
       
  2061                 {
       
  2062                 // this is for the special case where we have an empty RR
       
  2063                 // section (RC=0)
       
  2064                 
       
  2065                     RTCP_DEBUG_DETAIL( "RECEIVE: Empty RR Report" );
       
  2066                 
       
  2067                  if ( iRtcpObserver )
       
  2068             		{
       
  2069             		iRtcpObserver->RrReceived( iSessionId, iDefaultSSRC );
       
  2070             		}
       
  2071                 // expect an SDES after the empty RR
       
  2072                 parseResult = ERTCP_PACKET_MORE;
       
  2073                 }
       
  2074             else
       
  2075                 {
       
  2076                 // parse all RR reports
       
  2077                 parseResult = ProcessRRSectionL(
       
  2078                                   aExtractParam.TRTCP_HEADER.sourceCount );
       
  2079                 }
       
  2080             break;
       
  2081 
       
  2082         default:
       
  2083             parseResult = ERTCP_PACKET_ERROR;
       
  2084             break;
       
  2085         } // switch
       
  2086     return parseResult;
       
  2087     }
       
  2088 
       
  2089 // ---------------------------------------------------------------------------
       
  2090 // TRtpRtcpEnum CRtpSession::ProcessRTCPReportL()
       
  2091 // 
       
  2092 // ---------------------------------------------------------------------------
       
  2093 //
       
  2094 TRtpRtcpEnum CRtpSession::ProcessRTCPReportL()
       
  2095     {
       
  2096     if ( !iRtcpEnabled )
       
  2097         {
       
  2098         return ERTCP_PACKET_ERROR;
       
  2099         }
       
  2100 
       
  2101     TRtpPacketStreamParam streamParam;
       
  2102     TRtpPacketIOParam extractParam;
       
  2103     TRtpRtcpEnum parseResult( ERTCP_NO_ERROR );
       
  2104     TRtpSSRC sourceSSRC( NULL );
       
  2105     TInt iPacketLength( 0 );
       
  2106     TBool bHeader( ETrue );
       
  2107     TBool bContinue = ETrue;
       
  2108     
       
  2109     // set packet pointer to the beginning
       
  2110     iPktRtcpRcv->RtpPacketResetPtr();
       
  2111 
       
  2112     /* All RTCP reports are compound packets, consisting of at least:
       
  2113      * - an SR section (potentially containing RR sections) or an RR section
       
  2114      * - an SDES
       
  2115      * In addition, the packet may contain one APP or BYE section.
       
  2116      * This loop processes one section at a time.
       
  2117      */
       
  2118     while ( bContinue )
       
  2119         {
       
  2120         // parse RTCP packet header    
       
  2121         iPktRtcpRcv->SetType( ERTCP_HEADER );
       
  2122         // Get SSRC of packet sender for this report
       
  2123         // (This operation does not move the packet pointer)
       
  2124         sourceSSRC = iPktRtcpRcv->RtpPacketGetSSRC(); 
       
  2125         parseResult = iPktRtcpRcv->RtpPacketProcessL(
       
  2126                           &streamParam, &extractParam );
       
  2127 
       
  2128         if ( parseResult == ERTCP_PACKET_ERROR ||
       
  2129              parseResult == ERTCP_NO_ERROR )
       
  2130             {
       
  2131             return parseResult;
       
  2132             }
       
  2133         
       
  2134         /**
       
  2135          * The payload type field of the first RTCP packet in a compound packet
       
  2136          * must be equal to SR or RR
       
  2137          **/
       
  2138         if( bHeader && ( extractParam.TRTCP_HEADER.pt != ERTCP_SR && 
       
  2139                          extractParam.TRTCP_HEADER.pt != ERTCP_RR ) )
       
  2140             {
       
  2141             return ERTCP_PACKET_ERROR;
       
  2142             }
       
  2143             
       
  2144         if( bHeader )
       
  2145             {
       
  2146             /**
       
  2147              * The padding bit should be zero for for the first packet of a
       
  2148              * compound RTCP packet because padding should only be applied,
       
  2149              * if it is needed, to the last packet [RFC3550]
       
  2150              **/
       
  2151             if( extractParam.TRTP.padding != 0 )
       
  2152                 {
       
  2153                 return ERTCP_PACKET_ERROR;
       
  2154                 }
       
  2155             else
       
  2156                 {
       
  2157                 bHeader = EFalse;
       
  2158                 }
       
  2159             }
       
  2160         
       
  2161         parseResult = ProcessOneRTCPReportSectionL( extractParam, sourceSSRC );
       
  2162 
       
  2163         if ( parseResult == ERTCP_FOUND_RXSTREAM )
       
  2164             {
       
  2165             return ERTCP_FOUND_RXSTREAM;
       
  2166             }
       
  2167         
       
  2168         /**
       
  2169          * The length fields of the individual RTCP packets must add 
       
  2170          * up to the overall length of the compound RTCP packet as 
       
  2171          * received. [RFC3550]
       
  2172          **/
       
  2173         if ( parseResult != ERTCP_PACKET_MORE )
       
  2174             {
       
  2175             TInt iTruePacketLength = iPktRtcpRcv->Size();
       
  2176             
       
  2177             iPacketLength +=  ( ( extractParam.TRTCP_HEADER.length + 1 ) * 4 );
       
  2178             
       
  2179             if ( iPacketLength != iTruePacketLength )
       
  2180                 {
       
  2181                 return ERTCP_PACKET_ERROR;
       
  2182                 }
       
  2183             }
       
  2184         else //if( parseResult != ERTCP_NO_ERROR)
       
  2185             {
       
  2186             //Get the packet length and convert the 8 bit value to 32 bit
       
  2187             iPacketLength +=  ( ( extractParam.TRTCP_HEADER.length + 1 ) * 4 );
       
  2188             }
       
  2189           
       
  2190         
       
  2191         if ( parseResult == ERTCP_PACKET_ERROR ||
       
  2192              parseResult == ERTCP_NO_ERROR )
       
  2193             {
       
  2194             return parseResult;
       
  2195             }
       
  2196         } // for loop
       
  2197     
       
  2198     // We should never get here
       
  2199     return ERTCP_NO_ERROR;
       
  2200     }
       
  2201 
       
  2202 // ---------------------------------------------------------------------------
       
  2203 // TRtpRtcpEnum CRtpSession::ProcessSRSection()
       
  2204 // 
       
  2205 // ---------------------------------------------------------------------------
       
  2206 //
       
  2207 TRtpRtcpEnum CRtpSession::ProcessSRSectionL( TInt aSourceCount )
       
  2208     {
       
  2209     if ( !iRtcpEnabled )
       
  2210         {
       
  2211         return ERTCP_PACKET_ERROR;
       
  2212         }
       
  2213 
       
  2214     TRtpRtcpEnum parseResult( ERTCP_NO_ERROR );
       
  2215     CRtpStream* stream;
       
  2216     TRtpSSRC tempSSRC( NULL );
       
  2217     TUint streamAddress( 0 );
       
  2218     TStreamType streamType;
       
  2219     TInt ret( 0 );
       
  2220 
       
  2221     // update average RTCP report size
       
  2222     AverageRtcpSize( iPktRtcpRcv->Size() );
       
  2223 
       
  2224     // Get SSRC of SR packet, or SSRC_1 in RR Packet 
       
  2225     tempSSRC = iPktRtcpRcv->RtpPacketGetSSRC(); 
       
  2226     ret = MatchSSRCToStream( streamAddress, tempSSRC, streamType );
       
  2227 
       
  2228     if ( ret != KErrNone )
       
  2229         {
       
  2230         return ERTCP_PACKET_ERROR;
       
  2231         }
       
  2232     if ( streamType == ERxStream )
       
  2233         {
       
  2234         stream = ( CRtpRecvStream* ) streamAddress;
       
  2235         }
       
  2236     else //ETxStream
       
  2237         {
       
  2238         stream = ( CRtpTranStream* ) streamAddress;
       
  2239         }
       
  2240     if ( stream == NULL )
       
  2241         {
       
  2242         return ERTCP_PACKET_ERROR;
       
  2243         }
       
  2244     parseResult = stream->RtpStreamProcessRtcpReportSectionL( iPktRtcpRcv );
       
  2245     if ( parseResult == ERTCP_PACKET_ERROR ||
       
  2246          parseResult == ERTCP_NO_ERROR )
       
  2247         {
       
  2248         return parseResult;
       
  2249         }
       
  2250     
       
  2251                     
       
  2252     RTCP_DEBUG_DETAIL( "RECEIVE: Parsing SR Report source SSRC secion" );
       
  2253     
       
  2254     
       
  2255     if ( aSourceCount > 0 )
       
  2256         {
       
  2257         iPktRtcpRcv->SetType( ERTCP_RR );
       
  2258         parseResult = ProcessRRSectionL( aSourceCount );
       
  2259         }
       
  2260     return parseResult;
       
  2261     }
       
  2262 
       
  2263 // ---------------------------------------------------------------------------
       
  2264 // TRtpRtcpEnum CRtpSession::ProcessRRSection()
       
  2265 // 
       
  2266 // ---------------------------------------------------------------------------
       
  2267 //
       
  2268 TRtpRtcpEnum CRtpSession::ProcessRRSectionL( TInt aSourceCount )
       
  2269     {
       
  2270     if ( !iRtcpEnabled )
       
  2271         {
       
  2272         return ERTCP_PACKET_ERROR;
       
  2273         }
       
  2274 
       
  2275     TRtpRtcpEnum parseResult( ERTCP_NO_ERROR );
       
  2276     CRtpStream* stream;
       
  2277     TRtpSSRC tempSSRC( NULL );
       
  2278     TUint streamAddress( 0 );
       
  2279     TStreamType streamType;
       
  2280     TBool bExit( EFalse );
       
  2281     TInt ret( 0 );
       
  2282 
       
  2283     while ( aSourceCount && !bExit )
       
  2284         {
       
  2285         // extract SSRC_X of RR packet in SR Report packet
       
  2286         tempSSRC = iPktRtcpRcv->RtpPacketGetSSRC();
       
  2287         
       
  2288         RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Parsing RR Report source SSRC ", tempSSRC );
       
  2289         
       
  2290         
       
  2291         ret = MatchSSRCToStream( streamAddress, tempSSRC, streamType );
       
  2292         if ( ret != KErrNone )
       
  2293             {
       
  2294             return ERTCP_PACKET_ERROR;
       
  2295             }
       
  2296     
       
  2297         if ( streamType == ERxStream )
       
  2298             {
       
  2299             stream = reinterpret_cast<CRtpRecvStream*>( streamAddress );
       
  2300             }
       
  2301         else //ETxStream
       
  2302             {
       
  2303             stream = reinterpret_cast<CRtpTranStream*>( streamAddress );
       
  2304             }
       
  2305         // If this is an RR, the sender participant is identified by
       
  2306         // streamParam.TRTCP_HEADER.SSRC
       
  2307         // DETAIL: Processing RTCP_RR Packet
       
  2308         parseResult = stream->RtpStreamProcessRtcpReportSectionL( iPktRtcpRcv );
       
  2309         if ( parseResult == ERTCP_PACKET_ERROR ||
       
  2310              parseResult == ERTCP_NO_ERROR )
       
  2311             // parseResult should not be ERTCP_NO_ERROR, as the packet must
       
  2312             // include an SDES section
       
  2313             {
       
  2314             
       
  2315             RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Error/unexpected end of RTCP packet, sender SSRC ",
       
  2316                 tempSSRC );
       
  2317             
       
  2318             
       
  2319             return parseResult;
       
  2320             }
       
  2321   
       
  2322         aSourceCount--;
       
  2323     
       
  2324         if ( aSourceCount == 0 )
       
  2325             {
       
  2326             RTCP_DEBUG_DETAIL( "RECEIVE: Parsed Last RR Report" );
       
  2327             
       
  2328                     
       
  2329             bExit = ETrue;
       
  2330             }
       
  2331         }
       
  2332     return parseResult;
       
  2333     }
       
  2334 
       
  2335 // ---------------------------------------------------------------------------
       
  2336 // TRtpRtcpEnum CRtpSession::ProcessSDESSection()
       
  2337 // 
       
  2338 // ---------------------------------------------------------------------------
       
  2339 //
       
  2340 void CRtpSession::DeallocateMemoryForSdes( TRtpPacketIOParam* aParam )
       
  2341     {
       
  2342     if ( !iRtcpEnabled )
       
  2343         {
       
  2344         return;
       
  2345         }
       
  2346 
       
  2347     TInt index( 0 );
       
  2348     if ( aParam == NULL )
       
  2349         {
       
  2350         return;
       
  2351         }
       
  2352     // deallocate memory for SDES report packet
       
  2353     for ( index = 0; index < ERTCP_NUM_OF_SDES_ITEMS; index++ )
       
  2354         {
       
  2355         if ( aParam->TRTCP_SDES.sdesItems[index] != NULL )
       
  2356             {
       
  2357             User::Free( aParam->TRTCP_SDES.sdesItems[index] );
       
  2358             aParam->TRTCP_SDES.sdesItems[index] = NULL;
       
  2359             }
       
  2360         aParam->TRTCP_SDES.sdesItemsSize[index] = 0;
       
  2361         }
       
  2362     }
       
  2363 
       
  2364 // ---------------------------------------------------------------------------
       
  2365 // TRtpRtcpEnum CRtpSession::GetSDESFromSDESData()
       
  2366 // 
       
  2367 // ---------------------------------------------------------------------------
       
  2368 //
       
  2369 TInt CRtpSession::GetSDESFromSDESData( TRtpSdesParams* aTargetSdes,
       
  2370                                        const CRtpRecvStream* aRecvStream,
       
  2371                                        CRtpSDES* aSourceSdes )
       
  2372     {
       
  2373     if ( !iRtcpEnabled )
       
  2374         {
       
  2375         return KErrGeneral;
       
  2376         }
       
  2377 
       
  2378     if ( aRecvStream )
       
  2379         {
       
  2380         aRecvStream->iRemoteSDES->GetSDES( *aTargetSdes );
       
  2381         }
       
  2382     else
       
  2383         {
       
  2384         if ( !aSourceSdes )
       
  2385             {
       
  2386             return KErrGeneral;
       
  2387             }
       
  2388         aSourceSdes->GetSDES( *aTargetSdes );
       
  2389         }
       
  2390     return KErrNone;
       
  2391     }
       
  2392 
       
  2393 
       
  2394 // ---------------------------------------------------------------------------
       
  2395 // TRtpRtcpEnum CRtpSession::LogSDESData()
       
  2396 // 
       
  2397 // ---------------------------------------------------------------------------
       
  2398 //
       
  2399 #ifdef _DEBUG
       
  2400 void CRtpSession::LogSDESData( const TRtpPacketStreamParam& aStreamParam,
       
  2401                                const CRtpRecvStream* aRecvStream,
       
  2402                                const CRtpSDES* aSdes,
       
  2403                                const TBool aSdesSession )
       
  2404     {
       
  2405     
       
  2406     if ( !iRtcpEnabled )
       
  2407         {
       
  2408         return;
       
  2409         }
       
  2410     
       
  2411     RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: NEW SDES item for SSRC = ",
       
  2412               aStreamParam.TRTCP_SDES.SSRC );
       
  2413     
       
  2414     if ( !aSdesSession )
       
  2415         {
       
  2416         RTCP_DEBUG_DETAIL( "RECEIVE: Receiving NEW SDES items as" );
       
  2417         for ( TInt k = 0; k < ERTCP_NUM_OF_SDES_ITEMS; k++ )
       
  2418             {
       
  2419             RTCP_DEBUG_DETAIL( aRecvStream->iRemoteSDES->iSDESItems[k] );
       
  2420             }
       
  2421         }
       
  2422     else
       
  2423         {
       
  2424         RTCP_DEBUG_DETAIL( "RECEIVE: Receiving NEW SDES items as" );
       
  2425         for ( TInt k = 0; k < ERTCP_NUM_OF_SDES_ITEMS; k++ )
       
  2426             {
       
  2427             RTCP_DEBUG_DETAIL( aSdes->iSDESItems[k] );
       
  2428             }
       
  2429         }
       
  2430     
       
  2431     }
       
  2432 
       
  2433 #endif    
       
  2434 // ---------------------------------------------------------------------------
       
  2435 // TRtpRtcpEnum CRtpSession::ProcessSDESData()
       
  2436 // 
       
  2437 // ---------------------------------------------------------------------------
       
  2438 //
       
  2439 TRtpRtcpEnum CRtpSession::ProcessSDESDataL( TRtpPacketStreamParam& aStreamParam,
       
  2440                                            TRtpPacketIOParam& aExtractParam,
       
  2441                                            TRtpSSRC aSourceSSRC )
       
  2442     {
       
  2443     if ( !iRtcpEnabled )
       
  2444         {
       
  2445         return ERTCP_PACKET_ERROR;
       
  2446         }
       
  2447 
       
  2448     CRtpSDES* tempSdes = NULL;
       
  2449     // Flag to indicate whether SDES is to be assigned to the session
       
  2450     TBool fSdesSession( EFalse );
       
  2451     TInt newSDESSenderItemFlag( EFalse );
       
  2452     TInt err;
       
  2453 
       
  2454     // For finding the receiving stream that belongs to remote participant SSRC
       
  2455     TStreamType streamType;
       
  2456     TUint streamAddress( NULL );
       
  2457     TInt matchResult( KErrNone );
       
  2458     CRtpRecvStream* tempStream = NULL;
       
  2459     matchResult = MatchSSRCToStream( streamAddress,
       
  2460                                      aStreamParam.TRTCP_SDES.SSRC,
       
  2461                                      streamType );
       
  2462 
       
  2463     // If there are no RX streams, only TX streams, then assign the remote
       
  2464     // SDES to the session
       
  2465     if ( matchResult != KErrNone && iNumOfTxStreams && !iNumOfRxStreams )
       
  2466         {
       
  2467         TUint tempSdesAdress = NULL;
       
  2468         TBool result( EFalse );
       
  2469         TRAP( err, result = AddSdesToArrayL( tempSdesAdress,
       
  2470                                              &aExtractParam ) );
       
  2471         if ( result )
       
  2472             {
       
  2473             tempSdes = ( CRtpSDES * ) tempSdesAdress;
       
  2474             TRAP( err, tempSdes->AddSsrcToSdesL( aSourceSSRC ) );
       
  2475 
       
  2476             if ( err )
       
  2477                 return ERTCP_PACKET_ERROR;
       
  2478 
       
  2479             fSdesSession = ETrue;
       
  2480             // increase number of members in session
       
  2481             if ( iTotalParticipantsSession <= KRtcpMaximumParticipants )
       
  2482                 {
       
  2483                 iTotalParticipantsSession++;
       
  2484                 }
       
  2485             }
       
  2486         }
       
  2487     else if ( matchResult != KErrNone )
       
  2488         {
       
  2489         // did not find a matching stream
       
  2490         return ERTCP_PACKET_ERROR;
       
  2491         }
       
  2492     else  // check stream type
       
  2493         {
       
  2494         if ( streamType == ERxStream ) //Rx stream
       
  2495             {
       
  2496             tempStream = reinterpret_cast<CRtpRecvStream*>( streamAddress );
       
  2497             }
       
  2498         else // ETxStream
       
  2499             {
       
  2500             return ERTCP_PACKET_ERROR;
       
  2501             }
       
  2502         // update/add SDES in session SDES array
       
  2503         TUint tempSdesAdress = NULL;
       
  2504         TBool result( EFalse );
       
  2505         TRAP( err, result = AddSdesToArrayL( tempSdesAdress,
       
  2506                                              &aExtractParam ) );
       
  2507         if ( result )
       
  2508             {
       
  2509             tempSdes = reinterpret_cast<CRtpSDES*>( tempSdesAdress );
       
  2510             TRAP( err, tempSdes->AddSsrcToSdesL( aSourceSSRC ) );
       
  2511 
       
  2512             if ( err )
       
  2513                 return ERTCP_PACKET_ERROR;
       
  2514 
       
  2515             // increase number of members in session
       
  2516             if ( iTotalParticipantsSession <= KRtcpMaximumParticipants )
       
  2517                 {
       
  2518                 iTotalParticipantsSession++;
       
  2519                 }
       
  2520             }
       
  2521         }
       
  2522 
       
  2523     if ( matchResult == KErrNone && !fSdesSession )
       
  2524         {
       
  2525         newSDESSenderItemFlag = CompareAndUpdateSdes( tempStream->iRemoteSDES,
       
  2526                                                       &aExtractParam,
       
  2527                                                       ETrue /* Update all */ );
       
  2528         }
       
  2529 
       
  2530     if ( iNewSdes )
       
  2531         {
       
  2532         delete iNewSdes;
       
  2533         iNewSdes = NULL;
       
  2534         }
       
  2535     iNewSdes = new ( ELeave ) TRtpSdesParams() ;
       
  2536     if ( !iNewSdes ) 
       
  2537         {
       
  2538         delete iNewSdes;
       
  2539         iNewSdes = NULL;
       
  2540         return ERTCP_PACKET_ERROR;
       
  2541         }
       
  2542 
       
  2543     if ( newSDESSenderItemFlag || fSdesSession )
       
  2544         {
       
  2545         if ( fSdesSession )
       
  2546             {
       
  2547             GetSDESFromSDESData( iNewSdes, NULL, tempSdes );
       
  2548             }
       
  2549         else
       
  2550             {
       
  2551             GetSDESFromSDESData( iNewSdes, tempStream, NULL );
       
  2552             }
       
  2553         #ifdef _DEBUG 
       
  2554         LogSDESData( aStreamParam, tempStream, tempSdes, fSdesSession );
       
  2555         #endif
       
  2556         }
       
  2557     else
       
  2558         {
       
  2559         // No changes in SDES
       
  2560         
       
  2561         RTCP_DEBUG_DETAIL_DVALUE( "NO CHANGES in SDES item for SSRC = ",
       
  2562                   aStreamParam.TRTCP_SDES.SSRC );
       
  2563         
       
  2564         }
       
  2565 
       
  2566     // Finally, let the observer know what we received
       
  2567     if ( iRtcpObserver )
       
  2568         {
       
  2569         iRtcpObserver->SdesReceived( aStreamParam.TRTCP_SDES.SSRC, *iNewSdes );
       
  2570         }
       
  2571 
       
  2572     return ERTCP_NO_ERROR;
       
  2573     }
       
  2574 
       
  2575 // ---------------------------------------------------------------------------
       
  2576 // TRtpRtcpEnum CRtpSession::ProcessSDESSectionL()
       
  2577 // 
       
  2578 // ---------------------------------------------------------------------------
       
  2579 //
       
  2580 TRtpRtcpEnum CRtpSession::ProcessSDESSectionL( TRtpSSRC aSourceSSRC )
       
  2581     {
       
  2582     if ( !iRtcpEnabled )
       
  2583         {
       
  2584         return ERTCP_PACKET_ERROR;
       
  2585         }
       
  2586 
       
  2587     TRtpPacketStreamParam streamParam;
       
  2588     TRtpPacketIOParam extractParam;
       
  2589     TRtpRtcpEnum parseResult( ERTCP_NO_ERROR );
       
  2590     TRtpRtcpEnum processDataResult( ERTCP_NO_ERROR );
       
  2591     TRtpRtcpEnum returnValue( ERTCP_NO_ERROR );
       
  2592 
       
  2593     parseResult = iPktRtcpRcv->RtpPacketProcessL( &streamParam, &extractParam );
       
  2594 
       
  2595     if ( parseResult < ERTCP_NO_ERROR )
       
  2596         {
       
  2597         returnValue = parseResult;
       
  2598         }
       
  2599     else
       
  2600         {
       
  2601         processDataResult = ProcessSDESDataL( streamParam, extractParam,
       
  2602                                              aSourceSSRC );
       
  2603         if ( processDataResult != ERTCP_NO_ERROR )
       
  2604             {
       
  2605             returnValue = processDataResult;
       
  2606             }
       
  2607         else
       
  2608             {
       
  2609             // parseResult may be ERTCP_NO_ERROR or ERTCP_PACKET_MORE (BYE/APP)
       
  2610             returnValue = parseResult;
       
  2611             }
       
  2612         }
       
  2613 
       
  2614     DeallocateMemoryForSdes( &extractParam );
       
  2615     return returnValue;
       
  2616     }
       
  2617 
       
  2618 // ---------------------------------------------------------------------------
       
  2619 // TRtpRtcpEnum CRtpSession::ProcessBYESection()
       
  2620 // 
       
  2621 // ---------------------------------------------------------------------------
       
  2622 //
       
  2623 TRtpRtcpEnum CRtpSession::ProcessBYESectionL()
       
  2624     {
       
  2625     if ( !iRtcpEnabled )
       
  2626         {
       
  2627         return ERTCP_PACKET_ERROR;
       
  2628         }
       
  2629 
       
  2630     TRtpPacketStreamParam streamParam;
       
  2631     TRtpPacketIOParam extractParam;
       
  2632 
       
  2633     TRtpRtcpEnum parseResult = ERTCP_NO_ERROR;
       
  2634 
       
  2635     parseResult = iPktRtcpRcv->RtpPacketProcessL( &streamParam, &extractParam );
       
  2636 
       
  2637     if ( parseResult < ERTCP_NO_ERROR )
       
  2638         {
       
  2639         if ( extractParam.TRTCP_BYE.reason != NULL )
       
  2640             {
       
  2641             User::Free( extractParam.TRTCP_BYE.reason );
       
  2642             }
       
  2643         return ERTCP_PACKET_ERROR;
       
  2644         }
       
  2645 
       
  2646     // find the receiving stream that belongs to remote participant SSRC
       
  2647     TStreamType streamType;
       
  2648     TUint rcvStreamAddress = NULL;
       
  2649     TRtpId rcvStreamId = 0;
       
  2650     TInt ret = KErrNone;
       
  2651     CRtpRecvStream* rcvStream;
       
  2652     ret = MatchSSRCToStream( rcvStreamAddress, streamParam.TRTCP_BYE.SSRC,
       
  2653                              streamType );
       
  2654     if ( streamType == ERxStream )
       
  2655         {
       
  2656         rcvStream = reinterpret_cast<CRtpRecvStream*>( rcvStreamAddress );
       
  2657         }
       
  2658     else
       
  2659         {
       
  2660         if ( extractParam.TRTCP_BYE.reason != NULL )
       
  2661             {
       
  2662             User::Free( extractParam.TRTCP_BYE.reason );
       
  2663             }
       
  2664         return ERTCP_PACKET_ERROR;
       
  2665         }
       
  2666     if ( ret == KErrNone )
       
  2667         {
       
  2668         rcvStreamId = rcvStream->GetStreamID();
       
  2669         TPtrC8 reason( const_cast<const TUint8*>( extractParam.TRTCP_BYE.reason ),
       
  2670                        extractParam.TRTCP_BYE.reasonSize );
       
  2671         
       
  2672         RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Receiving BYE message RX stream ID ", rcvStreamId );
       
  2673         RTCP_DEBUG_DETAIL( extractParam.TRTCP_BYE.reason );
       
  2674         
       
  2675         
       
  2676         if ( iRtcpObserver )
       
  2677             {
       
  2678             iRtcpObserver->ByeReceived( rcvStreamId,
       
  2679                                         streamParam.TRTCP_BYE.SSRC, reason );
       
  2680             }
       
  2681 
       
  2682         // Delete the SDES information in the array for this SSRC and decrease
       
  2683         // number participants in session
       
  2684         ret = RemoveSdesFromArray( streamParam.TRTCP_BYE.SSRC );
       
  2685         if ( ret == KErrNone )
       
  2686             {
       
  2687             if ( iTotalParticipantsSession > 1 )
       
  2688                 {
       
  2689                 iTotalParticipantsSession--;
       
  2690                 }
       
  2691             }
       
  2692 
       
  2693         if ( extractParam.TRTCP_BYE.reason != NULL )
       
  2694             {
       
  2695             User::Free( extractParam.TRTCP_BYE.reason );
       
  2696             }
       
  2697         parseResult = ERTCP_FOUND_RXSTREAM;
       
  2698         }
       
  2699 
       
  2700     return parseResult;
       
  2701     }
       
  2702 
       
  2703 // ---------------------------------------------------------------------------
       
  2704 // TRtpRtcpEnum CRtpSession::ProcessAPPSection()
       
  2705 // 
       
  2706 // ---------------------------------------------------------------------------
       
  2707 //
       
  2708 TRtpRtcpEnum CRtpSession::ProcessAPPSectionL( TInt aTotalPacketLen,
       
  2709                                              TInt aSubType )
       
  2710     {
       
  2711     if ( !iRtcpEnabled )
       
  2712         {
       
  2713         return ERTCP_PACKET_ERROR;
       
  2714         }
       
  2715 
       
  2716     TRtpPacketStreamParam streamParam;
       
  2717     TRtpPacketIOParam extractParam;
       
  2718     TRtpRtcpEnum parseResult = ERTCP_NO_ERROR;
       
  2719 
       
  2720     streamParam.TRTCP_APP.totalPacketLen = aTotalPacketLen;
       
  2721     parseResult = iPktRtcpRcv->RtpPacketProcessL( &streamParam, &extractParam );
       
  2722 
       
  2723     if ( parseResult < ERTCP_NO_ERROR )
       
  2724         {
       
  2725         return ERTCP_PACKET_ERROR;
       
  2726         }
       
  2727     // find the receiving stream that belongs to remote participant SSRC
       
  2728     TStreamType streamType;
       
  2729     TUint rcvStreamAddress = NULL;
       
  2730     TRtpId rcvStreamId = 0;
       
  2731     TInt ret = KErrNone;
       
  2732     CRtpRecvStream* rcvStream;
       
  2733     ret = MatchSSRCToStream( rcvStreamAddress, streamParam.TRTCP_APP.SSRC,
       
  2734                              streamType );
       
  2735     if ( streamType == ERxStream )
       
  2736         {
       
  2737         rcvStream = reinterpret_cast<CRtpRecvStream*>( rcvStreamAddress );
       
  2738         }
       
  2739     else
       
  2740         {
       
  2741         return ERTCP_PACKET_ERROR;
       
  2742         }
       
  2743 
       
  2744     if ( ret == KErrNone )
       
  2745         {
       
  2746         rcvStreamId = rcvStream->GetStreamID();
       
  2747 
       
  2748         TRtcpApp app;
       
  2749         app.iSubType = aSubType;
       
  2750         Mem::Copy( app.iName, extractParam.TRTCP_APP.name,
       
  2751                    sizeof( TRtcpAppName ) );
       
  2752         Mem::Copy( app.iAppData, extractParam.TRTCP_APP.appData,
       
  2753                    extractParam.TRTCP_APP.appDataLen );
       
  2754         app.iAppDataLen = extractParam.TRTCP_APP.appDataLen;
       
  2755 
       
  2756         if ( iRtcpObserver )
       
  2757             {
       
  2758             iRtcpObserver->AppReceived( rcvStreamId,
       
  2759                                         streamParam.TRTCP_APP.SSRC, app );
       
  2760             }
       
  2761         
       
  2762         RTCP_DEBUG_DETAIL_DVALUE( "RECEIVE: Receiving APP message RX stream ID ", rcvStreamId );
       
  2763         RTCP_DEBUG_DETAIL( extractParam.TRTCP_APP.appData );
       
  2764         
       
  2765         
       
  2766         User::Free( extractParam.TRTCP_APP.appData );
       
  2767         parseResult = ERTCP_FOUND_RXSTREAM;
       
  2768         }
       
  2769 
       
  2770     return parseResult;
       
  2771     }
       
  2772 
       
  2773 // ---------------------------------------------------------------------------
       
  2774 // void CRtpSession::StopRtcpSending()
       
  2775 // 
       
  2776 // ---------------------------------------------------------------------------
       
  2777 //
       
  2778 TInt CRtpSession::StopRtcpSending()
       
  2779     {
       
  2780     TInt result( KErrNotSupported );
       
  2781     if ( iRtcpEnabled )
       
  2782         {
       
  2783         iSndRtcpTimer->Cancel();
       
  2784         iRtcpSendingSuspended = ETrue;
       
  2785         result = KErrNone;
       
  2786         }
       
  2787     return result;
       
  2788     }
       
  2789 
       
  2790 // ---------------------------------------------------------------------------
       
  2791 // void CRtpSession::IsRtcpSendingSuspended()
       
  2792 // 
       
  2793 // ---------------------------------------------------------------------------
       
  2794 //
       
  2795 TInt CRtpSession::IsRtcpSendingSuspended( TBool& aAutoSending )
       
  2796     {
       
  2797     TInt result( KErrNotSupported );
       
  2798     if ( iRtcpEnabled )
       
  2799         {
       
  2800         aAutoSending = !iRtcpSendingSuspended;
       
  2801         result = KErrNone;
       
  2802         }
       
  2803     return result;
       
  2804     }
       
  2805 
       
  2806 // ---------------------------------------------------------------------------
       
  2807 // void CRtpSession::ResumeRtcpSending()
       
  2808 // 
       
  2809 // ---------------------------------------------------------------------------
       
  2810 //
       
  2811 TInt CRtpSession::ResumeRtcpSending()
       
  2812     {
       
  2813     TInt result( KErrNotSupported );
       
  2814     if ( iRtcpEnabled )
       
  2815         {
       
  2816         iRtcpSendingSuspended = EFalse;
       
  2817         result = KErrNone;
       
  2818         if ( iSessionStarted )
       
  2819             {
       
  2820             if ( !iSndRtcpTimer->IsActive() )
       
  2821                 {
       
  2822                 iSndRtcpTimer->After( iRtcpTimeInterval *
       
  2823                                       KMicrosecondPerMillSecond );
       
  2824                 }
       
  2825             else
       
  2826                 {
       
  2827                 result = KErrInUse;
       
  2828                 }
       
  2829             }
       
  2830         }
       
  2831     return result;
       
  2832     }
       
  2833 
       
  2834 // ---------------------------------------------------------------------------
       
  2835 // CRtpSession::ScheduleRtcpSend()
       
  2836 // 
       
  2837 // ---------------------------------------------------------------------------
       
  2838 //
       
  2839 void CRtpSession::ScheduleRtcpSendL()
       
  2840     {
       
  2841     if ( iRtcpEnabled && !iRtcpSendingSuspended )
       
  2842         {
       
  2843         BuildSendRTCPReportL();
       
  2844         NextRtcpInterval();
       
  2845         iSndRtcpTimer->After( iRtcpTimeInterval * KMicrosecondPerMillSecond );
       
  2846         }
       
  2847     }
       
  2848 
       
  2849 // ---------------------------------------------------------------------------
       
  2850 // CRtpSession::OnExpiredL()
       
  2851 // Rtcp scheduler time out call back 
       
  2852 // ---------------------------------------------------------------------------
       
  2853 //
       
  2854 void CRtpSession::OnExpiredL( TInt /*aStatus*/ )
       
  2855     {
       
  2856     if ( iRtcpEnabled )
       
  2857         {
       
  2858         ScheduleRtcpSendL();
       
  2859         }
       
  2860     }
       
  2861 
       
  2862 // ---------------------------------------------------------------------------
       
  2863 // CRtpSession::OnRtpReceivedL()
       
  2864 // Rtp received callback
       
  2865 // ---------------------------------------------------------------------------
       
  2866 //
       
  2867 void CRtpSession::OnRtpReceivedL()
       
  2868     {
       
  2869     RTP_DEBUG_DETAIL( "CRtpSession::OnRtpReceivedL Entry" );
       
  2870     
       
  2871     TInt ret;
       
  2872     if ( iStandardRtp )
       
  2873         {
       
  2874         if ( iRtpRecvBuf.Size() < KMinRtpHeaderSize )
       
  2875             {
       
  2876             // The packet is too short to be valid (min 96 bits in header).
       
  2877             // Discard and continue to issue packet receiving.
       
  2878             
       
  2879             RTP_DEBUG_DETAIL( "Rtp packet too small, discarded" );
       
  2880             
       
  2881             
       
  2882             // if someone is interested receiving non-RTP control packets
       
  2883             if (iNonRTPDataObserver != NULL)
       
  2884                 {
       
  2885                 // notify user
       
  2886                 iNonRTPDataObserver->NonRTPDataReceived(GetRtpSocket()->LocalPort(), 
       
  2887                                                 ETrue, iRtpRecvBuf);
       
  2888                 }            
       
  2889             }
       
  2890         else
       
  2891             {
       
  2892         
       
  2893         	iPktRcv->SetSize( iRtpRecvBuf.Size() );
       
  2894             
       
  2895             RTP_DEBUG_DETAIL_DVALUE( "Rtp Packet size = ", iRtpRecvBuf.Size() );
       
  2896             
       
  2897         	// Give the packet to each Rx stream until the stream SSRC matches
       
  2898             // the SSRC of the RTP packet
       
  2899             
       
  2900             RTP_DEBUG_DETAIL( "FindRtpRxStream by SSRC" );
       
  2901                         
       
  2902         	ret = FindRtpRxStreamL();
       
  2903 
       
  2904         	if ( ret != KErrNone )
       
  2905             	{
       
  2906             	// If no RX streams match remote SSRC, then allow any RX stream
       
  2907                 // with Null SSRC to accept RTP packet
       
  2908              	
       
  2909             	RTP_DEBUG_DETAIL( "No RX streams match set remote SSRC" );
       
  2910             	
       
  2911             	RTP_DEBUG_DETAIL( "Check if any RX stream with Null SSRC to accept RTP packet" );                           
       
  2912             	ret = AssignRtpRxStreamL();
       
  2913             	
       
  2914             	if ( ret != KErrNone )
       
  2915                     {
       
  2916                      //FindAnotherSSRC   
       
  2917                    	
       
  2918               		if( ret == KErrNotFound )
       
  2919              			{
       
  2920              			
       
  2921             			RTP_DEBUG_DETAIL( "Find another remote SSRC " );
       
  2922                 
       
  2923         				RTP_DEBUG_DETAIL( "Either is NonRtp data or a new RTP SSRC stream has to be created" );
       
  2924 
       
  2925                			iPktRcv->RtpPacketResetPtr();
       
  2926 
       
  2927 			    		TRtpPacketStreamParam streamParam;
       
  2928 			    		TRtpPacketIOParam extractParam;
       
  2929 
       
  2930 			    		// process RTP packet
       
  2931 			    		iPktRcv->SetType( ERTP );
       
  2932 
       
  2933 			    		extractParam.TRTP.extension.data = NULL;
       
  2934 
       
  2935 				    	if ( iPktRcv->RtpPacketProcessL( &streamParam, &extractParam ) < 0 )
       
  2936 				        	{
       
  2937 					        RTP_DEBUG_DETAIL( "Invalid Rtp packet is received" );
       
  2938 					  
       
  2939 					    	if (iNonRTPDataObserver != NULL)
       
  2940 	                        	{
       
  2941 	                        	// notify user
       
  2942 	                        	iNonRTPDataObserver->NonRTPDataReceived(GetRtpSocket()->LocalPort(), 
       
  2943 	                                                ETrue, iRtpRecvBuf);
       
  2944 	                        	}                                
       
  2945 				       	 	}
       
  2946 				       	 else
       
  2947 				       		{
       
  2948 				       		//Valid RTP packet only
       
  2949 				       		RTP_DEBUG_DETAIL( "Create new Recv Stream for valid RTP packet " );
       
  2950 				       		CreateNewRecvStreamL();
       
  2951 			       			}
       
  2952              		
       
  2953              			}
       
  2954              			
       
  2955 			         else
       
  2956 			         	{
       
  2957 			         	// Maybe invalid packet received, continue to receive
       
  2958                     	// if someone is interested receiving non-RTP control packets
       
  2959                     
       
  2960 			         	if (iNonRTPDataObserver != NULL)
       
  2961                         	{
       
  2962                         	RTP_DEBUG_DETAIL( "NonRtpDataReceived" );
       
  2963                         	// notify user
       
  2964                         	iNonRTPDataObserver->NonRTPDataReceived(GetRtpSocket()->LocalPort(), 
       
  2965                                                 ETrue, iRtpRecvBuf);
       
  2966                         	}                                
       
  2967 			         	}
       
  2968 			         
       
  2969                     }
       
  2970 
       
  2971                 }	
       
  2972             }
       
  2973         }
       
  2974     else
       
  2975         {
       
  2976         RTP_DEBUG_DETAIL( "Not standard RTP packets" );
       
  2977             
       
  2978         iPktExtRcv->SetSize( iRtpRecvBuf.Size() );
       
  2979         TUint8* buf = 0;
       
  2980         TInt length;
       
  2981         ret = iPktExtRcv->RtpPacketProcess( buf, &length );
       
  2982         }
       
  2983     RTP_DEBUG_DETAIL( "CRtpSession::OnRtpReceivedL Exit" );
       
  2984     
       
  2985     IssueRtpRecv();    
       
  2986     }
       
  2987 
       
  2988 // ---------------------------------------------------------------------------
       
  2989 // CRtpSession::OnRtcpReceivedL()
       
  2990 // Rtcp received call back
       
  2991 // ---------------------------------------------------------------------------
       
  2992 //
       
  2993 void CRtpSession::OnRtcpReceivedL()
       
  2994     {
       
  2995     if ( !iRtcpEnabled )
       
  2996         {
       
  2997         return;
       
  2998         }
       
  2999 
       
  3000     TRtpRtcpEnum errPacket;
       
  3001     iPktRtcpRcv->SetSize( iRtcpRecvBuf.Size() );
       
  3002 
       
  3003     errPacket = ProcessRTCPReportL();
       
  3004     
       
  3005     // if someone is interested receiving non-RTP control packets
       
  3006     if (errPacket == ERTCP_PACKET_ERROR && iNonRTPDataObserver != NULL)
       
  3007         {
       
  3008         // notify user
       
  3009         iNonRTPDataObserver->NonRTPDataReceived(GetRtcpSocket()->LocalPort(), EFalse,
       
  3010                                                 iRtcpRecvBuf);
       
  3011         }
       
  3012     
       
  3013     IssueRtcpRecv();
       
  3014 
       
  3015     if ( errPacket == ERTCP_PACKET_ERROR )
       
  3016         {
       
  3017         iRtcpErrors++;
       
  3018         
       
  3019         
       
  3020         RTCP_DEBUG_DETAIL_DVALUE( "ERROR: Packet error Session ID = ", iSessionId );
       
  3021         
       
  3022         }
       
  3023     }
       
  3024 
       
  3025 // ---------------------------------------------------------------------------
       
  3026 // CRtpSession::OnReceptionError()
       
  3027 // Rtcp reception error call back
       
  3028 // ---------------------------------------------------------------------------
       
  3029 //
       
  3030 void CRtpSession::OnReceptionError( TPortType aPort, TInt aError )
       
  3031     {
       
  3032     
       
  3033     RTP_DEBUG_DETAIL( "CRtpSession::OnReceptionError" );
       
  3034     
       
  3035 
       
  3036     switch ( aError )
       
  3037         {
       
  3038         case KErrDisconnected:
       
  3039             
       
  3040             RTP_DEBUG_DETAIL( "Socket Disconnected" );
       
  3041             
       
  3042             break;
       
  3043             
       
  3044         default:
       
  3045             if ( aPort == ERTCPPort )
       
  3046                 {
       
  3047                 
       
  3048                 RTCP_DEBUG_DETAIL_DVALUE( "RTCP reception error",
       
  3049                           aError );
       
  3050                 
       
  3051                 IssueRtcpRecv();
       
  3052                 }
       
  3053             else
       
  3054                 {
       
  3055                 
       
  3056                 RTP_DEBUG_DETAIL_DVALUE( "RTP reception error",
       
  3057                           aError );
       
  3058                 
       
  3059                 IssueRtpRecv();
       
  3060                 }
       
  3061             
       
  3062             RTP_DEBUG_DETAIL( "Restarting reception" );
       
  3063             
       
  3064             break;
       
  3065         }
       
  3066     }
       
  3067 
       
  3068 // ---------------------------------------------------------------------------
       
  3069 // TRtpSSRC CRtpSession::OnPacketRejected()
       
  3070 // 
       
  3071 // ---------------------------------------------------------------------------
       
  3072 //
       
  3073 void CRtpSession::OnPacketRejected(TPortType aPort)
       
  3074     {
       
  3075     
       
  3076     RTP_DEBUG_DETAIL( "OnPacketRejected" );
       
  3077         
       
  3078     
       
  3079     if ( aPort == ERTPPort )
       
  3080         {
       
  3081         IssueRtpRecv();        
       
  3082         }
       
  3083     else
       
  3084         {
       
  3085         IssueRtcpRecv();        
       
  3086         }
       
  3087                             
       
  3088     }
       
  3089     
       
  3090 // ---------------------------------------------------------------------------
       
  3091 // TRtpSSRC CRtpSession::GenerateSSRC()
       
  3092 // 
       
  3093 // ---------------------------------------------------------------------------
       
  3094 //
       
  3095 TRtpSSRC CRtpSession::GenerateSSRC()
       
  3096     {
       
  3097     return static_cast<TUint32>( Random() );
       
  3098     }
       
  3099 
       
  3100 // ---------------------------------------------------------------------------
       
  3101 // TInt CRtpSession::AddStream()
       
  3102 // Add Stream object to array
       
  3103 // ---------------------------------------------------------------------------
       
  3104 //
       
  3105 TInt CRtpSession::AddStream( const TStream aArrayID, TStreamType aStreamType )
       
  3106     {
       
  3107     if ( aStreamType == ERxStream )
       
  3108         {
       
  3109         TRAPD( ret, iStreamRxArray->AppendL( aArrayID ) );
       
  3110         return ret;
       
  3111         }
       
  3112     else //ETxStream
       
  3113         {
       
  3114         TRAPD( ret, iStreamTxArray->AppendL( aArrayID ) );
       
  3115         return ret;
       
  3116         }
       
  3117     }
       
  3118 
       
  3119 // ---------------------------------------------------------------------------
       
  3120 // TInt CRtpSession::FindStream()
       
  3121 // Find Stream Object in array
       
  3122 // ---------------------------------------------------------------------------
       
  3123 //
       
  3124 TInt CRtpSession::FindStream( const TRtpId aMagicKey, TUint& aStreamAddress )
       
  3125     {
       
  3126     TKeyArrayFix magicKey( TStream::iOffset, ECmpTUint );
       
  3127     TInt index = -1;
       
  3128 
       
  3129     TStream match( aMagicKey, 0 );
       
  3130     if ( ( iStreamRxArray->Find( match, magicKey, index ) == KErrNone )
       
  3131          && ( index >= 0 ) )
       
  3132         {
       
  3133         aStreamAddress = iStreamRxArray->At( index ).GetStreamAddress();
       
  3134         }
       
  3135     else if ( ( iStreamTxArray->Find( match, magicKey, index ) == KErrNone )
       
  3136               && ( index >= 0 ) )
       
  3137         {
       
  3138         aStreamAddress = iStreamTxArray->At( index ).GetStreamAddress();
       
  3139         }
       
  3140     else
       
  3141         {
       
  3142         return KErrNotFound;
       
  3143         }
       
  3144 
       
  3145     return KErrNone;
       
  3146     }
       
  3147 
       
  3148 // ---------------------------------------------------------------------------
       
  3149 // TInt CRtpSession::FindStream()
       
  3150 // Find Stream Object in array
       
  3151 // ---------------------------------------------------------------------------
       
  3152 //
       
  3153 TInt CRtpSession::FindStream( const TRtpId aMagicKey, TUint& aStreamAddress, 
       
  3154                               TStreamType& aStreamType )
       
  3155     {
       
  3156     TKeyArrayFix magicKey( TStream::iOffset, ECmpTUint );
       
  3157     TUint temp = 0;
       
  3158     TInt index = -1;
       
  3159 
       
  3160     TStream match( aMagicKey, temp );
       
  3161     if ( ( iStreamRxArray->Find( match, magicKey, index ) == KErrNone )
       
  3162          && ( index >= 0 ) )
       
  3163         {
       
  3164         aStreamAddress = iStreamRxArray->At( index ).GetStreamAddress();
       
  3165         aStreamType = ERxStream;
       
  3166         }
       
  3167     else if ( ( iStreamTxArray->Find( match, magicKey, index ) == KErrNone )
       
  3168                && ( index >= 0 ) )
       
  3169         {
       
  3170         aStreamAddress = iStreamTxArray->At( index ).GetStreamAddress();
       
  3171         aStreamType = ETxStream;
       
  3172         }
       
  3173     else
       
  3174         {
       
  3175         return KErrNotFound;
       
  3176         }
       
  3177 
       
  3178     return KErrNone;
       
  3179     }
       
  3180 
       
  3181 // ---------------------------------------------------------------------------
       
  3182 // TInt CRtpSession::RemoveStream()
       
  3183 // Delete Stream object from array
       
  3184 // ---------------------------------------------------------------------------
       
  3185 //
       
  3186 TInt CRtpSession::RemoveStream( const TRtpId aMagicKey,
       
  3187                                 TStreamType& aStreamType )
       
  3188     {
       
  3189     TKeyArrayFix magicKey( TStream::iOffset, ECmpTUint );
       
  3190     TInt index = -1;
       
  3191 
       
  3192     TStream match( aMagicKey );
       
  3193     if ( ( iStreamRxArray->Find( match, magicKey, index ) == KErrNone )
       
  3194          && ( index >= 0 ) )
       
  3195         {
       
  3196         
       
  3197         RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: Remove Stream ID = ", 
       
  3198                   iStreamRxArray->At( index ).GetMagicKey() );
       
  3199         
       
  3200         
       
  3201         CRtpRecvStream* tempStream = reinterpret_cast<CRtpRecvStream*>(
       
  3202             ( iStreamRxArray->At( index ).GetStreamAddress() ) );
       
  3203  
       
  3204         
       
  3205         aStreamType = ERxStream;
       
  3206         delete tempStream;
       
  3207         iStreamRxArray->Delete( index );
       
  3208         iStreamRxArray->Compress();
       
  3209         }
       
  3210     else if ( ( iStreamTxArray->Find( match, magicKey, index ) == KErrNone )
       
  3211               && ( index >= 0 ) )
       
  3212         {
       
  3213         
       
  3214         RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: Remove Stream ID = ", 
       
  3215                   iStreamTxArray->At( index ).GetMagicKey() );
       
  3216         
       
  3217         
       
  3218         CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>(
       
  3219             ( iStreamTxArray->At( index ).GetStreamAddress() ) );
       
  3220         aStreamType = ETxStream;
       
  3221         delete tempStream;
       
  3222         iStreamTxArray->Delete( index );
       
  3223         iStreamTxArray->Compress();
       
  3224         }
       
  3225     else
       
  3226         {
       
  3227         return KErrNotFound;
       
  3228         }
       
  3229 
       
  3230     return KErrNone;
       
  3231     }
       
  3232 
       
  3233 // ---------------------------------------------------------------------------
       
  3234 // CRtpSession::RemoveAllStreams()
       
  3235 // Delete all stream objects from array
       
  3236 // ---------------------------------------------------------------------------
       
  3237 //
       
  3238 void CRtpSession::RemoveAllStreams()
       
  3239     {
       
  3240     if ( iStreamRxArray )
       
  3241 		{
       
  3242 		TInt totalRxStream( iStreamRxArray->Count() );
       
  3243 	   
       
  3244         //Delete all RX Streams starting with the last one
       
  3245         while( totalRxStream > 0 )
       
  3246             {
       
  3247             //totalRxStream - 1 is the last element in the array
       
  3248             
       
  3249             RTP_DEBUG_DETAIL_DVALUE("RX STREAM ARRAY: Remove ALL RX Stream ID = ",
       
  3250                       iStreamRxArray->At( totalRxStream - 1 ).GetMagicKey() );
       
  3251             
       
  3252             
       
  3253             CRtpRecvStream* tempStream = reinterpret_cast<CRtpRecvStream*>(
       
  3254                 ( iStreamRxArray->At( totalRxStream - 1 ).GetStreamAddress() ) );
       
  3255             delete tempStream;
       
  3256             
       
  3257             iStreamRxArray->Delete( totalRxStream - 1 );
       
  3258             totalRxStream = iStreamRxArray->Count();
       
  3259             }
       
  3260             
       
  3261         iStreamRxArray->Reset();   
       
  3262         iStreamRxArray->Compress();
       
  3263    	  	}
       
  3264 
       
  3265 	if ( iStreamTxArray )
       
  3266 		{
       
  3267 		TInt totalTxStream( iStreamTxArray->Count() );
       
  3268 	   
       
  3269 	    //Delete all TX Streams starting with the last one 
       
  3270         while ( totalTxStream > 0 )
       
  3271             {
       
  3272             //totalTxStream - 1 is the last element in the array
       
  3273             
       
  3274             RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: Remove ALL TX Stream ID = ", 
       
  3275                       iStreamTxArray->At( totalTxStream - 1 ).GetMagicKey() );
       
  3276             
       
  3277             
       
  3278             CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>(
       
  3279                 ( iStreamTxArray->At( totalTxStream - 1 ).GetStreamAddress() ) );
       
  3280             delete tempStream;
       
  3281             
       
  3282             iStreamTxArray->Delete( totalTxStream - 1 );
       
  3283             totalTxStream = iStreamTxArray->Count();
       
  3284             }
       
  3285              
       
  3286         iStreamTxArray->Reset();   
       
  3287         iStreamTxArray->Compress();
       
  3288 	    }
       
  3289     }
       
  3290     
       
  3291 // ---------------------------------------------------------------------------
       
  3292 // TInt CRtpSession::FindRtpRxStreamSSRC()
       
  3293 // Find a RX stream that matches RTP packet SSRC
       
  3294 // ---------------------------------------------------------------------------
       
  3295 //
       
  3296 void CRtpSession::FindRtpRxStreamSSRC(TRtpSSRC& aSSRC)
       
  3297     {
       
  3298     aSSRC=0;
       
  3299     TInt totalStream = iStreamRxArray->Count();
       
  3300 
       
  3301     if ( totalStream > 0 )
       
  3302         {
       
  3303         for ( TInt index = 0; index < totalStream; index++ )
       
  3304             {
       
  3305             CRtpRecvStream* tempStream = ( CRtpRecvStream* )
       
  3306                 ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  3307             
       
  3308             iPktRcv->RtpPacketResetPtr();
       
  3309 
       
  3310         	TRtpSSRC aRemoteSSRC( NULL );
       
  3311     	    iPktRcv->SetType( ERTP );
       
  3312         	aRemoteSSRC = iPktRcv->RtpPacketGetSSRC();
       
  3313         	//first if the stream has been assigned else assign it with this packetSSRC
       
  3314         	if ( tempStream->GetRemoteSSRC() == aRemoteSSRC )
       
  3315             	{
       
  3316             	aSSRC=aRemoteSSRC;
       
  3317             	
       
  3318             	}
       
  3319             }
       
  3320         if (aSSRC==0)
       
  3321         	{
       
  3322         	for ( TInt index = 0; index < totalStream; index++ )
       
  3323             	{
       
  3324             	CRtpRecvStream* tempStream = ( CRtpRecvStream* )
       
  3325                 	( iStreamRxArray->At( index ).GetStreamAddress() );
       
  3326             
       
  3327         		//find stream with emptly ssrc
       
  3328         		if ( tempStream->GetRemoteSSRC()==NULL )
       
  3329         			{
       
  3330 	        		iPktRcv->RtpPacketResetPtr();
       
  3331 	        		aSSRC = iPktRcv->RtpPacketGetSSRC();
       
  3332 	        		}
       
  3333         		}
       
  3334         	}
       
  3335         }
       
  3336      
       
  3337     }
       
  3338 // ---------------------------------------------------------------------------
       
  3339 // TInt CRtpSession::FindRtpRxStreamL()
       
  3340 // Find a RX stream that matches RTP packet SSRC
       
  3341 // ---------------------------------------------------------------------------
       
  3342 //
       
  3343 TInt CRtpSession::FindRtpRxStreamL()
       
  3344     {
       
  3345     TInt ret = KErrNotFound;
       
  3346     TBool assignRcvStream = EFalse;
       
  3347     TInt totalStream = iStreamRxArray->Count();
       
  3348 
       
  3349     if ( totalStream > 0 )
       
  3350         {
       
  3351         for ( TInt index = 0; index < totalStream; index++ )
       
  3352             {
       
  3353             CRtpRecvStream* tempStream = ( CRtpRecvStream* )
       
  3354                 ( iStreamRxArray->At( index ).GetStreamAddress() );
       
  3355             ret = tempStream->RtpStreamProcessRtpPacketL( iPktRcv,
       
  3356                                                          assignRcvStream );
       
  3357             if ( ret == KErrNone )
       
  3358                 {
       
  3359                 
       
  3360                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: FOUND RX (RTP) stream ID = ",
       
  3361                           tempStream->GetStreamID() );
       
  3362                 
       
  3363                 
       
  3364                 return ret;
       
  3365                 }
       
  3366             }
       
  3367         }
       
  3368     return ret;
       
  3369     }
       
  3370 
       
  3371 // ---------------------------------------------------------------------------
       
  3372 // TInt CRtpSession::AssignRtpRxStreamL()
       
  3373 // Assigns a RX stream to the RTP packet SSRC
       
  3374 // ---------------------------------------------------------------------------
       
  3375 //
       
  3376 TInt CRtpSession::AssignRtpRxStreamL()
       
  3377     {
       
  3378     RTP_DEBUG_DETAIL( "CRtpSession::AssignRtpRxStream Entry" );
       
  3379     TInt index;
       
  3380     TInt ret = KErrNotFound;
       
  3381     TBool assignRcvStream = ETrue;
       
  3382     TInt totalStream = iStreamRxArray->Count();
       
  3383 
       
  3384     if ( totalStream > 0 )
       
  3385         {
       
  3386         for ( index = 0; index < totalStream; index++ )
       
  3387             {
       
  3388             
       
  3389             RTP_DEBUG_DETAIL_DVALUE(
       
  3390                       "RX STREAM ARRAY: Assigning RTP to RX stream ID = ",
       
  3391                       iStreamRxArray->At( index ).GetMagicKey() );
       
  3392             
       
  3393             
       
  3394             CRtpRecvStream* tempStream = reinterpret_cast<CRtpRecvStream*>(
       
  3395                 ( iStreamRxArray->At( index ).GetStreamAddress() ) );
       
  3396             ret = tempStream->RtpStreamProcessRtpPacketL( iPktRcv,
       
  3397                                                          assignRcvStream );
       
  3398             if ( ret == KErrNone )
       
  3399                 {
       
  3400                 
       
  3401                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: ASSIGNED RX stream SSRC = ",
       
  3402                           tempStream->GetLocalSSRC() );
       
  3403                 
       
  3404 				RTP_DEBUG_DETAIL( "CRtpSession::AssignRtpRxStream Exit" );
       
  3405                 return ret;
       
  3406                 }
       
  3407             }
       
  3408         }
       
  3409     RTP_DEBUG_DETAIL( "no stream in RTP Sesssion" );
       
  3410         
       
  3411     RTP_DEBUG_DETAIL( "CRtpSession::AssignRtpRxStream Exit" );
       
  3412         
       
  3413     return ret;
       
  3414     }
       
  3415 
       
  3416 // ---------------------------------------------------------------------------
       
  3417 // TInt CRtpSession::MatchSSRCToStream()
       
  3418 // finds a stream for a given source SSRC
       
  3419 // ---------------------------------------------------------------------------
       
  3420 //
       
  3421 TInt CRtpSession::MatchSSRCToStream( TUint& aStreamAddress, 
       
  3422                                      TRtpSSRC aSSRC, 
       
  3423                                      TStreamType& aStreamType )
       
  3424     {
       
  3425     TInt index;
       
  3426     TInt totalRxStream = iStreamRxArray->Count();
       
  3427     TInt totalTxStream = iStreamTxArray->Count();
       
  3428 
       
  3429     if ( totalRxStream > 0 )
       
  3430         {
       
  3431         for ( index = 0; index < totalRxStream; index++ )
       
  3432             {
       
  3433             TUint streamAddr( iStreamRxArray->At( index ).GetStreamAddress() );
       
  3434             CRtpRecvStream* tempStream =
       
  3435                 reinterpret_cast<CRtpRecvStream*>( streamAddr );
       
  3436             if ( tempStream->GetRemoteSSRC() == aSSRC )
       
  3437                 {
       
  3438                 
       
  3439                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: MATCHED RX stream ID = ",
       
  3440                           tempStream->GetStreamID() );
       
  3441                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: MATCHED RX stream Key = ",
       
  3442                           iStreamRxArray->At( index ).GetMagicKey() );
       
  3443                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: MATCHED RX stream SSRC = ",
       
  3444                           tempStream->GetLocalSSRC() );
       
  3445                 
       
  3446                 
       
  3447                 aStreamAddress = streamAddr;
       
  3448                 aStreamType = ERxStream;
       
  3449                 return KErrNone;
       
  3450                 }
       
  3451             }
       
  3452         }
       
  3453 
       
  3454     if ( totalTxStream > 0 )
       
  3455         {
       
  3456         for ( index = 0; index < totalTxStream; index++ )
       
  3457             {
       
  3458             TUint streamAddr( iStreamTxArray->At( index ).GetStreamAddress() );
       
  3459             CRtpTranStream* tempStream =
       
  3460                 reinterpret_cast<CRtpTranStream*>( streamAddr );
       
  3461             if ( tempStream->GetLocalSSRC() == aSSRC )
       
  3462                 {
       
  3463                 
       
  3464                 RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: MATCHED TX stream ID = ",
       
  3465                           tempStream->GetStreamID() );
       
  3466                 RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: MATCHED TX stream Key = ",
       
  3467                           iStreamTxArray->At( index ).GetMagicKey() );
       
  3468                 RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: MATCHED TX stream SSRC = ",
       
  3469                           tempStream->GetLocalSSRC() );
       
  3470                 
       
  3471                 
       
  3472                 aStreamAddress = streamAddr;
       
  3473                 aStreamType = ETxStream;
       
  3474                 return KErrNone;
       
  3475                 }
       
  3476             }
       
  3477         }
       
  3478 
       
  3479     return  KErrNotFound;
       
  3480     }
       
  3481 
       
  3482 // ---------------------------------------------------------------------------
       
  3483 // TBool CRtpSession::FindStreamForSSRC()
       
  3484 // Finds if a send stream is already using the given SSRC
       
  3485 // ---------------------------------------------------------------------------
       
  3486 //
       
  3487 TBool CRtpSession::FindStreamForSSRC( TRtpSSRC aSSRC )
       
  3488     {
       
  3489     TInt index;
       
  3490     TInt totalRxStream = iStreamRxArray->Count();
       
  3491     TInt totalTxStream = iStreamTxArray->Count();
       
  3492 
       
  3493     if ( totalRxStream > 0 )
       
  3494         {
       
  3495         for ( index = 0; index < totalRxStream; index++ )
       
  3496             {
       
  3497             CRtpRecvStream* tempStream = reinterpret_cast<CRtpRecvStream*>(
       
  3498                 iStreamRxArray->At( index ).GetStreamAddress() );
       
  3499             if ( tempStream->GetLocalSSRC() == aSSRC )
       
  3500                 {
       
  3501                 
       
  3502                 RTP_DEBUG_DETAIL_DVALUE( "RX STREAM ARRAY: SSRC Being Used = ", aSSRC );
       
  3503                 
       
  3504                 
       
  3505                 return ETrue;
       
  3506                 }
       
  3507             }
       
  3508         }
       
  3509 
       
  3510     if ( totalTxStream > 0 )
       
  3511         {
       
  3512         for ( index = 0; index < totalTxStream; index++ )
       
  3513             {
       
  3514             CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>(
       
  3515                 iStreamTxArray->At( index ).GetStreamAddress() );
       
  3516             if ( tempStream->GetLocalSSRC() == aSSRC )
       
  3517                 {
       
  3518                RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: SSRC Being Used = ", aSSRC );
       
  3519                 
       
  3520                 
       
  3521                 return ETrue;
       
  3522                 }
       
  3523             }
       
  3524         }
       
  3525     return EFalse;
       
  3526     }
       
  3527 
       
  3528 // ---------------------------------------------------------------------------
       
  3529 // TBool CRtpSession::FindTxStreamForSSRC()
       
  3530 // Finds if a send stream is already using the given SSRC
       
  3531 // ---------------------------------------------------------------------------
       
  3532 //
       
  3533 TBool CRtpSession::FindTxStreamForSSRC( TRtpSSRC aSSRC,
       
  3534                                         CRtpTranStream** streamPointerPointer )
       
  3535     {
       
  3536     TInt index( 0 );
       
  3537     TInt totalTxStream( iStreamTxArray->Count() );
       
  3538 
       
  3539     if ( totalTxStream > 0 )
       
  3540         {
       
  3541         for ( index = 0; index < totalTxStream; index++ )
       
  3542             {
       
  3543             CRtpTranStream* tempStream = reinterpret_cast<CRtpTranStream*>(
       
  3544                 iStreamTxArray->At( index ).GetStreamAddress() );
       
  3545             if ( tempStream->GetLocalSSRC() == aSSRC )
       
  3546                 {
       
  3547                 RTP_DEBUG_DETAIL_DVALUE( "TX STREAM ARRAY: SSRC Being Used = ", aSSRC );
       
  3548                 
       
  3549                 if ( streamPointerPointer )
       
  3550                     {
       
  3551                     // put the address of the CRtpTranStream object into the
       
  3552                     // specified pointer
       
  3553                     *streamPointerPointer = tempStream;
       
  3554                     }
       
  3555                 return ETrue;
       
  3556                 }
       
  3557             }
       
  3558         }
       
  3559 
       
  3560     return EFalse;
       
  3561     }
       
  3562 
       
  3563 // ---------------------------------------------------------------------------
       
  3564 // TBool CRtpSession::RcvStreamActive()
       
  3565 // checks if a receive stream is active
       
  3566 // ---------------------------------------------------------------------------
       
  3567 //
       
  3568 TBool CRtpSession::RcvStreamActive( TInt aIndex )
       
  3569     {
       
  3570     CRtpRecvStream* tempRcvStream = reinterpret_cast<CRtpRecvStream*>(
       
  3571         iStreamRxArray->At( aIndex ).GetStreamAddress() );
       
  3572     if ( !tempRcvStream->FirstPkg() )
       
  3573         {
       
  3574         return ETrue;
       
  3575         }
       
  3576     else
       
  3577         {
       
  3578         return EFalse;
       
  3579         }
       
  3580     }
       
  3581 
       
  3582 // ---------------------------------------------------------------------------
       
  3583 // TBool CRtpSession::AnyRcvStreamReceivedRtpPacket()
       
  3584 // checks if any receive stream has received an RTP packet
       
  3585 // ---------------------------------------------------------------------------
       
  3586 //
       
  3587 TBool CRtpSession::AnyRcvStreamReceivedRtpPacket()
       
  3588     {
       
  3589     TInt totalRcvStream = iStreamRxArray->Count();
       
  3590     for ( TInt index = 0; index < totalRcvStream; index++ )
       
  3591         {
       
  3592         CRtpRecvStream* tempRcvStream = reinterpret_cast<CRtpRecvStream*>(
       
  3593             iStreamRxArray->At( index ).GetStreamAddress() );
       
  3594         if ( tempRcvStream->ReceivedRTPPackets() )
       
  3595             {
       
  3596             return ETrue;
       
  3597             }
       
  3598         }
       
  3599     return EFalse;
       
  3600     }
       
  3601 
       
  3602 
       
  3603 // ---------------------------------------------------------------------------
       
  3604 // TBool CRtpSession::RcvStreamReceivedRtpPacket()
       
  3605 // checks if a receive stream has received an RTP packet
       
  3606 // ---------------------------------------------------------------------------
       
  3607 //
       
  3608 TBool CRtpSession::RcvStreamReceivedRtpPacket( TInt aIndex )
       
  3609     {
       
  3610     CRtpRecvStream* tempRcvStream = reinterpret_cast<CRtpRecvStream*>(
       
  3611         iStreamRxArray->At( aIndex ).GetStreamAddress() );
       
  3612     if ( tempRcvStream->ReceivedRTPPackets() )
       
  3613         {
       
  3614         return ETrue;
       
  3615         }
       
  3616     else
       
  3617         {
       
  3618         return EFalse;
       
  3619         }
       
  3620     }
       
  3621 
       
  3622 // ---------------------------------------------------------------------------
       
  3623 // CRtpRecvStream* CRtpSession::GetRcvStreamByIndex()
       
  3624 // gets a receive stream based on an index value
       
  3625 // ---------------------------------------------------------------------------
       
  3626 //
       
  3627 CRtpRecvStream* CRtpSession::GetRcvStreamByIndex( TInt aIndex )
       
  3628     {
       
  3629     CRtpRecvStream* tempRcvStream = reinterpret_cast<CRtpRecvStream*>(
       
  3630         iStreamRxArray->At( aIndex ).GetStreamAddress() );
       
  3631     return tempRcvStream;
       
  3632     }
       
  3633 
       
  3634 // ---------------------------------------------------------------------------
       
  3635 // TBool CRtpSession::DoAddSdesToEmptyArrayL()
       
  3636 // Adds a new SDES object to the SDES array
       
  3637 // ---------------------------------------------------------------------------
       
  3638 //
       
  3639 CRtpSDES* CRtpSession::DoAddSdesToArrayL( TRtpPacketIOParam* aExtractParam )
       
  3640     {
       
  3641     if ( !iRtcpEnabled )
       
  3642         {
       
  3643         return NULL;
       
  3644         }
       
  3645 
       
  3646     CRtpSDES* tempSdes = CRtpSDES::NewL();
       
  3647     CleanupStack::PushL( tempSdes );    
       
  3648     // check for empty SDES sections
       
  3649     for ( TInt k = 0; k < ERTCP_NUM_OF_SDES_ITEMS; k++ )
       
  3650         {
       
  3651         if ( ( aExtractParam->TRTCP_SDES.sdesItemsSize[k] ) > 0 )
       
  3652             {
       
  3653             TRtpUtil::Strcpy( tempSdes->iSDESItems[k],
       
  3654                               aExtractParam->TRTCP_SDES.sdesItems[k] );
       
  3655             tempSdes->iSDESItemsSize[k] =
       
  3656                 aExtractParam->TRTCP_SDES.sdesItemsSize[k];
       
  3657             }
       
  3658         }
       
  3659     iSdesArray->AppendL( tempSdes ); 
       
  3660     CleanupStack::Pop( tempSdes );
       
  3661     return tempSdes;
       
  3662     }
       
  3663 
       
  3664 // ---------------------------------------------------------------------------
       
  3665 // TBool CRtpSession::CompareAndUpdateSdes()
       
  3666 // Compares the specified SDES values with the ones in the parameters and
       
  3667 // updates the SDES where the values differ, starting from the specified index
       
  3668 // ---------------------------------------------------------------------------
       
  3669 //
       
  3670 TBool CRtpSession::CompareAndUpdateSdes( CRtpSDES* aTempSdes,
       
  3671                                          TRtpPacketIOParam* aExtractParam,
       
  3672                                          TBool aUpdateCName )
       
  3673     {
       
  3674     TBool updatedSdes( EFalse );
       
  3675     TInt startValue( 0 );
       
  3676     if ( !aUpdateCName )
       
  3677         {
       
  3678         startValue = 1;
       
  3679         }
       
  3680     for ( TInt k = startValue; k < ERTCP_NUM_OF_SDES_ITEMS; k++ )
       
  3681         {
       
  3682         if ( ( aExtractParam->TRTCP_SDES.sdesItemsSize[k] ) > 0 )
       
  3683             {
       
  3684             TPtrC8 s2( _L8( aTempSdes->iSDESItems[k] ) );
       
  3685             if ( s2.Compare( _L8( aExtractParam->TRTCP_SDES.sdesItems[k] ) )
       
  3686                 != 0 )
       
  3687                 {
       
  3688                 TRtpUtil::Strcpy( aTempSdes->iSDESItems[k],
       
  3689                                   aExtractParam->TRTCP_SDES.sdesItems[k] );
       
  3690                 aTempSdes->iSDESItemsSize[k] =
       
  3691                     aExtractParam->TRTCP_SDES.sdesItemsSize[k];
       
  3692                 updatedSdes = ETrue;
       
  3693                 }
       
  3694             }
       
  3695         }
       
  3696     return updatedSdes;
       
  3697     }
       
  3698 
       
  3699 // ---------------------------------------------------------------------------
       
  3700 // TBool CRtpSession::AddSdesToArrayL()
       
  3701 // adds SDES object to SDES array if it is new SDES or updates SDES information
       
  3702 // if it has changed
       
  3703 // ---------------------------------------------------------------------------
       
  3704 //
       
  3705 TBool CRtpSession::AddSdesToArrayL( TUint& aSdesAddress,
       
  3706                                     TRtpPacketIOParam* aExtractParam )
       
  3707     {
       
  3708     if ( !iRtcpEnabled )
       
  3709         {
       
  3710         return EFalse;
       
  3711         }
       
  3712 
       
  3713     TBool fFoundSDES( EFalse );
       
  3714     TBool fUpdatedSDES( EFalse );
       
  3715     CRtpSDES* tempSdes = NULL;
       
  3716     // find if SDES exists or has been changed
       
  3717     TInt totalSdes( iSdesArray->Count() );
       
  3718 
       
  3719     if ( totalSdes == 0 )  
       
  3720         {
       
  3721         // First SDES item, add it to the array
       
  3722         aSdesAddress =
       
  3723             reinterpret_cast<TUint>( DoAddSdesToArrayL( aExtractParam ) );
       
  3724         return ETrue;
       
  3725         }
       
  3726 
       
  3727     // SDES items exist, check if this one needs to be added
       
  3728     TBool bExit = EFalse;
       
  3729 
       
  3730     for ( TInt index = 0; index < totalSdes && !bExit; index++ )
       
  3731         {
       
  3732         tempSdes = iSdesArray->At( index );
       
  3733         // assuming CNAME is always included and does not change, 
       
  3734         // then a new SDES will have a different CNAME
       
  3735             
       
  3736         if ( ( aExtractParam->TRTCP_SDES.
       
  3737                sdesItemsSize[ERTCP_SDES_CNAME] ) == 0 )
       
  3738             {
       
  3739             return EFalse;
       
  3740             }
       
  3741  
       
  3742         TPtrC8 s1( _L8( tempSdes->iSDESItems[ERTCP_SDES_CNAME] ) );
       
  3743         if ( s1.Compare( _L8( aExtractParam->TRTCP_SDES.
       
  3744              sdesItems[ERTCP_SDES_CNAME] ) ) == 0 )
       
  3745             {
       
  3746             fFoundSDES = ETrue;
       
  3747             bExit = ETrue;
       
  3748             // Update SDES, but exclude CName 
       
  3749             fUpdatedSDES = CompareAndUpdateSdes( tempSdes, aExtractParam,
       
  3750                                                  EFalse );
       
  3751             }
       
  3752         }
       
  3753         
       
  3754     if ( !fFoundSDES ) // new SDES, must add it to the array
       
  3755         {
       
  3756         aSdesAddress =
       
  3757             reinterpret_cast<TUint>( DoAddSdesToArrayL( aExtractParam ) );
       
  3758         return ETrue;
       
  3759         }
       
  3760     else if ( fUpdatedSDES )
       
  3761         {
       
  3762         aSdesAddress = reinterpret_cast<TUint>( tempSdes );
       
  3763         return EFalse;
       
  3764         }
       
  3765     else
       
  3766         {
       
  3767         return EFalse;
       
  3768         }
       
  3769     } 
       
  3770 
       
  3771 // ---------------------------------------------------------------------------
       
  3772 // TInt CRtpSession::RemoveSdesFromArray()
       
  3773 // Removes SDES object from SDES array 
       
  3774 // ---------------------------------------------------------------------------
       
  3775 //
       
  3776 TInt CRtpSession::RemoveSdesFromArray( TRtpSSRC aSSRC )
       
  3777     {
       
  3778     if ( !iRtcpEnabled )
       
  3779         {
       
  3780         return KErrGeneral;
       
  3781         }
       
  3782 
       
  3783     TInt totalSdes( iSdesArray->Count() );
       
  3784 
       
  3785     for ( TInt index = 0; index < totalSdes; index++ )
       
  3786         {
       
  3787         CRtpSDES* tempSdes = iSdesArray->At( index ); 
       
  3788         if ( tempSdes->GetSdesSsrc( aSSRC ) )
       
  3789             {
       
  3790             delete tempSdes;
       
  3791             iSdesArray->Delete( index );
       
  3792             iSdesArray->Compress();
       
  3793             
       
  3794             
       
  3795             RTCP_DEBUG_DETAIL_DVALUE( "SDES ARRAY: Remove SDES Info for SSRC = ", aSSRC );
       
  3796             
       
  3797             
       
  3798             return KErrNone;
       
  3799             }
       
  3800         }
       
  3801     return KErrNotFound;
       
  3802     }
       
  3803 
       
  3804 // ---------------------------------------------------------------------------
       
  3805 // CRtpSession::NextRtcpInterval()
       
  3806 // calculates new RTCP interval which depends on number of
       
  3807 // sender and receiver in session (see RFC 1889 for description)
       
  3808 // ---------------------------------------------------------------------------
       
  3809 //
       
  3810 void CRtpSession::NextRtcpInterval()
       
  3811     {
       
  3812     if ( !iRtcpEnabled )
       
  3813         {
       
  3814         return;
       
  3815         }
       
  3816 
       
  3817     TUint senders( 0 );
       
  3818     TUint rtcpBandwidth( static_cast<TUint>( iBandWidth * iFraction ) );
       
  3819     TUint members( iTotalParticipantsSession );
       
  3820     
       
  3821     
       
  3822     RTCP_DEBUG_DETAIL_DVALUE( "CRtpSession::NextRtcpInterval - total participants  = ", 
       
  3823               iTotalParticipantsSession );
       
  3824     
       
  3825     
       
  3826     senders = iNumOfRxStreams + iNumOfTxStreams;
       
  3827 
       
  3828     if ( senders > 0 && senders < members * KRtpDefaultSenderBWFrac )
       
  3829         {
       
  3830         if ( iNumOfTxStreams )
       
  3831             {
       
  3832             rtcpBandwidth = static_cast<TUint>
       
  3833                             ( rtcpBandwidth * KRtpDefaultSenderBWFrac );
       
  3834             members = senders;
       
  3835             }
       
  3836         else
       
  3837             {
       
  3838             rtcpBandwidth = static_cast<TUint>
       
  3839                             ( rtcpBandwidth * KRtpDefaultObserverBWFrac );
       
  3840             members -= senders;
       
  3841             }
       
  3842         }
       
  3843 
       
  3844     
       
  3845     RTCP_DEBUG_DETAIL_DVALUE( "CRtpSession::NextRtcpInterval - members  = ", 
       
  3846               members );
       
  3847 
       
  3848     RTCP_DEBUG_DETAIL_DVALUE( "CRtpSession::NextRtcpInterval - rtcpBandwidth  = ", 
       
  3849               rtcpBandwidth );
       
  3850     
       
  3851     
       
  3852     // 1000 - milliseconds in one second
       
  3853     // 8 - bits in one byte
       
  3854     iRtcpTimeInterval = static_cast<TUint>( ( 1000 * iAverageRtcpSize * 8 * members )
       
  3855                         / rtcpBandwidth );
       
  3856     if ( iRtcpTimeInterval < KMinRtcpTimeInterval )
       
  3857         {
       
  3858         iRtcpTimeInterval = KMinRtcpTimeInterval;
       
  3859         }
       
  3860     TReal rtcpTimeInterval64 = iRtcpTimeInterval * ( Random64() + 0.5 );
       
  3861     iRtcpTimeInterval = static_cast<TUint>( rtcpTimeInterval64 );
       
  3862     iRtcpTimeInterval = static_cast<TUint>( iRtcpTimeInterval / KCompensationRtcp );
       
  3863 
       
  3864     
       
  3865     RTCP_DEBUG_DETAIL_DVALUE( "CRtpSession::NextRtcpInterval - RTCP Interval (ms) = ",
       
  3866               iRtcpTimeInterval );
       
  3867     
       
  3868     }
       
  3869 
       
  3870 // ---------------------------------------------------------------------------
       
  3871 // CRtpSession::AverageRtcpSize()
       
  3872 // updates average RTCP report size
       
  3873 // ---------------------------------------------------------------------------
       
  3874 //
       
  3875 void CRtpSession::AverageRtcpSize( TUint aRtcpReportSize )
       
  3876     {
       
  3877     if ( !iRtcpEnabled )
       
  3878         {
       
  3879         return;
       
  3880         }
       
  3881 
       
  3882     iAverageRtcpSize = ( aRtcpReportSize ) * KRtcpSizeGain + 
       
  3883                        ( iAverageRtcpSize * KRtcpSizeGainRemainder );
       
  3884 
       
  3885     
       
  3886     RTCP_DEBUG_DETAIL_DVALUE( "RTCP: Actual RTCP report size ", aRtcpReportSize );
       
  3887     RTCP_DEBUG_DETAIL_DVALUE( "RTCP: Average RTCP report size ", (TInt) iAverageRtcpSize );
       
  3888     
       
  3889     }
       
  3890 
       
  3891 // ---------------------------------------------------------------------------
       
  3892 // CRtpSession::CheckRemoteSsrcL()
       
  3893 // SSRC collision handling
       
  3894 // ---------------------------------------------------------------------------
       
  3895 //
       
  3896 TInt CRtpSession::CheckRemoteSsrcL( TRtpSSRC& aSSRC )
       
  3897     {
       
  3898     CRtpTranStream* tranStream;
       
  3899     TBuf8<15> buf;
       
  3900     TRtpSSRC ssrc( 0 );
       
  3901     
       
  3902     // In case of collision, we change SSRC only once (RFC3550)
       
  3903     // So if we already jumped once, this function does nothing.
       
  3904     if ( iSSRCJumps > 0 )
       
  3905         {
       
  3906         return KErrNone;
       
  3907         }
       
  3908         
       
  3909     if ( FindTxStreamForSSRC( aSSRC, &tranStream ) )
       
  3910         {
       
  3911         // One of our Tx streams uses an SSRC value that collides with the
       
  3912         // specified value.
       
  3913         
       
  3914         RTCP_DEBUG_DETAIL( "RTCP: Jumping to new SSRC" );
       
  3915         
       
  3916                         
       
  3917         // Send a BYE packet for this SSRC
       
  3918         buf.Format( _L8( "SSRC collision" ) );
       
  3919         buf.ZeroTerminate();
       
  3920         ( void ) SendRtcpByePacketL( tranStream->GetStreamID(), buf );
       
  3921         
       
  3922         // Find a new unoccupied SSRC
       
  3923         ssrc = GetUniqueSSRC();
       
  3924         
       
  3925         // Take the new SSRC into use
       
  3926         tranStream->SetLocalSSRC( ssrc );
       
  3927         if ( iDefaultSSRC == aSSRC )
       
  3928             {
       
  3929             iDefaultSSRC = ssrc;
       
  3930             }
       
  3931         // Also let the receive stream know the new SSRC
       
  3932         aSSRC = ssrc;
       
  3933 
       
  3934         // Increment jump counter
       
  3935         iSSRCJumps++;
       
  3936         return 1; // a value larger than 0 indicates that the SSRC changed
       
  3937         }
       
  3938     return KErrNone;
       
  3939     }
       
  3940 
       
  3941 // ---------------------------------------------------------------------------
       
  3942 // CRtpSession::Random()
       
  3943 // Get a random 32-bit number
       
  3944 // ---------------------------------------------------------------------------
       
  3945 //
       
  3946 TInt CRtpSession::Random()
       
  3947     {
       
  3948     if ( iSeed == 0 )
       
  3949         {
       
  3950         TTime tmp_time;
       
  3951         tmp_time.HomeTime();
       
  3952         iSeed = tmp_time.Int64();
       
  3953         }
       
  3954     return TRtpUtil::Random( iSeed );
       
  3955     }
       
  3956 
       
  3957 // ---------------------------------------------------------------------------
       
  3958 // CRtpSession::Random64()
       
  3959 // Get a random 64-bit number
       
  3960 // ---------------------------------------------------------------------------
       
  3961 //
       
  3962 TReal CRtpSession::Random64()
       
  3963     {
       
  3964     if ( iSeed == 0 )
       
  3965         {
       
  3966         TTime tmp_time;
       
  3967         tmp_time.HomeTime();
       
  3968         iSeed = tmp_time.Int64();
       
  3969         }
       
  3970     return TRtpUtil::FloatRandom( iSeed );
       
  3971     }
       
  3972     
       
  3973 // ---------------------------------------------------------------------------
       
  3974 // CRtpSession::SSRC()
       
  3975 // Get Session default ssrc
       
  3976 // ---------------------------------------------------------------------------
       
  3977 //
       
  3978 TRtpSSRC CRtpSession::SSRC() const
       
  3979     {
       
  3980     return iDefaultSSRC;
       
  3981     };
       
  3982 
       
  3983 // ---------------------------------------------------------------------------
       
  3984 // CRtpSession::GetSessionId()
       
  3985 // Get Session ID
       
  3986 // ---------------------------------------------------------------------------
       
  3987 //
       
  3988 TRtpId CRtpSession::GetSessionId()
       
  3989     {
       
  3990     return iSessionId;
       
  3991     }
       
  3992 
       
  3993 // ---------------------------------------------------------------------------
       
  3994 // CRtpSession::SendRTCPByewithBanedSSRCL()
       
  3995 // 
       
  3996 // ---------------------------------------------------------------------------
       
  3997 //
       
  3998 void CRtpSession::SendRTCPByewithBanedSSRCL()
       
  3999 	{
       
  4000 	TInt count = iStreamTxArray->Count();    
       
  4001 
       
  4002 	for ( TInt index( 0 ); index < count; index++ )
       
  4003 		{
       
  4004 	 
       
  4005 		TBuf8<15> buf;    
       
  4006 		CRtpTranStream* tempStream = ( CRtpTranStream* )
       
  4007  		( iStreamTxArray->At( index ).GetStreamAddress() );
       
  4008  		//when sending RTCP report will use the localSSRC 
       
  4009  		//here we want to sendRTCPBye with specific SSRC which baned
       
  4010  		//so set back the original value at the end
       
  4011  		TRtpSSRC originSSRC= tempStream->GetLocalSSRC();
       
  4012  		tempStream->SetLocalSSRC(iPktRcv->RtpPacketGetSSRC());
       
  4013 		buf.Format( _L8( "SSRC baned" ) );
       
  4014 		buf.ZeroTerminate();
       
  4015 		( void ) SendRtcpByePacketL( tempStream->GetStreamID(), buf );
       
  4016 		tempStream->SetLocalSSRC(originSSRC);
       
  4017 		}	
       
  4018 	}
       
  4019 
       
  4020 // ---------------------------------------------------------------------------
       
  4021 // CRtpSession::IsSrtp()
       
  4022 // 
       
  4023 // ---------------------------------------------------------------------------
       
  4024 //
       
  4025 TBool CRtpSession::IsSrtp()
       
  4026 	{
       
  4027 	return iIsSrtp;
       
  4028 	}
       
  4029 // ---------------------------------------------------------------------------
       
  4030 // CRtpSession::SendSRTCPReport(TRtpSSRC aSSRC)
       
  4031 // virtual function
       
  4032 // ---------------------------------------------------------------------------
       
  4033 //	
       
  4034 void CRtpSession::SendSRTCPReportL(TRtpSSRC /*aSSRC*/)
       
  4035 	{
       
  4036 	//should not called base class
       
  4037 	User::Leave(KErrNotSupported);
       
  4038 	}
       
  4039 
       
  4040 // ---------------------------------------------------------------------------
       
  4041 // CRtpSession::CheckifBanded(TRtpSSRC aSSRC)
       
  4042 // 
       
  4043 // ---------------------------------------------------------------------------
       
  4044 //	
       
  4045 TBool CRtpSession::CheckifBanded()
       
  4046 	{
       
  4047 	TBool idFound = EFalse;
       
  4048 	RTP_DEBUG_DETAIL( "CRtpSession::CheckifBanded Entry" );
       
  4049 	RTP_DEBUG_DETAIL_DVALUE( "iPktRcv->RtpPacketGetSSRC() =", iPktRcv->RtpPacketGetSSRC() ); 
       
  4050 	for ( TInt i=0; ( i<iRxSSRCArray.Count() && !idFound ); i++)
       
  4051 		{
       
  4052 		if( iRxSSRCArray[i] == iPktRcv->RtpPacketGetSSRC())
       
  4053 			{
       
  4054 			idFound = ETrue;
       
  4055 			}
       
  4056 		}
       
  4057 	//baned the packet send RTCP bye
       
  4058 	// Originally it sent here but now it has to be removed 
       
  4059     // in case of RTCP Bye packet flooding causing by intrusion
       
  4060     // also in RFC, it is not required.
       
  4061 	RTP_DEBUG_DETAIL( "CRtpSession::CheckifBanded Exit" );
       
  4062 	return idFound;    
       
  4063 	}
       
  4064 
       
  4065 // ---------------------------------------------------------------------------
       
  4066 // CRtpSession::CheckRemoteAddr()
       
  4067 // 
       
  4068 // ---------------------------------------------------------------------------
       
  4069 //	
       
  4070 TBool CRtpSession::CheckRemoteAddr()
       
  4071 	{
       
  4072 	TBool isSetRemoteAddr=iCommNet->iReceiver[ERTPPort]->IsSetRemoteAdress();
       
  4073 	return isSetRemoteAddr;
       
  4074 	}
       
  4075 
       
  4076 // ---------------------------------------------------------------------------
       
  4077 // CRtpSession::CreateRecvStream(TRtpSSRC aSSRC)
       
  4078 // 
       
  4079 // ---------------------------------------------------------------------------
       
  4080 //		
       
  4081 void CRtpSession::CreateNewRecvStreamL()
       
  4082 	{
       
  4083 	//create new rcvstream
       
  4084 	//Related to RFC 3550 8.2
       
  4085      
       
  4086 	RTP_DEBUG_DETAIL( "CRtpSession::CreateNewRecvStream entry" );
       
  4087 
       
  4088 	if(iCommNet->iReceiver[ERTPPort]->IsSetRemoteAdress())   
       
  4089 		{
       
  4090 		RTP_DEBUG_DETAIL( "Packet comes from set remote address" );
       
  4091 
       
  4092 		if (iNumOfRxStreams<= KRtpLimitNumberOfRecvStream)
       
  4093 			{
       
  4094 			TRtpId newStreamId= iAssignUniqueID.AssignUniqueID();      
       
  4095 			if (!iAssignUniqueID.AddStreamToSession(iSessionId, newStreamId))
       
  4096 				{
       
  4097 				// It is valid RTP packet, not checking if stream created
       
  4098 				TRAPD( err, CreateReceiveStreamL( newStreamId, iPktRcv->RtpPacketGetPayloadType() ));
       
  4099 		   		if ( err != KErrNone )
       
  4100 		   			{
       
  4101 		   			RTP_DEBUG_DETAIL( "error occured when tried to create Recv Stream" );
       
  4102 		   			return;
       
  4103 		   			}
       
  4104 		   		RTP_DEBUG_DETAIL( "Assign new Rx stream" );	
       
  4105 		   		AssignRtpRxStreamL();
       
  4106 		   		}
       
  4107 			}
       
  4108 		//Can not create the Recv once it runs to the limit 
       
  4109 		//This is in case of device running out of memory
       
  4110 		RTP_DEBUG_DETAIL( "Can not create new recv stream!" );
       
  4111 		RTP_DEBUG_DETAIL( "The number of Recv streams is over 100" );
       
  4112 		}	
       
  4113 		
       
  4114 	else
       
  4115 		{
       
  4116 		//baned
       
  4117 		RTP_DEBUG_DETAIL( "Band SSRC" );
       
  4118 		iPktRcv->SetType( ERTP );
       
  4119         TRtpSSRC remoteSSRC( NULL );
       
  4120         remoteSSRC = iPktRcv->RtpPacketGetSSRC();
       
  4121         //Add to baned SSRC list
       
  4122         if (remoteSSRC )
       
  4123         	{
       
  4124         	if (!CheckifBanded())
       
  4125 	        	{
       
  4126 	        	//In the future it might also checked with SDES or CSRC
       
  4127 	        	TRAPD( err, iRxSSRCArray.Append(remoteSSRC) ) ;
       
  4128 	        	if ( err != KErrNone )
       
  4129 	        		{
       
  4130 	        		RTP_DEBUG_DETAIL( "Error ocured where to append banded ssrc" );
       
  4131 	        		return;
       
  4132 	        		}
       
  4133 	        	RTP_DEBUG_DETAIL_DVALUE( "Band ssrc =", remoteSSRC );		
       
  4134 	        	SendRTCPByewithBanedSSRCL();
       
  4135 	        	}
       
  4136 	        //abort packet	
       
  4137         	}
       
  4138 		//abort packet
       
  4139 		}
       
  4140 	RTP_DEBUG_DETAIL( "CRtpSession::CreateNewRecvStream exit" );	
       
  4141 	}
       
  4142 // End of File
       
  4143