omxilcomp/omxiltestcomp/md5sink/src/omxiltestmd5sinkprocessingfunction.cpp
changeset 0 58be5850fb6c
equal deleted inserted replaced
-1:000000000000 0:58be5850fb6c
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 #include <hash.h>
       
    25 #include "omxiltestmd5sinkprocessingfunction.h"
       
    26 #include "omxiltestmd5sinkconfigmanager.h"
       
    27 #include <openmax/il/common/omxilcallbacknotificationif.h>
       
    28 #include "omxiltestmd5sinkconst.h"
       
    29 
       
    30 enum { KMsgQueueSize = 10 };
       
    31 
       
    32 COmxILTestMD5SinkProcessingFunction* COmxILTestMD5SinkProcessingFunction::NewL(MOmxILCallbackNotificationIf& aCallbacks)
       
    33 	{
       
    34 	COmxILTestMD5SinkProcessingFunction* self = new (ELeave) COmxILTestMD5SinkProcessingFunction(aCallbacks);
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL(aCallbacks);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 COmxILTestMD5SinkProcessingFunction::COmxILTestMD5SinkProcessingFunction(MOmxILCallbackNotificationIf& aCallbacks)
       
    42 : COmxILProcessingFunction(aCallbacks)
       
    43 	{
       
    44 	}
       
    45 
       
    46 void COmxILTestMD5SinkProcessingFunction::ConstructL(MOmxILCallbackNotificationIf& aCallback)
       
    47 	{
       
    48 	iMD5Active = CMD5Active::NewL(aCallback);
       
    49 	iFlushHandler = CFlushActive::NewL(*iMD5Active);
       
    50 	}
       
    51 
       
    52 COmxILTestMD5SinkProcessingFunction::~COmxILTestMD5SinkProcessingFunction()
       
    53 	{
       
    54     delete iFlushHandler;
       
    55 	delete iMD5Active;
       
    56 	}
       
    57 
       
    58 void COmxILTestMD5SinkProcessingFunction::SetConfigManager(COmxILTestMD5SinkConfigManager* aConfigManager)
       
    59 	{
       
    60 	iMD5Active->SetConfigManager(aConfigManager);
       
    61 	}
       
    62 
       
    63 OMX_ERRORTYPE COmxILTestMD5SinkProcessingFunction::StateTransitionIndication(TStateIndex /*aNewState*/)
       
    64 	{
       
    65     return OMX_ErrorNone;
       
    66 	}
       
    67 
       
    68 OMX_ERRORTYPE COmxILTestMD5SinkProcessingFunction::BufferFlushingIndication(TUint32 aPortIndex, OMX_DIRTYPE aDirection)
       
    69 	{
       
    70     if ((aPortIndex != OMX_ALL) && ((aPortIndex != KMD5SINK_OPB0PORT_INDEX) || (aDirection != OMX_DirInput)))
       
    71         {
       
    72         return OMX_ErrorNone;
       
    73         }
       
    74     
       
    75     iFlushHandler->Flush();
       
    76     
       
    77 	return OMX_ErrorNone;
       
    78 	}
       
    79 
       
    80 OMX_ERRORTYPE COmxILTestMD5SinkProcessingFunction::ParamIndication(OMX_INDEXTYPE /*aParamIndex*/,
       
    81 													const TAny* /*apComponentParameterStructure*/)
       
    82 	{
       
    83 	return OMX_ErrorNone;
       
    84 	}
       
    85 
       
    86 OMX_ERRORTYPE COmxILTestMD5SinkProcessingFunction::ConfigIndication(OMX_INDEXTYPE /*aConfigIndex*/,
       
    87 												 const TAny* /*apComponentConfigStructure*/)
       
    88 	{
       
    89 	return OMX_ErrorNone;
       
    90 	}
       
    91 
       
    92 OMX_ERRORTYPE COmxILTestMD5SinkProcessingFunction::BufferIndication(OMX_BUFFERHEADERTYPE* apBufferHeader,
       
    93 																OMX_DIRTYPE aDirection)			
       
    94 	{
       
    95 	__ASSERT_ALWAYS(aDirection == OMX_DirInput, User::Invariant());
       
    96 	return iMD5Active->DeliverBuffer(apBufferHeader);
       
    97 	}
       
    98 
       
    99 OMX_BOOL COmxILTestMD5SinkProcessingFunction::BufferRemovalIndication(OMX_BUFFERHEADERTYPE* apBufferHeader, OMX_DIRTYPE aDirection)
       
   100 	{
       
   101 	__ASSERT_ALWAYS(aDirection == OMX_DirInput, User::Invariant());
       
   102 	return iMD5Active->RemoveBuffer(apBufferHeader);
       
   103 	}
       
   104 
       
   105 ///////////////////////////////////////////////////////////////////////////////////////////
       
   106 
       
   107 CMD5Active* CMD5Active::NewL(MOmxILCallbackNotificationIf& aCallback)
       
   108 	{
       
   109 	CMD5Active* self = new(ELeave) CMD5Active(aCallback);
       
   110 	CleanupStack::PushL(self);
       
   111 	self->ConstructL();
       
   112 	CleanupStack::Pop(self);
       
   113 	return self;
       
   114 	}
       
   115 
       
   116 CMD5Active::CMD5Active(MOmxILCallbackNotificationIf& aCallback):
       
   117 CActive(CActive::EPriorityStandard),
       
   118 iCallback(aCallback)
       
   119 	{
       
   120 	CActiveScheduler::Add(this);
       
   121 	}
       
   122 
       
   123 void CMD5Active::ConstructL()
       
   124 	{
       
   125 	iMD5 = CMD5::NewL();
       
   126 	User::LeaveIfError(iBuffers.CreateLocal(KMsgQueueSize));
       
   127 	iBuffers.NotifyDataAvailable(iStatus);
       
   128 	SetActive();
       
   129 	}
       
   130 
       
   131 CMD5Active::~CMD5Active()
       
   132 	{
       
   133 	Cancel();
       
   134 	delete iMD5;
       
   135 	}
       
   136 
       
   137 void CMD5Active::SetConfigManager(COmxILTestMD5SinkConfigManager* aConfigManager)
       
   138 	{
       
   139 	iConfigManager = aConfigManager;
       
   140 	}
       
   141 
       
   142 OMX_ERRORTYPE CMD5Active::DeliverBuffer(OMX_BUFFERHEADERTYPE* aBuffer)
       
   143 	{
       
   144 	TInt aErr = iBuffers.Send(aBuffer);
       
   145 	if(aErr == KErrNone)
       
   146 		{
       
   147 		return OMX_ErrorNone;
       
   148 		}
       
   149 	else if(aErr == KErrOverflow)
       
   150 		{
       
   151 		return OMX_ErrorInsufficientResources;
       
   152 		}
       
   153 	else
       
   154 		{
       
   155 		return OMX_ErrorUndefined;
       
   156 		}
       
   157 	}
       
   158 
       
   159 OMX_BOOL CMD5Active::RemoveBuffer(OMX_BUFFERHEADERTYPE* aBuffer)
       
   160 	{
       
   161 	TBool removed = EFalse;
       
   162 	TRAPD(err, removed = RemoveFromQueueL(iBuffers, aBuffer));
       
   163 	if(err)
       
   164 		{
       
   165 		User::Invariant();
       
   166 		}
       
   167 	return removed ? OMX_TRUE : OMX_FALSE;
       
   168 	}
       
   169 
       
   170 TBool CMD5Active::RemoveFromQueueL(RMsgQueue<OMX_BUFFERHEADERTYPE*>& aQueue, OMX_BUFFERHEADERTYPE* aBufferHeader)
       
   171 	{
       
   172 	TBool removed = EFalse;
       
   173 	RMsgQueue<OMX_BUFFERHEADERTYPE*> tempQueue;
       
   174 	User::LeaveIfError(tempQueue.CreateLocal(KMsgQueueSize));
       
   175 	OMX_BUFFERHEADERTYPE* bufferHeader = NULL;
       
   176 	while(aQueue.Receive(bufferHeader) != KErrUnderflow)
       
   177 		{
       
   178 		if(bufferHeader != aBufferHeader)
       
   179 			{
       
   180 			__ASSERT_ALWAYS(tempQueue.Send(bufferHeader) == KErrNone, User::Invariant());
       
   181 			}
       
   182 		else
       
   183 			{
       
   184 			removed = ETrue;
       
   185 			}
       
   186 		}
       
   187 	while(tempQueue.Receive(bufferHeader) != KErrUnderflow)
       
   188 		{
       
   189 		__ASSERT_ALWAYS(aQueue.Send(bufferHeader) == KErrNone, User::Invariant());
       
   190 		}
       
   191 	tempQueue.Close();
       
   192 	return removed;
       
   193 	}
       
   194 
       
   195 void CMD5Active::RunL()
       
   196 	{
       
   197 	OMX_BUFFERHEADERTYPE* aBuff;
       
   198 	// may have an empty queue despite being notified if a RemoveBuffer() occured in the meantime
       
   199 	if(iBuffers.Receive(aBuff) != KErrUnderflow)
       
   200 		{	
       
   201 		// hash the buffer
       
   202 		TPtrC8 aDes(aBuff->pBuffer + aBuff->nOffset, aBuff->nFilledLen);
       
   203 		TPtrC8 aHash = iMD5->Hash(aDes);
       
   204 		iConfigManager->SetHash(aHash);
       
   205 		aBuff->nFilledLen = 0;
       
   206 		OMX_U32 flags = aBuff->nFlags;
       
   207 		iCallback.BufferDoneNotification(aBuff, KMD5SINK_OPB0PORT_INDEX, OMX_DirInput);
       
   208 		if(flags)
       
   209 			{
       
   210 			iCallback.EventNotification(OMX_EventBufferFlag, 0, flags, NULL);
       
   211 			}
       
   212 		}	
       
   213 	iBuffers.NotifyDataAvailable(iStatus);
       
   214 	SetActive();
       
   215 	}
       
   216 
       
   217 void CMD5Active::FlushHeaders()
       
   218     {
       
   219     OMX_BUFFERHEADERTYPE* aBuff;
       
   220     
       
   221     while(iBuffers.Receive(aBuff) == KErrNone)
       
   222         {   
       
   223         aBuff->nFilledLen = 0;
       
   224         iCallback.BufferDoneNotification(aBuff, KMD5SINK_OPB0PORT_INDEX, OMX_DirInput);
       
   225         }
       
   226     }
       
   227 
       
   228 void CMD5Active::DoCancel()
       
   229 	{
       
   230 	iBuffers.CancelDataAvailable();
       
   231 	}
       
   232 
       
   233 ///////////////////////////////////////////////////////////////////////////////////////////
       
   234 
       
   235 CFlushActive* CFlushActive::NewL(CMD5Active& aBufferHandler)
       
   236     {
       
   237     CFlushActive* self = new (ELeave) CFlushActive(aBufferHandler);
       
   238     CleanupStack::PushL(self);
       
   239     self->ConstructL();
       
   240     CleanupStack::Pop(self);
       
   241     return self;
       
   242     }
       
   243 
       
   244 CFlushActive::CFlushActive(CMD5Active& aBufferHandler):
       
   245 CActive(CActive::EPriorityHigh),    // Higher priority than CMD5Active to ensure we run before them when the Active Scheduler is picking who to run next
       
   246 iBufferHandler(aBufferHandler)
       
   247     {
       
   248     CActiveScheduler::Add(this);
       
   249     iRunLContext = RThread();
       
   250     iStatus = KRequestPending;
       
   251     SetActive();
       
   252     }
       
   253 
       
   254 void CFlushActive::ConstructL()
       
   255     {
       
   256     User::LeaveIfError(iWaitSemaphore.CreateLocal(0));
       
   257     }
       
   258 
       
   259 CFlushActive::~CFlushActive()
       
   260     {
       
   261     Cancel();
       
   262     iRunLContext.Close();
       
   263     iWaitSemaphore.Close(); 
       
   264     }
       
   265 
       
   266 void CFlushActive::Flush()
       
   267     {
       
   268     RThread currentContext;
       
   269     if (currentContext.Id() == iRunLContext.Id())
       
   270         {
       
   271         // The same context so cannot be running concurrently with CMD5Active::RunL() so safe to drain buffer queue
       
   272         iBufferHandler.FlushHeaders();
       
   273         return;
       
   274         }
       
   275     
       
   276     // Different thread to the one in which the Active objects are running,
       
   277     TRequestStatus* selfStatus = &iStatus;
       
   278     iRunLContext.RequestComplete(selfStatus, KErrNone);
       
   279     iWaitSemaphore.Wait();
       
   280     }
       
   281      
       
   282 void CFlushActive::RunL()
       
   283     {
       
   284     // Now sure we are not running concurrently with CMD5Active::RunL()
       
   285     iBufferHandler.FlushHeaders();
       
   286     iStatus = KRequestPending;
       
   287     SetActive();
       
   288     iWaitSemaphore.Signal();
       
   289     }
       
   290 
       
   291 void CFlushActive::DoCancel()
       
   292     {
       
   293     iWaitSemaphore.Signal();    // Only happens as part of deletion so irrelevant whether we are blocked or not
       
   294     TRequestStatus* selfStatus = &iStatus;
       
   295     User::RequestComplete(selfStatus, KErrNone);
       
   296     }