natfw/natfwconnectionmultiplexer/src/cncmconnectionobserverhandler.cpp
changeset 0 1bce908db942
child 13 fb1bbf85a302
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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:    Handler for connection observers on multiplexer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "cncmconnectionobserverhandler.h"
       
    22 #include "mncmincomingconnectionobserver.h"
       
    23 #include "mncmoutgoingconnectionobserver.h"
       
    24 #include "mncmconnectionobserver.h"
       
    25 #include "ncmconnectionmultiplexerlogs.h"
       
    26 #include "ncmconnectionmultiplexer.hrh"
       
    27 #include "cncmmediasource.h"
       
    28 #include "ncmconnectionmultiplexerassert.h"
       
    29 #include "mncmmessageobserver.h"
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CNcmConnectionObserverHandler::CNcmConnectionObserverHandler
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CNcmConnectionObserverHandler::CNcmConnectionObserverHandler()
       
    36     {
       
    37     }
       
    38 
       
    39     
       
    40 // ---------------------------------------------------------------------------
       
    41 // CNcmConnectionObserverHandler::NewL
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CNcmConnectionObserverHandler*
       
    45     CNcmConnectionObserverHandler::NewL()
       
    46     {
       
    47     __CONNECTIONMULTIPLEXER( 
       
    48         "CNcmConnectionObserverHandler::NewL" )
       
    49         
       
    50     CNcmConnectionObserverHandler* self =
       
    51         CNcmConnectionObserverHandler::NewLC();
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CNcmConnectionObserverHandler::NewLC
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CNcmConnectionObserverHandler* CNcmConnectionObserverHandler::NewLC()
       
    62     {
       
    63     __CONNECTIONMULTIPLEXER( "CNcmConnectionObserverHandler::NewLC" )
       
    64         
       
    65     CNcmConnectionObserverHandler* self =
       
    66         new( ELeave ) CNcmConnectionObserverHandler;
       
    67     CleanupStack::PushL( self );
       
    68     return self;
       
    69     }
       
    70 
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CNcmConnectionObserverHandler::~CNcmConnectionObserverHandlerL
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CNcmConnectionObserverHandler::~CNcmConnectionObserverHandler()
       
    77     {
       
    78     __CONNECTIONMULTIPLEXER( 
       
    79         "CNcmConnectionObserverHandler::~CNcmConnectionObserverHandler" )
       
    80         
       
    81     iIncomingObservers.Close();
       
    82     iOutgoingObservers.Close();
       
    83     iConnectionObservers.Close();
       
    84     
       
    85     delete iMediaSource;
       
    86     iMessageObserver = NULL;
       
    87     
       
    88     iLatestHashes.Close();
       
    89     }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CNcmConnectionObserverHandler::RegisterOutgoingConnectionObserverL
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CNcmConnectionObserverHandler::RegisterOutgoingConnectionObserverL(
       
    97     const MNcmOutgoingConnectionObserver& aOutgoingConnectionObserver )
       
    98     {
       
    99     __CONNECTIONMULTIPLEXER(
       
   100         "CNcmConnectionObserverHandler::RegisterOutgoingConnectionObserver" )
       
   101       
       
   102     if ( KErrNotFound == iOutgoingObservers.Find(
       
   103         &aOutgoingConnectionObserver ) )
       
   104         {
       
   105         iOutgoingObservers.AppendL( &aOutgoingConnectionObserver );
       
   106         }
       
   107     }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CNcmConnectionObserverHandler::RegisterIncomingConnectionObserverL
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CNcmConnectionObserverHandler::RegisterIncomingConnectionObserverL(
       
   115     const MNcmIncomingConnectionObserver& aIncomingConnectionObserver )
       
   116     {
       
   117     __CONNECTIONMULTIPLEXER(
       
   118         "CNcmConnectionObserverHandler::RegisterIncomingConnectionObserver" )
       
   119    
       
   120     if ( KErrNotFound == iIncomingObservers.Find(
       
   121                                             &aIncomingConnectionObserver ) )
       
   122         {
       
   123         iIncomingObservers.AppendL( &aIncomingConnectionObserver );
       
   124         }
       
   125     }
       
   126 
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CNcmConnectionObserverHandler::UnregisterOutgoingConnectionObserverL
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CNcmConnectionObserverHandler::UnregisterOutgoingConnectionObserverL(
       
   133     const MNcmOutgoingConnectionObserver& aOutgoingConnectionObserver )
       
   134     {
       
   135     __CONNECTIONMULTIPLEXER(
       
   136         "CNcmConnectionObserverHandler::UnregisterOutgoingConnectionObserver" )
       
   137        
       
   138     TInt index( 0 );   
       
   139     index = iOutgoingObservers.Find( &aOutgoingConnectionObserver );
       
   140 
       
   141     if ( KErrNotFound != index )
       
   142         {
       
   143         iOutgoingObservers.Remove( index );
       
   144         }
       
   145     }
       
   146 
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CNcmConnectionObserverHandler::UnregisterIncomingConnectionObserver
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 void CNcmConnectionObserverHandler::UnregisterIncomingConnectionObserverL(
       
   153     const MNcmIncomingConnectionObserver& aIncomingConnectionObserver )
       
   154     {
       
   155     __CONNECTIONMULTIPLEXER(
       
   156         "CNcmConnectionObserverHandler::UnregisterIncomingConnectionObserver" )
       
   157          
       
   158     TInt index( 0 );  
       
   159     index = iIncomingObservers.Find( &aIncomingConnectionObserver );
       
   160     
       
   161     if ( KErrNotFound != index )
       
   162         {
       
   163         iIncomingObservers.Remove( index );
       
   164         }
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // CNcmConnectionObserverHandler::RegisterConnectionObserverL
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CNcmConnectionObserverHandler::RegisterConnectionObserverL(
       
   172     const MNcmConnectionObserver& aConnectionObserver )
       
   173     {
       
   174     __CONNECTIONMULTIPLEXER(
       
   175         "CNcmConnectionObserverHandler::RegisterConnectionObserverL" )
       
   176    
       
   177     if ( KErrNotFound == iConnectionObservers.Find( &aConnectionObserver ) )
       
   178         {
       
   179         iConnectionObservers.AppendL( &aConnectionObserver );
       
   180         }
       
   181     }
       
   182  
       
   183  
       
   184 // ---------------------------------------------------------------------------
       
   185 // CNcmConnectionObserverHandler::UnregisterIncomingConnectionObserver
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CNcmConnectionObserverHandler::UnregisterConnectionObserverL(
       
   189     const MNcmConnectionObserver& aConnectionObserver )
       
   190     {
       
   191     __CONNECTIONMULTIPLEXER(
       
   192         "CNcmConnectionObserverHandler::UnregisterConnectionObserverL" )
       
   193          
       
   194     TInt index( 0 ); 
       
   195     index = iConnectionObservers.Find( &aConnectionObserver );
       
   196     
       
   197     if ( KErrNotFound != index )
       
   198         {
       
   199         iConnectionObservers.Remove( index );
       
   200         }
       
   201     }   
       
   202 
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CNcmConnectionObserverHandler::RegisterMessageObserver
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CNcmConnectionObserverHandler::RegisterMessageObserver(
       
   209     MNcmMessageObserver& aMessageObserver )
       
   210     {
       
   211     __CONNECTIONMULTIPLEXER(
       
   212         "CNcmConnectionObserverHandler::RegisterMessageObserverL" )
       
   213     __CONNECTIONMULTIPLEXER_ASSERT_L(
       
   214         NULL == iMessageObserver, KErrNotReady );
       
   215        
       
   216     iMessageObserver = &aMessageObserver;
       
   217     }
       
   218 
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CNcmConnectionObserverHandler::UnregisterMessageObserver
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CNcmConnectionObserverHandler::UnregisterMessageObserver(
       
   225     const MNcmMessageObserver& aMessageObserver )
       
   226     {
       
   227     __CONNECTIONMULTIPLEXER(
       
   228         "CNcmConnectionObserverHandler::UnregisterMessageObserverL" )
       
   229      __CONNECTIONMULTIPLEXER_ASSERT_L( iMessageObserver, KErrNotFound );
       
   230      __CONNECTIONMULTIPLEXER_ASSERT_L(
       
   231         &aMessageObserver == iMessageObserver, KErrArgument );
       
   232          
       
   233     iMessageObserver = NULL;
       
   234     }  
       
   235    
       
   236  
       
   237 // ---------------------------------------------------------------------------
       
   238 // CNcmConnectionObserverHandler::IsDuplicatePacket
       
   239 // Used for replicated RTP/RTCP packet filtering. This is quick fix only,
       
   240 // usage of KSoReuseAddr should be minimized. Also RSocket::Connect can be
       
   241 // used to associate a socket to the explicit remote address. In this case
       
   242 // TCP/IP stack does not replicate packet to the other sockets bound to the
       
   243 // same local transport address.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TBool CNcmConnectionObserverHandler::IsDuplicatePacket( 
       
   247         const TDesC8& aMessage, TBool& aIsStunMessage )
       
   248     {
       
   249     TUint hash( 0 );
       
   250     TBool isDuplicate( EFalse );
       
   251     aIsStunMessage = EFalse;
       
   252     
       
   253     const TUint KMaxValueOfFirstByteOfStunMsg( 2 );
       
   254     if ( 0 != aMessage.Length() 
       
   255             && KMaxValueOfFirstByteOfStunMsg < aMessage[0] )
       
   256         {
       
   257         // In the beginning of RTP/RTCP header there is two bit long version
       
   258         // field, which has always value of 2. Thus value of first byte of 
       
   259         // RTP/RTCP message is at least 128.
       
   260         const TInt KRtpRtcpDiffByteOffset( 1 );
       
   261         const TInt KLowestAssignedRtcpMsgType = 200;
       
   262         const TInt KHighestAssignedRtcpMsgType = 209;
       
   263         TBool isRtcp = 
       
   264            ( KLowestAssignedRtcpMsgType <= aMessage[KRtpRtcpDiffByteOffset] &&
       
   265             aMessage[KRtpRtcpDiffByteOffset] <= KHighestAssignedRtcpMsgType );
       
   266         
       
   267         // This offset refers either to SR RTP timestamp or RR highest 
       
   268         // sequence number received. These are unique for a distinct 
       
   269         // RTCP packet.
       
   270         const TInt KRtcpDiffByteOffset( 19 );
       
   271         
       
   272         // This offset refers to lower byte of unique RTP sequence number.
       
   273         const TInt KRtpDiffByteOffset( 3 );
       
   274         TInt diffByteOffset 
       
   275             = isRtcp ? KRtcpDiffByteOffset : KRtpDiffByteOffset;
       
   276 
       
   277         if ( diffByteOffset < aMessage.Size() )
       
   278             {
       
   279             hash = aMessage[diffByteOffset];
       
   280             }
       
   281         else
       
   282             {
       
   283             __CONNECTIONMULTIPLEXER( 
       
   284             "CNcmConnectionObserverHandler::IsDuplicatePacket INVALID PACKET")
       
   285             return EFalse;
       
   286             }
       
   287 
       
   288         if ( KErrNotFound != iLatestHashes.Find( hash ) )
       
   289             {
       
   290             // this is duplicate packet
       
   291             __CONNECTIONMULTIPLEXER(
       
   292                 "CNcmConnectionObserverHandler::IsDuplicatePacket TRUE" )
       
   293             isDuplicate = ETrue;
       
   294             }
       
   295         else
       
   296             {
       
   297             isDuplicate = EFalse;
       
   298             }
       
   299         
       
   300         iLatestHashes.Append( hash );
       
   301         const TInt KMaxHashCount( 5 );
       
   302         if ( KMaxHashCount <= iLatestHashes.Count() )
       
   303             {
       
   304             iLatestHashes.Remove( 0 );
       
   305             }
       
   306         }
       
   307     else
       
   308         {
       
   309         __CONNECTIONMULTIPLEXER( 
       
   310             "CNcmConnectionObserverHandler::IsDuplicatePacket STUN MSG")
       
   311             
       
   312         aIsStunMessage = ETrue;
       
   313         }
       
   314     
       
   315     return isDuplicate;
       
   316     }
       
   317 
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CNcmConnectionObserverHandler::RevealIncomingMessageL
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 TBool CNcmConnectionObserverHandler::RevealIncomingMessageL(
       
   324     TUint aStreamId, HBufC8*& aMessage, const TInetAddr& aLocalAddr,
       
   325     const TInetAddr& aFromAddr )
       
   326     {
       
   327     __CONNECTIONMULTIPLEXER(
       
   328         "CNcmConnectionObserverHandler::RevealIncomingMessageL" )
       
   329     
       
   330     TBool isStunMessage( EFalse );
       
   331     
       
   332     if ( IsDuplicatePacket( *aMessage, isStunMessage ) )
       
   333         {
       
   334         return ETrue;
       
   335         }
       
   336     
       
   337     TInetAddr peerRemoteAddress( KInetAddrNone, 0 );
       
   338 
       
   339     HBufC8* modified( NULL );
       
   340     
       
   341     if ( iMessageObserver )
       
   342         {
       
   343         modified = iMessageObserver->IncomingMessageNotify( 
       
   344             aStreamId, *aMessage, aLocalAddr, aFromAddr, peerRemoteAddress );
       
   345             
       
   346         // replace message if new content is found
       
   347         if ( modified )
       
   348             {
       
   349             delete aMessage;
       
   350             aMessage = modified;
       
   351             }
       
   352         }
       
   353         
       
   354     // offer to all incoming observers and finish when comsumer is found
       
   355     TBool consumed( EFalse );
       
   356     TInt index( iIncomingObservers.Count() );
       
   357         
       
   358     while ( index-- && !consumed )
       
   359         {
       
   360         TRAP_IGNORE( iIncomingObservers[index]->IncomingMessageL( 
       
   361             aStreamId, *aMessage, aLocalAddr, aFromAddr, peerRemoteAddress,
       
   362             consumed ) )
       
   363         }
       
   364     
       
   365     if ( iMediaSource && !consumed && !isStunMessage  )
       
   366         {
       
   367         iMediaSource->PutMessageL( *aMessage );
       
   368         consumed = ETrue;
       
   369         }
       
   370         
       
   371     return consumed;
       
   372     }
       
   373 
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CNcmConnectionObserverHandler::RevealOutgoingMessageL
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 TBool CNcmConnectionObserverHandler::RevealOutgoingMessageL(
       
   380     TUint aStreamId, TUint aConnectionId, HBufC8*& aMessage,
       
   381     const TInetAddr& aDestinationAddress )
       
   382     {
       
   383     __CONNECTIONMULTIPLEXER(
       
   384         "CNcmConnectionObserverHandler::RevealOutgoingMessageL" )
       
   385          
       
   386     TInt observerCount( iOutgoingObservers.Count() );
       
   387     TBool consumed( EFalse );
       
   388     HBufC8* modified( NULL );
       
   389     
       
   390     if ( iMessageObserver )
       
   391         {
       
   392         modified = iMessageObserver->OutgoingMessageNotify( 
       
   393             aStreamId, aConnectionId, aDestinationAddress, *aMessage );
       
   394             
       
   395         // replace message if new content is found
       
   396         if ( modified )
       
   397             {
       
   398             delete aMessage;
       
   399             aMessage = modified;
       
   400             }
       
   401         }
       
   402     
       
   403     // offer to all outgoing observers and see if anyone consumed it
       
   404     for ( TInt i = 0; i < observerCount && !consumed; i++ )
       
   405         {
       
   406          iOutgoingObservers[i]->OutgoingMessageL( 
       
   407             aStreamId, aConnectionId, aDestinationAddress, *aMessage,
       
   408             consumed );
       
   409         }
       
   410 
       
   411     return consumed;
       
   412     }
       
   413 
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // CNcmConnectionObserverHandler::ConnectionNotify
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 void CNcmConnectionObserverHandler::ConnectionNotify(
       
   420     TUint aStreamId, TUint aConnectionId, 
       
   421     TMultiplexerConnectionNotifyType aType, TInt aError )
       
   422     {
       
   423     __CONNECTIONMULTIPLEXER(
       
   424         "CNcmConnectionObserverHandler::ConnectionNotify, start" )
       
   425     
       
   426     TInt ind( iConnectionObservers.Count() );
       
   427     while ( KErrNotFound != ind-- )
       
   428         {
       
   429         TInt maxIndex( iConnectionObservers.Count() - 1 );
       
   430         
       
   431         ind = ( ind >= maxIndex ) ? maxIndex : ind;
       
   432         
       
   433         if ( KErrNotFound != ind )
       
   434             {
       
   435             iConnectionObservers[ind]->ConnectionNotify( 
       
   436                 aStreamId, aConnectionId,
       
   437                 static_cast<MNcmConnectionObserver::TConnectionNotifyType>(
       
   438                     aType ), aError ); 
       
   439             }         
       
   440         }
       
   441 
       
   442     __CONNECTIONMULTIPLEXER(
       
   443         "CNcmConnectionObserverHandler::ConnectionNotify, end" )    
       
   444     }
       
   445 
       
   446 
       
   447 // ---------------------------------------------------------------------------
       
   448 // CNcmConnectionObserverHandler::RegisterMediaSourceL
       
   449 // ---------------------------------------------------------------------------
       
   450 //
       
   451 void CNcmConnectionObserverHandler::RegisterMediaSourceL( 
       
   452     CNcmMediaSource* aMediaSource )
       
   453     {
       
   454     __CONNECTIONMULTIPLEXER(
       
   455         "CNcmConnectionObserverHandler::RegisterMediaSource" )
       
   456     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL != aMediaSource, KErrArgument );
       
   457     delete iMediaSource;
       
   458     iMediaSource = NULL;
       
   459     iMediaSource = aMediaSource;
       
   460     }
       
   461 
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // CNcmConnectionObserverHandler::RegisterMediaSourceObserver
       
   465 // ---------------------------------------------------------------------------
       
   466 //      
       
   467 void CNcmConnectionObserverHandler::RegisterMediaSourceObserver( 
       
   468     MNcmSourceObserver& aMediaSourceObserver )
       
   469     {
       
   470     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL != iMediaSource, KErrArgument );
       
   471     iMediaSource->RegisterMessageObserver( aMediaSourceObserver );  
       
   472     }
       
   473 
       
   474     
       
   475 // ---------------------------------------------------------------------------
       
   476 // CNcmConnectionObserverHandler::UnregisterMediaSourceObserver
       
   477 // ---------------------------------------------------------------------------
       
   478 //     
       
   479 void CNcmConnectionObserverHandler::UnregisterMediaSourceObserver(
       
   480     const MNcmSourceObserver& aMediaSourceObserver )
       
   481     {
       
   482     __CONNECTIONMULTIPLEXER_ASSERT_L( NULL != iMediaSource, KErrArgument );
       
   483     iMediaSource->UnregisterMessageObserver( aMediaSourceObserver );   
       
   484     }
       
   485  
       
   486    
       
   487 // ---------------------------------------------------------------------------
       
   488 // CNcmConnectionObserverHandler::SetReceivingState
       
   489 // ---------------------------------------------------------------------------
       
   490 //     
       
   491 void CNcmConnectionObserverHandler::SetReceivingState(
       
   492     TNATFWStreamingState aState )
       
   493     {
       
   494     iMediaSource->SetReceivingState( aState );   
       
   495     }