phoneuis/BubbleManager/Src/BMVideoPlayer.cpp
changeset 0 5f000ab63145
equal deleted inserted replaced
-1:000000000000 0:5f000ab63145
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Video player implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <coemain.h>       // CCoeEnv 
       
    21 #include <eikenv.h>        // CEikEnv 
       
    22 #include <videoplayer.h>   // CVideoPlayerUtility
       
    23 #include "BMVideoPlayer.h"
       
    24 #include "BMVideoPlayerObserver.h"
       
    25 #include "BMPanic.h"
       
    26 #include "BMTrace.h"
       
    27 
       
    28 // EXTERNAL DATA STRUCTURES
       
    29 
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 
       
    32 // CONSTANTS
       
    33 const TInt KBubbleVideoMinVolumeLevel = 0;
       
    34 const TInt KBubbleVideoMaxVolumeLevel = 10;
       
    35 const TInt KBubbleVideoVolumeRampInterval = 3000000;
       
    36 const TInt KBubbleVideoVolumeRampStep = 1;
       
    37 const TInt KBubbleVideoRepeatsTrailPause = 1000000;
       
    38 const TInt KBubbleVideoIACoef = 1000; // Precision for fixed point math
       
    39 const TInt KBubbleMaxVideoCrop = 0; // Allowed max cropping. 0, 1 .. 20 (%).
       
    40 // The margin between aWindowRect and aClipRect parameters passed to player,
       
    41 // which can be used to eliminate black fill (scaled image is matched to
       
    42 // target area by clipping it).
       
    43 const TInt KBubbleVideoClipMarginX = 2;
       
    44 const TInt KBubbleVideoClipMarginY = 2;
       
    45 
       
    46 
       
    47 // MACROS
       
    48 
       
    49 // LOCAL CONSTANTS AND MACROS
       
    50 
       
    51 // MODULE DATA STRUCTURES
       
    52 
       
    53 // LOCAL FUNCTION PROTOTYPES
       
    54 TInt DivideAndCeil(const TInt aVal, const TInt aDiv)
       
    55     {
       
    56     return (((aVal%aDiv)>0) ? (TInt)((aVal/aDiv)+1):(TInt)(aVal/aDiv));
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // C++ constructor
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CBubbleVideoPlayer::CBubbleVideoPlayer(
       
    64     MBubbleVideoPlayerObserver& aObserver )
       
    65     :
       
    66     iObserver( aObserver ),
       
    67     iVideoPlayer( NULL ),
       
    68     iPlayerState( EVideoClosed ),
       
    69     iVolume( 0 )
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CBubbleVideoPlayer::ConstructL
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CBubbleVideoPlayer::ConstructL( 
       
    78     const TDesC& aFileName, 
       
    79     TInt aPriority, 
       
    80     TUint aPreference,
       
    81     RWindow& aVideoTarget  )
       
    82     {
       
    83     BUBBLE_PRINT("VideoPlayer::Const>");
       
    84     // Screen and clip rectangles to window dimensions
       
    85     TPoint wndPosition( aVideoTarget.AbsPosition() );
       
    86     TSize wndSize( aVideoTarget.Size() );
       
    87     TRect wndRect( wndPosition, wndSize );
       
    88     
       
    89     // Create video player instance
       
    90     CCoeEnv* coeEnv = CCoeEnv::Static();
       
    91     iVideoPlayer = CVideoPlayerUtility::NewL(
       
    92         *this,
       
    93         aPriority,
       
    94         static_cast<TMdaPriorityPreference>( aPreference ),
       
    95         coeEnv->WsSession(),
       
    96         *coeEnv->ScreenDevice(),    
       
    97         aVideoTarget,
       
    98         wndRect, 
       
    99         wndRect );
       
   100 
       
   101     RFs fs = CEikonEnv::Static()->FsSession();
       
   102     User::LeaveIfError( iFileHandle.Open( fs,
       
   103                                           aFileName,
       
   104                                           EFileShareReadersOnly | 
       
   105                                           EFileStream | 
       
   106                                           EFileRead ) );        
       
   107     
       
   108     iVideoPlayer->OpenFileL( iFileHandle ); // async
       
   109     
       
   110     iPlayerState = EVideoOpening;
       
   111     BUBBLE_PRINT("VideoPlayer::Const<");
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CBubbleVideoPlayer::NewL
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CBubbleVideoPlayer* CBubbleVideoPlayer::NewL(
       
   119     const TDesC& aRingingTone, 
       
   120     TInt aPriority, 
       
   121     TUint aPreference,
       
   122     MBubbleVideoPlayerObserver& aObserver, 
       
   123     RWindow& aVideoTarget )
       
   124     {
       
   125     CBubbleVideoPlayer* self = new(ELeave) CBubbleVideoPlayer( aObserver );
       
   126         
       
   127     CleanupStack::PushL( self );
       
   128     self->ConstructL( aRingingTone, 
       
   129                       aPriority, aPreference, 
       
   130                       aVideoTarget );
       
   131     CleanupStack::Pop();
       
   132 
       
   133     return self;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CBubbleVideoPlayer::New
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 CBubbleVideoPlayer* CBubbleVideoPlayer::New(
       
   141     const TDesC& aRingingTone,  
       
   142     TInt aPriority, 
       
   143     TUint aPreference,
       
   144     MBubbleVideoPlayerObserver& aObserver, 
       
   145     RWindow& aVideoTarget )
       
   146     {
       
   147     CBubbleVideoPlayer* self = NULL;
       
   148 
       
   149     TRAPD( ignore, self = CBubbleVideoPlayer::NewL(
       
   150                               aRingingTone, 
       
   151                               aPriority, 
       
   152                               aPreference,
       
   153                               aObserver,   
       
   154                               aVideoTarget ) );
       
   155     ignore = ignore;                              
       
   156     
       
   157     return self; // this is NULL if NewL leaves
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // Destructor
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 CBubbleVideoPlayer::~CBubbleVideoPlayer()
       
   165     {
       
   166     if ( iVolumeRampTimer )
       
   167         {
       
   168         iVolumeRampTimer->Cancel();
       
   169         delete iVolumeRampTimer;
       
   170         }
       
   171         
       
   172     if ( iRepeatsTrailPauseTimer )
       
   173         {
       
   174         iRepeatsTrailPauseTimer->Cancel();
       
   175         delete iRepeatsTrailPauseTimer;    
       
   176         }
       
   177 
       
   178     if ( iVideoPlayer )
       
   179         {
       
   180         iVideoPlayer->Close();
       
   181         delete iVideoPlayer;
       
   182         }
       
   183         
       
   184     iFileHandle.Close();        
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CBubbleVideoPlayer::Play
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CBubbleVideoPlayer::Play( TRingingType aRingType, 
       
   192                               TInt aVolume )
       
   193     {
       
   194     BUBBLE_PRINT("VideoPlayer::Play>");
       
   195     __ASSERT_DEBUG( iVideoPlayer, Panic( EBMPanicVideoPlayer ) );
       
   196     
       
   197     iRingingType = aRingType;
       
   198     iVolume = aVolume;
       
   199 
       
   200     switch( iPlayerState )
       
   201         {
       
   202         case EVideoOpening:
       
   203         case EVideoPreparing:
       
   204         case EVideoOpen:
       
   205             // see MvpuoPrepareComplete()
       
   206             iToBePlayed = ETrue;   
       
   207             break;
       
   208         
       
   209         case EVideoReady:
       
   210         case EVideoPaused:
       
   211             iVideoPlayer->Play();
       
   212             iPlayerState = EVideoPlaying;
       
   213             break;
       
   214         
       
   215         case EVideoPlaying:
       
   216         case EVideoClosed:
       
   217         case EVideoError:
       
   218         default:
       
   219             // NOP
       
   220             break;
       
   221         }
       
   222 
       
   223     BUBBLE_PRINT("VideoPlayer::Play<");
       
   224     return;
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CBubbleVideoPlayer::StopPlaying
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void CBubbleVideoPlayer::StopPlaying()
       
   232     {
       
   233     BUBBLE_PRINT("VideoPlayer::Stop>");
       
   234     iToBePlayed = EFalse;
       
   235 
       
   236     if ( iVolumeRampTimer )
       
   237         {
       
   238         iVolumeRampTimer->Cancel();
       
   239         }
       
   240         
       
   241     if ( iRepeatsTrailPauseTimer )        
       
   242         {
       
   243         iRepeatsTrailPauseTimer->Cancel();    
       
   244         }
       
   245     
       
   246     if ( iPlayerState == EVideoPlaying ||
       
   247          iPlayerState == EVideoPaused )
       
   248         {
       
   249         iVideoPlayer->Stop();
       
   250         iPlayerState = EVideoReady;
       
   251         }
       
   252     BUBBLE_PRINT("VideoPlayer::Stop<");        
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CBubbleVideoPlayer::PausePlaying
       
   257 // -----------------------------------------------------------------------------
       
   258 //    
       
   259 TInt CBubbleVideoPlayer::PausePlaying()
       
   260     {
       
   261     BUBBLE_PRINT("VideoPlayer::Pause>");
       
   262     TInt err = KErrNone;
       
   263     
       
   264     if ( iPlayerState == EVideoPlaying )
       
   265         {
       
   266         TRAP( err, iVideoPlayer->PauseL() );
       
   267         if ( err == KErrNone )    
       
   268             {
       
   269             iPlayerState = EVideoPaused;    
       
   270             }
       
   271         }
       
   272     else
       
   273         {
       
   274         err = KErrNotReady;    
       
   275         }        
       
   276 
       
   277     BUBBLE_PRINTF("VideoPlayer::Pause<:", err);        
       
   278     return err;        
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CBubbleVideoPlayer::ResumePlaying
       
   283 // -----------------------------------------------------------------------------
       
   284 //    
       
   285 void CBubbleVideoPlayer::ResumePlaying()
       
   286     {
       
   287     BUBBLE_PRINT("VideoPlayer::Resume>");
       
   288     if ( iPlayerState == EVideoPaused )
       
   289         {
       
   290         iVideoPlayer->Play(); 
       
   291         iPlayerState = EVideoPlaying;       
       
   292         }
       
   293     BUBBLE_PRINT("VideoPlayer::Resume<");        
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CBubbleVideoPlayer::MuteAudio
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 void CBubbleVideoPlayer::MuteAudio()
       
   301     {
       
   302     BUBBLE_PRINT("VideoPlayer::Mute");
       
   303     if ( iVolumeRampTimer )
       
   304         {
       
   305         iVolumeRampTimer->Cancel();
       
   306         }
       
   307 
       
   308     iVolume = 0;
       
   309     SetVolume( 0 );
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CBubbleVideoPlayer::SetVolume
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CBubbleVideoPlayer::SetVolume( TInt aVolume )
       
   317     {
       
   318     BUBBLE_PRINT("VideoPlayer::SetVol>");
       
   319     TInt maxVolume( iVideoPlayer->MaxVolume() );
       
   320     maxVolume = ( maxVolume > 0 ) ? maxVolume : KBubbleVideoMaxVolumeLevel; 
       
   321 
       
   322     TInt scaledVolume = ( aVolume * maxVolume ) / KBubbleVideoMaxVolumeLevel;
       
   323     
       
   324     TRAP_IGNORE( iVideoPlayer->SetVolumeL( scaledVolume ) );
       
   325     BUBBLE_PRINT("VideoPlayer::SetVol<");
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CBubbleVideoPlayer::SetRingingType
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 void CBubbleVideoPlayer::SetRingingType( TRingingType aRingingType )
       
   333     {
       
   334     BUBBLE_PRINT("VideoPlayer::SetRingType");
       
   335     if ( aRingingType == ETypeAscending )
       
   336         {
       
   337         if ( !iVolumeRampTimer )
       
   338             {
       
   339             iVolumeRampTimer = CPeriodic::New( CActive::EPriorityStandard );
       
   340             }
       
   341 
       
   342         if ( iVolumeRampTimer && !iVolumeRampTimer->IsActive() )
       
   343             {
       
   344             TCallBack cb( VolumeRampTimerCallback, this );
       
   345 
       
   346             SetVolume( KBubbleVideoMinVolumeLevel );
       
   347             iRampedVolume = KBubbleVideoMinVolumeLevel;
       
   348             iVolumeRampTimer->Start( KBubbleVideoVolumeRampInterval, 
       
   349                                      KBubbleVideoVolumeRampInterval, 
       
   350                                      cb );
       
   351             }
       
   352         }
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CBubbleVideoPlayer::VolumeRampTimerCallback
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 TInt CBubbleVideoPlayer::VolumeRampTimerCallback( TAny* aObj )
       
   360     {
       
   361     return static_cast<CBubbleVideoPlayer*>( aObj )->DoVolumeRamp();
       
   362     }
       
   363     
       
   364 
       
   365 // -----------------------------------------------------------------------------
       
   366 // CBubbleVideoPlayer::DoVolumeRamp
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 TInt CBubbleVideoPlayer::DoVolumeRamp()
       
   370     {
       
   371     BUBBLE_PRINT("VideoPlayer::DoVolRamp");
       
   372     if ( iRampedVolume < iVolume )
       
   373         {
       
   374         iRampedVolume = iRampedVolume + KBubbleVideoVolumeRampStep;
       
   375         if ( iRampedVolume >= iVolume )
       
   376             {
       
   377             // target volume level reached
       
   378             iRampedVolume = iVolume;
       
   379             iVolumeRampTimer->Cancel();
       
   380             }
       
   381 
       
   382         SetVolume( iRampedVolume );
       
   383         }
       
   384 
       
   385     return KErrNone;
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CBubbleVideoPlayer::RepeatsTrailPauseTimerCallback
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 TInt CBubbleVideoPlayer::RepeatsTrailPauseTimerCallback( TAny* aObj )
       
   393     {
       
   394     return static_cast<CBubbleVideoPlayer*>( aObj )->DoRingingRepeat();    
       
   395     }
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // CBubbleVideoPlayer::DoRingingRepeat
       
   399 // -----------------------------------------------------------------------------
       
   400 //    
       
   401 TInt CBubbleVideoPlayer::DoRingingRepeat()
       
   402     {
       
   403     iRepeatsTrailPauseTimer->Cancel();
       
   404     
       
   405     if ( iPlayerState == EVideoReady )
       
   406         {
       
   407         iVideoPlayer->Play();
       
   408         iPlayerState = EVideoPlaying;
       
   409         }
       
   410 
       
   411     return KErrNone;    
       
   412     }
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // CBubbleVideoPlayer::State
       
   416 // -----------------------------------------------------------------------------
       
   417 //
       
   418 CBubbleVideoPlayer::TVideoPlayerState CBubbleVideoPlayer::State() const
       
   419     {
       
   420     return iPlayerState;
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CBubbleVideoPlayer::VideoResolution
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 CBubbleVideoPlayer::TVideoResolution CBubbleVideoPlayer::VideoResolution() const
       
   428     {
       
   429     TSize frameSize( VideoFrameSize() );
       
   430     
       
   431     // SubQCCIF video resolution is 128x96 pixels
       
   432     if ( frameSize.iWidth == 128 && frameSize.iHeight == 96 )
       
   433         {
       
   434         return EVideoSubQCIF;
       
   435         }
       
   436     else if ( frameSize.iWidth == 176 && frameSize.iHeight == 144 )        
       
   437         {
       
   438         return EVideoQCIF;        
       
   439         }
       
   440     else
       
   441         {
       
   442         return EVideoOther;    
       
   443         }        
       
   444     }
       
   445 
       
   446 // -----------------------------------------------------------------------------
       
   447 // CBubbleVideoPlayer::VideoResolution
       
   448 // -----------------------------------------------------------------------------
       
   449 //    
       
   450 TSize CBubbleVideoPlayer::VideoFrameSize() const
       
   451     {
       
   452     TSize frameSize( 0,0 );
       
   453     
       
   454     TRAPD( err, iVideoPlayer->VideoFrameSizeL( frameSize ) );
       
   455     
       
   456     if ( err != KErrNone )
       
   457         {
       
   458         return TSize(0,0);
       
   459         }
       
   460 
       
   461     return frameSize;            
       
   462     }
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CBubbleVideoPlayer::AdjustToWindow
       
   466 // -----------------------------------------------------------------------------
       
   467 //    
       
   468 void CBubbleVideoPlayer::AdjustToWindow( RWindow& aDisplayWindow,
       
   469                                         const TRect& aUncropPane,
       
   470                                         TBool aArbitaryScalingSupported )
       
   471     {
       
   472     BUBBLE_PRINT("VideoPlayer::AdjToWin>");
       
   473     __ASSERT_DEBUG( ( iPlayerState == EVideoReady || 
       
   474                       iPlayerState == EVideoPaused ), 
       
   475                     Panic( EBMPanicVideoPlayer ) );
       
   476                     
       
   477     // Get video frame dimensions
       
   478     TSize  frameSize( VideoFrameSize() );
       
   479     if ( frameSize.iWidth == 0 || frameSize.iHeight == 0 )
       
   480         {
       
   481         return; // invalid            
       
   482         }
       
   483 
       
   484     // Get window position and dimensions.
       
   485     TSize  wndSize( aDisplayWindow.Size() );
       
   486     TPoint wndPosition( aDisplayWindow.AbsPosition() );
       
   487     
       
   488     // Set screenRect equal to aDisplayWindow.
       
   489     TRect screenRect( wndPosition, wndSize );
       
   490     
       
   491     TVideoResolution resolution = VideoResolution();         
       
   492     if ( resolution == EVideoQCIF || resolution == EVideoSubQCIF )
       
   493         {
       
   494         // aUncropPane is valid only for qcif and sqcif.
       
   495         
       
   496         // The pane where uncropped video is positioned.
       
   497         TSize uncropPaneSize( aUncropPane.Size() );
       
   498 
       
   499         // Calculate size for aScreenRect passed to video player. Size is
       
   500         // calculated so that auto scaled video frame covers uncrop pane.
       
   501         TSize screenRectSize = CalculateScreenRectSize( 
       
   502             uncropPaneSize, frameSize, aArbitaryScalingSupported );
       
   503     
       
   504         // Position aScreenRect.    
       
   505         screenRect = TRect( aUncropPane.iTl, screenRectSize );     
       
   506         TInt offsetX = (uncropPaneSize.iWidth - screenRectSize.iWidth) / 2;
       
   507         TInt offsetY = (uncropPaneSize.iHeight - screenRectSize.iHeight) / 2;
       
   508         screenRect.Move( offsetX, offsetY );    
       
   509         }
       
   510     
       
   511     // Video is autoscaled by video player.
       
   512     // iVideoPlayer->SetScaleFactorL(...);
       
   513     
       
   514     // Clip to display window
       
   515     TRect clipRect( wndPosition, wndSize );
       
   516     
       
   517     // Update settings to video player.
       
   518     CCoeEnv* coeEnv = CCoeEnv::Static();
       
   519     TRAP_IGNORE( iVideoPlayer->SetDisplayWindowL( 
       
   520            coeEnv->WsSession(),
       
   521            *coeEnv->ScreenDevice(),
       
   522            aDisplayWindow,
       
   523            screenRect,
       
   524            clipRect ) );
       
   525     BUBBLE_PRINT("VideoPlayer::AdjToWin<");           
       
   526     }
       
   527     
       
   528 // -----------------------------------------------------------------------------
       
   529 // CBubbleVideoPlayer::AdjustToWindow2
       
   530 // -----------------------------------------------------------------------------
       
   531 // 
       
   532 void CBubbleVideoPlayer::AdjustToWindow2( 
       
   533     RWindow& aDisplayWindow )
       
   534     {
       
   535     BUBBLE_PRINT("VideoPlayer::AdjToWin2>");
       
   536     __ASSERT_DEBUG( ( iPlayerState == EVideoReady || 
       
   537                       iPlayerState == EVideoPaused ), 
       
   538                     Panic( EBMPanicVideoPlayer ) );
       
   539                     
       
   540     // Get video frame dimensions
       
   541     TSize  frameSize( VideoFrameSize() );
       
   542     if ( frameSize.iWidth == 0 || frameSize.iHeight == 0 )
       
   543         {
       
   544         return; // invalid            
       
   545         }
       
   546         
       
   547     // Get video container position and dimensions
       
   548     TSize  wndSize( aDisplayWindow.Size() );
       
   549     TPoint wndPosition( aDisplayWindow.AbsPosition() );        
       
   550 
       
   551     // Get sizes for clip and screen rectangles.
       
   552     TSize clipSize;
       
   553     TSize screenSize;
       
   554     CalculateClipAndScreenRectSize( wndSize,
       
   555                                     frameSize,
       
   556                                     clipSize,
       
   557                                     screenSize );
       
   558     
       
   559     // Center clip rectangle to aDisplayWindow
       
   560     TRect clipRect( wndPosition, clipSize );
       
   561     TInt offsetX = (wndSize.iWidth - clipSize.iWidth) / 2;
       
   562     TInt offsetY = (wndSize.iHeight - clipSize.iHeight) / 2;
       
   563     clipRect.Move( offsetX, offsetY );
       
   564     
       
   565     // Center screen rectangle to aDisplayWindow
       
   566     TRect screenRect( wndPosition, screenSize );
       
   567     offsetX = (wndSize.iWidth - screenSize.iWidth) / 2;
       
   568     offsetY = (wndSize.iHeight - screenSize.iHeight) / 2;
       
   569     screenRect.Move( offsetX, offsetY );
       
   570     
       
   571     iObserver.HandleVideoPlayerBlittingAreaDefined( clipRect );
       
   572     
       
   573     // Update rectangles to video player.
       
   574     CCoeEnv* coeEnv = CCoeEnv::Static();
       
   575     TRAP_IGNORE( iVideoPlayer->SetDisplayWindowL( 
       
   576            coeEnv->WsSession(),
       
   577            *coeEnv->ScreenDevice(),
       
   578            aDisplayWindow,
       
   579            screenRect,
       
   580            clipRect ) );
       
   581     BUBBLE_PRINT("VideoPlayer::AdjToWin2<");             
       
   582     }
       
   583 
       
   584 // -----------------------------------------------------------------------------
       
   585 // CBubbleVideoPlayer::CalculateScreenRectSize
       
   586 // -----------------------------------------------------------------------------
       
   587 //
       
   588 TSize CBubbleVideoPlayer::CalculateScreenRectSize( 
       
   589     const TSize& aCanvasSize,
       
   590     const TSize& aVideoFrameSize,
       
   591     TBool aArbitraryScalingSupported )
       
   592     {
       
   593     // Mismatch between canvas size and video size
       
   594     TInt xDelta( aCanvasSize.iWidth - aVideoFrameSize.iWidth );
       
   595     TInt yDelta( aCanvasSize.iHeight - aVideoFrameSize.iHeight );
       
   596     
       
   597     // Find minimum scaling factor to cover canvas by video.
       
   598     TInt dScaleFactor( KBubbleVideoIACoef ); // use integer arithmetic 
       
   599             
       
   600     if ( xDelta == 0 && yDelta == 0 )
       
   601         {
       
   602         // correct size, scaling not needed
       
   603         }
       
   604     else if ( xDelta < 0 && yDelta == 0 )
       
   605         {
       
   606         // wide, but cannot downscale -> just crop
       
   607         }
       
   608     else if ( yDelta < 0 && xDelta == 0 )
       
   609         {
       
   610         // tall, but cannot downscale -> just crop    
       
   611         }    
       
   612     else if ( xDelta > 0 && yDelta > 0 )
       
   613         {            
       
   614         // small, narrow and flat  -> enlarge
       
   615         TInt xProp( ( KBubbleVideoIACoef * aCanvasSize.iWidth ) / 
       
   616                     aVideoFrameSize.iWidth );
       
   617         TInt yProp( ( KBubbleVideoIACoef * aCanvasSize.iHeight ) / 
       
   618                     aVideoFrameSize.iHeight );
       
   619         
       
   620         dScaleFactor = xProp > yProp ? xProp : yProp;
       
   621         }
       
   622     else if ( xDelta < 0 && yDelta < 0 ) 
       
   623         {
       
   624         // large, wide and tall -> downscale
       
   625         TInt xProp( ( KBubbleVideoIACoef * aCanvasSize.iWidth ) / 
       
   626                     aVideoFrameSize.iWidth );
       
   627         TInt yProp( ( KBubbleVideoIACoef * aCanvasSize.iHeight ) / 
       
   628                     aVideoFrameSize.iHeight );
       
   629         
       
   630         dScaleFactor = xProp > yProp ? xProp : yProp;      
       
   631         }
       
   632     else if ( xDelta > 0 && yDelta <= 0 )
       
   633         {
       
   634         // narrow -> enlarge
       
   635         dScaleFactor = ( KBubbleVideoIACoef * aCanvasSize.iWidth ) / 
       
   636                        aVideoFrameSize.iWidth;
       
   637         }
       
   638     else if ( yDelta > 0 && xDelta <= 0 )
       
   639         {
       
   640         // flat  -> enlarge
       
   641         dScaleFactor = ( KBubbleVideoIACoef * aCanvasSize.iHeight) / 
       
   642                        aVideoFrameSize.iHeight;              
       
   643         }
       
   644     else
       
   645         {
       
   646         // do nothing
       
   647         }
       
   648     
       
   649     if ( !aArbitraryScalingSupported )
       
   650         {
       
   651         TInt remainder( dScaleFactor % KBubbleVideoIACoef );
       
   652         
       
   653         ///////////////////////////////////////////////////////
       
   654         // Fixed scales are 200, 162, 137, 112, 100, 50  ( %)
       
   655         // Fixed values not used: 325, 275, 250, 225, 
       
   656         ///////////////////////////////////////////////////////
       
   657         if ( dScaleFactor > ( 2 * KBubbleVideoIACoef ) )
       
   658             {
       
   659             dScaleFactor = ( 2 * KBubbleVideoIACoef ); // 200% is MAX    
       
   660             }
       
   661         else if ( remainder )
       
   662             {
       
   663             if ( dScaleFactor > KBubbleVideoIACoef )    
       
   664                 {
       
   665                 // upscale
       
   666                 TInt scale = ( 100 * dScaleFactor ) / KBubbleVideoIACoef;
       
   667                 
       
   668                 if ( scale > 162 )
       
   669                     {
       
   670                     // 200%
       
   671                     dScaleFactor = 2 * KBubbleVideoIACoef;   
       
   672                     }
       
   673                 else if ( scale > 137 )
       
   674                     {
       
   675                     // 162%
       
   676                     dScaleFactor = 162 * ( KBubbleVideoIACoef / 100 );    
       
   677                     }
       
   678                 else if ( scale > 112 )
       
   679                     {
       
   680                     // 137%
       
   681                     dScaleFactor = 137 * ( KBubbleVideoIACoef / 100 );        
       
   682                     }
       
   683                 else
       
   684                     {
       
   685                     // 112%
       
   686                     dScaleFactor = 112 * ( KBubbleVideoIACoef / 100 );    
       
   687                     }                    
       
   688                 }
       
   689             else
       
   690                 {
       
   691                 // downscale    
       
   692                 if ( remainder > ( KBubbleVideoIACoef / 2 ) )
       
   693                     {
       
   694                     dScaleFactor = KBubbleVideoIACoef; // 100%    
       
   695                     }
       
   696                 else
       
   697                     {
       
   698                     dScaleFactor = ( KBubbleVideoIACoef / 2 ); // 50%    
       
   699                     }
       
   700                 }
       
   701             }        
       
   702         }
       
   703     
       
   704     ////////////////////////////////////////////////
       
   705     // Calculate scaled frame size (virtual canvas)
       
   706     ////////////////////////////////////////////////
       
   707     TInt screenWidth = DivideAndCeil( 
       
   708                            ( dScaleFactor * aVideoFrameSize.iWidth ), 
       
   709                            KBubbleVideoIACoef );
       
   710     TInt screenHeight = DivideAndCeil( 
       
   711                            ( dScaleFactor * aVideoFrameSize.iHeight ), 
       
   712                            KBubbleVideoIACoef );
       
   713     
       
   714     return TSize( screenWidth, screenHeight );    
       
   715     }
       
   716     
       
   717 // -----------------------------------------------------------------------------
       
   718 // CBubbleVideoPlayer::CalculateClipAndScreenRectSize
       
   719 // -----------------------------------------------------------------------------
       
   720 //
       
   721 void CBubbleVideoPlayer::CalculateClipAndScreenRectSize( 
       
   722     const TSize& aWindowSize,
       
   723     const TSize& aVideoFrameSize,
       
   724     TSize& aClipSize,
       
   725     TSize& aScreenSize )    
       
   726     {
       
   727     // Check aspect ratios
       
   728     // aWindowSize or aVideoFrameSize cannot be zero in this phase.
       
   729     TInt windowAspectRatio = ( KBubbleVideoIACoef * aWindowSize.iWidth ) / 
       
   730                              aWindowSize.iHeight;
       
   731     TInt videoAspectRatio = ( KBubbleVideoIACoef * aVideoFrameSize.iWidth ) / 
       
   732                              aVideoFrameSize.iHeight;                             
       
   733     
       
   734     
       
   735     if ( videoAspectRatio == windowAspectRatio )
       
   736         {
       
   737         // Aspect ratios are same.
       
   738         
       
   739         // Set clip and screen rect equal to window.
       
   740         // Scaled video will cover window entirely.
       
   741         aClipSize = aWindowSize;
       
   742         aScreenSize = aWindowSize;            
       
   743         }
       
   744     else if ( windowAspectRatio > videoAspectRatio )
       
   745         {
       
   746         // Window is more wide screen than video.
       
   747         
       
   748         // Window height sets the limit.
       
   749         TInt maxHeight = aWindowSize.iHeight;
       
   750 
       
   751         // Calculate max scale according to allowed crop.
       
   752         // Formula:
       
   753         // ( 100 - crop )   visible    window_height x scale x video_width
       
   754         // -------------- = ------- =  -----------------------------------
       
   755         //      100       scaled_size  (scale)^2 x video_width x video_height
       
   756         //
       
   757         TInt dScaleFactor = ( maxHeight * 100 * KBubbleVideoIACoef ) /
       
   758             ( (100 - KBubbleMaxVideoCrop) * aVideoFrameSize.iHeight );
       
   759             
       
   760         TInt scaledWidth = 
       
   761             DivideAndCeil( ( dScaleFactor * aVideoFrameSize.iWidth ), 
       
   762                            KBubbleVideoIACoef );
       
   763         
       
   764         // Limit to window width
       
   765         if ( scaledWidth > aWindowSize.iWidth )
       
   766             {
       
   767             scaledWidth = aWindowSize.iWidth;
       
   768             dScaleFactor = ( KBubbleVideoIACoef * aWindowSize.iWidth ) / 
       
   769                              aVideoFrameSize.iWidth;                
       
   770             }
       
   771             
       
   772         TInt scaledHeight = 
       
   773             DivideAndCeil( ( dScaleFactor * aVideoFrameSize.iHeight ), 
       
   774                            KBubbleVideoIACoef );            
       
   775             
       
   776         aClipSize = TSize( scaledWidth, maxHeight );
       
   777         aScreenSize = TSize( scaledWidth  + ( 2 * KBubbleVideoClipMarginX ), 
       
   778                              scaledHeight + ( 2 * KBubbleVideoClipMarginY ) );    
       
   779         }
       
   780     else
       
   781         {
       
   782         // Video is more wide screen than window.
       
   783         
       
   784         // Window width sets the limit.
       
   785         TInt maxWidth = aWindowSize.iWidth;
       
   786 
       
   787         // Calculate max scale according to allowed crop.
       
   788         // Formula:
       
   789         // ( 100 - crop )   visible    window_width x scale x video_heigth
       
   790         // -------------- = ------- =  -----------------------------------
       
   791         //      100       scaled_size  (scale)^2 x video_width x video_height
       
   792         //
       
   793         TInt dScaleFactor = ( maxWidth * 100 * KBubbleVideoIACoef ) /
       
   794             ( (100 - KBubbleMaxVideoCrop) * aVideoFrameSize.iWidth );
       
   795                                        
       
   796             
       
   797         TInt scaledHeight = 
       
   798             DivideAndCeil( ( dScaleFactor * aVideoFrameSize.iHeight ), 
       
   799                            KBubbleVideoIACoef );
       
   800         
       
   801         // Limit to window height
       
   802         if ( scaledHeight > aWindowSize.iHeight )
       
   803             {
       
   804             scaledHeight = aWindowSize.iHeight;
       
   805             dScaleFactor = ( KBubbleVideoIACoef * aWindowSize.iHeight ) / 
       
   806                              aVideoFrameSize.iHeight;                
       
   807             }
       
   808             
       
   809         TInt scaledWidth = 
       
   810             DivideAndCeil( ( dScaleFactor * aVideoFrameSize.iWidth ), 
       
   811                            KBubbleVideoIACoef );            
       
   812             
       
   813         aClipSize = TSize( maxWidth, scaledHeight );
       
   814         aScreenSize = TSize( scaledWidth  + ( 2 * KBubbleVideoClipMarginX ), 
       
   815                              scaledHeight + ( 2 * KBubbleVideoClipMarginY ) );   
       
   816         }
       
   817     }
       
   818 
       
   819 // ============================================================================
       
   820 // Callbacks from VideoPlayerutility
       
   821 // ============================================================================
       
   822 
       
   823 // ----------------------------------------------------------------------------
       
   824 // see MVideoPlayerUtilityObserver::MvpuoOpenComplete
       
   825 // ----------------------------------------------------------------------------
       
   826 //
       
   827 void CBubbleVideoPlayer::MvpuoOpenComplete( TInt aError )
       
   828     {
       
   829     BUBBLE_PRINTF("VideoPlayer::OpenComp:", aError);
       
   830     if ( aError == KErrNone )
       
   831         {
       
   832         // Prepare clip for playing
       
   833         iPlayerState = EVideoPreparing;
       
   834         iVideoPlayer->Prepare();
       
   835         }
       
   836     else // Report error
       
   837         {
       
   838         iPlayerState = EVideoError;
       
   839         iObserver.HandleVideoPlayerError( 
       
   840             MBubbleVideoPlayerObserver::EVideoPlayerInitializingFailure,
       
   841             aError );
       
   842         }
       
   843     }
       
   844 
       
   845 // ----------------------------------------------------------------------------
       
   846 // see MVideoPlayerUtilityObserver::MvpuoPrepareComplete
       
   847 // ----------------------------------------------------------------------------
       
   848 //
       
   849 void CBubbleVideoPlayer::MvpuoPrepareComplete( TInt aError )
       
   850     {
       
   851     BUBBLE_PRINTF("VideoPlayer::PrepComp:", aError);
       
   852     if ( aError == KErrNone )
       
   853         {
       
   854         iPlayerState = EVideoReady;
       
   855         iObserver.HandleVideoPlayerInitComplete();
       
   856 
       
   857         SetVolume( iVolume );
       
   858         SetRingingType( iRingingType );
       
   859 
       
   860         if ( iToBePlayed )
       
   861             {
       
   862             iVideoPlayer->Play();
       
   863             iPlayerState = EVideoPlaying;
       
   864             }
       
   865         }
       
   866     else  // Report error
       
   867         {
       
   868         iPlayerState = EVideoError;
       
   869         iObserver.HandleVideoPlayerError( 
       
   870             MBubbleVideoPlayerObserver::EVideoPlayerInitializingFailure,
       
   871             aError );
       
   872         }
       
   873     }
       
   874 
       
   875 // ----------------------------------------------------------------------------
       
   876 // see MVideoPlayerUtilityObserver::MvpuoFrameReady
       
   877 // ----------------------------------------------------------------------------
       
   878 //
       
   879 void CBubbleVideoPlayer::MvpuoFrameReady( CFbsBitmap& /*aFrame*/,
       
   880                                           TInt /*aError*/ )
       
   881     {
       
   882     // NOP
       
   883     }
       
   884 
       
   885 // ----------------------------------------------------------------------------
       
   886 // see MVideoPlayerUtilityObserver::MvpuoPlayComplete
       
   887 // ----------------------------------------------------------------------------
       
   888 //
       
   889 void CBubbleVideoPlayer::MvpuoPlayComplete( TInt aError )
       
   890     {
       
   891     BUBBLE_PRINTF("VideoPlayer::PlayComp:", aError);
       
   892     if ( aError == KErrNone )
       
   893         {
       
   894         iPlayerState = EVideoReady;
       
   895         
       
   896         if ( iRingingType == ETypeRingingOnce )
       
   897             {
       
   898             iObserver.HandleVideoPlayerPlayingComplete();
       
   899             } 
       
   900         else // continue playing
       
   901             {
       
   902             if ( !iRepeatsTrailPauseTimer )
       
   903                 {
       
   904                 iRepeatsTrailPauseTimer = 
       
   905                     CPeriodic::New( CActive::EPriorityStandard );
       
   906                 }
       
   907             
       
   908             if ( iRepeatsTrailPauseTimer && 
       
   909                  !iRepeatsTrailPauseTimer->IsActive() )
       
   910                 {
       
   911                 TCallBack cb( RepeatsTrailPauseTimerCallback, this );
       
   912                 iRepeatsTrailPauseTimer->Start( KBubbleVideoRepeatsTrailPause, 
       
   913                                          KBubbleVideoRepeatsTrailPause, 
       
   914                                          cb );
       
   915                 }
       
   916             }        
       
   917         }
       
   918     else // report error
       
   919         {
       
   920         iPlayerState = EVideoError;
       
   921         iObserver.HandleVideoPlayerError( 
       
   922             MBubbleVideoPlayerObserver::EVideoPlayerPlayingFailure,
       
   923             aError );
       
   924         }
       
   925     }
       
   926         
       
   927 // ----------------------------------------------------------------------------
       
   928 // see MVideoPlayerUtilityObserver::MvpuoEvent
       
   929 // ----------------------------------------------------------------------------
       
   930 //
       
   931 void CBubbleVideoPlayer::MvpuoEvent( const TMMFEvent& /*aEvent*/ )
       
   932     {
       
   933     // NOP
       
   934     }
       
   935     
       
   936 
       
   937 //  End of File