voicerecorder/RecViewSrc/CVRMdaRecorder.cpp
changeset 0 845549f293a7
child 5 35a9006a7fc7
equal deleted inserted replaced
-1:000000000000 0:845549f293a7
       
     1 /*
       
     2 * Copyright (c) 2002 - 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 "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 *     This class acts as a high level wrapper around the media server
       
    16 *     playback and recording functionalities.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CVRSystemEventHandler.h"
       
    23 #include <ctsydomainpskeys.h>
       
    24 
       
    25 #include <e32svr.h>
       
    26 #include <bautils.h>
       
    27 #include <eikenv.h>
       
    28 #include <aknappui.h>
       
    29 #include <e32property.h>
       
    30 #include <AudioPreference.h>
       
    31 #include <aknsoundsystem.h>
       
    32 #include <ScreensaverInternalPSKeys.h>
       
    33 #include <AknGlobalNote.h>
       
    34 #include <StringLoader.h>
       
    35 #include <ErrorUI.h>
       
    36 #include <sysutil.h>
       
    37 
       
    38 #include <featmgr.h> // Feature manager
       
    39 
       
    40 #include <voicerecorder.rsg>
       
    41 #include <VoiceRecorderUID.h>
       
    42 #include "VoiceRecorderPrivatePSKeys.h"
       
    43 #include "CVRMemo.h"
       
    44 #include "MVRObserver.h"
       
    45 #include "VRConsts.h"
       
    46 #include "VRUtils.h"
       
    47 #include "CVRTonePlayer.h"
       
    48 #include "CVRMdaRecorder.h"
       
    49 #include "CVRDiskSpaceWatcher.h"
       
    50 #include "CVRRecViewModel.h"
       
    51 #include <AudioInput.h> 
       
    52 
       
    53 
       
    54 // MACROS
       
    55 #ifdef _DEBUG
       
    56 #define DEB_PRN_0(str) RDebug::Print(str)
       
    57 #else
       
    58 #define DEB_PRN_0(str)
       
    59 #endif
       
    60 
       
    61 // CONSTANTS
       
    62 const TInt KVRMdaRecorderDefaultStep( 5 );
       
    63 const TInt KVRObserverArrayGranularity( 2 );
       
    64 const TInt KVRMaxNoteLabelLength( 256 );
       
    65 
       
    66 
       
    67 // ================= MEMBER FUNCTIONS ========================================
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CVRMdaRecorder::CVRMdaRecorder
       
    71 // 
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 CVRMdaRecorder::CVRMdaRecorder() :
       
    75 	iObservers( KVRObserverArrayGranularity ),
       
    76 	iIsAttachedToMemo( EFalse ),
       
    77 	iMediaServerError( KErrNone ),
       
    78 	iDimLight (EFalse),
       
    79 	iAttachingToMemo(EFalse),
       
    80 	ifInRecording(EFalse),
       
    81 	ifStopCalled(EFalse)
       
    82 	{
       
    83 	}
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CVRMdaRecorder::~CVRMdaRecorder
       
    88 // 
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CVRMdaRecorder::~CVRMdaRecorder()
       
    92 	{
       
    93 
       
    94 	delete iGlobalNote;
       
    95 	delete iAudioInput; 
       
    96 	delete iAudioRecorder;
       
    97 	delete iErrorUI;
       
    98 	delete iTimer;
       
    99 	delete iTonePlayer;
       
   100 	delete iLight;
       
   101 	delete iDiskSpaceWatcher;
       
   102     iObservers.Close();
       
   103     
       
   104     if (iCurrentCallHandler)
       
   105     	{
       
   106     	delete iCurrentCallHandler;
       
   107     	}
       
   108 	}
       
   109 
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CVRMdaRecorder::ConstructL
       
   113 // 
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CVRMdaRecorder::ConstructL()
       
   117 	{
       
   118 	_LIT_SECURITY_POLICY_S0( KVRWritePolicy, KVoiceRecorderAppUID3 );
       
   119 	_LIT_SECURITY_POLICY_PASS( KVRReadPolicy );
       
   120     // Connect to Pub&Sub:
       
   121     // Error ignored, key may be already defined which is ok for us
       
   122 	iPropVRState.Define( KPSUidVoiceRecorder, KVoiceRecorderMode, 0,
       
   123 	                     KVRReadPolicy, KVRWritePolicy );    
       
   124     
       
   125     User::LeaveIfError(
       
   126         iPropVRState.Attach(
       
   127         KPSUidVoiceRecorder, KVoiceRecorderMode ) );
       
   128         
       
   129    	// To allow/don't allow screensaver
       
   130    	// Errors ignored, no actions needed if API is not available
       
   131     iPropScreenSaver.Attach( KPSUidScreenSaver,
       
   132        KScreenSaverAllowScreenSaver );
       
   133 	iAudioRecorder = CMdaAudioRecorderUtility::NewL( *this, NULL,
       
   134 	  KAudioPriorityRecording, TMdaPriorityPreference( 
       
   135                                KAudioPrefRealOneLocalPlayback ) );
       
   136 
       
   137 	iGlobalNote = CAknGlobalNote::NewL();
       
   138 
       
   139 	iErrorUI = CErrorUI::NewL();
       
   140 
       
   141 	iTonePlayer = CVRTonePlayer::NewL();
       
   142 	iTonePlayer->PrepareToneL( EAvkonSIDVoiceRecordingStartTone );
       
   143 	
       
   144 	iLight = CHWRMLight::NewL(this);
       
   145 
       
   146 	// start observe diskdrive
       
   147     RFs& fs( CEikonEnv::Static()->FsSession() );
       
   148  
       
   149     iDiskSpaceWatcher = CVRDiskSpaceWatcher::NewL(*this, fs);	
       
   150 
       
   151 	}
       
   152 
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // CVRMdaRecorder::MoscoStateChangeEvent
       
   156 // 
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void CVRMdaRecorder::MoscoStateChangeEvent( CBase* /*aObject*/,
       
   160 										    TInt aPreviousState,
       
   161 											TInt aCurrentState,
       
   162 											TInt aErrorCode )
       
   163 	{
       
   164 #ifdef _DEBUG
       
   165 	RDebug::Print( _L( "VoiceRecorder: MoscoStateChangeEvent() prev: %d, current: %d, error: %d" ), 
       
   166 			aPreviousState, aCurrentState, aErrorCode );
       
   167 #endif
       
   168 	iMediaServerError = aErrorCode;
       
   169 	
       
   170    	TRAP_IGNORE(CreateAudioInputL());
       
   171    
       
   172 
       
   173 	// Stop the active scheduler wait (if present)
       
   174 	if ( iActiveWait.IsStarted() )
       
   175 		{
       
   176 		iActiveWait.AsyncStop();
       
   177 		}
       
   178 
       
   179 	if (iMediaServerError != KErrNone ) 
       
   180 		{
       
   181         
       
   182         // when mmc card is used and unpluged, this error returned
       
   183         if (iMediaServerError == KErrCorrupt)
       
   184         	{
       
   185         	iAudioRecorder->Close();
       
   186         	dynamic_cast <CVRRecViewModel*>(iAutoStopObserver)->HandleCommandL(EEikCmdExit);
       
   187         	}
       
   188  		
       
   189 		// Media server reports KErrDied if the playing was interrupted
       
   190 		// by some other system sound triggered by for example incoming SMS
       
   191 
       
   192         if (aPreviousState == CMdaAudioClipUtility::ERecording || aPreviousState == CMdaAudioClipUtility::EPlaying )
       
   193         	{ 	
       
   194 
       
   195 			if ( iMediaServerError != KErrDied &&  iMediaServerError != KErrAccessDenied )
       
   196 				{
       
   197 				TBuf< KVRMaxNoteLabelLength > label;
       
   198 
       
   199 				// If showing of error note fails, then.. well it fails
       
   200 				if ( iMediaServerError == KErrNotSupported )
       
   201 					{
       
   202 					CCoeEnv::Static()->
       
   203 					ReadResourceAsDes16( label, R_VR_UNSUPPORTED_INFONOTE_LABEL );
       
   204 				
       
   205 					TRAP_IGNORE( iGlobalNote->
       
   206 					ShowNoteL( EAknGlobalErrorNote, label ) );
       
   207 				
       
   208 					}
       
   209 				else if ( iMediaServerError == KErrCorrupt )
       
   210 					{
       
   211 					CCoeEnv::Static()->
       
   212 					ReadResourceAsDes16( label, R_VR_CORRUPT_INFONOTE_LABEL );
       
   213 				
       
   214 					TRAP_IGNORE( iGlobalNote->ShowNoteL( EAknGlobalErrorNote, 
       
   215 													 label ) );
       
   216 					}
       
   217 
       
   218 				else
       
   219 					{
       
   220 					TRAP_IGNORE( iErrorUI->ShowGlobalErrorNoteL( iMediaServerError ) );
       
   221 					}
       
   222 
       
   223 				}
       
   224 				StopAndNotify();
       
   225         	}
       
   226         
       
   227         //it will happen when the openfile fails
       
   228         if (aPreviousState == CMdaAudioClipUtility::ENotReady && aCurrentState == CMdaAudioClipUtility::ENotReady )
       
   229         	{
       
   230 #ifdef _DEBUG        	
       
   231         	RDebug::Print( _L( "VoiceRecorder: callback stop called" ) );
       
   232 #endif	    	
       
   233 	    	iAudioRecorder->Close();				
       
   234         	}
       
   235         
       
   236 #ifdef _DEBUG
       
   237 		if ( aCurrentState == CMdaAudioClipUtility::ERecording)
       
   238 			{
       
   239 	        RDebug::Print( _L( "VoiceRecorder: ERecording" ) );
       
   240 			}
       
   241 #endif
       
   242 
       
   243 		} // if ( iMediaServerError != KErrNone )
       
   244 
       
   245 	if ( aCurrentState == CMdaAudioClipUtility::ERecording ||
       
   246 		 aCurrentState == CMdaAudioClipUtility::EPlaying )
       
   247 		{
       
   248 	   	// Do not allow screen saver while playing
       
   249    	    // Errors ignored, no actions needed if API is not available	   	
       
   250 	   	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   251 			KScreenSaverAllowScreenSaver, EVRScreenSaverNotAllowed );
       
   252 		}
       
   253 
       
   254 	if ( aCurrentState == CMdaAudioClipUtility::EOpen )
       
   255 		{
       
   256 		switch ( aPreviousState )
       
   257 			{
       
   258 			case CMdaAudioClipUtility::ENotReady:
       
   259 				{
       
   260 				iMemo->UpdateModifiedDate();
       
   261 				if ( iAttachingToMemo )
       
   262 				    {
       
   263 				    iAttachingToMemo = EFalse;
       
   264 				    TRAP_IGNORE( ConfigureMemoL());
       
   265 				    }
       
   266 				break;
       
   267 				}
       
   268 			default:
       
   269 				{
       
   270 				StopAndNotify();
       
   271 				break;
       
   272 				}
       
   273 			}
       
   274 		}
       
   275     DEB_PRN_0( _L( "CVRMdaRecorder::MoscoStateChangeEvent::Exit"));
       
   276 	}
       
   277 
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CVRMdaRecorder::RegisterObserver
       
   281 // 
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CVRMdaRecorder::RegisterObserver( MVRObserver* aObserver )
       
   285 	{
       
   286 	if ( aObserver && iObservers.Find( aObserver ) == KErrNotFound )
       
   287 		{
       
   288 		iObservers.Append( aObserver );
       
   289 		}
       
   290 	}
       
   291 
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // CVRMdaRecorder::UnregisterObserver
       
   295 // 
       
   296 // ---------------------------------------------------------------------------
       
   297 //
       
   298 void CVRMdaRecorder::UnregisterObserver( MVRObserver* aObserver )
       
   299 	{
       
   300 	if ( !aObserver )
       
   301 		{
       
   302 		return;
       
   303 		}
       
   304 		
       
   305 	TInt index( iObservers.Find( aObserver ) );
       
   306 	if ( index != KErrNotFound )
       
   307 		{
       
   308 		iObservers.Remove( index );
       
   309 		}
       
   310 	}
       
   311 
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // CVRMdaRecorder::SetAutoStopObserver
       
   315 // 
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 void CVRMdaRecorder::SetAutoStopObserver( MVRObserver* aObserver )
       
   319 	{
       
   320 	iAutoStopObserver = aObserver;
       
   321 	}
       
   322 
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // CVRMdaRecorder::AttachToMemoL
       
   326 // Opens a memo file.
       
   327 // If the specified file does not exist (yet), it is created. 
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void CVRMdaRecorder::AttachToMemoL( CVRMemo* aMemo )
       
   331 	{
       
   332 #ifdef _DEBUG
       
   333      RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL -enter" ));      	
       
   334 #endif
       
   335 
       
   336      // if error happen, attach will fail 
       
   337      	
       
   338 	if( iIsAttachedToMemo )
       
   339 		{
       
   340 		User::Panic( KVRPanic,EPanicAlreadyAttached );
       
   341 		}
       
   342 
       
   343 	if( !aMemo )
       
   344 		{
       
   345 		User::Panic( KVRPanic, KErrNotFound );
       
   346 		}
       
   347 
       
   348 	iMemo = aMemo;
       
   349 
       
   350     // if the mmc card is full, should not attach to it
       
   351 
       
   352      RFs& fs( CEikonEnv::Static()->FsSession() );
       
   353 
       
   354 
       
   355 // old storage system
       
   356 #ifndef RD_MULTIPLE_DRIVE
       
   357 	if ( iMemo->MemoStore() == EMemoStorePhoneMemory )
       
   358 		{
       
   359 #ifdef  __AAC_ENCODER_PLUGIN
       
   360 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf )) || 
       
   361 		((iMemo->Quality() == EQualityHigh) && SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel )))
       
   362 #else
       
   363         // now AAC format is not enabled
       
   364         if (SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf ))
       
   365 #endif
       
   366         	{
       
   367  			VRUtils::ShowMemoryFullConfirmationQuery();
       
   368 			if(GetInRecordingFlag())
       
   369 				{
       
   370 				SetInRecordingFlag(EFalse);	
       
   371 				}       		
       
   372         	}
       
   373 			
       
   374 		}
       
   375 		
       
   376      // MMC card
       
   377         else
       
   378      	{
       
   379 #ifdef  __AAC_ENCODER_PLUGIN
       
   380 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::MMCSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf )) || 
       
   381 		((iMemo->Quality() == EQualityHigh) && SysUtil::MMCSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel )))
       
   382 #else
       
   383         // now AAC format is not enabled
       
   384         if (SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf ))
       
   385 #endif
       
   386         	{
       
   387 			VRUtils::ShowMemoryFullConfirmationQuery(ETrue);
       
   388 
       
   389 			if(GetInRecordingFlag())
       
   390 				{
       
   391 				SetInRecordingFlag(EFalse);	
       
   392 				}       		
       
   393         	}     		
       
   394      	}
       
   395 
       
   396 // now multiple drives
       
   397 #else
       
   398 
       
   399 #ifdef  __AAC_ENCODER_PLUGIN
       
   400 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf, iMemo->StorageDrive())) ||
       
   401 		((iMemo->Quality() == EQualityHigh) && SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel, iMemo->StorageDrive())))		 
       
   402 #else
       
   403         // now AAC format is not enabled
       
   404         if (SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf, iMemo->StorageDrive()))
       
   405 #endif 
       
   406 
       
   407 			{
       
   408 			//mmc card			
       
   409 			if ( iMemo->StorageDrive() == EDriveF)
       
   410 				{
       
   411  				VRUtils::ShowMemoryFullConfirmationQuery(ETrue);
       
   412 				}
       
   413 			else
       
   414 				{
       
   415 		 		VRUtils::ShowMemoryFullConfirmationQuery();
       
   416 				}
       
   417 			if(GetInRecordingFlag())
       
   418 				{
       
   419 				SetInRecordingFlag(EFalse);	
       
   420 				}			
       
   421 			}
       
   422 
       
   423 
       
   424 #endif
       
   425 	
       
   426 	// opens an existing file or creates new
       
   427 	iAudioRecorder->OpenFileL( iMemo->File() );
       
   428 	
       
   429 	iAttachingToMemo = ETrue;
       
   430 		
       
   431 	if( !iActiveWait.IsStarted() )
       
   432 		{
       
   433 #ifdef _DEBUG
       
   434 		RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL::AWaitStarting..."));
       
   435 #endif
       
   436 		iActiveWait.Start(); // CSI: 10 #
       
   437 
       
   438 #ifdef _DEBUG
       
   439 		RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL::AWResuming...[%d]"), iMediaServerError);
       
   440 #endif
       
   441 		}    
       
   442        
       
   443 	User::LeaveIfError( iMediaServerError );
       
   444 		
       
   445 
       
   446 
       
   447 /*
       
   448 	if( !iActiveWait.IsStarted() )
       
   449 		{
       
   450 		RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL::AWaitStarting..."));
       
   451 		iActiveWait.Start(); // CSI: 10 #
       
   452 		RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL::AWResuming...[%d]"), iMediaServerError);
       
   453 		}    
       
   454     
       
   455 	User::LeaveIfError( iMediaServerError );
       
   456 
       
   457     // Configure bitrate if recording an AMR clip.
       
   458     // Default bitrate is 12200 and it's used if codec doesn't support
       
   459     // the bitrate that is retrieved from Central Repository.
       
   460     if (iMemo->Quality() == EQualityMMSOptimized )
       
   461         {
       
   462         TUint configuredAmrBitrate( VRUtils::AMRBitrateL() );
       
   463         TUint amrBitrate( KVRDefaultAmrBitrate );
       
   464         
       
   465         // Fetch supported bitrates and find out if configured bitrate
       
   466         // is supported
       
   467         RArray<TUint> rates;
       
   468         iAudioRecorder->GetSupportedBitRatesL( rates );        
       
   469         TInt num = rates.Count();
       
   470         if(num > 0)
       
   471         	{
       
   472             rates.Sort();
       
   473             TInt found = rates.Find(configuredAmrBitrate);
       
   474             if (found == KErrNotFound)
       
   475             	{
       
   476             		amrBitrate = rates[num-1];
       
   477             	}
       
   478         	}
       
   479         rates.Reset();  // Clear array      
       
   480         iAudioRecorder->SetDestinationBitRateL( amrBitrate );                  
       
   481         }
       
   482 
       
   483 
       
   484 // added for supporting mp4 format
       
   485 #ifdef __AAC_ENCODER_PLUGIN
       
   486   else if (iMemo->Quality() == EQualityHigh)
       
   487     	{
       
   488 
       
   489          TInt configuredAacSamplerate( VRUtils::AACSamplerateL() );
       
   490          TInt aacSampleRate( KVRDefaultSamplerateAac );
       
   491  
       
   492          TInt configuredAacBitrate( VRUtils::AACBitrateL() );
       
   493          TInt aacBitrate( KVRDefaultBitrateAac );
       
   494 
       
   495          TInt configuredAudioMode( VRUtils::AACAudioModeL() );   
       
   496  
       
   497          // Sample rate
       
   498      	 RArray<TUint> supportedSampleRates;
       
   499 #ifdef _DEBUG
       
   500       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-GetSupportedSampleRatesL -before" ));      	
       
   501 #endif
       
   502          iAudioRecorder->GetSupportedSampleRatesL(supportedSampleRates);        
       
   503 
       
   504 #ifdef _DEBUG
       
   505       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-GetSupportedSampleRatesL -after" ));      	
       
   506 #endif
       
   507 
       
   508         TInt num1 = supportedSampleRates.Count();
       
   509         if(num1 > 0)
       
   510         	{
       
   511             supportedSampleRates.Sort();
       
   512             TInt found1 = supportedSampleRates.Find(configuredAacSamplerate);
       
   513             if (found1 == KErrNotFound)
       
   514             	{
       
   515             		aacSampleRate = supportedSampleRates[num1-1];
       
   516             	}
       
   517         	}
       
   518 
       
   519           supportedSampleRates.Reset();                           
       
   520 #ifdef _DEBUG
       
   521       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-SetDestinationSampleRateL -before, sampleRate is %d"), aacSampleRate);      	
       
   522 #endif
       
   523           iAudioRecorder->SetDestinationSampleRateL(aacSampleRate);
       
   524 
       
   525 #ifdef _DEBUG
       
   526       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-SetDestinationSampleRateL -after" ));      	
       
   527 #endif
       
   528 
       
   529 
       
   530          // Bit reate      
       
   531          RArray<TUint> supportedBitRates;         
       
   532 
       
   533 #ifdef _DEBUG
       
   534       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-GetSupportedBitRatesL -before" ));      	
       
   535 #endif
       
   536          iAudioRecorder->GetSupportedBitRatesL(supportedBitRates);
       
   537 
       
   538 #ifdef _DEBUG
       
   539       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-GetSupportedBitRatesL -after" ));      	
       
   540 #endif
       
   541 
       
   542         TInt num2 = supportedBitRates.Count();
       
   543         if(num2 > 0)
       
   544         	{
       
   545             supportedBitRates.Sort();
       
   546             TInt found2 = supportedBitRates.Find(configuredAacBitrate);
       
   547             if (found2 == KErrNotFound)
       
   548             	{
       
   549             		aacBitrate = supportedBitRates[num2-1];
       
   550             	}
       
   551         	}
       
   552 
       
   553          supportedBitRates.Reset();
       
   554 
       
   555 #ifdef _DEBUG 
       
   556         RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-SetDestinationBitRateL -before, bitrate is %d"), aacBitrate);      	
       
   557 #endif
       
   558          iAudioRecorder->SetDestinationBitRateL(aacBitrate);
       
   559          
       
   560 #ifdef _DEBUG 
       
   561       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-SetDestinationBitRateL -after" ));      	
       
   562 #endif
       
   563          
       
   564          // the audio mode flag                                    
       
   565 
       
   566 #ifdef _DEBUG
       
   567       RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL-GetSupportedNumberOfChannelsL -before" ));      	
       
   568 #endif
       
   569          RArray<TUint> supportedNumberOfChannels;       
       
   570          iAudioRecorder->GetSupportedNumberOfChannelsL(supportedNumberOfChannels); 
       
   571          TInt num3 = supportedNumberOfChannels.Count();
       
   572          if (num3 > 0)
       
   573          	{
       
   574          	supportedNumberOfChannels.Sort();
       
   575             TInt ifFound = supportedNumberOfChannels.Find(configuredAudioMode);
       
   576             if (ifFound == KErrNotFound)
       
   577          	    {
       
   578          	     configuredAudioMode = supportedNumberOfChannels [0];	
       
   579          	    }
       
   580          	}
       
   581          supportedNumberOfChannels.Reset();
       
   582          iAudioRecorder->SetDestinationNumberOfChannelsL(configuredAudioMode);
       
   583         
       
   584     	}
       
   585 #endif
       
   586  	    iIsAttachedToMemo = ETrue;
       
   587 	    ResetPosition();	
       
   588 */
       
   589 
       
   590 #ifdef _DEBUG
       
   591     RDebug::Print( _L( "CVRMdaRecorder::AttachToMemoL -exit" ));      	
       
   592 #endif
       
   593 	}
       
   594 
       
   595 
       
   596 // ---------------------------------------------------------------------------
       
   597 // CVRMdaRecorder::CreateNewMemoL
       
   598 // 
       
   599 // ---------------------------------------------------------------------------
       
   600 //
       
   601 void CVRMdaRecorder::CreateNewMemoL( CVRMemo* aMemo )
       
   602 	{
       
   603 	iMemo = aMemo;
       
   604 	iAudioRecorder->OpenFileL( aMemo->File() );
       
   605 	if ( !iActiveWait.IsStarted() )
       
   606 		{
       
   607 		iActiveWait.Start(); // CSI: 10 #
       
   608 		}    
       
   609 	User::LeaveIfError( iMediaServerError );
       
   610 	}
       
   611 
       
   612 
       
   613 // ---------------------------------------------------------------------------
       
   614 // CVRMdaRecorder::DetachMemo
       
   615 // 
       
   616 // ---------------------------------------------------------------------------
       
   617 //
       
   618 void CVRMdaRecorder::DetachMemo()
       
   619 	{
       
   620 	if ( iIsAttachedToMemo )
       
   621 		{
       
   622 		Stop();
       
   623 		//Need to delete iAudioInput before close!!!
       
   624 	    	DeleteAudioInput();
       
   625 		
       
   626 	    	iAudioRecorder->Close();				
       
   627 
       
   628 		iMemo = NULL;
       
   629 		iIsAttachedToMemo = EFalse;
       
   630 		}
       
   631 	}
       
   632 
       
   633 
       
   634 // ---------------------------------------------------------------------------
       
   635 // CVRMdaRecorder::SetVolume
       
   636 // 
       
   637 // ---------------------------------------------------------------------------
       
   638 //
       
   639 void CVRMdaRecorder::SetVolume( TInt aStep, TInt aMaxSteps )
       
   640 	{
       
   641 	if ( iIsAttachedToMemo )
       
   642 		{
       
   643 		// On device, MaxVolume() typically returns 9.
       
   644 		TInt maxVolume( iAudioRecorder->MaxVolume() );
       
   645 		TInt volume = maxVolume * aStep / aMaxSteps;
       
   646 		
       
   647 		// Just to make sure that volume is between 0 .. MaxVolume
       
   648 		if ( volume < 0)
       
   649 			{
       
   650 			volume = 0;
       
   651 			}
       
   652 		else if ( volume > maxVolume )
       
   653 			{
       
   654 			volume = maxVolume;
       
   655 			}
       
   656 			
       
   657 #ifdef _DEBUG
       
   658 		RDebug::Print( _L( "VoiceRecorder: Set volume: %d, max volume: %d" ),
       
   659 						 volume, maxVolume  );
       
   660 #endif			
       
   661 		iAudioRecorder->SetVolume( volume );
       
   662 		}
       
   663 	}
       
   664 
       
   665 
       
   666 // ---------------------------------------------------------------------------
       
   667 // CVRMdaRecorder::PlayL
       
   668 // 
       
   669 // ---------------------------------------------------------------------------
       
   670 //
       
   671 TInt CVRMdaRecorder::PlayL()
       
   672 	{
       
   673 	__ASSERT_DEBUG( iIsAttachedToMemo, User::Panic( KVRPanic,
       
   674 		EPanicNotAttached ) );
       
   675 
       
   676 	if( iPause )
       
   677 		{
       
   678 		iAudioRecorder->SetPosition( iPosition );
       
   679 		iPosition = 0;
       
   680 		iPause = EFalse;
       
   681 		}
       
   682 
       
   683 	// This line was added to prevent MMF crash, remove it when play works 
       
   684 	// without this
       
   685 	iAudioRecorder->SetPlaybackBalance( KMMFBalanceCenter );
       
   686 	
       
   687 	SetIfStopCalled(EFalse);
       
   688 	iAudioRecorder->PlayL();
       
   689 
       
   690     User::LeaveIfError( iPropVRState.Set( KPSUidVoiceRecorder,
       
   691         KVoiceRecorderMode, EVRPlaying ) );
       
   692 
       
   693    	// Do not allow screen saver while playing
       
   694    	// Errors ignored, no actions needed if API is not available   	
       
   695    	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   696 		KScreenSaverAllowScreenSaver, EVRScreenSaverNotAllowed );
       
   697 
       
   698 	if( !iActiveWait.IsStarted() )
       
   699 		{
       
   700 		iActiveWait.Start(); // CSI: 10 #
       
   701 		}    
       
   702 	StartTimerL();
       
   703 	return iMediaServerError;
       
   704 	}
       
   705 
       
   706 // ---------------------------------------------------------------------------
       
   707 // CVRMdaRecorder::RecordL
       
   708 // Starts the recording of a sound clip. Checks the memory space situation
       
   709 // and plays start tone before starting recording.
       
   710 // ---------------------------------------------------------------------------
       
   711 //
       
   712 TInt CVRMdaRecorder::RecordL()
       
   713 	{
       
   714 	// If we receive record command and file is not opened yet. wait for file
       
   715 	// to be opened and then resume.
       
   716 	if ( !iIsAttachedToMemo && iAttachingToMemo && !iActiveWait.IsStarted() )
       
   717 		{
       
   718 		iActiveWait.Start();
       
   719 		User::LeaveIfError( iMediaServerError );
       
   720 	    }
       
   721 	
       
   722 	__ASSERT_DEBUG( iIsAttachedToMemo, User::Panic( KVRPanic,
       
   723 														EPanicNotAttached ) );
       
   724     TBool wasPaused( iPause );
       
   725 	iPause = EFalse;
       
   726 
       
   727 	// Check if available memory is/goes below critical level 
       
   728 	// in that case record is not performed
       
   729 	// KVRRecStartMemBuf should be size of 60 sec AMR and 5 sec WAV
       
   730 	// New max time estimation for WAV is calculated after 5 secs
       
   731 	RFs& fs( CEikonEnv::Static()->FsSession() );
       
   732 	
       
   733 	TBool critical( EFalse );
       
   734 
       
   735 // the current storage system (phone memory and mmc card)
       
   736 #ifndef RD_MULTIPLE_DRIVE
       
   737 	if ( iMemo->MemoStore() == EMemoStorePhoneMemory )
       
   738 		{
       
   739 #ifdef  __AAC_ENCODER_PLUGIN
       
   740 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf )) || 
       
   741 		((iMemo->Quality() == EQualityHigh) && SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel )))
       
   742 #else
       
   743         // now AAC format is not enabled
       
   744         if (SysUtil::FFSSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf ))
       
   745 #endif 	    
       
   746 			{
       
   747 			VRUtils::ShowMemoryFullConfirmationQuery();
       
   748 			critical = ETrue;
       
   749 			}
       
   750 		}
       
   751 
       
   752      // MMC card
       
   753      else
       
   754      	{
       
   755 #ifdef  __AAC_ENCODER_PLUGIN
       
   756 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::MMCSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf )) || 
       
   757 		((iMemo->Quality() == EQualityHigh) && SysUtil::MMCSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel )))
       
   758 #else
       
   759         // now AAC format is not enabled
       
   760         if (SysUtil::MMCSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf ))
       
   761 #endif
       
   762 		    {
       
   763 			VRUtils::ShowMemoryFullConfirmationQuery( ETrue );
       
   764 			critical = ETrue;
       
   765 			}
       
   766 		
       
   767      	}
       
   768 
       
   769 // now for multiple drives
       
   770 #else
       
   771 #ifdef  __AAC_ENCODER_PLUGIN
       
   772 		if (((iMemo->Quality() != EQualityHigh) && SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf, iMemo->StorageDrive())) ||
       
   773 		((iMemo->Quality() == EQualityHigh) && SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRAACCriticalMemoryLevel, iMemo->StorageDrive())))		 
       
   774 #else
       
   775         // now AAC format is not enabled
       
   776         if (SysUtil::DiskSpaceBelowCriticalLevelL( &fs, KVRRecStartMemBuf, iMemo->StorageDrive()))
       
   777 #endif 
       
   778 			{
       
   779 			if ( iMemo->StorageDrive() == EDriveF)
       
   780 				{
       
   781  				VRUtils::ShowMemoryFullConfirmationQuery(ETrue);
       
   782 				}
       
   783 			else
       
   784 				{
       
   785 		 		VRUtils::ShowMemoryFullConfirmationQuery();
       
   786 				}
       
   787 			critical = ETrue;
       
   788 			}
       
   789 
       
   790 
       
   791 #endif
       
   792 
       
   793 	if( critical )
       
   794 		{
       
   795 		User::Leave( KErrDiskFull );
       
   796 		}
       
   797 
       
   798 
       
   799     if ( !wasPaused )
       
   800         {
       
   801     	iTonePlayer->PlayTone( EAvkonSIDVoiceRecordingStartTone );
       
   802     	iTonePlayer->PrepareToneL( EAvkonSIDVoiceRecordingStopTone );
       
   803         }
       
   804 
       
   805 // start monitor disk space 	 
       
   806 #ifdef  __AAC_ENCODER_PLUGIN
       
   807     if (iMemo->Quality() == EQualityHigh)
       
   808     	{
       
   809         TInt drive (0);
       
   810 #ifndef RD_MULTIPLE_DRIVE
       
   811 	    if ( iMemo->MemoStore() == EMemoStorePhoneMemory )
       
   812 		    {
       
   813 		    drive = EDriveC;	
       
   814 		    }
       
   815         else
       
   816     	    {
       
   817     	    drive = EDriveE;	
       
   818     	    }
       
   819 // multiple drives
       
   820 #else
       
   821         drive = iMemo->StorageDrive();
       
   822 #endif
       
   823 
       
   824         iDiskSpaceWatcher->RequestNotification(KVRAACCriticalMemoryLevel,drive);		
       
   825     	}
       
   826 #endif
       
   827 
       
   828 	// Set recording preference
       
   829 	iAudioRecorder->SetPriority( KAudioPriorityRecording, 
       
   830 				TMdaPriorityPreference( KAudioPrefVoiceRec ) );	
       
   831 	iAudioRecorder->SetGain( iAudioRecorder->MaxGain() );
       
   832 
       
   833 	SetIfStopCalled(EFalse);
       
   834 
       
   835 	iAudioRecorder->RecordL();
       
   836 	
       
   837 	// Sets the indication that recording has started -> 
       
   838 	// Current file's handle can't be reused for next file
       
   839 	iMemo->SetRecorded( ETrue );
       
   840 	
       
   841 			
       
   842     User::LeaveIfError( iPropVRState.Set( KPSUidVoiceRecorder,
       
   843         KVoiceRecorderMode, EVRRecording ) );
       
   844 
       
   845    	// Do not allow screen saver while recording
       
   846    	// Errors ignored, no actions needed if API is not available
       
   847    	iPropScreenSaver.Set( KPSUidScreenSaver, KScreenSaverAllowScreenSaver,
       
   848    		EVRScreenSaverNotAllowed );
       
   849 
       
   850 	if( !iActiveWait.IsStarted() )
       
   851 		{
       
   852 		iActiveWait.Start(); // CSI: 10 #
       
   853 		}    
       
   854 	StartTimerL();
       
   855 
       
   856 	return iMediaServerError;
       
   857 	}
       
   858 
       
   859 
       
   860 // ---------------------------------------------------------------------------
       
   861 // CVRMdaRecorder::Stop
       
   862 // 
       
   863 // ---------------------------------------------------------------------------
       
   864 //
       
   865 void CVRMdaRecorder::Stop()
       
   866 	{
       
   867 	if ( iIsAttachedToMemo )
       
   868 		{
       
   869 		TBool reallyStopping( iPause );		
       
   870 #ifdef _DEBUG
       
   871 		RDebug::Print( _L( "VoiceRecorder: Stop error: %d" ), 
       
   872 				iMediaServerError );
       
   873 #endif
       
   874 		iPause = EFalse;
       
   875 
       
   876 		if( iAudioRecorder->State() == CMdaAudioRecorderUtility::ERecording )
       
   877 			{
       
   878 			reallyStopping = ETrue;
       
   879             
       
   880             // if the dim light is on, turn off it.
       
   881             if(iDimLight)
       
   882 				{
       
   883 	            
       
   884 				iLight->ReleaseLight (CHWRMLight::EPrimaryDisplay);
       
   885 				User::ResetInactivityTime();
       
   886                 iDimLight = EFalse;
       
   887 				}			
       
   888 			
       
   889 			}
       
   890 	    // it will tell VRecorder not pause because it has been stopped.
       
   891 		SetIfStopCalled(ETrue);	
       
   892 		iAudioRecorder->Stop();
       
   893 
       
   894 	    // if error does not happen in recording, change the file to visible 
       
   895 	    iMemo->File().SetAtt( KEntryAttNormal, KEntryAttHidden);	
       
   896 				
       
   897 		iTonePlayer->PlayTone( EAvkonSIDVoiceRecordingStopTone );
       
   898 		iTonePlayer->PrepareTone( EAvkonSIDVoiceRecordingStartTone );			
       
   899 
       
   900 		if( reallyStopping )
       
   901 			{
       
   902 			// Set lower preference to ensure other sounds will 
       
   903 			// be played correctly
       
   904 			iAudioRecorder->SetPriority( KAudioPriorityRecording, 
       
   905 				TMdaPriorityPreference( KAudioPrefRealOneLocalPlayback ) );
       
   906 			}
       
   907 
       
   908         // Error value ignored if PS key is not available
       
   909         iPropVRState.Set( KPSUidVoiceRecorder, KVoiceRecorderMode, EVRIdle );
       
   910         // Used to check the state of a phone call if any
       
   911        
       
   912         if ( iCurrentCallHandler ) delete iCurrentCallHandler;
       
   913         
       
   914     	TRAP_IGNORE( iCurrentCallHandler = CVRSystemEventHandler::NewL() );
       
   915     	TInt currentState( iCurrentCallHandler->StateL(	KPSUidCtsyCallInformation, KCTsyCallState ) );
       
   916     	
       
   917     	// Do not allow screen saver if there is an active phone call
       
   918     	if(currentState == EPSCTsyCallStateRinging)
       
   919     		{
       
   920     	   	// Do not allow screen saver
       
   921        	  // Errors ignored, no actions needed if API is not available	   	
       
   922     	   	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   923         		KScreenSaverAllowScreenSaver, EVRScreenSaverNotAllowed );
       
   924     		}
       
   925     	else
       
   926     		{
       
   927     	   	// Allow screen saver
       
   928        	    // Errors ignored, no actions needed if API is not available	   	
       
   929     	   	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   930         		KScreenSaverAllowScreenSaver, EVRScreenSaverAllowed );
       
   931     		}
       
   932 
       
   933 		StopTimer();
       
   934 		ResetPosition();
       
   935 		iMemo->UpdateModifiedDate();
       
   936 		}
       
   937 	}
       
   938 
       
   939 
       
   940 // ---------------------------------------------------------------------------
       
   941 // CVRMdaRecorder::StopAndNotify
       
   942 // 
       
   943 // ---------------------------------------------------------------------------
       
   944 //
       
   945 void CVRMdaRecorder::StopAndNotify()
       
   946 	{
       
   947 	Stop();
       
   948 	if ( iAutoStopObserver )
       
   949 		{
       
   950 #ifdef _DEBUG
       
   951 		RDebug::Print( _L( "VoiceRecorder: StopAndNotify AutoStopObserver: %d" ),
       
   952 				iMediaServerError );
       
   953 #endif
       
   954 		iAutoStopObserver->Update();
       
   955 #ifdef _DEBUG
       
   956 		RDebug::Print( _L( "VoiceRecorder: StopAndNotify AutoStopObserver: %d" ),
       
   957 				iMediaServerError );
       
   958 #endif
       
   959 		}
       
   960 	}
       
   961 
       
   962 
       
   963 // ---------------------------------------------------------------------------
       
   964 // CVRMdaRecorder::Pause
       
   965 // 
       
   966 // ---------------------------------------------------------------------------
       
   967 void CVRMdaRecorder::Pause()
       
   968 	{
       
   969 	__ASSERT_DEBUG( iIsAttachedToMemo, User::Panic( KVRPanic,
       
   970 		EPanicNotAttached ) );
       
   971 
       
   972 	iPause = ETrue;
       
   973 	iPosition = iAudioRecorder->Position();
       
   974 
       
   975 #ifdef _DEBUG
       
   976 	RDebug::Print( _L("VoiceRecorder: Pause called. Position: %d:%d"),
       
   977 	    I64HIGH( iPosition.Int64() ), I64LOW( iPosition.Int64() )  );
       
   978 #endif
       
   979 
       
   980 	iAudioRecorder->Stop();
       
   981 
       
   982     // Error value ignored if PS key is not available
       
   983     iPropVRState.Set( KPSUidVoiceRecorder, KVoiceRecorderMode, EVRIdle );
       
   984 
       
   985    	// Don't allow screen saver
       
   986    	// Errors ignored, no actions needed if API is not available   	
       
   987    	iPropScreenSaver.Set( KPSUidScreenSaver,
       
   988 		KScreenSaverAllowScreenSaver, EVRScreenSaverAllowed );
       
   989 
       
   990 	StopTimer();
       
   991 	SyncAndNotify();
       
   992 	}
       
   993 
       
   994 
       
   995 // ---------------------------------------------------------------------------
       
   996 // CVRMdaRecorder::ForwardL
       
   997 // 
       
   998 // ---------------------------------------------------------------------------
       
   999 //
       
  1000 TInt CVRMdaRecorder::ForwardL()
       
  1001 	{
       
  1002 	if ( !MovePositionL( KVRMdaRecorderDefaultStep ) )
       
  1003 		{
       
  1004 		return KErrOverflow;
       
  1005 		}
       
  1006 	return iMediaServerError;
       
  1007 	}
       
  1008 
       
  1009 
       
  1010 // ---------------------------------------------------------------------------
       
  1011 // CVRMdaRecorder::RewindL
       
  1012 // 
       
  1013 // ---------------------------------------------------------------------------
       
  1014 //
       
  1015 TInt CVRMdaRecorder::RewindL()
       
  1016 	{
       
  1017 	MovePositionL( -KVRMdaRecorderDefaultStep );
       
  1018 	return iMediaServerError;
       
  1019 	}
       
  1020 
       
  1021 
       
  1022 // ---------------------------------------------------------------------------
       
  1023 // CVRMdaRecorder::CanAcceptCommands
       
  1024 // 
       
  1025 // ---------------------------------------------------------------------------
       
  1026 //
       
  1027 TBool CVRMdaRecorder::CanAcceptCommands() const
       
  1028 	{
       
  1029 	return !iActiveWait.IsStarted();
       
  1030 	}
       
  1031 
       
  1032 
       
  1033 // ---------------------------------------------------------------------------
       
  1034 // CVRMdaRecorder::MovePositionL
       
  1035 // 
       
  1036 // ---------------------------------------------------------------------------
       
  1037 //
       
  1038 TBool CVRMdaRecorder::MovePositionL( TInt aSeconds )
       
  1039 	{
       
  1040 	__ASSERT_DEBUG( iIsAttachedToMemo, User::Panic( KVRPanic,
       
  1041 													EPanicNotAttached ) );
       
  1042 
       
  1043 #ifdef _DEBUG
       
  1044 	RDebug::Print( _L("VoiceRecorder: MovePosition called. Base position: %d:%d"),
       
  1045 	    I64HIGH( iAudioRecorder->Position().Int64() ),
       
  1046         I64LOW( iAudioRecorder->Position().Int64() )  );
       
  1047 #endif
       
  1048 
       
  1049 	TInt64 interval;
       
  1050 	if (iPause)
       
  1051 		{
       
  1052 		interval = iPosition.Int64() + aSeconds * KVRSecondAsMicroSeconds;
       
  1053 		}
       
  1054 	// Not sure if iPosition value is valid other than when paused, so do as
       
  1055 	// previous for all other states:
       
  1056 	else 
       
  1057 		{
       
  1058 		interval = iAudioRecorder->Position().Int64() + 
       
  1059 								   aSeconds * 
       
  1060 								   KVRSecondAsMicroSeconds;
       
  1061 		}
       
  1062 	
       
  1063 	// Truncated to integer values	
       
  1064 	TInt integerInterval( interval / KVRSecondAsMicroSeconds );
       
  1065 	TInt integerDuration( iAudioRecorder->Duration().Int64() / 
       
  1066 						  KVRSecondAsMicroSeconds );	
       
  1067 	if ( interval < 0 )
       
  1068 		{
       
  1069 		interval = 0;
       
  1070 		}
       
  1071 	// if gone past the end or integer values are the same 
       
  1072 	// in duration and position	
       
  1073 	else if ( interval > iAudioRecorder->Duration().Int64() || 
       
  1074 		integerInterval == integerDuration )
       
  1075 		{
       
  1076 		interval = iAudioRecorder->Duration().Int64();
       
  1077 		}
       
  1078 
       
  1079 #ifdef _DEBUG
       
  1080 	RDebug::Print( _L("VoiceRecorder: MovePosition called. Interval: %d:%d"),
       
  1081 	 I64HIGH( interval ), I64LOW( interval )  );
       
  1082 #endif
       
  1083 
       
  1084 	TTimeIntervalMicroSeconds newPosition( interval );
       
  1085 
       
  1086 	if (iPause)
       
  1087 		{
       
  1088 		iPosition = newPosition;
       
  1089 		}
       
  1090 	else if ( iAudioRecorder->State() == CMdaAudioClipUtility::EOpen )
       
  1091 		{
       
  1092 		// not playing, recording or paused.
       
  1093 #ifdef _DEBUG
       
  1094 	RDebug::Print( _L("VoiceRecorder. Idle.: MovePosition called. New Position: %d:%d"),
       
  1095 	 I64HIGH( newPosition.Int64() ), I64LOW( newPosition.Int64() )  );
       
  1096 #endif
       
  1097 		iAudioRecorder->SetPosition( newPosition );
       
  1098 		}
       
  1099 	else
       
  1100 		{
       
  1101 
       
  1102 #ifdef _DEBUG
       
  1103 	RDebug::Print( _L("VoiceRecorder. Playing.: MovePosition called. New Position: %d:%d"),
       
  1104 	 I64HIGH( newPosition.Int64() ), I64LOW( newPosition.Int64() )  );
       
  1105 #endif
       
  1106 
       
  1107 		iAudioRecorder->Stop();
       
  1108 		iAudioRecorder->SetPosition( newPosition );
       
  1109 		iAudioRecorder->PlayL();
       
  1110 		if( !iActiveWait.IsStarted() )
       
  1111 			{
       
  1112 			iActiveWait.Start(); // CSI: 10 #
       
  1113 			}		
       
  1114 		}
       
  1115 	SyncAndNotify();
       
  1116 	return ETrue;
       
  1117 	}
       
  1118 
       
  1119 // ---------------------------------------------------------------------------
       
  1120 // CVRMdaRecorder::ConfigureMemoL()
       
  1121 // 
       
  1122 // ---------------------------------------------------------------------------
       
  1123 //
       
  1124 void CVRMdaRecorder::ConfigureMemoL()
       
  1125     {
       
  1126     DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Enter" ));
       
  1127 
       
  1128     // Configure bitrate if recording an AMR clip.
       
  1129     // Default bitrate is 12200 and it's used if codec doesn't support
       
  1130     // the bitrate that is retrieved from Central Repository.
       
  1131     if (iMemo->Quality() == EQualityMMSOptimized )
       
  1132         {
       
  1133         TUint configuredAmrBitrate( VRUtils::AMRBitrateL() );
       
  1134         TUint amrBitrate( KVRDefaultAmrBitrate );
       
  1135 
       
  1136         // Fetch supported bitrates and find out if configured bitrate
       
  1137         // is supported
       
  1138         RArray<TUint> rates;
       
  1139         iAudioRecorder->GetSupportedBitRatesL( rates );        
       
  1140         TInt num = rates.Count();
       
  1141         if(num > 0)
       
  1142             {
       
  1143             rates.Sort();
       
  1144             TInt found = rates.Find(configuredAmrBitrate);
       
  1145             if (found == KErrNotFound)
       
  1146                 {
       
  1147                 amrBitrate = rates[num-1];
       
  1148                 }
       
  1149             }
       
  1150         rates.Reset();  // Clear array      
       
  1151         iAudioRecorder->SetDestinationBitRateL( amrBitrate );                  
       
  1152 
       
  1153         DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Configured MMSOpt bitrate" ));
       
  1154         }
       
  1155 
       
  1156 
       
  1157     // added for supporting mp4 format
       
  1158 #ifdef __AAC_ENCODER_PLUGIN
       
  1159     else if (iMemo->Quality() == EQualityHigh)
       
  1160     	{
       
  1161         TInt configuredAacSamplerate( VRUtils::AACSamplerateL() );
       
  1162         TInt aacSampleRate( KVRDefaultSamplerateAac );
       
  1163 
       
  1164         TInt configuredAacBitrate( VRUtils::AACBitrateL() );
       
  1165         TInt aacBitrate( KVRDefaultBitrateAac );
       
  1166 
       
  1167         TInt configuredAudioMode( VRUtils::AACAudioModeL() );   
       
  1168 
       
  1169         // Sample rate
       
  1170         RArray<TUint> supportedSampleRates;
       
  1171         iAudioRecorder->GetSupportedSampleRatesL(supportedSampleRates);        
       
  1172 
       
  1173         TInt num1 = supportedSampleRates.Count();
       
  1174         if(num1 > 0)
       
  1175             {
       
  1176             supportedSampleRates.Sort();
       
  1177             TInt found1 = supportedSampleRates.Find(configuredAacSamplerate);
       
  1178             if (found1 == KErrNotFound)
       
  1179                 {
       
  1180                 aacSampleRate = supportedSampleRates[num1-1];
       
  1181                 }
       
  1182             }
       
  1183         supportedSampleRates.Reset();                           
       
  1184         iAudioRecorder->SetDestinationSampleRateL(aacSampleRate);
       
  1185 
       
  1186         DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Configured HiQ sampling rate" ));
       
  1187 
       
  1188         // Bit reate      
       
  1189         RArray<TUint> supportedBitRates;         
       
  1190         iAudioRecorder->GetSupportedBitRatesL(supportedBitRates);
       
  1191         TInt num2 = supportedBitRates.Count();
       
  1192         if(num2 > 0)
       
  1193             {
       
  1194             supportedBitRates.Sort();
       
  1195             TInt found2 = supportedBitRates.Find(configuredAacBitrate);
       
  1196             if (found2 == KErrNotFound)
       
  1197                 {
       
  1198                 aacBitrate = supportedBitRates[num2-1];
       
  1199                 }
       
  1200             }
       
  1201         supportedBitRates.Reset();
       
  1202         iAudioRecorder->SetDestinationBitRateL(aacBitrate);
       
  1203         DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Configured HiQ bit rate" ));
       
  1204 
       
  1205         // the audio mode flag                                    
       
  1206         RArray<TUint> supportedNumberOfChannels;       
       
  1207         iAudioRecorder->GetSupportedNumberOfChannelsL(supportedNumberOfChannels); 
       
  1208         TInt num3 = supportedNumberOfChannels.Count();
       
  1209         if (num3 > 0)
       
  1210             {
       
  1211             supportedNumberOfChannels.Sort();
       
  1212             TInt ifFound = supportedNumberOfChannels.Find(configuredAudioMode);
       
  1213             if (ifFound == KErrNotFound)
       
  1214                 {
       
  1215                 configuredAudioMode = supportedNumberOfChannels [0];	
       
  1216                 }
       
  1217             }
       
  1218         supportedNumberOfChannels.Reset();
       
  1219         iAudioRecorder->SetDestinationNumberOfChannelsL(configuredAudioMode);
       
  1220         
       
  1221         DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Configured HiQ channel" ));        
       
  1222         }
       
  1223 #endif // __AAC_ENCODER_PLUGIN
       
  1224     iIsAttachedToMemo = ETrue;
       
  1225     ResetPosition();
       
  1226     DEB_PRN_0( _L( "CVRMdaRecorder::ConfigureMemoL-Exit" ));
       
  1227     }
       
  1228 
       
  1229 // ---------------------------------------------------------------------------
       
  1230 // CVRMdaRecorder::ResetPosition
       
  1231 // 
       
  1232 // ---------------------------------------------------------------------------
       
  1233 //
       
  1234 void CVRMdaRecorder::ResetPosition()
       
  1235 	{
       
  1236 	if ( iIsAttachedToMemo )
       
  1237 		{
       
  1238 		iAudioRecorder->SetPosition( TTimeIntervalMicroSeconds( 0 ) );
       
  1239 		SyncAndNotify();
       
  1240 		}
       
  1241 	}
       
  1242 
       
  1243 
       
  1244 // ---------------------------------------------------------------------------
       
  1245 // CVRMdaRecorder::StartTimerL
       
  1246 // Create and start a timer with initial delay of 0
       
  1247 // and a frequency of one second 
       
  1248 // ---------------------------------------------------------------------------
       
  1249 //
       
  1250 void CVRMdaRecorder::StartTimerL()
       
  1251 	{
       
  1252 	StopTimer();	// stop if already running
       
  1253 
       
  1254 	TCallBack cb( TimerCallBack, this );
       
  1255 	iTimer = CPeriodic::NewL( 1 );  // priority = EPriorityNormal + 1 = 1
       
  1256 	iTimer->Start( 0, KVRDisplayUpdateDelay, cb );
       
  1257 	}
       
  1258 
       
  1259 
       
  1260 // ---------------------------------------------------------------------------
       
  1261 // CVRMdaRecorder::StopTimer
       
  1262 // Stop the timer by deleting the CPeriodic object 
       
  1263 // ---------------------------------------------------------------------------
       
  1264 //
       
  1265 void CVRMdaRecorder::StopTimer()
       
  1266 	{
       
  1267 	delete iTimer;
       
  1268 	iTimer = NULL;
       
  1269 	}
       
  1270 
       
  1271 
       
  1272 // ---------------------------------------------------------------------------
       
  1273 // CVRMdaRecorder::SyncAndNotify
       
  1274 // 
       
  1275 // ---------------------------------------------------------------------------
       
  1276 //
       
  1277 void CVRMdaRecorder::SyncAndNotify()
       
  1278 	{
       
  1279 	if (iPause)
       
  1280 		{
       
  1281 		iMemo->SetPosition( iPosition );
       
  1282 		}
       
  1283 	// Not sure if iPosition value is valid other than when paused, so do as 
       
  1284 	// previous for all other states:	
       
  1285 	else 
       
  1286 		{
       
  1287 		iMemo->SetPosition( iAudioRecorder->Position() );
       
  1288 		}
       
  1289 
       
  1290 	iMemo->SetDuration( iAudioRecorder->Duration() );
       
  1291 
       
  1292 	for ( int i = 0; i < iObservers.Count(); i++ )
       
  1293 		{
       
  1294 		TVRUpdateCommand command( EVRUpdate );
       
  1295 		if ( ( iMemo->Position().Int64() / KVRSecondAsMicroSeconds ) == 1 )
       
  1296 		    {
       
  1297 		    command = EVRUpdate1Second;
       
  1298 		    }
       
  1299 		iObservers[ i ]->Update( command );
       
  1300 		}
       
  1301 	}
       
  1302 
       
  1303 
       
  1304 // ---------------------------------------------------------------------------
       
  1305 // CVRMdaRecorder::TimerCallBack
       
  1306 // 
       
  1307 // ---------------------------------------------------------------------------
       
  1308 //
       
  1309 TInt CVRMdaRecorder::TimerCallBack( TAny* aMdaRecorder )
       
  1310 	{
       
  1311 	CVRMdaRecorder* recorder = reinterpret_cast< CVRMdaRecorder* >(
       
  1312 															   aMdaRecorder );
       
  1313 	recorder->HandleTick();
       
  1314 	return ETrue;
       
  1315 	}
       
  1316 
       
  1317 
       
  1318 // ---------------------------------------------------------------------------
       
  1319 // CVRMdaRecorder::HandleTick
       
  1320 // 
       
  1321 // ---------------------------------------------------------------------------
       
  1322 //
       
  1323 void CVRMdaRecorder::HandleTick()
       
  1324 	{
       
  1325 	SyncAndNotify();
       
  1326 
       
  1327 	if ( iAudioRecorder->State() == CMdaAudioClipUtility::ERecording )
       
  1328 		{
       
  1329 		
       
  1330 		TTimeIntervalMicroSeconds position( 0 );
       
  1331 		TTimeIntervalMicroSeconds duration( 0 );
       
  1332 		TTimeIntervalMicroSeconds maxDuration( 0 );
       
  1333 		
       
  1334 		position = iMemo->Position();
       
  1335 		duration = iMemo->Duration();
       
  1336 		maxDuration = iMemo->MaxDuration();
       
  1337 		
       
  1338 		// Position value is used for comparison because it gives more accurate 
       
  1339 		// value than Duration value
       
  1340 		if ( position >= maxDuration )
       
  1341 			{
       
  1342 #ifdef _DEBUG
       
  1343 	RDebug::Print( _L("VoiceRecorder: Max duration passed. Position: %d:%d"),
       
  1344 	 I64HIGH( position.Int64() ), I64LOW( position.Int64() )  );
       
  1345 #endif	
       
  1346 
       
  1347 #ifdef _DEBUG
       
  1348 	RDebug::Print( _L("VoiceRecorder: Max duration passed. Duration: %d:%d"),
       
  1349 	 I64HIGH( duration.Int64() ), I64LOW( duration.Int64() )  );
       
  1350 #endif	
       
  1351 
       
  1352 #ifdef _DEBUG
       
  1353 	RDebug::Print( _L("VoiceRecorder: Max duration passed. MaxDuration: %d:%d"),
       
  1354 	 I64HIGH( maxDuration.Int64() ), I64LOW( maxDuration.Int64() )  );
       
  1355 #endif	
       
  1356 			// Note that some data cropping is possible after stopping
       
  1357 			// ->Duration decreases
       
  1358 			StopAndNotify();
       
  1359 			}
       
  1360 		}
       
  1361 	}
       
  1362 
       
  1363 
       
  1364 // ---------------------------------------------------------------------------
       
  1365 // CVRMdaRecorder::SetIhf
       
  1366 // 
       
  1367 // ---------------------------------------------------------------------------
       
  1368 //
       
  1369 TInt CVRMdaRecorder::SetIhf( TBool /*aEnabled*/ )
       
  1370 	{
       
  1371 	return KErrNotSupported;
       
  1372 	}
       
  1373 
       
  1374 
       
  1375 // ---------------------------------------------------------------------------
       
  1376 // CVRMdaRecorder::SetPositionL
       
  1377 // 
       
  1378 // ---------------------------------------------------------------------------
       
  1379 //
       
  1380 void CVRMdaRecorder::SetPositionL( TInt aSeconds )
       
  1381 	{
       
  1382 	__ASSERT_DEBUG( iIsAttachedToMemo, User::Panic( KVRPanic,
       
  1383 											EPanicNotAttached ) );
       
  1384 	
       
  1385 	if( iAudioRecorder->State() == CMdaAudioClipUtility::ERecording ||
       
  1386 		iAudioRecorder->State() == CMdaAudioClipUtility::ENotReady )
       
  1387 		{
       
  1388 		return;
       
  1389 		}
       
  1390 		
       
  1391 	TInt64 interval ( TInt64( aSeconds ) * KVRSecondAsMicroSeconds );
       
  1392 	
       
  1393 	// Truncated to integer values	
       
  1394 	TInt integerInterval( interval / KVRSecondAsMicroSeconds );
       
  1395 	TInt integerDuration( iAudioRecorder->Duration().Int64() / 
       
  1396 										  KVRSecondAsMicroSeconds );	
       
  1397 	
       
  1398 	if ( interval < 0 || integerDuration == 0 )
       
  1399 		{
       
  1400 		interval = 0;
       
  1401 		}
       
  1402 	// if gone past the end or integer values are the same in 
       
  1403 	// duration and position
       
  1404 	else if ( interval > iAudioRecorder->Duration().Int64() || 
       
  1405 		integerInterval == integerDuration )
       
  1406 		{
       
  1407 		interval = iAudioRecorder->Duration().Int64();
       
  1408 		}
       
  1409 
       
  1410 	TTimeIntervalMicroSeconds newPosition( interval );
       
  1411 
       
  1412 	if ( iPause )
       
  1413 		{
       
  1414 		iPosition = newPosition;
       
  1415 		}
       
  1416 	else if ( iAudioRecorder->State() == CMdaAudioClipUtility::EOpen )
       
  1417 		{
       
  1418 		iAudioRecorder->SetPosition( newPosition );
       
  1419 		}
       
  1420 	else
       
  1421 		{
       
  1422 		iAudioRecorder->Stop();
       
  1423 		iAudioRecorder->SetPosition( newPosition );
       
  1424 		iAudioRecorder->PlayL();
       
  1425 		if( !iActiveWait.IsStarted() )
       
  1426 			{
       
  1427 			iActiveWait.Start(); // CSI: 10 #
       
  1428 			}
       
  1429 		}
       
  1430 	SyncAndNotify();	
       
  1431 	}
       
  1432 
       
  1433 // ---------------------------------------------------------
       
  1434 // From class MBacklightControlObserver.
       
  1435 // CNTExternalEventObserver::LightStatusChanged
       
  1436 // ---------------------------------------------------------
       
  1437 //
       
  1438 void CVRMdaRecorder::LightStatusChanged(TInt aTarget, CHWRMLight::TLightStatus aStatus)
       
  1439      {
       
  1440 
       
  1441      if(aTarget == CHWRMLight::EPrimaryDisplayAndKeyboard || aTarget == CHWRMLight::EPrimaryDisplay)
       
  1442      	{
       
  1443         if(aStatus == CHWRMLight::ELightOff)
       
  1444             {
       
  1445  	        if (iAudioRecorder)
       
  1446  	        	{	
       
  1447  	            if (iAudioRecorder->State() == CMdaAudioClipUtility::ERecording ) // set light to dimmed
       
  1448  	                {
       
  1449 
       
  1450                     TRAPD(err, iLight->LightOnL(CHWRMLight::EPrimaryDisplay, KHWRMInfiniteDuration, 10, ETrue));
       
  1451                     if ( err == KErrNone )
       
  1452                        {
       
  1453                        iDimLight = ETrue;
       
  1454                        }
       
  1455 
       
  1456  	                }
       
  1457                	}
       
  1458                	
       
  1459                 return;
       
  1460              }
       
  1461          
       
  1462          if((aStatus == CHWRMLight::ELightOn) && (aTarget == CHWRMLight::EPrimaryKeyboard))
       
  1463          	{
       
  1464              
       
  1465              if (iLight)
       
  1466              	{
       
  1467              		             	
       
  1468                 CHWRMLight::TLightStatus status = iLight->LightStatus(CHWRMLight::EPrimaryDisplay);
       
  1469                 
       
  1470                 if( (status == CHWRMLight::ELightOn)  && iDimLight )
       
  1471                 	{
       
  1472                 	    User::ResetInactivityTime();
       
  1473                 		iDimLight = EFalse;
       
  1474                 	}
       
  1475              	}
       
  1476             return;
       
  1477          	}
       
  1478      
       
  1479      	}
       
  1480      }
       
  1481 
       
  1482 
       
  1483 // ---------------------------------------------------------
       
  1484 // GetInRecordingFlag
       
  1485 // 
       
  1486 // ---------------------------------------------------------
       
  1487 //		
       
  1488 TBool CVRMdaRecorder::GetInRecordingFlag()
       
  1489 	{
       
  1490 		return ifInRecording;
       
  1491 	}
       
  1492 	
       
  1493 	
       
  1494 // ---------------------------------------------------------
       
  1495 // SetInRecordingFlag
       
  1496 // ---------------------------------------------------------
       
  1497 //
       
  1498 void CVRMdaRecorder::SetInRecordingFlag(TBool aFlag)
       
  1499 	{
       
  1500 	ifInRecording = aFlag;	
       
  1501 	}
       
  1502 
       
  1503 
       
  1504 // ---------------------------------------------------------
       
  1505 // GetInRecordingFlag
       
  1506 // 
       
  1507 // ---------------------------------------------------------
       
  1508 //		
       
  1509 TBool CVRMdaRecorder::GetIfStopCalled()
       
  1510 	{
       
  1511 		return ifStopCalled;
       
  1512 	}
       
  1513 	
       
  1514 	
       
  1515 // ---------------------------------------------------------
       
  1516 // SetInRecordingFlag
       
  1517 // ---------------------------------------------------------
       
  1518 //
       
  1519 void CVRMdaRecorder::SetIfStopCalled(TBool aFlag)
       
  1520 	{
       
  1521 	ifStopCalled = aFlag;	
       
  1522 	}
       
  1523 
       
  1524 // ---------------------------------------------------------
       
  1525 // IsAttachedToMemo
       
  1526 // ---------------------------------------------------------
       
  1527 //
       
  1528 TBool CVRMdaRecorder::IsAttachedToMemo()
       
  1529 	{
       
  1530 	return iIsAttachedToMemo;
       
  1531 	}
       
  1532 
       
  1533 
       
  1534 // ---------------------------------------------------------
       
  1535 // CreateAudioInput
       
  1536 // ---------------------------------------------------------
       
  1537 //	
       
  1538 void CVRMdaRecorder::CreateAudioInputL()
       
  1539 	{
       
  1540 	CAudioInput::TAudioInputPreference audioInput = CAudioInput::EDefaultMic;
       
  1541   
       
  1542 	if ((iAudioRecorder->State() == CMdaAudioClipUtility::EOpen)&&
       
  1543 	    (!iAudioInput))
       
  1544     	{  
       
  1545         iAudioInput = CAudioInput::NewL(*iAudioRecorder);        
       
  1546         SetAudioInputL(audioInput); 
       
  1547         iCurrentAudioInput = audioInput;                                    
       
  1548     	}
       
  1549     
       
  1550 	}
       
  1551 // ---------------------------------------------------------
       
  1552 // SetAudioInputL
       
  1553 // ---------------------------------------------------------
       
  1554 //
       
  1555 void CVRMdaRecorder::SetAudioInputL(CAudioInput::TAudioInputPreference aAudioInput)
       
  1556 	{
       
  1557 	if((iAudioInput)&&(iCurrentAudioInput!=aAudioInput))
       
  1558     	{	
       
  1559         RArray<CAudioInput::TAudioInputPreference> inputArray(4);
       
  1560         CleanupClosePushL( inputArray );
       
  1561         inputArray.Append( aAudioInput );
       
  1562         iAudioInput->SetAudioInputL( inputArray.Array() );   
       
  1563         CleanupStack::PopAndDestroy( &inputArray );
       
  1564         iCurrentAudioInput = aAudioInput;
       
  1565     	}
       
  1566     	
       
  1567 	}
       
  1568 	
       
  1569 // ---------------------------------------------------------
       
  1570 // DeleteAudioInput
       
  1571 // ---------------------------------------------------------
       
  1572 //	
       
  1573 void CVRMdaRecorder::DeleteAudioInput()
       
  1574 	{
       
  1575 	delete iAudioInput;
       
  1576 	iAudioInput = NULL;
       
  1577 	}
       
  1578 	
       
  1579 // ---------------------------------------------------------
       
  1580 // ConfigSampleRateOfVoiceCallL
       
  1581 // ---------------------------------------------------------
       
  1582 //	
       
  1583 void CVRMdaRecorder::ConfigSampleRateOfVoiceCallL()
       
  1584 	{
       
  1585 	// Sample rate
       
  1586 	TUint sampleRates = iAudioRecorder->DestinationSampleRateL();
       
  1587 	TInt configuredAacSamplerate( VRUtils::AACSamplerateL() );
       
  1588 	if(sampleRates == configuredAacSamplerate)
       
  1589 		{
       
  1590 		return;
       
  1591 		}
       
  1592 	
       
  1593 	if (iMemo->Quality() == EQualityHigh)
       
  1594 		{
       
  1595 		TInt aacSampleRate( KVRDefaultSamplerateAac );
       
  1596 		// Sample rate
       
  1597 		RArray<TUint> supportedSampleRates;
       
  1598 		iAudioRecorder->GetSupportedSampleRatesL(supportedSampleRates);
       
  1599 		TInt num1 = supportedSampleRates.Count();
       
  1600 		if(num1 > 0)
       
  1601 			{
       
  1602 			supportedSampleRates.Sort();
       
  1603 			TInt found1 = supportedSampleRates.Find(configuredAacSamplerate);
       
  1604 			if (found1 == KErrNotFound)
       
  1605 				{
       
  1606 				aacSampleRate = supportedSampleRates[num1-1];
       
  1607 				}
       
  1608 			}
       
  1609 		supportedSampleRates.Reset(); 
       
  1610 		iAudioRecorder->SetDestinationSampleRateL(aacSampleRate);
       
  1611 		}
       
  1612 	}
       
  1613 
       
  1614 // End of file