multimediacommscontroller/mmcccontroller/src/mccresourcepool.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006 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 FILES
       
    21 #include "mccresourcepool.h"
       
    22 #include "mccresourcecontainer.h"
       
    23 #include "mccresourceitem.h"
       
    24 #include "mccinternaldef.h"
       
    25 #include "mcccontrollerlogs.h"
       
    26 #include "mccinternalevents.h"
       
    27 #include "mccrtpdatasource.h"
       
    28 #include "mccrtpdatasink.h"
       
    29 #include "mcccamerahandler.h"
       
    30 #include "mccvideosource.h"
       
    31 #include "mccresourceiterators.h"
       
    32 #include "mcctimermanager.h"
       
    33 
       
    34 // EXTERNAL DATA STRUCTURES
       
    35 
       
    36 // EXTERNAL FUNCTION PROTOTYPES  
       
    37 
       
    38 // CONSTANTS
       
    39 
       
    40 // MACROS
       
    41 
       
    42 // LOCAL CONSTANTS AND MACROS
       
    43 
       
    44 // MODULE DATA STRUCTURES
       
    45 
       
    46 // LOCAL FUNCTION PROTOTYPES
       
    47 
       
    48 // FORWARD DECLARATIONS
       
    49 
       
    50 // ============================= LOCAL FUNCTIONS ===============================
       
    51 
       
    52 // ============================ MEMBER FUNCTIONS ===============================
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CMccResourcePool::NewL
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CMccResourcePool* CMccResourcePool::NewL()
       
    59     {
       
    60     CMccResourcePool* self = new ( ELeave ) CMccResourcePool;
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop( self );
       
    64     return self;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CMccResourcePool::~CMccResourcePool
       
    69 // -----------------------------------------------------------------------------
       
    70 //        
       
    71 CMccResourcePool::~CMccResourcePool()
       
    72     {
       
    73     iTempSources.Reset();
       
    74     iTempSinks.Reset();
       
    75     iResources.ResetAndDestroy();
       
    76     iResources.Close();
       
    77     iEndpoints.ResetAndDestroy();
       
    78     iEndpoints.Close();
       
    79     
       
    80     delete iCameraHandler;
       
    81     delete iTimerManager;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CMccResourcePool::ReuseL
       
    86 // -----------------------------------------------------------------------------
       
    87 //    
       
    88 void CMccResourcePool::ReuseL( 
       
    89     TUint32 aEndpointId )
       
    90     {
       
    91     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
    92     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
    93     
       
    94     if ( item->IsSource() )
       
    95         {
       
    96         iSourceCandidate = item->Source();
       
    97         iSourceCandidateEndpointId = aEndpointId;
       
    98         }
       
    99     else
       
   100         {
       
   101         iSinkCandidate = item->Sink();
       
   102         iSinkCandidateEndpointId = aEndpointId;
       
   103         }
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CMccResourcePool::ReferenceCountL
       
   108 // -----------------------------------------------------------------------------
       
   109 //    
       
   110 void CMccResourcePool::ReferenceCountL( TUint32 aEndpointId, TInt& aRefCount )
       
   111     {
       
   112     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   113     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   114     
       
   115     aRefCount = item->RefCount();
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CMccResourcePool::SourceCandidate
       
   120 // -----------------------------------------------------------------------------
       
   121 //   
       
   122 MDataSource* CMccResourcePool::SourceCandidate()
       
   123     {
       
   124     return iSourceCandidate;
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CMccResourcePool::SinkCandidate
       
   129 // -----------------------------------------------------------------------------
       
   130 //      
       
   131 MDataSink* CMccResourcePool::SinkCandidate()
       
   132     {
       
   133     return iSinkCandidate;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CMccResourcePool::SetEnableRtcpL
       
   138 // -----------------------------------------------------------------------------
       
   139 //   
       
   140 void CMccResourcePool::SetEnableRtcpL( TUint32 aStreamId, TBool aEnableRtcp )
       
   141     {
       
   142     CMccResourceContainer* container = FindResourceContainer( aStreamId );
       
   143     __ASSERT_ALWAYS( container, User::Leave( KErrNotFound ) );
       
   144     
       
   145     container->SetEnableRtcp( aEnableRtcp );
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CMccResourcePool::EnableRtcp
       
   150 // -----------------------------------------------------------------------------
       
   151 //   
       
   152 TBool CMccResourcePool::EnableRtcp( TUint32 aStreamId )
       
   153     {
       
   154     TBool enableRtcp( EFalse );
       
   155     CMccResourceContainer* container = FindResourceContainer( aStreamId );
       
   156     if ( container )
       
   157         {
       
   158         enableRtcp = container->EnableRtcp();
       
   159         }
       
   160     return enableRtcp;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CMccResourcePool::CameraHandlerL
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 CMccCameraHandler& CMccResourcePool::CameraHandlerL( 
       
   168     TBool aCurrentCamera, 
       
   169     TInt aCameraIndex )
       
   170     {
       
   171     __ASSERT_ALWAYS( aCurrentCamera || 
       
   172                      iCameraHandler->CameraIndex() == aCameraIndex,
       
   173                      User::Leave( KErrNotReady ) );
       
   174     
       
   175     return *iCameraHandler;
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CMccResourcePool::CreateNewCameraHandlerL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 CMccCameraHandler* CMccResourcePool::CreateNewCameraHandlerL( TInt aCameraIndex )
       
   183     {
       
   184     return CMccCameraHandler::NewL( *this, aCameraIndex );
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CMccResourcePool::UpdateResourceL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CMccResourcePool::UpdateResourceL( TUint32 aEndpointId, const TDesC8& aVal )
       
   192     {
       
   193     __CONTROLLER( "CMccResourcePool::UpdateResourceL" )
       
   194     
       
   195     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   196     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   197     __ASSERT_ALWAYS( item->IsInternal(), User::Leave( KErrNotSupported ) );
       
   198     
       
   199     if ( item->IsSink() )
       
   200         {
       
   201         static_cast<CMccDataSink*>( item->Sink() )->UpdateL( aVal );
       
   202         }
       
   203     else
       
   204         {
       
   205         static_cast<CMccDataSource*>( item->Source() )->UpdateL( aVal );
       
   206         }
       
   207     }
       
   208     
       
   209 // -----------------------------------------------------------------------------
       
   210 // CMccResourcePool::ReserveSinkL
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CMccResourcePool::ReserveSinkL( MDataSink* aSink, TUint32 aEndpointId )
       
   214     {
       
   215     __ASSERT_ALWAYS( aSink, User::Leave( KErrArgument ) );
       
   216     iSinkCandidate = aSink;
       
   217     iSinkCandidateEndpointId = aEndpointId;
       
   218     
       
   219     if ( MCC_INTERNAL_ENDPOINT( aSink->DataSinkType().iUid ) )
       
   220         {
       
   221         static_cast<CMccDataSink*>( aSink )->SetResources( this );
       
   222         }
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CMccResourcePool::ReserveSourceL
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void CMccResourcePool::ReserveSourceL( MDataSource* aSource, TUint32 aEndpointId )
       
   230     {
       
   231     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
   232     iSourceCandidate = aSource;
       
   233     iSourceCandidateEndpointId = aEndpointId;
       
   234     
       
   235     if ( MCC_INTERNAL_ENDPOINT( aSource->DataSourceType().iUid ) )
       
   236         {
       
   237         static_cast<CMccDataSource*>( aSource )->SetResources( this );
       
   238         }
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CMccResourcePool::PrepareEndpointRemovalL
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CMccResourcePool::PrepareEndpointRemovalL( 
       
   246     TUint32 aEndpointId, 
       
   247     RArray<TMccResourceParams>& aAffectedStreams )
       
   248     {
       
   249     __CONTROLLER( "CMccResourcePool::PrepareEndpointRemovalL" )
       
   250     
       
   251     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   252     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   253     
       
   254     item->UsersInfoL( aAffectedStreams );
       
   255     
       
   256     __CONTROLLER( "CMccResourcePool::PrepareEndpointRemovalL, exit" )
       
   257     }
       
   258     
       
   259 // -----------------------------------------------------------------------------
       
   260 // CMccResourcePool::RemoveEndpointL
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void CMccResourcePool::RemoveEndpointL( TUint32 aEndpointId )
       
   264     {
       
   265     __CONTROLLER( "CMccResourcePool::RemoveEndpointL" )
       
   266     
       
   267     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   268     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   269 
       
   270     for ( TInt i = 0; i < iResources.Count(); i++ )
       
   271         {
       
   272         iResources[ i ]->HandleEndpointRemoval( aEndpointId );
       
   273         }
       
   274     
       
   275     // Possibly release camera resources
       
   276     HandleCameraResourcesL();
       
   277     
       
   278     // Possibly clear candidate
       
   279     //
       
   280     
       
   281     if ( aEndpointId == iSinkCandidateEndpointId )
       
   282         {
       
   283         iSinkCandidateEndpointId = 0;
       
   284         iSinkCandidate = 0;
       
   285         }
       
   286         
       
   287     if ( aEndpointId == iSourceCandidateEndpointId )
       
   288         {
       
   289         iSourceCandidateEndpointId = 0;
       
   290         iSourceCandidate = 0;
       
   291         }
       
   292         
       
   293     __CONTROLLER( "CMccResourcePool::RemoveEndpointL, exit" )
       
   294     }
       
   295     
       
   296 // -----------------------------------------------------------------------------
       
   297 // CMccResourcePool::ReserveResourcesL
       
   298 // -----------------------------------------------------------------------------
       
   299 //      
       
   300 void CMccResourcePool::ReserveResourcesL( const TMccResourceParams& aParams )
       
   301     {
       
   302     __CONTROLLER( "CMccResourcePool::ReserveResourcesL" )
       
   303     
       
   304     __ASSERT_ALWAYS( iSinkCandidate && iSourceCandidate, 
       
   305                      User::Leave( KErrNotReady ) );
       
   306                    
       
   307     CMccResourceContainer* container = FindResourceContainer( aParams.iStreamId );
       
   308     __ASSERT_ALWAYS( !container, User::Leave( KErrAlreadyExists ) );
       
   309     
       
   310     CheckStandbyPropertiesL( aParams, *iSinkCandidate, *iSourceCandidate );
       
   311     
       
   312     CheckResourceAvailabilityL( *iSinkCandidate, *iSourceCandidate );
       
   313     
       
   314     CMccResourceItem* sinkItem = FindResourceItem( iSinkCandidateEndpointId );
       
   315     if ( !sinkItem )
       
   316         {
       
   317         sinkItem = CMccResourceItem::NewLC( aParams.iStreamId,
       
   318                                             iSinkCandidate, 
       
   319                                             0,
       
   320                                             iSinkCandidateEndpointId );
       
   321                                             
       
   322         iEndpoints.AppendL( sinkItem );
       
   323         CleanupStack::Pop( sinkItem );
       
   324         }
       
   325     
       
   326     CMccResourceItem* sourceItem = FindResourceItem( iSourceCandidateEndpointId );
       
   327     if ( !sourceItem )
       
   328         {
       
   329         sourceItem = CMccResourceItem::NewLC( aParams.iStreamId,
       
   330                                               0,
       
   331                                               iSourceCandidate, 
       
   332                                               iSourceCandidateEndpointId );
       
   333                                               
       
   334         iEndpoints.AppendL( sourceItem );
       
   335         CleanupStack::Pop( sourceItem );
       
   336         
       
   337         if ( iSourceCandidate->DataSourceType() == KMccVideoSourceUid )
       
   338             {
       
   339             static_cast<CMccVideoSource*>( iSourceCandidate )->SetCameraHandler( *iCameraHandler );
       
   340             }
       
   341         }
       
   342     
       
   343     container = CMccResourceContainer::NewL( aParams, iEndpoints );  
       
   344     CleanupStack::PushL( container ); 
       
   345     iResources.AppendL( container );
       
   346     CleanupStack::Pop( container );
       
   347 
       
   348     container->AddResourceItemL( aParams, sourceItem );
       
   349  
       
   350     container->AddResourceItemL( aParams, sinkItem );
       
   351     
       
   352     ClearCandidates();
       
   353     
       
   354     __CONTROLLER( "CMccResourcePool::ReserveResourcesL, exit" )
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CMccResourcePool::FreeResources
       
   359 // -----------------------------------------------------------------------------
       
   360 //     
       
   361 void CMccResourcePool::FreeResources( TUint32 aStreamId )
       
   362     {
       
   363     __CONTROLLER( "CMccResourcePool::FreeResources" )
       
   364     
       
   365     TMccResourceParams params( 0, 0, aStreamId, 0, EFalse, 0 );
       
   366     TMccResourceContainerIterator iterator( iResources, params );
       
   367     CMccResourceContainer* container = NULL;
       
   368     
       
   369     // Only one entry per stream id
       
   370     if ( iterator.Next( container ) )
       
   371         {
       
   372         iterator.Delete( iResources );
       
   373         }
       
   374     
       
   375     __CONTROLLER( "CMccResourcePool::FreeResources, exit" )
       
   376     }
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CMccResourcePool::IsResourceUsageAllowed
       
   380 // -----------------------------------------------------------------------------
       
   381 //                               
       
   382 TBool CMccResourcePool::IsResourceUsageAllowed( TUint32 /*aStreamId*/ )
       
   383     {
       
   384     return ETrue;
       
   385     }                    
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CMccResourcePool::SetResourceStateL
       
   389 // -----------------------------------------------------------------------------
       
   390 //                                    
       
   391 void CMccResourcePool::SetResourceStateL( 
       
   392     TUint32 aStreamId,
       
   393     TUint32 aEndpointId, 
       
   394     TResourceState aState,
       
   395     TBool& aControlNetworkResources,
       
   396     RArray<TUint32>& aControlledEndpoints,
       
   397     RArray<TUint32>& aUncontrolledEndpoints )
       
   398     {
       
   399     __CONTROLLER( "CMccResourcePool::SetResourceStateL" )
       
   400     
       
   401     CMccResourceContainer* container = FindResourceContainer( aStreamId );
       
   402     __ASSERT_ALWAYS( container, User::Leave( KErrNotFound ) );
       
   403     
       
   404     container->SetResourceStateL( aEndpointId, 
       
   405                                   aState, 
       
   406                                   aControlNetworkResources,
       
   407                                   aControlledEndpoints,
       
   408                                   aUncontrolledEndpoints );
       
   409 
       
   410     __CONTROLLER( "CMccResourcePool::SetResourceStateL, exit" )
       
   411     }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CMccResourcePool::ResourceStateSetL
       
   415 // -----------------------------------------------------------------------------
       
   416 //    
       
   417 void CMccResourcePool::ResourceStateSetL()
       
   418     {
       
   419     // Possibly reserve or release camera resources
       
   420     HandleCameraResourcesL();
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CMccResourcePool::EventReceived
       
   425 // Original event is possibly modified to contain standby stream control
       
   426 // information. Additional control event may be returned as well when
       
   427 // pausing existing active standby stream and activating other one.
       
   428 // -----------------------------------------------------------------------------
       
   429 //                                             
       
   430 TInt CMccResourcePool::EventReceived( 
       
   431     TMccEvent& aEvent, 
       
   432     TBool& aDiscardEvent,
       
   433     TBool& aEffectiveStandby,
       
   434     TMccEvent** aAdditionalEvent )
       
   435     {
       
   436     TRAPD( err, *aAdditionalEvent = 
       
   437         StandbyLogicL( aEvent, aDiscardEvent, aEffectiveStandby ) )
       
   438     return err;
       
   439     }
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // CMccResourcePool::Sinks
       
   443 // -----------------------------------------------------------------------------
       
   444 //
       
   445 const RPointerArray<MDataSink>& CMccResourcePool::Sinks( TBool aOnlyInternals )
       
   446     {
       
   447     iTempSinks.Reset();
       
   448     
       
   449     TMccResourceItemIterator iterator( iEndpoints, aOnlyInternals, ETrue, EFalse );
       
   450     CMccResourceItem* item = NULL;
       
   451     
       
   452     while( iterator.Next( item ) )
       
   453         {
       
   454         iTempSinks.Append( item->Sink() );
       
   455         }
       
   456 
       
   457     return iTempSinks;
       
   458     }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CMccResourcePool::Sources
       
   462 // -----------------------------------------------------------------------------
       
   463 //        
       
   464 const RPointerArray<MDataSource>& CMccResourcePool::Sources( TBool aOnlyInternals )
       
   465     {
       
   466     iTempSources.Reset();
       
   467     
       
   468     TMccResourceItemIterator iterator( iEndpoints, aOnlyInternals, EFalse, ETrue );
       
   469     CMccResourceItem* item = NULL;
       
   470     
       
   471     while( iterator.Next( item ) )
       
   472         {
       
   473         iTempSources.Append( item->Source() );
       
   474         }
       
   475         
       
   476     return iTempSources;
       
   477     }
       
   478 
       
   479 // -----------------------------------------------------------------------------
       
   480 // CMccResourcePool::MultiplexerL
       
   481 // -----------------------------------------------------------------------------
       
   482 //      
       
   483 CMccMultiplexer* CMccResourcePool::MultiplexerL( MDataSource* aSource )
       
   484     {
       
   485     CMccResourceItem* item = FindResourceItem( MCC_ENDPOINT_ID( aSource ) );
       
   486     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   487     
       
   488     return item->MultiplexerL();
       
   489     }
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CMccResourcePool::FindResource
       
   493 // -----------------------------------------------------------------------------
       
   494 //    
       
   495 TInt CMccResourcePool::FindResource( 
       
   496     TUint32 aLinkId,
       
   497     TUint32 aEndpointId, 
       
   498     TUid aResourceUid, 
       
   499     MDataSource** aSource, 
       
   500     MDataSink** aSink )
       
   501     {
       
   502     if ( !aLinkId )
       
   503         {
       
   504         CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   505         if ( item )
       
   506             {
       
   507             return GetEndpoint( *item, aResourceUid, aSource, aSink );
       
   508             }
       
   509         }
       
   510     else
       
   511         {
       
   512         // If link id is defined, search for any endpoint of given type which
       
   513         // is used in the link.
       
   514         
       
   515         TMccResourceParams params( 0, aLinkId, 0, 0, EFalse, 0 );
       
   516         
       
   517         TMccResourceItemIterator iterator( iEndpoints, params );
       
   518         CMccResourceItem* item = NULL;
       
   519     
       
   520         while( iterator.Next( item ) )
       
   521             {
       
   522             if ( GetEndpoint( *item, aResourceUid, aSource, aSink ) == KErrNone )
       
   523                 {
       
   524                 return KErrNone;
       
   525                 }
       
   526             }
       
   527         }
       
   528     return KErrNotFound;
       
   529     }
       
   530 
       
   531 // -----------------------------------------------------------------------------
       
   532 // CMccResourcePool::FindNetworkResourceByRtpStreamId
       
   533 // -----------------------------------------------------------------------------
       
   534 //   
       
   535 TInt CMccResourcePool::FindNetworkResourceByRtpStreamId( 
       
   536     const TUint32 aMccSessionId,
       
   537     const TRtpId& aRtpStreamId,
       
   538     MDataSource** aSource, 
       
   539     MDataSink** aSink )
       
   540     {
       
   541     TBool found( EFalse );
       
   542     
       
   543     TMccResourceParams params( aMccSessionId, 0, 0, 0, EFalse, 0 );
       
   544     TMccResourceItemIterator iterator( iEndpoints, KMccRtpSourceUid, params );
       
   545     CMccResourceItem* item = NULL;
       
   546     
       
   547     while ( iterator.Next( item ) )
       
   548         {
       
   549         CMccRtpDataSource* rtpSource = 
       
   550                 static_cast<CMccRtpDataSource*>( item->Source() );
       
   551                 
       
   552         if ( rtpSource->HandleByStreamId( aRtpStreamId ) )
       
   553             {
       
   554             *aSource = item->Source();
       
   555             found = ETrue;
       
   556             }
       
   557         }
       
   558     
       
   559     TMccResourceItemIterator iterator2( iEndpoints, KMccRtpSinkUid, params );
       
   560     item = NULL;
       
   561     
       
   562     while ( !found && iterator2.Next( item ) )
       
   563         {
       
   564         CMccRtpDataSink* rtpSink = 
       
   565                 static_cast<CMccRtpDataSink*>( item->Sink() );
       
   566                 
       
   567         if ( rtpSink->HandleByStreamId( aRtpStreamId ) )
       
   568             {
       
   569             *aSink = item->Sink();
       
   570             found = ETrue;
       
   571             }
       
   572         }
       
   573         
       
   574     return ( found ? KErrNone : KErrNotFound );
       
   575     }
       
   576 
       
   577 // -----------------------------------------------------------------------------
       
   578 // CMccResourcePool::FindNetworkResourceByRtpSsrc
       
   579 // -----------------------------------------------------------------------------
       
   580 //   
       
   581 TInt CMccResourcePool::FindNetworkResourceByRtpSsrc( 
       
   582     const TRtpSSRC& aRtpSsrc,
       
   583     MDataSource** aSource, 
       
   584     MDataSink** aSink )
       
   585     {
       
   586     TBool found( EFalse );
       
   587     
       
   588     TMccResourceItemIterator iterator( iEndpoints, KMccRtpSourceUid );
       
   589     CMccResourceItem* item = NULL;
       
   590     
       
   591     while ( iterator.Next( item ) )
       
   592         {
       
   593         CMccRtpDataSource* rtpSource = 
       
   594                 static_cast<CMccRtpDataSource*>( item->Source() );
       
   595                 
       
   596         if ( rtpSource->HandleBySsrc( aRtpSsrc ) )
       
   597             {
       
   598             *aSource = item->Source();
       
   599             found = ETrue;
       
   600             }
       
   601         }
       
   602     
       
   603     TMccResourceItemIterator iterator2( iEndpoints, KMccRtpSinkUid );
       
   604     item = NULL;
       
   605     
       
   606     while ( !found && iterator2.Next( item ) )
       
   607         {
       
   608         CMccRtpDataSink* rtpSink = 
       
   609                 static_cast<CMccRtpDataSink*>( item->Sink() );
       
   610                 
       
   611         if ( rtpSink->HandleBySsrc( aRtpSsrc ) )
       
   612             {
       
   613             *aSink = item->Sink();
       
   614             found = ETrue;
       
   615             }
       
   616         }
       
   617     
       
   618     return ( found ? KErrNone : KErrNotFound );
       
   619     }
       
   620 
       
   621 // -----------------------------------------------------------------------------
       
   622 // CMccResourcePool::TimerManager
       
   623 // -----------------------------------------------------------------------------
       
   624 // 
       
   625 MMccTimerManager& CMccResourcePool::TimerManager()
       
   626     {
       
   627     return *iTimerManager;
       
   628     }
       
   629 
       
   630 // -----------------------------------------------------------------------------
       
   631 // CMccResourcePool::StoreKeyFrameInfoL
       
   632 // -----------------------------------------------------------------------------
       
   633 //
       
   634 void CMccResourcePool::StoreKeyFrameInfoL( TUint32 aEndpointId, CMMFBuffer& aKeyFrame )
       
   635     {
       
   636     CMccResourceItem* item = FindResourceItem( aEndpointId );
       
   637     __ASSERT_ALWAYS( item, User::Leave( KErrNotFound ) );
       
   638     item->StoreKeyFrameInfoL( aKeyFrame );
       
   639     }
       
   640 
       
   641 // -----------------------------------------------------------------------------
       
   642 // CMccResourcePool::IsKeyFrame
       
   643 // -----------------------------------------------------------------------------
       
   644 //
       
   645 TBool CMccResourcePool::IsKeyFrame( TUint32 aEndpointId, CMMFBuffer& aBuffer )
       
   646     {
       
   647     // First find associated resource containers, some of their endpoints
       
   648     // may contain keyframe info
       
   649     TMccResourceContainerIterator iterator( iResources, aEndpointId );
       
   650     CMccResourceContainer* container = NULL;
       
   651         
       
   652     while( iterator.Next( container ) )
       
   653         {
       
   654         TMccResourceParams params2( 0, 0, container->StreamId(), 0, EFalse, 0 );
       
   655         TMccResourceItemIterator endpointIterator( iEndpoints, params2 );
       
   656         CMccResourceItem* item = NULL;
       
   657         while ( endpointIterator.Next( item ) )
       
   658             {
       
   659             if ( item->IsKeyFrame( aBuffer ) )
       
   660                 {
       
   661                 return ETrue;
       
   662                 }
       
   663             }
       
   664         }
       
   665     return EFalse;
       
   666     }
       
   667 
       
   668 // -----------------------------------------------------------------------------
       
   669 // CMccResourcePool::EnableViewFinderL
       
   670 // -----------------------------------------------------------------------------
       
   671 // 
       
   672 void CMccResourcePool::EnableViewFinderL( TMccVideoSinkSetting& aSetting )
       
   673     {
       
   674     __CONTROLLER( "CMccResourcePool::EnableViewFinderL" )
       
   675     
       
   676     CameraHandlerL().EnableViewFinderL( aSetting );
       
   677 
       
   678     HandleCameraResourcesL();
       
   679     
       
   680     __CONTROLLER( "CMccResourcePool::EnableViewFinderL, exit" )
       
   681     }
       
   682 
       
   683 // -----------------------------------------------------------------------------
       
   684 // CMccResourcePool::DisableViewFinderL
       
   685 // -----------------------------------------------------------------------------
       
   686 // 
       
   687 void CMccResourcePool::DisableViewFinderL()
       
   688     {
       
   689     __CONTROLLER( "CMccResourcePool::DisableViewFinderL" )
       
   690     
       
   691     CameraHandlerL().DisableViewFinderL();
       
   692     
       
   693     HandleCameraResourcesL();
       
   694     
       
   695     __CONTROLLER( "CMccResourcePool::DisableViewFinderL, exit" )
       
   696     }
       
   697         
       
   698 // -----------------------------------------------------------------------------
       
   699 // CMccResourcePool::FindResourceContainer
       
   700 // -----------------------------------------------------------------------------
       
   701 //    
       
   702 CMccResourceContainer* CMccResourcePool::FindResourceContainer( 
       
   703     TUint32 aStreamId )
       
   704     {
       
   705     TMccResourceParams params( 0, 0, aStreamId, 0, EFalse, 0 );
       
   706     TMccResourceContainerIterator iterator( iResources, params );
       
   707     CMccResourceContainer* container = NULL;
       
   708     
       
   709     iterator.Next( container );
       
   710 
       
   711     return container;
       
   712     }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // CMccResourcePool::FindResourceItem
       
   716 // -----------------------------------------------------------------------------
       
   717 //    
       
   718 CMccResourceItem* CMccResourcePool::FindResourceItem( TUint32 aEndpointId )
       
   719     {
       
   720     TMccResourceItemIterator iterator( iEndpoints, aEndpointId );
       
   721     CMccResourceItem* item = NULL;
       
   722     
       
   723     iterator.Next( item );
       
   724 
       
   725     return item;
       
   726     }
       
   727 
       
   728 // -----------------------------------------------------------------------------
       
   729 // CMccResourcePool::ClearCandidates
       
   730 // -----------------------------------------------------------------------------
       
   731 //    
       
   732 void CMccResourcePool::ClearCandidates()
       
   733     {
       
   734     iSourceCandidate = 0;
       
   735     iSourceCandidateEndpointId = 0;
       
   736     iSinkCandidate = 0;
       
   737     iSinkCandidateEndpointId = 0;
       
   738     }
       
   739     
       
   740 // -----------------------------------------------------------------------------
       
   741 // CMccResourcePool::HandleCameraResourcesL
       
   742 // -----------------------------------------------------------------------------
       
   743 // 
       
   744 void CMccResourcePool::HandleCameraResourcesL()
       
   745     {
       
   746     // If any video source is active, camera resources are needed.
       
   747     //
       
   748     
       
   749     CMccVideoSource* needsCamera = 0;
       
   750     
       
   751     TMccResourceItemIterator iterator( iEndpoints, KMccVideoSourceUid );
       
   752     CMccResourceItem* item = NULL;
       
   753     
       
   754     while ( iterator.Next( item ) && !needsCamera )
       
   755         {
       
   756         needsCamera = item->NeedsCamera( *iCameraHandler ) ?
       
   757                 static_cast<CMccVideoSource*>( item->Source() ) : NULL;
       
   758         }
       
   759    
       
   760     if ( needsCamera )
       
   761         {
       
   762         // The first videosource can define the camera used
       
   763         iCameraHandler->ReserveResourcesL( 
       
   764             static_cast<TInt>( needsCamera->CameraIndex() ) );
       
   765         }
       
   766    else
       
   767         {
       
   768         iCameraHandler->ReleaseResources();
       
   769         }
       
   770    }
       
   771 
       
   772 // -----------------------------------------------------------------------------
       
   773 // CMccResourcePool::GetEndpoint
       
   774 // -----------------------------------------------------------------------------
       
   775 //
       
   776 TInt CMccResourcePool::GetEndpoint( 
       
   777     CMccResourceItem& aItem,
       
   778     TUid aResourceUid,
       
   779     MDataSource** aSource, 
       
   780     MDataSink** aSink )
       
   781     {   
       
   782     if ( aItem.IsSource() && 
       
   783        ( aResourceUid == KNullUid || 
       
   784          aItem.Source()->DataSourceType() == aResourceUid ) )
       
   785         {
       
   786         *aSource = aItem.Source();
       
   787         return KErrNone;
       
   788         }
       
   789     
       
   790     if ( aItem.IsSink() && 
       
   791        ( aResourceUid == KNullUid ||
       
   792          aItem.Sink()->DataSinkType() == aResourceUid ) )
       
   793         {
       
   794         *aSink = aItem.Sink();
       
   795         return KErrNone;
       
   796         }
       
   797     return KErrNotFound;
       
   798     } 
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CMccResourcePool::CheckStandbyPropertiesL
       
   802 // -----------------------------------------------------------------------------
       
   803 //     
       
   804 void CMccResourcePool::CheckStandbyPropertiesL( 
       
   805     const TMccResourceParams& aParams, 
       
   806     MDataSink& aSink, 
       
   807     MDataSource& aSource )
       
   808     {
       
   809     __CONTROLLER( "CMccResourcePool::CheckStandbyPropertiesL" )
       
   810     
       
   811     if ( aParams.iIsStandby )
       
   812         {
       
   813         // Standby is allowed only for audio downlink
       
   814         __ASSERT_ALWAYS( aSink.DataSinkType() == KUidMmfAudioOutput &&
       
   815                          aSource.DataSourceType() == KMccRtpSourceUid,
       
   816                          User::Leave( KErrArgument ) );   
       
   817         }
       
   818     
       
   819     __CONTROLLER( "CMccResourcePool::CheckStandbyPropertiesL, exit" )
       
   820     }
       
   821     
       
   822 // -----------------------------------------------------------------------------
       
   823 // CMccResourcePool::CheckResourceAvailabilityL
       
   824 // -----------------------------------------------------------------------------
       
   825 //     
       
   826 void CMccResourcePool::CheckResourceAvailabilityL( 
       
   827     MDataSink& /*aSink*/, 
       
   828     MDataSource& aSource )
       
   829     {
       
   830     __CONTROLLER( "CMccResourcePool::CheckResourceAvailabilityL" )
       
   831     
       
   832     // Only one instance of the media recorder is allowed, having more
       
   833     // causes some serious system unstability. 
       
   834     // TBD: multiple videosource instances could be allowed if "resource not
       
   835     // available", "resource released", etc. events would be passed to the 
       
   836     // client and only one active videosource would be allowed. 
       
   837     if ( aSource.DataSourceType() == KMccVideoSourceUid )
       
   838         {
       
   839         TMccResourceItemIterator iterator( iEndpoints, KMccVideoSourceUid );
       
   840         CMccResourceItem* item = NULL;
       
   841     
       
   842         while ( iterator.Next( item ) )
       
   843             {
       
   844             __ASSERT_ALWAYS( item->Source() == &aSource, 
       
   845                              User::Leave( KErrAccessDenied ) );
       
   846             }
       
   847         }
       
   848     
       
   849     __CONTROLLER( "CMccResourcePool::CheckResourceAvailabilityL, exit" )
       
   850     }
       
   851 
       
   852 // -----------------------------------------------------------------------------
       
   853 // CMccResourcePool::StandbyLogicL
       
   854 // -----------------------------------------------------------------------------
       
   855 //    
       
   856 TMccEvent* CMccResourcePool::StandbyLogicL( 
       
   857     TMccEvent& aEvent, 
       
   858     TBool& aDiscardEvent,
       
   859     TBool& aEffectiveStandby )
       
   860     {
       
   861     TMccEvent* additionalControl = NULL;
       
   862     
       
   863     CMccResourceContainer* container = 
       
   864 	    	FindResourceContainer( aEvent.iStreamId );
       
   865 
       
   866     if ( container && container->IsStandBy() )
       
   867 		{
       
   868 		__CONTROLLER( "CMccResourcePool::StandbyLogicL" )
       
   869 		
       
   870 		if ( container->StandbyControl() && aEvent.iEventType != KMccStreamIdle )	
       
   871 			{
       
   872 			// If standby mechanism is controlling the stream, event is discarded
       
   873 			aDiscardEvent = ETrue;
       
   874 		    container->SetStandbyControl( EFalse );
       
   875 			}
       
   876 		else if ( aEvent.iEventType == KMccStandbyInactivityEvent )
       
   877 		    {
       
   878 		    StandbyInactivityL( *container, aEvent, aDiscardEvent, aEffectiveStandby );
       
   879 		    }
       
   880 		else if ( aEvent.iEventType == KMccStandbyActivityEvent )
       
   881 		    {
       
   882 		    additionalControl =
       
   883 		        StandbyActivityL( *container, aEvent, aDiscardEvent, aEffectiveStandby );
       
   884 		    }
       
   885 		else
       
   886 		    {
       
   887 		    // NOP
       
   888 		    }  
       
   889     
       
   890 		__CONTROLLER( "CMccResourcePool::StandbyLogicL, exit" )
       
   891 		}
       
   892 		
       
   893     return additionalControl;	
       
   894     }
       
   895 
       
   896 // -----------------------------------------------------------------------------
       
   897 // CMccResourcePool::StandbyInactivityL
       
   898 // -----------------------------------------------------------------------------
       
   899 //    
       
   900 void CMccResourcePool::StandbyInactivityL( 
       
   901     CMccResourceContainer& aContainer,
       
   902     TMccEvent& aEvent, 
       
   903     TBool& aDiscardEvent,
       
   904     TBool& aEffectiveStandby )
       
   905     {
       
   906 	__CONTROLLER( "CMccResourcePool::StandbyInactivityL" )
       
   907     
       
   908     // If already inactive, no need to do anything
       
   909     CMccResourceItem& standbyResource = aContainer.FetchStandbyEndpointL();
       
   910     if ( standbyResource.CurrentState() == MMccResources::EStarted )
       
   911         {
       
   912         __CONTROLLER( "CMccResourcePool::StandbyInactivityL, effective" )
       
   913         
       
   914         aContainer.SetStandbyControl( ETrue );
       
   915         aEvent.iEndpointId = standbyResource.EndpointId();
       
   916         aEffectiveStandby = ETrue;
       
   917         }
       
   918 	aDiscardEvent = ETrue;
       
   919     }
       
   920 
       
   921 // -----------------------------------------------------------------------------
       
   922 // CMccResourcePool::StandbyActivityL
       
   923 // -----------------------------------------------------------------------------
       
   924 // 
       
   925 TMccEvent* CMccResourcePool::StandbyActivityL(
       
   926     CMccResourceContainer& aContainer,
       
   927     TMccEvent& aEvent, 
       
   928     TBool& aDiscardEvent,
       
   929     TBool& aEffectiveStandby )
       
   930     {
       
   931     __CONTROLLER( "CMccResourcePool::StandbyActivityL" )
       
   932     
       
   933     TMccEvent* additionalControl = NULL;
       
   934 		    	
       
   935     // If already active, no need to do anything
       
   936     CMccResourceItem& standbyResource = aContainer.FetchStandbyEndpointL();
       
   937     if ( standbyResource.CurrentState() == MMccResources::EPrepared ||
       
   938          standbyResource.CurrentState() == MMccResources::EResourcePaused )
       
   939         {
       
   940         __CONTROLLER( "CMccResourcePool::StandbyActivityL, effective" )
       
   941         
       
   942         // There might be other active standby stream within the link,
       
   943         // that has to be paused.
       
   944         TMccResourceParams params( 0, aEvent.iLinkId, 0, 0, EFalse, 0 );
       
   945         TMccResourceContainerIterator iterator( iResources, params );
       
   946         CMccResourceContainer* assocContainer = NULL;
       
   947         while ( iterator.Next( assocContainer ) && !additionalControl )
       
   948             {
       
   949             if ( assocContainer->IsStandBy() && 
       
   950                  assocContainer->FetchStandbyEndpointL().CurrentState() == 
       
   951                     MMccResources::EStarted )
       
   952                 {
       
   953                 __CONTROLLER( "CMccResourcePool::StandbyActivityL, other stream has to be paused" )
       
   954                 
       
   955                 additionalControl = new ( ELeave ) TMccEvent( aEvent );
       
   956                 additionalControl->iEventType = KMccStandbyInactivityEvent;
       
   957                 additionalControl->iStreamId = assocContainer->StreamId();
       
   958                 additionalControl->iEndpointId = 
       
   959                     assocContainer->FetchStandbyEndpointL().EndpointId();
       
   960                 assocContainer->SetStandbyControl( ETrue );
       
   961                 }
       
   962             }
       
   963         
       
   964 		aContainer.SetStandbyControl( ETrue );
       
   965 		aEvent.iEndpointId = standbyResource.EndpointId(); 
       
   966 		aEffectiveStandby = ETrue;
       
   967         }
       
   968 	aDiscardEvent = ETrue;
       
   969 	
       
   970 	return additionalControl;
       
   971     }
       
   972 // -----------------------------------------------------------------------------
       
   973 // CMccResourcePool::CMccResourcePool
       
   974 // -----------------------------------------------------------------------------
       
   975 //      
       
   976 CMccResourcePool::CMccResourcePool()
       
   977     {
       
   978     }
       
   979 
       
   980 // -----------------------------------------------------------------------------
       
   981 // CMccResourcePool::ConstructL
       
   982 // -----------------------------------------------------------------------------
       
   983 // 
       
   984 void CMccResourcePool::ConstructL()
       
   985     {
       
   986     iTimerManager = CMccTimerManager::NewL();
       
   987     iCameraHandler = CMccCameraHandler::NewL( *this );
       
   988     }
       
   989