os/mm/mmhais/refacladapt/src/audiocodec/audiocodec.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 //audiocodec.cpp
     2 
     3 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     4 // All rights reserved.
     5 // This component and the accompanying materials are made available
     6 // under the terms of "Eclipse Public License v1.0"
     7 // which accompanies this distribution, and is available
     8 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     9 //
    10 // Initial Contributors:
    11 // Nokia Corporation - initial contribution.
    12 //
    13 // Contributors:
    14 //
    15 // Description:
    16 //
    17 
    18 
    19 
    20 #include <a3f/a3fbase.h>
    21 #include <a3f/a3ffourcclookup.h>
    22 #include <a3f/audioprocessingunittypeuids.h>
    23 #include "maudiocodecadaptationobserver.h"
    24 #include "audiocodec.h"
    25 #include "resourcedata.h"
    26 
    27 // THIS WILL REMAIN UNTIL THE REQUIREMENT WILL BE DEFINED
    28 // THIS IMPLEMENTATION WILL WORK ONLY FOR HW DEVICES IMPLEMNETED AS SOFTWARE CODEC WRAPPERS
    29 #define INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
    30 
    31 #ifdef INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
    32 #include <mmf/server/mmfswcodecwrappercustominterfacesuids.hrh>
    33 #endif
    34 
    35 const TInt KMicroSecsInOneSec = 1000000;
    36 const TInt KHwDeviceDefaultDataLength= 9;
    37 const TUint KDefaultMaxGain = 255; // TODO should discover at run time but this is value of current Symbian sound driver adaptor
    38 
    39 //Values to find the request ask by the client
    40 const TInt KRequestSampleRate = 0x0001;
    41 const TInt KRequestMode = 0x0002;
    42 const TAudioModeTableEntry KModeTableLookup[] = {
    43 							{ EMMFMono, {KA3FModeMonoValue} },
    44 							{ EMMFStereo, {KA3FModeStereoNonInterleavedValue} },
    45 							};
    46 
    47 
    48 // ---------------------------------------------------------------------------
    49 // Constructor
    50 // ---------------------------------------------------------------------------
    51 CAudioCodec::CAudioCodec(TUid aTypeId, const CFourCCConvertor& aFourCCConvertor)
    52 	: CActive(EPriorityStandard),
    53 	iLastBuffer(EFalse),
    54 	iHwDeviceState(EIdle),
    55 	iLastBufferAck(EFalse), 
    56 	iRampOperation(KNullUid)
    57 	{
    58 	TRACE_CREATE();
    59 	DP_CONTEXT(CAudioCodec::CAudioCodec *CD1*, CtxDevSound, DPLOCAL);
    60 	DP_IN();
    61 	if(aTypeId==KUidAudioEncoder)
    62 		{
    63 		iMode = EEncode;
    64 		}
    65 	else if(aTypeId==KUidAudioDecoder)
    66 		{
    67 		iMode = EDecode;
    68 		}
    69 	iFourCCConvertor = static_cast<CFourCCConvertor*>( const_cast<CFourCCConvertor*>(&aFourCCConvertor) );
    70 
    71 	iHwDeviceInitArgs.iEapStreamId = 0;
    72 	iHwDeviceInitArgs.iPolicyId = 0;
    73 	DP_OUT();
    74 	}
    75 
    76 // ---------------------------------------------------------------------------
    77 // Factory method
    78 // ---------------------------------------------------------------------------
    79 EXPORT_C CAudioCodec* CAudioCodec::NewL(TUid aTypeId, const CFourCCConvertor& aFourCCConvertor)
    80 	{
    81 	DP_STATIC_CONTEXT(CAudioCodec::NewL *CD0*, CtxDevSound, DPLOCAL);
    82 	DP_IN();
    83 	CAudioCodec* self = new(ELeave)CAudioCodec(aTypeId, aFourCCConvertor);
    84 	CleanupStack::PushL(self);
    85 	self->ConstructL();
    86 	CleanupStack::Pop(self);
    87 	DP0_RET(self, "0x%x");
    88 	}
    89 
    90 // ---------------------------------------------------------------------------
    91 // Second phase constructor
    92 // ---------------------------------------------------------------------------
    93 void CAudioCodec::ConstructL()
    94 	{
    95 	DP_CONTEXT(CAudioCodec::ConstructL *CD1*, CtxDevSound, DPLOCAL);
    96 	DP_IN();
    97 	CActiveScheduler::Add(this);
    98 	DP_OUT();
    99 	}
   100 
   101 // ---------------------------------------------------------------------------
   102 // Destructor
   103 // ---------------------------------------------------------------------------
   104 CAudioCodec::~CAudioCodec()
   105 	{
   106 	DP_CONTEXT(CAudioCodec::~CAudioCodec *CD1*, CtxDevSound, DPLOCAL);
   107 	DP_IN();
   108 	iAudioCodecObservers.Close();
   109 	if (iHwDevice)
   110 		{
   111 		if (!iHwDeviceState == EStopped)
   112 			{
   113 			DP0(DLINFO, "StopAndDeleteCodec");
   114 			iHwDevice->StopAndDeleteCodec();
   115 			}
   116 		delete iHwDevice;
   117 		iHwDevice = NULL;
   118 		}
   119 	Cancel();
   120 	DP_OUT();
   121 	}
   122 
   123 // ---------------------------------------------------------------------------
   124 // CAudioCodec::SetFormat
   125 // ---------------------------------------------------------------------------
   126 EXPORT_C TInt CAudioCodec::SetFormat(TUid aFormat)
   127 	{
   128 	DP_CONTEXT(CAudioCodec::SetFormat *CD1*, CtxDevSound, DPLOCAL);
   129 	DP_IN();
   130 	iFormat = aFormat;
   131 	DP0_RET(KErrNone, "%d");
   132 	}
   133 
   134 // ---------------------------------------------------------------------------
   135 // CAudioCodec::Initialize
   136 // ---------------------------------------------------------------------------
   137 EXPORT_C TInt CAudioCodec::Initialize()
   138 	{
   139 	DP_CONTEXT(CAudioCodec::Initialize *CD1*, CtxDevSound, DPLOCAL);
   140 	DP_IN();
   141 	TInt err(KErrNone);
   142 	TFourCC aFourCC(0);
   143 	err = iFourCCConvertor->FormatToFourCC(iFormat,aFourCC);
   144 	if(err == KErrNotFound)
   145 		{
   146 		err = KErrNotSupported;
   147 		}
   148 	if(err == KErrNone)
   149 		{
   150 		TRAP(err,  FourCCToHwDeviceUidL(aFourCC, iMode, iDeviceUid) );
   151 		}
   152 	if (err == KErrNone)
   153 		{
   154 		err = LoadHwDevice();
   155 		}
   156 		
   157 #ifdef INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
   158 	if (err == KErrNone)
   159 		{
   160 		TUid interfaceUid = {0};
   161 		TAny* interface = NULL;
   162 		if (iMode == EDecode)
   163 			{
   164 			interfaceUid.iUid = KMmfPlaySettingsCustomInterface;
   165 			}
   166 		else if(iMode == EEncode)
   167 			{
   168 			interfaceUid.iUid = KMmfRecordSettingsCustomInterface;
   169 			}
   170 		
   171 		// This call at the sw codec wrapper creates the custom interfaces for playing and record
   172 		if(iHwDevice)
   173 			{
   174 			interface = iHwDevice->CustomInterface(interfaceUid);
   175 			if (iMode == EDecode)
   176 				{
   177 				iPlayCustomInterface = static_cast<MPlayCustomInterface*>(interface);
   178 				}
   179 			else if(iMode == EEncode)
   180 				{
   181 				iRecordCustomInterface = static_cast<MRecordCustomInterface*>(interface);
   182 				}
   183 			}
   184 		}
   185 
   186 #endif
   187 	if(err == KErrNone)
   188 		{
   189 		err = InitHwDevice();
   190 		}
   191 	DP0_RET(err, "%d");
   192 	}
   193 	
   194 // ---------------------------------------------------------------------------
   195 // CAudioCodec::Load
   196 // ---------------------------------------------------------------------------
   197 EXPORT_C TInt CAudioCodec::Load(TInt aSampleRate, TUid aMode)
   198 	{
   199 	DP_CONTEXT(CAudioCodec::Load *CD1*, CtxDevSound, DPLOCAL);
   200 	DP_IN();
   201 	iSampleRateConfig = aSampleRate;
   202 	iModeConfig = aMode;
   203 	TInt err(KErrNone);
   204 	err = SetHwDeviceConfig();
   205 	DP0_RET(err, "%d");
   206 	}
   207 
   208 // ---------------------------------------------------------------------------
   209 // CAudioCodec::Start
   210 // ---------------------------------------------------------------------------
   211 EXPORT_C TInt CAudioCodec::Start()
   212 	{
   213 	DP_CONTEXT(CAudioCodec::Start *CD1*, CtxDevSound, DPLOCAL);
   214 	DP_IN();
   215 	TInt err(KErrNone);
   216 	iLastBuffer = EFalse;
   217 	
   218 	// Needed since ToneHwDevice only exchange one buffer with its client
   219 	// Resume shouldn't clear the last buffer flag 
   220 	// since this wont' result on a new buffer request
   221 	if(iHwDeviceState == EPaused && iDeviceUid.iUid == KUidToneHwDevice)
   222 		{
   223 		iLastBuffer = ETrue;
   224 		}
   225 
   226 	if (iHwDevice)
   227 		{
   228 		PRF(PRF_ID, PRF_START, PRF_TIME, AA_DS_StartHwDevice, "");
   229 		if (iMode == EDecode)
   230 			{
   231 			// PLAY
   232 			err = iHwDevice->Start(EDevDecode, EDevOutFlow);
   233 			DP1(DLINFO,"hwdevice decode start: error %d",err);
   234 			iIgnoreUnderflowInterface = reinterpret_cast<MIgnoreUnderflowEventsCustomInterface*>(iHwDevice->CustomInterface(KIgnoreUnderflowCustomInterfaceTypeUid));	
   235 			if(iIgnoreUnderflowInterface)
   236 				{
   237 				iIgnoreUnderflowInterface->IgnoreUnderflowEvents();
   238 				}
   239 			}
   240 		else if (iMode == EEncode)
   241 			{
   242 			// RECORD
   243 			err = iHwDevice->Start(EDevEncode, EDevInFlow);
   244 			DP1(DLINFO,"hwdevice encode start: error %d",err);
   245 			}
   246 		else
   247 			{
   248 			err = KErrNotSupported;
   249 			DP1(DLINFO,"CAudioCodec::StartHwDevice - Unknown mode %d", iMode);
   250 			}
   251 		PRF(PRF_ID, PRF_STOP, PRF_TIME, AA_DS_StartHwDevice, "");
   252 		}
   253 	else
   254 		{
   255 		err = KErrHardwareNotAvailable;
   256 		}
   257 
   258 	if (err == KErrNone)
   259 		{
   260 		iHwDeviceState = ERunning;
   261 		}
   262 	iLastBufferAck = EFalse;	
   263 	DP0_RET(err, "%d");
   264 	}
   265 
   266 // ---------------------------------------------------------------------------
   267 // CAudioCodec::Pause
   268 // ---------------------------------------------------------------------------
   269 EXPORT_C TInt CAudioCodec::Pause()
   270 	{
   271 	DP_CONTEXT(CAudioCodec::Pause *CD1*, CtxDevSound, DPLOCAL);
   272 	DP_IN();
   273 	TInt err(KErrNone);
   274 	if (iHwDevice)
   275 		{
   276 		DP0(DLINFO,"Pause hwdevice");
   277 		err = iHwDevice->Pause();
   278 		}
   279 	iHwDeviceState = EPaused;
   280 	DP0_RET(err, "%d");
   281 	}
   282 
   283 // ---------------------------------------------------------------------------
   284 // CAudioCodec::Stop
   285 // ---------------------------------------------------------------------------
   286 EXPORT_C TInt CAudioCodec::Stop()
   287 	{
   288 	DP_CONTEXT(CAudioCodec::Stop *CD1*, CtxDevSound, DPLOCAL);
   289 	DP_IN();
   290 	TInt err(KErrNone);
   291 	if (iHwDevice && !(iHwDeviceState == EStopped)) 
   292 		{
   293 		DP0(DLINFO,"Stop hwdevice");
   294 		UpdateBytesPlayed();
   295 		err = iHwDevice->Stop();
   296 		iHwDeviceState = EStopped;
   297 		}
   298 	iLastBufferAck = EFalse;
   299 	DP0_RET(err, "%d");
   300 	}
   301 
   302 // ---------------------------------------------------------------------------
   303 // CAudioCodec::GetSupportedSampleRates
   304 // ---------------------------------------------------------------------------
   305 TInt CAudioCodec::GetSupportedSampleRates(RArray<TInt>& aSupportedRates)
   306 	{
   307 	DP_CONTEXT(CAudioCodec::GetSupportedSampleRates *CD1*, CtxDevSound, DPLOCAL);
   308 	DP_IN();
   309 	TInt err(KErrNone);
   310 	aSupportedRates.Reset();
   311 	// Ask for rates from resource files
   312 	err = ReadResourceRates(aSupportedRates);
   313 
   314 	DP0_RET(err, "%d");
   315 	}
   316 
   317 // ---------------------------------------------------------------------------
   318 // CAudioCodec::GetSupportedModes
   319 // ---------------------------------------------------------------------------
   320 TInt CAudioCodec::GetSupportedModes(RArray<TUid>& aSupportedModes)
   321 	{
   322 	DP_CONTEXT(CAudioCodec::GetSupportedModes *CD1*, CtxDevSound, DPLOCAL);
   323 	DP_IN();
   324 	TInt err(KErrNone);
   325 	aSupportedModes.Reset();
   326 	// Ask for modes from resource files
   327 	err = ReadResourceModes(aSupportedModes);
   328 
   329 	DP0_RET(err, "%d");
   330 	}
   331 
   332 // ---------------------------------------------------------------------------
   333 // CAudioCodec::GetCustomInterface
   334 // ---------------------------------------------------------------------------
   335 TInt CAudioCodec::RequestCustomInterface(TUid aUid, TAny*& aPtr)
   336 	{
   337 	DP_CONTEXT(CAudioCodec::GetCustomInterface *CD1*, CtxDevSound, DPLOCAL);
   338 	DP_IN();
   339 	TInt err = KErrNone;
   340 	if(iHwDevice)
   341 		{
   342 		aPtr = iHwDevice->CustomInterface(aUid);
   343 		}
   344 	else
   345 		{
   346 		err = KErrNotReady;
   347 		}
   348 	DP0_RET(err, "%d");
   349 	}
   350 
   351 // ---------------------------------------------------------------------------
   352 // CAudioCodec::RegisterObserver
   353 // ---------------------------------------------------------------------------
   354 TInt CAudioCodec::RegisterObserver(MCustomInterfaceSupportObserver& aObserver)
   355 	{
   356 	DP_CONTEXT(CAudioCodec::RegisterObserver *CD1*, CtxDevSound, DPLOCAL);
   357 	DP_IN();
   358 	TInt err = KErrNone;
   359 	err = iCISupportObservers.Find(&aObserver);
   360 	if( err != KErrNotFound )
   361 		{
   362 		err = KErrAlreadyExists;
   363 		}
   364 	else
   365 		{
   366 		err = iCISupportObservers.Append(&aObserver);
   367 		}
   368 	DP0_RET(err, "%d");
   369 	}
   370 
   371 // ---------------------------------------------------------------------------
   372 // CAudioCodec::UnRegisterObserver
   373 // ---------------------------------------------------------------------------
   374 void CAudioCodec::UnRegisterObserver(MCustomInterfaceSupportObserver& aObserver)
   375 	{
   376 	DP_CONTEXT(CAudioCodec::UnRegisterObserver *CD1*, CtxDevSound, DPLOCAL);
   377 	DP_IN();
   378 	TInt idxOrErr = iCISupportObservers.Find(&aObserver);
   379 	if( idxOrErr != KErrNotFound )
   380 		{
   381 		iCISupportObservers.Remove(idxOrErr);
   382 		}
   383 	DP_OUT();
   384 	}
   385 
   386 // ---------------------------------------------------------------------------
   387 // CAudioCodec::LoadHwDevice
   388 // ---------------------------------------------------------------------------
   389 TInt CAudioCodec::LoadHwDevice()
   390 	{
   391 	DP_CONTEXT(CAudioCodec::LoadHwDevice *CD1*, CtxDevSound, DPLOCAL);
   392 	DP_IN();
   393 	TInt err(KErrNone);
   394 
   395 	if(iDeviceUid==KNullUid)
   396 		{
   397 		DP0_RET(KErrNotSupported, "%d");
   398 		}
   399 
   400 	TRAP(err, iHwDevice = CMMFHwDevice::NewL(iDeviceUid));
   401 	if(err == KErrNotFound)
   402 		{
   403 		err = KErrNotSupported;
   404 		}
   405 	DP0_RET(err, "%d");
   406 	}
   407 
   408 // ---------------------------------------------------------------------------
   409 // CAudioCodec::InitHwDevice
   410 // ---------------------------------------------------------------------------
   411 TInt CAudioCodec::InitHwDevice()
   412 	{
   413 	DP_CONTEXT(CAudioCodec::InitHwDevice *CD1*, CtxDevSound, DPLOCAL);
   414 	DP_IN();
   415 	TInt err(KErrNone);
   416 	DP1(DLINFO, "eapStreamId %d", iHwDeviceInitArgs.iEapStreamId);
   417 	DP1(DLINFO, "policyId %d", iHwDeviceInitArgs.iPolicyId);
   418 
   419 	iHwDeviceInitParams.iHwDeviceObserver = this;
   420 	iHwDeviceInitParams.iHwDeviceInitArgsPtr.Set((TUint8*)&(iHwDeviceInitArgs),
   421 														sizeof(THwDeviceInitArgs),
   422 														sizeof(THwDeviceInitArgs));
   423 	if (iHwDevice)
   424 		{
   425 		err = iHwDevice->Init(iHwDeviceInitParams);
   426 		}
   427 	else
   428 		{
   429 		err = KErrHardwareNotAvailable;
   430 		}
   431 	DP0_RET(err, "%d");
   432 	}
   433 
   434 // ---------------------------------------------------------------------------
   435 // CAudioCodec::SetHwDeviceConfig
   436 // ---------------------------------------------------------------------------
   437 TInt CAudioCodec::SetHwDeviceConfig()
   438 	{
   439 	DP_CONTEXT(CAudioCodec::SetHwDeviceConfig *CD1*, CtxDevSound, DPLOCAL);
   440 	DP_IN();
   441 	TInt err(KErrNone);
   442 
   443 	//TODO Look for another way to set those parameters
   444 	TTaskConfig taskconfig;
   445 	taskconfig.iUid = KUidRefDevSoundTaskConfig;
   446 	taskconfig.iStereoMode = ResolveMode(iModeConfig);
   447 	taskconfig.iRate = iSampleRateConfig;
   448 
   449 	DP1(DLINFO, "taskconfig.iRate %d", taskconfig.iRate);
   450 	DP1(DLINFO, "taskconfig.iStereoMode %d", taskconfig.iStereoMode);
   451 	DP1(DLINFO, "taskconfig.iUid %d", taskconfig.iUid);
   452 
   453 	if (err == KErrNone && iHwDevice)
   454 		{
   455 		err = iHwDevice->SetConfig(taskconfig);
   456 		}
   457 	else
   458 		{
   459 		err = KErrHardwareNotAvailable;
   460 		}
   461 	DP0_RET(err, "%d");
   462 	}
   463 
   464 // ---------------------------------------------------------------------------
   465 // CAudioCodec::ReadResourceRates
   466 // ---------------------------------------------------------------------------
   467 TInt CAudioCodec::ReadResourceRates(RArray<TInt>& aRates)
   468 	{
   469 	DP_CONTEXT(CAudioCodec::ReadResourceRates*CD1*, CtxDevSound, DPLOCAL);
   470 	DP_IN();
   471 	TInt err(KErrNone);
   472 
   473 	CResourceData* resource = NULL;
   474 	TRAP(err, resource = CResourceData::NewL(iDeviceUid));
   475 	if (err == KErrNone && resource)
   476 		{
   477 		err = resource->GetSSampleRates(iMode,aRates);
   478 		delete resource;
   479 		}
   480 
   481 	DP0_RET(err, "%d");
   482 	}
   483 
   484 // ---------------------------------------------------------------------------
   485 // CAudioCodec::ReadResourceModes
   486 // ---------------------------------------------------------------------------
   487 TInt CAudioCodec::ReadResourceModes(RArray<TUid>& aModes)
   488 	{
   489 	DP_CONTEXT(CAudioCodec::ReadResourceModes*CD1*, CtxDevSound, DPLOCAL);
   490 	DP_IN();
   491 	TInt err(KErrNone);
   492 
   493 	CResourceData* resource = NULL;
   494 	TRAP(err, resource = CResourceData::NewL(iDeviceUid));
   495 	if (err == KErrNone && resource)
   496 		{
   497 		err = resource->GetSModes(iMode,aModes);
   498 		delete resource;
   499 		}
   500 
   501 	DP0_RET(err, "%d");
   502 	}
   503 
   504 // ---------------------------------------------------------------------------
   505 // from class MAudioComponent
   506 // CAudioCodec::GetOutputPort
   507 // ---------------------------------------------------------------------------
   508 TInt CAudioCodec::GetOutputPort(MOutputPort*& aOutputPort)
   509 	{
   510 	DP_CONTEXT(CAudioCodec::GetOutputPort *CD1*, CtxDevSound, DPLOCAL);
   511 	DP_IN();
   512 	aOutputPort = this;
   513 	DP0_RET(KErrNone, "%d");
   514 	}
   515 
   516 // ---------------------------------------------------------------------------
   517 // from class MAudioComponent
   518 // CAudioCodec::GetInputPort
   519 // ---------------------------------------------------------------------------
   520 TInt CAudioCodec::GetInputPort(MInputPort*& aInputPort)
   521 	{
   522 	DP_CONTEXT(CAudioCodec::GetInputPort *CD1*, CtxDevSound, DPLOCAL);
   523 	DP_IN();
   524 	aInputPort = this;
   525 	DP0_RET(KErrNone, "%d");
   526 	}
   527 
   528 // ---------------------------------------------------------------------------
   529 // from class MAudioComponent
   530 // CAudioCodec::RegisterAudioComponentObserver
   531 // ---------------------------------------------------------------------------
   532 EXPORT_C TInt CAudioCodec::RegisterAudioCodecObserver(MAudioCodecAdaptationObserver& aObserver)
   533 	{
   534 	DP_CONTEXT(CAudioCodec::RegisterAudioCodecObserver *CD1*, CtxDevSound, DPLOCAL);
   535 	DP_IN();
   536 	TInt err = iAudioCodecObservers.Find(&aObserver);
   537 	if(err == KErrNotFound)
   538 		{
   539 		iAudioCodecObservers.Append(&aObserver);
   540 		err = KErrNone;
   541 		}
   542 	else
   543 		{
   544 		err = KErrAlreadyExists;
   545 		}
   546 	DP0_RET(err,"%d");
   547 	}
   548 
   549 // ---------------------------------------------------------------------------
   550 // from class MAudioComponent
   551 // CAudioCodec::UnregisterAudioComponentObserver
   552 // ---------------------------------------------------------------------------
   553 EXPORT_C void CAudioCodec::UnregisterAudioCodecObserver(MAudioCodecAdaptationObserver& aObserver)
   554 	{
   555 	DP_CONTEXT(CAudioCodec::UnregisterAudioCodecObserver *CD1*, CtxDevSound, DPLOCAL);
   556 	DP_IN();
   557 	TInt idxOrErr = iAudioCodecObservers.Find(&aObserver);
   558 	if( idxOrErr != KErrNotFound )
   559 		{
   560 		iAudioCodecObservers.Remove(idxOrErr);
   561 		}
   562 	DP_OUT();
   563 	}
   564 
   565 // ---------------------------------------------------------------------------
   566 // from class MInputPort
   567 // CAudioCodec::EmptyBuffer
   568 // ---------------------------------------------------------------------------
   569 TInt CAudioCodec::EmptyBuffer(CMMFBuffer* /*aBuffer*/, MOutputPort* /*aSupplier*/)
   570 	{
   571 	DP_CONTEXT(CAudioCodec::EmptyBuffer *CD1*, CtxDevSound, DPLOCAL);
   572 	DP_IN();
   573 	DP0_RET(KErrNotReady, "%d");
   574 	}
   575 
   576 // ---------------------------------------------------------------------------
   577 // from class MInputPort
   578 // CAudioCodec::BufferFilled
   579 // ---------------------------------------------------------------------------
   580 TInt CAudioCodec::BufferFilled(CMMFBuffer* aBuffer)
   581 	{
   582 	DP_CONTEXT(CAudioCodec::BufferFilled *CD1*, CtxDevSound, DPLOCAL);
   583 	DP_IN();
   584 	PRF(PRF_ID, PRF_STOP, PRF_LOAD, AA_DS_Fill, "");
   585 	TInt err(KErrNone);
   586 	if(aBuffer->LastBuffer())
   587 		{
   588 		iLastBuffer = ETrue;
   589 		}
   590 	if(iHwDevice)
   591 		{
   592 		err = iHwDevice->ThisHwBufferFilled(*aBuffer);
   593 		}
   594 	else
   595 		{
   596 		err = KErrNotFound;
   597 		}
   598 	DP0_RET(err,"%d");
   599 	}
   600 
   601 // ---------------------------------------------------------------------------
   602 // from class MInputPort
   603 // CAudioCodec::SetOutput
   604 // ---------------------------------------------------------------------------
   605 TInt CAudioCodec::SetOutput(MOutputPort* aOutput)
   606 	{
   607 	DP_CONTEXT(CAudioCodec::SetOutput *CD1*, CtxDevSound, DPLOCAL);
   608 	DP_IN();
   609 	TInt err = KErrNone;
   610 	
   611 	iOutputPort = aOutput;
   612 	
   613 	DP0_RET(err, "%d");
   614 	}
   615 
   616 // ---------------------------------------------------------------------------
   617 // from class MInputPort
   618 // CAudioCodec::RemoveOutput
   619 // ---------------------------------------------------------------------------
   620 TInt CAudioCodec::RemoveOutput(MOutputPort* /*aOutput*/)
   621 	{
   622 	DP_CONTEXT(CAudioCodec::RemoveOutput *CD1*, CtxDevSound, DPLOCAL);
   623 	DP_IN();
   624 	DP0_RET(KErrNone, "%d");
   625 	}
   626 
   627 // ---------------------------------------------------------------------------
   628 // from class MOutputPort
   629 // CAudioCodec::FillBuffer
   630 // ---------------------------------------------------------------------------
   631 TInt CAudioCodec::FillBuffer(CMMFBuffer* /*aBuffer*/, MInputPort* /*aConsumer*/)
   632 	{
   633 	DP_CONTEXT(CAudioCodec::FillBuffer *CD1*, CtxDevSound, DPLOCAL);
   634 	DP_IN();
   635 	DP0_RET(KErrNotReady, "%d");
   636 	}
   637 
   638 // ---------------------------------------------------------------------------
   639 // from class MOutputPort
   640 // CAudioCodec::BufferEmptied
   641 // ---------------------------------------------------------------------------
   642 TInt CAudioCodec::BufferEmptied(CMMFBuffer* aBuffer)
   643 	{
   644 	DP_CONTEXT(CAudioCodec::BufferEmptied *CD1*, CtxDevSound, DPLOCAL);
   645 	DP_IN();
   646 	TInt err(KErrNone);
   647 	
   648 	if (!aBuffer)
   649 		{
   650 		err = KErrNotFound;
   651 		DP0_RET(err, "%d");
   652 		}
   653 
   654 	if (iLastBufferAck) //if the hwdevice return a buffer with the lastbuffer flag set, rather than returning a non-empty buffer
   655 		{				//in this case we generate an empty buffer on behalf of the adaptation
   656 		TInt count = iAudioCodecObservers.Count();
   657 		for ( TInt i(0); i < count; i++ ) // causes a ProcessingFinished() to be called on the stream
   658 			{
   659 			iAudioCodecObservers[i]->AllBuffersProcessed();
   660 			}
   661 		iLastBufferAck = EFalse;
   662 		}
   663 	else 
   664 		{
   665 		if(aBuffer->LastBuffer())
   666 			{
   667 			iLastBuffer = ETrue;
   668 			}
   669 		if(iHwDevice)
   670 			{ 
   671 			err = iHwDevice->ThisHwBufferEmptied(*aBuffer);
   672 			}
   673 		else
   674 			{
   675 			err = KErrNotFound;
   676 			}
   677 		}
   678 	DP0_RET(err,"%d");
   679 	}
   680 
   681 // ---------------------------------------------------------------------------
   682 // from class MOutputPort
   683 // CAudioCodec::SetInput
   684 // ---------------------------------------------------------------------------
   685 TInt CAudioCodec::SetInput(MInputPort* aInput)
   686 	{
   687 	DP_CONTEXT(CAudioCodec::SetInput *CD1*, CtxDevSound, DPLOCAL);
   688 	DP_IN();
   689 	TInt err = KErrNone;
   690 	
   691 	iInputPort = aInput;
   692 	
   693 	DP0_RET(err, "%d");
   694 	}
   695 
   696 // ---------------------------------------------------------------------------
   697 // from class MOutputPort
   698 // CAudioCodec::RemoveInput
   699 // ---------------------------------------------------------------------------
   700 TInt CAudioCodec::RemoveInput(MInputPort* /*aInput*/)
   701 	{
   702 	return KErrNone;
   703 	}
   704 
   705 // ---------------------------------------------------------------------------
   706 // from class MOutputPort
   707 // CAudioCodec::FlushBuffer
   708 // ---------------------------------------------------------------------------
   709 TInt CAudioCodec::FlushBuffer(MFlushHandlerObserver* aFlushObserver)
   710 	{
   711 	TInt err = KErrNone;
   712 	TAny* ptr = NULL;
   713 
   714 	// Flush low layer buffers
   715 	if(iHwDevice)
   716 		{
   717 		ptr = iHwDevice->CustomInterface(TUid::Uid(KMmfUidEmptyBuffersCustomInterface));
   718 		MMMFDevSoundEmptyBuffers* emptybuffers = static_cast<MMMFDevSoundEmptyBuffers*>(ptr);
   719 		if (emptybuffers)
   720 			{
   721 			// Due to defect DEF127468, this do nothing
   722 			err = emptybuffers->EmptyBuffers();
   723 			}
   724 		else
   725 			{
   726 			err = KErrNotSupported;
   727 			}
   728 		}
   729 	else
   730 		{
   731 		err = KErrNotReady;
   732 		}
   733 
   734 	// Flush source through an asynchronous request
   735 	if(err == KErrNone)
   736 		{
   737 		err = iOutputPort->FlushBuffer(aFlushObserver);
   738 		}
   739 	return err;
   740 	}
   741 
   742 
   743 // ---------------------------------------------------------------------------
   744 // from class MMMFHwDeviceObserver
   745 // CAudioCodec::FillThisHwBuffer
   746 // ---------------------------------------------------------------------------
   747 TInt CAudioCodec::FillThisHwBuffer(CMMFBuffer& aHwBuffer)
   748 	{
   749 	DP_CONTEXT(CAudioCodec::FillThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   750 	DP_IN();
   751 	PRF(PRF_ID, PRF_START, PRF_LOAD, AA_DS_Fill, "");
   752 
   753 	TInt err(KErrNone);
   754 	iAdaptationBuffer = static_cast<CMMFDataBuffer*> (&aHwBuffer);
   755 
   756 	ASSERT(iAdaptationBuffer);
   757 	if (iLastBuffer)
   758 		{
   759 		iAdaptationBuffer->Data().SetLength(0);
   760 		BufferFilled(iAdaptationBuffer);
   761 		}
   762 	else
   763 		{
   764 		TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().MaxLength() ));
   765 		if(err == KErrNone)
   766 			{
   767 			if(iOutputPort)
   768 				{
   769 				err = iOutputPort->FillBuffer(&aHwBuffer, this);
   770 				}
   771 			else
   772 				{
   773 				err = KErrNotFound;
   774 				}
   775 			}
   776 		}
   777 	DP0_RET(err,"%d");
   778 	}
   779 
   780 // ---------------------------------------------------------------------------
   781 // from class MMMFHwDeviceObserver
   782 // CAudioCodec::EmptyThisHwBuffer
   783 // ---------------------------------------------------------------------------
   784 TInt CAudioCodec::EmptyThisHwBuffer(CMMFBuffer& aHwBuffer)
   785 	{
   786 	DP_CONTEXT(CAudioCodec::EmptyThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   787 	DP_IN();
   788 
   789 	TInt err(KErrNone);
   790 	iAdaptationBuffer = static_cast<CMMFDataBuffer*> (&aHwBuffer);
   791 	ASSERT(iAdaptationBuffer);
   792 	if(iHwDeviceState == EPaused)
   793 		{
   794 		if (iAdaptationBuffer->Data().Length() == 0) // empty buffer
   795 			{
   796 			TInt count = iAudioCodecObservers.Count();
   797 			for ( TInt i(0); i < count; i++ ) // causes a ProcessingFinished() to be called on the stream
   798 				{
   799 				iAudioCodecObservers[i]->AllBuffersProcessed();
   800 				}
   801 			}
   802 		else
   803 			{
   804 			if(aHwBuffer.LastBuffer())
   805 				{
   806 				aHwBuffer.SetLastBuffer(EFalse);	// a buffer coming from hardware device should never have last buffer set...
   807 				iLastBufferAck = ETrue;
   808 				}
   809 			
   810 			TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().Length()));
   811 			if(err == KErrNone)
   812 				{
   813 				if(iInputPort)
   814 					{
   815 					err = iInputPort->EmptyBuffer(&aHwBuffer, this);
   816 					}
   817 				else
   818 					{
   819 					err = KErrNotFound;
   820 					}
   821 				}			
   822 			}
   823 		}
   824 	else
   825 		{
   826 		TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().Length()));
   827 		if(err == KErrNone)
   828 			{
   829 			if(iInputPort)
   830 				{
   831 				err = iInputPort->EmptyBuffer(&aHwBuffer, this);
   832 				}
   833 			else
   834 				{
   835 				err = KErrNotFound;
   836 				}
   837 			}
   838 		}
   839 	DP0_RET(err,"%d");
   840 	}
   841 
   842 // ---------------------------------------------------------------------------
   843 // from class MMMFHwDeviceObserver
   844 // CAudioCodec::MsgFromHwDevice
   845 // ---------------------------------------------------------------------------
   846 TInt CAudioCodec::MsgFromHwDevice(TUid aMessageType, const TDesC8& aMsg)
   847 	{
   848 	DP_CONTEXT(CAudioCodec::MsgFromHwDevice *CD1*, CtxDevSound, DPLOCAL);
   849 	DP_IN();
   850 	TBuf<50> formattedNumber;
   851 	formattedNumber.Format(_L("MessageType 0x%08x"), aMessageType.iUid);
   852 	TBuf<50> buf;
   853 	buf.Copy(aMsg);
   854 	buf.Append(formattedNumber);
   855 	if(aMessageType.iUid == KMmfHwDeviceObserverUpdateBytesPlayed)
   856 		{
   857 		//This is used by sw codec wrapper to request a bytes played update
   858 		//Bytes played won't be updated in Stopped() or Error() on sw codec wrapper
   859 		//As the sound device is closed. Non sw codec wrapper Hw device plugins
   860 		//Can get the bytes updated on Stopped() and/or Error()
   861 		UpdateBytesPlayed();
   862 		}
   863 	DP1(DLINFO, "DeviceMessage is %S",&buf);
   864 	DP0_RET(KErrNone, "%d");
   865 	}
   866 
   867 // ---------------------------------------------------------------------------
   868 // from class MMMFHwDeviceObserver
   869 // CAudioCodec::Stopped
   870 // ---------------------------------------------------------------------------
   871 void CAudioCodec::Stopped()
   872 	{
   873 	DP_CONTEXT(CAudioCodec::Stopped *CD1*, CtxDevSound, DPLOCAL);
   874 	DP_IN();
   875 	//For sw codec wrappers hw devices bytes played are updated in MsgFromHwDevice
   876 	//But non sw codec wrappers hw devices may do it differently
   877 	//Also don't know if non sw codec wrappers hw devices will call Stopped or Error first
   878 	UpdateBytesPlayed();
   879 	DP_OUT();
   880 	}
   881 
   882 // ---------------------------------------------------------------------------
   883 // from class MMMFHwDeviceObserver
   884 // CAudioCodec::Error
   885 // ---------------------------------------------------------------------------
   886 //
   887 void CAudioCodec::Error(TInt aError)
   888 	{
   889 	DP_CONTEXT(CAudioCodec::Error *CD1*, CtxDevSound, DPLOCAL);
   890 	DP_IN();
   891 
   892 	TTimeIntervalMicroSeconds currentPosition;
   893 	TInt err = KErrNone;
   894 
   895 #ifdef _DEBUG
   896 	RDebug::Print(_L("iDeviceUid=0x%x  aError=%d iLastBuffer=%d"), iDeviceUid.iUid, aError, iLastBuffer);
   897 #endif 
   898 	// The most problable receiver is the logicalaudiocodec which should notify to the client through MAudioProcessingUnit
   899 	TUint count = iAudioCodecObservers.Count();
   900 	
   901 	// Notify only for the
   902 	if (aError == KErrUnderflow && iMode == EDecode ) 
   903 		{
   904 		// Notify the observers
   905 		if(iLastBuffer)
   906 			{
   907 			for ( TUint i(0); i < count; i++ )
   908 				{
   909 				iAudioCodecObservers[i]->AllBuffersProcessed();
   910 				}
   911 			}
   912 		else
   913 			{		
   914 			// Re-start decoding
   915 
   916 			//For sw codec wrappers hw devices bytes played are updated in MsgFromHwDevice
   917 			//But non sw codec wrappers hw devices may do it differently
   918 			//Also don't know if non sw codec wrappers hw devices will call Stopped or Error first
   919 			UpdateBytesPlayed();
   920 
   921 			err = GetControlPosition(currentPosition);
   922 			if(err != KErrNone)
   923 				{
   924 				// Ignore safely
   925 				}
   926 			
   927 #ifdef _DEBUG
   928 	RDebug::Print(_L("Restart decoding"));
   929 #endif 			
   930 					
   931 			Start();
   932 			}
   933 		}
   934 	else if (aError == KErrOverflow && iMode == EEncode )
   935 		{
   936 		if (!iLastBuffer  && iHwDeviceState == ERunning)
   937 			{
   938 			// Re-start encoding
   939 			err = GetControlPosition(currentPosition);
   940 			if(err != KErrNone)
   941 				{
   942 				// Ignore safely
   943 				}
   944 			Start();
   945 			}
   946 		else
   947 			{
   948 				if (iDeviceUid.iUid != KUidToneHwDevice) 
   949 					{
   950 #ifdef _DEBUG
   951 	RDebug::Print(_L("Is this is not needed for other encoders but toneHwDevice"));
   952 #endif 
   953 					for ( TUint i(0); i < count; i++ )
   954 						{
   955 						iAudioCodecObservers[i]->AllBuffersProcessed();
   956 						}
   957 					}
   958 			}
   959 		}
   960 	else
   961 		{
   962 		for ( TUint i(0); i < count; i++ )
   963 			{
   964 			iAudioCodecObservers[i]->ProcessingUnitError(aError);
   965 			}
   966 		}
   967 	DP_OUT();
   968 	}
   969 
   970 // HELPER CLASS
   971 // ---------------------------------------------------------------------------
   972 // from class MGainHelper
   973 // CAudioCodec::GetGain
   974 // ---------------------------------------------------------------------------
   975 TInt CAudioCodec::GetGain(TInt& aGain) const
   976 	{
   977 	DP_CONTEXT(CAudioCodec::GetGain *CD1*, CtxDevSound, DPLOCAL);
   978 	DP_IN();
   979 	TInt err = KErrNone;
   980 	if (iMode == EDecode  && iPlayCustomInterface)
   981 		{
   982 		aGain = iPlayCustomInterface->Volume();
   983 		}
   984 	else if( iMode == EEncode  && iRecordCustomInterface) 
   985 		{
   986 		aGain = iRecordCustomInterface->Gain();
   987 		}
   988 	DP0_RET(err, "%d");
   989 	}
   990 
   991 // ---------------------------------------------------------------------------
   992 // from class MGainHelper
   993 // CAudioCodec::SetGain
   994 // ---------------------------------------------------------------------------
   995 TInt CAudioCodec::SetGain(RArray<TAudioChannelGain>& aChannels)
   996 	{
   997 	DP_CONTEXT(CAudioCodec::SetGain *CD1*, CtxDevSound, DPLOCAL);
   998 	DP_IN();
   999 	TInt err = KErrNone;
  1000 
  1001 	// Current adaptation doesn't support multichannel
  1002 	// Use average
  1003 	TUint count = aChannels.Count();
  1004 	TInt gain = 0;
  1005 	if (count)
  1006 		{
  1007 		TInt totalGain = 0;
  1008 		for (TUint i(0); i < count; i++)
  1009 			{
  1010 			totalGain  =+ aChannels[i].iGain;
  1011 			}
  1012 		gain = totalGain / count;
  1013 		}
  1014 
  1015 	if (gain > KDefaultMaxGain)
  1016 		{
  1017 		gain = KDefaultMaxGain;
  1018 		}
  1019 	else if (gain < 0)
  1020 		{
  1021 		gain = 0;
  1022 		}
  1023 
  1024 	if (iMode == EDecode  && iPlayCustomInterface)
  1025 		{
  1026 		iPlayCustomInterface->SetVolume(gain);
  1027 		}
  1028 	else if( iMode == EEncode  && iRecordCustomInterface) 
  1029 		{
  1030 		iRecordCustomInterface->SetGain(gain);
  1031 		}
  1032 	
  1033 	DP0_RET(err, "%d");
  1034 		}
  1035 
  1036 
  1037 TInt CAudioCodec::ConfigureRamp(TUid aRampOperation, const TTimeIntervalMicroSeconds& aRampDuration)
  1038 	{
  1039 	DP_CONTEXT(CAudioCodec::ConfigureRamp *CD1*, CtxDevSound, DPLOCAL);
  1040 	DP_IN();
  1041 	TInt err = KErrNone;
  1042 
  1043 	if (aRampOperation == KUidGainSawTooth)
  1044 		{
  1045 		if (iMode == EDecode  && iPlayCustomInterface)
  1046 			{
  1047 			iPlayCustomInterface->SetVolumeRamp(aRampDuration);
  1048 			}
  1049 		}
  1050 	else if (iRampOperation == KNullUid)
  1051 		{
  1052 		if (iMode == EDecode  && iPlayCustomInterface)
  1053 			{
  1054 			iPlayCustomInterface->SetVolumeRamp(0);
  1055 			}
  1056 		}
  1057 	else
  1058 		{
  1059 		err = KErrA3fUnsupportedRamp;
  1060 		}
  1061 
  1062 	DP0_RET(err, "%d");
  1063 	}
  1064 
  1065 // ---------------------------------------------------------------------------
  1066 // from class MPositionControl
  1067 // CAudioCodec::GetControlPosition
  1068 // ---------------------------------------------------------------------------
  1069 TInt CAudioCodec::GetControlPosition(TTimeIntervalMicroSeconds& aPosition)
  1070 	{
  1071 	DP_CONTEXT(CAudioCodec::GetControlPosition *CD1*, CtxDevSound, DPLOCAL);
  1072 	DP_IN();
  1073 	TInt err = KErrNone;
  1074 	TInt sampleRateValue = iSampleRateConfig;
  1075 
  1076 	// At this adaptation EMMFSoundEncoding16BitPCM encoding is assumed
  1077 	// Due RMdaDevSound which is always pcm16 each sample is 2 bytes
  1078 	TInt bytesPerAudioSample = 2;
  1079 
  1080 	// Here is secure to convert to TInt a TAudioMode since the values 
  1081 	// are chossen according to the value they represent.
  1082 	TInt numberOfChannels = ResolveMode(iModeConfig);
  1083 
  1084 	TInt64 samples = 0;
  1085 	if (err == KErrNone)
  1086 		{
  1087 		if( iMode == EDecode && iPlayCustomInterface)
  1088 			{
  1089 			TInt64 bytesPlayed = iPlayCustomInterface->BytesPlayed();
  1090 			if (bytesPlayed)
  1091 				{
  1092 				iPlayedBytesCount = bytesPlayed;
  1093 				}
  1094 
  1095 			samples = iPlayedBytesCount;
  1096 
  1097 			if(numberOfChannels > 1)
  1098 				{
  1099 				samples /= numberOfChannels;
  1100 				}
  1101 
  1102 			if(bytesPerAudioSample > 1)
  1103 				{
  1104 				samples /= bytesPerAudioSample;
  1105 				}
  1106 			}
  1107 		else if( iMode == EEncode && iRecordCustomInterface)
  1108 			{
  1109 			samples = iRecordCustomInterface->BytesRecorded();
  1110 			if(numberOfChannels > 1)
  1111 				{
  1112 				samples /= numberOfChannels;
  1113 				}
  1114 			if(bytesPerAudioSample > 1)
  1115 				{
  1116 				samples /= bytesPerAudioSample;
  1117 				}
  1118 			}
  1119 		}
  1120 
  1121 	if(sampleRateValue)
  1122 		{
  1123 		iPosition = (TInt64(samples) * KMicroSecsInOneSec / sampleRateValue);
  1124 		}
  1125 	aPosition = iPosition;
  1126 	DP1(DLINFO,"GetControlPosition Samples = %d", samples);
  1127 #ifdef _DEBUG
  1128 	RDebug::Print(_L("GetControlPosition Position=%d"), iPosition);
  1129 #endif 
  1130 	DP0_RET(err, "%d");
  1131 	}
  1132 
  1133 // ---------------------------------------------------------------------------
  1134 // CAudioCodec::ResetControlPosition
  1135 // ---------------------------------------------------------------------------
  1136 void CAudioCodec::ResetControlPosition()
  1137 	{
  1138 	DP_CONTEXT(CAudioCodec::ResetControlPosition *CD1*, CtxDevSound, DPLOCAL);
  1139 	DP_IN();
  1140 	iPosition = 0;
  1141 	iPlayedBytesCount = 0;
  1142 	DP_OUT();
  1143 	}
  1144 
  1145 // ---------------------------------------------------------------------------
  1146 // CAudioCodec::FourCCToHwDeviceUid
  1147 // ---------------------------------------------------------------------------
  1148 void CAudioCodec::FourCCToHwDeviceUidL(TFourCC aFourCC, TMode aMode, TUid &aHWDev)
  1149 	{
  1150 	DP_CONTEXT(CAudioCodec::FourCCToHwDeviceUid *CD1*, CtxDevSound, DPLOCAL);
  1151 	DP_IN();
  1152 			
  1153 	//check argument precondition for aState
  1154 	if ((aMode != EDecode) && (aMode != EEncode))
  1155 		{
  1156 		User::Leave(KErrArgument);
  1157 		}
  1158 
  1159 	// Array to return hw device plugin resource info(place on cleanupstack
  1160 	// _after_ ListImplementationsL() )
  1161 	RImplInfoPtrArray plugInArray;
  1162 	TUid KUidMmfHWPluginInterfaceCodec = {KMmfUidPluginInterfaceHwDevice};
  1163 
  1164 	// ListImplementationsL leaves if it cannot find anything so trap the error
  1165 	TRAPD(err, REComSession::ListImplementationsL(KUidMmfHWPluginInterfaceCodec,
  1166 													plugInArray));
  1167 	CleanupResetAndDestroyPushL(plugInArray);	
  1168 
  1169 	TUint numberOfHwDevicePlugins = plugInArray.Count();
  1170 
  1171 	// if no errors and have hwdevice plugin resource entries then scan entries
  1172 	// matching on a datatype of pcm16 as the destination datatype for play and
  1173 	// the source datatype for record. If a match is found and isn't already in
  1174 	// the list of supported data types, then add it to the list
  1175 	if ((err == KErrNone) && (numberOfHwDevicePlugins))
  1176 		{
  1177 		CImplementationInformation* hwDeviceResourceEntry = NULL;
  1178 		_LIT8(KPCM16FourCCString, " P16");
  1179 		TBufC8<KFOURCCLENGTH> fourCCStringPCM16(KPCM16FourCCString);
  1180 		TPtr8 fourCCPtrPCM16 = fourCCStringPCM16.Des();
  1181 
  1182 		// check each resource entry for dst 4CC = P16 for play and
  1183 		// src 4CC = P16 for record
  1184 		for (TUint hwDeviceEntry = 0;
  1185 				hwDeviceEntry < numberOfHwDevicePlugins;
  1186 				hwDeviceEntry++)
  1187 			{
  1188 			hwDeviceResourceEntry = plugInArray[hwDeviceEntry];
  1189 
  1190 			if (IsDataTypeMatch(hwDeviceResourceEntry, fourCCPtrPCM16, aMode))
  1191 				{
  1192 				// resource entry data field has dest/src datatype ' P16'
  1193 				// i.e. pcm16 for play/record
  1194 				TPtrC8 fourCCPtr(0,0);
  1195 
  1196 				if (aMode == EDecode)
  1197 					{
  1198 					// datatype supported 4CC is left 4 chars
  1199 					fourCCPtr.Set(
  1200 					hwDeviceResourceEntry->DataType().Left(KFOURCCLENGTH));
  1201 					}
  1202 				else if (aMode == EEncode)
  1203 					{
  1204 					// datatype supported 4CC is right 4 chars
  1205 					fourCCPtr.Set(
  1206 					hwDeviceResourceEntry->DataType().Right(KFOURCCLENGTH));
  1207 					}
  1208 
  1209 				TFourCC fourCCEntry(fourCCPtr);
  1210 
  1211 				// New Mapping
  1212 				if (fourCCEntry == aFourCC)
  1213 					{
  1214 					// get the Uid
  1215 					aHWDev = hwDeviceResourceEntry->ImplementationUid();
  1216 					break;
  1217 					}
  1218 				}
  1219 			}
  1220 			
  1221 		// Verify there is a HwDeviceUid
  1222 		if(aHWDev.iUid == 0)
  1223 			{
  1224 			User::Leave(KErrNotSupported);
  1225 			}
  1226 		}
  1227 	else
  1228 		{
  1229 		// if an error occured and not KErrNotFound then must be a 'real' error
  1230 		// e.g. KErrNoMemory
  1231 		if ((err != KErrNotFound) && (err != KErrNone))
  1232 			{
  1233 			User::Leave(err);
  1234 			}
  1235 		}
  1236 
  1237 	CleanupStack::PopAndDestroy(&plugInArray);
  1238 	DP_OUT();
  1239 	}
  1240 
  1241 // ---------------------------------------------------------------------------
  1242 // CAudioCodec::IsDataTypeMatch
  1243 // ---------------------------------------------------------------------------
  1244 TBool CAudioCodec::IsDataTypeMatch(CImplementationInformation* aHwDeviceResourceEntry,
  1245 	const TDesC8& aHwMatchFourCC, TMode aState)
  1246 	{
  1247 	DP_CONTEXT(CAudioCodec::IsDataTypeMatch *CD1*, CtxDevSound, DPLOCAL);
  1248 	DP_IN();
  1249 	TBool match = EFalse;
  1250 
  1251 	// Check for resource entry lenght since for HwDeviceAdaptor it is just "*"
  1252 	if (aHwDeviceResourceEntry->DataType().Length()>=KHwDeviceDefaultDataLength)
  1253 		{
  1254 		if (aState == EDecode)
  1255 			{
  1256 			//play need to match with the right four characters
  1257 			match = (!(aHwMatchFourCC.Match(
  1258 				aHwDeviceResourceEntry->DataType().Right(KFOURCCLENGTH)) ==
  1259 				KErrNotFound));
  1260 			}
  1261 		else if (aState == EEncode)
  1262 			{
  1263 			//record need to match with the left four characters
  1264 			match =
  1265 				(!(aHwMatchFourCC.Match(
  1266 				aHwDeviceResourceEntry->DataType().Left(KFOURCCLENGTH)) ==
  1267 				KErrNotFound));
  1268 			}
  1269 		}
  1270 	else
  1271 		{
  1272 #ifdef _DEBUG
  1273 		RDebug::Print(_L("HwDeviceEntry %S"), &aHwDeviceResourceEntry->DataType());
  1274 #endif			
  1275 		}
  1276 
  1277 	DP0_RET(match, "0x%x");
  1278 	}
  1279 
  1280 // ---------------------------------------------------------------------------
  1281 // CAudioCodec::ResolveMode
  1282 // ---------------------------------------------------------------------------
  1283 TInt CAudioCodec::ResolveMode(TUid aMode)
  1284 	{
  1285 	DP_CONTEXT(CAudioCodec::ResolveMode *CD1*, CtxDevSound, DPLOCAL);
  1286 	DP_IN();
  1287 	TInt result = 0;
  1288 
  1289 	//Mapping an uid mode to an int channel
  1290 	for (TUint i=0; i<=KMaxModeIndex; i++)
  1291 		{
  1292 		if(KModeTableLookup[i].iAudioMode == aMode)
  1293 			{
  1294 			result = KModeTableLookup[i].iAudioModeValue;
  1295 			break;
  1296 			}
  1297 		}
  1298 	DP0_RET(result, "%d");
  1299 	}
  1300 
  1301 // ---------------------------------------------------------------------------
  1302 // CAudioCodec::SupportedRates
  1303 // ---------------------------------------------------------------------------
  1304 EXPORT_C TInt CAudioCodec::SupportedRates(RArray<TInt>& aSupportedRates)
  1305 	{
  1306 	DP_CONTEXT(CAudioCodec::Service *CD1*, CtxDevSound, DPLOCAL);
  1307 	DP_IN();
  1308 	iRequest |= KRequestSampleRate;
  1309 
  1310 	//Get request capabilities
  1311 	iErrorRates = GetSupportedSampleRates(aSupportedRates);
  1312 	
  1313 	// Simulate an asyncronous response
  1314 	if (!IsActive())
  1315 		{
  1316 		TRequestStatus* status = &iStatus;
  1317 		User::RequestComplete(status, KErrNone);
  1318 		SetActive();
  1319 		}
  1320 	DP0_RET(iErrorRates, "%d");
  1321 	}
  1322 
  1323 // ---------------------------------------------------------------------------
  1324 // CAudioCodec::SupportedModes
  1325 // ---------------------------------------------------------------------------
  1326 EXPORT_C TInt CAudioCodec::SupportedModes(RArray<TUid>& aSupportedModes)
  1327 	{
  1328 	DP_CONTEXT(CAudioCodec::Service *CD1*, CtxDevSound, DPLOCAL);
  1329 	DP_IN();
  1330 	iRequest |= KRequestMode;
  1331 
  1332 	iErrorModes = GetSupportedModes(aSupportedModes);
  1333 
  1334 	// Simulate an asyncronous response
  1335 	if (!IsActive())
  1336 		{
  1337 		TRequestStatus* status = &iStatus;
  1338 		User::RequestComplete(status, KErrNone);
  1339 		SetActive();
  1340 		}
  1341 	DP0_RET(iErrorModes, "%d");
  1342 	}
  1343 
  1344 
  1345 //From CActive
  1346 // ---------------------------------------------------------------------------
  1347 // CAudioCodec::RunL
  1348 // ---------------------------------------------------------------------------
  1349 void CAudioCodec::RunL()
  1350 	{
  1351 	DP_CONTEXT(CAudioCodec::RunL *CD1*, CtxDevSound, DPLOCAL);
  1352 	DP_IN();
  1353 
  1354 	// Send the callback for the request operation
  1355 	if (iRequest & KRequestMode)
  1356 		{
  1357 		TUint count = iAudioCodecObservers.Count();
  1358 		for ( TUint i(0); i < count; i++ )
  1359 			{
  1360 			iAudioCodecObservers[i]->GetSupportedAModesComplete(iErrorModes);
  1361 			}
  1362 
  1363 		// Reset the request flag
  1364 		iRequest &= ~KRequestMode;
  1365 		}
  1366 
  1367 	// Send the callback for the request operation
  1368 	if (iRequest & KRequestSampleRate)
  1369 		{
  1370 		TUint count = iAudioCodecObservers.Count();
  1371 		for ( TUint i(0); i < count; i++ )
  1372 			{
  1373 			iAudioCodecObservers[i]->GetSupportedARatesComplete(iErrorRates);
  1374 			}
  1375 
  1376 		// Reset the request flag
  1377 		iRequest &= ~KRequestSampleRate;
  1378 		}
  1379 
  1380 	DP_OUT();
  1381 	}
  1382 
  1383 // ---------------------------------------------------------------------------
  1384 // CAudioCodec::RunError
  1385 // ---------------------------------------------------------------------------
  1386 TInt CAudioCodec::RunError(TInt aError)
  1387 	{
  1388 	DP_CONTEXT(CAudioCodec::RunError *CD1*, CtxDevSound, DPLOCAL);
  1389 	DP_IN();
  1390 	DP0_RET(aError, "%d");
  1391 	}
  1392 
  1393 // ---------------------------------------------------------------------------
  1394 // CAudioCodec::DoCancel
  1395 // ---------------------------------------------------------------------------
  1396 void CAudioCodec::DoCancel()
  1397 	{
  1398 	DP_CONTEXT(CAudioCodec::DoCancel *CD1*, CtxDevSound, DPLOCAL);
  1399 	DP_IN();
  1400 	iRequest = 0;
  1401 	DP_OUT();
  1402 	}
  1403 
  1404 // end of file