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 <bluetoothav.h>
|
sl@0
|
17 |
#include <mmf/server/mmfcodec.h>
|
sl@0
|
18 |
#include "mmfSBCCodecImplementationUIDs.hrh"
|
sl@0
|
19 |
#include <hal.h>
|
sl@0
|
20 |
#include "A2dpCodecUtilities.h"
|
sl@0
|
21 |
#include "AudioBufferArray.h"
|
sl@0
|
22 |
#include "RTPStreamer.h"
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
/**
|
sl@0
|
26 |
RTP Streamer Panics
|
sl@0
|
27 |
**/
|
sl@0
|
28 |
enum TRTPStreamerPanic
|
sl@0
|
29 |
{
|
sl@0
|
30 |
ERTPStreamerSendPacketMiscount, //0
|
sl@0
|
31 |
ERTPStreamerSendNotCompleted, //1
|
sl@0
|
32 |
ERTPStreamerEmptyBuffer, //2
|
sl@0
|
33 |
ERTPStreamerRTPEventError, //3
|
sl@0
|
34 |
ERTPStreamerIncompleteSBCFrame, //4
|
sl@0
|
35 |
ERTPStreamerUnexpectedEvent, //5
|
sl@0
|
36 |
ERTPStreamerBufferProcessingLengthMismatch, //6
|
sl@0
|
37 |
ERTPStreamerInvalidDataType, //7
|
sl@0
|
38 |
ERTPStreamerNoConfiguration //8
|
sl@0
|
39 |
};
|
sl@0
|
40 |
|
sl@0
|
41 |
|
sl@0
|
42 |
static void Panic(TRTPStreamerPanic aPanic)
|
sl@0
|
43 |
// Panic client
|
sl@0
|
44 |
{
|
sl@0
|
45 |
_LIT(KRTPStreamerPanicName, "RTP Streamer");
|
sl@0
|
46 |
User::Panic(KRTPStreamerPanicName, aPanic);
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
|
sl@0
|
50 |
|
sl@0
|
51 |
/**
|
sl@0
|
52 |
Creates CActiveRTPStreamer
|
sl@0
|
53 |
|
sl@0
|
54 |
@param aSock RSocket that can be used to stream audio to the headset
|
sl@0
|
55 |
@param aRTPStreamObserver mixin used to inform the CA2dpBTHeadsetAudioInterface
|
sl@0
|
56 |
of asynchronous error events
|
sl@0
|
57 |
@return CActiveRTPStreamer*
|
sl@0
|
58 |
*/
|
sl@0
|
59 |
CActiveRTPStreamer* CActiveRTPStreamer::NewL(RSocket& aSock, MRTPStreamerObserver& aRTPStreamerObserver)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
CActiveRTPStreamer* self = new (ELeave) CActiveRTPStreamer (aRTPStreamerObserver);
|
sl@0
|
62 |
CleanupStack::PushL(self);
|
sl@0
|
63 |
self->ConstructL(aSock);
|
sl@0
|
64 |
CleanupStack::Pop(self);
|
sl@0
|
65 |
return self;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
|
sl@0
|
69 |
/**
|
sl@0
|
70 |
Make priortiy high so other RunLs don't impact CTimer accuracy too much
|
sl@0
|
71 |
*/
|
sl@0
|
72 |
CActiveRTPStreamer::CActiveRTPStreamer(MRTPStreamerObserver& aRTPStreamerObserver) : CTimer(EPriorityHigh), iRTPStreamerObserver(aRTPStreamerObserver), iRtpCanSend(ETrue)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
CActiveScheduler::Add(this);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
|
sl@0
|
78 |
CActiveRTPStreamer::~CActiveRTPStreamer()
|
sl@0
|
79 |
{
|
sl@0
|
80 |
Cancel();
|
sl@0
|
81 |
delete iAudioBufferArray;
|
sl@0
|
82 |
|
sl@0
|
83 |
//RTP defect fix DEF57144- RRtpSendSource Cancel()
|
sl@0
|
84 |
//If the line of code below does not compile then your build is too old
|
sl@0
|
85 |
iRTPSendSource.Cancel();
|
sl@0
|
86 |
|
sl@0
|
87 |
iRTPSendSource.Close();
|
sl@0
|
88 |
iRTPSession.Close();
|
sl@0
|
89 |
}
|
sl@0
|
90 |
|
sl@0
|
91 |
|
sl@0
|
92 |
void CActiveRTPStreamer::ConstructL(RSocket& aSock)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
CTimer::ConstructL();
|
sl@0
|
95 |
// get the MTU length limit
|
sl@0
|
96 |
TInt mtu = 0;
|
sl@0
|
97 |
User::LeaveIfError(aSock.GetOpt(EAvdtpMediaGetMaximumPacketSize, KSolBtAVDTPMedia, mtu));
|
sl@0
|
98 |
iMaxMTULength = mtu;//the line above wont accept iMaxMTULength directly because it is unsigned
|
sl@0
|
99 |
iRTPSession.OpenL(aSock, iMaxMTULength);
|
sl@0
|
100 |
iRTPSendSource = iRTPSession.NewSendSourceL();
|
sl@0
|
101 |
|
sl@0
|
102 |
//register callbacks - all terminal callbacks are one shot
|
sl@0
|
103 |
iRTPSession.RegisterEventCallbackL(ERtpAnyEvent,RTPCallback,this);
|
sl@0
|
104 |
iRTPSession.RegisterEventCallbackL(ERtpSessionFail,RTPCallback,this, ERtpOneShot);
|
sl@0
|
105 |
iRTPSession.RegisterEventCallbackL(ERtpBufferOverflow,RTPCallback,this);
|
sl@0
|
106 |
iRTPSession.RegisterEventCallbackL(ERtpUndersizedPacket,RTPCallback,this);
|
sl@0
|
107 |
iRTPSendSource.RegisterEventCallbackL(ERtpAnyEvent,RTPSendSourceCallback,this);
|
sl@0
|
108 |
iRTPSendSource.RegisterEventCallbackL(ERtpSendFail,RTPSendSourceCallback,this, ERtpOneShot);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
|
sl@0
|
112 |
/**
|
sl@0
|
113 |
Function called by the CA2dpBTHeadsetAudioInterface to set the codec and the codec
|
sl@0
|
114 |
settings used by the CActiveRTPStreamer.
|
sl@0
|
115 |
Calling this function forces a recalculation of all the timings the next
|
sl@0
|
116 |
time Send() is called
|
sl@0
|
117 |
|
sl@0
|
118 |
@param aCodec the codec to be used. If this is set to NULL then no codec is used
|
sl@0
|
119 |
only the CSBCCodec can be used here
|
sl@0
|
120 |
@param aConfigType a Uid to identify the aConfigData used to configure the settings
|
sl@0
|
121 |
only KMmfUidSBCConfigure is currently defined. In future other types may be defined
|
sl@0
|
122 |
for mp3, AAC and ATRAC3
|
sl@0
|
123 |
@param aConfigData The configuration data
|
sl@0
|
124 |
@return standard SymbianOS error code
|
sl@0
|
125 |
*/
|
sl@0
|
126 |
void CActiveRTPStreamer::SetCodec(CMMFCodec& aCodec)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
//if there is a codec then it must be SBC else codec must be on the headset
|
sl@0
|
129 |
iCodec = &aCodec;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
|
sl@0
|
133 |
void CActiveRTPStreamer::SetAudioConfiguration(const CA2dpAudioCodecConfiguration& aAudioCodecConfiguration)
|
sl@0
|
134 |
{
|
sl@0
|
135 |
iA2dpAudioCodecConfiguration = const_cast<CA2dpAudioCodecConfiguration*>(&aAudioCodecConfiguration);
|
sl@0
|
136 |
//if there is a new codec configuration then cannot assume the buffer
|
sl@0
|
137 |
//length will be the same so reset
|
sl@0
|
138 |
iBufferLength = 0;
|
sl@0
|
139 |
iBufferParamsInitialized = EFalse;//will result in a call to InitializeForSendL
|
sl@0
|
140 |
iTimeStampIncrement = 0;
|
sl@0
|
141 |
iNumberOfInputBytesToMakeRTPPacket = 0;
|
sl@0
|
142 |
}
|
sl@0
|
143 |
|
sl@0
|
144 |
/**
|
sl@0
|
145 |
Internal function to perform frame size related initialization
|
sl@0
|
146 |
ie creation and setting of the RTPSendPacket audio buffer array.
|
sl@0
|
147 |
Assumes all buffers are the same size (except for the last buffer)
|
sl@0
|
148 |
|
sl@0
|
149 |
@param The length of the audio buffer sent in Send()
|
sl@0
|
150 |
*/
|
sl@0
|
151 |
void CActiveRTPStreamer::InitializeForSendL(const TDesC8& aData)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
iSendBufferSize = aData.Size(); //store buffer length - this shouldn't change till the last buffer
|
sl@0
|
154 |
__ASSERT_DEBUG(iSendBufferSize,Panic(ERTPStreamerEmptyBuffer));
|
sl@0
|
155 |
__ASSERT_DEBUG(iA2dpAudioCodecConfiguration,Panic(ERTPStreamerNoConfiguration));
|
sl@0
|
156 |
TUint encodedBufferSize = iSendBufferSize;
|
sl@0
|
157 |
|
sl@0
|
158 |
if (iCodec)
|
sl@0
|
159 |
{
|
sl@0
|
160 |
iCodec->ResetL(); //clear out any cached data from previous settings
|
sl@0
|
161 |
//if we are using a local codec - ie SBC then we get the frame length
|
sl@0
|
162 |
//and bit rate from the local codec settings
|
sl@0
|
163 |
//since aData will contain pcm16
|
sl@0
|
164 |
iFrameLength = iA2dpAudioCodecConfiguration->LocalSBCCodecConfiguration().CalcFrameLength();
|
sl@0
|
165 |
iBitRate = iA2dpAudioCodecConfiguration->LocalSBCCodecConfiguration().CalcBitRate(iFrameLength)*1000;//*1000 as bitrate is in KHz
|
sl@0
|
166 |
//if we are putting data through the local SBC codec then
|
sl@0
|
167 |
//the encoded buffer size sent to the headset in not the same as the aData buffer in Send()
|
sl@0
|
168 |
encodedBufferSize = iA2dpAudioCodecConfiguration->CalculateSBCBufferLength(iSendBufferSize);
|
sl@0
|
169 |
}
|
sl@0
|
170 |
else
|
sl@0
|
171 |
{
|
sl@0
|
172 |
//if we don't use a local codec then we get the frame legth and bit rate
|
sl@0
|
173 |
//direct from the header
|
sl@0
|
174 |
CA2dpCodecFrameHeaderParser* headerParser = CA2dpCodecFrameHeaderParser::NewL(iA2dpAudioCodecConfiguration->HeadsetCodecDataType(), aData);
|
sl@0
|
175 |
iFrameLength = headerParser->FrameLength();
|
sl@0
|
176 |
iBitRate = headerParser->BitRate();
|
sl@0
|
177 |
delete headerParser;
|
sl@0
|
178 |
}
|
sl@0
|
179 |
iPayloadType = TRTPa2dpCodecSpecificUtils::PayloadType(iA2dpAudioCodecConfiguration->HeadsetCodecDataType());
|
sl@0
|
180 |
|
sl@0
|
181 |
//if the settings have changed then any existing buffered audio buffers
|
sl@0
|
182 |
// will have the old settings so we need to delete the buffer array
|
sl@0
|
183 |
//and recreate from new with the new settings.
|
sl@0
|
184 |
//we also need to cancel in case we are waiting on a RTPSendSourceCallback
|
sl@0
|
185 |
//from a previous send packet
|
sl@0
|
186 |
//RTP defect fix DEF57144- RRtpSendSource Cancel() not in MCL
|
sl@0
|
187 |
//decomment this out when the Cancel method is on the MCL
|
sl@0
|
188 |
//iRTPSendSource.Cancel();
|
sl@0
|
189 |
|
sl@0
|
190 |
delete iAudioBufferArray;
|
sl@0
|
191 |
iAudioBufferArray = NULL;
|
sl@0
|
192 |
//calculate the size of the RTP header
|
sl@0
|
193 |
TUint mediaPayloadHeaderLength = TRTPa2dpCodecSpecificUtils::MediaPayloadHeaderLength(iA2dpAudioCodecConfiguration->HeadsetCodecDataType());
|
sl@0
|
194 |
TUint rtpHeaderLength = KRTPHeaderSize + mediaPayloadHeaderLength;
|
sl@0
|
195 |
iAudioBufferArray = CAudioBufferArray::NewL(iRTPSendSource, KSendBucketSize, encodedBufferSize, iMaxMTULength, rtpHeaderLength, iFrameLength);
|
sl@0
|
196 |
|
sl@0
|
197 |
//determine the payload header
|
sl@0
|
198 |
switch(const_cast<TFourCC&>(iA2dpAudioCodecConfiguration->HeadsetCodecDataType()).FourCC())
|
sl@0
|
199 |
{
|
sl@0
|
200 |
case KMMFFourCCCodeSBC:
|
sl@0
|
201 |
iMediaPayloadHeader.Append(iAudioBufferArray->NumberOfFramesPerRtpPacket());
|
sl@0
|
202 |
break;
|
sl@0
|
203 |
case KMMFFourCCCodeMP3:
|
sl@0
|
204 |
//RFC2250-section 3.5 MBZ+Frag_Offset
|
sl@0
|
205 |
//= 4 bytes all set to 0
|
sl@0
|
206 |
iMediaPayloadHeader.FillZ(4); //0000
|
sl@0
|
207 |
break;
|
sl@0
|
208 |
case KMMFFourCCCodeAAC:
|
sl@0
|
209 |
break;
|
sl@0
|
210 |
case KMMFFourCCCodeATRAC3:
|
sl@0
|
211 |
break;
|
sl@0
|
212 |
default:
|
sl@0
|
213 |
//the datatype is a non A2DP datatype
|
sl@0
|
214 |
//which is not supported so panic
|
sl@0
|
215 |
Panic(ERTPStreamerInvalidDataType);
|
sl@0
|
216 |
break;
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
//get the number of bytes of data sent that was sent to the RTP streamer
|
sl@0
|
220 |
//that make up one RTP packet
|
sl@0
|
221 |
//in the case of a codec this is the value pre codec processing
|
sl@0
|
222 |
if (iCodec)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
iNumberOfInputBytesToMakeRTPPacket = iSendBufferSize/iAudioBufferArray->NumberOfRtpPacketsPerAudioBuffer();
|
sl@0
|
225 |
if (iNumberOfInputBytesToMakeRTPPacket%2)
|
sl@0
|
226 |
{//we have an odd number of bytes
|
sl@0
|
227 |
iNumberOfInputBytesToMakeRTPPacket++;//round up to next byte
|
sl@0
|
228 |
}
|
sl@0
|
229 |
}
|
sl@0
|
230 |
else
|
sl@0
|
231 |
{
|
sl@0
|
232 |
iNumberOfInputBytesToMakeRTPPacket = iAudioBufferArray->InputBytesPerRTPPacket();
|
sl@0
|
233 |
}
|
sl@0
|
234 |
//else other codecs not supported
|
sl@0
|
235 |
|
sl@0
|
236 |
//we set the iFrameDuration which is used to trigger the timer
|
sl@0
|
237 |
//this means that RunL should be called every iFrameDuration and
|
sl@0
|
238 |
//if an RTP packet is ready to sent then it shall be sent
|
sl@0
|
239 |
//since there is no control channel back from the headset, the best
|
sl@0
|
240 |
//we can hope for is to send the data at approx the rate the headset
|
sl@0
|
241 |
//would expect it and hope that the headset provides it's own approriate
|
sl@0
|
242 |
//internal buffering
|
sl@0
|
243 |
//Note that dues to other AOs running the timing is not accurate and
|
sl@0
|
244 |
//usually slower than the specified time - what is really needed
|
sl@0
|
245 |
//here is a feedback loop where the initial time interval is somewhat
|
sl@0
|
246 |
//faster than the calculated time interval and is adjusted against the
|
sl@0
|
247 |
//system clock and bit rate throughput acordingly so the throughput always
|
sl@0
|
248 |
//matches the bit rate.
|
sl@0
|
249 |
iRTPPacketDuration = TTimeIntervalMicroSeconds32(TFrameTimingUtils::FrameDuration(iFrameLength,iBitRate).Int() * iAudioBufferArray->NumberOfFramesPerRtpPacket());
|
sl@0
|
250 |
RDebug::Printf("RTP Packet Duration = %d mS", iRTPPacketDuration.Int());
|
sl@0
|
251 |
TInt fastCounterFrequency;
|
sl@0
|
252 |
HAL::Get(HALData::EFastCounterFrequency,fastCounterFrequency);
|
sl@0
|
253 |
RDebug::Printf("sys clock timing frequency = %d Hz", fastCounterFrequency);
|
sl@0
|
254 |
iTimeStampIncrement = TFrameTimingUtils::TimeStampIncrementPerFrame(iA2dpAudioCodecConfiguration->HeadsetCodecDataType(), iFrameLength, iBitRate, iA2dpAudioCodecConfiguration->SampleRate())
|
sl@0
|
255 |
* iAudioBufferArray->NumberOfFramesPerRtpPacket();
|
sl@0
|
256 |
RDebug::Printf("Calculated RTP packet time stamp increment = %d",iTimeStampIncrement);
|
sl@0
|
257 |
RDebug::Printf("FrameLength = %d", iFrameLength);
|
sl@0
|
258 |
RDebug::Printf("Calculated bitRate = %d", iBitRate);
|
sl@0
|
259 |
RDebug::Printf("Number of frames per RTP packet = %d", iAudioBufferArray->NumberOfFramesPerRtpPacket());
|
sl@0
|
260 |
RDebug::Printf("Number of RTP packets per audio buffer = %d", iAudioBufferArray->NumberOfRtpPacketsPerAudioBuffer());
|
sl@0
|
261 |
RDebug::Printf("Sample rate = %d", iA2dpAudioCodecConfiguration->SampleRate());
|
sl@0
|
262 |
}
|
sl@0
|
263 |
|
sl@0
|
264 |
|
sl@0
|
265 |
/**
|
sl@0
|
266 |
Internal function to pass the pcm16 audio data in aData and use the codec
|
sl@0
|
267 |
to process the data back in aPayload
|
sl@0
|
268 |
|
sl@0
|
269 |
@return the number of source bytes processed
|
sl@0
|
270 |
*/
|
sl@0
|
271 |
TUint CActiveRTPStreamer::CodecProcessPayloadL(const TDesC8& aData,TDes8& aPayload)
|
sl@0
|
272 |
{
|
sl@0
|
273 |
TPtr8 srcBufferPtr(const_cast<TUint8*>(aData.Ptr()),aData.Length());
|
sl@0
|
274 |
srcBufferPtr.SetLength(aData.Length());
|
sl@0
|
275 |
//+1 -1 to skip SBC media payload header
|
sl@0
|
276 |
TPtr8 dstBufferPtr(const_cast<TUint8*>(aPayload.Ptr()+1),aPayload.MaxLength()-1);
|
sl@0
|
277 |
CMMFPtrBuffer* srcBuffer = CMMFPtrBuffer::NewL(srcBufferPtr);
|
sl@0
|
278 |
CleanupStack::PushL(srcBuffer);
|
sl@0
|
279 |
CMMFPtrBuffer* dstBuffer = CMMFPtrBuffer::NewL(dstBufferPtr);
|
sl@0
|
280 |
CleanupStack::PushL(dstBuffer);
|
sl@0
|
281 |
TCodecProcessResult result = iCodec->ProcessL(*srcBuffer,*dstBuffer);
|
sl@0
|
282 |
if (result == TCodecProcessResult::EProcessIncomplete)
|
sl@0
|
283 |
{
|
sl@0
|
284 |
User::Leave(KErrArgument);
|
sl@0
|
285 |
}
|
sl@0
|
286 |
aPayload.Append(dstBuffer->Data());
|
sl@0
|
287 |
CleanupStack::PopAndDestroy(dstBuffer);
|
sl@0
|
288 |
CleanupStack::PopAndDestroy(srcBuffer);
|
sl@0
|
289 |
return result.iSrcBytesProcessed;
|
sl@0
|
290 |
}
|
sl@0
|
291 |
|
sl@0
|
292 |
|
sl@0
|
293 |
/**
|
sl@0
|
294 |
This is the main function for CActiveRTPStreamer in that it is the
|
sl@0
|
295 |
function used to send data to the headset over RTP.
|
sl@0
|
296 |
The function is asynchronous to the RunL which does the actual sending.
|
sl@0
|
297 |
The data is stored in the CRtpSendPackets FIFO and will be sent at the
|
sl@0
|
298 |
next RunL provided the RTP can accept the data. If not it just stays
|
sl@0
|
299 |
buffered in the CRtpSendPackets FIFO until it can be sent.
|
sl@0
|
300 |
The request status is completed when the buffer is stored, not when it is sent
|
sl@0
|
301 |
this is to more closely mimic the behaviour of the sound driver.
|
sl@0
|
302 |
If adding the buffer to the CRtpSendPackets FIFO causes it to be full
|
sl@0
|
303 |
then the request status won't be completed until there is space in the FIFO
|
sl@0
|
304 |
which won't be until a callback from the RTP stack has been received
|
sl@0
|
305 |
indicating that the CRtpSendPackets FIFO can now discard that entry.
|
sl@0
|
306 |
Only one Send at a time is accepted ie the request status
|
sl@0
|
307 |
of the previous send must be completed before Send can be called again.
|
sl@0
|
308 |
To simplify the software and improve performance, fixed sized buffers are assumed.ie
|
sl@0
|
309 |
the buffer length is only calculated once on the first frame and when the settings
|
sl@0
|
310 |
change.
|
sl@0
|
311 |
|
sl@0
|
312 |
@param aData The data to be sent to the headset. This may go via
|
sl@0
|
313 |
a codec eg if the data is pcm16 or sent directly to the headset if the data is SBC,mp3,AAC,ATRAC3
|
sl@0
|
314 |
It is the responsibility of the client ie CA2dpBTHeadsetAudioInterface to
|
sl@0
|
315 |
call SetCodecConfiguration first.
|
sl@0
|
316 |
|
sl@0
|
317 |
@param aStatus
|
sl@0
|
318 |
*/
|
sl@0
|
319 |
void CActiveRTPStreamer::Send(const TDesC8& aData, TRequestStatus& aStatus)
|
sl@0
|
320 |
{
|
sl@0
|
321 |
if (iSendStatus)
|
sl@0
|
322 |
{
|
sl@0
|
323 |
__ASSERT_DEBUG((*iSendStatus != KRequestPending),Panic(ERTPStreamerSendNotCompleted));
|
sl@0
|
324 |
}
|
sl@0
|
325 |
iSendStatus = &aStatus;
|
sl@0
|
326 |
*iSendStatus = KRequestPending;
|
sl@0
|
327 |
|
sl@0
|
328 |
if (iUnrecoverableError)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
User::RequestComplete(iSendStatus,iUnrecoverableError);
|
sl@0
|
331 |
return;
|
sl@0
|
332 |
}
|
sl@0
|
333 |
|
sl@0
|
334 |
if (!iBufferParamsInitialized)
|
sl@0
|
335 |
{
|
sl@0
|
336 |
TRAPD(err,InitializeForSendL(aData));
|
sl@0
|
337 |
if (err)
|
sl@0
|
338 |
{
|
sl@0
|
339 |
User::RequestComplete(iSendStatus,err);
|
sl@0
|
340 |
return;
|
sl@0
|
341 |
}
|
sl@0
|
342 |
iBufferParamsInitialized = ETrue;
|
sl@0
|
343 |
}
|
sl@0
|
344 |
|
sl@0
|
345 |
TUint numberOfRtpPacketsPerAudioBuffer = iAudioBufferArray->NumberOfRtpPacketsPerAudioBuffer();
|
sl@0
|
346 |
|
sl@0
|
347 |
if (aData.Size() != iSendBufferSize)
|
sl@0
|
348 |
{
|
sl@0
|
349 |
//then we are on the last buffer
|
sl@0
|
350 |
//in which case we need to recalculate the number of Rtp packets
|
sl@0
|
351 |
//required to make up the audio frame since the last buffer
|
sl@0
|
352 |
//is smaller.
|
sl@0
|
353 |
TUint lastBufferLength = aData.Size();
|
sl@0
|
354 |
if (iCodec)
|
sl@0
|
355 |
{
|
sl@0
|
356 |
lastBufferLength = iA2dpAudioCodecConfiguration->CalculateSBCBufferLength(aData.Size());
|
sl@0
|
357 |
}
|
sl@0
|
358 |
//we keep the same number of RTP packets per audio buffer as before
|
sl@0
|
359 |
TUint numberOfSBCFramesInLastBuffer = lastBufferLength/iFrameLength;
|
sl@0
|
360 |
TUint numberOfFramesPerRtpPacket = iAudioBufferArray->NumberOfFramesPerRtpPacket();
|
sl@0
|
361 |
|
sl@0
|
362 |
//the devisor below may not always devide without a remainder
|
sl@0
|
363 |
//which means the last Rtp packet sent will not be full
|
sl@0
|
364 |
//if we have a remainder then we need another Rtp packet
|
sl@0
|
365 |
numberOfRtpPacketsPerAudioBuffer = numberOfSBCFramesInLastBuffer/numberOfFramesPerRtpPacket;
|
sl@0
|
366 |
if (numberOfSBCFramesInLastBuffer%numberOfFramesPerRtpPacket)
|
sl@0
|
367 |
{
|
sl@0
|
368 |
numberOfRtpPacketsPerAudioBuffer++;
|
sl@0
|
369 |
}
|
sl@0
|
370 |
}
|
sl@0
|
371 |
|
sl@0
|
372 |
|
sl@0
|
373 |
CRtpSendPackets* sendPackets = iAudioBufferArray->CurrentAudioBufferRtpSendPackets();
|
sl@0
|
374 |
TUint srcBytesProcessed = 0;
|
sl@0
|
375 |
for (TUint i=0; i<numberOfRtpPacketsPerAudioBuffer;i++)
|
sl@0
|
376 |
{
|
sl@0
|
377 |
RDebug::Printf("NewRTPPacketReceived %d",User::FastCounter());
|
sl@0
|
378 |
RRtpSendPacket& sendPacket = sendPackets->Packet(i);
|
sl@0
|
379 |
sendPacket.SetPayloadType(iPayloadType);
|
sl@0
|
380 |
sendPacket.SetTimestamp(iTimeStamp);
|
sl@0
|
381 |
iTimeStamp += iTimeStampIncrement;
|
sl@0
|
382 |
TDes8& payload = sendPacket.WritePayload();
|
sl@0
|
383 |
payload.Zero();
|
sl@0
|
384 |
payload.Append(iMediaPayloadHeader);
|
sl@0
|
385 |
|
sl@0
|
386 |
//aData may have to be sent as multiple packets
|
sl@0
|
387 |
TUint8* sourceDataOffset = const_cast<TUint8*>(aData.Ptr())+srcBytesProcessed;
|
sl@0
|
388 |
TPtr8 srcBufferPtr(sourceDataOffset,iNumberOfInputBytesToMakeRTPPacket);
|
sl@0
|
389 |
TUint srcBytesStillRemaining = aData.Size() - srcBytesProcessed;
|
sl@0
|
390 |
TUint lengthOfsrcBuffer = iNumberOfInputBytesToMakeRTPPacket;
|
sl@0
|
391 |
if (srcBytesStillRemaining < lengthOfsrcBuffer )
|
sl@0
|
392 |
{//probably a last buffer condition or modulo 2 pcm16 rounding condition
|
sl@0
|
393 |
lengthOfsrcBuffer = srcBytesStillRemaining;
|
sl@0
|
394 |
}
|
sl@0
|
395 |
srcBufferPtr.SetLength(lengthOfsrcBuffer);
|
sl@0
|
396 |
|
sl@0
|
397 |
//sanity check - the following should always be true
|
sl@0
|
398 |
__ASSERT_DEBUG((srcBytesProcessed == iNumberOfInputBytesToMakeRTPPacket*i),Panic(ERTPStreamerBufferProcessingLengthMismatch));
|
sl@0
|
399 |
|
sl@0
|
400 |
if (iCodec)
|
sl@0
|
401 |
{
|
sl@0
|
402 |
TRAPD(err,srcBytesProcessed += CodecProcessPayloadL(srcBufferPtr,payload));
|
sl@0
|
403 |
if (err)
|
sl@0
|
404 |
{
|
sl@0
|
405 |
//something has gone wrong so abort streaming
|
sl@0
|
406 |
User::RequestComplete(iSendStatus,err);
|
sl@0
|
407 |
return;
|
sl@0
|
408 |
}
|
sl@0
|
409 |
}
|
sl@0
|
410 |
else //no need to process via codec - aData can go straight to the headset
|
sl@0
|
411 |
{
|
sl@0
|
412 |
srcBytesProcessed +=lengthOfsrcBuffer;
|
sl@0
|
413 |
payload.Append(srcBufferPtr);
|
sl@0
|
414 |
}
|
sl@0
|
415 |
}// for (TUint i=0; i<iAudioBufferArray->NumberOfRtpPacketsPerAudioBuffer();i++)
|
sl@0
|
416 |
iAudioBufferArray->CurrentAudioBufferReadyToSend();
|
sl@0
|
417 |
//else we have no more send packets so cannot complete request status
|
sl@0
|
418 |
//until one of the send packets has been sent and acknowledged.
|
sl@0
|
419 |
|
sl@0
|
420 |
|
sl@0
|
421 |
//we'll send an event to ourselves and either send the packet if we can
|
sl@0
|
422 |
//we could complete the iSendStatus TRequestStatus here before returing
|
sl@0
|
423 |
//from the Send but we won't in order to more closely mimic the existing
|
sl@0
|
424 |
// sound driver PlayData behaviour
|
sl@0
|
425 |
//if there is already a request active then it will be the timer
|
sl@0
|
426 |
//ie this will effectively kick things off if the timer is not running
|
sl@0
|
427 |
TRequestStatus* stat = &iStatus;
|
sl@0
|
428 |
if (!IsActive())
|
sl@0
|
429 |
{
|
sl@0
|
430 |
User::RequestComplete(stat, KErrNone);
|
sl@0
|
431 |
SetActive();
|
sl@0
|
432 |
}
|
sl@0
|
433 |
}
|
sl@0
|
434 |
|
sl@0
|
435 |
|
sl@0
|
436 |
/**
|
sl@0
|
437 |
Function to stop further internally buffered packets being sent to the headset
|
sl@0
|
438 |
*/
|
sl@0
|
439 |
void CActiveRTPStreamer::Pause()
|
sl@0
|
440 |
{
|
sl@0
|
441 |
iPaused = ETrue;
|
sl@0
|
442 |
Cancel();
|
sl@0
|
443 |
}
|
sl@0
|
444 |
|
sl@0
|
445 |
|
sl@0
|
446 |
/**
|
sl@0
|
447 |
Function called after pause to resume sending buffers to the headset
|
sl@0
|
448 |
*/
|
sl@0
|
449 |
void CActiveRTPStreamer::Resume()
|
sl@0
|
450 |
{
|
sl@0
|
451 |
if (iPaused)
|
sl@0
|
452 |
{
|
sl@0
|
453 |
iPaused = EFalse;
|
sl@0
|
454 |
TRequestStatus* stat = &iStatus;
|
sl@0
|
455 |
if (!IsActive())
|
sl@0
|
456 |
{
|
sl@0
|
457 |
User::RequestComplete(stat, KErrNone);
|
sl@0
|
458 |
SetActive();
|
sl@0
|
459 |
}
|
sl@0
|
460 |
}
|
sl@0
|
461 |
}
|
sl@0
|
462 |
|
sl@0
|
463 |
|
sl@0
|
464 |
/**
|
sl@0
|
465 |
Function called from CA2dpBTHeadsetAudioInterface::CancelPlayData()
|
sl@0
|
466 |
Used to cancel an outstanding status request for a Send()
|
sl@0
|
467 |
*/
|
sl@0
|
468 |
void CActiveRTPStreamer::CancelLastSendBuffer()
|
sl@0
|
469 |
{
|
sl@0
|
470 |
if (iSendStatus)
|
sl@0
|
471 |
{
|
sl@0
|
472 |
if (*iSendStatus == KRequestPending)//make sure there is a pending request to cancel
|
sl@0
|
473 |
{
|
sl@0
|
474 |
iAudioBufferArray->CancelMostRecentAudioBuffer(!iRtpCanSend);
|
sl@0
|
475 |
User::RequestComplete(iSendStatus, KErrCancel);
|
sl@0
|
476 |
}
|
sl@0
|
477 |
}
|
sl@0
|
478 |
}
|
sl@0
|
479 |
|
sl@0
|
480 |
|
sl@0
|
481 |
/**
|
sl@0
|
482 |
Function to flush out the bufferes stored in CRtpSenPackets
|
sl@0
|
483 |
*/
|
sl@0
|
484 |
void CActiveRTPStreamer::FlushPendingSendBuffers()
|
sl@0
|
485 |
{
|
sl@0
|
486 |
iAudioBufferArray->FlushPendingPackets();
|
sl@0
|
487 |
|
sl@0
|
488 |
if(iCodec)
|
sl@0
|
489 |
{//flush out codec cache
|
sl@0
|
490 |
TRAP_IGNORE(iCodec->ResetL());
|
sl@0
|
491 |
}
|
sl@0
|
492 |
}
|
sl@0
|
493 |
|
sl@0
|
494 |
|
sl@0
|
495 |
/**
|
sl@0
|
496 |
Function to return total number of bytes sent prior to codec processing
|
sl@0
|
497 |
ie bytes of pcm16 not SBC
|
sl@0
|
498 |
Note this the number of bytes sent is only updated when the packet
|
sl@0
|
499 |
has been acknowledged as being sent correctly by the RTP stack
|
sl@0
|
500 |
ie this value will always be slightly less than the bytes sent in Send()
|
sl@0
|
501 |
*/
|
sl@0
|
502 |
TUint CActiveRTPStreamer::BytesSent() const
|
sl@0
|
503 |
{
|
sl@0
|
504 |
return iBytesSent;
|
sl@0
|
505 |
}
|
sl@0
|
506 |
|
sl@0
|
507 |
|
sl@0
|
508 |
/**
|
sl@0
|
509 |
Function to reset the number of bytes sent
|
sl@0
|
510 |
*/
|
sl@0
|
511 |
void CActiveRTPStreamer::ResetBytesSent()
|
sl@0
|
512 |
{
|
sl@0
|
513 |
iBytesSent = 0;
|
sl@0
|
514 |
}
|
sl@0
|
515 |
|
sl@0
|
516 |
|
sl@0
|
517 |
/**
|
sl@0
|
518 |
The RunL is called every frame duration interval.
|
sl@0
|
519 |
It checks to see if there are any packets to be sent to the headset
|
sl@0
|
520 |
and if so send it.
|
sl@0
|
521 |
One issue to be resolved at integration testing is if there are no packets
|
sl@0
|
522 |
to send then this is analogous to a KErrUnderflow condition on the
|
sl@0
|
523 |
sound driver. Do we need to mimic this behaviour for the a2dp interface?
|
sl@0
|
524 |
|
sl@0
|
525 |
The Send request status is completed if there is room in the CRtpSendPackets
|
sl@0
|
526 |
for another buffer.
|
sl@0
|
527 |
*/
|
sl@0
|
528 |
void CActiveRTPStreamer::RunL()
|
sl@0
|
529 |
{
|
sl@0
|
530 |
if ((iPaused)||(!iAudioBufferArray))
|
sl@0
|
531 |
{
|
sl@0
|
532 |
return;
|
sl@0
|
533 |
}
|
sl@0
|
534 |
|
sl@0
|
535 |
if(iRtpCanSend && iAudioBufferArray->NumberOfAudioBuffersReadyToSend())
|
sl@0
|
536 |
{
|
sl@0
|
537 |
RRtpSendPacket& sendPacket = iAudioBufferArray->CurrentSendPacket();
|
sl@0
|
538 |
sendPacket.Send();
|
sl@0
|
539 |
iRtpCanSend = EFalse; //have to wait for callback before we can send again
|
sl@0
|
540 |
}
|
sl@0
|
541 |
|
sl@0
|
542 |
if (iSendStatus)
|
sl@0
|
543 |
{
|
sl@0
|
544 |
if ((iAudioBufferArray->NumberOfAudioBuffersReadyToSend() < KSendBucketSize)
|
sl@0
|
545 |
&&(*iSendStatus == KRequestPending))
|
sl@0
|
546 |
{//still some free packets to fill so complete request status
|
sl@0
|
547 |
User::RequestComplete(iSendStatus, KErrNone);
|
sl@0
|
548 |
iSendStatus = NULL;
|
sl@0
|
549 |
}
|
sl@0
|
550 |
//else if the iRtpSendPackets FIFO is full then we can't complete
|
sl@0
|
551 |
//the request status until we've had an ERtpSendSucceeded event
|
sl@0
|
552 |
}
|
sl@0
|
553 |
//are there any more buffers that are ready to send?
|
sl@0
|
554 |
//if so then send the next packet after a time delay
|
sl@0
|
555 |
//keep calling this RunL every frame duration till as long as we have packets to send
|
sl@0
|
556 |
//if there are no packets ready to send then we need to wait
|
sl@0
|
557 |
//for another call to Send();
|
sl@0
|
558 |
if (iAudioBufferArray->NumberOfAudioBuffersReadyToSend())
|
sl@0
|
559 |
{//there are packets ready to send so fire off next RunL after one RTP packet duration
|
sl@0
|
560 |
RDebug::Printf("RTPPacket Sent %d",User::FastCounter());
|
sl@0
|
561 |
After(iRTPPacketDuration);
|
sl@0
|
562 |
}
|
sl@0
|
563 |
}
|
sl@0
|
564 |
|
sl@0
|
565 |
|
sl@0
|
566 |
/**
|
sl@0
|
567 |
Cancel
|
sl@0
|
568 |
*/
|
sl@0
|
569 |
void CActiveRTPStreamer::DoCancel()
|
sl@0
|
570 |
{
|
sl@0
|
571 |
CTimer::DoCancel();
|
sl@0
|
572 |
CompleteSendRequestStatus(KErrCancel);
|
sl@0
|
573 |
}
|
sl@0
|
574 |
|
sl@0
|
575 |
|
sl@0
|
576 |
/**
|
sl@0
|
577 |
Utility function to complete Send TRequestStatus with aError
|
sl@0
|
578 |
*/
|
sl@0
|
579 |
void CActiveRTPStreamer::CompleteSendRequestStatus(TInt aError)
|
sl@0
|
580 |
{
|
sl@0
|
581 |
if (iSendStatus)
|
sl@0
|
582 |
{
|
sl@0
|
583 |
if (*iSendStatus == KRequestPending)
|
sl@0
|
584 |
{
|
sl@0
|
585 |
User::RequestComplete(iSendStatus, aError);
|
sl@0
|
586 |
}
|
sl@0
|
587 |
}
|
sl@0
|
588 |
}
|
sl@0
|
589 |
|
sl@0
|
590 |
|
sl@0
|
591 |
/**
|
sl@0
|
592 |
Called by RTP stack when a packet has been sent
|
sl@0
|
593 |
If the packet was sent ok then complete the iSendStatus if it is pending
|
sl@0
|
594 |
and update the number of bytes sent
|
sl@0
|
595 |
If the packet was not sent ok then the error is regarded as unrecoverable
|
sl@0
|
596 |
since this should not happen. If it does happen then the CA2dpBTHeadsetAudioInterface
|
sl@0
|
597 |
is informed. If there is an outstanding Send TRequestStatus then this is
|
sl@0
|
598 |
completed with KErrCommsFrame. Not sure if this is the most appropriate error code?
|
sl@0
|
599 |
*/
|
sl@0
|
600 |
void CActiveRTPStreamer::PacketSent(TRtpEventType aEvent)
|
sl@0
|
601 |
{
|
sl@0
|
602 |
if (aEvent == ERtpSendSucceeded)
|
sl@0
|
603 |
{
|
sl@0
|
604 |
RDebug::Printf("Sent RTPPacket Acknowledged %d",User::FastCounter());
|
sl@0
|
605 |
TBool entireAudioBufferSent = EFalse;
|
sl@0
|
606 |
iAudioBufferArray->CurrentSendPacketSent(entireAudioBufferSent);
|
sl@0
|
607 |
//check if there is an outstanding send request status
|
sl@0
|
608 |
//we can only complete the send request status if all the
|
sl@0
|
609 |
//RTP packets in the audio buffer have been sent.
|
sl@0
|
610 |
if (entireAudioBufferSent)
|
sl@0
|
611 |
{
|
sl@0
|
612 |
CompleteSendRequestStatus(KErrNone);
|
sl@0
|
613 |
}
|
sl@0
|
614 |
iRtpCanSend = ETrue;
|
sl@0
|
615 |
iBytesSent += iNumberOfInputBytesToMakeRTPPacket;
|
sl@0
|
616 |
}
|
sl@0
|
617 |
else if (aEvent == ERtpSendFail)
|
sl@0
|
618 |
{
|
sl@0
|
619 |
//if we fail to send the packet then chances are something
|
sl@0
|
620 |
//has gone wrong that may not be recoverable
|
sl@0
|
621 |
//so we will complete the request status and halt further streaming
|
sl@0
|
622 |
//some testing may be required to see if it is possible to
|
sl@0
|
623 |
//recover in which case the packet could be sent again
|
sl@0
|
624 |
iUnrecoverableError = KErrCommsFrame;//probably the nearest error code
|
sl@0
|
625 |
CompleteSendRequestStatus(iUnrecoverableError);
|
sl@0
|
626 |
//inform iRTPStreamerObserver ie the CA2dpBTHeadsetAudioInterface
|
sl@0
|
627 |
//this will initiate a GAVDP state machine reset which will destroy the CActiveRTPStreamer
|
sl@0
|
628 |
iRTPStreamerObserver.RTPStreamerEvent(iUnrecoverableError);
|
sl@0
|
629 |
}
|
sl@0
|
630 |
else
|
sl@0
|
631 |
{//we've not registered for any other events so this shouldn't happen
|
sl@0
|
632 |
Panic(ERTPStreamerUnexpectedEvent);
|
sl@0
|
633 |
}
|
sl@0
|
634 |
}
|
sl@0
|
635 |
|
sl@0
|
636 |
|
sl@0
|
637 |
/**
|
sl@0
|
638 |
Called by RTP stack when some sort of general error has occured
|
sl@0
|
639 |
eg switching off the headset, or the headset going out of range
|
sl@0
|
640 |
The CA2dpBTHeadsetAudioInterface is informed.
|
sl@0
|
641 |
*/
|
sl@0
|
642 |
void CActiveRTPStreamer::RTPSessionEvent(const TRtpEvent& aEvent)
|
sl@0
|
643 |
{
|
sl@0
|
644 |
switch(aEvent.Type())
|
sl@0
|
645 |
{
|
sl@0
|
646 |
case ERtpSessionFail:
|
sl@0
|
647 |
iUnrecoverableError = KErrDisconnected;
|
sl@0
|
648 |
break;
|
sl@0
|
649 |
case ERtpBufferOverflow:
|
sl@0
|
650 |
iUnrecoverableError = KErrOverflow;
|
sl@0
|
651 |
break;
|
sl@0
|
652 |
case ERtpUndersizedPacket:
|
sl@0
|
653 |
iUnrecoverableError = KErrCommsFrame;
|
sl@0
|
654 |
break;
|
sl@0
|
655 |
default:
|
sl@0
|
656 |
Panic(ERTPStreamerRTPEventError); //we haven't registered for anything else
|
sl@0
|
657 |
break;
|
sl@0
|
658 |
}
|
sl@0
|
659 |
|
sl@0
|
660 |
//complete outstanding Send (CA2dpBTHeadsetAudioInterface::PlayData) request status
|
sl@0
|
661 |
CompleteSendRequestStatus(iUnrecoverableError);
|
sl@0
|
662 |
//inform CA2dpBTHeadsetAudioInterface
|
sl@0
|
663 |
iRTPStreamerObserver.RTPStreamerEvent(iUnrecoverableError);
|
sl@0
|
664 |
}
|
sl@0
|
665 |
|
sl@0
|
666 |
|
sl@0
|
667 |
/**
|
sl@0
|
668 |
Static callback from RTP stack
|
sl@0
|
669 |
*/
|
sl@0
|
670 |
void CActiveRTPStreamer::RTPSendSourceCallback(CActiveRTPStreamer* aStreamer, const TRtpEvent& aEvent)
|
sl@0
|
671 |
{
|
sl@0
|
672 |
__ASSERT_DEBUG((aEvent.IsSendSourceEvent()),Panic(ERTPStreamerRTPEventError));
|
sl@0
|
673 |
__ASSERT_DEBUG((aEvent.SendSource() == aStreamer->iRTPSendSource),Panic(ERTPStreamerRTPEventError));
|
sl@0
|
674 |
// for now assume it was sending complete
|
sl@0
|
675 |
// do next bit
|
sl@0
|
676 |
aStreamer->PacketSent(aEvent.Type());
|
sl@0
|
677 |
}
|
sl@0
|
678 |
|
sl@0
|
679 |
|
sl@0
|
680 |
/**
|
sl@0
|
681 |
Static callback from RTP stack
|
sl@0
|
682 |
*/
|
sl@0
|
683 |
void CActiveRTPStreamer::RTPCallback(CActiveRTPStreamer* aStreamer, const TRtpEvent& aEvent)
|
sl@0
|
684 |
{
|
sl@0
|
685 |
__ASSERT_DEBUG((aEvent.IsSessionEvent()),Panic(ERTPStreamerRTPEventError));
|
sl@0
|
686 |
__ASSERT_DEBUG((aEvent.Session() == aStreamer->iRTPSession),Panic(ERTPStreamerRTPEventError));
|
sl@0
|
687 |
|
sl@0
|
688 |
aStreamer->RTPSessionEvent(aEvent);
|
sl@0
|
689 |
}
|