khronosfws/openmax_al/src/mmf_adaptation/positionupdatetimer.cpp
changeset 25 6f7ceef7b1d1
parent 19 4a629bc82c5e
equal deleted inserted replaced
21:2ed61feeead6 25:6f7ceef7b1d1
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5  * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description: Handles new position timer implementation 
    14  * Description: Handles new position timer implementation 
    15 *
    15  *
    16 */
    16  */
    17 
    17 
    18 #include "positionupdatetimer.h"
    18 #include "positionupdatetimer.h"
    19 #include <mdaaudiosampleplayer.h>
    19 #include <mdaaudiosampleplayer.h>
    20 #include <videoplayer2.h>
    20 #include <videoplayer2.h>
    21 #include <e32math.h>
    21 #include <e32math.h>
    22 
    22 
    23 extern "C" {
    23 extern "C"
       
    24     {
    24 #include "xarecorditfadaptationmmf.h"
    25 #include "xarecorditfadaptationmmf.h"
    25 }
    26     }
    26 
    27 
    27 #define RET_ERR_IF_ERR(s) if (s!=KErrNone) return s;
    28 #define RET_ERR_IF_ERR(s) if (s!=KErrNone) return s;
    28 #define RET_IF_ERR(s) if (s!=KErrNone) return;
    29 #define RET_IF_ERR(s) if (s!=KErrNone) return;
    29 
    30 
    30 CPositionUpdateTimer::CPositionUpdateTimer(
    31 CPositionUpdateTimer::CPositionUpdateTimer(
    31         CMdaAudioPlayerUtility* aAudioPlayer,
    32         CMdaAudioPlayerUtility* aAudioPlayer,
    32         CVideoPlayerUtility2* aVideoPlayer)
    33         CVideoPlayerUtility2* aVideoPlayer) :
    33 :CActive(CActive::EPriorityStandard),
    34     CActive(CActive::EPriorityStandard), iAudioPlayer(aAudioPlayer),
    34  iAudioPlayer(aAudioPlayer),
    35             iVideoPlayer(aVideoPlayer)
    35  iVideoPlayer(aVideoPlayer)
       
    36     {
    36     {
    37     CActiveScheduler::Add(this);
    37     CActiveScheduler::Add(this);
    38     }
    38     }
    39 
    39 
    40 CPositionUpdateTimer::~CPositionUpdateTimer()
    40 CPositionUpdateTimer::~CPositionUpdateTimer()
    45 
    45 
    46 CPositionUpdateTimer* CPositionUpdateTimer::NewL(
    46 CPositionUpdateTimer* CPositionUpdateTimer::NewL(
    47         CMdaAudioPlayerUtility* aAudioPlayer,
    47         CMdaAudioPlayerUtility* aAudioPlayer,
    48         CVideoPlayerUtility2* aVideoPlayer)
    48         CVideoPlayerUtility2* aVideoPlayer)
    49     {
    49     {
    50     CPositionUpdateTimer* self = new (ELeave)CPositionUpdateTimer(aAudioPlayer, aVideoPlayer);
    50     CPositionUpdateTimer* self = new (ELeave) CPositionUpdateTimer(
       
    51             aAudioPlayer, aVideoPlayer);
    51     CleanupStack::PushL(self);
    52     CleanupStack::PushL(self);
    52     self->CostructL();
    53     self->CostructL();
    53     CleanupStack::Pop(self);
    54     CleanupStack::Pop(self);
    54     return self;
    55     return self;
    55     }
    56     }
    56 
    57 
    57 void CPositionUpdateTimer::CostructL()
    58 void CPositionUpdateTimer::CostructL()
    58     {
    59     {
    59     User::LeaveIfError(iTimer.CreateLocal());    
    60     User::LeaveIfError(iTimer.CreateLocal());
    60     }
    61     }
    61 
    62 
    62 void CPositionUpdateTimer::SetContext(TAny* aCtx)
    63 void CPositionUpdateTimer::SetContext(TAny* aCtx)
    63     {
    64     {
    64     iCtx = aCtx;    
    65     iCtx = aCtx;
    65     }
    66     }
    66 
    67 
    67 void CPositionUpdateTimer::SetPositionUpdatePeriod(XAmillisecond aPos)
    68 void CPositionUpdateTimer::SetPositionUpdatePeriod(XAmillisecond aPos)
    68     {
    69     {
    69     iPositionUpdatePeriod = aPos;
    70     iPositionUpdatePeriod = aPos;
    70     iDelay = TTimeIntervalMicroSeconds32(aPos*1000);
    71     iDelay = TTimeIntervalMicroSeconds32(aPos * 1000);
    71     }
    72     }
    72 
    73 
    73 void CPositionUpdateTimer::SetCallbackEventMask(XAuint32 aMask)
    74 void CPositionUpdateTimer::SetCallbackEventMask(XAuint32 aMask)
    74     {
    75     {
    75     iCallbackEventMask = aMask;
    76     iCallbackEventMask = aMask;
    80     iCallback = aCallback;
    81     iCallback = aCallback;
    81     }
    82     }
    82 
    83 
    83 void CPositionUpdateTimer::UseAudioPlayer()
    84 void CPositionUpdateTimer::UseAudioPlayer()
    84     {
    85     {
    85     iPlayerToUse = static_cast<CBase*>(iAudioPlayer);
    86     iPlayerToUse = static_cast<CBase*> (iAudioPlayer);
    86     }
    87     }
    87 void CPositionUpdateTimer::UseVideoPlayer()
    88 void CPositionUpdateTimer::UseVideoPlayer()
    88     {
    89     {
    89     iPlayerToUse = static_cast<CBase*>(iVideoPlayer);
    90     iPlayerToUse = static_cast<CBase*> (iVideoPlayer);
    90     }
    91     }
    91 
    92 
    92 void CPositionUpdateTimer::ResetPlayer()
    93 void CPositionUpdateTimer::ResetPlayer()
    93     {
    94     {
    94     iPlayerToUse = NULL;
    95     iPlayerToUse = NULL;
    99     TInt retVal(KErrNone);
   100     TInt retVal(KErrNone);
   100     if (IsActive())
   101     if (IsActive())
   101         {
   102         {
   102         Cancel();
   103         Cancel();
   103         }
   104         }
   104     if ((iCallbackEventMask & XA_PLAYEVENT_HEADATNEWPOS) &&
   105     if ((iCallbackEventMask & XA_PLAYEVENT_HEADATNEWPOS) && iCallback
   105         iCallback &&
   106             && iPlayerToUse)
   106         iPlayerToUse)
       
   107         {
   107         {
   108         TTimeIntervalMicroSeconds curPlayPos;
   108         TTimeIntervalMicroSeconds curPlayPos;
   109         /* Convert milli to micro */
   109         /* Convert milli to micro */
   110         TTimeIntervalMicroSeconds posUpdatePeriod(iPositionUpdatePeriod * 1000);
   110         TTimeIntervalMicroSeconds posUpdatePeriod(iPositionUpdatePeriod
       
   111                 * 1000);
   111         TTimeIntervalMicroSeconds32 delay;
   112         TTimeIntervalMicroSeconds32 delay;
   112         /* Convert milli to micro */
   113         /* Convert milli to micro */
   113         TReal res;
   114         TReal res;
   114         TReal p;
   115         TReal p;
   115         TReal q(posUpdatePeriod.Int64());
   116         TReal q(posUpdatePeriod.Int64());
   132         // Keep this block for debugging purposes
   133         // Keep this block for debugging purposes
   133         RDebug::Print(_L("CPositionUpdateTimer::RunL:SyncPlayHead[%u]Delay Reset[%u]microSec"),
   134         RDebug::Print(_L("CPositionUpdateTimer::RunL:SyncPlayHead[%u]Delay Reset[%u]microSec"),
   134                 iSyncToPlayHeadStartPos.Int64(),
   135                 iSyncToPlayHeadStartPos.Int64(),
   135                 delay.Int());
   136                 delay.Int());
   136 #endif /* POSITIONUPDATETIMERLOG */
   137 #endif /* POSITIONUPDATETIMERLOG */
   137         if ( delay >= TTimeIntervalMicroSeconds32(0))
   138         if (delay >= TTimeIntervalMicroSeconds32(0))
   138             {
   139             {
   139             iStatus = KRequestPending;
   140             iStatus = KRequestPending;
   140             iTimer.After(iStatus, delay);
   141             iTimer.After(iStatus, delay);
   141             SetActive();
   142             SetActive();
   142             }
   143             }
   151 
   152 
   152 void CPositionUpdateTimer::RunL()
   153 void CPositionUpdateTimer::RunL()
   153     {
   154     {
   154     TInt retVal(KErrNone);
   155     TInt retVal(KErrNone);
   155     /* Make sure some of the attributes are not unset */
   156     /* Make sure some of the attributes are not unset */
   156     if ((iStatus == KErrNone) &&
   157     if ((iStatus == KErrNone) && iCtx && (iCallbackEventMask
   157             iCtx &&
   158             & XA_PLAYEVENT_HEADATNEWPOS) && iCallback && iPlayerToUse)
   158             (iCallbackEventMask & XA_PLAYEVENT_HEADATNEWPOS) &&
       
   159             iCallback &&
       
   160             iPlayerToUse)
       
   161         {
   159         {
   162         TTimeIntervalMicroSeconds curPlayPos;
   160         TTimeIntervalMicroSeconds curPlayPos;
   163         if (iSyncToPlayHead)
   161         if (iSyncToPlayHead)
   164             {
   162             {
   165             retVal = GetCurrentPlayPosition(curPlayPos);
   163             retVal = GetCurrentPlayPosition(curPlayPos);
   167 
   165 
   168             /* If the play head hasn't moved, re-start all over again */
   166             /* If the play head hasn't moved, re-start all over again */
   169             if (curPlayPos == iSyncToPlayHeadStartPos)
   167             if (curPlayPos == iSyncToPlayHeadStartPos)
   170                 {
   168                 {
   171 #ifdef POSITIONUPDATETIMERLOG
   169 #ifdef POSITIONUPDATETIMERLOG
   172             RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]SyncPlayHead[%u]microSec. Restart"),
   170                 RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]SyncPlayHead[%u]microSec. Restart"),
   173                     iSyncToPlayHeadStartPos.Int64(),
   171                         iSyncToPlayHeadStartPos.Int64(),
   174                     curPlayPos.Int64());
   172                         curPlayPos.Int64());
   175 #endif /* POSITIONUPDATETIMERLOG */
   173 #endif /* POSITIONUPDATETIMERLOG */
   176                 Start();
   174                 Start();
   177                 return;
   175                 return;
   178                 }
   176                 }
   179             /* Play head has moved. calculate remaining time and set the timer */
   177             /* Play head has moved. calculate remaining time and set the timer */
   180             /* Convert milli to micro */
   178             /* Convert milli to micro */
   181             TTimeIntervalMicroSeconds posUpdatePeriod(iPositionUpdatePeriod * 1000);
   179             TTimeIntervalMicroSeconds posUpdatePeriod(iPositionUpdatePeriod
       
   180                     * 1000);
   182             TReal res;
   181             TReal res;
   183             TReal p;
   182             TReal p;
   184             TReal q(posUpdatePeriod.Int64());
   183             TReal q(posUpdatePeriod.Int64());
   185 
   184 
   186             p = curPlayPos.Int64();
   185             p = curPlayPos.Int64();
   187             iSyncToPlayHead = EFalse;
   186             iSyncToPlayHead = EFalse;
   188 
   187 
   189             retVal = Math::Mod(res, p, q);
   188             retVal = Math::Mod(res, p, q);
   190             RET_IF_ERR(retVal);
   189             RET_IF_ERR(retVal);
   191 
   190 
   192             TTimeIntervalMicroSeconds32 delay = (posUpdatePeriod.Int64() - res);
   191             TTimeIntervalMicroSeconds32 delay = (posUpdatePeriod.Int64()
       
   192                     - res);
   193 #ifdef POSITIONUPDATETIMERLOG
   193 #ifdef POSITIONUPDATETIMERLOG
   194             RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]SyncPlayHead[%u]Delay Reset[%u]microSec"),
   194             RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]SyncPlayHead[%u]Delay Reset[%u]microSec"),
   195                     iSyncToPlayHeadStartPos.Int64(),
   195                     iSyncToPlayHeadStartPos.Int64(),
   196                     curPlayPos.Int64(),
   196                     curPlayPos.Int64(),
   197                     delay.Int());
   197                     delay.Int());
   198 #endif /* POSITIONUPDATETIMERLOG */
   198 #endif /* POSITIONUPDATETIMERLOG */
   199             if ( delay >= TTimeIntervalMicroSeconds32(0))
   199             if (delay >= TTimeIntervalMicroSeconds32(0))
   200                 {
   200                 {
   201                 iStatus = KRequestPending;
   201                 iStatus = KRequestPending;
   202                 iTimer.After(iStatus, delay);
   202                 iTimer.After(iStatus, delay);
   203                 SetActive();
   203                 SetActive();
   204                 }
   204                 }
   207 
   207 
   208 #ifdef POSITIONUPDATETIMERLOG
   208 #ifdef POSITIONUPDATETIMERLOG
   209         retVal = GetCurrentPlayPosition(curPlayPos);
   209         retVal = GetCurrentPlayPosition(curPlayPos);
   210         RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]microSec. Posting XA_PLAYEVENT_HEADATNEWPOS."), curPlayPos.Int64());
   210         RDebug::Print(_L("CPositionUpdateTimer::RunL:CurPlayPos[%u]microSec. Posting XA_PLAYEVENT_HEADATNEWPOS."), curPlayPos.Int64());
   211 #endif /* POSITIONUPDATETIMERLOG */
   211 #endif /* POSITIONUPDATETIMERLOG */
   212         XAAdaptEvent xaevent = {XA_PLAYITFEVENTS, XA_PLAYEVENT_HEADATNEWPOS, 0, 0 };
   212         XAAdaptEvent xaevent =
   213         XAAdaptationBase_SendAdaptEvents((XAAdaptationBaseCtx*)iCtx, &xaevent );
   213             {
       
   214             XA_PLAYITFEVENTS, XA_PLAYEVENT_HEADATNEWPOS, 0, 0
       
   215             };
       
   216         XAAdaptationBase_SendAdaptEvents((XAAdaptationBaseCtx*) iCtx,
       
   217                 &xaevent);
   214         iStatus = KRequestPending;
   218         iStatus = KRequestPending;
   215         iTimer.After(iStatus, iDelay);
   219         iTimer.After(iStatus, iDelay);
   216         SetActive();
   220         SetActive();
   217         }
   221         }
   218     }
   222     }
   220 void CPositionUpdateTimer::DoCancel()
   224 void CPositionUpdateTimer::DoCancel()
   221     {
   225     {
   222     iTimer.Cancel();
   226     iTimer.Cancel();
   223     }
   227     }
   224 
   228 
   225 TInt CPositionUpdateTimer::RunError(TInt /*aError*/)
   229 TInt CPositionUpdateTimer::RunError(TInt aError)
   226     {
   230     {
   227     return KErrNone;
   231     return aError;
   228     }
   232     }
   229 
   233 
   230 TInt CPositionUpdateTimer::GetCurrentPlayPosition(TTimeIntervalMicroSeconds& aPos)
   234 TInt CPositionUpdateTimer::GetCurrentPlayPosition(
       
   235         TTimeIntervalMicroSeconds& aPos)
   231     {
   236     {
   232     TTimeIntervalMicroSeconds pos;
   237     TTimeIntervalMicroSeconds pos;
   233     TInt err(KErrNone);
   238     TInt err(KErrNone);
   234     if (iPlayerToUse == iAudioPlayer)
   239     if (iPlayerToUse && (iPlayerToUse == iAudioPlayer))
   235         {
   240         {
   236         iAudioPlayer->GetPosition(aPos);
   241         iAudioPlayer->GetPosition(aPos);
   237         }
   242         }
   238     else if (iPlayerToUse == iVideoPlayer)
   243     else if (iPlayerToUse && (iPlayerToUse == iVideoPlayer))
   239         {
   244         {
   240         TRAP(err, aPos = iVideoPlayer->PositionL());
   245         TRAP(err, aPos = iVideoPlayer->PositionL());
   241         }
   246         }
   242     return err;
   247     return err;
   243     }
   248     }