tsrc/xmltestharness/xmlclient/src/baseprofilehandler.cpp
changeset 0 0e4a32b9112d
equal deleted inserted replaced
-1:000000000000 0:0e4a32b9112d
       
     1 /*
       
     2 * Copyright (c) 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 #include "baseprofilehandler.h"
       
    20 
       
    21 _LIT(KErrDialogInvalidPortIndex, "Base Profile support command called on port not configured for base profile comms, component: %S, port: %d");
       
    22 _LIT(KErrDialogBufferNumber, "Test script specified %d buffers, less than the port's minimum of %d, component: %S, port: %d");
       
    23 _LIT(KErrDialogFillBufferInvalidBufIndex, "Test script specified invalid buffer index on a FillThisBuffer call, component: %S, port: %d, buffer index passed: %d");
       
    24 _LIT(KErrDialogEmptyBufferInvalidBufIndex, "Test script specified invalid buffer index on a EmptyThisBuffer call, component: %S, port: %d, buffer index passed: %d");
       
    25 
       
    26 
       
    27 CBaseProfileHandler::CBaseProfileHandler(ROmxScriptTest& aTestOwner, RASBreakEventHandler& aParentEventHandler)
       
    28 : iErrorCallbacks(aTestOwner), iEventHandlerCallbacks(aParentEventHandler)
       
    29     {
       
    30     iOmxCallbacks.EmptyBufferDone = EmptyBufferDone;
       
    31     iOmxCallbacks.EventHandler = EventHandler;
       
    32     iOmxCallbacks.FillBufferDone = FillBufferDone;
       
    33     iMutex.CreateLocal();
       
    34     }
       
    35 
       
    36 CBaseProfileHandler::~CBaseProfileHandler()
       
    37     {
       
    38     iMutex.Close();
       
    39     delete iXmlName;
       
    40     iBufferHeaders.ResetAndDestroy();
       
    41     iPortsUsingBaseProfile.Close();
       
    42     }
       
    43 
       
    44 void CBaseProfileHandler::AddPortSupportL(TInt aPortIndex)
       
    45     {
       
    46     TPortInfo portInfo;
       
    47     portInfo.iPortIndex = aPortIndex;
       
    48     portInfo.iAutoMode = EFalse;
       
    49     portInfo.iWeAreBufferSupplier = ETrue;
       
    50     iPortsUsingBaseProfile.AppendL(portInfo);
       
    51     }
       
    52 
       
    53 void CBaseProfileHandler::SetBufferSupplier(TInt aPortIndex, TBool aComponentBufferSupplier)
       
    54     {
       
    55     TInt portIndex = iPortsUsingBaseProfile.Find(aPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
    56     if (portIndex == KErrNotFound)
       
    57         {
       
    58         TBuf<140> errorString;
       
    59         errorString.Format(KErrDialogInvalidPortIndex, iXmlName, aPortIndex);
       
    60         iErrorCallbacks.FailTest(errorString);
       
    61         return;
       
    62         }
       
    63     iPortsUsingBaseProfile[portIndex].iWeAreBufferSupplier = !aComponentBufferSupplier;
       
    64     }
       
    65 
       
    66 void CBaseProfileHandler::SetAutoMode(TInt aPortIndex, TBool aAutoMode)
       
    67     {
       
    68     TInt portIndex = iPortsUsingBaseProfile.Find(aPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
    69     if (portIndex == KErrNotFound)
       
    70         {
       
    71         TBuf<140> errorString;
       
    72         errorString.Format(KErrDialogInvalidPortIndex, iXmlName, aPortIndex);
       
    73         iErrorCallbacks.FailTest(errorString);
       
    74         return;
       
    75         }
       
    76     iPortsUsingBaseProfile[portIndex].iAutoMode = aAutoMode;
       
    77     }
       
    78 
       
    79 TInt CBaseProfileHandler::LocateBufferForPort(TInt aPortIndex, TInt aBufferIndexInPort)
       
    80     {
       
    81     //Implementation based on the fact that:
       
    82     //-All the buffers for a port are allocated and appended to iBufferHeaders
       
    83     //in a single contiguous block.
       
    84     //-So once the first relevant buffer is found this index marks the zero index
       
    85     //for the set of indexed buffers relevant to that port.
       
    86     
       
    87     TInt result = -1;
       
    88     TInt bufferCount =  iBufferHeaders.Count();
       
    89     for (TInt bufferIndex = 0; bufferIndex < bufferCount; bufferIndex++)
       
    90         {
       
    91         if (iBufferHeaders[bufferIndex]->iPortIndex != aPortIndex)
       
    92             {
       
    93             continue;
       
    94             }
       
    95         
       
    96         //Safety check that the script hasn't made a manual Fill/Empty call on 
       
    97         //an invalid buffer index
       
    98         if (iBufferHeaders[bufferIndex]->iPortIndex == iBufferHeaders[(bufferIndex + aBufferIndexInPort)]->iPortIndex)
       
    99             {
       
   100             result = bufferIndex + aBufferIndexInPort;
       
   101             }
       
   102         break;
       
   103         }
       
   104     return result;
       
   105     }
       
   106 
       
   107 void CBaseProfileHandler::FillThisBuffer(TInt aPortIndex, TInt aBufferIndexInPort)
       
   108     {
       
   109     TInt bufHeaderIndex = LocateBufferForPort(aPortIndex, aBufferIndexInPort);
       
   110     if (bufHeaderIndex >= 0)
       
   111         {
       
   112         iBufferHeaders[bufHeaderIndex]->iBufferAvailable = EFalse;
       
   113         iOmxComponent->FillThisBuffer(iOmxComponent, iBufferHeaders[bufHeaderIndex]->iBufferHeader);
       
   114         }
       
   115     else
       
   116         {
       
   117         TBuf<140> errorString;
       
   118         errorString.Format(KErrDialogFillBufferInvalidBufIndex, iXmlName, aPortIndex, aBufferIndexInPort);
       
   119         iErrorCallbacks.FailTest(errorString);
       
   120         }
       
   121     }
       
   122 
       
   123 void CBaseProfileHandler::EmptyThisBuffer(TInt aPortIndex, TInt aBufferIndexInPort)
       
   124     {
       
   125     TInt bufHeaderIndex = LocateBufferForPort(aPortIndex, aBufferIndexInPort);
       
   126     if (bufHeaderIndex >= 0)
       
   127         {
       
   128         iBufferHeaders[bufHeaderIndex]->iBufferAvailable = EFalse;
       
   129         iOmxComponent->EmptyThisBuffer(iOmxComponent, iBufferHeaders[bufHeaderIndex]->iBufferHeader);
       
   130         }
       
   131     else
       
   132         {
       
   133         TBuf<140> errorString;
       
   134         errorString.Format(KErrDialogEmptyBufferInvalidBufIndex, iXmlName, aPortIndex, aBufferIndexInPort);
       
   135         iErrorCallbacks.FailTest(errorString);
       
   136         }    
       
   137     }
       
   138 
       
   139 void CBaseProfileHandler::WaitForBufferCompletion(TInt aPortIndex, TInt aBufferIndexInPort)
       
   140     {
       
   141     TInt bufHeaderIndex = LocateBufferForPort(aPortIndex, aBufferIndexInPort);
       
   142     iMutex.Wait();
       
   143     if (iBufferHeaders[bufHeaderIndex]->iBufferAvailable)
       
   144         {
       
   145         //Buffer has already completed either while waiting on another buffer or as a result of test command processing delay, no need to wait
       
   146         iMutex.Signal();
       
   147         }
       
   148     else
       
   149         {
       
   150         iWaitingOnBuffer = iBufferHeaders[bufHeaderIndex]->iBufferHeader;
       
   151         iMutex.Signal();
       
   152         iErrorCallbacks.BeginWait();
       
   153         }
       
   154 
       
   155     }
       
   156 
       
   157 OMX_ERRORTYPE CBaseProfileHandler::EventHandler(OMX_HANDLETYPE /*hComponent*/, OMX_PTR pAppData, OMX_EVENTTYPE eEvent, OMX_U32 nData1, OMX_U32 nData2, OMX_PTR pEventData)
       
   158     {
       
   159     reinterpret_cast<CBaseProfileHandler*>(pAppData)->HandleEventReceived(eEvent, nData1, nData2, pEventData);
       
   160     return OMX_ErrorNone;
       
   161     }
       
   162 
       
   163 OMX_ERRORTYPE CBaseProfileHandler::FillBufferDone(OMX_IN OMX_HANDLETYPE /*hComponent*/, OMX_IN OMX_PTR pAppData, OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
       
   164     {
       
   165     reinterpret_cast<CBaseProfileHandler*>(pAppData)->HandleFillBufferDone(pBuffer);
       
   166     return OMX_ErrorNone;
       
   167     }
       
   168 
       
   169 OMX_ERRORTYPE CBaseProfileHandler::EmptyBufferDone(OMX_IN OMX_HANDLETYPE /*hComponent*/, OMX_IN OMX_PTR pAppData, OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
       
   170     {
       
   171     reinterpret_cast<CBaseProfileHandler*>(pAppData)->HandleEmptyBufferDone(pBuffer);
       
   172     return OMX_ErrorNone;
       
   173     }
       
   174 
       
   175 void CBaseProfileHandler::HandleEventReceived(OMX_EVENTTYPE aeEvent, OMX_U32 anData1, OMX_U32 anData2, OMX_PTR apEventData)
       
   176     {
       
   177     DoEventReceived(aeEvent, anData1, anData2, apEventData);
       
   178     iEventHandlerCallbacks.EventHandler(iOmxComponent, &iEventHandlerCallbacks, aeEvent, anData1, anData2, apEventData);
       
   179     }
       
   180 
       
   181 void CBaseProfileHandler::HandleFillBufferDone(OMX_IN OMX_BUFFERHEADERTYPE* aFilledBuffer)
       
   182     {
       
   183     iMutex.Wait();
       
   184     DoFillBufferDone(aFilledBuffer);
       
   185     TInt bufferIndex = iBufferHeaders.Find(*aFilledBuffer, CBaseProfileHandler::BufferHeaderMatchComparison);
       
   186     iBufferHeaders[bufferIndex]->iBufferAvailable = ETrue;
       
   187     TInt portIndex = iPortsUsingBaseProfile.Find((*iBufferHeaders[bufferIndex]).iPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
   188     
       
   189     if (iPortsUsingBaseProfile[portIndex].iAutoMode)
       
   190         {
       
   191         iBufferHeaders[bufferIndex]->iBufferAvailable = EFalse;
       
   192         iOmxComponent->FillThisBuffer(iOmxComponent, aFilledBuffer);
       
   193         }
       
   194     
       
   195     if (iWaitingOnBuffer == iBufferHeaders[bufferIndex]->iBufferHeader)
       
   196         {
       
   197         iWaitingOnBuffer = NULL;
       
   198         iErrorCallbacks.EndWait();
       
   199         }
       
   200     
       
   201     iMutex.Signal();
       
   202     }
       
   203 
       
   204 void CBaseProfileHandler::HandleEmptyBufferDone(OMX_IN OMX_BUFFERHEADERTYPE* aEmptiedBuffer)
       
   205     {
       
   206     iMutex.Wait();
       
   207     DoEmptyBufferDone(aEmptiedBuffer);
       
   208     TInt bufferIndex = iBufferHeaders.Find(*aEmptiedBuffer, CBaseProfileHandler::BufferHeaderMatchComparison);
       
   209     iBufferHeaders[bufferIndex]->iBufferAvailable = ETrue;
       
   210     TInt portIndex = iPortsUsingBaseProfile.Find((*iBufferHeaders[bufferIndex]).iPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
   211     
       
   212     if (iPortsUsingBaseProfile[portIndex].iAutoMode)
       
   213         {
       
   214         iBufferHeaders[bufferIndex]->iBufferAvailable = EFalse;
       
   215         iOmxComponent->EmptyThisBuffer(iOmxComponent, aEmptiedBuffer);
       
   216         }
       
   217     
       
   218     if (iWaitingOnBuffer == iBufferHeaders[bufferIndex]->iBufferHeader)
       
   219         {
       
   220         iWaitingOnBuffer = NULL;
       
   221         iErrorCallbacks.EndWait();
       
   222         }
       
   223     
       
   224     iMutex.Signal();
       
   225     }
       
   226    
       
   227 OMX_COMPONENTTYPE* CBaseProfileHandler::LoadComponentL(const TDesC8& aTestSpecificName, const TDesC8& aOmxName)
       
   228     {
       
   229     
       
   230     iXmlName = HBufC::NewL(aTestSpecificName.Length());
       
   231 	iXmlName->Des().Copy(aTestSpecificName);
       
   232     // allow room for the '\0' used in call to OMX_GetHandle
       
   233     HBufC8* omxNameToLoad = HBufC8::NewL(aOmxName.Length() + 1);
       
   234     *omxNameToLoad = aOmxName;
       
   235     
       
   236     OMX_ERRORTYPE error = OMX_GetHandle((TAny**) &iOmxComponent, (OMX_STRING) omxNameToLoad->Des().PtrZ(), this, &iOmxCallbacks);
       
   237     delete omxNameToLoad;
       
   238     if (error != OMX_ErrorNone)
       
   239         {
       
   240         iErrorCallbacks.FailWithOmxError(_L("OMX_GetHandle()"), error);
       
   241         }
       
   242     
       
   243     return iOmxComponent;
       
   244     }
       
   245 
       
   246 void CBaseProfileHandler::FreeAllocatedBuffersL()	
       
   247 	{
       
   248 	for (TInt i = 0; i < iBufferHeaders.Count(); ++i)
       
   249 		{
       
   250 		delete iBufferHeaders[i];
       
   251 		}
       
   252 	iBufferHeaders.Reset();
       
   253 	}
       
   254 	
       
   255 void CBaseProfileHandler::SetupBuffersL(TInt aPortIndex, TInt aNumberBuffers)
       
   256     {
       
   257     TInt portInfoIndex = iPortsUsingBaseProfile.Find(aPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
   258     if (portInfoIndex == KErrNotFound)
       
   259         {
       
   260         TBuf<140> errorString;
       
   261         errorString.Format(KErrDialogInvalidPortIndex, iXmlName, aPortIndex);
       
   262         iErrorCallbacks.FailTest(errorString);
       
   263         return;   
       
   264         }
       
   265     
       
   266     OMX_PARAM_PORTDEFINITIONTYPE portDef;
       
   267     portDef.nSize = sizeof(portDef);
       
   268     portDef.nVersion = KOmxVersion;
       
   269     portDef.nPortIndex = aPortIndex;
       
   270     OMX_ERRORTYPE error = iOmxComponent->GetParameter(iOmxComponent, OMX_IndexParamPortDefinition, &portDef);
       
   271     if(error != OMX_ErrorNone)
       
   272         {
       
   273         iErrorCallbacks.FailWithOmxError(_L("GetParameter"), error);
       
   274         return;
       
   275         }
       
   276 
       
   277     if (aNumberBuffers < portDef.nBufferCountMin)
       
   278         {
       
   279         TBuf<140> errorString;
       
   280         errorString.Format(KErrDialogBufferNumber, aNumberBuffers, portDef.nBufferCountMin, iXmlName, aPortIndex);
       
   281         iErrorCallbacks.FailTest(errorString);
       
   282         return;
       
   283         }
       
   284     
       
   285     TInt bufSize = portDef.nBufferSize;    
       
   286     
       
   287     for (TInt bufIndex=0; bufIndex < aNumberBuffers; bufIndex++)
       
   288         {
       
   289         CBufferHeaderInfo* headerInfo = new (ELeave) CBufferHeaderInfo(*this);
       
   290         iBufferHeaders.AppendL(headerInfo);
       
   291         headerInfo->iPortIndex = aPortIndex;
       
   292              
       
   293         if (iPortsUsingBaseProfile[portInfoIndex].iWeAreBufferSupplier)
       
   294             {
       
   295             OMX_U8* buffer = new (ELeave) OMX_U8[bufSize];
       
   296             iOmxComponent->UseBuffer(iOmxComponent, &(headerInfo->iBufferHeader), aPortIndex, NULL, bufSize, buffer);
       
   297             }
       
   298         else
       
   299             {
       
   300             iOmxComponent->AllocateBuffer(iOmxComponent, &(headerInfo->iBufferHeader), aPortIndex, NULL, bufSize);
       
   301             }
       
   302         }
       
   303     }
       
   304 
       
   305 void CBaseProfileHandler::DoEventReceived(OMX_EVENTTYPE /*aeEvent*/, OMX_U32 /*anData1*/, OMX_U32 /*anData2*/, OMX_PTR /*apEventData*/)
       
   306     {
       
   307     //Uninterested
       
   308     }
       
   309 
       
   310 void CBaseProfileHandler::DoFillBufferDone(OMX_BUFFERHEADERTYPE* /*aFilledBuffer*/)
       
   311     {
       
   312     //Uninterested
       
   313     }
       
   314 
       
   315 void CBaseProfileHandler::DoEmptyBufferDone(OMX_BUFFERHEADERTYPE* /*aEmptiedBuffer*/)
       
   316     {
       
   317     //Uninterested
       
   318     }
       
   319 
       
   320 CBaseProfileHandler::CBufferHeaderInfo::CBufferHeaderInfo(CBaseProfileHandler& aParent)
       
   321 : iBufferAvailable(ETrue), iParent(aParent)
       
   322     {
       
   323     }
       
   324     
       
   325 CBaseProfileHandler::CBufferHeaderInfo::~CBufferHeaderInfo()
       
   326     {
       
   327     OMX_U8* buffer = iBufferHeader->pBuffer;
       
   328     OMX_ERRORTYPE error = iParent.iOmxComponent->FreeBuffer(iParent.iOmxComponent, iPortIndex, iBufferHeader);
       
   329     if (error != OMX_ErrorNone)
       
   330         {
       
   331         iParent.iErrorCallbacks.FailWithOmxError(_L("OMX_FreeBuffer()"), error);
       
   332         return;
       
   333         }
       
   334     
       
   335     TInt portInfoIndex = iParent.iPortsUsingBaseProfile.Find(iPortIndex, CBaseProfileHandler::PortIndexMatchComparison);
       
   336     if (iParent.iPortsUsingBaseProfile[portInfoIndex].iWeAreBufferSupplier)
       
   337         {
       
   338         delete[] buffer; 
       
   339         }
       
   340     }
       
   341         
       
   342 TBool CBaseProfileHandler::PortIndexMatchComparison(const TInt* aPortIndex, const TPortInfo& aPortInfo)
       
   343     {
       
   344     if (*aPortIndex == aPortInfo.iPortIndex)
       
   345         {
       
   346         return ETrue;
       
   347         }
       
   348     return EFalse;
       
   349     }
       
   350 
       
   351 TBool CBaseProfileHandler::BufferHeaderMatchComparison(const OMX_BUFFERHEADERTYPE* aBufferHeader, const CBufferHeaderInfo& aBufferHeaderInfo)
       
   352     {
       
   353     if (aBufferHeader == aBufferHeaderInfo.iBufferHeader)
       
   354         {
       
   355         return ETrue;
       
   356         }
       
   357     return EFalse;
       
   358     }
       
   359