First public contribution.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "char_a3f_devsound_testbase.h"
17 #include "char_a3f_devsound_playrecordclients.h"
18 #include "char_a3f_devsound_toneclient.h"
20 const TUint KRateLookup[] =
23 EMMFSampleRate11025Hz,
24 EMMFSampleRate16000Hz,
25 EMMFSampleRate22050Hz,
26 EMMFSampleRate32000Hz,
27 EMMFSampleRate44100Hz,
28 EMMFSampleRate48000Hz,
29 EMMFSampleRate88200Hz,
30 EMMFSampleRate96000Hz,
31 EMMFSampleRate12000Hz,
32 EMMFSampleRate24000Hz,
36 const TUint KChannelsLookup[] =
42 RA3FDevSoundTestBase::RA3FDevSoundTestBase(const TDesC& aTestName)
43 : iFourCCString(KNullDesC),
44 iFourCCCode(KMMFFourCCCodeNULL),
45 iDevSoundState(EStateInitial),
46 iDevsoundToneClient(NULL),
47 iDevsoundPlayClient(NULL),
48 iDevsoundRecordClient(NULL),
50 iTimer(NULL), iBuffer(NULL)
52 iTestStepName = aTestName;
55 void RA3FDevSoundTestBase::KickoffTestL()
57 INFO_PRINTF1(_L("__________ Creating DevSound object ___________"));
59 // Create devsound object
60 TRAPD(err, iMMFDevSound = CMMFDevSound::NewL());
63 ERR_PRINTF2(_L("Could not create DevSound object. Error = %d"), err);
67 INFO_PRINTF1(_L("DevSound State: EStateCreated"));
68 iDevSoundState = EStateCreated;
73 ERR_PRINTF2(_L("Could not connect to Filesystem. Error = %d"), err);
78 INFO_PRINTF1(_L("DevSound Event: EEventInitialize"));
79 Fsm(EEventInitialize, KErrNone);
82 void RA3FDevSoundTestBase::CloseTest()
86 INFO_PRINTF1(KMsgDeleteDevsound);
93 if(iDevsoundToneClient)
95 delete iDevsoundToneClient;
97 if(iDevsoundPlayClient)
99 delete iDevsoundPlayClient;
101 if(iDevsoundRecordClient)
103 delete iDevsoundRecordClient;
105 if (iAsyncWriteBTFAO)
107 delete iAsyncWriteBTFAO;
108 iAsyncWriteBTFAO = NULL;
120 void RA3FDevSoundTestBase::CallStopTest(TInt aError)
126 void RA3FDevSoundTestBase::StartTimer(TTimeIntervalMicroSeconds32 aWaitTime)
128 TTimeIntervalMicroSeconds32 timeInterval;
130 if(aWaitTime <= TTimeIntervalMicroSeconds32(0))
132 timeInterval = KMicroSecsInOneSec;
136 timeInterval = aWaitTime;
138 TCallBack callback (TimerCallback, this);
139 iTimer->Start(timeInterval, timeInterval, callback);
140 INFO_PRINTF1(_L("Timer has been started"));
143 TInt RA3FDevSoundTestBase::TimerCallback(TAny* aPtr)
145 static_cast<RA3FDevSoundTestBase*>(aPtr)->DoTimerCallback();
149 void RA3FDevSoundTestBase::DoTimerCallback()
151 __ASSERT_ALWAYS(0, Panic(_L("RA3FDevSoundTestBase"), EInvalidCallbackCall));
154 void RA3FDevSoundTestBase::EncodingFromStringToTFourCC(const TDesC& aFourCCString)
156 iFourCCString.Copy(aFourCCString);
157 if(aFourCCString.Length() <= KTFourCC)
159 while( iFourCCString.Length() < KTFourCC )
161 iFourCCString.Insert(0, _L(" "));
163 iFourCCCode = TFourCC(iFourCCString[3] << 24 | iFourCCString[2] << 16 | iFourCCString[1] << 8 | iFourCCString[0]);
167 ERR_PRINTF2(KMsgErrorFourccLength,iFourCCString.Length());
168 StopTest(KErrUnknown);
172 void RA3FDevSoundTestBase::InitializeComplete(TInt aError)
174 INFO_PRINTF1(_L("========== DevSound InitializeComplete() callback =========="));
175 INFO_PRINTF3(KMsgErrorDevSoundCallback, &KInitializeCompleteText, aError);
176 if(iDevSoundState == EStateInitializing)
178 INFO_PRINTF1(_L("DevSound Event: EEventInitComplete"));
179 Fsm(EEventInitComplete, aError);
183 void RA3FDevSoundTestBase::ToneFinished(TInt aError)
185 INFO_PRINTF1(_L("========== DevSound ToneFinished() callback =========="));
186 if (aError == KErrUnderflow)
188 INFO_PRINTF2(_L("DevSound called CMMFDevSound::ToneFinished with error = %d as expected"), aError);
189 StopTest(aError,EPass);
193 ERR_PRINTF2(_L("DevSound called CMMFDevSound::ToneFinished with error = %d"), aError);
194 ERR_PRINTF2(_L("Expected error = %d"), KErrUnderflow);
199 void RA3FDevSoundTestBase::BufferToBeFilled(CMMFBuffer* aBuffer)
201 INFO_PRINTF1(_L("========== DevSound BufferToBeFilled() callback =========="));
204 ERR_PRINTF1(_L("BufferToBeFilled callback received a NULL CMMFBuffer!"));
205 StopTest(KErrGeneral);
210 INFO_PRINTF1(_L("DevSound Event: EEventBTBF"));
211 Fsm(EEventBTBF, KErrNone);
215 void RA3FDevSoundTestBase::PlayError(TInt aError)
217 INFO_PRINTF1(_L("========== DevSound PlayError() callback =========="));
218 INFO_PRINTF3(KMsgErrorDevSoundCallback, &KPlayErrorText, aError);
219 if(iBuffer->LastBuffer() && (aError == KErrUnderflow))
221 INFO_PRINTF1(_L("Playback completed normally"));
226 INFO_PRINTF1(_L("Playback completed with error"));
227 StopTest(aError, EFail);
231 void RA3FDevSoundTestBase::BufferToBeEmptied(CMMFBuffer* aBuffer)
233 INFO_PRINTF1(_L("DevSound called BufferToBeEmptied."));
236 INFO_PRINTF1(_L("BufferToBeEmptied callback received a NULL CMMFBuffer"));
237 StopTest(KErrGeneral);
241 if(aBuffer->LastBuffer())
243 if(iDevSoundState == EStatePause)
245 // We need to call CMMFDevSound->Stop() here if last buffer flag set
246 INFO_PRINTF1(_L("Devsound is in Paused state and CMMFBuffer::LastBuffer is set"));
247 INFO_PRINTF1(_L("iMMFDevSound->Stop()"));
248 iMMFDevSound->Stop();
253 INFO_PRINTF1(_L("***** Unknown behaviour: Last buffer flag set before calling CMMFDevSound->Pause()"));
254 StopTest(KErrUnknown);
259 INFO_PRINTF1(_L("DevSound Event: EEventBTBE"));
260 Fsm(EEventBTBE, KErrNone);
264 void RA3FDevSoundTestBase::RecordError(TInt aError)
266 INFO_PRINTF2(_L("DevSound called RecordError with error = %d"), aError);
267 if (aError == KErrUnderflow)
273 void RA3FDevSoundTestBase::ConvertError(TInt /*aError*/)
275 __ASSERT_ALWAYS(0, Panic(_L("RA3FDevSoundTestBase"), EInvalidCallbackCall));
278 void RA3FDevSoundTestBase::DeviceMessage(TUid /*aMessageType*/, const TDesC8& /*aMsg*/)
280 __ASSERT_ALWAYS(0, Panic(_L("RA3FDevSoundTestBase"), EInvalidCallbackCall));
283 void RA3FDevSoundTestBase::SendEventToClient(const TMMFEvent& aEvent)
285 INFO_PRINTF3(_L("RA3FDevSoundTestBase::SendEventToClient type=0x%08x errorCode=%d"),aEvent.iEventType, aEvent.iErrorCode);
286 if(aEvent.iEventType == KMMFEventCategoryAudioResourceAvailable)
288 INFO_PRINTF1(_L("Received KMMFEventCategoryAudioResourceAvailable"));
289 Fsm(EResourceAvailable, aEvent.iErrorCode);
293 void RA3FDevSoundTestBase::SampleRateFromTIntToTMMFSampleRate(TInt aSampleRate, TMMFSampleRate &aESampleRate)
295 const TSampleRateToTMMFSampleRate SampleRateLookUp [] =
297 {8000, EMMFSampleRate8000Hz},
298 {11025, EMMFSampleRate11025Hz},
299 {16000, EMMFSampleRate16000Hz},
300 {22050, EMMFSampleRate22050Hz},
301 {32000, EMMFSampleRate32000Hz},
302 {44100, EMMFSampleRate44100Hz},
303 {48000, EMMFSampleRate48000Hz},
304 {88200, EMMFSampleRate88200Hz},
305 {96000, EMMFSampleRate96000Hz},
306 {12000, EMMFSampleRate12000Hz},
307 {24000, EMMFSampleRate24000Hz},
308 {64000, EMMFSampleRate64000Hz}
311 const TInt length = sizeof SampleRateLookUp / sizeof *SampleRateLookUp;
313 for(TInt i = 0; i < length; i++)
315 if(aSampleRate == SampleRateLookUp[i].iTIntSampleRate)
317 aESampleRate = SampleRateLookUp[i].iTMMFSampleRate;
321 ERR_PRINTF1(_L("User SampleRate doesn't match any of the specified sample rates"));
322 StopTest(KErrGeneral);
326 void RA3FDevSoundTestBase::SampleRateFromTUintToString(TUint aSampleRate, TDes& aStringSampleRate)
328 const TSampleRateToString SampleRateLookUp [] =
330 {0x00000001, KEMMFSampleRate8000Hz() },
331 {0x00000002, KEMMFSampleRate11025Hz()},
332 {0x00000004, KEMMFSampleRate16000Hz()},
333 {0x00000008, KEMMFSampleRate22050Hz()},
334 {0x00000010, KEMMFSampleRate32000Hz()},
335 {0x00000020, KEMMFSampleRate44100Hz()},
336 {0x00000040, KEMMFSampleRate48000Hz()},
337 {0x00000080, KEMMFSampleRate88200Hz()},
338 {0x00000100, KEMMFSampleRate96000Hz()},
339 {0x00000200, KEMMFSampleRate12000Hz()},
340 {0x00000400, KEMMFSampleRate24000Hz()},
341 {0x00000800, KEMMFSampleRate64000Hz()}
344 const TInt length = sizeof SampleRateLookUp / sizeof *SampleRateLookUp;
346 for (TInt i =0; i < length; i++)
348 if(aSampleRate == SampleRateLookUp[i].iTUIntSampleRate)
350 aStringSampleRate.Copy(SampleRateLookUp[i].iTPtrSampleRate);
354 ERR_PRINTF1(_L("SampleRate doesn't match any of the specified sample rates"));
355 StopTest(KErrGeneral);
358 void RA3FDevSoundTestBase::ChannelsFromTUintToString(TUint aChannels,TDes& aStringChannels)
360 const TChannelsToString ChannelsLookUp [] =
362 {0x00000001, KEMMFMono() },
363 {0x00000002, KEMMFStereo()},
365 const TInt length = sizeof ChannelsLookUp / sizeof *ChannelsLookUp;
367 for (TInt i =0; i < length; i++)
369 if(aChannels == ChannelsLookUp[i].iTUIntChannels)
371 aStringChannels.Copy(ChannelsLookUp[i].iTPtrChannels);
375 ERR_PRINTF1(_L("Channels doesn't match any of the specified channels"));
376 StopTest(KErrGeneral);
380 void RA3FDevSoundTestBase::PrintSupportedCapabilities(TUint aSampleRate,TUint aChannel)
382 TInt length = sizeof KRateLookup / sizeof *KRateLookup;
383 TBuf<KMaxSampleRateStringLength> stringSampleRate;
384 TBuf<KMaxChannelsStringLength> channels;
386 for(TInt i = 0; i < length ; i++)
388 if(aSampleRate & KRateLookup[i])
390 SampleRateFromTUintToString(KRateLookup[i],stringSampleRate);
391 INFO_PRINTF3(_L("Supported Sample rate 0x%08x %S"),KRateLookup[i],&stringSampleRate);
394 length = sizeof KChannelsLookup / sizeof *KChannelsLookup;
396 for(TInt i = 0; i < length ; i++)
398 if(aChannel & KChannelsLookup[i])
400 ChannelsFromTUintToString(KChannelsLookup[i],channels);
401 INFO_PRINTF3(_L("Supported channels 0x%08x %S"),KChannelsLookup[i],&channels);
407 // CAsyncWriteBufferToFile
410 CAsyncWriteBufferToFile::CAsyncWriteBufferToFile(RFile& aFile, CMMFDevSound* aDevSound, RA3FDevSoundTestBase& aTestStep)
411 :CActive(EPriorityStandard),
413 iDevSound(aDevSound),
416 CActiveScheduler::Add(this);
419 CAsyncWriteBufferToFile::~CAsyncWriteBufferToFile()
424 CAsyncWriteBufferToFile* CAsyncWriteBufferToFile::NewL(RFile& aFile, CMMFDevSound* aDevSound, RA3FDevSoundTestBase& aTestStep)
426 CAsyncWriteBufferToFile* self = new(ELeave) CAsyncWriteBufferToFile(aFile, aDevSound, aTestStep);
427 CleanupStack::PushL(self);
429 CleanupStack::Pop(self);
433 void CAsyncWriteBufferToFile::ConstructL()
435 // Nothing to do here
438 void CAsyncWriteBufferToFile::RunL()
440 //If error occurs then deal with problem in RunError()
441 User::LeaveIfError(iStatus.Int());
442 // Continue recording data to a buffer
443 iDevSound->RecordData();
447 void CAsyncWriteBufferToFile::Start(CMMFDataBuffer* aBuffer)
449 iFile.Write(aBuffer->Data(), iStatus);
453 void CAsyncWriteBufferToFile::DoCancel()
455 // Can't cancel an async write request
458 TInt CAsyncWriteBufferToFile::RunError(TInt aError)
460 iTestStep.CallStopTest(aError);