browserplugins/browseraudiovideoplugin/src/BavpControllerAudio.cpp
branchRCL_3
changeset 48 8e6fa1719340
parent 0 84ad3b177aa3
equal deleted inserted replaced
47:6385c4c93049 48:8e6fa1719340
       
     1 /*
       
     2 * Copyright (c) 2006 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 the License "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:  Controller class for handling browser requests to play video   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <AudioPreference.h>        // For priority/pref values
       
    21 #include <mmf/common/mmferrors.h>
       
    22 
       
    23 #include "BavpLogger.h"
       
    24 #include "BavpControllerAudio.h"
       
    25 #include "BavpView.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CBavpControllerAudio::NewL
       
    31 // Two-phased constructor.
       
    32 // -----------------------------------------------------------------------------
       
    33 CBavpControllerAudio* CBavpControllerAudio::NewL( MBavpView* aView,
       
    34                                                   TUint aAccessPtId,
       
    35                                                   TBavpMediaType aMediaType,
       
    36                                                   const TDesC& aFileName )
       
    37     {
       
    38     CBavpControllerAudio* self = new( ELeave ) CBavpControllerAudio( aView, aAccessPtId );
       
    39 
       
    40     Log( EFalse, _L("CBavpControllerAudio::NewL this="), (TInt)self );
       
    41 
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL( aMediaType, aFileName );
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CBavpControllerAudio::~CBavpControllerAudio()
       
    50 // Destructor
       
    51 // -----------------------------------------------------------------------------
       
    52 CBavpControllerAudio::~CBavpControllerAudio()
       
    53     {
       
    54     Log( EFalse, _L("CBavpControllerAudio::~CBavpControllerAudio") );
       
    55 
       
    56     // If currently playing, call Stop before you close and delete
       
    57     if ( iCurrentState == EBavpPlaying && iAudioPlayer != NULL)
       
    58         {
       
    59         iAudioPlayer->Stop();
       
    60         }
       
    61     if ( iAudioPlayer )
       
    62         {
       
    63         iAudioPlayer->Close();
       
    64         delete iAudioPlayer;
       
    65         }
       
    66 
       
    67     iFs.Close();
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CBavpControllerAudio::CBavpControllerAudio
       
    72 // C++ default constructor can NOT contain any code, that
       
    73 // might leave.
       
    74 // -----------------------------------------------------------------------------
       
    75 CBavpControllerAudio::CBavpControllerAudio( MBavpView* aView,
       
    76                                             TUint aAccessPtId )
       
    77     : CBavpController( aView, aAccessPtId )
       
    78     {
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CBavpControllerAudio::ConstructL
       
    83 // Symbian 2nd phase constructor can leave.
       
    84 // -----------------------------------------------------------------------------
       
    85 void CBavpControllerAudio::ConstructL( TBavpMediaType aMediaType,
       
    86                                        const TDesC& aFileName )
       
    87     {
       
    88     // Construct the BavpController base class
       
    89     BaseConstructL( aMediaType, aFileName );
       
    90 
       
    91     // Create Audio Player Client:
       
    92     iAudioPlayer = CMdaAudioPlayerUtility::NewL( *this, KAudioPriorityRealOnePlayer,
       
    93                         TMdaPriorityPreference( KAudioPrefRealOneLocalPlayback ) );
       
    94     
       
    95     // Connect to file server:
       
    96     User::LeaveIfError( iFs.Connect() );
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CBavpControllerAudio::OpenAudioUrlL
       
   101 // Open the audio url link
       
   102 // -----------------------------------------------------------------------------
       
   103 void CBavpControllerAudio::OpenAudioUrlL()
       
   104     {
       
   105     Log( EFalse, _L("CBavpControllerAudio::OpenUrlL() ") );
       
   106 
       
   107     // Note: Play() is called from MapcInitComplete()
       
   108     iAudioPlayer->OpenUrlL( *iClipInfo->iUrl, iAccessPtId, KNullDesC8 );
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CBavpControllerAudio::OpenAudioFileL
       
   113 // Open the audio file
       
   114 // -----------------------------------------------------------------------------
       
   115 void CBavpControllerAudio::OpenAudioFileL()
       
   116     {
       
   117     Log( EFalse, _L("CBavpControllerAudio::OpenFileL() ") );
       
   118 
       
   119     // Note: Play() is called from MapcInitComplete()
       
   120     iAudioPlayer->OpenFileL( *iClipInfo->iFileName );
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CBavpControllerAudio::PlayAudioVideoL
       
   125 // Handles request to open and play an audio or video file
       
   126 // TDesC& aFilename: Name of audio file, or video file, or URL link
       
   127 // -----------------------------------------------------------------------------
       
   128 void CBavpControllerAudio::PlayAudioVideoL()
       
   129     {
       
   130     Log( EFalse, _L("CBavpControllerAudio::OpenAndPlayL() ") );
       
   131 
       
   132     // If currently playing, call Stop before you attempt to open and play
       
   133     if ( iCurrentState == EBavpPlaying )
       
   134         {
       
   135         Stop();
       
   136         }
       
   137 
       
   138     OpenAudioFileL();
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CBavpControllerAudio::Stop
       
   143 // Stops audio from playing, if playing.
       
   144 // -----------------------------------------------------------------------------
       
   145 void CBavpControllerAudio::Stop()
       
   146     {
       
   147     Log( EFalse, _L("CBavpControllerAudio::Stop") );
       
   148 
       
   149     iAudioPlayer->Stop();
       
   150     iClipInfo->iPosition = 0;
       
   151     iAudioPlayer->SetPosition( iClipInfo->iPosition );
       
   152     iCurrentState = EBavpStopped;
       
   153 
       
   154     iBavpView->UpdateView();
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CBavpControllerAudio::Play
       
   159 // Non leaving method to play audio
       
   160 // -----------------------------------------------------------------------------
       
   161 void CBavpControllerAudio::Play()
       
   162     {
       
   163     // To keep LeaveScan from complaining everwhere, we use this non leaving
       
   164     // method
       
   165     TRAP_IGNORE( PlayL() );
       
   166     }
       
   167     
       
   168 // -----------------------------------------------------------------------------
       
   169 // CBavpControllerAudio::PlayL
       
   170 // Plays audio
       
   171 // -----------------------------------------------------------------------------
       
   172 void CBavpControllerAudio::PlayL()
       
   173     {
       
   174     Log( EFalse, _L("CBavpControllerAudio::PlayL") );
       
   175     
       
   176     SetPlayerVolume( iCurrentVolume );
       
   177 
       
   178     // Handle multiple media object case
       
   179     if ( iBavpControllerArray.Count() > 1 && IsAnotherControllerPlaying() )
       
   180         {
       
   181         if ( iLastCommand == EBavpCmdPlay )
       
   182             {
       
   183             // Another controller is playing, and the user wants to play
       
   184             // this media object. Pause (or stop) all of the others and
       
   185             // play this one.
       
   186             PauseOtherControllersPlaying();
       
   187             }
       
   188         }
       
   189 
       
   190     iAudioPlayer->SetPosition( iClipInfo->iPosition );
       
   191     iAudioPlayer->Play();
       
   192     iCurrentState = EBavpPlaying;
       
   193 
       
   194     iBavpView->UpdateView();
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CBavpControllerAudio::Pause
       
   199 // Non leaving method to pause playing audio
       
   200 // -----------------------------------------------------------------------------
       
   201 void CBavpControllerAudio::Pause()
       
   202     {
       
   203     // To keep LeaveScan from complaining everwhere, we use this non leaving
       
   204     // method
       
   205     TRAP_IGNORE( PauseL() );
       
   206     }
       
   207     
       
   208 // -----------------------------------------------------------------------------
       
   209 // CBavpControllerAudio::PauseL
       
   210 // Pauses video while playing, if playing.
       
   211 // -----------------------------------------------------------------------------
       
   212 void CBavpControllerAudio::PauseL()
       
   213     {
       
   214     Log( EFalse, _L("CBavpControllerAudio::PauseL") );
       
   215 
       
   216     if ( IsClipSeekable() && 
       
   217         (iCurrentState == EBavpPlaying || iCurrentState == EBavpPaused))
       
   218         {
       
   219         // Save clip position, in case we want to resume playing
       
   220         iAudioPlayer->GetPosition(iClipInfo->iPosition);
       
   221         iAudioPlayer->Pause();
       
   222         iCurrentState = EBavpPaused;
       
   223         }
       
   224     else
       
   225         {
       
   226         // Not seekable content, i.e. Live stream
       
   227         iAudioPlayer->Stop();
       
   228         iClipInfo->iPosition = 0;
       
   229         iCurrentState = EBavpStopped;
       
   230         }
       
   231 
       
   232     iBavpView->UpdateView();
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CBavpControllerAudio::FastForwardL
       
   237 // Fast forwards the audio
       
   238 // -----------------------------------------------------------------------------
       
   239 void CBavpControllerAudio::FastForwardL()
       
   240     {
       
   241     }
       
   242     
       
   243 // -----------------------------------------------------------------------------
       
   244 // CBavpControllerAudio::RewindL
       
   245 // Rewinds the audio
       
   246 // -----------------------------------------------------------------------------
       
   247 void CBavpControllerAudio::RewindL()
       
   248     {
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CBavpControllerAudio::SetPlayerVolume
       
   253 // Calculate the player volume from the CR volume (aka BavpController
       
   254 // volume value) and sets it
       
   255 // -----------------------------------------------------------------------------
       
   256 void CBavpControllerAudio::SetPlayerVolume( TInt aVolume )
       
   257     {
       
   258     TInt playerVolume( 0 );
       
   259     if ( iPlayerInitialized )
       
   260         {
       
   261         TInt maxVolume = iAudioPlayer->MaxVolume();
       
   262         if (aVolume < 0)
       
   263           {
       
   264           playerVolume = 0;
       
   265           }
       
   266         else if (aVolume > maxVolume)
       
   267           {
       
   268           playerVolume = maxVolume;
       
   269           }
       
   270         else 
       
   271           {
       
   272           // The CR volume is 0-10, convert to Audio player volume 0-100
       
   273           playerVolume = ( aVolume * iPlayerMaxVolume ) / KCRVolumeMax;     
       
   274           }
       
   275     
       
   276         // Set Video player volume
       
   277         iAudioPlayer->SetVolume( playerVolume );
       
   278       }
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CBavpControllerAudio::GetPlayerVolume
       
   283 // Calculate the player volume value and sets the playback volume for the audio 
       
   284 // track of the video clip.
       
   285 // -----------------------------------------------------------------------------
       
   286 TInt CBavpControllerAudio::GetPlayerVolume()
       
   287     {
       
   288     TInt playerVolume( 0 );
       
   289     TInt CRVolume( 0 );
       
   290     
       
   291     if ( iPlayerInitialized )
       
   292         {
       
   293         // Audio player returns 0-100 
       
   294         iAudioPlayer->GetVolume( playerVolume );
       
   295 
       
   296         // Convert to CR volume 0-10
       
   297         CRVolume = ( playerVolume * KCRVolumeMax ) / iPlayerMaxVolume;
       
   298         }
       
   299 
       
   300     return CRVolume;
       
   301     }
       
   302 
       
   303 // --------------------------------------------------------------------------
       
   304 // CBavpControllerAudio::HandleError
       
   305 // Handle error codes. We can receive errors from the MMF Video Player
       
   306 // and its MMF callbacks (mvpuoPlayComplete, etc).
       
   307 // The MMF errors are for handling content issues, such as decoding content
       
   308 // and network access.
       
   309 // NOTES:
       
   310 // MMF errors start at -12000, see /epoc32/include/mmf/common/MMFErrors.h
       
   311 // MMF also returns -1 (KErrNotFound) and few other system-wide errors
       
   312 // -----------------------------------------------------------------------------
       
   313 void CBavpControllerAudio::HandleError( TInt aError )
       
   314     {
       
   315     Log( EFalse, _L("In CBavpControllerAudioo::HandleError(): err="), aError );
       
   316     
       
   317     switch ( aError )
       
   318         {
       
   319         case KErrMMVideoDevice:
       
   320         case KErrMMAudioDevice:
       
   321             // We get this when the controllers can only support
       
   322             // playing one media at a time.
       
   323             HandleMultiInstanceError();
       
   324             break;
       
   325 
       
   326         default:
       
   327             iCurrentState = EBavpBadContent;
       
   328             iBavpView->UpdateView();
       
   329             break;
       
   330         }
       
   331     }
       
   332     
       
   333 // -----------------------------------------------------------------------------
       
   334 // CBavpControllerAudio::MapcInitComplete
       
   335 // Required due to MMdaAudioPlayerCallback derivation.
       
   336 // Called by client to indicate initialization has completed and device is ready
       
   337 // to play.
       
   338 // -----------------------------------------------------------------------------
       
   339 void CBavpControllerAudio::MapcInitComplete( TInt aError,
       
   340                                 const TTimeIntervalMicroSeconds& /*aDuration */ )
       
   341     {
       
   342     Log( EFalse, _L("MapcInitComplete aError="), aError );
       
   343 
       
   344     // Check for error
       
   345     if ( aError != KErrNone )
       
   346         {
       
   347         HandleError( aError );
       
   348         return;
       
   349         }
       
   350 
       
   351     // Audio player is initialized, so we can interact with it, volume,
       
   352     iPlayerInitialized = ETrue;
       
   353     
       
   354     iClipInfo->iDurationInSeconds = iAudioPlayer->Duration();
       
   355     iClipInfo->iPosition = 0;
       
   356     iAudioPlayer->SetPosition( iClipInfo->iPosition );
       
   357     
       
   358     // Get the max player volume, so we can scale volume
       
   359     iClipInfo->iMaxVolume = iAudioPlayer->MaxVolume();
       
   360     iPlayerMaxVolume = iClipInfo->iMaxVolume;
       
   361 
       
   362     // Handle multiple media object case
       
   363     if ( iBavpControllerArray.Count() > 1 && IsAnotherControllerPlaying() )
       
   364         {
       
   365         if ( iLastCommand == EBavpCmdUnknown )
       
   366             {
       
   367             // Another controller is playing, and this controller was
       
   368             // initializing. Because we can only play one media object at a
       
   369             // time, stop this media and let the first one
       
   370             // initialized continue to play.
       
   371             Stop();
       
   372             }
       
   373         else if ( iCurrentState == EBavpPlaying && iLastCommand == EBavpCmdPlay )
       
   374             {
       
   375             // Another controller is playing, and the user wants to play
       
   376             // another media object. Pause (or stop) all of the others and
       
   377             // play this one.
       
   378             PauseOtherControllersPlaying();
       
   379             Play();
       
   380             }
       
   381         }
       
   382     // Handle the single media object case
       
   383     else
       
   384         {
       
   385         Play();        
       
   386         }
       
   387 
       
   388     // Stop the buffering animation and play the audio only animation
       
   389     iBavpView->UpdateView();
       
   390     }
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CBavpControllerAudio::MapcPlayComplete
       
   394 // Required due to MMdaAudioPlayerCallback derivation.
       
   395 // Called by client to indicate audio file has completed or has stopped playing.
       
   396 // -----------------------------------------------------------------------------
       
   397 void CBavpControllerAudio::MapcPlayComplete( TInt aError )
       
   398     {
       
   399     Log( EFalse, _L("MapcPlayComplete aError="), aError );
       
   400     
       
   401     if ( iInfiniteLoopFlag )
       
   402         {
       
   403         // If infinite flag set: play audio again and again...
       
   404         iClipInfo->iPosition = 0;
       
   405         Play();
       
   406         }
       
   407     else if ( ( iLoopCount - 1 ) > 0 )
       
   408         {
       
   409         // If loop count: play, decrement loop count, play again...
       
   410         iClipInfo->iPosition = 0;
       
   411         Play();
       
   412         iLoopCount-- ;
       
   413         }
       
   414     else
       
   415         {
       
   416         // We're done looping, or just done playing the audio
       
   417         iCurrentState = EBavpPlayComplete;
       
   418         iClipInfo->iPosition = 0;
       
   419         iLoopCount = iInitLoopCount;
       
   420 
       
   421         iBavpView->UpdateView();
       
   422         }
       
   423     }
       
   424     
       
   425 // -----------------------------------------------------------------------------
       
   426 // CBavpControllerAudio::getPositionL
       
   427 // To get the current position of the clip
       
   428 // -----------------------------------------------------------------------------
       
   429     
       
   430  TTimeIntervalMicroSeconds CBavpControllerAudio::getPositionL() 
       
   431     {
       
   432         if ( iAudioPlayer )
       
   433         {
       
   434             TTimeIntervalMicroSeconds pos;
       
   435             iAudioPlayer->GetPosition( pos );
       
   436             return pos;
       
   437         }
       
   438         return NULL;
       
   439     }
       
   440     
       
   441  // -----------------------------------------------------------------------------
       
   442 // CBavpControllerAudio::setPositionL
       
   443 // To set the position of the clip
       
   444 // -----------------------------------------------------------------------------
       
   445     
       
   446   void CBavpControllerAudio::setPositionL( TTimeIntervalMicroSeconds val )    
       
   447     {
       
   448         if ( iAudioPlayer )
       
   449         {
       
   450 
       
   451             if ( !IsClipSeekable() && !IsClipLocalMedia() )
       
   452             {
       
   453                 return;
       
   454             }
       
   455             
       
   456             TBool didWePause = EFalse;
       
   457             // If playing, pause it (PauseL stops the PositionUpdater)
       
   458             if ( iCurrentState == EBavpPlaying )
       
   459             {
       
   460                 PauseL();
       
   461                 didWePause = ETrue;
       
   462             }
       
   463             
       
   464             if ( (val > iClipInfo->iDurationInSeconds ) || (val < 0) ) 
       
   465             {
       
   466                 val = 0;    
       
   467             }
       
   468                 
       
   469             iAudioPlayer->SetPosition(val);
       
   470             iClipInfo->iPosition = val;
       
   471             if ( didWePause )
       
   472             {
       
   473                 PlayL();
       
   474             }
       
   475         }
       
   476     }
       
   477 
       
   478 
       
   479 //  End of File