os/mm/devsound/sounddevbt/src/A2dpBlueTooth/headsetaudioif/A2dpBTheadsetAudioIf.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <mmf/server/mmfcodec.h>
sl@0
    17
#include "mmfSBCCodecImplementationUIDs.hrh"
sl@0
    18
#include <mmffourcc.h>
sl@0
    19
#include "A2dpCodecUtilities.h"
sl@0
    20
#include "A2dpBTheadsetAudioIf.h"
sl@0
    21
#include "MMFBtRoutingSoundDevice.h"	// for TRange
sl@0
    22
sl@0
    23
/**
sl@0
    24
Panics
sl@0
    25
**/
sl@0
    26
enum TA2dpBTHeadsetPanic
sl@0
    27
	{
sl@0
    28
	EA2dpBTHeadsetUnExpectedState,//0
sl@0
    29
	EA2dpBTHeadsetNoRTPStreamer,//1
sl@0
    30
	EA2dpBTHeadsetNoData,//2
sl@0
    31
	EA2dpBTHeadsetUnexpectedDataType,//3
sl@0
    32
	EA2dpBTHeadsetBTAddrNotSet, //4
sl@0
    33
	EA2dpBTHeadsetNoCodec //5
sl@0
    34
	};
sl@0
    35
sl@0
    36
sl@0
    37
static void Panic(TA2dpBTHeadsetPanic aPanic)
sl@0
    38
// Panic client
sl@0
    39
	{
sl@0
    40
	_LIT(KA2dpBTHeadsetPanicName, "A2DP BT If Panic");
sl@0
    41
	User::Panic(KA2dpBTHeadsetPanicName, aPanic);
sl@0
    42
	}
sl@0
    43
sl@0
    44
sl@0
    45
CA2dpBTHeadsetAudioInterface::CA2dpBTHeadsetAudioInterface()
sl@0
    46
	{
sl@0
    47
	}
sl@0
    48
sl@0
    49
void CA2dpBTHeadsetAudioInterface::ConstructL()
sl@0
    50
	{
sl@0
    51
	iA2dpCodecConfiguration = CA2dpAudioCodecConfiguration::NewL();
sl@0
    52
	User::LeaveIfError(iSocketServer.Connect());
sl@0
    53
	iGAVDPStateMachine = CGAVDPStateMachine::NewL(*this, *iA2dpCodecConfiguration, iSocketServer);	
sl@0
    54
	}
sl@0
    55
	
sl@0
    56
/**
sl@0
    57
Creates CA2dpBTHeadsetAudioInterface
sl@0
    58
Note there should only ever be one CA2dpBTHeadsetAudioInterface*
sl@0
    59
However this being a singleton is not enforced in this class however
sl@0
    60
The client of this class ie the CA2dpBTHeadsetAudioServer should enforce this.
sl@0
    61
 
sl@0
    62
@return CA2dpBTHeadsetAudioInterface*
sl@0
    63
*/	
sl@0
    64
EXPORT_C CA2dpBTHeadsetAudioInterface* CA2dpBTHeadsetAudioInterface::NewL()
sl@0
    65
	{
sl@0
    66
	CA2dpBTHeadsetAudioInterface* self = new(ELeave) CA2dpBTHeadsetAudioInterface();
sl@0
    67
	CleanupStack::PushL(self);
sl@0
    68
	self->ConstructL();
sl@0
    69
	CleanupStack::Pop(self);
sl@0
    70
	return self;
sl@0
    71
	}
sl@0
    72
sl@0
    73
/**
sl@0
    74
destructor
sl@0
    75
*/	
sl@0
    76
EXPORT_C CA2dpBTHeadsetAudioInterface::~CA2dpBTHeadsetAudioInterface()
sl@0
    77
	{
sl@0
    78
	SetSniffMode(ETrue);
sl@0
    79
	delete iGAVDPStateMachine;
sl@0
    80
	delete iCodec;
sl@0
    81
	delete iA2dpCodecConfiguration;
sl@0
    82
	delete iRTPStreamer;
sl@0
    83
	iSocketServer.Close();
sl@0
    84
	}
sl@0
    85
	
sl@0
    86
sl@0
    87
/**
sl@0
    88
Procedure to perform a GAVDP initialization sequence on an A2DP headset
sl@0
    89
If the headset is already initialized then this procedure does not perform
sl@0
    90
any further initialization.
sl@0
    91
if link has gone down since a previous call to Initialize then
sl@0
    92
calling this function will perform a full reinitialize.
sl@0
    93
if link goes down during initialization will generate error
sl@0
    94
The GAVDPStateChangeComplete callback is called when the headset device
sl@0
    95
is in the open state.
sl@0
    96
The headset is in the GAVDP_Open state on completion
sl@0
    97
It is not possible to perform an Initialize if the headset is already in the
sl@0
    98
streaming state or is already being initialized.
sl@0
    99
sl@0
   100
@param aRemoteAddress Address of the bluetooth headset we are trying to initialize
sl@0
   101
@param aStatus
sl@0
   102
*/	
sl@0
   103
EXPORT_C void CA2dpBTHeadsetAudioInterface::Initialize(const TBTDevAddr& aRemoteAddress, TRequestStatus& aStatus)
sl@0
   104
	{
sl@0
   105
	__ASSERT_ALWAYS(((iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle)||
sl@0
   106
					(iGAVDPStateMachine->State() == TGAVDPState::EGAVDPOpen)), Panic(EA2dpBTHeadsetUnExpectedState));
sl@0
   107
	__ASSERT_ALWAYS(!iReconfiguring, Panic(EA2dpBTHeadsetUnExpectedState)); //can't initialize while we're reconfiguring
sl@0
   108
	iInitializeRequestStatus = &aStatus;
sl@0
   109
	*iInitializeRequestStatus = KRequestPending;
sl@0
   110
	
sl@0
   111
	//this will cause a full initialization if the address has changed
sl@0
   112
	iGAVDPStateMachine->SetBTAddress(aRemoteAddress);
sl@0
   113
	
sl@0
   114
	TGAVDPState state(TGAVDPState::EGAVDPOpen);
sl@0
   115
	TInt err = iGAVDPStateMachine->ChangeState(state);
sl@0
   116
	if (err)
sl@0
   117
		{
sl@0
   118
		User::RequestComplete(iInitializeRequestStatus, err);
sl@0
   119
		}	
sl@0
   120
	}
sl@0
   121
sl@0
   122
sl@0
   123
/**
sl@0
   124
Procedure to cancel a GAVDP initialization sequence on an A2DP headset
sl@0
   125
*/		
sl@0
   126
EXPORT_C void CA2dpBTHeadsetAudioInterface::CancelInitialize()
sl@0
   127
	{
sl@0
   128
	if (iInitializeRequestStatus)
sl@0
   129
		{
sl@0
   130
		if (*iInitializeRequestStatus == KRequestPending)//make sure there is a pending request to cancel
sl@0
   131
			{
sl@0
   132
			iGAVDPStateMachine->CancelChangeState();
sl@0
   133
			}
sl@0
   134
		}
sl@0
   135
	}
sl@0
   136
	
sl@0
   137
sl@0
   138
/**
sl@0
   139
Handles the state change GAVDPIdle->GAVDPOpen
sl@0
   140
This state change usually occurs as a result to an Initialize call
sl@0
   141
Can also occur due to an error condition resulting in a GAVDP state machine reset 
sl@0
   142
*/
sl@0
   143
void CA2dpBTHeadsetAudioInterface::ProcessGAVDPStateChangeIdleToOpen(TInt aError)
sl@0
   144
	{
sl@0
   145
	__ASSERT_DEBUG((aError||iGAVDPStateMachine->State()==TGAVDPState::EGAVDPOpen||
sl@0
   146
	iGAVDPStateMachine->State()==TGAVDPState::EGAVDPIdle),Panic(EA2dpBTHeadsetUnExpectedState)); //if there is no error then current state should be open
sl@0
   147
	
sl@0
   148
	if (!aError)
sl@0
   149
		{
sl@0
   150
		SetSniffMode(ETrue);
sl@0
   151
		iPaused = EFalse;
sl@0
   152
		iReconfiguring = EFalse;
sl@0
   153
		if (iRTPStreamer) //if this is true then an error has occured somewhere
sl@0
   154
			{//and we need to reset the streamer and close the socket
sl@0
   155
			delete iRTPStreamer;
sl@0
   156
			iRTPStreamer = NULL;
sl@0
   157
			iGAVDPStateMachine->BearerSocket().Close();
sl@0
   158
			}
sl@0
   159
		}
sl@0
   160
sl@0
   161
	if (iInitializeRequestStatus)
sl@0
   162
		{
sl@0
   163
		if (*iInitializeRequestStatus == KRequestPending)
sl@0
   164
			{
sl@0
   165
			User::RequestComplete(iInitializeRequestStatus, aError);
sl@0
   166
			}
sl@0
   167
		}
sl@0
   168
	}
sl@0
   169
	
sl@0
   170
sl@0
   171
/**
sl@0
   172
Handles the state change GAVDPOpen->GAVDPStreaming
sl@0
   173
This state change occurs as a result to an OpenDevice call 
sl@0
   174
This can get called twice, once if a reconfiguration is needed prior 
sl@0
   175
to going in the streaming state where the state machine should still be in the open state
sl@0
   176
and a second time when the state machine is in the streaming state
sl@0
   177
*/	
sl@0
   178
void CA2dpBTHeadsetAudioInterface::ProcessGAVDPStateChangeOpenToStreaming(TInt aError)
sl@0
   179
	{
sl@0
   180
	if (iOpenDeviceRequestStatus)
sl@0
   181
		{
sl@0
   182
		iOpenDeviceError = aError;
sl@0
   183
		if (*iOpenDeviceRequestStatus == KRequestPending)
sl@0
   184
			{
sl@0
   185
			if (iReconfiguring)
sl@0
   186
				{//should be in the OPEN state if we are reconfiguring
sl@0
   187
				__ASSERT_DEBUG((iGAVDPStateMachine->State() == TGAVDPState::EGAVDPOpen),Panic(EA2dpBTHeadsetUnExpectedState));
sl@0
   188
				iReconfiguring = EFalse;
sl@0
   189
				if (!iOpenDeviceError)
sl@0
   190
					{
sl@0
   191
					//move GAVDP state to streaming
sl@0
   192
					TGAVDPState state(TGAVDPState::EGAVDPStreaming);
sl@0
   193
					iOpenDeviceError = iGAVDPStateMachine->ChangeState(state);
sl@0
   194
					}
sl@0
   195
				if (iOpenDeviceError)
sl@0
   196
					{
sl@0
   197
					SetSniffMode(ETrue); //go back to sniff mode
sl@0
   198
					User::RequestComplete(iOpenDeviceRequestStatus, iOpenDeviceError);
sl@0
   199
					}
sl@0
   200
				}//if (iReconfiguring)
sl@0
   201
			else
sl@0
   202
				{//callback must be in response to a streaming change state
sl@0
   203
				__ASSERT_DEBUG((aError||iGAVDPStateMachine->State()==TGAVDPState::EGAVDPStreaming),Panic(EA2dpBTHeadsetUnExpectedState)); //if there is no error then current state should be open
sl@0
   204
				if (!iOpenDeviceError)
sl@0
   205
					{
sl@0
   206
					__ASSERT_DEBUG((iGAVDPStateMachine->State()==TGAVDPState::EGAVDPStreaming),Panic(EA2dpBTHeadsetUnExpectedState));
sl@0
   207
					delete iRTPStreamer;
sl@0
   208
					iRTPStreamer = NULL;
sl@0
   209
					TRAP(iOpenDeviceError,iRTPStreamer = CActiveRTPStreamer::NewL(iGAVDPStateMachine->BearerSocket(),*this));
sl@0
   210
					if (!iOpenDeviceError)
sl@0
   211
						{//create codec
sl@0
   212
						iAudioSettingsHaveChanged = EFalse;
sl@0
   213
						iRTPStreamer->SetAudioConfiguration(*iA2dpCodecConfiguration);
sl@0
   214
						//if we have not set the data type ourselves then get iDataType
sl@0
   215
						//from the code configuration
sl@0
   216
						if (iDataType == KMMFFourCCCodeNULL)
sl@0
   217
							{
sl@0
   218
							iDataType = iA2dpCodecConfiguration->HeadsetCodecDataType();
sl@0
   219
							if (iDataType == KMMFFourCCCodeSBC)
sl@0
   220
								{//then the audio interfacef code will be pcm16
sl@0
   221
								iDataType = KMMFFourCCCodePCM16;
sl@0
   222
								}
sl@0
   223
							}
sl@0
   224
						if (iDataType == KMMFFourCCCodePCM16)
sl@0
   225
							{//then an SBC codec is required
sl@0
   226
							const TUid KSbcCodecUid = { KMmfUidCodecPCM16ToSBC };
sl@0
   227
							TRAP(iOpenDeviceError,iCodec = CMMFCodec::NewL(KSbcCodecUid));
sl@0
   228
							if (!iOpenDeviceError)
sl@0
   229
								{
sl@0
   230
								TUid configType;
sl@0
   231
								configType.iUid = KMmfUidSBCConfigure;
sl@0
   232
								TSBCFrameParameters& SBCFrameParameters = iA2dpCodecConfiguration->UpdateLocalSBCCodecConfiguration();
sl@0
   233
								//use a package buffer for codec config to keep flexability should
sl@0
   234
								//it be possible to use other codecs rather than SBC in future.
sl@0
   235
								TPckgBuf<TSBCFrameParameters> SBCFrameParametersBuf(SBCFrameParameters);
sl@0
   236
								TRAP(iOpenDeviceError,iCodec->ConfigureL(configType, SBCFrameParametersBuf));
sl@0
   237
								iRTPStreamer->SetCodec(*iCodec);
sl@0
   238
								}
sl@0
   239
							}
sl@0
   240
						}//if (!iOpenDeviceError)
sl@0
   241
					}//if (!iOpenDeviceError)
sl@0
   242
				if ((iOpenDeviceError) && (iGAVDPStateMachine->State()==TGAVDPState::EGAVDPStreaming))
sl@0
   243
					{
sl@0
   244
					//there was an error so wind back state to EGAVDPOpen
sl@0
   245
					//don't complete the OpenDevice request status till we've wound back
sl@0
   246
					TGAVDPState state(TGAVDPState::EGAVDPOpen);
sl@0
   247
					TInt iOpenDeviceError = iGAVDPStateMachine->ChangeState(state);
sl@0
   248
					//if the above fails there's not much we can do
sl@0
   249
					}
sl@0
   250
				else
sl@0
   251
					{
sl@0
   252
					//if we get to here then we should either be in the streaming state with no errors
sl@0
   253
					//or in the open state with an error - either way we complete the OpenDevice request status
sl@0
   254
					User::RequestComplete(iOpenDeviceRequestStatus, iOpenDeviceError);
sl@0
   255
					if (iOpenDeviceError)
sl@0
   256
						{
sl@0
   257
						SetSniffMode(ETrue);
sl@0
   258
						}
sl@0
   259
					}
sl@0
   260
				}//else  if not (iReconfiguring)
sl@0
   261
			}//	if (*iOpenRequestStatus == KRequestPending)
sl@0
   262
		}//if (iOpenRequestStatus)
sl@0
   263
	}
sl@0
   264
sl@0
   265
sl@0
   266
/*
sl@0
   267
Handles the state change GAVDPStreaming->GAVDPOpen
sl@0
   268
The transition from streaming to open can be caused by one of two ways
sl@0
   269
1 - A call to CloseDevice
sl@0
   270
2 - A call to Open device where there was an error after the transition to streaming
sl@0
   271
*/	
sl@0
   272
void CA2dpBTHeadsetAudioInterface::ProcessGAVDPStateChangeStreamingToOpen(TInt aError)
sl@0
   273
	{
sl@0
   274
	__ASSERT_DEBUG((aError||iGAVDPStateMachine->State()==TGAVDPState::EGAVDPOpen),Panic(EA2dpBTHeadsetUnExpectedState)); //if there is no error then current state should be open
sl@0
   275
	
sl@0
   276
	//only have streamer and codec in streaming state
sl@0
   277
	delete iRTPStreamer;
sl@0
   278
	iRTPStreamer = NULL;
sl@0
   279
	delete iCodec;
sl@0
   280
	iCodec = NULL;
sl@0
   281
	
sl@0
   282
	if (iCloseDeviceRequestStatus)
sl@0
   283
		{//state change is in response to a close device
sl@0
   284
		if (*iCloseDeviceRequestStatus == KRequestPending)
sl@0
   285
			{
sl@0
   286
			User::RequestComplete(iCloseDeviceRequestStatus, aError);
sl@0
   287
			}
sl@0
   288
		}
sl@0
   289
	else if (iOpenDeviceRequestStatus)
sl@0
   290
		{
sl@0
   291
		//it must be a failed OpenDevice where the state has been wound back to EGAVDPOpen
sl@0
   292
		__ASSERT_DEBUG((iGAVDPStateMachine->State() == TGAVDPState::EGAVDPOpen),Panic(EA2dpBTHeadsetUnExpectedState));
sl@0
   293
		//the following ASSERT_DEBUG should be present but is commented out as the RVCT 
sl@0
   294
		//compiler generates a warning
sl@0
   295
		//__ASSERT_DEBUG((iOpenDeviceError), EA2dpBTHeadsetUnExpectedState);
sl@0
   296
		if (*iOpenDeviceRequestStatus == KRequestPending)
sl@0
   297
			{
sl@0
   298
			User::RequestComplete(iOpenDeviceRequestStatus, iOpenDeviceError);
sl@0
   299
			}
sl@0
   300
		}
sl@0
   301
	SetSniffMode(ETrue);
sl@0
   302
	}
sl@0
   303
sl@0
   304
/**
sl@0
   305
Callback by the GAVDP state machine when a GAVDP state change has been completed
sl@0
   306
sl@0
   307
@param aInitialState The TGAVDPState prior to the start of the state change
sl@0
   308
@param aCurrentState The current TGAVDPState of the GAVDP state machine
sl@0
   309
@param aError standard Symbian error code.  
sl@0
   310
*/
sl@0
   311
void CA2dpBTHeadsetAudioInterface::GAVDPStateChangeComplete(const TGAVDPState& aInitialState, TInt aError)
sl@0
   312
	{	
sl@0
   313
	switch(aInitialState.State())
sl@0
   314
		{
sl@0
   315
		case TGAVDPState::EGAVDPIdle:
sl@0
   316
			ProcessGAVDPStateChangeIdleToOpen(aError);
sl@0
   317
			break;
sl@0
   318
		case TGAVDPState::EGAVDPOpen:
sl@0
   319
			ProcessGAVDPStateChangeOpenToStreaming(aError);
sl@0
   320
			break;
sl@0
   321
		case TGAVDPState::EGAVDPStreaming:
sl@0
   322
			ProcessGAVDPStateChangeStreamingToOpen(aError);	
sl@0
   323
			break;
sl@0
   324
		default:
sl@0
   325
			Panic(EA2dpBTHeadsetUnExpectedState);
sl@0
   326
			break;
sl@0
   327
		}
sl@0
   328
	}
sl@0
   329
sl@0
   330
sl@0
   331
/**
sl@0
   332
Returns a list of the supported datatypes for audio that can be sent to the headset.
sl@0
   333
The list is obtained from the headset, so the headset need to have been Initialized first
sl@0
   334
	
sl@0
   335
@param  aSupportedDataTypes
sl@0
   336
The array of supported data types that will be filled in by this function.
sl@0
   337
The supported data types are in the form of an array
sl@0
   338
of TFourCC codes. Any existing entries in the array will be overwritten on 
sl@0
   339
calling this function. 
sl@0
   340
@return standard Symbian error code.   
sl@0
   341
KErrNotReady if headset has not been initialized.
sl@0
   342
*/
sl@0
   343
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::GetSupportedDataTypes(RArray<TFourCC>& aSupportedDataTypes) const
sl@0
   344
	{
sl@0
   345
	aSupportedDataTypes.Reset(); //clear any existing data in aSupportedDataTypes array
sl@0
   346
	TInt err = KErrNone;
sl@0
   347
	RArray<TUsableSEP>& usableSEPs = iGAVDPStateMachine->UsableSEPs();
sl@0
   348
	TUint numberOfUsableSEPS = usableSEPs.Count();
sl@0
   349
	if (numberOfUsableSEPS)
sl@0
   350
		{
sl@0
   351
		TUint i;
sl@0
   352
		TUsableSEP SEP;
sl@0
   353
		//iterate through the list of usable SEPs looking for one that supports
sl@0
   354
		//the requested data type
sl@0
   355
		for (i=0;  i<usableSEPs.Count(); i++)
sl@0
   356
			{
sl@0
   357
			SEP = usableSEPs[i];
sl@0
   358
			//both the headset and the A2dpBTHeadsetAudioInterface need to
sl@0
   359
			//support the datatype so check we also support the datatype
sl@0
   360
			//currently only SBC & MP3 data types are supported
sl@0
   361
			if ((SEP.iDataType == KMMFFourCCCodeSBC)||(SEP.iDataType == KMMFFourCCCodeMP3))
sl@0
   362
				{
sl@0
   363
				err = aSupportedDataTypes.Append(SEP.iDataType);
sl@0
   364
				}
sl@0
   365
			if (err)
sl@0
   366
				{
sl@0
   367
				break;
sl@0
   368
				}
sl@0
   369
			if (SEP.iDataType == KMMFFourCCCodeSBC)
sl@0
   370
				{//then we also support pcm16
sl@0
   371
				err = aSupportedDataTypes.Append(KMMFFourCCCodePCM16);
sl@0
   372
				if (err)
sl@0
   373
					{
sl@0
   374
					break;
sl@0
   375
					}
sl@0
   376
				}
sl@0
   377
			}
sl@0
   378
		}
sl@0
   379
	else if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle)
sl@0
   380
		{//no usable SEPs available so can't yet get supported data types
sl@0
   381
		err = KErrNotReady;
sl@0
   382
		}
sl@0
   383
		
sl@0
   384
	if (err)
sl@0
   385
		{
sl@0
   386
		aSupportedDataTypes.Reset();
sl@0
   387
		}
sl@0
   388
	return err;
sl@0
   389
	}
sl@0
   390
	
sl@0
   391
sl@0
   392
/**
sl@0
   393
Returns the sample rates supported by the headset.
sl@0
   394
In the case of SBC the supported values are obtained from the headset.
sl@0
   395
In the case of mp3,AAC & ATRAC 3 the values are not obtained from
sl@0
   396
the headset as the underlying bluetoothav code does not have a defined structure
sl@0
   397
for containing these values, therefore the mandatory values are returned
sl@0
   398
	
sl@0
   399
@param  aSupportedDiscreteRates
sl@0
   400
The array of supported sample rates that will be filled in by this function.
sl@0
   401
The supported data types are in the form of an array
sl@0
   402
of TFourCC codes. Any existing entries in the array will be overwritten on 
sl@0
   403
calling this function. 
sl@0
   404
@param aSupportedRateRanges
sl@0
   405
To cover the case where headsets support a range of sample rates
sl@0
   406
@return standard Symbian error code.   
sl@0
   407
KErrNotReady if headset has not been initialized.
sl@0
   408
*/	
sl@0
   409
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::GetSupportedSampleRates(RArray<TUint>& aSupportedDiscreteRates, RArray<TRange>& aSupportedRateRanges) const
sl@0
   410
	{
sl@0
   411
	TInt error = KErrNone;
sl@0
   412
	aSupportedDiscreteRates.Reset();
sl@0
   413
	aSupportedRateRanges.Reset();
sl@0
   414
	
sl@0
   415
	//get the codec capabilites from the GAVDP state machine
sl@0
   416
	TAvdtpMediaCodecCapabilities* codecCaps = NULL;
sl@0
   417
	error = iGAVDPStateMachine->CodecCaps(codecCaps);
sl@0
   418
sl@0
   419
	if (!error)
sl@0
   420
		{
sl@0
   421
		error = TA2dpCodecCapabilityParser::GetSupportedSampleRates(*codecCaps,aSupportedDiscreteRates);
sl@0
   422
		}
sl@0
   423
	if (error)
sl@0
   424
		{
sl@0
   425
		aSupportedDiscreteRates.Reset();
sl@0
   426
		}
sl@0
   427
	
sl@0
   428
	return error;
sl@0
   429
	}
sl@0
   430
	
sl@0
   431
sl@0
   432
/**
sl@0
   433
Returns the number of channels supported by the headset.
sl@0
   434
In the case of SBC the supported values are obtained from the headset.
sl@0
   435
In the case of mp3,AAC & ATRAC 3 the values are not obtained from
sl@0
   436
the headset as the underlying bluetoothav code does not have a defined structure
sl@0
   437
for containing these values, therefore the mandatory values are returned
sl@0
   438
	
sl@0
   439
@param  aSupportedChannels
sl@0
   440
The array of supported number of channels that will be filled in by this function.
sl@0
   441
Any existing entries in the array will be overwritten on 
sl@0
   442
calling this function. 
sl@0
   443
@param aStereoSupport
sl@0
   444
Additional parameter to specifiy the stereo support
sl@0
   445
@return standard Symbian error code.
sl@0
   446
@see TMMFStereoSupport
sl@0
   447
KErrNotReady if headset has not been initialized.
sl@0
   448
*/		
sl@0
   449
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::GetSupportedChannels(RArray<TUint>& aSupportedChannels, TMMFStereoSupport& aStereoSupport) const
sl@0
   450
	{
sl@0
   451
	aSupportedChannels.Reset();
sl@0
   452
	aStereoSupport = EMMFNone;
sl@0
   453
	
sl@0
   454
	TInt error = KErrNone;
sl@0
   455
sl@0
   456
	//get the codec capabilites from the GAVDP state machine
sl@0
   457
	TAvdtpMediaCodecCapabilities* codecCaps = NULL;
sl@0
   458
	error = iGAVDPStateMachine->CodecCaps(codecCaps);
sl@0
   459
	
sl@0
   460
	if (!error)
sl@0
   461
		{
sl@0
   462
		error = TA2dpCodecCapabilityParser::GetSupportedChannels(*codecCaps, aSupportedChannels, aStereoSupport);
sl@0
   463
		}
sl@0
   464
	if (error) 
sl@0
   465
		{
sl@0
   466
		aSupportedChannels.Reset();
sl@0
   467
		aStereoSupport = EMMFNone;
sl@0
   468
		}
sl@0
   469
	return error;
sl@0
   470
	}
sl@0
   471
	
sl@0
   472
sl@0
   473
/**
sl@0
   474
Sets the data type of the data sent to the CA2dpBTHeadsetAudioInterface
sl@0
   475
Note that data type specified here may not be identical to the data type
sl@0
   476
sent to the headset and hence the data type used in the the a2dp codec configuration.  
sl@0
   477
ie if the CA2dpBTHeadsetAudioInterface data type set by this method is pcm16
sl@0
   478
then the data type sent to the headset is SBC.
sl@0
   479
note that this method just sets the internal iDatatype member
sl@0
   480
the datatype cannot be changed on the fly, but only gets changed
sl@0
   481
when the GAVDP state machine is reconfigured in CA2dpBTHeadsetAudioInterface::OpenDevice()
sl@0
   482
sl@0
   483
@param  aSinkFourCC
sl@0
   484
        The 4CC code of the data to be supplied to this class instance.
sl@0
   485
        
sl@0
   486
@return System wide error code indicating if the function call was successful.
sl@0
   487
		KErrNotReady if the headset is not initialized
sl@0
   488
		KErrInUse if the headset is already streaming
sl@0
   489
		KErrNotSupported if the datatype is not supported
sl@0
   490
*/
sl@0
   491
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::SetDataType(const TFourCC& aDataType)
sl@0
   492
	{
sl@0
   493
	TInt error = KErrNone;
sl@0
   494
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle)
sl@0
   495
		{
sl@0
   496
		return KErrNotReady;
sl@0
   497
		}
sl@0
   498
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPStreaming)
sl@0
   499
		{
sl@0
   500
		return KErrInUse; //don't allow this while we're streaming audio
sl@0
   501
		}
sl@0
   502
		
sl@0
   503
	//the requested data type is a non standard data type
sl@0
   504
	//so first check if there is a SEP that supports it
sl@0
   505
	RArray<TUsableSEP>& usableSEPs = iGAVDPStateMachine->UsableSEPs();
sl@0
   506
	TUint i;
sl@0
   507
	TUsableSEP SEP;
sl@0
   508
	TBool dataTypeSupported = EFalse;
sl@0
   509
	//iterate through the list of usable SEPs looking for one that supports
sl@0
   510
	//the requested data type
sl@0
   511
	for (i=0; i<usableSEPs.Count(); i++)
sl@0
   512
		{
sl@0
   513
		SEP = usableSEPs[i];
sl@0
   514
		if (SEP.iDataType == aDataType)
sl@0
   515
			{//one of the usable SEPs supports the requested data type
sl@0
   516
			dataTypeSupported = ETrue;
sl@0
   517
			iDataType = aDataType;
sl@0
   518
			if (aDataType != iA2dpCodecConfiguration->HeadsetCodecDataType())
sl@0
   519
				{
sl@0
   520
				iA2dpCodecConfiguration->SetHeadsetCodecDataType(aDataType);
sl@0
   521
				iAudioSettingsHaveChanged = ETrue;
sl@0
   522
				}
sl@0
   523
			break;
sl@0
   524
			}
sl@0
   525
		else if ((SEP.iDataType == KMMFFourCCCodeSBC) && (aDataType == KMMFFourCCCodePCM16))
sl@0
   526
			{
sl@0
   527
			dataTypeSupported = ETrue;
sl@0
   528
			iDataType = aDataType;
sl@0
   529
			if (iA2dpCodecConfiguration->HeadsetCodecDataType()!= KMMFFourCCCodeSBC)
sl@0
   530
				{
sl@0
   531
				iA2dpCodecConfiguration->SetHeadsetCodecDataType(KMMFFourCCCodeSBC);
sl@0
   532
				iAudioSettingsHaveChanged = ETrue;
sl@0
   533
				}
sl@0
   534
			break;
sl@0
   535
			}		
sl@0
   536
		}
sl@0
   537
	if (!dataTypeSupported)
sl@0
   538
		{
sl@0
   539
		error = KErrNotSupported;
sl@0
   540
		}		
sl@0
   541
	return error;
sl@0
   542
	}
sl@0
   543
	
sl@0
   544
sl@0
   545
/**
sl@0
   546
Sets the sample rate 
sl@0
   547
sl@0
   548
@param aSampleRate
sl@0
   549
@return System wide error code indicating if the function call was successful.
sl@0
   550
		KErrNotReady if the headset is not initialized
sl@0
   551
		KErrInUse if the headset is already streaming
sl@0
   552
		KErrNotSupported if the sample rate is not supported
sl@0
   553
*/	
sl@0
   554
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::SetSampleRate(TUint aSampleRate)
sl@0
   555
	{
sl@0
   556
	TInt error = KErrNone;
sl@0
   557
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle)
sl@0
   558
		{
sl@0
   559
		return KErrNotReady;
sl@0
   560
		}
sl@0
   561
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPStreaming)
sl@0
   562
		{
sl@0
   563
		//don't allow this while we're streaming audio
sl@0
   564
		return KErrInUse; 
sl@0
   565
		}
sl@0
   566
	
sl@0
   567
	//check that the sample rate is supported
sl@0
   568
	RArray<TUint> supportedDiscreteRates;
sl@0
   569
	RArray<TRange> supportedRateRanges;
sl@0
   570
	GetSupportedSampleRates(supportedDiscreteRates, supportedRateRanges);
sl@0
   571
	if (supportedDiscreteRates.Find(aSampleRate) == KErrNotFound)
sl@0
   572
		{
sl@0
   573
		//in theory we should iterate through the ranges as well, however
sl@0
   574
		//SBC only suports discrete values so just return KErrNotSupported
sl@0
   575
		error = KErrNotSupported;
sl@0
   576
		}
sl@0
   577
	else
sl@0
   578
		{
sl@0
   579
		if (aSampleRate != iA2dpCodecConfiguration->SampleRate())
sl@0
   580
			{
sl@0
   581
			iA2dpCodecConfiguration->SetSampleRate(aSampleRate);
sl@0
   582
			iAudioSettingsHaveChanged = ETrue;
sl@0
   583
			}
sl@0
   584
		}
sl@0
   585
	supportedDiscreteRates.Close();
sl@0
   586
	supportedRateRanges.Close();
sl@0
   587
	return error;
sl@0
   588
	}
sl@0
   589
	
sl@0
   590
sl@0
   591
/**
sl@0
   592
Sets the number of channels 
sl@0
   593
sl@0
   594
@param aSampleChannels
sl@0
   595
@param aStereoSupport.  note that the aStereoSupport in the case of pcm16 refers to the output to the headset.
sl@0
   596
the data going for pcm16 is always either interleaved stereo pcm16 or mono
sl@0
   597
the aStereoSupport parameter is ignored for non stereo
sl@0
   598
@see TMMFStereoSupport
sl@0
   599
@return System wide error code indicating if the function call was successful.
sl@0
   600
		KErrNotReady if the headset is not initialized
sl@0
   601
		KErrInUse if the headset is already streaming
sl@0
   602
		KErrNotSupported if the datatype is not supported
sl@0
   603
*/	
sl@0
   604
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::SetChannels(TUint aChannels, TMMFStereoSupport aStereoSupport)
sl@0
   605
	{
sl@0
   606
	TInt error = KErrNone;
sl@0
   607
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle)
sl@0
   608
		{
sl@0
   609
		return KErrNotReady;
sl@0
   610
		}
sl@0
   611
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPStreaming)
sl@0
   612
		{
sl@0
   613
		return KErrInUse; //don't allow this while we're streaming audio
sl@0
   614
		}
sl@0
   615
sl@0
   616
	//check that the number of channels is supported
sl@0
   617
	RArray<TUint> supportedChannels;
sl@0
   618
	TMMFStereoSupport stereoSupport;
sl@0
   619
	GetSupportedChannels(supportedChannels, stereoSupport);
sl@0
   620
	if (supportedChannels.Find(aChannels) == KErrNotFound)
sl@0
   621
		{
sl@0
   622
		error = KErrNotSupported;
sl@0
   623
		}	
sl@0
   624
	else if (aChannels == EMMFStereo)
sl@0
   625
		{
sl@0
   626
		//now check stereo support
sl@0
   627
		if ((aStereoSupport & stereoSupport) != aStereoSupport)
sl@0
   628
			{
sl@0
   629
			error = KErrNotSupported;
sl@0
   630
			}
sl@0
   631
		else if (iA2dpCodecConfiguration->StereoSupport() != aStereoSupport)
sl@0
   632
			{
sl@0
   633
			iA2dpCodecConfiguration->SetStereoSupport(aStereoSupport);
sl@0
   634
			iAudioSettingsHaveChanged = ETrue;
sl@0
   635
			}
sl@0
   636
		}
sl@0
   637
	if (!error)
sl@0
   638
		{
sl@0
   639
		if (iA2dpCodecConfiguration->Channels() != aChannels)
sl@0
   640
			{
sl@0
   641
			iA2dpCodecConfiguration->SetChannels(aChannels);
sl@0
   642
			iAudioSettingsHaveChanged = ETrue;
sl@0
   643
			}
sl@0
   644
		}
sl@0
   645
	return error;
sl@0
   646
	}
sl@0
   647
sl@0
   648
	
sl@0
   649
/**
sl@0
   650
Procedure to gets the headset into the GAVDP Streaming state where 
sl@0
   651
it is ready to accept audio data.  The procedure causes a reconfiguration
sl@0
   652
of the headset incase the settings have changed - and if they haven't then the 
sl@0
   653
reconfigure does nothing.
sl@0
   654
The GAVDPStateChangeComplete callback is called after the reconfigure
sl@0
   655
where it puts the GAVDP state machine in the streaming state
sl@0
   656
The GAVDPStateChangeComplete callback is called again when the headset device
sl@0
   657
is in the streaming state.
sl@0
   658
sl@0
   659
@param aStatus
sl@0
   660
*/
sl@0
   661
EXPORT_C void CA2dpBTHeadsetAudioInterface::OpenDevice(TRequestStatus& aStatus)
sl@0
   662
	{
sl@0
   663
	iOpenDeviceRequestStatus = &aStatus;
sl@0
   664
	*iOpenDeviceRequestStatus = KRequestPending;
sl@0
   665
	
sl@0
   666
	if (iGAVDPStateMachine->State() == TGAVDPState::EGAVDPStreaming)
sl@0
   667
		{
sl@0
   668
		//device is already open
sl@0
   669
		User::RequestComplete(iOpenDeviceRequestStatus, KErrNone);
sl@0
   670
		return;
sl@0
   671
		}
sl@0
   672
	
sl@0
   673
	//this isn't a local error variable as it is used in the GAVDPStateChangeComplete callback
sl@0
   674
	iOpenDeviceError = KErrNone;
sl@0
   675
	//take out of sniff mode - no point in checking error code as there is
sl@0
   676
	//not much that can be done
sl@0
   677
	SetSniffMode(EFalse);
sl@0
   678
	
sl@0
   679
	//first reconfigure if the configuration has changed
sl@0
   680
	//note that there is a subtle difference between the configuration of the 
sl@0
   681
	//CA2dpBTHeadsetAudioInterface and that of the GAVDP state machine
sl@0
   682
	//the configuration of the CA2dpBTHeadsetAudioInterface is the configuration
sl@0
   683
	//seen from the DevSound whereas the CGAVDPStateMachine is the configuration
sl@0
   684
	//as seen by the headset.  In most cases they would be identical, the only
sl@0
   685
	//current case where the differ is in the data type where the DevSound sees
sl@0
   686
	//the data tpye as pcm16 but the headset is SBC
sl@0
   687
	iOpenDeviceError = iGAVDPStateMachine->Reconfigure(iAudioSettingsHaveChanged);
sl@0
   688
	if (iOpenDeviceError)
sl@0
   689
		{
sl@0
   690
		User::RequestComplete(iOpenDeviceRequestStatus, iOpenDeviceError);
sl@0
   691
		}
sl@0
   692
	else
sl@0
   693
		{
sl@0
   694
		//the Reconfigure will result in a GAVDPStateChangeComplete callback
sl@0
   695
		iReconfiguring = ETrue;
sl@0
   696
		}
sl@0
   697
	}
sl@0
   698
sl@0
   699
sl@0
   700
/**
sl@0
   701
 Procedure to cancel a GAVDP initialization sequence on an A2DP headset
sl@0
   702
*/	
sl@0
   703
EXPORT_C void CA2dpBTHeadsetAudioInterface::CancelOpenDevice()
sl@0
   704
	{
sl@0
   705
	if (iOpenDeviceRequestStatus)
sl@0
   706
		{
sl@0
   707
		if (*iOpenDeviceRequestStatus == KRequestPending)//make sure there is a pending request to cancel
sl@0
   708
			{
sl@0
   709
			iGAVDPStateMachine->CancelChangeState();
sl@0
   710
			}
sl@0
   711
		}
sl@0
   712
	}
sl@0
   713
sl@0
   714
/**
sl@0
   715
Procedure to put a headset that is in the GAVDP Streaming state back into
sl@0
   716
the GAVDP Open state
sl@0
   717
The GAVDPStateChangeComplete callback is called again when the headset device
sl@0
   718
is in the open state.
sl@0
   719
sl@0
   720
@param aStatus
sl@0
   721
*/	
sl@0
   722
EXPORT_C void CA2dpBTHeadsetAudioInterface::CloseDevice(TRequestStatus& aStatus)
sl@0
   723
	{
sl@0
   724
	iCloseDeviceRequestStatus = &aStatus;
sl@0
   725
	*iCloseDeviceRequestStatus = KRequestPending;
sl@0
   726
	
sl@0
   727
	if (iGAVDPStateMachine->State() != TGAVDPState::EGAVDPStreaming)
sl@0
   728
		{
sl@0
   729
		//device isn't open
sl@0
   730
		User::RequestComplete(iCloseDeviceRequestStatus, KErrNone);
sl@0
   731
		return;
sl@0
   732
		}
sl@0
   733
	else
sl@0
   734
		{
sl@0
   735
		if (iRTPStreamer)
sl@0
   736
			{
sl@0
   737
			iRTPStreamer->Pause();
sl@0
   738
			}
sl@0
   739
		//note the callback ProcessGAVDPStateChangeStreamingToOpen
sl@0
   740
		//will put the BT link back in sniff mode
sl@0
   741
		TGAVDPState state(TGAVDPState::EGAVDPOpen);
sl@0
   742
		TInt err = iGAVDPStateMachine->ChangeState(state);
sl@0
   743
		if (err)
sl@0
   744
			{
sl@0
   745
			User::RequestComplete(iCloseDeviceRequestStatus, err);
sl@0
   746
			}
sl@0
   747
		}
sl@0
   748
	}
sl@0
   749
sl@0
   750
/**
sl@0
   751
Procedure to get the headset volume
sl@0
   752
sl@0
   753
@return volume in the range 0-255
sl@0
   754
*/	
sl@0
   755
EXPORT_C TUint CA2dpBTHeadsetAudioInterface::Volume() const
sl@0
   756
	{
sl@0
   757
	return 0;
sl@0
   758
	}
sl@0
   759
	
sl@0
   760
sl@0
   761
/**
sl@0
   762
Procedure to set the volume in the range 0-255
sl@0
   763
Note this procedure requires the AVRCP bluettooth profile
sl@0
   764
in order to change the volume which is not implemented
sl@0
   765
sl@0
   766
@param aVolume the volume in the range 0-255
sl@0
   767
*/	
sl@0
   768
EXPORT_C TInt CA2dpBTHeadsetAudioInterface::SetVolume(TUint /*aVolume*/)
sl@0
   769
	{
sl@0
   770
	return KErrNone;
sl@0
   771
	}
sl@0
   772
	
sl@0
   773
sl@0
   774
/**
sl@0
   775
This function causes the CA2dpBTHeadsetAudioInterface to send the audio data contained in aData to the initialized A2DP headset.
sl@0
   776
The data is buffered internally and CA2dpBTHeadsetAudioInterface may already be playing audio
sl@0
   777
when this method is called.
sl@0
   778
The TRequestStatus is completed when the CA2dpBTHeadsetAudioInterface  has finished with the audio data contained in aData.  
sl@0
   779
By 'finished' this does not mean finished as in physically finished playing the audio,
sl@0
   780
but to inform the client that:
sl@0
   781
a)	it can repopulate the buffer with new audio data.
sl@0
   782
b)	the  CA2dpBTHeadsetAudioInterface is ready to accept another buffer of audio data via a further call to PlayData()
sl@0
   783
(ie the CA2dpBTHeadsetAudioInterface is not expected to accept multiple buffers in succession prior to completing their
sl@0
   784
TRequestStatus as this would not provide the client with any indication of whether the
sl@0
   785
sound drivers internal buffers are full).
sl@0
   786
sl@0
   787
@param aStatus  The TRequestStatus completed when the sound driver has finished with the audio data contained in aData.
sl@0
   788
The TRequestStatus is set to KErrNone unless the request has been cancelled via CancelPlayData()
sl@0
   789
in which case the status will be set to KErrCancel. 
sl@0
   790
The TRequestStatus is completed as soon as possible and before the audio has been physically played
sl@0
   791
in order for the client to send further audio buffers before the buffers already in the sound driver
sl@0
   792
are played out. 
sl@0
   793
Prior to calling PlayData() the client would be expected to Initialize Configure and Open the CA2dpBTHeadsetAudioInterface
sl@0
   794
and register with the sound driver that it requires notification of error conditions via a call to NotifyPlayError().
sl@0
   795
	
sl@0
   796
@param aData  Buffer containing the audio data to be played.  
sl@0
   797
aData, is not owned or created by the sound driver but is owned by the client.  
sl@0
   798
*/
sl@0
   799
EXPORT_C void CA2dpBTHeadsetAudioInterface::PlayData(const TDesC8& aData, TRequestStatus& aStatus)
sl@0
   800
	{
sl@0
   801
	__ASSERT_DEBUG((aData.Length()), Panic(EA2dpBTHeadsetNoData));
sl@0
   802
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   803
	
sl@0
   804
	iRTPStreamer->Send(aData, aStatus);
sl@0
   805
	}
sl@0
   806
	
sl@0
   807
/**
sl@0
   808
Cancels the current request for PlayData() if currently active.
sl@0
   809
The currently active PlayData() completes the request status immediately with KErrCancel.
sl@0
   810
*/	
sl@0
   811
EXPORT_C void CA2dpBTHeadsetAudioInterface::CancelPlayData()
sl@0
   812
	{
sl@0
   813
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   814
		
sl@0
   815
	iRTPStreamer->CancelLastSendBuffer();
sl@0
   816
	}
sl@0
   817
	
sl@0
   818
sl@0
   819
/**
sl@0
   820
Used to clear all internally buffered audio.  
sl@0
   821
This function does not complete any pending TRequestStatuses.  
sl@0
   822
*/	
sl@0
   823
EXPORT_C void CA2dpBTHeadsetAudioInterface::FlushBuffer()
sl@0
   824
	{
sl@0
   825
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   826
	
sl@0
   827
	iRTPStreamer->FlushPendingSendBuffers();
sl@0
   828
	}
sl@0
   829
sl@0
   830
	
sl@0
   831
/**
sl@0
   832
Returns the total number of bytes of data sent in PlayData that have been
sl@0
   833
sent to the headset.  ie the number of bytes sent prior to any encoding by the SBC codec.
sl@0
   834
It is updated after each buffer has been sent to the headset. 
sl@0
   835
The BytesPlayed are not reset to 0 on an error condition.
sl@0
   836
It must be explicitly reset via a call to ResetBytesPlayed().
sl@0
   837
	
sl@0
   838
@param The number of bytes played
sl@0
   839
*/	
sl@0
   840
EXPORT_C TUint CA2dpBTHeadsetAudioInterface::BytesPlayed() const
sl@0
   841
	{
sl@0
   842
	TUint bytesPlayed = 0;
sl@0
   843
	if (iRTPStreamer)//don't ASSERT iRTPStreamer here as this API still has meaning with no streamer
sl@0
   844
		{
sl@0
   845
		bytesPlayed = iRTPStreamer->BytesSent();
sl@0
   846
		}
sl@0
   847
	return bytesPlayed;
sl@0
   848
	}
sl@0
   849
sl@0
   850
sl@0
   851
/**
sl@0
   852
Resets bytes played to 0.
sl@0
   853
*/	
sl@0
   854
EXPORT_C void CA2dpBTHeadsetAudioInterface::ResetBytesPlayed()
sl@0
   855
	{
sl@0
   856
	if (iRTPStreamer)//don't ASSERT iRTPStreamer here as this API still has meaning with no streamer
sl@0
   857
		{
sl@0
   858
		iRTPStreamer->ResetBytesSent();
sl@0
   859
		}
sl@0
   860
	}
sl@0
   861
	
sl@0
   862
sl@0
   863
/**
sl@0
   864
Function to halt the sending of buffers to the headset.
sl@0
   865
Note that even if no buffers are being send via PlayData buffers
sl@0
   866
can still be sent to the headset as they are buffered internally
sl@0
   867
Calling PauseBuffer stops the internally buffered data being sent as well
sl@0
   868
as any buffers sent via PlayData
sl@0
   869
If the TRequestStatus from the previous PlayData() is outstanding then PausePlayBuffer()
sl@0
   870
does not complete the TRequestStatus.
sl@0
   871
The TRequestStatus will only be completed after ResumePlaying() is called.
sl@0
   872
*/	
sl@0
   873
EXPORT_C void CA2dpBTHeadsetAudioInterface::PauseBuffer()
sl@0
   874
	{
sl@0
   875
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   876
	
sl@0
   877
	iRTPStreamer->Pause();
sl@0
   878
	iPaused = ETrue;
sl@0
   879
	}
sl@0
   880
sl@0
   881
	
sl@0
   882
/**
sl@0
   883
This resumes playing from where playing was halted from the previous call to PauseBuffer().
sl@0
   884
The next TRequestStatus (if active) to be completed, is the request status from the previous PlayData()
sl@0
   885
prior to calling PausePlayBuffer().
sl@0
   886
If PausePlayBuffer() has not been called prior to ResumePlaying(),
sl@0
   887
then this method does nothing.
sl@0
   888
*/	
sl@0
   889
EXPORT_C void CA2dpBTHeadsetAudioInterface::ResumePlaying()
sl@0
   890
	{
sl@0
   891
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   892
	
sl@0
   893
	iRTPStreamer->Resume();
sl@0
   894
	iPaused = EFalse;
sl@0
   895
	}
sl@0
   896
	
sl@0
   897
sl@0
   898
/**
sl@0
   899
This function is used when the client is requesting notification of an error that occurs during playback.
sl@0
   900
The TRequestStatus passed in the NotifyPlayError() method is separate to the TRequestStatus passed into the PlayData() method,
sl@0
   901
and hence the client would typically have a separate active object for both the PlayData()
sl@0
   902
and NotifyPlayError() TRequestStatuses.
sl@0
   903
The NotifyPlayError() method is required because if an error occurs during playback,
sl@0
   904
the PlayData() TRequestStatus may not be active.
sl@0
   905
If a PlayData() request status is active then the error is reported back via the PlayData()
sl@0
   906
TRequestStatus and not via the NotifyPlayError() TRequestStatus.
sl@0
   907
If no PlayData() TRequestStatus is active then the error is reported back by completing the NotifyPlayError()
sl@0
   908
TRequestStatus with the appropriate error code.
sl@0
   909
Note that a call to CancelPlayData() should complete the PlayData() request status with KErrCancel
sl@0
   910
and should not complete the NotifyPlayError() TRequestStatus.
sl@0
   911
sl@0
   912
@param aStatus TRequestStatus completed when a play error occurs.
sl@0
   913
*/	
sl@0
   914
EXPORT_C void CA2dpBTHeadsetAudioInterface::NotifyError(TRequestStatus& aStatus)
sl@0
   915
	{
sl@0
   916
	iNotifyRequestStatus= &aStatus;
sl@0
   917
	*iNotifyRequestStatus = KRequestPending;
sl@0
   918
	}
sl@0
   919
	
sl@0
   920
sl@0
   921
/**
sl@0
   922
Cancels the NotifyPlayError() TRequestStatus if currently active.
sl@0
   923
The TRequestStatus completes immediately with KErrCancel.
sl@0
   924
*/	
sl@0
   925
EXPORT_C void CA2dpBTHeadsetAudioInterface::CancelNotifyError()
sl@0
   926
	{
sl@0
   927
	User::RequestComplete(iNotifyRequestStatus, KErrCancel);
sl@0
   928
	}
sl@0
   929
sl@0
   930
sl@0
   931
/**
sl@0
   932
Callback from the RTPStreamer when an unexpected event occurs
sl@0
   933
sl@0
   934
@param aError
sl@0
   935
*/
sl@0
   936
void CA2dpBTHeadsetAudioInterface::RTPStreamerEvent(TInt aError)
sl@0
   937
	{
sl@0
   938
	//an error has occured while streaming
sl@0
   939
	__ASSERT_DEBUG((iRTPStreamer), Panic(EA2dpBTHeadsetNoRTPStreamer));
sl@0
   940
	//could also be EGAVDPIdle as we could have just received a GAVDPStateMachineEvent
sl@0
   941
	//which would reset the state to EGAVDPIdle
sl@0
   942
	__ASSERT_DEBUG((iGAVDPStateMachine->State() == TGAVDPState::EGAVDPStreaming) ||
sl@0
   943
	(iGAVDPStateMachine->State() == TGAVDPState::EGAVDPIdle), Panic(EA2dpBTHeadsetUnExpectedState));
sl@0
   944
	TGAVDPState state(TGAVDPState::EGAVDPIdle);
sl@0
   945
	TInt err = iGAVDPStateMachine->ChangeState(state);
sl@0
   946
	//the above returns an error code but there's not much we can do if it returns an error
sl@0
   947
sl@0
   948
	if (iNotifyRequestStatus)
sl@0
   949
		{
sl@0
   950
		if (*iNotifyRequestStatus == KRequestPending)
sl@0
   951
			{
sl@0
   952
			User::RequestComplete(iNotifyRequestStatus, aError);
sl@0
   953
			}
sl@0
   954
		}
sl@0
   955
	}
sl@0
   956
sl@0
   957
sl@0
   958
/**
sl@0
   959
Callback from the GAVDPStateMachine when an error event occurs
sl@0
   960
sl@0
   961
@param aError
sl@0
   962
*/	
sl@0
   963
void  CA2dpBTHeadsetAudioInterface::GAVDPStateMachineEvent(TInt aError)
sl@0
   964
	{
sl@0
   965
	//an error has occured - if an error occurs then it could be
sl@0
   966
	//because we've lost the connection to the headset eg because it is out of range
sl@0
   967
	//has been switched off etc there we assume that the connection has been
sl@0
   968
	//lost and put the GAVDP state back to idle
sl@0
   969
	//note we're simplifying things somewhat as some errors may not be a result 
sl@0
   970
	//of a loss of connection to the headset
sl@0
   971
	TGAVDPState state(TGAVDPState::EGAVDPIdle);
sl@0
   972
	TInt err = iGAVDPStateMachine->ChangeState(state);
sl@0
   973
	if (!iRTPStreamer)
sl@0
   974
		{//if we don't have an RTP streamer
sl@0
   975
		//then close the bearer socket.  If we do have an RTP streamer
sl@0
   976
		//then delay the close till after we have deleted the RTPStreamer in the
sl@0
   977
		//the GAVDPStateChangeComplete callback.  this is because
sl@0
   978
		//closing the bearer socket before the RTP streamer is deleted
sl@0
   979
		//can panic the RTP stack.
sl@0
   980
		iGAVDPStateMachine->BearerSocket().Close();
sl@0
   981
		}	
sl@0
   982
	if (iNotifyRequestStatus)
sl@0
   983
		{
sl@0
   984
		if (*iNotifyRequestStatus == KRequestPending)
sl@0
   985
			{
sl@0
   986
			User::RequestComplete(iNotifyRequestStatus, aError);
sl@0
   987
			}
sl@0
   988
		}
sl@0
   989
	}
sl@0
   990
sl@0
   991
	
sl@0
   992
/**
sl@0
   993
Callback from the GAVDPStateMachine when the headset has suspended the stream
sl@0
   994
*/
sl@0
   995
void  CA2dpBTHeadsetAudioInterface::GAVDPStateMachineStreamSuspendedByRemoteHeadset()
sl@0
   996
	{
sl@0
   997
	if (iRTPStreamer)
sl@0
   998
		{
sl@0
   999
		iRTPStreamer->Pause();
sl@0
  1000
		}
sl@0
  1001
	}
sl@0
  1002
sl@0
  1003
sl@0
  1004
/**
sl@0
  1005
Callback from the GAVDPStateMachine when the headset has resumed a suspended stream
sl@0
  1006
*/	
sl@0
  1007
void  CA2dpBTHeadsetAudioInterface::GAVDPStateMachineStreamResumedByRemoteHeadset()
sl@0
  1008
	{
sl@0
  1009
	if ((!iPaused)&&(iRTPStreamer))
sl@0
  1010
		{
sl@0
  1011
		iRTPStreamer->Resume();
sl@0
  1012
		}
sl@0
  1013
	}
sl@0
  1014
sl@0
  1015
sl@0
  1016
/**
sl@0
  1017
Callback from the GAVDPStateMachine when the headset has reconfigured the parameters
sl@0
  1018
reconfigures codec with new configuration and inform RTP streamer
sl@0
  1019
sl@0
  1020
@return SymbianOS error code
sl@0
  1021
*/
sl@0
  1022
TInt  CA2dpBTHeadsetAudioInterface::GAVDPStateMachineReconfigureByRemoteHeadset()
sl@0
  1023
	{
sl@0
  1024
	//we're only allowing the headset to reconfigure for SBC SEP
sl@0
  1025
	__ASSERT_DEBUG(iA2dpCodecConfiguration->HeadsetCodecDataType() == KMMFFourCCCodeSBC, Panic(	EA2dpBTHeadsetUnexpectedDataType));
sl@0
  1026
	__ASSERT_DEBUG(iCodec,Panic(EA2dpBTHeadsetNoCodec));
sl@0
  1027
	TUid configType;
sl@0
  1028
	TInt err = KErrNone;
sl@0
  1029
	configType.iUid = KMmfUidSBCConfigure;
sl@0
  1030
	TSBCFrameParameters& SBCFrameParameters = iA2dpCodecConfiguration->UpdateLocalSBCCodecConfiguration();
sl@0
  1031
	TPckgBuf<TSBCFrameParameters> SBCFrameParametersBuf(SBCFrameParameters);
sl@0
  1032
	TRAP(err,iCodec->ConfigureL(configType, SBCFrameParametersBuf)); //update codec with new configuration
sl@0
  1033
	iRTPStreamer->SetAudioConfiguration(*iA2dpCodecConfiguration); //and tell RTP streamer	
sl@0
  1034
	return err;
sl@0
  1035
	}
sl@0
  1036
sl@0
  1037
sl@0
  1038
/**
sl@0
  1039
Function to set/release the BT link into low power sniff mode
sl@0
  1040
sl@0
  1041
@internalComponent
sl@0
  1042
@return Error code, this is used for debugging, it's not checked
sl@0
  1043
by the calling code as if SetSniffMode fails there's not much that can be
sl@0
  1044
done in the calling code
sl@0
  1045
*/
sl@0
  1046
TInt CA2dpBTHeadsetAudioInterface::SetSniffMode(TBool aSniffMode)
sl@0
  1047
	{
sl@0
  1048
	TInt err = KErrNone;
sl@0
  1049
	
sl@0
  1050
	if (!iGAVDPStateMachine)
sl@0
  1051
		{
sl@0
  1052
		return KErrNotReady;
sl@0
  1053
		}
sl@0
  1054
		
sl@0
  1055
	if (iGAVDPStateMachine->BTAddress() == TBTDevAddr(0))
sl@0
  1056
		{
sl@0
  1057
		return KErrNotReady;
sl@0
  1058
		}
sl@0
  1059
sl@0
  1060
	err = iBTPhysicalLinkAdapter.Open(iSocketServer, iGAVDPStateMachine->BTAddress());
sl@0
  1061
	if (!err)
sl@0
  1062
		{
sl@0
  1063
		if (aSniffMode)
sl@0
  1064
			{
sl@0
  1065
			err = iBTPhysicalLinkAdapter.AllowLowPowerModes(ESniffMode);
sl@0
  1066
			err = iBTPhysicalLinkAdapter.ActivateSniffRequester();
sl@0
  1067
			}
sl@0
  1068
		else
sl@0
  1069
			{
sl@0
  1070
			err = iBTPhysicalLinkAdapter.CancelLowPowerModeRequester();
sl@0
  1071
			}
sl@0
  1072
		iBTPhysicalLinkAdapter.Close();
sl@0
  1073
		}
sl@0
  1074
sl@0
  1075
	return err;
sl@0
  1076
	}
sl@0
  1077
	
sl@0
  1078
sl@0
  1079
/**
sl@0
  1080
This function should only used by the TSU_MMF_A2DPBLUETOOTH unit test harness
sl@0
  1081
It allows the test harness to get at the MGavdpUser in order
sl@0
  1082
to emulate actions from the headset
sl@0
  1083
sl@0
  1084
@return the GavdpState machine as a MGavdpUser mixin
sl@0
  1085
*/	
sl@0
  1086
EXPORT_C MGavdpUser* CA2dpBTHeadsetAudioInterface::TEST_MGavdpUser()
sl@0
  1087
	{
sl@0
  1088
	return static_cast<MGavdpUser*>(iGAVDPStateMachine);
sl@0
  1089
	}
sl@0
  1090
	
sl@0
  1091
sl@0
  1092
/**
sl@0
  1093
This function should only used by the TSU_MMF_A2DPBLUETOOTH unit test harness
sl@0
  1094
It allows the test harness to override the configuration of the remote headset
sl@0
  1095
for the SBC codec with regard to sampling frequencies, block size; subbands and allocation method
sl@0
  1096
sl@0
  1097
@param
sl@0
  1098
*/
sl@0
  1099
EXPORT_C void CA2dpBTHeadsetAudioInterface::TEST_ForceRemoteSBCCodecConfiguration(const TSBCCodecCapabilities& aRemoteCodecConfiguration)
sl@0
  1100
	{
sl@0
  1101
	iA2dpCodecConfiguration->TEST_ForceRemoteSBCCodecConfiguration(aRemoteCodecConfiguration);
sl@0
  1102
	}