multimediacommsengine/mmcecli/src/mcemediastream.cpp
changeset 0 1bce908db942
child 49 64c62431ac08
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 "mcemediastream.h"
       
    22 #include "mcesession.h"
       
    23 #include "mcemanager.h"
       
    24 #include "mcestreamobserver.h"
       
    25 #include "mcertpobserver.h"
       
    26 #include "mcecommediastream.h"
       
    27 #include "mcefactory.h"
       
    28 #include "mceserial.h"
       
    29 #include "mceevents.h"
       
    30 #include "mceclilogs.h"
       
    31 #include "mcesessionobserver.h"
       
    32 
       
    33 #define _FLAT_DATA static_cast<CMceComMediaStream*>( iFlatData )
       
    34 #define FLAT_DATA( data ) _FLAT_DATA->data
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CMceMediaStream::~CMceMediaStream
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 EXPORT_C CMceMediaStream::~CMceMediaStream()
       
    44     {
       
    45     
       
    46     MCECLI_DEBUG("CMceMediaStream::~CMceMediaStream, Entry");
       
    47     MCECLI_DEBUG_DVALUE("id", iFlatData ? Id().iId : KMceNotAssigned );
       
    48     
       
    49     if ( iSource )
       
    50         {
       
    51         DeleteSource();
       
    52         }
       
    53     
       
    54     if ( _FLAT_DATA && FLAT_DATA( iLinkOwner ) )
       
    55 	    {
       
    56     	delete iLinkedStream;
       
    57 	    }
       
    58 
       
    59 	while( iSinks.Count() > 0 )
       
    60 	    {
       
    61 	    DeleteSink( 0 );
       
    62 	    }
       
    63 	    
       
    64     iSinks.Reset();
       
    65     iSinks.Close();
       
    66     
       
    67     delete iFlatData;
       
    68     MCECLI_DEBUG("CMceMediaStream::~CMceMediaStream, Exit");
       
    69     }
       
    70     
       
    71 // -----------------------------------------------------------------------------
       
    72 // CMceMediaStream::Type
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C TMceMediaType CMceMediaStream::Type() const
       
    76     {
       
    77     return FLAT_DATA( iType );
       
    78     }
       
    79     
       
    80 // -----------------------------------------------------------------------------
       
    81 // CMceMediaStream::SetMediaAttributeLinesL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C void CMceMediaStream::SetMediaAttributeLinesL(
       
    85     CDesC8Array* aMediaSDPLines )
       
    86     {
       
    87     MCECLI_DEBUG("CMceMediaStream::SetMediaAttributeLinesL, Entry");
       
    88     User::LeaveIfError( !iSession || 
       
    89                       ( iSession->State() == CMceSession::EIdle ||
       
    90 		                iSession->State() == CMceSession::EIncoming ||
       
    91 		                iSession->State() == CMceSession::EEstablished ) ? 
       
    92 		                KErrNone : KErrNotReady );
       
    93     
       
    94     CDesC8Array* lines = NULL;
       
    95     TBool linesPushed = EFalse;
       
    96     
       
    97     if ( aMediaSDPLines )
       
    98         {
       
    99         lines = aMediaSDPLines;
       
   100         }
       
   101     else
       
   102         {
       
   103         lines = new (ELeave) CDesC8ArrayFlat( KMceArrayGranularity );
       
   104         CleanupStack::PushL( lines );
       
   105         linesPushed = ETrue;
       
   106         }
       
   107         
       
   108     if ( iLinkedStream )
       
   109         {
       
   110         CDesC8Array* copyLines = 
       
   111             new (ELeave) CDesC8ArrayFlat( KMceArrayGranularity );
       
   112         CleanupStack::PushL( copyLines );
       
   113         for ( TInt i = 0; i < lines->MdcaCount(); i++ )
       
   114             {
       
   115             copyLines->AppendL( lines->MdcaPoint( i ) );
       
   116             }
       
   117         delete iLinkedStream->iFlatData->iLocalMediaSDPLines;
       
   118         iLinkedStream->iFlatData->iLocalMediaSDPLines = copyLines;
       
   119         CleanupStack::Pop( copyLines );
       
   120         }
       
   121         
       
   122     if ( linesPushed )
       
   123         {
       
   124         CleanupStack::Pop( lines );
       
   125         }
       
   126         
       
   127     delete FLAT_DATA( iLocalMediaSDPLines );
       
   128     FLAT_DATA( iLocalMediaSDPLines ) = lines;
       
   129     MCECLI_DEBUG("CMceMediaStream::SetMediaAttributeLinesL, Exit");
       
   130     }
       
   131     
       
   132 // -----------------------------------------------------------------------------
       
   133 // CMceMediaStream::MediaAttributeLinesL
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 EXPORT_C MDesC8Array* CMceMediaStream::MediaAttributeLinesL()
       
   137     {
       
   138     CDesC8Array*  lines = new (ELeave) CDesC8ArrayFlat( KMceArrayGranularity );
       
   139     CleanupStack::PushL( lines );
       
   140     for ( TInt i=0; i < FLAT_DATA( iRemoteMediaSDPLines )->Count(); i++ )
       
   141         {
       
   142         lines->AppendL( FLAT_DATA( iRemoteMediaSDPLines )->MdcaPoint( i ) );
       
   143         }
       
   144     CleanupStack::Pop( lines );
       
   145     return lines;
       
   146     }
       
   147         
       
   148 // -----------------------------------------------------------------------------
       
   149 // CMceMediaStream::SetStartMediaPort
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C void CMceMediaStream::SetLocalMediaPortL( TUint aLocalMediaPort )
       
   153     {
       
   154     MCECLI_DEBUG("CMceMediaStream::SetLocalMediaPortL, Entry");
       
   155     MCECLI_DEBUG_DVALUE("port", aLocalMediaPort );
       
   156     
       
   157     FLAT_DATA( iLocalMediaPort ) = aLocalMediaPort;
       
   158 
       
   159     MCECLI_DEBUG("CMceMediaStream::SetLocalMediaPortL, Exit");
       
   160     
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CMceMediaStream::StartMediaPort
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 EXPORT_C TUint CMceMediaStream::LocalMediaPort() const        
       
   168     {
       
   169     return FLAT_DATA( iLocalMediaPort );
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CMceMediaStream::Session
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 EXPORT_C CMceSession* CMceMediaStream::Session() const
       
   177     {
       
   178     return iSession;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CMceMediaStream::EnableL
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CMceMediaStream::EnableL() 
       
   186     {
       
   187     MCECLI_DEBUG("CMceMediaStream::EnableL, Entry");
       
   188     
       
   189     if ( iSource )
       
   190         {
       
   191     	iSource->Enabled( ETrue );
       
   192         }
       
   193         
       
   194     for ( TInt i = 0; i < iSinks.Count(); i++ )
       
   195         {
       
   196         iSinks[ i ]->Enabled( ETrue );
       
   197         }
       
   198         
       
   199     FLAT_DATA( iIsEnabled ) = ETrue;
       
   200     MCECLI_DEBUG("CMceMediaStream::EnableL, Exit");
       
   201     
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CMceMediaStream::DisableL
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 void CMceMediaStream::DisableL() 
       
   209     {
       
   210     MCECLI_DEBUG("CMceMediaStream::DisableL, Entry");
       
   211     
       
   212     if ( iSource )
       
   213         {
       
   214     	iSource->Enabled( EFalse );
       
   215         }
       
   216         
       
   217     for ( TInt i = 0; i < iSinks.Count(); i++ )
       
   218         {
       
   219         iSinks[ i ]->Enabled( EFalse );
       
   220         }
       
   221     
       
   222     FLAT_DATA( iIsEnabled ) = EFalse;
       
   223 
       
   224     MCECLI_DEBUG("CMceMediaStream::DisableL, Exit");
       
   225     
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CMceMediaStream::IsEnabled
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 EXPORT_C TBool CMceMediaStream::IsEnabled() const
       
   233     {
       
   234     return FLAT_DATA( iIsEnabled );
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CMceMediaStream::SetSourceL
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 EXPORT_C void CMceMediaStream::SetSourceL( CMceMediaSource* aSource )
       
   242     {
       
   243     if ( aSource )
       
   244         {
       
   245         aSource->ReferenceCount()++;
       
   246         }
       
   247     
       
   248     if ( iSource )
       
   249         {
       
   250         DeleteSource();
       
   251         }
       
   252     iSource = aSource;
       
   253     
       
   254     }
       
   255 
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CMceMediaStream::AddSinkL
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 EXPORT_C void CMceMediaStream::AddSinkL( CMceMediaSink* aSink )
       
   262     {
       
   263     __ASSERT_ALWAYS( aSink, User::Leave( KErrArgument ) );
       
   264     __ASSERT_ALWAYS( iSinks.Count() == 0, User::Leave( KErrNotSupported ) );
       
   265     iSinks.AppendL( aSink );
       
   266     aSink->ReferenceCount()++;    
       
   267     }
       
   268 
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CMceMediaStream::Source
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 EXPORT_C CMceMediaSource* CMceMediaStream::Source() const
       
   275     {
       
   276     return iSource;
       
   277     }
       
   278 
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CMceMediaStream::Sinks
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C const RPointerArray<CMceMediaSink>& CMceMediaStream::Sinks() const
       
   285     {
       
   286     return iSinks;
       
   287     }
       
   288 
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CMceMediaStream::RemoveSinkL
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 EXPORT_C void CMceMediaStream::RemoveSinkL( CMceMediaSink& aSink )
       
   295     {
       
   296 	for ( TInt i = 0; i < iSinks.Count(); i++ )
       
   297 	    {
       
   298 	    if ( iSinks[i] == &aSink )
       
   299 	        {
       
   300 	        DeleteSink( i );
       
   301 	        iSinks.Compress();
       
   302 	        return;
       
   303 	        }
       
   304 	    }
       
   305 	User::Leave( KErrNotFound );
       
   306     }
       
   307 
       
   308 
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CMceMediaStream::BindL
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 EXPORT_C void CMceMediaStream::BindL( CMceMediaStream* aStream )
       
   315     {
       
   316     __ASSERT_ALWAYS( !(! FLAT_DATA( iLinkOwner ) &&  
       
   317                          FLAT_DATA( iLinkedStream ) ) , 
       
   318                          User::Leave( KErrArgument ) );
       
   319     
       
   320     if ( aStream )
       
   321         {
       
   322         FLAT_DATA(iLinkOwner) = ETrue;
       
   323         aStream->DoBindL( this );
       
   324         DoBindL( aStream );
       
   325         }
       
   326     else
       
   327         {
       
   328         FLAT_DATA(iLinkOwner) = EFalse;
       
   329     	delete iLinkedStream;
       
   330     	iLinkedStream = NULL;
       
   331         }
       
   332     
       
   333     }
       
   334 
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CMceMediaStream::BoundStreamL
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 EXPORT_C CMceMediaStream& CMceMediaStream::BoundStreamL() const
       
   341     {
       
   342     __ASSERT_ALWAYS( iLinkedStream , User::Leave( KErrNotFound ) );
       
   343     
       
   344     return *iLinkedStream;
       
   345     
       
   346     }
       
   347 
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CMceMediaStream::BoundStream
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 EXPORT_C TBool CMceMediaStream::BoundStream() const
       
   354     {
       
   355     return iLinkedStream != NULL;
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CMceMediaStream::State
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 EXPORT_C CMceMediaStream::TState CMceMediaStream::State() const
       
   363     {
       
   364     return FLAT_DATA( iState );
       
   365     }
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CMceMediaStream::InternalizeFlatL
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 void CMceMediaStream::InternalizeFlatL( RReadStream& aReadStream )
       
   372     {
       
   373     __ASSERT_ALWAYS( iFlatData , User::Leave( KErrNotReady ) );
       
   374     _FLAT_DATA->InternalizeFlatL( aReadStream );
       
   375             
       
   376     }
       
   377     
       
   378 // -----------------------------------------------------------------------------
       
   379 // CMceMediaStream::ExternalizeFlatL
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 void CMceMediaStream::ExternalizeFlatL( RWriteStream& aWriteStream )
       
   383     {
       
   384     __ASSERT_ALWAYS( iFlatData , User::Leave( KErrNotReady ) );
       
   385     _FLAT_DATA->ExternalizeFlatL( aWriteStream );
       
   386     
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CMceMediaStream::SerializationId
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 TUint64 CMceMediaStream::SerializationId() const
       
   394     {
       
   395     return FLAT_DATA( SerializationId() );
       
   396     }
       
   397 
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CMceMediaStream::InternalizeL
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 void CMceMediaStream::InternalizeL( MMceComSerializationContext& aSerCtx )
       
   404     {
       
   405     TMceMediaStreamSerializer<CMceMediaStream> serial( *this, _FLAT_DATA );
       
   406     serial.InternalizeL( aSerCtx );
       
   407     }
       
   408 
       
   409 // -----------------------------------------------------------------------------
       
   410 // CMceMediaStream::ExternalizeL
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CMceMediaStream::ExternalizeL( MMceComSerializationContext& aSerCtx )
       
   414     {
       
   415     
       
   416     TMceMediaStreamSerializer<CMceMediaStream> serial( *this, _FLAT_DATA );
       
   417     serial.ExternalizeL( aSerCtx );
       
   418     
       
   419     }
       
   420 
       
   421     
       
   422 // -----------------------------------------------------------------------------
       
   423 // CMceMediaStream::SetSourceL
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void CMceMediaStream::SetSourceL( MMceComSerializationContext& aSerCtx )
       
   427     {
       
   428 
       
   429     RReadStream& readStream = aSerCtx.ReadStream();
       
   430 
       
   431     TBool externalizedReference = readStream.ReadUint8L();
       
   432     
       
   433     if ( externalizedReference )
       
   434         {        
       
   435         TMceMediaId id;
       
   436         MceSerial::DecodeL( id, readStream );
       
   437         CMceMediaSource* source = NULL;
       
   438         TInt index = 0;
       
   439         MCECLI_DEBUG_DVALUE("CMceMediaStream::SetSourceL, using cached source. id", id.iId );
       
   440         
       
   441         while( !source && index < aSerCtx.SourceCache().Count() )
       
   442             {
       
   443             source = static_cast<CMceMediaSource*>( aSerCtx.SourceCache()[ index++ ] );
       
   444             source = source->SerializationId() == id.Uint64() ? source : NULL;
       
   445             }
       
   446         __ASSERT_ALWAYS( source, User::Leave( KErrNotFound ) );
       
   447         SetSourceL( source );
       
   448         }
       
   449     else
       
   450         {
       
   451         CMceMediaSource* source = 
       
   452             BaseFactory().SourceFactory().CreateLC( aSerCtx );
       
   453         SetSourceL( source );
       
   454         CleanupStack::Pop( source );
       
   455         aSerCtx.SourceCache().AppendL( source );
       
   456         
       
   457         }
       
   458     }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CMceMediaStream::AddSinkL
       
   462 // -----------------------------------------------------------------------------
       
   463 //
       
   464 void CMceMediaStream::AddSinkL( MMceComSerializationContext& aSerCtx )
       
   465     {
       
   466 
       
   467     RReadStream& readStream = aSerCtx.ReadStream();
       
   468 
       
   469     TBool externalizedReference = readStream.ReadUint8L();
       
   470     
       
   471     if ( externalizedReference )
       
   472         {
       
   473         TMceMediaId id;
       
   474         MceSerial::DecodeL( id, readStream );
       
   475         CMceMediaSink* sink = NULL;
       
   476         TInt index = 0;
       
   477         MCECLI_DEBUG_DVALUE("CMceMediaStream::AddSinkL, using cached sink. id", id.iId );
       
   478         while( !sink && index < aSerCtx.SinkCache().Count() )
       
   479             {
       
   480             sink = static_cast<CMceMediaSink*>( aSerCtx.SinkCache()[ index++ ] );
       
   481             sink = sink->SerializationId() == id.Uint64() ? sink : NULL;
       
   482             }
       
   483         __ASSERT_ALWAYS( sink, User::Leave( KErrNotFound ) );
       
   484         AddSinkL( sink );
       
   485         }
       
   486     else
       
   487         {
       
   488         CMceMediaSink* sink = 
       
   489             BaseFactory().SinkFactory().CreateLC( aSerCtx );
       
   490         AddSinkL( sink );
       
   491         CleanupStack::Pop( sink );
       
   492         aSerCtx.SinkCache().AppendL( sink );
       
   493         
       
   494         }
       
   495     
       
   496     }
       
   497 
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // CMceMediaStream::BaseFactory
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 TMceFactory CMceMediaStream::BaseFactory() const
       
   504     {
       
   505     return TMceFactory();
       
   506     }
       
   507 
       
   508 
       
   509 // -----------------------------------------------------------------------------
       
   510 // CMceMediaStream::Factory
       
   511 // -----------------------------------------------------------------------------
       
   512 //
       
   513 TMceMediaStreamFactory CMceMediaStream::Factory()
       
   514     {
       
   515     return TMceMediaStreamFactory();
       
   516     }
       
   517 
       
   518 
       
   519 // -----------------------------------------------------------------------------
       
   520 // CMceMediaStream::Binder
       
   521 // -----------------------------------------------------------------------------
       
   522 //
       
   523 TBool CMceMediaStream::Binder() const
       
   524     {
       
   525     return _FLAT_DATA->Binder();
       
   526     }
       
   527 
       
   528 
       
   529 // -----------------------------------------------------------------------------
       
   530 // CMceMediaStream::Id
       
   531 // -----------------------------------------------------------------------------
       
   532 //
       
   533 TMceMediaId CMceMediaStream::Id() const
       
   534     {
       
   535     return FLAT_DATA( iID );
       
   536     }
       
   537     
       
   538 
       
   539 // -----------------------------------------------------------------------------
       
   540 // CMceMediaStream::InitializeL
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 void CMceMediaStream::InitializeL( 
       
   544     CMceSession& aParent, 
       
   545     TBool aDiscardUnusedCodecs )
       
   546     {
       
   547     iSession = &aParent;
       
   548 
       
   549     if ( !Id().IsAssigned() )
       
   550         {
       
   551         FLAT_DATA( iID ) = iSession->Manager().NextMediaId();
       
   552         }
       
   553     
       
   554     ReorderCodecsByPreferenceL( aDiscardUnusedCodecs ); 
       
   555     
       
   556     if ( Binder() && BoundStream() )
       
   557         {
       
   558         iLinkedStream->InitializeL( aParent, aDiscardUnusedCodecs );
       
   559         }
       
   560         
       
   561     if ( iSource )
       
   562         {
       
   563         iSource->InitializeL( *this );
       
   564         }
       
   565         
       
   566     for( TInt i = 0; i < iSinks.Count();i++)
       
   567         {
       
   568         iSinks[i]->InitializeL( *this );
       
   569         }
       
   570     }
       
   571 
       
   572 
       
   573 // -----------------------------------------------------------------------------
       
   574 // CMceMediaStream::InitializeL
       
   575 // -----------------------------------------------------------------------------
       
   576 //
       
   577 void CMceMediaStream::InitializeL( CMceManager* aManager, CMceSession& aSession )
       
   578     {
       
   579     __ASSERT_ALWAYS( aManager, User::Leave( KErrArgument ) );
       
   580     iSession = &aSession;
       
   581     
       
   582     if ( Binder() && BoundStream() )
       
   583         {
       
   584         iLinkedStream->InitializeL( aManager, aSession );
       
   585         }
       
   586         
       
   587     if ( iSource )
       
   588         {
       
   589         iSource->InitializeL( aManager );
       
   590         }
       
   591         
       
   592     for( TInt i = 0; i < iSinks.Count();i++)
       
   593         {
       
   594         iSinks[i]->InitializeL( aManager );
       
   595         }
       
   596     
       
   597     if ( State() == EUninitialized )
       
   598         {
       
   599         SetState( EInitialized );
       
   600         }
       
   601               
       
   602     }
       
   603 
       
   604 // -----------------------------------------------------------------------------
       
   605 // CMceMediaStream::AddedL
       
   606 // -----------------------------------------------------------------------------
       
   607 //
       
   608 void CMceMediaStream::AddedL()
       
   609     {
       
   610     
       
   611     if ( Binder() && BoundStream() )
       
   612         {
       
   613         iLinkedStream->AddedL();
       
   614         }
       
   615         
       
   616     if ( iSource )
       
   617         {
       
   618         iSource->StreamAddedL( *this );
       
   619         }
       
   620         
       
   621     for( TInt i = 0; i < iSinks.Count();i++)
       
   622         {
       
   623         iSinks[i]->StreamAddedL( *this );
       
   624         }
       
   625 
       
   626     }
       
   627     
       
   628     
       
   629 // -----------------------------------------------------------------------------
       
   630 // CMceMediaStream::EventReceivedL
       
   631 // -----------------------------------------------------------------------------
       
   632 //
       
   633 TInt CMceMediaStream::EventReceivedL( TMceEvent& aEvent )
       
   634     {
       
   635     MCECLI_DEBUG("CMceMediaStream::EventReceivedL, Entry");
       
   636     MCECLI_DEBUG_DVALUES2("this: type", Type(), "id", Id().iId, 
       
   637                           "main", ( !BoundStream() || Binder() ) );
       
   638     
       
   639     TInt status = KMceEventNotOwner;
       
   640     TBool idMatch = aEvent.Id().iMediaID == Id();
       
   641     if ( idMatch )
       
   642         {
       
   643         if ( aEvent.Id().IsMediaId() )
       
   644             {
       
   645             status = HandleEvent( aEvent );
       
   646             }
       
   647         else
       
   648             {
       
   649             status = KMceEventNotConsumed;
       
   650             }
       
   651         }
       
   652     else
       
   653         {
       
   654         //NOP
       
   655         //might be for linked stream
       
   656         }
       
   657 
       
   658     if ( status == KMceEventConsumed || status == KMceEventUpdate )
       
   659         {
       
   660         MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, before exit. enabled", IsEnabled() );
       
   661         TState state = State();
       
   662         MCECLI_DEBUG_MEDIASTATE("CMceMediaStream::EventReceivedL, before exit. state", state );
       
   663         MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, Exit. status", status );
       
   664         return status;
       
   665         }
       
   666 
       
   667     //try binded
       
   668     if ( status == KMceEventNotOwner )
       
   669         {
       
   670         if ( Binder() && BoundStream() )
       
   671             {
       
   672             status = iLinkedStream->EventReceivedL( aEvent );
       
   673             if ( status == KMceEventUpdate )
       
   674                 {
       
   675                 delete iLinkedStream;
       
   676                 iLinkedStream = NULL;
       
   677                 CMceMsgObject<CMceMediaStream>* updateMsg = 
       
   678                     static_cast<CMceMsgObject<CMceMediaStream>*>( aEvent.Message() );
       
   679                 CMceMediaStream* updateStream = updateMsg->Object();
       
   680                 CleanupStack::PushL( updateStream );
       
   681                 BindL( updateStream );
       
   682                 CleanupStack::Pop( updateStream );
       
   683                 InitializeL( *(Session()), EFalse );
       
   684                 updateStream->Updated();
       
   685                 status = KMceEventConsumed;
       
   686                 }
       
   687             }
       
   688         MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, before exit. enabled", IsEnabled() );
       
   689         TState state = State();
       
   690         MCECLI_DEBUG_MEDIASTATE("CMceMediaStream::EventReceivedL, before exit. state", state );
       
   691         MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, Entry. status", status );
       
   692         return status;
       
   693         }
       
   694         
       
   695     //try source
       
   696     if ( aEvent.Id().IsSourceId() )
       
   697         {
       
   698         if ( iSource )
       
   699             {
       
   700             TInt sourceStatus = iSource->EventReceivedL( aEvent );
       
   701             if ( sourceStatus == KMceEventUpdate )
       
   702                 {
       
   703                 CMceMsgObject<CMceMediaSource>* updateMsg = 
       
   704                     static_cast<CMceMsgObject<CMceMediaSource>*>( aEvent.Message() );
       
   705                 CMceMediaSource* updateSource = updateMsg->iObject;
       
   706                 iSource->UpdateL( *updateSource );
       
   707                 iSource->Updated();
       
   708                 sourceStatus = KMceEventConsumed;
       
   709                 }
       
   710             status = sourceStatus == KMceEventNotOwner ? 
       
   711                 KMceEventNotConsumed : sourceStatus;
       
   712             }
       
   713         return status;
       
   714         }
       
   715         
       
   716     //try sinks
       
   717     if ( aEvent.Id().IsSinkId() )
       
   718         {
       
   719         TInt sinkStatus = status;
       
   720         TInt j = 0;
       
   721         while ( sinkStatus != KMceEventConsumed && j < iSinks.Count() )
       
   722             {
       
   723             CMceMediaSink* sink = iSinks[j]; 
       
   724             sinkStatus = sink->EventReceivedL( aEvent );
       
   725             if ( sinkStatus == KMceEventUpdate )
       
   726                 {
       
   727                 CMceMsgObject<CMceMediaSink>* updateMsg = 
       
   728                     static_cast<CMceMsgObject<CMceMediaSink>*>( aEvent.Message() );
       
   729                 CMceMediaSink* updateSink = updateMsg->iObject;
       
   730                 sink->UpdateL( *updateSink );
       
   731                 sink->Updated();
       
   732                 sinkStatus = KMceEventConsumed;
       
   733                 }
       
   734             j++;                
       
   735             }
       
   736         status = sinkStatus == KMceEventNotOwner ? 
       
   737             KMceEventNotConsumed : sinkStatus;        
       
   738         }
       
   739 
       
   740     MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, before exit. enabled", IsEnabled() );
       
   741     TState state = State();
       
   742     MCECLI_DEBUG_MEDIASTATE("CMceMediaStream::EventReceivedL, before exit. state", state );
       
   743     MCECLI_DEBUG_DVALUE("CMceMediaStream::EventReceivedL, Exit. status", status );
       
   744     return status;    
       
   745     
       
   746     
       
   747     }
       
   748 
       
   749 
       
   750 // -----------------------------------------------------------------------------
       
   751 // CMceMediaStream::Updated
       
   752 // -----------------------------------------------------------------------------
       
   753 //
       
   754 void CMceMediaStream::Updated()
       
   755     {
       
   756     MMceStreamObserver* observer = iSession->Manager().MediaObserver();
       
   757     
       
   758     if ( observer )
       
   759         {
       
   760         observer->StreamStateChanged( *this );
       
   761         }
       
   762     
       
   763     }
       
   764 
       
   765 // -----------------------------------------------------------------------------
       
   766 // CMceMediaStream::HandleMediaError
       
   767 // -----------------------------------------------------------------------------
       
   768 //
       
   769 TBool CMceMediaStream::HandleMediaError( TState aState, TBool aIsEnabled, TInt aError )
       
   770     {
       
   771     TBool mediaErrorHandled( EFalse );
       
   772     if ( aState == EDisabled && !aIsEnabled && aError != KErrNone )
       
   773         {
       
   774         MCECLI_DEBUG_DVALUE( "CMceMediaStream::HandleMediaError, fatal error:", aError )
       
   775         
       
   776         CMceManager& manager = iSession->Manager();
       
   777         
       
   778         TRAPD( err, iSession->TerminateL() )
       
   779         if ( err )
       
   780             {
       
   781             CMceSession::TState state = iSession->State();
       
   782             MCECLI_DEBUG_DVALUES( "CMceMediaStream::HandleMediaError, terminating failed:", err, 
       
   783                                   " at state:", state )
       
   784             
       
   785             // Nothing to do if already terminating                      
       
   786             if ( state != CMceSession::ETerminating && 
       
   787                  state != CMceSession::ETerminated )
       
   788                 {
       
   789                 TRAP_IGNORE( iSession->ErrorOccuredL( aError ) )
       
   790                 }
       
   791             }
       
   792         else if ( manager.SessionObserver() )
       
   793             {
       
   794             // Inform client that we are terminating
       
   795             manager.SessionObserver()->SessionStateChanged( 
       
   796                 *iSession, manager.TransactionContainer() );
       
   797             }
       
   798         else
       
   799             {
       
   800             // NOP
       
   801             }
       
   802             
       
   803         mediaErrorHandled = ETrue;
       
   804             
       
   805         MCECLI_DEBUG( "CMceMediaStream::HandleMediaError, exit" )
       
   806         }
       
   807     return mediaErrorHandled;
       
   808     }
       
   809     
       
   810 // -----------------------------------------------------------------------------
       
   811 // CMceMediaStream::CMceMediaStream
       
   812 // -----------------------------------------------------------------------------
       
   813 //
       
   814 CMceMediaStream::CMceMediaStream()
       
   815     :iLinkedStream( NULL ),
       
   816      iSource( NULL )
       
   817     {
       
   818     }
       
   819 
       
   820 
       
   821 // -----------------------------------------------------------------------------
       
   822 // CMceMediaStream::ConstructL
       
   823 // -----------------------------------------------------------------------------
       
   824 //
       
   825 void CMceMediaStream::ConstructL( CMceComMediaStream* aFlatData )
       
   826     {
       
   827     __ASSERT_ALWAYS( aFlatData , User::Leave( KErrArgument ) );
       
   828     iFlatData = aFlatData;
       
   829     FLAT_DATA( iID ) = TMceMediaId();//as not assigned
       
   830     
       
   831     }
       
   832 
       
   833 // -----------------------------------------------------------------------------
       
   834 // CMceMediaStream::DeleteSink
       
   835 // -----------------------------------------------------------------------------
       
   836 //
       
   837 void CMceMediaStream::DeleteSink( TInt aIndex )
       
   838     {
       
   839     MCECLI_DEBUG("CMceMediaStream::DeleteSink, Entry");
       
   840     MCECLI_DEBUG_DVALUE("sink id", iSinks[ aIndex ]->Id().iId );
       
   841     
       
   842     iSinks[ aIndex ]->ReferenceCount()--;
       
   843     if ( iSinks[ aIndex ]->ReferenceCount() == 0 )
       
   844         {
       
   845         delete iSinks[ aIndex ];
       
   846         }
       
   847     else
       
   848         {
       
   849         MCECLI_DEBUG("CMceMediaStream::DeleteSink, \
       
   850 there are references to sink. Not deleted");
       
   851         iSinks[ aIndex ]->UnInitialize( *this );
       
   852         }
       
   853     iSinks.Remove( aIndex );
       
   854     MCECLI_DEBUG("CMceMediaStream::DeleteSink, Exit");
       
   855     
       
   856     }
       
   857     
       
   858 // -----------------------------------------------------------------------------
       
   859 // CMceMediaStream::SetState
       
   860 // -----------------------------------------------------------------------------
       
   861 //
       
   862 TBool CMceMediaStream::SetState( CMceMediaStream::TState aState )
       
   863     {
       
   864     // If in transcoding state, do not allow certain state changes
       
   865     if ( ( FLAT_DATA( iState ) == CMceMediaStream::ETranscodingRequired || 
       
   866            FLAT_DATA( iState ) == CMceMediaStream::ETranscoding ) &&
       
   867          ( aState == EBuffering ||
       
   868 	       aState == EIdle ||
       
   869 	       aState == EStreaming ||
       
   870 	       aState == EDisabled ) )
       
   871         {
       
   872         return EFalse;
       
   873         }
       
   874     FLAT_DATA( iState ) = aState;
       
   875     
       
   876     MCECLI_DEBUG_DVALUE("CMceMediaStream::SetState, id", Id().iId );
       
   877     TState state = State();
       
   878     MCECLI_DEBUG_MEDIASTATE("CMceMediaStream::SetState, current state", state );
       
   879     return ETrue;
       
   880     }
       
   881     
       
   882 // -----------------------------------------------------------------------------
       
   883 // CMceMediaStream::DeleteSource
       
   884 // -----------------------------------------------------------------------------
       
   885 //
       
   886 void CMceMediaStream::DeleteSource()
       
   887     {
       
   888     MCECLI_DEBUG("CMceMediaStream::DeleteSource, Entry");
       
   889     MCECLI_DEBUG_DVALUE("source id", iSource->Id().iId );
       
   890     
       
   891     iSource->ReferenceCount()--;
       
   892     if ( iSource->ReferenceCount() == 0 )
       
   893         {
       
   894         delete iSource;
       
   895         }
       
   896     else
       
   897         {
       
   898         MCECLI_DEBUG("CMceMediaStream::DeleteSource, \
       
   899 there are references to source. Not deleted");
       
   900         iSource->UnInitialize( *this );
       
   901         }
       
   902     iSource = NULL;
       
   903     MCECLI_DEBUG("CMceMediaStream::DeleteSource, Exit");
       
   904     }
       
   905 
       
   906 
       
   907 // -----------------------------------------------------------------------------
       
   908 // CMceMediaStream::HandleEvent
       
   909 // -----------------------------------------------------------------------------
       
   910 //
       
   911 TInt CMceMediaStream::HandleEvent( TMceEvent& aEvent )
       
   912     {
       
   913     MCECLI_DEBUG("CMceMediaStream::HandleEvent, Entry");
       
   914     MCECLI_DEBUG_DVALUES2("this: type", Type(), "id", Id().iId, 
       
   915                           "main", ( !BoundStream() || Binder() ) );
       
   916     
       
   917     TInt status = KMceEventNotConsumed;
       
   918 
       
   919     if ( aEvent.MessageType() == EMceItcMsgTypeStream )
       
   920         {
       
   921         MCECLI_DEBUG("CMceMediaStream::HandleEvent, content of stream changed");
       
   922         status = KMceEventUpdate;
       
   923         }
       
   924     else if ( aEvent.Action() == EMceItcStateChanged ) 
       
   925         {
       
   926         MCECLI_DEBUG("CMceMediaStream::HandleEvent, state changed");
       
   927         FLAT_DATA( iIsEnabled ) = static_cast<TBool>( aEvent.ActionData() );
       
   928         Updated();
       
   929         status = KMceEventConsumed;
       
   930         }
       
   931     else if ( aEvent.Action() == EMceItcInProgress ) 
       
   932         {
       
   933         MCECLI_DEBUG("CMceMediaStream::HandleEvent, media state changed");
       
   934         SetState( static_cast<CMceMediaStream::TState>( aEvent.ActionData() ) );
       
   935         
       
   936         if ( !HandleMediaError( FLAT_DATA( iState ), 
       
   937                                 FLAT_DATA( iIsEnabled ), 
       
   938                                 aEvent.Id().iStatus ) )
       
   939             {
       
   940             Updated();
       
   941             }
       
   942 
       
   943         status = KMceEventConsumed;
       
   944         }
       
   945     else if ( aEvent.Action() == EMceItcRRReceived ) 
       
   946         {
       
   947         MCECLI_DEBUG("CMceMediaStream::HandleEvent, RR received");
       
   948         MMceRtpObserver* observer = iSession->Manager().RtpObserver();
       
   949         if ( observer )
       
   950             {
       
   951             observer->RRReceived( *iSession, *this );
       
   952             }
       
   953         status = KMceEventConsumed;
       
   954         }
       
   955     else if ( aEvent.Action() == EMceItcLinkCreated )
       
   956         {
       
   957         MCECLI_DEBUG("CMceMediaStream::HandleEvent, link created");
       
   958         _FLAT_DATA->SetLocalMediaPort( aEvent.ActionData() );
       
   959         status = KMceEventConsumed;
       
   960         }
       
   961     else
       
   962         {
       
   963         //NOP
       
   964         MCECLI_DEBUG("CMceMediaStream::HandleEvent, not consumed");
       
   965         }    
       
   966         
       
   967     MCECLI_DEBUG_DVALUE("CMceMediaStream::HandleEvent, Exit. status", status );
       
   968     
       
   969     return status;
       
   970     }
       
   971 
       
   972 
       
   973 // -----------------------------------------------------------------------------
       
   974 // CMceMediaStream::DoBindL
       
   975 // -----------------------------------------------------------------------------
       
   976 //
       
   977 void CMceMediaStream::DoBindL( CMceMediaStream* aStream )
       
   978     {
       
   979     if ( Session() )
       
   980         {
       
   981         aStream->InitializeL( *Session(), EFalse );
       
   982         }
       
   983     delete iLinkedStream;    
       
   984     iLinkedStream = aStream;
       
   985     }
       
   986 
       
   987 // -----------------------------------------------------------------------------
       
   988 // CMceMediaStream::Bound
       
   989 // -----------------------------------------------------------------------------
       
   990 //
       
   991 CMceMediaStream* CMceMediaStream::Bound() const
       
   992     {
       
   993     return iLinkedStream;
       
   994     }
       
   995 
       
   996 // -----------------------------------------------------------------------------
       
   997 // CMceMediaStream::FlatData
       
   998 // -----------------------------------------------------------------------------
       
   999 //
       
  1000 CMceComMediaStream* CMceMediaStream::FlatData()
       
  1001     {
       
  1002     return iFlatData;
       
  1003     }
       
  1004 
       
  1005 // -----------------------------------------------------------------------------
       
  1006 // CMceMediaStream::FindCodec
       
  1007 // -----------------------------------------------------------------------------
       
  1008 //
       
  1009 CMceCodec* CMceMediaStream::FindCodec( 
       
  1010     const RPointerArray<CMceCodec>& aCodecs, 
       
  1011     CMceCodec& aCodec )
       
  1012     {
       
  1013     CMceCodec* foundCodec = NULL;
       
  1014     TInt index = aCodecs.Find( &aCodec );
       
  1015     if ( index == KErrNotFound )
       
  1016         {
       
  1017         TInt boundCodecIndex( KErrNotFound );
       
  1018         if ( iLinkedStream != NULL )
       
  1019             {
       
  1020             // If multiple matches based exist based on SDP, index based match
       
  1021             // will be done. Find out index of the codec in bound stream.
       
  1022             const RPointerArray<CMceCodec>& boundCodecs = iLinkedStream->BaseCodecs();
       
  1023             boundCodecIndex = boundCodecs.Find( &aCodec );
       
  1024             }
       
  1025             
       
  1026         for ( TInt i = 0; i < aCodecs.Count(); i++ )
       
  1027             {
       
  1028             if ( aCodecs[ i ]->SdpName().CompareF( aCodec.SdpName() ) == 0 )
       
  1029                 {
       
  1030                 if ( foundCodec )
       
  1031                     {
       
  1032                     if ( boundCodecIndex != KErrNotFound &&  i == boundCodecIndex )
       
  1033                         { 
       
  1034                         // This codec is selected as it has the same index as
       
  1035                         // search term codec has in bound stream
       
  1036                         foundCodec = aCodecs[ i ];
       
  1037                         }
       
  1038                     }
       
  1039                 else
       
  1040                     {
       
  1041                     foundCodec = aCodecs[ i ];
       
  1042                     }
       
  1043                 }     
       
  1044             }
       
  1045         }
       
  1046     else
       
  1047         {
       
  1048         foundCodec = aCodecs[ index ];
       
  1049         }
       
  1050         
       
  1051     return foundCodec;
       
  1052     }
       
  1053     
       
  1054 // End of file