multimediacommsengine/mmceshared/src/mcecomaudiostream.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     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 
       
    21 #include "mceaudiostream.h"
       
    22 #include "mcecomaudiostream.h"
       
    23 #include "mcecomaudiocodec.h"
       
    24 #include "mcecommediasource.h"
       
    25 #include "mcecommediasink.h"
       
    26 #include "mcertpsource.h"
       
    27 #include "mcertpsink.h"
       
    28 #include "mceserial.h"
       
    29 #include "mceevents.h"
       
    30 #include "cleanupresetanddestroy.h"
       
    31 
       
    32 #ifdef MCE_COMMON_SERVER_SIDE
       
    33 
       
    34 #include <mmcccodecinformationfactory.h>
       
    35 #include <mmcccodecinformation.h>
       
    36 
       
    37     
       
    38 #endif//MCE_COMMON_SERVER_SIDE
       
    39 
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CMceComAudioStream::NewL
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CMceComAudioStream* CMceComAudioStream::NewL()
       
    49     {
       
    50     CMceComAudioStream* self = NewLC();
       
    51     CleanupStack::Pop( self );
       
    52     return self;
       
    53     }
       
    54 
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CMceComAudioStream::NewLC
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CMceComAudioStream* CMceComAudioStream::NewLC()
       
    61     {
       
    62     CMceComAudioStream* self = new (ELeave) CMceComAudioStream();
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CMceComAudioStream::~CMceComAudioStream
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CMceComAudioStream::~CMceComAudioStream()
       
    74     {
       
    75     iCodecs.ResetAndDestroy();
       
    76     }
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CMceComAudioStream::CMceComAudioStream
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CMceComAudioStream::CMceComAudioStream()
       
    84     :CMceComMediaStream( KMceAudio )
       
    85     {
       
    86     }
       
    87 
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CMceComAudioStream::AddCodecL
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 void CMceComAudioStream::AddCodecL( CMceComCodec* aCodec )
       
    94     {
       
    95     __ASSERT_ALWAYS( aCodec, User::Leave( KErrArgument ) );
       
    96 
       
    97     CMceComAudioCodec* codec = static_cast< CMceComAudioCodec* >( aCodec );
       
    98     iCodecs.AppendL( codec );
       
    99     }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CMceComAudioStream::RemoveCodecL
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CMceComAudioStream::RemoveCodecL( CMceComCodec* aCodec )
       
   107     {
       
   108     TInt index = FindCodecFromList( aCodec );
       
   109 
       
   110     if ( index >= 0 )
       
   111         {
       
   112         RemoveCodecFromListL( index );
       
   113         }
       
   114 
       
   115     delete aCodec;
       
   116     }
       
   117 
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CMceComAudioStream::CodecL
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 CMceComAudioCodec* CMceComAudioStream::CodecL( TInt aIndex ) const
       
   124 	{
       
   125 	__ASSERT_ALWAYS( aIndex >= 0 && aIndex < iCodecs.Count(),
       
   126 		User::Leave( KErrArgument ) );
       
   127 	
       
   128 	return iCodecs[aIndex];
       
   129 	}
       
   130 
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CMceComAudioStream::CodecCount
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 TInt CMceComAudioStream::CodecCount() const
       
   137 	{
       
   138 	return iCodecs.Count();
       
   139 	}
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CMceComAudioStream::Codecs
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 const RPointerArray<CMceComAudioCodec>& CMceComAudioStream::Codecs() const
       
   147     {
       
   148     return iCodecs;
       
   149     }
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CMceComAudioStream::FindCodecL
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 CMceComCodec* CMceComAudioStream::FindCodecL( CMceComCodec& aCodec )
       
   157     {
       
   158     CMceComCodec* foundCodec = NULL;
       
   159     CMceComAudioCodec* codec = static_cast<CMceComAudioCodec*>( &aCodec );
       
   160 
       
   161     TInt index = FindCodecFromList( codec );
       
   162     if ( index == KErrNotFound )
       
   163         {
       
   164         const TInt codecCount = CodecCount();
       
   165 
       
   166         for ( TInt i = 0 ; i < codecCount && !foundCodec; ++i )
       
   167             {
       
   168             if ( CodecL( i )->iSdpName.CompareF( aCodec.iSdpName ) == 0 )
       
   169                 {
       
   170                 foundCodec = CodecL( i );
       
   171                 }     
       
   172             }
       
   173         }
       
   174     else
       
   175         {
       
   176         foundCodec = CodecL( index );
       
   177         }
       
   178         
       
   179     return foundCodec;
       
   180     }
       
   181 
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CMceComAudioStream::FindCodecFromList
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 TInt CMceComAudioStream::FindCodecFromList( CMceComCodec* aCodec ) const
       
   188 	{
       
   189 	CMceComAudioCodec* codec = static_cast< CMceComAudioCodec* >( aCodec );
       
   190 	return iCodecs.Find( codec );
       
   191 	}
       
   192 
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CMceComAudioStream::RemoveCodecFromListL
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CMceComAudioStream::RemoveCodecFromListL( TInt aIndex )
       
   199 	{
       
   200 	__ASSERT_ALWAYS( aIndex >= 0 && aIndex < iCodecs.Count(),
       
   201 		User::Leave( KErrArgument ) );
       
   202 
       
   203 	iCodecs.Remove( aIndex );
       
   204 	}
       
   205 
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CMceComAudioStream::DestroyCodecs
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 void CMceComAudioStream::DestroyCodecs()
       
   212 	{
       
   213 	iCodecs.ResetAndDestroy();
       
   214 	}
       
   215 
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CMceComAudioStream::InternalizeFlatL
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 void CMceComAudioStream::InternalizeFlatL( RReadStream& aReadStream )
       
   222     {
       
   223     CMceComMediaStream::InternalizeFlatL( aReadStream );
       
   224     }
       
   225 
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CMceComAudioStream::ExternalizeFlatL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CMceComAudioStream::ExternalizeFlatL( RWriteStream& aWriteStream )
       
   232     {
       
   233     CMceComMediaStream::ExternalizeFlatL( aWriteStream );
       
   234     }
       
   235 
       
   236   
       
   237 // -----------------------------------------------------------------------------
       
   238 // CMceComAudioStream::InternalizeL
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CMceComAudioStream::InternalizeL( MMceComSerializationContext& aSerCtx )
       
   242     {
       
   243     CMceComMediaStream::InternalizeL( aSerCtx );
       
   244     
       
   245     TMceAudioStreamSerializer<CMceComAudioStream> serial( *this );
       
   246     serial.InternalizeL( aSerCtx );
       
   247         
       
   248     }
       
   249     
       
   250 // -----------------------------------------------------------------------------
       
   251 // CMceComAudioStream::ExternalizeL
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CMceComAudioStream::ExternalizeL( MMceComSerializationContext& aSerCtx )
       
   255     {
       
   256     CMceComMediaStream::ExternalizeL( aSerCtx );
       
   257     
       
   258     TMceAudioStreamSerializer<CMceComAudioStream> serial( *this );
       
   259     serial.ExternalizeL( aSerCtx );
       
   260     
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CMceComAudioStream::CloneL
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 CMceComMediaStream* CMceComAudioStream::CloneL()
       
   268     {
       
   269     CMceComAudioStream* copy = new (ELeave) CMceComAudioStream();
       
   270     CleanupStack::PushL( copy );
       
   271     copy->ConstructL( *this );
       
   272     CleanupStack::Pop( copy );
       
   273     return copy;
       
   274     }
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CMceComAudioStream::ConstructL
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CMceComAudioStream::ConstructL()
       
   281     {
       
   282     CMceComMediaStream::ConstructL();
       
   283     }
       
   284 
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CMceComAudioStream::ConstructL
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 void CMceComAudioStream::ConstructL( CMceComAudioStream& aStream )
       
   291     {
       
   292     CMceComMediaStream::ConstructL( aStream );
       
   293     
       
   294     const TInt codecCount = aStream.CodecCount();
       
   295 
       
   296     for ( TInt i = 0 ; i < codecCount ; ++i )
       
   297         {
       
   298         CMceComAudioCodec* codec = aStream.CodecL( i )->CloneL();
       
   299         CleanupStack::PushL( codec );
       
   300         AddCodecL( codec );
       
   301         CleanupStack::Pop( codec );
       
   302         }
       
   303     
       
   304     }
       
   305 
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CMceComAudioStream::ConstructL
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CMceComAudioStream::InitializeL( CMceComSession& aParent )
       
   312     {
       
   313     CMceComMediaStream::InitializeL( aParent );
       
   314 
       
   315 	const TInt codecCount = CodecCount();
       
   316 	
       
   317     for ( TInt i = 0 ; i < codecCount ; ++i )
       
   318         {
       
   319         CodecL( i )->InitializeL( *this );
       
   320         }
       
   321     }
       
   322 
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CMceComAudioStream::UpdateL
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CMceComAudioStream::UpdateL( CMceComMediaStream& aStream )
       
   329     {  
       
   330     CMceComMediaStream::UpdateL( aStream );
       
   331 
       
   332     CMceComAudioStream* stream = static_cast<CMceComAudioStream*>( &aStream );
       
   333     
       
   334     RPointerArray<CMceComAudioCodec> updated;
       
   335     
       
   336     MceCleanupResetAndDestroyPushL( updated );
       
   337 
       
   338     for ( TInt i = 0 ; i < stream->CodecCount() ; ++i )
       
   339         {
       
   340         CMceComAudioCodec* update = stream->CodecL( i );
       
   341         TBool isUpdated = EFalse;
       
   342         TInt j = 0;
       
   343 
       
   344         while( !isUpdated && j < CodecCount() )
       
   345             {
       
   346             CMceComAudioCodec* codec = CodecL( j );
       
   347             if ( codec->Id() == update->Id() )
       
   348                 {
       
   349                 codec->UpdateL( *update );
       
   350                 stream->RemoveCodecFromListL( i );
       
   351                 delete update;
       
   352                 updated.AppendL( codec );
       
   353                 RemoveCodecFromListL( j );
       
   354                 i--;
       
   355                 isUpdated = ETrue;
       
   356                 }
       
   357             j++;
       
   358             }
       
   359         }
       
   360         
       
   361     if ( CodecCount() > 0 )	//	Codecs have been removed
       
   362         {
       
   363         Session()->IsStructureChanged() = ETrue;
       
   364         }
       
   365         
       
   366     DestroyCodecs();
       
   367     
       
   368     while( updated.Count() > 0 )
       
   369         {
       
   370         AddCodecL( updated[0] );
       
   371         updated.Remove( 0 );
       
   372         }
       
   373         
       
   374     CleanupStack::PopAndDestroy();	// updated
       
   375             
       
   376     if ( stream->CodecCount() > 0 )	// Streams have been added
       
   377         {
       
   378         Session()->IsStructureChanged() = ETrue;
       
   379             
       
   380         while( stream->CodecCount() > 0 )
       
   381             {
       
   382             CMceComAudioCodec* add = stream->CodecL( 0 );
       
   383             AddCodecL( add );
       
   384             stream->RemoveCodecFromListL( 0 );
       
   385             }
       
   386         }
       
   387     }
       
   388 
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CMceComAudioStream::ReorderCodecs
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 void CMceComAudioStream::ReorderCodecs()
       
   395     {
       
   396     TLinearOrder<CMceComAudioCodec> order( CMceComAudioCodec::CompareSdpIndex );
       
   397     
       
   398     iCodecs.Sort( order );
       
   399     }
       
   400 
       
   401 
       
   402 #ifdef MCE_COMMON_SERVER_SIDE
       
   403 
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CMceComAudioStream::SynchronizeL
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 void CMceComAudioStream::SynchronizeL( TBool aRoleAnswerer )
       
   410     {
       
   411     TInt codecIndex( 0 );
       
   412 	
       
   413     while ( codecIndex < CodecCount() )
       
   414         {
       
   415         CMceComCodec& codec = *CodecL( codecIndex );
       
   416         
       
   417         //When role is offerer stream cannot remove codecs. 
       
   418         //It will be done by mediamanager together with mcc
       
   419         //if there is session update decoded there might be codecs
       
   420         //which are not valid anymore, and thus, needed to be removed.
       
   421         TBool remove( !codec.iIsNegotiated && aRoleAnswerer );
       
   422         
       
   423         if ( BoundStream() )
       
   424             {
       
   425             CMceComCodec* sync = BoundStreamL().FindCodecL( codec );
       
   426             
       
   427             if ( remove )
       
   428                 {
       
   429                 BoundStreamL().RemoveCodecL( sync );
       
   430                 }
       
   431             else if ( sync )
       
   432                 {
       
   433                 sync->SynchronizeL( codec, aRoleAnswerer );
       
   434                 }
       
   435             else if ( aRoleAnswerer )
       
   436                 {
       
   437                 MCE_DEFINE_AUDIO_CODEC( audioCodec, codec );
       
   438                 CMceComAudioCodec* copy = audioCodec.CloneL();
       
   439                 CleanupStack::PushL( copy );
       
   440                 copy->InitializeL( BoundStreamL() );
       
   441                 MCE_AUDIO( BoundStreamL() ).AddCodecL( copy );
       
   442                 CleanupStack::Pop( copy );
       
   443                 }
       
   444             else
       
   445                 {
       
   446                 //NOP
       
   447                 }
       
   448             }
       
   449             
       
   450         if ( remove )
       
   451             {
       
   452             RemoveCodecL( &codec );
       
   453             }
       
   454         else
       
   455             {
       
   456             codecIndex++;
       
   457             }
       
   458         }
       
   459     }
       
   460 
       
   461 
       
   462 // -----------------------------------------------------------------------------
       
   463 // CMceComAudioStream::UpdateDefaultCodecL
       
   464 // -----------------------------------------------------------------------------
       
   465 //
       
   466 void CMceComAudioStream::UpdateDefaultCodecL()
       
   467     {
       
   468     CMceComCodec* highestPrioritySendCodec = NULL;
       
   469     CMceComCodec* codec = NULL;
       
   470 
       
   471     const RPointerArray< CMceComCodec >& allCodecs = reinterpret_cast< const RPointerArray<CMceComCodec>& >( Codecs() );
       
   472     CMceComCodec::TIterator codecs( allCodecs, 
       
   473                                     CMceComCodec::TIterator::EFilterIsNegotiated );
       
   474     
       
   475     MCE_ITERATOR_FIND_NEXT( codecs, highestPrioritySendCodec, 
       
   476                             highestPrioritySendCodec->SendSupported() );
       
   477     
       
   478     // If no negotiated send codecs, disable all.
       
   479     // If send codec changed, enable new one and disable other send codecs.                                       
       
   480     if ( !highestPrioritySendCodec ||
       
   481        ( highestPrioritySendCodec &&
       
   482          !highestPrioritySendCodec->iIsEnabled ) )
       
   483         {
       
   484         TBool newDefaultCodecSet( EFalse );
       
   485         codecs.Reset();
       
   486         codecs.SetFilter( CMceComCodec::TIterator::EFilterNone );
       
   487 
       
   488         while( codecs.Next( codec ) )
       
   489             {
       
   490             if ( !newDefaultCodecSet && codec->iIsNegotiated && codec->SendSupported() )
       
   491                 {
       
   492                 codec->SetEnabled( ETrue );
       
   493                 newDefaultCodecSet = ETrue;
       
   494                 }
       
   495             else
       
   496                 {
       
   497                 codec->SetEnabled( EFalse );
       
   498                 }
       
   499             }
       
   500         }
       
   501     }
       
   502 
       
   503 
       
   504 // -----------------------------------------------------------------------------
       
   505 // CMceComAudioStream::PrepareL
       
   506 // -----------------------------------------------------------------------------
       
   507 //
       
   508 void CMceComAudioStream::PrepareL()
       
   509     {
       
   510     __ASSERT_ALWAYS ( CodecCount() > 0, User::Leave( KErrArgument ) );
       
   511 
       
   512     if ( BoundStream() )
       
   513     	{
       
   514     	MCE_DEFINE_AUDIO( boundStream, BoundStreamL() );
       
   515         __ASSERT_ALWAYS ( boundStream.CodecCount() > 0, User::Leave( KErrArgument ) );
       
   516     	}
       
   517     }
       
   518 
       
   519 
       
   520 // -----------------------------------------------------------------------------
       
   521 // CMceComAudioStream::MccStreamType
       
   522 // -----------------------------------------------------------------------------
       
   523 //
       
   524 TInt CMceComAudioStream::MccStreamType() const
       
   525     {
       
   526     
       
   527     TInt type = KMccAudioLocalStream;
       
   528     
       
   529     if ( iStreamType == EReceiveStream || 
       
   530          iStreamType == EReceiveOnlyStream )
       
   531         {
       
   532         type = KMccAudioDownlinkStream;
       
   533         }
       
   534     else if ( iStreamType == ESendStream || 
       
   535               iStreamType == ESendOnlyStream )
       
   536         {
       
   537         type = KMccAudioUplinkStream;
       
   538         }
       
   539     else
       
   540         {
       
   541         type = KMccAudioLocalStream;
       
   542         }
       
   543         
       
   544     return type;
       
   545     
       
   546     }
       
   547 
       
   548 
       
   549 // -----------------------------------------------------------------------------
       
   550 // CMceComAudioStream::MccStreamType
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 TInt CMceComAudioStream::MccLinkType() const
       
   554     {
       
   555     TInt type = KMccLinkGeneral;
       
   556     
       
   557     if ( iStreamType == ELocalStream )
       
   558         {
       
   559         type = KMccLinkLocal;
       
   560         }
       
   561     if (iSession->iClientCryptoSuites.Count()>0)
       
   562     	{
       
   563     	type =KMccLinkSecure;
       
   564     	}
       
   565     return type;        
       
   566     }
       
   567 
       
   568 #endif//MCE_COMMON_SERVER_SIDE