Update contrib.
1 // Copyright (c) 2002-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.
16 #include <mmf/common/mmfcontrollerpluginresolver.h>
17 #include <mmf/server/mmfdatabuffer.h>
18 #include <mmf/common/mmfutilities.h>
19 #include <mmf/server/mmfclip.h>
20 #include <mmf/server/mmffile.h>
21 #include <mmf/server/mmfformat.h>
23 //static const TUid KUidMmfPluginInterfaceFormatDecode = {KMmfUidPluginInterfaceFormatDecode};
24 //static const TUid KUidMmfPluginInterfaceFormatEncode = {KMmfUidPluginInterfaceFormatEncode};
26 const TInt KMmfHeaderBufferSize = 512 ; //512 bytes read to parse the header
28 CMMFFormatDecode* CMMFFormatDecode::CreateFormatL(TUid aUid, MDataSource* aSource)
30 CMMFFormatDecode* s = REINTERPRET_CAST( CMMFFormatDecode*,
31 REComSession::CreateImplementationL( aUid, _FOFF( CMMFFormatDecode, iDtor_ID_Key),
32 STATIC_CAST( TAny*, aSource ) ) ) ;
33 s->iImplementationUid = aUid;
38 Allocates and constructs an ECom format decode object.
40 This is used to explicitly instantiate a format decoder where the UID is known. This method might
41 be used by controllers that only support one type of format such that the UID of the format is
42 already known. For example, if an mp3 controller has been instantiated, there is no need for any
43 further format recognition as this has already been performed in the controller instantiation, thus
44 the controller can instantiate an mp3 format directly from it’s UID.
47 The implementation UID. This is used by Com to create the plugin.
49 The controller MDataSource and is the source of the data for the format decode plugin. The
50 format decode plugin is the sink of data for the MDataSource.
52 @return If successful, returns the address of the format decode plugin object created. If not successful,
53 leaves with KErrNotFound.
55 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( TUid aUid, MDataSource* aSource )
57 return CreateFormatL(aUid, aSource);
63 Attempt to locate and instantiate a FormatDecode using a filename or an extension.
65 Only the extension is used. If no extension is supplied (no dot is present) the whole of the
66 filename will be treated as the extension.
68 If the file already exists, the file header is scanned to find a match in the opaque_data field of
72 The file name of target file. May be extension only or may include full path.
74 The controller's MDataSource. This is the source of the data for the format decode plugin. This must
75 be a CMMFFile source when instantiating a CMMFFormatDecode using a file name.
76 @param aPreferredSupplier
77 If this is provided the list of matching plugins will be further searched for the latest version of a
78 plugin supplied by supplier named.
80 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
82 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC16& aFileName, MDataSource* aSource, const TDesC& aPreferredSupplier )
84 CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
85 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
86 formatParams->SetMatchToFileNameL(aFileName);
87 pluginSelector->SetRequiredFormatSupportL(*formatParams);
88 if (aPreferredSupplier.Length() > 0)
89 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
91 // Instantiate this format
92 CMMFFormatDecode* theChosenOne =
93 MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource);
96 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
102 Attempt to locate and instantiate a FormatDecode using a filename or an extension.
104 Only the extension is used. If no extension is supplied (no dot is present) the whole of the
105 filename will be treated as the extension.
107 If the file already exists, the file header is scanned to find a match in the opaque_data field of
111 The file name of target file. May be extension only or may include full path.
113 The controller's MDataSource. This is the source of the data for the format decode plugin. This must
114 be a CMMFFile source when instantiating a CMMFFormatDecode using a file name.
115 @param aPreferredSupplier
116 If this is provided the list of matching plugins will be further searched for the latest version of a
117 plugin supplied by supplier named.
118 @param aSupportsCustomInterfaces
119 Indicates whether the instantiated FormatDecode supports custom interfaces.
121 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
123 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC16& aFileName, MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
125 CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
126 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
127 formatParams->SetMatchToFileNameL(aFileName);
128 pluginSelector->SetRequiredFormatSupportL(*formatParams);
129 if (aPreferredSupplier.Length() > 0)
130 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
132 // Instantiate this format
133 CMMFFormatDecode* theChosenOne =
134 MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource, aSupportsCustomInterfaces);
137 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
144 Attempts to locate and instantiate a CMMFFormatDecode using data in a buffer. The buffer is expected to contain
145 header data (from a file, stream or descriptor).
147 Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
149 This function uses the match string as a resolver parameter. The format base class uses the match string
150 to find a match to the match string defined in the opaque_data field of the resource file. The
151 match string would typically be a signature for a particular format usually defined in the format header.
154 The data which is searched for matching signatures.
156 The controller's MDataSource and the source of the data for the format decode plugin.
157 @param aPreferredSupplier
158 If this is provided the list of matching plugins will be further searched for the latest version of a
159 plugin supplied by the specified supplier.
161 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
163 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC8& aSourceHeader, MDataSource* aSource, const TDesC& aPreferredSupplier )
165 CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
166 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
167 formatParams->SetMatchToHeaderDataL(aSourceHeader);
168 pluginSelector->SetRequiredFormatSupportL(*formatParams);
169 if (aPreferredSupplier.Length() > 0)
170 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
172 // Instantiate this format
173 CMMFFormatDecode* theChosenOne =
174 MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource);
177 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
183 Attempts to locate and instantiate a CMMFFormatDecode using data in a buffer. The buffer is expected to contain
184 header data (from a file, stream or descriptor).
186 Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
188 This function uses the match string as a resolver parameter. The format base class uses the match string
189 to find a match to the match string defined in the opaque_data field of the resource file. The
190 match string would typically be a signature for a particular format usually defined in the format header.
193 The data which is searched for matching signatures.
195 The controller's MDataSource and the source of the data for the format decode plugin.
196 @param aPreferredSupplier
197 If this is provided the list of matching plugins will be further searched for the latest version of a
198 plugin supplied by the specified supplier.
199 @param aSupportsCustomInterfaces
200 Indicates whether the instantiated FormatDecode supports custom interfaces.
202 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
204 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC8& aSourceHeader, MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
206 CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
207 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
208 formatParams->SetMatchToHeaderDataL(aSourceHeader);
209 pluginSelector->SetRequiredFormatSupportL(*formatParams);
210 if (aPreferredSupplier.Length() > 0)
211 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
213 // Instantiate this format
214 CMMFFormatDecode* theChosenOne =
215 MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource, aSupportsCustomInterfaces);
218 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
224 Attempts to locate and instantiate a CMMFFormatDecode using data from MDataSource.
226 The data is expected to contain header data (from a file, stream or descriptor). Signatures
227 (supplied by the plugin registry information) are sought in aSource.
230 Header data. Must be derived from CMMFClip.
231 @param aPreferredSupplier
232 If this is provided, the list of matching plugins will be further searched for the latest version of a
233 plugin supplied by the specified supplier.
235 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
237 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( MDataSource* aSource, const TDesC& aPreferredSupplier )
239 // Read header data from aSource. Call source header version
241 if ( !( (aSource->DataSourceType() == KUidMmfFileSource ) || ( aSource->DataSourceType() == KUidMmfDescriptorSource) ) )
242 User::Leave( KErrNotSupported ) ;
244 CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
245 CleanupStack::PushL( buffer ) ;
247 aSource->SourcePrimeL();
248 TCleanupItem srcCleanupItem(DoDataSourceStop, aSource);
249 CleanupStack::PushL(srcCleanupItem);
251 STATIC_CAST( CMMFClip*, aSource )->ReadBufferL( buffer, 0 ) ;
254 aSource->SourceStopL();
256 // attempt to instantiate the format by header data
257 // if this fails, try using the file extension
258 CMMFFormatDecode* ret = NULL;
260 TRAP(err, ret = NewL( buffer->Data(), aSource, aPreferredSupplier ));
261 if (err != KErrNone && aSource->DataSourceType() == KUidMmfFileSource)
263 CMMFFile* mmfFile = static_cast<CMMFFile*> (aSource);
264 TRAP(errFile, ret = NewL( mmfFile->FullName(), aSource, aPreferredSupplier ));
265 if (errFile == KErrNone)
268 User::LeaveIfError(err);
270 CleanupStack::PopAndDestroy() ; // buffer
276 Attempts to locate and instantiate a CMMFFormatDecode using data from MDataSource.
278 The data is expected to contain header data (from a file, stream or descriptor). Signatures
279 (supplied by the plugin registry information) are sought in aSource.
282 Header data. Must be derived from CMMFClip.
283 @param aPreferredSupplier
284 If this is provided, the list of matching plugins will be further searched for the latest version of a
285 plugin supplied by the specified supplier.
286 @param aSupportsCustomInterfaces
287 Indicates whether the instantiated FormatDecode supports custom interfaces.
289 @return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
291 EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
293 // Read header data from aSource. Call source header version
295 if ( !( (aSource->DataSourceType() == KUidMmfFileSource ) || ( aSource->DataSourceType() == KUidMmfDescriptorSource) ) )
296 User::Leave( KErrNotSupported ) ;
298 CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
299 CleanupStack::PushL( buffer ) ;
301 aSource->SourcePrimeL();
302 TCleanupItem srcCleanupItem(DoDataSourceStop, aSource);
303 CleanupStack::PushL(srcCleanupItem);
305 STATIC_CAST( CMMFClip*, aSource )->ReadBufferL( buffer, 0 ) ;
308 aSource->SourceStopL();
310 // attempt to instantiate the format by header data
311 // if this fails, try using the file extension
312 CMMFFormatDecode* ret = NULL;
314 TRAP(err, ret = NewL( buffer->Data(), aSource, aPreferredSupplier, aSupportsCustomInterfaces ));
315 if (err != KErrNone && aSource->DataSourceType() == KUidMmfFileSource)
317 CMMFFile* mmfFile = static_cast<CMMFFile*> (aSource);
318 TRAP(errFile, ret = NewL( mmfFile->FullName(), aSource, aPreferredSupplier, aSupportsCustomInterfaces ));
319 if (errFile == KErrNone)
322 User::LeaveIfError(err);
324 CleanupStack::PopAndDestroy() ; // buffer
329 CMMFFormatEncode* CMMFFormatEncode::CreateFormatL(TUid aUid, MDataSink* aSink)
331 CMMFFormatEncode* s = REINTERPRET_CAST( CMMFFormatEncode*,
332 REComSession::CreateImplementationL( aUid, _FOFF( CMMFFormatEncode, iDtor_ID_Key ),
333 STATIC_CAST( TAny*, aSink ) ) ) ;
334 s->iImplementationUid = aUid;
339 Allocates and constructs an ECom format encode object.
342 The implementation UID.
346 @return If successful, returns the address of the format decode plugin object created. If not successful,
347 leaves with KErrNotFound.
349 EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( TUid aUid, MDataSink* aSink )
351 return CreateFormatL(aUid, aSink);
355 Attempts to locate and instantiate a CMMFFormatEncode using a filename or an extension.
357 Only the extension of the supplied file name is used. If no extension is supplied (ie. no dot is present)
358 the whole of the filename will be treated as the extension.
361 File name of target file. May be extension only or may include the full path.
364 @param aPreferredSupplier
365 If this is provided, the list of matching plugins will be further searched for the latest version of a
366 plugin supplied by supplier named.
368 @return If successful, returns the address of the format decode plugin object created. If not successful,
369 leaves with KErrNotFound.
371 EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( const TDesC16& aFileName, MDataSink* aSink, const TDesC& aPreferredSupplier )
373 CMMFFormatEncodePluginSelectionParameters* pluginSelector = CMMFFormatEncodePluginSelectionParameters::NewLC();
374 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
375 formatParams->SetMatchToFileNameL(aFileName);
376 pluginSelector->SetRequiredFormatSupportL(*formatParams);
377 if (aPreferredSupplier.Length() > 0)
378 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
380 TUid chosenUid = MMFFormatEcomUtilities::SelectFormatPluginL(*pluginSelector);
382 // Instantiate this format
383 CMMFFormatEncode* theChosenOne = CreateFormatL(chosenUid, aSink);
386 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
392 Attempts to locate and instantiate a CMMFFormatEncode using data in the specified buffer.
394 The buffer is expected to contain header data (from a file, stream or descriptor).
395 Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
398 The data which is searched for matching signatures.
401 @param aPreferredSupplier
402 If this is provided the list of matching plugins will be further searched for the latest version of a
403 plugin supplied by supplier named.
405 @return If successful, returns the address of the format decode plugin object created. If not successful,
406 leaves with KErrNotFound.
408 EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( const TDesC8& aSourceHeader, MDataSink* aSink, const TDesC& aPreferredSupplier )
410 CMMFFormatEncodePluginSelectionParameters* pluginSelector = CMMFFormatEncodePluginSelectionParameters::NewLC();
411 CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
412 formatParams->SetMatchToHeaderDataL(aSourceHeader);
413 pluginSelector->SetRequiredFormatSupportL(*formatParams);
414 if (aPreferredSupplier.Length() > 0)
415 pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
417 TUid chosenUid = MMFFormatEcomUtilities::SelectFormatPluginL(*pluginSelector);
419 // Instantiate this format
420 CMMFFormatEncode* theChosenOne = CreateFormatL(chosenUid, aSink);
423 CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
429 Attempts to locate and instantiate a CMMFFormatEncode using data from aSink.
431 The data is expected to contain header data (from a file, stream or descriptor).
432 Signatures (supplied by the plugin registry information) are sought in the source header.
435 The header data. Must be derived from CMMFClip.
436 @param aPreferredSupplier
437 If this is provided, the list of matching plugins will be further searched for the latest version of a
438 plugin supplied by supplier specified.
440 @return If successful, returns the address of the format decode plugin object created. If not successful,
441 leaves with KErrNotFound.
443 EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( MDataSink* aSink, const TDesC& aPreferredSupplier )
445 // Read header data from aSource. Call source header version if there is header data, file name version otherwise.
447 if ( !( (aSink->DataSinkType() == KUidMmfFileSink ) || ( aSink->DataSinkType() == KUidMmfDescriptorSink) ) )
448 User::Leave( KErrNotSupported ) ;
450 CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
451 CleanupStack::PushL( buffer ) ;
454 TCleanupItem sinkCleanupItem(DoDataSinkStop, aSink);
455 CleanupStack::PushL(sinkCleanupItem);
457 STATIC_CAST( CMMFClip*, aSink )->ReadBufferL( buffer, 0 ) ;
462 CMMFFormatEncode* ret = NULL ; // set to null to avoid compiler warning.
463 // Check for data in the buffer
464 if ( buffer->BufferSize() != 0 )
465 ret = NewL( buffer->Data(), aSink, aPreferredSupplier ) ;
466 else if ( aSink->DataSinkType() == KUidMmfFileSink )
467 ret = NewL( STATIC_CAST(CMMFFile*, aSink)->Extension(), aSink, aPreferredSupplier ) ;
469 User::Leave( KErrNotSupported ) ;
471 CleanupStack::PopAndDestroy() ; // buffer
476 TUid MMFFormatEcomUtilities::SelectFormatPluginL(const CMMFFormatPluginSelectionParameters& aSelectParams)
478 RMMFFormatImplInfoArray pluginArray;
479 CleanupResetAndDestroyPushL(pluginArray);
480 aSelectParams.ListImplementationsL(pluginArray);
482 // Just leave if no implementations were found
483 if (pluginArray.Count() == 0)
484 User::Leave(KErrNotSupported);
486 // Return the uid of the first plugin in the list
487 TUid ret = pluginArray[0]->Uid();
488 CleanupStack::PopAndDestroy();//pluginArray
493 * instantiate each format decode plugin in turn until we find one that works
495 CMMFFormatDecode* MMFFormatEcomUtilities::SelectFormatDecodePluginL(const CMMFFormatPluginSelectionParameters& aSelectParams, MDataSource* aSource)
497 RMMFFormatImplInfoArray pluginArray;
498 CleanupResetAndDestroyPushL(pluginArray);
499 aSelectParams.ListImplementationsL(pluginArray);
501 TInt pluginCount = pluginArray.Count();
502 CMMFFormatDecode* theChosenOne = NULL;
504 TInt err = KErrNotSupported;
505 for (TInt n=0; n<pluginCount; n++)
507 // Try to instantiate this format
508 TRAP(err, theChosenOne = CMMFFormatDecode::NewL(pluginArray[n]->Uid(), aSource));
509 // Ensure OOM or any unexpected error is caught immediately
510 // i.e. don't try the next plugin
511 if (err != KErrNotSupported && err != KErrCorrupt && err != KErrArgument)
514 User::LeaveIfError(err);
516 CleanupStack::PopAndDestroy(&pluginArray);
522 * instantiate each format decode plugin in turn until we find one that works
524 CMMFFormatDecode* MMFFormatEcomUtilities::SelectFormatDecodePluginL(const CMMFFormatPluginSelectionParameters& aSelectParams, MDataSource* aSource, TBool& aSupportsCustomInterfaces)
526 RMMFFormatImplInfoArray pluginArray;
527 CleanupResetAndDestroyPushL(pluginArray);
528 aSelectParams.ListImplementationsL(pluginArray);
530 TInt pluginCount = pluginArray.Count();
531 CMMFFormatDecode* theChosenOne = NULL;
533 TInt err = KErrNotSupported;
534 CMMFFormatImplementationInformation* implInfo = NULL;
535 for (TInt n=0; n<pluginCount; n++)
537 implInfo = pluginArray[n];
538 // Try to instantiate this format
539 TRAP(err, theChosenOne = CMMFFormatDecode::NewL(implInfo->Uid(), aSource));
540 // Ensure OOM or any unexpected error is caught immediately
541 // i.e. don't try the next plugin
542 if (err != KErrNotSupported && err != KErrCorrupt && err != KErrArgument)
545 User::LeaveIfError(err);
546 aSupportsCustomInterfaces = implInfo->SupportsCustomInterfaces();
548 CleanupStack::PopAndDestroy(&pluginArray);