os/mm/mmhais/refacladapt/src/audiocodec/resourcedata.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
//audiocodec.cpp
sl@0
     2
sl@0
     3
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
// All rights reserved.
sl@0
     5
// This component and the accompanying materials are made available
sl@0
     6
// under the terms of "Eclipse Public License v1.0"
sl@0
     7
// which accompanies this distribution, and is available
sl@0
     8
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     9
//
sl@0
    10
// Initial Contributors:
sl@0
    11
// Nokia Corporation - initial contribution.
sl@0
    12
//
sl@0
    13
// Contributors:
sl@0
    14
//
sl@0
    15
// Description:
sl@0
    16
//
sl@0
    17
sl@0
    18
sl@0
    19
sl@0
    20
sl@0
    21
// INCLUDES
sl@0
    22
#include "resourcedata.h"
sl@0
    23
#include <barsc.h>
sl@0
    24
#include <a3f/a3f_trace_utils.h>
sl@0
    25
sl@0
    26
_LIT(KDC_RESOURCE_FILES_DIR, "\\resource\\a3f\\");
sl@0
    27
const TInt KAssumedResourceId =1;
sl@0
    28
sl@0
    29
const TSampleRateTableEntry KRateTableLookup[] = {
sl@0
    30
							{ 8000, EMMFSampleRate8000Hz },
sl@0
    31
							{ 11025, EMMFSampleRate11025Hz },
sl@0
    32
							{ 12000, EMMFSampleRate12000Hz },
sl@0
    33
							{ 16000, EMMFSampleRate16000Hz },
sl@0
    34
							{ 22050, EMMFSampleRate22050Hz },
sl@0
    35
							{ 24000, EMMFSampleRate24000Hz },
sl@0
    36
							{ 32000, EMMFSampleRate32000Hz },
sl@0
    37
							{ 44100, EMMFSampleRate44100Hz },
sl@0
    38
							{ 48000, EMMFSampleRate48000Hz },
sl@0
    39
							{ 64000, EMMFSampleRate64000Hz },
sl@0
    40
							{ 88200, EMMFSampleRate88200Hz },
sl@0
    41
							{ 96000, EMMFSampleRate96000Hz },
sl@0
    42
						};
sl@0
    43
sl@0
    44
const TAudioModeTableEntry KModeTableLookup[] = {
sl@0
    45
							{ EMMFMono, {KA3FModeMonoValue} },
sl@0
    46
							{ EMMFStereo, {KA3FModeStereoNonInterleavedValue} },
sl@0
    47
							};
sl@0
    48
sl@0
    49
const TInt KMaxSampleRateIndex = 11;
sl@0
    50
sl@0
    51
// ============================ MEMBER FUNCTIONS ===============================
sl@0
    52
sl@0
    53
CResourceData::CResourceData()
sl@0
    54
	{
sl@0
    55
	TRACE_CREATE();
sl@0
    56
	}
sl@0
    57
sl@0
    58
// -----------------------------------------------------------------------------
sl@0
    59
// CResourceData::NewL
sl@0
    60
// Two-phased constructor.
sl@0
    61
// -----------------------------------------------------------------------------
sl@0
    62
//
sl@0
    63
CResourceData* CResourceData::NewL(TUid aResourceUid)
sl@0
    64
	{
sl@0
    65
	DP_STATIC_CONTEXT( CResourceData::NewL *CD0*, CtxDevSound, DPAPI);
sl@0
    66
	DP1_IN("aResourceUid = 0x%x", aResourceUid);
sl@0
    67
sl@0
    68
	CResourceData* obj = NULL;
sl@0
    69
	obj = new ( ELeave ) CResourceData;
sl@0
    70
	CleanupStack::PushL( obj );
sl@0
    71
	obj->ConstructL( aResourceUid );
sl@0
    72
	CleanupStack::Pop(obj);
sl@0
    73
sl@0
    74
	DP0_RET(obj, "obj = 0x%x" );
sl@0
    75
	}
sl@0
    76
sl@0
    77
// -----------------------------------------------------------------------------
sl@0
    78
// CResourceData::~CResourceData
sl@0
    79
// Destructor.
sl@0
    80
// -----------------------------------------------------------------------------
sl@0
    81
//
sl@0
    82
CResourceData::~CResourceData()
sl@0
    83
	{
sl@0
    84
	DP_CONTEXT(CResourceData::~CResourceData *CD1*, CtxDevSound, DPLOCAL);
sl@0
    85
	DP_IN();
sl@0
    86
	delete iResourceData;
sl@0
    87
	DP_OUT();
sl@0
    88
	}
sl@0
    89
sl@0
    90
sl@0
    91
// -----------------------------------------------------------------------------
sl@0
    92
// CResourceData::GetSModes
sl@0
    93
// Reads the capabilities data.
sl@0
    94
// -----------------------------------------------------------------------------
sl@0
    95
//
sl@0
    96
TInt CResourceData::GetSModes(TMode aMode, RArray<TUid>& aModeConfig)
sl@0
    97
	{
sl@0
    98
	DP_CONTEXT( CResourceData::GetSampleModes *CD1*, CtxDevSound, DPLOCAL );
sl@0
    99
	DP1_IN( "TMode aMode = 0x%x", aMode );
sl@0
   100
	TInt ret = SeekToMode(aMode);
sl@0
   101
	if (ret != KErrNone)
sl@0
   102
		{
sl@0
   103
		DP0_RET(ret, "%d");
sl@0
   104
		}
sl@0
   105
sl@0
   106
	// capability data size
sl@0
   107
	TInt capDataSize = iResourceReader.ReadInt16();
sl@0
   108
sl@0
   109
	// sanity check for cap data size
sl@0
   110
	if (iResourceReader.Ptr() > iResourceDataEnd-capDataSize)
sl@0
   111
		{
sl@0
   112
		DP0( DLERR, "Pointer mismatch with cap data" );
sl@0
   113
		DP0_RET(KErrCorrupt, "KErrCorrupt" );
sl@0
   114
		}
sl@0
   115
sl@0
   116
	//Getting new Capabilities so clean the array.
sl@0
   117
	aModeConfig.Reset();
sl@0
   118
sl@0
   119
	TInt tempSampleRate = iResourceReader.ReadInt32();
sl@0
   120
	TInt tempEnconding = iResourceReader.ReadInt32();
sl@0
   121
	TInt tempMode = iResourceReader.ReadInt32();
sl@0
   122
sl@0
   123
	TInt err(KErrNone);
sl@0
   124
sl@0
   125
	for (TUint i=0; i<=KMaxModeIndex; i++)
sl@0
   126
		{
sl@0
   127
		if((KModeTableLookup[i].iAudioModeValue) & tempMode)
sl@0
   128
			{
sl@0
   129
			err = aModeConfig.Append(KModeTableLookup[i].iAudioMode);
sl@0
   130
			if (err != KErrNone)
sl@0
   131
				{
sl@0
   132
				break;
sl@0
   133
				}
sl@0
   134
			}
sl@0
   135
		}
sl@0
   136
sl@0
   137
	DP0_RET(err, "");
sl@0
   138
	}
sl@0
   139
sl@0
   140
sl@0
   141
// -----------------------------------------------------------------------------
sl@0
   142
// CResourceData::GetSSampleRates
sl@0
   143
// Reads the capabilities data.
sl@0
   144
// -----------------------------------------------------------------------------
sl@0
   145
//
sl@0
   146
TInt CResourceData::GetSSampleRates(TMode aMode, RArray<TInt>& aSampleRateConfig)
sl@0
   147
	{
sl@0
   148
	DP_CONTEXT( CResourceData::GetSSampleRates *CD1*, CtxDevSound, DPLOCAL );
sl@0
   149
	DP1_IN( "TMode aMode = 0x%x", aMode );
sl@0
   150
	//TODO to be change every hwdevice is either decode or encode
sl@0
   151
	//By the moment The same resource file has both modes
sl@0
   152
	TInt ret = SeekToMode(aMode);
sl@0
   153
	if (ret != KErrNone)
sl@0
   154
		{
sl@0
   155
		DP0_RET(ret, "%d");
sl@0
   156
		}
sl@0
   157
sl@0
   158
	// capability data size
sl@0
   159
	TInt capDataSize = iResourceReader.ReadInt16();
sl@0
   160
sl@0
   161
	// sanity check for cap data size
sl@0
   162
	if (iResourceReader.Ptr() > iResourceDataEnd-capDataSize)
sl@0
   163
		{
sl@0
   164
		DP0( DLERR, "Pointer mismatch with cap data" );
sl@0
   165
		DP0_RET(KErrCorrupt, "KErrCorrupt" );
sl@0
   166
		}
sl@0
   167
sl@0
   168
	//Getting new Capabilities so clean the array.
sl@0
   169
	aSampleRateConfig.Reset();
sl@0
   170
sl@0
   171
	TInt tempSampleRate = iResourceReader.ReadInt32();
sl@0
   172
	TInt err(KErrNone);
sl@0
   173
sl@0
   174
	for (TUint i=0; i<=KMaxSampleRateIndex; i++)
sl@0
   175
		{
sl@0
   176
		if(KRateTableLookup[i].iSampleRate & tempSampleRate)
sl@0
   177
			{
sl@0
   178
			err = aSampleRateConfig.Append(KRateTableLookup[i].iSampleRateValue);
sl@0
   179
			if (err != KErrNone)
sl@0
   180
				{
sl@0
   181
				break;
sl@0
   182
				}
sl@0
   183
			}
sl@0
   184
		}
sl@0
   185
sl@0
   186
	DP0_RET(err, "");
sl@0
   187
	}
sl@0
   188
sl@0
   189
// -----------------------------------------------------------------------------
sl@0
   190
// CResourceData::ConstructL
sl@0
   191
// Symbian 2nd phase constructor can leave.
sl@0
   192
// Reads the resource file for the correct UID.
sl@0
   193
// -----------------------------------------------------------------------------
sl@0
   194
//
sl@0
   195
void CResourceData::ConstructL(TUid aResourceUid)
sl@0
   196
	{
sl@0
   197
	DP_CONTEXT( CResourceData::ConstructL *CD1*, CtxDevSound, DPLOCAL );
sl@0
   198
	DP1_IN( "aResourceUid = 0x%x", aResourceUid );
sl@0
   199
	// Open a file server session.
sl@0
   200
	RFs fs;
sl@0
   201
	User::LeaveIfError( fs.Connect() );
sl@0
   202
	CleanupClosePushL( fs );
sl@0
   203
sl@0
   204
	TBuf16<KResFileNameLength> fileName;
sl@0
   205
	TBuf16<KResFileNameAndPathLength> pathAndFileName (KDrive); // Z:
sl@0
   206
	fileName.Format(KFileNameFormat,aResourceUid.iUid); // HwDev0x<UID>
sl@0
   207
	pathAndFileName.Append(KDC_RESOURCE_FILES_DIR); /*  \\resource\\ */
sl@0
   208
	pathAndFileName.Append(fileName); // HwDevxxx.rsc
sl@0
   209
	pathAndFileName.ZeroTerminate();
sl@0
   210
	HBufC16* fileNamePtr = pathAndFileName.AllocL();
sl@0
   211
	CleanupStack::PushL( fileNamePtr );
sl@0
   212
	RResourceFile rscFile;
sl@0
   213
	rscFile.OpenL(fs, fileNamePtr->Des());
sl@0
   214
	CleanupClosePushL(rscFile);
sl@0
   215
	// read the resource data
sl@0
   216
	iResourceData = rscFile.AllocReadL(KAssumedResourceId);
sl@0
   217
	// initialize the reader
sl@0
   218
	iResourceReader.SetBuffer( iResourceData );
sl@0
   219
	iResourceUid =  aResourceUid;
sl@0
   220
	CleanupStack::PopAndDestroy( 3, &fs ); // fs, fileNamePtr,rscFile
sl@0
   221
	DP_OUT();
sl@0
   222
	}
sl@0
   223
sl@0
   224
// -----------------------------------------------------------------------------
sl@0
   225
// CResourceData::SeekToMode
sl@0
   226
// Utility method for seeking resource to the correct mode. Leaves with
sl@0
   227
// KErrNotFound if mode is not found in the opened resource.
sl@0
   228
// -----------------------------------------------------------------------------
sl@0
   229
//
sl@0
   230
TInt CResourceData::SeekToMode(TMode aMode)
sl@0
   231
	{
sl@0
   232
	DP_CONTEXT( CResourceData::SeekToMode *CD1*, CtxDevSound, DPLOCAL );
sl@0
   233
	DP1_IN( "aMode = 0x%x", aMode );
sl@0
   234
	// set buffer position to beginning
sl@0
   235
	iResourceReader.SetBuffer(iResourceData);
sl@0
   236
	// read the resource uid
sl@0
   237
	TUid resourceUid = {iResourceReader.ReadInt32()};
sl@0
   238
	if (resourceUid != iResourceUid)
sl@0
   239
		{
sl@0
   240
		DP2( DLERR, "Mismatching resource uids resourceUid = %x iResourceUid = %x",resourceUid.iUid,iResourceUid.iUid );
sl@0
   241
		DP0_RET(KErrCorrupt, "KErrCorrupt" );
sl@0
   242
		}
sl@0
   243
	// read the number of mode-entries in the resource
sl@0
   244
	TInt modeCount = iResourceReader.ReadInt16();
sl@0
   245
	for (TInt modeIndex = 0; modeIndex < modeCount; modeIndex++)
sl@0
   246
		{
sl@0
   247
		// read the mode number
sl@0
   248
		TMode mode = (TMode) iResourceReader.ReadInt32();
sl@0
   249
		if (mode == aMode)
sl@0
   250
			{
sl@0
   251
			DP0_RET(KErrNone, "" );
sl@0
   252
			}
sl@0
   253
		// capability data size
sl@0
   254
		TInt capDataSize = iResourceReader.ReadInt16();
sl@0
   255
sl@0
   256
		// skip capability data
sl@0
   257
		if (iResourceReader.Ptr() > iResourceDataEnd-capDataSize)
sl@0
   258
			{
sl@0
   259
			DP0( DLERR, "Pointer mismatch with cap data" );
sl@0
   260
			DP0_RET(KErrCorrupt, "KErrCorrupt" );
sl@0
   261
			}
sl@0
   262
		iResourceReader.Advance( capDataSize );
sl@0
   263
		}
sl@0
   264
	// not found
sl@0
   265
	DP1( DLERR, "Mode 0x%x not found?", aMode );
sl@0
   266
	DP0_RET(KErrNotFound, "KErrNotFound" );
sl@0
   267
	}
sl@0
   268
sl@0
   269