devsound/a3fdevsound/src/devsoundadaptor/cdevaudiocontrol.cpp
changeset 0 40261b775718
child 7 94dbab0a2133
child 13 efebd1779a59
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 #include "cdevaudiocontrol.h"
       
    19 
       
    20 #include <a3f/audioprocessingunittypeuids.h>
       
    21 #include <mmf/server/mmfswcodecwrappercustominterfacesuids.hrh>
       
    22 #include <a3f/maudiocontext.h>
       
    23 #include <a3f/maudiostream.h>
       
    24 #include <a3f/maudiocodec.h>
       
    25 #include <a3f/maudiogaincontrol.h>
       
    26 
       
    27 
       
    28 const TInt KMicroSecondsInSecond = 1000000;
       
    29 const TInt KDefaultBufferSize = 4096;
       
    30 
       
    31 const TUint KMaxCallbacksExpected = 2;
       
    32 const TUint KDefaultSampleRate = 8000;
       
    33 const TUid KDefaultMode = {KA3FModeMonoValue};
       
    34 
       
    35 class TSampleRateTableEntry
       
    36 	{
       
    37 public:
       
    38 	TInt		iSampleRateValue;
       
    39 	TMMFSampleRate iSampleRate;
       
    40 	};
       
    41 
       
    42 const TSampleRateTableEntry KRateTableLookup[] = { 
       
    43 							{ 8000, EMMFSampleRate8000Hz },
       
    44 							{ 11025, EMMFSampleRate11025Hz },
       
    45 							{ 12000, EMMFSampleRate12000Hz },
       
    46 							{ 16000, EMMFSampleRate16000Hz },
       
    47 							{ 22050, EMMFSampleRate22050Hz },
       
    48 							{ 24000, EMMFSampleRate24000Hz },
       
    49 							{ 32000, EMMFSampleRate32000Hz },
       
    50 							{ 44100, EMMFSampleRate44100Hz },
       
    51 							{ 48000, EMMFSampleRate48000Hz },
       
    52 							{ 64000, EMMFSampleRate64000Hz },
       
    53 							{ 88200, EMMFSampleRate88200Hz },
       
    54 							{ 96000, EMMFSampleRate96000Hz },
       
    55 						};
       
    56 const TInt KMaxSampleRateIndex = 11; // must agree with length of table
       
    57 
       
    58 class TAudioModeTableEntry
       
    59 	{
       
    60 public:
       
    61 	TMMFMonoStereo	iAudioModeValue;
       
    62 	TUid			iAudioMode;
       
    63 	};
       
    64 
       
    65 const TAudioModeTableEntry KModeTableLookup[] = {
       
    66 							{ EMMFMono, {KA3FModeMonoValue} },
       
    67 							{ EMMFStereo, {KA3FModeStereoNonInterleavedValue} },
       
    68 							};
       
    69 
       
    70 const TInt KMaxModeIndex = 1; // must agree with length of table
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Default constructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CDevAudioControl::CDevAudioControl()
       
    77 	{
       
    78 	TRACE_CREATE();
       
    79 	DP_CONTEXT(CDevAudioControl::CDevAudioControl *CD1*, CtxDevSound, DPLOCAL);
       
    80 	DP_IN();
       
    81 
       
    82 	// Local cache default values
       
    83 	iCurrentSampleRate = KDefaultSampleRate;
       
    84 	iCurrentMode = KDefaultMode; 
       
    85 	iOutstandingCallbacks = KMaxCallbacksExpected; //by default we expect 2 callbacks for capabilities.
       
    86 	iCallbackFromAdaptor = KCallbackNone;
       
    87 	
       
    88 	DP_OUT();
       
    89 	}
       
    90 
       
    91 
       
    92 void CDevAudioControl::ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
       
    93 	{
       
    94 	TRACE_CREATE();
       
    95 	DP_CONTEXT(CDevAudioControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
       
    96 	DP_IN();
       
    97 	iDevAudio = aDevAudio; 
       
    98 	iAdaptationObserver = &aDevSoundObserver;
       
    99 	
       
   100 	iObserverRegistered = EFalse;
       
   101 
       
   102 	TAudioChannelGain left;
       
   103 	TAudioChannelGain right;
       
   104 	
       
   105 	left.iLocation = TAudioChannelGain::ELeft;
       
   106 	right.iLocation = TAudioChannelGain::ERight;
       
   107 		
       
   108 	User::LeaveIfError(iChannelGains.Append(left)); // assumed element 0 in rest of code
       
   109 	User::LeaveIfError(iChannelGains.Append(right)); // assumed element 1 in rest of code
       
   110 
       
   111 	// Note this could be delayed now (volume is internal to adaptor until later in cycle)
       
   112 	// Needed to allow CMMFDevSound::MaxVolume and similar calls before CMMFDevSound::InitializeL
       
   113 	TAny* interface(NULL);
       
   114 	interface = iDevAudio->iGainControl->Interface(KUidAudioGainControl);
       
   115 	iGainControl = static_cast<MAudioGainControl*>(interface);
       
   116 	DP_OUT();
       
   117 	}
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // Destructor
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 CDevAudioControl::~CDevAudioControl()
       
   124 	{
       
   125 	DP_CONTEXT(CDevAudioControl::~CDevAudioControl *CD1*, CtxDevSound, DPLOCAL);
       
   126 	DP_IN();
       
   127 	iChannelGains.Close();
       
   128 	iSupportedRates.Close();
       
   129 	iSupportedModes.Close();
       
   130 	DP_OUT();
       
   131 	}
       
   132 	
       
   133 // -----------------------------------------------------------------------------
       
   134 // CacheAudioCodecIf
       
   135 //
       
   136 TInt CDevAudioControl::CacheAudioCodecIf()
       
   137 	{
       
   138 	DP_CONTEXT(CDevAudioControl::CacheAudioCodecIf *CD1*, CtxDevSound, DPLOCAL);
       
   139 	DP_IN();
       
   140 	if (iDevAudio->iAudioCodec==NULL)
       
   141 		{
       
   142 		DP0_RET(KErrNotReady,"%d");
       
   143 		}
       
   144 	TInt err = KErrNone;
       
   145 	if (iAudioCodecIf==NULL)
       
   146 		{
       
   147 		MAudioCodec* codecInterface = static_cast<MAudioCodec*>(iDevAudio->iAudioCodec->Interface(KUidAudioCodec));
       
   148 		__ASSERT_ALWAYS (codecInterface, CDevAudioControl::Panic(EAudioCodecIsNull));
       
   149 		iAudioCodecIf = codecInterface; 
       
   150 		}
       
   151 	DP0_RET(err,"%d");
       
   152 	}
       
   153 
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CDevAudioControl::Initialize
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TInt CDevAudioControl::Initialize(TUid /*aFormat*/)
       
   160 	{
       
   161 	DP_CONTEXT(CDevAudioControl::Initialize *CD1*, CtxDevSound, DPLOCAL);
       
   162 	DP_IN();
       
   163 	ASSERT(EFalse);
       
   164 	iObserverRegistered = EFalse;
       
   165 	DP0_RET(KErrNone,"%d");
       
   166 	}
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CDevAudioControl::Uninitialize
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TInt CDevAudioControl::Uninitialize()
       
   173 	{
       
   174 	DP_CONTEXT(CDevAudioControl::Uninitialize *CD1*, CtxDevSound, DPLOCAL);
       
   175 	DP_IN();
       
   176 
       
   177 	// Remove pUnits only allowed when stream is uninitialized
       
   178 	TInt err = iDevAudio->iAudioStream->Uninitialize();
       
   179 
       
   180 	if (err == KErrNone)
       
   181 		{
       
   182 		iDevAudio->iActiveState = EDevSoundAdaptorUninitialising;
       
   183 		err = iDevAudio->iAudioContext->Commit();
       
   184 		}
       
   185 
       
   186 	DP0_RET(err,"%d");
       
   187 	}
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CDevAudioControl::Unload
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 TInt CDevAudioControl::Unload()
       
   194 	{
       
   195 	DP_CONTEXT(CDevAudioControl::Unload *CD1*, CtxDevSound, DPLOCAL);
       
   196 	DP_IN();
       
   197 
       
   198 	TInt err = iDevAudio->iAudioStream->Unload();
       
   199 	if (err == KErrNone)
       
   200 		{
       
   201 		err = iDevAudio->iAudioContext->Commit();
       
   202 		}
       
   203 	if (err == KErrNone)
       
   204 		{
       
   205 		iDevAudio->iActiveState = EDevSoundAdaptorUnloading;
       
   206 		}
       
   207 
       
   208 	DP0_RET(err,"%d");
       
   209 	}
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CDevAudioControl::GetCapabilities
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TInt CDevAudioControl::GetCapabilities(TMMFCapabilities& aCap)
       
   216 	{
       
   217 	DP_CONTEXT(CDevAudioControl::GetCapabilities *CD1*, CtxDevSound, DPLOCAL);
       
   218 	DP_IN();
       
   219 	// At this phase of CAP, we only care about current codec capabilities. 
       
   220 	// This will be supported as soon as the A3F API changes to support this are ready
       
   221 	TInt err = KErrNone;
       
   222 		
       
   223 	if (iDevAudio->iActiveState != EDevSoundAdaptorCreated_Uninitialised)
       
   224 		{
       
   225 		err = CacheAudioCodecIf();
       
   226 		if (err != KErrNone)
       
   227 			{
       
   228 			DP0_RET(err, "%d");			
       
   229 			}
       
   230 			
       
   231 		if (err == KErrNone)
       
   232 			{
       
   233 			err = iAudioCodecIf->GetSupportedModes(iSupportedModes);
       
   234 			if (err == KErrNone)
       
   235 				{
       
   236 				aCap.iChannels = GetModes(iSupportedModes);
       
   237 				err = iAudioCodecIf->GetSupportedSamplesRates(iSupportedRates);
       
   238 				
       
   239 				if (err == KErrNone)
       
   240 					{
       
   241 					aCap.iRate = GetSampleRates(iSupportedRates);
       
   242 					}
       
   243 				else
       
   244 					{
       
   245 					//If was a problem getting sampleRates we donīt expect callbacks and return
       
   246 					iOutstandingCallbacks = 0;
       
   247 					}
       
   248 					
       
   249 				}
       
   250 			else
       
   251 				{
       
   252 				//If was a problem getting modes we donīt expect callbacks and return
       
   253 				iOutstandingCallbacks = 0;
       
   254 				}
       
   255 			}
       
   256 		}
       
   257 	else
       
   258 		{
       
   259 		err = KErrNotReady;
       
   260 		}
       
   261 	DP0_RET(err, "%d");
       
   262 	}
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CDevAudioControl::GetConfig
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 TInt CDevAudioControl::GetConfig(TMMFCapabilities& aConfig)
       
   269 	{
       
   270 	DP_CONTEXT(CDevAudioControl::GetConfig *CD1*, CtxDevSound, DPLOCAL);
       
   271 	DP_IN();
       
   272 	TInt err(KErrNone);
       
   273 
       
   274 	//TODO add a return error code if the sample rate or the channels are not found
       
   275 	//We need to transform the local values to a supported value for the client
       
   276 	aConfig.iRate = static_cast<TMMFSampleRate>(GetSampleRate(iCurrentSampleRate));
       
   277 	aConfig.iChannels = static_cast<TMMFMonoStereo>(GetMode(iCurrentMode));
       
   278 	aConfig.iBufferSize = KDefaultBufferSize;
       
   279 	
       
   280 	DP2(DLINFO, "rate 0x%x, channels 0x%x", aConfig.iRate, aConfig.iChannels);
       
   281 
       
   282 	DP0_RET(err, "%d");
       
   283 	}
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CDevAudioControl::SetConfig
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TInt CDevAudioControl::SetConfig(const TMMFCapabilities& aConfig)
       
   290 	{
       
   291 	DP_CONTEXT(CDevAudioControl::SetConfig *CD1*, CtxDevSound, DPLOCAL);
       
   292 	DP_IN();
       
   293 	
       
   294 	// TODO need to ensure if iChannels or iSampleRate is outside known values, then
       
   295 	// the code handles that gracefully and returns the appropriate error code
       
   296 	
       
   297 	TInt err(KErrNone);
       
   298 	TUid mode = KNullUid;
       
   299 	//Reset the desired values
       
   300 	iDesiredSampleRate = 0;
       
   301 	iDesiredMode = KNullUid;
       
   302 
       
   303 	err = ResolveMode(aConfig.iChannels, mode);
       
   304 	if (err != KErrNone)
       
   305 		{
       
   306 		DP0_RET(err, "%d");
       
   307 		}
       
   308 
       
   309 	err = ResolveSampleRate(aConfig.iRate, iDesiredSampleRate);
       
   310 	if (err != KErrNone)
       
   311 		{
       
   312 		DP0_RET(err, "%d");
       
   313 		}
       
   314 
       
   315 	// At this phase of CAP, we only care about codec, which checks config against
       
   316 	// its own capabilities. Verification against stream specific capabilities
       
   317 	// should be added later on.
       
   318 	
       
   319 	err = CacheAudioCodecIf();
       
   320 	if (err != KErrNone)
       
   321 		{
       
   322 		DP0_RET(err, "%d");
       
   323 		}
       
   324 	
       
   325 	err = iAudioCodecIf->SetSampleRate(iDesiredSampleRate);
       
   326 		
       
   327 	
       
   328 	if(err == KErrNone)
       
   329 		{
       
   330 		err = iAudioCodecIf->SetMode(mode);
       
   331 		}
       
   332 	
       
   333 	if(err == KErrNone)
       
   334 		{
       
   335 		err = iDevAudio->iAudioContext->Commit();
       
   336 		if (err == KErrNone)
       
   337 			{
       
   338 			iDesiredMode = mode;
       
   339 			}
       
   340 		}
       
   341 
       
   342 	DP0_RET(err, "%d");
       
   343 	}
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CDevAudioControl::ProcessInit
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 TInt CDevAudioControl::ProcessInit()
       
   350 	{
       
   351 	DP_CONTEXT(CDevAudioControl::ProcessInit *CD1*, CtxDevSound, DPLOCAL);
       
   352 	DP_IN();
       
   353 	ASSERT(EFalse);
       
   354 	DP0_RET(KErrNone,"%d");
       
   355 	}
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CDevAudioControl::ProcessData
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 void CDevAudioControl::ProcessData()
       
   362 	{
       
   363 	DP_CONTEXT(CDevAudioControl::ProcessData *CD1*, CtxDevSound, DPLOCAL);
       
   364 	DP_IN();
       
   365 	ASSERT(EFalse);
       
   366 	DP_OUT();
       
   367 	}
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CDevAudioControl::GetSamples
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 TInt CDevAudioControl::GetSamples()
       
   374 	{
       
   375 	DP_CONTEXT(CDevAudioControl::GetSamples *CD1*, CtxDevSound, DPLOCAL);
       
   376 	DP_IN();
       
   377 	TInt err(KErrNone);
       
   378 	TInt samplesplayed(0);
       
   379 	
       
   380 	TTimeIntervalMicroSeconds timeProcessed(0);
       
   381 	err = iDevAudio->iAudioStream->GetStreamTime(timeProcessed);
       
   382 
       
   383 	DP1(DLINFO,"CDevAudioControl::GetSamples iCurrentSampleRate %d",iCurrentSampleRate);
       
   384 	
       
   385 	if(err == KErrNone)
       
   386 		{
       
   387 		samplesplayed = (timeProcessed.Int64() * iCurrentSampleRate + KMicroSecondsInSecond/2) / TInt64(KMicroSecondsInSecond);
       
   388 		}
       
   389 	//TODO manage the error
       
   390 
       
   391 	DP0_RET(samplesplayed, "%d");
       
   392 	}
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CDevAudioControl::Stop
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 TInt CDevAudioControl::Stop()
       
   399 	{
       
   400 	DP_CONTEXT(CDevAudioControl::Stop *CD1*, CtxDevSound, DPLOCAL);
       
   401 	DP_IN();
       
   402 	ASSERT(EFalse);
       
   403 	DP0_RET(KErrNone, "%d");
       
   404 	}
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CDevAudioControl::Pause
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 TInt CDevAudioControl::Pause()
       
   411 	{
       
   412 	DP_CONTEXT(CDevAudioControl::Pause *CD1*, CtxDevSound, DPLOCAL);
       
   413 	DP_IN();
       
   414 	ASSERT(EFalse);
       
   415 	DP0_RET(KErrNone, "%d");
       
   416 	}
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CDevAudioControl::CustomInterface
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 TAny* CDevAudioControl::CustomInterface(TUid aInterfaceId)
       
   423 	{
       
   424 	DP_CONTEXT(CDevAudioControl::CustomInterface *CD1*, CtxDevSound, DPLOCAL);
       
   425 	DP_IN();
       
   426 	TAny* ciPtr(NULL);
       
   427 	TInt err = KErrNone;
       
   428 	if(iDevAudio->iAudioStream)
       
   429 		{
       
   430 		TAny* ptr = iDevAudio->iAudioStream->Interface(KUidExtensionInferface);
       
   431 		if(ptr)
       
   432 			{
       
   433 			MCustomInterfaceSupport* ciSupport  = static_cast<MCustomInterfaceSupport*>(ptr);
       
   434 			if(!iObserverRegistered)
       
   435 				{
       
   436 				err = ciSupport->RegisterObserver(*this);
       
   437 				if(err == KErrNone)
       
   438 					{
       
   439 					iObserverRegistered = ETrue;
       
   440 					}
       
   441 				}
       
   442 			err = ciSupport->RequestCustomInterface(aInterfaceId, ciPtr);
       
   443 			if( err != KErrNone)
       
   444 				{
       
   445 				ciPtr = NULL;
       
   446 				}
       
   447 			}
       
   448 		}
       
   449 	DP0_RET(ciPtr, "0x%x");
       
   450 	}
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // CDevAudioControl::SetGain
       
   454 // -----------------------------------------------------------------------------
       
   455 TInt CDevAudioControl::SetGains(TInt aDevSoundGain, TInt aDevSoundMaxGain, TInt aBalance[2], const TTimeIntervalMicroSeconds& aRampDuration, TBool aBecomingActive)
       
   456 	{
       
   457 	DP_CONTEXT(CDevAudioControl::SetGains *CD1*, CtxDevSound, DPLOCAL);
       
   458 	DP_IN();
       
   459 	ASSERT(aDevSoundGain>=0 && aDevSoundGain<=aDevSoundMaxGain); // higher layer assumed to scale
       
   460 	ASSERT(aBalance[ELeftCh]>=0 && aBalance[ELeftCh]<=100);
       
   461 	ASSERT(aBalance[ERightCh]>=0 && aBalance[ERightCh]<=100);
       
   462 	ASSERT(aDevSoundMaxGain>0); // assumed max gain is positive
       
   463 	TInt a3fMaxGain;
       
   464 	TInt err = iGainControl->GetMaxGain(a3fMaxGain);
       
   465 	if (err==KErrNone)
       
   466 		{
       
   467 		iLegacyGain = TInt(TReal(aDevSoundGain)*TReal(a3fMaxGain)/TReal(aDevSoundMaxGain)+0.5);
       
   468 		ASSERT(iLegacyGain>=0 && iLegacyGain<=a3fMaxGain);
       
   469 		iLegacyLeft = aBalance[ELeftCh];
       
   470 		iLegacyRight = aBalance[ERightCh];
       
   471 
       
   472 		MapGains();
       
   473 
       
   474 		// VolumeRamp is only applied when DevSound is becoming active
       
   475 		if(err == KErrNone)
       
   476 			{
       
   477 			if (aRampDuration > 0 && aBecomingActive)
       
   478 				{
       
   479 				err = iGainControl->SetGain(iChannelGains, KUidGainSawTooth, aRampDuration);
       
   480 				}
       
   481 			else
       
   482 				{
       
   483 				err = iGainControl->SetGain(iChannelGains);
       
   484 				}
       
   485 			}
       
   486 
       
   487 		// This call will result on commit only when we are already active
       
   488 		// otherwise the changes will be commited by the DevAudioControl 
       
   489 		// It means we're here due to RequestGainAndBalance call
       
   490 		if(err == KErrNone && !aBecomingActive)
       
   491 			{
       
   492 			err = iDevAudio->iAudioContext->Commit();
       
   493 			}		
       
   494 		}
       
   495 	DP0_RET(err,"%d");
       
   496 	}
       
   497 
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // CDevAudioControl::MapAndSetGains
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 void CDevAudioControl::MapGains()
       
   504 	{
       
   505 	DP_CONTEXT(CDevAudioControl::MapGains *CD1*, CtxDevSound, DPLOCAL);
       
   506 	DP_IN();
       
   507 
       
   508 	// Map legacy values to CAP channel array.
       
   509 	if ( iLegacyLeft == iLegacyRight )
       
   510 		{
       
   511 		iChannelGains[ELeftCh].iGain = iLegacyGain;
       
   512 		iChannelGains[ERightCh].iGain = iLegacyGain;
       
   513 		}
       
   514 	else if ( iLegacyLeft > iLegacyRight )
       
   515 		{
       
   516 		iChannelGains[ELeftCh].iGain = iLegacyGain;
       
   517 		iChannelGains[ERightCh].iGain =
       
   518 						static_cast<TUint>((iLegacyGain*iLegacyRight)/iLegacyLeft);
       
   519 		}
       
   520 	else
       
   521 		{
       
   522 		iChannelGains[ERightCh].iGain = iLegacyGain;
       
   523 		iChannelGains[ELeftCh].iGain =
       
   524 						static_cast<TUint>((iLegacyGain*iLegacyLeft)/iLegacyRight);
       
   525 		}
       
   526 
       
   527 	DP_OUT();
       
   528 	}
       
   529 
       
   530 // -----------------------------------------------------------------------------
       
   531 // CDevAudioControl::DestroyChain
       
   532 // -----------------------------------------------------------------------------
       
   533 //
       
   534 TBool CDevAudioControl::DestroyChain()
       
   535 	{
       
   536 	DP_CONTEXT(CDevAudioControl::DestroyChain *CD1*, CtxDevSound, DPLOCAL);
       
   537 	DP_IN();
       
   538 
       
   539 	TInt err = KErrNone;
       
   540 	TBool readyToDestroy = EFalse;
       
   541 	switch(iDevAudio->iActiveState)
       
   542 		{
       
   543 		case EDevSoundAdaptorActive_Active:
       
   544 		case EDevSoundAdaptorPaused_Primed:
       
   545 			{
       
   546 			err = iDevAudio->iAudioStream->Stop();
       
   547 			if(err == KErrNone)
       
   548 				{
       
   549 				err = iDevAudio->iAudioContext->Commit();
       
   550 				}
       
   551 			if (err == KErrNone)
       
   552 				{
       
   553 				iDevAudio->iActiveState = EDevSoundAdaptorStopping;
       
   554 				}
       
   555 			}
       
   556 			break;
       
   557 		case EDevSoundAdaptorInitialised_Idle:
       
   558 			{
       
   559 			err = iDevAudio->iAudioStream->Unload();
       
   560 			if(err == KErrNone)
       
   561 				{
       
   562 				err = iDevAudio->iAudioContext->Commit();	
       
   563 				}
       
   564 			if (err == KErrNone)
       
   565 				{
       
   566 				iDevAudio->iActiveState = EDevSoundAdaptorUnloading;
       
   567 				}
       
   568 			}
       
   569 			break;
       
   570 		case EDevSoundAdaptorInitialised_Initialised:
       
   571 			{
       
   572 			err = iDevAudio->iAudioStream->Uninitialize();
       
   573 			if(err == KErrNone)
       
   574 				{
       
   575 				err = iDevAudio->iAudioContext->Commit();
       
   576 				}
       
   577 			if (err == KErrNone)
       
   578 				{
       
   579 				iDevAudio->iActiveState = EDevSoundAdaptorUninitialising;
       
   580 				}
       
   581 			}
       
   582 			break;
       
   583 		case EDevSoundAdaptorCreated_Uninitialised:
       
   584 			readyToDestroy = ETrue;
       
   585 		default:
       
   586 			break;
       
   587 		}
       
   588 	
       
   589 	// Destroy sequence fail!
       
   590 	if(err != KErrNone)
       
   591 		{
       
   592 		DP0(DLINFO, "================ Destroy sequence fail! ================");
       
   593 		readyToDestroy = ETrue;
       
   594 		}
       
   595 	// Set the flag only when needed
       
   596 	iDevAudio->iClosing = !readyToDestroy;
       
   597 	DP0_RET(readyToDestroy,"%d");
       
   598 	}
       
   599 
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // CDevAudioControl::RemoveProcessingUnits
       
   603 // -----------------------------------------------------------------------------
       
   604 //
       
   605 TInt CDevAudioControl::RemoveProcessingUnits()
       
   606 	{
       
   607 	DP_CONTEXT(CDevAudioControl::RemoveProcessingUnits *CD1*, CtxDevSound, DPLOCAL);
       
   608 	DP_IN();
       
   609 
       
   610 	// Remove pUnits only allowed when stream is uninitialized
       
   611 	TInt err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioSource);
       
   612 		
       
   613 	if(err == KErrNone)
       
   614 		{
       
   615 		err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioSink);	
       
   616 		}
       
   617 	if(err == KErrNone)
       
   618 		{
       
   619 		err = iDevAudio->iAudioStream->RemoveProcessingUnit(iDevAudio->iAudioCodec);
       
   620 		}
       
   621 
       
   622 	if (err == KErrNone)
       
   623 		{
       
   624 		err = iDevAudio->iAudioContext->Commit();
       
   625 		}
       
   626 		
       
   627 	if(err == KErrNone)
       
   628 		{
       
   629 		iDevAudio->iActiveState = EDevSoundAdaptorRemovingProcessingUnits;		
       
   630 		}
       
   631 	DP0_RET(err,"%d");
       
   632 	}
       
   633 
       
   634 // -----------------------------------------------------------------------------
       
   635 // From class MAudioStreamObserver
       
   636 // CDevAudioControl::StateEvent
       
   637 // -----------------------------------------------------------------------------
       
   638 //
       
   639 void CDevAudioControl::StateEvent(MAudioStream& /*aStream*/, 
       
   640 									TInt /*aReason*/, 
       
   641 									TAudioState /*aNewState*/)
       
   642 	{
       
   643 	DP_CONTEXT(CDevAudio::StateEvent *CD1*, CtxDevSound, DPLOCAL);
       
   644 	DP_IN();
       
   645 	ASSERT(EFalse);
       
   646 	DP_OUT();
       
   647 	}
       
   648 
       
   649 // -----------------------------------------------------------------------------
       
   650 // From class MAudioStreamObserver
       
   651 // CDevAudioControl::AddProcessingUnitComplete
       
   652 // -----------------------------------------------------------------------------
       
   653 //
       
   654 void CDevAudioControl::AddProcessingUnitComplete(MAudioStream& /*aStream*/, 
       
   655 												MAudioProcessingUnit* /*aInstance*/,
       
   656 												TInt /*aError*/)
       
   657 	{
       
   658 	}
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // From class MAudioStreamObserver
       
   662 // CDevAudioControl::RemoveProcessingUnitComplete
       
   663 // -----------------------------------------------------------------------------
       
   664 //
       
   665 void CDevAudioControl::RemoveProcessingUnitComplete(MAudioStream& /*aStream*/,
       
   666 													MAudioProcessingUnit* /*aInstance*/,
       
   667 													TInt /*aError*/)
       
   668 	{
       
   669 	}
       
   670 
       
   671 // -----------------------------------------------------------------------------
       
   672 // From class MAudioStreamObserver
       
   673 // CDevAudioControl::ProcessingFinished
       
   674 // -----------------------------------------------------------------------------
       
   675 //
       
   676 void CDevAudioControl::ProcessingFinished(MAudioStream& /*aStream*/)
       
   677 	{
       
   678 	}
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // From class MAudioStreamObserver
       
   682 // CDevAudioControl::FlushComplete
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 void CDevAudioControl::FlushComplete (MAudioStream& /*aStream*/, TInt aError)
       
   686 	{
       
   687 	// no action needed - should complete as part of the ContextEvent.
       
   688 	// otherwise could callback.
       
   689 	TInt err = KErrNone;
       
   690 	
       
   691 	if(iPauseResumeSequenceDueToEmptyBuffers)
       
   692 		{
       
   693 		// Flush operation failed
       
   694 		if(aError != KErrNone)
       
   695 			{
       
   696 			iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   697 			iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   698 			}
       
   699 		// Handle throw-off scenarios, resume is not possible from here
       
   700 		// 1. ProcessingFinished has occurred
       
   701 		// 2. Preemption occurred 
       
   702 		else if(iCallbackFromAdaptor != KCallbackNone || 
       
   703 			iDevAudio->iActiveState != EDevSoundAdaptorPaused_Primed)
       
   704 			{
       
   705 			iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   706 			iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, KErrNone);
       
   707 			
       
   708 			}
       
   709 		else
       
   710 			{
       
   711 			err = Resume();
       
   712 			if(err != KErrNone)
       
   713 				{
       
   714 				iPauseResumeSequenceDueToEmptyBuffers = EFalse;
       
   715 				iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   716 				}
       
   717 			// Once ContextEvent be received 
       
   718 			// EmptyBuffers can be considered completed
       
   719 			}
       
   720 		}
       
   721 	// EmptyBuffers operation has concluded here
       
   722 	// we didn't go through pause - resume sequence
       
   723 	else
       
   724 		{
       
   725 		iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackFlushComplete, aError);
       
   726 		}
       
   727 	}
       
   728 	
       
   729 // -----------------------------------------------------------------------------
       
   730 // From class MAudioGainControlObserver
       
   731 // CDevAudioControl::MaxRampTimeChanged
       
   732 // -----------------------------------------------------------------------------
       
   733 //
       
   734 void CDevAudioControl::MaxRampTimeChanged(MAudioGainControl& /*aGain*/)
       
   735 	{
       
   736 	DP_CONTEXT(CDevAudioControl::MaxRampTimeChanged *CD1*, CtxDevSound, DPLOCAL);
       
   737 	DP_IN();
       
   738 	// this is not cached, no actions needed
       
   739 	DP_OUT();
       
   740 	}
       
   741 
       
   742 // -----------------------------------------------------------------------------
       
   743 // From class MAudioGainControlObserver
       
   744 // CDevAudioControl::MaxGainChanged
       
   745 // -----------------------------------------------------------------------------
       
   746 //
       
   747 void CDevAudioControl::MaxGainChanged(MAudioGainControl& /*aGain*/)
       
   748 	{
       
   749 	DP_CONTEXT(CDevAudioControl::MaxGainChanged *CD1*, CtxDevSound, DPLOCAL);
       
   750 	DP_IN();
       
   751 	// this is not cached, no actions needed
       
   752 	DP_OUT();
       
   753 	}
       
   754 
       
   755 // -----------------------------------------------------------------------------
       
   756 // From class MAudioGainControlObserver
       
   757 // CDevAudioControl::GainChanged
       
   758 // -----------------------------------------------------------------------------
       
   759 //
       
   760 void CDevAudioControl::GainChanged(MAudioGainControl& aGain, TInt aError)
       
   761 	{
       
   762 	DP_CONTEXT(CDevAudioControl::GainChanged *CD1*, CtxDevSound, DPLOCAL);
       
   763 	DP1_IN("aError=%d", aError);
       
   764 
       
   765 	if(aError != KErrNone)
       
   766 		{
       
   767 		// Either our request failed, or MMRC has forced some values, and we
       
   768 		// have to update local values.
       
   769 		aGain.GetGain(iChannelGains);
       
   770 		ASSERT(iChannelGains.Count()==2);
       
   771 		// Map CAP channel array to legacy values. 
       
   772 		// assumption: left%+right%=100
       
   773 		if ( iChannelGains[ELeftCh].iGain == iChannelGains[ERightCh].iGain )
       
   774 			{
       
   775 			iLegacyGain = iChannelGains[ELeftCh].iGain;
       
   776 			iLegacyLeft = 50;
       
   777 			iLegacyRight = 50;
       
   778 			}
       
   779 		else if ( iChannelGains[ELeftCh].iGain > iChannelGains[ERightCh].iGain )
       
   780 			{
       
   781 			iLegacyGain = iChannelGains[ELeftCh].iGain;
       
   782 			iLegacyLeft = static_cast<TUint>
       
   783 				((100*iLegacyGain)/(iLegacyGain+iChannelGains[ERightCh].iGain));
       
   784 			iLegacyRight = 100 - iLegacyLeft;
       
   785 			//(not that accurate, but sufficient for now)
       
   786 			}
       
   787 		else
       
   788 			{
       
   789 			iLegacyGain = iChannelGains[ERightCh].iGain;
       
   790 			iLegacyRight = static_cast<TUint>
       
   791 				((100*iLegacyGain)/(iLegacyGain+iChannelGains[ELeftCh].iGain));
       
   792 			iLegacyLeft = 100 - iLegacyRight;
       
   793 			}
       
   794 
       
   795 		DP3(DLINFO,"New values :iLegacyGain %d, iLegacyLeft %d, iLegacyRight %d",
       
   796 				iLegacyGain,iLegacyLeft,iLegacyRight);
       
   797 		}
       
   798 	else
       
   799 		{
       
   800 		// our request completed succesfully, no need to update cached values
       
   801 		// just print values in debug version
       
   802 		#ifdef _DEBUG
       
   803 		RArray<TAudioChannelGain> gains;
       
   804 		TUint left;
       
   805 		TUint right;
       
   806 		TUint gain;
       
   807 		aGain.GetGain(gains);
       
   808 		ASSERT(gains.Count()==2);
       
   809 		if ( iChannelGains[ELeftCh].iGain == iChannelGains[ERightCh].iGain )
       
   810 			{
       
   811 			gain = iChannelGains[ELeftCh].iGain;
       
   812 			left = 50;
       
   813 			right = 50;
       
   814 			}
       
   815 		else if ( iChannelGains[ELeftCh].iGain > iChannelGains[ERightCh].iGain )
       
   816 			{
       
   817 			gain = iChannelGains[ELeftCh].iGain;
       
   818 			left = 
       
   819 				static_cast<TUint>((100*gain)/(gain+iChannelGains[ERightCh].iGain));
       
   820 			right = 100 - left;
       
   821 			}
       
   822 		else
       
   823 			{
       
   824 			gain = iChannelGains[ERightCh].iGain;
       
   825 			right = 
       
   826 				static_cast<TUint>((100*gain)/(gain+iChannelGains[ELeftCh].iGain));
       
   827 			left = 100 - right;
       
   828 			}
       
   829 		gains.Close();
       
   830 		DP3(DLINFO,"KErrNone (gain %d, left %d, right %d)", gain,left,right);
       
   831 		#endif
       
   832 		}
       
   833 
       
   834 	DP_OUT();
       
   835 	}
       
   836 
       
   837 // -----------------------------------------------------------------------------
       
   838 // From class MAudioCodecObserver
       
   839 // CDevAudioControl::SampleRateSet
       
   840 // -----------------------------------------------------------------------------
       
   841 void CDevAudioControl::SampleRateSet(TInt aError)
       
   842 	{
       
   843 	if(aError==KErrNone)
       
   844 		{
       
   845 		//Review if we call SetConfig or is only the first time that we load the codec
       
   846 		if (iDesiredSampleRate > 0)
       
   847 			{
       
   848 			iCurrentSampleRate = iDesiredSampleRate;
       
   849 			}
       
   850 		iDesiredSampleRate = 0;
       
   851 		}
       
   852 	}
       
   853 
       
   854 // -----------------------------------------------------------------------------
       
   855 // From class MAudioCodecObserver
       
   856 // CDevAudioControl::ModeSet
       
   857 // -----------------------------------------------------------------------------
       
   858 void CDevAudioControl::ModeSet(TInt aError)
       
   859 	{
       
   860 	if(aError==KErrNone)
       
   861 		{
       
   862 		//Review if we call SetConfig or is only the first time that we load the codec
       
   863 		if (iDesiredMode != KNullUid)
       
   864 			{
       
   865 			iCurrentMode = iDesiredMode;
       
   866 			}
       
   867 		iDesiredMode = KNullUid;
       
   868 		}
       
   869 	}
       
   870 
       
   871 // -----------------------------------------------------------------------------
       
   872 // From class MAudioCodecObserver
       
   873 // CDevAudioControl::GetSupportedSampleRatesComplete
       
   874 // -----------------------------------------------------------------------------
       
   875 void CDevAudioControl::GetSupportedSampleRatesComplete(TInt aError)
       
   876 	{
       
   877 	iSupportedRates.Reset();
       
   878 	CompleteMessageCap(aError);
       
   879 	}
       
   880 
       
   881 // -----------------------------------------------------------------------------
       
   882 // From class MAudioCodecObserver
       
   883 // CDevAudioControl::GetSupportedModesComplete
       
   884 // -----------------------------------------------------------------------------
       
   885 void CDevAudioControl::GetSupportedModesComplete(TInt aError)
       
   886 	{
       
   887 	iSupportedModes.Reset();
       
   888 	CompleteMessageCap(aError);
       
   889 	}
       
   890 
       
   891 // -----------------------------------------------------------------------------
       
   892 // CDevAudioControl::CompleteMessageCap
       
   893 // -----------------------------------------------------------------------------
       
   894 void CDevAudioControl::CompleteMessageCap(TInt aError)
       
   895 	{
       
   896 	if (iOutstandingCallbacks > 1) //waiting until the 2 outstanding callbacks arrival.
       
   897 		{
       
   898 		iOutstandingCallbacks--;
       
   899 		iError = aError; //keeping the error.
       
   900 		}
       
   901 	else
       
   902 		{
       
   903 		if (iError == KErrNone)
       
   904 			{
       
   905 			iAdaptationObserver->AsynchronousOperationComplete(aError, ETrue);
       
   906 			}
       
   907 		else
       
   908 			{
       
   909 			iAdaptationObserver->AsynchronousOperationComplete(iError, ETrue);
       
   910 			}
       
   911 		iError = KErrNone;
       
   912 		iOutstandingCallbacks = KMaxCallbacksExpected;
       
   913 		}
       
   914 	}
       
   915 
       
   916 // -----------------------------------------------------------------------------
       
   917 // CDevAudioControl::SetToneData
       
   918 // -----------------------------------------------------------------------------
       
   919 //
       
   920 TInt CDevAudioControl::SetToneData(TToneData& /*aToneData*/)
       
   921 	{
       
   922 	DP_CONTEXT(CDevAudioControl::SetToneData *CD1*, CtxDevSound, DPLOCAL);
       
   923 	DP_IN();
       
   924 	DP0_RET(KErrNotSupported, "%d");
       
   925 	}
       
   926 
       
   927 
       
   928 // -----------------------------------------------------------------------------
       
   929 // From class MAudioContextObserver
       
   930 // CDevAudio::ContextEvent
       
   931 // -----------------------------------------------------------------------------
       
   932 //
       
   933 void CDevAudioControl::ContextEvent(TUid aEvent, TInt aError)
       
   934 	{
       
   935 	DP_CONTEXT(CDevAudioControl::ContextEvent *CD1*, CtxDevSound, DPLOCAL);
       
   936 	DP_IN();
       
   937 
       
   938 	if (aEvent == KUidA3FContextUpdateComplete)
       
   939 		{
       
   940 		iAdaptationObserver->AsynchronousOperationComplete(aError, ETrue);
       
   941 		}
       
   942 	else if(aEvent == KUidA3FContextPreEmption || aEvent == KUidA3FContextPreEmptedCommit)
       
   943 		{
       
   944 		
       
   945 		//Preemption during the below states should complete invoke AsynOperationComplete
       
   946 		if(iDevAudio->iActiveState!=EDevSoundAdaptorActivating && iDevAudio->iActiveState!=EDevSoundAdaptorLoading && 
       
   947 			iDevAudio->iActiveState!=EDevSoundAdaptorStopping && iDevAudio->iActiveState!=EDevSoundAdaptorUnloading
       
   948 			 && iDevAudio->iActiveState!=EDevSoundAdaptorPausing)
       
   949 			{
       
   950 			iIgnoreAsyncOpComplete = ETrue;
       
   951 			iAdaptationObserver->PreemptionStartedCallbackReceived();
       
   952 			}
       
   953 		}
       
   954 	DP_OUT();
       
   955 	}
       
   956 
       
   957 
       
   958 // -----------------------------------------------------------------------------
       
   959 // From class MCustomInterfaceSupportObserver
       
   960 // CDevAudio::CustomInterfaceRemoval
       
   961 // -----------------------------------------------------------------------------
       
   962 //
       
   963 void CDevAudioControl::CustomInterfaceRemoval(TUid aUid, TAny* /*aPtr*/)
       
   964 	{
       
   965 	DP_CONTEXT(CDevAudioControl::CustomInterfaceRemoval *CD1*, CtxDevSound, DPLOCAL);
       
   966 	DP_IN();
       
   967 	// TODO: Review this functionality
       
   968 	iAdaptationObserver->InterfaceDeleted(aUid);
       
   969 	DP_OUT();
       
   970 	}
       
   971 
       
   972 // ---------------------------------------------------------------------------
       
   973 // CDevAudioControl::ResolveSampleRate
       
   974 // ---------------------------------------------------------------------------
       
   975 TInt CDevAudioControl::ResolveSampleRate(TInt aSampleRate, TInt& aSampleRateValue)
       
   976 	{
       
   977 	DP_CONTEXT(CDevAudioControl::ResolveSampleRate, CtxDevSound, DPLOCAL);
       
   978 	DP_IN();
       
   979 	TInt err(KErrArgument);
       
   980 
       
   981 	for (TUint i=0; i<=KMaxSampleRateIndex; i++)
       
   982 		{
       
   983 		if(KRateTableLookup[i].iSampleRate == aSampleRate)
       
   984 			{
       
   985 			aSampleRateValue = KRateTableLookup[i].iSampleRateValue;
       
   986 			err = KErrNone;
       
   987 			break;
       
   988 			}
       
   989 		}
       
   990 
       
   991 	//To avoid the value return a non desired value.
       
   992 	if (err != KErrNone)
       
   993 		{
       
   994 		aSampleRateValue = 0;
       
   995 		}
       
   996 
       
   997 	DP0_RET(err, "%d");
       
   998 	}
       
   999 
       
  1000 
       
  1001 // ---------------------------------------------------------------------------
       
  1002 // CDevAudioControl::ResolveMode
       
  1003 // ---------------------------------------------------------------------------
       
  1004 TInt CDevAudioControl::ResolveMode(TUint aModeValue, TUid& aMode)
       
  1005 	{
       
  1006 	DP_CONTEXT(CDevAudioControl::ResolveMode *CD1*, CtxDevSound, DPLOCAL);
       
  1007 	DP_IN();
       
  1008 	TInt err(KErrArgument);
       
  1009 
       
  1010 	
       
  1011 	for (TInt i=0; i<=KMaxModeIndex; i++)
       
  1012 		{
       
  1013 		const TAudioModeTableEntry& entry = KModeTableLookup[i];
       
  1014 		if (entry.iAudioModeValue == aModeValue)
       
  1015 			{
       
  1016 			aMode = entry.iAudioMode;
       
  1017 			err = KErrNone;
       
  1018 			break;
       
  1019 			}
       
  1020 		}
       
  1021 	
       
  1022 	DP0_RET(err,"%d");
       
  1023 	}
       
  1024 
       
  1025 // ---------------------------------------------------------------------------
       
  1026 // CDevAudioControl::GetModes
       
  1027 // ---------------------------------------------------------------------------
       
  1028 TUint CDevAudioControl::GetModes(const RArray<TUid>& aMode)
       
  1029 	{
       
  1030 	DP_CONTEXT(CDevAudioControl::GetModes *CD1*, CtxDevSound, DPLOCAL);
       
  1031 	DP_IN();
       
  1032 
       
  1033 	TUint result = 0;
       
  1034 	TInt count = aMode.Count();
       
  1035 
       
  1036 	for (TInt i=0; i<count; i++)
       
  1037 		{
       
  1038 		result |= GetMode(aMode[i]);
       
  1039 		}
       
  1040 	DP0_RET(result,"%d");
       
  1041 	}
       
  1042 
       
  1043 
       
  1044 // ---------------------------------------------------------------------------
       
  1045 // CDevAudioControl::GetMode
       
  1046 // ---------------------------------------------------------------------------
       
  1047 TUint CDevAudioControl::GetMode(TUid aMode)
       
  1048 	{
       
  1049 	DP_CONTEXT(CDevAudioControl::GetMode *CD1*, CtxDevSound, DPLOCAL);
       
  1050 	DP_IN();
       
  1051 	
       
  1052 	TUint result = 0;
       
  1053 	
       
  1054 	for (TInt e=0; e<=KMaxModeIndex; e++)
       
  1055 		{
       
  1056 		if(KModeTableLookup[e].iAudioMode == aMode)
       
  1057 			{
       
  1058 			result = KModeTableLookup[e].iAudioModeValue;
       
  1059 			break;
       
  1060 			}
       
  1061 		}
       
  1062 	DP0_RET(result,"%d");
       
  1063 	}
       
  1064 
       
  1065 // ---------------------------------------------------------------------------
       
  1066 // CDevAudioControl::GetSampleRates
       
  1067 // ---------------------------------------------------------------------------
       
  1068 TUint CDevAudioControl::GetSampleRates(const RArray<TInt>& aSampleRates)
       
  1069 	{
       
  1070 	DP_CONTEXT(CDevAudioControl::GetSampleRates *CD1*, CtxDevSound, DPLOCAL);
       
  1071 	DP_IN();
       
  1072 	
       
  1073 	TUint result = 0;
       
  1074 	TInt count = aSampleRates.Count();
       
  1075 	
       
  1076 	for (TInt i=0; i<count; i++)
       
  1077 		{
       
  1078 		result |= GetSampleRate(aSampleRates[i]);
       
  1079 		}
       
  1080 	DP0_RET(result,"%d");
       
  1081 	}
       
  1082 
       
  1083 // ---------------------------------------------------------------------------
       
  1084 // CDevAudioControl::GetSampleRate
       
  1085 // ---------------------------------------------------------------------------
       
  1086 TUint CDevAudioControl::GetSampleRate(TInt aSampleRates)
       
  1087 	{
       
  1088 	DP_CONTEXT(CDevAudioControl::GetSampleRate *CD1*, CtxDevSound, DPLOCAL);
       
  1089 	DP_IN();
       
  1090 
       
  1091 	TUint result = 0;
       
  1092 	TInt position = 0;
       
  1093 	TInt lowerbound = 0;
       
  1094 	TInt upperbound = KMaxSampleRateIndex;
       
  1095 
       
  1096 	if ((aSampleRates < KRateTableLookup[lowerbound].iSampleRateValue) || (aSampleRates > KRateTableLookup[upperbound].iSampleRateValue)) 
       
  1097 		{
       
  1098 		//value request not found in the array.
       
  1099 		DP0_RET(result,"%d");
       
  1100 		}
       
  1101 
       
  1102 	//Binary Search
       
  1103 	position = ( lowerbound + upperbound) / 2;
       
  1104 
       
  1105 	while((KRateTableLookup[position].iSampleRateValue != aSampleRates) && (lowerbound <= upperbound))
       
  1106 		{
       
  1107 		if (KRateTableLookup[position].iSampleRateValue > aSampleRates)
       
  1108 			{
       
  1109 			upperbound = position - 1;
       
  1110 			}
       
  1111 		else
       
  1112 			{
       
  1113 			lowerbound = position + 1;
       
  1114 			}
       
  1115 		position = (lowerbound + upperbound) / 2;
       
  1116 		}
       
  1117 
       
  1118 	result = KRateTableLookup[position].iSampleRate;
       
  1119 
       
  1120 	DP0_RET(result,"%d");
       
  1121 	}
       
  1122 
       
  1123 // ---------------------------------------------------------------------------
       
  1124 // CDevAudioControl::ProcessingFinishedReceived
       
  1125 // ---------------------------------------------------------------------------
       
  1126 TInt CDevAudioControl::ProcessingFinishedReceived(TBool& /*aAyncOperation*/)
       
  1127 	{
       
  1128 	return KErrNone;
       
  1129 	}
       
  1130 	
       
  1131 // ---------------------------------------------------------------------------
       
  1132 // CDevAudioControl::ProcessingError
       
  1133 // ---------------------------------------------------------------------------
       
  1134 TInt CDevAudioControl::ProcessingError(TBool& /*aAyncOperation*/)
       
  1135     {
       
  1136     return KErrNone;
       
  1137     }
       
  1138 
       
  1139 // ---------------------------------------------------------------------------
       
  1140 // CDevAudioControl::RequestEmptyBuffers
       
  1141 // ---------------------------------------------------------------------------	
       
  1142 TInt CDevAudioControl::RequestEmptyBuffers()
       
  1143 	{
       
  1144 	DP_CONTEXT(CDevAudioControl::RequestEmptyBuffers *CD1*, CtxDevSound, DPLOCAL);
       
  1145 	DP_IN();
       
  1146 
       
  1147 	TInt err(KErrNotReady);
       
  1148 
       
  1149 	if(iDevAudio)
       
  1150 		{
       
  1151 		if(iDevAudio->iActiveState == EDevSoundAdaptorPaused_Primed)
       
  1152 			{
       
  1153 			err = iDevAudio->iAudioStream->Flush();
       
  1154 			}
       
  1155 		else if (iDevAudio->iActiveState == EDevSoundAdaptorActive_Active)
       
  1156 			{
       
  1157 			err = Pause();
       
  1158 			if(err == KErrNone)
       
  1159 				{
       
  1160 				iPauseResumeSequenceDueToEmptyBuffers = ETrue;
       
  1161 				}
       
  1162 			
       
  1163 			}
       
  1164 		}
       
  1165 	DP0_RET(err,"%d");
       
  1166 	};
       
  1167 
       
  1168 void CDevAudioControl::Panic(TDevSoundAdaptorPanicCode aCode)
       
  1169 	{
       
  1170 	_LIT(KMMFDevSoundAdaptorPanicCategory, "DevSoundAdaptor");
       
  1171 	User::Panic(KMMFDevSoundAdaptorPanicCategory, aCode);
       
  1172 	}
       
  1173 
       
  1174 // ---------------------------------------------------------------------------
       
  1175 // CDevAudioControl::GetTimePlayed
       
  1176 // ---------------------------------------------------------------------------
       
  1177 TInt CDevAudioControl::GetTimePlayed(TTimeIntervalMicroSeconds& aTime)
       
  1178 	{
       
  1179 	DP_CONTEXT(CDevAudioControl::GetTimePlayed *CD1*, CtxDevSound, DPLOCAL);
       
  1180 	DP_IN();
       
  1181 	TInt err = iDevAudio->iAudioStream->GetStreamTime(aTime);
       
  1182 	DP0_RET(err,"%d");
       
  1183 	}
       
  1184 
       
  1185 // ---------------------------------------------------------------------------
       
  1186 // CDevAudioControl::Resume
       
  1187 // ---------------------------------------------------------------------------
       
  1188 TBool CDevAudioControl::Resume()
       
  1189 	{
       
  1190 	DP_CONTEXT(CDevAudioControl::Stop *CD1*, CtxDevSound, DPLOCAL);
       
  1191 	DP_IN();
       
  1192 	ASSERT(EFalse);
       
  1193 	DP0_RET(KErrNone, "%d");
       
  1194 	}
       
  1195 
       
  1196 // -----------------------------------------------------------------------------
       
  1197 // CDevAudioControl::BufferErrorEvent
       
  1198 // -----------------------------------------------------------------------------
       
  1199 //
       
  1200 void CDevAudioControl::BufferErrorEvent()
       
  1201 	{
       
  1202 	ASSERT(EFalse); //This should never happen
       
  1203 	}
       
  1204 // End of file