vtprotocolplugins/VideoSource/src/CVSDataSourceImp.cpp
changeset 0 ed9695c8bcbe
equal deleted inserted replaced
-1:000000000000 0:ed9695c8bcbe
       
     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:  Video Source subsystem.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <fbs.h>
       
    22 #include <e32svr.h>
       
    23 #include <badesca.h>
       
    24 #include <mmfdatasourcesink.hrh>
       
    25 
       
    26 #include "CVSMMFDataBuffer.h"
       
    27 #include "CVSDataSourceImp.h"
       
    28 #include "VSPanic.h"
       
    29 
       
    30 // EXTERNAL FUNCTION PROTOTYPES
       
    31 
       
    32 // MACROS
       
    33 
       
    34 #ifdef _DEBUG
       
    35 #    define __IF_DEBUG(t) {RDebug::t;}
       
    36 #else
       
    37 #    define __IF_DEBUG(t)
       
    38 #endif
       
    39 
       
    40 // LOCAL CONSTANTS AND MACROS
       
    41 
       
    42 static const TInt KQcifWidth = 176;
       
    43 static const TInt KQcifHeight = 144;
       
    44 
       
    45  // 4:2:0 planar and 32 bytes extra
       
    46  static const TInt KSourceBufferSize =
       
    47     ( KQcifWidth * KQcifHeight ) + ( ( KQcifWidth * KQcifHeight ) / 2 ) + 12;
       
    48 
       
    49 // Granularity for video frame format array
       
    50 static const TInt KNoOfSupportedFormats = 4;
       
    51 
       
    52 // How many sequential FillBufferL calls is supported
       
    53 static const TInt KNoOfSupportedBuffers = 2;
       
    54 
       
    55 // ============================ MEMBER FUNCTIONS ===============================
       
    56 
       
    57 // ============================ CVSProviderSwitchAO ============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CVSProviderSwitchAO::NewL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CVSProviderSwitchAO* CVSProviderSwitchAO::NewL(
       
    64     CVSDataSourceImp* aDataSourceImp )
       
    65     {
       
    66     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::NewL() >>"), RThread().Id().operator TUint()));
       
    67     CVSProviderSwitchAO* self =
       
    68         new ( ELeave ) CVSProviderSwitchAO( aDataSourceImp );
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL();
       
    71     CleanupStack::Pop(); // self
       
    72     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::NewL() <<"), RThread().Id().operator TUint()));
       
    73     return self;
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CVSProviderSwitchAO::ConstructL
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void CVSProviderSwitchAO::ConstructL()
       
    81     {
       
    82     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ConstructL() >>"), RThread().Id().operator TUint()));
       
    83     User::LeaveIfError( iSwitchCs.CreateLocal() );
       
    84     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ConstructL() <<"), RThread().Id().operator TUint()));
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CVSProviderSwitchAO::CVSProviderSwitchAO
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 CVSProviderSwitchAO::CVSProviderSwitchAO( CVSDataSourceImp* aDataSourceImp )
       
    92 : CActive( EPriorityStandard ), iDataSourceImp( aDataSourceImp )
       
    93     {
       
    94     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::CVSProviderSwitchAO() >>"), RThread().Id().operator TUint()));
       
    95     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::CVSProviderSwitchAO() <<"), RThread().Id().operator TUint()));
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CVSProviderSwitchAO::~CVSProviderSwitchAO
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CVSProviderSwitchAO::~CVSProviderSwitchAO()
       
   103     {
       
   104     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::~CVSProviderSwitchAO() >>"), RThread().Id().operator TUint()));
       
   105     iSwitchCs.Close();
       
   106     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::~CVSProviderSwitchAO() <<"), RThread().Id().operator TUint()));
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CVSProviderSwitchAO::ThreadLogon
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TInt CVSProviderSwitchAO::ThreadLogon( MAsyncEventHandler& aEventHandler )
       
   114     {
       
   115     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ThreadLogon() >>"), RThread().Id().operator TUint()));
       
   116     iEventHandler = &aEventHandler;
       
   117     RThread me;
       
   118     iThreadId = me.Id();
       
   119     CActiveScheduler::Add( this );
       
   120     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ThreadLogon() <<"), RThread().Id().operator TUint()));
       
   121     return KErrNone;
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CVSProviderSwitchAO::ThreadLogoff
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CVSProviderSwitchAO::ThreadLogoff()
       
   129     {
       
   130     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ThreadLogoff() >>"), RThread().Id().operator TUint()));
       
   131     if( RThread().Id().operator TUint() == iThreadId )
       
   132         {
       
   133         Deque();
       
   134         iThreadId = 0;
       
   135         iNewProvider = NULL;
       
   136         }
       
   137     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::ThreadLogoff() <<"), RThread().Id().operator TUint()));
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CVSProviderSwitchAO::InitiateProviderSwitch
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CVSProviderSwitchAO::InitiateProviderSwitch(
       
   145     CVSDataProvider* aNewProvider )
       
   146     {
       
   147     iSwitchCs.Wait();
       
   148     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::InitiateProviderSwitch() >>"), RThread().Id().operator TUint()));
       
   149     iNewProvider = aNewProvider;
       
   150     iStatus = KRequestPending;
       
   151     SetActive();
       
   152     Signal( KErrNone );
       
   153     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::InitiateProviderSwitch() <<"), RThread().Id().operator TUint()));
       
   154     iSwitchCs.Signal();
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CVSProviderSwitchAO::RunL
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CVSProviderSwitchAO::RunL()
       
   162     {
       
   163     iSwitchCs.Wait();
       
   164     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::RunL() >>"), RThread().Id().operator TUint()));
       
   165     if( iStatus.Int() == KErrNone )
       
   166         {
       
   167         if ( iDataSourceImp->SwitchPending() )
       
   168             {
       
   169             Panic( EVSPanicSwitchAlreadyPending );
       
   170             }
       
   171         TRAPD(
       
   172             leaveCode,
       
   173             iDataSourceImp->DoProviderSwitchL( iNewProvider, *iEventHandler ) );
       
   174         if ( leaveCode != KErrNone )
       
   175             {
       
   176             iDataSourceImp->ProviderSwitchError( leaveCode );
       
   177             }
       
   178         }
       
   179     else
       
   180         {
       
   181         iNewProvider = NULL;
       
   182         }
       
   183     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::RunL() <<"), RThread().Id().operator TUint()));
       
   184     iSwitchCs.Signal();
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CVSProviderSwitchAO::DoCancel
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CVSProviderSwitchAO::DoCancel()
       
   192     {
       
   193     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::DoCancel() >>"), RThread().Id().operator TUint()));
       
   194     // Nothing to do here. This AO is always signalled when it's activated.
       
   195     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::DoCancel() <<"), RThread().Id().operator TUint()));
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CVSProviderSwitchAO::Signal
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CVSProviderSwitchAO::Signal( TInt aError )
       
   203     {
       
   204     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::Signal() >>"), RThread().Id().operator TUint()));
       
   205     if( IsActive() )
       
   206         {
       
   207         TRequestStatus* pStatus = &iStatus;
       
   208         RThread me;
       
   209         if( me.Id() == iThreadId )
       
   210             {
       
   211             User::RequestComplete( pStatus, aError );
       
   212             }
       
   213         else
       
   214             {
       
   215             if( me.Open( iThreadId ) == KErrNone )
       
   216                 {
       
   217                 *pStatus = KRequestPending;
       
   218                 me.RequestComplete( pStatus, aError );
       
   219                 me.Close();
       
   220                 }
       
   221             else
       
   222                 {
       
   223                 Panic( EVSPanicThreadOpenFailure );
       
   224                 }
       
   225             }
       
   226         }
       
   227     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSProviderSwitchAO::Signal() <<"), RThread().Id().operator TUint()));
       
   228     }
       
   229 
       
   230 // ============================ CVSDataSourceObserverAO ========================
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CVSDataSourceObserverAO::CVSDataSourceObserverAO
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 CVSDataSourceObserverAO::CVSDataSourceObserverAO()
       
   237 : CActive( EPriorityStandard )
       
   238     {
       
   239     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::CVSDataSourceObserverAO() >>"), RThread().Id().operator TUint()));
       
   240     RThread me;
       
   241     iThreadId = me.Id();
       
   242     CActiveScheduler::Add( this );
       
   243     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::CVSDataSourceObserverAO() <<"), RThread().Id().operator TUint()));
       
   244     }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CVSDataSourceObserverAO::~CVSDataSourceObserverA
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 CVSDataSourceObserverAO::~CVSDataSourceObserverAO()
       
   251     {
       
   252     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::~CVSDataSourceObserverAO() >>"), RThread().Id().operator TUint()));
       
   253     Cancel();
       
   254     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::~CVSDataSourceObserverAO() <<"), RThread().Id().operator TUint()));
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CVSDataSourceObserverAO::vsProviderSwitchDone
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 void CVSDataSourceObserverAO::vsProviderSwitchDone(
       
   262     MVSDataSourceObserver& aObserver,
       
   263     CVSDataProvider* aOldProvider )
       
   264     {
       
   265     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::vsProviderSwitchDone() >>"), RThread().Id().operator TUint()));
       
   266     iObserver = &aObserver;
       
   267     iOldProvider = aOldProvider;
       
   268     iStatus = KRequestPending;
       
   269     SetActive();
       
   270     Signal( KErrNone );
       
   271     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::vsProviderSwitchDone() <<"), RThread().Id().operator TUint()));
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // CVSDataSourceObserverAO::RunL
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 void CVSDataSourceObserverAO::RunL()
       
   279     {
       
   280     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::RunL() >>"), RThread().Id().operator TUint()));
       
   281     if( iStatus.Int() == KErrNone )
       
   282         {
       
   283         iObserver->vsProviderSwitchDone( iOldProvider );
       
   284         }
       
   285     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::RunL() <<"), RThread().Id().operator TUint()));
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CVSDataSourceObserverAO::DoCancel
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CVSDataSourceObserverAO::DoCancel()
       
   293     {
       
   294     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::DoCancel() >>"), RThread().Id().operator TUint()));
       
   295     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::DoCancel() <<"), RThread().Id().operator TUint()));
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // CVSDataSourceObserverAO::Signal
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void CVSDataSourceObserverAO::Signal( TInt aError )
       
   303     {
       
   304     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::Signal() >>"), RThread().Id().operator TUint()));
       
   305     TRequestStatus* pStatus = &iStatus;
       
   306     RThread me;
       
   307     if( me.Id() == iThreadId )
       
   308         {
       
   309         User::RequestComplete( pStatus, aError );
       
   310         }
       
   311     else
       
   312         {
       
   313         if( me.Open( iThreadId ) == KErrNone )
       
   314             {
       
   315             *pStatus = KRequestPending;
       
   316             me.RequestComplete( pStatus, aError );
       
   317             me.Close();
       
   318             }
       
   319         else
       
   320             {
       
   321             Panic( EVSPanicThreadOpenFailure );
       
   322             }
       
   323         }
       
   324     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceObserverAO::Signal() <<"), RThread().Id().operator TUint()));
       
   325     }
       
   326 
       
   327 
       
   328 
       
   329 // =============================== CVSDataSource ===============================
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CVSDataSource::NewSourceL
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 CVSDataSource* CVSDataSource::NewSourceL( MVSDataSourceObserver* aObserver )
       
   336     {
       
   337     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::NewSourceL() >>"), RThread().Id().operator TUint()));
       
   338     if ( aObserver == NULL )
       
   339         {
       
   340         User::Leave( KErrArgument );
       
   341         }
       
   342     CVSDataSource* self = CVSDataSourceImp::NewSourceL();
       
   343     CleanupStack::PushL( self );
       
   344     self->ConstructL( aObserver );
       
   345     CleanupStack::Pop( self );
       
   346     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::NewSourceL() <<"), RThread().Id().operator TUint()));
       
   347     return self;
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CVSDataSource::CVSDataSource
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 CVSDataSource::CVSDataSource() : MVTVideoSource( KNullUid )
       
   355     {
       
   356     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::CVSDataSource() >>"), RThread().Id().operator TUint()));
       
   357     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::CVSDataSource() <<"), RThread().Id().operator TUint()));
       
   358     }
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CVSDataSource::ConstructL
       
   362 // -----------------------------------------------------------------------------
       
   363 //
       
   364 void CVSDataSource::ConstructL( MVSDataSourceObserver* aObserver )
       
   365     {
       
   366     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::ConstructL() >>"), RThread().Id().operator TUint()));
       
   367     iObserver = aObserver;
       
   368     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSource::ConstructL() <<"), RThread().Id().operator TUint()));
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CVSDataSourceImp::NewSourceL
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 CVSDataSourceImp* CVSDataSourceImp::NewSourceL()
       
   376     {
       
   377     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::NewSourceL() >>"), RThread().Id().operator TUint()));
       
   378     CVSDataSourceImp* self = new (ELeave) CVSDataSourceImp;
       
   379     CleanupStack::PushL( self );
       
   380     self->ConstructSourceL( KNullDesC8 );
       
   381     CleanupStack::Pop( self );
       
   382     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::NewSourceL() <<"), RThread().Id().operator TUint()));
       
   383     return self;
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CVSDataSourceImp::SwitchDataProviderL
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 void CVSDataSourceImp::SwitchDataProviderL(
       
   391     CVSDataProvider* aNewProvider )
       
   392     {
       
   393     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SwitchDataProviderL() >>"), RThread().Id().operator TUint()));
       
   394     if ( aNewProvider == NULL )
       
   395         {
       
   396         __IF_DEBUG(Print(_L("SwitchDataProviderL(): NULL argument, leaving.")));
       
   397         User::Leave( KErrArgument );
       
   398         }
       
   399     // Is new provider same as old, then no need to change provider
       
   400     if ( aNewProvider == &DataProvider() )
       
   401         {
       
   402         __IF_DEBUG(Print(_L("SwitchDataProviderL(): New provider same as old: 0x%x"), aNewProvider));
       
   403         Observer().vsProviderSwitchDone( aNewProvider );
       
   404         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SwitchDataProvider() <<"), RThread().Id().operator TUint()));
       
   405         return;
       
   406         }
       
   407     // If Protocol is in the currently initializing itself then complete this switch
       
   408     // request when Protocol ready (from SourcePlayL). No need to protect
       
   409     // iProtoInitOngoing test with CS.
       
   410     if ( iProtoInitOngoing )
       
   411         {
       
   412         __IF_DEBUG(Print(_L("PROTOCOL INIT ONGOING, PENDING SWITCH FOR A WHILE")));
       
   413         iProtoInitCS.Wait();
       
   414         iProviderSwitchRequestDuringProtoInit = ETrue;
       
   415         iNewProvider = aNewProvider;
       
   416         iProtoInitCS.Signal();
       
   417         return;
       
   418         }
       
   419     // Switch provider
       
   420     if ( iThreadLogonCalled )
       
   421         {
       
   422         iProviderSwitchAO->InitiateProviderSwitch( aNewProvider );
       
   423         }
       
   424     else
       
   425         {
       
   426         CVSDataProvider* oldProvider = SwitchProvider( aNewProvider );
       
   427         Observer().vsProviderSwitchDone( oldProvider );
       
   428         }
       
   429     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SwitchDataProviderL() <<"), RThread().Id().operator TUint()));
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CVSDataSourceImp::PauseSending
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 void CVSDataSourceImp::PauseSending()
       
   437     {
       
   438     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::PauseSending() >>"), RThread().Id().operator TUint()));
       
   439     iPauseCs.Wait();
       
   440     iSendingPaused = ETrue;
       
   441     iPauseCs.Signal();
       
   442     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::PauseSending() <<"), RThread().Id().operator TUint()));
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CVSDataSourceImp::ResumeSending
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CVSDataSourceImp::ResumeSending()
       
   450     {
       
   451     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::ResumeSending() >>"), RThread().Id().operator TUint()));
       
   452     iPauseCs.Wait();
       
   453     iSendingPaused = EFalse;
       
   454     iPauseCs.Signal();
       
   455     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::ResumeSending() <<"), RThread().Id().operator TUint()));
       
   456     }
       
   457 
       
   458 // -----------------------------------------------------------------------------
       
   459 // CVSDataSourceImp::~CVSDataSourceImp
       
   460 // -----------------------------------------------------------------------------
       
   461 //
       
   462 CVSDataSourceImp::~CVSDataSourceImp()
       
   463     {
       
   464     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::~CVSDataSourceImp() >>"), RThread().Id().operator TUint()));
       
   465     delete iDataSourceObserverAO;
       
   466     delete iProviderSwitchAO;
       
   467     iProtoMimeTypes.Close();
       
   468     delete iSupportedVideoFormats;
       
   469     iSupportedVideoSizes.Close();
       
   470     iSupportedFrameRates.Close();
       
   471     // Assuming no need to use critical section here
       
   472     iFreeQueue.Close();
       
   473     iActiveQueue.Close();
       
   474     iAllocatedBuffers.ResetAndDestroy();
       
   475     iQueueCs.Close();
       
   476     iPauseCs.Close();
       
   477     iProtoInitCS.Close();
       
   478     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::~CVSDataSourceImp() <<"), RThread().Id().operator TUint()));
       
   479     }
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // CVSDataSourceImp::FillBufferL
       
   483 // -----------------------------------------------------------------------------
       
   484 //
       
   485 void CVSDataSourceImp::FillBufferL(
       
   486     CMMFBuffer* aBuffer,
       
   487     MVTVideoSink* aConsumer,
       
   488     TMediaId aMediaId )
       
   489     {
       
   490     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL() >>"), RThread().Id().operator TUint()));
       
   491 
       
   492     if ( aBuffer == NULL || aConsumer == NULL )
       
   493         {
       
   494         User::Leave( KErrArgument );
       
   495         }
       
   496 
       
   497     if ( aBuffer->Type() != KUidMmfDataBuffer )
       
   498         {
       
   499         User::Leave( KErrArgument );
       
   500         }
       
   501 
       
   502     DataProvider().SetFillBufferParams( aConsumer, aMediaId );
       
   503 
       
   504     iPauseCs.Wait();
       
   505     const TBool sendingPaused( iSendingPaused );
       
   506     iPauseCs.Signal();
       
   507 
       
   508     if( iSwitchOngoing )
       
   509         {
       
   510         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL(): Switch ongoing"), RThread().Id().operator TUint()));
       
   511         // queues are locked by us -> it's safe to modify queues without
       
   512         // locking
       
   513         User::LeaveIfError(
       
   514             FreeBufferNoWait( static_cast<CVSMMFDataBuffer*>( aBuffer ) ) );
       
   515         }
       
   516     else if( iSwitchPending )
       
   517         {
       
   518         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL(): Switch pending"), RThread().Id().operator TUint()));
       
   519         FreeBufferL( static_cast<CVSMMFDataBuffer*>( aBuffer ) );
       
   520         TRAPD(
       
   521             err,
       
   522             DoProviderSwitchL(
       
   523                 iProviderSwitchAO->NewDataProvider(),
       
   524                 iProviderSwitchAO->EventHandler() ) );
       
   525         if( err != KErrNone )
       
   526             {
       
   527             __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL(): Error in switch = %d"), RThread().Id().operator TUint(), err));
       
   528             // Not leaving from here (it was provider switch related error),
       
   529             // let VT take care of the rest
       
   530             ProviderSwitchError( err );
       
   531             }
       
   532         }
       
   533     else if( sendingPaused )
       
   534         {
       
   535         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL(): Sending Paused"), RThread().Id().operator TUint()));
       
   536         // queues are locked by us -> it's safe to modify queues without
       
   537         // locking
       
   538         User::LeaveIfError(
       
   539             FreeBufferNoWait( static_cast<CVSMMFDataBuffer*>( aBuffer ) ) );
       
   540         }
       
   541     else
       
   542         {
       
   543         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL(): No switch -> normal FillBufferL()"), RThread().Id().operator TUint()));
       
   544         FreeBufferL( static_cast<CVSMMFDataBuffer*>( aBuffer ) );
       
   545         DataProvider().FillBufferL( aBuffer, aConsumer, aMediaId );
       
   546         }
       
   547 
       
   548     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FillBufferL() <<"), RThread().Id().operator TUint()));
       
   549     }
       
   550 
       
   551 // -----------------------------------------------------------------------------
       
   552 // CVSDataSourceImp::BufferEmptiedL
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void CVSDataSourceImp::BufferEmptiedL( CMMFBuffer* /*aBuffer*/ )
       
   556     {
       
   557     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::BufferEmptiedL() >>"), RThread().Id().operator TUint()));
       
   558     User::Leave( KErrNotSupported );
       
   559     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::BufferEmptiedL() <<"), RThread().Id().operator TUint()));
       
   560     }
       
   561 
       
   562 // -----------------------------------------------------------------------------
       
   563 // CVSDataSourceImp::CanCreateSourceBuffer
       
   564 // -----------------------------------------------------------------------------
       
   565 //
       
   566 TBool CVSDataSourceImp::CanCreateSourceBuffer()
       
   567     {
       
   568     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CanCreateSourceBuffer() >>"), RThread().Id().operator TUint()));
       
   569     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CanCreateSourceBuffer) <<"), RThread().Id().operator TUint()));
       
   570     return ETrue;
       
   571     }
       
   572 
       
   573 // -----------------------------------------------------------------------------
       
   574 // CVSDataSourceImp::CreateSourceBufferL
       
   575 // -----------------------------------------------------------------------------
       
   576 //
       
   577 CMMFBuffer* CVSDataSourceImp::CreateSourceBufferL(
       
   578     TMediaId /*aMediaId*/,
       
   579     TBool &aReference )
       
   580     {
       
   581     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL() >>"), RThread().Id().operator TUint()));
       
   582     if ( iRequestedBufferCount >= KNoOfSupportedBuffers )
       
   583         {
       
   584         User::Leave( KErrOverflow );
       
   585         }
       
   586     aReference = ETrue;
       
   587     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL(): Returning buffer: %d [0x%08x]"), RThread().Id().operator TUint(), iRequestedBufferCount + 1, iAllocatedBuffers[ iRequestedBufferCount ]));
       
   588     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL() <<"), RThread().Id().operator TUint()));
       
   589     return iAllocatedBuffers[ iRequestedBufferCount++ ];
       
   590     }
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // CVSDataSourceImp::CreateSourceBufferL
       
   594 // -----------------------------------------------------------------------------
       
   595 //
       
   596 CMMFBuffer* CVSDataSourceImp::CreateSourceBufferL(
       
   597     TMediaId /*aMediaId*/,
       
   598     CMMFBuffer& /*aSinkBuffer*/,
       
   599     TBool &aReference )
       
   600     {
       
   601     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL() >>"), RThread().Id().operator TUint()));
       
   602     if ( iRequestedBufferCount >= KNoOfSupportedBuffers )
       
   603         {
       
   604         User::Leave( KErrOverflow );
       
   605         }
       
   606     aReference = ETrue;
       
   607     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL(): Returning buffer: %d [0x%08x]"), RThread().Id().operator TUint(), iRequestedBufferCount + 1, iAllocatedBuffers[ iRequestedBufferCount ]));
       
   608     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CreateSourceBufferL() <<"), RThread().Id().operator TUint()));
       
   609     return iAllocatedBuffers[ iRequestedBufferCount++ ];
       
   610     }
       
   611 
       
   612 // -----------------------------------------------------------------------------
       
   613 // CVSDataSourceImp::SourceThreadLogon
       
   614 // -----------------------------------------------------------------------------
       
   615 //
       
   616 TInt CVSDataSourceImp::SourceThreadLogon( MAsyncEventHandler& aEventHandler )
       
   617     {
       
   618     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceThreadLogon() >>"), RThread().Id().operator TUint()));
       
   619     __ASSERT_DEBUG( (&DataProvider()) != NULL,
       
   620         Panic( EVSPanicProviderNotSet ) );
       
   621     TInt err( KErrNone );
       
   622     if( !iFbsStarted )
       
   623         {
       
   624         // Caller does not start the FBS server when compiled on proxy mode
       
   625         // so we have to take care of that by ourself.
       
   626         err = FbsStartup();
       
   627         if( err == KErrNone )
       
   628             {
       
   629             err = RFbsSession::Connect();
       
   630             if( err == KErrNone )
       
   631                 {
       
   632                 iFbsStarted = ETrue;
       
   633                 }
       
   634             }
       
   635         }
       
   636     if( !iThreadLogonCalled && ( err == KErrNone ) )
       
   637         {
       
   638         err = DataProvider().SourceThreadLogon( aEventHandler );
       
   639         if (err != KErrNone)
       
   640             {
       
   641             return err;
       
   642             }
       
   643         iProviderSwitchAO->ThreadLogon( aEventHandler );
       
   644         iThreadLogonCalled = ETrue;
       
   645         iProtoInitOngoing = ETrue;
       
   646         }
       
   647     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceThreadLogon() <<"), RThread().Id().operator TUint()));
       
   648     return err;
       
   649     }
       
   650 
       
   651 // -----------------------------------------------------------------------------
       
   652 // CVSDataSourceImp::SourceThreadLogoff
       
   653 // -----------------------------------------------------------------------------
       
   654 //
       
   655 void CVSDataSourceImp::SourceThreadLogoff()
       
   656     {
       
   657     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceThreadLogoff() >>"), RThread().Id().operator TUint()));
       
   658 
       
   659     iThreadLogonCalled = EFalse;
       
   660 
       
   661     if ( iProviderSwitchRequestDuringProtoInit )
       
   662         {
       
   663         __IF_DEBUG(Print(_L("PROTOCOL INIT FAILED, NOW COMPLETING THE SWITCH REQUEST WITH ERROR")));
       
   664         iProviderSwitchRequestDuringProtoInit = EFalse;
       
   665         ProviderSwitchError( KErrAbort );
       
   666         }
       
   667 
       
   668     if( &DataProvider() )
       
   669         {
       
   670         DataProvider().SourceThreadLogoff();
       
   671         }
       
   672 
       
   673     iSwitchOngoing = iSwitchPending = EFalse;
       
   674 
       
   675     iProtoInitOngoing = EFalse;
       
   676 
       
   677     // If provider switch ongoing complete it
       
   678     if ( iProviderSwitchAO->NewDataProvider() != NULL )
       
   679         {
       
   680         CVSDataProvider* oldProvider =
       
   681             SwitchProvider( iProviderSwitchAO->NewDataProvider() );
       
   682         Observer().vsProviderSwitchDone( oldProvider );
       
   683         }
       
   684 
       
   685     iProviderSwitchAO->ThreadLogoff();
       
   686 
       
   687     delete iFormat; iFormat = 0;
       
   688 
       
   689     iCallFlags = 0;
       
   690 
       
   691     // Clear iFreeQueue
       
   692     while( iFreeQueue.Count() )
       
   693         {
       
   694         iFreeQueue.Remove( 0 );
       
   695         }
       
   696 
       
   697     // Clear iActiveQueue
       
   698     while( iActiveQueue.Count() )
       
   699         {
       
   700         iActiveQueue.Remove( 0 );
       
   701         }
       
   702 
       
   703     // Set frame buffer to NULL for all buffers
       
   704     for( TInt i = 0; i < iRequestedBufferCount; i++ )
       
   705         {
       
   706         TInt err = KErrNone;
       
   707         // Does not leave when framebuffer is set to NULL
       
   708         // Both type of buffers needs to be nulled
       
   709         TRAP( err, iAllocatedBuffers[i]->SetFrameBufferL( ( MCameraBuffer* )NULL ));
       
   710 		TRAP( err, iAllocatedBuffers[i]->SetFrameBufferL( ( MFrameBuffer* )NULL ));        }
       
   711 
       
   712     // All pre-created buffers are free again
       
   713     iRequestedBufferCount = 0;
       
   714 
       
   715     if( iFbsStarted )
       
   716         {
       
   717         RFbsSession::Disconnect();
       
   718         iFbsStarted = EFalse;
       
   719         }
       
   720 
       
   721     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceThreadLogoff() <<"), RThread().Id().operator TUint()));
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CVSDataSourceImp::SourcePrimeL
       
   726 // -----------------------------------------------------------------------------
       
   727 //
       
   728 TInt CVSDataSourceImp::SourcePrimeL()
       
   729     {
       
   730     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePrimeL() >>"), RThread().Id().operator TUint()));
       
   731     TRAPD( result, DataProvider().SourcePrimeL() );
       
   732     if( ( result == KErrNone ) || ( result == KRequestPending ) )
       
   733         {
       
   734         iDSState = CVSDataSourceImp::EPrimed;
       
   735         }
       
   736     else
       
   737         {
       
   738         User::Leave( result );
       
   739         }
       
   740     iProtoInitOngoing = EFalse;
       
   741     // If provider switch was requested when Proto was  initializing itself then
       
   742     // fill switch request now
       
   743     iProtoInitCS.Wait();
       
   744     if ( iProviderSwitchRequestDuringProtoInit )
       
   745         {
       
   746         __IF_DEBUG(Print(_L("PROTOCOL INIT READY, NOW COMPLETING THE SWITCH REQUEST")));
       
   747         iProviderSwitchRequestDuringProtoInit = EFalse;
       
   748         iProviderSwitchAO->InitiateProviderSwitch( iNewProvider );
       
   749         iNewProvider = NULL;
       
   750         }
       
   751     iProtoInitCS.Signal();
       
   752     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePrimeL() <<"), RThread().Id().operator TUint()));
       
   753     return result;
       
   754     }
       
   755 
       
   756 // -----------------------------------------------------------------------------
       
   757 // CVSDataSourceImp::SourcePlayL
       
   758 // -----------------------------------------------------------------------------
       
   759 //
       
   760 TInt CVSDataSourceImp::SourcePlayL()
       
   761     {
       
   762     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePlayL() >>"), RThread().Id().operator TUint()));
       
   763     if( !iSwitchOngoing )
       
   764         {
       
   765         DataProvider().SourcePlayL();
       
   766         }
       
   767     else
       
   768         {
       
   769         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePlayL(): Switch ongoing -> updating state only, play will be called after switch"), RThread().Id().operator TUint()));
       
   770         }
       
   771     iDSState = CVSDataSourceImp::EPlaying;
       
   772     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePlayL() <<"), RThread().Id().operator TUint()));
       
   773     return KErrNone;
       
   774     }
       
   775 
       
   776 // -----------------------------------------------------------------------------
       
   777 // CVSDataSourceImp::SourcePauseL
       
   778 // -----------------------------------------------------------------------------
       
   779 //
       
   780 TInt CVSDataSourceImp::SourcePauseL()
       
   781     {
       
   782     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePauseL() >>"), RThread().Id().operator TUint()));
       
   783     if( !iSwitchOngoing )
       
   784         {
       
   785         DataProvider().SourcePauseL();
       
   786         }
       
   787     else
       
   788         {
       
   789         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePlayL(): Switch ongoing -> updating state only"), RThread().Id().operator TUint()));
       
   790         }
       
   791     iDSState = CVSDataSourceImp::EPrimed;
       
   792     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePauseL() <<"), RThread().Id().operator TUint()));
       
   793     return KErrNone;
       
   794     }
       
   795 
       
   796 // -----------------------------------------------------------------------------
       
   797 // CVSDataSourceImp::SourceStopL
       
   798 // -----------------------------------------------------------------------------
       
   799 //
       
   800 TInt CVSDataSourceImp::SourceStopL()
       
   801     {
       
   802     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceStopL() >>"), RThread().Id().operator TUint()));
       
   803     if( !iSwitchOngoing )
       
   804         {
       
   805         DataProvider().SourceStopL();
       
   806         }
       
   807     else
       
   808         {
       
   809         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourcePlayL(): Switch ongoing -> updating state only"), RThread().Id().operator TUint()));
       
   810         }
       
   811     iDSState = CVSDataSourceImp::EStopped;
       
   812     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SourceStopL() <<"), RThread().Id().operator TUint()));
       
   813     return KErrNone;
       
   814     }
       
   815 
       
   816 // -----------------------------------------------------------------------------
       
   817 // CVSDataSourceImp::ConstructSourceL
       
   818 // -----------------------------------------------------------------------------
       
   819 //
       
   820 void CVSDataSourceImp::ConstructSourceL( const TDesC8& /*aInitData*/ )
       
   821     {
       
   822     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::ConstructSourceL() >>"), RThread().Id().operator TUint()));
       
   823     iYuv420PlanarDesc.Zero();
       
   824     iYuv420PlanarDesc.Append( KVtVideoMIMETypeYUV420 );
       
   825     User::LeaveIfError( iProtoMimeTypes.Append( &iYuv420PlanarDesc ) );
       
   826     iProviderSwitchAO = CVSProviderSwitchAO::NewL( this );
       
   827     iDataSourceObserverAO = new (ELeave) CVSDataSourceObserverAO();
       
   828     iSupportedVideoFormats =
       
   829         new (ELeave) CDesC8ArrayFlat( KNoOfSupportedFormats );
       
   830     // Get formats supported by all providers
       
   831     CVSDataProvider::EnumerateVideoFrameFormatsL( iSupportedVideoFormats );
       
   832     // Get video frame sizes for specified format supported by all providers
       
   833     TInt i, j;
       
   834     i = j = 0;
       
   835     for ( i = 0; i < iSupportedVideoFormats->Count(); i++ )
       
   836         {
       
   837         TPtrC8 format = iSupportedVideoFormats->MdcaPoint( i );
       
   838         RArray<TSize> sizeArray;
       
   839         CleanupClosePushL( sizeArray );
       
   840         CVSDataProvider::EnumerateVideoFrameSizesL( sizeArray, format );
       
   841         for ( j = 0; j < sizeArray.Count(); j++ )
       
   842             {
       
   843             TVSFrameSize sizePerFormat;
       
   844             sizePerFormat.iFormat.Set( format );
       
   845             sizePerFormat.iSize = sizeArray[j];
       
   846             User::LeaveIfError( iSupportedVideoSizes.Append( sizePerFormat ) );
       
   847             }
       
   848         CleanupStack::PopAndDestroy(); // sizeArray
       
   849         }
       
   850     // Get video frame rates for specified frame format and size supported
       
   851     // by all providers
       
   852     for ( i = 0; i < iSupportedVideoSizes.Count(); i++ )
       
   853         {
       
   854         RArray<TReal32> rateArray;
       
   855         CleanupClosePushL( rateArray );
       
   856         CVSDataProvider::EnumerateVideoFrameRatesL(
       
   857             rateArray,
       
   858             iSupportedVideoSizes[i].iFormat,
       
   859             iSupportedVideoSizes[i].iSize );
       
   860         for ( j = 0; j < rateArray.Count(); j++ )
       
   861             {
       
   862             TVSFrameRate framePerSizeAndFormat;
       
   863             framePerSizeAndFormat.iFormat.Set(
       
   864                 iSupportedVideoSizes[i].iFormat );
       
   865             framePerSizeAndFormat.iSize = iSupportedVideoSizes[i].iSize;
       
   866             framePerSizeAndFormat.iRate = rateArray[j];
       
   867             User::LeaveIfError( iSupportedFrameRates.Append(
       
   868                 framePerSizeAndFormat ) );
       
   869             }
       
   870         CleanupStack::PopAndDestroy(); // rateArray
       
   871         }
       
   872     // Create queue criticalsection
       
   873     User::LeaveIfError( iQueueCs.CreateLocal() );
       
   874     // Create VTEng-Protocol guard
       
   875     User::LeaveIfError( iPauseCs.CreateLocal() );
       
   876     // Pre-create buffers
       
   877     for( i = 0; i < KNoOfSupportedBuffers; i++ )
       
   878         {
       
   879         User::LeaveIfError( iFreeQueue.Append( 0 ) );
       
   880         User::LeaveIfError( iActiveQueue.Append( 0 ) );
       
   881         CVSMMFDataBuffer* buffer = CVSMMFDataBuffer::NewL( KSourceBufferSize );
       
   882         CleanupStack::PushL( buffer );
       
   883         User::LeaveIfError( iAllocatedBuffers.Append( buffer ) );
       
   884         CleanupStack::Pop(); // buffer
       
   885         }
       
   886     for( i = 0; i < KNoOfSupportedBuffers; i++ )
       
   887         {
       
   888         iFreeQueue.Remove( 0 );
       
   889         iActiveQueue.Remove( 0 );
       
   890         }
       
   891     // Init critical sections
       
   892     User::LeaveIfError( iProtoInitCS.CreateLocal() );
       
   893     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::ConstructSourceL() <<"), RThread().Id().operator TUint()));
       
   894     }
       
   895 
       
   896 // -----------------------------------------------------------------------------
       
   897 // CVSDataSourceImp::GetMultimediaTypesL
       
   898 // -----------------------------------------------------------------------------
       
   899 //
       
   900 const RArray<TDesC8*>& CVSDataSourceImp::GetMultimediaTypesL() const
       
   901     {
       
   902     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetMultimediaTypeL() >>"), RThread().Id().operator TUint()));
       
   903     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetMultimediaTypeL() <<"), RThread().Id().operator TUint()));
       
   904     return iProtoMimeTypes;
       
   905     }
       
   906 
       
   907 
       
   908 // -----------------------------------------------------------------------------
       
   909 // CVSDataSourceImp::SetFormatL
       
   910 // -----------------------------------------------------------------------------
       
   911 //
       
   912 void CVSDataSourceImp::SetFormatL( const TDesC8& aFormat )
       
   913     {
       
   914     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetFormatL() >>"), RThread().Id().operator TUint()));
       
   915     if ( &DataProvider() == NULL )
       
   916         {
       
   917         User::Leave( KErrBadHandle );
       
   918         }
       
   919     TBool supported = EFalse;
       
   920     for ( TInt i = 0; i < iSupportedVideoFormats->Count(); i++ )
       
   921         {
       
   922         TPtrC8 format = iSupportedVideoFormats->MdcaPoint( i );
       
   923         if ( aFormat == format )
       
   924             {
       
   925             supported = ETrue;
       
   926             break;
       
   927             }
       
   928         }
       
   929     if ( !supported )
       
   930         {
       
   931         User::Leave( KErrNotSupported );
       
   932         }
       
   933     DataProvider().SetFormatL( aFormat );
       
   934     delete iFormat; iFormat = 0;
       
   935     iFormat = aFormat.AllocL();
       
   936     iCallFlags |= ESetFormatCalled;
       
   937     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetFormatL() <<"), RThread().Id().operator TUint()));
       
   938     }
       
   939 
       
   940 // -----------------------------------------------------------------------------
       
   941 // CVSDataSourceImp::SetFrameRateL
       
   942 // -----------------------------------------------------------------------------
       
   943 //
       
   944 void CVSDataSourceImp::SetFrameRateL( TReal32 aFrameRate )
       
   945     {
       
   946     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetFrameRateL() >>"), RThread().Id().operator TUint()));
       
   947     if ( &DataProvider() == NULL )
       
   948         {
       
   949         User::Leave( KErrBadHandle );
       
   950         }
       
   951     TBool supported = EFalse;
       
   952     for ( TInt i = 0; i < iSupportedFrameRates.Count(); i++ )
       
   953         {
       
   954         TVSFrameRate rate = iSupportedFrameRates[i];
       
   955         if ( rate.iFormat == iFormat->Des() && rate.iSize == iSize)
       
   956             {
       
   957             if ( aFrameRate == rate.iRate )
       
   958                 {
       
   959                 supported = ETrue;
       
   960                 break;
       
   961                 }
       
   962             }
       
   963         }
       
   964     if ( !supported )
       
   965         {
       
   966         User::Leave( KErrNotSupported );
       
   967         }
       
   968     DataProvider().SetFrameRateL( aFrameRate );
       
   969     iFrameRate = aFrameRate;
       
   970     iCallFlags |= ESetFrameRateCalled;
       
   971     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetFrameRateL() <<"), RThread().Id().operator TUint()));
       
   972     }
       
   973 
       
   974 // -----------------------------------------------------------------------------
       
   975 // CVSDataSourceImp::SetVideoFrameSizeL
       
   976 // -----------------------------------------------------------------------------
       
   977 //
       
   978 void CVSDataSourceImp::SetVideoFrameSizeL( const TSize& aSize )
       
   979     {
       
   980     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetVideoFrameSizeL() >>"), RThread().Id().operator TUint()));
       
   981     if ( &DataProvider() == NULL )
       
   982         {
       
   983         User::Leave( KErrBadHandle );
       
   984         }
       
   985     TBool supported = EFalse;
       
   986     for ( TInt i = 0; i < iSupportedVideoSizes.Count(); i++ )
       
   987         {
       
   988         TVSFrameSize size = iSupportedVideoSizes[i];
       
   989         if ( size.iFormat == iFormat->Des() )
       
   990             {
       
   991             if ( aSize == size.iSize )
       
   992                 {
       
   993                 supported = ETrue;
       
   994                 break;
       
   995                 }
       
   996             }
       
   997         }
       
   998     if ( !supported )
       
   999         {
       
  1000         User::Leave( KErrNotSupported );
       
  1001         }
       
  1002     DataProvider().SetVideoFrameSizeL( aSize );
       
  1003     iSize = aSize;
       
  1004     iCallFlags |= ESetSizeCalled;
       
  1005     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::SetVideoFrameSizeL() <<"), RThread().Id().operator TUint()));
       
  1006     }
       
  1007 
       
  1008 // -----------------------------------------------------------------------------
       
  1009 // CVSDataSourceImp::GetVideoFrameSizeL
       
  1010 // -----------------------------------------------------------------------------
       
  1011 //
       
  1012 void CVSDataSourceImp::GetVideoFrameSizeL( TSize& aSize ) const
       
  1013     {
       
  1014     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetVideoFrameSizeL() >>"), RThread().Id().operator TUint()));
       
  1015     if ( &DataProvider() == NULL )
       
  1016         {
       
  1017         User::Leave( KErrBadHandle );
       
  1018         }
       
  1019     DataProvider().GetVideoFrameSizeL( aSize );
       
  1020     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetVideoFrameSizeL() <<"), RThread().Id().operator TUint()));
       
  1021     }
       
  1022 
       
  1023 
       
  1024 // -----------------------------------------------------------------------------
       
  1025 // CVSDataSourceImp::DoProviderSwitchL
       
  1026 // -----------------------------------------------------------------------------
       
  1027 //
       
  1028 void CVSDataSourceImp::DoProviderSwitchL(
       
  1029     CVSDataProvider* aNewProvider,
       
  1030     MAsyncEventHandler& aEventHandler )
       
  1031     {
       
  1032     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::DoProviderSwitchL() >>"), RThread().Id().operator TUint()));
       
  1033 
       
  1034     iQueueCs.Wait();
       
  1035 
       
  1036     // If there are entries in iActiveQueue wait until they are freed by Protocol
       
  1037     if ( iActiveQueue.Count() > 0 )
       
  1038         {
       
  1039         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::DoProviderSwitchL(): activequeue count > 0, <<"), RThread().Id().operator TUint()));
       
  1040         iSwitchPending = ETrue;
       
  1041         iQueueCs.Signal();
       
  1042         return;
       
  1043         }
       
  1044     else
       
  1045         {
       
  1046         iSwitchPending = EFalse;
       
  1047         }
       
  1048 
       
  1049     iSwitchOngoing = ETrue;
       
  1050 
       
  1051     /*
       
  1052      * THE FOLLOWING CALLS USE CActiveSchedulerWait WHICH MEANS SOME OTHER
       
  1053      * OBJECT MAY RUN IN BETWEEN THE FOLLOWING CALLS I.E. THESE CALLS ARE NOT
       
  1054      * SYNCHRONOUS.
       
  1055      */
       
  1056 
       
  1057     User::LeaveIfError( aNewProvider->SourceThreadLogon( aEventHandler ) );
       
  1058 
       
  1059     // Is format set by the Protocol
       
  1060     if ( iCallFlags & ESetFormatCalled )
       
  1061         {
       
  1062         aNewProvider->SetFormatL( *iFormat );
       
  1063         }
       
  1064     // Is size set by the Protocol
       
  1065     if ( iCallFlags & ESetSizeCalled )
       
  1066         {
       
  1067         aNewProvider->SetVideoFrameSizeL( iSize );
       
  1068         }
       
  1069     // Is framerate set by the Protocol
       
  1070     if ( iCallFlags & ESetFrameRateCalled )
       
  1071         {
       
  1072         aNewProvider->SetFrameRateL( iFrameRate );
       
  1073         }
       
  1074     // Stop old provider
       
  1075     if ( &DataProvider() != NULL )
       
  1076         {
       
  1077         switch( iDSState )
       
  1078             {
       
  1079         case EPlaying:
       
  1080             DataProvider().SourcePauseL();
       
  1081             DataProvider().SourceStopL();
       
  1082             break;
       
  1083 
       
  1084         case EPrimed:
       
  1085             DataProvider().SourceStopL();
       
  1086             break;
       
  1087 
       
  1088         default:
       
  1089             break;
       
  1090             };
       
  1091         DataProvider().SourceThreadLogoff();
       
  1092         }
       
  1093 
       
  1094     // Set new provider state according to current state
       
  1095     switch ( iDSState )
       
  1096         {
       
  1097         case EStopped:
       
  1098             break;
       
  1099 
       
  1100         case EPrimed:
       
  1101         case EPlaying:
       
  1102             aNewProvider->PrimeL();
       
  1103 
       
  1104             // check if SourcePlayL() was called during prime
       
  1105             if( iDSState == EPlaying )
       
  1106                 {
       
  1107                 aNewProvider->SourcePlayL();
       
  1108                 aNewProvider->SwitchFrom( DataProvider() );
       
  1109                 }
       
  1110             // or SourceStopL()
       
  1111             else if( iDSState == EStopped )
       
  1112                 {
       
  1113                 aNewProvider->SourceStopL();
       
  1114                 }
       
  1115             break;
       
  1116         }
       
  1117     /*
       
  1118      * THE FOLLOWING CALLS ARE SUPPOSED TO BE SYNCHRONOUS IN THAT THEY DO NOT
       
  1119      * LET ACTIVE SCHEDULER RUN SOME OTHER OBJECT.
       
  1120      */
       
  1121 
       
  1122     // Complete switch
       
  1123     CVSDataProvider* oldProvider = SwitchProvider( aNewProvider );
       
  1124 
       
  1125     // Set consumer and media id
       
  1126     DataProvider().SetFillBufferParams(
       
  1127         oldProvider->Consumer(),
       
  1128         oldProvider->MediaId() );
       
  1129 
       
  1130     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::DoProviderSwitchL(): iFreeQueue.Count() = %d, iActiveQueue.Count() = %d"), RThread().Id().operator TUint(), iFreeQueue.Count(), iActiveQueue.Count() ));
       
  1131 
       
  1132     // If old provider has pending FillBufferL() calls -> call FillBufferL()
       
  1133     // for new provider if it is in Playing state.
       
  1134 	if( iDSState == EPlaying )
       
  1135 		{
       
  1136         for ( TInt i = 0; i < iFreeQueue.Count(); i++ )
       
  1137             {
       
  1138             iFreeQueue[ i ]->Reset();
       
  1139             DataProvider().FillBufferL(
       
  1140                 iFreeQueue[i],
       
  1141                 DataProvider().Consumer(),
       
  1142                 DataProvider().MediaId() );
       
  1143     		}
       
  1144         }
       
  1145 
       
  1146     // Buffers that are being used by Protocol are set to be needing a reset
       
  1147     for ( TInt i = 0; i < iActiveQueue.Count(); i++ )
       
  1148         {
       
  1149         iActiveQueue[ i ]->SetNeedsReset( ETrue );
       
  1150         }
       
  1151 
       
  1152     iSwitchOngoing = EFalse;
       
  1153 
       
  1154     iQueueCs.Signal();
       
  1155 
       
  1156     iDataSourceObserverAO->vsProviderSwitchDone( Observer(), oldProvider );
       
  1157     iProviderSwitchAO->NewDataProvider() = NULL;
       
  1158 
       
  1159     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::DoProviderSwitchL() <<"), RThread().Id().operator TUint()));
       
  1160     }
       
  1161 
       
  1162 // -----------------------------------------------------------------------------
       
  1163 // CVSDataSourceImp::CVSDataSourceImp
       
  1164 // -----------------------------------------------------------------------------
       
  1165 //
       
  1166 void CVSDataSourceImp::ProviderSwitchError( TInt aError )
       
  1167     {
       
  1168     DataProvider().NotifyError( aError );
       
  1169     }
       
  1170 
       
  1171 // -----------------------------------------------------------------------------
       
  1172 // CVSDataSourceImp::CVSDataSourceImp
       
  1173 // -----------------------------------------------------------------------------
       
  1174 //
       
  1175 CVSDataSourceImp::CVSDataSourceImp()
       
  1176 : iFreeQueue( KNoOfSupportedBuffers ), iActiveQueue( KNoOfSupportedBuffers )
       
  1177     {
       
  1178     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CVSDataSourceImp() >>"), RThread().Id().operator TUint()));
       
  1179     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::CVSDataSourceImp() <<"), RThread().Id().operator TUint()));
       
  1180     }
       
  1181 
       
  1182 // -----------------------------------------------------------------------------
       
  1183 // CVSDataSourceImp::GetBufferL
       
  1184 // -----------------------------------------------------------------------------
       
  1185 //
       
  1186 CVSMMFDataBuffer* CVSDataSourceImp::GetBufferL( TBool aRemove )
       
  1187     {
       
  1188     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetBufferL() >>"), RThread().Id().operator TUint()));
       
  1189     if( !iQueueCs.IsBlocked() )
       
  1190         {
       
  1191         iPauseCs.Wait();
       
  1192         const TBool sendingPaused( iSendingPaused );
       
  1193         iPauseCs.Signal();
       
  1194 
       
  1195         iQueueCs.Wait();
       
  1196 
       
  1197         // while switch is pending, always return NULL we don't want to let
       
  1198         // buffer float around
       
  1199         if( iSwitchPending || sendingPaused )
       
  1200             {
       
  1201             iQueueCs.Signal();
       
  1202             __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetBufferL(): Switch pending or sending paused -> returning NULL"), RThread().Id().operator TUint()));
       
  1203             return 0;
       
  1204             }
       
  1205 
       
  1206         // If no buffers in free (FillBufferL is called) queue -> return NULL
       
  1207         if( !iFreeQueue.Count() )
       
  1208             {
       
  1209             iQueueCs.Signal();
       
  1210             __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetBufferL(): NO free buffers -> returning NULL"), RThread().Id().operator TUint()));
       
  1211             return 0;
       
  1212             }
       
  1213         // Otherwise get first buffer
       
  1214         CVSMMFDataBuffer* temp = iFreeQueue[ 0 ];
       
  1215         // Move to active (BufferFilledL is called) queue if requested
       
  1216         if( aRemove )
       
  1217             {
       
  1218             iFreeQueue.Remove( 0 );
       
  1219             TInt err = iActiveQueue.Append( temp );
       
  1220             if ( err != KErrNone )
       
  1221                 {
       
  1222                 iQueueCs.Signal();
       
  1223                 User::Leave( err );
       
  1224                 }
       
  1225             }
       
  1226         iQueueCs.Signal();
       
  1227         __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetBufferL() returning [$%x]"), RThread().Id().operator TUint(), temp));
       
  1228         return temp;
       
  1229         }
       
  1230     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::GetBufferL(): blocked, returning [$%08x]"), RThread().Id().operator TUint(), 0));
       
  1231     return 0;
       
  1232     }
       
  1233 
       
  1234 // -----------------------------------------------------------------------------
       
  1235 // CVSDataSourceImp::FreeBufferL
       
  1236 // -----------------------------------------------------------------------------
       
  1237 //
       
  1238 void CVSDataSourceImp::FreeBufferL( CVSMMFDataBuffer* aBuffer )
       
  1239     {
       
  1240     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FreeBufferL() >>"), RThread().Id().operator TUint()));
       
  1241     iQueueCs.Wait();
       
  1242     TInt err = FreeBufferNoWait( aBuffer );
       
  1243     iQueueCs.Signal();
       
  1244     User::LeaveIfError( err );
       
  1245     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FreeBufferL() <<"), RThread().Id().operator TUint()));
       
  1246     }
       
  1247 
       
  1248 
       
  1249 // -----------------------------------------------------------------------------
       
  1250 // CVSDataSourceImp::FreeBufferNoWait( CVSMMFDataBuffer* aBuffer )
       
  1251 // -----------------------------------------------------------------------------
       
  1252 //
       
  1253 TInt CVSDataSourceImp::FreeBufferNoWait( CVSMMFDataBuffer* aBuffer )
       
  1254     {
       
  1255     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FreeBufferNoWait() >>"), RThread().Id().operator TUint()));
       
  1256 
       
  1257     // Append buffer into free (FillBufferL is called) queue
       
  1258     TInt err( KErrNone );
       
  1259 
       
  1260     // Try finding buffer from active (BufferFilledL is called) queue
       
  1261     TInt actIdx = iActiveQueue.Find( aBuffer );
       
  1262 
       
  1263     // If it is found, remove it from there
       
  1264     if( actIdx != KErrNotFound )
       
  1265         {
       
  1266         iActiveQueue.Remove( actIdx );
       
  1267         TRAP( err, aBuffer->ReleaseL() );
       
  1268         if( err != KErrNone )
       
  1269             {
       
  1270             return err;
       
  1271             }
       
  1272         }
       
  1273 
       
  1274     if( iFreeQueue.Find( aBuffer ) == KErrNotFound )
       
  1275         {
       
  1276         err = iFreeQueue.Append( aBuffer );
       
  1277         }
       
  1278 
       
  1279 #ifdef _DEBUG
       
  1280     TBuf<256> b;
       
  1281     TInt i;
       
  1282     b = _L("Active: ");
       
  1283     for( i = 0; i < iActiveQueue.Count(); i++ )
       
  1284         {
       
  1285         if( i > 0 )
       
  1286             {
       
  1287             b.Append( _L(", ") );
       
  1288             }
       
  1289         TBuf<16> b1;
       
  1290         b1.Format( _L("[$%08x]"), iActiveQueue[i] );
       
  1291         b.Append( b1 );
       
  1292         }
       
  1293     RDebug::Print( b );
       
  1294 
       
  1295     b = _L("Free  : ");
       
  1296     for( i = 0; i < iFreeQueue.Count(); i++ )
       
  1297         {
       
  1298         if( i > 0 )
       
  1299             {
       
  1300             b.Append( _L(", ") );
       
  1301             }
       
  1302         TBuf<16> b1;
       
  1303         b1.Format( _L("[$%08x]"), iFreeQueue[i] );
       
  1304         b.Append( b1 );
       
  1305         }
       
  1306     RDebug::Print( b );
       
  1307 #endif
       
  1308     __IF_DEBUG(Print(_L("VideoSource[%d]: CVSDataSourceImp::FreeBufferNoWait() <<"), RThread().Id().operator TUint()));
       
  1309 
       
  1310     return err;
       
  1311     }
       
  1312 
       
  1313 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1314 
       
  1315 //  End of File