os/mm/mmlibs/mmfw/src/server/BaseClasses/mmfdatapath2.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-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
// source\server\mmfdatapath2.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32math.h>
sl@0
    19
#include <mmf/common/mmffourcc.h>
sl@0
    20
#include <mmf/common/mmfpaniccodes.h>
sl@0
    21
#include <mmf/server/mmfaudiooutput.h>
sl@0
    22
#include <mmf/server/mmfaudioinput.h>
sl@0
    23
#include "mmfdatapath2.h"
sl@0
    24
#include "mmfclientaudiostreamutils.h"
sl@0
    25
#include <mmf/common/mmfaudio.h>
sl@0
    26
#include <mmf/plugin/mmfcodecimplementationuids.hrh> // KUidMmfCodecAudioSettings
sl@0
    27
#include <mmf/server/devsoundstandardcustominterfaces.h>
sl@0
    28
#include <mmf/server/mmffile.h>
sl@0
    29
#include <mda/client/resource.h>
sl@0
    30
sl@0
    31
static void Panic(TMMFDataPathPanicCode aPanicCode, TInt aSourceLineNumber)
sl@0
    32
	{
sl@0
    33
	_LIT(KMMFDataPathPanicCategory, "MMFDataPath2");
sl@0
    34
	User::Panic(KMMFDataPathPanicCategory, STATIC_CAST(TInt,aPanicCode) + aSourceLineNumber);
sl@0
    35
	}
sl@0
    36
sl@0
    37
/**
sl@0
    38
Allocates and constructs a data path.
sl@0
    39
sl@0
    40
Use this function if the codec UID is not already known by CMMFController
sl@0
    41
and there is no data path ambiguity - ie only one data path is possible.
sl@0
    42
sl@0
    43
Will create codec via fourCC.
sl@0
    44
sl@0
    45
@param  aEventHandler
sl@0
    46
        Installs an event handler to provide message passing between clients and sources/sinks.
sl@0
    47
sl@0
    48
@return Newly constructed data path object.
sl@0
    49
*/
sl@0
    50
sl@0
    51
EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(MAsyncEventHandler& aEventHandler)
sl@0
    52
	{
sl@0
    53
	CMMFDataPath2* self = new(ELeave) CMMFDataPath2(TMediaId(), aEventHandler);
sl@0
    54
	CleanupStack::PushL(self);
sl@0
    55
	self->ConstructL();
sl@0
    56
	CleanupStack::Pop();
sl@0
    57
	return self;
sl@0
    58
	}
sl@0
    59
sl@0
    60
sl@0
    61
/**
sl@0
    62
Allocates and constructs a data path according to the specified media ID.
sl@0
    63
sl@0
    64
Use this function if the codec UID is not already known by CMMFController
sl@0
    65
and there is ambiguity with the data path ie. there is more than one possible data path.
sl@0
    66
sl@0
    67
@param  aMediaId
sl@0
    68
        Optional media ID parameter when there are multiple media types.
sl@0
    69
@param  aEventHandler
sl@0
    70
        Installs an event handler to provide message passing between clients and sources/sinks.
sl@0
    71
sl@0
    72
@return A newly constructed data path object.
sl@0
    73
*/
sl@0
    74
sl@0
    75
EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TMediaId aMediaId, MAsyncEventHandler& aEventHandler)
sl@0
    76
	{
sl@0
    77
	CMMFDataPath2* self = new(ELeave) CMMFDataPath2(aMediaId, aEventHandler);
sl@0
    78
	CleanupStack::PushL(self);
sl@0
    79
	self->ConstructL();
sl@0
    80
	CleanupStack::Pop();
sl@0
    81
	return self;
sl@0
    82
	}
sl@0
    83
sl@0
    84
/**
sl@0
    85
Allocates and constructs a data path according to the specified codec UID.
sl@0
    86
sl@0
    87
Use this function if the codec UID is already known by CMMFController
sl@0
    88
and there is no data path ambiguity ie. only one data path is possible
sl@0
    89
will create codec explicitly using the supplied codec Uid
sl@0
    90
sl@0
    91
@param  aCodecUid
sl@0
    92
        Optional mediaID parameter when there are multiple media types
sl@0
    93
@param  aEventHandler
sl@0
    94
        Installs an event handler to provide message passing between clients and sources/sinks.
sl@0
    95
sl@0
    96
@return A newly constructed data path object.
sl@0
    97
*/
sl@0
    98
sl@0
    99
EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TUid aCodecUid, MAsyncEventHandler& aEventHandler)
sl@0
   100
	{
sl@0
   101
	CMMFDataPath2* self = new(ELeave) CMMFDataPath2(TMediaId(), aEventHandler);
sl@0
   102
	CleanupStack::PushL(self);
sl@0
   103
	self->ConstructL(aCodecUid);
sl@0
   104
	CleanupStack::Pop();
sl@0
   105
	return self;
sl@0
   106
	}
sl@0
   107
sl@0
   108
sl@0
   109
/**
sl@0
   110
Allocates and constructs a data path according to the specified codec UID.
sl@0
   111
sl@0
   112
Use this function if the codec UID is already known by CMMFController
sl@0
   113
and there is ambiguity ie. more than one possible data path.
sl@0
   114
TMediaId used to select the path.
sl@0
   115
sl@0
   116
@param  aCodecUid
sl@0
   117
		The codec UID.
sl@0
   118
@param  aMediaId
sl@0
   119
        Optional mediaID parameter when there are multiple media types.
sl@0
   120
@param  aEventHandler
sl@0
   121
        Installs an event handler to provide message passing between clients and sources/sinks.
sl@0
   122
sl@0
   123
@return A newly constructed data path object.
sl@0
   124
*/
sl@0
   125
EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TUid aCodecUid, TMediaId aMediaId, MAsyncEventHandler& aEventHandler)
sl@0
   126
	{
sl@0
   127
	CMMFDataPath2* self = new(ELeave) CMMFDataPath2(aMediaId, aEventHandler);
sl@0
   128
	CleanupStack::PushL(self);
sl@0
   129
	self->ConstructL(aCodecUid);
sl@0
   130
	CleanupStack::Pop();
sl@0
   131
	return self;
sl@0
   132
	}
sl@0
   133
sl@0
   134
CMMFDataPath2::CMMFDataPath2(TMediaId aMediaId, MAsyncEventHandler& aEventHandler) 
sl@0
   135
	: CMMFDataPath(aMediaId, aEventHandler), iTimeLeftToPlayComplete(-1)
sl@0
   136
	{
sl@0
   137
	}
sl@0
   138
		
sl@0
   139
void CMMFDataPath2::ConstructL(TUid aCodecUid)
sl@0
   140
	{
sl@0
   141
	CMMFDataPath::ConstructL(aCodecUid);
sl@0
   142
	iRepeatTrailingSilenceTimer = CPeriodic::NewL(CActive::EPriorityStandard); 
sl@0
   143
	}
sl@0
   144
	
sl@0
   145
/**
sl@0
   146
Standard destructor.
sl@0
   147
*/
sl@0
   148
sl@0
   149
CMMFDataPath2::~CMMFDataPath2()
sl@0
   150
	{
sl@0
   151
	if(iRepeatTrailingSilenceTimer)
sl@0
   152
		{
sl@0
   153
		iRepeatTrailingSilenceTimer->Cancel();
sl@0
   154
		delete iRepeatTrailingSilenceTimer;
sl@0
   155
		}
sl@0
   156
	}
sl@0
   157
sl@0
   158
TInt CMMFDataPath2::RepeatTrailingSilenceTimerComplete(TAny* aDataPath)
sl@0
   159
	{
sl@0
   160
	CMMFDataPath2* dataPath = static_cast<CMMFDataPath2*>(aDataPath);
sl@0
   161
	
sl@0
   162
	TRAPD(err, dataPath->DoRepeatTrailingSilenceTimerCompleteL());
sl@0
   163
	if (err != KErrNone)
sl@0
   164
		{
sl@0
   165
		dataPath->DoSendEventToClient(KMMFEventCategoryPlaybackComplete, err);
sl@0
   166
		}	
sl@0
   167
	return KErrNone;
sl@0
   168
	}
sl@0
   169
	
sl@0
   170
TInt CMMFDataPath2::DoRepeatTrailingSilenceTimerCompleteL()
sl@0
   171
	{
sl@0
   172
	//cancel this periodic timer
sl@0
   173
	iRepeatTrailingSilenceTimer->Cancel();
sl@0
   174
	if(iTimeLeftToPlayComplete.Int64()>0)
sl@0
   175
		{
sl@0
   176
		iTimeLeftToPlayComplete=0;
sl@0
   177
		}
sl@0
   178
		
sl@0
   179
	if (iTrailingSilenceLeftToPlay.Int64() > 0)
sl@0
   180
		{
sl@0
   181
		PlaySilence();
sl@0
   182
		}
sl@0
   183
	else
sl@0
   184
		{
sl@0
   185
		SetPositionL(iPlayWindowStartPosition);
sl@0
   186
		iTimeLeftToPlayComplete=-1;
sl@0
   187
		FillSourceBufferL();		
sl@0
   188
		}
sl@0
   189
	return KErrNone;
sl@0
   190
	}
sl@0
   191
	
sl@0
   192
void CMMFDataPath2::PlaySilence()
sl@0
   193
	{		
sl@0
   194
	// iRepeatTrailingSilenceTimer->After() takes a TTimeIntervalMicroSeconds32
sl@0
   195
	// so for longer periods of silence call it repeatedly with KMaxTInt lengths
sl@0
   196
	TTimeIntervalMicroSeconds32 silence;
sl@0
   197
	if(iTimeLeftToPlayComplete.Int64() > 0)
sl@0
   198
		{
sl@0
   199
		silence = I64INT(iTimeLeftToPlayComplete.Int64());
sl@0
   200
		}
sl@0
   201
	else if (iTrailingSilenceLeftToPlay.Int64() > KMaxTInt)
sl@0
   202
		{
sl@0
   203
		silence = KMaxTInt;
sl@0
   204
		iTrailingSilenceLeftToPlay = iTrailingSilenceLeftToPlay.Int64() - KMaxTInt;
sl@0
   205
		}
sl@0
   206
	else
sl@0
   207
		{
sl@0
   208
		silence = I64INT(iTrailingSilenceLeftToPlay.Int64());
sl@0
   209
		iTrailingSilenceLeftToPlay = 0;
sl@0
   210
		}
sl@0
   211
	iRepeatTrailingSilenceTimer->Start(silence, silence , TCallBack(RepeatTrailingSilenceTimerComplete, this));
sl@0
   212
	}
sl@0
   213
sl@0
   214
/* 
sl@0
   215
 *  FillSourceBufferL
sl@0
   216
 * 
sl@0
   217
 *	Function to get data from the datapath's iDataSource
sl@0
   218
 */
sl@0
   219
sl@0
   220
void CMMFDataPath2::DoFillSourceBufferL()
sl@0
   221
	{
sl@0
   222
#ifdef _DP_DEBUG
sl@0
   223
	RDebug::Print(_L("DP::FillSourceBufferL tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   224
#endif
sl@0
   225
sl@0
   226
	__ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || (iState == EPrimed && iPauseCalled && iIsUsingResumeSupport)), Panic(EMMFDataPathPanicBadState,__LINE__)); 
sl@0
   227
sl@0
   228
	// clear the no-more-source flag here (as well as in PlayL()) because 
sl@0
   229
	// there may have been a re-position since the last call to BufferFilledL()
sl@0
   230
	iNoMoreSourceData = EFalse;
sl@0
   231
sl@0
   232
	if(!iObtainingAsyncSourceBuffer) 
sl@0
   233
		{//this is a normal request for data. 
sl@0
   234
		//If we are getting asynchronous buffers, then can't do this as iSourceBuffer == NULL
sl@0
   235
		iSourceBuffer->SetFrameNumber(++iCurrentSourceFrameNumber); //so source knows which data to load buffer with
sl@0
   236
		iSourceBuffer->SetStatus(EBeingFilled);
sl@0
   237
		iSourceBuffer->SetLastBuffer(EFalse);
sl@0
   238
		}
sl@0
   239
sl@0
   240
#ifdef _DP_DEBUG
sl@0
   241
	RDebug::Print(_L("DP asking for buffer %d  - ptr=0x%x   (this 0x%x)\n"), iCurrentSourceFrameNumber, iSourceBuffer,this);	
sl@0
   242
#endif
sl@0
   243
sl@0
   244
	iSourceBufferWithSource = ETrue;
sl@0
   245
sl@0
   246
	// wait for BufferFilled callback from source. Do this here as some sources cause
sl@0
   247
	//re-entrancy into data path via BufferFilledL
sl@0
   248
	ChangeDataPathTransferState(EWaitSource);  
sl@0
   249
sl@0
   250
	iDataSource->FillBufferL(iSourceBuffer, this, iMediaId);
sl@0
   251
sl@0
   252
#ifdef _DP_DEBUG
sl@0
   253
	RDebug::Print(_L("DP::FillSourceBufferL - DONE tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   254
#endif
sl@0
   255
	}
sl@0
   256
sl@0
   257
/**
sl@0
   258
Runs the clip depending on the current data path and transfer state.
sl@0
   259
sl@0
   260
For example, fills the sink buffer if TDataPathState is EPlaying and TTransferState is ENeedSinkData.
sl@0
   261
*/
sl@0
   262
void CMMFDataPath2::RunL()
sl@0
   263
	{
sl@0
   264
#ifdef _DP_DEBUG 
sl@0
   265
	RDebug::Print(_L("DP::RunL transfer state %d, iPausedCalled %d, tick-%d   (this 0x%x)\n"),iTransferState, iPauseCalled, User::TickCount(),this);
sl@0
   266
#endif
sl@0
   267
	
sl@0
   268
	switch (iState)
sl@0
   269
		{
sl@0
   270
	case EStopped:
sl@0
   271
		break;
sl@0
   272
	case EPrimed: // In the paused state we still continue to feed buffers to the sink. The sink (DevSound) handles the logic of whether the buffers should be emptied
sl@0
   273
	    {
sl@0
   274
	    if (!iPauseCalled || !iIsUsingResumeSupport)
sl@0
   275
	        break;
sl@0
   276
	    }	    
sl@0
   277
	    // fall-through
sl@0
   278
	case EPlaying:
sl@0
   279
	case ERecording:
sl@0
   280
	case EConverting:
sl@0
   281
		switch (iTransferState)
sl@0
   282
			{
sl@0
   283
		case EWaitSink:
sl@0
   284
		case EWaitSource:
sl@0
   285
			break;
sl@0
   286
		case EInitializeSink:
sl@0
   287
			InitializeSinkL();
sl@0
   288
			break;
sl@0
   289
		case EInitializeSource:
sl@0
   290
			InitializeSourceL();
sl@0
   291
			break;
sl@0
   292
		case ENeedSourceData:
sl@0
   293
			FillSourceBufferL();
sl@0
   294
			break;
sl@0
   295
		case ENeedSinkData:
sl@0
   296
			FillSinkBufferL();
sl@0
   297
			break;
sl@0
   298
		case ENeedToMatchSourceToSink:
sl@0
   299
			FillSinkBufferL();
sl@0
   300
			break;
sl@0
   301
		case ESendDataToSink:
sl@0
   302
			EmptySinkBufferL();
sl@0
   303
			break;
sl@0
   304
		case EEndOfData:
sl@0
   305
			EndOfData();
sl@0
   306
			break;
sl@0
   307
			}
sl@0
   308
		break;
sl@0
   309
	default:
sl@0
   310
		break;
sl@0
   311
		}
sl@0
   312
#ifdef _DP_DEBUG
sl@0
   313
	RDebug::Print(_L("DP::RunL DONE\n"));
sl@0
   314
#endif
sl@0
   315
	}
sl@0
   316
	
sl@0
   317
/* 
sl@0
   318
 *  FillSourceBufferL
sl@0
   319
 * 
sl@0
   320
 *	Function to get data from the datapath's iDataSource
sl@0
   321
 */
sl@0
   322
sl@0
   323
void CMMFDataPath2::FillSourceBufferL()
sl@0
   324
	{
sl@0
   325
	__ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || (iState == EPrimed && iPauseCalled && iIsUsingResumeSupport) ), Panic(EMMFDataPathPanicBadState,__LINE__)); 
sl@0
   326
sl@0
   327
	//if the silence timer is active then dont propagate the request
sl@0
   328
	if(iRepeatTrailingSilenceTimer->IsActive())
sl@0
   329
		{
sl@0
   330
		return;
sl@0
   331
		}
sl@0
   332
	
sl@0
   333
	//play the silence period and dont propagate the request
sl@0
   334
	if(iTrailingSilenceLeftToPlay>0 || iVerifyPlayComplete)
sl@0
   335
		{
sl@0
   336
		if(iVerifyPlayComplete)//case when the trailing silence is zero
sl@0
   337
			{
sl@0
   338
			if (!*iDisableAutoIntent && iDrmSource)
sl@0
   339
				{
sl@0
   340
				CMMFFile* file = static_cast<CMMFFile*>(iDrmSource);
sl@0
   341
				TInt err = file->ExecuteIntent(ContentAccess::EPlay);
sl@0
   342
				if (err != KErrNone)
sl@0
   343
					{
sl@0
   344
					DoSendEventToClient(KMMFEventCategoryPlaybackComplete, err);
sl@0
   345
					return;
sl@0
   346
					}
sl@0
   347
				}
sl@0
   348
			
sl@0
   349
			//Retrieve the current play time and add "duration-currentplaytime" to the silence period
sl@0
   350
			//This is to ensure that silence timer is not started before the previous play is actually completed by the devsound
sl@0
   351
			TTimeIntervalMicroSeconds currentTime = CalculateAudioOutputPosition();
sl@0
   352
			if(currentTime.Int64()>iPlayWindowStartPosition.Int64())
sl@0
   353
				{
sl@0
   354
				iTimeLeftToPlayComplete = iPlayWindowEndPosition.Int64()-currentTime.Int64();
sl@0
   355
				}
sl@0
   356
			else
sl@0
   357
				{
sl@0
   358
				iTimeLeftToPlayComplete = 0;
sl@0
   359
				}
sl@0
   360
sl@0
   361
			iVerifyPlayComplete = EFalse;
sl@0
   362
			}
sl@0
   363
		if(iTrailingSilenceLeftToPlay==0 && iTimeLeftToPlayComplete==0)
sl@0
   364
			{
sl@0
   365
			SetPositionL(iPlayWindowStartPosition);
sl@0
   366
			iTimeLeftToPlayComplete=-1;
sl@0
   367
			}
sl@0
   368
		else
sl@0
   369
			{
sl@0
   370
			PlaySilence();
sl@0
   371
			return;
sl@0
   372
			}
sl@0
   373
		}
sl@0
   374
	
sl@0
   375
	DoFillSourceBufferL();
sl@0
   376
	}
sl@0
   377
sl@0
   378
sl@0
   379
/** 
sl@0
   380
Indicates the data source has filled the specified buffer.
sl@0
   381
sl@0
   382
Called by the CMMFDataPath2's MDataSource when it has filled the buffer.
sl@0
   383
sl@0
   384
@param aBuffer
sl@0
   385
       A pointer to the filled buffer.
sl@0
   386
*/
sl@0
   387
void CMMFDataPath2::BufferFilledL(CMMFBuffer* aBuffer)
sl@0
   388
	{	
sl@0
   389
#ifdef _DP_DEBUG
sl@0
   390
	RDebug::Print(_L("DP::BufferFilledL src has filled buffer %d (ptr=0x%x) with %d bytes EoF = %d  tick-%d    (this 0x%x)\n"),aBuffer->FrameNumber(),aBuffer, aBuffer->BufferSize(),aBuffer->LastBuffer(), User::TickCount(),this);
sl@0
   391
#endif
sl@0
   392
	
sl@0
   393
	TBool isInTruePause = (iState == EPrimed  && iPauseCalled && iIsUsingResumeSupport);
sl@0
   394
	//state only used if we are passing data
sl@0
   395
	__ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || isInTruePause), Panic(EMMFDataPathPanicBadState,__LINE__));
sl@0
   396
sl@0
   397
	__ASSERT_DEBUG((!iNoMoreSourceData), Panic(EMMFDataPathPanicBadState,__LINE__)); 
sl@0
   398
	
sl@0
   399
	//if we have been asked to repeat and this is the last buffer, reset last buffer flag and send to the device
sl@0
   400
	if(aBuffer!= NULL && aBuffer->LastBuffer())
sl@0
   401
		{
sl@0
   402
		iNumberOfTimesPlayed++;
sl@0
   403
		if ((iNumberOfTimesPlayed <= iNumberOfTimesToRepeat) || iNumberOfTimesToRepeat == KMdaRepeatForever)
sl@0
   404
			{
sl@0
   405
			aBuffer->SetLastBuffer(EFalse);
sl@0
   406
			
sl@0
   407
			//this will trigger the trailing silence timer next time a buffer is requested.
sl@0
   408
			iTrailingSilenceLeftToPlay = iTrailingSilence;
sl@0
   409
			iVerifyPlayComplete = ETrue;
sl@0
   410
			}
sl@0
   411
		}		
sl@0
   412
		
sl@0
   413
	iSourceBufferWithSource = EFalse;
sl@0
   414
sl@0
   415
	//Has the datapath stopped running, if so were not interested in any callbacks.
sl@0
   416
	if(iState == EStopped || (iState == EPrimed  && !isInTruePause))
sl@0
   417
		{
sl@0
   418
#ifdef _DP_DEBUG
sl@0
   419
		RDebug::Print(_L("DP::BufferFilledL called while not expecting callback iState=%d  iPauseCalled=%d  (this 0x%x)\n"),iState, iPauseCalled,this);
sl@0
   420
#endif
sl@0
   421
		return;
sl@0
   422
		}
sl@0
   423
sl@0
   424
#ifdef REPOSITION_SPEEDUP
sl@0
   425
	// if the source has been re-positioned, then go & get some more source data now
sl@0
   426
	if (!iObtainingAsyncSourceBuffer && iSourceBuffer->FrameNumber() != iCurrentSourceFrameNumber)
sl@0
   427
		{
sl@0
   428
#ifdef _DP_DEBUG
sl@0
   429
		RDebug::Print(_L("DP::BufferFilledL source was re-positioned re-requesting source data (this 0x%x)\n"),this);
sl@0
   430
#endif
sl@0
   431
		ChangeDataPathTransferState(ENeedSourceData);
sl@0
   432
		return;
sl@0
   433
		}
sl@0
   434
#endif //REPOSITION_SPEEDUP
sl@0
   435
sl@0
   436
	//bufer is NULL, indicating no more source data.
sl@0
   437
	if (!aBuffer)
sl@0
   438
		{
sl@0
   439
		//If we only hold a reference to the source buffer, set that to NULL
sl@0
   440
		if(iSnkBufRef)
sl@0
   441
			{
sl@0
   442
			iSourceBuffer = NULL;
sl@0
   443
			}
sl@0
   444
		
sl@0
   445
		iNoMoreSourceData = ETrue;
sl@0
   446
sl@0
   447
		if(!iCodec || //there's only one buffer and that has been returned as NULL, so must be end of data
sl@0
   448
		  iSinkBufferWithSink) //buffer is with sink, we don't have any more data to put in it, so must be end of data
sl@0
   449
			{
sl@0
   450
			ChangeDataPathTransferState(EEndOfData);
sl@0
   451
			}
sl@0
   452
		else //sink buffer is with datapath, see if there is anything to send to sink
sl@0
   453
			{
sl@0
   454
			ChangeDataPathTransferState(ENeedToMatchSourceToSink);
sl@0
   455
			}
sl@0
   456
		
sl@0
   457
#ifdef _DP_DEBUG
sl@0
   458
		RDebug::Print(_L("DP::BufferFilledL DONE aBuffer==NULL tick-%d (this 0x%x)\n"),User::TickCount(),this);
sl@0
   459
#endif
sl@0
   460
		return;
sl@0
   461
		} 
sl@0
   462
sl@0
   463
	
sl@0
   464
	//We were waiting for a response from the source to get an asynchronous buffer.
sl@0
   465
	//We now have it, and we proceed to transfer this data to the sink.
sl@0
   466
	if	(iObtainingAsyncSourceBuffer)
sl@0
   467
		{
sl@0
   468
		iObtainingAsyncSourceBuffer = EFalse;
sl@0
   469
		}
sl@0
   470
	
sl@0
   471
sl@0
   472
	aBuffer->SetStatus(EFull);
sl@0
   473
sl@0
   474
	if(iSourceBuffer != aBuffer)
sl@0
   475
		{//buffer has been changed by the source
sl@0
   476
		iSourceBuffer = aBuffer;
sl@0
   477
		if (!(iBuffersToUse & ENeedSinkBuffer))
sl@0
   478
			{//we only need one buffer and use source
sl@0
   479
			iSinkBuffer = iSourceBuffer;
sl@0
   480
			iSnkBufRef = ETrue;
sl@0
   481
			}
sl@0
   482
#ifdef _DP_DEBUG
sl@0
   483
	RDebug::Print(_L("DP::BufferFilledL - iSourceBuffer=0x%x ref=%d   iSinkBuffer=0x%x ref=%d (this 0x%x)\n"),iSourceBuffer,iSrcBufRef,iSinkBuffer,iSnkBufRef, this);
sl@0
   484
#endif	
sl@0
   485
		}
sl@0
   486
	//Is this the last buffer from the source (0 length or LastBuffer flag set)
sl@0
   487
	//or have reached the end of the play window; we only look at the play window here
sl@0
   488
	//if we are converting. For conversion we look at the data we have read. This is then passed onto 
sl@0
   489
	//the source
sl@0
   490
	if (!iSourceBuffer->BufferSize() || iSourceBuffer->LastBuffer() ||
sl@0
   491
		(((iState == EConverting) || (iState == EPlaying)) && (iPlayWindowEndPosition < iCachedSourceDuration) && ( InputPosition() >= iPlayWindowEndPosition ))) 
sl@0
   492
		{
sl@0
   493
		//When it resumes in silence , position of the buffer is in end so we need to skip the increament.
sl@0
   494
		if(!iPauseCalledInsilence) 
sl@0
   495
		{    
sl@0
   496
			iNumberOfTimesPlayed++;
sl@0
   497
		}else
sl@0
   498
		{
sl@0
   499
			iPauseCalledInsilence=EFalse;
sl@0
   500
		}
sl@0
   501
		if ((iNumberOfTimesPlayed <= iNumberOfTimesToRepeat) || iNumberOfTimesToRepeat == KMdaRepeatForever)
sl@0
   502
			{
sl@0
   503
			iSourceBuffer->SetLastBuffer(EFalse);
sl@0
   504
			//this will trigger the trailing silence timer next time a buffer is requested.
sl@0
   505
			iTrailingSilenceLeftToPlay = iTrailingSilence;
sl@0
   506
			iVerifyPlayComplete = ETrue;
sl@0
   507
			}
sl@0
   508
		else
sl@0
   509
			{		
sl@0
   510
	#ifdef _DP_DEBUG
sl@0
   511
			RDebug::Print(_L("DP::BufferFilledL end of input data  tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   512
			RDebug::Print(_L("iSourceBuffer->BufferSize()=%d\n"),iSourceBuffer->BufferSize());
sl@0
   513
			RDebug::Print(_L("iSourceBuffer->LastBuffer()=%d\n"),iSourceBuffer->LastBuffer());
sl@0
   514
			RDebug::Print(_L("InputPosition()=%d  >= iPlayWindowEndPosition=%d\n"),I64INT(InputPosition().Int64()),I64INT(iPlayWindowEndPosition.Int64()));
sl@0
   515
	#endif
sl@0
   516
			iNoMoreSourceData = ETrue;
sl@0
   517
			iSourceBuffer->SetLastBuffer(ETrue); //just in-case we are terminating on BufferSize == 0 or play window
sl@0
   518
			}
sl@0
   519
		}
sl@0
   520
sl@0
   521
sl@0
   522
	if (!iCodec)
sl@0
   523
		{
sl@0
   524
		ChangeDataPathTransferState(ESendDataToSink);
sl@0
   525
		}
sl@0
   526
	else if(!iSinkBufferWithSink) //sink buffer is with data path, can try to fill it
sl@0
   527
		{
sl@0
   528
		ChangeDataPathTransferState(ENeedToMatchSourceToSink);	
sl@0
   529
		}
sl@0
   530
	//else wait for sink to return buffer BufferEmptied will send us into ENeedToMatchSourceToSink state
sl@0
   531
sl@0
   532
#ifdef _DP_DEBUG
sl@0
   533
	RDebug::Print(_L("DP::BufferFilledL - DONE tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   534
#endif
sl@0
   535
	}
sl@0
   536
	
sl@0
   537
/** 
sl@0
   538
Sets the data path position.
sl@0
   539
sl@0
   540
@param  aPosition
sl@0
   541
		The data path position.
sl@0
   542
*/
sl@0
   543
void CMMFDataPath2::SetPositionL(const TTimeIntervalMicroSeconds& aPosition)
sl@0
   544
	{//need to map to source position to frame position 
sl@0
   545
#ifdef _DP_DEBUG
sl@0
   546
	RDebug::Print(_L("DP::SetPositionL = %d  ticks-%d   (this 0x%x)\n"),I64INT(aPosition.Int64()), User::TickCount(),this);
sl@0
   547
#endif
sl@0
   548
sl@0
   549
	if (iState == EStopped)
sl@0
   550
		{
sl@0
   551
		User::Leave(KErrNotReady); //can only set position if primed
sl@0
   552
		}
sl@0
   553
	
sl@0
   554
	if(iGetTimePlayedSupported)
sl@0
   555
		{
sl@0
   556
		TTimeIntervalMicroSeconds timePlayed(0);
sl@0
   557
		if(iState == EPlaying && iDataSink->DataSinkType() == KUidMmfAudioOutput)
sl@0
   558
			{
sl@0
   559
			CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink);
sl@0
   560
			CMMFDevSound& devSound = audioOutput->SoundDevice();
sl@0
   561
			TInt err= devSound.GetTimePlayed(timePlayed);
sl@0
   562
			if(err == KErrNone)
sl@0
   563
				{
sl@0
   564
				iDevSoundRepositionTime = timePlayed.Int64();
sl@0
   565
				}
sl@0
   566
			}
sl@0
   567
		else
sl@0
   568
			{
sl@0
   569
			iDevSoundRepositionTime = 0;
sl@0
   570
			}
sl@0
   571
		}
sl@0
   572
	else //roll back to samplesplayed
sl@0
   573
		{
sl@0
   574
		//As this will affect the position, we need to know how many bytes were 
sl@0
   575
		//played when position was updated. Future Position() requests will
sl@0
   576
		//then use this refernce to determine the current position.
sl@0
   577
		iReferenceAudioSamplesPlayed = AudioSamplesPlayed();
sl@0
   578
		}
sl@0
   579
	
sl@0
   580
	// Force the new position to be inside the play window (also within the file duration)
sl@0
   581
	if ( aPosition < iPlayWindowStartPosition )
sl@0
   582
		{
sl@0
   583
		iStartPosition = iPlayWindowStartPosition;
sl@0
   584
		}
sl@0
   585
	else if ( aPosition > iPlayWindowEndPosition )
sl@0
   586
		{
sl@0
   587
		iStartPosition = iPlayWindowEndPosition; //clearly this will cause nothing to be played
sl@0
   588
		}
sl@0
   589
	else
sl@0
   590
		{
sl@0
   591
		iStartPosition = aPosition;
sl@0
   592
		}
sl@0
   593
	
sl@0
   594
	TTimeIntervalMicroSeconds interval;
sl@0
   595
sl@0
   596
	//can only set the position on an MDataSource that is a format object
sl@0
   597
	//Note: position defaults to source if both source & sink are clips
sl@0
   598
	if (iDataSource->DataSourceType() == KUidMmfFormatDecode)
sl@0
   599
		{
sl@0
   600
		//position is not beyond the end of file
sl@0
   601
		interval = ((CMMFFormatDecode*)iDataSource)->FrameTimeInterval(iMediaId);
sl@0
   602
sl@0
   603
		// for some reason this code won't compile without these intermediate steps
sl@0
   604
		TInt64 position = iStartPosition.Int64();
sl@0
   605
		TInt64 interval64 = interval.Int64();
sl@0
   606
		if (interval64 == 0)
sl@0
   607
			User::Leave(KErrDivideByZero); 
sl@0
   608
		TInt64 datapos64 = position/interval64; 
sl@0
   609
		iCurrentSourceFrameNumber = I64LOW(datapos64);
sl@0
   610
sl@0
   611
sl@0
   612
        // Try to set the position directly on the format
sl@0
   613
        TRAP_IGNORE(((CMMFFormatDecode*)iDataSource)->SetPositionL(iStartPosition));
sl@0
   614
        //NB: don't worry about error, since we'll reposition anyway when we get the next buffer
sl@0
   615
		}
sl@0
   616
	else if (iDataSink->DataSinkType() == KUidMmfFormatEncode)
sl@0
   617
		{			
sl@0
   618
		//position is not beyond the end of file
sl@0
   619
		interval = ((CMMFFormatEncode*)iDataSink)->FrameTimeInterval(iMediaId);
sl@0
   620
sl@0
   621
		//convert to TUint - for some reason it won't compile without these intermediate steps
sl@0
   622
		TInt64 position = iStartPosition.Int64();
sl@0
   623
		TInt64 interval64 = interval.Int64();
sl@0
   624
		if (interval64 == 0)
sl@0
   625
			User::Leave(KErrDivideByZero); 
sl@0
   626
		TInt64 datapos64 = position/interval64; 
sl@0
   627
		iCurrentSinkFrameNumber = I64LOW(datapos64);
sl@0
   628
sl@0
   629
sl@0
   630
        // Try to set the position directly on the format
sl@0
   631
        TRAP_IGNORE(((CMMFFormatEncode*)iDataSink)->SetPositionL(iStartPosition));
sl@0
   632
        //NB: don't worry about error, since we'll reposition anyway when we get the next buffer
sl@0
   633
		}
sl@0
   634
	else
sl@0
   635
		{//can only set position if source or sink is a format
sl@0
   636
		//If both source and sink are formats position is relative to the source
sl@0
   637
		User::Leave(KErrNotSupported); //can't set position if neither source nor sink are clips
sl@0
   638
		}
sl@0
   639
sl@0
   640
	if(iCodec) //we have a real codec, must reset it
sl@0
   641
		{
sl@0
   642
		iCodec->ResetL(); // Need to preserve sync when resuming play
sl@0
   643
		}
sl@0
   644
		
sl@0
   645
	// Once we've sent the last buffer to the sink it's too late to start
sl@0
   646
	// changing the state since we may get a RunError(KErrUnderflow) at any time.
sl@0
   647
	// Once this happens, the sound driver may have unloaded etc..and recovery
sl@0
   648
	// would be complicated.
sl@0
   649
	if (iAllDataSentToSink)
sl@0
   650
		{
sl@0
   651
		return;
sl@0
   652
		}
sl@0
   653
#ifdef _DP_DEBUG
sl@0
   654
	RDebug::Print(_L("DP::SetPosition - Done iCurrentSourceFrameNumber=%d  iStartPosition=%d  ticks-%d   (this 0x%x)\n"),iCurrentSourceFrameNumber, I64INT(iStartPosition.Int64()), User::TickCount(),this);	
sl@0
   655
#endif
sl@0
   656
	}
sl@0
   657
sl@0
   658
/** 
sl@0
   659
Gets the data path position.
sl@0
   660
sl@0
   661
@return The data path position.
sl@0
   662
*/
sl@0
   663
TTimeIntervalMicroSeconds CMMFDataPath2::Position() const
sl@0
   664
	{
sl@0
   665
	if ((iState == ERecording) || (iState == EConverting))
sl@0
   666
		{
sl@0
   667
		return InputPosition();
sl@0
   668
		}
sl@0
   669
	else if(iState == EPlaying)
sl@0
   670
		{
sl@0
   671
		return OutputPosition();
sl@0
   672
		}
sl@0
   673
	else
sl@0
   674
		{
sl@0
   675
		return iStartPosition;
sl@0
   676
		}
sl@0
   677
	}
sl@0
   678
sl@0
   679
TTimeIntervalMicroSeconds CMMFDataPath2::OutputPosition() const
sl@0
   680
	{
sl@0
   681
	TTimeIntervalMicroSeconds interval;
sl@0
   682
	TTimeIntervalMicroSeconds position;
sl@0
   683
sl@0
   684
    if (iDataSink->DataSinkType() == KUidMmfAudioOutput)
sl@0
   685
        {
sl@0
   686
		position = CalculateAudioOutputPosition();
sl@0
   687
#ifdef _DP_DEBUG
sl@0
   688
		RDebug::Print(_L("DP::OutputPosition from audio output= %d\n"),I64INT(position.Int64()));
sl@0
   689
#endif
sl@0
   690
        }
sl@0
   691
	else if (iDataSink->DataSinkType() == KUidMmfFormatEncode)
sl@0
   692
		{
sl@0
   693
		//note Encode format position takes priority if both source & sink are formats?
sl@0
   694
        // try to get the position directly from the format. If that fails, work it out here
sl@0
   695
        TRAPD(error, position = ((CMMFFormatEncode*)iDataSink)->PositionL());
sl@0
   696
        if (error)//getting the position from the format didn't work so calculate it here
sl@0
   697
            {
sl@0
   698
		    interval = ((CMMFFormatEncode*)iDataSink)->FrameTimeInterval(iMediaId);
sl@0
   699
			TInt64 position64 = interval.Int64() * iCurrentSinkFrameNumber;
sl@0
   700
			position = position64;
sl@0
   701
            }
sl@0
   702
sl@0
   703
		TTimeIntervalMicroSeconds duration = CONST_CAST(CMMFDataPath2*,this)->Duration(); //need this to check position doesn't exceed duration
sl@0
   704
		if (position > duration)//this can happen on last buffer 
sl@0
   705
			{
sl@0
   706
			position = duration;
sl@0
   707
			}
sl@0
   708
#ifdef _DP_DEBUG
sl@0
   709
		RDebug::Print(_L("DP::OutputPosition  from format= %d\n"),I64INT(position.Int64()));
sl@0
   710
#endif
sl@0
   711
		}
sl@0
   712
	else
sl@0
   713
		{//can only read output position if sink is a format or an audio output
sl@0
   714
		return TTimeIntervalMicroSeconds(0);
sl@0
   715
		}
sl@0
   716
sl@0
   717
	return position;
sl@0
   718
	}
sl@0
   719
			
sl@0
   720
TTimeIntervalMicroSeconds CMMFDataPath2::CalculateAudioOutputPosition() const
sl@0
   721
    {
sl@0
   722
	//This operation can only be carried out on an Audio Output
sl@0
   723
	__ASSERT_ALWAYS(iDataSink->DataSinkType() == KUidMmfAudioOutput, Panic(EMMFDataPathPanicProgrammingError,__LINE__));
sl@0
   724
sl@0
   725
sl@0
   726
	//If we are not playing, simply return where we will play from
sl@0
   727
	if(iState != EPlaying || iCurrentSinkFrameNumber == 0 || iStartPosition == iPlayWindowEndPosition )
sl@0
   728
		{
sl@0
   729
		return iStartPosition;
sl@0
   730
		}
sl@0
   731
#ifdef _DP_DEBUG
sl@0
   732
	RDebug::Print(_L("DP::CalculateAudioOutputDuration from %d\n"),iReferenceAudioSamplesPlayed);
sl@0
   733
#endif
sl@0
   734
sl@0
   735
	CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink);
sl@0
   736
	CMMFDevSound& devSound = audioOutput->SoundDevice();
sl@0
   737
	
sl@0
   738
	TTimeIntervalMicroSeconds devSoundTimePlayed(0);
sl@0
   739
	TInt64 timePlayed(0);
sl@0
   740
	TInt err = KErrNone;
sl@0
   741
	if(iGetTimePlayedSupported)
sl@0
   742
		{
sl@0
   743
		err= devSound.GetTimePlayed(devSoundTimePlayed);
sl@0
   744
		if(err == KErrNone)
sl@0
   745
			{
sl@0
   746
			timePlayed = devSoundTimePlayed.Int64()-iDevSoundRepositionTime.Int64();
sl@0
   747
			}
sl@0
   748
		}
sl@0
   749
	else //Roll back to SamplesPlayed
sl@0
   750
		{
sl@0
   751
		TReal samplesPlayed = AudioSamplesPlayed() - iReferenceAudioSamplesPlayed;
sl@0
   752
		TMMFCapabilities devSoundConfig = devSound.Config();
sl@0
   753
		TInt samplingFreq = StreamUtils::SampleRateAsValue(devSoundConfig);
sl@0
   754
	#ifdef _DP_DEBUG
sl@0
   755
		RDebug::Print(_L("samplingFreq %d\n"),samplingFreq);
sl@0
   756
	#endif
sl@0
   757
		
sl@0
   758
		TReal timePlayedSeconds = 0;
sl@0
   759
		if(samplesPlayed)
sl@0
   760
			{
sl@0
   761
			timePlayedSeconds = samplesPlayed/samplingFreq;
sl@0
   762
			}
sl@0
   763
		timePlayed = I64DOUBLECAST(timePlayedSeconds * 1000000);
sl@0
   764
sl@0
   765
	#ifdef _DP_DEBUG
sl@0
   766
		RDebug::Print(_L("timePlayed %d\n"), I64LOW(timePlayed));
sl@0
   767
	#endif
sl@0
   768
		}
sl@0
   769
	if(err == KErrNone)
sl@0
   770
		{
sl@0
   771
		// When Resume is not supported. Datapath simulates Pause() through DevSound's Stop
sl@0
   772
		// the time played is lost. So we need to saved the last position
sl@0
   773
		// On the opposite, when Resume is supported the time played returned by DevSound 
sl@0
   774
		// reflects the real position, so there is no needed to recalculate at least playwindow is being used
sl@0
   775
		// Finally, if Resume is supported but is not used the position also need to be saved
sl@0
   776
		if(!iIsUsingResumeSupport || iPlayWindowStartPosition.Int64() > 0)
sl@0
   777
			{
sl@0
   778
			timePlayed = timePlayed + iStartPosition.Int64();	
sl@0
   779
			}
sl@0
   780
		
sl@0
   781
		//During repeats. we need to reset the positin manually to playstart once playend is reached
sl@0
   782
		//this is because the bytes returned by devsound are not accurate in all the cases
sl@0
   783
		if(iRepeatTrailingSilenceTimer->IsActive() || iTimeLeftToPlayComplete==0)//loop play
sl@0
   784
			{
sl@0
   785
			if(iTimeLeftToPlayComplete==0)
sl@0
   786
				{
sl@0
   787
				timePlayed = iPlayWindowStartPosition.Int64();
sl@0
   788
				}
sl@0
   789
			}
sl@0
   790
		else if(timePlayed>=(iPlayWindowEndPosition.Int64()+1))
sl@0
   791
			{
sl@0
   792
			timePlayed = iPlayWindowStartPosition.Int64();
sl@0
   793
			}
sl@0
   794
		}
sl@0
   795
	
sl@0
   796
	return TTimeIntervalMicroSeconds(timePlayed);
sl@0
   797
    }
sl@0
   798
   
sl@0
   799
/** 
sl@0
   800
Stops playing.
sl@0
   801
sl@0
   802
Resets datapath position - currently does not clean up buffers. Sends KMMFErrorCategoryDataPathGeneralError 
sl@0
   803
to the client if an error occurs.
sl@0
   804
*/
sl@0
   805
void CMMFDataPath2::Stop()
sl@0
   806
	{ 
sl@0
   807
#ifdef _DP_DEBUG
sl@0
   808
	RDebug::Print(_L("DP::Stop current state=%d  tick-%d   (this 0x%x)\n"), iTransferState, User::TickCount(),this);
sl@0
   809
#endif
sl@0
   810
	if ((iDataPathCreated)  && (iState != EStopped))
sl@0
   811
		{ 
sl@0
   812
		TRAPD(err, DoStopL());
sl@0
   813
		if (err)
sl@0
   814
			{
sl@0
   815
			DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err);
sl@0
   816
			}
sl@0
   817
		}
sl@0
   818
	}
sl@0
   819
sl@0
   820
void CMMFDataPath2::DoStopL()
sl@0
   821
	{
sl@0
   822
	CMMFDataPath::DoStopL();
sl@0
   823
	iRepeatTrailingSilenceTimer->Cancel();
sl@0
   824
	iIsUsingResumeSupport = EFalse;
sl@0
   825
	}
sl@0
   826
	
sl@0
   827
/** 
sl@0
   828
Pauses playing.
sl@0
   829
sl@0
   830
Sends KMMFErrorCategoryDataPathGeneralError to the client if an error occurs.
sl@0
   831
*/
sl@0
   832
void CMMFDataPath2::Pause()
sl@0
   833
	{
sl@0
   834
#ifdef _DP_DEBUG
sl@0
   835
	RDebug::Print(_L("DP::Pause, on src buff %d  sink buf %d   (this 0x%x)\n"), iCurrentSourceFrameNumber, iCurrentSinkFrameNumber,this);			
sl@0
   836
	RDebug::Print(_L("DP::Pause current state=%d  tick-%d    (this 0x%x)\n"),iTransferState, User::TickCount(),this);
sl@0
   837
#endif
sl@0
   838
sl@0
   839
	TRAPD(err, DoPauseL());
sl@0
   840
	
sl@0
   841
	if (err)
sl@0
   842
		{
sl@0
   843
		DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err);
sl@0
   844
		}
sl@0
   845
#ifdef _DP_DEBUG
sl@0
   846
	RDebug::Print(_L("DP::Pause - DONE tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   847
#endif
sl@0
   848
	}
sl@0
   849
sl@0
   850
EXPORT_C void CMMFDataPath2::PreEmptionPause()
sl@0
   851
    {
sl@0
   852
    TRAPD(err, DoPreEmptionPauseL());
sl@0
   853
    
sl@0
   854
    if (err)
sl@0
   855
        {
sl@0
   856
        DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err);
sl@0
   857
        }
sl@0
   858
    }
sl@0
   859
sl@0
   860
/** 
sl@0
   861
Pauses playing.
sl@0
   862
sl@0
   863
Sends KMMFErrorCategoryDataPathGeneralError to the client if an error occurs.
sl@0
   864
*/
sl@0
   865
void CMMFDataPath2::DoPreEmptionPauseL()
sl@0
   866
	{
sl@0
   867
sl@0
   868
    if ((iDataPathCreated) && ((iState == EPlaying) || (iState == EConverting) && ( iDataSink->DataSinkType() == KUidMmfAudioOutput )))
sl@0
   869
        {
sl@0
   870
        iDataSource->SourcePauseL();        // propagate state change to source
sl@0
   871
        SetPositionL(Position());
sl@0
   872
        iIsUsingResumeSupport = EFalse;
sl@0
   873
sl@0
   874
        iDataSink->SinkStopL();
sl@0
   875
	    iPauseCalled = ETrue;               // indicate pause is called
sl@0
   876
     
sl@0
   877
	    iState = EPrimed;                   // go back to primed state (we're not playing)
sl@0
   878
		
sl@0
   879
		iSinkBufferWithSink = EFalse;
sl@0
   880
		iSourceBufferWithSource = EFalse;
sl@0
   881
  
sl@0
   882
		ResetRefBuffers();                  // buffer references may not be valid any more
sl@0
   883
  
sl@0
   884
		Cancel(); //Stop the state machine		
sl@0
   885
	    }
sl@0
   886
	if(iState == ERecording)
sl@0
   887
	     {
sl@0
   888
	     User::Leave(KErrNotSupported);
sl@0
   889
	     }
sl@0
   890
	 iRepeatTrailingSilenceTimer->Cancel();
sl@0
   891
	}
sl@0
   892
	
sl@0
   893
void CMMFDataPath2::DoPauseL()
sl@0
   894
	{
sl@0
   895
#ifdef _DP_DEBUG
sl@0
   896
	RDebug::Print(_L("DP::DoPauseL tick-%d   (this 0x%x)\n"),User::TickCount(),this);
sl@0
   897
#endif
sl@0
   898
sl@0
   899
sl@0
   900
	if ((iDataPathCreated) && ((iState == EPlaying) || (iState == EConverting)))
sl@0
   901
		{
sl@0
   902
		if (iDataSink->DataSinkType() == KUidMmfAudioOutput && iIsResumeSupported)
sl@0
   903
			{
sl@0
   904
			iDataSink->SinkPauseL();
sl@0
   905
			// When true pause is supported only the datapath's position 
sl@0
   906
			// should be updated, MDataSource position should be changed
sl@0
   907
			iStartPosition = Position();
sl@0
   908
			iIsUsingResumeSupport = ETrue;
sl@0
   909
			if(iRepeatTrailingSilenceTimer->IsActive())
sl@0
   910
			{   
sl@0
   911
				iPauseCalledInsilence=ETrue;
sl@0
   912
			}   
sl@0
   913
			// If we wait for the sink to complete play, then we do not proceed with supplying the buffers to the sink
sl@0
   914
			// In this case we need to reset the buffers so that InitializeSinkL won't attempt bringing in new ones
sl@0
   915
			if (iTransferState == EWaitSink)
sl@0
   916
				{
sl@0
   917
				ResetRefBuffers();
sl@0
   918
				}
sl@0
   919
			}
sl@0
   920
		else
sl@0
   921
			{
sl@0
   922
			// If we use resume support, then there is no need to pause source as we would continue to supply buffers to the sink
sl@0
   923
			// Here we are not using resume support, thus we're pausing the source
sl@0
   924
			iDataSource->SourcePauseL();		
sl@0
   925
			SetPositionL(Position());
sl@0
   926
			iDataSink->SinkStopL();
sl@0
   927
			ResetRefBuffers();                  // buffer references may not be valid any more
sl@0
   928
  
sl@0
   929
			Cancel(); //Stop the state machine					
sl@0
   930
			}
sl@0
   931
		iPauseCalled = ETrue;				// indicate pause is called
sl@0
   932
		
sl@0
   933
		iState = EPrimed;					// go back to primed state (we're not playing)
sl@0
   934
		}
sl@0
   935
	else if(iState == ERecording)
sl@0
   936
		{
sl@0
   937
		iPauseCalled = ETrue;
sl@0
   938
#ifdef _DP_DEBUG
sl@0
   939
		RDebug::Print(_L("DP::DoPauseL Recording, pause datasource\n"));
sl@0
   940
#endif
sl@0
   941
		iDataSource->SourcePauseL();
sl@0
   942
		}
sl@0
   943
	iRepeatTrailingSilenceTimer->Cancel();
sl@0
   944
#ifdef _DP_DEBUG
sl@0
   945
	RDebug::Print(_L("DP::DoPauseL - Done iReferenceAudioSamplesPlayed = %d\n"),iReferenceAudioSamplesPlayed);
sl@0
   946
	RDebug::Print(_L("DP::DoPauseL - Done restart at %d tick-%d   (this 0x%x)\n"),I64INT(iStartPosition.Int64()), User::TickCount(),this);
sl@0
   947
#endif
sl@0
   948
	}
sl@0
   949
sl@0
   950
/**
sl@0
   951
Cancels the silence timer.
sl@0
   952
*/
sl@0
   953
void CMMFDataPath2::DoCancel()
sl@0
   954
	{
sl@0
   955
	//cancel repeats
sl@0
   956
	iRepeatTrailingSilenceTimer->Cancel();
sl@0
   957
	iNumberOfTimesToRepeat=0;
sl@0
   958
	}
sl@0
   959
sl@0
   960
/**
sl@0
   961
Allocates buffers in preparation to play.
sl@0
   962
sl@0
   963
Must be called before calling PlayL().
sl@0
   964
sl@0
   965
iSnkBufRef and iSrcBufRef contain ETrue if these buffers are created and owned by a MDataSource or MDataSink
sl@0
   966
For clean-up purposes, datapath only cleans up buffers allocated directly by PrimeL().
sl@0
   967
*/
sl@0
   968
void CMMFDataPath2::PrimeL()
sl@0
   969
	{
sl@0
   970
	CMMFDataPath::PrimeL();
sl@0
   971
	if(iDataSink->DataSinkType() == KUidMmfAudioOutput)
sl@0
   972
		{
sl@0
   973
		CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink);
sl@0
   974
		CMMFDevSound& devSound = audioOutput->SoundDevice();
sl@0
   975
		iGetTimePlayedSupported = devSound.IsGetTimePlayedSupported();
sl@0
   976
		iIsResumeSupported = devSound.IsResumeSupported();
sl@0
   977
		iIsUsingResumeSupport = EFalse;
sl@0
   978
		iPauseCalledInsilence = EFalse;
sl@0
   979
		}
sl@0
   980
	}
sl@0
   981
sl@0
   982
/**
sl@0
   983
Starts an active scheduler 'play' loop.
sl@0
   984
sl@0
   985
Can only play from the primed state.
sl@0
   986
*/
sl@0
   987
void CMMFDataPath2::PlayL()
sl@0
   988
	{
sl@0
   989
sl@0
   990
#if defined(__PROFILING)
sl@0
   991
	RDebug::ProfileEnd(1);
sl@0
   992
#endif  // defined(__PROFILING)
sl@0
   993
sl@0
   994
#ifdef _DP_DEBUG
sl@0
   995
	RDebug::Print(_L("DP::PlayL, on src buff %d  sink buf %d (this 0x%x)\n"), iCurrentSourceFrameNumber, iCurrentSinkFrameNumber,this);		
sl@0
   996
	RDebug::Print(_L("iStartPosition = %d\n"), I64INT(iStartPosition.Int64()));		
sl@0
   997
#endif
sl@0
   998
sl@0
   999
	if ((iDataPathCreated) && (iState == EPrimed))
sl@0
  1000
		{
sl@0
  1001
		//can only play from the primed state
sl@0
  1002
sl@0
  1003
		if(iPauseCalled) //sink and source will have been stopped, and we will not have been re-primed
sl@0
  1004
			{
sl@0
  1005
			//When pause is called silence,we need to send the buffer while resume so icansendbuffer should enabled
sl@0
  1006
			if(!iPauseCalledInsilence)
sl@0
  1007
			{
sl@0
  1008
				iDataSink->SinkPrimeL(); //propagate change down to sink
sl@0
  1009
			}
sl@0
  1010
			iPauseCalled = EFalse;
sl@0
  1011
			}
sl@0
  1012
sl@0
  1013
		iCurrentSourceFrameNumber = 0; //reset to beginning
sl@0
  1014
		iCurrentSinkFrameNumber = 0; //reset to beginning
sl@0
  1015
sl@0
  1016
		iSourceBufferWithSource = EFalse;
sl@0
  1017
		iSinkBufferWithSink = EFalse;
sl@0
  1018
sl@0
  1019
		iNoMoreSourceData = EFalse;
sl@0
  1020
		iAllDataSentToSink=EFalse;
sl@0
  1021
		iDataPathCompletedErrorCode=KErrNone;
sl@0
  1022
sl@0
  1023
		if(!iIsResumeSupported || !iIsUsingResumeSupport)
sl@0
  1024
			{
sl@0
  1025
			SetPositionL( iStartPosition );
sl@0
  1026
			}
sl@0
  1027
		iReferenceAudioSamplesPlayed = 0;
sl@0
  1028
		iReferenceAudioSamplesRecorded = 0;
sl@0
  1029
		
sl@0
  1030
		//complete a request on iStatus to invoke play code
sl@0
  1031
		iDataSource->SourcePlayL(); //propagate state change to source
sl@0
  1032
sl@0
  1033
		// This need to be done always since CMMFAudioOutput::EmptyBuffer
sl@0
  1034
		// doesn't start playback anymore
sl@0
  1035
		iDataSink->SinkPlayL(); //propogate state change to sink
sl@0
  1036
sl@0
  1037
		if (iDataSink->DataSinkType() == KUidMmfAudioOutput)
sl@0
  1038
			iState = EPlaying;
sl@0
  1039
		else if(iDataSource->DataSourceType() == KUidMmfAudioInput)
sl@0
  1040
			iState = ERecording;
sl@0
  1041
		else
sl@0
  1042
			iState = EConverting;
sl@0
  1043
sl@0
  1044
		//need to re-initialize any buffer(s) that we only own references to
sl@0
  1045
		ChangeDataPathTransferState(EInitializeSink);
sl@0
  1046
		}
sl@0
  1047
	iDevSoundRepositionTime = 0;
sl@0
  1048
	if(!iRetainRepeatInfo)
sl@0
  1049
		{
sl@0
  1050
		iNumberOfTimesPlayed = 0;
sl@0
  1051
		iTimeLeftToPlayComplete = -1;
sl@0
  1052
		iVerifyPlayComplete = EFalse;
sl@0
  1053
		}
sl@0
  1054
	iRetainRepeatInfo = EFalse;
sl@0
  1055
#ifdef _DP_DEBUG
sl@0
  1056
	RDebug::Print(_L("DP::Play - DONE\n"));		
sl@0
  1057
#endif
sl@0
  1058
	}
sl@0
  1059
sl@0
  1060
/**
sl@0
  1061
Deletes buffers if this datapath's sources and sinks own the buffers returned by PrimeL().
sl@0
  1062
Typically if buffers are created asychronously, the datapath doesn't own the buffer
sl@0
  1063
so leaves cleanup handling to the owner sources/sinks.
sl@0
  1064
sl@0
  1065
Called when source and sink needs to be de-referenced. Sets iDataPathCreated, iSinkCanReceive, 
sl@0
  1066
iSnkBufRef and iSrcBufRef to EFalse; sets iState to EStopped.
sl@0
  1067
*/
sl@0
  1068
void CMMFDataPath2::ResetL()
sl@0
  1069
	{
sl@0
  1070
	CMMFDataPath::ResetL();
sl@0
  1071
	iDrmSource = NULL;
sl@0
  1072
	}
sl@0
  1073
sl@0
  1074
/**
sl@0
  1075
Sets the number of times the audio sample is to be repeated during the
sl@0
  1076
playback operation.
sl@0
  1077
sl@0
  1078
A period of silence can follow each playing of the sample. The audio
sl@0
  1079
sample can be repeated indefinitely.
sl@0
  1080
sl@0
  1081
@param   aRepeatNumberOfTimes
sl@0
  1082
         The number of times the audio sample, together with
sl@0
  1083
         the trailing silence, is to be repeated. If this is
sl@0
  1084
         set to KMdaRepeatForever, then the audio
sl@0
  1085
         sample, together with the trailing silence, is
sl@0
  1086
         repeated indefinitely or until Stop() is
sl@0
  1087
         called. If this is set to zero, then the audio sample
sl@0
  1088
         is not repeated.
sl@0
  1089
@param   aTrailingSilence
sl@0
  1090
         The time interval of the trailing silence in microseconds.
sl@0
  1091
sl@0
  1092
*/
sl@0
  1093
EXPORT_C void CMMFDataPath2::SetRepeats(TInt aRepeatNumberOfTimes, const TTimeIntervalMicroSeconds& aTrailingSilence)
sl@0
  1094
	{
sl@0
  1095
	iNumberOfTimesToRepeat=aRepeatNumberOfTimes;
sl@0
  1096
	iTrailingSilence=aTrailingSilence;
sl@0
  1097
	}
sl@0
  1098
	
sl@0
  1099
/**
sl@0
  1100
Sets the Drm file source and the automatic execute intent flag. This method is used by the controller plugin
sl@0
  1101
to pass these to the datapath in order to execute the play intent during loop play.
sl@0
  1102
sl@0
  1103
@param  aSource
sl@0
  1104
        Data Source on which the play intent needs to be executed. This is usually the CMMFFile source
sl@0
  1105
@param  aDisableAutoIntent
sl@0
  1106
		Boolean variable which states whether the controller plugin or the datapath needs to execute play intent
sl@0
  1107
		automatically or not.
sl@0
  1108
*/	
sl@0
  1109
EXPORT_C void CMMFDataPath2::SetDrmProperties(MDataSource* aSource, TBool *aDisableAutoIntent)
sl@0
  1110
	{
sl@0
  1111
	iDrmSource = aSource;
sl@0
  1112
	iDisableAutoIntent = aDisableAutoIntent;
sl@0
  1113
	}
sl@0
  1114
sl@0
  1115
/**
sl@0
  1116
This call indicates PlayL not to reset the iNumberOfTimesPlayed property. This method is used by the controller plugin
sl@0
  1117
during repositioning. PlayL call during seeking should not reset the iNumberOfTimesPlayed property.
sl@0
  1118
*/
sl@0
  1119
EXPORT_C void CMMFDataPath2::RetainRepeatInfo()
sl@0
  1120
	{
sl@0
  1121
	iRetainRepeatInfo = ETrue;
sl@0
  1122
	}