multimediacommsengine/mmcecli/src/mcesessionutility.cpp
branchrcs
changeset 49 64c62431ac08
equal deleted inserted replaced
44:fb024d5e35fa 49:64c62431ac08
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <badesca.h>
       
    21 #include <mcesession.h>
       
    22 #include <uri8.h>
       
    23 #include <sdpcodecstringpool.h>
       
    24 #include <SdpAttributeField.h>
       
    25 #include <sdpdocument.h>
       
    26 #include "mceclilogs.h"
       
    27 #include "mcecomsession.h"
       
    28 #include "mcecommediastream.h"
       
    29 #include "mcesessionutility.h"
       
    30 #include <SdpCodecStringPool.h>
       
    31 #include <stringpool.h>
       
    32 #include "mcecommediastream.h"
       
    33 #include <mcertpsource.h>
       
    34 #include <mcemsrpsource.h>
       
    35 #include <sdpconnectionfield.h>
       
    36 #include <sdpbandwidthfield.h>
       
    37 #include <mcertpsource.h>
       
    38 
       
    39 //# define _OPEN_SDPCODEC_STRINGPOOL  SdpCodecStringPool::OpenL()
       
    40 
       
    41 #define _CLOSE_SDPCODEC_STRINGPOOL   SdpCodecStringPool::Close();
       
    42 
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // TMceSessionUtility::getSessionSdPLineL
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 EXPORT_C HBufC8* TMceSessionUtility::getSessionSdPLineL(CMceSession* aSession, 
       
    51         TSDPLineName aSdpLineName)
       
    52     {
       
    53     __ASSERT_ALWAYS(aSession, User::Leave(KErrArgument));    
       
    54     
       
    55     // Ownership is tranferred to us for sessionLines
       
    56     CDesC8Array* sessionLines = static_cast<CDesC8Array*> (aSession->SessionSDPLinesL());
       
    57     
       
    58     if (sessionLines->MdcaCount() == 0)
       
    59         {
       
    60         MCECLI_DEBUG("TMceSessionUtility::getSessionSdPLineL: No session attributes set in the passed session \n");
       
    61         sessionLines->Reset();
       
    62         delete sessionLines;
       
    63         return NULL;
       
    64         }
       
    65     
       
    66     // Open SIP coec string pool
       
    67     TRAPD(error, SdpCodecStringPool::OpenL());
       
    68     if (error != KErrAlreadyExists && error != KErrNone)
       
    69         {
       
    70         delete sessionLines;
       
    71         User::Leave(error);
       
    72         }
       
    73     CSdpDocument* sdpDoc = NULL;
       
    74     CleanupStack::PushL(sessionLines);
       
    75     ConstructSdpDocumentL(sessionLines, sdpDoc); 
       
    76     CleanupStack::Pop(sessionLines);
       
    77     
       
    78     HBufC8* retValue = NULL;
       
    79     switch(aSdpLineName)
       
    80         {
       
    81         case EVersion:
       
    82             {
       
    83             TInt value = sdpDoc->SdpVersion();
       
    84             TBuf8<10> tmp;
       
    85             tmp.Num(value);
       
    86             retValue = tmp.AllocL();
       
    87             }
       
    88             break;
       
    89          
       
    90         case ESession:
       
    91             retValue = sdpDoc->SessionName().AllocL();
       
    92             break;
       
    93             
       
    94         case EInfo:
       
    95             retValue = sdpDoc->Info().AllocL();
       
    96             break;
       
    97             
       
    98         case EUri:
       
    99             {
       
   100             // Ownership will not be transferred to us so shouldn't delete the pointer
       
   101             CUri8* uri = sdpDoc->Uri();
       
   102             if (uri != NULL)
       
   103                 {
       
   104                 retValue = uri->Uri().UriDes().AllocL();
       
   105                 }
       
   106             }
       
   107             break;
       
   108             
       
   109         case EZone:
       
   110             retValue = sdpDoc->ZoneAdjustments().AllocL();
       
   111             break;
       
   112             
       
   113         default:
       
   114             sessionLines->Reset();
       
   115             delete sessionLines;
       
   116             delete sdpDoc;
       
   117             _CLOSE_SDPCODEC_STRINGPOOL;
       
   118             User::Leave(KErrNotSupported);
       
   119         }
       
   120     sessionLines->Reset();
       
   121     delete sessionLines;
       
   122     delete sdpDoc;
       
   123     
       
   124     if (error == KErrNone)
       
   125         _CLOSE_SDPCODEC_STRINGPOOL;
       
   126     __ASSERT_ALWAYS(retValue!=NULL, User::Leave(KErrArgument));
       
   127     return retValue;
       
   128     }
       
   129         
       
   130 
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // TMceSessionUtility::getRemoteMediaSDPAttrL
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 EXPORT_C HBufC8* TMceSessionUtility::getRemoteMediaSDPAttrL(CMceMediaStream* aStream,
       
   137                 const TDesC8& aAttrName)
       
   138     {
       
   139     __ASSERT_ALWAYS(aStream, User::Leave(KErrArgument));
       
   140     // Ownership is not transferred to us
       
   141     CMceSession* session = aStream->Session();
       
   142     __ASSERT_ALWAYS(session, User::Leave(KErrArgument));
       
   143     
       
   144 	if ( (session->State() >= CMceSession::ECancelling) || 
       
   145 		(session->State() <= CMceSession::EIdle) )
       
   146         {
       
   147         MCECLI_DEBUG(" Session state shouldn't be idle Or shouldn't be either in cancelling, terminating , terminated  \n");
       
   148         User::Leave(KErrArgument);
       
   149         }
       
   150     
       
   151     CDesC8Array* mediaAttrs = NULL; 
       
   152     mediaAttrs = aStream->FlatData()->iRemoteMediaSDPLines;
       
   153     
       
   154     if (mediaAttrs == NULL || mediaAttrs->MdcaCount() == 0)
       
   155         {
       
   156         if (aStream->BoundStream())
       
   157 			{
       
   158 			mediaAttrs = aStream->BoundStreamL().FlatData()->iRemoteMediaSDPLines;		
       
   159 			}
       
   160 		}
       
   161 		
       
   162 	if (mediaAttrs == NULL || mediaAttrs->MdcaCount() == 0)
       
   163 		{
       
   164 		// Look for the another staream of same type to extract the remote media sdp attributes
       
   165         CMceMediaStream* tempStrm;
       
   166         for(TInt i=0; i<session->Streams().Count(); i++)
       
   167             {
       
   168             tempStrm = session->Streams()[i];
       
   169             if (tempStrm != aStream && (tempStrm->Type() == aStream->Type()) )
       
   170                 {
       
   171                 mediaAttrs = tempStrm->FlatData()->iRemoteMediaSDPLines;
       
   172                 }
       
   173             }
       
   174         if (mediaAttrs == NULL || mediaAttrs->MdcaCount() == 0)
       
   175             {
       
   176             MCECLI_DEBUG("TMceSessionUtility::getRemoteMediaSDPAttrL: No Remote Media SDP attributes set to the streams \n");
       
   177             return NULL;
       
   178             }
       
   179         }
       
   180     //_OPEN_SDPCODEC_STRINGPOOL;
       
   181     TRAPD(error, SdpCodecStringPool::OpenL());
       
   182     if (error != KErrAlreadyExists && error != KErrNone)
       
   183         {
       
   184         User::Leave(error);
       
   185         }
       
   186     // If the attribute is found then the value will be in aValue.
       
   187     HBufC8* value = NULL;
       
   188     value = FindAttributeL(mediaAttrs, aAttrName);
       
   189     if (error == KErrNone)
       
   190         _CLOSE_SDPCODEC_STRINGPOOL;
       
   191     return value;
       
   192     }
       
   193         
       
   194  
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // TMceSessionUtility::GetLocalConnectionAddrL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void TMceSessionUtility::GetLocalConnectionAddrL(CMceSession* aSess, TInetAddr& aAddr)
       
   201     {
       
   202     __ASSERT_ALWAYS(aSess, User::Leave(KErrArgument));
       
   203     aAddr = aSess->FlatData()->iLocalIpAddress;
       
   204     }
       
   205 
       
   206  
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // TMceSessionUtility::GetRemoteConnectionAddrL
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 EXPORT_C HBufC8* TMceSessionUtility::GetRemoteConnectionAddrL(CMceMediaStream* aStream/*, TDesC8* hostAddr*/)
       
   213     {
       
   214     CMceSession* session = aStream->Session();
       
   215     __ASSERT_ALWAYS(session, User::Leave(KErrArgument));
       
   216     if ( (session->State() >= CMceSession::ECancelling) || 
       
   217             (session->State() <= CMceSession::EIdle) )
       
   218         {
       
   219         MCECLI_DEBUG("TMceSessionUtility::GetRemoteConnectionAddrL: Session state shouldn't be idle Or shouldn't be either in cancelling, terminating , terminated");
       
   220         User::Leave(KErrArgument);
       
   221         }
       
   222     TBuf8<10> attrName;
       
   223     
       
   224     if (aStream->Type() == KMceMessage)
       
   225         {
       
   226         // Get the remote end's path attribute and extracts the host number from that
       
   227         //ownership willbe transferred to us so have to free up later
       
   228         HBufC8* pathValue = NULL;
       
   229         attrName.Zero();
       
   230         attrName.Copy(_L8("path"));
       
   231         pathValue = TMceSessionUtility::getRemoteMediaSDPAttrL(aStream, attrName);
       
   232         if (NULL!=pathValue)
       
   233             {
       
   234             TUriParser8 parser;
       
   235             TInt parseValue = parser.Parse(*pathValue); 
       
   236                     
       
   237             MCECLI_DEBUG_DVALUE("\n CMceMessageSdpCodec::SetHostAddrPortFromPathAttr: Parsed return value for MSRP URI: ", 
       
   238                     parseValue);
       
   239                 
       
   240             User::LeaveIfError(parseValue);
       
   241             //TBuf8<50> host = parser.Extract(EUriHost);
       
   242             HBufC8* hostAddr = parser.Extract(EUriHost).AllocL();
       
   243             delete pathValue;
       
   244             return hostAddr;
       
   245             }
       
   246         }
       
   247     
       
   248     // check if c line exists for the m-line correspands to this stream
       
   249     attrName.Zero();
       
   250     attrName.Copy(_L8("c"));
       
   251     HBufC8* hostAddr = NULL;
       
   252     hostAddr = TMceSessionUtility::getRemoteMediaSDPAttrL(aStream, attrName);
       
   253     if (hostAddr!=NULL)
       
   254         return hostAddr;
       
   255     else
       
   256         {
       
   257         TBuf<32> addr;
       
   258         session->FlatData()->iRemoteIpAddress.Output(addr);
       
   259         TBuf8<32> value;
       
   260         value.Copy(addr);
       
   261         return value.AllocL();
       
   262         }
       
   263     }
       
   264     
       
   265 // -----------------------------------------------------------------------------
       
   266 // TMceSessionUtility::GetLocalMediaPortL   
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 EXPORT_C void TMceSessionUtility::GetLocalMediaPortL(CMceMediaStream* aStream, TUint& aMediaPort )
       
   270     {
       
   271     MCECLI_DEBUG("TMceSessionUtility::GetLocalMediaPortL: Entry ");
       
   272     __ASSERT_ALWAYS(aStream!=NULL, User::Leave(KErrArgument));
       
   273 
       
   274 	if ( (aStream->Session()->State() >= CMceSession::ECancelling) || 
       
   275 			(aStream->Session()->State() <= CMceSession::EIdle) )
       
   276         {
       
   277         MCECLI_DEBUG("TMceSessionUtility::GetRemoteConnectionAddrL :  Session state shouldn't be idle Or shouldn't be either in cancelling, terminating , terminated");
       
   278         User::Leave(KErrArgument);
       
   279         }
       
   280     
       
   281 	aMediaPort = aStream->FlatData()->LocalMediaPort();
       
   282 	MCECLI_DEBUG_DVALUE("TMceSessionUtility::GetLocalMediaPortL : ", aMediaPort)
       
   283 	MCECLI_DEBUG("TMceSessionUtility::GetLocalMediaPortL: Exit ");
       
   284     }
       
   285          
       
   286 
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // TMceSessionUtility::GetRemoteMediaPortL
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 EXPORT_C void TMceSessionUtility::GetRemoteMediaPortL(CMceMediaStream* aStream, 
       
   293         TUint& aMediaPort )
       
   294     {
       
   295     MCECLI_DEBUG("TMceSessionUtility::GetRemoteMediaPortL: Entry ");
       
   296     __ASSERT_ALWAYS(aStream!=NULL, User::Leave(KErrArgument));
       
   297     CMceSession* session = aStream->Session();
       
   298     __ASSERT_ALWAYS(session, User::Leave(KErrArgument));
       
   299     if ( (session->State() >= CMceSession::ECancelling) || 
       
   300             (session->State() <= CMceSession::EIdle) )
       
   301         {
       
   302         MCECLI_DEBUG("TMceSessionUtility::GetRemoteConnectionAddrL :  Session state shouldn't be idle Or shouldn't be either in cancelling, terminating , terminated");
       
   303         User::Leave(KErrArgument);
       
   304         }
       
   305     if (aStream->Type() == KMceMessage)
       
   306         {
       
   307         TBuf8<10> aAttrName(_L8("path"));
       
   308     
       
   309         // Get the remote end's path attribute and extracts the port number from that
       
   310         //ownership willbe transferred to us so have to free up later
       
   311         HBufC8* pathValue = NULL;
       
   312         pathValue = TMceSessionUtility::getRemoteMediaSDPAttrL(aStream, aAttrName);
       
   313     
       
   314         if (pathValue == NULL)
       
   315             {
       
   316             aMediaPort = aStream->FlatData()->RemoteMediaPort();
       
   317             return;
       
   318             }
       
   319         // 
       
   320         TUriParser8 parser;
       
   321         TInt parseValue = parser.Parse(pathValue->Des()); 
       
   322         MCECLI_DEBUG_DVALUE("\n CMceMsrpSource::SetHostAddrPortFromPathAttr: Parsed return value for MSRP URI: ",
       
   323                 parseValue);
       
   324             
       
   325         TBuf8<10> portBuf = parser.Extract(EUriPort);
       
   326             
       
   327         TBuf16<10> portBuf16;
       
   328         portBuf16.Copy(portBuf);
       
   329         TLex iLex(portBuf16);
       
   330         iLex.Val(aMediaPort);  // returns the port value
       
   331         delete pathValue;
       
   332         return;
       
   333         }
       
   334     else
       
   335         {
       
   336         // Get send streams  and use remotemediaport
       
   337         if ( aStream->Source()->Type() == KMceRTPSource)
       
   338             {
       
   339             // find the send stream and get the remote media port
       
   340             if (aStream->BoundStream() && 
       
   341                     aStream->BoundStreamL().Source() != NULL &&
       
   342                     aStream->BoundStreamL().Source()->Type() != KMceRTPSource)
       
   343                 {
       
   344                 aMediaPort = aStream->BoundStreamL().FlatData()->RemoteMediaPort();
       
   345                 return;
       
   346                 }
       
   347             // if the streams are not binded then find the send stream aong the session streams
       
   348             for (TInt i=0; i<session->Streams().Count(); i++)
       
   349                 {
       
   350                 if (session->Streams()[i]!=NULL && (session->Streams()[i]!=aStream) &&
       
   351                         session->Streams()[i]->Type() == aStream->Type())
       
   352                     {
       
   353                     if (session->Streams()[i]->Source()->Type()!=KMceRTPSource)
       
   354                         {
       
   355                         aMediaPort = session->Streams()[i]->FlatData()->RemoteMediaPort();
       
   356                         return;
       
   357                         }
       
   358                     } // inside if
       
   359                 } // For loop
       
   360             } // 
       
   361         }
       
   362     aMediaPort = aStream->FlatData()->RemoteMediaPort();
       
   363     MCECLI_DEBUG_DVALUE("TMceSessionUtility::GetRemoteMediaPortL : ", aMediaPort)
       
   364     MCECLI_DEBUG("TMceSessionUtility::GetRemoteMediaPortL: Exit ");
       
   365     }
       
   366 
       
   367 
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // TMceSessionUtility::FindAttributeL
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 HBufC8* TMceSessionUtility::FindAttributeL(CDesC8Array* asdpLines , const TDesC8& aAttrName)
       
   374     {
       
   375     // Put all those session level names in single DesC8 object and pass that to CSdpDocument
       
   376     TBool attrFound = EFalse; 
       
   377     CSdpAttributeField* attr=NULL;
       
   378     RStringF searchAttr = SdpCodecStringPool::StringPoolL().OpenFStringL(aAttrName);
       
   379     HBufC8* retValue = NULL;
       
   380     for (TInt i=0; i< asdpLines->MdcaCount()&& !attrFound; i++)
       
   381         {
       
   382         TBuf8<200> attrText(asdpLines->MdcaPoint(i));
       
   383         
       
   384         // media attrivutes can also contain 'b' &'c' lines so skip them 
       
   385         _LIT8( KMceSdpConnectionLineMarker, "c=" );
       
   386         _LIT8( KMceSdpBandwidthLineMarker, "b=" );
       
   387         
       
   388         // Look for media specific conneciton(c= ) line
       
   389         if ( (attrText.Find(KMceSdpConnectionLineMarker) >= 0) )
       
   390             {
       
   391             if (attrText.Find(aAttrName) >= 0) 
       
   392                 {
       
   393                 // get the conneciton filed value 
       
   394                 CSdpConnectionField* conn = NULL;
       
   395                 TRAPD(err, conn = CSdpConnectionField::DecodeL(attrText));
       
   396                 if (err != KErrNone)
       
   397                     {
       
   398                     searchAttr.Close();
       
   399                     _CLOSE_SDPCODEC_STRINGPOOL;
       
   400                     User::Leave(err);
       
   401                     }
       
   402                 __ASSERT_ALWAYS(conn!=NULL, User::Leave(KErrArgument));
       
   403                 retValue = conn->Address().Alloc();
       
   404                 attrFound = ETrue;
       
   405                 delete conn; // Free the memory for conn pointer
       
   406                 }
       
   407             }
       
   408         // Look for media specific bandwidth(b= ) line
       
   409         else if ( (attrText.Find(KMceSdpBandwidthLineMarker) >= 0) )
       
   410             {
       
   411             if ((attrText.Find(aAttrName) >= 0))
       
   412                 {
       
   413                 // get the bandwidth filed and return the value
       
   414                 CSdpBandwidthField* bandWidth = NULL;
       
   415                 TRAPD(err, bandWidth = CSdpBandwidthField::DecodeL(attrText));
       
   416                 if (err != KErrNone)
       
   417                     {
       
   418                     searchAttr.Close();
       
   419                     _CLOSE_SDPCODEC_STRINGPOOL;
       
   420                     User::Leave(err);
       
   421                     }
       
   422                 __ASSERT_ALWAYS(bandWidth!=NULL, User::Leave(KErrArgument));
       
   423                 TBuf8<20> value;
       
   424                 value.Num(bandWidth->Value());
       
   425                 retValue = value.Alloc();
       
   426                 attrFound = ETrue;
       
   427                 delete bandWidth;
       
   428                 }
       
   429             }
       
   430         else
       
   431             // Look for media specific attribute(a= ) lines
       
   432             {
       
   433             // Have to free up later point of time
       
   434             TRAPD(err, attr = CSdpAttributeField::DecodeL(attrText));
       
   435             if (err != KErrNone)
       
   436                 {
       
   437                 searchAttr.Close();
       
   438                 _CLOSE_SDPCODEC_STRINGPOOL;
       
   439                 User::Leave(err);
       
   440                 }
       
   441             if (attr == NULL)
       
   442                 {
       
   443                 MCECLI_DEBUG("TMceSessionUtility::FindAttributeL: Error in decoding the media attribute : ");
       
   444                 searchAttr.Close();
       
   445                 _CLOSE_SDPCODEC_STRINGPOOL;
       
   446                 User::Leave(KErrArgument);
       
   447                 }
       
   448             if (attr->Attribute() ==  searchAttr && !attrFound)
       
   449                 {
       
   450                 retValue = attr->Value().Alloc();
       
   451                 attrFound = ETrue;
       
   452                 }
       
   453             delete attr;
       
   454             }
       
   455         }
       
   456     if (attrFound == EFalse)
       
   457         {
       
   458         retValue = NULL;
       
   459         }
       
   460     searchAttr.Close();
       
   461     return retValue;
       
   462     }
       
   463 
       
   464 
       
   465 // -----------------------------------------------------------------------------
       
   466 // TMceSessionUtility::ConstructSdpDocumentL
       
   467 // -----------------------------------------------------------------------------
       
   468 //
       
   469 void TMceSessionUtility::ConstructSdpDocumentL(CDesC8Array* asdpLines, CSdpDocument*& asdpDoc)
       
   470     {
       
   471     if (asdpDoc)
       
   472         {
       
   473         delete asdpDoc;
       
   474         asdpDoc = NULL;
       
   475         }
       
   476     TBuf8<1000> text; 
       
   477     for(TInt i=0; i<asdpLines->MdcaCount(); i++)
       
   478         {
       
   479         TBuf8<200> temp(asdpLines->MdcaPoint(i));
       
   480         text.Append( temp );
       
   481         }
       
   482     TInt leaveValue = KErrNone;
       
   483     // Ownership for sdpDoc is tranferred to us.. 
       
   484     TRAP(leaveValue, asdpDoc = CSdpDocument::DecodeL(text));
       
   485     
       
   486     if (leaveValue != KErrNone)
       
   487         {
       
   488         MCECLI_DEBUG_DVALUE("TMceSessionUtility::ConstructSdpDocument: Error in decoding the SdpDocument text : %d", leaveValue);
       
   489         SdpCodecStringPool::Close();
       
   490         User::Leave(leaveValue);
       
   491         }    
       
   492     }