vtengines/videoteleng/Src/State/CVtEngStateBase.cpp
changeset 0 ed9695c8bcbe
equal deleted inserted replaced
-1:000000000000 0:ed9695c8bcbe
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Base class for states
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "CVtEngStateBase.h"
       
    21 #include    "CVtEngOperation.h"
       
    22 #include    "CVtEngHandlerContainer.h"
       
    23 #include    "TVtEngOperationUtils.h"
       
    24 #include    "CVtEngStateInitializing.h"
       
    25 #include    "CVtEngStateAlerting.h"
       
    26 #include    "CVtEngStateRinging.h"
       
    27 #include    "CVtEngStateConnected.h"
       
    28 #include    "CVtEngStateClearing.h"
       
    29 #include    "CVtEngStateOpen.h"
       
    30 #include    "CVtEngStateNegotiating.h"
       
    31 #include    "CVtEngStateIdle.h"
       
    32 #include    "CVtEngSettings.h"
       
    33 #include    "MVtEngSessionInfo.h"
       
    34 #include    "VtEngUtils.h"
       
    35 #include    "VtEngPanic.h"
       
    36 #include    <cvtlogger.h>
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CVtEngStateBase::CVtEngStateBase
       
    42 // C++ constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CVtEngStateBase::CVtEngStateBase(
       
    47     CVtEngStateManager& aStateManager,
       
    48     CVtEngEventManager& aEventManager )
       
    49     : iStateManager( aStateManager ),
       
    50       iEventManager( aEventManager )
       
    51     {
       
    52     }
       
    53 
       
    54 // Destructor
       
    55 CVtEngStateBase::~CVtEngStateBase()
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CVtEngStateBase::OpenL
       
    61 // Changes state.
       
    62 //
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CVtEngStateBase::OpenL( CVtEngStateBase* aOldState )
       
    66     {
       
    67     __VTPRINTENTER( "StateBase.Open" )
       
    68     __VTPRINT( DEBUG_GEN, "StateBase. **** State Change ****" )
       
    69     iStateManager.SetState( *this );
       
    70     if ( aOldState )
       
    71         {
       
    72         aOldState->Close();
       
    73         }
       
    74     // call derived class open.
       
    75     DoOpenL();
       
    76     __VTPRINTEXIT( "StateBase.Open" )
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CVtEngStateBase::Close
       
    81 // Closes state.
       
    82 //
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CVtEngStateBase::Close()
       
    86     {
       
    87     delete this;
       
    88     }
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CVtEngStateBase::SaveToSettingsL
       
    93 //
       
    94 // Saves configuration to settings.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CVtEngStateBase::SaveToSettingsL( CVtEngOperation& aOp )
       
    98     {
       
    99     const TVtEngCommandId command( aOp.Command() );
       
   100 
       
   101     if ( command == KVtEngSetAudioVolume )
       
   102         {
       
   103         MVtEngAudio::TVtEngOutputVolume volume;
       
   104         TVtEngOpParamUtil<MVtEngAudio::TVtEngOutputVolume>::Set( volume, aOp );
       
   105         CVtEngSettings& settings = CVtEngUtility::Settings();
       
   106         settings.SetVolume( volume.iHandsetVolume, volume.iHandsfreeVolume, ETrue );
       
   107         settings.SetVolume( volume.iHandsetVolume, volume.iHandsfreeVolume, EFalse );
       
   108         }    
       
   109     }
       
   110 // -----------------------------------------------------------------------------
       
   111 // CVtEngStateBase::NotifyStateChangeL
       
   112 // Notifies state change to event manager.
       
   113 //
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CVtEngStateBase::NotifyStateChange(
       
   117     MVtEngSessionInfo::TSessionState /*aNewState*/ )
       
   118     {
       
   119     iEventManager.SessionStateChanged( );
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CVtEngStateBase::SetVideoEnabled
       
   124 //
       
   125 //
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CVtEngStateBase::SetVideoEnabled( TBool aEnabled )
       
   129     {
       
   130     CVtEngSettings& settings = CVtEngUtility::Settings();
       
   131         settings.SetVideoEnabled( aEnabled );
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CVtEngStateBase::HandleL
       
   136 // Handles an operation when behavior is state dependent.
       
   137 //
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 TBool CVtEngStateBase::HandleL( CVtEngOperation& aOp )
       
   141     {
       
   142     __VTPRINTENTER( "StateBase.Handle" )
       
   143     __VTPRINT2( DEBUG_GEN, "StateBase.Handle cmd=%d", aOp.Command() )
       
   144     TBool handled( EFalse );
       
   145     switch ( aOp.Command() )
       
   146         {
       
   147         case KVtEngSetAudioRouting:
       
   148             // Change audio routing
       
   149             AudioHandler().HandleL( aOp );
       
   150             break;
       
   151 
       
   152         case KVtEngSetAudioVolume:
       
   153         case KVtEngIncreaseAudioVolume:
       
   154         case KVtEngDecreaseAudioVolume:
       
   155             // Change audio volume
       
   156             MediaHandler().HandleL( aOp );
       
   157             break;
       
   158         case KVtEngTerminateSession:
       
   159         default:
       
   160             break;
       
   161         }
       
   162     __VTPRINTEXITR( "StateBase.Handle handled=%d", handled )
       
   163     return handled;
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CVtEngStateBase::SessionHandler
       
   168 // Returns session handler.
       
   169 //
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 CVtEngSessionHandler& CVtEngStateBase::SessionHandler()
       
   173     {
       
   174     MVtEngSessionInfo& msession = iStateManager.Handlers().Session();
       
   175     CVtEngSessionHandler& handler =
       
   176         static_cast<CVtEngSessionHandler&>( msession );
       
   177     return handler;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CVtEngStateBase::MediaHandler
       
   182 // Returns media handler.
       
   183 //
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 CVtEngMediaHandler& CVtEngStateBase::MediaHandler()
       
   187     {
       
   188     MVtEngMedia& msession = iStateManager.Handlers().Media();
       
   189     CVtEngMediaHandler& handler =
       
   190         static_cast<CVtEngMediaHandler&>( msession );
       
   191     return handler;
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CVtEngStateBase::AudioHandler
       
   196 // Returns audio handler.
       
   197 //
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 CVtEngAudioHandler& CVtEngStateBase::AudioHandler()
       
   201     {
       
   202     MVtEngAudio& maudio = iStateManager.Handlers().Audio();
       
   203     CVtEngAudioHandler& handler =
       
   204         static_cast<CVtEngAudioHandler&>( maudio );
       
   205     return handler;
       
   206     }
       
   207 // -----------------------------------------------------------------------------
       
   208 // CVtEngStateBase::SessionHandler
       
   209 // Returns session handler.
       
   210 //
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 CVtEngStateBase* CVtEngStateBase::NewStateL(
       
   214     const MVtEngSessionInfo::TSessionState aState )
       
   215     {
       
   216     CVtEngStateBase* newState = NULL;
       
   217     switch ( aState )
       
   218         {
       
   219         case MVtEngSessionInfo::EIdle:
       
   220             newState = new ( ELeave )
       
   221                 CVtEngStateIdle( iStateManager, iEventManager );
       
   222             break;
       
   223         case MVtEngSessionInfo::EInitializing:
       
   224             newState = new ( ELeave )
       
   225                 CVtEngStateInitializing( iStateManager, iEventManager );
       
   226             break;
       
   227         case MVtEngSessionInfo::ERemoteAlerting:
       
   228             newState = new ( ELeave )
       
   229                 CVtEngStateAlerting( iStateManager, iEventManager );
       
   230             break;
       
   231         case MVtEngSessionInfo::EReceiving:
       
   232             newState = new ( ELeave )
       
   233                 CVtEngStateRinging( iStateManager, iEventManager );
       
   234             break;
       
   235         case MVtEngSessionInfo::EConnected:
       
   236             newState = new ( ELeave )
       
   237                 CVtEngStateConnected( iStateManager, iEventManager );
       
   238             break;
       
   239         case MVtEngSessionInfo::ENegotiating:
       
   240             newState = new ( ELeave )
       
   241                 CVtEngStateNegotiating( iStateManager, iEventManager );
       
   242             break;
       
   243         case MVtEngSessionInfo::EOpen:
       
   244             newState = new ( ELeave )
       
   245                 CVtEngStateOpen( iStateManager, iEventManager );
       
   246             break;
       
   247         case MVtEngSessionInfo::EClearing:
       
   248             newState = new ( ELeave )
       
   249                 CVtEngStateClearing( iStateManager, iEventManager );
       
   250             break;
       
   251         default:
       
   252             Panic( EVtEngPanicInvalidSessionState );
       
   253             break;
       
   254         }
       
   255     return newState;
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CVtEngStateBase::NegotiatingOrConnectedStateL
       
   260 //
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 CVtEngStateBase* CVtEngStateBase::NegotiatingOrConnectedStateL(
       
   264     TBool aAcceptOnlyNegotiating )
       
   265     {
       
   266     __VTPRINTENTER( "StateBase.NegOrCon" )
       
   267     __VTPRINT2( DEBUG_GEN, "StateBase.NegOrCon %d", aAcceptOnlyNegotiating)
       
   268     const MVtCtlCallControl::TVtCtlState bearerstate =
       
   269         SessionHandler().RealState();
       
   270     CVtEngStateBase* newState = NULL;
       
   271     if ( bearerstate == MVtCtlCallControl::EConnected )
       
   272         {
       
   273         const MVtProtocolCommand::TVtProtocolState state( MediaHandler().ProtoState() );
       
   274         const CVtEngSettings& settings = CVtEngUtility::Settings();
       
   275         const TBool readyForConnect = settings.Config().iReadyForConnect;
       
   276 
       
   277 		__VTPRINT3( DEBUG_GEN, "StateBase.NegOrCon readyForConnect:%d, state:%d", readyForConnect, state)
       
   278 		__VTPRINT2( DEBUG_GEN, "StateBase.NegOrCon iIsDataportLoaned:%d", settings.Config().iIsDataportLoaned)
       
   279 
       
   280         if ( readyForConnect && state == MVtProtocolCommand::ESetup && settings.Config().iIsDataportLoaned )
       
   281             {
       
   282             __VTPRINT( DEBUG_GEN | DEBUG_DETAIL,
       
   283                 "StateBase.NegOrCon ready & ESetup")
       
   284             const TInt res( MediaHandler().Connect() );
       
   285             if ( res == KErrNotReady )
       
   286                 {
       
   287                 // Cannot be done yet, need to wait for an operation
       
   288                 // to complete.
       
   289                 __VTPRINT( DEBUG_GEN | DEBUG_DETAIL,
       
   290                     "StateBase.NegOrCon not ready" )
       
   291                 }
       
   292             else if ( res != KErrNone )
       
   293                 {
       
   294                 __VTPRINT( DEBUG_GEN | DEBUG_DETAIL,
       
   295                     "StateBase.NegOrCon fail")
       
   296                 iEventManager.NotifyEvent( KVtEngNegotiationProblem );
       
   297                 }
       
   298             else
       
   299                 {
       
   300                 // change to negotiating state
       
   301                 newState = NewStateL( MVtEngSessionInfo::ENegotiating );
       
   302                 __VTPRINT( DEBUG_GEN | DEBUG_DETAIL,
       
   303                     "StateBase.NegOrCon-->negotiating")
       
   304                 }
       
   305             }
       
   306         else if ( !aAcceptOnlyNegotiating )
       
   307             {
       
   308             newState = NewStateL( MVtEngSessionInfo::EConnected );
       
   309             __VTPRINT( DEBUG_GEN | DEBUG_DETAIL,
       
   310                 "StateBase.NegOrCon-->connected")
       
   311             }
       
   312         }
       
   313     __VTPRINTEXIT( "StateBase.NegOrCon" )
       
   314     return newState;
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CVtEngStateBase::TerminateSessionL
       
   319 //
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 void CVtEngStateBase::TerminateSessionL( CVtEngOperation& aOp )
       
   323     {
       
   324     // State transition to "clearing state" initiates disconnect on protocol.
       
   325     CVtEngStateBase* newState = NewStateL( MVtEngSessionInfo::EClearing );
       
   326     // just store aOp for callback to UI
       
   327     CVtEngMediaHandler& mHandler = MediaHandler();
       
   328     CleanupStack::PushL( newState );
       
   329     newState->OpenL( this );
       
   330     CleanupStack::Pop(); // newState
       
   331     mHandler.HandleL( aOp );
       
   332     }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CVtEngStateBase::ValidateCommand
       
   336 //
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 TBool CVtEngStateBase::ValidateCommand(
       
   340             const TVtEngCommandId aCommandId )
       
   341     {
       
   342     __VTPRINT( DEBUG_DETAIL | DEBUG_GEN, "StateBase.Validate")
       
   343     TBool okToPerform( EFalse );
       
   344     switch ( aCommandId )
       
   345         {
       
   346             // Flow through, ok to execute ==>  check the state possibilities
       
   347         case KVtEngInitializeEngine:
       
   348         case KVtEngInitializeEngineDiag:
       
   349         case KVtEngInitializeEngineTest:
       
   350         case KVtEngResetEngine:
       
   351         case KVtEngStartRenderRemote:
       
   352         case KVtEngStopRenderRemote:
       
   353         case KVtEngPrepareRemoteRender:
       
   354         case KVtEngPrepareRemoteRenderDSA:
       
   355         case KVtEngPrepareRemoteRenderDP:
       
   356         case KVtEngPrepareRemoteRenderNGA:        
       
   357         case KVtEngPrepareViewFinder:
       
   358         // Media
       
   359         case KVtEngSetUIForeground:
       
   360             okToPerform = ETrue;
       
   361             break;
       
   362         case KVtEngPrepareCamera:
       
   363         case KVtEngSetSource:
       
   364         case KVtEngSetZoomStep:
       
   365         case KVtEngStartViewFinder:
       
   366         case KVtEngFreeze:
       
   367         case KVtEngUnfreeze:
       
   368         case KVtEngSetVideoQuality:
       
   369         case KVtEngHandleLayoutChange:
       
   370 
       
   371         // Media object sharing
       
   372         case KVtEngInitializeShareImage:
       
   373         case KVtEngStartShareImage:
       
   374         case KVtEngStopShareImage:
       
   375 
       
   376         case KVtEngMuteOutgoingAudio:
       
   377         case KVtEngUnmuteOutgoingAudio:
       
   378         case KVtEngSetAudioRouting:
       
   379         case KVtEngSetAudioVolume:
       
   380         case KVtEngIncreaseAudioVolume:
       
   381         case KVtEngDecreaseAudioVolume:
       
   382         case KVtEngRequestLastRemoteFrame:
       
   383 
       
   384             okToPerform =
       
   385                 ( MediaHandler().ValidateCommand( aCommandId ) == KErrNone );
       
   386             break;
       
   387         default:
       
   388             case KVtEngTerminateSession: // valid only in open state
       
   389             break;
       
   390         }
       
   391     return okToPerform;
       
   392     }
       
   393 
       
   394 //  End of File
       
   395