sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include "MMFpcmU8ToPcm16HwDevice.h"
sl@0: 
sl@0: /**
sl@0:  *
sl@0:  *	Returns the created hw device for unsigned pcm8 to signed pcm16 audio.
sl@0:  *
sl@0:  *	@return	"CMMFPcmU8ToPcm16HwDevice"
sl@0:  *
sl@0:  */
sl@0: CMMFPcmU8ToPcm16HwDevice* CMMFPcmU8ToPcm16HwDevice::NewL()
sl@0: 	{
sl@0:     CMMFPcmU8ToPcm16HwDevice* self = new (ELeave) CMMFPcmU8ToPcm16HwDevice();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL();
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  *
sl@0:  *	Second phase constructor.
sl@0:  *
sl@0:  */
sl@0: void CMMFPcmU8ToPcm16HwDevice::ConstructL()
sl@0: 	{
sl@0:     iCodec = new (ELeave) CMMFPcmU8ToPcm16Codec();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * ~CMMFPcmU8ToPcm16HwDevice
sl@0: *
sl@0: */
sl@0: CMMFPcmU8ToPcm16HwDevice::~CMMFPcmU8ToPcm16HwDevice()
sl@0: 	{
sl@0: 	//The codec is deleted in the base class CMMFSwCodecWrapper
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * Codec
sl@0: * @return 'CMMFSwCodec&'
sl@0: *
sl@0: */
sl@0: CMMFSwCodec& CMMFPcmU8ToPcm16HwDevice::Codec()
sl@0: 	{
sl@0: 	return *iCodec;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * ProcessL
sl@0: * @param aSource
sl@0: * @param aDst 
sl@0: * @return 'CMMFSwCodec::TCodecProcessResult'
sl@0: *
sl@0: */
sl@0: CMMFSwCodec::TCodecProcessResult CMMFPcmU8ToPcm16Codec::ProcessL(const CMMFBuffer& aSource, CMMFBuffer& aDest)
sl@0: 	{
sl@0: 	TCodecProcessResult result;
sl@0: 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
sl@0: 	
sl@0: 	//convert from generic CMMFBuffer to CMMFDataBuffer
sl@0: 	const CMMFDataBuffer* src = STATIC_CAST(const CMMFDataBuffer*, &aSource);
sl@0: 	CMMFDataBuffer* dst = STATIC_CAST(CMMFDataBuffer*, &aDest);
sl@0: 	
sl@0: 	//[ check preconditions ]
sl@0: 	if( !CheckPreconditions( src, dst ))
sl@0: 		{
sl@0: 		//[ precondition violation ]
sl@0: 		User::Leave( KErrArgument );
sl@0: 		}
sl@0: 	
sl@0: 	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
sl@0: 	TUint8* pSrc = CONST_CAST(TUint8*,src->Data().Ptr());
sl@0: 	TUint8* pDst = CONST_CAST(TUint8*,dst->Data().Ptr());
sl@0: 	
sl@0: 	TInt srcUse = src->Data().Length();
sl@0: 	TInt noSamples = srcUse;
sl@0: 	iAudioU8ToS16Pcm.Convert(pSrc, pDst, noSamples );
sl@0: 	
sl@0: 	TInt dstBytesAdded = srcUse * 2;
sl@0: 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
sl@0: 	result.iSrcBytesProcessed  = srcUse;
sl@0: 	result.iDstBytesAdded      = dstBytesAdded;
sl@0: 	
sl@0: 	dst->Data().SetLength(dstBytesAdded);
sl@0: 	
sl@0: 	//[ check post conditions ]
sl@0: 	__ASSERT_DEBUG( (src->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
sl@0: 	__ASSERT_DEBUG( (dst->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
sl@0: 	__ASSERT_DEBUG( src->Data().Length() == dst->Data().Length()/2, TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation )); // 2 here signifies sizeof TInt16
sl@0: 	__ASSERT_DEBUG( dst->Data().Length() % 2 == 0, TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation )); // pcm output
sl@0: 	
sl@0: 	return result;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * Preconditions
sl@0: * This methos tests the preconditions of the ProcessL method
sl@0: * @return TBool ETrue for sucess and EFalse for failure of the preconditions
sl@0: *
sl@0: **/
sl@0: TBool CMMFPcmU8ToPcm16Codec::CheckPreconditions( const CMMFDataBuffer* aSrcBuffer, CMMFDataBuffer* aDestBuffer )
sl@0: 	{
sl@0: 	TBool result = EFalse;
sl@0: 	
sl@0: 	if(! aSrcBuffer )
sl@0: 		{
sl@0: 		return result;
sl@0: 		}
sl@0: 	
sl@0: 	if( ! aDestBuffer )
sl@0: 		{
sl@0: 		return result;
sl@0: 		}
sl@0: 	
sl@0: 	// Check position of src and dest are 0
sl@0: 	if( aSrcBuffer->Position() )
sl@0: 		{
sl@0: 		return result;
sl@0: 		}
sl@0: 	
sl@0: 	// Check position of src and dest are 0
sl@0: 	if( aDestBuffer->Position() )
sl@0: 		{
sl@0: 		return result;
sl@0: 		}
sl@0: 	
sl@0: 	// check there are sufficient bytes in the output to consume the input
sl@0: 	if( aSrcBuffer->Data().Length() * 2 > aDestBuffer->Data().MaxLength() )
sl@0: 		{
sl@0: 		return result;
sl@0: 		}
sl@0: 	
sl@0: 	result = ETrue;  // preconditions have been satisfied
sl@0: 	
sl@0: 	return result;
sl@0: 	}