omxilvideocomps/omxilvideoscheduler/src/comxilvideoschedulerpf.cpp
changeset 0 5d29cba61097
equal deleted inserted replaced
-1:000000000000 0:5d29cba61097
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20 @file
       
    21 @internalComponent
       
    22 */
       
    23 
       
    24 
       
    25 #include <openmax/il/common/omxilutil.h>
       
    26 
       
    27 #include "comxilvideoschedulerpf.h"
       
    28 #include "comxilvideoscheduler.h"
       
    29 #include "resourcefilereader.h"
       
    30 #include "buffercopierstatemonitor.h"
       
    31 #include "omxilvideoschedulerextensionsindexes.h"
       
    32 #include <openmax/il/extensions/omxildroppedframeeventextension.h>
       
    33 #include <openmax/il/common/omxilcallbacknotificationif.h>
       
    34 #include "log.h"
       
    35 
       
    36 _LIT(KVideoSchedulerPanicCategory, "omxilvscheduler"); //should restrict to 16 characters as it is used in User::Panic
       
    37 _LIT(KResourceFileName, "Z:\\resource\\videoscheduler\\videoscheduler.rsc");
       
    38 const TInt KMaxRenderTime = 1000000;
       
    39 const TInt KMaxGraphicSinkBufferCount(2); //This is set as the maximum number of buffers that can be sent to the graphic sink without the risk of overloading it.
       
    40 
       
    41 
       
    42 
       
    43 COmxILVideoSchedulerPF* COmxILVideoSchedulerPF::NewL(MOmxILCallbackNotificationIf& aCallbacks, COmxILVideoScheduler& aComponent, OMX_COMPONENTTYPE* aHandle)
       
    44 	{
       
    45 	COmxILVideoSchedulerPF* self = new (ELeave) COmxILVideoSchedulerPF(aCallbacks, aComponent, aHandle);
       
    46 	CleanupStack::PushL(self);
       
    47 	self->ConstructL();
       
    48 	CleanupStack::Pop(self);
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 COmxILVideoSchedulerPF::COmxILVideoSchedulerPF(MOmxILCallbackNotificationIf& aCallbacks, COmxILVideoScheduler& aComponent, OMX_COMPONENTTYPE* aHandle)
       
    53 : COmxILProcessingFunction(aCallbacks),
       
    54   iComponent(aComponent),
       
    55   iIsClockStopped(ETrue),
       
    56   iInvalid(EFalse),
       
    57   iTimeStamp(KMinTInt64),
       
    58   iHandle(aHandle)
       
    59  	{
       
    60 	}
       
    61 
       
    62 void COmxILVideoSchedulerPF::ConstructL()
       
    63 	{
       
    64 	User::LeaveIfError(iMutex.CreateLocal());
       
    65 	iBufferCopierStateMonitor = CBufferCopierStateMonitor::NewL(*this, iComponent);
       
    66 	// get timer info
       
    67 	CResourceFileReader* reader = CResourceFileReader::NewLC(KResourceFileName);
       
    68 	reader->ReadTimerInfoL(iRenderTime, iMaxLateness);
       
    69 	CleanupStack::PopAndDestroy(reader);
       
    70 
       
    71 	// Prefill the render time array with the default render time read from resource file
       
    72 	for (TInt count = 0; count < KRenderTimeListLength; ++count)
       
    73 		{
       
    74 		iRenderTimeList[count] = iRenderTime;
       
    75 		}
       
    76 	iRenderTimeSum = iRenderTime * KRenderTimeListLength;
       
    77 	}
       
    78 
       
    79 COmxILVideoSchedulerPF::~COmxILVideoSchedulerPF()
       
    80 	{
       
    81 	delete iBufferCopierStateMonitor;
       
    82     iMutex.Wait();
       
    83 	iWaitingBuffers.Reset();
       
    84 	iCompletedBuffersHeldByPause.Reset();
       
    85     iMutex.Signal();	
       
    86 	iMutex.Close();
       
    87 	}
       
    88 
       
    89 OMX_ERRORTYPE COmxILVideoSchedulerPF::StateTransitionIndication(TStateIndex aNewState)
       
    90 	{
       
    91 	switch(aNewState)
       
    92 		{
       
    93 		case EStateExecuting:
       
    94 			{
       
    95 			if (iPausedState)
       
    96 				{
       
    97 			    iMutex.Wait();
       
    98 				iPausedState = EFalse;
       
    99 				
       
   100 				// send any buffers that received time updates during paused state
       
   101 				if (iOutputBufferSentCount < KMaxGraphicSinkBufferCount)   // only allowed to send 2 buffers at a time
       
   102 					{
       
   103 					SubmitBufferHeldByPause();
       
   104 					}
       
   105                 iMutex.Signal();
       
   106 				}
       
   107 			break;
       
   108 			}
       
   109 		case EStatePause:
       
   110 			{
       
   111 			iPausedState = ETrue;
       
   112 			break;
       
   113 			}
       
   114 		case ESubStateLoadedToIdle:
       
   115 			{
       
   116 			TUint32 bufferCount = iComponent.BufferCount();
       
   117 			
       
   118 			TInt error = iBufferCopierStateMonitor->SetState(CBufferCopierStateMonitor::ESubLoadedToIdle);
       
   119 			
       
   120 			if (error != KErrNone)
       
   121 			    {
       
   122 			    return SymbianErrorToOmx(error);                    
       
   123 			    }
       
   124 			
       
   125 			error = iWaitingBuffers.Reserve(bufferCount);
       
   126 			if (error != KErrNone)
       
   127 			    {
       
   128 			    return SymbianErrorToOmx(error);                    
       
   129 			    }
       
   130 
       
   131 			error = iCompletedBuffersHeldByPause.Reserve(bufferCount);
       
   132 			if (error != KErrNone)
       
   133 			    {
       
   134 			    return SymbianErrorToOmx(error);                    
       
   135 			    }
       
   136 			
       
   137 			break;
       
   138 			}
       
   139 		case EStateIdle:
       
   140 			{
       
   141 			iOutputBufferSentCount = iComponent.BufferCount();
       
   142 			break;
       
   143 			}
       
   144 		case ESubStateIdleToLoaded:
       
   145 			{
       
   146 			TInt error = iBufferCopierStateMonitor->SetState(CBufferCopierStateMonitor::ESubIdleToLoaded);
       
   147 			if (error != KErrNone)
       
   148 			    {
       
   149 			    return SymbianErrorToOmx(error);                    
       
   150 			    }
       
   151 			break;
       
   152 			}
       
   153 		default:
       
   154 			{
       
   155 			break;
       
   156 			}	
       
   157 		}
       
   158     
       
   159 	return OMX_ErrorNone;	
       
   160 	}
       
   161 
       
   162 OMX_ERRORTYPE COmxILVideoSchedulerPF::BufferFlushingIndication(TUint32 aPortIndex, OMX_DIRTYPE aDirection)
       
   163 	{
       
   164 	// called from command thread
       
   165 	
       
   166     iMutex.Wait();
       
   167 	if (iBufferCopierStateMonitor->BufferCopier())
       
   168 		{
       
   169 		if (aPortIndex == OMX_ALL)
       
   170 			{
       
   171 			iBufferCopierStateMonitor->BufferCopier()->FlushBuffers(OMX_DirInput);
       
   172 			iBufferCopierStateMonitor->BufferCopier()->FlushBuffers(OMX_DirOutput);
       
   173 			}
       
   174 		else
       
   175 			{
       
   176 			iBufferCopierStateMonitor->BufferCopier()->FlushBuffers(aDirection);
       
   177 			}
       
   178 		}
       
   179 
       
   180 	if (aDirection == OMX_DirOutput || aPortIndex == OMX_ALL)
       
   181 		{
       
   182 		while (iWaitingBuffers.Count() > 0)
       
   183 			{
       
   184 			iWaitingBuffers[0]->nFilledLen = 0;
       
   185 			iWaitingBuffers[0]->nOffset = 0;
       
   186 			iWaitingBuffers[0]->nTimeStamp = 0;
       
   187 			iCallbacks.BufferDoneNotification(iWaitingBuffers[0], 1, OMX_DirOutput);
       
   188 			iWaitingBuffers.Remove(0);
       
   189 			iOutputBufferSentCount++;
       
   190 			}
       
   191 		if(iSinkPendingBuffer)
       
   192 			{
       
   193 			iSinkPendingBuffer->nFilledLen = 0;
       
   194 			iSinkPendingBuffer->nOffset = 0;
       
   195 			iSinkPendingBuffer->nTimeStamp = 0;
       
   196 			iCallbacks.BufferDoneNotification(iSinkPendingBuffer, 1, OMX_DirOutput);
       
   197 			iSinkPendingBuffer = NULL;
       
   198 			iOutputBufferSentCount++;
       
   199 			}
       
   200 		}
       
   201     iMutex.Signal();        
       
   202 
       
   203 	return OMX_ErrorNone;
       
   204 	}
       
   205 
       
   206 OMX_ERRORTYPE COmxILVideoSchedulerPF::ParamIndication(OMX_INDEXTYPE aParamIndex,
       
   207 													const TAny* apComponentParameterStructure)
       
   208 	{
       
   209 	DEBUG_PRINTF(_L8("COmxILVideoSchedulerProcessingFunction::ParamIndication"));
       
   210 
       
   211 	if(aParamIndex == OMX_NokiaIndexParamDroppedFrameEvent)
       
   212 		{
       
   213 		const OMX_NOKIA_PARAM_DROPPEDFRAMEEVENT* dropFrame = static_cast<const OMX_NOKIA_PARAM_DROPPEDFRAMEEVENT*>(apComponentParameterStructure);
       
   214 		iEnableDropFrameEvent = dropFrame->bEnabled;
       
   215 		}
       
   216 
       
   217 	return OMX_ErrorNone;
       
   218 	}
       
   219 
       
   220 OMX_ERRORTYPE COmxILVideoSchedulerPF::ConfigIndication(OMX_INDEXTYPE /*aConfigIndex*/, const TAny* /*apComponentConfigStructure*/)
       
   221 	{
       
   222 	return OMX_ErrorNone;	
       
   223 	}
       
   224 
       
   225 OMX_ERRORTYPE COmxILVideoSchedulerPF::BufferIndication(OMX_BUFFERHEADERTYPE* apBufferHeader,
       
   226 	  												   OMX_DIRTYPE aDirection)			
       
   227 	{
       
   228 	if (iInvalid)
       
   229 	    {
       
   230 	    return OMX_ErrorInvalidState;
       
   231 	    }
       
   232 
       
   233 	// called from decoder data thread or sink data thread
       
   234 	iMutex.Wait();
       
   235 	if(aDirection == OMX_DirOutput)
       
   236 		{
       
   237 		apBufferHeader->nFlags = 0;
       
   238 		iOutputBufferSentCount--;
       
   239 		ASSERT(iOutputBufferSentCount <= iComponent.BufferCount());
       
   240 		
       
   241 		DEBUG_PRINTF2(_L8("VS2::BufferIndication : apBufferHeader->nTickCount = %d"), apBufferHeader->nTickCount);
       
   242 
       
   243 		// update the render time if it is set
       
   244 		if (apBufferHeader->nTickCount > 0 && apBufferHeader->nTickCount <= KMaxRenderTime)
       
   245 			{
       
   246 			// Add new render time to render time list, and recalculate average
       
   247 			iRenderTimeSum -= iRenderTimeList[iRenderTimeListPos];
       
   248 			iRenderTimeSum += apBufferHeader->nTickCount;
       
   249 			iRenderTimeList[iRenderTimeListPos] = apBufferHeader->nTickCount;
       
   250 			++iRenderTimeListPos;
       
   251 			iRenderTimeListPos %= KRenderTimeListLength;
       
   252 
       
   253 			iRenderTime = iRenderTimeSum / KRenderTimeListLength;
       
   254 
       
   255 			DEBUG_PRINTF2(_L8("VS2::BufferIndication : New iRenderTime = %ld"), iRenderTime);
       
   256 			}
       
   257 
       
   258 		// previously sent buffer has come back
       
   259 		// send any buffers that received time updates
       
   260 		// at startup, iOutputBufferSentCount may be >2 if output port is non-supplier
       
   261 		if (!iPausedState && iOutputBufferSentCount < KMaxGraphicSinkBufferCount)
       
   262 			{
       
   263 			SubmitBufferHeldByPause();
       
   264 			}
       
   265 		}
       
   266 	else if(apBufferHeader->nFlags & OMX_BUFFERFLAG_DECODEONLY)
       
   267 		{
       
   268 		// this frame is not to be rendered (probably as part of an accurate seek)
       
   269 		// drop the data and send it back to the decoder
       
   270 		apBufferHeader->nFilledLen = 0;
       
   271 		apBufferHeader->nFlags = 0;
       
   272 		apBufferHeader->nOffset = 0;
       
   273 		iCallbacks.BufferDoneNotification(apBufferHeader, 0, OMX_DirInput);
       
   274 		iMutex.Signal();
       
   275 		return OMX_ErrorNone;
       
   276 		}
       
   277 	if (iBufferCopierStateMonitor->BufferCopier())
       
   278 	    {
       
   279 	    iBufferCopierStateMonitor->BufferCopier()->DeliverBuffer(apBufferHeader, aDirection);
       
   280 	    }
       
   281     iMutex.Signal();
       
   282     
       
   283 	return OMX_ErrorNone;
       
   284 	}
       
   285 
       
   286 OMX_ERRORTYPE COmxILVideoSchedulerPF::MediaTimeIndication(const OMX_TIME_MEDIATIMETYPE& aTimeInfo)
       
   287 	{
       
   288 	// called from clock thread
       
   289 	
       
   290 	switch(aTimeInfo.eUpdateType)
       
   291 		{
       
   292 	case OMX_TIME_UpdateRequestFulfillment:
       
   293 		{
       
   294 		
       
   295 		iMutex.Wait();
       
   296 
       
   297 		TInt index = -1;
       
   298 		OMX_BUFFERHEADERTYPE* buffer = reinterpret_cast<OMX_BUFFERHEADERTYPE*>(aTimeInfo.nClientPrivate);
       
   299 		__ASSERT_DEBUG(buffer->nTimeStamp == aTimeInfo.nMediaTimestamp, Panic(EPanicBadAssociation));
       
   300 		if(FindWaitingBuffer(buffer, aTimeInfo.nMediaTimestamp, index))
       
   301 			{
       
   302 			if (iPausedState || iCompletedBuffersHeldByPause.Count() > 0)
       
   303 				{
       
   304 				TBufferMessage bufferMessage;
       
   305 				bufferMessage.iBufferHeader = buffer;
       
   306 				bufferMessage.iMediaTimeInfo = aTimeInfo;
       
   307 				
       
   308 				OMX_ERRORTYPE error = SymbianErrorToOmx(iCompletedBuffersHeldByPause.Append(bufferMessage)); // note append cannot fail, allocated enough slots
       
   309 				iMutex.Signal();
       
   310 				return error;
       
   311 				}
       
   312 			else 
       
   313 				{
       
   314 				SendTimedOutputBuffer(buffer, aTimeInfo, index);
       
   315 				}
       
   316 			}
       
   317 		else
       
   318 			{
       
   319 			// TODO [SL] now what?
       
   320 			User::Invariant();
       
   321 			}
       
   322 
       
   323 		iMutex.Signal();
       
   324 		return OMX_ErrorNone;
       
   325 		}
       
   326 
       
   327 	case OMX_TIME_UpdateScaleChanged:
       
   328 		if(aTimeInfo.xScale >= 0)
       
   329 			{
       
   330 			// the clock takes care completing requests at the correct media time
       
   331 			return OMX_ErrorNone;
       
   332 			}
       
   333 		else
       
   334 			{
       
   335 			// TODO think harder about implications of negative scale
       
   336 			// certainly the iTimeStamp checking must be reversed
       
   337 			ASSERT(0);
       
   338 			return OMX_ErrorNotImplemented;
       
   339 			}
       
   340 
       
   341 	case OMX_TIME_UpdateClockStateChanged:
       
   342 		iClockState.eState = aTimeInfo.eState;
       
   343 		switch(aTimeInfo.eState)
       
   344 			{
       
   345 		case OMX_TIME_ClockStateStopped:
       
   346 		    {
       
   347 		    // clock stopped so remove any pending buffers from the list as time requests
       
   348 		    // will be resent when the clock is running again
       
   349 		    
       
   350 	        iIsClockStopped	= ETrue;	 
       
   351 
       
   352 	        iMutex.Wait();
       
   353 			while (iCompletedBuffersHeldByPause.Count() > 0)
       
   354                 { 
       
   355 				iCompletedBuffersHeldByPause.Remove(0);
       
   356                 }           	         
       
   357 	        
       
   358 			if(iSinkPendingBuffer && iBufferCopierStateMonitor)
       
   359 				{
       
   360 				// if sink pending buffer exist (as sink is bottleneck) then drop the frame
       
   361 				iBufferCopierStateMonitor->BufferCopier()->DeliverBuffer(iSinkPendingBuffer, OMX_DirOutput);
       
   362 
       
   363 				// dropped a frame, so send an event if the dropped frame extension is enabled
       
   364 				if (iEnableDropFrameEvent)
       
   365 					{
       
   366 					//TODO DL: iCallbacks.EventNotification(OMX_EventNokiaDroppedFrame, 1, 0, NULL);
       
   367 					}
       
   368 
       
   369 				iSinkPendingBuffer = NULL;
       
   370 				}
       
   371             iMutex.Signal();            
       
   372 	        }
       
   373 			break;
       
   374 			
       
   375 		case OMX_TIME_ClockStateWaitingForStartTime:
       
   376 		    {
       
   377 		    iIsClockStopped = EFalse;
       
   378 		    // if now in WaitingForStartTime state and start time already received, send it now
       
   379 		    if (iStartTimePending)
       
   380 		        {
       
   381 		        OMX_ERRORTYPE error = iComponent.SetVideoStartTime(iStartTime);
       
   382 		        if (error != OMX_ErrorNone)
       
   383 		            {
       
   384 		            // iStartTimePending = EFalse; // FIXME - Is this required?
       
   385 		            return error;
       
   386 		            }
       
   387 		        }
       
   388 		    }
       
   389 			break;
       
   390 			
       
   391 		case OMX_TIME_ClockStateRunning:
       
   392 			{
       
   393 			iTimeStamp = KMinTInt64;
       
   394 			if(iIsClockStopped)
       
   395 				{
       
   396 				// the clock is running after being stopped previously
       
   397 				// resend time requests for waiting buffers
       
   398 				iIsClockStopped = EFalse;
       
   399 				
       
   400 				for (TInt i = 0; i < iWaitingBuffers.Count(); ++i)
       
   401 					{
       
   402 					iComponent.MediaTimeRequest(iWaitingBuffers[i], iWaitingBuffers[i]->nTimeStamp, iRenderTime);
       
   403 					}
       
   404 				}
       
   405 			}
       
   406 			break;
       
   407 			}
       
   408 		
       
   409 		iStartTimePending = EFalse;
       
   410 		DEBUG_PRINTF2(_L8("VS2::MediaTimeIndication : ClockStateChanged = %d"), aTimeInfo.eState);
       
   411 		
       
   412 		return OMX_ErrorNone;
       
   413 		
       
   414 	default:
       
   415 		return OMX_ErrorBadParameter;
       
   416 		}
       
   417 	
       
   418 	}
       
   419 
       
   420 /* Check if aBuffer still exist in the waiting queue */ 
       
   421 TBool COmxILVideoSchedulerPF::FindWaitingBuffer(const OMX_BUFFERHEADERTYPE* aBuffer, const OMX_TICKS& aMediaTime, TInt& aIndex) const
       
   422 	{
       
   423 	__ASSERT_DEBUG(const_cast<RMutex&>(iMutex).IsHeld(), Panic(EPanicMutexUnheld));
       
   424 	
       
   425 	TBool found = EFalse;
       
   426 		
       
   427 	for (TInt i=0; i<iWaitingBuffers.Count(); ++i)
       
   428 		{
       
   429 		if ((iWaitingBuffers[i] == aBuffer) && (iWaitingBuffers[i]->nTimeStamp == aMediaTime))
       
   430 			{
       
   431 			found = ETrue;
       
   432 			aIndex = i;
       
   433 			break;
       
   434 			}
       
   435 		}
       
   436 
       
   437 	return found;
       
   438 	}
       
   439 
       
   440 /**
       
   441 Check if a specified buffer is currently held by the processing function,
       
   442 and remove it if found.
       
   443 
       
   444 @param apBufferHeader Buffer to remove
       
   445 @param aDirection Port direction
       
   446 @return Flag to indicate if buffer was removed.
       
   447 */
       
   448 OMX_BOOL COmxILVideoSchedulerPF::BufferRemovalIndication(OMX_BUFFERHEADERTYPE* apBufferHeader, OMX_DIRTYPE aDirection)
       
   449 	{
       
   450     iMutex.Wait();	
       
   451 	if(iBufferCopierStateMonitor->BufferCopier() && iBufferCopierStateMonitor->BufferCopier()->RemoveBuffer(apBufferHeader, aDirection))
       
   452 		{
       
   453 		if(aDirection == OMX_DirOutput)
       
   454 			{
       
   455 			iOutputBufferSentCount++;
       
   456 			}
       
   457 		iMutex.Signal();
       
   458 		return OMX_TRUE;
       
   459 		}
       
   460 	else if(aDirection == OMX_DirOutput)
       
   461 		{
       
   462 		for (TInt i = 0; i < iWaitingBuffers.Count(); ++i)
       
   463 			{
       
   464 			if (iWaitingBuffers[i] == apBufferHeader)
       
   465 				{
       
   466 				iWaitingBuffers[i]->nFilledLen = 0;
       
   467 				iWaitingBuffers.Remove(i);
       
   468 				iOutputBufferSentCount++;
       
   469 			    iMutex.Signal();
       
   470 				return OMX_TRUE;
       
   471 				}
       
   472 			}
       
   473 		if(apBufferHeader == iSinkPendingBuffer)
       
   474 			{
       
   475 			iSinkPendingBuffer = NULL;
       
   476 			iOutputBufferSentCount++;
       
   477 			iMutex.Signal();
       
   478 			return OMX_TRUE;
       
   479 			}
       
   480 		}
       
   481  
       
   482 	iMutex.Signal();    
       
   483 	return OMX_FALSE;
       
   484 	}
       
   485 
       
   486 /* Submit the first time update buffer that still exists in the waiting queue. */
       
   487 void COmxILVideoSchedulerPF::SubmitBufferHeldByPause()
       
   488 	{
       
   489 	__ASSERT_DEBUG(iMutex.IsHeld(), Panic(EPanicMutexUnheld));
       
   490 	__ASSERT_DEBUG(iOutputBufferSentCount < KMaxGraphicSinkBufferCount, Panic(EPanicBadOutputRegulation));
       
   491 
       
   492 	if(iSinkPendingBuffer)
       
   493 		{
       
   494 		DEBUG_PRINTF(_L8("VS2::SubmitBufferHeldByPause ***************************SEND SINK PENDING BUFFER"));
       
   495 		OMX_BUFFERHEADERTYPE* buffer = iSinkPendingBuffer;
       
   496 		iSinkPendingBuffer = NULL;
       
   497 		SendOutputBuffer(buffer);
       
   498 		return;
       
   499 		}
       
   500 
       
   501 	TInt index = -1;
       
   502 	TBool bufferSent = EFalse;
       
   503 	while (iCompletedBuffersHeldByPause.Count() > 0 && !bufferSent)
       
   504 		{
       
   505 		TBufferMessage& msg = iCompletedBuffersHeldByPause[0];
       
   506 		if (FindWaitingBuffer(msg.iBufferHeader, 
       
   507 					msg.iMediaTimeInfo.nMediaTimestamp, index))
       
   508 			{
       
   509 			DEBUG_PRINTF(_L8("VS2::SubmitBufferHeldByPause ***************************SEND HELD BUFFER"));
       
   510 			bufferSent = SendTimedOutputBuffer(msg.iBufferHeader, msg.iMediaTimeInfo, index);
       
   511 			}
       
   512 		iCompletedBuffersHeldByPause.Remove(0);
       
   513 		}
       
   514 	}
       
   515 
       
   516 /** Returns ETrue if aBuffer was sent, EFalse otherwise */
       
   517 TBool COmxILVideoSchedulerPF::SendTimedOutputBuffer(OMX_BUFFERHEADERTYPE* aBuffer, const OMX_TIME_MEDIATIMETYPE& aMediaTimeInfo, TInt aIndex)
       
   518 	{
       
   519 	__ASSERT_DEBUG(iMutex.IsHeld(), Panic(EPanicMutexUnheld));
       
   520 	__ASSERT_DEBUG(aBuffer->nTimeStamp == aMediaTimeInfo.nMediaTimestamp, Panic(EPanicBadAssociation));
       
   521 	__ASSERT_DEBUG(aBuffer == reinterpret_cast<OMX_BUFFERHEADERTYPE*>(aMediaTimeInfo.nClientPrivate), Panic(EPanicBadAssociation));
       
   522 	
       
   523 #ifdef _OMXIL_COMMON_DEBUG_TRACING_ON	
       
   524 	DEBUG_PRINTF(_L8("VS2::SendTimedOutputBuffer **********************************"));
       
   525 	TTime t;
       
   526 	t.HomeTime();
       
   527 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : t.HomeTime() = %ld"), t.Int64());
       
   528 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : aMediaTimeInfo.nClientPrivate = 0x%X"), aMediaTimeInfo.nClientPrivate);
       
   529 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : aMediaTimeInfo.nMediaTimestamp = %ld"), aMediaTimeInfo.nMediaTimestamp);
       
   530 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : aMediaTimeInfo.nOffset = %ld"), aMediaTimeInfo.nOffset);
       
   531 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : aMediaTimeInfo.nWallTimeAtMediaTime = %ld"), aMediaTimeInfo.nWallTimeAtMediaTime);
       
   532 #endif
       
   533 	
       
   534 	TBool bufferSent = EFalse;
       
   535 
       
   536 	OMX_U32 flags = aBuffer->nFlags;
       
   537 
       
   538 	// Work out how long it is from now until the frame will be rendered.
       
   539 	// This will be the time it takes the sink to render, minus the offset
       
   540 	// value from the clock completion (i.e how far before the requested
       
   541 	// time that the clock has completed us). A lateness of 0 means we are at
       
   542 	// the correct time to send the buffer, a positive lateness means we are
       
   543 	// late sending the buffer, and a lateness waitTime means we are early.
       
   544 	// For the first frame we were not able to request an early completion to
       
   545 	// offset the render time, so assume that the render time is 0.
       
   546 	OMX_TICKS lateness = 0 - aMediaTimeInfo.nOffset;
       
   547 	if (!(flags & OMX_BUFFERFLAG_STARTTIME))
       
   548 		{
       
   549 		lateness += iRenderTime;
       
   550 		}
       
   551 
       
   552 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : iRenderTime = %ld"), iRenderTime);
       
   553 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : lateness = %ld"), lateness);
       
   554 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : iMaxLateness = %ld"), iMaxLateness);
       
   555 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : iTimeStamp = %ld"), iTimeStamp);
       
   556 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : iFrameDroppedCount = %d"), iFrameDroppedCount);
       
   557 	DEBUG_PRINTF2(_L8("VS2::SendTimedOutputBuffer : flags = %d"), flags);
       
   558 
       
   559 	iWaitingBuffers.Remove(aIndex);
       
   560 
       
   561 	// Send the buffer if the wait time is within the maximum allowed delay and timestamp is later than the previous timestamp, otherwise skip the buffer	
       
   562 	if ((lateness <= iMaxLateness || iFrameDroppedCount >= KMaxGraphicSinkBufferCount) && aMediaTimeInfo.nMediaTimestamp > iTimeStamp)   // shouldn't drop more than 2 frames at a time when decoder is slow
       
   563 		{
       
   564 		DEBUG_PRINTF(_L8("VS2::SendTimedOutputBuffer ***************************SHOW"));
       
   565 		
       
   566 		bufferSent = ETrue;
       
   567 		iFrameDroppedCount = 0;
       
   568 
       
   569 		SendOutputBuffer(aBuffer);
       
   570 		}
       
   571 	else
       
   572 		{
       
   573 		DEBUG_PRINTF(_L8("VS2::SendTimedOutputBuffer ***************************DROP"));
       
   574 		
       
   575 		iFrameDroppedCount++;
       
   576 
       
   577 		// dropped a frame, so send an event if the dropped frame extension is enabled
       
   578 		if(iEnableDropFrameEvent)
       
   579 			{
       
   580             //TODO DL: iCallbacks.EventNotification(OMX_EventNokiaDroppedFrame, 1, 0, NULL);
       
   581 			}
       
   582 
       
   583 		// if EOS was on the buffer, send an empty buffer with EOS and send the EOS event
       
   584 		// if not, discard the buffer contents and post the buffer for another copy
       
   585 		if(flags & OMX_BUFFERFLAG_EOS)
       
   586 			{
       
   587 			DEBUG_PRINTF(_L8("VS2::SendTimedOutputBuffer ***************************SEND EMPTY EOS BUFFER"));
       
   588 			aBuffer->nFilledLen = 0;
       
   589 			aBuffer->nOffset = 0;
       
   590 			aBuffer->nTimeStamp = 0;
       
   591 			SendOutputBuffer(aBuffer);
       
   592 			}
       
   593 		else
       
   594 			{
       
   595 			TOmxILUtil::ClearBufferContents(aBuffer);
       
   596 			aBuffer->nOffset = 0;
       
   597 			if (iBufferCopierStateMonitor->BufferCopier())
       
   598 			    {
       
   599 			    iBufferCopierStateMonitor->BufferCopier()->DeliverBuffer(aBuffer, OMX_DirOutput);
       
   600 			    }
       
   601 			}
       
   602 		}
       
   603 	
       
   604 	return bufferSent;	
       
   605 	}
       
   606 
       
   607 void COmxILVideoSchedulerPF::SendOutputBuffer(OMX_BUFFERHEADERTYPE* aBuffer)
       
   608 	{
       
   609 	__ASSERT_DEBUG(iMutex.IsHeld(), Panic(EPanicMutexUnheld));
       
   610 	__ASSERT_DEBUG(iTimeStamp < aBuffer->nTimeStamp || aBuffer->nFlags & OMX_BUFFERFLAG_EOS, Panic(EPanicTimestampEmissionUnordered));
       
   611 
       
   612 	if(iOutputBufferSentCount >= KMaxGraphicSinkBufferCount)
       
   613 		{
       
   614 		DEBUG_PRINTF(_L8("VS2::SendOutputBuffer : *****************STORING SINK PENDING BUFFER"));
       
   615 
       
   616 		// sink is bottleneck, keep the most recent pending frame but return the rest so decoder keeps running
       
   617 		// when sink returns a buffer send the most recent frame
       
   618 		if(iSinkPendingBuffer && iBufferCopierStateMonitor->BufferCopier())
       
   619 			{
       
   620 			if (iSinkPendingBuffer->nFlags & OMX_BUFFERFLAG_EOS)
       
   621 			    {
       
   622 			    //if (bizarrely) pending buffer has EOS flag and another buffer replaces it.
       
   623 			    DoSendOutputBuffer(iSinkPendingBuffer);
       
   624 			    }
       
   625 			else
       
   626 			    {
       
   627 			    iBufferCopierStateMonitor->BufferCopier()->DeliverBuffer(iSinkPendingBuffer, OMX_DirOutput);
       
   628 			    }
       
   629 
       
   630 			DEBUG_PRINTF(_L8("VS2::SendOutputBuffer : *****************DROPPED EXISTING SINK PENDING BUFFER"));
       
   631 
       
   632 			// dropped a frame, so send an event if the dropped frame extension is enabled
       
   633 			if(iEnableDropFrameEvent)
       
   634 				{
       
   635                 //TODO DL: iCallbacks.EventNotification(OMX_EventNokiaDroppedFrame, 1, 0, NULL);
       
   636 				}
       
   637 			}
       
   638 
       
   639 		iSinkPendingBuffer = aBuffer;
       
   640 		}
       
   641 	else
       
   642 		{
       
   643 		DoSendOutputBuffer(aBuffer);
       
   644 		}
       
   645 	}
       
   646 
       
   647 /** Called when the buffer copier has transferred the data from an input buffer to an output buffer. */
       
   648 void COmxILVideoSchedulerPF::MbcBufferCopied(OMX_BUFFERHEADERTYPE* aInBuffer, OMX_BUFFERHEADERTYPE* aOutBuffer)
       
   649 	{
       
   650 	iMutex.Wait();
       
   651 	
       
   652 	// send input buffer back
       
   653 	aInBuffer->nFilledLen = 0;
       
   654 	aInBuffer->nOffset = 0;
       
   655 	aInBuffer->nFlags = 0;
       
   656 	aInBuffer->nTimeStamp = 0;
       
   657 
       
   658 	// Deal with any buffer marks. Currently the component framework makes an attempt to deal with
       
   659 	// them, but it cannot associate the input buffer mark with the corresponding output buffer so
       
   660 	// we may need to do some tweaking here.
       
   661 	if (aInBuffer->hMarkTargetComponent)
       
   662 		{
       
   663 		if (aInBuffer->hMarkTargetComponent == iHandle)
       
   664 			{
       
   665 			// There was a buffer mark on the input buffer intended for us. That means there is no
       
   666 			// need to send it out on the output buffer. Also, it is OK to let the component framework
       
   667 			// deal with it in this situation.
       
   668 			aOutBuffer->hMarkTargetComponent = NULL;
       
   669 			aOutBuffer->pMarkData = NULL;
       
   670 			}
       
   671 		else
       
   672 			{
       
   673 			// There was a buffer mark on the input buffer but it is not intended for us. If
       
   674 			// we let the component framework deal with it then we will get multiple marks sent
       
   675 			// out because we have copied it to the output buffer, and the framework will also
       
   676 			// store it to send out later. Clear it here so the framework does not see it.
       
   677 			aInBuffer->hMarkTargetComponent = NULL;
       
   678 			aInBuffer->pMarkData = NULL;
       
   679 			}
       
   680 		}
       
   681 
       
   682 	OMX_ERRORTYPE error;
       
   683 
       
   684 	iCallbacks.BufferDoneNotification(aInBuffer, 0, OMX_DirInput);
       
   685 	
       
   686 	if(aOutBuffer->nFilledLen > 0 || (aOutBuffer->nFlags & OMX_BUFFERFLAG_EOS))
       
   687 		{
       
   688 		iWaitingBuffers.Append(aOutBuffer);  // note append cannot fail, allocated enough slots
       
   689 		}
       
   690 	
       
   691 	if(aOutBuffer->nFlags & OMX_BUFFERFLAG_STARTTIME)
       
   692 		{
       
   693 		if(OMX_TIME_ClockStateWaitingForStartTime == iClockState.eState)
       
   694 			{
       
   695 			error = iComponent.SetVideoStartTime(aOutBuffer->nTimeStamp);
       
   696 			if (error != OMX_ErrorNone)
       
   697 			    {
       
   698 			    HandleIfError(error);
       
   699 			    }
       
   700 			iStartTimePending = EFalse;
       
   701 			}
       
   702 		else
       
   703 			{
       
   704 			// delay sending until clock transitions to WaitingForStartTime
       
   705 			iStartTime = aOutBuffer->nTimeStamp;
       
   706 			iStartTimePending = ETrue;
       
   707 			}
       
   708 		}
       
   709 		
       
   710 #ifdef _OMXIL_COMMON_DEBUG_TRACING_ON	
       
   711 	DEBUG_PRINTF(_L8("VS2::MbcBufferCopied **********************************"));
       
   712 	TTime t;
       
   713 	t.HomeTime();
       
   714 	DEBUG_PRINTF2(_L8("VS2::MbcBufferCopied : t.HomeTime() = %ld"), t.Int64());
       
   715 	DEBUG_PRINTF2(_L8("VS2::MbcBufferCopied : aOutBuffer = 0x%X"), aOutBuffer);
       
   716 	DEBUG_PRINTF2(_L8("VS2::MbcBufferCopied : aOutBuffer->nTimeStamp = %ld"), aOutBuffer->nTimeStamp);
       
   717 #endif	
       
   718 	iMutex.Signal();
       
   719 	
       
   720 	if (aOutBuffer->nFilledLen == 0 && !(aOutBuffer->nFlags & OMX_BUFFERFLAG_EOS))
       
   721 		{
       
   722 		// A likely cause of receiving an empty buffer is if the decoder implements a flush as
       
   723 		// returning buffers to supplier or always sending buffers to peer, rather than emptied
       
   724 		// and queued on the output port. In this case we return the buffer immediately without
       
   725 		// making a media time request. Probably the timestamp is invalid or the clock is not in
       
   726 		// the running state, in either case we could deadlock by queueing the empty buffers after
       
   727 		// a flush and preventing new data from being delivered. However these buffers were not
       
   728 		// returned in BufferIndication() in case there were any flags that need processing.
       
   729 		iBufferCopierStateMonitor->BufferCopier()->DeliverBuffer(aOutBuffer, OMX_DirOutput);
       
   730 		}
       
   731 	else
       
   732 		{
       
   733 		if (!iIsClockStopped)
       
   734 		    {
       
   735 		    error = iComponent.MediaTimeRequest(aOutBuffer, aOutBuffer->nTimeStamp, iRenderTime);
       
   736 		    if (error != OMX_ErrorNone)
       
   737 		        {
       
   738 		        HandleIfError(error);
       
   739 		        }
       
   740 		    }
       
   741 		}
       
   742 	}
       
   743 
       
   744 /** Called when a buffer is flushed from the buffer copier. */
       
   745 void COmxILVideoSchedulerPF::MbcBufferFlushed(OMX_BUFFERHEADERTYPE* aBuffer, OMX_DIRTYPE aDirection)
       
   746 	{
       
   747 	TInt portIndex = 0;
       
   748 	aBuffer->nFilledLen = 0;
       
   749 	aBuffer->nOffset = 0;
       
   750 	aBuffer->nFlags = 0;
       
   751 	aBuffer->nTimeStamp = 0;
       
   752 
       
   753 	if (aDirection == OMX_DirOutput)
       
   754 		{
       
   755 		++iOutputBufferSentCount;
       
   756 		portIndex = 1;
       
   757 		}
       
   758 
       
   759 	iCallbacks.BufferDoneNotification(aBuffer, portIndex, aDirection);
       
   760 	}
       
   761 
       
   762 void COmxILVideoSchedulerPF::DoSendOutputBuffer(OMX_BUFFERHEADERTYPE* aBuffer)
       
   763     {
       
   764     OMX_ERRORTYPE error;
       
   765     // A zero length buffer means this buffer is just being sent because it
       
   766     // has the EOS flag.
       
   767     if (aBuffer->nFilledLen > 0)
       
   768         {
       
   769         aBuffer->nTickCount = 0xC0C0C0C0;
       
   770         iTimeStamp = aBuffer->nTimeStamp;
       
   771         }
       
   772     
       
   773     error = iCallbacks.BufferDoneNotification(aBuffer, 1, OMX_DirOutput);
       
   774     if (error != OMX_ErrorNone)
       
   775         {
       
   776         HandleIfError(error);
       
   777         }
       
   778 
       
   779     iOutputBufferSentCount++;
       
   780 
       
   781     OMX_U32 flags = aBuffer->nFlags;
       
   782     if(flags & OMX_BUFFERFLAG_EOS)
       
   783         {
       
   784         error = iCallbacks.EventNotification(OMX_EventBufferFlag, 1, flags, NULL);
       
   785         if (error != OMX_ErrorNone)
       
   786             {
       
   787             HandleIfError(error);
       
   788             }
       
   789         }
       
   790     }
       
   791 
       
   792 void COmxILVideoSchedulerPF::HandleIfError(OMX_ERRORTYPE aOmxError)
       
   793     {
       
   794     if (aOmxError != OMX_ErrorNone)
       
   795         {
       
   796         iInvalid = ETrue;
       
   797         iCallbacks.ErrorEventNotification(aOmxError);
       
   798         }
       
   799     }
       
   800 
       
   801 OMX_ERRORTYPE COmxILVideoSchedulerPF::SymbianErrorToOmx(TInt aError)
       
   802 	{
       
   803 	switch(aError)
       
   804 		{
       
   805 	case KErrNone:
       
   806 		return OMX_ErrorNone;
       
   807 	case KErrNoMemory:
       
   808 		return OMX_ErrorInsufficientResources;
       
   809 	default:
       
   810 		return OMX_ErrorUndefined;
       
   811 		}
       
   812 	}
       
   813 
       
   814 
       
   815 
       
   816 void COmxILVideoSchedulerPF::Panic(TVideoSchedulerPanic aPanicCode) const
       
   817 	{
       
   818 	// const allows const methods to panic using this method
       
   819 	// however we wish to release the mutex to avoid blocking other threads
       
   820 	RMutex& mutex = const_cast<RMutex&>(iMutex);
       
   821 	if(mutex.IsHeld())
       
   822 		{
       
   823 		mutex.Signal();
       
   824 		}
       
   825 	User::Panic(KVideoSchedulerPanicCategory, aPanicCode);
       
   826 	}