upnpavcontroller/upnprenderingstatemachine/src/upnprenderingplaytimecalculator.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
       
     1 /*
       
     2 * Copyright (c) 2007,2009 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:  Implementation of playback time for rendering state machine
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include "upnprenderingplaytimecalculator.h"
       
    20 
       
    21 _LIT( KComponentLogfile, "upnprenderingstatemachine.txt");
       
    22 #include "upnplog.h"
       
    23 
       
    24 // CONSTANTS
       
    25 const TInt KMicrosecondsInMillisecond = 1000;
       
    26 const TInt KMillisecondsInSecond = 1000;
       
    27 const TInt KDurationErrorMargin = 3000; // 3 seconds
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // --------------------------------------------------------------------------
       
    32 // CUpnpRenderingPlaytimeCalculator::CUpnpRenderingPlaytimeCalculator
       
    33 // --------------------------------------------------------------------------
       
    34 //
       
    35 CUpnpRenderingPlaytimeCalculator::CUpnpRenderingPlaytimeCalculator()
       
    36     {
       
    37     iPlaying = EFalse;
       
    38     iPaused = EFalse;
       
    39     iStartMark = 0;
       
    40     iStopMark = 0;
       
    41     iPauseMark = 0;
       
    42     iPauseTime = 0;
       
    43     iOffset = 0;
       
    44     iDuration = KErrNotFound;
       
    45     }
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // CUpnpRenderingPlaytimeCalculator::Start
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 void CUpnpRenderingPlaytimeCalculator::Start()
       
    52     {
       
    53     __LOG("PlaytimeCalculator: Start()" );
       
    54     iStartMark.UniversalTime();
       
    55     iPauseTime = 0;
       
    56     iPlaying = ETrue;
       
    57     iPaused = EFalse;
       
    58     iOffset = 0;
       
    59     }
       
    60 
       
    61 // --------------------------------------------------------------------------
       
    62 // CUpnpRenderingPlaytimeCalculator::Pause
       
    63 // --------------------------------------------------------------------------
       
    64 //
       
    65 void CUpnpRenderingPlaytimeCalculator::Pause()
       
    66     {
       
    67     __LOG("PlaytimeCalculator: Pause()" );
       
    68     if( !iPaused )
       
    69         {
       
    70         iPauseMark.UniversalTime();
       
    71         iPaused = ETrue;
       
    72         }
       
    73     }
       
    74 
       
    75 // --------------------------------------------------------------------------
       
    76 // CUpnpRenderingPlaytimeCalculator::Resume
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 void CUpnpRenderingPlaytimeCalculator::Resume()
       
    80     {
       
    81     __LOG("PlaytimeCalculator: Resume()" );
       
    82     TTime resumeMark;
       
    83     resumeMark.UniversalTime();
       
    84     iPauseTime += PausetimeAt( resumeMark );
       
    85     iPaused = EFalse;
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CUpnpRenderingPlaytimeCalculator::Stop
       
    90 // --------------------------------------------------------------------------
       
    91 //
       
    92 void CUpnpRenderingPlaytimeCalculator::Stop()
       
    93     {
       
    94     __LOG("PlaytimeCalculator: Stop()" );
       
    95     iStopMark.UniversalTime();
       
    96     iPlaying = EFalse;
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CUpnpRenderingPlaytimeCalculator::SetDuration
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 void CUpnpRenderingPlaytimeCalculator::SetDuration( TInt aDuration )
       
   104     {
       
   105     iDuration = aDuration;
       
   106     __LOG1("PlaytimeCalculator:SetDuration iDuration %d", iDuration );
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CUpnpRenderingPlaytimeCalculator::Duration
       
   111 // --------------------------------------------------------------------------
       
   112 //
       
   113 TInt CUpnpRenderingPlaytimeCalculator::Duration() const
       
   114     {
       
   115     __LOG1("PlaytimeCalculator: iDuration %d", iDuration );
       
   116     return iDuration;
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CUpnpRenderingPlaytimeCalculator::AcknowledgePositionInfo
       
   121 // --------------------------------------------------------------------------
       
   122 //
       
   123 void CUpnpRenderingPlaytimeCalculator::AcknowledgePositionInfo(
       
   124     TInt aDuration, TInt aPosition )
       
   125     {
       
   126     __LOG3("PlaytimeCalculator::AcknowledgePositionInfo \
       
   127 aDuration %d aPosition %d, iPlaying %d ", 
       
   128     aDuration, aPosition, iPlaying );
       
   129     
       
   130     // handle DURATION
       
   131     if ( aDuration > 0 )
       
   132         {
       
   133         if ( iDuration <= 0)
       
   134             {
       
   135             // we did not know duration before, set it
       
   136             iDuration = aDuration;
       
   137             }
       
   138         else if ( aDuration > iDuration )
       
   139             {
       
   140             // renderer thinks track is longer. trust longer value!
       
   141             // too long is more safe than too short.
       
   142             // TOO LONG -> playlist play may discontinue. (annoying)
       
   143             // TOO SHORT -> pressing stop may cause track skip and
       
   144             //              user can't stop the music (critical)
       
   145             iDuration = aDuration;
       
   146             }
       
   147         }
       
   148     // handle POSITION
       
   149     if ( iPlaying )
       
   150         {
       
   151         // calculate playtime now, compare to given position
       
   152         // and manipulate offset accordingly
       
   153         iOffset = OffsetFromNow( aPosition );
       
   154         __LOG1("PlaytimeCalculator: New Offset %d", iOffset );
       
   155         }
       
   156     else
       
   157         {
       
   158         // start timer and set position according to ongoing playback
       
   159         Start();
       
   160         TTimeIntervalSeconds origPos = aPosition / KMillisecondsInSecond;
       
   161         iStartMark -= origPos;
       
   162         iOffset = OffsetFromNow( aPosition );
       
   163         }
       
   164     
       
   165     __LOG2("PlaytimeCalculator::AcknowledgePositionInfo \
       
   166 iDuration %d Current Position %d ", iDuration, Position() );
       
   167     }
       
   168 
       
   169 // --------------------------------------------------------------------------
       
   170 // CUpnpRenderingPlaytimeCalculator::Position
       
   171 // --------------------------------------------------------------------------
       
   172 //
       
   173 TInt CUpnpRenderingPlaytimeCalculator::Position() const
       
   174     {
       
   175     TTime tempTime;
       
   176     if ( iPlaying )
       
   177         {
       
   178         __LOG("PlaytimeCalculator: Position() - using current time" );
       
   179         // measure time until NOW.
       
   180         tempTime.UniversalTime();
       
   181         }
       
   182     else
       
   183         {
       
   184         // measure time until last track STOPPED.
       
   185         tempTime = iStopMark;
       
   186         }        
       
   187     return PositionAt( tempTime );
       
   188     }
       
   189 
       
   190 // --------------------------------------------------------------------------
       
   191 // CUpnpRenderingPlaytimeCalculator::IsTrackComplete
       
   192 // --------------------------------------------------------------------------
       
   193 //
       
   194 TBool CUpnpRenderingPlaytimeCalculator::IsTrackComplete() const
       
   195     {
       
   196     TBool isCompleted = ETrue;
       
   197     TInt playtime = Position();
       
   198     if ( playtime >= 0 &&
       
   199         playtime < iDuration - KDurationErrorMargin )
       
   200         {
       
   201         // [0 - duration-margin]
       
   202         isCompleted= EFalse;
       
   203         }
       
   204     else if ( playtime >= iDuration - KDurationErrorMargin &&
       
   205         playtime <= iDuration + KDurationErrorMargin )
       
   206         {
       
   207         // [duration-margin - duration+margin]
       
   208         isCompleted= ETrue;
       
   209         }
       
   210     else
       
   211         {
       
   212         // position either negative or greater than duration ??
       
   213         __LOG2("PlaytimeCalculator: WARNING: playtime=%d duration=%d",
       
   214             playtime,
       
   215             iDuration );
       
   216         isCompleted= ETrue;
       
   217         }
       
   218     return isCompleted;
       
   219     }
       
   220 
       
   221 // --------------------------------------------------------------------------
       
   222 // CUpnpRenderingPlaytimeCalculator::IsPlaying
       
   223 // --------------------------------------------------------------------------
       
   224 //
       
   225 TBool CUpnpRenderingPlaytimeCalculator::IsPlaying() const
       
   226     {
       
   227     return iPlaying;
       
   228     }
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CUpnpRenderingPlaytimeCalculator::RestartAt
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 void CUpnpRenderingPlaytimeCalculator::RestartAt( TInt aNewPosition )
       
   235     {
       
   236     __LOG("PlaytimeCalculator: RestartAt()" );
       
   237     Stop();
       
   238     Start();
       
   239     // Consider new position to be equivalent to current position
       
   240     TTimeIntervalSeconds newPos = 
       
   241             aNewPosition / KMillisecondsInSecond;
       
   242     iStartMark -= newPos;
       
   243     }
       
   244 
       
   245 // --------------------------------------------------------------------------
       
   246 // CUpnpRenderingPlaytimeCalculator::PositionAt
       
   247 // --------------------------------------------------------------------------
       
   248 //
       
   249 TInt CUpnpRenderingPlaytimeCalculator::PositionAt( TTime& aMark ) const
       
   250     {
       
   251     TTimeIntervalMicroSeconds played =
       
   252         aMark.MicroSecondsFrom( iStartMark );
       
   253     TInt playtime = ( played.Int64() / KMicrosecondsInMillisecond );
       
   254     __LOG1("PlaytimeCalculator: PositionAt played %d", playtime );
       
   255     
       
   256     playtime -= iPauseTime;
       
   257     playtime -= PausetimeAt( aMark );
       
   258     playtime += iOffset;
       
   259     __LOG3("PlaytimeCalculator: playtime=%d pausetime=%d offset=%d",
       
   260         playtime,
       
   261         (iPauseTime + PausetimeAt( aMark )),
       
   262         iOffset );
       
   263     return playtime;
       
   264     }
       
   265 
       
   266 // --------------------------------------------------------------------------
       
   267 // CUpnpRenderingPlaytimeCalculator::PausetimeAt
       
   268 // --------------------------------------------------------------------------
       
   269 //
       
   270 TInt CUpnpRenderingPlaytimeCalculator::PausetimeAt( TTime& aMark ) const
       
   271     {
       
   272     TInt pausetime = 0;
       
   273     if ( iPaused )
       
   274         {
       
   275         TTimeIntervalMicroSeconds latestPause =
       
   276             aMark.MicroSecondsFrom( iPauseMark );
       
   277         pausetime = ( latestPause.Int64() / KMicrosecondsInMillisecond );
       
   278         __LOG1("PlaytimeCalculator: PausetimeAt pause time %d", pausetime );
       
   279         }
       
   280     return pausetime;
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CUpnpRenderingPlaytimeCalculator::OffsetFromNow
       
   285 // --------------------------------------------------------------------------
       
   286 //
       
   287 TInt CUpnpRenderingPlaytimeCalculator::OffsetFromNow( TInt aPosition ) const
       
   288     {
       
   289     TTime timeNow;
       
   290     timeNow.UniversalTime();
       
   291     TInt tempPlaytime = PositionAt( timeNow );
       
   292     TInt o = aPosition - tempPlaytime;
       
   293     __LOG2("PlaytimeCalculator: renderer position (%d), \
       
   294 tempPlaytime (%d)", aPosition, tempPlaytime );
       
   295     __LOG3("PlaytimeCalculator: old off(%d) current off(%d), \
       
   296 new offset(%d)", iOffset, o, iOffset+o);
       
   297     return iOffset+o;
       
   298     }
       
   299 
       
   300 // end of file