phoneapp/phoneuiview/src/cphoneaudioplayer.cpp
branchRCL_3
changeset 25 5266b1f337bd
equal deleted inserted replaced
24:41a7f70b3818 25:5266b1f337bd
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:  Audio player implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <eikenv.h>
       
    22 #include <apgcli.h>
       
    23 #include <apmrec.h>
       
    24 #include <AudioPreference.h>
       
    25 #include <utf.h>    // For converting data for TTS
       
    26 #include <c3dringingtoneinterface.h>
       
    27 #include <AudioOutput.h>
       
    28 
       
    29 #include "cphoneaudioplayer.h"
       
    30 #include "cphoneringingtone.h"
       
    31 #include "mphoneaudioplayerobserver.h"
       
    32 #include "phoneconstants.h"
       
    33 #include "phoneuiviewtrace.h"
       
    34 #include "phoneui.pan"
       
    35 
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 
       
    40 _LIT( KFileListRngMimeType, "application/vnd.nokia.ringing-tone" );
       
    41 //Min volume level
       
    42 const TInt KMinVolumeLevel = 1;
       
    43 //Max volume level
       
    44 const TInt KMaxVolumeLevel = 10;
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CPhoneAudioPlayer::CPhoneAudioPlayer
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CPhoneAudioPlayer::CPhoneAudioPlayer( TAudioDataFormat aDataFormat,
       
    52     MPhoneAudioPlayerObserver& aObserver, 
       
    53     TInt aPriority, 
       
    54     TUint aPreference,
       
    55     TInt aId )
       
    56     : iFormat( aDataFormat ),
       
    57     iObserver( aObserver ),
       
    58     iPriority( aPriority ),
       
    59     iPreference( aPreference ),
       
    60     iId( aId )
       
    61     {
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CPhoneAudioPlayer::ConstructL
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CPhoneAudioPlayer::ConstructL(
       
    69     const TDesC& aFileName,
       
    70     CMdaServer* aMdaServer )
       
    71     {
       
    72     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructL() <" );
       
    73     if ( iFormat == EFormatTone )
       
    74         {
       
    75         iTonePlayer = CMdaAudioToneUtility::NewL( *this, aMdaServer );
       
    76         iTonePlayer->PrepareToPlayFileSequence( aFileName );
       
    77         iPlayerStatus = EToneLoading;
       
    78         }
       
    79     else
       
    80         {
       
    81         //audio sample player
       
    82         PHONEUIVIEW_PRINTF3(
       
    83             "CPhoneAudioPlayer::ConstructL - aFileName(%S), iPriority(%d), iPreference(%d)",
       
    84             &aFileName, 
       
    85             iPriority, 
       
    86             iPreference );
       
    87 
       
    88         iSamplePlayer = CMdaAudioPlayerUtility::NewFilePlayerL( 
       
    89             aFileName,
       
    90             *this, 
       
    91             iPriority,
       
    92             static_cast< TMdaPriorityPreference >( iPreference ),
       
    93             aMdaServer );
       
    94         iPlayerStatus = EToneLoading;
       
    95 
       
    96         }
       
    97     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructL() >" );
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CPhoneAudioPlayer::ConstructSeqL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CPhoneAudioPlayer::ConstructSeqL( 
       
   105     const TDesC8& aSequence,
       
   106         CMdaServer* aMdaServer )
       
   107     {
       
   108     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructSeqL() <" );
       
   109     iTonePlayer = CMdaAudioToneUtility::NewL( *this, aMdaServer );
       
   110     iSequence = aSequence.AllocL();
       
   111     iTonePlayer->PrepareToPlayDesSequence( *iSequence );
       
   112     iPlayerStatus = EToneLoading;
       
   113     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructSeqL() >" );
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CPhoneAudioPlayer::ConstructTtsL
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CPhoneAudioPlayer::ConstructTtsL( 
       
   121     const TDesC& aTtsText, 
       
   122     TInt aPriority,
       
   123     TUint aPreference )
       
   124     {
       
   125     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructTtsL() <" );
       
   126     delete iTtsPlayer; // if any
       
   127     iTtsPlayer = NULL;
       
   128     iTtsPlayer = 
       
   129         CMdaAudioPlayerUtility::NewL( 
       
   130             *this, aPriority, 
       
   131             static_cast< TMdaPriorityPreference >( aPreference ) );
       
   132 
       
   133     delete iTtsText;  // if any
       
   134     iTtsText = NULL;
       
   135     // UTF-8 strings can take up to 4 bytes per character
       
   136     iTtsText = HBufC8::NewL( aTtsText.Length() << KTimesToMultiply ); 
       
   137 
       
   138     TPtr8 refText = iTtsText->Des(); 
       
   139     User::LeaveIfError( 
       
   140         CnvUtfConverter::ConvertFromUnicodeToUtf8( refText, aTtsText ) );
       
   141 
       
   142     // UTF-8 chars can be up to 4 bytes long, but usually 
       
   143     // take 1-2 bytes, 3 for asian chars.
       
   144     HBufC8* oldText = iTtsText;
       
   145     iTtsText = iTtsText->ReAlloc( iTtsText->Length() );
       
   146     if ( !iTtsText )
       
   147         {
       
   148         // ReAlloc failed, set back to original.
       
   149         iTtsText = oldText;
       
   150         }
       
   151 
       
   152     iTtsPlayer->OpenDesL( *iTtsText );
       
   153     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ConstructTtsL() >" );
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CPhoneAudioPlayer::New
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 CPhoneAudioPlayer* CPhoneAudioPlayer::New( 
       
   161     const CPhoneRingingTone& aRingingTone, 
       
   162     TInt aPriority, 
       
   163     TUint aPreference,
       
   164     MPhoneAudioPlayerObserver& aObserver, 
       
   165     TInt aId,
       
   166     CMdaServer* aMdaServer,
       
   167     TBool aExtSecNeeded )
       
   168     {
       
   169     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::New() <" );
       
   170     CPhoneAudioPlayer* self = NULL;
       
   171     TRAPD( err, self = CPhoneAudioPlayer::NewL( 
       
   172         aRingingTone,
       
   173         aPriority,
       
   174         aPreference,
       
   175         aObserver,
       
   176         aId,
       
   177         aMdaServer,
       
   178         aExtSecNeeded ) );
       
   179     if ( err != KErrNone )
       
   180         {
       
   181         return NULL;
       
   182         }
       
   183     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::New() >" );
       
   184     return self;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CPhoneAudioPlayer::NewL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 CPhoneAudioPlayer* CPhoneAudioPlayer::NewL( 
       
   192     const CPhoneRingingTone& aRingingTone, 
       
   193     TInt aPriority,
       
   194     TUint aPreference,
       
   195     MPhoneAudioPlayerObserver& aObserver,
       
   196     TInt aId,
       
   197     CMdaServer* aMdaServer,
       
   198     TBool aExtSecNeeded )
       
   199     {
       
   200     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL() <" );
       
   201     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::NewL - aExtSecNeeded(%d)",
       
   202         aExtSecNeeded );
       
   203 
       
   204     // Check the file DRM property if extend secure is needed.
       
   205     if ( aExtSecNeeded )
       
   206         {
       
   207         if ( !aRingingTone.IsFileInRom() &&
       
   208              !aRingingTone.IsFileDrmProtected() )
       
   209             {
       
   210             PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL - DRM extend security check permission denied" );
       
   211             User::Leave( KErrPermissionDenied );
       
   212             }
       
   213         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL - DRM extend security check ok" );
       
   214         }
       
   215     // RNG file types have to be played with CMdaAudioToneUtility player.
       
   216     // Otherwise use sample player
       
   217     TAudioDataFormat format = 
       
   218     ( aRingingTone.MimeType().CompareF( KFileListRngMimeType ) == KErrNone ) 
       
   219     ? EFormatTone :EFormatSample;
       
   220 
       
   221     CPhoneAudioPlayer* self = 
       
   222         new (ELeave) CPhoneAudioPlayer( 
       
   223             format,
       
   224             aObserver, 
       
   225             aPriority,
       
   226             aPreference,
       
   227             aId );
       
   228 
       
   229     CleanupStack::PushL( self );
       
   230     self->ConstructL( aRingingTone.FileName(), aMdaServer );
       
   231     CleanupStack::Pop( self );
       
   232     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL() >" );
       
   233     return self;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CPhoneAudioPlayer::NewL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 CPhoneAudioPlayer* CPhoneAudioPlayer::NewL( 
       
   241     const TDesC& aFileName, 
       
   242     TInt aPriority,
       
   243     TUint aPreference,
       
   244     MPhoneAudioPlayerObserver& aObserver,
       
   245     TInt aId,
       
   246     CMdaServer* aMdaServer,
       
   247     TBool aExtSecNeeded )
       
   248     {
       
   249     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL() <" );
       
   250     CPhoneRingingTone* tone = CPhoneRingingTone::NewL( aFileName );
       
   251     CleanupStack::PushL( tone );
       
   252 
       
   253     CPhoneAudioPlayer* player = CPhoneAudioPlayer::NewL( *tone,
       
   254                                                          aPriority,
       
   255                                                          aPreference,
       
   256                                                          aObserver,
       
   257                                                          aId,
       
   258                                                          aMdaServer,
       
   259                                                          aExtSecNeeded );
       
   260 
       
   261     CleanupStack::PopAndDestroy( tone );
       
   262     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewL() >");
       
   263     return player;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CPhoneAudioPlayer::NewSeqL
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 CPhoneAudioPlayer* CPhoneAudioPlayer::NewSeqL(
       
   271     const TDesC8& aSequence,
       
   272     TInt aPriority,
       
   273     TUint aPreference,
       
   274     MPhoneAudioPlayerObserver& aObserver, 
       
   275     TInt aId,
       
   276     CMdaServer* aMdaServer )
       
   277     {
       
   278     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewSeqL() <" );
       
   279     CPhoneAudioPlayer* self = 
       
   280         new (ELeave) CPhoneAudioPlayer( 
       
   281             EFormatTone, 
       
   282             aObserver, 
       
   283             aPriority,
       
   284             aPreference,
       
   285             aId );
       
   286 
       
   287     CleanupStack::PushL( self );
       
   288     self->ConstructSeqL( aSequence, aMdaServer  );
       
   289     CleanupStack::Pop( self );
       
   290 
       
   291     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewSeqL() >" );
       
   292     return self;
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CPhoneAudioPlayer::NewTtsL
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 CPhoneAudioPlayer* CPhoneAudioPlayer::NewTtsL(
       
   300     const TDesC& aText,
       
   301     TInt aPriority,
       
   302     TUint aPreference,
       
   303     MPhoneAudioPlayerObserver& aObserver, 
       
   304     TInt aId )
       
   305     {
       
   306     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewTtsL() <" );
       
   307     CPhoneAudioPlayer* self = 
       
   308         new (ELeave) CPhoneAudioPlayer( 
       
   309             EFormatTts, 
       
   310             aObserver, 
       
   311             aPriority,
       
   312             aPreference,
       
   313             aId );
       
   314 
       
   315     CleanupStack::PushL( self );
       
   316     self->ConstructTtsL( aText, aPriority, aPreference );
       
   317     CleanupStack::Pop( self );
       
   318     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::NewTtsL() >" );
       
   319     return self;
       
   320     }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CPhoneAudioPlayer::~CPhoneAudioPlayer
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 CPhoneAudioPlayer::~CPhoneAudioPlayer()
       
   327     {
       
   328 
       
   329     if ( iAudioOutput )
       
   330         {
       
   331         delete iAudioOutput;
       
   332         }
       
   333     if ( iTonePlayer )
       
   334         {
       
   335         delete iTonePlayer;
       
   336         } 
       
   337     if ( i3DPlugin )
       
   338        {
       
   339         i3DPlugin->Stop();
       
   340         delete i3DPlugin;
       
   341         }
       
   342     // ECom cleanup
       
   343     REComSession::FinalClose();
       
   344     if ( iSamplePlayer )
       
   345         {
       
   346         delete iSamplePlayer; 
       
   347         }
       
   348     if ( iTtsPlayer )
       
   349         {
       
   350         delete iTtsPlayer; 
       
   351         }
       
   352     if ( iSequence )
       
   353         {
       
   354         delete iSequence; 
       
   355         }
       
   356     if ( iTtsText )
       
   357         {
       
   358         delete iTtsText;
       
   359         } 
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CPhoneAudioPlayer::Play
       
   364 // -----------------------------------------------------------------------------
       
   365 //
       
   366 void CPhoneAudioPlayer::Play( TRingingType aRingType, TInt aVolume, TInt aTTsToBePlayed )
       
   367     {
       
   368     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::Play() <" );
       
   369     PHONEUIVIEW_PRINTF2( 
       
   370         "CPhoneAudioPlayer::Play - iFormat(%d), iPlayerStatus(%d)",
       
   371         iFormat, 
       
   372         iPlayerStatus );
       
   373     PHONEUIVIEW_PRINTF3( 
       
   374         "CPhoneAudioPlayer::Play - aRingType(%d), aVolume(%d), aTTsToBePlayed(%d)",
       
   375         aRingType, 
       
   376         aVolume,
       
   377         aTTsToBePlayed );
       
   378     iRingType = aRingType;
       
   379     iVolume = aVolume;
       
   380     iTTsToBePlayed = aTTsToBePlayed;
       
   381 
       
   382     if ( iFormat == EFormatTone )
       
   383         {
       
   384         switch ( iPlayerStatus )
       
   385             {
       
   386             case ETonePlaying:
       
   387                 iTonePlayer->CancelPlay();
       
   388                 DoPlay();
       
   389                 break;
       
   390 
       
   391             case EToneReady:
       
   392                 iPlayerStatus = ETonePlaying;
       
   393                 DoPlay();
       
   394                 break;
       
   395 
       
   396             case EToneLoading:
       
   397                 iToBePlaying = ETrue;
       
   398                 break;
       
   399 
       
   400             default:
       
   401                 __ASSERT_DEBUG( EFalse, 
       
   402                        Panic( EPhoneViewCaseNotHandled ) );
       
   403             }
       
   404         }
       
   405     else if ( iFormat == EFormatTts )
       
   406         {
       
   407         switch ( iPlayerStatus )
       
   408             {
       
   409             case ETonePlaying:
       
   410                 //stop previous playing
       
   411                 iTtsPlayer->Stop();
       
   412                 DoPlay();
       
   413                 break;
       
   414 
       
   415             case EToneReady:
       
   416                 iPlayerStatus = ETonePlaying;
       
   417                 DoPlay();
       
   418                 break;
       
   419 
       
   420             case EToneLoading:
       
   421                 iToBePlaying = ETrue;
       
   422                 break;
       
   423 
       
   424             default:
       
   425                 __ASSERT_DEBUG( EFalse, 
       
   426                         Panic( EPhoneViewCaseNotHandled ) );
       
   427             }
       
   428         }
       
   429     else
       
   430         {
       
   431         switch ( iPlayerStatus )
       
   432             {
       
   433             case ETonePlaying:
       
   434                 //stop previous playing
       
   435                 StopPlaying();
       
   436                 iPlayerStatus = ETonePlaying;
       
   437                 DoPlay();
       
   438                 break;
       
   439 
       
   440             case EToneReady:
       
   441                 iPlayerStatus = ETonePlaying;
       
   442                 DoPlay();
       
   443                 break;
       
   444 
       
   445             case EToneLoading:
       
   446                 iToBePlaying = ETrue;
       
   447                 break;
       
   448 
       
   449             default:
       
   450                 __ASSERT_DEBUG( EFalse, 
       
   451                         Panic( EPhoneViewCaseNotHandled ) );
       
   452             }
       
   453         }
       
   454     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::Play() >" );
       
   455     }
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CPhoneAudioPlayer::ReStartPlaying
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 void CPhoneAudioPlayer::ReStartPlaying()
       
   462     {
       
   463     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ReStartPlaying() <" );
       
   464     PHONEUIVIEW_PRINTF3( 
       
   465         "CPhoneAudioPlayer::ReStartPlaying - iFormat(%d), iRingType(%d), iVolume(%d)",
       
   466         iFormat, 
       
   467         iRingType, 
       
   468         iVolume );
       
   469 
       
   470     if ( iFormat == EFormatTone )
       
   471         {
       
   472         iTonePlayer->Play();
       
   473         iPlayerStatus = ETonePlaying;
       
   474         }
       
   475     else if ( iFormat == EFormatTts )
       
   476         {
       
   477         iTtsPlayer->Play();
       
   478         iPlayerStatus = ETonePlaying;
       
   479         }
       
   480     else
       
   481         {
       
   482         iSamplePlayer->Play();
       
   483         iPlayerStatus = ETonePlaying;
       
   484         }
       
   485     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::ReStartPlaying() >" );
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CPhoneAudioPlayer::StopPlaying
       
   490 // -----------------------------------------------------------------------------
       
   491 //
       
   492 void CPhoneAudioPlayer::StopPlaying()
       
   493     {
       
   494    PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::StopPlaying() <" );
       
   495 
       
   496     iToBePlaying = EFalse;
       
   497 
       
   498     if ( iFormat == EFormatTone )
       
   499         {
       
   500         if ( EMdaAudioToneUtilityPrepared == iTonePlayer->State() )
       
   501             {
       
   502             iTonePlayer->CancelPrepare();
       
   503             }
       
   504         else if ( EMdaAudioToneUtilityPlaying == iTonePlayer->State() )
       
   505             {
       
   506             iTonePlayer->CancelPlay();
       
   507             }
       
   508         }
       
   509     else if ( iFormat == EFormatTts )
       
   510         {
       
   511         iTtsPlayer->Stop();
       
   512         }
       
   513     else
       
   514         {
       
   515         if ( i3DPlugin )
       
   516             {
       
   517             i3DPlugin->Stop();
       
   518             delete i3DPlugin;
       
   519             i3DPlugin = NULL;
       
   520             }
       
   521         else
       
   522             {
       
   523             iSamplePlayer->Stop();
       
   524             }
       
   525         }
       
   526 
       
   527     iPlayerStatus = EToneReady;
       
   528     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::StopPlaying() >" );
       
   529     }
       
   530 
       
   531 // -----------------------------------------------------------------------------
       
   532 // CPhoneAudioPlayer::MapcInitComplete
       
   533 // -----------------------------------------------------------------------------
       
   534 //
       
   535 void CPhoneAudioPlayer::MapcInitComplete(
       
   536     TInt aError, 
       
   537     const TTimeIntervalMicroSeconds& /*aDuration*/ )
       
   538     {
       
   539     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MapcInitComplete() <" );
       
   540     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::MapcInitComplete - aError(%d)",
       
   541         aError );
       
   542     __ASSERT_DEBUG( ( ( iFormat == EFormatSample ) || ( iFormat == EFormatTts ) ) && 
       
   543         ( iPlayerStatus == EToneLoading ), Panic( EPhoneViewGeneralError ) );
       
   544     if ( aError == KErrNone )
       
   545         {
       
   546         if ( iToBePlaying )
       
   547             {
       
   548             iPlayerStatus = ETonePlaying;
       
   549             iToBePlaying = EFalse;
       
   550             DoPlay();
       
   551             }
       
   552         else
       
   553             {
       
   554             iPlayerStatus = EToneReady;
       
   555             }
       
   556         iObserver.HandleAudioPlayerInitComplete( iId );
       
   557         }
       
   558     else
       
   559         {
       
   560         iObserver.HandleAudioPlayerError( 
       
   561             MPhoneAudioPlayerObserver::EAudioPlayerInitializingFailure, 
       
   562             aError, iId );
       
   563         }
       
   564     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MapcInitComplete() >" );
       
   565     }
       
   566 
       
   567 // -----------------------------------------------------------------------------
       
   568 // CPhoneAudioPlayer::MapcPlayComplete
       
   569 // -----------------------------------------------------------------------------
       
   570 //
       
   571 void CPhoneAudioPlayer::MapcPlayComplete( TInt aError )
       
   572     {
       
   573     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MapcPlayComplete() <" );
       
   574     PHONEUIVIEW_PRINTF2( "CPhoneAudioPlayer::MapcPlayComplete - iFormat(%d), aError(%d)",
       
   575         iFormat, aError );
       
   576     iPlayerStatus = EToneReady;
       
   577 
       
   578     //Destruct 3D plugin before player itself gets destructed.
       
   579     //In case of continuous ringing type, stopplaying() handles
       
   580     //3d plugin destruction
       
   581     if ( i3DPlugin )
       
   582         {
       
   583         i3DPlugin->Stop();
       
   584         delete i3DPlugin;
       
   585         i3DPlugin = NULL;
       
   586         }
       
   587 
       
   588     if ( aError != KErrNone )
       
   589         {
       
   590         iObserver.HandleAudioPlayerError( 
       
   591             MPhoneAudioPlayerObserver::EAudioPlayerPlayingFailure, 
       
   592             aError, iId );
       
   593         }
       
   594     else
       
   595         {
       
   596         iObserver.HandlePlayingComplete( iId );
       
   597         }
       
   598     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MapcPlayComplete() >" );
       
   599     }
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // CPhoneAudioPlayer::MatoPrepareComplete
       
   603 // -----------------------------------------------------------------------------
       
   604 //
       
   605 void CPhoneAudioPlayer::MatoPrepareComplete( TInt aError )
       
   606     {
       
   607     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MatoPrepareComplete() <" );
       
   608     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::MatoPrepareComplete - aError(%d)",
       
   609         aError );
       
   610 
       
   611     __ASSERT_DEBUG( ( iFormat == EFormatTone ) && ( iPlayerStatus == EToneLoading ), 
       
   612         Panic( EPhoneViewGeneralError ) );
       
   613     if ( aError == KErrNone )
       
   614         {
       
   615         if ( iToBePlaying )
       
   616             {
       
   617             iPlayerStatus = ETonePlaying;
       
   618             iToBePlaying = EFalse;
       
   619             DoPlay();
       
   620             }
       
   621         else
       
   622             {
       
   623             iPlayerStatus = EToneReady;
       
   624             }
       
   625         iObserver.HandleAudioPlayerInitComplete( iId );
       
   626         }
       
   627     else
       
   628         {
       
   629         iObserver.HandleAudioPlayerError( 
       
   630             MPhoneAudioPlayerObserver::EAudioPlayerInitializingFailure, 
       
   631             aError, iId );
       
   632         }
       
   633     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MatoPrepareComplete() >" );
       
   634     }
       
   635 
       
   636 // -----------------------------------------------------------------------------
       
   637 // CPhoneAudioPlayer::MatoPlayComplete
       
   638 // -----------------------------------------------------------------------------
       
   639 //
       
   640 void CPhoneAudioPlayer::MatoPlayComplete(TInt aError)
       
   641     {
       
   642     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MatoPlayComplete() <" );
       
   643     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::MatoPlayComplete - aError(%d)",
       
   644         aError );
       
   645     if ( aError != KErrNone )
       
   646         {
       
   647         iObserver.HandleAudioPlayerError( 
       
   648             MPhoneAudioPlayerObserver::EAudioPlayerPlayingFailure, 
       
   649             aError, iId );
       
   650         }
       
   651     else
       
   652         {
       
   653         iObserver.HandlePlayingComplete( iId );
       
   654         }
       
   655     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::MatoPlayComplete() >" );
       
   656     }
       
   657 
       
   658 // -----------------------------------------------------------------------------
       
   659 // CPhoneAudioPlayer::DoPlay
       
   660 // -----------------------------------------------------------------------------
       
   661 //
       
   662 void CPhoneAudioPlayer::DoPlay()
       
   663     {
       
   664     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::DoPlay() <" );
       
   665     __ASSERT_DEBUG( iPlayerStatus == ETonePlaying, 
       
   666         Panic( EPhoneViewGeneralError ) );
       
   667 
       
   668     SetRingingTypeProperties();
       
   669 
       
   670     TInt err(KErrNone);
       
   671     if ( iFormat == EFormatTone )
       
   672         {
       
   673         iTonePlayer->Play();
       
   674         }
       
   675     else if ( iFormat == EFormatTts )
       
   676         {
       
   677         iTtsPlayer->Play();
       
   678         }
       
   679     else
       
   680         {
       
   681         TRAP( err,i3DPlugin = C3DRingingToneInterface::NewL( KNullUid ) );
       
   682         if ( !err && i3DPlugin )
       
   683             {
       
   684             i3DPlugin->SetAttr( E3DRTIAttrAudioPlayerUtility, iSamplePlayer );
       
   685             TRAP( err,i3DPlugin->PlayL() )
       
   686             }
       
   687         if ( err || !i3DPlugin )
       
   688             {
       
   689             if ( !iAudioOutput )
       
   690                  {
       
   691                  PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::DoPlay c aud ");
       
   692                  TRAP (err, iAudioOutput = CAudioOutput::NewL( *iSamplePlayer ) );
       
   693                 if ( err )
       
   694                     {
       
   695                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::DoPlay iAudioOutput.err ", err );
       
   696                     }
       
   697                }
       
   698             if ( !err && iAudioOutput )
       
   699                  {
       
   700                  PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::DoPlay EALL ");
       
   701                  TRAP (err, iAudioOutput->SetAudioOutputL( CAudioOutput::EAll ) );
       
   702                  if ( err )
       
   703                      {
       
   704                      PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::DoPlay SetAudioOutputL.err ", err ); 
       
   705                      }
       
   706 
       
   707                  }
       
   708             iSamplePlayer->Play();
       
   709             }
       
   710         }
       
   711     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::DoPlay() >" );
       
   712     }
       
   713 
       
   714 // -----------------------------------------------------------------------------
       
   715 // CPhoneAudioPlayer::SetRingingTypeProperties
       
   716 // -----------------------------------------------------------------------------
       
   717 //
       
   718 void CPhoneAudioPlayer::SetRingingTypeProperties()
       
   719     {
       
   720     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetRingingTypeProperties() <" );
       
   721     PHONEUIVIEW_PRINTF2( "CPhoneAudioPlayer::SetRingingTypeProperties - iFormat(%d), iRingType(%d)",
       
   722         iFormat, iRingType );
       
   723     TInt rampTime( 0 ); 
       
   724     if ( iFormat == EFormatTone )
       
   725         {
       
   726         switch ( iRingType )
       
   727             {
       
   728             case ETypeRinging:
       
   729                 //If we have TTS activated and ringingtype is ringing:
       
   730                 //We need to play TTS sequence again when ringingtone restarts.
       
   731                 //Thats why we need to set ringingtype to ETypeRingingOnce because
       
   732                 //it is the only way of knowing when ringingtone is completed. 
       
   733                 //Then we can restart it with new TTS iterations.
       
   734                 if ( iTTsToBePlayed )
       
   735                     {
       
   736                      //play only once
       
   737                     iTonePlayer->SetRepeats( 0, 
       
   738                         TTimeIntervalMicroSeconds(
       
   739                         KPhoneRingingRepeatsTrailPause ) ); 
       
   740                     iTonePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   741                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ringing tone with TTS, Set volume(%d) and ring once",
       
   742                         iVolume );
       
   743                     }
       
   744                 else
       
   745                     {
       
   746                     iTonePlayer->SetRepeats( KPhoneMdaAudioToneRepeatForever,
       
   747                         TTimeIntervalMicroSeconds( 
       
   748                          KPhoneRingingRepeatsTrailPause ) );
       
   749                     iTonePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   750                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ringing tone, Set volume(%d)",
       
   751                         iVolume );
       
   752                     }
       
   753                 break;
       
   754 
       
   755             case ETypeRingingOnce:
       
   756                 //play only once
       
   757                 iTonePlayer->SetRepeats( 0, 
       
   758                     TTimeIntervalMicroSeconds(
       
   759                     KPhoneRingingRepeatsTrailPause ) ); 
       
   760                 iTonePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   761                 PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ring once tone, set volume(%d)",
       
   762                     iVolume );
       
   763                 break;
       
   764 
       
   765             case ETypeAscending:
       
   766                 iTonePlayer->SetRepeats( KPhoneMdaAudioToneRepeatForever,
       
   767                     TTimeIntervalMicroSeconds(
       
   768                     KPhoneRingingRepeatsTrailPause ) );
       
   769 
       
   770                 //Special case for TTs + ascending profile. Volume 
       
   771                 //and ramptime needs to be different as usual
       
   772                 if ( iTTsToBePlayed )
       
   773                     {
       
   774                     //rampTime in this case is only 3 seconds and volume is 1.
       
   775                     rampTime = KPhoneAudioAscendingRampDuration;
       
   776                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ascending tone with TTS, Start from lowest volume, rampTime(%d)",
       
   777                         rampTime );
       
   778                     iTonePlayer->SetVolumeRamp( 
       
   779                         TTimeIntervalMicroSeconds( rampTime ) ); 
       
   780 
       
   781                     iCurrentVolume = 1; //we start from the lowest volume
       
   782                     iTonePlayer->SetVolume( ConvertVolume( iCurrentVolume ) );
       
   783                     }
       
   784                 else
       
   785                     {
       
   786                     //rampTime: time for one step * volume level
       
   787                     rampTime = KPhoneAudioAscendingRampDuration*iVolume;
       
   788                     PHONEUIVIEW_PRINTF2( "CPhoneAudioPlayer::SetRingingTypeProperties - Ascending tone, Set volume(%d), rampTime(%d)",
       
   789                         iVolume, rampTime );
       
   790                     iTonePlayer->SetVolumeRamp( 
       
   791                         TTimeIntervalMicroSeconds( rampTime ) );
       
   792 
       
   793                     iCurrentVolume = 1; //we start from the lowest volume
       
   794                     iTonePlayer->SetVolume( ConvertVolume( iVolume ) ); 
       
   795                     }
       
   796                 break;
       
   797 
       
   798             default:
       
   799                 Panic( EPhoneViewGeneralError );
       
   800                 break;
       
   801             }
       
   802 
       
   803         PHONEUIVIEW_PRINTF3( "CPhoneAudioPlayer::SetRingingTypeProperties - Tone, iPriority(%d), iPreference(%d), iVolume(%d)",
       
   804             iPriority, iPreference, iVolume );
       
   805 
       
   806         //Set priority and preference
       
   807         iTonePlayer->SetPriority( iPriority, 
       
   808             static_cast< TMdaPriorityPreference >( iPreference ) );
       
   809         }
       
   810     else if ( iFormat == EFormatTts )
       
   811         {
       
   812         switch ( iRingType )
       
   813             {
       
   814             case ETypeRinging:
       
   815             case ETypeAscending:
       
   816                 // Not possible iRingType for TTS.
       
   817                 break;
       
   818 
       
   819             case ETypeRingingOnce:
       
   820                 iTtsPlayer->SetRepeats( 0, // play only once
       
   821                     TTimeIntervalMicroSeconds( 
       
   822                     KPhoneRingingRepeatsTrailPause ) );
       
   823                 iTtsPlayer->SetVolume( ConvertVolume( iVolume ) );
       
   824                 PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - TTS, Set volume(%d)",
       
   825                     iVolume );
       
   826                 break;
       
   827 
       
   828             default:
       
   829                 __ASSERT_DEBUG( EFalse, 
       
   830                         Panic( EPhoneViewCaseNotHandled ) );
       
   831             }
       
   832         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetRingingTypeProperties - TTS" );
       
   833         }
       
   834     else
       
   835         {
       
   836         switch ( iRingType )
       
   837             {
       
   838             case ETypeRinging:
       
   839                 //If we have TTS activated and ringingtype is ringing:
       
   840                 //We need to play TTS sequence again when ringingtone restarts.
       
   841                 //Thats why we need to set ringingtype to ETypeRingingOnce because
       
   842                 //it is theonly way of knowing when ringingtone is completed. 
       
   843                 //Then we can restartit with new TTS iterations.
       
   844                 if ( iTTsToBePlayed )
       
   845                     {
       
   846                      //play only once
       
   847                     iSamplePlayer->SetRepeats( 0, 
       
   848                         TTimeIntervalMicroSeconds(
       
   849                         KPhoneRingingRepeatsTrailPause ) ); 
       
   850                     iSamplePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   851                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ringing sample with TTS, Ring only once, Set volume(%d)",
       
   852                         iVolume );
       
   853                     }
       
   854                 else
       
   855                     {
       
   856                     iSamplePlayer->SetRepeats( KPhoneMdaAudioToneRepeatForever,
       
   857                         TTimeIntervalMicroSeconds( 
       
   858                         KPhoneRingingRepeatsTrailPause ) );
       
   859                     iSamplePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   860                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ringing sample, Set volume(%d)",
       
   861                         iVolume );
       
   862                     }
       
   863                 break;
       
   864 
       
   865             case ETypeRingingOnce:
       
   866                 iSamplePlayer->SetRepeats( 0,//play only once
       
   867                     TTimeIntervalMicroSeconds( 
       
   868                     KPhoneRingingRepeatsTrailPause ) ); 
       
   869                 iSamplePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   870                 PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ring once sample, set volume(%d)",
       
   871                     iVolume );
       
   872                 break;
       
   873 
       
   874             case ETypeAscending:
       
   875                 iSamplePlayer->SetRepeats( KPhoneMdaAudioToneRepeatForever,
       
   876                     TTimeIntervalMicroSeconds(
       
   877                     KPhoneRingingRepeatsTrailPause ) );
       
   878 
       
   879                 //Special case for TTs + ascending profile. Volume 
       
   880                 //and ramptime needs to be different as usual
       
   881                 if ( iTTsToBePlayed )
       
   882                     {
       
   883                     //rampTime in this case is only 3 seconds and volume is 1.
       
   884                     rampTime = KPhoneAudioAscendingRampDuration;
       
   885                     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetRingingTypeProperties - Ascending sample with TTS, Start from lowest volume, rampTime(%d)",
       
   886                         rampTime );
       
   887                     iSamplePlayer->SetVolumeRamp( 
       
   888                         TTimeIntervalMicroSeconds( rampTime ) ); 
       
   889 
       
   890                     iCurrentVolume = 1; //we start from the lowest volume
       
   891                     iSamplePlayer->SetVolume( ConvertVolume( iCurrentVolume ) );
       
   892                     }
       
   893                 else
       
   894                     {
       
   895                     //rampTime: time for one step * volume level
       
   896                     rampTime = KPhoneAudioAscendingRampDuration*iVolume;
       
   897                     PHONEUIVIEW_PRINTF2( "CPhoneAudioPlayer::SetRingingTypeProperties - Ascending sample, Set volume(%d), rampTime(%d)",
       
   898                         iVolume, rampTime );
       
   899                     iSamplePlayer->SetVolumeRamp( 
       
   900                         TTimeIntervalMicroSeconds( rampTime ) );
       
   901 
       
   902                     iCurrentVolume = 1; //we start from the lowest volume
       
   903                     iSamplePlayer->SetVolume( ConvertVolume( iVolume ) );
       
   904                     }
       
   905                 break;
       
   906 
       
   907             default:
       
   908                 Panic( EPhoneViewGeneralError );
       
   909                 break;
       
   910             }
       
   911 
       
   912         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetRingingTypeProperties - Sample" );
       
   913         }
       
   914     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetRingingTypeProperties() >" );
       
   915     }
       
   916 
       
   917 // -----------------------------------------------------------------------------
       
   918 // CPhoneAudioPlayer::SetNewVolumeAndRamptime
       
   919 // -----------------------------------------------------------------------------
       
   920 //
       
   921 void CPhoneAudioPlayer::SetNewVolumeAndRamptime( TInt aVolume, TInt aRamptime )
       
   922     {
       
   923     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetNewVolumeAndRamptime() <" );
       
   924  
       
   925     //Check that volume is in valid range.
       
   926     TInt volume = aVolume<1 ? 1:aVolume;
       
   927     PHONEUIVIEW_PRINTF( "CPhoneAudioPlayer::SetNewVolumeAndRamptime - aVolume(%d)",
       
   928         aVolume ); 
       
   929 
       
   930     if ( iFormat == EFormatTone && iTonePlayer )
       
   931         {
       
   932         iTonePlayer->SetVolumeRamp(TTimeIntervalMicroSeconds( aRamptime ) );
       
   933         iTonePlayer->SetVolume( ConvertVolume( volume ) );
       
   934         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetNewVolumeAndRamptime - Tone player" );
       
   935         }
       
   936     else if ( iFormat == EFormatTts && iTtsPlayer )
       
   937         {
       
   938         iTtsPlayer->SetVolumeRamp(TTimeIntervalMicroSeconds( aRamptime ) );
       
   939         iTtsPlayer->SetVolume( ConvertVolume( volume ) );
       
   940         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetNewVolumeAndRamptime - TTS player" );
       
   941         }
       
   942     else 
       
   943         {
       
   944         iSamplePlayer->SetVolumeRamp(TTimeIntervalMicroSeconds( aRamptime ) );
       
   945         iSamplePlayer->SetVolume( ConvertVolume( volume ) );
       
   946         PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetNewVolumeAndRamptime - Sample player" );
       
   947         }
       
   948  
       
   949     PHONEUIVIEW_PRINT( "CPhoneAudioPlayer::SetNewVolumeAndRamptime() >" );
       
   950     }
       
   951  
       
   952 
       
   953 // -----------------------------------------------------------------------------
       
   954 // CPhoneAudioPlayer::ConvertVolume
       
   955 // -----------------------------------------------------------------------------
       
   956 //
       
   957 TInt CPhoneAudioPlayer::ConvertVolume( TInt aVolume )
       
   958     {
       
   959     TInt result( 0 );
       
   960 
       
   961     if ( iFormat == EFormatTone && iTonePlayer )
       
   962         {
       
   963         result = iTonePlayer->MaxVolume() * aVolume / KMaxVolumeLevel;
       
   964         }
       
   965     else if ( iFormat == EFormatTts && iTtsPlayer )
       
   966         {
       
   967         result = iTtsPlayer->MaxVolume() * aVolume / KMaxVolumeLevel;
       
   968         }
       
   969     else
       
   970         {
       
   971         result = iSamplePlayer->MaxVolume() * aVolume / KMaxVolumeLevel; 
       
   972         }
       
   973 
       
   974     // if user has selected minimum volume level
       
   975     // set HW volume 1
       
   976     if ( aVolume == KMinVolumeLevel && result == 0 )
       
   977         {
       
   978         result = 1; 
       
   979         }
       
   980  
       
   981     //_DDPRINT( 5, "P.AudPlayer.ConvertVolume.result", result ); 
       
   982     return result;
       
   983     }
       
   984 
       
   985  
       
   986 // -----------------------------------------------------------------------------
       
   987 // CPhoneAudioPlayer::SetTTsToBePlayed
       
   988 // -----------------------------------------------------------------------------
       
   989 //
       
   990 void CPhoneAudioPlayer::SetTTsToBePlayed( TBool aTTsToBePlayed )
       
   991     {
       
   992     iTTsToBePlayed = aTTsToBePlayed;
       
   993     }
       
   994 
       
   995 // -----------------------------------------------------------------------------
       
   996 // CPhoneAudioPlayer::MutePlaying
       
   997 // -----------------------------------------------------------------------------
       
   998 //
       
   999 void CPhoneAudioPlayer::MutePlaying()
       
  1000     {
       
  1001     //_DPRINT( 5, "P.Aud.Mute" );
       
  1002     if ( iPlayerStatus == ETonePlaying )
       
  1003         {
       
  1004         if ( iFormat == EFormatTone && iTonePlayer )
       
  1005             {
       
  1006             iTonePlayer->SetVolume(0);
       
  1007             }
       
  1008         else if ( iFormat == EFormatTts && iTtsPlayer )
       
  1009             {
       
  1010             iTtsPlayer->SetVolume(0);
       
  1011             }
       
  1012         else // EFormatSample
       
  1013             {
       
  1014             iSamplePlayer->SetVolume(0);
       
  1015             }
       
  1016         }
       
  1017     else
       
  1018         {
       
  1019         // Mute called during EToneLoading state.
       
  1020         iToBePlaying = EFalse;
       
  1021         } 
       
  1022     }
       
  1023  
       
  1024 //  End of File