os/mm/devsound/sounddevbt/src/Plugin/HwDevice/Audio/MmfBtPcmS16ToPcmS8HwDevice.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "MmfBtPcmS16ToPcmS8HwDevice.h"
    17 #include "../../MmfBtFileDependencyUtil.h"
    18 
    19 /**
    20  *
    21  *	Returns the created hw device for passing audio through audio.
    22  *  for the wins implementation this would always be pcm16 although
    23  *  this is effectively a null hw device that will pass any datatype through
    24  *	@return	"CMMFPcm16ToPcmU16BEHwDevice"
    25  *
    26  */
    27 CMMFPcmS16ToPcmS8HwDevice* CMMFPcmS16ToPcmS8HwDevice::NewL()
    28 	{
    29     CMMFPcmS16ToPcmS8HwDevice* self = new (ELeave) CMMFPcmS16ToPcmS8HwDevice();
    30 	CleanupStack::PushL(self);
    31 	self->ConstructL();
    32 	CleanupStack::Pop(self);
    33 	return self;
    34 	}
    35 
    36 /**
    37 *
    38 * ~CMMFPcmS16ToPcmS8HwDevice
    39 */
    40 CMMFPcmS16ToPcmS8HwDevice::~CMMFPcmS16ToPcmS8HwDevice()
    41 	{
    42 	}
    43 
    44 /**
    45 *
    46 * ConstructL
    47 *
    48 */
    49 void CMMFPcmS16ToPcmS8HwDevice::ConstructL()
    50 	{
    51 	iCodec = new (ELeave) CMMFPcmS16ToPcmS8Codec();
    52 	}
    53 
    54 /**
    55 *
    56 * Codec
    57 * @return 'CMMFSwCodec&'
    58 *
    59 */
    60 CMMFSwCodec& CMMFPcmS16ToPcmS8HwDevice::Codec()
    61 	{
    62 	return *iCodec;
    63 	}
    64 
    65 /**
    66 *
    67 * ProcessL
    68 * @param aSrc
    69 * @param aDst
    70 * @param aSamples
    71 *
    72 */
    73 CMMFSwCodec::TCodecProcessResult CMMFPcmS16ToPcmS8Codec::ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst)
    74 	{
    75 	TCodecProcessResult result;
    76 	result.iCodecProcessStatus = 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
    77 	
    78 	//convert from generic CMMFBuffer to CMMFDataBuffer
    79 	const CMMFDataBuffer* src = STATIC_CAST(const CMMFDataBuffer*, &aSrc);
    80 	CMMFDataBuffer* dst = STATIC_CAST(CMMFDataBuffer*, &aDst);
    81 	
    82 	//[Check Preconditions]
    83 	if( !CheckPreconditions( src, dst ))
    84 		{
    85 		//[ precondition violation ]
    86 		User::Leave( KErrArgument );
    87 		}
    88 	
    89 	//we need to cast away CONST even on the source, as the TClass needs a TUint8*
    90 	TUint8* pSrc = CONST_CAST(TUint8*,src->Data().Ptr());
    91 	TUint8* pDst = CONST_CAST(TUint8*,dst->Data().Ptr());
    92     
    93 	TInt srcUse = src->Data().Length();
    94 	TInt noSamples = srcUse/2;
    95 	
    96 	//compress TWO bytes (16-bit PCM word) into a to 1 byte sample
    97 	iAudioPcmS16ToPcmS8.Convert(pSrc, pDst, noSamples );
    98 	
    99 	result.iCodecProcessStatus = TCodecProcessResult::EProcessComplete;
   100 	result.iSrcBytesProcessed  = srcUse;
   101 	result.iDstBytesAdded      = srcUse/2;
   102 	
   103 	dst->Data().SetLength(result.iDstBytesAdded);
   104 	
   105 	//[ post conditions
   106     // srcbytes/2 == destbytes added
   107 	// pos src == 0
   108 	// pos dest == 0 ]
   109 	__ASSERT_DEBUG( (src->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
   110 	__ASSERT_DEBUG( (dst->Position() == 0), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
   111 	__ASSERT_DEBUG( src->Data().Length()/2 == dst->Data().Length(), TMmfAudioCodecPanicsNameSpace::Panic( 	TMmfAudioCodecPanicsNameSpace::EPostConditionViolation ));
   112 	
   113 	return result;
   114 	}
   115 
   116 /**
   117 *
   118 * Preconditions
   119 * This methos tests the preconditions of the ProcessL method
   120 * @return TBool ETrue for sucess and EFalse for failure of the preconditions
   121 *
   122 **/
   123 TBool CMMFPcmS16ToPcmS8Codec::CheckPreconditions( const CMMFDataBuffer* aSrcBuffer, CMMFDataBuffer* aDestBuffer )
   124 	{
   125 	TBool result = EFalse;
   126 	
   127 	if(! aSrcBuffer )
   128 		{
   129 		return result;
   130 		}
   131 	
   132 	if( ! aDestBuffer )
   133 		{
   134 		return result;
   135 		}
   136 	
   137 	// Check position of src and dest are 0
   138 	if( aSrcBuffer->Position() )
   139 		{
   140 		return result;
   141 		}
   142 	
   143 	// Check position of src and dest are 0
   144 	if( aDestBuffer->Position() )
   145 		{
   146 		return result;
   147 		}
   148 	
   149 	// check there are sufficient bytes in the output to consume the input
   150 	if( ( aSrcBuffer->Data().Length()/2 > aDestBuffer->Data().MaxLength() ) &&
   151 		( aSrcBuffer->Data().Length() % 2 == 0 ) ) // must have pcm16 samples on input
   152 		{
   153 		return result;
   154 		}
   155 	
   156 	result = ETrue;  // preconditions have been satisfied
   157 	
   158 	return result;
   159 	}
   160