devsound/sounddevbt/src/SoundDevice/BtDevSoundUtility.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2001-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 // MMF\SoundDev\src\SoundDevice\BtDevSoundUtility.cpp
       
    15 // File:		BtDevSoundUtility.cpp
       
    16 // Author:		Vasudevan Ramachandraiah
       
    17 // Date:		July 16, 2002
       
    18 // Class that provides API to list ECOM plugin implementation IDs
       
    19 // (c) Nokia Inc.
       
    20 // Revisions:
       
    21 // Date:			Author			Description
       
    22 // 
       
    23 //
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <barsc.h>
       
    27 #include <barsread.h>
       
    28 #include <mmfbase.h>
       
    29 #include <mmfplugininterfaceuids.hrh>
       
    30 #include <mmf/plugin/mmfbthwdeviceimplementationuids.hrh>
       
    31 #include <fixedsequence.rsg>
       
    32 #include "BtDevSoundUtility.h"
       
    33 #include <mmf/common/mmfcontroller.h> //needed for CleanupResetAndDestroyPushL()
       
    34 
       
    35 _LIT(KFixedSequenceResourceFile, "Z:\\Resource\\DevSound\\FixedSequence.rsc"); 
       
    36 
       
    37 inline TMMFRawPackage::TMMFRawPackage(TInt aDerivedSize)
       
    38 #pragma warning( disable : 4355 )	// 'this' : used in base member initializer list
       
    39 : iThis((TUint8*)this,aDerivedSize,aDerivedSize) 
       
    40 #pragma warning( default : 4355 )
       
    41 	{ 
       
    42 	}
       
    43 
       
    44 inline TPtr8& TMMFRawPackage::Package()
       
    45 	{ 
       
    46 	((TMMFRawPackage*)this)->iThis.Set((TUint8*)this,iThis.Length(),iThis.MaxLength()); 
       
    47 	return iThis; 
       
    48 	}
       
    49 
       
    50 inline const TPtr8& TMMFRawPackage::Package() const
       
    51 	{ 
       
    52 	((TMMFRawPackage*)this)->iThis.Set((TUint8*)this,iThis.Length(),iThis.MaxLength()); 
       
    53 	return iThis; 
       
    54 	}
       
    55 
       
    56 inline void TMMFRawPackage::SetSize(TInt aDerivedSize)
       
    57 	{ 
       
    58 	iThis.Set((TUint8*)this,aDerivedSize,aDerivedSize);
       
    59 	}
       
    60 
       
    61 inline TMMFToneFixedSequenceNames::TMMFToneFixedSequenceNames() :
       
    62 TMMFRawPackage(sizeof(TMMFToneFixedSequenceNames)) {}
       
    63 
       
    64 #ifdef _UNICODE
       
    65 class TNameBuf : public TBufCBase16
       
    66 #else
       
    67 class TNameBuf : public TBufCBase8
       
    68 #endif
       
    69 	{
       
    70 	friend class HMMFToneFixedSequenceNames;
       
    71 	};
       
    72 
       
    73 HMMFToneFixedSequenceNames::HMMFToneFixedSequenceNames()
       
    74 	{
       
    75 	iCount = 0;
       
    76 	}
       
    77 
       
    78 HMMFToneFixedSequenceNames* HMMFToneFixedSequenceNames::AddNameL(const TDesC& aName)
       
    79 // Append a copy of the supplied descriptor to the end of the 
       
    80 // current heap cell. This will involve a realloc that will normally
       
    81 // result in the object moving
       
    82 	{
       
    83 	TInt size = Package().Length();
       
    84 	TInt desSize = aName.Size() + sizeof(TInt);
       
    85 	if (desSize&3) 
       
    86 		desSize = ((desSize+4)&(~3)); // Must round up to word boundary to keep aligned
       
    87 	HMMFToneFixedSequenceNames* self = REINTERPRET_CAST(HMMFToneFixedSequenceNames*,User::ReAllocL(STATIC_CAST(TAny*,this),size + desSize));
       
    88 	TUint8* newDesPtr = REINTERPRET_CAST(TUint8*,self) + size;
       
    89 	Mem::FillZ(newDesPtr,desSize);
       
    90 	TNameBuf* newDes = REINTERPRET_CAST(TNameBuf*,newDesPtr);
       
    91 	newDes->Copy(aName,aName.Length());
       
    92 	self->SetSize(size+desSize);
       
    93 	self->iCount++;
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 
       
    98 /******************************************************************
       
    99  *				CMMFDevSoundUtility
       
   100  ******************************************************************/
       
   101 CMMFDevSoundUtility::CMMFDevSoundUtility()
       
   102 	{
       
   103 	// No default implementation
       
   104 	}
       
   105 
       
   106 
       
   107 CMMFDevSoundUtility::~CMMFDevSoundUtility()
       
   108 	{
       
   109 	delete iInfo;
       
   110 	delete iFixedSequenceNames;
       
   111 	}
       
   112 
       
   113 CMMFDevSoundUtility* CMMFDevSoundUtility::NewL()
       
   114 	{
       
   115 	CMMFDevSoundUtility* self = NewLC();
       
   116 	CleanupStack::Pop();
       
   117 	return self;
       
   118 	}
       
   119 
       
   120 CMMFDevSoundUtility* CMMFDevSoundUtility::NewLC()
       
   121 	{
       
   122 	CMMFDevSoundUtility* self = new(ELeave) CMMFDevSoundUtility();
       
   123 	CleanupStack::PushL(self);
       
   124 	self->ConstructL();
       
   125 	// Leave it on Cleanupstack
       
   126 	return self;
       
   127 	}
       
   128 
       
   129 void CMMFDevSoundUtility::ConstructL()
       
   130 	{
       
   131 	iFixedSequenceNames = new (ELeave) HMMFToneFixedSequenceNames;
       
   132 	}
       
   133 
       
   134 void CMMFDevSoundUtility::SeekUsingFourCCL(TUid aInterfaceUid, RImplInfoPtrArray& aPlugInArray, const TFourCC& aSrcDataType, const TFourCC& aDstDataType, const TDesC& aPreferredSupplier)
       
   135 	{
       
   136 
       
   137 	// Create a match string using the two FourCC codes.
       
   138 	_LIT8(KEmptyFourCCString, "    ,    ");
       
   139 	TBufC8<9> fourCCString(KEmptyFourCCString);
       
   140 	TPtr8 fourCCPtr = fourCCString.Des();
       
   141 	TPtr8 fourCCPtr1(&fourCCPtr[0], 4);
       
   142 	TPtr8 fourCCPtr2(&fourCCPtr[5], 4 );
       
   143 	aSrcDataType.FourCC(&fourCCPtr1);
       
   144 	aDstDataType.FourCC(&fourCCPtr2);
       
   145 
       
   146 	// Create a TEcomResolverParams structure.
       
   147 	TEComResolverParams resolverParams;
       
   148 	resolverParams.SetDataType(fourCCPtr);
       
   149 	resolverParams.SetWildcardMatch(EFalse); 
       
   150 
       
   151 	// ListImplementationsL leaves if it cannot find anything so trap the error and ignore it.
       
   152 	TRAPD(err, REComSession::ListImplementationsL(aInterfaceUid, resolverParams, aPlugInArray));
       
   153 
       
   154 	User::LeaveIfError(err); //The error above may not be KErrNotFound eg could be KErrNoMemory in which case leave
       
   155 
       
   156 	// IF there are no plugins, return failure
       
   157 	if(aPlugInArray.Count() == 0)
       
   158 		User::Leave( KErrNotFound ) ; 
       
   159 
       
   160 	// If more than one match.  Narrow the search by preferred supplier
       
   161 	if((aPlugInArray.Count() > 1) && aPreferredSupplier.Length())
       
   162 		SelectByPreference( aPlugInArray, aPreferredSupplier ) ;
       
   163 
       
   164 	}
       
   165 
       
   166 
       
   167 /*
       
   168  * local function to disable items which do not match the preferred supplier.
       
   169  * Note that at least one enabled item is returned (if there was an enabled item to begin with) which
       
   170  * may not match the preferred supplier.
       
   171  * 
       
   172  */
       
   173 void CMMFDevSoundUtility::SelectByPreference( RImplInfoPtrArray& aPlugInArray, const TDesC& aPreferredSupplier ) 
       
   174 	{
       
   175 
       
   176 	// Use the Disabled flag to eliminated all currently enabled matches that
       
   177 	// do not match the preferred supplier.
       
   178 	TInt firstEnabled = -1 ; // to ensure that we return something valid
       
   179 	TInt matchCount = 0 ;
       
   180 	for ( TInt ii = 0 ; ii < aPlugInArray.Count() ; ii++ )
       
   181 		{
       
   182 		if ( !( aPlugInArray[ii]->Disabled() ) )
       
   183 			{
       
   184 			if ( firstEnabled == -1 )
       
   185 				firstEnabled = ii ;
       
   186 			if ( aPlugInArray[ii]->DisplayName().FindF( aPreferredSupplier ) == KErrNotFound )
       
   187 				aPlugInArray[ii]->SetDisabled( ETrue ) ;
       
   188 			else
       
   189 				matchCount++ ;
       
   190 			}
       
   191 		}
       
   192 
       
   193 	// If there are no matches then re-enable the first enabled
       
   194 	if ( matchCount == 0 )
       
   195 		aPlugInArray[firstEnabled]->SetDisabled( EFalse ) ;
       
   196 	else if ( matchCount > 1 )
       
   197 		{
       
   198 		// find the latest version from more than one match
       
   199 		TInt highestVersionIndex = -1 ;
       
   200 		for ( TInt ii = 0 ; ii < aPlugInArray.Count() ; ii++ )
       
   201 			{
       
   202 			if ( !( aPlugInArray[ii]->Disabled() ) )  // only interested in enabled elements
       
   203 				{
       
   204 				if ( highestVersionIndex == -1 )
       
   205 					{ // first match.  Store this.  Keep it enabled
       
   206 					highestVersionIndex = ii ;
       
   207 					}
       
   208 				else if ( aPlugInArray[ii]->Version() > aPlugInArray[highestVersionIndex]->Version() )
       
   209 					{ // a new leader.  Disable the previous leader.  Keep this one.
       
   210 					aPlugInArray[highestVersionIndex]->SetDisabled( ETrue ) ;
       
   211 					highestVersionIndex = ii ;
       
   212 					}
       
   213 				else  // we already have a higher version.
       
   214 					aPlugInArray[ii]->SetDisabled( ETrue ) ;
       
   215 				}
       
   216 			}
       
   217 		}
       
   218 	}
       
   219 
       
   220 
       
   221 /*
       
   222  *  SeekHwDevicePluginsL
       
   223  *  This method looks for hwDevicePlugins that support the state given in aState which
       
   224  *  must be either EMMFStatePlaying or EMMFStateRecording
       
   225  *  For each HwDevice plugin found the datatype as indicated by its fourCC code
       
   226  *  from the default_data field in the resource file is added to the array of aSupportedDataTypes
       
   227  *
       
   228  *  @internalComponent
       
   229  *
       
   230  *	@param	"RArray<TFourCC>& aSupportedDataTypes"
       
   231  *  an array of fourCC codes that has a fourCC code added to for each hardware device found
       
   232  *
       
   233  *	@param  "TMMFState aState"
       
   234  *  must be set to EMMFStatePlaying if seeking HwDevice plugins that support play and
       
   235  *  EMMFStateRecording if seeking HwDevice plugins that support record
       
   236  *	
       
   237  *  @leave KErrArgument if aState is not EMMFStatePlaying or EMMFStateRecording else leaves
       
   238  *  with standard symbian OS error code
       
   239  */
       
   240 void CMMFDevSoundUtility::SeekHwDevicePluginsL(RArray<TFourCC>& aSupportedDataTypes, TMMFState aState)
       
   241 	{	
       
   242 	//check argument precondition for aState
       
   243 	if ((aState != EMMFStatePlaying) && (aState != EMMFStateRecording))
       
   244 		User::Leave(KErrArgument);
       
   245 
       
   246 	aSupportedDataTypes.Reset(); //clear any existing data in aSupportedDataTypes array
       
   247 
       
   248 	RImplInfoPtrArray plugInArray ; // Array to return hw device plugin resource info(place on cleanupstack _after_ ListImplementationsL() )
       
   249 	TUid KUidMmfHWPluginInterfaceCodec = {KMmfUidBtPluginInterfaceHwDevice};
       
   250 
       
   251 	// ListImplementationsL leaves if it cannot find anything so trap the error
       
   252 	TRAPD( err, REComSession::ListImplementationsL(KUidMmfHWPluginInterfaceCodec, plugInArray ) ) ;
       
   253 	CleanupResetAndDestroyPushL(plugInArray);
       
   254 
       
   255 	TUint numberOfHwDevicePlugins = plugInArray.Count();
       
   256 
       
   257 	//if no errors and have hwdevice plugin resource entries then scan entries
       
   258 	//matching on a datatype of pcm16 as the destination datatype for play and the 
       
   259 	//source datatype for record
       
   260 	//if a match is found and isn't already in list of supported data types
       
   261 	//then add it to the list
       
   262 	if ((err == KErrNone)&&(numberOfHwDevicePlugins))
       
   263 		{					
       
   264 		CImplementationInformation* hwDeviceResourceEntry = NULL;
       
   265 		_LIT8(KPCM16FourCCString, " P16");
       
   266 		TBufC8<KFourCCLength> fourCCStringPCM16(KPCM16FourCCString);
       
   267 		TPtr8 fourCCPtrPCM16 = fourCCStringPCM16.Des();
       
   268 		TUint entryNumber = 0;
       
   269 
       
   270 		//check each resource entry for dst 4CC = P16 for play and src 4CC = P16 for record
       
   271 		for (TUint hwDeviceEntry = 0; hwDeviceEntry < numberOfHwDevicePlugins; hwDeviceEntry++)
       
   272 			{
       
   273 			hwDeviceResourceEntry = plugInArray[hwDeviceEntry];
       
   274 			if (IsDataTypeMatch(hwDeviceResourceEntry, fourCCPtrPCM16, aState))
       
   275 				{//resource entry data field has dest/src datatype ' P16' ie pcm16 for play/record
       
   276 				TPtrC8 fourCCPtr(0,0);
       
   277 				if (aState == EMMFStatePlaying)//then datatype supported 4CC is left 4 chars
       
   278 					fourCCPtr.Set(hwDeviceResourceEntry->DataType().Left(KFourCCLength));
       
   279 				else if (aState == EMMFStateRecording) //then datatype supported 4CC is right 4 chars
       
   280 					fourCCPtr.Set(hwDeviceResourceEntry->DataType().Right(KFourCCLength));
       
   281 				TFourCC fourCCEntry(fourCCPtr);
       
   282 				//need to check if entry already exists to prevent duplicate entries
       
   283 				TBool alreadyExists = EFalse;
       
   284 				for (TUint fourCCEntryNumber = 0; fourCCEntryNumber < entryNumber; fourCCEntryNumber++)
       
   285 					{
       
   286 					if (aSupportedDataTypes[fourCCEntryNumber]==fourCCEntry)
       
   287 						{
       
   288 						alreadyExists = ETrue;//we already have this 4CC in the supported data types
       
   289 						break;
       
   290 						}
       
   291 					}
       
   292 				if (!alreadyExists)
       
   293 					{
       
   294 					err = aSupportedDataTypes.Append(fourCCEntry);
       
   295 					if (err)
       
   296 						{//note we don't destroy array because we don't own it
       
   297 						//but we do reset it as it is incomplete
       
   298 						aSupportedDataTypes.Reset();
       
   299 						User::Leave(err);
       
   300 						}
       
   301 					}
       
   302 				}//if (IsDataTypeMatch(hwDeviceResourceEntry, fourCCPtrPCM16, aState))
       
   303 			}//for (TUint hwDeviceEntry = 0; hwDeviceEntry < numberOfHwDevicePlugins; hwDeviceEntry++)
       
   304 		}//if ((err == KErrNone)&&(numberOfHwDevicePlugins))
       
   305 	else
       
   306 		{//if an error occured and not KErrNotFound then must be a 'real' error eg KErrNoMemory
       
   307 		if ((err != KErrNotFound)&&(err != KErrNone))
       
   308 			User::Leave(err);
       
   309 		}
       
   310 	CleanupStack::PopAndDestroy(&plugInArray);
       
   311 	}
       
   312 
       
   313 
       
   314 /*
       
   315  *	IsDataTypeMatch
       
   316  *  This method takes a given resource entry from a hardware device and determines 
       
   317  *  whether the hwdevice plugin is a data type match for playing or recording
       
   318  *  depending on the setting of aState
       
   319  *  The method matchs the default_data field from the hw device resource entry matching
       
   320  *  it with the aHwMatchFourCC code.
       
   321  *
       
   322  *  @internalComponent
       
   323  *
       
   324  *	@param	"CImplementationInformation aHwDeviceResourceEntry"
       
   325  *  the hw device resource entry that is to be checked 
       
   326  *  whether it can be used to play or record
       
   327  *
       
   328  *	@param  "TDesC8& aHwMatchFourCC
       
   329  *	the data type fourCC code to match to that the hardware device that must convert to for
       
   330  *	playing and convert from for recording - for the reference DevSound this is always ' P16' ie pcm16
       
   331  *
       
   332  *  @param "TMMFState aState"
       
   333  *  this determines whether the match is for playing or recording and should take 
       
   334  *  either the values EMMFStatePlaying or EMMFStateRecording
       
   335  *
       
   336  *  @return ETrue if a match for play or record else EFalse
       
   337  */
       
   338 TBool CMMFDevSoundUtility::IsDataTypeMatch(CImplementationInformation* aHwDeviceResourceEntry,const TDesC8& aHwMatchFourCC, TMMFState aState)
       
   339 	{
       
   340 	TBool match = EFalse;
       
   341 	if (aState == EMMFStatePlaying)
       
   342 		{//play need to match with the right four characters
       
   343 		match =  (!(aHwMatchFourCC.Match(aHwDeviceResourceEntry->DataType().Right(KFourCCLength))==KErrNotFound));
       
   344 		}
       
   345 	else if (aState == EMMFStateRecording)
       
   346 		{//record need to match with the left four characters
       
   347 		match = (!(aHwMatchFourCC.Match(aHwDeviceResourceEntry->DataType().Left(KFourCCLength))==KErrNotFound));
       
   348 		}
       
   349 	return match;
       
   350 	}
       
   351 
       
   352 
       
   353 /**
       
   354  *	Populate fixed sequences
       
   355  *
       
   356  */
       
   357 void CMMFDevSoundUtility::InitializeFixedSequenceL(CPtrC8Array** aFixedSequences)
       
   358 	{
       
   359 
       
   360 	RFs fsSession;
       
   361 	User::LeaveIfError(fsSession.Connect());
       
   362 	CleanupClosePushL(fsSession);
       
   363 
       
   364 	// Open the resource file
       
   365 	RResourceFile resourceFile;
       
   366 	resourceFile.OpenL(fsSession, KFixedSequenceResourceFile);
       
   367 	CleanupClosePushL(resourceFile);
       
   368 
       
   369 	// Allocate buffer to hold resource data in binary format
       
   370 	iInfo = resourceFile.AllocReadL(FIXED_TONE_SEQUENCE);
       
   371 
       
   372 	TResourceReader reader;
       
   373 	reader.SetBuffer(iInfo);
       
   374 
       
   375 	// Create array to hold fixed sequences data
       
   376 	CPtrC8Array* tempSequences = new(ELeave) CPtrC8Array(8); //  granularity
       
   377 	CleanupStack::PushL(tempSequences);
       
   378 
       
   379 	// First word gives number of entries
       
   380 	TInt numberOfEntries = reader.ReadUint16(); 
       
   381 	ASSERT(!(numberOfEntries&1)); // Should have atleast one entry
       
   382 
       
   383 	// There must be an even number entries as each sequence structure
       
   384 	// is made of a name string and a data string (SEQUENCE_NAME and SEQUENCE_DATA)
       
   385 
       
   386 	HMMFToneFixedSequenceNames* names = new (ELeave) HMMFToneFixedSequenceNames;
       
   387 	CleanupStack::PushL(names);
       
   388 	for (TInt i=0;i<numberOfEntries;i+=2)
       
   389 		{
       
   390 		// Copy name from resource array to returnable array
       
   391 		HMMFToneFixedSequenceNames* newNames = names->AddNameL(reader.ReadTPtrC());
       
   392 		if (names != newNames)
       
   393 			{ // May have moved so fixup cleanupstack reference
       
   394 			CleanupStack::Pop();
       
   395 			names = newNames;
       
   396 			CleanupStack::PushL(names);
       
   397 			}
       
   398 		TInt len = reader.ReadUint16();
       
   399 		TPtrC8 tempTPtrC8(REINTERPRET_CAST(const TUint8*,reader.Ptr()),len<<1);
       
   400 		tempSequences->AppendL(tempTPtrC8);
       
   401 		reader.Advance(len<<1);
       
   402 		}
       
   403 	CleanupStack::Pop(); // names
       
   404 
       
   405 	// Delete the old fixed sequence names
       
   406 	delete iFixedSequenceNames;
       
   407 	iFixedSequenceNames = NULL;
       
   408 	iFixedSequenceNames = names;
       
   409 
       
   410 	*aFixedSequences = tempSequences;
       
   411 	CleanupStack::Pop(tempSequences);
       
   412 	CleanupStack::PopAndDestroy(2);	// resourceFile, fsSession
       
   413 	}
       
   414 
       
   415 TBool CMMFDevSoundUtility::RecognizeSequence(const TDesC8& aData)
       
   416 	{
       
   417 	// Reference plug-in only supports its own sequence format
       
   418 	_LIT8(KSequenceSignature,"SQNC");
       
   419 	if (aData.Length() > 4)
       
   420 		{
       
   421 		if (aData.Left(4) == KSequenceSignature)
       
   422 			return ETrue;
       
   423 		}
       
   424 	// Didn't recognise
       
   425 	return EFalse;
       
   426 	}
       
   427 
       
   428 const TDesC& CMMFDevSoundUtility::FixedSequenceName(TInt aSequenceNumber)
       
   429 	{
       
   430 	ASSERT(iFixedSequenceNames); // Defect if this not true when previous was true
       
   431 	ASSERT((aSequenceNumber>=0)&&(aSequenceNumber<iFixedSequenceNames->iCount));
       
   432 
       
   433 	// Ptr to first descriptor
       
   434 	TUint8* ptr = REINTERPRET_CAST(TUint8*,&(iFixedSequenceNames->iCount))+sizeof(TInt);
       
   435 	TDesC* desPtr = REINTERPRET_CAST(TDesC*,ptr); // First des
       
   436 	while (aSequenceNumber--)
       
   437 		{
       
   438 		TInt size = desPtr->Size();
       
   439 		if (size&3)
       
   440 			size = ((size+4)&(~3));
       
   441 		ptr += sizeof(TInt) + size;
       
   442 		desPtr = REINTERPRET_CAST(TDesC*,ptr); // Next des
       
   443 		}
       
   444 	return *desPtr;
       
   445 	}