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