multimediacommscontroller/mmccrtpsourcesink/src/mccrtpinterface.cpp
changeset 0 1bce908db942
child 32 f2ed1fc4c163
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2002-2008 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:    Method implementations common to RTP data source and -sink.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "rtpapi.h"
       
    24 #include "mccrtpinterface.h"
       
    25 #include "mccrtpdefs.h"
       
    26 #include "mccinternalevents.h"
       
    27 #include "mccrtpkeepalivecontainer.h"
       
    28 
       
    29 #include "mmcccryptocontext.h"
       
    30 #include "mmcccryptoadapter.h"
       
    31 
       
    32 #include <srtpcryptocontext.h>
       
    33 #include <srtpstream.h>
       
    34 
       
    35 // EXTERNAL DATA STRUCTURES
       
    36 
       
    37 // EXTERNAL FUNCTION PROTOTYPES  
       
    38 
       
    39 // CONSTANTS
       
    40 
       
    41 // MACROS
       
    42 
       
    43 // LOCAL CONSTANTS AND MACROS
       
    44 
       
    45 // MODULE DATA STRUCTURES
       
    46 
       
    47 // LOCAL FUNCTION PROTOTYPES
       
    48 
       
    49 // FORWARD DECLARATIONS
       
    50 
       
    51 // ============================= LOCAL FUNCTIONS ===============================
       
    52 
       
    53 // ============================ MEMBER FUNCTIONS ===============================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // MMccRtpInterface::SetSessionParamsL
       
    57 // Set session level parameters.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 void MMccRtpInterface::SetSessionParamsL( const TMccRtpSessionParams& aParams )
       
    61     {
       
    62     TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetSessionParamsL IN" )
       
    63     
       
    64     User::LeaveIfNull( aParams.iRtpAPI );
       
    65     User::LeaveIfNull( aParams.iRtpKeepalive );
       
    66     
       
    67     iRtpAPI = aParams.iRtpAPI;
       
    68     iEnableRtcp = aParams.iEnableRTCP;
       
    69     iSessionID = aParams.iSessionId;
       
    70     iRtpKeepalive = aParams.iRtpKeepalive;
       
    71     iSecSession = aParams.iSecSession;
       
    72     
       
    73     TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetSessionParamsL OUT" )
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // MMccRtpInterface::CheckBufferSupportL
       
    78 // Check the buffer support
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void MMccRtpInterface::CheckBufferSupportL( CMMFBuffer* aBuffer ) const
       
    82     {
       
    83     User::LeaveIfNull( aBuffer );
       
    84     TUid type = aBuffer->Type();
       
    85     
       
    86     __ASSERT_ALWAYS( type == KUidMmfDataBuffer ||
       
    87                      type == KUidMmfTransferBuffer ||
       
    88                      type == KUidMmfAudioBuffer ||
       
    89                      type == KUidMmfPtrBuffer,
       
    90                      User::Leave( KErrNotSupported ) );
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // MMccRtpInterface::SetStateL
       
    95 // Make transition to the new state if acceptable. Otherwise leave.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void MMccRtpInterface::SetStateL( TSourceSinkState aState )
       
    99     {
       
   100     TRACE_RTP_INTERFACE_PRINT3( "MMccRtpInterface::SetStateL - OLD STATE: %d, NEW STATE: %d",
       
   101         iState, aState )
       
   102     
       
   103     TBool transitionOk( EFalse );
       
   104     if ( aState == iState )
       
   105         {
       
   106         TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetStateL - WARNING STATES ARE SAME" )
       
   107         transitionOk = ETrue;
       
   108         }
       
   109         
       
   110     switch ( aState )
       
   111         {
       
   112         case ERtpStateConstructed:
       
   113             transitionOk = ETrue;
       
   114             break;
       
   115 
       
   116         case ERtpStateStopped:
       
   117             transitionOk = ETrue;
       
   118             break;
       
   119 
       
   120         case ERtpStatePrimed:
       
   121             if ( KNullId != RtpStreamId() && iRtpAPI )
       
   122                 {
       
   123                 if ( ERtpStateStopped == iState || ERtpStateConstructed == iState )
       
   124                     {
       
   125                     transitionOk = ETrue;
       
   126                     }
       
   127                 else if ( ERtpStatePlaying == iState || ERtpStatePaused == iState )
       
   128                     {
       
   129                     // Rtp component might be already started by some other user,
       
   130                     // and preparing is not therefore anymore needed
       
   131                     TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetStateL - PREPARED BY OTHER USER" )
       
   132                     aState = iState;
       
   133                     transitionOk = ETrue;
       
   134                     }
       
   135                 else
       
   136                     {
       
   137                     // NOP
       
   138                     }
       
   139                 }
       
   140             break;
       
   141 
       
   142         case ERtpStatePlaying:
       
   143             if ( ( ERtpStatePrimed == iState || ERtpStatePaused == iState )
       
   144             	 && KNullId != RtpStreamId() && iRtpAPI )
       
   145                 {
       
   146                 transitionOk = ETrue;
       
   147                 }
       
   148             break;
       
   149 
       
   150         case ERtpStatePaused:
       
   151             if ( (ERtpStatePlaying == iState )
       
   152             	 && KNullId != RtpStreamId() && iRtpAPI )
       
   153                 {
       
   154                 transitionOk = ETrue;
       
   155                 }
       
   156             break;
       
   157           
       
   158         default:
       
   159             TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetStateL - DEFAULT SWITCH CASE" )
       
   160             break;            
       
   161         }
       
   162 
       
   163     if ( transitionOk )
       
   164         {
       
   165         iState = aState;
       
   166         }
       
   167     else
       
   168         {
       
   169         #ifdef TRACE_RTP_INTERFACE
       
   170             TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::SetStateL - LEAVE KErrNotReady" )
       
   171           	TRACE_RTP_INTERFACE_PRINT2( "MMccRtpInterface::SetStateL - LEAVE iStreamID: %d", RtpStreamId() )
       
   172             TRACE_RTP_INTERFACE_PRINT2( "MMccRtpInterface::SetStateL - LEAVE iRtpAPI: 0x%x", iRtpAPI )
       
   173         #endif
       
   174         				
       
   175        	__ASSERT_ALWAYS (transitionOk, User::Leave (KErrNotReady));
       
   176         }
       
   177         
       
   178     TRACE_RTP_INTERFACE_PRINT2( "MMccRtpInterface::SetStateL - EXIT WITH STATE: %d", iState )
       
   179     }
       
   180     
       
   181 // -----------------------------------------------------------------------------
       
   182 // MMccRtpInterface::CloseStreams
       
   183 // close all RTP streams
       
   184 // -----------------------------------------------------------------------------
       
   185 //  
       
   186 void MMccRtpInterface::CloseStreams()
       
   187 	{ 
       
   188    	if ( iRtpAPI )
       
   189    	    {
       
   190    	    iRtpAPI->CloseStream( RtpStreamId() );
       
   191    	    }
       
   192     if ( !iSrtpStream )
       
   193         {
       
   194         delete iContext;
       
   195         }
       
   196     iContext = NULL;
       
   197     
       
   198     delete iSrtpStream;
       
   199     iSrtpStream = NULL;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // MMccRtpInterface::ConfigureL
       
   204 // Configures the RTP source/sink
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void MMccRtpInterface::ConfigureL( const TDesC8& aConfig )
       
   208     {
       
   209     TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::ConfigureL, Entry" )
       
   210     __ASSERT_ALWAYS( NULL != iRtpKeepalive, User::Leave( KErrNotReady ) );
       
   211     
       
   212     if ( aConfig.Size() == sizeof( TMccCodecInfo ) )
       
   213         {
       
   214         TMccCodecInfoBuffer infoBuffer;
       
   215         infoBuffer.Copy( aConfig );
       
   216         const TMccCodecInfo& codecInfo( infoBuffer() );
       
   217         
       
   218         if ( KNullId == RtpStreamId() )
       
   219             {
       
   220             iCodecInfo = codecInfo;
       
   221             DoCreateStreamL();
       
   222             }
       
   223         else
       
   224             {
       
   225             // Keep latest valid keep-alive configuration. Keep-alive can be
       
   226             // kept disabled by not configuring interval at any phase.
       
   227             TBool updateNeeded = 
       
   228                 ( iCodecInfo.iKeepalivePT != codecInfo.iKeepalivePT ||
       
   229                 iCodecInfo.iKeepaliveInterval != codecInfo.iKeepaliveInterval ||
       
   230                 iCodecInfo.iKeepaliveData != codecInfo.iKeepaliveData ) &&
       
   231                 0 != codecInfo.iKeepaliveInterval;
       
   232             
       
   233             if ( updateNeeded )
       
   234                 {
       
   235                 TRACE_RTP_INTERFACE_PRINT( 
       
   236                     "MMccRtpInterface::ConfigureL - Update" )
       
   237                 
       
   238                 iCodecInfo.iKeepalivePT = codecInfo.iKeepalivePT;
       
   239                 iCodecInfo.iKeepaliveInterval = codecInfo.iKeepaliveInterval;
       
   240                 iCodecInfo.iKeepaliveData = codecInfo.iKeepaliveData;
       
   241                 iRtpKeepalive->UpdateParamsL( *this, codecInfo );
       
   242                 }
       
   243             }
       
   244         }
       
   245     else
       
   246         {
       
   247         TRACE_RTP_INTERFACE_PRINT( "MMccRtpInterface::ConfigureL KErrArgument" )
       
   248         User::Leave( KErrArgument );
       
   249         }
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // MMccRtpInterface::SendRTCPReceiverReport
       
   254 // Sends RTCP receiver report to uplink if possible.
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 TInt MMccRtpInterface::SendRTCPReceiverReport()
       
   258     {
       
   259     TRtpId streamId = RtpStreamId();
       
   260     if ( iRtpAPI && KNullId != streamId )
       
   261         {
       
   262         return iRtpAPI->SendRtcpRrPacket( streamId );
       
   263         }
       
   264     else
       
   265         {
       
   266         return KErrNotReady;
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // MMccRtpInterface::SendRTCPSenderReport
       
   272 // Sends RTCP sender report to uplink if possible.
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 TInt MMccRtpInterface::SendRTCPSenderReport()
       
   276     {
       
   277     TRtpId streamId = RtpStreamId();
       
   278     if ( iRtpAPI && KNullId != streamId )
       
   279         {
       
   280         return iRtpAPI->SendRtcpSrPacket( streamId );
       
   281         }
       
   282     else
       
   283         {
       
   284         return KErrNotReady;
       
   285         }
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // MMccRtpInterface::SendRTCPDataL
       
   290 // Sends RTCP sender report to uplink if possible.
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 void MMccRtpInterface::SendRTCPDataL( const TDesC8& /*aData*/ )
       
   294     {
       
   295     User::Leave( KErrNotSupported );
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // MMccRtpInterface::StartInactivityTimerL
       
   300 // Starts inactivity timer for a stream
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 void MMccRtpInterface::StartInactivityTimerL( TUint32 /*aTimeoutTime*/ )
       
   304     {
       
   305     User::Leave( KErrNotSupported );
       
   306     }
       
   307 
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // MMccRtpInterface::StopInactivityTimerL
       
   311 // Stops inactivity timer for a stream
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 void MMccRtpInterface::StopInactivityTimerL( )
       
   315     {
       
   316     User::Leave( KErrNotSupported );
       
   317     }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // MMccRtpInterface::StandBy
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 TInt MMccRtpInterface::StandBy( 
       
   324     TMccStandbyActionType /*aActionType*/, TUint /*aPayloadType*/ )
       
   325     {
       
   326     return KErrNotSupported;
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // MMccRtpInterface::HandleBySsrc
       
   331 // -----------------------------------------------------------------------------
       
   332 //   
       
   333 TBool MMccRtpInterface::HandleBySsrc( const TRtpSSRC& /*aSsrc*/ )
       
   334     {
       
   335     return EFalse;
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // MMccRtpInterface::HandleByStreamId
       
   340 // -----------------------------------------------------------------------------
       
   341 //        
       
   342 TBool MMccRtpInterface::HandleByStreamId( const TRtpId& aStreamId )
       
   343     {
       
   344     return ( RtpStreamId() == aStreamId );
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // MMccRtpInterface::IsSink
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 TBool MMccRtpInterface::IsSink() const
       
   352     {
       
   353     return EFalse;
       
   354     }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // MMccRtpInterface::IsSending
       
   358 // -----------------------------------------------------------------------------
       
   359 //        
       
   360 TBool MMccRtpInterface::IsSending() const
       
   361     {
       
   362     return EFalse;
       
   363     }
       
   364         
       
   365 // -----------------------------------------------------------------------------
       
   366 // MMccRtpInterface::SendInternalRtpEventToClient
       
   367 // -----------------------------------------------------------------------------
       
   368 //   
       
   369 void MMccRtpInterface::SendInternalRtpEventToClient( 
       
   370     MAsyncEventHandler* aEventHandler,
       
   371     TUid aEventOriginator,
       
   372     TMccInternalEventType aInternalEventType,
       
   373     TMccEventType aEventType,
       
   374     TUint32 aEndpointId,
       
   375     TInt aError, 
       
   376     TUint64 aJitterEstimate,
       
   377     TUint32 aPacketsReceived,
       
   378     TInt64 aPrevTransTime,
       
   379     TUint64 aTriggeredJitterLevel )
       
   380     {
       
   381     TRACE_RTP_SOURCE_PRINT( "MMccRtpInterface::SendInternalRtpEventToClient" )
       
   382     
       
   383     if ( aEventHandler )
       
   384         {
       
   385         ClearMccEvent();
       
   386         
       
   387         iMccEvent.iEventCategory = KMccEventCategoryRtp;
       
   388         iMccEvent.iStreamId = iMccStreamId;
       
   389         iMccEvent.iEndpointId = aEndpointId;
       
   390         iMccEvent.iEventType = aEventType;
       
   391         iMccEvent.iErrorCode = aError;
       
   392         
       
   393         // Save some stack memory
       
   394         {
       
   395         TMccRtpEventData eventData;
       
   396         eventData.iJitterEstimate = aJitterEstimate;
       
   397         eventData.iPacketsReceived = aPacketsReceived;
       
   398         eventData.iPrevTransTime = aPrevTransTime;
       
   399         eventData.iTriggeredJitterLevel = aTriggeredJitterLevel;
       
   400         
       
   401         iMccEvent.iEventData.Copy( TMccRtpEventDataPackage( eventData ) );
       
   402         }
       
   403         
       
   404         TMccInternalEvent internalEvent( aEventOriginator, 
       
   405                                          aInternalEventType,
       
   406                                          iMccEvent );
       
   407         
       
   408         aEventHandler->SendEventToClient( internalEvent );
       
   409         }
       
   410     else
       
   411         {
       
   412 	    TRACE_RTP_INTERFACE_PRINT("MMccRtpInterface::SendInternalRtpEventToClient, \
       
   413 aEventHandler=NULL")
       
   414         }
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // MMccRtpInterface::SendInternalRtpEventToAllClients
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 void MMccRtpInterface::SendInternalRtpEventToAllClients( 
       
   422     RArray<TMccRtpUser>& aUsers,
       
   423     TUid aEventOriginator,
       
   424     TMccInternalEventType aInternalEventType,
       
   425     TMccEventType aEventType,
       
   426     TUint32 aEndpointId,
       
   427     TInt aError,
       
   428     TUint64 aJitterEstimate,
       
   429     TUint32 aPacketsReceived,
       
   430     TInt64 aPrevTransTime,
       
   431     TUint64 aTriggeredJitterLevel )
       
   432     {
       
   433     TRACE_RTP_SOURCE_PRINT( "MMccRtpInterface::SendInternalRtpEventToAllClients" )
       
   434     
       
   435     TInt index( aUsers.Count() );
       
   436     while ( index-- )
       
   437         {
       
   438         SendInternalRtpEventToClient( aUsers[index].iEventHandler, aEventOriginator, 
       
   439             aInternalEventType, aEventType, aEndpointId, aError, aJitterEstimate,
       
   440             aPacketsReceived, aPrevTransTime, aTriggeredJitterLevel );
       
   441         }
       
   442     }
       
   443     
       
   444 // -----------------------------------------------------------------------------
       
   445 // MMccRtpInterface::ClearMccEvent
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 void MMccRtpInterface::ClearMccEvent()
       
   449     {
       
   450     iMccEvent = TMccEvent();
       
   451     }
       
   452 
       
   453 // -----------------------------------------------------------------------------
       
   454 // MMccRtpInterface::StartKeepaliveL
       
   455 // -----------------------------------------------------------------------------
       
   456 //     
       
   457 void MMccRtpInterface::StartKeepaliveL( CMccRtpMediaClock& aRtpMediaClock )
       
   458     {
       
   459     if ( iRtpKeepalive )
       
   460         {
       
   461         iRtpKeepalive->StartKeepaliveL( *this, iCodecInfo, aRtpMediaClock );
       
   462         }
       
   463     }
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // MMccRtpInterface::StopKeepalive
       
   467 // -----------------------------------------------------------------------------
       
   468 //    
       
   469 void MMccRtpInterface::StopKeepalive()
       
   470     {
       
   471     if ( iRtpKeepalive )
       
   472         {
       
   473         iRtpKeepalive->StopKeepalive( *this, iCodecInfo );
       
   474         }
       
   475     }
       
   476     
       
   477 // -----------------------------------------------------------------------------
       
   478 // MMccRtpInterface::ResetKeepaliveTimer
       
   479 // -----------------------------------------------------------------------------
       
   480 //    
       
   481 void MMccRtpInterface::ResetKeepaliveTimer()
       
   482     {
       
   483     if ( iRtpKeepalive )
       
   484         {
       
   485         iRtpKeepalive->ResetKeepaliveTimer();
       
   486         }
       
   487     }    
       
   488     
       
   489 // -----------------------------------------------------------------------------
       
   490 // MMccRtpInterface::ApplyCryptoContextL
       
   491 // -----------------------------------------------------------------------------
       
   492 // 
       
   493 void MMccRtpInterface::ApplyCryptoContextL( const TMccCryptoContext& aContextParams )
       
   494     {
       
   495     if ( iSrtpStream )
       
   496         {
       
   497         TMccCryptoAdapter::UpdateCryptoContextL( aContextParams, *iContext );
       
   498         }
       
   499     else
       
   500         {
       
   501         delete iContext;
       
   502         iContext = NULL;
       
   503         iContext = TMccCryptoAdapter::CreateCryptoContextL( aContextParams );
       
   504         iContextId = aContextParams.ContextId();
       
   505         
       
   506         DoCreateSrtpStreamL();
       
   507         }
       
   508     }
       
   509   
       
   510 // -----------------------------------------------------------------------------
       
   511 // MMccRtpInterface::CreateContext
       
   512 // -----------------------------------------------------------------------------
       
   513 // 
       
   514 TInt MMccRtpInterface::RemoveCryptoContext()
       
   515     {
       
   516     if ( !iSrtpStream )
       
   517         {
       
   518         delete iContext;
       
   519         } 
       
   520         
       
   521     delete iSrtpStream;
       
   522     iSrtpStream = NULL;
       
   523     iContext = NULL;
       
   524     
       
   525     return KErrNone;
       
   526     }    
       
   527 
       
   528 // -----------------------------------------------------------------------------
       
   529 // MMccRtpInterface::SendSecureRtpEventToClient
       
   530 // -----------------------------------------------------------------------------
       
   531 //   
       
   532 void MMccRtpInterface::SendSecureRtpEventToClient( 
       
   533     MAsyncEventHandler* aEventHandler,
       
   534     TUid aEventOriginator,
       
   535     TMccInternalEventType aInternalEventType,
       
   536     TMccEventType aEventType,
       
   537     TUint32 aEndpointId )
       
   538     {
       
   539     TRACE_RTP_SOURCE_PRINT( "MMccRtpInterface::SendSecureRtpEventToClient" )
       
   540     
       
   541     if ( aEventHandler )
       
   542         {
       
   543         ClearMccEvent();
       
   544         
       
   545         iMccEvent.iEventCategory = KMccEventCategoryRtp;
       
   546         iMccEvent.iStreamId = iMccStreamId;
       
   547         iMccEvent.iEndpointId = aEndpointId;
       
   548         iMccEvent.iEventType = aEventType;
       
   549         iMccEvent.iEventNumData = iContextId;
       
   550         
       
   551         
       
   552         TMccInternalEvent internalEvent( aEventOriginator, 
       
   553                                          aInternalEventType,
       
   554                                          iMccEvent );
       
   555         
       
   556         aEventHandler->SendEventToClient( internalEvent );
       
   557         }
       
   558     else
       
   559         {
       
   560 	    TRACE_RTP_INTERFACE_PRINT("MMccRtpInterface::SendSecureRtpEventToClient, \
       
   561         aEventHandler=NULL")
       
   562         }
       
   563     }  
       
   564                  
       
   565 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   566 
       
   567 //  End of File