os/mm/mmdevicefw/mdf/src/audio/AudioDevice/audiodevice.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmdevicefw/mdf/src/audio/AudioDevice/audiodevice.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,749 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <ecom/implementationproxy.h>
    1.20 +#include <mmf/server/mmfdatabuffer.h>
    1.21 +#include "audiodevice.hrh"
    1.22 +#include "audiodevice.h"
    1.23 +#include <e32debug.h>
    1.24 +#include <mdf/mdfpuconfig.h>
    1.25 +#include <mda/common/audio.h>
    1.26 +
    1.27 +// we need 16k to hold a pcm packet
    1.28 +const TInt KBufferSize = 16384;
    1.29 +
    1.30 +const TInt KDefaultSampleRate = 8000;
    1.31 +const TInt KDefaultNumberChannels = 1;
    1.32 +_LIT(KAudioDevicePanic, "CAudioDevice Panic");
    1.33 +
    1.34 +enum TAudioDevicePanics
    1.35 +	{
    1.36 +	EObserverNotSet, 
    1.37 +	EInvalidUsage
    1.38 +	};
    1.39 +	
    1.40 +const TInt KInputPortIndex = 0;
    1.41 +const TInt KOutputPortIndex = 1;
    1.42 +	
    1.43 +// ------------------------------------------------------------------------------------------	
    1.44 +// CAudioDevice::CInput port class implementation
    1.45 +
    1.46 +	
    1.47 +CAudioDevice::CInputPort::CInputPort(CAudioDevice& aParent) 
    1.48 +	: CActive(EPriorityNormal),
    1.49 +	  iParent(aParent),
    1.50 +	  iSampleRate(KDefaultSampleRate),
    1.51 +	  iChannels(KDefaultNumberChannels),
    1.52 +	  iBufferSize(KBufferSize)
    1.53 +	{
    1.54 +	}
    1.55 +		
    1.56 +CAudioDevice::CInputPort::~CInputPort()
    1.57 +	{
    1.58 +	Cancel();
    1.59 +	iBuffers.Close();
    1.60 +	}
    1.61 +
    1.62 +CAudioDevice::CInputPort* CAudioDevice::CInputPort::NewL(CAudioDevice& aParent)
    1.63 +	{
    1.64 +	CInputPort* self = new (ELeave) CInputPort(aParent);
    1.65 +	CleanupStack::PushL(self);
    1.66 +	self->ConstructL();
    1.67 +	CleanupStack::Pop(self);
    1.68 +	return self;
    1.69 +	}
    1.70 +
    1.71 +void CAudioDevice::CInputPort::ConstructL()
    1.72 +	{
    1.73 +	CActiveScheduler::Add(this);	
    1.74 +	}
    1.75 +
    1.76 +TInt CAudioDevice::CInputPort::MipConfigure(const TPuConfig& aConfig)
    1.77 +	{
    1.78 +	if (aConfig.Uid() == TUid::Uid(KUidTTaskConfig))
    1.79 +		{
    1.80 +		const TTaskConfig* config = TPuTaskConfig::GetStructure(aConfig);	
    1.81 +	
    1.82 +		iSampleRate = config->iRate;
    1.83 +		iChannels = (config->iStereoMode & ETaskMono)? 1 : 2;
    1.84 +		iInterleaved = (config->iStereoMode & ETaskInterleaved)?ETrue : EFalse;
    1.85 +		return KErrNone;
    1.86 +		}
    1.87 +	
    1.88 +	if (aConfig.Uid() == TUid::Uid(KUidTTaskConfig2))
    1.89 +		{
    1.90 +		const TTaskConfig2* config = TPuTaskConfig2::GetStructure(aConfig);	
    1.91 +	
    1.92 +		iSampleRate = config->iRate;
    1.93 +		iChannels = config->iNumberOfChannels;
    1.94 +		iInterleaved = (config->iStereoMode & ETaskInterleaved)?ETrue : EFalse;
    1.95 +		return KErrNone;
    1.96 +		}	
    1.97 +		
    1.98 +	return KErrNotSupported;
    1.99 +	}
   1.100 +
   1.101 +TInt CAudioDevice::CInputPort::MipGetConfig(TPuConfig& /*aConfig*/)
   1.102 +	{
   1.103 +	return KErrNotSupported;
   1.104 +	}
   1.105 +
   1.106 +TInt CAudioDevice::CInputPort::MipTunnelRequest(const MMdfOutputPort& aOutputPortToBeConnectedTo,
   1.107 +			TTunnelFlags& /*aTunnelFlags*/, TSupplierType& /*aSupplierType*/)
   1.108 +	{
   1.109 +	if ((iParent.State()!=EProcessingUnitLoaded) && (!iStopped))
   1.110 +		{
   1.111 +		// invalid state
   1.112 +		return EInvalidState;
   1.113 +		}
   1.114 +	
   1.115 +	if (iPortConnectedTo)
   1.116 +		{
   1.117 +		// the port is already connected, return an error
   1.118 +		return EPortAlreadyTunnelled;
   1.119 +		}
   1.120 +	iPortConnectedTo = const_cast<MMdfOutputPort*>(&aOutputPortToBeConnectedTo);
   1.121 +	return KErrNone;
   1.122 +	}
   1.123 +
   1.124 +void CAudioDevice::CInputPort::MipWriteData(CMMFBuffer& aInputBuffer)
   1.125 +	{
   1.126 +	TInt err = iBuffers.Append(&aInputBuffer);
   1.127 +	if (err == KErrNone)
   1.128 +		{
   1.129 +		if (iParent.State() == EProcessingUnitExecuting && !IsActive())
   1.130 +			{
   1.131 +			SetActive();
   1.132 +			TRequestStatus* status = &iStatus;
   1.133 +			User::RequestComplete(status, KErrNone);
   1.134 +			}
   1.135 +		}
   1.136 +	else
   1.137 +		{
   1.138 +		if (iObserver)
   1.139 +			{
   1.140 +			iObserver->MipoWriteDataComplete(this, &aInputBuffer, err);
   1.141 +			}
   1.142 +		}
   1.143 +	}
   1.144 +
   1.145 +void CAudioDevice::CInputPort::Execute()
   1.146 +	{
   1.147 +	if (!IsActive() && iBuffers.Count()>0)
   1.148 +		{
   1.149 +		SetActive();
   1.150 +		TRequestStatus* status = &iStatus;
   1.151 +		User::RequestComplete(status, KErrNone);
   1.152 +		}
   1.153 +	}
   1.154 +
   1.155 +TBool CAudioDevice::CInputPort::MipIsTunnelled() const
   1.156 +	{
   1.157 +	if (iPortConnectedTo)
   1.158 +		{
   1.159 +		return ETrue;
   1.160 +		}
   1.161 +	else
   1.162 +		{
   1.163 +		return EFalse;			
   1.164 +		}
   1.165 +	}
   1.166 +
   1.167 +TInt CAudioDevice::CInputPort::MipIndex() const
   1.168 +	{
   1.169 +	return KInputPortIndex;
   1.170 +	}
   1.171 +	
   1.172 +CMMFBuffer* CAudioDevice::CInputPort::MipCreateBuffer(TInt /*aBufferSize*/)
   1.173 +	{
   1.174 +	__ASSERT_ALWAYS(EFalse, User::Panic(KAudioDevicePanic, EInvalidUsage));
   1.175 +	return NULL;
   1.176 +	}
   1.177 +
   1.178 +TInt CAudioDevice::CInputPort::MipUseBuffer(CMMFBuffer& /*aBuffer*/)
   1.179 +	{
   1.180 +	return KErrNone;
   1.181 +	}
   1.182 +
   1.183 +TInt CAudioDevice::CInputPort::MipFreeBuffer(CMMFBuffer* /*aBuffer*/)
   1.184 +	{
   1.185 +	return KErrNone;
   1.186 +	}
   1.187 +	
   1.188 +void CAudioDevice::CInputPort::MipDisconnectTunnel()
   1.189 +	{
   1.190 +	}
   1.191 +
   1.192 +void CAudioDevice::CInputPort::MipRestartTunnel()
   1.193 +	{
   1.194 +	}
   1.195 +
   1.196 +TUint32 CAudioDevice::CInputPort::MipBufferSize() const
   1.197 +	{
   1.198 +	return iBufferSize;
   1.199 +	}
   1.200 +
   1.201 +void CAudioDevice::CInputPort::Pause()
   1.202 +	{
   1.203 +	if (iParent.SoundDevice().Handle())
   1.204 +		{
   1.205 +		iParent.SoundDevice().PausePlayBuffer();
   1.206 +		}
   1.207 +	}
   1.208 +	
   1.209 +void CAudioDevice::CInputPort::Stop()
   1.210 +	{
   1.211 +	Cancel();	
   1.212 +	}
   1.213 +	
   1.214 +TInt CAudioDevice::CInputPort::MipCreateCustomInterface(TUid aUid)
   1.215 +	{
   1.216 +	if (aUid.iUid == KMmfPlaySettingsCustomInterface)
   1.217 +		{
   1.218 +		return KErrNone;
   1.219 +		}
   1.220 +	return KErrNotSupported;	
   1.221 +	}
   1.222 +
   1.223 +TAny* CAudioDevice::CInputPort::MipCustomInterface(TUid aUid)
   1.224 +	{
   1.225 +	if (aUid.iUid == KMmfPlaySettingsCustomInterface)
   1.226 +		{
   1.227 +		return static_cast<MPlayCustomInterface*>(this);
   1.228 +		}
   1.229 +	return NULL;
   1.230 +	}
   1.231 +	
   1.232 +void CAudioDevice::CInputPort::SetVolume(TUint aVolume)
   1.233 +	{
   1.234 +	iVolume = aVolume;
   1.235 +	}
   1.236 +
   1.237 +TUint CAudioDevice::CInputPort::Volume()
   1.238 +	{
   1.239 +	return iVolume;
   1.240 +	}
   1.241 +
   1.242 +TUint CAudioDevice::CInputPort::BytesPlayed()
   1.243 +	{
   1.244 +	return iBytesPlayed;
   1.245 +	}
   1.246 +
   1.247 +void CAudioDevice::CInputPort::SetVolumeRamp(const TTimeIntervalMicroSeconds& aRampDuration)
   1.248 +	{
   1.249 +	iRampDuration = aRampDuration;
   1.250 +	}
   1.251 +
   1.252 +TTimeIntervalMicroSeconds& CAudioDevice::CInputPort::VolumeRamp()
   1.253 +	{
   1.254 +	return iRampDuration;
   1.255 +	}
   1.256 +
   1.257 +void CAudioDevice::CInputPort::RunL()
   1.258 +	{
   1.259 +	if (iCurrentBuffer != NULL)
   1.260 +		{
   1.261 +		// If we've been signalled with a buffer, callback that we've completed the writing of the
   1.262 +		// buffer
   1.263 +		if (iObserver)
   1.264 +			{		
   1.265 +			iObserver->MipoWriteDataComplete(this, iCurrentBuffer, iStatus.Int());
   1.266 +			if (iCurrentBuffer->LastBuffer())
   1.267 +				{
   1.268 +				iParent.Observer()->ExecuteComplete(&iParent, KErrUnderflow);
   1.269 +				iParent.SoundDevice().Close();
   1.270 +				}
   1.271 +			}
   1.272 +		iCurrentBuffer = NULL;
   1.273 +		}
   1.274 +	// only process the next buffer if there is no error
   1.275 +	// error callbacks were handled in the previous block
   1.276 +	if (iStatus == KErrNone)
   1.277 +		{
   1.278 +		if (iBuffers.Count()>0)
   1.279 +			{
   1.280 +			iCurrentBuffer = iBuffers[0];
   1.281 +			iBuffers.Remove(0);
   1.282 +			
   1.283 +			if (CMMFBuffer::IsSupportedDataBuffer(iCurrentBuffer->Type()))
   1.284 +				{
   1.285 + 				TDes8& aBufferDes = (static_cast<CMMFDataBuffer*>(iCurrentBuffer))->Data();
   1.286 + 				iStatus = KRequestPending;
   1.287 +				iParent.SoundDevice().PlayData(iStatus, aBufferDes);
   1.288 +				SetActive();
   1.289 +				}
   1.290 +			}
   1.291 +		}
   1.292 +	}
   1.293 +
   1.294 +void CAudioDevice::CInputPort::DoCancel()
   1.295 +	{
   1.296 +	if (iParent.SoundDevice().Handle())
   1.297 +		{
   1.298 +		iParent.SoundDevice().CancelPlayData();
   1.299 +		iParent.SoundDevice().FlushPlayBuffer();
   1.300 +		}
   1.301 +	}
   1.302 +	
   1.303 +// CAudioDevice::CInput port class implementation
   1.304 +CAudioDevice::COutputPort::COutputPort(CAudioDevice& aParent) 
   1.305 +	: CActive(EPriorityNormal),
   1.306 +	iParent(aParent),
   1.307 +	iSampleRate(KDefaultSampleRate),
   1.308 +	iChannels(KDefaultNumberChannels),
   1.309 +	iBufferSize(KBufferSize)
   1.310 +	{	
   1.311 +	}
   1.312 +
   1.313 +TInt CAudioDevice::CInputPort::SampleRate()
   1.314 +	{
   1.315 +	return iSampleRate;
   1.316 +	}
   1.317 +	
   1.318 +TInt CAudioDevice::CInputPort::Channels()
   1.319 +	{
   1.320 +	return iChannels;
   1.321 +	}
   1.322 +
   1.323 +void CAudioDevice::CInputPort::MipInitialize()
   1.324 +	{
   1.325 +	// Nothing to do
   1.326 +	}
   1.327 +	
   1.328 +CAudioDevice::COutputPort* CAudioDevice::COutputPort::NewL(CAudioDevice& aParent)
   1.329 +	{
   1.330 +	COutputPort* self = new (ELeave) COutputPort(aParent);
   1.331 +	CleanupStack::PushL(self);
   1.332 +	self->ConstructL();
   1.333 +	CleanupStack::Pop(self);
   1.334 +	return self;
   1.335 +	}
   1.336 +
   1.337 +void CAudioDevice::COutputPort::ConstructL()
   1.338 +	{
   1.339 +	CActiveScheduler::Add(this);	
   1.340 +	}
   1.341 +
   1.342 +TInt CAudioDevice::COutputPort::MopConfigure(const TPuConfig& aConfig)
   1.343 +	{
   1.344 +	if (aConfig.Uid() == TUid::Uid(KUidTTaskConfig))
   1.345 +		{
   1.346 +		const TTaskConfig* config = TPuTaskConfig::GetStructure(aConfig);	
   1.347 +	
   1.348 +		iSampleRate = config->iRate;
   1.349 +		iChannels = (config->iStereoMode & ETaskMono)? 1 : 2;
   1.350 +		iInterleaved = (config->iStereoMode & ETaskInterleaved)?ETrue : EFalse;
   1.351 +		return KErrNone;
   1.352 +		}
   1.353 +		
   1.354 +	if (aConfig.Uid() == TUid::Uid(KUidTTaskConfig2))
   1.355 +		{
   1.356 +		const TTaskConfig2* config = TPuTaskConfig2::GetStructure(aConfig);	
   1.357 +	
   1.358 +		iSampleRate = config->iRate;
   1.359 +		iChannels = config->iNumberOfChannels;
   1.360 +		iInterleaved = (config->iStereoMode & ETaskInterleaved)?ETrue : EFalse;
   1.361 +		return KErrNone;
   1.362 +		}	
   1.363 +	return KErrNotSupported;
   1.364 +	}
   1.365 +	
   1.366 +TInt CAudioDevice::COutputPort::MopGetConfig(TPuConfig& /*aConfig*/)
   1.367 +	{
   1.368 +	return KErrNotSupported;
   1.369 +	}
   1.370 +
   1.371 +void CAudioDevice::COutputPort::MopInitialize()
   1.372 +	{
   1.373 +	// Nothing to do
   1.374 +	}
   1.375 +
   1.376 +TInt CAudioDevice::COutputPort::MopTunnelRequest(const MMdfInputPort& /*aInputPortToBeConnectedTo*/,
   1.377 +	TTunnelFlags& /*aTunnelFlags*/, TSupplierType& /*aSupplierType*/)
   1.378 +	{
   1.379 +	return KErrNone;
   1.380 +	}
   1.381 +
   1.382 +void CAudioDevice::COutputPort::MopReadData(CMMFBuffer& aOutputBuffer)
   1.383 +	{
   1.384 +	TInt err = iBuffers.Append(&aOutputBuffer);
   1.385 +	if (err == KErrNone)
   1.386 +		{
   1.387 +		if ((iParent.State() == EProcessingUnitExecuting || iParent.State() == EProcessingUnitPaused) && !IsActive())
   1.388 +			{
   1.389 +			SetActive();
   1.390 +			TRequestStatus* status = &iStatus;
   1.391 +			User::RequestComplete(status, KErrNone);	
   1.392 +			}		
   1.393 +		}
   1.394 +	else
   1.395 +		{
   1.396 +		if (iObserver)
   1.397 +			{
   1.398 +			iObserver->MopoReadDataComplete(this, &aOutputBuffer, err);
   1.399 +			}
   1.400 +		}
   1.401 +	}
   1.402 +
   1.403 +TBool CAudioDevice::COutputPort::MopIsTunnelled() const
   1.404 +	{
   1.405 +	return EFalse;	
   1.406 +	}
   1.407 +
   1.408 +TInt CAudioDevice::COutputPort::MopIndex() const
   1.409 +	{
   1.410 +	return KOutputPortIndex;
   1.411 +	}
   1.412 +	
   1.413 +CMMFBuffer* CAudioDevice::COutputPort::MopCreateBuffer(TInt /*aBufferSize*/)
   1.414 +	{
   1.415 +	return NULL;
   1.416 +	}
   1.417 +	
   1.418 +TInt CAudioDevice::COutputPort::MopUseBuffer(CMMFBuffer& /*aBuffer*/) 
   1.419 +	{
   1.420 +	return KErrNone;
   1.421 +	}
   1.422 +
   1.423 +TInt CAudioDevice::COutputPort::MopFreeBuffer(CMMFBuffer* /*aBuffer*/) 
   1.424 +	{
   1.425 +	return KErrNone;
   1.426 +	}
   1.427 +
   1.428 +void CAudioDevice::COutputPort::Execute()
   1.429 +	{
   1.430 +	if (!IsActive() && iBuffers.Count()>0)
   1.431 +		{
   1.432 +		SetActive();
   1.433 +		TRequestStatus* status = &iStatus;
   1.434 +		User::RequestComplete(status, KErrNone);
   1.435 +		}
   1.436 +	}
   1.437 +	
   1.438 +void CAudioDevice::COutputPort::Pause()
   1.439 +	{
   1.440 +	if(iParent.SoundDevice().Handle())
   1.441 +		{
   1.442 +		iParent.SoundDevice().FlushRecordBuffer();
   1.443 +		}	
   1.444 +	}
   1.445 +	
   1.446 +void CAudioDevice::COutputPort::Stop()
   1.447 +	{
   1.448 +	Cancel();
   1.449 +	}
   1.450 +
   1.451 +void CAudioDevice::COutputPort::MopDisconnectTunnel()
   1.452 +	{
   1.453 +	// Find the last buffer in the array and set it as the 'LastBuffer'
   1.454 +	if(iBuffers.Count() > 0)
   1.455 +		{
   1.456 +		(iBuffers[iBuffers.Count() - 1])->SetLastBuffer(ETrue);		
   1.457 +		}
   1.458 +	// Still have to send callback and cancel driver
   1.459 +	iObserver->MopoDisconnectTunnelComplete(this, KErrNone);
   1.460 +	iParent.SoundDevice().CancelRecordData();
   1.461 +	}
   1.462 +	
   1.463 +void CAudioDevice::COutputPort::MopRestartTunnel()
   1.464 +	{
   1.465 +	}
   1.466 +	
   1.467 +TUint32 CAudioDevice::COutputPort::MopBufferSize() const
   1.468 +	{
   1.469 +	return iBufferSize;
   1.470 +	}
   1.471 +	
   1.472 +void CAudioDevice::CInputPort::MipSetObserver(const MMdfInputPortObserver& aObserver)
   1.473 +	{
   1.474 +	iObserver = const_cast<MMdfInputPortObserver*>(&aObserver);
   1.475 +	}
   1.476 +
   1.477 +void CAudioDevice::COutputPort::MopSetObserver(const MMdfOutputPortObserver& aObserver)
   1.478 +	{
   1.479 +	iObserver = const_cast<MMdfOutputPortObserver*>(&aObserver);
   1.480 +	}
   1.481 +
   1.482 +TInt CAudioDevice::COutputPort::MopCreateCustomInterface(TUid aUid)
   1.483 +	{
   1.484 +	if (aUid.iUid == KMmfRecordSettingsCustomInterface)
   1.485 +		{
   1.486 +		return KErrNone;
   1.487 +		}
   1.488 +	return KErrNotSupported;	
   1.489 +	}
   1.490 +	
   1.491 +TAny* CAudioDevice::COutputPort::MopCustomInterface(TUid aUid)
   1.492 +	{
   1.493 +	if (aUid.iUid == KMmfRecordSettingsCustomInterface)
   1.494 +		{
   1.495 +		return dynamic_cast<MRecordCustomInterface*>(this);
   1.496 +		}
   1.497 +
   1.498 +	return NULL;
   1.499 +	}
   1.500 +	
   1.501 +void CAudioDevice::COutputPort::RunL()
   1.502 +	{
   1.503 +	if (iCurrentBuffer != NULL)
   1.504 +		{
   1.505 +		// If we've been signalled with a buffer, callback that we've completed the writing of the
   1.506 +		// buffer
   1.507 +		if (iObserver)
   1.508 +			{		
   1.509 +			TInt length = iCurrentBuffer->BufferSize();
   1.510 +			iBytesRecorded += length;
   1.511 +			if (length<iBufferSize)
   1.512 +				{
   1.513 +				iCurrentBuffer->SetLastBuffer(ETrue);
   1.514 +				iParent.SoundDevice().CancelRecordData(); // Required so that a resume of an encode functions correctly
   1.515 +				}
   1.516 +					
   1.517 +			iObserver->MopoReadDataComplete(this, iCurrentBuffer, iStatus.Int());
   1.518 +			
   1.519 +			}
   1.520 +			iCurrentBuffer = NULL;
   1.521 +		}
   1.522 +		
   1.523 +	if (iStatus == KErrNone)
   1.524 +		{
   1.525 +		if (iBuffers.Count()>0)
   1.526 +			{
   1.527 +			iCurrentBuffer = iBuffers[0];
   1.528 +			iBuffers.Remove(0);
   1.529 +			
   1.530 +			if (CMMFBuffer::IsSupportedDataBuffer(iCurrentBuffer->Type()))
   1.531 +				{
   1.532 +				TDes8& aBufferDes = (static_cast<CMMFDataBuffer*>(iCurrentBuffer))->Data();
   1.533 +				iStatus = KRequestPending;
   1.534 +				iParent.SoundDevice().RecordData(iStatus, aBufferDes);
   1.535 +				SetActive();
   1.536 +				}
   1.537 +			}
   1.538 +		}
   1.539 +	}
   1.540 +
   1.541 +void CAudioDevice::COutputPort::DoCancel()
   1.542 +	{
   1.543 +	if (iParent.SoundDevice().Handle())
   1.544 +		{
   1.545 +		iParent.SoundDevice().CancelRecordData();
   1.546 +		iParent.SoundDevice().FlushRecordBuffer();
   1.547 +		}		
   1.548 +	}
   1.549 +	
   1.550 +TInt CAudioDevice::COutputPort::SampleRate()
   1.551 +	{
   1.552 +	return iSampleRate;
   1.553 +	}
   1.554 +	
   1.555 +TInt CAudioDevice::COutputPort::Channels()
   1.556 +	{
   1.557 +	return iChannels;
   1.558 +	}
   1.559 +
   1.560 +CAudioDevice::COutputPort::~COutputPort()
   1.561 +	{
   1.562 +	Cancel();
   1.563 +	iBuffers.Close();
   1.564 +	}
   1.565 +	
   1.566 +// from MRecordCustomInterface
   1.567 +void CAudioDevice::COutputPort::SetGain(TUint aGain)
   1.568 +	{
   1.569 +	iGain = aGain;
   1.570 +	}
   1.571 +TUint CAudioDevice::COutputPort::Gain()
   1.572 +	{
   1.573 +	return iGain;
   1.574 +	}
   1.575 +	
   1.576 +TUint CAudioDevice::COutputPort::BytesRecorded()
   1.577 +	{
   1.578 +	return iBytesRecorded;
   1.579 +	}
   1.580 +
   1.581 +// ------------------------------------------------------------------------------------------
   1.582 +// CAudioDevice Implementation
   1.583 +
   1.584 +
   1.585 +CAudioDevice::CAudioDevice()
   1.586 +	{
   1.587 +	}
   1.588 +	
   1.589 +CAudioDevice::~CAudioDevice()
   1.590 +	{
   1.591 +	delete iInputPort;
   1.592 +	delete iOutputPort;
   1.593 +	iSoundDevice.Close();
   1.594 +	}
   1.595 +
   1.596 +void CAudioDevice::Execute()
   1.597 +	{
   1.598 +	__ASSERT_ALWAYS(iObserver, User::Panic(KAudioDevicePanic, EObserverNotSet));
   1.599 +	TInt err = KErrNone;
   1.600 +	if(!iSoundDevice.Handle())
   1.601 +		{
   1.602 +		err = iSoundDevice.Open();
   1.603 +		}
   1.604 +	
   1.605 +	RMdaDevSound::TCurrentSoundFormatBuf buf;
   1.606 +	if (err == KErrNone)
   1.607 +		{
   1.608 +		if(iState == EProcessingUnitPaused)
   1.609 +			{
   1.610 +			iSoundDevice.ResumePlaying();
   1.611 +			}
   1.612 +		else
   1.613 +			{
   1.614 +			// Set play format (for input port)
   1.615 +			iSoundDevice.GetPlayFormat(buf);
   1.616 +			buf().iRate = iInputPort->SampleRate();
   1.617 +			buf().iChannels = iInputPort->Channels();
   1.618 +			buf().iBufferSize = KBufferSize;
   1.619 +			buf().iEncoding = RMdaDevSound::EMdaSoundEncoding16BitPCM;
   1.620 +			err = iSoundDevice.SetPlayFormat(buf);
   1.621 +			}
   1.622 +		}
   1.623 +	iState = EProcessingUnitExecuting;	
   1.624 +	if (err == KErrNone)
   1.625 +		{
   1.626 +		// Set record format (for output format)
   1.627 +		iSoundDevice.GetRecordFormat(buf);
   1.628 +		buf().iRate = iOutputPort->SampleRate();
   1.629 +		buf().iChannels = iOutputPort->Channels();
   1.630 +		buf().iBufferSize = KBufferSize;
   1.631 +		buf().iEncoding = RMdaDevSound::EMdaSoundEncoding16BitPCM;
   1.632 +		iSoundDevice.SetRecordFormat(buf);
   1.633 +		iInputPort->Execute();
   1.634 +		iOutputPort->Execute();
   1.635 +		}
   1.636 +	else 
   1.637 +		{
   1.638 +		iObserver->ExecuteComplete(this, err);
   1.639 +		}		
   1.640 +	}
   1.641 +
   1.642 +TInt CAudioDevice::Pause()
   1.643 +	{
   1.644 +	iState = EProcessingUnitPaused;
   1.645 +	iInputPort->Pause();
   1.646 +	iOutputPort->Pause();
   1.647 +	return KErrNone;
   1.648 +	}
   1.649 +
   1.650 +void CAudioDevice::Stop()
   1.651 +	{
   1.652 +	if(iState == EProcessingUnitExecuting || iState == EProcessingUnitPaused)
   1.653 +		{
   1.654 +		// Cancel and flush the device driver		
   1.655 +		iInputPort->Stop();
   1.656 +		iOutputPort->Stop();
   1.657 +		iState = EProcessingUnitIdle;
   1.658 +		
   1.659 +		// Close the sound device
   1.660 +		iSoundDevice.Close();	
   1.661 +		}		
   1.662 +	}
   1.663 +	
   1.664 +CAudioDevice* CAudioDevice::NewL()
   1.665 +	{
   1.666 +	CAudioDevice* self = new (ELeave) CAudioDevice;
   1.667 +	CleanupStack::PushL(self);
   1.668 +	self->ConstructL();
   1.669 +	CleanupStack::Pop(self);
   1.670 +	return self;
   1.671 +	}
   1.672 +	
   1.673 +TInt CAudioDevice::Create(const MMdfProcessingUnitObserver& aObserver)
   1.674 +	{
   1.675 +	iObserver = const_cast<MMdfProcessingUnitObserver*>(&aObserver);
   1.676 +	return KErrNone;
   1.677 +	}
   1.678 +	
   1.679 +TInt CAudioDevice::Configure(const TPuConfig& /*aConfigurationSetup*/)
   1.680 +	{
   1.681 +	return KErrNotSupported;
   1.682 +	}
   1.683 +	
   1.684 +TInt CAudioDevice::GetConfig(TPuConfig& /*aConfigurationSetup*/)
   1.685 +	{
   1.686 +	return KErrNotSupported;
   1.687 +	}
   1.688 +
   1.689 +TInt CAudioDevice::GetInputPorts(RPointerArray<MMdfInputPort>& aComponentInputPorts )
   1.690 +	{
   1.691 +	return aComponentInputPorts.Append(iInputPort);
   1.692 +	}
   1.693 +
   1.694 +TInt CAudioDevice::GetOutputPorts(RPointerArray<MMdfOutputPort>& aComponentOutputPorts )
   1.695 +	{
   1.696 +	return aComponentOutputPorts.Append(iOutputPort);
   1.697 +	}
   1.698 +
   1.699 +void CAudioDevice::ConstructL()
   1.700 +	{
   1.701 +	iInputPort = CInputPort::NewL(*this);
   1.702 +	iOutputPort = COutputPort::NewL(*this);
   1.703 +	iState = EProcessingUnitLoaded;
   1.704 +	}
   1.705 +
   1.706 +void CAudioDevice::Initialize()
   1.707 +	{
   1.708 +	__ASSERT_ALWAYS(iObserver, User::Panic(KAudioDevicePanic, EObserverNotSet));
   1.709 +	
   1.710 +	iObserver->InitializeComplete(this, KErrNone);
   1.711 +	iState = EProcessingUnitIdle;
   1.712 +	}
   1.713 +
   1.714 +MMdfProcessingUnitObserver* CAudioDevice::Observer()
   1.715 +	{
   1.716 +	return iObserver;	
   1.717 +	}
   1.718 +		
   1.719 +TProcessingUnitState CAudioDevice::State()
   1.720 +	{
   1.721 +	return iState;	
   1.722 +	}
   1.723 +		
   1.724 +RMdaDevSound& CAudioDevice::SoundDevice()
   1.725 +	{
   1.726 +	return iSoundDevice;
   1.727 +	}
   1.728 +	
   1.729 +TAny* CAudioDevice::CustomInterface(TUid /*aUid*/)
   1.730 +	{
   1.731 +	return NULL;
   1.732 +	}
   1.733 +		
   1.734 +TInt CAudioDevice::CreateCustomInterface(TUid /*aUid*/)
   1.735 +	{
   1.736 +	return KErrNotSupported;
   1.737 +	}
   1.738 +
   1.739 +// ------------------------------------------------------------------------------------------	
   1.740 +// ECOM Implementation table entry	
   1.741 +	
   1.742 +const TImplementationProxy ImplementationTable[] = 
   1.743 +	{
   1.744 +	IMPLEMENTATION_PROXY_ENTRY(KUidPUAudioDevice,	CAudioDevice::NewL),
   1.745 +	};
   1.746 +
   1.747 +EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
   1.748 +	{
   1.749 +	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
   1.750 +	return ImplementationTable;
   1.751 +	}
   1.752 +