os/mm/mmlibs/mmfw/src/server/BaseClasses/Mmfformat.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.
sl@0
     1
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <mmf/common/mmfcontrollerpluginresolver.h>
sl@0
    17
#include <mmf/server/mmfdatabuffer.h>
sl@0
    18
#include <mmf/common/mmfutilities.h>
sl@0
    19
#include <mmf/server/mmfclip.h>
sl@0
    20
#include <mmf/server/mmffile.h>
sl@0
    21
#include <mmf/server/mmfformat.h>
sl@0
    22
sl@0
    23
//static const TUid KUidMmfPluginInterfaceFormatDecode = {KMmfUidPluginInterfaceFormatDecode};
sl@0
    24
//static const TUid KUidMmfPluginInterfaceFormatEncode = {KMmfUidPluginInterfaceFormatEncode};
sl@0
    25
sl@0
    26
const TInt KMmfHeaderBufferSize = 512 ;		//512 bytes read to parse the header
sl@0
    27
sl@0
    28
CMMFFormatDecode* CMMFFormatDecode::CreateFormatL(TUid aUid, MDataSource* aSource)
sl@0
    29
	{
sl@0
    30
	CMMFFormatDecode* s = REINTERPRET_CAST( CMMFFormatDecode*,
sl@0
    31
				REComSession::CreateImplementationL( aUid, _FOFF( CMMFFormatDecode, iDtor_ID_Key),
sl@0
    32
				STATIC_CAST( TAny*, aSource ) ) ) ;
sl@0
    33
	s->iImplementationUid = aUid;
sl@0
    34
	return s;
sl@0
    35
	}
sl@0
    36
sl@0
    37
/**
sl@0
    38
Allocates and constructs an ECom format decode object.
sl@0
    39
sl@0
    40
This is used to explicitly instantiate a format decoder where the UID is known. This method might
sl@0
    41
be used by controllers that only support one type of format such that the UID of the format is
sl@0
    42
already known. For example, if an mp3 controller has been instantiated, there is no need for any
sl@0
    43
further format recognition as this has already been performed in the controller instantiation, thus
sl@0
    44
the controller can instantiate an mp3 format directly from it’s UID.
sl@0
    45
sl@0
    46
@param  aUid 
sl@0
    47
        The implementation UID. This is used by Com to create the plugin.
sl@0
    48
@param  aSource 
sl@0
    49
        The controller MDataSource and is the source of the data for the format decode plugin. The 
sl@0
    50
        format decode plugin is the sink of data for the MDataSource.
sl@0
    51
sl@0
    52
@return If successful, returns the address of the format decode plugin object created. If not successful, 
sl@0
    53
        leaves with KErrNotFound.
sl@0
    54
*/
sl@0
    55
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( TUid aUid, MDataSource* aSource )
sl@0
    56
	{
sl@0
    57
	return CreateFormatL(aUid, aSource);
sl@0
    58
	}
sl@0
    59
sl@0
    60
sl@0
    61
sl@0
    62
/**
sl@0
    63
Attempt to locate and instantiate a FormatDecode using a filename or an extension.
sl@0
    64
sl@0
    65
Only the extension is used.  If no extension  is supplied (no dot is present) the whole of the 
sl@0
    66
filename will be treated as the extension.
sl@0
    67
sl@0
    68
If the file already exists, the file header is scanned to find a match in the opaque_data field of 
sl@0
    69
the resource file.
sl@0
    70
sl@0
    71
@param  aFileName
sl@0
    72
        The file name of target file.  May be extension only or may include full path.
sl@0
    73
@param  aSource
sl@0
    74
        The controller's MDataSource. This is the source of the data for the format decode plugin. This must
sl@0
    75
        be a CMMFFile source when instantiating a CMMFFormatDecode using a file name.
sl@0
    76
@param  aPreferredSupplier
sl@0
    77
        If this is provided the list of matching plugins will be further searched for the latest version of a
sl@0
    78
        plugin supplied by supplier named.
sl@0
    79
sl@0
    80
@return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
sl@0
    81
*/
sl@0
    82
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC16& aFileName, MDataSource* aSource, const TDesC& aPreferredSupplier )
sl@0
    83
	{
sl@0
    84
	CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
sl@0
    85
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
    86
	formatParams->SetMatchToFileNameL(aFileName);
sl@0
    87
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
    88
	if (aPreferredSupplier.Length() > 0)
sl@0
    89
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
    90
sl@0
    91
	// Instantiate this format
sl@0
    92
	CMMFFormatDecode* theChosenOne = 
sl@0
    93
		MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource);
sl@0
    94
sl@0
    95
	// Now clean up.
sl@0
    96
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
    97
sl@0
    98
	return theChosenOne;
sl@0
    99
	}
sl@0
   100
sl@0
   101
/**
sl@0
   102
Attempt to locate and instantiate a FormatDecode using a filename or an extension.
sl@0
   103
sl@0
   104
Only the extension is used.  If no extension  is supplied (no dot is present) the whole of the 
sl@0
   105
filename will be treated as the extension.
sl@0
   106
sl@0
   107
If the file already exists, the file header is scanned to find a match in the opaque_data field of 
sl@0
   108
the resource file.
sl@0
   109
sl@0
   110
@param  aFileName
sl@0
   111
        The file name of target file.  May be extension only or may include full path.
sl@0
   112
@param  aSource
sl@0
   113
        The controller's MDataSource. This is the source of the data for the format decode plugin. This must
sl@0
   114
        be a CMMFFile source when instantiating a CMMFFormatDecode using a file name.
sl@0
   115
@param  aPreferredSupplier
sl@0
   116
        If this is provided the list of matching plugins will be further searched for the latest version of a
sl@0
   117
        plugin supplied by supplier named.
sl@0
   118
@param  aSupportsCustomInterfaces
sl@0
   119
		Indicates whether the instantiated FormatDecode supports custom interfaces.
sl@0
   120
sl@0
   121
@return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
sl@0
   122
*/
sl@0
   123
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC16& aFileName, MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
sl@0
   124
	{
sl@0
   125
	CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
sl@0
   126
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
   127
	formatParams->SetMatchToFileNameL(aFileName);
sl@0
   128
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
   129
	if (aPreferredSupplier.Length() > 0)
sl@0
   130
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
   131
sl@0
   132
	// Instantiate this format
sl@0
   133
	CMMFFormatDecode* theChosenOne = 
sl@0
   134
		MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource, aSupportsCustomInterfaces);
sl@0
   135
sl@0
   136
	// Now clean up.
sl@0
   137
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
   138
sl@0
   139
	return theChosenOne;
sl@0
   140
	}
sl@0
   141
sl@0
   142
sl@0
   143
/**
sl@0
   144
Attempts to locate and instantiate a CMMFFormatDecode using data in a buffer.  The buffer is expected to contain 
sl@0
   145
header data (from a file, stream or descriptor).
sl@0
   146
sl@0
   147
Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
sl@0
   148
sl@0
   149
This function uses the match string as a resolver parameter. The format base class uses the match string
sl@0
   150
to find a match to the match string defined in the opaque_data field of the resource file. The
sl@0
   151
match string would typically be a signature for a particular format usually defined in the format header.
sl@0
   152
sl@0
   153
@param  aSourceHeader
sl@0
   154
        The data which is searched for matching signatures.
sl@0
   155
@param  aSource
sl@0
   156
        The controller's MDataSource and the source of the data for the format decode plugin.
sl@0
   157
@param  aPreferredSupplier
sl@0
   158
        If this is provided the list of matching plugins will be further searched for the latest version of a
sl@0
   159
        plugin supplied by the specified supplier.
sl@0
   160
sl@0
   161
@return If successful returns an instantiated CMMFFormatDecode from a plugin.  If not successful, leaves with KErrNotFound.
sl@0
   162
*/
sl@0
   163
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC8& aSourceHeader, MDataSource* aSource, const TDesC& aPreferredSupplier )
sl@0
   164
	{
sl@0
   165
	CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
sl@0
   166
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
   167
	formatParams->SetMatchToHeaderDataL(aSourceHeader);
sl@0
   168
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
   169
	if (aPreferredSupplier.Length() > 0)
sl@0
   170
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
   171
sl@0
   172
	// Instantiate this format
sl@0
   173
	CMMFFormatDecode* theChosenOne = 
sl@0
   174
		MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource);
sl@0
   175
sl@0
   176
	// Now clean up.
sl@0
   177
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
   178
sl@0
   179
	return theChosenOne;
sl@0
   180
	}
sl@0
   181
sl@0
   182
/**
sl@0
   183
Attempts to locate and instantiate a CMMFFormatDecode using data in a buffer.  The buffer is expected to contain 
sl@0
   184
header data (from a file, stream or descriptor).
sl@0
   185
sl@0
   186
Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
sl@0
   187
sl@0
   188
This function uses the match string as a resolver parameter. The format base class uses the match string
sl@0
   189
to find a match to the match string defined in the opaque_data field of the resource file. The
sl@0
   190
match string would typically be a signature for a particular format usually defined in the format header.
sl@0
   191
sl@0
   192
@param  aSourceHeader
sl@0
   193
        The data which is searched for matching signatures.
sl@0
   194
@param  aSource
sl@0
   195
        The controller's MDataSource and the source of the data for the format decode plugin.
sl@0
   196
@param  aPreferredSupplier
sl@0
   197
        If this is provided the list of matching plugins will be further searched for the latest version of a
sl@0
   198
        plugin supplied by the specified supplier.
sl@0
   199
@param  aSupportsCustomInterfaces
sl@0
   200
		Indicates whether the instantiated FormatDecode supports custom interfaces.
sl@0
   201
sl@0
   202
@return If successful returns an instantiated CMMFFormatDecode from a plugin.  If not successful, leaves with KErrNotFound.
sl@0
   203
*/
sl@0
   204
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( const TDesC8& aSourceHeader, MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
sl@0
   205
	{
sl@0
   206
	CMMFFormatDecodePluginSelectionParameters* pluginSelector = CMMFFormatDecodePluginSelectionParameters::NewLC();
sl@0
   207
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
   208
	formatParams->SetMatchToHeaderDataL(aSourceHeader);
sl@0
   209
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
   210
	if (aPreferredSupplier.Length() > 0)
sl@0
   211
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
   212
sl@0
   213
	// Instantiate this format
sl@0
   214
	CMMFFormatDecode* theChosenOne = 
sl@0
   215
		MMFFormatEcomUtilities::SelectFormatDecodePluginL(*pluginSelector, aSource, aSupportsCustomInterfaces);
sl@0
   216
sl@0
   217
	// Now clean up.
sl@0
   218
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
   219
sl@0
   220
	return theChosenOne;
sl@0
   221
	}
sl@0
   222
sl@0
   223
/**
sl@0
   224
Attempts to locate and instantiate a CMMFFormatDecode using data from MDataSource.  
sl@0
   225
sl@0
   226
The data is expected to contain header data (from a file, stream or descriptor). Signatures 
sl@0
   227
(supplied by the plugin registry information) are sought in aSource.
sl@0
   228
sl@0
   229
@param  aSource
sl@0
   230
        Header data. Must be derived from CMMFClip.
sl@0
   231
@param  aPreferredSupplier
sl@0
   232
        If this is provided, the list of matching plugins will be further searched for the latest version of a
sl@0
   233
        plugin supplied by the specified supplier.
sl@0
   234
sl@0
   235
@return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
sl@0
   236
*/
sl@0
   237
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( MDataSource* aSource, const TDesC& aPreferredSupplier )
sl@0
   238
	{
sl@0
   239
	// Read header data from aSource.  Call source header version
sl@0
   240
sl@0
   241
	if ( !( (aSource->DataSourceType() == KUidMmfFileSource ) || ( aSource->DataSourceType() == KUidMmfDescriptorSource) ) )
sl@0
   242
		User::Leave( KErrNotSupported ) ;
sl@0
   243
sl@0
   244
	CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
sl@0
   245
	CleanupStack::PushL( buffer ) ;
sl@0
   246
sl@0
   247
	aSource->SourcePrimeL();
sl@0
   248
	TCleanupItem srcCleanupItem(DoDataSourceStop, aSource);
sl@0
   249
	CleanupStack::PushL(srcCleanupItem);
sl@0
   250
sl@0
   251
	STATIC_CAST( CMMFClip*, aSource )->ReadBufferL( buffer, 0 ) ;
sl@0
   252
sl@0
   253
	CleanupStack::Pop();
sl@0
   254
	aSource->SourceStopL();
sl@0
   255
sl@0
   256
	// attempt to instantiate the format by header data
sl@0
   257
	// if this fails, try using the file extension
sl@0
   258
	CMMFFormatDecode* ret = NULL;
sl@0
   259
	TInt err, errFile;
sl@0
   260
	TRAP(err, ret = NewL( buffer->Data(), aSource, aPreferredSupplier ));
sl@0
   261
	if (err != KErrNone && aSource->DataSourceType() == KUidMmfFileSource)
sl@0
   262
		{
sl@0
   263
		CMMFFile* mmfFile = static_cast<CMMFFile*> (aSource);
sl@0
   264
		TRAP(errFile, ret = NewL( mmfFile->FullName(), aSource, aPreferredSupplier ));
sl@0
   265
		if (errFile == KErrNone)
sl@0
   266
			err = errFile;
sl@0
   267
		}
sl@0
   268
	User::LeaveIfError(err);
sl@0
   269
sl@0
   270
	CleanupStack::PopAndDestroy() ; // buffer
sl@0
   271
	return ret ;
sl@0
   272
	
sl@0
   273
	}
sl@0
   274
sl@0
   275
/**
sl@0
   276
Attempts to locate and instantiate a CMMFFormatDecode using data from MDataSource.  
sl@0
   277
sl@0
   278
The data is expected to contain header data (from a file, stream or descriptor). Signatures 
sl@0
   279
(supplied by the plugin registry information) are sought in aSource.
sl@0
   280
sl@0
   281
@param  aSource
sl@0
   282
        Header data. Must be derived from CMMFClip.
sl@0
   283
@param  aPreferredSupplier
sl@0
   284
        If this is provided, the list of matching plugins will be further searched for the latest version of a
sl@0
   285
        plugin supplied by the specified supplier.
sl@0
   286
@param  aSupportsCustomInterfaces
sl@0
   287
		Indicates whether the instantiated FormatDecode supports custom interfaces.
sl@0
   288
sl@0
   289
@return If successful returns an instantiated CMMFFormatDecode from a plugin. If not successful, leaves with KErrNotFound.
sl@0
   290
*/
sl@0
   291
EXPORT_C CMMFFormatDecode* CMMFFormatDecode::NewL( MDataSource* aSource, const TDesC& aPreferredSupplier, TBool& aSupportsCustomInterfaces )
sl@0
   292
	{
sl@0
   293
	// Read header data from aSource.  Call source header version
sl@0
   294
sl@0
   295
	if ( !( (aSource->DataSourceType() == KUidMmfFileSource ) || ( aSource->DataSourceType() == KUidMmfDescriptorSource) ) )
sl@0
   296
		User::Leave( KErrNotSupported ) ;
sl@0
   297
sl@0
   298
	CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
sl@0
   299
	CleanupStack::PushL( buffer ) ;
sl@0
   300
sl@0
   301
	aSource->SourcePrimeL();
sl@0
   302
	TCleanupItem srcCleanupItem(DoDataSourceStop, aSource);
sl@0
   303
	CleanupStack::PushL(srcCleanupItem);
sl@0
   304
sl@0
   305
	STATIC_CAST( CMMFClip*, aSource )->ReadBufferL( buffer, 0 ) ;
sl@0
   306
sl@0
   307
	CleanupStack::Pop();
sl@0
   308
	aSource->SourceStopL();
sl@0
   309
sl@0
   310
	// attempt to instantiate the format by header data
sl@0
   311
	// if this fails, try using the file extension
sl@0
   312
	CMMFFormatDecode* ret = NULL;
sl@0
   313
	TInt err, errFile;
sl@0
   314
	TRAP(err, ret = NewL( buffer->Data(), aSource, aPreferredSupplier, aSupportsCustomInterfaces ));
sl@0
   315
	if (err != KErrNone && aSource->DataSourceType() == KUidMmfFileSource)
sl@0
   316
		{
sl@0
   317
		CMMFFile* mmfFile = static_cast<CMMFFile*> (aSource);
sl@0
   318
		TRAP(errFile, ret = NewL( mmfFile->FullName(), aSource, aPreferredSupplier, aSupportsCustomInterfaces ));
sl@0
   319
		if (errFile == KErrNone)
sl@0
   320
			err = errFile;
sl@0
   321
		}
sl@0
   322
	User::LeaveIfError(err);
sl@0
   323
sl@0
   324
	CleanupStack::PopAndDestroy() ; // buffer
sl@0
   325
	return ret ;
sl@0
   326
	
sl@0
   327
	}
sl@0
   328
sl@0
   329
CMMFFormatEncode* CMMFFormatEncode::CreateFormatL(TUid aUid, MDataSink* aSink)
sl@0
   330
	{
sl@0
   331
	CMMFFormatEncode* s = REINTERPRET_CAST( CMMFFormatEncode*,
sl@0
   332
									REComSession::CreateImplementationL( aUid, _FOFF( CMMFFormatEncode, iDtor_ID_Key ),
sl@0
   333
									STATIC_CAST( TAny*, aSink ) ) ) ;
sl@0
   334
	s->iImplementationUid = aUid;
sl@0
   335
	return s;
sl@0
   336
	}
sl@0
   337
sl@0
   338
/**
sl@0
   339
Allocates and constructs an ECom format encode object.
sl@0
   340
sl@0
   341
@param  aUid
sl@0
   342
        The implementation UID.
sl@0
   343
@param  aSink
sl@0
   344
        The data sink.
sl@0
   345
sl@0
   346
@return If successful, returns the address of the format decode plugin object created. If not successful,
sl@0
   347
        leaves with KErrNotFound.
sl@0
   348
*/
sl@0
   349
EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( TUid aUid, MDataSink* aSink )
sl@0
   350
	{
sl@0
   351
	return CreateFormatL(aUid, aSink);
sl@0
   352
	}
sl@0
   353
sl@0
   354
/**
sl@0
   355
Attempts to locate and instantiate a CMMFFormatEncode using a filename or an extension.
sl@0
   356
sl@0
   357
Only the extension of the supplied file name is used. If no extension is supplied (ie. no dot is present)
sl@0
   358
the whole of the filename will be treated as the extension.
sl@0
   359
sl@0
   360
@param  aFileName
sl@0
   361
        File name of target file. May be extension only or may include the full path.
sl@0
   362
@param  aSink
sl@0
   363
		The data source.
sl@0
   364
@param  aPreferredSupplier
sl@0
   365
        If this is provided, the list of matching plugins will be further searched for the latest version of a
sl@0
   366
        plugin supplied by supplier named.
sl@0
   367
sl@0
   368
@return If successful, returns the address of the format decode plugin object created. If not successful,
sl@0
   369
        leaves with KErrNotFound.
sl@0
   370
*/
sl@0
   371
EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( const TDesC16& aFileName, MDataSink* aSink, const TDesC& aPreferredSupplier )
sl@0
   372
	{
sl@0
   373
	CMMFFormatEncodePluginSelectionParameters* pluginSelector = CMMFFormatEncodePluginSelectionParameters::NewLC();
sl@0
   374
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
   375
	formatParams->SetMatchToFileNameL(aFileName);
sl@0
   376
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
   377
	if (aPreferredSupplier.Length() > 0)
sl@0
   378
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
   379
sl@0
   380
	TUid chosenUid = MMFFormatEcomUtilities::SelectFormatPluginL(*pluginSelector);
sl@0
   381
sl@0
   382
	// Instantiate this format
sl@0
   383
	CMMFFormatEncode* theChosenOne = CreateFormatL(chosenUid, aSink);
sl@0
   384
sl@0
   385
	// Now clean up.
sl@0
   386
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
   387
sl@0
   388
	return theChosenOne;
sl@0
   389
	}
sl@0
   390
sl@0
   391
/**
sl@0
   392
Attempts to locate and instantiate a CMMFFormatEncode using data in the specified buffer.
sl@0
   393
sl@0
   394
The buffer is expected to contain header data (from a file, stream or descriptor).
sl@0
   395
Signatures (supplied by the plugin registry information) are sought in aSourceHeader.
sl@0
   396
sl@0
   397
@param  aSourceHeader
sl@0
   398
        The data which is searched for matching signatures.
sl@0
   399
@param  aSink
sl@0
   400
        The data sink.
sl@0
   401
@param  aPreferredSupplier
sl@0
   402
        If this is provided the list of matching plugins will be further searched for the latest version of a
sl@0
   403
        plugin supplied by supplier named.
sl@0
   404
sl@0
   405
@return If successful, returns the address of the format decode plugin object created. If not successful,
sl@0
   406
        leaves with KErrNotFound.
sl@0
   407
*/
sl@0
   408
EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( const TDesC8& aSourceHeader, MDataSink* aSink,  const TDesC& aPreferredSupplier )
sl@0
   409
	{
sl@0
   410
	CMMFFormatEncodePluginSelectionParameters* pluginSelector = CMMFFormatEncodePluginSelectionParameters::NewLC();
sl@0
   411
	CMMFFormatSelectionParameters* formatParams = CMMFFormatSelectionParameters::NewLC();
sl@0
   412
	formatParams->SetMatchToHeaderDataL(aSourceHeader);
sl@0
   413
	pluginSelector->SetRequiredFormatSupportL(*formatParams);
sl@0
   414
	if (aPreferredSupplier.Length() > 0)
sl@0
   415
		pluginSelector->SetPreferredSupplierL(aPreferredSupplier, CMMFPluginSelectionParameters::EOnlyPreferredSupplierPluginsReturned);
sl@0
   416
sl@0
   417
	TUid chosenUid = MMFFormatEcomUtilities::SelectFormatPluginL(*pluginSelector);
sl@0
   418
sl@0
   419
	// Instantiate this format
sl@0
   420
	CMMFFormatEncode* theChosenOne = CreateFormatL(chosenUid, aSink);
sl@0
   421
sl@0
   422
	// Now clean up.
sl@0
   423
	CleanupStack::PopAndDestroy(2);//formatParams, pluginSelector
sl@0
   424
sl@0
   425
	return theChosenOne;
sl@0
   426
	}
sl@0
   427
sl@0
   428
/**
sl@0
   429
Attempts to locate and instantiate a CMMFFormatEncode using data from aSink.
sl@0
   430
sl@0
   431
The data is expected to contain header data (from a file, stream or descriptor).
sl@0
   432
Signatures (supplied by the plugin registry information) are sought in the source header.
sl@0
   433
sl@0
   434
@param  aSink
sl@0
   435
        The header data. Must be derived from CMMFClip.
sl@0
   436
@param  aPreferredSupplier
sl@0
   437
        If this is provided, the list of matching plugins will be further searched for the latest version of a
sl@0
   438
        plugin supplied by supplier specified.
sl@0
   439
sl@0
   440
@return If successful, returns the address of the format decode plugin object created. If not successful,
sl@0
   441
        leaves with KErrNotFound.
sl@0
   442
*/
sl@0
   443
EXPORT_C CMMFFormatEncode* CMMFFormatEncode::NewL( MDataSink* aSink, const TDesC& aPreferredSupplier )
sl@0
   444
	{
sl@0
   445
	// Read header data from aSource.  Call source header version if there is header data, file name version otherwise.
sl@0
   446
sl@0
   447
	if ( !( (aSink->DataSinkType() == KUidMmfFileSink ) || ( aSink->DataSinkType() == KUidMmfDescriptorSink) ) )
sl@0
   448
		User::Leave( KErrNotSupported ) ;
sl@0
   449
sl@0
   450
	CMMFDataBuffer* buffer = CMMFDataBuffer::NewL(KMmfHeaderBufferSize) ;
sl@0
   451
	CleanupStack::PushL( buffer ) ;
sl@0
   452
sl@0
   453
	aSink->SinkPrimeL();
sl@0
   454
	TCleanupItem sinkCleanupItem(DoDataSinkStop, aSink);
sl@0
   455
	CleanupStack::PushL(sinkCleanupItem);
sl@0
   456
sl@0
   457
	STATIC_CAST( CMMFClip*, aSink )->ReadBufferL( buffer, 0 ) ;
sl@0
   458
sl@0
   459
	CleanupStack::Pop();
sl@0
   460
	aSink->SinkStopL();
sl@0
   461
sl@0
   462
	CMMFFormatEncode* ret = NULL ;  // set to null to avoid compiler warning.
sl@0
   463
	// Check for data in the buffer
sl@0
   464
	if (  buffer->BufferSize() != 0 )
sl@0
   465
		ret = NewL( buffer->Data(), aSink, aPreferredSupplier ) ;
sl@0
   466
	else if ( aSink->DataSinkType() == KUidMmfFileSink )
sl@0
   467
		ret = NewL( STATIC_CAST(CMMFFile*, aSink)->Extension(), aSink, aPreferredSupplier ) ;
sl@0
   468
	else
sl@0
   469
		User::Leave( KErrNotSupported ) ;
sl@0
   470
sl@0
   471
	CleanupStack::PopAndDestroy() ; // buffer
sl@0
   472
	return ret ;
sl@0
   473
	}
sl@0
   474
sl@0
   475
sl@0
   476
TUid MMFFormatEcomUtilities::SelectFormatPluginL(const CMMFFormatPluginSelectionParameters& aSelectParams)
sl@0
   477
	{
sl@0
   478
	RMMFFormatImplInfoArray pluginArray;
sl@0
   479
	CleanupResetAndDestroyPushL(pluginArray);
sl@0
   480
	aSelectParams.ListImplementationsL(pluginArray);
sl@0
   481
sl@0
   482
	// Just leave if no implementations were found
sl@0
   483
	if (pluginArray.Count() == 0)
sl@0
   484
		User::Leave(KErrNotSupported);
sl@0
   485
sl@0
   486
	// Return the uid of the first plugin in the list
sl@0
   487
	TUid ret = pluginArray[0]->Uid();
sl@0
   488
	CleanupStack::PopAndDestroy();//pluginArray
sl@0
   489
	return ret;
sl@0
   490
	}
sl@0
   491
sl@0
   492
/*
sl@0
   493
 * instantiate each format decode plugin in turn until we find one that works
sl@0
   494
 */
sl@0
   495
CMMFFormatDecode* MMFFormatEcomUtilities::SelectFormatDecodePluginL(const CMMFFormatPluginSelectionParameters& aSelectParams, MDataSource* aSource)
sl@0
   496
	{
sl@0
   497
	RMMFFormatImplInfoArray pluginArray;
sl@0
   498
	CleanupResetAndDestroyPushL(pluginArray);
sl@0
   499
	aSelectParams.ListImplementationsL(pluginArray);
sl@0
   500
sl@0
   501
	TInt pluginCount = pluginArray.Count();
sl@0
   502
	CMMFFormatDecode* theChosenOne = NULL;
sl@0
   503
sl@0
   504
	TInt err = KErrNotSupported;
sl@0
   505
	for (TInt n=0; n<pluginCount; n++)
sl@0
   506
		{
sl@0
   507
		// Try to instantiate this format
sl@0
   508
		TRAP(err, theChosenOne = CMMFFormatDecode::NewL(pluginArray[n]->Uid(), aSource));
sl@0
   509
		// Ensure OOM or any unexpected error is caught immediately 
sl@0
   510
		// i.e. don't try the next plugin
sl@0
   511
		if (err != KErrNotSupported && err != KErrCorrupt && err != KErrArgument)
sl@0
   512
			break;
sl@0
   513
		}
sl@0
   514
	User::LeaveIfError(err);
sl@0
   515
sl@0
   516
	CleanupStack::PopAndDestroy(&pluginArray);
sl@0
   517
sl@0
   518
	return theChosenOne;
sl@0
   519
	}
sl@0
   520
	
sl@0
   521
/*
sl@0
   522
 * instantiate each format decode plugin in turn until we find one that works
sl@0
   523
 */
sl@0
   524
CMMFFormatDecode* MMFFormatEcomUtilities::SelectFormatDecodePluginL(const CMMFFormatPluginSelectionParameters& aSelectParams, MDataSource* aSource, TBool& aSupportsCustomInterfaces)
sl@0
   525
	{
sl@0
   526
	RMMFFormatImplInfoArray pluginArray;
sl@0
   527
	CleanupResetAndDestroyPushL(pluginArray);
sl@0
   528
	aSelectParams.ListImplementationsL(pluginArray);
sl@0
   529
sl@0
   530
	TInt pluginCount = pluginArray.Count();
sl@0
   531
	CMMFFormatDecode* theChosenOne = NULL;
sl@0
   532
sl@0
   533
	TInt err = KErrNotSupported;
sl@0
   534
	CMMFFormatImplementationInformation* implInfo = NULL;
sl@0
   535
	for (TInt n=0; n<pluginCount; n++)
sl@0
   536
		{
sl@0
   537
		implInfo = pluginArray[n];
sl@0
   538
		// Try to instantiate this format
sl@0
   539
		TRAP(err, theChosenOne = CMMFFormatDecode::NewL(implInfo->Uid(), aSource));
sl@0
   540
		// Ensure OOM or any unexpected error is caught immediately 
sl@0
   541
		// i.e. don't try the next plugin
sl@0
   542
		if (err != KErrNotSupported && err != KErrCorrupt && err != KErrArgument)
sl@0
   543
			break;
sl@0
   544
		}
sl@0
   545
	User::LeaveIfError(err);
sl@0
   546
	aSupportsCustomInterfaces = implInfo->SupportsCustomInterfaces();
sl@0
   547
sl@0
   548
	CleanupStack::PopAndDestroy(&pluginArray);
sl@0
   549
sl@0
   550
	return theChosenOne;
sl@0
   551
	}
sl@0
   552