os/mm/mmhais/refacladapt/src/audiocodec/audiocodec.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmhais/refacladapt/src/audiocodec/audiocodec.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1404 @@
     1.4 +//audiocodec.cpp
     1.5 +
     1.6 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.7 +// All rights reserved.
     1.8 +// This component and the accompanying materials are made available
     1.9 +// under the terms of "Eclipse Public License v1.0"
    1.10 +// which accompanies this distribution, and is available
    1.11 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.12 +//
    1.13 +// Initial Contributors:
    1.14 +// Nokia Corporation - initial contribution.
    1.15 +//
    1.16 +// Contributors:
    1.17 +//
    1.18 +// Description:
    1.19 +//
    1.20 +
    1.21 +
    1.22 +
    1.23 +#include <a3f/a3fbase.h>
    1.24 +#include <a3f/a3ffourcclookup.h>
    1.25 +#include <a3f/audioprocessingunittypeuids.h>
    1.26 +#include "maudiocodecadaptationobserver.h"
    1.27 +#include "audiocodec.h"
    1.28 +#include "resourcedata.h"
    1.29 +
    1.30 +// THIS WILL REMAIN UNTIL THE REQUIREMENT WILL BE DEFINED
    1.31 +// THIS IMPLEMENTATION WILL WORK ONLY FOR HW DEVICES IMPLEMNETED AS SOFTWARE CODEC WRAPPERS
    1.32 +#define INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
    1.33 +
    1.34 +#ifdef INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
    1.35 +#include <mmf/server/mmfswcodecwrappercustominterfacesuids.hrh>
    1.36 +#endif
    1.37 +
    1.38 +const TInt KMicroSecsInOneSec = 1000000;
    1.39 +const TInt KHwDeviceDefaultDataLength= 9;
    1.40 +const TUint KDefaultMaxGain = 255; // TODO should discover at run time but this is value of current Symbian sound driver adaptor
    1.41 +
    1.42 +//Values to find the request ask by the client
    1.43 +const TInt KRequestSampleRate = 0x0001;
    1.44 +const TInt KRequestMode = 0x0002;
    1.45 +const TAudioModeTableEntry KModeTableLookup[] = {
    1.46 +							{ EMMFMono, {KA3FModeMonoValue} },
    1.47 +							{ EMMFStereo, {KA3FModeStereoNonInterleavedValue} },
    1.48 +							};
    1.49 +
    1.50 +
    1.51 +// ---------------------------------------------------------------------------
    1.52 +// Constructor
    1.53 +// ---------------------------------------------------------------------------
    1.54 +CAudioCodec::CAudioCodec(TUid aTypeId, const CFourCCConvertor& aFourCCConvertor)
    1.55 +	: CActive(EPriorityStandard),
    1.56 +	iLastBuffer(EFalse),
    1.57 +	iHwDeviceState(EIdle),
    1.58 +	iLastBufferAck(EFalse), 
    1.59 +	iRampOperation(KNullUid)
    1.60 +	{
    1.61 +	TRACE_CREATE();
    1.62 +	DP_CONTEXT(CAudioCodec::CAudioCodec *CD1*, CtxDevSound, DPLOCAL);
    1.63 +	DP_IN();
    1.64 +	if(aTypeId==KUidAudioEncoder)
    1.65 +		{
    1.66 +		iMode = EEncode;
    1.67 +		}
    1.68 +	else if(aTypeId==KUidAudioDecoder)
    1.69 +		{
    1.70 +		iMode = EDecode;
    1.71 +		}
    1.72 +	iFourCCConvertor = static_cast<CFourCCConvertor*>( const_cast<CFourCCConvertor*>(&aFourCCConvertor) );
    1.73 +
    1.74 +	iHwDeviceInitArgs.iEapStreamId = 0;
    1.75 +	iHwDeviceInitArgs.iPolicyId = 0;
    1.76 +	DP_OUT();
    1.77 +	}
    1.78 +
    1.79 +// ---------------------------------------------------------------------------
    1.80 +// Factory method
    1.81 +// ---------------------------------------------------------------------------
    1.82 +EXPORT_C CAudioCodec* CAudioCodec::NewL(TUid aTypeId, const CFourCCConvertor& aFourCCConvertor)
    1.83 +	{
    1.84 +	DP_STATIC_CONTEXT(CAudioCodec::NewL *CD0*, CtxDevSound, DPLOCAL);
    1.85 +	DP_IN();
    1.86 +	CAudioCodec* self = new(ELeave)CAudioCodec(aTypeId, aFourCCConvertor);
    1.87 +	CleanupStack::PushL(self);
    1.88 +	self->ConstructL();
    1.89 +	CleanupStack::Pop(self);
    1.90 +	DP0_RET(self, "0x%x");
    1.91 +	}
    1.92 +
    1.93 +// ---------------------------------------------------------------------------
    1.94 +// Second phase constructor
    1.95 +// ---------------------------------------------------------------------------
    1.96 +void CAudioCodec::ConstructL()
    1.97 +	{
    1.98 +	DP_CONTEXT(CAudioCodec::ConstructL *CD1*, CtxDevSound, DPLOCAL);
    1.99 +	DP_IN();
   1.100 +	CActiveScheduler::Add(this);
   1.101 +	DP_OUT();
   1.102 +	}
   1.103 +
   1.104 +// ---------------------------------------------------------------------------
   1.105 +// Destructor
   1.106 +// ---------------------------------------------------------------------------
   1.107 +CAudioCodec::~CAudioCodec()
   1.108 +	{
   1.109 +	DP_CONTEXT(CAudioCodec::~CAudioCodec *CD1*, CtxDevSound, DPLOCAL);
   1.110 +	DP_IN();
   1.111 +	iAudioCodecObservers.Close();
   1.112 +	if (iHwDevice)
   1.113 +		{
   1.114 +		if (!iHwDeviceState == EStopped)
   1.115 +			{
   1.116 +			DP0(DLINFO, "StopAndDeleteCodec");
   1.117 +			iHwDevice->StopAndDeleteCodec();
   1.118 +			}
   1.119 +		delete iHwDevice;
   1.120 +		iHwDevice = NULL;
   1.121 +		}
   1.122 +	Cancel();
   1.123 +	DP_OUT();
   1.124 +	}
   1.125 +
   1.126 +// ---------------------------------------------------------------------------
   1.127 +// CAudioCodec::SetFormat
   1.128 +// ---------------------------------------------------------------------------
   1.129 +EXPORT_C TInt CAudioCodec::SetFormat(TUid aFormat)
   1.130 +	{
   1.131 +	DP_CONTEXT(CAudioCodec::SetFormat *CD1*, CtxDevSound, DPLOCAL);
   1.132 +	DP_IN();
   1.133 +	iFormat = aFormat;
   1.134 +	DP0_RET(KErrNone, "%d");
   1.135 +	}
   1.136 +
   1.137 +// ---------------------------------------------------------------------------
   1.138 +// CAudioCodec::Initialize
   1.139 +// ---------------------------------------------------------------------------
   1.140 +EXPORT_C TInt CAudioCodec::Initialize()
   1.141 +	{
   1.142 +	DP_CONTEXT(CAudioCodec::Initialize *CD1*, CtxDevSound, DPLOCAL);
   1.143 +	DP_IN();
   1.144 +	TInt err(KErrNone);
   1.145 +	TFourCC aFourCC(0);
   1.146 +	err = iFourCCConvertor->FormatToFourCC(iFormat,aFourCC);
   1.147 +	if(err == KErrNotFound)
   1.148 +		{
   1.149 +		err = KErrNotSupported;
   1.150 +		}
   1.151 +	if(err == KErrNone)
   1.152 +		{
   1.153 +		TRAP(err,  FourCCToHwDeviceUidL(aFourCC, iMode, iDeviceUid) );
   1.154 +		}
   1.155 +	if (err == KErrNone)
   1.156 +		{
   1.157 +		err = LoadHwDevice();
   1.158 +		}
   1.159 +		
   1.160 +#ifdef INCLUDE_HWDEVICES_AS_SWCODEC_WRAPPERS
   1.161 +	if (err == KErrNone)
   1.162 +		{
   1.163 +		TUid interfaceUid = {0};
   1.164 +		TAny* interface = NULL;
   1.165 +		if (iMode == EDecode)
   1.166 +			{
   1.167 +			interfaceUid.iUid = KMmfPlaySettingsCustomInterface;
   1.168 +			}
   1.169 +		else if(iMode == EEncode)
   1.170 +			{
   1.171 +			interfaceUid.iUid = KMmfRecordSettingsCustomInterface;
   1.172 +			}
   1.173 +		
   1.174 +		// This call at the sw codec wrapper creates the custom interfaces for playing and record
   1.175 +		if(iHwDevice)
   1.176 +			{
   1.177 +			interface = iHwDevice->CustomInterface(interfaceUid);
   1.178 +			if (iMode == EDecode)
   1.179 +				{
   1.180 +				iPlayCustomInterface = static_cast<MPlayCustomInterface*>(interface);
   1.181 +				}
   1.182 +			else if(iMode == EEncode)
   1.183 +				{
   1.184 +				iRecordCustomInterface = static_cast<MRecordCustomInterface*>(interface);
   1.185 +				}
   1.186 +			}
   1.187 +		}
   1.188 +
   1.189 +#endif
   1.190 +	if(err == KErrNone)
   1.191 +		{
   1.192 +		err = InitHwDevice();
   1.193 +		}
   1.194 +	DP0_RET(err, "%d");
   1.195 +	}
   1.196 +	
   1.197 +// ---------------------------------------------------------------------------
   1.198 +// CAudioCodec::Load
   1.199 +// ---------------------------------------------------------------------------
   1.200 +EXPORT_C TInt CAudioCodec::Load(TInt aSampleRate, TUid aMode)
   1.201 +	{
   1.202 +	DP_CONTEXT(CAudioCodec::Load *CD1*, CtxDevSound, DPLOCAL);
   1.203 +	DP_IN();
   1.204 +	iSampleRateConfig = aSampleRate;
   1.205 +	iModeConfig = aMode;
   1.206 +	TInt err(KErrNone);
   1.207 +	err = SetHwDeviceConfig();
   1.208 +	DP0_RET(err, "%d");
   1.209 +	}
   1.210 +
   1.211 +// ---------------------------------------------------------------------------
   1.212 +// CAudioCodec::Start
   1.213 +// ---------------------------------------------------------------------------
   1.214 +EXPORT_C TInt CAudioCodec::Start()
   1.215 +	{
   1.216 +	DP_CONTEXT(CAudioCodec::Start *CD1*, CtxDevSound, DPLOCAL);
   1.217 +	DP_IN();
   1.218 +	TInt err(KErrNone);
   1.219 +	iLastBuffer = EFalse;
   1.220 +	
   1.221 +	// Needed since ToneHwDevice only exchange one buffer with its client
   1.222 +	// Resume shouldn't clear the last buffer flag 
   1.223 +	// since this wont' result on a new buffer request
   1.224 +	if(iHwDeviceState == EPaused && iDeviceUid.iUid == KUidToneHwDevice)
   1.225 +		{
   1.226 +		iLastBuffer = ETrue;
   1.227 +		}
   1.228 +
   1.229 +	if (iHwDevice)
   1.230 +		{
   1.231 +		PRF(PRF_ID, PRF_START, PRF_TIME, AA_DS_StartHwDevice, "");
   1.232 +		if (iMode == EDecode)
   1.233 +			{
   1.234 +			// PLAY
   1.235 +			err = iHwDevice->Start(EDevDecode, EDevOutFlow);
   1.236 +			DP1(DLINFO,"hwdevice decode start: error %d",err);
   1.237 +			iIgnoreUnderflowInterface = reinterpret_cast<MIgnoreUnderflowEventsCustomInterface*>(iHwDevice->CustomInterface(KIgnoreUnderflowCustomInterfaceTypeUid));	
   1.238 +			if(iIgnoreUnderflowInterface)
   1.239 +				{
   1.240 +				iIgnoreUnderflowInterface->IgnoreUnderflowEvents();
   1.241 +				}
   1.242 +			}
   1.243 +		else if (iMode == EEncode)
   1.244 +			{
   1.245 +			// RECORD
   1.246 +			err = iHwDevice->Start(EDevEncode, EDevInFlow);
   1.247 +			DP1(DLINFO,"hwdevice encode start: error %d",err);
   1.248 +			}
   1.249 +		else
   1.250 +			{
   1.251 +			err = KErrNotSupported;
   1.252 +			DP1(DLINFO,"CAudioCodec::StartHwDevice - Unknown mode %d", iMode);
   1.253 +			}
   1.254 +		PRF(PRF_ID, PRF_STOP, PRF_TIME, AA_DS_StartHwDevice, "");
   1.255 +		}
   1.256 +	else
   1.257 +		{
   1.258 +		err = KErrHardwareNotAvailable;
   1.259 +		}
   1.260 +
   1.261 +	if (err == KErrNone)
   1.262 +		{
   1.263 +		iHwDeviceState = ERunning;
   1.264 +		}
   1.265 +	iLastBufferAck = EFalse;	
   1.266 +	DP0_RET(err, "%d");
   1.267 +	}
   1.268 +
   1.269 +// ---------------------------------------------------------------------------
   1.270 +// CAudioCodec::Pause
   1.271 +// ---------------------------------------------------------------------------
   1.272 +EXPORT_C TInt CAudioCodec::Pause()
   1.273 +	{
   1.274 +	DP_CONTEXT(CAudioCodec::Pause *CD1*, CtxDevSound, DPLOCAL);
   1.275 +	DP_IN();
   1.276 +	TInt err(KErrNone);
   1.277 +	if (iHwDevice)
   1.278 +		{
   1.279 +		DP0(DLINFO,"Pause hwdevice");
   1.280 +		err = iHwDevice->Pause();
   1.281 +		}
   1.282 +	iHwDeviceState = EPaused;
   1.283 +	DP0_RET(err, "%d");
   1.284 +	}
   1.285 +
   1.286 +// ---------------------------------------------------------------------------
   1.287 +// CAudioCodec::Stop
   1.288 +// ---------------------------------------------------------------------------
   1.289 +EXPORT_C TInt CAudioCodec::Stop()
   1.290 +	{
   1.291 +	DP_CONTEXT(CAudioCodec::Stop *CD1*, CtxDevSound, DPLOCAL);
   1.292 +	DP_IN();
   1.293 +	TInt err(KErrNone);
   1.294 +	if (iHwDevice && !(iHwDeviceState == EStopped)) 
   1.295 +		{
   1.296 +		DP0(DLINFO,"Stop hwdevice");
   1.297 +		UpdateBytesPlayed();
   1.298 +		err = iHwDevice->Stop();
   1.299 +		iHwDeviceState = EStopped;
   1.300 +		}
   1.301 +	iLastBufferAck = EFalse;
   1.302 +	DP0_RET(err, "%d");
   1.303 +	}
   1.304 +
   1.305 +// ---------------------------------------------------------------------------
   1.306 +// CAudioCodec::GetSupportedSampleRates
   1.307 +// ---------------------------------------------------------------------------
   1.308 +TInt CAudioCodec::GetSupportedSampleRates(RArray<TInt>& aSupportedRates)
   1.309 +	{
   1.310 +	DP_CONTEXT(CAudioCodec::GetSupportedSampleRates *CD1*, CtxDevSound, DPLOCAL);
   1.311 +	DP_IN();
   1.312 +	TInt err(KErrNone);
   1.313 +	aSupportedRates.Reset();
   1.314 +	// Ask for rates from resource files
   1.315 +	err = ReadResourceRates(aSupportedRates);
   1.316 +
   1.317 +	DP0_RET(err, "%d");
   1.318 +	}
   1.319 +
   1.320 +// ---------------------------------------------------------------------------
   1.321 +// CAudioCodec::GetSupportedModes
   1.322 +// ---------------------------------------------------------------------------
   1.323 +TInt CAudioCodec::GetSupportedModes(RArray<TUid>& aSupportedModes)
   1.324 +	{
   1.325 +	DP_CONTEXT(CAudioCodec::GetSupportedModes *CD1*, CtxDevSound, DPLOCAL);
   1.326 +	DP_IN();
   1.327 +	TInt err(KErrNone);
   1.328 +	aSupportedModes.Reset();
   1.329 +	// Ask for modes from resource files
   1.330 +	err = ReadResourceModes(aSupportedModes);
   1.331 +
   1.332 +	DP0_RET(err, "%d");
   1.333 +	}
   1.334 +
   1.335 +// ---------------------------------------------------------------------------
   1.336 +// CAudioCodec::GetCustomInterface
   1.337 +// ---------------------------------------------------------------------------
   1.338 +TInt CAudioCodec::RequestCustomInterface(TUid aUid, TAny*& aPtr)
   1.339 +	{
   1.340 +	DP_CONTEXT(CAudioCodec::GetCustomInterface *CD1*, CtxDevSound, DPLOCAL);
   1.341 +	DP_IN();
   1.342 +	TInt err = KErrNone;
   1.343 +	if(iHwDevice)
   1.344 +		{
   1.345 +		aPtr = iHwDevice->CustomInterface(aUid);
   1.346 +		}
   1.347 +	else
   1.348 +		{
   1.349 +		err = KErrNotReady;
   1.350 +		}
   1.351 +	DP0_RET(err, "%d");
   1.352 +	}
   1.353 +
   1.354 +// ---------------------------------------------------------------------------
   1.355 +// CAudioCodec::RegisterObserver
   1.356 +// ---------------------------------------------------------------------------
   1.357 +TInt CAudioCodec::RegisterObserver(MCustomInterfaceSupportObserver& aObserver)
   1.358 +	{
   1.359 +	DP_CONTEXT(CAudioCodec::RegisterObserver *CD1*, CtxDevSound, DPLOCAL);
   1.360 +	DP_IN();
   1.361 +	TInt err = KErrNone;
   1.362 +	err = iCISupportObservers.Find(&aObserver);
   1.363 +	if( err != KErrNotFound )
   1.364 +		{
   1.365 +		err = KErrAlreadyExists;
   1.366 +		}
   1.367 +	else
   1.368 +		{
   1.369 +		err = iCISupportObservers.Append(&aObserver);
   1.370 +		}
   1.371 +	DP0_RET(err, "%d");
   1.372 +	}
   1.373 +
   1.374 +// ---------------------------------------------------------------------------
   1.375 +// CAudioCodec::UnRegisterObserver
   1.376 +// ---------------------------------------------------------------------------
   1.377 +void CAudioCodec::UnRegisterObserver(MCustomInterfaceSupportObserver& aObserver)
   1.378 +	{
   1.379 +	DP_CONTEXT(CAudioCodec::UnRegisterObserver *CD1*, CtxDevSound, DPLOCAL);
   1.380 +	DP_IN();
   1.381 +	TInt idxOrErr = iCISupportObservers.Find(&aObserver);
   1.382 +	if( idxOrErr != KErrNotFound )
   1.383 +		{
   1.384 +		iCISupportObservers.Remove(idxOrErr);
   1.385 +		}
   1.386 +	DP_OUT();
   1.387 +	}
   1.388 +
   1.389 +// ---------------------------------------------------------------------------
   1.390 +// CAudioCodec::LoadHwDevice
   1.391 +// ---------------------------------------------------------------------------
   1.392 +TInt CAudioCodec::LoadHwDevice()
   1.393 +	{
   1.394 +	DP_CONTEXT(CAudioCodec::LoadHwDevice *CD1*, CtxDevSound, DPLOCAL);
   1.395 +	DP_IN();
   1.396 +	TInt err(KErrNone);
   1.397 +
   1.398 +	if(iDeviceUid==KNullUid)
   1.399 +		{
   1.400 +		DP0_RET(KErrNotSupported, "%d");
   1.401 +		}
   1.402 +
   1.403 +	TRAP(err, iHwDevice = CMMFHwDevice::NewL(iDeviceUid));
   1.404 +	if(err == KErrNotFound)
   1.405 +		{
   1.406 +		err = KErrNotSupported;
   1.407 +		}
   1.408 +	DP0_RET(err, "%d");
   1.409 +	}
   1.410 +
   1.411 +// ---------------------------------------------------------------------------
   1.412 +// CAudioCodec::InitHwDevice
   1.413 +// ---------------------------------------------------------------------------
   1.414 +TInt CAudioCodec::InitHwDevice()
   1.415 +	{
   1.416 +	DP_CONTEXT(CAudioCodec::InitHwDevice *CD1*, CtxDevSound, DPLOCAL);
   1.417 +	DP_IN();
   1.418 +	TInt err(KErrNone);
   1.419 +	DP1(DLINFO, "eapStreamId %d", iHwDeviceInitArgs.iEapStreamId);
   1.420 +	DP1(DLINFO, "policyId %d", iHwDeviceInitArgs.iPolicyId);
   1.421 +
   1.422 +	iHwDeviceInitParams.iHwDeviceObserver = this;
   1.423 +	iHwDeviceInitParams.iHwDeviceInitArgsPtr.Set((TUint8*)&(iHwDeviceInitArgs),
   1.424 +														sizeof(THwDeviceInitArgs),
   1.425 +														sizeof(THwDeviceInitArgs));
   1.426 +	if (iHwDevice)
   1.427 +		{
   1.428 +		err = iHwDevice->Init(iHwDeviceInitParams);
   1.429 +		}
   1.430 +	else
   1.431 +		{
   1.432 +		err = KErrHardwareNotAvailable;
   1.433 +		}
   1.434 +	DP0_RET(err, "%d");
   1.435 +	}
   1.436 +
   1.437 +// ---------------------------------------------------------------------------
   1.438 +// CAudioCodec::SetHwDeviceConfig
   1.439 +// ---------------------------------------------------------------------------
   1.440 +TInt CAudioCodec::SetHwDeviceConfig()
   1.441 +	{
   1.442 +	DP_CONTEXT(CAudioCodec::SetHwDeviceConfig *CD1*, CtxDevSound, DPLOCAL);
   1.443 +	DP_IN();
   1.444 +	TInt err(KErrNone);
   1.445 +
   1.446 +	//TODO Look for another way to set those parameters
   1.447 +	TTaskConfig taskconfig;
   1.448 +	taskconfig.iUid = KUidRefDevSoundTaskConfig;
   1.449 +	taskconfig.iStereoMode = ResolveMode(iModeConfig);
   1.450 +	taskconfig.iRate = iSampleRateConfig;
   1.451 +
   1.452 +	DP1(DLINFO, "taskconfig.iRate %d", taskconfig.iRate);
   1.453 +	DP1(DLINFO, "taskconfig.iStereoMode %d", taskconfig.iStereoMode);
   1.454 +	DP1(DLINFO, "taskconfig.iUid %d", taskconfig.iUid);
   1.455 +
   1.456 +	if (err == KErrNone && iHwDevice)
   1.457 +		{
   1.458 +		err = iHwDevice->SetConfig(taskconfig);
   1.459 +		}
   1.460 +	else
   1.461 +		{
   1.462 +		err = KErrHardwareNotAvailable;
   1.463 +		}
   1.464 +	DP0_RET(err, "%d");
   1.465 +	}
   1.466 +
   1.467 +// ---------------------------------------------------------------------------
   1.468 +// CAudioCodec::ReadResourceRates
   1.469 +// ---------------------------------------------------------------------------
   1.470 +TInt CAudioCodec::ReadResourceRates(RArray<TInt>& aRates)
   1.471 +	{
   1.472 +	DP_CONTEXT(CAudioCodec::ReadResourceRates*CD1*, CtxDevSound, DPLOCAL);
   1.473 +	DP_IN();
   1.474 +	TInt err(KErrNone);
   1.475 +
   1.476 +	CResourceData* resource = NULL;
   1.477 +	TRAP(err, resource = CResourceData::NewL(iDeviceUid));
   1.478 +	if (err == KErrNone && resource)
   1.479 +		{
   1.480 +		err = resource->GetSSampleRates(iMode,aRates);
   1.481 +		delete resource;
   1.482 +		}
   1.483 +
   1.484 +	DP0_RET(err, "%d");
   1.485 +	}
   1.486 +
   1.487 +// ---------------------------------------------------------------------------
   1.488 +// CAudioCodec::ReadResourceModes
   1.489 +// ---------------------------------------------------------------------------
   1.490 +TInt CAudioCodec::ReadResourceModes(RArray<TUid>& aModes)
   1.491 +	{
   1.492 +	DP_CONTEXT(CAudioCodec::ReadResourceModes*CD1*, CtxDevSound, DPLOCAL);
   1.493 +	DP_IN();
   1.494 +	TInt err(KErrNone);
   1.495 +
   1.496 +	CResourceData* resource = NULL;
   1.497 +	TRAP(err, resource = CResourceData::NewL(iDeviceUid));
   1.498 +	if (err == KErrNone && resource)
   1.499 +		{
   1.500 +		err = resource->GetSModes(iMode,aModes);
   1.501 +		delete resource;
   1.502 +		}
   1.503 +
   1.504 +	DP0_RET(err, "%d");
   1.505 +	}
   1.506 +
   1.507 +// ---------------------------------------------------------------------------
   1.508 +// from class MAudioComponent
   1.509 +// CAudioCodec::GetOutputPort
   1.510 +// ---------------------------------------------------------------------------
   1.511 +TInt CAudioCodec::GetOutputPort(MOutputPort*& aOutputPort)
   1.512 +	{
   1.513 +	DP_CONTEXT(CAudioCodec::GetOutputPort *CD1*, CtxDevSound, DPLOCAL);
   1.514 +	DP_IN();
   1.515 +	aOutputPort = this;
   1.516 +	DP0_RET(KErrNone, "%d");
   1.517 +	}
   1.518 +
   1.519 +// ---------------------------------------------------------------------------
   1.520 +// from class MAudioComponent
   1.521 +// CAudioCodec::GetInputPort
   1.522 +// ---------------------------------------------------------------------------
   1.523 +TInt CAudioCodec::GetInputPort(MInputPort*& aInputPort)
   1.524 +	{
   1.525 +	DP_CONTEXT(CAudioCodec::GetInputPort *CD1*, CtxDevSound, DPLOCAL);
   1.526 +	DP_IN();
   1.527 +	aInputPort = this;
   1.528 +	DP0_RET(KErrNone, "%d");
   1.529 +	}
   1.530 +
   1.531 +// ---------------------------------------------------------------------------
   1.532 +// from class MAudioComponent
   1.533 +// CAudioCodec::RegisterAudioComponentObserver
   1.534 +// ---------------------------------------------------------------------------
   1.535 +EXPORT_C TInt CAudioCodec::RegisterAudioCodecObserver(MAudioCodecAdaptationObserver& aObserver)
   1.536 +	{
   1.537 +	DP_CONTEXT(CAudioCodec::RegisterAudioCodecObserver *CD1*, CtxDevSound, DPLOCAL);
   1.538 +	DP_IN();
   1.539 +	TInt err = iAudioCodecObservers.Find(&aObserver);
   1.540 +	if(err == KErrNotFound)
   1.541 +		{
   1.542 +		iAudioCodecObservers.Append(&aObserver);
   1.543 +		err = KErrNone;
   1.544 +		}
   1.545 +	else
   1.546 +		{
   1.547 +		err = KErrAlreadyExists;
   1.548 +		}
   1.549 +	DP0_RET(err,"%d");
   1.550 +	}
   1.551 +
   1.552 +// ---------------------------------------------------------------------------
   1.553 +// from class MAudioComponent
   1.554 +// CAudioCodec::UnregisterAudioComponentObserver
   1.555 +// ---------------------------------------------------------------------------
   1.556 +EXPORT_C void CAudioCodec::UnregisterAudioCodecObserver(MAudioCodecAdaptationObserver& aObserver)
   1.557 +	{
   1.558 +	DP_CONTEXT(CAudioCodec::UnregisterAudioCodecObserver *CD1*, CtxDevSound, DPLOCAL);
   1.559 +	DP_IN();
   1.560 +	TInt idxOrErr = iAudioCodecObservers.Find(&aObserver);
   1.561 +	if( idxOrErr != KErrNotFound )
   1.562 +		{
   1.563 +		iAudioCodecObservers.Remove(idxOrErr);
   1.564 +		}
   1.565 +	DP_OUT();
   1.566 +	}
   1.567 +
   1.568 +// ---------------------------------------------------------------------------
   1.569 +// from class MInputPort
   1.570 +// CAudioCodec::EmptyBuffer
   1.571 +// ---------------------------------------------------------------------------
   1.572 +TInt CAudioCodec::EmptyBuffer(CMMFBuffer* /*aBuffer*/, MOutputPort* /*aSupplier*/)
   1.573 +	{
   1.574 +	DP_CONTEXT(CAudioCodec::EmptyBuffer *CD1*, CtxDevSound, DPLOCAL);
   1.575 +	DP_IN();
   1.576 +	DP0_RET(KErrNotReady, "%d");
   1.577 +	}
   1.578 +
   1.579 +// ---------------------------------------------------------------------------
   1.580 +// from class MInputPort
   1.581 +// CAudioCodec::BufferFilled
   1.582 +// ---------------------------------------------------------------------------
   1.583 +TInt CAudioCodec::BufferFilled(CMMFBuffer* aBuffer)
   1.584 +	{
   1.585 +	DP_CONTEXT(CAudioCodec::BufferFilled *CD1*, CtxDevSound, DPLOCAL);
   1.586 +	DP_IN();
   1.587 +	PRF(PRF_ID, PRF_STOP, PRF_LOAD, AA_DS_Fill, "");
   1.588 +	TInt err(KErrNone);
   1.589 +	if(aBuffer->LastBuffer())
   1.590 +		{
   1.591 +		iLastBuffer = ETrue;
   1.592 +		}
   1.593 +	if(iHwDevice)
   1.594 +		{
   1.595 +		err = iHwDevice->ThisHwBufferFilled(*aBuffer);
   1.596 +		}
   1.597 +	else
   1.598 +		{
   1.599 +		err = KErrNotFound;
   1.600 +		}
   1.601 +	DP0_RET(err,"%d");
   1.602 +	}
   1.603 +
   1.604 +// ---------------------------------------------------------------------------
   1.605 +// from class MInputPort
   1.606 +// CAudioCodec::SetOutput
   1.607 +// ---------------------------------------------------------------------------
   1.608 +TInt CAudioCodec::SetOutput(MOutputPort* aOutput)
   1.609 +	{
   1.610 +	DP_CONTEXT(CAudioCodec::SetOutput *CD1*, CtxDevSound, DPLOCAL);
   1.611 +	DP_IN();
   1.612 +	TInt err = KErrNone;
   1.613 +	
   1.614 +	iOutputPort = aOutput;
   1.615 +	
   1.616 +	DP0_RET(err, "%d");
   1.617 +	}
   1.618 +
   1.619 +// ---------------------------------------------------------------------------
   1.620 +// from class MInputPort
   1.621 +// CAudioCodec::RemoveOutput
   1.622 +// ---------------------------------------------------------------------------
   1.623 +TInt CAudioCodec::RemoveOutput(MOutputPort* /*aOutput*/)
   1.624 +	{
   1.625 +	DP_CONTEXT(CAudioCodec::RemoveOutput *CD1*, CtxDevSound, DPLOCAL);
   1.626 +	DP_IN();
   1.627 +	DP0_RET(KErrNone, "%d");
   1.628 +	}
   1.629 +
   1.630 +// ---------------------------------------------------------------------------
   1.631 +// from class MOutputPort
   1.632 +// CAudioCodec::FillBuffer
   1.633 +// ---------------------------------------------------------------------------
   1.634 +TInt CAudioCodec::FillBuffer(CMMFBuffer* /*aBuffer*/, MInputPort* /*aConsumer*/)
   1.635 +	{
   1.636 +	DP_CONTEXT(CAudioCodec::FillBuffer *CD1*, CtxDevSound, DPLOCAL);
   1.637 +	DP_IN();
   1.638 +	DP0_RET(KErrNotReady, "%d");
   1.639 +	}
   1.640 +
   1.641 +// ---------------------------------------------------------------------------
   1.642 +// from class MOutputPort
   1.643 +// CAudioCodec::BufferEmptied
   1.644 +// ---------------------------------------------------------------------------
   1.645 +TInt CAudioCodec::BufferEmptied(CMMFBuffer* aBuffer)
   1.646 +	{
   1.647 +	DP_CONTEXT(CAudioCodec::BufferEmptied *CD1*, CtxDevSound, DPLOCAL);
   1.648 +	DP_IN();
   1.649 +	TInt err(KErrNone);
   1.650 +	
   1.651 +	if (!aBuffer)
   1.652 +		{
   1.653 +		err = KErrNotFound;
   1.654 +		DP0_RET(err, "%d");
   1.655 +		}
   1.656 +
   1.657 +	if (iLastBufferAck) //if the hwdevice return a buffer with the lastbuffer flag set, rather than returning a non-empty buffer
   1.658 +		{				//in this case we generate an empty buffer on behalf of the adaptation
   1.659 +		TInt count = iAudioCodecObservers.Count();
   1.660 +		for ( TInt i(0); i < count; i++ ) // causes a ProcessingFinished() to be called on the stream
   1.661 +			{
   1.662 +			iAudioCodecObservers[i]->AllBuffersProcessed();
   1.663 +			}
   1.664 +		iLastBufferAck = EFalse;
   1.665 +		}
   1.666 +	else 
   1.667 +		{
   1.668 +		if(aBuffer->LastBuffer())
   1.669 +			{
   1.670 +			iLastBuffer = ETrue;
   1.671 +			}
   1.672 +		if(iHwDevice)
   1.673 +			{ 
   1.674 +			err = iHwDevice->ThisHwBufferEmptied(*aBuffer);
   1.675 +			}
   1.676 +		else
   1.677 +			{
   1.678 +			err = KErrNotFound;
   1.679 +			}
   1.680 +		}
   1.681 +	DP0_RET(err,"%d");
   1.682 +	}
   1.683 +
   1.684 +// ---------------------------------------------------------------------------
   1.685 +// from class MOutputPort
   1.686 +// CAudioCodec::SetInput
   1.687 +// ---------------------------------------------------------------------------
   1.688 +TInt CAudioCodec::SetInput(MInputPort* aInput)
   1.689 +	{
   1.690 +	DP_CONTEXT(CAudioCodec::SetInput *CD1*, CtxDevSound, DPLOCAL);
   1.691 +	DP_IN();
   1.692 +	TInt err = KErrNone;
   1.693 +	
   1.694 +	iInputPort = aInput;
   1.695 +	
   1.696 +	DP0_RET(err, "%d");
   1.697 +	}
   1.698 +
   1.699 +// ---------------------------------------------------------------------------
   1.700 +// from class MOutputPort
   1.701 +// CAudioCodec::RemoveInput
   1.702 +// ---------------------------------------------------------------------------
   1.703 +TInt CAudioCodec::RemoveInput(MInputPort* /*aInput*/)
   1.704 +	{
   1.705 +	return KErrNone;
   1.706 +	}
   1.707 +
   1.708 +// ---------------------------------------------------------------------------
   1.709 +// from class MOutputPort
   1.710 +// CAudioCodec::FlushBuffer
   1.711 +// ---------------------------------------------------------------------------
   1.712 +TInt CAudioCodec::FlushBuffer(MFlushHandlerObserver* aFlushObserver)
   1.713 +	{
   1.714 +	TInt err = KErrNone;
   1.715 +	TAny* ptr = NULL;
   1.716 +
   1.717 +	// Flush low layer buffers
   1.718 +	if(iHwDevice)
   1.719 +		{
   1.720 +		ptr = iHwDevice->CustomInterface(TUid::Uid(KMmfUidEmptyBuffersCustomInterface));
   1.721 +		MMMFDevSoundEmptyBuffers* emptybuffers = static_cast<MMMFDevSoundEmptyBuffers*>(ptr);
   1.722 +		if (emptybuffers)
   1.723 +			{
   1.724 +			// Due to defect DEF127468, this do nothing
   1.725 +			err = emptybuffers->EmptyBuffers();
   1.726 +			}
   1.727 +		else
   1.728 +			{
   1.729 +			err = KErrNotSupported;
   1.730 +			}
   1.731 +		}
   1.732 +	else
   1.733 +		{
   1.734 +		err = KErrNotReady;
   1.735 +		}
   1.736 +
   1.737 +	// Flush source through an asynchronous request
   1.738 +	if(err == KErrNone)
   1.739 +		{
   1.740 +		err = iOutputPort->FlushBuffer(aFlushObserver);
   1.741 +		}
   1.742 +	return err;
   1.743 +	}
   1.744 +
   1.745 +
   1.746 +// ---------------------------------------------------------------------------
   1.747 +// from class MMMFHwDeviceObserver
   1.748 +// CAudioCodec::FillThisHwBuffer
   1.749 +// ---------------------------------------------------------------------------
   1.750 +TInt CAudioCodec::FillThisHwBuffer(CMMFBuffer& aHwBuffer)
   1.751 +	{
   1.752 +	DP_CONTEXT(CAudioCodec::FillThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   1.753 +	DP_IN();
   1.754 +	PRF(PRF_ID, PRF_START, PRF_LOAD, AA_DS_Fill, "");
   1.755 +
   1.756 +	TInt err(KErrNone);
   1.757 +	iAdaptationBuffer = static_cast<CMMFDataBuffer*> (&aHwBuffer);
   1.758 +
   1.759 +	ASSERT(iAdaptationBuffer);
   1.760 +	if (iLastBuffer)
   1.761 +		{
   1.762 +		iAdaptationBuffer->Data().SetLength(0);
   1.763 +		BufferFilled(iAdaptationBuffer);
   1.764 +		}
   1.765 +	else
   1.766 +		{
   1.767 +		TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().MaxLength() ));
   1.768 +		if(err == KErrNone)
   1.769 +			{
   1.770 +			if(iOutputPort)
   1.771 +				{
   1.772 +				err = iOutputPort->FillBuffer(&aHwBuffer, this);
   1.773 +				}
   1.774 +			else
   1.775 +				{
   1.776 +				err = KErrNotFound;
   1.777 +				}
   1.778 +			}
   1.779 +		}
   1.780 +	DP0_RET(err,"%d");
   1.781 +	}
   1.782 +
   1.783 +// ---------------------------------------------------------------------------
   1.784 +// from class MMMFHwDeviceObserver
   1.785 +// CAudioCodec::EmptyThisHwBuffer
   1.786 +// ---------------------------------------------------------------------------
   1.787 +TInt CAudioCodec::EmptyThisHwBuffer(CMMFBuffer& aHwBuffer)
   1.788 +	{
   1.789 +	DP_CONTEXT(CAudioCodec::EmptyThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   1.790 +	DP_IN();
   1.791 +
   1.792 +	TInt err(KErrNone);
   1.793 +	iAdaptationBuffer = static_cast<CMMFDataBuffer*> (&aHwBuffer);
   1.794 +	ASSERT(iAdaptationBuffer);
   1.795 +	if(iHwDeviceState == EPaused)
   1.796 +		{
   1.797 +		if (iAdaptationBuffer->Data().Length() == 0) // empty buffer
   1.798 +			{
   1.799 +			TInt count = iAudioCodecObservers.Count();
   1.800 +			for ( TInt i(0); i < count; i++ ) // causes a ProcessingFinished() to be called on the stream
   1.801 +				{
   1.802 +				iAudioCodecObservers[i]->AllBuffersProcessed();
   1.803 +				}
   1.804 +			}
   1.805 +		else
   1.806 +			{
   1.807 +			if(aHwBuffer.LastBuffer())
   1.808 +				{
   1.809 +				aHwBuffer.SetLastBuffer(EFalse);	// a buffer coming from hardware device should never have last buffer set...
   1.810 +				iLastBufferAck = ETrue;
   1.811 +				}
   1.812 +			
   1.813 +			TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().Length()));
   1.814 +			if(err == KErrNone)
   1.815 +				{
   1.816 +				if(iInputPort)
   1.817 +					{
   1.818 +					err = iInputPort->EmptyBuffer(&aHwBuffer, this);
   1.819 +					}
   1.820 +				else
   1.821 +					{
   1.822 +					err = KErrNotFound;
   1.823 +					}
   1.824 +				}			
   1.825 +			}
   1.826 +		}
   1.827 +	else
   1.828 +		{
   1.829 +		TRAP(err,iAdaptationBuffer->SetRequestSizeL(iAdaptationBuffer->Data().Length()));
   1.830 +		if(err == KErrNone)
   1.831 +			{
   1.832 +			if(iInputPort)
   1.833 +				{
   1.834 +				err = iInputPort->EmptyBuffer(&aHwBuffer, this);
   1.835 +				}
   1.836 +			else
   1.837 +				{
   1.838 +				err = KErrNotFound;
   1.839 +				}
   1.840 +			}
   1.841 +		}
   1.842 +	DP0_RET(err,"%d");
   1.843 +	}
   1.844 +
   1.845 +// ---------------------------------------------------------------------------
   1.846 +// from class MMMFHwDeviceObserver
   1.847 +// CAudioCodec::MsgFromHwDevice
   1.848 +// ---------------------------------------------------------------------------
   1.849 +TInt CAudioCodec::MsgFromHwDevice(TUid aMessageType, const TDesC8& aMsg)
   1.850 +	{
   1.851 +	DP_CONTEXT(CAudioCodec::MsgFromHwDevice *CD1*, CtxDevSound, DPLOCAL);
   1.852 +	DP_IN();
   1.853 +	TBuf<50> formattedNumber;
   1.854 +	formattedNumber.Format(_L("MessageType 0x%08x"), aMessageType.iUid);
   1.855 +	TBuf<50> buf;
   1.856 +	buf.Copy(aMsg);
   1.857 +	buf.Append(formattedNumber);
   1.858 +	if(aMessageType.iUid == KMmfHwDeviceObserverUpdateBytesPlayed)
   1.859 +		{
   1.860 +		//This is used by sw codec wrapper to request a bytes played update
   1.861 +		//Bytes played won't be updated in Stopped() or Error() on sw codec wrapper
   1.862 +		//As the sound device is closed. Non sw codec wrapper Hw device plugins
   1.863 +		//Can get the bytes updated on Stopped() and/or Error()
   1.864 +		UpdateBytesPlayed();
   1.865 +		}
   1.866 +	DP1(DLINFO, "DeviceMessage is %S",&buf);
   1.867 +	DP0_RET(KErrNone, "%d");
   1.868 +	}
   1.869 +
   1.870 +// ---------------------------------------------------------------------------
   1.871 +// from class MMMFHwDeviceObserver
   1.872 +// CAudioCodec::Stopped
   1.873 +// ---------------------------------------------------------------------------
   1.874 +void CAudioCodec::Stopped()
   1.875 +	{
   1.876 +	DP_CONTEXT(CAudioCodec::Stopped *CD1*, CtxDevSound, DPLOCAL);
   1.877 +	DP_IN();
   1.878 +	//For sw codec wrappers hw devices bytes played are updated in MsgFromHwDevice
   1.879 +	//But non sw codec wrappers hw devices may do it differently
   1.880 +	//Also don't know if non sw codec wrappers hw devices will call Stopped or Error first
   1.881 +	UpdateBytesPlayed();
   1.882 +	DP_OUT();
   1.883 +	}
   1.884 +
   1.885 +// ---------------------------------------------------------------------------
   1.886 +// from class MMMFHwDeviceObserver
   1.887 +// CAudioCodec::Error
   1.888 +// ---------------------------------------------------------------------------
   1.889 +//
   1.890 +void CAudioCodec::Error(TInt aError)
   1.891 +	{
   1.892 +	DP_CONTEXT(CAudioCodec::Error *CD1*, CtxDevSound, DPLOCAL);
   1.893 +	DP_IN();
   1.894 +
   1.895 +	TTimeIntervalMicroSeconds currentPosition;
   1.896 +	TInt err = KErrNone;
   1.897 +
   1.898 +#ifdef _DEBUG
   1.899 +	RDebug::Print(_L("iDeviceUid=0x%x  aError=%d iLastBuffer=%d"), iDeviceUid.iUid, aError, iLastBuffer);
   1.900 +#endif 
   1.901 +	// The most problable receiver is the logicalaudiocodec which should notify to the client through MAudioProcessingUnit
   1.902 +	TUint count = iAudioCodecObservers.Count();
   1.903 +	
   1.904 +	// Notify only for the
   1.905 +	if (aError == KErrUnderflow && iMode == EDecode ) 
   1.906 +		{
   1.907 +		// Notify the observers
   1.908 +		if(iLastBuffer)
   1.909 +			{
   1.910 +			for ( TUint i(0); i < count; i++ )
   1.911 +				{
   1.912 +				iAudioCodecObservers[i]->AllBuffersProcessed();
   1.913 +				}
   1.914 +			}
   1.915 +		else
   1.916 +			{		
   1.917 +			// Re-start decoding
   1.918 +
   1.919 +			//For sw codec wrappers hw devices bytes played are updated in MsgFromHwDevice
   1.920 +			//But non sw codec wrappers hw devices may do it differently
   1.921 +			//Also don't know if non sw codec wrappers hw devices will call Stopped or Error first
   1.922 +			UpdateBytesPlayed();
   1.923 +
   1.924 +			err = GetControlPosition(currentPosition);
   1.925 +			if(err != KErrNone)
   1.926 +				{
   1.927 +				// Ignore safely
   1.928 +				}
   1.929 +			
   1.930 +#ifdef _DEBUG
   1.931 +	RDebug::Print(_L("Restart decoding"));
   1.932 +#endif 			
   1.933 +					
   1.934 +			Start();
   1.935 +			}
   1.936 +		}
   1.937 +	else if (aError == KErrOverflow && iMode == EEncode )
   1.938 +		{
   1.939 +		if (!iLastBuffer  && iHwDeviceState == ERunning)
   1.940 +			{
   1.941 +			// Re-start encoding
   1.942 +			err = GetControlPosition(currentPosition);
   1.943 +			if(err != KErrNone)
   1.944 +				{
   1.945 +				// Ignore safely
   1.946 +				}
   1.947 +			Start();
   1.948 +			}
   1.949 +		else
   1.950 +			{
   1.951 +				if (iDeviceUid.iUid != KUidToneHwDevice) 
   1.952 +					{
   1.953 +#ifdef _DEBUG
   1.954 +	RDebug::Print(_L("Is this is not needed for other encoders but toneHwDevice"));
   1.955 +#endif 
   1.956 +					for ( TUint i(0); i < count; i++ )
   1.957 +						{
   1.958 +						iAudioCodecObservers[i]->AllBuffersProcessed();
   1.959 +						}
   1.960 +					}
   1.961 +			}
   1.962 +		}
   1.963 +	else
   1.964 +		{
   1.965 +		for ( TUint i(0); i < count; i++ )
   1.966 +			{
   1.967 +			iAudioCodecObservers[i]->ProcessingUnitError(aError);
   1.968 +			}
   1.969 +		}
   1.970 +	DP_OUT();
   1.971 +	}
   1.972 +
   1.973 +// HELPER CLASS
   1.974 +// ---------------------------------------------------------------------------
   1.975 +// from class MGainHelper
   1.976 +// CAudioCodec::GetGain
   1.977 +// ---------------------------------------------------------------------------
   1.978 +TInt CAudioCodec::GetGain(TInt& aGain) const
   1.979 +	{
   1.980 +	DP_CONTEXT(CAudioCodec::GetGain *CD1*, CtxDevSound, DPLOCAL);
   1.981 +	DP_IN();
   1.982 +	TInt err = KErrNone;
   1.983 +	if (iMode == EDecode  && iPlayCustomInterface)
   1.984 +		{
   1.985 +		aGain = iPlayCustomInterface->Volume();
   1.986 +		}
   1.987 +	else if( iMode == EEncode  && iRecordCustomInterface) 
   1.988 +		{
   1.989 +		aGain = iRecordCustomInterface->Gain();
   1.990 +		}
   1.991 +	DP0_RET(err, "%d");
   1.992 +	}
   1.993 +
   1.994 +// ---------------------------------------------------------------------------
   1.995 +// from class MGainHelper
   1.996 +// CAudioCodec::SetGain
   1.997 +// ---------------------------------------------------------------------------
   1.998 +TInt CAudioCodec::SetGain(RArray<TAudioChannelGain>& aChannels)
   1.999 +	{
  1.1000 +	DP_CONTEXT(CAudioCodec::SetGain *CD1*, CtxDevSound, DPLOCAL);
  1.1001 +	DP_IN();
  1.1002 +	TInt err = KErrNone;
  1.1003 +
  1.1004 +	// Current adaptation doesn't support multichannel
  1.1005 +	// Use average
  1.1006 +	TUint count = aChannels.Count();
  1.1007 +	TInt gain = 0;
  1.1008 +	if (count)
  1.1009 +		{
  1.1010 +		TInt totalGain = 0;
  1.1011 +		for (TUint i(0); i < count; i++)
  1.1012 +			{
  1.1013 +			totalGain  =+ aChannels[i].iGain;
  1.1014 +			}
  1.1015 +		gain = totalGain / count;
  1.1016 +		}
  1.1017 +
  1.1018 +	if (gain > KDefaultMaxGain)
  1.1019 +		{
  1.1020 +		gain = KDefaultMaxGain;
  1.1021 +		}
  1.1022 +	else if (gain < 0)
  1.1023 +		{
  1.1024 +		gain = 0;
  1.1025 +		}
  1.1026 +
  1.1027 +	if (iMode == EDecode  && iPlayCustomInterface)
  1.1028 +		{
  1.1029 +		iPlayCustomInterface->SetVolume(gain);
  1.1030 +		}
  1.1031 +	else if( iMode == EEncode  && iRecordCustomInterface) 
  1.1032 +		{
  1.1033 +		iRecordCustomInterface->SetGain(gain);
  1.1034 +		}
  1.1035 +	
  1.1036 +	DP0_RET(err, "%d");
  1.1037 +		}
  1.1038 +
  1.1039 +
  1.1040 +TInt CAudioCodec::ConfigureRamp(TUid aRampOperation, const TTimeIntervalMicroSeconds& aRampDuration)
  1.1041 +	{
  1.1042 +	DP_CONTEXT(CAudioCodec::ConfigureRamp *CD1*, CtxDevSound, DPLOCAL);
  1.1043 +	DP_IN();
  1.1044 +	TInt err = KErrNone;
  1.1045 +
  1.1046 +	if (aRampOperation == KUidGainSawTooth)
  1.1047 +		{
  1.1048 +		if (iMode == EDecode  && iPlayCustomInterface)
  1.1049 +			{
  1.1050 +			iPlayCustomInterface->SetVolumeRamp(aRampDuration);
  1.1051 +			}
  1.1052 +		}
  1.1053 +	else if (iRampOperation == KNullUid)
  1.1054 +		{
  1.1055 +		if (iMode == EDecode  && iPlayCustomInterface)
  1.1056 +			{
  1.1057 +			iPlayCustomInterface->SetVolumeRamp(0);
  1.1058 +			}
  1.1059 +		}
  1.1060 +	else
  1.1061 +		{
  1.1062 +		err = KErrA3fUnsupportedRamp;
  1.1063 +		}
  1.1064 +
  1.1065 +	DP0_RET(err, "%d");
  1.1066 +	}
  1.1067 +
  1.1068 +// ---------------------------------------------------------------------------
  1.1069 +// from class MPositionControl
  1.1070 +// CAudioCodec::GetControlPosition
  1.1071 +// ---------------------------------------------------------------------------
  1.1072 +TInt CAudioCodec::GetControlPosition(TTimeIntervalMicroSeconds& aPosition)
  1.1073 +	{
  1.1074 +	DP_CONTEXT(CAudioCodec::GetControlPosition *CD1*, CtxDevSound, DPLOCAL);
  1.1075 +	DP_IN();
  1.1076 +	TInt err = KErrNone;
  1.1077 +	TInt sampleRateValue = iSampleRateConfig;
  1.1078 +
  1.1079 +	// At this adaptation EMMFSoundEncoding16BitPCM encoding is assumed
  1.1080 +	// Due RMdaDevSound which is always pcm16 each sample is 2 bytes
  1.1081 +	TInt bytesPerAudioSample = 2;
  1.1082 +
  1.1083 +	// Here is secure to convert to TInt a TAudioMode since the values 
  1.1084 +	// are chossen according to the value they represent.
  1.1085 +	TInt numberOfChannels = ResolveMode(iModeConfig);
  1.1086 +
  1.1087 +	TInt64 samples = 0;
  1.1088 +	if (err == KErrNone)
  1.1089 +		{
  1.1090 +		if( iMode == EDecode && iPlayCustomInterface)
  1.1091 +			{
  1.1092 +			TInt64 bytesPlayed = iPlayCustomInterface->BytesPlayed();
  1.1093 +			if (bytesPlayed)
  1.1094 +				{
  1.1095 +				iPlayedBytesCount = bytesPlayed;
  1.1096 +				}
  1.1097 +
  1.1098 +			samples = iPlayedBytesCount;
  1.1099 +
  1.1100 +			if(numberOfChannels > 1)
  1.1101 +				{
  1.1102 +				samples /= numberOfChannels;
  1.1103 +				}
  1.1104 +
  1.1105 +			if(bytesPerAudioSample > 1)
  1.1106 +				{
  1.1107 +				samples /= bytesPerAudioSample;
  1.1108 +				}
  1.1109 +			}
  1.1110 +		else if( iMode == EEncode && iRecordCustomInterface)
  1.1111 +			{
  1.1112 +			samples = iRecordCustomInterface->BytesRecorded();
  1.1113 +			if(numberOfChannels > 1)
  1.1114 +				{
  1.1115 +				samples /= numberOfChannels;
  1.1116 +				}
  1.1117 +			if(bytesPerAudioSample > 1)
  1.1118 +				{
  1.1119 +				samples /= bytesPerAudioSample;
  1.1120 +				}
  1.1121 +			}
  1.1122 +		}
  1.1123 +
  1.1124 +	if(sampleRateValue)
  1.1125 +		{
  1.1126 +		iPosition = (TInt64(samples) * KMicroSecsInOneSec / sampleRateValue);
  1.1127 +		}
  1.1128 +	aPosition = iPosition;
  1.1129 +	DP1(DLINFO,"GetControlPosition Samples = %d", samples);
  1.1130 +#ifdef _DEBUG
  1.1131 +	RDebug::Print(_L("GetControlPosition Position=%d"), iPosition);
  1.1132 +#endif 
  1.1133 +	DP0_RET(err, "%d");
  1.1134 +	}
  1.1135 +
  1.1136 +// ---------------------------------------------------------------------------
  1.1137 +// CAudioCodec::ResetControlPosition
  1.1138 +// ---------------------------------------------------------------------------
  1.1139 +void CAudioCodec::ResetControlPosition()
  1.1140 +	{
  1.1141 +	DP_CONTEXT(CAudioCodec::ResetControlPosition *CD1*, CtxDevSound, DPLOCAL);
  1.1142 +	DP_IN();
  1.1143 +	iPosition = 0;
  1.1144 +	iPlayedBytesCount = 0;
  1.1145 +	DP_OUT();
  1.1146 +	}
  1.1147 +
  1.1148 +// ---------------------------------------------------------------------------
  1.1149 +// CAudioCodec::FourCCToHwDeviceUid
  1.1150 +// ---------------------------------------------------------------------------
  1.1151 +void CAudioCodec::FourCCToHwDeviceUidL(TFourCC aFourCC, TMode aMode, TUid &aHWDev)
  1.1152 +	{
  1.1153 +	DP_CONTEXT(CAudioCodec::FourCCToHwDeviceUid *CD1*, CtxDevSound, DPLOCAL);
  1.1154 +	DP_IN();
  1.1155 +			
  1.1156 +	//check argument precondition for aState
  1.1157 +	if ((aMode != EDecode) && (aMode != EEncode))
  1.1158 +		{
  1.1159 +		User::Leave(KErrArgument);
  1.1160 +		}
  1.1161 +
  1.1162 +	// Array to return hw device plugin resource info(place on cleanupstack
  1.1163 +	// _after_ ListImplementationsL() )
  1.1164 +	RImplInfoPtrArray plugInArray;
  1.1165 +	TUid KUidMmfHWPluginInterfaceCodec = {KMmfUidPluginInterfaceHwDevice};
  1.1166 +
  1.1167 +	// ListImplementationsL leaves if it cannot find anything so trap the error
  1.1168 +	TRAPD(err, REComSession::ListImplementationsL(KUidMmfHWPluginInterfaceCodec,
  1.1169 +													plugInArray));
  1.1170 +	CleanupResetAndDestroyPushL(plugInArray);	
  1.1171 +
  1.1172 +	TUint numberOfHwDevicePlugins = plugInArray.Count();
  1.1173 +
  1.1174 +	// if no errors and have hwdevice plugin resource entries then scan entries
  1.1175 +	// matching on a datatype of pcm16 as the destination datatype for play and
  1.1176 +	// the source datatype for record. If a match is found and isn't already in
  1.1177 +	// the list of supported data types, then add it to the list
  1.1178 +	if ((err == KErrNone) && (numberOfHwDevicePlugins))
  1.1179 +		{
  1.1180 +		CImplementationInformation* hwDeviceResourceEntry = NULL;
  1.1181 +		_LIT8(KPCM16FourCCString, " P16");
  1.1182 +		TBufC8<KFOURCCLENGTH> fourCCStringPCM16(KPCM16FourCCString);
  1.1183 +		TPtr8 fourCCPtrPCM16 = fourCCStringPCM16.Des();
  1.1184 +
  1.1185 +		// check each resource entry for dst 4CC = P16 for play and
  1.1186 +		// src 4CC = P16 for record
  1.1187 +		for (TUint hwDeviceEntry = 0;
  1.1188 +				hwDeviceEntry < numberOfHwDevicePlugins;
  1.1189 +				hwDeviceEntry++)
  1.1190 +			{
  1.1191 +			hwDeviceResourceEntry = plugInArray[hwDeviceEntry];
  1.1192 +
  1.1193 +			if (IsDataTypeMatch(hwDeviceResourceEntry, fourCCPtrPCM16, aMode))
  1.1194 +				{
  1.1195 +				// resource entry data field has dest/src datatype ' P16'
  1.1196 +				// i.e. pcm16 for play/record
  1.1197 +				TPtrC8 fourCCPtr(0,0);
  1.1198 +
  1.1199 +				if (aMode == EDecode)
  1.1200 +					{
  1.1201 +					// datatype supported 4CC is left 4 chars
  1.1202 +					fourCCPtr.Set(
  1.1203 +					hwDeviceResourceEntry->DataType().Left(KFOURCCLENGTH));
  1.1204 +					}
  1.1205 +				else if (aMode == EEncode)
  1.1206 +					{
  1.1207 +					// datatype supported 4CC is right 4 chars
  1.1208 +					fourCCPtr.Set(
  1.1209 +					hwDeviceResourceEntry->DataType().Right(KFOURCCLENGTH));
  1.1210 +					}
  1.1211 +
  1.1212 +				TFourCC fourCCEntry(fourCCPtr);
  1.1213 +
  1.1214 +				// New Mapping
  1.1215 +				if (fourCCEntry == aFourCC)
  1.1216 +					{
  1.1217 +					// get the Uid
  1.1218 +					aHWDev = hwDeviceResourceEntry->ImplementationUid();
  1.1219 +					break;
  1.1220 +					}
  1.1221 +				}
  1.1222 +			}
  1.1223 +			
  1.1224 +		// Verify there is a HwDeviceUid
  1.1225 +		if(aHWDev.iUid == 0)
  1.1226 +			{
  1.1227 +			User::Leave(KErrNotSupported);
  1.1228 +			}
  1.1229 +		}
  1.1230 +	else
  1.1231 +		{
  1.1232 +		// if an error occured and not KErrNotFound then must be a 'real' error
  1.1233 +		// e.g. KErrNoMemory
  1.1234 +		if ((err != KErrNotFound) && (err != KErrNone))
  1.1235 +			{
  1.1236 +			User::Leave(err);
  1.1237 +			}
  1.1238 +		}
  1.1239 +
  1.1240 +	CleanupStack::PopAndDestroy(&plugInArray);
  1.1241 +	DP_OUT();
  1.1242 +	}
  1.1243 +
  1.1244 +// ---------------------------------------------------------------------------
  1.1245 +// CAudioCodec::IsDataTypeMatch
  1.1246 +// ---------------------------------------------------------------------------
  1.1247 +TBool CAudioCodec::IsDataTypeMatch(CImplementationInformation* aHwDeviceResourceEntry,
  1.1248 +	const TDesC8& aHwMatchFourCC, TMode aState)
  1.1249 +	{
  1.1250 +	DP_CONTEXT(CAudioCodec::IsDataTypeMatch *CD1*, CtxDevSound, DPLOCAL);
  1.1251 +	DP_IN();
  1.1252 +	TBool match = EFalse;
  1.1253 +
  1.1254 +	// Check for resource entry lenght since for HwDeviceAdaptor it is just "*"
  1.1255 +	if (aHwDeviceResourceEntry->DataType().Length()>=KHwDeviceDefaultDataLength)
  1.1256 +		{
  1.1257 +		if (aState == EDecode)
  1.1258 +			{
  1.1259 +			//play need to match with the right four characters
  1.1260 +			match = (!(aHwMatchFourCC.Match(
  1.1261 +				aHwDeviceResourceEntry->DataType().Right(KFOURCCLENGTH)) ==
  1.1262 +				KErrNotFound));
  1.1263 +			}
  1.1264 +		else if (aState == EEncode)
  1.1265 +			{
  1.1266 +			//record need to match with the left four characters
  1.1267 +			match =
  1.1268 +				(!(aHwMatchFourCC.Match(
  1.1269 +				aHwDeviceResourceEntry->DataType().Left(KFOURCCLENGTH)) ==
  1.1270 +				KErrNotFound));
  1.1271 +			}
  1.1272 +		}
  1.1273 +	else
  1.1274 +		{
  1.1275 +#ifdef _DEBUG
  1.1276 +		RDebug::Print(_L("HwDeviceEntry %S"), &aHwDeviceResourceEntry->DataType());
  1.1277 +#endif			
  1.1278 +		}
  1.1279 +
  1.1280 +	DP0_RET(match, "0x%x");
  1.1281 +	}
  1.1282 +
  1.1283 +// ---------------------------------------------------------------------------
  1.1284 +// CAudioCodec::ResolveMode
  1.1285 +// ---------------------------------------------------------------------------
  1.1286 +TInt CAudioCodec::ResolveMode(TUid aMode)
  1.1287 +	{
  1.1288 +	DP_CONTEXT(CAudioCodec::ResolveMode *CD1*, CtxDevSound, DPLOCAL);
  1.1289 +	DP_IN();
  1.1290 +	TInt result = 0;
  1.1291 +
  1.1292 +	//Mapping an uid mode to an int channel
  1.1293 +	for (TUint i=0; i<=KMaxModeIndex; i++)
  1.1294 +		{
  1.1295 +		if(KModeTableLookup[i].iAudioMode == aMode)
  1.1296 +			{
  1.1297 +			result = KModeTableLookup[i].iAudioModeValue;
  1.1298 +			break;
  1.1299 +			}
  1.1300 +		}
  1.1301 +	DP0_RET(result, "%d");
  1.1302 +	}
  1.1303 +
  1.1304 +// ---------------------------------------------------------------------------
  1.1305 +// CAudioCodec::SupportedRates
  1.1306 +// ---------------------------------------------------------------------------
  1.1307 +EXPORT_C TInt CAudioCodec::SupportedRates(RArray<TInt>& aSupportedRates)
  1.1308 +	{
  1.1309 +	DP_CONTEXT(CAudioCodec::Service *CD1*, CtxDevSound, DPLOCAL);
  1.1310 +	DP_IN();
  1.1311 +	iRequest |= KRequestSampleRate;
  1.1312 +
  1.1313 +	//Get request capabilities
  1.1314 +	iErrorRates = GetSupportedSampleRates(aSupportedRates);
  1.1315 +	
  1.1316 +	// Simulate an asyncronous response
  1.1317 +	if (!IsActive())
  1.1318 +		{
  1.1319 +		TRequestStatus* status = &iStatus;
  1.1320 +		User::RequestComplete(status, KErrNone);
  1.1321 +		SetActive();
  1.1322 +		}
  1.1323 +	DP0_RET(iErrorRates, "%d");
  1.1324 +	}
  1.1325 +
  1.1326 +// ---------------------------------------------------------------------------
  1.1327 +// CAudioCodec::SupportedModes
  1.1328 +// ---------------------------------------------------------------------------
  1.1329 +EXPORT_C TInt CAudioCodec::SupportedModes(RArray<TUid>& aSupportedModes)
  1.1330 +	{
  1.1331 +	DP_CONTEXT(CAudioCodec::Service *CD1*, CtxDevSound, DPLOCAL);
  1.1332 +	DP_IN();
  1.1333 +	iRequest |= KRequestMode;
  1.1334 +
  1.1335 +	iErrorModes = GetSupportedModes(aSupportedModes);
  1.1336 +
  1.1337 +	// Simulate an asyncronous response
  1.1338 +	if (!IsActive())
  1.1339 +		{
  1.1340 +		TRequestStatus* status = &iStatus;
  1.1341 +		User::RequestComplete(status, KErrNone);
  1.1342 +		SetActive();
  1.1343 +		}
  1.1344 +	DP0_RET(iErrorModes, "%d");
  1.1345 +	}
  1.1346 +
  1.1347 +
  1.1348 +//From CActive
  1.1349 +// ---------------------------------------------------------------------------
  1.1350 +// CAudioCodec::RunL
  1.1351 +// ---------------------------------------------------------------------------
  1.1352 +void CAudioCodec::RunL()
  1.1353 +	{
  1.1354 +	DP_CONTEXT(CAudioCodec::RunL *CD1*, CtxDevSound, DPLOCAL);
  1.1355 +	DP_IN();
  1.1356 +
  1.1357 +	// Send the callback for the request operation
  1.1358 +	if (iRequest & KRequestMode)
  1.1359 +		{
  1.1360 +		TUint count = iAudioCodecObservers.Count();
  1.1361 +		for ( TUint i(0); i < count; i++ )
  1.1362 +			{
  1.1363 +			iAudioCodecObservers[i]->GetSupportedAModesComplete(iErrorModes);
  1.1364 +			}
  1.1365 +
  1.1366 +		// Reset the request flag
  1.1367 +		iRequest &= ~KRequestMode;
  1.1368 +		}
  1.1369 +
  1.1370 +	// Send the callback for the request operation
  1.1371 +	if (iRequest & KRequestSampleRate)
  1.1372 +		{
  1.1373 +		TUint count = iAudioCodecObservers.Count();
  1.1374 +		for ( TUint i(0); i < count; i++ )
  1.1375 +			{
  1.1376 +			iAudioCodecObservers[i]->GetSupportedARatesComplete(iErrorRates);
  1.1377 +			}
  1.1378 +
  1.1379 +		// Reset the request flag
  1.1380 +		iRequest &= ~KRequestSampleRate;
  1.1381 +		}
  1.1382 +
  1.1383 +	DP_OUT();
  1.1384 +	}
  1.1385 +
  1.1386 +// ---------------------------------------------------------------------------
  1.1387 +// CAudioCodec::RunError
  1.1388 +// ---------------------------------------------------------------------------
  1.1389 +TInt CAudioCodec::RunError(TInt aError)
  1.1390 +	{
  1.1391 +	DP_CONTEXT(CAudioCodec::RunError *CD1*, CtxDevSound, DPLOCAL);
  1.1392 +	DP_IN();
  1.1393 +	DP0_RET(aError, "%d");
  1.1394 +	}
  1.1395 +
  1.1396 +// ---------------------------------------------------------------------------
  1.1397 +// CAudioCodec::DoCancel
  1.1398 +// ---------------------------------------------------------------------------
  1.1399 +void CAudioCodec::DoCancel()
  1.1400 +	{
  1.1401 +	DP_CONTEXT(CAudioCodec::DoCancel *CD1*, CtxDevSound, DPLOCAL);
  1.1402 +	DP_IN();
  1.1403 +	iRequest = 0;
  1.1404 +	DP_OUT();
  1.1405 +	}
  1.1406 +
  1.1407 +// end of file