os/mm/mmlibs/mmfw/src/Client/Audio/mmfclientaudioinputstream.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
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 "mmfclientaudioinputstream.h"
sl@0
    17
#include "mmfclientaudiostreamutils.h"
sl@0
    18
#include <mmf/server/devsoundstandardcustominterfaces.h>
sl@0
    19
#include <mmf/common/mmfpaniccodes.h>
sl@0
    20
#include "MmfFifo.h"
sl@0
    21
#include <mdaaudioinputstream.h>
sl@0
    22
sl@0
    23
const TInt KMicroSecsInOneSec = 1000000;
sl@0
    24
sl@0
    25
sl@0
    26
enum TMmfAudioInputPanic
sl@0
    27
	{
sl@0
    28
	EAudioInputPanicNotSupported
sl@0
    29
	};
sl@0
    30
sl@0
    31
_LIT(KAudioInputStreamCategory, "CMMFMdaAudioInputStream");
sl@0
    32
LOCAL_C void Panic(const TMmfAudioInputPanic aPanic)
sl@0
    33
	{
sl@0
    34
	User::Panic(KAudioInputStreamCategory, aPanic);
sl@0
    35
	}
sl@0
    36
sl@0
    37
/**
sl@0
    38
 *
sl@0
    39
 * Static NewL
sl@0
    40
 *
sl@0
    41
 * @return CMdaAudioInputStream*
sl@0
    42
 *
sl@0
    43
 */
sl@0
    44
EXPORT_C CMdaAudioInputStream* CMdaAudioInputStream::NewL(MMdaAudioInputStreamCallback& aCallback)
sl@0
    45
	{
sl@0
    46
	return NewL(aCallback, EMdaPriorityNormal, EMdaPriorityPreferenceTimeAndQuality);
sl@0
    47
	}
sl@0
    48
sl@0
    49
/**
sl@0
    50
 *
sl@0
    51
 * Static NewL
sl@0
    52
 *
sl@0
    53
 * @return CMdaAudioInputStream*
sl@0
    54
 *
sl@0
    55
 */
sl@0
    56
EXPORT_C CMdaAudioInputStream* CMdaAudioInputStream::NewL(MMdaAudioInputStreamCallback& aCallback, TInt aPriority, TInt aPref)
sl@0
    57
	{
sl@0
    58
	CMdaAudioInputStream* self = new(ELeave) CMdaAudioInputStream();
sl@0
    59
	CleanupStack::PushL(self);
sl@0
    60
	self->iProperties = CMMFMdaAudioInputStream::NewL(aCallback, aPriority, aPref);
sl@0
    61
	CleanupStack::Pop(self);
sl@0
    62
	return self;
sl@0
    63
	}
sl@0
    64
sl@0
    65
CMdaAudioInputStream::CMdaAudioInputStream()
sl@0
    66
	{
sl@0
    67
	}
sl@0
    68
sl@0
    69
CMdaAudioInputStream::~CMdaAudioInputStream()
sl@0
    70
	{
sl@0
    71
	if(iProperties)
sl@0
    72
		{
sl@0
    73
		iProperties->ShutDown();	
sl@0
    74
		}
sl@0
    75
	
sl@0
    76
	delete iProperties;
sl@0
    77
	}
sl@0
    78
sl@0
    79
EXPORT_C void CMdaAudioInputStream::SetAudioPropertiesL(TInt aSampleRate, TInt aChannels)
sl@0
    80
	{
sl@0
    81
	ASSERT(iProperties);
sl@0
    82
	iProperties->SetAudioPropertiesL(aSampleRate, aChannels);
sl@0
    83
	}
sl@0
    84
sl@0
    85
EXPORT_C void CMdaAudioInputStream::Open(TMdaPackage* aSettings)
sl@0
    86
	{
sl@0
    87
	ASSERT(iProperties);
sl@0
    88
	iProperties->Open(aSettings);
sl@0
    89
	}
sl@0
    90
sl@0
    91
EXPORT_C void CMdaAudioInputStream::SetGain(TInt aNewGain)
sl@0
    92
	{
sl@0
    93
	ASSERT(iProperties);
sl@0
    94
	iProperties->SetGain(aNewGain);
sl@0
    95
	}
sl@0
    96
sl@0
    97
EXPORT_C TInt CMdaAudioInputStream::Gain()  const
sl@0
    98
	{
sl@0
    99
	ASSERT(iProperties);
sl@0
   100
	return iProperties->Gain();
sl@0
   101
	}
sl@0
   102
sl@0
   103
EXPORT_C TInt CMdaAudioInputStream::MaxGain()  const
sl@0
   104
	{
sl@0
   105
	ASSERT(iProperties);
sl@0
   106
	return iProperties->MaxGain();
sl@0
   107
	}
sl@0
   108
sl@0
   109
EXPORT_C void CMdaAudioInputStream::SetBalanceL(TInt aBalance)
sl@0
   110
	{
sl@0
   111
	ASSERT(iProperties);
sl@0
   112
	iProperties->SetBalanceL(aBalance);
sl@0
   113
	}
sl@0
   114
sl@0
   115
EXPORT_C TInt CMdaAudioInputStream::GetBalanceL() const
sl@0
   116
	{
sl@0
   117
	ASSERT(iProperties);
sl@0
   118
	return iProperties->GetBalanceL();
sl@0
   119
	}
sl@0
   120
sl@0
   121
EXPORT_C void CMdaAudioInputStream::SetPriority(TInt aPriority, TInt aPref)
sl@0
   122
	{
sl@0
   123
	ASSERT(iProperties);
sl@0
   124
	iProperties->SetPriority(aPriority, aPref);
sl@0
   125
	}
sl@0
   126
sl@0
   127
EXPORT_C void CMdaAudioInputStream::ReadL(TDes8& aData)
sl@0
   128
	{
sl@0
   129
	ASSERT(iProperties);
sl@0
   130
	iProperties->ReadL(aData);
sl@0
   131
	}
sl@0
   132
sl@0
   133
EXPORT_C void CMdaAudioInputStream::Stop()
sl@0
   134
	{
sl@0
   135
	ASSERT(iProperties);
sl@0
   136
	iProperties->Stop();
sl@0
   137
	}
sl@0
   138
sl@0
   139
EXPORT_C void CMdaAudioInputStream::RequestStop()
sl@0
   140
	{
sl@0
   141
	ASSERT(iProperties);
sl@0
   142
	iProperties->RequestStop();
sl@0
   143
	}
sl@0
   144
sl@0
   145
EXPORT_C const TTimeIntervalMicroSeconds& CMdaAudioInputStream::Position()
sl@0
   146
	{
sl@0
   147
	ASSERT(iProperties);
sl@0
   148
	return iProperties->Position();
sl@0
   149
	}
sl@0
   150
sl@0
   151
EXPORT_C TInt CMdaAudioInputStream::GetBytes()
sl@0
   152
	{
sl@0
   153
	ASSERT(iProperties);
sl@0
   154
	return iProperties->GetBytes();
sl@0
   155
	}
sl@0
   156
sl@0
   157
EXPORT_C void CMdaAudioInputStream::SetSingleBufferMode(TBool aSingleMode)
sl@0
   158
	{
sl@0
   159
	ASSERT(iProperties);
sl@0
   160
	iProperties->SetSingleBufferMode(aSingleMode);
sl@0
   161
	}
sl@0
   162
sl@0
   163
/**
sl@0
   164
sl@0
   165
*/
sl@0
   166
EXPORT_C void CMdaAudioInputStream::SetDataTypeL(TFourCC aAudioType)
sl@0
   167
	{
sl@0
   168
	ASSERT(iProperties);
sl@0
   169
	iProperties->SetDataTypeL(aAudioType);
sl@0
   170
	}	
sl@0
   171
sl@0
   172
/**
sl@0
   173
sl@0
   174
*/
sl@0
   175
EXPORT_C TFourCC CMdaAudioInputStream::DataType() const
sl@0
   176
	{
sl@0
   177
	ASSERT(iProperties);
sl@0
   178
	return iProperties->DataType();
sl@0
   179
	}
sl@0
   180
	
sl@0
   181
	
sl@0
   182
EXPORT_C TInt CMdaAudioInputStream::BitRateL() const
sl@0
   183
	{
sl@0
   184
	ASSERT(iProperties);
sl@0
   185
	return iProperties->BitRateL();
sl@0
   186
	}
sl@0
   187
	
sl@0
   188
EXPORT_C void CMdaAudioInputStream::SetBitRateL(TInt aBitRate)
sl@0
   189
	{
sl@0
   190
	ASSERT(iProperties);
sl@0
   191
	iProperties->SetBitRateL(aBitRate);
sl@0
   192
	}
sl@0
   193
sl@0
   194
	
sl@0
   195
EXPORT_C void CMdaAudioInputStream::GetSupportedBitRatesL(RArray<TInt>& aSupportedBitRates)
sl@0
   196
	{
sl@0
   197
	ASSERT(iProperties);
sl@0
   198
	iProperties->GetSupportedBitRatesL(aSupportedBitRates);
sl@0
   199
	}
sl@0
   200
sl@0
   201
EXPORT_C TAny* CMdaAudioInputStream::CustomInterface(TUid aInterfaceId)
sl@0
   202
	{
sl@0
   203
	ASSERT(iProperties);
sl@0
   204
	return iProperties->CustomInterface(aInterfaceId);
sl@0
   205
	}
sl@0
   206
sl@0
   207
//
sl@0
   208
sl@0
   209
CMMFMdaAudioInputStream* CMMFMdaAudioInputStream::NewL(MMdaAudioInputStreamCallback& aCallback)
sl@0
   210
	{
sl@0
   211
	return CMMFMdaAudioInputStream::NewL(aCallback, EMdaPriorityNormal, EMdaPriorityPreferenceTimeAndQuality);
sl@0
   212
	}
sl@0
   213
sl@0
   214
CMMFMdaAudioInputStream* CMMFMdaAudioInputStream::NewL(MMdaAudioInputStreamCallback& aCallback, TInt aPriority, TInt aPref)
sl@0
   215
	{
sl@0
   216
	CMMFMdaAudioInputStream* self = new(ELeave) CMMFMdaAudioInputStream(aCallback);
sl@0
   217
	CleanupStack::PushL(self);
sl@0
   218
	self->ConstructL(aPriority, aPref);
sl@0
   219
	CleanupStack::Pop(self);
sl@0
   220
	return self;
sl@0
   221
	}
sl@0
   222
/**
sl@0
   223
 *
sl@0
   224
 * Construct
sl@0
   225
 *
sl@0
   226
 * @param	"MMdaAudioInputStreamCallback&"
sl@0
   227
 *			a reference to MMdaAudioInputStreamCallback
sl@0
   228
 *			a perference value
sl@0
   229
 *
sl@0
   230
 */
sl@0
   231
CMMFMdaAudioInputStream::CMMFMdaAudioInputStream(MMdaAudioInputStreamCallback& aCallback)
sl@0
   232
	: iCallback(aCallback), iStorageItem (NULL, 0), iBufferPtr(NULL, 0)
sl@0
   233
	// Depending on zero for construction (i.e. attribute of CBase)
sl@0
   234
	//   iSingleBuffer (EFalse)
sl@0
   235
	//   iState(EStopped)
sl@0
   236
	//   iIsOpened(EFalse)
sl@0
   237
	//   iCallbackMade(EFalse)
sl@0
   238
	//   iAudioDataStored(EFalse)
sl@0
   239
	{
sl@0
   240
	iDataTypeCode.Set(TFourCC(' ','P','1','6'));
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
 *
sl@0
   245
 *	Second phase constructor
sl@0
   246
 *
sl@0
   247
 */
sl@0
   248
void CMMFMdaAudioInputStream::ConstructL(TInt aPriority, TInt aPref)
sl@0
   249
	{
sl@0
   250
	iDevSound = CMMFDevSound::NewL();
sl@0
   251
	SetPriority(aPriority, aPref);
sl@0
   252
	iFifo = new(ELeave) CMMFFifo<TDes8>();
sl@0
   253
	iActiveCallback = new(ELeave) CActiveCallback(iCallback);
sl@0
   254
	iActiveSchedulerWait = new(ELeave) CActiveSchedulerWait;
sl@0
   255
	}
sl@0
   256
sl@0
   257
/**
sl@0
   258
 *
sl@0
   259
 *	Destructor
sl@0
   260
 *
sl@0
   261
 */
sl@0
   262
CMMFMdaAudioInputStream::~CMMFMdaAudioInputStream()
sl@0
   263
	{
sl@0
   264
	delete iFifo;
sl@0
   265
	delete iDevSound;
sl@0
   266
	delete iActiveCallback;
sl@0
   267
	delete iActiveSchedulerWait;
sl@0
   268
	}
sl@0
   269
sl@0
   270
/**
sl@0
   271
 *
sl@0
   272
 *  Set audio input stream properties	
sl@0
   273
 *
sl@0
   274
 *	@param	"TInt aSampleRate"	
sl@0
   275
 *			a specified priority value
sl@0
   276
 *	@param	"TInt aChannels"		
sl@0
   277
 *			a specified preference value
sl@0
   278
 *
sl@0
   279
 */
sl@0
   280
void CMMFMdaAudioInputStream::SetAudioPropertiesL(TInt aSampleRate, TInt aChannels)
sl@0
   281
	{
sl@0
   282
	if (iIsOpenState == EIsOpen)
sl@0
   283
		{
sl@0
   284
		RealSetAudioPropertiesL(aSampleRate, aChannels);
sl@0
   285
		}
sl@0
   286
	else
sl@0
   287
		{
sl@0
   288
		// cache for application later
sl@0
   289
		iSettings.iSampleRate = aSampleRate;
sl@0
   290
		iSettings.iChannels = aChannels;
sl@0
   291
		iAudioDataStored = ETrue;
sl@0
   292
		}
sl@0
   293
	}
sl@0
   294
	
sl@0
   295
void CMMFMdaAudioInputStream::RealSetAudioPropertiesL(TInt aSampleRate, TInt aChannels)
sl@0
   296
	{
sl@0
   297
	TMMFCapabilities capabilities = iDevSound->Config();
sl@0
   298
	capabilities.iChannels = StreamUtils::MapChannelsMdaToMMFL(aChannels);
sl@0
   299
	capabilities.iRate = StreamUtils::MapSampleRateMdaToMMFL(aSampleRate);
sl@0
   300
	iDevSound->SetConfigL(capabilities);
sl@0
   301
	}
sl@0
   302
sl@0
   303
/**
sl@0
   304
 *
sl@0
   305
 *  Open a audio ouptut stream	
sl@0
   306
 *
sl@0
   307
 *	@param	"TMdaPackage* Settings"	
sl@0
   308
 *			a pointer point to TMdaPackage
sl@0
   309
 *
sl@0
   310
 */
sl@0
   311
void CMMFMdaAudioInputStream::Open(TMdaPackage* aSettings)
sl@0
   312
	{
sl@0
   313
	iIsOpenState = EIsOpening;
sl@0
   314
	//store aSettings
sl@0
   315
	if (aSettings && (aSettings->Type().iUid == KUidMdaDataTypeSettingsDefine))
sl@0
   316
		{
sl@0
   317
		TMdaAudioDataSettings* tmpSettings = STATIC_CAST(TMdaAudioDataSettings*, aSettings);
sl@0
   318
		iSettings = *tmpSettings;
sl@0
   319
		iAudioDataStored = ETrue;
sl@0
   320
		}
sl@0
   321
	TRAPD(err, iDevSound->InitializeL(*this, iDataTypeCode, EMMFStateRecording));
sl@0
   322
	if (err != KErrNone)
sl@0
   323
		{
sl@0
   324
		// Signal for the MaiscOpenComplete callback to be called asynchronously
sl@0
   325
		iActiveCallback->Signal(err);
sl@0
   326
		iIsOpenState = EIsNotOpen;
sl@0
   327
		iAudioDataStored = EFalse; // reset - if was set we throw away due to failure
sl@0
   328
		}
sl@0
   329
	}
sl@0
   330
sl@0
   331
/**
sl@0
   332
 *
sl@0
   333
 *  To get the maximum gain level	
sl@0
   334
 *
sl@0
   335
 *	@return	"TInt"	
sl@0
   336
 *			the maximum gain value in integer
sl@0
   337
 *
sl@0
   338
 */
sl@0
   339
TInt CMMFMdaAudioInputStream::MaxGain() const
sl@0
   340
	{
sl@0
   341
	return iDevSound->MaxGain();
sl@0
   342
	}
sl@0
   343
sl@0
   344
/**
sl@0
   345
 *
sl@0
   346
 *  To get the current gain level	
sl@0
   347
 *
sl@0
   348
 *	@return	"TInt"	
sl@0
   349
 *			the current gain value in integer
sl@0
   350
 *
sl@0
   351
 */
sl@0
   352
TInt CMMFMdaAudioInputStream::Gain() const
sl@0
   353
	{
sl@0
   354
	return iDevSound->Gain();
sl@0
   355
	} 
sl@0
   356
sl@0
   357
/**
sl@0
   358
 *
sl@0
   359
 *  Set audio input stream gain to the specified value
sl@0
   360
 *
sl@0
   361
 *	@param	"TInt aGain"	
sl@0
   362
 *			a specified gain value
sl@0
   363
 *
sl@0
   364
 */
sl@0
   365
void CMMFMdaAudioInputStream::SetGain(TInt aGain)
sl@0
   366
	{
sl@0
   367
	iDevSound->SetGain(aGain);
sl@0
   368
	}
sl@0
   369
sl@0
   370
/**
sl@0
   371
 *
sl@0
   372
 *  Set audio input stream balance	
sl@0
   373
 *
sl@0
   374
 *	@param	"TInt aBalance"	
sl@0
   375
 *			a specified balance value
sl@0
   376
 *
sl@0
   377
 */
sl@0
   378
void CMMFMdaAudioInputStream::SetBalanceL(TInt aBalance)
sl@0
   379
	{
sl@0
   380
	// test and clip balance to min / max range [-100 <-> 100]
sl@0
   381
	// clip rather than leave as this isn't a leaving function
sl@0
   382
	if (aBalance < KMMFBalanceMaxLeft) aBalance = KMMFBalanceMaxLeft;
sl@0
   383
	if (aBalance > KMMFBalanceMaxRight) aBalance = KMMFBalanceMaxRight;
sl@0
   384
sl@0
   385
	// separate out left and right balance
sl@0
   386
	TInt left  = 0;
sl@0
   387
	TInt right = 0;
sl@0
   388
	StreamUtils::CalculateLeftRightBalance( left, right, aBalance );
sl@0
   389
sl@0
   390
	// send the balance to SoundDevice
sl@0
   391
	iDevSound->SetRecordBalanceL(left, right);
sl@0
   392
	}
sl@0
   393
sl@0
   394
/**
sl@0
   395
 *
sl@0
   396
 *  To get the current balance value.This function may not return the same value 
sl@0
   397
 *	as passed to SetBalanceL depending on the internal implementation in 
sl@0
   398
 *	the underlying components.
sl@0
   399
 *
sl@0
   400
 *	@return	"TInt"	
sl@0
   401
 *			the current balance value in integer
sl@0
   402
 *
sl@0
   403
 */
sl@0
   404
TInt CMMFMdaAudioInputStream::GetBalanceL() const
sl@0
   405
	{
sl@0
   406
	TInt rightBalance = 0;
sl@0
   407
	TInt leftBalance  = 0;
sl@0
   408
	iDevSound->GetRecordBalanceL(leftBalance, rightBalance);
sl@0
   409
	TInt balance  = 0;
sl@0
   410
	StreamUtils::CalculateBalance( balance, leftBalance, rightBalance );
sl@0
   411
	return balance;
sl@0
   412
	}
sl@0
   413
sl@0
   414
/**
sl@0
   415
 *
sl@0
   416
 *  Set audio input stream priority	
sl@0
   417
 *
sl@0
   418
 *	@param	"TInt aPriority"	
sl@0
   419
 *			a specified priority value
sl@0
   420
 *	@param	"TInt aPref"		
sl@0
   421
 *			a specified preference value
sl@0
   422
 *
sl@0
   423
 */
sl@0
   424
void CMMFMdaAudioInputStream::SetPriority(TInt aPriority, TInt aPref)
sl@0
   425
	{
sl@0
   426
	TMMFPrioritySettings settings;
sl@0
   427
	settings.iPriority = aPriority;
sl@0
   428
	settings.iPref = aPref;
sl@0
   429
	iDevSound->SetPrioritySettings(settings);
sl@0
   430
	}
sl@0
   431
sl@0
   432
sl@0
   433
/**
sl@0
   434
 *
sl@0
   435
 *  To read data from input stream 	
sl@0
   436
 *
sl@0
   437
 *	@param	"TDesC8& aData"	
sl@0
   438
 *			a stream data 
sl@0
   439
 * 
sl@0
   440
 *  @capability	UserEnvironment
sl@0
   441
 *			For recording - the requesting client process must have the 
sl@0
   442
 *			UserEnvironment capability.
sl@0
   443
 */
sl@0
   444
void CMMFMdaAudioInputStream::ReadL(TDes8& aData)
sl@0
   445
	{
sl@0
   446
	User::LeaveIfError(Read(aData));
sl@0
   447
sl@0
   448
	if (iState == EStopped)
sl@0
   449
		{
sl@0
   450
		iDevSound->RecordInitL();
sl@0
   451
		iState = ERecording;
sl@0
   452
		}
sl@0
   453
	}
sl@0
   454
sl@0
   455
/**
sl@0
   456
 *
sl@0
   457
 *  To read data from input stream 	
sl@0
   458
 *
sl@0
   459
 *	@param	"TDesC8& aData"	
sl@0
   460
 *			a stream data 
sl@0
   461
 * 
sl@0
   462
 *  @capability	UserEnvironment
sl@0
   463
 *			For recording - the requesting client process must have the 
sl@0
   464
 *			UserEnvironment capability.
sl@0
   465
 */
sl@0
   466
TInt CMMFMdaAudioInputStream::Read(TDes8& aData)
sl@0
   467
	{
sl@0
   468
	TMMFFifoItem<TDes8>* item = new TMMFFifoItem<TDes8>(aData);
sl@0
   469
	if (!item)
sl@0
   470
		{
sl@0
   471
		return KErrNoMemory;
sl@0
   472
		}
sl@0
   473
sl@0
   474
	iFifo->AddToFifo(*item);	// no issue with memory alloc
sl@0
   475
								// each element contains storage space for link to the next
sl@0
   476
	return KErrNone;
sl@0
   477
	}
sl@0
   478
sl@0
   479
/**
sl@0
   480
 *
sl@0
   481
 *  To stop write data to stream 	
sl@0
   482
 *
sl@0
   483
 */
sl@0
   484
void CMMFMdaAudioInputStream::Stop()
sl@0
   485
	{
sl@0
   486
	// Need to take for the case where Stop is invoked directly after a call to RequestStop.
sl@0
   487
	// We have chosen to allow the Stop to go through as this could be more important.
sl@0
   488
	// This is non-reentrant code but will suffice for our needs.
sl@0
   489
	if (iState != EStopped)
sl@0
   490
		{
sl@0
   491
		// Amend the state so RequestStop or Stop initiated by it (indirectly) won't function
sl@0
   492
		iState = EStopped;
sl@0
   493
sl@0
   494
		if (iSingleBuffer && iStorageItem.Ptr() != NULL)
sl@0
   495
			{
sl@0
   496
			iCallback.MaiscBufferCopied(KErrAbort, iStorageItem);
sl@0
   497
			iStorageItem.Set (NULL,0,0);
sl@0
   498
			}
sl@0
   499
	 
sl@0
   500
		// Delete all buffers in the fifo and notify the observer
sl@0
   501
		TMMFFifoItem<TDes8>* firstItem;
sl@0
   502
		while((firstItem = iFifo->Get()) != NULL)
sl@0
   503
			{
sl@0
   504
			iFifo->RemoveFirstItem();
sl@0
   505
			if (!iSingleBuffer)
sl@0
   506
				{
sl@0
   507
				iCallback.MaiscBufferCopied(KErrAbort,firstItem->GetData());		 		 		 		 
sl@0
   508
				}
sl@0
   509
			delete firstItem;
sl@0
   510
			}
sl@0
   511
	 
sl@0
   512
		iDevSound->Stop();
sl@0
   513
		}
sl@0
   514
	}
sl@0
   515
sl@0
   516
sl@0
   517
/**
sl@0
   518
 *
sl@0
   519
 *  To pause write data to stream 
sl@0
   520
 *  Allow yet unprocessed buffers to be processed and passed back via BufferToBeEmptied.
sl@0
   521
 *  When the last (empty) buffer arrives, Stop is called.
sl@0
   522
 */
sl@0
   523
void CMMFMdaAudioInputStream::RequestStop()
sl@0
   524
	{
sl@0
   525
	// [ precondition that we are not already stopped 
sl@0
   526
	// && if we are stopped do nothing.
sl@0
   527
	// If we are stopping a recording, we need to give the server a chance to 
sl@0
   528
	// process that data which has already been captured. We therefore stay in the EPause
sl@0
   529
	// state.
sl@0
   530
	if (iState != EStopped)
sl@0
   531
		{
sl@0
   532
		if (iState != EStopping)
sl@0
   533
			{
sl@0
   534
			// We can only be Recording, if we have other states later they can be tested here.
sl@0
   535
			iDevSound->Pause();
sl@0
   536
			iState = EStopping;
sl@0
   537
			}
sl@0
   538
		}
sl@0
   539
	}
sl@0
   540
sl@0
   541
sl@0
   542
/**
sl@0
   543
 *
sl@0
   544
 *  To get the current position in the data stream	
sl@0
   545
 *
sl@0
   546
 *	@return	"TTimeIntervalMicroSeconds&"	
sl@0
   547
 *			the current position in integer
sl@0
   548
 *
sl@0
   549
 */
sl@0
   550
const TTimeIntervalMicroSeconds& CMMFMdaAudioInputStream::Position()
sl@0
   551
	{
sl@0
   552
	TInt64 position = iDevSound->SamplesRecorded();
sl@0
   553
	position = position * KMicroSecsInOneSec / StreamUtils::SampleRateAsValue(iDevSound->Config());
sl@0
   554
	iPosition = (iState == ERecording) ? position : 0; // Shouldn't need to check for playing but CMMFDevSound doesn't reset bytes played after a stop
sl@0
   555
	return iPosition;
sl@0
   556
	}
sl@0
   557
sl@0
   558
sl@0
   559
sl@0
   560
/**
sl@0
   561
 *
sl@0
   562
 *  To return the current number of bytes recorded by audio hardware
sl@0
   563
 *	@return "the current current number of bytes rendered by audio hardware in integer"	
sl@0
   564
 *
sl@0
   565
 */
sl@0
   566
TInt CMMFMdaAudioInputStream::GetBytes()
sl@0
   567
	{
sl@0
   568
	return iBytesRecorded;
sl@0
   569
	}
sl@0
   570
sl@0
   571
void CMMFMdaAudioInputStream::SetDataTypeL(TFourCC aAudioType)
sl@0
   572
	{
sl@0
   573
	if(iState != EStopped)
sl@0
   574
		User::Leave(KErrServerBusy);
sl@0
   575
sl@0
   576
	if(aAudioType == iDataTypeCode)
sl@0
   577
		return;
sl@0
   578
sl@0
   579
	TMMFPrioritySettings prioritySettings ; 	
sl@0
   580
	prioritySettings.iState = EMMFStateRecording;
sl@0
   581
	
sl@0
   582
	RArray<TFourCC> supportedDataTypes;
sl@0
   583
	
sl@0
   584
	CleanupClosePushL(supportedDataTypes);
sl@0
   585
sl@0
   586
	TRAPD(err, iDevSound->GetSupportedOutputDataTypesL(supportedDataTypes, prioritySettings));
sl@0
   587
sl@0
   588
	if (err == KErrNone)
sl@0
   589
		{
sl@0
   590
		if (supportedDataTypes.Find(aAudioType) == KErrNotFound)
sl@0
   591
			{
sl@0
   592
			User::Leave(KErrNotSupported);	
sl@0
   593
			}
sl@0
   594
		//if match, set the 4CC of AudioType to match
sl@0
   595
		iDataTypeCode.Set(aAudioType);
sl@0
   596
		}
sl@0
   597
	else //we had a real leave error from GetSupportedInputDataTypesL
sl@0
   598
		{
sl@0
   599
		User::Leave(err);	
sl@0
   600
		}
sl@0
   601
	CleanupStack::PopAndDestroy(&supportedDataTypes);
sl@0
   602
sl@0
   603
	if(iIsOpenState!=EIsNotOpen)
sl@0
   604
		{
sl@0
   605
		// need to recall or restart InitializeL() process
sl@0
   606
		iDevSound->CancelInitialize(); // call just in case mid-InitializeL. No harm if not. 
sl@0
   607
									   // if not supported then assume old DevSound behaviour anyway
sl@0
   608
									   // where InitializeL() implicitly cancels, so no harm either way
sl@0
   609
		iIsOpenState = EIsOpening;
sl@0
   610
		iInitCallFrmSetDataType = ETrue;
sl@0
   611
		TRAPD(err, iDevSound->InitializeL(*this, iDataTypeCode, EMMFStateRecording));
sl@0
   612
		if (err != KErrNone)
sl@0
   613
			{
sl@0
   614
			// Leave if error.
sl@0
   615
			iIsOpenState = EIsNotOpen;
sl@0
   616
			iInitCallFrmSetDataType = EFalse;
sl@0
   617
			User::Leave(err);
sl@0
   618
			}
sl@0
   619
		// In some implementations InitializeComplete is sent 
sl@0
   620
		// in context, so check before starting activeSchedulerWait.
sl@0
   621
		else if(iIsOpenState == EIsOpening)
sl@0
   622
			{
sl@0
   623
			iInitializeState = KRequestPending;
sl@0
   624
			iActiveSchedulerWait->Start();
sl@0
   625
			}
sl@0
   626
		iInitCallFrmSetDataType = EFalse;
sl@0
   627
		User::LeaveIfError(iInitializeState);
sl@0
   628
		}	
sl@0
   629
	}
sl@0
   630
sl@0
   631
/**
sl@0
   632
sl@0
   633
*/
sl@0
   634
TFourCC CMMFMdaAudioInputStream::DataType() const
sl@0
   635
	{
sl@0
   636
	return iDataTypeCode;
sl@0
   637
	}
sl@0
   638
	
sl@0
   639
TInt CMMFMdaAudioInputStream::BitRateL() const
sl@0
   640
	{
sl@0
   641
	TAny* ptr = iDevSound->CustomInterface(KUidCustomInterfaceDevSoundBitRate);
sl@0
   642
	if (ptr == NULL)
sl@0
   643
		User::Leave(KErrNotSupported);
sl@0
   644
	
sl@0
   645
	MMMFDevSoundCustomInterfaceBitRate* bitrate = static_cast<MMMFDevSoundCustomInterfaceBitRate*>(ptr);
sl@0
   646
	return bitrate->BitRateL();
sl@0
   647
	}
sl@0
   648
	
sl@0
   649
	
sl@0
   650
void CMMFMdaAudioInputStream::SetBitRateL(TInt aBitRate)
sl@0
   651
	{
sl@0
   652
	TAny* ptr = iDevSound->CustomInterface(KUidCustomInterfaceDevSoundBitRate);
sl@0
   653
	if (ptr == NULL)
sl@0
   654
		User::Leave(KErrNotSupported);
sl@0
   655
	
sl@0
   656
	MMMFDevSoundCustomInterfaceBitRate* bitrate = static_cast<MMMFDevSoundCustomInterfaceBitRate*>(ptr);
sl@0
   657
	bitrate->SetBitRateL(aBitRate);
sl@0
   658
	}
sl@0
   659
	
sl@0
   660
void CMMFMdaAudioInputStream::GetSupportedBitRatesL(RArray<TInt>& aSupportedBitRates)
sl@0
   661
	{
sl@0
   662
	// ensure that the array is empty before passing it in
sl@0
   663
	aSupportedBitRates.Reset();
sl@0
   664
	TAny* ptr = iDevSound->CustomInterface(KUidCustomInterfaceDevSoundBitRate);
sl@0
   665
	if (ptr == NULL)
sl@0
   666
		User::Leave(KErrNotSupported);
sl@0
   667
	MMMFDevSoundCustomInterfaceBitRate* bitrate = static_cast<MMMFDevSoundCustomInterfaceBitRate*>(ptr);
sl@0
   668
	bitrate->GetSupportedBitRatesL(aSupportedBitRates);
sl@0
   669
	}
sl@0
   670
sl@0
   671
sl@0
   672
//
sl@0
   673
sl@0
   674
CMMFMdaAudioInputStream::CActiveCallback::CActiveCallback(MMdaAudioInputStreamCallback& aCallback)
sl@0
   675
	: CActive(EPriorityStandard), iCallback(aCallback)
sl@0
   676
	{
sl@0
   677
	CActiveScheduler::Add(this);
sl@0
   678
	}
sl@0
   679
sl@0
   680
CMMFMdaAudioInputStream::CActiveCallback::~CActiveCallback()
sl@0
   681
	{
sl@0
   682
	Cancel();
sl@0
   683
	}
sl@0
   684
sl@0
   685
void CMMFMdaAudioInputStream::CActiveCallback::RunL()
sl@0
   686
	{
sl@0
   687
	iCallback.MaiscOpenComplete(iStatus.Int());
sl@0
   688
	}
sl@0
   689
sl@0
   690
void CMMFMdaAudioInputStream::CActiveCallback::DoCancel()
sl@0
   691
	{
sl@0
   692
	}
sl@0
   693
sl@0
   694
void CMMFMdaAudioInputStream::CActiveCallback::Signal(const TInt aReason)
sl@0
   695
	{
sl@0
   696
	ASSERT(!IsActive());
sl@0
   697
sl@0
   698
	// Signal ourselves to run with the given completion code
sl@0
   699
	TRequestStatus* status = &iStatus;
sl@0
   700
	User::RequestComplete(status, aReason);
sl@0
   701
	SetActive();
sl@0
   702
	}
sl@0
   703
sl@0
   704
//
sl@0
   705
sl@0
   706
/**
sl@0
   707
 *
sl@0
   708
 *  To be called when intialize stream complete	
sl@0
   709
 *
sl@0
   710
 *	@param	"TInt aError"	
sl@0
   711
 *			error code, initialize stream succeed when aError = 0
sl@0
   712
 *
sl@0
   713
 */
sl@0
   714
void CMMFMdaAudioInputStream::InitializeComplete(TInt aError)
sl@0
   715
	{
sl@0
   716
	TInt err = aError;
sl@0
   717
	if(iIsOpenState == EIsOpening)
sl@0
   718
		{
sl@0
   719
		if (err == KErrNone)
sl@0
   720
			{
sl@0
   721
			// Use settings to set audio properties after the dev sound has been
sl@0
   722
			// successfully initialised
sl@0
   723
			if(iAudioDataStored)
sl@0
   724
				{
sl@0
   725
				TRAP(err, RealSetAudioPropertiesL(iSettings.iSampleRate, iSettings.iChannels));
sl@0
   726
				}
sl@0
   727
sl@0
   728
			}
sl@0
   729
			
sl@0
   730
		// Signal for the MaiscOpenComplete callback to be called asynchronously.Ignore if InitializeL is called from SetDataTypeL
sl@0
   731
		if(!iInitCallFrmSetDataType)
sl@0
   732
			{
sl@0
   733
			iActiveCallback->Signal(err);
sl@0
   734
			}
sl@0
   735
		iIsOpenState = err ? EIsNotOpen : EIsOpen;
sl@0
   736
		//reset iAudioDataStored flag if set - whatever don't want to use next time
sl@0
   737
		iAudioDataStored = EFalse;
sl@0
   738
		if(iInitializeState == KRequestPending)
sl@0
   739
			{
sl@0
   740
			iInitializeState = err;
sl@0
   741
			iActiveSchedulerWait->AsyncStop();
sl@0
   742
			}
sl@0
   743
		else
sl@0
   744
			{
sl@0
   745
			iInitializeState = err;//Set the error.		
sl@0
   746
			}
sl@0
   747
		}
sl@0
   748
	}
sl@0
   749
sl@0
   750
/**
sl@0
   751
 *
sl@0
   752
 *  Do not support
sl@0
   753
 *
sl@0
   754
 */
sl@0
   755
void CMMFMdaAudioInputStream::ToneFinished(TInt /*aError*/)
sl@0
   756
	{
sl@0
   757
	Panic(EAudioInputPanicNotSupported);
sl@0
   758
	}
sl@0
   759
sl@0
   760
/**
sl@0
   761
 *
sl@0
   762
 *  Called when sound device has filled data buffer
sl@0
   763
 *
sl@0
   764
 *	@param	"CMMFBuffer* aBuffer"	
sl@0
   765
 *			a pointer point to CMMFBuffer, which is used for recieved data
sl@0
   766
 *
sl@0
   767
 */
sl@0
   768
void CMMFMdaAudioInputStream::BufferToBeEmptied(CMMFBuffer* aBuffer)
sl@0
   769
	{
sl@0
   770
	// Simply put, tries to copy the data from aBuffer into the clients storage buffers. 
sl@0
   771
	//
sl@0
   772
	// The input stream iFifo data member is used to store the clients storage buffers
sl@0
   773
	// that are passed to it via a call to ReadL.
sl@0
   774
	//
sl@0
   775
	// If iSingleBuffer is False, the first buffer on the fifo is copied to. 
sl@0
   776
	// This buffer is then removed off the fifo.
sl@0
   777
	// The callback MaiscBufferCopied is invoked after each copy, passing that buffer.
sl@0
   778
	// If the data is greater than the buffer then this process repeats with the next buffer.
sl@0
   779
	//
sl@0
   780
	// If iSingleBuffer is True, it is assumed only one buffer is on the fifo.
sl@0
   781
	// The behaviour is the same as above except that a descriptor representing the 
sl@0
   782
	// buffers empty part is placed at the end of the fifo, and the callback 
sl@0
   783
	// MaiscBufferCopied is invoked only when the buffer is full.
sl@0
   784
	//
sl@0
   785
	// If the client sets iSingleBuffer to True and places more than one buffer on the fifo
sl@0
   786
	// the behaviour is undefined and unsupported.
sl@0
   787
	//
sl@0
   788
	// If there are no more storage buffers on the fifo, the callback 
sl@0
   789
	// MaiscRecordComplete(KErrOverflow) is invoked.
sl@0
   790
sl@0
   791
	const TDesC8& buffer = STATIC_CAST(CMMFDataBuffer*, aBuffer)->Data();
sl@0
   792
	
sl@0
   793
	TInt lengthCopied = 0;
sl@0
   794
	iBytesRecorded += buffer.Length();
sl@0
   795
sl@0
   796
	// A stop was requested after all the data has been received
sl@0
   797
	if (iState == EStopping && buffer.Length() == 0)
sl@0
   798
		{
sl@0
   799
	 	Stop();
sl@0
   800
		iCallback.MaiscRecordComplete(KErrNone);
sl@0
   801
		return;
sl@0
   802
		}
sl@0
   803
	else
sl@0
   804
		{
sl@0
   805
		// The fifo may have multiple storage buffers, i.e. one in each of its entries.
sl@0
   806
		// Fill what we can in each. If we get an empty buffer then we have finished recording.
sl@0
   807
		while (lengthCopied < buffer.Length())
sl@0
   808
			{
sl@0
   809
			// Chop up aBuffer into slices the buffers in iFifo can handle
sl@0
   810
			TMMFFifoItem<TDes8>* firstItem = iFifo->Get();
sl@0
   811
			
sl@0
   812
			if(firstItem != NULL)
sl@0
   813
				{
sl@0
   814
				TDes8& writeBuf = firstItem->GetData();
sl@0
   815
						
sl@0
   816
				// We have a spare buffer slot
sl@0
   817
				TInt slotLength = Min(buffer.Length()-lengthCopied, writeBuf.MaxLength());
sl@0
   818
				writeBuf = buffer.Mid(lengthCopied, slotLength);
sl@0
   819
				lengthCopied += slotLength;
sl@0
   820
				
sl@0
   821
				// Determine whether to callback the client or not.
sl@0
   822
				// I.e. if we have multiple small buffers that we want to process quickly or
sl@0
   823
				// when a singular buffer is FULL.
sl@0
   824
				// Note: That if the client asks to Stop, the buffer may not be filled!
sl@0
   825
				if (iSingleBuffer)
sl@0
   826
					{
sl@0
   827
					// Remember this item for later. 
sl@0
   828
					// We really only want the first item as this covers the entire
sl@0
   829
					// client storage buffer. We will adjust the actual length later.
sl@0
   830
					if (iStorageItem.Ptr() == NULL)
sl@0
   831
						{
sl@0
   832
						iStorageItem.Set (const_cast<TUint8*>(writeBuf.Ptr()), 0, writeBuf.MaxLength());
sl@0
   833
						}
sl@0
   834
					
sl@0
   835
					// In this iteration we may just be looking at a right-part of the original
sl@0
   836
					// buffer. Update the actual length of data.
sl@0
   837
					TInt actualLength = (writeBuf.Ptr()-iStorageItem.Ptr()) + writeBuf.Length();
sl@0
   838
					iStorageItem.SetLength(actualLength);
sl@0
   839
sl@0
   840
					// Is the buffer full?
sl@0
   841
					if (writeBuf.Length() == writeBuf.MaxLength())
sl@0
   842
						{
sl@0
   843
						// The singular buffer has been filled so pass it back to the client
sl@0
   844
						iCallback.MaiscBufferCopied(KErrNone, iStorageItem);
sl@0
   845
						iStorageItem.Set (NULL,0,0);
sl@0
   846
						}
sl@0
   847
					else
sl@0
   848
						{
sl@0
   849
						// Create a window to the 'remaining' free section of the storage buffer
sl@0
   850
						iBufferPtr.Set (const_cast<TUint8*>(writeBuf.Ptr())+lengthCopied, 0, writeBuf.MaxLength()-lengthCopied);
sl@0
   851
						
sl@0
   852
						// Add the window to the fifo
sl@0
   853
						TInt err = Read(iBufferPtr);
sl@0
   854
						if (err)
sl@0
   855
							{
sl@0
   856
							Stop();
sl@0
   857
							iCallback.MaiscRecordComplete(err);
sl@0
   858
							return;
sl@0
   859
							}
sl@0
   860
						ASSERT(iState == ERecording);
sl@0
   861
						}
sl@0
   862
					}
sl@0
   863
				else
sl@0
   864
					{
sl@0
   865
					// Notify client
sl@0
   866
					iCallback.MaiscBufferCopied(KErrNone, writeBuf);
sl@0
   867
					}
sl@0
   868
				//Check if client called Stop from the MaiscBufferCopied. 
sl@0
   869
				//If so, we should neither continue this loop nor delete the first item. Stop cleans up all the buffers
sl@0
   870
				if(iState == EStopped)
sl@0
   871
					{
sl@0
   872
					break;
sl@0
   873
					}
sl@0
   874
				else
sl@0
   875
					{
sl@0
   876
					// Remove this storage buffer from the fifo as we want to have access to any others behind it.
sl@0
   877
					iFifo->RemoveFirstItem();
sl@0
   878
					delete firstItem;						
sl@0
   879
					}
sl@0
   880
				}
sl@0
   881
			else
sl@0
   882
				{
sl@0
   883
				// run out of buffers - report an overflow error
sl@0
   884
				Stop();
sl@0
   885
				iCallback.MaiscRecordComplete(KErrOverflow);
sl@0
   886
				return;
sl@0
   887
				}
sl@0
   888
			}// while
sl@0
   889
		} // else
sl@0
   890
		
sl@0
   891
	// Keep recording if there are free buffers
sl@0
   892
	if (!iFifo->IsEmpty())
sl@0
   893
		iDevSound->RecordData();
sl@0
   894
	}
sl@0
   895
sl@0
   896
/**
sl@0
   897
 *
sl@0
   898
 *  Called when record operation complete, successfully or otherwise	
sl@0
   899
 *
sl@0
   900
 *	@param	"TInt aError"	
sl@0
   901
 *			an error value which will indicate playing successfully complete
sl@0
   902
 *			if error value is 0
sl@0
   903
 *
sl@0
   904
 */
sl@0
   905
void CMMFMdaAudioInputStream::PlayError(TInt /*aError*/)
sl@0
   906
	{
sl@0
   907
	Panic(EAudioInputPanicNotSupported);
sl@0
   908
	}
sl@0
   909
sl@0
   910
sl@0
   911
/**
sl@0
   912
 *
sl@0
   913
 *  Do not support
sl@0
   914
 *
sl@0
   915
 */
sl@0
   916
void CMMFMdaAudioInputStream::BufferToBeFilled(CMMFBuffer* /*aBuffer*/)
sl@0
   917
	{
sl@0
   918
	Panic(EAudioInputPanicNotSupported);
sl@0
   919
	}
sl@0
   920
sl@0
   921
/**
sl@0
   922
 *
sl@0
   923
 *  Do not support
sl@0
   924
 *
sl@0
   925
 */
sl@0
   926
void CMMFMdaAudioInputStream::RecordError(TInt aError)
sl@0
   927
	{
sl@0
   928
	if (iState == ERecording)
sl@0
   929
		{
sl@0
   930
		if (aError != KErrCancel)
sl@0
   931
			{
sl@0
   932
			iCallback.MaiscRecordComplete(aError);
sl@0
   933
			}
sl@0
   934
		// else must have been cancelled by client. Doesn't need to be notified
sl@0
   935
sl@0
   936
		iState = EStopped;
sl@0
   937
		}
sl@0
   938
	}
sl@0
   939
sl@0
   940
/**
sl@0
   941
 *
sl@0
   942
 *  Do not support
sl@0
   943
 *
sl@0
   944
 */
sl@0
   945
void CMMFMdaAudioInputStream::ConvertError(TInt /*aError*/)
sl@0
   946
	{
sl@0
   947
	Panic(EAudioInputPanicNotSupported);
sl@0
   948
	}
sl@0
   949
sl@0
   950
/**
sl@0
   951
 *
sl@0
   952
 *  Do not support
sl@0
   953
 *
sl@0
   954
 */
sl@0
   955
void CMMFMdaAudioInputStream::DeviceMessage(TUid /*aMessageType*/, const TDesC8& /*aMsg*/)
sl@0
   956
	{
sl@0
   957
	Panic(EAudioInputPanicNotSupported);
sl@0
   958
	}
sl@0
   959
sl@0
   960
// CustomInferface - just pass on to DevSound. 
sl@0
   961
TAny* CMMFMdaAudioInputStream::CustomInterface(TUid aInterfaceId)
sl@0
   962
	{
sl@0
   963
	return iDevSound->CustomInterface(aInterfaceId);
sl@0
   964
	}
sl@0
   965
	
sl@0
   966
sl@0
   967
void CMMFMdaAudioInputStream::SetSingleBufferMode(TBool aSingleMode)
sl@0
   968
	{
sl@0
   969
	iSingleBuffer = aSingleMode;
sl@0
   970
	}
sl@0
   971
sl@0
   972
void CMMFMdaAudioInputStream::ShutDown()
sl@0
   973
	{
sl@0
   974
	// Need to take for the case where Stop is invoked from the destructor of CMdaAudioInputStream 
sl@0
   975
	// Need to ensure that there are no callbacks to the client at this stage
sl@0
   976
	if (iState != EStopped)
sl@0
   977
		{
sl@0
   978
		iState = EStopped;
sl@0
   979
sl@0
   980
		if (iSingleBuffer && iStorageItem.Ptr() != NULL)
sl@0
   981
			{
sl@0
   982
			iStorageItem.Set (NULL,0,0);
sl@0
   983
			}
sl@0
   984
	 
sl@0
   985
		// Delete all buffers in the fifo
sl@0
   986
		TMMFFifoItem<TDes8>* firstItem;
sl@0
   987
		while((firstItem = iFifo->Get()) != NULL)
sl@0
   988
			{
sl@0
   989
			iFifo->RemoveFirstItem();
sl@0
   990
			delete firstItem;
sl@0
   991
			}
sl@0
   992
	 
sl@0
   993
		iDevSound->Stop();
sl@0
   994
		}
sl@0
   995
	}