mobilemessaging/audiomsg/src/audiomessagerecorder.cpp
branchRCL_3
changeset 27 7fdbb852d323
equal deleted inserted replaced
26:ebe688cedc25 27:7fdbb852d323
       
     1 /*
       
     2 * Copyright (c) 2005-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:   This class acts as a high level wrapper around the 
       
    15 *                CMdaAudioRecorderUtility playback and recording 
       
    16 *                functionalities. Functons for output route handling.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <AudioPreference.h>
       
    23 #include <AudioOutput.h>
       
    24 #include <AudioInput.h>
       
    25 #include <avkon.hrh>
       
    26 #include "audiomessagebeep.h"
       
    27 #include "audiomessagerecorder.h"
       
    28 #include "amsvoiceobserver.h"
       
    29 #include "AudioMessageLogging.h"
       
    30 
       
    31 const TInt KAmsVolumeSteps = 10;
       
    32 
       
    33 const TUint KAmsDefaultAmrBitrate( 12200 );  //Voice Recorder uses this value for MMS
       
    34                                              //optimized voice
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------
       
    39 // CAudioMessageRecorder::NewL
       
    40 // ---------------------------------------------------------
       
    41 //
       
    42 CAudioMessageRecorder* CAudioMessageRecorder::NewL( const RFile& aFile,  const TBool aAudioOutput )
       
    43 	{
       
    44 	CAudioMessageRecorder * recorder = new( ELeave ) CAudioMessageRecorder( );
       
    45 	CleanupStack::PushL( recorder );
       
    46 	recorder->ConstructL( aFile, aAudioOutput );
       
    47 	CleanupStack::Pop( recorder );
       
    48 	return recorder;	
       
    49 	}
       
    50 
       
    51 // ---------------------------------------------------------
       
    52 // CAudioMessageRecorder::ConstructL
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 void CAudioMessageRecorder::ConstructL( const RFile& aFile, const TBool aAudioOutput )
       
    56 	{
       
    57 AMSLOGGER_WRITE( "CAudioMessageRecorder::ConstructL" );	
       
    58     iRecUtility = CMdaAudioRecorderUtility::NewL(
       
    59        	*this,
       
    60         NULL,
       
    61         KAudioPriorityRecording,
       
    62         TMdaPriorityPreference( KAudioPrefRealOneLocalPlayback ) );
       
    63 
       
    64 	iRecUtility->OpenFileL( aFile );
       
    65 	iBeepPlayer = CAudioMessageBeep::NewL();
       
    66 	iBeepPlayer->PrepareToneL( EAvkonSIDVoiceRecordingStartTone );
       
    67 	iPause = EFalse;
       
    68 	iSupportAudioOutput = aAudioOutput;
       
    69 	iTimer = CPeriodic::NewL( EPriorityNormal + 1 ); 
       
    70 	
       
    71    	if ( !iActiveWait.IsStarted() )
       
    72 		{
       
    73 		iActiveWait.Start();
       
    74 		}
       
    75 AMSLOGGER_WRITE( "CAudioMessageRecorder::ConstructL DONE!" );		
       
    76 	}
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 // CAudioMessageRecorder::MoscoStateChangeEvent
       
    81 // ---------------------------------------------------------
       
    82 void CAudioMessageRecorder::MoscoStateChangeEvent( CBase* /*aObject*/, TInt aPreviousState, 
       
    83 	TInt aCurrentState, TInt  aErrorCode)
       
    84 	{
       
    85 	AMSLOGGER_WRITEF( _L( "CAudioMessageRecorder: MoscoStateChangeEvent() prev: %d, current: %d, error: %d" ), aPreviousState,
       
    86 			aCurrentState, aErrorCode );
       
    87 
       
    88 			
       
    89 	if ( iActiveWait.IsStarted() )
       
    90 		{
       
    91 		iActiveWait.AsyncStop();
       
    92 		}
       
    93 
       
    94     iErrorCode = aErrorCode;
       
    95     
       
    96     //Let's not go here in case there has been an error in opening the file.
       
    97     //That's why the error code has been checked here
       
    98     if( aCurrentState == CMdaAudioClipUtility::EOpen 
       
    99          && aPreviousState == CMdaAudioClipUtility::ENotReady 
       
   100          && !iAudioInput
       
   101          && iErrorCode == KErrNone )
       
   102         {
       
   103         TRAPD(error, iAudioInput = CAudioInput::NewL( *iRecUtility ); 
       
   104             ResetAudioInputL();
       
   105             iRecUtility->SetDestinationBitRateL( KAmsDefaultAmrBitrate ));
       
   106         
       
   107         //KErrNotSupported is allowed because it works OK even if CAudioOutput
       
   108         //is not supported
       
   109         if( error != KErrNone && error != KErrNotSupported )
       
   110             {
       
   111             iErrorCode = error;
       
   112             }
       
   113         
       
   114         }
       
   115     else if ( aCurrentState == CMdaAudioClipUtility::EOpen && aPreviousState ==
       
   116     	CMdaAudioClipUtility::EPlaying )
       
   117    		{ 
       
   118    		iObserver->PlayingStops();   
       
   119  		}   
       
   120     else if ( aCurrentState == CMdaAudioClipUtility::EOpen && aPreviousState == 
       
   121     	CMdaAudioClipUtility::ERecording )
       
   122    		{ 
       
   123    		if ( !iPause )
       
   124 	   		{
       
   125 	   		iObserver->PlayingStops();   
       
   126 	   		}
       
   127    		}
       
   128  	 else if ( aCurrentState == CMdaAudioClipUtility::ERecording && 
       
   129  	    aPreviousState == CMdaAudioClipUtility::EOpen )
       
   130  	    {
       
   131  	    iBeepPlayer->PrepareTone( EAvkonSIDVoiceRecordingStopTone );    
       
   132  	    }	  
       
   133 	}
       
   134 
       
   135 // ---------------------------------------------------------
       
   136 // CAudioMessageRecorder::SetVol
       
   137 // ---------------------------------------------------------
       
   138 //
       
   139 void CAudioMessageRecorder::SetVol( TInt aVal )
       
   140 	{
       
   141 	TInt max = iRecUtility->MaxVolume();
       
   142 	TInt step = max / KAmsVolumeSteps;
       
   143 	iCurrentVolume = aVal * step;
       
   144 	
       
   145     if ( iCurrentVolume >= max )
       
   146         iRecUtility->SetVolume( max );
       
   147     else if ( iCurrentVolume <= 0)
       
   148         iRecUtility->SetVolume( 0 );
       
   149     else
       
   150         iRecUtility->SetVolume( iCurrentVolume );
       
   151 
       
   152 	}
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CAudioMessageRecorder::StartPlayingL
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 void CAudioMessageRecorder::StartPlayingL()
       
   159 	{  
       
   160 	if( iErrorCode != KErrNone )
       
   161 	    {
       
   162 AMSLOGGER_WRITEF( _L( "CAudioMessageRecorder: StartPlayingL(): Leave ( %d ) occured." ), iErrorCode );
       
   163 	    User::Leave( iErrorCode);
       
   164 	    return;
       
   165 	    }	 
       
   166 	iRecUtility->SetPlaybackBalance( KMMFBalanceCenter );
       
   167 	iRecUtility->SetVolume( iCurrentVolume ) ;
       
   168     iRecUtility->PlayL();
       
   169  	if ( !iActiveWait.IsStarted() )
       
   170 		{
       
   171 		iActiveWait.Start();
       
   172 		}
       
   173 	StartTimer();
       
   174 	}
       
   175 
       
   176 // ---------------------------------------------------------
       
   177 // CAudioMessageRecorder::StartRecordingL
       
   178 // ---------------------------------------------------------
       
   179 //
       
   180 void CAudioMessageRecorder::StartRecordingL()
       
   181 	{ 
       
   182 	if( iErrorCode != KErrNone )
       
   183 	    {
       
   184 AMSLOGGER_WRITEF( _L( "CAudioMessageRecorder: StartRecordingL(): Leave ( %d ) occured." ), iErrorCode );
       
   185 	    User::Leave( iErrorCode);
       
   186 	    return;
       
   187 	    }
       
   188 	
       
   189     iRecUtility->SetAudioDeviceMode( CMdaAudioRecorderUtility::ELocal );
       
   190     iRecUtility->SetPriority( KAudioPriorityRecording, 
       
   191         TMdaPriorityPreference( KAudioPrefVoiceRec ) );	
       
   192     iRecUtility->SetGain( iRecUtility->MaxGain() );
       
   193     iRecUtility->SetPosition( TTimeIntervalMicroSeconds( 0 ) );
       
   194 
       
   195 	iBeepPlayer->PlayTone( EAvkonSIDVoiceRecordingStartTone );
       
   196 	//iBeepPlayer->PrepareToneL( EAvkonSIDVoiceRecordingStopTone );    
       
   197 
       
   198     iRecUtility->RecordL();
       
   199  	if ( !iActiveWait.IsStarted() )
       
   200 		{
       
   201 		iActiveWait.Start();
       
   202 		}
       
   203 	StartTimer();
       
   204 	}
       
   205 
       
   206 // ---------------------------------------------------------
       
   207 // CAudioMessageRecorder::Pause
       
   208 // ---------------------------------------------------------
       
   209 //
       
   210 void CAudioMessageRecorder::Pause()
       
   211 	{
       
   212 	iPause = ETrue;
       
   213     iRecUtility->Stop();
       
   214     StopTimer();
       
   215 	iRecUtility->SetPosition( TTimeIntervalMicroSeconds( 0 ) );
       
   216     iRecUtility->SetPriority( KAudioPriorityRecording, 
       
   217     	TMdaPriorityPreference( KAudioPrefRealOneLocalPlayback ) );	
       
   218 	}
       
   219 
       
   220 // ---------------------------------------------------------
       
   221 // CAudioMessageRecorder::Stop
       
   222 // ---------------------------------------------------------
       
   223 //
       
   224 void CAudioMessageRecorder::StopL()
       
   225 	{
       
   226 	iPause = EFalse;
       
   227     iRecUtility->Stop();
       
   228     StopTimer();
       
   229 	iRecUtility->SetPosition( TTimeIntervalMicroSeconds( 0 ) );
       
   230     iRecUtility->SetPriority( KAudioPriorityRecording, 
       
   231     	TMdaPriorityPreference( KAudioPrefRealOneLocalPlayback) );	
       
   232 	iBeepPlayer->PlayTone( EAvkonSIDVoiceRecordingStopTone );
       
   233 	//iBeepPlayer->PrepareToneL( EAvkonSIDVoiceRecordingStartTone );		
       
   234 	}
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // CAudioMessageRecorder::ContinueL
       
   238 // ---------------------------------------------------------
       
   239 //
       
   240 void CAudioMessageRecorder::ContinueL()
       
   241 	{
       
   242 	if( iErrorCode != KErrNone )
       
   243 	    {
       
   244 AMSLOGGER_WRITEF( _L( "CAudioMessageRecorder: ContinueL(): Leave ( %d ) occured." ), iErrorCode );
       
   245 	    User::Leave( iErrorCode);
       
   246 	    return;
       
   247 	    }
       
   248 	
       
   249 	iPause = EFalse;
       
   250 	iRecUtility->SetPriority( KAudioPriorityRecording, TMdaPriorityPreference( KAudioPrefVoiceRec ) );
       
   251         iRecUtility->RecordL(); 
       
   252  	if ( !iActiveWait.IsStarted() )
       
   253 		{
       
   254 		iActiveWait.Start();
       
   255 		}
       
   256 	StartTimer();
       
   257 	}
       
   258 
       
   259 // ---------------------------------------------------------
       
   260 // CAudioMessageRecorder::Constructor
       
   261 // ---------------------------------------------------------
       
   262 //
       
   263 CAudioMessageRecorder::CAudioMessageRecorder() 
       
   264 	{
       
   265   	}
       
   266 
       
   267 // ---------------------------------------------------------
       
   268 // CAudioMessageRecorder::Destructor
       
   269 // ---------------------------------------------------------
       
   270 //
       
   271 CAudioMessageRecorder::~CAudioMessageRecorder()
       
   272 	{ 
       
   273 	delete iAudioOutput;
       
   274   	delete iAudioInput;
       
   275     delete iRecUtility;
       
   276 	delete iTimer;
       
   277    	delete iBeepPlayer;
       
   278 	}
       
   279 
       
   280 // ---------------------------------------------------------
       
   281 // CAudioMessageRecorder::SetObserver
       
   282 // ---------------------------------------------------------
       
   283 //
       
   284 void CAudioMessageRecorder::SetObserver(MAmsVoiceObserver* aObserver )
       
   285 	{
       
   286 	iObserver = aObserver;
       
   287 	}
       
   288 	
       
   289 // ---------------------------------------------------------
       
   290 // CAudioMessageRecorder::StartTimer
       
   291 // ---------------------------------------------------------
       
   292 //
       
   293 void CAudioMessageRecorder::StartTimer()
       
   294 	{
       
   295 	StopTimer();	// stop if already running
       
   296 	TCallBack cb( TimerCallBack, this );
       
   297 	iTimer->Start( 0, KAmsDisplayUpdateDelay, cb );
       
   298 	}
       
   299 
       
   300 
       
   301 // ---------------------------------------------------------
       
   302 // CAudioMessageRecorder::StopTimer
       
   303 // ---------------------------------------------------------
       
   304 //
       
   305 void CAudioMessageRecorder::StopTimer()
       
   306 	{
       
   307 	iTimer->Cancel();
       
   308 	}
       
   309 
       
   310 // ---------------------------------------------------------
       
   311 // CAudioMessageRecorder::TimerCallBack
       
   312 // ---------------------------------------------------------
       
   313 //
       
   314 TInt CAudioMessageRecorder::TimerCallBack( TAny* aRecorder )
       
   315 	{
       
   316 	CAudioMessageRecorder* recorder = reinterpret_cast< CAudioMessageRecorder* >( aRecorder );
       
   317 	recorder->HandleTick();
       
   318 	return ETrue;
       
   319 	}
       
   320 
       
   321 // ---------------------------------------------------------
       
   322 // CAudioMessageRecorder::HandleTick
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 void CAudioMessageRecorder::HandleTick()
       
   326 	{	
       
   327 	TRAPD( error, iObserver->UpdateL( iRecUtility->Position(), iErrorCode ) );
       
   328 	if ( error != KErrNone )
       
   329 	    {
       
   330 	    iErrorCode = error;
       
   331 	    }
       
   332 	}
       
   333 
       
   334 // ---------------------------------------------------------
       
   335 // CAudioMessageRecorder::GetPosition
       
   336 // ---------------------------------------------------------
       
   337 //
       
   338 TTimeIntervalMicroSeconds CAudioMessageRecorder::GetPosition()
       
   339 	{
       
   340 	return iRecUtility->Position();
       
   341 	}
       
   342 
       
   343 // ---------------------------------------------------------
       
   344 // CAudioMessageRecorder::GetDuration
       
   345 // ---------------------------------------------------------
       
   346 //
       
   347 TTimeIntervalMicroSeconds CAudioMessageRecorder::GetDuration()
       
   348 	{
       
   349 	return iRecUtility->Duration();
       
   350 	}
       
   351 	
       
   352 // ---------------------------------------------------------
       
   353 // CAudioMessageRecorder::SetAudioOutput
       
   354 // ---------------------------------------------------------
       
   355 //
       
   356 TInt CAudioMessageRecorder::SetAudioOutput( TBool aOutputPublic )
       
   357 	{ 
       
   358 	TInt err ( KErrNone );
       
   359 	CAudioOutput::TAudioOutputPreference outputPckg;
       
   360 
       
   361 	if ( iAudioOutput )
       
   362 		{
       
   363 		if ( aOutputPublic )
       
   364 			{
       
   365 			outputPckg = CAudioOutput::EPublic;
       
   366 			}
       
   367 		else
       
   368 			{
       
   369 			outputPckg = CAudioOutput::EPrivate;
       
   370 			}
       
   371 		TRAP( err, iAudioOutput->SetAudioOutputL( outputPckg ) );
       
   372 		}
       
   373 	return err;
       
   374 	}
       
   375 	
       
   376 // ---------------------------------------------------------
       
   377 // CAudioMessageRecorder::ResetAudioInputL
       
   378 // ---------------------------------------------------------
       
   379 //
       
   380 void CAudioMessageRecorder::ResetAudioInputL(  )
       
   381 	{ 
       
   382     if( iAudioInput )
       
   383         {
       
   384 AMSLOGGER_WRITE( "CAudioMessageRecorder::ResetAudioInput, iAudioInput EXISTS" );        
       
   385         RArray<CAudioInput::TAudioInputPreference> inputArray(4);
       
   386         CleanupClosePushL( inputArray );
       
   387         inputArray.Append( CAudioInput::EDefaultMic );
       
   388         iAudioInput->SetAudioInputL( inputArray.Array() );   
       
   389         CleanupStack::PopAndDestroy( &inputArray );
       
   390         }
       
   391 #ifdef _DEBUG	
       
   392     else
       
   393         {
       
   394 AMSLOGGER_WRITE( "CAudioMessageRecorder::ResetAudioInput, iAudioInput == NULL!" );
       
   395         }
       
   396 #endif
       
   397 	}
       
   398 
       
   399 // ---------------------------------------------------------
       
   400 // CAudioMessageRecorder::GetCurrentVolume
       
   401 // ---------------------------------------------------------
       
   402 //
       
   403 TInt CAudioMessageRecorder::GetCurrentVolume()
       
   404 	{
       
   405 	TInt current;
       
   406 	iRecUtility->GetVolume( current );
       
   407 	TInt stepsize = iRecUtility->MaxVolume() / KAmsVolumeSteps;
       
   408 	return ( current / stepsize );	
       
   409 	}
       
   410 
       
   411 // ---------------------------------------------------------
       
   412 // CAudioMessageRecorder::GetBitRateL
       
   413 // ---------------------------------------------------------
       
   414 //
       
   415 TUint CAudioMessageRecorder::GetBitRateL()
       
   416 	{
       
   417 	if( iErrorCode != KErrNone )
       
   418 	    {
       
   419 AMSLOGGER_WRITEF( _L( "CAudioMessageRecorder::GetBitRateL(): Leave ( %d ) occured." ), iErrorCode );
       
   420 	    User::Leave( iErrorCode);
       
   421 	    return 0;
       
   422 	    }	
       
   423 	return iRecUtility->DestinationBitRateL();
       
   424 	}
       
   425 
       
   426 // ---------------------------------------------------------
       
   427 // CAudioMessageRecorder::IncrementVol
       
   428 // ---------------------------------------------------------
       
   429 //
       
   430 void CAudioMessageRecorder::IncrementVol()
       
   431     {
       
   432     TInt maxVolume ( iRecUtility->MaxVolume() );
       
   433     TInt currentVolume ( 0 );
       
   434     iRecUtility->GetVolume( currentVolume );
       
   435     TInt volumeValue = currentVolume + (maxVolume /KAmsVolumeSteps);
       
   436     
       
   437     if ( volumeValue >= maxVolume )
       
   438         iRecUtility->SetVolume( maxVolume );
       
   439     else
       
   440         iRecUtility->SetVolume( volumeValue );
       
   441     
       
   442     }
       
   443 
       
   444 // ---------------------------------------------------------
       
   445 // CAudioMessageRecorder::DecrementVol
       
   446 // ---------------------------------------------------------
       
   447 //
       
   448 void CAudioMessageRecorder::DecrementVol()
       
   449     {
       
   450     TInt maxVolume ( iRecUtility->MaxVolume() );
       
   451     TInt currentVolume ( 0 );
       
   452     iRecUtility->GetVolume( currentVolume );
       
   453     TInt volumeValue = currentVolume - (maxVolume/KAmsVolumeSteps);
       
   454     
       
   455     if ( volumeValue <= 0 )
       
   456         iRecUtility->SetVolume( 0 );
       
   457     else
       
   458         iRecUtility->SetVolume( volumeValue );
       
   459     }
       
   460     
       
   461 // ---------------------------------------------------------
       
   462 // CAudioMessageRecorder::InitializeAudioRoutingL
       
   463 // ---------------------------------------------------------
       
   464 //
       
   465 void CAudioMessageRecorder::InitializeAudioRoutingL()
       
   466 	{
       
   467 	if ( iSupportAudioOutput )
       
   468 		{
       
   469 		if ( !iAudioOutput )
       
   470 			{
       
   471 		    TRAPD( err, iAudioOutput = CAudioOutput::NewL( *iRecUtility, EFalse ) );
       
   472 			}
       
   473 		}
       
   474 	}
       
   475