javauis/mmapi_akn/baseline/src/cmmastoptimecontrol.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002 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:  This class is used for stoptime controlling
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <jdebug.h>
       
    21 #include <e32std.h>
       
    22 
       
    23 #include "cmmastoptimecontrol.h"
       
    24 #include "cmmaplayer.h"
       
    25 
       
    26 
       
    27 _LIT(KControlName, "StopTimeControl");
       
    28 
       
    29 _LIT(KMMAStopTimeControlError, "StopTimeControl Symbian OS error: %d");
       
    30 const TInt KMMAStopTimeControlErrorLength = 50;
       
    31 
       
    32 CMMAStopTimeControl::CStopTimer* CMMAStopTimeControl::CStopTimer::NewL(
       
    33     CMMAStopTimeControl* aControl)
       
    34 {
       
    35     CStopTimer* self = new(ELeave) CStopTimer(aControl);
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop(self);
       
    39     return self;
       
    40 }
       
    41 
       
    42 
       
    43 CMMAStopTimeControl::CStopTimer::CStopTimer(CMMAStopTimeControl* aControl)
       
    44         : CTimer(CActive::EPriorityStandard)
       
    45 {
       
    46     iControl = aControl;
       
    47 }
       
    48 
       
    49 
       
    50 void CMMAStopTimeControl::CStopTimer::ConstructL()
       
    51 {
       
    52     CTimer::ConstructL();
       
    53     CActiveScheduler::Add(this);
       
    54 }
       
    55 
       
    56 
       
    57 void CMMAStopTimeControl::CStopTimer::RunL()
       
    58 {
       
    59     DEBUG("MMA:CMMAStopTimeControl:CStopTimer:RunL timer triggered");
       
    60     iControl->StopAtTimeL();
       
    61 }
       
    62 
       
    63 const TDesC& CMMAStopTimeControl::ClassName() const
       
    64 {
       
    65     return KControlName;
       
    66 }
       
    67 
       
    68 
       
    69 CMMAStopTimeControl* CMMAStopTimeControl::NewL(CMMAPlayer* aPlayer)
       
    70 {
       
    71     CMMAStopTimeControl* control =
       
    72         new(ELeave) CMMAStopTimeControl(aPlayer);
       
    73     CleanupStack::PushL(control);
       
    74     control->ConstructL();
       
    75     CleanupStack::Pop(); // control
       
    76     return control;
       
    77 }
       
    78 
       
    79 
       
    80 CMMAStopTimeControl::~CMMAStopTimeControl()
       
    81 {
       
    82     delete iTimer;
       
    83 }
       
    84 
       
    85 
       
    86 CMMAStopTimeControl::CMMAStopTimeControl(CMMAPlayer* aPlayer)
       
    87         : iPlayer(aPlayer), iNoTimer((MAKE_TINT64(KMaxTInt, KMaxTUint)))
       
    88 {
       
    89     iStopTime = iNoTimer;
       
    90 }
       
    91 
       
    92 
       
    93 void CMMAStopTimeControl::ConstructL()
       
    94 {
       
    95     iTimer = CStopTimer::NewL(this);
       
    96     iPlayer->AddStateListenerL(this);
       
    97 }
       
    98 
       
    99 
       
   100 void CMMAStopTimeControl::StaticGetStopTime(CMMAStopTimeControl* aControl,
       
   101         TInt64* aTime)
       
   102 {
       
   103     aControl->GetStopTime(*aTime);
       
   104 }
       
   105 
       
   106 
       
   107 void CMMAStopTimeControl::StaticSetStopTimeL(CMMAStopTimeControl* aControl,
       
   108         TInt64* aTime)
       
   109 {
       
   110     aControl->SetStopTimeL(*aTime);
       
   111 }
       
   112 
       
   113 
       
   114 void CMMAStopTimeControl::StopAtTimeL()
       
   115 {
       
   116     DEBUG("MMA:CMMAStopTimeControl::StopAtTime");
       
   117 
       
   118     // Stop the player only when it's playing
       
   119     if (iPlayer->State() == CMMAPlayer::EStarted)
       
   120     {
       
   121         TInt64 time;
       
   122         iPlayer->GetMediaTime(&time);
       
   123         if (time >= 0 && time < iStopTime)
       
   124         {
       
   125             DEBUG_INT("MMA:CMMAStopTimeControl::StopAtTime - Called %dms too early",
       
   126                       I64INT((time - iStopTime)/1000));
       
   127             StartTimer(time);
       
   128             return;
       
   129         }
       
   130 
       
   131         TInt64 stopTime;
       
   132 
       
   133         // MediaTime is known
       
   134         if (time >= 0)
       
   135         {
       
   136             DEBUG_INT("MMA:CMMAStopTimeControl::StopAtTime - called %dms late", I64INT((time - iStopTime)/1000));
       
   137             stopTime = time;
       
   138         }
       
   139         else
       
   140         {
       
   141             // Use the requested time
       
   142             stopTime = iStopTime;
       
   143         }
       
   144 
       
   145         iPlayer->StopL(EFalse);
       
   146 
       
   147         // Inform the player that it's "stopped at time"
       
   148         iPlayer->PostLongEvent(CMMAPlayerEvent::EStoppedAtTime, stopTime);
       
   149 
       
   150         iStopTime = iNoTimer; // Timer is reseted
       
   151     }
       
   152 }
       
   153 
       
   154 
       
   155 /**
       
   156  * Get stop time, in microseconds
       
   157  */
       
   158 void CMMAStopTimeControl::GetStopTime(TInt64& aTime)
       
   159 {
       
   160     aTime = iStopTime;
       
   161 }
       
   162 
       
   163 
       
   164 /**
       
   165  * Set stop time, in microseconds
       
   166  * @param aTime is iNoTimer if the timer should be reseted
       
   167  */
       
   168 void CMMAStopTimeControl::SetStopTimeL(const TInt64& aTime)
       
   169 {
       
   170     iStopTime = aTime;
       
   171 
       
   172     if (aTime != iNoTimer)
       
   173     {
       
   174         DEBUG_INT("MMA:CMMAStopTimeControl:SetStopTime(%dms)",
       
   175                   I64INT(aTime / 1000));
       
   176 
       
   177         if (iPlayer->State() == CMMAPlayer::EStarted)
       
   178         {
       
   179             TInt64 currentTime(0);
       
   180             iPlayer->GetMediaTime(&currentTime);
       
   181 
       
   182             StartTimer(currentTime);
       
   183         }
       
   184     }
       
   185     else
       
   186     {
       
   187         DEBUG("MMA:CMMAStopTimeControl:SetStopTime(RESET)");
       
   188 
       
   189         iTimer->Cancel();
       
   190     }
       
   191 }
       
   192 
       
   193 
       
   194 /*
       
   195  * Start timer
       
   196  * @param aCurrentTime current position of the player
       
   197  */
       
   198 void CMMAStopTimeControl::StartTimer(const TInt64& aCurrentTime)
       
   199 {
       
   200     // StopTime is defined
       
   201     TInt64 time = iStopTime - aCurrentTime;
       
   202     DEBUG_INT("MMA:CMMAStopTimeControl:StartTimer timer started; time=%dms",
       
   203               I64INT(time / 1000));
       
   204     iTimer->Cancel();
       
   205 
       
   206     if (time >= 0)
       
   207     {
       
   208         // Value is too large to represent with TInt
       
   209         // use the biggest possible value instead
       
   210         if (I64HIGH(time) != 0 || I64INT(time) < 0)
       
   211         {
       
   212             time = KMaxTInt;
       
   213         }
       
   214 
       
   215         iTimer->After(TTimeIntervalMicroSeconds32(I64INT(time)));
       
   216     }
       
   217     else
       
   218     {
       
   219         // Stop the player immediatelly
       
   220         TRAPD(err,  StopAtTimeL());
       
   221         if (err != KErrNone)
       
   222         {
       
   223             TBuf< KMMAStopTimeControlErrorLength > errorMsg;
       
   224             errorMsg.Format(KMMAStopTimeControlError, err);
       
   225             iPlayer->PostStringEvent(CMMAPlayerEvent::EError,
       
   226                                      errorMsg);
       
   227         }
       
   228     }
       
   229 }
       
   230 
       
   231 
       
   232 void CMMAStopTimeControl::StateChanged(TInt aState)
       
   233 {
       
   234     switch ((CMMAPlayer::TPlayerState) aState)
       
   235     {
       
   236     case CMMAPlayer::EStarted:
       
   237     {
       
   238         if (iStopTime != iNoTimer)
       
   239         {
       
   240             // (Re)start the timer
       
   241             TInt64 time;
       
   242             iPlayer->GetMediaTime(&time);
       
   243 
       
   244             StartTimer(time);
       
   245         }
       
   246         break;
       
   247     }
       
   248     case CMMAPlayer::ERealized:
       
   249     case CMMAPlayer::EPrefetched:
       
   250     case CMMAPlayer::EClosed:
       
   251     {
       
   252         // Player is not running anymore
       
   253         iTimer->Cancel();
       
   254         break;
       
   255     }
       
   256     default:
       
   257     {
       
   258         // other states are ignored
       
   259     }
       
   260     }
       
   261 }
       
   262 
       
   263 //  END OF FILE