os/mm/devsound/a3fdevsound/src/devsoundadaptor/cdevaudio.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "cdevaudio.h"
    19 #include "cdevaudiocontrol.h"
    20 #include "cdevplaycontrol.h"
    21 #include "cdevrecordcontrol.h"
    22 #include "cdevtonecontrol.h"
    23 #include "cdevgencontrol.h"
    24 
    25 #include <a3f/audioprocessingunittypeuids.h>
    26 #include <a3f/devsoundadaptationinfo.h>
    27 #include <a3f/a3ffourcclookup.h>
    28 #include <a3f/audiocontextfactory.h>
    29 #include <a3f/maudioprocessingunit.h>
    30 #include <a3f/maudiocontext.h>
    31 #include <a3f/mcontexteffectiveclient.h>
    32 #include <a3f/maudiostream.h>
    33 #include <a3f/maudiocodec.h>
    34 #include <a3f/maudiogaincontrol.h>
    35 #include <a3f/mbuffersource.h>
    36 #include <a3f/mbuffersink.h>
    37 
    38 #include "mglobalproperties.h"
    39 
    40 const TInt KMidWayBalance = 50; // 50%
    41 const TInt KMaxBalance = 100; // 100%
    42 const TInt KLeftChannel = 0;
    43 const TInt KRightChannel = 1;
    44 
    45 
    46 // ======== LOCAL FUNCTIONS ========
    47 
    48 // ======== MEMBER FUNCTIONS ========
    49 
    50 // ---------------------------------------------------------------------------
    51 // Default constructor
    52 // ---------------------------------------------------------------------------
    53 //
    54 CDevAudio::CDevAudio(MDevSoundAdaptationObserver& aAdaptationObserver)
    55 	: iAdaptationObserver(aAdaptationObserver), iMode(EMMFStateIdle), 
    56 	iActiveState(EDevSoundAdaptorCreated_Uninitialised),
    57 	iActiveStreamState(EUninitialized)
    58 	{
    59 	TRACE_CREATE();
    60 	DP_CONTEXT(CDevAudio::CDevAudio *CD1*, CtxDevSound, DPLOCAL);
    61 	DP_IN();
    62 		
    63 	iDevSoundPlayBalance[KLeftChannel] = iDevSoundPlayBalance[KRightChannel] = KMidWayBalance;
    64 	iDevSoundRecordBalance[KLeftChannel] = iDevSoundRecordBalance[KRightChannel] = KMidWayBalance;
    65 
    66 	DP_OUT();
    67 	}
    68 
    69 // ---------------------------------------------------------------------------
    70 // Destructor
    71 // ---------------------------------------------------------------------------
    72 //
    73 CDevAudio::~CDevAudio()
    74 	{
    75 	DP_CONTEXT(CDevAudio::~CDevAudio *CD1*, CtxDevSound, DPLOCAL);
    76 	DP_IN();
    77 	
    78 	delete iAdaptationInfo;
    79 	
    80 	iSupportedInputFormats.Close();
    81 	iSupportedOutputFormats.Close();
    82 
    83 	// This part is fine at the destructor
    84 	// All this is syncronous
    85 	if ( iAudioContext )
    86 		{
    87 		if ( iGainControl )
    88 			{
    89 			iAudioContext->DeleteAudioProcessingUnit(iGainControl);
    90 			}
    91 		if ( iAudioSink )
    92 			{
    93 			iAudioContext->DeleteAudioProcessingUnit(iAudioSink);
    94 			}
    95 		if ( iAudioCodec )
    96 			{
    97 			iAudioContext->DeleteAudioProcessingUnit(iAudioCodec);
    98 			}
    99 		if ( iAudioSource )
   100 			{
   101 			iAudioContext->DeleteAudioProcessingUnit(iAudioSource);
   102 			}
   103 		if ( iAudioStream )
   104 			{
   105 			iAudioContext->DeleteAudioStream(iAudioStream);
   106 			}
   107 		}
   108 
   109 	iAudioContextFactory->DeleteAudioContext(iAudioContext);
   110 	
   111 	if ( iAudioContextFactory )
   112 		{
   113 		delete iAudioContextFactory;
   114 		}
   115 
   116 	if ( iDevPlayControl )
   117 		{
   118 		delete iDevPlayControl;
   119 		}
   120 	if ( iDevRecordControl )
   121 		{
   122 		delete iDevRecordControl;
   123 		}
   124 	if ( iDevToneControl )
   125 		{
   126 		delete iDevToneControl;
   127 		}
   128 	if ( iDevGenControl )
   129 		{
   130 		delete iDevGenControl;
   131 		}
   132 	
   133 	DP_OUT();
   134 	}
   135 
   136 // -----------------------------------------------------------------------------
   137 // CDevAudio::NewL
   138 // Two-phased constructor.
   139 // -----------------------------------------------------------------------------
   140 //
   141 CDevAudio* CDevAudio::NewL(MDevSoundAdaptationObserver& aAdaptationObserver, 
   142 									MGlobalProperties& aGlobalProperties)
   143 	{
   144 	DP_STATIC_CONTEXT(CDevAudio::NewL *CD0*, CtxDevSound, DPLOCAL);
   145 	DP_IN();
   146 	CDevAudio* self = new (ELeave) CDevAudio(aAdaptationObserver);
   147 	CleanupStack::PushL(self);
   148 	self->ConstructL(aGlobalProperties);
   149 	CleanupStack::Pop(self);
   150 	DP0_RET(self, "0x%x");
   151 	}
   152 
   153 
   154 // -----------------------------------------------------------------------------
   155 // CDevAudio::ConstructL
   156 // -----------------------------------------------------------------------------
   157 //
   158 void CDevAudio::ConstructL(MGlobalProperties& aGlobalProperties)
   159 	{
   160 	DP_CONTEXT(CDevAudio::ConstructL *CD1*, CtxDevSound, DPLOCAL);
   161 	DP_IN();
   162 	
   163 	iGlobalProperties = &aGlobalProperties;
   164 
   165 	iAudioContextFactory = CAudioContextFactory::NewL();
   166 	
   167 	User::LeaveIfError(iAudioContextFactory->CreateAudioContext(iAudioContext));
   168 
   169 	User::LeaveIfError(iAudioContext->CreateAudioStream(iAudioStream));
   170 	
   171 	User::LeaveIfError(iAudioContext->CreateAudioProcessingUnit(KUidAudioGainControl, iGainControl));
   172 	User::LeaveIfError(iAudioStream->AddGainControl(iGainControl));
   173 
   174 	iDevPlayControl = CDevPlayControl::NewL(this, iAdaptationObserver);
   175 	iDevRecordControl = CDevRecordControl::NewL(this, iAdaptationObserver);
   176 	iDevToneControl = CDevToneControl::NewL(this, iAdaptationObserver);
   177 	iDevGenControl = CDevGenControl::NewL(this, iAdaptationObserver);
   178 	
   179 	iAdaptationInfo = CA3FDevSoundAdaptationInfo::NewL(*this, const_cast<CFourCCConvertor&>(iGlobalProperties->GetFourCCConvertor()));
   180 
   181 	// The generic audio control is the only one receiving the callbacks at this moment
   182 	iCurrentAudioControl = static_cast<CDevAudioControl*>(iDevGenControl);
   183 	TInt err = iAudioContext->RegisterAudioContextObserver(*iCurrentAudioControl);
   184 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   185 		{
   186 		User::Leave(err);
   187 		}
   188 	DP_OUT();
   189 	}
   190 	
   191 // -----------------------------------------------------------------------------
   192 // CDevAudio::PostOpen
   193 // -----------------------------------------------------------------------------
   194 //
   195 TInt CDevAudio::PostOpen()
   196 	{
   197 	DP_CONTEXT(CDevAudio::PostOpen *CD1*, CtxDevSound, DPLOCAL);
   198 	DP_IN();
   199 	ASSERT(iPreOpenState==EPreOpenStateIdle);
   200 	TInt err = KErrNone;
   201 	
   202 	if(iPreOpenState == EPreOpenStateIdle)
   203 		{
   204 		err = iAdaptationInfo->RequestMaxGain(KUidAudioDecoder);
   205 		if (err == KErrNone)
   206 			{
   207 			iPreOpenState = EPreOpenStateRequestingMaxVolume;
   208 			}
   209 		}
   210 	
   211 	DP0_RET(err, "%d");
   212 	}
   213 	
   214 
   215 TBool CDevAudio::IsResumeSupported()
   216 	{
   217 	DP_CONTEXT(CDevAudio::IsResumeSupported *CD1*, CtxDevSound, DPLOCAL);
   218 	DP_IN();
   219 
   220 	TUid codecType(KNullUid);
   221 	if(iMode == EMMFStatePlaying || iMode == EMMFStateTonePlaying)
   222 		{
   223 		codecType = KUidAudioDecoder;
   224 		}
   225 	else if (iMode == EMMFStateRecording)
   226 		{
   227 		codecType = KUidAudioEncoder;
   228 		}
   229 	TBool supported = iAdaptationInfo->IsResumeSupported(codecType, iFormat);
   230 
   231 	DP0_RET(supported, "%d");
   232 	}
   233 
   234 
   235 // -----------------------------------------------------------------------------
   236 // CDevAudio::RequestMaxGainComplete
   237 // -----------------------------------------------------------------------------
   238 //
   239 void CDevAudio::RequestMaxGainComplete (TUid __DEBUG_ONLY(aCodecType), TInt aError, TInt aResult)
   240 	{
   241 	DP_CONTEXT(CDevAudio::RequestMaxGainComplete *CD1*, CtxDevSound, DPLOCAL);
   242 	DP_IN();
   243 	ASSERT(iPreOpenState==EPreOpenStateRequestingMaxVolume && aCodecType==KUidAudioDecoder||
   244 			iPreOpenState==EPreOpenStateRequestingMaxGain && aCodecType==KUidAudioEncoder);
   245 
   246 	TInt err = aError;
   247 	TBool asyncComplete = EFalse;
   248 	
   249 	if (err == KErrNone)
   250 		{
   251 		if (iPreOpenState==EPreOpenStateRequestingMaxVolume)
   252 			{
   253 			iDevSoundMaxVolume = aResult;
   254 						
   255 			err = SetDevSoundVolume((iDevSoundMaxVolume+1)/2, asyncComplete);
   256 
   257 			__ASSERT_DEBUG(asyncComplete==EFalse, User::Invariant());
   258 			
   259 			if(err == KErrNone)
   260 				{
   261 				// first request - so request max gain
   262 				err = iAdaptationInfo->RequestMaxGain(KUidAudioEncoder);
   263 				if (err == KErrNone)
   264 					{
   265 					iPreOpenState = EPreOpenStateRequestingMaxGain;
   266 					}
   267 				}
   268 			}
   269 		else if(iPreOpenState==EPreOpenStateRequestingMaxGain)
   270 			{
   271 			iDevSoundMaxGain = aResult;
   272 			
   273 			err = SetDevSoundGain((iDevSoundMaxGain+1)/2, asyncComplete);
   274 
   275 			__ASSERT_DEBUG(asyncComplete==EFalse, User::Invariant());
   276 			
   277 			if(err == KErrNone)
   278 				{
   279 				// second request - complete whatever - Ready for Formats
   280 				err = iAdaptationInfo->RequestSupportedFormats(KUidAudioDecoder, iSupportedInputFormats);
   281 				if (err == KErrNone)
   282 					{
   283 					iPreOpenState = EPreOpenStateRequestingInputFormats;
   284 					}
   285 				}
   286 			}
   287 		}
   288 
   289 	if (err!=KErrNone)
   290 		{
   291 		iPreOpenState = EPreOpenStateIdle;
   292 		iAdaptationObserver.AsynchronousOperationComplete(err, ETrue);
   293 		}
   294 	
   295 	DP_OUT();
   296 	}
   297 
   298 // -----------------------------------------------------------------------------
   299 // CDevAudio::RequestSupportedFormatsComplete
   300 // -----------------------------------------------------------------------------
   301 //
   302 void CDevAudio::RequestSupportedFormatsComplete(TUid __DEBUG_ONLY(aCodecType), TInt aError)
   303 	{
   304 	DP_CONTEXT(CDevAudio::RequestSupportedFormatsComplete *CD1*, CtxDevSound, DPLOCAL);
   305 	DP_IN();
   306 	ASSERT(iPreOpenState==EPreOpenStateRequestingInputFormats && aCodecType==KUidAudioDecoder||
   307 			iPreOpenState==EPreOpenStateRequestingOutputFormats && aCodecType==KUidAudioEncoder);
   308 
   309 	TInt err = aError;
   310 
   311 	if (err == KErrNone)
   312 		{
   313 		if (iPreOpenState==EPreOpenStateRequestingInputFormats)
   314 			{
   315 			// first request - so request output formats
   316 			err = iAdaptationInfo->RequestSupportedFormats(KUidAudioEncoder, iSupportedOutputFormats);
   317 			if (err == KErrNone)
   318 				{
   319 				iPreOpenState = EPreOpenStateRequestingOutputFormats;
   320 				}
   321 			}
   322 		else
   323 			{
   324 			ASSERT(iPreOpenState==EPreOpenStateRequestingOutputFormats);
   325 			// second request - complete whatever
   326 			iPreOpenState = EPreOpenStateIdle;
   327 			iAdaptationObserver.AsynchronousOperationComplete(aError, ETrue);
   328 			}
   329 		}
   330 
   331 	if (err!=KErrNone)
   332 		{
   333 		iPreOpenState = EPreOpenStateIdle;
   334 		iAdaptationObserver.AsynchronousOperationComplete(err, ETrue);
   335 		}
   336 	DP_OUT();
   337 	}
   338 
   339 
   340 
   341 // -----------------------------------------------------------------------------
   342 // CDevAudio::Initialize
   343 // -----------------------------------------------------------------------------
   344 //
   345 TInt CDevAudio::Initialize(TUid aFormat, TMMFState aMode)
   346 	{
   347 	DP_CONTEXT(CDevAudio::Initialize*CD1*, CtxDevSound, DPLOCAL);
   348 	DP_IN();
   349 	TInt err(KErrNone);
   350 
   351 	if(iActiveState != EDevSoundAdaptorCreated_Uninitialised &&
   352 		iActiveState != EDevSoundAdaptorInitialised_Initialised && 
   353 		iActiveState != EDevSoundAdaptorInitialised_Idle &&
   354 		iActiveState != EDevSoundAdaptorUnitialised_Uninitialised )
   355 		{
   356 		DP0_RET(KErrNotReady, "%d");
   357 		}
   358 
   359 	// Reinitialization 
   360 	if (iActiveState == EDevSoundAdaptorInitialised_Initialised || 
   361 		iActiveState == EDevSoundAdaptorInitialised_Idle)
   362 		{
   363 		iReinitializing = ETrue;
   364 		iTargetFormat = aFormat;
   365 		iTargetMode = aMode;
   366 		if(iActiveState == EDevSoundAdaptorInitialised_Idle)
   367 			{
   368 			err = iAudioStream->Unload();
   369 			if(err == KErrNone)
   370 				{
   371 				err = CommitAudioContext();
   372 				}
   373 			if(err == KErrNone)
   374 				{
   375 				iActiveState = EDevSoundAdaptorUnloading;
   376 				}
   377 			}
   378 		else
   379 			{
   380 			err = iCurrentAudioControl->Uninitialize();
   381 			}
   382 		DP0_RET(err, "%d");
   383 		}
   384 
   385 	// Redo partial initialization after pre-emption clash event in
   386 	// EDevSoundAdaptorRemovingProcessingUnits state
   387 	if (iActiveState == EDevSoundAdaptorUnitialised_Uninitialised &&
   388 			iPreviousState == EDevSoundAdaptorRemovingProcessingUnits)
   389 		{
   390 		err = iCurrentAudioControl->RemoveProcessingUnits();
   391 		DP0_RET(err, "%d");
   392 		}
   393 
   394 	// Delete pUnits if already created
   395 	if (iAudioSource)
   396 		{
   397 		iAudioContext->DeleteAudioProcessingUnit(iAudioSource);
   398 		iAudioSource = NULL;
   399 		}
   400 
   401 	if (iAudioSink)
   402 		{
   403 		iAudioContext->DeleteAudioProcessingUnit(iAudioSink);
   404 		iAudioSink = NULL;
   405 		}
   406 
   407 	if (iAudioCodec)
   408 		{
   409 		iAudioContext->DeleteAudioProcessingUnit(iAudioCodec);
   410 		iAudioCodec = NULL;
   411 		}
   412 
   413 	//	Create pUnits and select proper DevSound Adaptor Helper
   414 	if (aMode == EMMFStatePlaying)
   415 		{
   416 		// create correct type sink&source&codec
   417 		err = CreateAudioProcessingUnits(KUidMmfBufferSource, KUidAudioDeviceSink, KUidAudioDecoder);
   418 		if (err == KErrNone)
   419 			{
   420 			// Now just the playcontrol should receive the only one receiving events from 
   421 			// AudioContext
   422 			iAudioContext->UnregisterAudioContextObserver(*iCurrentAudioControl);
   423 			iCurrentAudioControl = static_cast<CDevAudioControl*>(iDevPlayControl);
   424 			err = iAudioContext->RegisterAudioContextObserver(*iCurrentAudioControl);
   425 			}
   426 		if ((err != KErrNone) && (err != KErrAlreadyExists))
   427 			{
   428 			DeleteAudioProcessingUnits();
   429 			}
   430 		}
   431 		
   432 	else if (aMode == EMMFStateRecording)
   433 		{
   434 		// create correct type sink&source&codec
   435 		err = CreateAudioProcessingUnits(KUidAudioDeviceSource, KUidMmfBufferSink, KUidAudioEncoder);
   436 		if (err == KErrNone)
   437 			{
   438 			iAudioContext->UnregisterAudioContextObserver(*iCurrentAudioControl);
   439 			iCurrentAudioControl = static_cast<CDevAudioControl*>(iDevRecordControl);
   440 			err = iAudioContext->RegisterAudioContextObserver(*iCurrentAudioControl);
   441 			}
   442 		if ((err != KErrNone) && (err != KErrAlreadyExists))
   443 			{
   444 			DeleteAudioProcessingUnits();
   445 			}
   446 		}
   447 	else if (aMode == EMMFStateTonePlaying)
   448 		{
   449 		// create correct type sink&source&codec
   450 		err = CreateAudioProcessingUnits(KUidMmfBufferSource, KUidAudioDeviceSink, KUidAudioDecoder);
   451 		if (err == KErrNone)
   452 			{
   453 			iAudioContext->UnregisterAudioContextObserver(*iCurrentAudioControl);
   454 			iCurrentAudioControl =  static_cast<CDevAudioControl*>(iDevToneControl);
   455 			err = iAudioContext->RegisterAudioContextObserver(*iCurrentAudioControl);
   456 			}
   457 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   458 			{
   459 			DeleteAudioProcessingUnits();
   460 			}
   461 		}
   462 	else if (aMode == EMMFStateIdle)
   463 		{
   464 		// Unsure about this
   465 		iAudioContext->UnregisterAudioContextObserver(*iCurrentAudioControl);
   466 		iCurrentAudioControl =  static_cast<CDevAudioControl*>(iDevGenControl);
   467 		err = iAudioContext->RegisterAudioContextObserver(*iCurrentAudioControl);
   468 		}
   469 	else
   470 		{
   471 		iCurrentAudioControl = static_cast<CDevAudioControl*>(iDevGenControl);
   472 		err = KErrNotSupported;
   473 		}
   474 
   475 	if (err == KErrNone)
   476 		{
   477 		err = iCurrentAudioControl->Initialize(aFormat);	
   478 		}
   479 	
   480 	if(err == KErrNone)
   481 		{
   482 		iMode = aMode;
   483 		iFormat = aFormat;
   484 		}
   485 
   486 	DP0_RET(err, "%d");
   487 	}
   488 
   489 // -----------------------------------------------------------------------------
   490 // CDevAudio::CancelInitialize
   491 // -----------------------------------------------------------------------------
   492 //
   493 TInt CDevAudio::CancelInitialize()
   494 	{
   495 	DP_CONTEXT(CDevAudio::CancelInitialize *CD1*, CtxDevSound, DPLOCAL);
   496 	DP_IN();
   497 	TInt err(KErrNone);
   498 
   499 	// Redo partial cancelling of initialization after pre-emption clash event in
   500 	// EDevSoundAdaptorRemovingProcessingUnits state.
   501 	if (iActiveState == EDevSoundAdaptorUnitialised_Uninitialised &&
   502 			iPreviousState == EDevSoundAdaptorRemovingProcessingUnits)
   503 		{
   504 		err = iCurrentAudioControl->RemoveProcessingUnits();
   505 		DP0_RET(err, "%d");
   506 		}
   507 	else if(iActiveState != EDevSoundAdaptorInitialised_Initialised)
   508 		{
   509 		DP0_RET(KErrNotReady, "%d");
   510 		}
   511 
   512 	err = iCurrentAudioControl->Uninitialize();
   513 	
   514 	DP0_RET(err, "%d");
   515 	}
   516 
   517 // -----------------------------------------------------------------------------
   518 // CDevAudio::GetAudioControl
   519 // -----------------------------------------------------------------------------
   520 //
   521 CDevAudioControl* CDevAudio::GetAudioControl()
   522 	{
   523 	DP_CONTEXT(CDevAudio::GetAudioControl *CD1*, CtxDevSound, DPLOCAL);
   524 	DP_IN();
   525 	DP_OUT();
   526 	return iCurrentAudioControl;
   527 	}
   528 
   529 // -----------------------------------------------------------------------------
   530 // CDevAudio::SetPrioritySettings
   531 // -----------------------------------------------------------------------------
   532 //
   533 TInt CDevAudio::SetPrioritySettings(const TMMFPrioritySettings& aPrioritySettings)
   534 	{
   535 	DP_CONTEXT(CDevAudio::SetPrioritySettings *CD1*, CtxDevSound, DPLOCAL);
   536 	DP_IN();
   537 	iPrioritySettings.iPriority = aPrioritySettings.iPriority;
   538 	iPrioritySettings.iPref = aPrioritySettings.iPref;
   539 	iPriorityFlag = ETrue;
   540 	DP2(DLINFO, "Priority = 0x%x Preference = 0x%x", iPrioritySettings.iPriority,iPrioritySettings.iPref);
   541 	DP0_RET(0, "%d");
   542 	}
   543 
   544 // -----------------------------------------------------------------------------
   545 // CDevAudio::GetPrioritySettings
   546 // -----------------------------------------------------------------------------
   547 //
   548 void CDevAudio::GetPrioritySettings(TAudioTypeSettings& aPrioritySettings)
   549 	{
   550 	aPrioritySettings = iPrioritySettings;
   551 	}
   552 // -----------------------------------------------------------------------------
   553 // CDevAudio::IsPrioritySet
   554 // -----------------------------------------------------------------------------
   555 //
   556 TBool CDevAudio::IsPrioritySet()
   557 	{
   558 	return iPriorityFlag;
   559 	}
   560 
   561 // -----------------------------------------------------------------------------
   562 // CDevAudio::SetClientConfig
   563 // -----------------------------------------------------------------------------
   564 //
   565 TInt CDevAudio::SetClientConfig(const TProcessId& aProcessId)
   566 	{
   567 	DP_CONTEXT(CDevAudio::SetClientConfig *CD1*, CtxDevSound, DPLOCAL);
   568 	DP_IN();
   569 	// TODO: Check if the TClientContextSettings atributte go back since 
   570 	// if not there is no way to send the vendor id
   571 	TClientContextSettings context;
   572 	context.iProcessId = aProcessId;
   573 	TInt err = iAudioContext->SetClientSettings(context);
   574 	if (err != KErrNone)
   575 		{
   576 		DP1(DLERR, "Error %d setting client context!",err);
   577 		}
   578 	DP0_RET(err, "%d");
   579 	}
   580 
   581 TInt CDevAudio::SetClientConfig(const TProcessId& aActualProcessId, const TProcessId& aProcessId)
   582 	{
   583 	DP_CONTEXT(CDevAudio::SetClientConfig *CD1*, CtxDevSound, DPLOCAL);
   584 	DP_IN();
   585 	
   586 	MContextSetEffectiveClient* setEffectiveClient
   587         = static_cast<MContextSetEffectiveClient*>(iAudioContext->Interface(KSetClientInfoUid));
   588 	
   589 	TInt err;
   590 	
   591 	if (!setEffectiveClient)
   592 	    {
   593         DP0(DLINFO, "MContextSetEffectiveClient not supported, revert to old behaviour of just passing actual client info");
   594         err = SetClientConfig(aActualProcessId);
   595 	    }
   596 	else
   597 	    {
   598         TClientContextSettings context;
   599         context.iProcessId = aProcessId;
   600         err = iAudioContext->SetClientSettings(context);
   601         if (err != KErrNone)
   602             {
   603             DP1(DLERR, "Error %d setting client context!",err);
   604             }
   605         if (!err)
   606             {
   607             err = setEffectiveClient->SetEffectiveClientInfo(aActualProcessId);
   608             if (err != KErrNone)
   609                 {
   610                 DP1(DLERR, "Error %d setting effective client context!",err);
   611                 }
   612             }
   613 	    }
   614 
   615 	DP0_RET(err, "%d");
   616 	}
   617 
   618 
   619 void CDevAudio::ContextEvent(TUid /*aEvent*/, TInt /*aError*/)
   620 	{
   621 	DP_CONTEXT(CDevAudio::ContextEvent *CD1*, CtxDevSound, DPLOCAL);
   622 	DP_IN();
   623 	DP_OUT();
   624 	}
   625 
   626 // -----------------------------------------------------------------------------
   627 // CDevAudio::ActiveState
   628 // -----------------------------------------------------------------------------
   629 //
   630 TDevSoundAdaptorState CDevAudio::ActiveState() const
   631 	{
   632 	DP_CONTEXT(CDevAudio::ActiveState *CD1*, CtxDevSound, DPLOCAL);
   633 	DP_IN();
   634 	DP0_RET(iActiveState, "%d");
   635 	}
   636 
   637 // -----------------------------------------------------------------------------
   638 // CDevAudio::ActiveState
   639 // -----------------------------------------------------------------------------
   640 //
   641 TDevSoundAdaptorState CDevAudio::PreviousState() const
   642 	{
   643 	DP_CONTEXT(CDevAudio::PreviousState *CD1*, CtxDevSound, DPLOCAL);
   644 	DP_IN();
   645 	DP0_RET(iPreviousState, "%d");
   646 	}
   647 
   648 // -----------------------------------------------------------------------------
   649 // CDevAudio::SetActiveState
   650 // -----------------------------------------------------------------------------
   651 //
   652 void CDevAudio::SetActiveState(TDevSoundAdaptorState aAdaptorState)
   653 	{
   654 	DP_CONTEXT(CDevAudio::SetActiveState *CD1*, CtxDevSound, DPLOCAL);
   655 	DP_IN();
   656 	iActiveState = aAdaptorState;
   657 	DP_OUT();
   658 	}
   659 
   660 // -----------------------------------------------------------------------------
   661 // CDevAudio::SetPreviousState
   662 // -----------------------------------------------------------------------------
   663 //
   664 void CDevAudio::SetPreviousState(TDevSoundAdaptorState aAdaptorState)
   665 	{
   666 	DP_CONTEXT(CDevAudio::SetPreviousState *CD1*, CtxDevSound, DPLOCAL);
   667 	DP_IN();
   668 	iPreviousState = aAdaptorState;
   669 	DP_OUT();
   670 	}
   671 
   672 // -----------------------------------------------------------------------------
   673 // CDevAudio::SetDevSoundVolume
   674 // -----------------------------------------------------------------------------
   675 //
   676 TInt CDevAudio::SetDevSoundVolume(TInt aVolume, TBool& aAsyncComplete)
   677 	{
   678 	DP_CONTEXT(CDevAudio::SetDevSoundVolume *CD1*, CtxDevSound, DPLOCAL);
   679 	DP_IN();
   680 
   681 	// Values are clipped between 0 and MaxGain. 
   682 	// because MAudioGainControl expects this to be done
   683 	TInt volume = aVolume;
   684 	if (volume < 0)
   685 		{
   686 		volume = 0;
   687 		}
   688 	else if (volume > iDevSoundMaxVolume)
   689 		{
   690 		volume = iDevSoundMaxVolume;
   691 		}
   692 	iDevSoundVolume = volume;
   693 	
   694 	TInt error = SetGainAndBalance(EFalse, aAsyncComplete);
   695 
   696 	DP0_RET(error, "%d");
   697 	}
   698 
   699 
   700 // -----------------------------------------------------------------------------
   701 // CDevAudio::SetDevSoundGain
   702 // -----------------------------------------------------------------------------
   703 //
   704 TInt CDevAudio::SetDevSoundGain(TInt aGain, TBool& aAsyncComplete)
   705 	{
   706 	DP_CONTEXT(CDevAudio::SetDevSoundGain *CD1*, CtxDevSound, DPLOCAL);
   707 	DP_IN();
   708 
   709 	// Values are clipped between 0 and MaxGain. 
   710 	// because MAudioGainControl expects this to be done
   711 	TInt gain = aGain;
   712 	if (gain < 0)
   713 		{
   714 		gain = 0;
   715 		}
   716 	else if (gain > iDevSoundMaxGain)
   717 		{
   718 		gain = iDevSoundMaxGain;
   719 		}
   720 	iDevSoundGain = gain;
   721 
   722 	TInt error = SetGainAndBalance(EFalse, aAsyncComplete);
   723 
   724 	DP0_RET(error, "%d");
   725 	}
   726 
   727 // -----------------------------------------------------------------------------
   728 // CDevAudio::SetDevSoundPlayBalance
   729 // -----------------------------------------------------------------------------
   730 //
   731 TInt CDevAudio::SetDevSoundPlayBalance(TInt aLeftBalance, TInt aRightBalance, TBool& aAsyncComplete)
   732 	{
   733 	DP_CONTEXT(CDevAudio::SetDevSoundPlayBalance *CD1*, CtxDevSound, DPLOCAL);
   734 	DP_IN();
   735 	
   736 	// Clipping values
   737 	TInt left = aLeftBalance;
   738 	if (left < 0)
   739 		{
   740 		left = 0;
   741 		}
   742 	else if (left > KMaxBalance)
   743 		{
   744 		left = KMaxBalance;
   745 		}
   746 	TInt right = aRightBalance;
   747 	if (right < 0)
   748 		{
   749 		right = 0;
   750 		}
   751 	else if (right > KMaxBalance)
   752 		{
   753 		right = KMaxBalance;
   754 		}
   755 	iDevSoundPlayBalance[KLeftChannel] = left;
   756 	iDevSoundPlayBalance[KRightChannel] = right;
   757 
   758 	TInt error = SetGainAndBalance(EFalse, aAsyncComplete);
   759 
   760 	DP0_RET(error, "%d");
   761 	}
   762 
   763 // -----------------------------------------------------------------------------
   764 // CDevAudio::SetDevSoundRecordBalance
   765 // -----------------------------------------------------------------------------
   766 //
   767 TInt CDevAudio::SetDevSoundRecordBalance(TInt aLeftBalance, TInt aRightBalance, TBool& aAsyncComplete)
   768 	{
   769 	DP_CONTEXT(CDevAudio::SetDevSoundRecordBalance *CD1*, CtxDevSound, DPLOCAL);
   770 	DP_IN();
   771 
   772 	// Clipping values	
   773 	TInt left = aLeftBalance;
   774 	if (left < 0)
   775 		{
   776 		left = 0;
   777 		}
   778 	else if (left > KMaxBalance)
   779 		{
   780 		left = KMaxBalance;
   781 		}
   782 	TInt right = aRightBalance;
   783 	if (right < 0)
   784 		{
   785 		right = 0;
   786 		}
   787 	else if (right > KMaxBalance)
   788 		{
   789 		right = KMaxBalance;
   790 		}
   791 	iDevSoundRecordBalance[KLeftChannel] = left;
   792 	iDevSoundRecordBalance[KRightChannel] = right;
   793 
   794 	TInt error = SetGainAndBalance(EFalse, aAsyncComplete);
   795 
   796 	DP0_RET(error, "%d");
   797 	}
   798 
   799 
   800 // -----------------------------------------------------------------------------
   801 // CDevAudio::SetVolumeRamp
   802 // -----------------------------------------------------------------------------
   803 //
   804 TInt CDevAudio::SetVolumeRamp(const TTimeIntervalMicroSeconds& aRampDuration)
   805 	{
   806 	DP_CONTEXT(CDevAudioControl::SetVolumeRamp *CD1*, CtxDevSound, DPLOCAL);
   807 	DP_IN();
   808 
   809 	iRampDuration = aRampDuration;
   810 	
   811 	DP0_RET(KErrNone,"%d");
   812 	}
   813 
   814 // -----------------------------------------------------------------------------
   815 // CDevAudio::RequestGainAndBalance
   816 // -----------------------------------------------------------------------------
   817 //
   818 TInt CDevAudio::RequestGainAndBalance(CDevAudioControl* aCallingControl)
   819 	{
   820 	DP_CONTEXT(CDevAudio::RequestGainAndBalance *CD1*, CtxDevSound, DPLOCAL);
   821 	DP_IN();
   822 	
   823 	TInt error = KErrNone;
   824 	if (aCallingControl != iCurrentAudioControl)
   825 		{
   826 		error = KErrNotReady;
   827 		}
   828 	else
   829 		{
   830 		TBool dummy;
   831 		error = SetGainAndBalance(ETrue, dummy);
   832 		}
   833 	DP0_RET(error, "%d");
   834 	}
   835 	
   836 // -----------------------------------------------------------------------------
   837 // CDevAudio::SetGainAndBalance
   838 // -----------------------------------------------------------------------------
   839 //
   840 TInt CDevAudio::SetGainAndBalance(TBool aBecomingActive, TBool& aAsyncComplete)
   841 	{
   842 	DP_CONTEXT(CDevAudio::SetGainAndBalance *CD1*, CtxDevSound, DPLOCAL);
   843 	DP_IN();
   844 	
   845 	TInt error = KErrNone;
   846 	aAsyncComplete = EFalse; // let's assume this - makes logic easier below
   847 	// Apply cached settings. 
   848 	// If aBecomingActive is true it indicates this call is doing before the MAudioStream::Activate() transition, 
   849 	// so if required will apply volume ramp and won't itself generate a Commit() call. 
   850 	// Otherwise, when Active, the calls made here will generate a Commit() 
   851 	// and aAsyncComplete is returned true unless the SetGain() call fails
   852 	if (aBecomingActive || iActiveState == EDevSoundAdaptorActive_Active) 
   853 		{
   854 		if (iCurrentAudioControl==iDevRecordControl)
   855 			{
   856 			// TODO assumes we are not mid Commit cycle
   857 			// we are recing, need to change current volume at A3F layer
   858 			error = iCurrentAudioControl->SetGains(iDevSoundGain, iDevSoundMaxGain, iDevSoundRecordBalance, 0, aBecomingActive);
   859 			if (error==KErrNone)
   860 				{
   861 				aAsyncComplete = ETrue;
   862 				}
   863 			}
   864 		else if (iCurrentAudioControl==iDevPlayControl || iCurrentAudioControl==iDevToneControl)
   865 			{
   866 			// TODO assumes we are not mid Commit cycle
   867 			// we are playing, need to change current volume at A3F layer
   868 			error = iCurrentAudioControl->SetGains(iDevSoundVolume, iDevSoundMaxVolume, iDevSoundPlayBalance, iRampDuration, aBecomingActive);
   869 			if (error==KErrNone)
   870 				{
   871 				aAsyncComplete = ETrue;
   872 				}
   873 			}
   874 		
   875 		} 
   876 
   877 		
   878 	DP0_RET(error, "%d");	
   879 	}
   880 
   881 // -----------------------------------------------------------------------------
   882 // CDevAudio::GetDevSoundPlayBalance
   883 // -----------------------------------------------------------------------------
   884 //
   885 void CDevAudio::GetDevSoundPlayBalance(TInt& aLeftBalance, TInt& aRightBalance)
   886 	{
   887 	DP_CONTEXT(CDevAudio::GetDevSoundPlayBalance *CD1*, CtxDevSound, DPLOCAL);
   888 	DP_IN();
   889 	aLeftBalance = iDevSoundPlayBalance[KLeftChannel];
   890 	aRightBalance = iDevSoundPlayBalance[KRightChannel];
   891 	DP_OUT();
   892 	}
   893 	
   894 
   895 // -----------------------------------------------------------------------------
   896 // CDevAudio::GetDevSoundRecordBalance
   897 // -----------------------------------------------------------------------------
   898 //
   899 void CDevAudio::GetDevSoundRecordBalance(TInt& aLeftBalance, TInt& aRightBalance)
   900 	{
   901 	DP_CONTEXT(CDevAudio::GetDevSoundRecordBalance *CD1*, CtxDevSound, DPLOCAL);
   902 	DP_IN();
   903 	aLeftBalance = iDevSoundRecordBalance[KLeftChannel];
   904 	aRightBalance = iDevSoundRecordBalance[KRightChannel];
   905 	DP_OUT();
   906 	}
   907 
   908 // -----------------------------------------------------------------------------
   909 // CDevAudio::DevSoundSupportedDataTypesL
   910 // -----------------------------------------------------------------------------
   911 //
   912 void CDevAudio::DevSoundSupportedDataTypesL(RArray<TFourCC>& aSupportedDataTypes, TUint aDataType)
   913 	{
   914 	DP_CONTEXT(CDevAudio::DevSoundSupportedDataTypesL *CD1*, CtxDevSound, DPLOCAL);
   915 	DP_IN();
   916 	
   917 	//Clear any existing data
   918 	aSupportedDataTypes.Reset();
   919 	
   920 	if(aDataType == KDataForPlay)
   921 		{
   922 		ConvertToFourCcL(aSupportedDataTypes, iSupportedInputFormats);
   923 		}
   924 	else if(aDataType == KDataForRecord)
   925 		{
   926 		ConvertToFourCcL(aSupportedDataTypes, iSupportedOutputFormats);
   927 		}
   928 	
   929 	DP_OUT();
   930 	}
   931 	
   932 // -----------------------------------------------------------------------------
   933 // CDevAudio::ConvertToFourCcL
   934 // -----------------------------------------------------------------------------
   935 //
   936 void CDevAudio::ConvertToFourCcL(RArray<TFourCC>& aSupportedDataTypes, RArray<TUid>& aSupportedFormats)
   937 	{
   938 	DP_CONTEXT(CDevAudio::ConvertToFourCcL *CD1*, CtxDevSound, DPLOCAL);
   939 	DP_IN();
   940 	
   941 	if(aSupportedFormats.Count() > 0)
   942 		{
   943 		TFourCC fourCC;
   944 		
   945 		for(TInt element=0; element<aSupportedFormats.Count(); element++)
   946 			{		
   947 			User::LeaveIfError(const_cast<CFourCCConvertor&>(iGlobalProperties->GetFourCCConvertor()).FormatToFourCC(aSupportedFormats[element],fourCC));
   948 			aSupportedDataTypes.AppendL(fourCC);	
   949 			}
   950 		}
   951 	DP_OUT();
   952 	}
   953 
   954 
   955 // -----------------------------------------------------------------------------
   956 // CDevAudio::CreateAudioProcessingUnits
   957 // -----------------------------------------------------------------------------
   958 //
   959 TInt CDevAudio::CreateAudioProcessingUnits(TUid aSource, TUid aSink, TUid aCodec)
   960 	{
   961 	DP_CONTEXT(CDevAudio::CreateAudioProcessingUnits *CD1*, CtxDevSound, DPLOCAL);
   962 	DP_IN();
   963 	TInt err = iAudioContext->CreateAudioProcessingUnit(aSource, iAudioSource);
   964 	if(err != KErrNone)
   965 		{
   966 		DP0_RET(err,"Audio source creation failed!");
   967 		}
   968 	err = iAudioContext->CreateAudioProcessingUnit(aSink, iAudioSink);
   969 	if(err != KErrNone)
   970 		{
   971 		DP0_RET(err,"Audio sink creation failed!");
   972 		}
   973 	err = iAudioContext->CreateAudioProcessingUnit(aCodec, iAudioCodec);
   974 	if(err != KErrNone)
   975 		{
   976 		DP0_RET(err,"Audio codec creation failed!");
   977 		}
   978 
   979 	DP0_RET(err,"");
   980 	}
   981 
   982 // -----------------------------------------------------------------------------
   983 // CDevAudio::DeleteAudioProcessingUnits
   984 // -----------------------------------------------------------------------------
   985 //
   986 void CDevAudio::DeleteAudioProcessingUnits()
   987 	{
   988 	DP_CONTEXT(CDevAudio::DeleteAudioProcessingUnits *CD1*, CtxDevSound, DPLOCAL);
   989 	DP_IN();
   990 	if (iAudioSource)
   991 		{
   992 		iAudioContext->DeleteAudioProcessingUnit(iAudioSource);
   993 		iAudioSource = NULL;
   994 		}
   995 	if (iAudioSink)
   996 		{
   997 		iAudioContext->DeleteAudioProcessingUnit(iAudioSink);
   998 		iAudioSink = NULL;
   999 		}
  1000 	if (iAudioCodec)
  1001 		{
  1002 		iAudioContext->DeleteAudioProcessingUnit(iAudioCodec);
  1003 		iAudioCodec = NULL;
  1004 		}
  1005 	DP_OUT();
  1006 	}
  1007 
  1008 TInt CDevAudio::CommitAudioContext()
  1009 	{
  1010 	DP_CONTEXT(CDevAudio::CommitAudioContext *CD1*, CtxDevSound, DPLOCAL);
  1011 	DP_IN();
  1012 
  1013 	//If we are in mid state then Panic as DevSound server-side session (CMMFDevSoundSession) is not blocking properly
  1014 	__ASSERT_DEBUG(!IsMidState(iActiveState), Panic(EValidStateBeforeCommit));
  1015 
  1016 	TInt err = KErrNone;
  1017 	iPreviousState = iActiveState;
  1018 	err = iAudioContext->Commit();
  1019 
  1020 	DP0_RET(err,"%d");
  1021 	}
  1022 
  1023 TBool CDevAudio::IsMidState(TDevSoundAdaptorState aAdaptorState)
  1024 	{
  1025 	DP_CONTEXT(CDevAudio::IsMidState *CD1*, CtxDevSound, DPLOCAL);
  1026 	DP_IN();
  1027 	if (aAdaptorState == EDevSoundAdaptorRemovingProcessingUnits || aAdaptorState == EDevSoundAdaptorUninitialising ||
  1028 			aAdaptorState == EDevSoundAdaptorInitialising || aAdaptorState == EDevSoundAdaptorLoading ||
  1029 			aAdaptorState == EDevSoundAdaptorUnloading || aAdaptorState == EDevSoundAdaptorStopping ||
  1030 			aAdaptorState == EDevSoundAdaptorActivating || aAdaptorState == EDevSoundAdaptorPausing)
  1031 		{
  1032 		DP0_RET(ETrue,"%d");
  1033 		}
  1034 	DP0_RET(EFalse,"%d");
  1035 	}
  1036 
  1037 // -----------------------------------------------------------------------------
  1038 // From MA3FDevSoundAutoPauseResume
  1039 // CDevAudio::RegisterAsClient
  1040 // -----------------------------------------------------------------------------
  1041 //
  1042 TInt CDevAudio::RegisterAsClient(TUid aEventType, const TDesC8& aNotificationRegistrationData)
  1043 	{
  1044 	DP_CONTEXT(CDevAudio::RegisterAsClient *CD1*, CtxDevSound, DPLOCAL);
  1045 	DP_IN();
  1046 	TInt err = KErrNotSupported;
  1047 	MA3FDevSoundAutoPauseResume* iAPR =  static_cast<MA3FDevSoundAutoPauseResume*>(iAudioContext->Interface(KUIdAudioResourceNotification));
  1048 	if(iAPR)
  1049 		{
  1050 		err = iAPR->RegisterAsClient(aEventType, aNotificationRegistrationData, this);
  1051 		}
  1052 	DP0_RET(err, "%d");	
  1053 	}
  1054 	
  1055 
  1056 // -----------------------------------------------------------------------------
  1057 // CDevAudio::CancelRegisterAsClient
  1058 // -----------------------------------------------------------------------------
  1059 //
  1060 TInt CDevAudio::CancelRegisterAsClient(TUid aEventType)
  1061 	{
  1062 	DP_CONTEXT(CDevAudio::CancelRegisterAsClient *CD1*, CtxDevSound, DPLOCAL);
  1063 	DP_IN();
  1064 	TInt err = KErrNotSupported;
  1065 	MA3FDevSoundAutoPauseResume* iAPR =  static_cast<MA3FDevSoundAutoPauseResume*>(iAudioContext->Interface(KUIdAudioResourceNotification));
  1066 	if(iAPR)
  1067 		{
  1068 		err = iAPR->CancelRegisterAsClient(aEventType);
  1069 		}
  1070 	DP0_RET(err, "%d");	
  1071 	}
  1072 
  1073 
  1074 // -----------------------------------------------------------------------------
  1075 // CDevAudio::WillResumePlay
  1076 // -----------------------------------------------------------------------------
  1077 //
  1078 TInt CDevAudio::WillResumePlay()
  1079 	{
  1080 	DP_CONTEXT(CDevAudio::WillResumePlay *CD1*, CtxDevSound, DPLOCAL);
  1081 	DP_IN();
  1082 	TInt err = KErrNotSupported;
  1083 	MA3FDevSoundAutoPauseResume* iAPR =  static_cast<MA3FDevSoundAutoPauseResume*>(iAudioContext->Interface(KUIdAudioResourceNotification));
  1084 	if(iAPR)
  1085 		{
  1086 		err = iAPR->WillResumePlay();
  1087 		}
  1088 	DP0_RET(err, "%d");	
  1089 	}
  1090 
  1091 
  1092 // -----------------------------------------------------------------------------
  1093 // CDevAudio::NotifyResume
  1094 // -----------------------------------------------------------------------------
  1095 //
  1096 void CDevAudio::NotifyResume()
  1097 	{
  1098 	DP_CONTEXT(CDevAudio::NotifyResume *CD1*, CtxDevSound, DPLOCAL);
  1099 	DP_IN();
  1100 	iAdaptationObserver.CallbackFromAdaptorReceived(KCallbackAutoPauseResume, KErrNone);
  1101 	DP_OUT();
  1102 	}
  1103 
  1104 void CDevAudio::Panic(TMMFDevAudioPanicCodes aCode)
  1105 	{
  1106 	User::Panic(KMMFDevAudioPanicCategory, aCode);
  1107 	}
  1108 
  1109 // End of file