os/mm/mmswadaptation/videorenderer/src/rendererrelay.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2007-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
/**
sl@0
    17
 @file
sl@0
    18
 @internalComponent
sl@0
    19
*/
sl@0
    20
sl@0
    21
#include <hal.h>
sl@0
    22
#include <graphics/suerror.h>
sl@0
    23
#include "rendererrelay.h"
sl@0
    24
#include "buflistener.h"
sl@0
    25
#include "buffermanager.h"
sl@0
    26
#include "videoframebuffer.h"
sl@0
    27
#include "rendererutil.h"
sl@0
    28
#include "renderertimer.h"
sl@0
    29
sl@0
    30
/** static factory contruction */
sl@0
    31
CRendererRelay* CRendererRelay::NewL(MVideoRendererObserver& aObserver)
sl@0
    32
	{
sl@0
    33
	CRendererRelay* self = new (ELeave) CRendererRelay(aObserver);
sl@0
    34
	CleanupStack::PushL(self);
sl@0
    35
	self->ConstructL();
sl@0
    36
	CleanupStack::Pop(self);
sl@0
    37
	return self;
sl@0
    38
	}
sl@0
    39
sl@0
    40
CRendererRelay::CRendererRelay(MVideoRendererObserver& aObserver)
sl@0
    41
:iObserver(aObserver), 
sl@0
    42
 iBufAvailListeners(CBufAvailListener::iOffset), 
sl@0
    43
 iAvailListenersIter(iBufAvailListeners)
sl@0
    44
	{
sl@0
    45
	}
sl@0
    46
sl@0
    47
void CRendererRelay::ConstructL()
sl@0
    48
	{
sl@0
    49
	User::LeaveIfError(iSurfaceUpdateSession.Connect());
sl@0
    50
	User::LeaveIfError(HAL::Get(HAL::EFastCounterFrequency, iFastCounterFrequency));
sl@0
    51
	User::LeaveIfError(HAL::Get(HAL::EFastCounterCountsUp, iFastCounterCountsUp));
sl@0
    52
	}
sl@0
    53
sl@0
    54
/** Destructor */
sl@0
    55
CRendererRelay::~CRendererRelay()
sl@0
    56
	{
sl@0
    57
	delete iDisplayListener;
sl@0
    58
	
sl@0
    59
	iAvailListenersIter.SetToFirst();
sl@0
    60
	CBufAvailListener* listener = NULL;
sl@0
    61
	while ((listener = iAvailListenersIter++) != NULL)
sl@0
    62
		{
sl@0
    63
		listener->iDblQueLink.Deque();
sl@0
    64
		delete listener;
sl@0
    65
		}
sl@0
    66
	
sl@0
    67
	iSurfaceUpdateSession.Close();
sl@0
    68
	}
sl@0
    69
sl@0
    70
/** Update buffer manager pointer in this class and listeners */
sl@0
    71
void CRendererRelay::SetBufferManager(CRendererBufferManager* aBufManager)
sl@0
    72
	{
sl@0
    73
	iBufManager = aBufManager;
sl@0
    74
	
sl@0
    75
	// change buffer manager pointer for listeners
sl@0
    76
	iAvailListenersIter.SetToFirst();
sl@0
    77
	CBufAvailListener* listener = NULL;
sl@0
    78
	while ((listener = iAvailListenersIter++) != NULL)
sl@0
    79
		{
sl@0
    80
		listener->SetBufferManager(aBufManager);
sl@0
    81
		
sl@0
    82
		if (!aBufManager && listener->IsAdded())
sl@0
    83
			{
sl@0
    84
			listener->Deque();
sl@0
    85
			}
sl@0
    86
		else if (aBufManager && !listener->IsAdded())
sl@0
    87
			{
sl@0
    88
			CActiveScheduler::Add(listener);
sl@0
    89
			}
sl@0
    90
		}
sl@0
    91
	}
sl@0
    92
sl@0
    93
/** Return the next unused CBufAvailListener. */
sl@0
    94
CBufAvailListener* CRendererRelay::BufAvailListener()
sl@0
    95
	{
sl@0
    96
	CBufAvailListener* listener = NULL;
sl@0
    97
	iAvailListenersIter.SetToFirst();
sl@0
    98
	while ((listener = iAvailListenersIter++) != NULL)
sl@0
    99
		{
sl@0
   100
		if (listener->IsActive() == EFalse)
sl@0
   101
			{
sl@0
   102
			// Move to end so that the next search is a bit faster
sl@0
   103
			listener->iDblQueLink.Deque();
sl@0
   104
			iBufAvailListeners.AddLast(*listener);
sl@0
   105
			return listener;
sl@0
   106
			}
sl@0
   107
		}
sl@0
   108
sl@0
   109
	// Should not reach here as the number of listeners is same as number of buffer
sl@0
   110
	__ASSERT_DEBUG(EFalse, User::Panic(_L("CRR::BufAvailListener"), KErrUnknown));
sl@0
   111
	return listener;
sl@0
   112
	}
sl@0
   113
sl@0
   114
/** Set update parameter and create listeners */
sl@0
   115
void CRendererRelay::PrepareL(const TSurfaceId& aSurfaceId, TInt aNumBuffers, TRequestStatus* /*aRequestStatus*/)
sl@0
   116
	{
sl@0
   117
	iUpdateSubmitted = EFalse;
sl@0
   118
	iSurfaceId = aSurfaceId;
sl@0
   119
	
sl@0
   120
	if (!iDisplayListener)
sl@0
   121
		{
sl@0
   122
		iDisplayListener = CBufDisplayListener::NewL(iObserver, iSurfaceUpdateSession, *this, iFastCounterFrequency, iFastCounterCountsUp);
sl@0
   123
		}
sl@0
   124
sl@0
   125
	TInt numListeners = 0;
sl@0
   126
	iAvailListenersIter.SetToFirst();
sl@0
   127
	while (iAvailListenersIter++ != NULL)
sl@0
   128
		{
sl@0
   129
		numListeners++;
sl@0
   130
		}
sl@0
   131
	
sl@0
   132
	CBufAvailListener* listener = NULL;
sl@0
   133
	// create new listeners if there are more numBuffers than listeners
sl@0
   134
	for (TInt i = numListeners; i < aNumBuffers; ++i)
sl@0
   135
		{
sl@0
   136
		listener = CBufAvailListener::NewL(iObserver, iSurfaceUpdateSession);
sl@0
   137
		iBufAvailListeners.AddFirst(*listener);
sl@0
   138
		}
sl@0
   139
	}
sl@0
   140
sl@0
   141
/** Handle update buffer request in non-timed mode */
sl@0
   142
void CRendererRelay::UpdateBuffer(TVideoFrameBuffer* aBuffer, const TTime& aPresentationTime, TRequestStatus* /* aRequestStatus */)
sl@0
   143
	{
sl@0
   144
	__ASSERT_DEBUG(iBufManager != NULL, User::Panic(_L("CRR::UpdateBuffer"), KErrNotReady));
sl@0
   145
	
sl@0
   146
	iBufManager->UpdateBuffer(aBuffer, aPresentationTime);
sl@0
   147
	
sl@0
   148
	if (iUpdateSubmitted == EFalse)
sl@0
   149
		{
sl@0
   150
		// an update haven't been submitted, submit one now
sl@0
   151
		SubmitBuffer();
sl@0
   152
		}
sl@0
   153
	}
sl@0
   154
sl@0
   155
/** Callback from display listener that a buffer has been displayed or skipped*/
sl@0
   156
void CRendererRelay::BufferDisplayed(TBool aDisplayed, TInt64 aDelay)
sl@0
   157
	{
sl@0
   158
	DEBUGPRINT3(_L("CRendererRelay::BufferDisplayed entered aDisplayed=%d, aDelay=%Ld"), aDisplayed, aDelay);
sl@0
   159
sl@0
   160
	iUpdateSubmitted = EFalse;
sl@0
   161
	
sl@0
   162
	if (iRendererTimer)
sl@0
   163
		{
sl@0
   164
		// rendering in timed mode
sl@0
   165
		if (aDisplayed)
sl@0
   166
			{
sl@0
   167
			// update delay value
sl@0
   168
			iDelay = aDelay;
sl@0
   169
			}
sl@0
   170
		SubmitBufferTimed();
sl@0
   171
		}
sl@0
   172
	else
sl@0
   173
		{
sl@0
   174
		// rendering in non timed mode
sl@0
   175
		SubmitBuffer();
sl@0
   176
		}
sl@0
   177
	}
sl@0
   178
sl@0
   179
/** Submit the next buffer that is waiting to be submitted in non-timed mode*/
sl@0
   180
void CRendererRelay::SubmitBuffer()
sl@0
   181
	{
sl@0
   182
	DEBUGPRINT1(_L("CRendererRelay::SubmitBuffer entered"));
sl@0
   183
	__ASSERT_DEBUG(iBufManager != NULL, User::Panic(_L("CRR::SubmitBuffer"), KErrCorrupt));
sl@0
   184
	__ASSERT_DEBUG(iUpdateSubmitted == EFalse, User::Panic(_L("CRR::SubmitBuffer"), KErrGeneral));
sl@0
   185
sl@0
   186
	TBool lastBuf;
sl@0
   187
	TVideoFrameBuffer* buf = iBufManager->WaitingBuffer(ETrue, lastBuf);
sl@0
   188
	TInt numBufferSkipped = 0;
sl@0
   189
	
sl@0
   190
	TTime now;
sl@0
   191
	now.UniversalTime();
sl@0
   192
	TUint32 fastCounter = User::FastCounter();
sl@0
   193
	while (buf != NULL)
sl@0
   194
		{
sl@0
   195
		TInt bufId = buf->BufferId();
sl@0
   196
		
sl@0
   197
		DEBUGPRINT4(_L("bufId=%d presTime=%Ld, now=%Ld"), 
sl@0
   198
					bufId, buf->PresentationTime().Int64(), now.Int64());
sl@0
   199
sl@0
   200
		// submit the buffer for update if presntation time is not in past 
sl@0
   201
		// or if the buffer is the last in queue or presentation time is zero
sl@0
   202
		if (buf->PresentationTime() >= now || lastBuf || buf->PresentationTime().Int64() == 0)
sl@0
   203
			{
sl@0
   204
			DoUpdateBuffer(bufId, now, fastCounter);
sl@0
   205
			break;
sl@0
   206
			}
sl@0
   207
		
sl@0
   208
		// The buffer presentation time occurs in past if codeflow reaches here.
sl@0
   209
		// Change the buffer status to available and notify observer about the skipped buffer
sl@0
   210
		iBufManager->BufferAvailable(bufId);
sl@0
   211
		iObserver.MvroBufferSkipped(bufId);
sl@0
   212
		numBufferSkipped++;
sl@0
   213
sl@0
   214
		// get next buffer
sl@0
   215
		buf = iBufManager->WaitingBuffer(ETrue, lastBuf);
sl@0
   216
		}
sl@0
   217
	
sl@0
   218
	//notifiy observer about the available buffers
sl@0
   219
	for (TInt i = 0; i < numBufferSkipped; ++i)
sl@0
   220
		{
sl@0
   221
		iObserver.MvroVideoBufferAvailable();
sl@0
   222
		}
sl@0
   223
	}
sl@0
   224
sl@0
   225
/** Set a pointer to renderer timer in timed mode */
sl@0
   226
void CRendererRelay::SetRendererTimer(CRendererTimer* aRendererTimer)
sl@0
   227
	{
sl@0
   228
	iRendererTimer = aRendererTimer;
sl@0
   229
	}
sl@0
   230
sl@0
   231
/** Callback function when a renderer timer has expired */
sl@0
   232
void CRendererRelay::RendererTimerExpired()
sl@0
   233
	{
sl@0
   234
	SubmitBufferTimed();
sl@0
   235
	}
sl@0
   236
sl@0
   237
/** Submit the next buffer in timed mode */
sl@0
   238
void CRendererRelay::SubmitBufferTimed()
sl@0
   239
	{
sl@0
   240
	DEBUGPRINT1(_L("CRendererRelay::SubmitBufferTimed entered"));
sl@0
   241
sl@0
   242
	__ASSERT_DEBUG(iBufManager != NULL, User::Panic(_L("CRR::SubmitBufferTimed"), KErrCorrupt));
sl@0
   243
	__ASSERT_DEBUG(iUpdateSubmitted == EFalse, 
sl@0
   244
			User::Panic(_L("CRR::SubmitBufferTimed"), KErrGeneral));
sl@0
   245
	__ASSERT_DEBUG(iRendererTimer && iRendererTimer->IsActive() == EFalse, 
sl@0
   246
					User::Panic(_L("CRR::SubmitBufferTimed"), KErrInUse));
sl@0
   247
	
sl@0
   248
	TBool lastBuf;
sl@0
   249
	TVideoFrameBuffer* buf = iBufManager->WaitingBuffer(EFalse, lastBuf);
sl@0
   250
	TInt numBufferSkipped = 0;
sl@0
   251
	
sl@0
   252
	TTime now;
sl@0
   253
	now.UniversalTime();
sl@0
   254
	TUint32 fastCounter = User::FastCounter();
sl@0
   255
	while (buf != NULL)
sl@0
   256
		{
sl@0
   257
		TInt bufId = buf->BufferId();
sl@0
   258
		TTimeIntervalMicroSeconds deltaFromNow = buf->PresentationTime().MicroSecondsFrom(now);
sl@0
   259
		
sl@0
   260
		TInt64 waitTime = 0;
sl@0
   261
		if (buf->PresentationTime().Int64() != 0)
sl@0
   262
			{
sl@0
   263
			// presentation time is not zero, calculate wait time. Otherwise, wait time is zero.
sl@0
   264
			waitTime = deltaFromNow.Int64() - iDelay;
sl@0
   265
			}
sl@0
   266
		
sl@0
   267
		DEBUGPRINT4(_L("bufId=%d presTime=%Ld, now=%Ld"), 
sl@0
   268
				bufId, buf->PresentationTime().Int64(), now.Int64());
sl@0
   269
sl@0
   270
		// submit the buffer for update if presntation time is not in past 
sl@0
   271
		// or if the buffer is the last in queue
sl@0
   272
		if (waitTime > 0)
sl@0
   273
			{
sl@0
   274
			iRendererTimer->Start(waitTime);
sl@0
   275
			break;
sl@0
   276
			}
sl@0
   277
		else if (buf->PresentationTime().Int64() == 0 ||
sl@0
   278
					deltaFromNow.Int64() + iMaxDelay >= 0 || 
sl@0
   279
					lastBuf)
sl@0
   280
			{
sl@0
   281
			// if presentation time is zero (waitTime is not used for checking because it may be zero from calculation)
sl@0
   282
			// or the frame is within maximun allowed delay, i.e. (presentation time + max delay >= now)
sl@0
   283
			// or submission time has passed but this is the last buf, submit the buffer now
sl@0
   284
sl@0
   285
			iBufManager->BufferSubmitted(buf);
sl@0
   286
			DoUpdateBuffer(bufId, now, fastCounter);
sl@0
   287
			break;
sl@0
   288
			}
sl@0
   289
		
sl@0
   290
		// The buffer presentation time has passed maxDelay if codeflow reaches here, skip the buffer.
sl@0
   291
		// Change the buffer status to available and notify observer
sl@0
   292
		iBufManager->BufferAvailable(bufId);
sl@0
   293
		iObserver.MvroBufferSkipped(bufId);
sl@0
   294
		numBufferSkipped++;
sl@0
   295
sl@0
   296
		// get next buffer
sl@0
   297
		buf = iBufManager->WaitingBuffer(EFalse, lastBuf);
sl@0
   298
		}
sl@0
   299
	
sl@0
   300
	//notifiy observer about the available buffers
sl@0
   301
	for (TInt i = 0; i < numBufferSkipped; ++i)
sl@0
   302
		{
sl@0
   303
		iObserver.MvroVideoBufferAvailable();
sl@0
   304
		}
sl@0
   305
	}
sl@0
   306
sl@0
   307
/**
sl@0
   308
Submits a buffer to be updated at the specified time.
sl@0
   309
*/
sl@0
   310
sl@0
   311
void CRendererRelay::DoUpdateBuffer(TInt aBufferId, const TTime& aTime, TUint32 aFastCounter)
sl@0
   312
	{
sl@0
   313
	CBufAvailListener* availListener = BufAvailListener();
sl@0
   314
			
sl@0
   315
	availListener->Start(aBufferId);
sl@0
   316
	iDisplayListener->Start(aBufferId, aTime, aFastCounter);
sl@0
   317
	
sl@0
   318
	TInt err = iSurfaceUpdateSession.SubmitUpdate(KAllScreens, iSurfaceId, aBufferId);
sl@0
   319
sl@0
   320
	DEBUGPRINT2(_L("SubmitUpdate return %d"), err);
sl@0
   321
			
sl@0
   322
	// error will also be returned from listener, so the next submit updated will be triggered by display listener
sl@0
   323
	iUpdateSubmitted = ETrue;
sl@0
   324
	}
sl@0
   325
sl@0
   326
/** 
sl@0
   327
Return ETrue if an update has been submitted and the display notification 
sl@0
   328
haven't been received yet. i.e. Need to wait till for listener callback before 
sl@0
   329
the next buffer can be submitted.
sl@0
   330
*/
sl@0
   331
TBool CRendererRelay::UpdateSubmitted()
sl@0
   332
	{
sl@0
   333
	return iUpdateSubmitted;
sl@0
   334
	}
sl@0
   335
sl@0
   336
/** return the delay for surface update */
sl@0
   337
TInt64 CRendererRelay::Delay()
sl@0
   338
	{
sl@0
   339
	return iDelay;
sl@0
   340
	}
sl@0
   341
sl@0
   342
/** Cancel all update notification when a surface is destroyed */
sl@0
   343
void CRendererRelay::DestroySurface(TRequestStatus* /* aRequestStatus */ )
sl@0
   344
	{
sl@0
   345
	iSurfaceUpdateSession.CancelAllUpdateNotifications();
sl@0
   346
	}
sl@0
   347
sl@0
   348
/* This function is not used */
sl@0
   349
void CRendererRelay::SetRendererThread(RThread* /* aRendererThread */)
sl@0
   350
	{
sl@0
   351
	__ASSERT_DEBUG(EFalse, User::Panic(_L("CRR::SetRendererThread"), KErrUnknown));
sl@0
   352
	}
sl@0
   353
sl@0
   354
/* This function is not used */
sl@0
   355
void CRendererRelay::Terminate(TRequestStatus& /* aRequestStatus */)
sl@0
   356
	{
sl@0
   357
	__ASSERT_DEBUG(EFalse, User::Panic(_L("CRR::Terminate"), KErrUnknown));
sl@0
   358
	}
sl@0
   359
sl@0
   360
/** Set timer info for timed mode, this function is not called in non-timed mode */
sl@0
   361
void CRendererRelay::SetTimerInfo(TInt64 aDefaultDelay, TInt64 aMaxDelay)
sl@0
   362
	{
sl@0
   363
	iDelay = aDefaultDelay;
sl@0
   364
	iMaxDelay = aMaxDelay;
sl@0
   365
	}
sl@0
   366
sl@0
   367
/** Two-phased constructor. */
sl@0
   368
CRendererThreadRelay* CRendererThreadRelay::NewL(MVideoRendererObserver& aObserver, TThreadId aMainThreadId)
sl@0
   369
	{
sl@0
   370
	CRendererThreadRelay* self = new (ELeave) CRendererThreadRelay(aObserver);
sl@0
   371
	CleanupStack::PushL(self);
sl@0
   372
	self->ConstructL(aMainThreadId);
sl@0
   373
	CleanupStack::Pop(self);
sl@0
   374
	return self;
sl@0
   375
	}
sl@0
   376
sl@0
   377
CRendererThreadRelay::CRendererThreadRelay(MVideoRendererObserver& aObserver) 
sl@0
   378
: CActive(EPriorityStandard), iObserver(aObserver)
sl@0
   379
	{
sl@0
   380
	CActiveScheduler::Add(this);
sl@0
   381
	}
sl@0
   382
sl@0
   383
/** Second-phase constructor */
sl@0
   384
void CRendererThreadRelay::ConstructL(TThreadId aMainThreadId)
sl@0
   385
	{
sl@0
   386
	User::LeaveIfError(iMainThread.Open(aMainThreadId));
sl@0
   387
	iRendererRelay = CRendererRelay::NewL(*this);
sl@0
   388
	iRendererTimer = CRendererTimer::NewL(*iRendererRelay);
sl@0
   389
	iRendererRelay->SetRendererTimer(iRendererTimer);
sl@0
   390
	}
sl@0
   391
sl@0
   392
/** 
sl@0
   393
Destructor. 
sl@0
   394
This active object is always cancelled by main thread so no need to cancel this active object here
sl@0
   395
*/
sl@0
   396
CRendererThreadRelay::~CRendererThreadRelay()
sl@0
   397
	{
sl@0
   398
	delete iRendererCallbackListener;
sl@0
   399
	delete iRendererTimer;
sl@0
   400
	delete iRendererRelay;
sl@0
   401
	iMainThread.Close();
sl@0
   402
	}
sl@0
   403
sl@0
   404
void CRendererThreadRelay::DoCancel()
sl@0
   405
	{
sl@0
   406
	// Don't need to do anything, will be stopped by main thread
sl@0
   407
	}
sl@0
   408
sl@0
   409
/** Function for making the initial request */
sl@0
   410
void CRendererThreadRelay::Start()
sl@0
   411
	{
sl@0
   412
	__ASSERT_DEBUG(IsActive() == EFalse, User::Panic(_L("CRTR::Start"), KErrInUse));
sl@0
   413
	iStatus = KRequestPending;
sl@0
   414
	SetActive();
sl@0
   415
	}
sl@0
   416
sl@0
   417
/** Handle requests from main thread */
sl@0
   418
void CRendererThreadRelay::RunL()
sl@0
   419
	{
sl@0
   420
	__ASSERT_DEBUG(iStatus == KErrNone, User::Panic(_L("CRTR::RunL"), KErrUnknown));
sl@0
   421
	TInt result = KErrNone;
sl@0
   422
sl@0
   423
	if (iFunctionCode == ESubmitUpdate)
sl@0
   424
		{
sl@0
   425
		Start();
sl@0
   426
		RunUpdateBuffer(iBuffer, iPresentationTime);
sl@0
   427
		}
sl@0
   428
	else if (iFunctionCode == EDestroySurface)
sl@0
   429
		{
sl@0
   430
		Start();
sl@0
   431
		RunDestroySurface();
sl@0
   432
		}
sl@0
   433
	else if (iFunctionCode == EPrepare)
sl@0
   434
		{
sl@0
   435
		Start();
sl@0
   436
		TRAP(result, RunPrepareL());
sl@0
   437
		}
sl@0
   438
	else if (iFunctionCode == ESetBufferManager)
sl@0
   439
		{
sl@0
   440
		Start();
sl@0
   441
		RunSetBufferManager();
sl@0
   442
		}
sl@0
   443
	else // iFunctionCode == ETermination
sl@0
   444
		{
sl@0
   445
		CActiveScheduler::Stop();
sl@0
   446
		}
sl@0
   447
sl@0
   448
	TRequestStatus *status = iCallingStatus;
sl@0
   449
	iMainThread.RequestComplete(status, result);
sl@0
   450
	}
sl@0
   451
sl@0
   452
/** Send a signal to the main thread to indicate that the thread startup was successful. */
sl@0
   453
void CRendererThreadRelay::SignalSetupComplete(TRequestStatus* aSetupComplete)
sl@0
   454
	{
sl@0
   455
	iMainThread.RequestComplete(aSetupComplete, KErrNone);
sl@0
   456
	}
sl@0
   457
sl@0
   458
/** Send update buffer request from main thread to renderer thread */
sl@0
   459
void CRendererThreadRelay::UpdateBuffer(TVideoFrameBuffer* aBuffer, const TTime& aPresentationTime, TRequestStatus* aRequestStatus)
sl@0
   460
	{
sl@0
   461
	__ASSERT_DEBUG(aRequestStatus != NULL, User::Panic(_L("CRTR::UpdateBuffer"), KErrArgument));
sl@0
   462
sl@0
   463
	// set function parameters
sl@0
   464
	iCallingStatus = aRequestStatus;
sl@0
   465
	iFunctionCode = ESubmitUpdate;
sl@0
   466
	iBuffer = aBuffer;
sl@0
   467
	iPresentationTime = aPresentationTime;
sl@0
   468
	
sl@0
   469
	// send request to renderer thread
sl@0
   470
	TRequestStatus* rendererRequest = Status();
sl@0
   471
	iRendererThread->RequestComplete(rendererRequest, KErrNone);
sl@0
   472
	}
sl@0
   473
sl@0
   474
/** Send terminate renderer thread request from main thread to renderer thread */
sl@0
   475
void CRendererThreadRelay::Terminate(TRequestStatus& aRequestStatus)
sl@0
   476
	{
sl@0
   477
	iCallingStatus = &aRequestStatus;
sl@0
   478
	iFunctionCode = ETermination;
sl@0
   479
	
sl@0
   480
	if (iRendererCallbackListener)
sl@0
   481
		{
sl@0
   482
		iRendererCallbackListener->Cancel();
sl@0
   483
		}
sl@0
   484
	
sl@0
   485
	// send request to renderer thread
sl@0
   486
	TRequestStatus* rendererRequest = Status();
sl@0
   487
	iRendererThread->RequestComplete(rendererRequest, KErrNone);
sl@0
   488
	}
sl@0
   489
sl@0
   490
/** Send destroy surface request from main thread to renderer thread */
sl@0
   491
void CRendererThreadRelay::DestroySurface(TRequestStatus* aRequestStatus)
sl@0
   492
	{
sl@0
   493
	__ASSERT_DEBUG(aRequestStatus != NULL, User::Panic(_L("CRTR::DestroySurface"), KErrArgument));
sl@0
   494
	
sl@0
   495
	iCallingStatus = aRequestStatus;
sl@0
   496
	iFunctionCode = EDestroySurface;
sl@0
   497
	
sl@0
   498
	// send request to renderer thread
sl@0
   499
	TRequestStatus* rendererRequest = Status();
sl@0
   500
	iRendererThread->RequestComplete(rendererRequest, KErrNone);
sl@0
   501
	}
sl@0
   502
sl@0
   503
/* Prepare the object after a surface is created */
sl@0
   504
void CRendererThreadRelay::PrepareL(const TSurfaceId& aSurfaceId, TInt aNumBuffers, TRequestStatus* aRequestStatus)
sl@0
   505
	{
sl@0
   506
	__ASSERT_DEBUG(aRequestStatus != NULL, User::Panic(_L("CRTR::PrepareL"), KErrArgument));
sl@0
   507
	
sl@0
   508
	if(!iRendererCallbackListener)
sl@0
   509
		{
sl@0
   510
		// first, create callback listener in the main thread
sl@0
   511
		iRendererCallbackListener = CRendererCallbackListener::NewL(iObserver, aNumBuffers);
sl@0
   512
		iRendererCallbackListener->Start();		
sl@0
   513
		}
sl@0
   514
	else if (iNumBuffers < aNumBuffers)
sl@0
   515
		{
sl@0
   516
		iRendererCallbackListener->Cancel();
sl@0
   517
		iRendererCallbackListener->ExtendMsgQueueL(aNumBuffers);
sl@0
   518
		iRendererCallbackListener->Start();		
sl@0
   519
		}
sl@0
   520
sl@0
   521
	// set function parameters
sl@0
   522
	iCallingStatus = aRequestStatus;
sl@0
   523
	iFunctionCode = EPrepare;
sl@0
   524
	iSurfaceId = aSurfaceId;
sl@0
   525
	iNumBuffers = aNumBuffers;
sl@0
   526
	
sl@0
   527
	// send request to renderer thread
sl@0
   528
	TRequestStatus* rendererRequest = Status();
sl@0
   529
	iRendererThread->RequestComplete(rendererRequest, KErrNone);
sl@0
   530
	}
sl@0
   531
sl@0
   532
/* Prepare the object after a surface is created */
sl@0
   533
void CRendererThreadRelay::RunPrepareL()
sl@0
   534
	{
sl@0
   535
	iRendererRelay->PrepareL(iSurfaceId, iNumBuffers, NULL);
sl@0
   536
	}
sl@0
   537
sl@0
   538
/** Run update buffer in renderer thread */
sl@0
   539
void CRendererThreadRelay::RunUpdateBuffer(TVideoFrameBuffer* aBuffer, const TTime& aPresentationTime)
sl@0
   540
	{
sl@0
   541
	DEBUGPRINT1(_L("CRendererThreadRelay::RunUpdateBuffer entered"));
sl@0
   542
	__ASSERT_DEBUG(iBufManager != NULL, User::Panic(_L("CRTR::RunUpdateBuffer"), KErrCorrupt));
sl@0
   543
sl@0
   544
	/*
sl@0
   545
	Buffer update is determined as follow:
sl@0
   546
	 
sl@0
   547
	If wait list is empty (imply no active timer), always add buffer to list, 
sl@0
   548
		If a preceding buffer hasn't been displayed, the new buffer will be handled after display callback.
sl@0
   549
			otherwise, decide whether timer should be started for submit update
sl@0
   550
	If wait list is not empty (imply either timer is active or waiting for display callback)
sl@0
   551
		If waiting for display callback, add new buffer to list and it will be handled after display callback.
sl@0
   552
			(note: presentation time is not check because the new buffer may be newer than the waiting buffers even though both have passed due time)
sl@0
   553
		If timer is active, first check if presentation time is zero. If so, display right away
sl@0
   554
			otherwise, then check if this frame can be timed (presentation time - delay <= now), if not, check max display to skip the frame or display right away
sl@0
   555
			otherwise, then check if presentation time < head list presentation time
sl@0
   556
				if so, add the buffer to wait list, stop the timer and start timer with new time
sl@0
   557
				else, just need to add buffer to wait list, the next timer expiry will hander the head buffer
sl@0
   558
	*/
sl@0
   559
	
sl@0
   560
	if (iBufManager->WaitingListIsEmpty())
sl@0
   561
		{
sl@0
   562
		iBufManager->UpdateBuffer(aBuffer, aPresentationTime);
sl@0
   563
sl@0
   564
		if (iRendererRelay->UpdateSubmitted() == EFalse)
sl@0
   565
			{
sl@0
   566
			// an update haven't been submitted, prepare to submit one now
sl@0
   567
			iRendererRelay->SubmitBufferTimed();
sl@0
   568
			}
sl@0
   569
		}
sl@0
   570
	else
sl@0
   571
		{
sl@0
   572
		// waiting list is not empty
sl@0
   573
		if (iRendererRelay->UpdateSubmitted())
sl@0
   574
			{
sl@0
   575
			// waiting for listener callback, just update waiting list
sl@0
   576
			iBufManager->UpdateBuffer(aBuffer, aPresentationTime);
sl@0
   577
			}
sl@0
   578
		else
sl@0
   579
			{
sl@0
   580
			// the timer is waiting
sl@0
   581
sl@0
   582
			if (aPresentationTime.Int64() == 0)
sl@0
   583
				{
sl@0
   584
				// presentation time is zero, display right away.
sl@0
   585
				iRendererTimer->Cancel();
sl@0
   586
				iBufManager->UpdateBuffer(aBuffer, aPresentationTime);
sl@0
   587
				iRendererRelay->SubmitBufferTimed();
sl@0
   588
				return;
sl@0
   589
				}
sl@0
   590
sl@0
   591
			TTime now;
sl@0
   592
			now.UniversalTime();
sl@0
   593
			TTimeIntervalMicroSeconds deltaFromNow = aPresentationTime.MicroSecondsFrom(now);
sl@0
   594
			TInt64 waitTime = deltaFromNow.Int64() - iRendererRelay->Delay();
sl@0
   595
				
sl@0
   596
			if (waitTime <= 0)
sl@0
   597
				{
sl@0
   598
				// the wait time has passed
sl@0
   599
				if (deltaFromNow.Int64() + iMaxDelay >= 0)
sl@0
   600
					{
sl@0
   601
					// the frame is within maximun allowed delay, i.e. (presentation time + max delay >= now)
sl@0
   602
					// display right away
sl@0
   603
					iRendererTimer->Cancel();
sl@0
   604
					iBufManager->UpdateBuffer(aBuffer, aPresentationTime);
sl@0
   605
					iRendererRelay->SubmitBufferTimed();
sl@0
   606
					}
sl@0
   607
				else
sl@0
   608
					{
sl@0
   609
					// skip the buffer
sl@0
   610
					iBufManager->ReleaseBuffer(aBuffer);
sl@0
   611
					MvroBufferSkipped(aBuffer->BufferId());
sl@0
   612
	
sl@0
   613
					//notifiy observer about the available buffers
sl@0
   614
					MvroVideoBufferAvailable();
sl@0
   615
					}
sl@0
   616
				}
sl@0
   617
			else
sl@0
   618
				{
sl@0
   619
				// wait time has not passed, add to waiting list
sl@0
   620
				if (iBufManager->UpdateBuffer(aBuffer, aPresentationTime))
sl@0
   621
					{
sl@0
   622
					// head of waiting list has changed, start timer with new wait time
sl@0
   623
					iRendererTimer->Cancel();
sl@0
   624
					iRendererTimer->Start(waitTime);
sl@0
   625
					}
sl@0
   626
				}
sl@0
   627
			}
sl@0
   628
		}
sl@0
   629
	}
sl@0
   630
sl@0
   631
/** Run destroy surface in renderer thread */
sl@0
   632
void CRendererThreadRelay::RunDestroySurface()
sl@0
   633
	{
sl@0
   634
	iRendererTimer->Cancel();
sl@0
   635
	iRendererRelay->DestroySurface(NULL);
sl@0
   636
	}
sl@0
   637
sl@0
   638
/* Update buffer manager pointer */
sl@0
   639
void CRendererThreadRelay::SetBufferManager(CRendererBufferManager* aBufManager)
sl@0
   640
	{
sl@0
   641
		TRequestStatus request = KRequestPending;
sl@0
   642
		TRequestStatus logonRequest = KRequestPending;
sl@0
   643
sl@0
   644
		// While a function call is in progress this thread is suspended
sl@0
   645
		// and the undertaker will not catch panics, listen for these here
sl@0
   646
		iRendererThread->Logon(logonRequest);
sl@0
   647
sl@0
   648
		// Set the call parameters
sl@0
   649
		iCallingStatus = &request;
sl@0
   650
		iFunctionCode = ESetBufferManager;
sl@0
   651
		iBufManager = aBufManager;
sl@0
   652
	
sl@0
   653
		// send request to renderer thread
sl@0
   654
		TRequestStatus* rendererRequest = Status();
sl@0
   655
		iRendererThread->RequestComplete(rendererRequest, KErrNone);
sl@0
   656
		
sl@0
   657
		User::WaitForRequest(logonRequest, request);
sl@0
   658
sl@0
   659
		if(logonRequest != KRequestPending)
sl@0
   660
			{
sl@0
   661
			// renderer thread got panic from surface update session, so panic client
sl@0
   662
			TInt reason = iRendererThread->ExitReason();
sl@0
   663
			TExitCategoryName category = iRendererThread->ExitCategory();
sl@0
   664
			User::Panic(category,reason);
sl@0
   665
			}
sl@0
   666
sl@0
   667
		// Thread is still alive and well
sl@0
   668
		iRendererThread->LogonCancel(logonRequest);
sl@0
   669
		User::WaitForRequest(logonRequest); // Consume the signal
sl@0
   670
sl@0
   671
		__ASSERT_DEBUG(request != KRequestPending, User::Panic(_L("CRTR::SetBufferManager"), KErrCorrupt));
sl@0
   672
	}
sl@0
   673
sl@0
   674
void CRendererThreadRelay::RunSetBufferManager()
sl@0
   675
	{
sl@0
   676
	iRendererRelay->SetBufferManager(iBufManager);
sl@0
   677
	}
sl@0
   678
sl@0
   679
/** Store a pointer to the renderer thread object. */
sl@0
   680
void CRendererThreadRelay::SetRendererThread(RThread* aRendererThread)
sl@0
   681
	{
sl@0
   682
	iRendererThread = aRendererThread;
sl@0
   683
	}
sl@0
   684
sl@0
   685
/** Return a pointer to the function call listener's request status. */
sl@0
   686
TRequestStatus* CRendererThreadRelay::Status()
sl@0
   687
	{
sl@0
   688
	return &iStatus;
sl@0
   689
	}
sl@0
   690
sl@0
   691
/** Set timer info for timed mode */
sl@0
   692
void CRendererThreadRelay::SetTimerInfo(TInt64 aDefaultDelay, TInt64 aMaxDelay)
sl@0
   693
	{
sl@0
   694
	iMaxDelay = aMaxDelay;
sl@0
   695
	iRendererRelay->SetTimerInfo(aDefaultDelay, aMaxDelay);
sl@0
   696
	}
sl@0
   697
sl@0
   698
void CRendererThreadRelay::MvroVideoBufferAvailable()
sl@0
   699
	{
sl@0
   700
	iRendererCallbackListener->SendCallback(CRendererCallbackListener::EBufferAvailable, -1, 0);
sl@0
   701
	}
sl@0
   702
sl@0
   703
void CRendererThreadRelay::MvroBufferDisplayed(TInt aBufferId, const TTime& aTime)
sl@0
   704
	{
sl@0
   705
	iRendererCallbackListener->SendCallback(CRendererCallbackListener::EBufferDisplayed, aBufferId, aTime);
sl@0
   706
	}
sl@0
   707
sl@0
   708
void CRendererThreadRelay::MvroBufferSkipped(TInt aBufferId)
sl@0
   709
	{
sl@0
   710
	iRendererCallbackListener->SendCallback(CRendererCallbackListener::EBufferSkipped, aBufferId, 0);
sl@0
   711
	}
sl@0
   712
sl@0
   713
sl@0
   714
/** Private constructor */
sl@0
   715
CRendererCallbackListener::CRendererCallbackListener(MVideoRendererObserver& aObserver)
sl@0
   716
: CActive(EPriorityStandard),
sl@0
   717
  iObserver(aObserver)
sl@0
   718
	{
sl@0
   719
	CActiveScheduler::Add(this);
sl@0
   720
	}
sl@0
   721
sl@0
   722
/** Two-phased constructor */
sl@0
   723
CRendererCallbackListener* CRendererCallbackListener::NewL(MVideoRendererObserver& aObserver, TInt aNumBuffer)
sl@0
   724
	{
sl@0
   725
	CRendererCallbackListener* self = new (ELeave) CRendererCallbackListener(aObserver);
sl@0
   726
	CleanupStack::PushL(self);
sl@0
   727
	self->ConstructL(aNumBuffer);
sl@0
   728
	CleanupStack::Pop(self);
sl@0
   729
	return self;
sl@0
   730
	}
sl@0
   731
sl@0
   732
/** Second-phase constructor */
sl@0
   733
void CRendererCallbackListener::ConstructL(TInt aNumBuffer)
sl@0
   734
	{
sl@0
   735
	// There could potentially be three messages outstanding per buffer
sl@0
   736
	// size message queue accordingly
sl@0
   737
	TInt slot = aNumBuffer * 3;
sl@0
   738
	if (slot > RMsgQueueBase::KMaxLength)
sl@0
   739
		{
sl@0
   740
		slot = RMsgQueueBase::KMaxLength;
sl@0
   741
		}
sl@0
   742
	User::LeaveIfError(iMsgQueue.CreateLocal(slot));
sl@0
   743
	}
sl@0
   744
sl@0
   745
CRendererCallbackListener::~CRendererCallbackListener()
sl@0
   746
	{
sl@0
   747
	Cancel(); // Cancel any request, if outstanding
sl@0
   748
	iMsgQueue.Close();
sl@0
   749
	}
sl@0
   750
sl@0
   751
void CRendererCallbackListener::ExtendMsgQueueL(TInt aNumBuffer)
sl@0
   752
	{
sl@0
   753
	// close the message queue and construct another one
sl@0
   754
	iMsgQueue.Close();
sl@0
   755
	ConstructL(aNumBuffer);
sl@0
   756
	}
sl@0
   757
sl@0
   758
void CRendererCallbackListener::DoCancel()
sl@0
   759
	{
sl@0
   760
	iMsgQueue.CancelDataAvailable();
sl@0
   761
	}
sl@0
   762
sl@0
   763
/** Start listener */
sl@0
   764
void CRendererCallbackListener::Start()
sl@0
   765
	{
sl@0
   766
	if (!IsActive())
sl@0
   767
		{
sl@0
   768
		iMsgQueue.NotifyDataAvailable(iStatus);
sl@0
   769
		SetActive();
sl@0
   770
		}
sl@0
   771
	}
sl@0
   772
sl@0
   773
/** Set the callback function */
sl@0
   774
void CRendererCallbackListener::SendCallback(TFunctionCode aFunctionCode, TInt aBufferId, const TTime& aTime)
sl@0
   775
	{
sl@0
   776
	DEBUGPRINT2(_L("CRendererCallbackListener::SendCallback entered aFunctionCode=%d"), aFunctionCode);
sl@0
   777
	
sl@0
   778
	TCallbackData data;
sl@0
   779
	data.iFunctionCode = aFunctionCode;
sl@0
   780
	data.iBufferId = aBufferId;
sl@0
   781
	data.iTime = aTime;
sl@0
   782
	
sl@0
   783
	iMsgQueue.Send(data);
sl@0
   784
	}
sl@0
   785
sl@0
   786
/** Call the callback function within the main thread */
sl@0
   787
void CRendererCallbackListener::RunL()
sl@0
   788
	{
sl@0
   789
	TCallbackData data;
sl@0
   790
	TInt err = iMsgQueue.Receive(data);
sl@0
   791
	if (err == KErrNone)
sl@0
   792
		{
sl@0
   793
		if (data.iFunctionCode == EBufferAvailable)
sl@0
   794
			{
sl@0
   795
			DEBUGPRINT1(_L("CRendererCallbackListener::RunL - EBufferAvailable"));
sl@0
   796
			iObserver.MvroVideoBufferAvailable();
sl@0
   797
			}
sl@0
   798
		else if (data.iFunctionCode == EBufferDisplayed)
sl@0
   799
			{
sl@0
   800
			DEBUGPRINT1(_L("CRendererCallbackListener::RunL - EBufferDisplayed"));
sl@0
   801
			iObserver.MvroBufferDisplayed(data.iBufferId, data.iTime);
sl@0
   802
			}
sl@0
   803
		else if (data.iFunctionCode == EBufferSkipped)
sl@0
   804
			{
sl@0
   805
			DEBUGPRINT1(_L("CRendererCallbackListener::RunL - EBufferSkipped"));
sl@0
   806
			iObserver.MvroBufferSkipped(data.iBufferId);
sl@0
   807
			}
sl@0
   808
		}
sl@0
   809
	else
sl@0
   810
		{
sl@0
   811
		DEBUGPRINT2(_L("CRendererCallbackListener::RunL err=%d"), err);
sl@0
   812
		}
sl@0
   813
sl@0
   814
	Start();
sl@0
   815
	}