os/mm/devsound/devsoundrefplugin/src/swcodecwrapper/mmfswaudioinputpriv.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2003-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 // mmfswaudioinputpriv.h
    15 // 
    16 //
    17 
    18 #ifndef SWAUDIOINPUTPRIV_H
    19 #define SWAUDIOINPUTPRIV_H
    20 
    21 #include <e32def.h>
    22 #include "mmfswaudioinput.h"
    23 #include <d32soundsc.h>
    24 #include <e32base.h>
    25 #include <e32std.h>
    26 
    27 #define KNumRecorders (2) // 2 for double buffering, 4 quadruple etc
    28 
    29 enum TPanicCodes
    30     {
    31     KPanicBadTotalQueueLength=1,    // once constructed, total queue length should be KNumRecorders
    32     KPanicBadTotalQueueLength2,     // during construction or delation, total queue length should be <= KNumRecorders
    33     KPanicBadBusyQueueLength,       // Busy queue should never exceed length of 1
    34     EPanicBusyRecorderNotActive,    // If in the busy queue, should be active
    35     EPanicNonBusyRecorderActive,    // opposite
    36     };
    37 
    38 //Total Number of sample rates
    39 class CChannelAndSampleRateConverter;  // forward dec
    40 
    41 class TRecordSharedChunkBufConfig : public TSharedChunkBufConfigBase
    42 	{
    43 public:
    44 	TInt iBufferOffsetList[3];
    45 	};
    46 
    47 NONSHARABLE_CLASS(CAudioInput) : public CBase, 
    48 					public MAudioInput,
    49 					public MAIParamInterface
    50 	{
    51 	NONSHARABLE_CLASS( CRecorder ) : public CActive
    52 		{
    53 	public:
    54 		CRecorder(CAudioInput& aParent, TInt aIndex);
    55 		~CRecorder();
    56 		void Cancel();
    57 		
    58 		void RecordData();
    59 		TInt Index() const;
    60 		void ReleaseBuffer(TBool aDoAnyway=EFalse);
    61 		TBool IsBusy() const;
    62 		TBool BufferHeld() const;
    63 		TInt Length() const; 
    64 		TInt Offset() const;
    65 		TInt StatusOrOffset() const;
    66 		
    67 		TSglQueLink iLink; // used to form queues. Treat as private.
    68 		
    69 		// from CActive
    70 		void RunL();
    71 		void DoCancel(); 
    72 	private:
    73 		CAudioInput& iParent;
    74 		const TInt iIndex;
    75 		TInt iLength;
    76 		TBool iBufferHeld;
    77 		};
    78 
    79 private:
    80 	class TFormatData
    81 		{
    82 	public:
    83 		inline TFormatData():
    84 			iSampleRate(8000), iRequestedChannels(1) // default
    85 			{
    86 			}
    87 	public:
    88 		TInt iSampleRate;
    89 		TInt iActualRate;
    90 		TInt iRequestedChannels;
    91 		TInt iActualChannels;			
    92 		};
    93 	
    94 public:
    95 	static CAudioInput* NewL(MAudioInputObserver& aObserver);
    96 	~CAudioInput();
    97 	
    98 	// from MAudioInput
    99 	void Release();
   100 	TInt Initialize(const TAudioInputParams& aParams);
   101 	void Close();
   102 	TInt Start();
   103 	void BufferAck();
   104 	TInt Pause();
   105 	TInt Resume();
   106 	TInt Flush();
   107 	void Stop();
   108 	TAny* Interface(TUid aInterfaceUid);
   109 	
   110 	// from MAIParamInterface
   111 	TInt SetGain(TInt aGain);
   112 	TInt GetBufferSizes(TInt& aMinSize, TInt& aMaxSize);
   113 	TInt GetSupportedSampleRates(RArray<TInt>& aSupportedSampleRates);
   114 	
   115 	RSoundSc& RecordSoundDevice();
   116 	void BufferArrives(CRecorder* aRecorder);
   117 	void BufferError(CRecorder* aRecorder, TInt aError);
   118 	TInt SetFormat(const TAudioInputParams& aFormat);
   119 	TInt NegotiateFormat(const TAudioInputParams& aFormat, TFormatData &aFormatData);
   120 	TInt GetSupportedSampleRates(RArray<TInt>& aSupportedSampleRates, RSoundSc& aSoundDevice);
   121 
   122 private:
   123 	CAudioInput(MAudioInputObserver& aObserver);
   124 	void ConstructL();
   125 	void Cancel();
   126 
   127 	static TInt Callback(TAny* aPtr);
   128 	void AsyncCallbackL();
   129     void RequestCallback();
   130 	
   131 	void CancelRecorders();
   132     void CancelPendingRecorders();
   133     void CancelBusyRecorder();
   134 	void InternalStop();
   135 	void InternalFlush();
   136 	void RecordAllIdle();
   137 	
   138 	void UseBuffer(CRecorder* aRecorder);
   139 	void HandleBufferAck();
   140 	
   141 	void CheckFullInvariant();
   142 	void CheckInvariant(TBool aKnownConstructed=ETrue);
   143 	
   144 #ifdef _DEBUG
   145     void CheckActiveRecorders();
   146     void CheckActiveRecorders(TSglQue<CRecorder>& aQueue, TBool aExpected, TInt aPanicCode);
   147 #endif
   148 
   149 	static TInt QLength(TSglQue<CRecorder>& aQueue);
   150 	CRecorder* QPop(TSglQue<CRecorder>& aQueue);
   151 	TBool QPop(CRecorder*& aRecorder, TSglQue<CRecorder>& aQueue);
   152 	
   153 	inline TInt BufferLength() const { return iBufferLength; } // TODO required?
   154 		
   155 private:
   156 	MAudioInputObserver& iObserver;
   157 	
   158 	enum TState
   159 		{
   160 		EStateCreated2,
   161 		EStateInitialized2,
   162 		EStateRecordWait2,
   163 		EStateRecordWaitAck2,
   164 		};
   165 	
   166 	enum TRunningState // when in RecordWait2 or RecordWaitAck2
   167 	    {
   168 	    ERStateRunning,        // recording
   169 	    ERStatePaused,         // paused state
   170 	    ERStateFinishing,      // look to do InputFinished() on next tick
   171 	    ERStateFinished,       // InputFinished() sent
   172 	    ERStateFailed,         // InputError() sent 
   173 	    };
   174 		
   175 	TState iState;
   176 	TRunningState iRState;
   177 	TRecordSharedChunkBufConfig iRecordBufferConfig;
   178 	RSoundSc iRecordSoundDevice;
   179 	RChunk iChunk;
   180 	CRecorder* iRecorders[KNumRecorders];
   181 	CAsyncCallBack* iAsyncCallBack;
   182 	TInt iBufferLength;	// this is the length of buffers we request
   183     CChannelAndSampleRateConverter* iConverter;
   184     TPtrC8 iBufPtr;     // this is usually the descriptor sent to the observer
   185     RBuf8 iConvBuff;    // extra buffer from when we use a converter
   186     
   187     TSglQue<CRecorder> iIdleQueue;        // just sitting there
   188     TSglQue<CRecorder> iRecordingQueue;   // record operation outstanding
   189     TSglQue<CRecorder> iPendingQueue;     // buffer has been recorded, waiting to be processed
   190     TSglQue<CRecorder> iBusyQueue;        // mid InputAvailable()/BufferAck() cycle. Length <= 1
   191 	};
   192 
   193 inline TBool CAudioInput::QPop(CRecorder*& aRecorder, TSglQue<CRecorder>& aQueue)
   194 // overload of QPop(), since "while (recorder = QPop(...))" etc gives warnings
   195 // This allows "while (QPop(recorder, ...))" instead
   196 	{
   197 	CRecorder* recorder = QPop(aQueue);
   198 	if (recorder)
   199 		{
   200 		aRecorder = recorder;
   201 		return ETrue;
   202 		}
   203 	return EFalse;
   204 	}
   205 
   206 #endif // SWAUDIOINPUTPRIV_H
   207