omxilcomp/omxilaudioemulator/pcmcapturer/src/omxilmicsourceprocessingfunction.h
changeset 0 58be5850fb6c
equal deleted inserted replaced
-1:000000000000 0:58be5850fb6c
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 /**
       
    19    @file
       
    20    @internalComponent
       
    21 */
       
    22 
       
    23 #ifndef OMXILMICSOURCEPROCESSINGFUNCTION_H
       
    24 #define OMXILMICSOURCEPROCESSINGFUNCTION_H
       
    25 
       
    26 #include <d32soundsc.h>
       
    27 #include <openmax/il/khronos/v1_x/OMX_Component.h>
       
    28 #include <openmax/il/common/omxilprocessingfunction.h>
       
    29 #include <e32msgqueue.h>
       
    30 
       
    31 /**
       
    32     Mic Source PF Panic category
       
    33 */
       
    34 _LIT(KOmxILMicSourcePFPanicCategory, "OmxILPcmCapturerProcFunctionPanic");
       
    35 
       
    36 // Forward declarations
       
    37 class MOmxILClockComponentCmdsIf;
       
    38 
       
    39 const TInt KMaxNumberOfSharedBuffers = 6;
       
    40 
       
    41 NONSHARABLE_CLASS(COmxILMicSourceProcessingFunction) :
       
    42 	public COmxILProcessingFunction
       
    43 	{
       
    44 
       
    45 public:
       
    46 	static COmxILMicSourceProcessingFunction* NewL(MOmxILCallbackNotificationIf& aCallbacks,
       
    47 												   MOmxILClockComponentCmdsIf& aClientClockPort);
       
    48 
       
    49 	~COmxILMicSourceProcessingFunction();
       
    50 
       
    51 	OMX_ERRORTYPE StateTransitionIndication(TStateIndex aNewState);
       
    52 	OMX_ERRORTYPE BufferFlushingIndication(TUint32 aPortIndex, OMX_DIRTYPE aDirection);
       
    53 	OMX_ERRORTYPE ParamIndication(OMX_INDEXTYPE aParamIndex, const TAny* apComponentParameterStructure);
       
    54 	OMX_ERRORTYPE ConfigIndication(OMX_INDEXTYPE aConfigIndex, const TAny* apComponentConfigStructure);
       
    55 
       
    56 	OMX_ERRORTYPE BufferIndication(OMX_BUFFERHEADERTYPE* apBufferHeader, OMX_DIRTYPE aDirection);
       
    57 	OMX_ERRORTYPE MediaTimeIndication(const OMX_TIME_MEDIATIMETYPE& aMediaTime);
       
    58 
       
    59 	OMX_BOOL BufferRemovalIndication(OMX_BUFFERHEADERTYPE* apBufferHeader, OMX_DIRTYPE aDirection);
       
    60 
       
    61 	OMX_ERRORTYPE DoBufferAllocation(OMX_U32 aSizeBytes, OMX_U8*& apPortSpecificBuffer, OMX_PARAM_PORTDEFINITIONTYPE& aPortDefinition);
       
    62 	void DoBufferDeallocation(OMX_PTR apPortSpecificBuffer, OMX_PTR apPortPrivate);
       
    63 	OMX_ERRORTYPE DoBufferWrapping(OMX_U32 aSizeBytes, OMX_U8* apBuffer, OMX_PTR& apPortPrivate, OMX_PARAM_PORTDEFINITIONTYPE& aPortDefinition);
       
    64 	void DoBufferUnwrapping(OMX_PTR apPortSpecificBuffer, OMX_PTR apPortPrivate);
       
    65 
       
    66 	void FillParamPCMModeType(OMX_AUDIO_PARAM_PCMMODETYPE& aPcmModeType) const;
       
    67 	TInt GetVolume() const;
       
    68 	
       
    69 	OMX_TICKS CalculateTimestamp(TUint64 aBytesTransSinceStart);	
       
    70     void ReleasePendingBufferIfExists();	
       
    71     
       
    72 private:
       
    73 	COmxILMicSourceProcessingFunction(MOmxILCallbackNotificationIf& aCallbacks,
       
    74 									  MOmxILClockComponentCmdsIf& aClientClockPort);
       
    75 	void ConstructL();
       
    76 
       
    77 	void FlushBufferList(RPointerArray<OMX_BUFFERHEADERTYPE>& aBufferList);
       
    78 	OMX_ERRORTYPE PostVolumeChangeEvent();
       
    79 	OMX_ERRORTYPE PostAudioFormatChangeEvent();
       
    80 
       
    81 	void ProcessMediaTimeUpdate(const OMX_TIME_MEDIATIMETYPE& aMediaTime);
       
    82 	void StartRecording();
       
    83 
       
    84 	static OMX_U32 ConvertEnumToSampleRate(TSoundRate aEnum);
       
    85 	static TSoundRate ConvertEnumToSampleRate(OMX_U32 aSampleRate);
       
    86 	static OMX_U32 ConvertEnumToBitsPerSample(TSoundEncoding aEnum);
       
    87 
       
    88 	void WaitForTransitionToPauseToFinish();
       
    89 	void TransitionToPauseFinished();	
       
    90 private:
       
    91 	OMX_STATETYPE iState;
       
    92 	RSoundSc iMicSource;
       
    93 	
       
    94 	//We have to start recording when:
       
    95 	//-We receive a call to move to executing state and clock port is disabled
       
    96 	//-We receive a call to move to executing state and the clock is running or vice versa
       
    97 	//As a result we must call RecordData() on the driver but may not have an OpenMAX IL
       
    98 	//buffer to fill.  On completion of this first RecordData() call if we still do not 
       
    99 	//have an OMX buffer to fill then we store the details of the buffer within the shared
       
   100 	//chunk in these member variables.  There is no need to queue a second RecordData(),
       
   101 	//the driver will continue recording.
       
   102 	TInt iInitialPendingBufferOffset;
       
   103 	TInt iInitialPendingBufferLength;
       
   104 	
       
   105 	static const OMX_U32 KSampleRates[];
       
   106 
       
   107 	class TSharedChunkBufConfig : public TSharedChunkBufConfigBase
       
   108 		{
       
   109 	public:
       
   110 		TInt iBufferOffsetList[KMaxNumberOfSharedBuffers];
       
   111 		};
       
   112 
       
   113 	class TMicrophoneSettings
       
   114 		{
       
   115 	public:
       
   116 		//Settings in RSoundSc
       
   117 		TInt iVolume;
       
   118 		TBool iMute;
       
   119 		TCurrentSoundFormatV02 iCurSoundFormat;
       
   120 		TSoundFormatsSupportedV02 iSupportedSoundFormat;
       
   121 		} iMicSourceSettings;
       
   122 
       
   123 	class CBufferQueue : public CActive
       
   124 		{
       
   125 	public:
       
   126 		static CBufferQueue* NewL(COmxILMicSourceProcessingFunction& aParent);
       
   127 		~CBufferQueue();
       
   128 
       
   129 		// from CActive
       
   130 		void RunL();
       
   131 		void DoCancel();
       
   132 
       
   133 		void RecordData();
       
   134 		void Cleanup();
       
   135 
       
   136         void Pause();
       
   137         void Stop();
       
   138         void Resume();
       
   139         
       
   140         OMX_ERRORTYPE AllocateBuffer(OMX_U8*& aPortSpecificBuffer, const OMX_PARAM_PORTDEFINITIONTYPE& aPortDefinition);
       
   141 		OMX_ERRORTYPE UseBuffer(OMX_U8* apBuffer, const OMX_PARAM_PORTDEFINITIONTYPE& aPortDefinition);
       
   142 		void RemoveBuffer(OMX_PTR apPortSpecificBuffer);
       
   143 		void FreeBuffer(OMX_PTR aPortSpecificBuffer);
       
   144 
       
   145 		OMX_ERRORTYPE QueueBuffer(OMX_BUFFERHEADERTYPE* apBufferHeader);
       
   146 		OMX_BOOL RemoveBuffer(OMX_BUFFERHEADERTYPE* apBufferHeader);
       
   147 		void FlushBuffers(OMX_DIRTYPE aDirection);
       
   148 		void FillFirstOpenMaxIlBuffer(TInt aChunkOffset, TInt aLengthOfData);
       
   149 
       
   150 	private:
       
   151 		CBufferQueue(COmxILMicSourceProcessingFunction& aParent);
       
   152 		void ConstructL();
       
   153 		OMX_ERRORTYPE CreateBuffer(const OMX_PARAM_PORTDEFINITIONTYPE& aPortDefinition);
       
   154 		void RemoveBufferFromQueue(OMX_PTR apPortSpecificBuffer);
       
   155 		void FillOpenMaxIlBuffer(OMX_BUFFERHEADERTYPE* aBufferHeader, TInt aChunkOffset, TInt aLengthOfData);
       
   156 		void EmitEOSBuffer();
       
   157         void FlushDriver();
       
   158 
       
   159 	private:
       
   160 		COmxILMicSourceProcessingFunction& iParent;
       
   161 
       
   162 		RChunk iSharedChunk;
       
   163 		TSharedChunkBufConfig iSharedChuckConfig;
       
   164 		TInt iSharedBufferIndex;
       
   165 		TInt iRecordedLength;
       
   166 
       
   167 		TBool iIsSupplier;
       
   168 		RFastLock iMutex;
       
   169 		RPointerArray<OMX_BUFFERHEADERTYPE> iBuffersToFill;
       
   170 		} *iBufferQueue;
       
   171 
       
   172 
       
   173 	class CCommandsQueue : public CActive
       
   174 		{
       
   175 	public:
       
   176 		static CCommandsQueue* NewL(COmxILMicSourceProcessingFunction& aParent);
       
   177 		~CCommandsQueue();
       
   178 
       
   179 		// from CActive
       
   180 		void RunL();
       
   181 		void DoCancel();
       
   182 
       
   183 		TInt Pause();
       
   184 		TInt Stop();
       
   185         TInt StopAndReset();		
       
   186 		TInt RecordData();
       
   187 		TInt StartRecording();
       
   188 		TInt MediaTimeIndication(const OMX_TIME_MEDIATIMETYPE& aMediaTime);
       
   189 		TInt CleanupBufferQueue();
       
   190 		TInt CloseDevice();
       
   191 		TInt Resume();
       
   192 		
       
   193 		enum TMessageType
       
   194 			{
       
   195 			EMsgPause,
       
   196 			EMsgStop,
       
   197             EMsgStopAndReset,			
       
   198 			EMsgRecord,
       
   199 			EMsgStartRecording,
       
   200 			EMsgMediaTime,
       
   201 			EMsgCleanupBuffer,
       
   202 			EMsgCloseDevice,
       
   203 			EResume
       
   204 			};
       
   205 
       
   206 		class TProcMessage
       
   207 			{
       
   208 		public:
       
   209 			TMessageType iType;
       
   210 			OMX_TIME_MEDIATIMETYPE iMediaTime;
       
   211 			};
       
   212 
       
   213 		RMsgQueue<TProcMessage> iMsgQueue;
       
   214 
       
   215 	private:
       
   216 		CCommandsQueue(COmxILMicSourceProcessingFunction& aParent);
       
   217 		void ConstructL();
       
   218 
       
   219 	private:
       
   220 		static const TInt KMaxMsgQueueEntries = 10;
       
   221 		COmxILMicSourceProcessingFunction& iParent;
       
   222 		} *iCommandsQueue;
       
   223 
       
   224 	MOmxILClockComponentCmdsIf* ipClientClockPort;
       
   225 	OMX_TICKS iMediaStartTime;
       
   226 	TUint iTotalBytesRecorded;
       
   227 	
       
   228 	TBool iStartedRecording;     //To ensure we don't initialise recording twice
       
   229 	TThreadId iOwnerThreadId;
       
   230 	CActiveSchedulerWait* iTransitionToPauseWait;
       
   231 	RSemaphore iTransitionToPauseWaitSemaphore;
       
   232 	};
       
   233 
       
   234 #endif // OMXILMICSOURCEPROCESSINGFUNCTION_H