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 "MMFpcm16ToPcmU8HwDevice.h"
sl@0: 
sl@0: /**
sl@0:  *
sl@0:  *	Returns the created hw device for signed pcm16 to unsigned pcm8 audio.
sl@0:  *
sl@0:  *	@return	"CMMFPcm16ToPcmU8HwDevice"
sl@0:  *
sl@0:  */
sl@0: CMMFPcm16ToPcmU8HwDevice* CMMFPcm16ToPcmU8HwDevice::NewL()
sl@0: 	{
sl@0:     CMMFPcm16ToPcmU8HwDevice* self = new (ELeave) CMMFPcm16ToPcmU8HwDevice();
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 CMMFPcm16ToPcmU8HwDevice::ConstructL()
sl@0: 	{
sl@0:     iCodec = new (ELeave) CMMFPcm16ToPcmU8Codec();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * ~CMMFPcm16ToPcmU8HwDevice
sl@0: */
sl@0: CMMFPcm16ToPcmU8HwDevice::~CMMFPcm16ToPcmU8HwDevice()
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& CMMFPcm16ToPcmU8HwDevice::Codec()
sl@0: 	{
sl@0: 	return *iCodec;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * ProcessL
sl@0: * @param aSrc 
sl@0: * @param aDst
sl@0: * @return 'CMMFSwCodec::TCodecProcessResult'
sl@0: *
sl@0: */
sl@0: CMMFSwCodec::TCodecProcessResult CMMFPcm16ToPcmU8Codec::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: 	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: 	
sl@0: 	TInt noSamples = srcUse /2;
sl@0: 	iAudioS16ToU8Pcm.Convert(pSrc, pDst, noSamples );
sl@0: 	
sl@0: 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
sl@0: 	result.iSrcBytesProcessed  = srcUse;
sl@0: 	result.iDstBytesAdded      = srcUse / 2;
sl@0: 	
sl@0: 	dst->Data().SetLength(result.iDstBytesAdded);
sl@0: 	
sl@0: 	//[ post conditions
sl@0:     // srcbytes/2 == destbytes added
sl@0: 	// pos src == 0
sl@0: 	// pos dest == 0 ]
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()/2 == dst->Data().Length(), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
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 CMMFPcm16ToPcmU8Codec::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: 	}