profilesservices/FileList/Src/CFLDRingingTonePlayer.cpp
changeset 68 13e71d907dc3
parent 0 8c5d936e5675
equal deleted inserted replaced
40:6465d5bb863a 68:13e71d907dc3
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *     CFLDRingingTonePlayer is used to play sound files. It takes into account
       
    16 *     the current volume and ringing type settings.
       
    17 *
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 // CLASS HEADER
       
    23 #include "CFLDRingingTonePlayer.h"
       
    24 
       
    25 // EXTERNAL INCLUDES
       
    26 #include <AudioPreference.h>
       
    27 #include <mda/common/resource.h>
       
    28 #include <c3dringingtoneinterface.h>		// C3DRingingToneInterface
       
    29 #include <ScreensaverInternalPSKeys.h>
       
    30 
       
    31 // CONSTANTS
       
    32 namespace
       
    33 	{
       
    34 	const TInt KAscendingVolumeInterval( 3000000 ); // 3 seconds
       
    35 	const TInt KToneInterval( 1000000 ); // 1 second pause between tones
       
    36 
       
    37     _LIT( KDataTypeNone, "" );
       
    38 	_LIT( KFileListRngMimeType, "application/vnd.nokia.ringing-tone" );
       
    39 	_LIT8( KFileListBeepSequence, "\x00\x11\x06\x0A\x08\x73\x0A\x40\x28\x0A\xF7\
       
    40 	\x05\xFC\x40\x64\x0A\x08\x40\x32\x0A\xF7\x06\x0B" ); //sequence for playing a beep once sound
       
    41 	}
       
    42 
       
    43 // ============================ MEMBER FUNCTIONS ===============================
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CFLDRingingTonePlayer::NewL
       
    47 // Two-phased constructor.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CFLDRingingTonePlayer* CFLDRingingTonePlayer::NewL( TBool aShowErrorMsgs )
       
    51     {
       
    52     CFLDRingingTonePlayer* self = new( ELeave ) CFLDRingingTonePlayer( aShowErrorMsgs );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self ); // self
       
    56     return self;
       
    57     }
       
    58 
       
    59 // Destructor
       
    60 CFLDRingingTonePlayer::~CFLDRingingTonePlayer()
       
    61     {
       
    62     Cancel(); // Stops possible playback
       
    63 
       
    64 	delete iAudioPlayer;
       
    65 	delete iTonePlayer;
       
    66 	
       
    67 	delete i3dRingingTonePlugin;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CFLDRingingTonePlayer::CFLDRingingTonePlayer
       
    72 // C++ constructor can NOT contain any code, that might leave.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CFLDRingingTonePlayer::CFLDRingingTonePlayer( TBool aShowErrorMsgs )
       
    76     :   CFLDPlayerBase( aShowErrorMsgs ),
       
    77         iAudioPlayerStatus( EAudioPlayerNotCreated ),
       
    78 		iTonePlayerStatus( EAudioPlayerNotCreated )
       
    79     {
       
    80     i3dEffects = ETrue;
       
    81     i3DEffect = KErrNotFound;
       
    82     i3DEcho = KErrNotFound;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CFLDRingingTonePlayer::ConstructL
       
    87 // Symbian 2nd phase constructor can leave.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CFLDRingingTonePlayer::ConstructL()
       
    91     {
       
    92     BaseConstructL();
       
    93 
       
    94    	// To allow/not allow screensaver
       
    95    	// Errors ignored, no actions needed if API is not available
       
    96     iPropScreenSaver.Attach( KPSUidScreenSaver, KScreenSaverAllowScreenSaver );
       
    97 
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CFLDRingingTonePlayer::ProcessFileL()
       
   102 // (other items were commented in a header).
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CFLDRingingTonePlayer::ProcessFileL( const TDesC& aFileName, RWindow* /*aWindow*/ )
       
   106     {
       
   107     if ( aFileName == KNullDesC || iFocusLost )
       
   108         {
       
   109         return;
       
   110         }
       
   111 
       
   112     Cancel(); // Stops possible playback
       
   113 
       
   114 	TDataType dataType;
       
   115 	TInt error( DataType( aFileName, dataType ) );
       
   116     if( ( error != KErrNotFound ) && ( error != KErrNone ) )
       
   117         {
       
   118         User::Leave( error );
       
   119         }
       
   120 
       
   121    	// Do not allow screen saver while playing
       
   122     // Errors ignored, no actions needed if API is not available	   	
       
   123    	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   124 			KScreenSaverAllowScreenSaver, EFLScreenSaverNotAllowed );
       
   125 
       
   126 	delete iTonePlayer;
       
   127     iTonePlayer = NULL;
       
   128 	
       
   129     delete iAudioPlayer;
       
   130     iAudioPlayer = NULL;
       
   131 
       
   132 	if ( iRingingType == ERingingTypeBeepOnce )
       
   133 		{
       
   134 		// Active profile ringing tone is set to Beep Once
       
   135 		// Don't initialize a FileSequence but use DesSequence instead
       
   136         iTonePlayer = CMdaAudioToneUtility::NewL( *this );
       
   137 		iTonePlayer->PrepareToPlayDesSequence( KFileListBeepSequence() );
       
   138 		iTonePlayerStatus = EAudioPlayerInitializing;
       
   139 		}
       
   140     else
       
   141 		{
       
   142 		TPtrC ptr( aFileName );
       
   143         if( error )
       
   144             {
       
   145             ptr.Set( iDefaultTone );
       
   146             }
       
   147         if( dataType.Des().CompareF( KFileListRngMimeType ) == 0 )
       
   148             {
       
   149             //Ringingtone is a RNG-file
       
   150             iTonePlayer = CMdaAudioToneUtility::NewL( *this );
       
   151             iTonePlayer->PrepareToPlayFileSequence( ptr );
       
   152             iTonePlayerStatus = EAudioPlayerInitializing;
       
   153             }
       
   154         // If the mime type is NULL, the file can't be play.
       
   155         else if ( dataType.Des().CompareF( KDataTypeNone ) == 0 )
       
   156             {
       
   157             return;
       
   158             }
       
   159         else
       
   160             {
       
   161             iAudioPlayer = CDrmPlayerUtility::NewFilePlayerL(
       
   162              	ptr, *this, KAudioPriorityRingingTonePreview,
       
   163                 TMdaPriorityPreference ( 
       
   164                 iVibra ? KAudioPrefRingFilePreviewVibra :
       
   165                 KAudioPrefRingFilePreview ) );
       
   166 	
       
   167             iAudioPlayerStatus = EAudioPlayerInitializing;
       
   168             }
       
   169 		}
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CFLDRingingTonePlayer::Cancel()
       
   174 // (other items were commented in a header).
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CFLDRingingTonePlayer::Cancel()
       
   178     {
       
   179    	if ( iAudioPlayerStatus == EAudioPlayerPlayingWith3DEffect )
       
   180    	    {
       
   181    	    i3dRingingTonePlugin->Stop();
       
   182    	    // plugin calls AudioPlayer->Stop()
       
   183 	    iAudioPlayer->Close();
       
   184 	    delete iAudioPlayer;
       
   185 	    iAudioPlayer = NULL;
       
   186    	    }
       
   187     
       
   188    	if ( iAudioPlayerStatus == EAudioPlayerPlaying )
       
   189         {
       
   190         if( iAudioPlayer )
       
   191 	        {
       
   192     	    iAudioPlayer->Stop();
       
   193     	    iAudioPlayer->Close();
       
   194     	    delete iAudioPlayer;
       
   195     	    iAudioPlayer = NULL;
       
   196 	        }
       
   197         }
       
   198 
       
   199     iAudioPlayerStatus = EAudioPlayerReady;
       
   200 
       
   201 	if ( iTonePlayerStatus == EAudioPlayerPlaying )
       
   202         {
       
   203         if( iTonePlayer )
       
   204 	        {
       
   205     	    iTonePlayer->CancelPlay();
       
   206     	    delete iTonePlayer;
       
   207     	    iTonePlayer = NULL;
       
   208 	        }
       
   209         }
       
   210     iTonePlayerStatus = EAudioPlayerReady;
       
   211 
       
   212    	// Allow screen saver, unless there's a call ongoing
       
   213    	if( !IsCallOngoing() )
       
   214    		{
       
   215 	    // Errors ignored, no actions needed if API is not available	   	
       
   216 	   	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   217 	    		KScreenSaverAllowScreenSaver, EFLScreenSaverAllowed );
       
   218    		}
       
   219 
       
   220 }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CFLDRingingTonePlayer::DoSetRingingType()
       
   224 // (other items were commented in a header).
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CFLDRingingTonePlayer::DoSetRingingType( TInt aRingingType )
       
   228     {
       
   229     if( iAudioPlayerStatus == EAudioPlayerInitialized )
       
   230         {
       
   231         switch( aRingingType )
       
   232             {
       
   233 			// Fall through
       
   234             case ERingingTypeRinging:
       
   235             case ERingingTypeSilent:
       
   236                 {
       
   237                 iAudioPlayer->SetRepeats(
       
   238                  KMdaRepeatForever, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   239                 break;
       
   240                 }
       
   241             case ERingingTypeAscending:
       
   242                 {
       
   243                 iAudioPlayer->SetRepeats(
       
   244                 	KMdaRepeatForever, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   245                 TInt volRamp = KAscendingVolumeInterval * iRingingVolume;
       
   246                 iAudioPlayer->SetVolumeRamp( TTimeIntervalMicroSeconds( volRamp ) );
       
   247 				break;
       
   248                 }
       
   249             case ERingingTypeRingOnce:
       
   250                 {
       
   251                 iAudioPlayer->SetRepeats(
       
   252                  0, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   253                 break;
       
   254                 }
       
   255 
       
   256             default:
       
   257                 {
       
   258                 break;
       
   259                 }
       
   260             }
       
   261         }
       
   262     else if( iTonePlayerStatus == EAudioPlayerInitialized )
       
   263         {
       
   264         switch( aRingingType )
       
   265             {
       
   266 			// Fall through
       
   267             case ERingingTypeRinging:
       
   268 			case ERingingTypeSilent:
       
   269                 {
       
   270                 iTonePlayer->SetRepeats(
       
   271                  KMdaRepeatForever, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   272                 break;
       
   273                 }
       
   274             case ERingingTypeAscending:
       
   275                 {
       
   276                 iTonePlayer->SetRepeats(
       
   277                  KMdaRepeatForever, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   278                 TInt volRamp = KAscendingVolumeInterval * iRingingVolume;
       
   279                 iTonePlayer->SetVolumeRamp( TTimeIntervalMicroSeconds( volRamp ) );
       
   280                 break;
       
   281                 }
       
   282 			// Fall through
       
   283             case ERingingTypeRingOnce:
       
   284 			case ERingingTypeBeepOnce:
       
   285                 {
       
   286                 iTonePlayer->SetRepeats(
       
   287                  0, TTimeIntervalMicroSeconds( KToneInterval ) );
       
   288                 break;
       
   289                 }
       
   290 	       default:
       
   291                 {
       
   292                 break;
       
   293                 }
       
   294             }
       
   295         }
       
   296     }
       
   297     
       
   298 // -----------------------------------------------------------------------------
       
   299 // CFLDRingingTonePlayer::ConvertVolume()
       
   300 // (other items were commented in a header).
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 TInt CFLDRingingTonePlayer::ConvertVolume( TInt aVolume )
       
   304     {
       
   305     TInt result( 0 );
       
   306     if ( iAudioPlayerStatus == EAudioPlayerInitialized ||
       
   307         iAudioPlayerStatus == EAudioPlayerPlaying )
       
   308         {
       
   309         result = BaseConvertVolume( aVolume, iAudioPlayer->MaxVolume() );
       
   310         }
       
   311     else if( iTonePlayerStatus == EAudioPlayerInitialized ||
       
   312         iTonePlayerStatus == EAudioPlayerPlaying )
       
   313         {
       
   314         result = BaseConvertVolume( aVolume, iTonePlayer->MaxVolume() );
       
   315         }
       
   316 	//if user has selected silent ringing type, set volume off
       
   317 	if( iRingingType == ERingingTypeSilent )
       
   318 		{
       
   319 		result = 0;
       
   320 		}
       
   321 
       
   322     return result;
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CFLDRingingTonePlayer::MdapcInitComplete()
       
   327 // (other items were commented in a header).
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CFLDRingingTonePlayer::MdapcInitComplete(
       
   331     TInt aError, const TTimeIntervalMicroSeconds& /* aDuration */ )
       
   332     {
       
   333     if ( !aError && iAudioPlayerStatus == EAudioPlayerInitializing )
       
   334         {
       
   335         iAudioPlayerStatus = EAudioPlayerInitialized;
       
   336         DoSetRingingType( iRingingType );
       
   337         iAudioPlayer->SetVolume( ConvertVolume( iRingingVolume ) );
       
   338 		iAudioPlayer->SetPriority( KAudioPriorityPreview,
       
   339             TMdaPriorityPreference( iVibra ? KAudioPrefRingFilePreviewVibra :
       
   340                                              KAudioPrefRingFilePreview ) );
       
   341                                              
       
   342         if ( iRingingType == ERingingTypeBeepOnce )
       
   343             {
       
   344             return; // beeb once not handled here
       
   345             }
       
   346         
       
   347         if( !i3dEffects )
       
   348             {
       
   349             iAudioPlayer->Play();
       
   350          	iAudioPlayerStatus = EAudioPlayerPlaying;
       
   351          	return;
       
   352             }
       
   353 
       
   354         if ( !i3dRingingTonePlugin )
       
   355             {
       
   356             TUid emptyUid = { 0 };
       
   357             TRAPD( err, i3dRingingTonePlugin = C3DRingingToneInterface::NewL( emptyUid ) );
       
   358             if ( err != KErrNone || !i3dRingingTonePlugin )
       
   359                 {
       
   360            	    iAudioPlayer->Play();
       
   361           	    iAudioPlayerStatus = EAudioPlayerPlaying;
       
   362            	    return;
       
   363                 }
       
   364             }
       
   365         if ( i3DEffect != KErrNotFound && i3DEcho != KErrNotFound )
       
   366             {
       
   367             i3dRingingTonePlugin->SetAttr( E3DRTIAttr3DEffect, i3DEffect );
       
   368             i3dRingingTonePlugin->SetAttr( E3DRTIAttr3DEcho, i3DEcho );
       
   369             }
       
   370             
       
   371         i3dRingingTonePlugin->SetAttr( E3DRTIAttrDrmPlayerUtility, iAudioPlayer );
       
   372         TRAP_IGNORE( i3dRingingTonePlugin->PlayL() );
       
   373         iAudioPlayerStatus = EAudioPlayerPlayingWith3DEffect;
       
   374         }
       
   375     else
       
   376         {
       
   377         Cancel();
       
   378         if ( ( aError == KErrNotSupported ) || ( aError == KErrCorrupt ) )
       
   379             {
       
   380             // Don't care about leave, if the note can't be displayed.
       
   381             TRAP_IGNORE( DisplayErrorNoteL() );
       
   382             }
       
   383         }
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CFLDRingingTonePlayer::MdapcPlayComplete()
       
   388 // (other items were commented in a header).
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CFLDRingingTonePlayer::MdapcPlayComplete( TInt aError )
       
   392     {
       
   393     if ( ( aError == KErrNotSupported ) || ( aError == KErrCorrupt ) )
       
   394         {
       
   395         // Don't care about leave, if the note can't be displayed.
       
   396         TRAP_IGNORE( DisplayErrorNoteL() );
       
   397         }
       
   398 
       
   399 	Cancel();
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CFLDRingingTonePlayer::MatoPrepareComplete()
       
   404 // (other items were commented in a header).
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 void CFLDRingingTonePlayer::MatoPrepareComplete( TInt aError )
       
   408     {
       
   409     if ( !aError && iTonePlayerStatus == EAudioPlayerInitializing )
       
   410         {
       
   411         iTonePlayerStatus = EAudioPlayerInitialized;
       
   412         DoSetRingingType( iRingingType );
       
   413         iTonePlayer->SetVolume( ConvertVolume( iRingingVolume ) );
       
   414         iTonePlayer->SetPriority( KAudioPriorityPreview,
       
   415             TMdaPriorityPreference( iVibra ? KAudioPrefRingFilePreviewVibra :
       
   416                                              KAudioPrefRingFilePreview ) );
       
   417         iTonePlayer->Play();
       
   418         iTonePlayerStatus = EAudioPlayerPlaying;
       
   419         }
       
   420     else
       
   421         {
       
   422         Cancel();
       
   423         if ( ( aError == KErrNotSupported ) || ( aError == KErrCorrupt ) )
       
   424             {
       
   425             // Don't care about leave, if the note can't be displayed.
       
   426             TRAP_IGNORE( DisplayErrorNoteL() );
       
   427             }
       
   428         }
       
   429     }
       
   430 
       
   431 // -----------------------------------------------------------------------------
       
   432 // CFLDRingingTonePlayer::MatoPlayComplete()
       
   433 // (other items were commented in a header).
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 void CFLDRingingTonePlayer::MatoPlayComplete( TInt aError )
       
   437     {
       
   438     if ( ( aError == KErrNotSupported ) || ( aError == KErrCorrupt ) )
       
   439         {
       
   440         // Don't care about leave, if the note can't be displayed.
       
   441         TRAP_IGNORE( DisplayErrorNoteL() );
       
   442         }
       
   443 
       
   444 	Cancel();
       
   445     }
       
   446 
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // CFLDRingingTonePlayer::Set3dEffects()
       
   450 // (other items were commented in a header).
       
   451 // -----------------------------------------------------------------------------
       
   452 //
       
   453 void CFLDRingingTonePlayer::Set3dEffects( TBool a3dEffects )
       
   454 	{
       
   455 	const TInt K3DEffectBase = 1000;
       
   456     const TInt K3DEchoBase =   2000;
       
   457     
       
   458 	i3dEffects = a3dEffects;
       
   459 	
       
   460 	if ( a3dEffects >= K3DEffectBase && a3dEffects < K3DEchoBase )
       
   461 	    {
       
   462 	    i3DEffect = a3dEffects - K3DEffectBase;
       
   463 	    return;
       
   464 	    }
       
   465 	
       
   466 	if ( a3dEffects >= K3DEchoBase )
       
   467 	    {
       
   468 	    i3DEcho = a3dEffects - K3DEchoBase;
       
   469 	    return;
       
   470 	    }
       
   471 	}
       
   472 
       
   473 
       
   474 //  End of File