devsound/a3fdevsound/src/devsoundadaptor/cdevaudiocontrol.h
changeset 0 40261b775718
child 12 b74061f7f3d2
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 /*
       
     2 * Copyright (c) 2006-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 
       
    21 #ifndef CDEVAUDIOCONTROL_H
       
    22 #define CDEVAUDIOCONTROL_H
       
    23 
       
    24 #include <e32base.h>
       
    25 
       
    26 #include "cdevaudio.h"
       
    27 
       
    28 #include <a3f/maudiostreamobserver.h>
       
    29 #include <a3f/maudiocodecobserver.h>
       
    30 #include <a3f/maudiogaincontrolobserver.h>
       
    31 #include <a3f/msynccisupport.h>
       
    32 #include <a3f/a3f_trace_utils.h>
       
    33 
       
    34 
       
    35 class MAudioCodec;
       
    36 class MAudioGainControl;
       
    37 
       
    38 /**
       
    39 *  Panic category and codes that DevSoundAdaptor raises on the client
       
    40 */
       
    41 enum TDevSoundAdaptorPanicCode
       
    42 	{
       
    43 	ECommitFailedDuringStop = 1, 
       
    44 	ECommitFailedDuringPause,
       
    45 	EStreamBeingDemotedToEIdle,
       
    46 	EAudioCodecIsNull,
       
    47 	EStreamMismatch,
       
    48 	EBufferMismatch
       
    49 	};
       
    50 
       
    51 
       
    52 /**
       
    53  * CDevAudioControl.
       
    54  *
       
    55  * ?description
       
    56  *
       
    57  * @lib mmfdevsoundadaptation.lib
       
    58  * @since
       
    59  */
       
    60 NONSHARABLE_CLASS(CDevAudioControl) :	public CBase,
       
    61 										public MAudioStreamObserver,
       
    62 										public MAudioGainControlObserver,
       
    63 										public MAudioContextObserver,
       
    64 										public MCustomInterfaceSupportObserver,
       
    65 										public MAudioCodecObserver
       
    66 	{
       
    67 
       
    68 public:
       
    69 
       
    70 	enum TStereoCh
       
    71 		{
       
    72 		ELeftCh=0,
       
    73 		ERightCh=1
       
    74 		};
       
    75 
       
    76 	~CDevAudioControl();
       
    77 
       
    78 	/**
       
    79 	* Initialize wanted state control components
       
    80 	* @since
       
    81 	* @return error code
       
    82 	*/
       
    83 	virtual TInt Initialize(TUid aFormat);
       
    84 
       
    85 	/**
       
    86 	* Uninitialize wanted state control components
       
    87 	* @since
       
    88 	* @return error code
       
    89 	*/
       
    90 	virtual TInt Uninitialize();
       
    91 
       
    92 	/**
       
    93 	* Remove any processing unit
       
    94 	* @since
       
    95 	* @return error code
       
    96 	*/
       
    97 	virtual TInt RemoveProcessingUnits();
       
    98 
       
    99 	/**
       
   100 	* Returns the supported Audio settings ie. encoding, sample rates,
       
   101 	* mono/stereo operation, buffer size etc..
       
   102 	* @since
       
   103 	* @param aCaps on return, contains supported capabilities
       
   104 	* @return error code
       
   105 	*/
       
   106 	TInt GetCapabilities(TMMFCapabilities& aCap);
       
   107 
       
   108 	/**
       
   109 	* Returns the current device configuration.
       
   110 	* @since
       
   111 	* @param aConfig on return, contains device settings.
       
   112 	* @return error code
       
   113 	*/
       
   114 	TInt GetConfig(TMMFCapabilities& aConfig);
       
   115 
       
   116 	/**
       
   117 	* Configure CMMFDevSound object with the settings in aConfig. Use this
       
   118 	* to set sampling rate, encoding and mono/stereo.
       
   119 	* Leaves on failure.
       
   120 	* @since
       
   121 	* @param const TMMFCapabilities& aConfig The attribute values to which
       
   122 	*        CMMFDevSound object will be configured to.
       
   123 	* @return error code
       
   124 	*/
       
   125 	TInt SetConfig(const TMMFCapabilities& aCaps);
       
   126 
       
   127 	/**
       
   128 	* Initializes and starts the wanted operation (Play, Record, TonePlay).
       
   129 	* @since
       
   130 	* @return error code
       
   131 	*/
       
   132 	virtual TInt ProcessInit();
       
   133 
       
   134 	/**
       
   135 	* Processes the data (PlayData, RecordData).
       
   136 	* @since
       
   137 	* @return void
       
   138 	*/
       
   139 	virtual void ProcessData();
       
   140 
       
   141 	/**
       
   142 	* Stops the ongoing operation (Play, Record, TonePlay).
       
   143 	* @since
       
   144 	* @return void
       
   145 	*/
       
   146 	virtual TInt Stop();
       
   147 
       
   148 	/**
       
   149 	* Temporarily Stops the ongoing operation (Play, Record, TonePlay).
       
   150 	* @since
       
   151 	* @return void
       
   152 	*/
       
   153 	virtual TInt Pause();
       
   154 
       
   155 	/**
       
   156 	* Returns the samples played/recorded so far
       
   157 	* @since
       
   158 	* @return TInt Returns the samples played/recorded.
       
   159 	*/
       
   160 	virtual TInt GetSamples();
       
   161 
       
   162 	/**
       
   163 	* Retrieves a custom interface to the device.
       
   164 	* @since
       
   165 	* @param TUid aInterfaceId The interface UID, defined with the custom
       
   166 	*        interface.
       
   167 	* @return TAny* A pointer to the interface implementation, or NULL if
       
   168 	*        the device does not implement the interface requested. The
       
   169 	*        return value must be cast to the correct type by the user.
       
   170 	*/
       
   171 	virtual TAny* CustomInterface(TUid aInterfaceId);
       
   172 
       
   173 	/**
       
   174 	* Apply gains, balance. Scale gain to underlying (a3f) max gain.
       
   175 	* Assumed to be in active state. When called for SetVolume() etc call on higher-level
       
   176 	* aCommit should be ETrue, and will call Commit() if derived result is KErrNone.
       
   177 	* @param aDevSoundGain the DevSound gain to be applied
       
   178 	* @param aDevSoundMaxGain the cached A3F Max Gain to be used as boundary
       
   179 	* @param aBalance the balance value to be applied
       
   180 	* @param const TTimeIntervalMicroSeconds &aRampDuration The period over
       
   181 	*        which the volume is to rise. A zero value causes the
       
   182 	*        sample to be played at the normal level for the full duration
       
   183 	*        of the playback. A value, which is longer than the duration of
       
   184 	*        the overall clip means that the sample never reaches its normal
       
   185 	*        volume level.
       
   186 	* @param aBecomingActive indicates if DevSoundAdaptor is becoming active
       
   187 	*        which is needed to avoid if a commit here could clash with the one that is going to activate the stream
       
   188 
       
   189 	* @return error code
       
   190 	*/
       
   191 	TInt SetGains(TInt aDevSoundGain, TInt aDevSoundMaxGain, TInt aBalance[2], const TTimeIntervalMicroSeconds& aRampDuration, TBool aBecomingActive);
       
   192 
       
   193 	/**
       
   194 	* Maps "legacy" volume/gain values to CAP channel array
       
   195 	* @since
       
   196 	*/
       
   197 	void MapGains();
       
   198 
       
   199 	/**
       
   200 	* Destroy logical chain
       
   201 	* @since
       
   202 	* @
       
   203 	*/
       
   204 	TBool DestroyChain();
       
   205 
       
   206 	// From base class MAudioStreamObserver
       
   207 
       
   208 	/**
       
   209 	* Handles audio stream state change event.
       
   210 	*/
       
   211 	virtual void StateEvent(MAudioStream& aStream, TInt aReason, TAudioState aNewState);
       
   212 
       
   213 	/**
       
   214 	* Notifies that adding of processing unit to the stream has been completed
       
   215 	* successfully or otherwise.
       
   216 	*/
       
   217 	virtual void AddProcessingUnitComplete(MAudioStream& aStream, MAudioProcessingUnit* aInstance, TInt aError);
       
   218 
       
   219 	/**
       
   220 	* Notifies that removing of processing unit from the stream has been completed
       
   221 	* successfully or otherwise.
       
   222 	*/
       
   223 	virtual void RemoveProcessingUnitComplete(MAudioStream& aStream, MAudioProcessingUnit* aInstance, TInt aError);
       
   224 
       
   225 	/*
       
   226 	Call-back indicating that is the last buffer has been processed by the sink.
       
   227 	*/
       
   228 	virtual void ProcessingFinished (MAudioStream & aStream);
       
   229 
       
   230 	/**
       
   231 	* Signals completion of a Flush() operation.
       
   232 	*/
       
   233 	virtual void FlushComplete (MAudioStream& aStream, TInt aError);
       
   234 
       
   235 	// From base class MAudioGainControlObserver
       
   236 
       
   237 	/**
       
   238 	* Notifies the observer that the max ramp time supported by the stream, has changed.
       
   239 	*
       
   240 	* @since
       
   241 	* @param aStream a reference to the stream whose max ramp time changed.
       
   242 	*/
       
   243 	virtual void MaxRampTimeChanged(MAudioGainControl& aGain);
       
   244 
       
   245 	/**
       
   246 	* Notifies the observer that the maximum gain value supported by the stream has changed.
       
   247 	*
       
   248 	* @since
       
   249 	* @param aStream a reference to the stream whose supported con
       
   250 	*/
       
   251 	virtual void MaxGainChanged(MAudioGainControl& aGain);
       
   252 
       
   253 	/**
       
   254 	* Notifies the observer that the stream gain has changes, due to request from the client
       
   255 	* or otherwise.
       
   256 	*
       
   257 	* If gain change by the client cannot be fulfilled, a gain change with an error code
       
   258 	* other than KErrNone will be issued.
       
   259 	*
       
   260 	* @since
       
   261 	* @param aStream a reference to the stream whose gain has changed.
       
   262 	* @param aStream an error code. KErrNone if the gain change was requested by the client
       
   263 	*        and was completed successfully.
       
   264 	*/
       
   265 	virtual void GainChanged(MAudioGainControl& aGain, TInt aError);
       
   266 
       
   267 	/**
       
   268 	* Saves tonedata for later use
       
   269 	* This includes information about tone type, data, length, etc.
       
   270 	*
       
   271 	* @since
       
   272 	* @param aToneData
       
   273 	* @return error code
       
   274 	*/
       
   275 	virtual TInt SetToneData(TToneData& aToneData);
       
   276 
       
   277 	// From base class MAudioContextObserver
       
   278 	/**
       
   279 	* Callback to context observer to show progression through Commit() and pre-emption cycles
       
   280 	*
       
   281 	* @param aEvent  a Uid giving the specific event.
       
   282 	* @param aError  an error code. KErrNone if successful, otherwise one of the system wide error codes.
       
   283 	*/
       
   284 	virtual void ContextEvent(TUid aEvent, TInt aError);
       
   285 
       
   286 	// from MCustomInterfaceSupportObserver
       
   287 	virtual void CustomInterfaceRemoval(TUid, TAny* aPtr);
       
   288 
       
   289 	//From MAudioCodecObserver
       
   290 
       
   291 	virtual void SampleRateSet(TInt aError);
       
   292 
       
   293 	virtual void ModeSet(TInt aError);
       
   294 
       
   295 	virtual void GetSupportedSampleRatesComplete (TInt aError);
       
   296 
       
   297 	virtual void GetSupportedModesComplete (TInt aError);
       
   298 
       
   299 	/**
       
   300 	* Called when a ProcessingFinished callback is received
       
   301 	*
       
   302 	* @since
       
   303 	* @param TBool& aAyncCompletion
       
   304 	* @return an error code KErrNone if successful
       
   305 	*/
       
   306 	virtual TInt ProcessingFinishedReceived(TBool& aAyncCompletion);
       
   307 
       
   308 	virtual TInt ProcessingError(TBool& aAyncCompletion);
       
   309 	
       
   310 	/**
       
   311 	* EmptyBuffers using A3F::Flush
       
   312 	*
       
   313 	* @since
       
   314 	* @return an error code. KErrNone if successful
       
   315 	*						KErrNotReady if not playing or paused
       
   316 	*						KErrNotSupported if the operation is not supported
       
   317 	*/
       
   318 	virtual TInt RequestEmptyBuffers();
       
   319 
       
   320 	/**
       
   321 	* Gets the current play time from the audio renderer
       
   322 	* @since 
       
   323 	* @param TTimeIntervalMicroSeconds& aTime On return contains the current play time
       
   324 	* @return an error code KErrNone if successful
       
   325 	*/
       
   326 	virtual TInt GetTimePlayed(TTimeIntervalMicroSeconds& aTime);
       
   327 
       
   328 	/**
       
   329 	* Resume the operation (Play, Record, TonePlay) temporarily paused .
       
   330 	* @since
       
   331 	* @return TInt KErrNone if succesful
       
   332 	*              KErrNotSupported if the operation is not supported by this implementation
       
   333 	*/
       
   334 	virtual TInt Resume();
       
   335 
       
   336 	/*
       
   337 	Used to send a stop call when there is a error in the buffer
       
   338 	*/
       
   339 	virtual void BufferErrorEvent();
       
   340 	
       
   341 protected:
       
   342 
       
   343 	CDevAudioControl();
       
   344 
       
   345 	void ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver);
       
   346 
       
   347 	// Ensure iAudioCodecIf is setup properly
       
   348 	TInt CacheAudioCodecIf();
       
   349 
       
   350 	/**
       
   351 	* Returns to initialized state wanted control components
       
   352 	* @since 
       
   353 	* @return error code
       
   354 	*/
       
   355 	TInt Unload();
       
   356 
       
   357 
       
   358 	/**
       
   359 	* Panic the thread
       
   360 	* @since 
       
   361 	* @param panic code
       
   362 	*/
       
   363 	void Panic(TDevSoundAdaptorPanicCode aCode);
       
   364 	
       
   365 private:
       
   366 	TInt ResolveMode(TUint aMode, TUid& aModeValue);
       
   367 	TInt ResolveSampleRate(TInt aSampleRate, TInt& aSampleRateValue);
       
   368 	TUint GetModes(const RArray<TUid>& aMode);
       
   369 	TUint GetMode(TUid aMode);
       
   370 	TUint GetSampleRates(const RArray<TInt>& aSampleRates);
       
   371 	TUint GetSampleRate(TInt aSampleRates);
       
   372 	void CompleteMessageCap(TInt aError);
       
   373 
       
   374 /**
       
   375  * Member data is protected for subclass access
       
   376  */
       
   377 protected: // data
       
   378 
       
   379 	// Not own
       
   380 	CDevAudio *iDevAudio;
       
   381 
       
   382 	/**
       
   383 	* Pointer to audio codec
       
   384 	* Not own.
       
   385 	*/
       
   386 	MAudioCodec* iAudioCodecIf;
       
   387 
       
   388 	/**
       
   389 	* Pointer to audio gain control
       
   390 	* Not own.
       
   391 	*/
       
   392 	MAudioGainControl* iGainControl;
       
   393 
       
   394 	/**
       
   395 	* Observer for callbacks to DevSound Framework
       
   396 	* Not own.
       
   397 	*/
       
   398 	MDevSoundAdaptationObserver *iAdaptationObserver;
       
   399 
       
   400 
       
   401 	/*
       
   402 	* TODO: Review if this for buffer exchange
       
   403 	*/
       
   404 	CMMFBuffer* iBuffer;
       
   405 
       
   406 	/**
       
   407 	* Local cache of volume stuff
       
   408 	*/
       
   409 	TInt iLegacyGain;
       
   410 	TInt iLegacyLeft;
       
   411 	TInt iLegacyRight;
       
   412 
       
   413 	TInt 	iError;
       
   414 	TBool 	iGainUpdateNeeded;
       
   415 	TBool	iStateEventReceived;
       
   416 	TInt 	iStateEventError;
       
   417 	TInt	iCallbackFromAdaptor;
       
   418 	TInt 	iProcessingUnitError;
       
   419 
       
   420 	TInt 	iErrorCondition;
       
   421 	
       
   422 	TBool	iObserverRegistered;
       
   423 	
       
   424 	//Indicates whether the AsyncOperationComplete callback needs to be made during Preemption
       
   425 	TBool iIgnoreAsyncOpComplete;
       
   426 
       
   427 	TBool	iPauseResumeSequenceDueToEmptyBuffers;
       
   428 
       
   429 private:
       
   430 
       
   431 	RArray<TAudioChannelGain>	iChannelGains;
       
   432 
       
   433 	RArray<TUid>				iSupportedModes;
       
   434 	RArray<TInt>				iSupportedRates;
       
   435 	TInt						iDesiredSampleRate;
       
   436 	TUid						iDesiredMode;
       
   437 	TInt						iCurrentSampleRate;
       
   438 	TUid						iCurrentMode;
       
   439 	TInt						iOutstandingCallbacks;
       
   440 
       
   441 	};
       
   442 
       
   443 #endif // CDEVAUDIOCONTROL_H