tsrc/xmltestharness/xmlclient/src/asbreakeventhandler.cpp
changeset 0 0e4a32b9112d
equal deleted inserted replaced
-1:000000000000 0:0e4a32b9112d
       
     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 #include <e32debug.h>
       
    25 #include "asbreakeventhandler.h"
       
    26 
       
    27 const TDesC* StateDes(OMX_STATETYPE);
       
    28 
       
    29 RASBreakEventHandler::RASBreakEventHandler(MASBreakCallback& aHandler):
       
    30 iHandler(aHandler),
       
    31 iCounter(0),
       
    32 iMode(EIdle)
       
    33 	{
       
    34 	iOmxCallbackType.EventHandler = EventHandler;
       
    35 	
       
    36 	// do nothing function is set for these callbacks, rather than using NULL
       
    37 	iOmxCallbackType.EmptyBufferDone = EmptyBufferDone;
       
    38 	iOmxCallbackType.FillBufferDone = FillBufferDone;
       
    39 	}
       
    40 
       
    41 TInt RASBreakEventHandler::Create()
       
    42 	{
       
    43 	return iMutex.CreateLocal();
       
    44 	}
       
    45 
       
    46 void RASBreakEventHandler::Close()
       
    47 	{
       
    48 	for(TInt aIndex = 0, aCount = iComponents.Count(); aIndex < aCount; aIndex++)
       
    49 		{
       
    50 		delete iComponents[aIndex].iName;
       
    51 		}
       
    52 	iComponents.Close();
       
    53 	iCounter = 0;
       
    54 	iMode = EIdle;
       
    55 	iMutex.Close();
       
    56 	}
       
    57 
       
    58 void RASBreakEventHandler::AddComponentL(OMX_COMPONENTTYPE* aComponent, const TDesC& aName)
       
    59 	{
       
    60 	ASSERT(iMode == EIdle);
       
    61 	TComponentInfo componentInfo;
       
    62 	componentInfo.iHandle = aComponent;
       
    63 	componentInfo.iName = HBufC::NewLC(aName.Length());
       
    64 	*(componentInfo.iName) = aName;
       
    65 	componentInfo.iComplete = EFalse;
       
    66 	User::LeaveIfError(iComponents.Append(componentInfo));
       
    67 	CleanupStack::Pop(componentInfo.iName);
       
    68 	}
       
    69 
       
    70 OMX_ERRORTYPE RASBreakEventHandler::FillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
       
    71                                                    OMX_IN OMX_PTR pAppData,
       
    72                                                    OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
       
    73 	{
       
    74 	return reinterpret_cast<RASBreakEventHandler*>(pAppData)->DoBufferDone(reinterpret_cast<OMX_COMPONENTTYPE*>(hComponent), pBuffer, ETrue);
       
    75 	}
       
    76 
       
    77 OMX_ERRORTYPE RASBreakEventHandler::EmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
       
    78                                                     OMX_IN OMX_PTR pAppData,
       
    79                                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
       
    80 	{
       
    81 	return reinterpret_cast<RASBreakEventHandler*>(pAppData)->DoBufferDone(reinterpret_cast<OMX_COMPONENTTYPE*>(hComponent), pBuffer, EFalse);
       
    82 	}
       
    83 
       
    84 OMX_ERRORTYPE RASBreakEventHandler::DoBufferDone(OMX_COMPONENTTYPE* aComponent, OMX_BUFFERHEADERTYPE* aBufHdr, TBool aSource)
       
    85 	{
       
    86 	iHandler.BufferDone(aComponent, aBufHdr, aSource);
       
    87 	return OMX_ErrorNone;
       
    88 	}
       
    89 
       
    90 OMX_ERRORTYPE RASBreakEventHandler::EventHandler(OMX_HANDLETYPE hComponent,
       
    91 		OMX_PTR pAppData,
       
    92 		OMX_EVENTTYPE eEvent,
       
    93 		OMX_U32 nData1,
       
    94 		OMX_U32 nData2,
       
    95 		OMX_PTR pEventData)
       
    96 	{
       
    97 	return reinterpret_cast<RASBreakEventHandler*>(pAppData)->DoEventHandler(reinterpret_cast<OMX_COMPONENTTYPE*>(hComponent), eEvent, nData1, nData2, pEventData);
       
    98 	}
       
    99 
       
   100 OMX_ERRORTYPE RASBreakEventHandler::DoEventHandler(OMX_COMPONENTTYPE* aComponent, OMX_EVENTTYPE aEvent, TUint32 aData1, TUint32 aData2, TAny* aEventData)
       
   101 	{
       
   102 	iMutex.Wait();
       
   103 	if((iMode == EAwaitingTransition || iMode == EAwaitingSingleTransition) && aEvent == OMX_EventCmdComplete && aData1 == OMX_CommandStateSet)
       
   104 		{
       
   105 		StateSet(aComponent, (OMX_STATETYPE) aData2);
       
   106 		}
       
   107 	else if(iMode == EAwaitingEOS && aEvent == OMX_EventBufferFlag && aData2 & OMX_BUFFERFLAG_EOS)
       
   108 		{
       
   109 		EndOfStream(aComponent);
       
   110 		}
       
   111 	else
       
   112 		{
       
   113 		iHandler.EventReceived(aComponent, aEvent, aData1, aData2, aEventData);
       
   114 		}
       
   115 	iMutex.Signal();
       
   116 	return OMX_ErrorNone;
       
   117 	}
       
   118 
       
   119 void RASBreakEventHandler::StateSet(OMX_COMPONENTTYPE* aComponent, OMX_STATETYPE aState)
       
   120 	{
       
   121 	RDebug::Print(_L("State transition: component %S is in state %S\n"), ComponentName(aComponent), StateDes(aState));
       
   122 	if(iMode == EAwaitingTransition)
       
   123 		{
       
   124 		__ASSERT_ALWAYS(aState == iNewState, User::Panic(_L("RASBreakEventHandler"), 2));
       
   125 		if(--iCounter == 0)
       
   126 			{
       
   127 			iMode = EIdle;
       
   128 			iHandler.AllComponentsTransitioned(iNewState, iOldState);
       
   129 			}
       
   130 		}
       
   131 	else if (iMode == EAwaitingSingleTransition)
       
   132 		{
       
   133 		__ASSERT_ALWAYS(aState == iNewState, User::Panic(_L("RASBreakEventHandler"), 2));
       
   134 		iMode = EIdle;
       
   135 		iHandler.ComponentTransitioned(iNewState, iOldState);		
       
   136 		}
       
   137 	}
       
   138 
       
   139 void RASBreakEventHandler::EndOfStream(OMX_COMPONENTTYPE* aComponent)
       
   140 	{
       
   141 	RDebug::Print(_L("End of stream from component %S\n"), ComponentName(aComponent));
       
   142 	if(iMode == EAwaitingEOS)
       
   143 		{
       
   144 		TInt index = iComponents.Find(aComponent, TComponentInfo::CompareHandle);
       
   145 		// panic if component was not found
       
   146 		__ASSERT_ALWAYS(index >= 0, User::Invariant());
       
   147 	
       
   148 		// only respond to EOS changing state, to allow potential duplicates
       
   149 		if(!iComponents[index].iComplete)
       
   150 			{
       
   151 			iComponents[index].iComplete = ETrue;
       
   152 			iCounter--;
       
   153 			// if iEOSComponent is NULL, wait for all components, otherwise wait for individual component
       
   154 			if(iEOSComponent == NULL && iCounter == 0 || iEOSComponent == aComponent)
       
   155 				{
       
   156 				iMode = EIdle;
       
   157 				iHandler.AllComponentsEOS();
       
   158 				}
       
   159 			}
       
   160 		}
       
   161 	}
       
   162 
       
   163 void RASBreakEventHandler::AwaitTransition(OMX_STATETYPE aNewState, OMX_STATETYPE aOldState)
       
   164 	{
       
   165 	RDebug::Print(_L("Awaiting all components to transition from %S to %S\n"), StateDes(aOldState), StateDes(aNewState));
       
   166 	ASSERT(iMode == EIdle);
       
   167 	iMode = EAwaitingTransition;
       
   168 	iNewState = aNewState;
       
   169 	iOldState = aOldState;
       
   170 	iCounter = iComponents.Count();
       
   171 	}
       
   172 
       
   173 void RASBreakEventHandler::AwaitSingleTransition(OMX_COMPONENTTYPE* aComponent, OMX_STATETYPE aNewState, OMX_STATETYPE aOldState)
       
   174 	{
       
   175 	RDebug::Print(_L("Awaiting %S components to transition from %S to %S\n"), ComponentName(aComponent), StateDes(aOldState), StateDes(aNewState));	
       
   176 	ASSERT(iMode == EIdle);
       
   177 	iMode = EAwaitingSingleTransition;
       
   178 	iNewState = aNewState;
       
   179 	iOldState = aOldState;
       
   180 	}
       
   181 
       
   182 void RASBreakEventHandler::AwaitEOS()
       
   183 	{
       
   184 	RDebug::Print(_L("Awaiting End Of Stream flag from all components\n"));
       
   185 	ASSERT(iMode == EIdle);
       
   186 	iMode = EAwaitingEOS;
       
   187 	iEOSComponent = NULL;	// wait for all components, not an individual component
       
   188 	for(iCounter = 0; iCounter < iComponents.Count(); iCounter++)
       
   189 		{
       
   190 		iComponents[iCounter].iComplete = EFalse;
       
   191 		}
       
   192 	}
       
   193 
       
   194 void RASBreakEventHandler::AwaitEOS(OMX_COMPONENTTYPE* aComponent)
       
   195 	{
       
   196 	RDebug::Print(_L("Awaiting End Of Stream flag from component %S\n"), ComponentName(aComponent));
       
   197 	ASSERT(iMode == EIdle);
       
   198 	iMode = EAwaitingEOS;
       
   199 	iEOSComponent = aComponent;
       
   200 	for(iCounter = 0; iCounter < iComponents.Count(); iCounter++)
       
   201 		{
       
   202 		iComponents[iCounter].iComplete = EFalse;
       
   203 		}
       
   204 	}
       
   205 
       
   206 const TDesC* RASBreakEventHandler::ComponentName(OMX_COMPONENTTYPE* aComponent)
       
   207 	{
       
   208 	TInt index = iComponents.Find(aComponent, TComponentInfo::CompareHandle);
       
   209 	if(index == KErrNotFound)
       
   210 		{
       
   211 		return NULL;
       
   212 		}
       
   213 	return iComponents[index].iName;
       
   214 	}
       
   215 
       
   216 OMX_CALLBACKTYPE& RASBreakEventHandler::CallbackStruct()
       
   217 	{
       
   218 	return iOmxCallbackType;
       
   219 	}