os/mm/devsound/sounddevbt/PlatSec/src/Server/AudioServer/MmfBtDevSoundSessionBody.inl
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 #include <mmfbtswcodecwrapper.h> //for SwCodecWrapper CustomInterfaces
    17 #include <mmfbtswcodecwrappercustominterfacesuids.hrh>
    18 #include <mmf/plugin/mmfbthwdeviceimplementationuids.hrh>
    19 // Test only
    20 #include <bttypes.h>
    21 #include <centralrepository.h>	// for BT device address retrieval
    22 #include "MmfBtRoutingPrioritySettings.h" //for ESpeaker, EA2DPBluetoothHeadset etc
    23 
    24 /*
    25  *	[TODO] this function is now too long to be inline
    26  *	Initializes DevSound object for the mode aMode for processing audio data
    27  *	with hardware device aHWDev.
    28  *
    29  *	On completion of Initialization, the observer will be notified via call back
    30  *	InitializeComplete().
    31  *
    32  *	Leaves on failure.
    33  *  
    34  *	@param	"MDevSoundObserver& aDevSoundObserver"
    35  *			A reference to DevSound Observer instance.
    36  *
    37  *	@param	"TUid aHWDev"
    38  *			CMMFHwDevice implementation identifier.
    39  *
    40  *	@param	"TMMFState aMode"
    41  *			Mode for which this object will be used.
    42  *
    43  */
    44 inline void CMMFDevSoundSvrImp::InitializeL(MDevSoundObserver& aDevSoundObserver, TUid aHWDev, TMMFState aMode)
    45 	{
    46 	TInt initError = KErrNone;
    47 	iDevSoundObserver = &aDevSoundObserver;
    48 
    49 	if (aMode == EMMFStateIdle)
    50 		{
    51 		User::Leave(KErrNotSupported);
    52 		}
    53 	iMode = aMode;
    54 
    55 	// Given the mode, update the iDevInfo with the audio's route
    56 	if ((iMode == EMMFStateRecording)||(iMode == EMMFStatePlayingRecording))
    57 		{//DEF037912 incase the recording capabilities differ from play
    58 		User::LeaveIfError(InitializeFormat(iRecordFormatsSupported, iRecordFormat));
    59 		iDevInfo.iInStream.iConnection.iType = EConnectionGpp;
    60 		iDevInfo.iInStream.iConnection.iId = KDeviceUidMic.iUid;
    61 		}
    62 	if ((iMode == EMMFStatePlaying)||(iMode == EMMFStateTonePlaying)||(iMode == EMMFStatePlayingRecording))
    63 		{
    64 		iDevInfo.iOutStream.iConnection.iType = EConnectionGpp;
    65 		// If the bit on the priority settings is set indicating
    66 		// we are routing to the by headset then the HwDevice device id
    67 		// needs setting for the bluetooth headset
    68 		if (iAudioPolicyPrioritySettings.iPref & EA2DPBluetoothHeadset)
    69 			{
    70 			iDevInfo.iOutStream.iConnection.iId = KDeviceUidA2dpHeadset.iUid;
    71 			CRepository* centralRepository = NULL;
    72 			centralRepository = CRepository::NewL(KUidBTHeadsetRepository);
    73 			CleanupStack::PushL(centralRepository);
    74 			TInt64 btHeadsetAddress = 0;
    75 			TBuf<16> btHeadsetAddressString;
    76 			User::LeaveIfError(centralRepository->Get(0x01, btHeadsetAddressString));
    77 			TLex stringConverter(btHeadsetAddressString.Ptr());
    78 			User::LeaveIfError(stringConverter.Val(btHeadsetAddress,EHex));
    79 			TBTDevAddr bTDevAddr(btHeadsetAddress);
    80 			iDevInfo.iOutStream.iDeviceName = bTDevAddr.Des();
    81 			CleanupStack::Pop(centralRepository);
    82 			delete centralRepository;
    83 			}
    84 		else
    85 			{
    86 			iDevInfo.iOutStream.iConnection.iId = KDeviceUidSpeaker.iUid;
    87 			}	
    88 		}
    89 
    90 	iDevSoundObserver = &aDevSoundObserver;
    91 	iHwDeviceID.iUid = aHWDev.iUid;
    92     if(iCMMFHwDevice)
    93 		{
    94 		delete iCMMFHwDevice;
    95 		iHwDeviceBuffer = NULL; // buffer is deleted by HwDevice delete
    96 		iPlayCustomInterface = NULL; //custom interfaces are also invalid
    97 		iRecordCustomInterface = NULL;
    98 		}
    99     
   100     iCMMFHwDevice = NULL;
   101 
   102 	// Load the HwDevice implementation from ECom
   103 	TRAP(initError, iCMMFHwDevice = CMMFHwDevice2::NewL(aHWDev));
   104 
   105 	if (initError == KErrNone)
   106 		{	
   107 		iDevInfo.iHwDeviceObserver = this;
   108 	
   109 		iCMMFHwDevice->Init(iDevInfo, iRSDHandler->iStatus);
   110 		iRSDHandler->Start();
   111 		
   112 		TUid playCustomInterfaceUid;
   113 		playCustomInterfaceUid.iUid = KMmfPlaySettingsCustomInterface;
   114 		TUid recordCustomInterfaceUid;
   115 		recordCustomInterfaceUid.iUid = KMmfRecordSettingsCustomInterface;
   116 		iPlayCustomInterface = 
   117 			(MPlayCustomInterface*)iCMMFHwDevice->CustomInterface(playCustomInterfaceUid);
   118 		if (!iPlayCustomInterface) 
   119 			{//DEF40443 need to check custom interface has been created
   120 			initError = KErrNoMemory;//it won't if there is no memory
   121 			}
   122 		else
   123 			{
   124 			iRecordCustomInterface = 
   125 				(MRecordCustomInterface*)iCMMFHwDevice->CustomInterface(recordCustomInterfaceUid);
   126 			if (!iRecordCustomInterface)
   127 				{
   128 				initError = KErrNoMemory;
   129 				}
   130 			}
   131 		} 
   132 
   133 	if (initError)
   134 		{
   135 		iDevSoundObserver->InitializeComplete(initError);  
   136 		User::Leave(initError);
   137 		}
   138 	}
   139 
   140 /*
   141  *
   142  *	Initializes DevSound object for the mode aMode for processing audio data
   143  *	using an array of Hardware devices identified by aHWDevArray identifier
   144  *	array. The hardware devices are chained together with data flow starting
   145  *	with first array element.
   146  *
   147  *	On completion of Initialization, the observer will be notified via call back
   148  *	InitializeComplete().
   149  *
   150  *	Leaves on failure.
   151  *  
   152  *	@param	"MDevSoundObserver& aDevSoundObserver"
   153  *			A reference to DevSound Observer instance.
   154  *
   155  *	@param	"CArrayPtr<TUid> aHWDevArray"
   156  *			Array of CMMFHwDevice implementation identifiers.
   157  *
   158  *	@param	"TMMFState aMode"
   159  *			Mode for which this object will be used.
   160  *
   161  */
   162 inline void CMMFDevSoundSvrImp::InitializeL(MDevSoundObserver& /*aDevSoundObserver*/, CArrayPtr<TUid> /*aHWDevArray*/, TMMFState /*aMode*/)
   163 	{
   164 	User::Leave(KErrNotSupported);
   165 	}
   166 
   167 /*
   168  *
   169  *	Initializes DevSound object for the mode aMode for processing audio data
   170  *	with hardware device supporting FourCC aDesiredFourCC.
   171  *
   172  *	On completion of Initialization, the observer will be notified via call back
   173  *	InitializeComplete().
   174  *
   175  *	Leaves on failure.
   176  *  
   177  *	@param	"MDevSoundObserver& aDevSoundObserver"
   178  *			A reference to DevSound Observer instance.
   179  *
   180  *	@param	"TFourCC aDesiredFourCC"
   181  *			CMMFHwDevice implementation FourCC.
   182  *
   183  *	@param	"TMMFState aMode"
   184  *			Mode for which this object will be used.
   185  *
   186  */
   187 inline void CMMFDevSoundSvrImp::InitializeL(MDevSoundObserver& aDevSoundObserver, TFourCC aDesiredFourCC, TMMFState aMode)
   188 	{
   189 	// to get HW Uid from the FourCC
   190 	RImplInfoPtrArray plugInArray;
   191 	TFourCC KPCM16FourCC(' ','P','1','6');
   192 	_LIT(KNullString, "");
   193 
   194 	TUid hwDevicePluginInterface = {KMmfUidBtPluginInterfaceHwDevice};
   195 	TUid implUid = {0};  
   196 
   197 	CleanupResetAndDestroyPushL( plugInArray );
   198 	// Get the implementation UID based on the FourCC and mode.
   199 	if (aMode == EMMFStatePlaying)
   200 		{//destination four CC is pcm16
   201 		iDevSoundUtil->SeekUsingFourCCL(hwDevicePluginInterface, plugInArray, aDesiredFourCC, KPCM16FourCC, KNullString);
   202 		}
   203 	else if (aMode == EMMFStateRecording)
   204 		{//source fourCC is pcm16
   205 		iDevSoundUtil->SeekUsingFourCCL(hwDevicePluginInterface, plugInArray, KPCM16FourCC, aDesiredFourCC, KNullString);
   206 		}
   207 	else User::Leave(KErrNotSupported);//invalid aMode cant set 4CC for tone
   208 
   209 	if(plugInArray.Count() == 0)
   210 		{ // couldn't find Decoder only implementation, try to get Decoder/Encoder
   211 		iDevSoundUtil->SeekUsingFourCCL(hwDevicePluginInterface, plugInArray, aDesiredFourCC, aDesiredFourCC, KNullString);
   212 		if(plugInArray.Count() == 0)
   213 			User::Leave(KErrNotSupported);
   214 		}
   215 	implUid = plugInArray[0]->ImplementationUid(); // Just pick the first in the list
   216 
   217 	// If we made it here, there we have found implementation UID
   218 	InitializeL(aDevSoundObserver, implUid, aMode);
   219 	CleanupStack::PopAndDestroy() ;  //pluginArray
   220 	}
   221 
   222 /*
   223  *
   224  *	Returns the supported Audio settings.
   225  *  
   226  *	@return	"TMMFCapabilities"
   227  *			Device settings.
   228  *
   229  */
   230 inline TMMFCapabilities CMMFDevSoundSvrImp::Capabilities()
   231 	{
   232 	if ((iAudioPolicyPrioritySettings.iPref & EA2DPBluetoothHeadset) && (iPlayCustomInterface))
   233 		{
   234 		//then we need to get the caps from the HwDevice
   235 		//actually we should get the Caps from the HwDevice in any case
   236 		//but am being cautious so as not to cause test regressions
   237 		TTaskConfig hwDeviceCaps = iPlayCustomInterface->Caps();
   238 		iDeviceCapabilities.iRate = hwDeviceCaps.iRate; //the two structures use the same enums so this is ok
   239 		if (hwDeviceCaps.iStereoMode & ETaskMono)
   240 			{
   241 			iDeviceCapabilities.iChannels = EMMFMono;
   242 			}
   243 		if (hwDeviceCaps.iStereoMode & ETaskInterleaved)
   244 			{
   245 			iDeviceCapabilities.iChannels |= EMMFStereo;
   246 			}
   247 		}
   248 	return iDeviceCapabilities;
   249 	}
   250 
   251 /*
   252  *
   253  *	Returns the current audio settings.
   254  *  
   255  *	@return	"TMMFCapabilities"
   256  *			Device settings.
   257  *
   258  */
   259 inline TMMFCapabilities CMMFDevSoundSvrImp::Config() const
   260 	{
   261 	return iDeviceConfig;
   262 	}
   263 
   264 /*
   265  *
   266  *	Returns an integer representing the maximum volume.
   267  *
   268  *	This is the maximum value which can be passed to CMMFDevSoundProxy::SetVolume.
   269  *  
   270  *	@return	"TInt"
   271  *			The maximum volume. This value is platform dependent but is always
   272  *			greater than or equal to one.
   273  *
   274  */
   275 inline TInt CMMFDevSoundSvrImp::MaxVolume()
   276 	{
   277 	return iPlayFormatsSupported().iMaxVolume;
   278 	}
   279 
   280 /*
   281  *
   282  *	Returns an integer representing the current volume.
   283  * 
   284  *	@return	"TInt"
   285  *			The current volume level.
   286  *
   287  */
   288 inline TInt CMMFDevSoundSvrImp::Volume()
   289 	{
   290 	return iVolume;
   291 	}
   292 
   293 /*
   294  *
   295  *	Returns an integer representing the maximum gain.
   296  *
   297  *	This is the maximum value which can be passed to CMMFDevSoundProxy::SetGain.
   298  * 
   299  *	@return	"TInt"
   300  *			The maximum gain. This value is platform dependent but is always
   301  *			greater than or equal to one.
   302  *
   303  */
   304 inline TInt CMMFDevSoundSvrImp::MaxGain()
   305 	{
   306 	return iRecordFormatsSupported().iMaxVolume;//uses iMaxVolume for iMaxGain
   307 	}
   308 
   309 /*
   310  *
   311  *	Returns an integer representing the current gain.
   312  *
   313  *	@return	"TInt"
   314  *			The current gain level.
   315  *
   316  */
   317 inline TInt CMMFDevSoundSvrImp::Gain()
   318 	{
   319 	return iGain;
   320 	}
   321 
   322 /*
   323  *
   324  *	Returns the speaker balance set for playing.
   325  *
   326  *	Leaves on failure.
   327  *
   328  *	@param	"TInt& aLeftPrecentage"
   329  *			On return contains the left speaker volume percentage.
   330  *
   331  *	@param	"TInt& aRightPercentage"
   332  *			On return contains the right speaker volume percentage.
   333  *
   334  */
   335 inline void CMMFDevSoundSvrImp::GetPlayBalanceL(TInt& aLeftPercentage, TInt& aRightPercentage)
   336 	{
   337 	aLeftPercentage = iLeftPlayBalance;
   338 	aRightPercentage = iRightPlayBalance;
   339 	}
   340 
   341 /*
   342  *
   343  *	Returns the microphone gain balance set for recording.
   344  *
   345  *	Leaves on failure.
   346  *
   347  *	@param	"TInt& aLeftPercentage"
   348  *			On return contains the left microphone gain percentage.
   349  *
   350  *	@param	"TInt& aRightPercentage"
   351  *			On return contains the right microphone gain percentage.
   352  *
   353  */
   354 inline void CMMFDevSoundSvrImp::GetRecordBalanceL(TInt& aLeftPercentage, TInt& aRightPercentage)
   355 	{
   356 	aLeftPercentage = iLeftRecordBalance;
   357 	aRightPercentage = iRightRecordBalance;
   358 	}
   359 
   360 /*
   361  *
   362  *	Contine the process of recording. Once the buffer is filled with recorded
   363  *	data, the Observer gets reference to buffer along with callback
   364  *	BufferToBeEmptied(). After processing the buffer (copying over to a
   365  *	different buffer or writing to file) the client should call this
   366  *	method to continue recording process.
   367  *
   368  */
   369 inline void CMMFDevSoundSvrImp::RecordData(const RMmfIpcMessage& aMessage)
   370 	{
   371 	ASSERT(iDevSoundObserver);
   372 
   373 	// Checkes if the client has a UserEnvironment capability
   374 	if (!aMessage.HasCapability(ECapabilityUserEnvironment))
   375 		{
   376 		iDevSoundObserver->RecordError(KErrPermissionDenied);
   377 		return;
   378 		}
   379 		
   380 	if(iCMMFHwDevice)
   381 		{
   382 		if(iMode== EMMFStateRecording)
   383 			{
   384 			iHwDeviceBuffer->Data().SetLength(iHwDeviceBuffer->RequestSize());
   385 			iCMMFHwDevice->ThisHwBufferEmptied(*iHwDeviceBuffer);
   386 			}
   387 		}
   388 	}
   389 
   390 /*
   391  *
   392  *	Defines the number of times the audio is to be repeated during the tone
   393  *	playback operation.
   394  *
   395  *	A period of silence can follow each playing of tone. The tone playing can
   396  *	be repeated indefinitely.
   397  *
   398  *	@param	"TInt aRepeatCount"
   399  *			The number of times the tone, together with the trailing silence,
   400  *			is to be repeated. If this is set to KMdaRepeatForever, then the
   401  *			tone, together with the trailing silence, is repeated indefinitely
   402  *			or until Stop() is called. If this is set to zero, then the tone is
   403  *			not repeated.
   404  *
   405  *			Supported only during tone playing.
   406  *
   407  */
   408 inline void CMMFDevSoundSvrImp::SetToneRepeats(TInt aRepeatCount,
   409 				const TTimeIntervalMicroSeconds& aRepeatTrailingSilence)
   410 	{
   411 	iRepeatCount = aRepeatCount;
   412 	iRepeatTrailingSilence = aRepeatTrailingSilence;
   413 	}
   414 
   415 /*
   416  *
   417  *	Defines the priority settings that should be used for this instance.
   418  *
   419  *	@param	"const TMMFPrioritySettings& aPrioritySettings"
   420  *			An class type representing the client's priority, priority 
   421  *			preference and state.
   422  *
   423  */
   424 inline void CMMFDevSoundSvrImp::SetPrioritySettings(const TMMFPrioritySettings& aPrioritySettings)
   425 	{
   426 	iAudioPolicyPrioritySettings.iPref = aPrioritySettings.iPref;
   427 	iAudioPolicyPrioritySettings.iPriority = aPrioritySettings.iPriority;
   428 	}
   429 
   430 /*
   431  *
   432  *	Initializes and starts conversion process. Once the process is initiated,
   433  *	observer's call back method BufferToBeFilled() is called with reference to
   434  *	the buffer into which source format data is to be read.
   435  *
   436  *	The maximum size of data (in bytes) that can be converted is specified in
   437  *	CMMFBuffer::RequestSize(). Any data that is read into buffer beyond this
   438  *	size will be ignored.
   439  *
   440  *	Leaves on failure.
   441  *  @prototype
   442  */
   443 inline void CMMFDevSoundSvrImp::ConvertInitL()
   444 	{
   445 	User::Leave(KErrNotSupported);
   446 	}
   447 
   448 /*
   449  *
   450  *	Converts the data in the buffer from source format to destination format.
   451  *	After the data is converted to destination format, a reference to the buffer
   452  *	containing data in destination format is passed in the observer call back
   453  *	method BufferToBeEmptied().
   454  *
   455  *	The amount of data contained in buffer is specified in
   456  *	CMMFBuffer::RequestSize().
   457  *  @prototype
   458  */
   459 inline void CMMFDevSoundSvrImp::ConvertData()
   460 	{
   461 	// No implementation
   462 	}
   463 
   464 // No custom interfaces are supported so return NULL.
   465 inline TAny* CMMFDevSoundSvrImp::CustomInterface(TUid /*aInterfaceId*/)
   466 	{
   467 	return NULL;
   468 	}
   469 
   470 /*
   471  *
   472  *	Returns the number of available pre-defined tone sequences.
   473  *
   474  *	This is the number of fixed sequence supported by DevSound by default.
   475  *
   476  *	@return	"TInt"
   477  *			The fixed sequence count. This value is implementation dependent
   478  *			but is always greater than or equal to zero.
   479  *
   480  */
   481 inline TInt CMMFDevSoundSvrImp::FixedSequenceCount()
   482 	{
   483 	return iFixedSequences->Count();
   484 	}
   485 
   486 /*
   487  *
   488  *	Returns the name assigned to a specific pre-defined tone sequence.
   489  *
   490  *	This is the number of fixed sequence supported by DevSound by default.
   491  *
   492  *	The function raises a panic if sequence number specified invalid.
   493  *
   494  *	@return	"TDesC&"
   495  *			A reference to a Descriptor containing the fixed sequence
   496  *			name indexed by aSequenceNumber.
   497  *
   498  *	@param	"TInt aSequenceNumber"
   499  *			The index identifying the specific pre-defined tone sequence. Index
   500  *			values are relative to zero.
   501  *			This can be any value from zero to the value returned by a call to
   502  *			CMdaAudioPlayerUtility::FixedSequenceCount() - 1.
   503  *			The function raises a panic if sequence number is not within this
   504  *			range.
   505  *
   506  */
   507 inline const TDesC& CMMFDevSoundSvrImp::FixedSequenceName(TInt aSequenceNumber)
   508 	{
   509 	ASSERT((aSequenceNumber >= 0)&&(aSequenceNumber < iFixedSequences->Count()));
   510 	return iDevSoundUtil->FixedSequenceName(aSequenceNumber);
   511 	}
   512 
   513 /*
   514  *
   515  *	Sets Id for this instance of DevSound
   516  *
   517  *	@param	"TInt aDevSoundId"
   518  *			Integer value assigned by Audio Policy Server
   519  *
   520  */
   521 inline void CMMFDevSoundSvrImp::SetDevSoundId(TInt aDevSoundId)
   522 	{
   523 	iDevSoundInfo.iDevSoundId = aDevSoundId;
   524 	}
   525 
   526 /*
   527  *
   528  *	Returns information about this DevSound instance.
   529  *
   530  *	This method is used by Audio Policy Server to make audio policy decisions.
   531  * 
   532  *	@return	"TMMFDevSoundinfo"
   533  *			A reference to TMMFDevSoundinfo object holding the current settings
   534  *			of this DevSound instance.
   535  *
   536  */
   537 inline TMMFDevSoundInfo CMMFDevSoundSvrImp::DevSoundInfo()
   538 	{
   539  	return iDevSoundInfo;
   540 	}
   541 
   542 
   543 /*
   544  *	Updates the total bytes played.
   545  *
   546  */
   547 inline void CMMFDevSoundSvrImp::UpdateBytesPlayed()
   548 	{
   549 	if (iPlayCustomInterface)
   550 		iPlayedBytesCount = iPlayCustomInterface->BytesPlayed();
   551 	}