videoplayback/videohelix/src/mpxvideoseeker.cpp
changeset 0 96612d01cf9f
child 9 5294c000a26d
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2008 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: Seek or trickplay
       
    15  *
       
    16 */
       
    17 
       
    18 // Version : %version: 7 %
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <mpxplaybackpluginobserver.h>
       
    23 
       
    24 #include "mpxvideoseeker.h"
       
    25 #include "mpxvideoplaybackcontroller.h"
       
    26 #include "mpxvideo_debug.h"
       
    27 
       
    28 // CONSTANTS
       
    29 
       
    30 #if TRICK_PLAY
       
    31 const TInt KDefaultPlaySpeed(100);        // 1x
       
    32 const TInt KDefaultTrickPlaySpeed(400);   // 4x
       
    33 #else
       
    34 const TInt KTrickPlayTimeout(5000000);    // 5 sec
       
    35 const TInt KSpeedInterval(125000);        // 125 msec
       
    36 const TInt KDurationA(90000000);          // 90 sec
       
    37 const TInt KDurationB(180000000);         // 180 sec
       
    38 const TInt64 KSpeedAIncrements(2000000);  // 2 sec
       
    39 const TInt64 KSpeedBIncrements(4000000);  // 4 sec
       
    40 #endif
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===================================================
       
    43 
       
    44 // -------------------------------------------------------------------------------------------------
       
    45 // CMPXVideoSeeker::CMPXVideoSeeker
       
    46 // C++ default constructor can NOT contain any code, that might leave.
       
    47 // -------------------------------------------------------------------------------------------------
       
    48 //
       
    49 CMPXVideoSeeker::CMPXVideoSeeker( CMPXVideoPlaybackController* aVideoPlaybackCtlr )
       
    50     : iVideoPlaybackCtlr( aVideoPlaybackCtlr )
       
    51 {
       
    52     MPX_DEBUG(_L("CMPXVideoSeeker::CMPXVideoSeeker()"));
       
    53 }
       
    54 
       
    55 // -------------------------------------------------------------------------------------------------
       
    56 // CMPXVideoSeeker::ConstructL
       
    57 // Symbian 2nd phase constructor can leave.
       
    58 // -------------------------------------------------------------------------------------------------
       
    59 //
       
    60 void CMPXVideoSeeker::ConstructL()
       
    61 {
       
    62     MPX_DEBUG(_L("CMPXVideoSeeker::ConstructL()"));
       
    63 
       
    64     iTrickPlayTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    65     iSeekTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    66 }
       
    67 
       
    68 // -------------------------------------------------------------------------------------------------
       
    69 // CMPXVideoSeeker::NewL
       
    70 // Two-phased constructor.
       
    71 // -------------------------------------------------------------------------------------------------
       
    72 //
       
    73 CMPXVideoSeeker* CMPXVideoSeeker::NewL( CMPXVideoPlaybackController* aVideoPlaybackCtlr )
       
    74 {
       
    75     MPX_DEBUG(_L("CMPXVideoSeeker::NewL()"));
       
    76 
       
    77     CMPXVideoSeeker* self = new( ELeave ) CMPXVideoSeeker( aVideoPlaybackCtlr );
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop();
       
    81     return self;
       
    82 }
       
    83 
       
    84 // -------------------------------------------------------------------------------------------------
       
    85 // CMPXVideoSeeker::~CMPXVideoSeeker
       
    86 // Destructor
       
    87 // -------------------------------------------------------------------------------------------------
       
    88 //
       
    89 CMPXVideoSeeker::~CMPXVideoSeeker()
       
    90 {
       
    91     MPX_DEBUG(_L("CMPXVideoSeeker::~CMPXVideoSeeker()"));
       
    92 
       
    93     if ( iTrickPlayTimer )
       
    94     {
       
    95         iTrickPlayTimer->Cancel();
       
    96         delete iTrickPlayTimer;
       
    97     }
       
    98 
       
    99     if ( iSeekTimer )
       
   100     {
       
   101         iSeekTimer->Cancel();
       
   102         delete iSeekTimer;
       
   103     }
       
   104 }
       
   105 
       
   106 // -------------------------------------------------------------------------------------------------
       
   107 // CMPXVideoSeeker::StartSeeking
       
   108 // -------------------------------------------------------------------------------------------------
       
   109 //
       
   110 void CMPXVideoSeeker::StartSeekingL( TBool aForward,
       
   111                                      TMPXVideoPlaybackState aState,
       
   112                                      TInt64 aPosition,
       
   113                                      TInt64 aDuration )
       
   114 {
       
   115     MPX_DEBUG(_L("CMPXVideoSeeker::StartSeekingL() forward = (%d)"), aForward );
       
   116 
       
   117     TInt err = KErrNone;
       
   118     iPreviousState = aState;
       
   119     iForward = aForward;
       
   120     iPosition = aPosition;
       
   121     iDuration = aDuration;
       
   122 
       
   123 #if TRICK_PLAY
       
   124     if ( iPreviousState == EMPXVideoPaused )
       
   125     {
       
   126         iVideoPlaybackCtlr->iPlayer->Play();
       
   127     }
       
   128 
       
   129     TInt trickplaySpeed = KDefaultTrickPlaySpeed;
       
   130 
       
   131     if ( !iForward )
       
   132     {
       
   133         trickplaySpeed *= -1;
       
   134     }
       
   135 
       
   136     //
       
   137     // Start trick play with 4x speed as default
       
   138     //
       
   139     MPX_TRAP( iVideoPlaybackCtlr->iPlayer->SetPlayVelocityL( trickplaySpeed ) );
       
   140 
       
   141 #else
       
   142     if ( iPreviousState == EMPXVideoPlaying )
       
   143     {
       
   144         iVideoPlaybackCtlr->iPlayer->PauseL();
       
   145     }
       
   146 
       
   147     iIncrements = KSpeedAIncrements / 2;
       
   148     TInt interval = KSpeedInterval * 2;
       
   149 
       
   150     if ( !iForward )
       
   151     {
       
   152         iIncrements *= -1;
       
   153     }
       
   154 
       
   155     iSeekTimer->Start( 0,
       
   156                        interval,
       
   157                        TCallBack( CMPXVideoSeeker::UpdatePosition, this ) );
       
   158 #endif
       
   159 
       
   160     if ( err == KErrNone )
       
   161     {
       
   162         iTrickPlayTimer->Start( KTrickPlayTimeout,
       
   163                                 0,
       
   164                                 TCallBack( CMPXVideoSeeker::StopTrickPlay, this ) );
       
   165     }
       
   166 }
       
   167 
       
   168 // -------------------------------------------------------------------------------------------------
       
   169 // CMPXVideoSeeker::StopSeekingL
       
   170 // -------------------------------------------------------------------------------------------------
       
   171 //
       
   172 void CMPXVideoSeeker::StopSeekingL()
       
   173 {
       
   174     MPX_DEBUG(_L("CMPXVideoSeeker::StopSeekingL()"));
       
   175 
       
   176     TBool seekTimerActive = EFalse;
       
   177 
       
   178     if ( iTrickPlayTimer->IsActive() )
       
   179     {
       
   180         iTrickPlayTimer->Cancel();
       
   181     }
       
   182 
       
   183     if ( iSeekTimer->IsActive() )
       
   184     {
       
   185         seekTimerActive = ETrue;
       
   186         iSeekTimer->Cancel();
       
   187     }
       
   188 
       
   189     //
       
   190     // If it reaches the end of a clip, issue stop
       
   191     //
       
   192     if ( iDuration <= iPosition )
       
   193     {
       
   194         iVideoPlaybackCtlr->iState->HandleEndOfClip();
       
   195     }
       
   196     else
       
   197     {
       
   198         if ( seekTimerActive )
       
   199         {
       
   200             iVideoPlaybackCtlr->iPlayer->SetPositionL( iPosition );
       
   201 
       
   202             if ( iPreviousState == EMPXVideoPlaying )
       
   203             {
       
   204                 iVideoPlaybackCtlr->DoHandleCommandL( EPbCmdPlay );
       
   205             }
       
   206             else if (iPreviousState == EMPXVideoPaused )
       
   207             {
       
   208                 iVideoPlaybackCtlr->DoHandleCommandL( EPbCmdPause );
       
   209             }
       
   210             else
       
   211             {
       
   212                 iVideoPlaybackCtlr->ChangeState( iPreviousState );
       
   213             }
       
   214         }
       
   215 
       
   216 #if TRICK_PLAY
       
   217         else
       
   218         {
       
   219             //
       
   220             // Start trick play with default speed
       
   221             //
       
   222             MPX_TRAP( iVideoPlaybackCtlr->iPlayer->SetPlayVelocityL( KDefaultPlaySpeed ) );
       
   223 
       
   224             if ( iPreviousState == EMPXVideoPaused )
       
   225             {
       
   226                 iVideoPlaybackCtlr->DoHandleCommandL( EPbCmdPause );
       
   227             }
       
   228             else if ( iPreviousState == EMPXVideoPlaying )
       
   229             {
       
   230                 iVideoPlaybackCtlr->DoHandleCommandL( EPbCmdPlay );
       
   231             }
       
   232         }
       
   233 #endif
       
   234     }
       
   235 
       
   236 }
       
   237 
       
   238 // -------------------------------------------------------------------------------------------------
       
   239 // CMPXVideoSeeker::StopTrickPlay
       
   240 // -------------------------------------------------------------------------------------------------
       
   241 //
       
   242 TInt CMPXVideoSeeker::StopTrickPlay( TAny* aPtr )
       
   243 {
       
   244     MPX_DEBUG(_L("CMPXVideoSeeker::StopTrickPlay()"));
       
   245 
       
   246 	static_cast<CMPXVideoSeeker*>(aPtr)->DoStopTrickPlay();
       
   247 
       
   248 	return KErrNone;
       
   249 }
       
   250 
       
   251 // -------------------------------------------------------------------------------------------------
       
   252 // CMPXVideoSeeker::DoStopTrickPlay
       
   253 // -------------------------------------------------------------------------------------------------
       
   254 //
       
   255 void CMPXVideoSeeker::DoStopTrickPlay()
       
   256 {
       
   257     MPX_DEBUG(_L("CMPXVideoSeeker::DoStopTrickPlay()"));
       
   258 
       
   259     TInt err = KErrNone;
       
   260 
       
   261     //
       
   262     // Stop trick play
       
   263     //
       
   264     if ( iTrickPlayTimer->IsActive() )
       
   265     {
       
   266         iTrickPlayTimer->Cancel();
       
   267     }
       
   268 
       
   269 #if TRICK_PLAY
       
   270     MPX_TRAP( err,
       
   271               iVideoPlaybackCtlr->iPlayer->SetPlayVelocityL( KDefaultPlaySpeed );
       
   272               iVideoPlaybackCtlr->iPlayer->PauseL() );
       
   273 #endif
       
   274 
       
   275     if ( err == KErrNone )
       
   276     {
       
   277         ConvertToSeeking();
       
   278     }
       
   279 }
       
   280 
       
   281 // -------------------------------------------------------------------------------------------------
       
   282 // CMPXVideoSeeker::ConvertToSeeking
       
   283 // -------------------------------------------------------------------------------------------------
       
   284 //
       
   285 void CMPXVideoSeeker::ConvertToSeeking()
       
   286 {
       
   287     MPX_DEBUG(_L("CMPXVideoSeeker::ConvertToSeeking()"));
       
   288 
       
   289     //
       
   290     // Start seeking (increments in every 125 msec)
       
   291     // - ~ 90sec      : Increment is 2 sec
       
   292     // - 91sec~180sec : Increment is 4 sec
       
   293     // - 181sec~      : Increment depends on the duration
       
   294     //                  ( 0.5 % * duration ) + 3 sec
       
   295     //
       
   296     if ( iDuration < KDurationA )
       
   297     {
       
   298         iIncrements = KSpeedAIncrements;
       
   299     }
       
   300     else
       
   301     {
       
   302         if ( iDuration < KDurationB )
       
   303         {
       
   304             iIncrements = KSpeedBIncrements;
       
   305         }
       
   306         else
       
   307         {
       
   308             iIncrements = (TInt)( iDuration / 200 ) + 3;
       
   309         }
       
   310     }
       
   311 
       
   312     if ( !iForward )
       
   313     {
       
   314         iIncrements *= -1;
       
   315     }
       
   316 
       
   317     if ( iSeekTimer->IsActive() )
       
   318     {
       
   319         iSeekTimer->Cancel();
       
   320     }
       
   321 
       
   322     iSeekTimer->Start( 0, KSpeedInterval, TCallBack( CMPXVideoSeeker::UpdatePosition, this ) );
       
   323 }
       
   324 
       
   325 // -------------------------------------------------------------------------------------------------
       
   326 // CMPXVideoSeeker::UpdatePosition
       
   327 // -------------------------------------------------------------------------------------------------
       
   328 //
       
   329 TInt CMPXVideoSeeker::UpdatePosition( TAny* aPtr )
       
   330 {
       
   331     MPX_DEBUG(_L("CMPXVideoSeeker::UpdatePosition()"));
       
   332 
       
   333     static_cast<CMPXVideoSeeker*>(aPtr)->DoUpdatePosition();
       
   334     return KErrNone;
       
   335 }
       
   336 
       
   337 // -------------------------------------------------------------------------------------------------
       
   338 // CMPXVideoSeeker::DoUpdatePosition
       
   339 // -------------------------------------------------------------------------------------------------
       
   340 //
       
   341 void CMPXVideoSeeker::DoUpdatePosition()
       
   342 {
       
   343     MPX_DEBUG(_L("CMPXVideoSeeker::DoUpdatePosition()"));
       
   344 
       
   345     iPosition += iIncrements;
       
   346 
       
   347     if ( iPosition > iDuration )
       
   348     {
       
   349         iPosition = iDuration;
       
   350     }
       
   351     else if ( iPosition < 0 )
       
   352     {
       
   353         iPosition = 0;
       
   354     }
       
   355 
       
   356     iVideoPlaybackCtlr->iPlaybackMode->UpdateSeekPosition( iPosition );
       
   357 }
       
   358 
       
   359 //  End of File