Update contrib.
1 // Copyright (c) 2006-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.
18 #include "cdevtonecontrol.h"
20 #include <a3f/maudiocontext.h>
21 #include <a3f/mbuffersource.h>
22 #include <a3f/maudiocodec.h>
23 #include <a3f/maudiogaincontrol.h>
24 #include <a3f/audioprocessingunittypeuids.h>
27 // ---------------------------------------------------------------------------
28 // Default constructor
29 // ---------------------------------------------------------------------------
31 CDevToneControl::CDevToneControl()
34 DP_CONTEXT(CDevToneControl::CDevToneControl *CD1*, CtxDevSound, DPLOCAL);
39 // -----------------------------------------------------------------------------
40 // Symbian 2nd phase constructor
41 // -----------------------------------------------------------------------------
43 void CDevToneControl::ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
45 DP_CONTEXT(CDevToneControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
47 CDevAudioControl::ConstructL(aDevAudio, aDevSoundObserver);
51 // -----------------------------------------------------------------------------
52 // Symbian constructor
53 // -----------------------------------------------------------------------------
55 CDevToneControl* CDevToneControl::NewL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
57 DP_STATIC_CONTEXT(CDevToneControl::NewL *CD0*, CtxDevSound, DPLOCAL);
59 CDevToneControl* self = new (ELeave) CDevToneControl();
60 CleanupStack::PushL(self);
61 self->ConstructL(aDevAudio, aDevSoundObserver);
62 CleanupStack::Pop(self);
63 DP0_RET(self, "0x%x");
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 CDevToneControl::~CDevToneControl()
72 DP_CONTEXT(CDevToneControl::~CDevToneControl *CD1*, CtxDevSound, DPLOCAL);
77 // -----------------------------------------------------------------------------
78 // CDevToneControl::Initialize
79 // -----------------------------------------------------------------------------
81 TInt CDevToneControl::Initialize(TUid aFormat)
83 DP_CONTEXT(CDevToneControl::Initialize *CD1*, CtxDevSound, DPLOCAL);
85 ASSERT(iDevAudio->iAudioSource && iDevAudio->iAudioCodec && iGainControl);
89 err = iDevAudio->iAudioStream->AddSource(iDevAudio->iAudioSource);
92 err = iDevAudio->iAudioStream->AddSink(iDevAudio->iAudioSink);
95 err = iDevAudio->iAudioStream->AddAudioCodec(iDevAudio->iAudioCodec);
99 // KErrAlreadyExist shouldn't occurr
100 // It's mandatory unregister any observer when no more events are required
101 // Otherwise the notifications will be sent to all previously registered observer
102 // when reinitialising for different mode
103 err = iDevAudio->iAudioStream->RegisterAudioStreamObserver(*this);
106 DP0_RET(err, "Stream observer registeration failed, err = %d");
108 iGainControl->RegisterAudioGainControlObserver(*this);
111 DP0_RET(err, "Gaincontrol observer registeration failed, err = %d");
114 err = CacheAudioCodecIf();
117 DP0_RET(err, "CacheAudioCodecIf() failed, err = %d");
120 err = iAudioCodecIf->RegisterAudioCodecObserver(*this);
123 DP0_RET(err, "Registration failed, err = %d");
126 __ASSERT_ALWAYS (iAudioCodecIf, CDevAudioControl::Panic(EAudioCodecIsNull));
127 err = iAudioCodecIf->SetFormat(aFormat);
130 DP0_RET(err, "SetFormat() failed, err = %d");
133 // Needed to get ProcessingUnitError notification
134 err = iDevAudio->iAudioCodec->RegisterProcessingUnitObserver(*this);
135 if ((err != KErrAlreadyExists) && (err != KErrNone))
137 DP0_RET(err, "Audiocodec observer registeration failed, err = %d");
140 // get correct BufferSource interface of DevAudio::iAudioSource
141 TAny* interface = iDevAudio->iAudioSource->Interface(KUidMmfBufferSource);
142 if (interface == NULL)
144 DP0_RET(KErrNotSupported, "Incorrect source type! %d");
148 iBufferSource = static_cast<MMMFBufferSource*>(interface);
149 err = iBufferSource->SetDataSupplier(*this);
150 if (err != KErrNone )
152 DP0_RET(err, "Data suplier setting failed, err = %d");
156 err = iDevAudio->iAudioStream->Initialize();
159 err = iDevAudio->CommitAudioContext();
163 iDevAudio->iActiveState = EDevSoundAdaptorInitialising;
170 // -----------------------------------------------------------------------------
171 // CDevToneControl::ProcessInit
172 // -----------------------------------------------------------------------------
174 TInt CDevToneControl::ProcessInit()
176 DP_CONTEXT(CDevToneControl::ProcessInit *CD1*, CtxDevSound, DPLOCAL);
178 PRF(PRF_ID, PRF_START, PRF_TIME, AA_DS_PlayTone, "");
181 DP1(DLINFO,"iActiveState = %d", iDevAudio->iActiveState);
182 switch (iDevAudio->iActiveState)
184 case EDevSoundAdaptorInitialised_Initialised:
186 if (iDevAudio->IsPrioritySet())
188 TAudioTypeSettings priority;
189 iDevAudio->GetPrioritySettings(priority);
190 err = iDevAudio->iAudioStream->SetAudioType(priority);
192 if ( err == KErrNone)
194 err = iDevAudio->iAudioStream->Load();
196 if ( err == KErrNone)
198 err = iDevAudio->CommitAudioContext();
202 iDevAudio->iActiveState = EDevSoundAdaptorLoading;
207 case EDevSoundAdaptorGoingActive:
209 //If following condition is false, then we are here because of a
210 //pre-emption clash in last Commit cycle started from
211 //CDevCommonControl::ContextEventUpdateWithStateEventNoError.
212 if(iDevAudio->iPreviousState != EDevSoundAdaptorActivating)
216 //Fall through as required
218 case EDevSoundAdaptorPaused_Primed:
219 case EDevSoundAdaptorInitialised_Idle:
221 //If following condition is true, then we are here because of a
222 //pre-emption clash in last Commit cycle started from
223 //CDevCommonControl::ContextEventUpdateWithStateEventAndError.
224 if(iDevAudio->iPreviousState == EDevSoundAdaptorUnloading)
229 DP0(DLINFO, "Already in loaded state");
230 err = iDevAudio->RequestGainAndBalance(this);
233 err = iDevAudio->iAudioStream->Activate();
237 err = iDevAudio->CommitAudioContext();
241 iDevAudio->iActiveState = EDevSoundAdaptorActivating;
245 case EDevSoundAdaptorActive_Active:
253 // -----------------------------------------------------------------------------
254 // CDevToneControl::ProcessData
255 // -----------------------------------------------------------------------------
257 void CDevToneControl::ProcessData()
259 DP_CONTEXT(CDevToneControl::ProcessData *CD1*, CtxDevSound, DPLOCAL);
264 // -----------------------------------------------------------------------------
265 // From class MAudioDataSupplier
266 // CDevToneControl::BufferToBeFilled
267 // -----------------------------------------------------------------------------
269 void CDevToneControl::BufferToBeFilled(MMMFBufferSource* aSource, CMMFBuffer* aBuffer)
271 DP_CONTEXT(CDevToneControl::BufferToBeFilled *CD1*, CtxDevSound, DPLOCAL);
273 __ASSERT_ALWAYS(iBufferSource==aSource, CDevAudioControl::Panic(EBufferMismatch));
276 CMMFDataBuffer* myBuffer = static_cast<CMMFDataBuffer*> (aBuffer);
277 Mem::Copy((TAny*)(myBuffer->Data().Ptr()), (TAny*)(&iToneData), sizeof(TToneData) );
279 myBuffer->SetLastBuffer(ETrue);
281 err = iBufferSource->BufferFilled(myBuffer);
282 if ( err != KErrNone)
284 iErrorCondition = err;
291 // -----------------------------------------------------------------------------
292 // From class MAudioDataSupplier
293 // CDevToneControl::DiscardBuffers
294 // -----------------------------------------------------------------------------
296 void CDevToneControl::DiscardBuffers(MMMFBufferSource* /*aSource*/)
298 DP_CONTEXT(CDevToneControl::DiscardBuffers *CD1*, CtxDevSound, DPLOCAL);
303 // -----------------------------------------------------------------------------
304 // CDevToneControl::ProcessingFinished
305 // -----------------------------------------------------------------------------
307 void CDevToneControl::ProcessingFinished(MAudioStream& /*aStream*/)
309 DP_CONTEXT(CDevToneControl::ProcessingFinished *CD1*, CtxDevSound, DPLOCAL);
311 PRF(PRF_ID, PRF_STOP, PRF_TIME, AA_DS_PlayTone, "");
313 if(iCallbackFromAdaptor == KCallbackNone)
315 iCallbackFromAdaptor = KCallbackProcessingFinished;
316 iAdaptationObserver->CallbackFromAdaptorReceived(KCallbackProcessingFinished, KErrNone);
320 // Multiple callbacks from adaptor
321 DP0(DLERR, "Multiple callbacks from adaptor");
326 // -----------------------------------------------------------------------------
327 // CDevToneControl::SetToneData
328 // -----------------------------------------------------------------------------
330 TInt CDevToneControl::SetToneData(TToneData& aToneData)
332 DP_CONTEXT(CDevToneControl::SetToneData *CD1*, CtxDevSound, DPLOCAL);
335 iToneData = aToneData;
337 DP0_RET(KErrNone, "%d");
341 // -----------------------------------------------------------------------------
342 // CDevToneControl::FinishWithError
343 // -----------------------------------------------------------------------------
345 void CDevToneControl::FinishWithError(TInt aError) // from CDevCommonControl
347 iAdaptationObserver->ToneFinished(aError);
351 // -----------------------------------------------------------------------------
352 // CDevPlayControl::ProcessingFinishedReceived
353 // -----------------------------------------------------------------------------
355 TInt CDevToneControl::ProcessingFinishedReceived(TBool& asyncOperation)
357 DP_CONTEXT(CDevToneControl::ProcessingFinishedReceived *CD1*, CtxDevSound, DPLOCAL);
359 asyncOperation = EFalse;
360 TInt err = iDevAudio->iAudioStream->Stop();
361 if ( err == KErrNone)
363 err = iDevAudio->CommitAudioContext();
366 iDevAudio->iActiveState = EDevSoundAdaptorStopping;
367 asyncOperation = ETrue;
370 // Something was wrong, clear the flag.
373 iCallbackFromAdaptor = KCallbackNone;
378 // ---------------------------------------------------------------------------
379 // CDevToneControl::ProcessingError
380 // ---------------------------------------------------------------------------
381 TInt CDevToneControl::ProcessingError(TBool& aAyncOperation)
383 TInt err = ProcessingFinishedReceived(aAyncOperation);