rtp/rtpstack/src/rtpcomm.cpp
changeset 0 307788aac0a8
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 "rtpcomm.h"
       
    23 
       
    24 // ================= MEMBER FUNCTIONS =======================
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // C++ default constructor can NOT contain any code, that
       
    28 // might leave.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CRtpComm::CRtpComm()
       
    32     {
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // Symbian 2nd phase constructor can leave.
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 void CRtpComm::ConstructL( TUint& aLocalPort,
       
    40                            RSocketServ& aSocketServ,
       
    41                            RConnection& aRConn, 
       
    42                            const TCreateSessionParams& aParams,
       
    43                            MRtpErrNotify& aErrNotify,
       
    44                            TBool aEnableRtcp )
       
    45     {
       
    46     
       
    47     
       
    48     RTP_DEBUG_DETAIL_DVALUE( "CRtpComm::ConstructL(), local port = ", aLocalPort );
       
    49     
       
    50     TInt  err( 0 );
       
    51     TBool bRequiredPortsOpen( EFalse );
       
    52     TUint dynLocalPort( aLocalPort );
       
    53     
       
    54     if ( ( dynLocalPort % 2 ) != 0 )
       
    55         {
       
    56         // If an odd number is supplied then the application should replace
       
    57         // that number with the next lower (even) number to use as the base
       
    58         // of the port pair. (RFC3550)
       
    59         dynLocalPort--;
       
    60         }
       
    61 
       
    62     if ( dynLocalPort == 0 ) // use default port 
       
    63         {
       
    64         dynLocalPort = KDefaultLocalPort;
       
    65         }
       
    66 
       
    67     TInetAddr localRtpAddress( dynLocalPort );
       
    68     TInetAddr localRtcpAddress( dynLocalPort + 1 );
       
    69     
       
    70     // Open RTP socket
       
    71     CleanupClosePushL( iSocket[ERTPPort] );
       
    72     err = iSocket[ERTPPort].Open( aSocketServ, 
       
    73                                   KAfInet,
       
    74                                   KSockDatagram, 
       
    75                                   KProtocolInetUdp, 
       
    76                                   aRConn 
       
    77                                 );
       
    78     User::LeaveIfError( err );
       
    79 
       
    80     if ( aEnableRtcp )
       
    81         {
       
    82         // Open RTCP socket
       
    83         CleanupClosePushL( iSocket[ERTCPPort] );
       
    84         err = iSocket[ERTCPPort].Open( aSocketServ, 
       
    85                                        KAfInet,
       
    86                                        KSockDatagram, 
       
    87                                        KProtocolInetUdp, 
       
    88                                        aRConn             
       
    89                                      );
       
    90         User::LeaveIfError( err );
       
    91         }
       
    92     
       
    93    	// Find consecutive ports for RTP and RTCP
       
    94     while ( !bRequiredPortsOpen )
       
    95         {
       
    96         err = iSocket[ERTPPort].SetLocalPort(dynLocalPort);
       
    97         if ( err == KErrNone && aEnableRtcp )
       
    98             {
       
    99          err = iSocket[ERTCPPort].SetLocalPort(dynLocalPort + 1 );
       
   100             }
       
   101         if ( err == KErrNone )
       
   102             {
       
   103             bRequiredPortsOpen = ETrue;
       
   104             }
       
   105         else
       
   106             {
       
   107             // RTP always uses even ports, so increment by 2 and retry.
       
   108             dynLocalPort += 2;
       
   109             if ( dynLocalPort >= KMaxLocalPort )
       
   110                 {
       
   111                 // Reached maximum port number
       
   112                 User::LeaveIfError( KErrTooBig );
       
   113                 }
       
   114             }
       
   115         }            
       
   116 
       
   117     err = iSocket[ERTPPort].SetOpt( KSORecvBuf, KSOLSocket,
       
   118                                     aParams.iSocketBufSize );
       
   119     User::LeaveIfError( err );
       
   120 
       
   121     err = iSocket[ERTPPort].SetOpt( KSOSendBuf, KSOLSocket, aParams.iSocketBufSize );
       
   122     User::LeaveIfError( err );  
       
   123 
       
   124     err = iSocket[ERTPPort].SetOpt( KSoUdpSynchronousSend, KSolInetUdp,
       
   125                                     ETrue );
       
   126     User::LeaveIfError( err );          
       
   127 
       
   128     if ( aEnableRtcp )
       
   129         {
       
   130         err = iSocket[ERTCPPort].SetOpt( KSORecvBuf, KSOLSocket,
       
   131                                          KSocketBufSize );
       
   132         User::LeaveIfError( err );
       
   133 
       
   134         err = iSocket[ERTCPPort].SetOpt( KSOSendBuf, KSOLSocket,
       
   135                                          KSocketBufSize );
       
   136         User::LeaveIfError( err );          
       
   137 
       
   138         err = iSocket[ERTCPPort].SetOpt( KSoUdpSynchronousSend, KSolInetUdp,
       
   139                                          ETrue );
       
   140         User::LeaveIfError( err );          
       
   141         }
       
   142 
       
   143     // Let the application know what port we ended up using
       
   144     aLocalPort = dynLocalPort;
       
   145 
       
   146     RTP_DEBUG_DETAIL_DVALUE( "CRtpComm::ConstructL(), local port chosen = ", aLocalPort );
       
   147     
       
   148     iPriority = aParams.iPriority;
       
   149     iErrNotify = &aErrNotify;
       
   150     iRtcpEnabled = aEnableRtcp;
       
   151     iSocketSize = aParams.iSocketBufSize;
       
   152 
       
   153     // The sender and receiver objects are created separately
       
   154     // in ConstructSenderL and ConstructReceiverL
       
   155 
       
   156     CleanupStack::Pop(); // iSocket[ERTPPort]
       
   157     if ( aEnableRtcp )
       
   158         {
       
   159         CleanupStack::Pop(); // iSocket[ERTCPPort]
       
   160         }
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // Two-phased constructor.
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 CRtpComm* CRtpComm::NewL( TUint& aLocalPort,
       
   168                           RSocketServ& aSocketServ,
       
   169                           RConnection& aRConn, 
       
   170                           const TCreateSessionParams& aParams,
       
   171                           MRtpErrNotify& aErrNotify,
       
   172                           TBool aEnableRtcp )
       
   173     {
       
   174     CRtpComm* self = new ( ELeave ) CRtpComm();
       
   175     CleanupStack::PushL( self );
       
   176     self->ConstructL( aLocalPort, aSocketServ, aRConn, aParams, aErrNotify,
       
   177                       aEnableRtcp );
       
   178     CleanupStack::Pop(); //self
       
   179     return self;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Destructor
       
   184 // ---------------------------------------------------------------------------
       
   185 // 
       
   186 CRtpComm::~CRtpComm()
       
   187      {
       
   188      for ( TInt k = 0; k < KMaxPorts; k++ ) 
       
   189          {
       
   190          if ( iReceiver[ k ] )
       
   191          	{
       
   192          	delete iReceiver[ k ];
       
   193          	}
       
   194          if ( iSender[ k ] )
       
   195          	{
       
   196          	delete iSender[ k ];
       
   197          	}
       
   198          }
       
   199  
       
   200      Close();
       
   201      }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CRtpComm::ConstructSenderL()
       
   205 // 
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CRtpComm::ConstructSenderL(TInetAddr& aRtpAddr, TInetAddr& aRtcpAddr)
       
   209     {
       
   210     if ( !iSender[ERTPPort] )
       
   211         {
       
   212         iSender[ERTPPort] = CRtpCommSend::NewL( iSocket[ERTPPort], *iErrNotify, 
       
   213                                                 /*KMaxPackets * 1024,*/ aRtpAddr );
       
   214         }
       
   215     if ( iRtcpEnabled && !iSender[ERTCPPort] )
       
   216         {
       
   217         iSender[ERTCPPort] = CRtpCommSend::NewL( iSocket[ERTCPPort],
       
   218                                                  *iErrNotify,
       
   219                                                  /*KMaxPackets * 256,*/ aRtcpAddr );
       
   220         // Boost the priority of the RTCP transmission, to make sure those
       
   221         // few packets reach their destination.
       
   222         iSender[ERTCPPort]->SetPriority( TCreateSessionParams::EPriorityHigh );
       
   223         }
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // CRtpComm::ConstructReceiverL()
       
   228 // 
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void CRtpComm::ConstructReceiverL(TBool aNonRtpObserverSet)
       
   232     {
       
   233     if ( !iReceiver[ERTPPort] )
       
   234         {
       
   235         iReceiver[ERTPPort] = CRtpCommRecv::NewL( ERTPPort, iSocket[ERTPPort], 
       
   236                                                   iPriority, *iErrNotify, aNonRtpObserverSet );
       
   237         iReceiver[ERTPPort]->RegisterReceivedNotify( iReceivedNotify );
       
   238         }
       
   239     if ( iRtcpEnabled && !iReceiver[ERTCPPort] )
       
   240         {
       
   241         iReceiver[ERTCPPort] = CRtpCommRecv::NewL( ERTCPPort,
       
   242                                                    iSocket[ERTCPPort], 
       
   243                                                    TCreateSessionParams::EPriorityHigh,
       
   244                                                    *iErrNotify, aNonRtpObserverSet );
       
   245         iReceiver[ERTCPPort]->RegisterReceivedNotify( iReceivedNotify );
       
   246         }
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // CRtpComm::Close()
       
   251 // 
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CRtpComm::Close()
       
   255     {
       
   256     
       
   257     for ( TInt k = 0; k < KMaxPorts; k++ )
       
   258         {
       
   259         iSocket[k].Close();
       
   260         }
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CRtpComm::SetToAddress()
       
   265 // Return TRequestStatus.Int()
       
   266 // ---------------------------------------------------------------------------
       
   267 //  
       
   268 TInt CRtpComm::SetToAddress( TPortType aPortType, TInetAddr& aAddr )
       
   269     {
       
   270     TInt ret = KErrNone;
       
   271     
       
   272     CRtpCommSend* aCommSend = iSender[aPortType];
       
   273         
       
   274     if (aCommSend)    
       
   275         {
       
   276         aCommSend->SetToAddress(aAddr);       
       
   277         }
       
   278         
       
   279     CRtpCommRecv* aCommRecv = iReceiver[aPortType];
       
   280         
       
   281     if (aCommRecv)    
       
   282         {
       
   283         aCommRecv->SetRemoteAddress(aAddr);       
       
   284         }
       
   285         
       
   286     return ret;
       
   287     }
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // CRtpComm::SetAcceptedFromAddress()
       
   291 // 
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CRtpComm::SetAcceptedFromAddress(TInetAddr& aAddr)
       
   295     {
       
   296      iReceiver[ERTPPort]->SetRemoteAddress(aAddr);                                          
       
   297     
       
   298     if ( iRtcpEnabled && iReceiver[ERTCPPort] )
       
   299         {
       
   300         iReceiver[ERTCPPort]->SetRemoteAddress(aAddr);                                          
       
   301         }
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // CRtpComm::SetNonRtpObserverFlag()
       
   306 // 
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 void CRtpComm::SetNonRtpObserverFlag(TBool aValue)
       
   310     {
       
   311     if (iReceiver[ERTPPort] )
       
   312         {
       
   313         iReceiver[ERTPPort]->SetNonRtpObserverFlag(aValue);        
       
   314         }
       
   315     if (iReceiver[ERTCPPort] )
       
   316         {
       
   317         iReceiver[ERTCPPort]->SetNonRtpObserverFlag(aValue);        
       
   318         }    
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // CRtpComm::MaxSocketSize()
       
   323 // 
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 TInt CRtpComm::MaxSocketSize()
       
   327     {
       
   328     return iSocketSize;
       
   329     }
       
   330     
       
   331 // ---------------------------------------------------------------------------
       
   332 // CRtpComm::Send()
       
   333 // 
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 TInt CRtpComm::Send( TPortType aPortType, const TDesC8& aSendBuf )
       
   337     {
       
   338     TInt result( KErrNotReady );
       
   339     if ( iSender[aPortType] )
       
   340         {
       
   341         result = iSender[aPortType]->Send( aSendBuf );
       
   342         }
       
   343     return result;
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // CRtpComm::Send()
       
   348 // 
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 void CRtpComm::Send( TPortType aPortType, const TDesC8& aSendBuf, TRequestStatus& aStatus )
       
   352     {
       
   353     if ( iSender[aPortType] )
       
   354         {
       
   355         iSender[aPortType]->Send( aSendBuf, aStatus );
       
   356         }
       
   357     else
       
   358         {
       
   359         TRequestStatus* status = &aStatus;
       
   360         User::RequestComplete( status, KErrNotReady );
       
   361         }
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // CRtpComm::CancelSend()
       
   366 // 
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 void CRtpComm::CancelSend( TPortType aPortType )
       
   370     {
       
   371     if ( iSender[aPortType] )
       
   372         {
       
   373         iSender[aPortType]->CancelAsyncSend();
       
   374         }
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 // CRtpComm::Receive()
       
   379 // 
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 TInt CRtpComm::Receive( TPortType aPortType, TDes8& aRecvBuf )
       
   383     {
       
   384     TInt result( KErrNotReady );
       
   385     if ( iReceiver[aPortType] )
       
   386         {
       
   387         result = iReceiver[aPortType]->Recv( aRecvBuf );
       
   388         }
       
   389     return result;
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // CRtpComm::RegisterReceivedNotify()
       
   394 // 
       
   395 // ---------------------------------------------------------------------------
       
   396 //
       
   397 void CRtpComm::RegisterReceivedNotify( MReceivedNotify* aNotify )
       
   398     {
       
   399     iReceivedNotify = aNotify;
       
   400     if ( iReceiver[ERTPPort] )
       
   401         {
       
   402         iReceiver[ERTPPort]->RegisterReceivedNotify( aNotify );
       
   403         }
       
   404     if ( iReceiver[ERTCPPort] )
       
   405         {
       
   406         iReceiver[ERTCPPort]->RegisterReceivedNotify( aNotify );
       
   407         }
       
   408     }
       
   409 
       
   410 // ---------------------------------------------------------------------------
       
   411 // RSocket* CRtpComm::GetSocketObject()
       
   412 // 
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 RSocket* CRtpComm::GetSocket( TPortType aPortType )
       
   416     {
       
   417     return &iSocket[aPortType];
       
   418     }
       
   419 
       
   420 //  End of File