mpx/playbackframework/playbackengine/src/mpxautoresumehandler.cpp
changeset 0 a2952bb97e68
child 11 780c925249c1
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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: Implemention of the auto resume handler.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <ctsydomainpskeys.h>
       
    21 #include <mpxpskeywatcher.h>
       
    22 #include <mpxlog.h>
       
    23 #include "mpxplaybackengine.h"
       
    24 #include "mpxautoresumehandler.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KMPXErrDiedTimeout = 2000000;
       
    28 
       
    29 // Time to wait before resume after call has ended.
       
    30 const TInt KMPXResumeWaitTime = 3000000; // 3.0s
       
    31 
       
    32 
       
    33 
       
    34 // ================= MEMBER FUNCTIONS =======================
       
    35 
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // C++ constructor
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CMPXAutoResumeHandler::CMPXAutoResumeHandler(
       
    42     CMPXPlaybackEngine& aEngine,
       
    43     TBool aMixerSupport) :
       
    44     iEngine(aEngine),
       
    45     iMixerSupport(aMixerSupport),
       
    46     iAutoResume(ETrue)
       
    47     {
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // Symbian OS default constructor
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CMPXAutoResumeHandler::ConstructL()
       
    55     {
       
    56     // Listen to call state changes
       
    57     iStateObserver = CMPXPSKeyWatcher::NewL(KPSUidCtsyCallInformation,
       
    58                                             KCTsyCallState,this);
       
    59 
       
    60     // Listen to call type changes
       
    61     iTypeObserver = CMPXPSKeyWatcher::NewL(KPSUidCtsyCallInformation,
       
    62                                            KCTsyCallType,this);
       
    63     iResumeTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // Constructs a new entry with given values.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CMPXAutoResumeHandler* CMPXAutoResumeHandler::NewL(
       
    71                                          CMPXPlaybackEngine& aEngine,
       
    72                                          TBool aMixerSupport)
       
    73     {
       
    74     CMPXAutoResumeHandler* self =
       
    75         new (ELeave) CMPXAutoResumeHandler(aEngine, aMixerSupport);
       
    76 
       
    77     CleanupStack::PushL(self);
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop( self );
       
    80 
       
    81     return self;
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CMPXAutoResumeHandler::CMPXAutoResumeHandler()
       
    86 // Destructor
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 CMPXAutoResumeHandler::~CMPXAutoResumeHandler()
       
    90     {
       
    91     delete iStateObserver;
       
    92     delete iTypeObserver;
       
    93     if ( iResumeTimer )
       
    94         {
       
    95         CancelResumeTimer();
       
    96         delete iResumeTimer;
       
    97         }
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // Open file completed
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CMPXAutoResumeHandler::HandleOpenFileComplete()
       
   105     {
       
   106     MPX_FUNC("CMPXAutoResumeHandler::HandleOpenFileComplete()");
       
   107     iPausedForCall = EFalse;
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CMPXAutoResumeHandler::MPlayerStateChanged
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CMPXAutoResumeHandler::HandlePlaybackStateChange(TMPXPlaybackState aState)
       
   115     {
       
   116     MPX_DEBUG2("CMPXAutoResumeHandler::HandlePlaybackStateChange(%d) entering", aState);
       
   117 
       
   118     // Any state change means that possible waiting resume is not to be done
       
   119     // anymore.
       
   120     CancelResumeTimer();
       
   121 
       
   122     if (aState != EPbStatePaused)
       
   123         {
       
   124         iPausedForCall = EFalse;
       
   125         }
       
   126     MPX_DEBUG1("CMPXAutoResumeHandler::HandlePlaybackStateChange() exiting");
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CMPXAutoResumeHandler::MPlayerPlayComplete
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CMPXAutoResumeHandler::HandlePlaybackComplete(TInt aError)
       
   134     {
       
   135     MPX_DEBUG2("CMPXAutoResumeHandler::HandlePlaybackComplete(%d) entering", aError);
       
   136     iPausedForCall = EFalse;
       
   137     if ( KErrDied == aError ||
       
   138          KErrAccessDenied == aError )
       
   139         {
       
   140         iKErrDiedTime.HomeTime();
       
   141 
       
   142         TInt callType = EPSCTsyCallTypeNone;
       
   143         TInt callState = EPSCTsyCallStateNone;
       
   144 
       
   145         if (!iTypeObserver->GetValue(callType) &&
       
   146             !iStateObserver->GetValue(callState))
       
   147             {
       
   148             if ((callState == EPSCTsyCallStateRinging && iMixerSupport) ||
       
   149                  ShouldPause())
       
   150                 {
       
   151                 // Getting a play complete with KErrDied here
       
   152                 // means Audio Policy terminated our playback,
       
   153                 // due to phone call being connected. Enable
       
   154                 // autoresume.
       
   155                 iPausedForCall = ETrue;
       
   156                 }
       
   157             }
       
   158         }
       
   159     MPX_DEBUG1("CMPXAutoResumeHandler::HandlePlaybackComplete() exiting");
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CMPXAutoResumeHandler::CancelResumeTimer
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CMPXAutoResumeHandler::CancelResumeTimer()
       
   167     {
       
   168     if ( iResumeTimer )
       
   169         {
       
   170         iResumeTimer->Cancel();
       
   171         }
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CMPXAutoResumeHandler::HandlePSEvent
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void CMPXAutoResumeHandler::HandlePSEvent(TUid /*aUid*/, TInt /*aKey*/)
       
   179     {
       
   180     MPX_FUNC("CMPXAutoResumeHandler::HandlePSEvent()");
       
   181     TRAP_IGNORE(DoHandleStateChangeL());
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CMPXAutoResumeHandler::DoHandleStateChangeL
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CMPXAutoResumeHandler::DoHandleStateChangeL()
       
   189     {
       
   190     MPX_FUNC("CMPXAutoResumeHandler::DoHandleStateChangeL()");
       
   191     MPX_DEBUG2("CMPXAutoResumeHandler::DoHandleStateChangeL(): iPausedForCall = %d", iPausedForCall);
       
   192     MPX_DEBUG2("CMPXAutoResumeHandler::DoHandleStateChangeL(): engineState = %d", iEngine.State());
       
   193     // if autoresume is disabled, do nothing
       
   194     if ( !iAutoResume )
       
   195         {
       
   196         return;
       
   197         }
       
   198     
       
   199     TBool shouldPause = ShouldPause();
       
   200     if (shouldPause &&
       
   201         !iPausedForCall &&
       
   202         iEngine.State() == EPbStatePlaying)
       
   203         {
       
   204         iEngine.HandleCommandL(EPbCmdPause);
       
   205         iPausedForCall = ETrue;
       
   206         }
       
   207     else if ( shouldPause &&
       
   208             !iPausedForCall &&
       
   209             ( iEngine.State() == EPbStateSeekingForward ||
       
   210             iEngine.State() == EPbStateSeekingBackward ) )
       
   211         {
       
   212         iEngine.HandleCommandL( EPbCmdStopSeeking );
       
   213         if ( iEngine.State() == EPbStatePlaying )
       
   214             {
       
   215             iEngine.HandleCommandL( EPbCmdPause );
       
   216             iPausedForCall = ETrue;
       
   217             }
       
   218         }
       
   219     else if(!shouldPause &&
       
   220             iPausedForCall &&
       
   221             iEngine.State() == EPbStatePaused)
       
   222         {
       
   223         MPX_DEBUG1("CMPXAutoResumeHandler::DoHandleStateChangeL(): starting resume timer");
       
   224         if ( iResumeTimer->IsActive() )
       
   225             iResumeTimer->Cancel();
       
   226         iResumeTimer->Start(
       
   227             KMPXResumeWaitTime,
       
   228             KMPXResumeWaitTime,
       
   229             TCallBack(ResumeTimerCallback, this) );
       
   230         iPausedForCall = EFalse;
       
   231         }
       
   232     else if ( shouldPause &&
       
   233              iEngine.State() == EPbStatePaused &&
       
   234              !iPausedForCall &&
       
   235              iKErrDiedTime.Int64())
       
   236         {
       
   237         // Check if we recently got a playcomplete with KErrDied,
       
   238         // it was most likely caused by an active call
       
   239         TTime now;
       
   240         now.HomeTime();
       
   241         TInt64 deltaTime = now.MicroSecondsFrom(iKErrDiedTime).Int64();
       
   242         if ( deltaTime > 0 &&
       
   243              deltaTime < KMPXErrDiedTimeout)
       
   244             {
       
   245             iResumeTimer->Cancel();
       
   246             iPausedForCall = ETrue;
       
   247             }
       
   248         }
       
   249     MPX_DEBUG2("CMPXAutoResumeHandler::DoHandleStateChangeL(): iPausedForCall = %d", iPausedForCall);
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CMPXAutoResumeHandler::ShouldPause
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 TBool CMPXAutoResumeHandler::ShouldPause()
       
   257     {
       
   258     MPX_DEBUG1("CMPXAutoResumeHandler::ShouldPause() entering");
       
   259     TBool ret = EFalse;
       
   260 
       
   261     if ( !IsPlaybackRemote() )
       
   262         {
       
   263         TInt callType;
       
   264         TInt callState;
       
   265         iTypeObserver->GetValue(callType);
       
   266         iStateObserver->GetValue(callState);
       
   267         MPX_DEBUG3("CMPXAutoResumeHandler::ShouldPause(): type = %d, state = %d", callType, callState);
       
   268 
       
   269         if (callType == EPSCTsyCallTypeCSVoice ||
       
   270             callType == EPSCTsyCallTypeH324Multimedia ||
       
   271             callType == EPSCTsyCallTypeVoIP ||
       
   272             callType == EPSCTsyCallTypeUninitialized)
       
   273             {
       
   274             switch (callState)
       
   275                 {
       
   276                 case EPSCTsyCallStateAnswering:
       
   277                 case EPSCTsyCallStateAlerting:
       
   278                 case EPSCTsyCallStateConnected:
       
   279                 case EPSCTsyCallStateDialling:
       
   280                 case EPSCTsyCallStateHold:
       
   281                 case EPSCTsyCallStateDisconnecting:
       
   282                     {
       
   283                     ret = ETrue;
       
   284                     break;
       
   285                     }
       
   286                 case EPSCTsyCallStateRinging:
       
   287                     {
       
   288                     if (iPausedForCall)
       
   289                        {
       
   290                        ret = ETrue;
       
   291                        }
       
   292                     else
       
   293                         {
       
   294                         // Pause playback if we cannot mix music playback
       
   295                         // with ringing tone.
       
   296                         ret = !iMixerSupport;
       
   297                         }
       
   298                     break;
       
   299                     }
       
   300                 default:
       
   301                     {
       
   302                     // Default is no pause
       
   303                     break;
       
   304                     }
       
   305                 }
       
   306             }
       
   307         }
       
   308     MPX_DEBUG2("CMPXAutoResumeHandler::ShouldPause() exiting: %d", ret);
       
   309     return ret;
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CMPXAutoResumeHandler::HandleResumeTimerCallback
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CMPXAutoResumeHandler::HandleResumeTimerCallback()
       
   317     {
       
   318     MPX_FUNC("CMPXAutoResumeHandler::HandleResumeTimerCallback() entering");
       
   319 
       
   320     CancelResumeTimer();
       
   321     TRAP_IGNORE( iEngine.HandleCommandL( EPbCmdPlayWithFadeIn ));
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CMPXAutoResumeHandler::ResumeTimerCallbackL
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 TInt CMPXAutoResumeHandler::ResumeTimerCallback(TAny* aPtr)
       
   329     {
       
   330     MPX_FUNC("CMPXAutoResumeHandler::ResumeTimerCallback()");
       
   331 
       
   332     CMPXAutoResumeHandler* ptr =
       
   333         static_cast<CMPXAutoResumeHandler*>(aPtr);
       
   334     ptr->HandleResumeTimerCallback();
       
   335 
       
   336     return KErrNone;
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CMPXAutoResumeHandler::IsPlaybackRemote
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 TBool CMPXAutoResumeHandler::IsPlaybackRemote()
       
   344     {
       
   345     MPX_DEBUG1("CMPXAutoResumeHandler::IsPlaybackRemote() entering");
       
   346     TBool isRemote = EFalse;
       
   347 
       
   348     if ( iEngine.State() == EPbStatePlaying )
       
   349         {
       
   350         TMPXPlaybackPlayerType type( EPbLocal );
       
   351         TUid uid;
       
   352         TInt index;
       
   353         TPtrC subPlayerName( KNullDesC );
       
   354         iEngine.PluginHandler()->GetSelection( type, uid,
       
   355                                                index, subPlayerName );
       
   356 
       
   357         if ( type != EPbLocal )
       
   358             {
       
   359             isRemote = ETrue;
       
   360             }
       
   361         }
       
   362 
       
   363     MPX_DEBUG2("CMPXAutoResumeHandler::IsPlaybackRemote() exiting: %d", isRemote);
       
   364     return isRemote;
       
   365     }
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // Set autoresume value
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 void CMPXAutoResumeHandler::SetAutoResume(TBool aAutoResume)
       
   372     {
       
   373     MPX_DEBUG2("CMPXAutoResumeHandler::SetAutoResume(): AutoResume = %d", aAutoResume);
       
   374     iAutoResume = aAutoResume;
       
   375     }
       
   376 
       
   377 //  End of File