Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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
21 // Date: Author Description
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()
35 _LIT(KFixedSequenceResourceFile, "Z:\\Resource\\DevSound\\FixedSequence.rsc");
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 )
44 inline TPtr8& TMMFRawPackage::Package()
46 ((TMMFRawPackage*)this)->iThis.Set((TUint8*)this,iThis.Length(),iThis.MaxLength());
50 inline const TPtr8& TMMFRawPackage::Package() const
52 ((TMMFRawPackage*)this)->iThis.Set((TUint8*)this,iThis.Length(),iThis.MaxLength());
56 inline void TMMFRawPackage::SetSize(TInt aDerivedSize)
58 iThis.Set((TUint8*)this,aDerivedSize,aDerivedSize);
61 inline TMMFToneFixedSequenceNames::TMMFToneFixedSequenceNames() :
62 TMMFRawPackage(sizeof(TMMFToneFixedSequenceNames)) {}
65 class TNameBuf : public TBufCBase16
67 class TNameBuf : public TBufCBase8
70 friend class HMMFToneFixedSequenceNames;
73 HMMFToneFixedSequenceNames::HMMFToneFixedSequenceNames()
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
83 TInt size = Package().Length();
84 TInt desSize = aName.Size() + sizeof(TInt);
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);
98 /******************************************************************
100 ******************************************************************/
101 CMMFDevSoundUtility::CMMFDevSoundUtility()
103 // No default implementation
107 CMMFDevSoundUtility::~CMMFDevSoundUtility()
110 delete iFixedSequenceNames;
113 CMMFDevSoundUtility* CMMFDevSoundUtility::NewL()
115 CMMFDevSoundUtility* self = NewLC();
120 CMMFDevSoundUtility* CMMFDevSoundUtility::NewLC()
122 CMMFDevSoundUtility* self = new(ELeave) CMMFDevSoundUtility();
123 CleanupStack::PushL(self);
125 // Leave it on Cleanupstack
129 void CMMFDevSoundUtility::ConstructL()
131 iFixedSequenceNames = new (ELeave) HMMFToneFixedSequenceNames;
134 void CMMFDevSoundUtility::SeekUsingFourCCL(TUid aInterfaceUid, RImplInfoPtrArray& aPlugInArray, const TFourCC& aSrcDataType, const TFourCC& aDstDataType, const TDesC& aPreferredSupplier)
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);
146 // Create a TEcomResolverParams structure.
147 TEComResolverParams resolverParams;
148 resolverParams.SetDataType(fourCCPtr);
149 resolverParams.SetWildcardMatch(EFalse);
151 // ListImplementationsL leaves if it cannot find anything so trap the error and ignore it.
152 TRAPD(err, REComSession::ListImplementationsL(aInterfaceUid, resolverParams, aPlugInArray));
154 User::LeaveIfError(err); //The error above may not be KErrNotFound eg could be KErrNoMemory in which case leave
156 // IF there are no plugins, return failure
157 if(aPlugInArray.Count() == 0)
158 User::Leave( KErrNotFound ) ;
160 // If more than one match. Narrow the search by preferred supplier
161 if((aPlugInArray.Count() > 1) && aPreferredSupplier.Length())
162 SelectByPreference( aPlugInArray, aPreferredSupplier ) ;
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.
173 void CMMFDevSoundUtility::SelectByPreference( RImplInfoPtrArray& aPlugInArray, const TDesC& aPreferredSupplier )
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++ )
182 if ( !( aPlugInArray[ii]->Disabled() ) )
184 if ( firstEnabled == -1 )
186 if ( aPlugInArray[ii]->DisplayName().FindF( aPreferredSupplier ) == KErrNotFound )
187 aPlugInArray[ii]->SetDisabled( ETrue ) ;
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 )
198 // find the latest version from more than one match
199 TInt highestVersionIndex = -1 ;
200 for ( TInt ii = 0 ; ii < aPlugInArray.Count() ; ii++ )
202 if ( !( aPlugInArray[ii]->Disabled() ) ) // only interested in enabled elements
204 if ( highestVersionIndex == -1 )
205 { // first match. Store this. Keep it enabled
206 highestVersionIndex = ii ;
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 ;
213 else // we already have a higher version.
214 aPlugInArray[ii]->SetDisabled( ETrue ) ;
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
230 * @param "RArray<TFourCC>& aSupportedDataTypes"
231 * an array of fourCC codes that has a fourCC code added to for each hardware device found
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
237 * @leave KErrArgument if aState is not EMMFStatePlaying or EMMFStateRecording else leaves
238 * with standard symbian OS error code
240 void CMMFDevSoundUtility::SeekHwDevicePluginsL(RArray<TFourCC>& aSupportedDataTypes, TMMFState aState)
242 //check argument precondition for aState
243 if ((aState != EMMFStatePlaying) && (aState != EMMFStateRecording))
244 User::Leave(KErrArgument);
246 aSupportedDataTypes.Reset(); //clear any existing data in aSupportedDataTypes array
248 RImplInfoPtrArray plugInArray ; // Array to return hw device plugin resource info(place on cleanupstack _after_ ListImplementationsL() )
249 TUid KUidMmfHWPluginInterfaceCodec = {KMmfUidBtPluginInterfaceHwDevice};
251 // ListImplementationsL leaves if it cannot find anything so trap the error
252 TRAPD( err, REComSession::ListImplementationsL(KUidMmfHWPluginInterfaceCodec, plugInArray ) ) ;
253 CleanupResetAndDestroyPushL(plugInArray);
255 TUint numberOfHwDevicePlugins = plugInArray.Count();
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))
264 CImplementationInformation* hwDeviceResourceEntry = NULL;
265 _LIT8(KPCM16FourCCString, " P16");
266 TBufC8<KFourCCLength> fourCCStringPCM16(KPCM16FourCCString);
267 TPtr8 fourCCPtrPCM16 = fourCCStringPCM16.Des();
268 TUint entryNumber = 0;
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++)
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++)
286 if (aSupportedDataTypes[fourCCEntryNumber]==fourCCEntry)
288 alreadyExists = ETrue;//we already have this 4CC in the supported data types
294 err = aSupportedDataTypes.Append(fourCCEntry);
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();
302 }//if (IsDataTypeMatch(hwDeviceResourceEntry, fourCCPtrPCM16, aState))
303 }//for (TUint hwDeviceEntry = 0; hwDeviceEntry < numberOfHwDevicePlugins; hwDeviceEntry++)
304 }//if ((err == KErrNone)&&(numberOfHwDevicePlugins))
306 {//if an error occured and not KErrNotFound then must be a 'real' error eg KErrNoMemory
307 if ((err != KErrNotFound)&&(err != KErrNone))
310 CleanupStack::PopAndDestroy(&plugInArray);
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.
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
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
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
336 * @return ETrue if a match for play or record else EFalse
338 TBool CMMFDevSoundUtility::IsDataTypeMatch(CImplementationInformation* aHwDeviceResourceEntry,const TDesC8& aHwMatchFourCC, TMMFState aState)
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));
345 else if (aState == EMMFStateRecording)
346 {//record need to match with the left four characters
347 match = (!(aHwMatchFourCC.Match(aHwDeviceResourceEntry->DataType().Left(KFourCCLength))==KErrNotFound));
354 * Populate fixed sequences
357 void CMMFDevSoundUtility::InitializeFixedSequenceL(CPtrC8Array** aFixedSequences)
361 User::LeaveIfError(fsSession.Connect());
362 CleanupClosePushL(fsSession);
364 // Open the resource file
365 RResourceFile resourceFile;
366 resourceFile.OpenL(fsSession, KFixedSequenceResourceFile);
367 CleanupClosePushL(resourceFile);
369 // Allocate buffer to hold resource data in binary format
370 iInfo = resourceFile.AllocReadL(FIXED_TONE_SEQUENCE);
372 TResourceReader reader;
373 reader.SetBuffer(iInfo);
375 // Create array to hold fixed sequences data
376 CPtrC8Array* tempSequences = new(ELeave) CPtrC8Array(8); // granularity
377 CleanupStack::PushL(tempSequences);
379 // First word gives number of entries
380 TInt numberOfEntries = reader.ReadUint16();
381 ASSERT(!(numberOfEntries&1)); // Should have atleast one entry
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)
386 HMMFToneFixedSequenceNames* names = new (ELeave) HMMFToneFixedSequenceNames;
387 CleanupStack::PushL(names);
388 for (TInt i=0;i<numberOfEntries;i+=2)
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
396 CleanupStack::PushL(names);
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);
403 CleanupStack::Pop(); // names
405 // Delete the old fixed sequence names
406 delete iFixedSequenceNames;
407 iFixedSequenceNames = NULL;
408 iFixedSequenceNames = names;
410 *aFixedSequences = tempSequences;
411 CleanupStack::Pop(tempSequences);
412 CleanupStack::PopAndDestroy(2); // resourceFile, fsSession
415 TBool CMMFDevSoundUtility::RecognizeSequence(const TDesC8& aData)
417 // Reference plug-in only supports its own sequence format
418 _LIT8(KSequenceSignature,"SQNC");
419 if (aData.Length() > 4)
421 if (aData.Left(4) == KSequenceSignature)
428 const TDesC& CMMFDevSoundUtility::FixedSequenceName(TInt aSequenceNumber)
430 ASSERT(iFixedSequenceNames); // Defect if this not true when previous was true
431 ASSERT((aSequenceNumber>=0)&&(aSequenceNumber<iFixedSequenceNames->iCount));
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--)
438 TInt size = desPtr->Size();
440 size = ((size+4)&(~3));
441 ptr += sizeof(TInt) + size;
442 desPtr = REINTERPRET_CAST(TDesC*,ptr); // Next des