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 "MmfPcm16toAlawhwDevice.h"
sl@0: 
sl@0: /**
sl@0: *
sl@0: * NewL
sl@0: *
sl@0: */
sl@0: CMMFPcm16ToAlawHwDevice* CMMFPcm16ToAlawHwDevice::NewL()
sl@0: 	{
sl@0: 	CMMFPcm16ToAlawHwDevice* self=new(ELeave) CMMFPcm16ToAlawHwDevice();
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: * ~CMMFPcm16ToAlawHwDevice
sl@0: *
sl@0: */
sl@0: CMMFPcm16ToAlawHwDevice::~CMMFPcm16ToAlawHwDevice()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: *
sl@0: * ConstructL
sl@0: *
sl@0: */
sl@0: void CMMFPcm16ToAlawHwDevice::ConstructL()
sl@0: 	{
sl@0: 	iCodec = new (ELeave) CMMFPcm16ToALawCodec();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: *
sl@0: * Codec
sl@0: *
sl@0: */
sl@0: CMMFSwCodec &CMMFPcm16ToAlawHwDevice::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: * @pre position of buffer aSrc is 0
sl@0: * @pre position of buffer aDst is 0
sl@0: * @pre sufficient bytes in output to consume input
sl@0: * @return TCodecProcessResult
sl@0: *
sl@0: */
sl@0: CMMFSwCodec::TCodecProcessResult CMMFPcm16ToALawCodec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
sl@0: 	{
sl@0: 	CMMFSwCodec::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*, &aSrc);
sl@0: 	CMMFDataBuffer* dst = STATIC_CAST(CMMFDataBuffer*, &aDst);
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: 	pSrc += src->Position();
sl@0: 	TUint8* pDst = CONST_CAST(TUint8*,dst->Data().Ptr());
sl@0: 	pDst += dst->Position();
sl@0: 	
sl@0: 	TUint destUse = src->Data().Length()/2;
sl@0: 	
sl@0: 	//compress TWO bytes (16-bit PCM word) into a to 1 byte sample
sl@0: 	iPcm16ToALaw.Convert(pSrc, pDst, destUse );
sl@0: 	
sl@0: 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
sl@0: 	result.iSrcBytesProcessed  = src->Data().Length();
sl@0: 	result.iDstBytesAdded      = destUse;
sl@0: 	
sl@0: 	dst->Data().SetLength(result.iDstBytesAdded);
sl@0: 	
sl@0: 	//[ post condition evaluation here ]
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 CMMFPcm16ToALawCodec::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: 		( aSrcBuffer->Data().Length() % 2 != 0 ))
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: 	}