os/mm/devsound/sounddevbt/src/A2dpBlueTooth/client/A2dpBTHeadsetAudioIfClient.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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 <bttypes.h>
    17 #include <s32mem.h>	// RDesReadStream
    18 
    19 #include "A2dpBTHeadsetAudioIfClient.h"
    20 #include "A2dpBTHeadsetAudioIfClientServer.h"
    21 #include "A2dpBTHeadsetAudioIfServerStart.h"
    22 #include "MMFBtRoutingSoundDevice.h"
    23 
    24 const TInt KBluetoothAddressBufferLength = 32;
    25 
    26 EXPORT_C RA2dpBTHeadsetAudioInterface::RA2dpBTHeadsetAudioInterface()
    27 	{
    28 	}
    29 
    30 EXPORT_C TInt RA2dpBTHeadsetAudioInterface::Connect()
    31 	{
    32 	TRAPD(err, iBufAddr = HBufC::NewL(KBluetoothAddressBufferLength));
    33 	if (err)
    34 		{
    35 		delete iBufAddr;
    36 		iBufAddr = NULL;
    37 		return err;
    38 		}
    39 
    40 	TRAP(err, iPckgBuf = new(ELeave)TPckgBuf<TBTDevAddr>);
    41 	if (err)
    42 		{
    43 		delete iPckgBuf;
    44 		iPckgBuf = NULL;
    45 		return err;
    46 		}
    47 
    48 	TVersion version(KBTAudioServerMajorVersionNumber,
    49 					KBTAudioServerMinorVersionNumber,
    50 					KBTAudioServerBuildVersionNumber);
    51 	// Assume the server is already running and attempt to create a session	
    52 	return CreateSession(KA2DPAudioServerName, version);
    53 	}
    54 
    55 EXPORT_C void RA2dpBTHeadsetAudioInterface::Close()
    56 	{
    57 	// Call the base class
    58 	RMmfSessionBase::Close();
    59 	
    60 	delete iPckgBuf;
    61 	iPckgBuf = NULL;
    62 	
    63 	delete iBufAddr;
    64 	iBufAddr = NULL;
    65 	}
    66 
    67 EXPORT_C void RA2dpBTHeadsetAudioInterface::Initialize(const TBTDevAddr& aRemoteAddress,
    68 														TRequestStatus& aStatus)
    69 	{
    70 	if (iPckgBuf)
    71 		{
    72 		(*iPckgBuf)() = aRemoteAddress;
    73 		SendReceiveResult(EBTAudioServerInitialize, *iPckgBuf, aStatus);
    74 		}
    75 	else
    76 		{
    77 		// iPckgBuf not created => Connect() wasn't called or the returned error code was ignored.
    78 		TRequestStatus* status = &aStatus;
    79 		User::RequestComplete(status, KErrDisconnected);
    80 		}
    81 	}
    82 
    83 EXPORT_C void RA2dpBTHeadsetAudioInterface::CancelInitialize()
    84 	{
    85 	SendReceive(EBTAudioServerCancelInitialize);
    86 	}
    87 
    88 EXPORT_C void RA2dpBTHeadsetAudioInterface::GetSupportedDataTypesL(RArray<TFourCC>& aSupportedDataTypes) const
    89 	{
    90 	aSupportedDataTypes.Reset();
    91 	TPckgBuf<TInt> numberOfElementsPckg;
    92 	TInt err = SendReceiveResult(EBTAudioServerGetSupportedDataTypes, numberOfElementsPckg);
    93 	User::LeaveIfError(err);
    94 
    95 	HBufC8* buf = HBufC8::NewLC(numberOfElementsPckg() * sizeof(TFourCC));
    96 	TPtr8 ptr = buf->Des();
    97 	err = SendReceiveResult(EBTAudioServerCopyFourCCArrayData, ptr);
    98 	User::LeaveIfError(err);
    99 
   100 	RDesReadStream stream(ptr);
   101 	CleanupClosePushL(stream);
   102 	
   103 	for (TInt i = 0; i < numberOfElementsPckg(); i++)
   104 		{
   105 		err = aSupportedDataTypes.Append(stream.ReadInt32L());
   106 		if (err)
   107 			{//note we don't destroy array because we don't own it
   108 			//but we do reset it as it is incomplete
   109 			aSupportedDataTypes.Reset();
   110 			User::Leave(err);
   111 			}
   112 		}
   113 	CleanupStack::PopAndDestroy(2, buf);
   114 	}
   115 	
   116 EXPORT_C void RA2dpBTHeadsetAudioInterface::GetSupportedSampleRatesL(RArray<TUint>& aSupportedDiscreteRates,
   117 																	RArray<TRange>& aSupportedRateRanges) const
   118 	{
   119 	aSupportedDiscreteRates.Reset();
   120 	TPckgBuf<TRatesArrayElements> numberOfElementsPckg;
   121 	
   122 	TInt err = SendReceiveResult(EBTAudioServerGetSupportedSampleRates, numberOfElementsPckg);
   123 	User::LeaveIfError(err);
   124 	
   125 	HBufC8* buf = HBufC8::NewLC(numberOfElementsPckg().iDiscrete * sizeof(TUint));
   126 	TPtr8 ptr = buf->Des();
   127 	err = SendReceiveResult(EBTAudioServerGetSupportedSampleRatesDiscrete, ptr);
   128 	User::LeaveIfError(err);
   129 	
   130 	RDesReadStream stream(ptr);
   131 	CleanupClosePushL(stream);
   132 
   133 	// Populate the discrete rates array
   134 	for (TInt i = 0; i < numberOfElementsPckg().iDiscrete; i++)
   135 		{
   136 		err = aSupportedDiscreteRates.Append(stream.ReadInt32L());
   137 		if (err)
   138 			{//note we don't destroy array because we don't own it
   139 			//but we do reset it as it is incomplete
   140 			aSupportedDiscreteRates.Reset();
   141 			User::Leave(err);
   142 			}
   143 		}
   144 		
   145 	ptr.SetLength(0); //clear out exiting data	
   146 	// Get the rates range array (# of elements and the elements themselves)
   147 	buf = buf->ReAllocL(numberOfElementsPckg().iRange * sizeof(TRange));
   148 	ptr = buf->Des();
   149 	stream.Close();
   150 	stream.Open(ptr);
   151 	err = SendReceiveResult(EBTAudioServerGetSupportedSampleRatesRange, ptr);
   152 	User::LeaveIfError(err);
   153 	TRange range;
   154 	for (TInt i = 0; i < numberOfElementsPckg().iRange; i++)
   155 		{
   156 		range.iLow = stream.ReadInt32L();
   157 		range.iHigh = stream.ReadInt32L();					
   158 		err = aSupportedRateRanges.Append(range);
   159 		if (err)
   160 			{
   161 			aSupportedRateRanges.Reset();
   162 			User::Leave(err);
   163 			}
   164 		}
   165 		
   166 	CleanupStack::PopAndDestroy(2, buf);//stream, buf		
   167 	}
   168 	
   169 EXPORT_C void RA2dpBTHeadsetAudioInterface::GetSupportedChannelsL(RArray<TUint>& aSupportedChannels,
   170 																TMMFStereoSupport& aStereoSupport) const
   171 
   172 	{
   173 	aSupportedChannels.Reset();
   174 	TChannelsSupport channelsSupport;
   175 	channelsSupport.iElementCount = 0;
   176 	channelsSupport.iSupport = EMMFNone;
   177 	TPckgBuf<TChannelsSupport> channelsSupportPckg(channelsSupport);
   178 	
   179 	TInt err = SendReceiveResult(EBTAudioServerGetSupportedChannels, channelsSupportPckg);
   180 	User::LeaveIfError(err);
   181 
   182 	aStereoSupport = channelsSupportPckg().iSupport;
   183 	HBufC8* buf = HBufC8::NewLC(channelsSupportPckg().iElementCount * sizeof(TUint));
   184 	TPtr8 ptr = buf->Des();
   185 	err = SendReceiveResult(EBTAudioServerCopyChannelsArrayData, ptr);
   186 	User::LeaveIfError(err);
   187 	
   188 	RDesReadStream stream(ptr);
   189 	CleanupClosePushL(stream);
   190 
   191 	// Populate the stereo support array
   192 	for (TInt i = 0; i < channelsSupportPckg().iElementCount; i++)
   193 		{
   194 		err = aSupportedChannels.Append(stream.ReadInt32L());
   195 		if (err)
   196 			{//note we don't destroy array because we don't own it
   197 			//but we do reset it as it is incomplete
   198 			aSupportedChannels.Reset();
   199 			User::Leave(err);
   200 			}
   201 		}		
   202 	CleanupStack::PopAndDestroy(2, buf); //stream, buf
   203 	}
   204 
   205 EXPORT_C TInt RA2dpBTHeadsetAudioInterface::SetDataType(const TFourCC& aDataType)
   206 	{
   207 	TPckgBuf<TFourCC> dataTypePckg(aDataType);
   208 	return SendReceiveResult(EBTAudioServerSetDataType, dataTypePckg);
   209 	}
   210 	
   211 EXPORT_C TInt RA2dpBTHeadsetAudioInterface::SetSampleRate(TUint aSampleRate)
   212 	{
   213 	TPckgBuf<TUint> sampleRatePckg(aSampleRate);
   214 	return SendReceiveResult(EBTAudioServerSetSampleRate, sampleRatePckg);
   215 	}
   216 
   217 EXPORT_C TInt RA2dpBTHeadsetAudioInterface::SetChannels(TUint aChannels,
   218 														TMMFStereoSupport aStereoSupport)
   219 	{
   220 	TChannelsSupport channelsSupport;
   221 	channelsSupport.iElementCount = aChannels;
   222 	channelsSupport.iSupport = aStereoSupport;
   223 	TPckgBuf<TChannelsSupport> pckgBuf(channelsSupport);
   224 	return SendReceiveResult(EBTAudioServerSetChannels, pckgBuf);
   225 	}
   226 	
   227 EXPORT_C void RA2dpBTHeadsetAudioInterface::OpenDevice(TRequestStatus& aStatus)
   228 	{
   229 	aStatus = KRequestPending;
   230 	SendReceive(EBTAudioServerOpenDevice, aStatus);
   231 	}
   232 
   233 EXPORT_C void RA2dpBTHeadsetAudioInterface::CancelOpenDevice()
   234 	{
   235 	SendReceive(EBTAudioServerCancelOpenDevice);
   236 	}
   237 	
   238 EXPORT_C void RA2dpBTHeadsetAudioInterface::CloseDevice(TRequestStatus& aStatus)
   239 	{
   240 	aStatus = KRequestPending;
   241 	SendReceive(EBTAudioServerCloseDevice, aStatus);
   242 	}
   243 	
   244 EXPORT_C TUint RA2dpBTHeadsetAudioInterface::Volume() const
   245 	{
   246 	TUint volume = 0;
   247 	TPckgBuf<TUint> volumePckg(volume);
   248 	SendReceiveResult(EBTAudioServerVolume, volumePckg);
   249 	return volumePckg();
   250 	}
   251 
   252 EXPORT_C TInt RA2dpBTHeadsetAudioInterface::SetVolume(TUint aVolume)
   253 	{
   254 	TPckgBuf<TUint> volumePckg(aVolume);
   255 	return SendReceiveResult(EBTAudioServerSetVolume, volumePckg);
   256 	}
   257 
   258 EXPORT_C void RA2dpBTHeadsetAudioInterface::PlayData(const TDesC8& aData, TRequestStatus& aStatus)
   259 	{
   260 	aStatus = KRequestPending;
   261 	SendReceive(EBTAudioServerPlayData, aData, aStatus);
   262 	}
   263 
   264 EXPORT_C void RA2dpBTHeadsetAudioInterface::CancelPlayData()
   265 	{
   266 	SendReceive(EBTAudioServerCancelPlayData);
   267 	}
   268 
   269 EXPORT_C void RA2dpBTHeadsetAudioInterface::FlushBuffer()
   270 	{
   271 	SendReceive(EBTAudioServerFlushBuffer);
   272 	}
   273 
   274 EXPORT_C TUint RA2dpBTHeadsetAudioInterface::BytesPlayed() const
   275 	{
   276 	TUint bytes = 0;
   277 	TPckgBuf<TUint> bytesPlayedPckg(bytes);
   278 	SendReceive(EBTAudioServerBytesPlayed, bytesPlayedPckg);
   279 	return bytesPlayedPckg();
   280 	}
   281 
   282 EXPORT_C void RA2dpBTHeadsetAudioInterface::ResetBytesPlayed()
   283 	{
   284 	SendReceive(EBTAudioServerResetBytesPlayed);
   285 	}
   286 
   287 EXPORT_C void RA2dpBTHeadsetAudioInterface::PauseBuffer()
   288 	{
   289 	SendReceive(EBTAudioServerPauseBuffer);
   290 	}
   291 
   292 EXPORT_C void RA2dpBTHeadsetAudioInterface::ResumePlaying()
   293 	{
   294 	SendReceive(EBTAudioServerResumePlaying);
   295 	}
   296 
   297 EXPORT_C void RA2dpBTHeadsetAudioInterface::NotifyError(TRequestStatus& aStatus)
   298 	{
   299 	aStatus = KRequestPending;
   300 	SendReceive(EBTAudioServerNotifyError, aStatus);
   301 	}
   302 
   303 EXPORT_C void RA2dpBTHeadsetAudioInterface::CancelNotifyError()
   304 	{
   305 	SendReceive(EBTAudioServerCancelNotifyError);
   306 	}
   307 
   308