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 "cdevplaycontrol.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 CDevPlayControl::CDevPlayControl()
34 DP_CONTEXT(CDevPlayControl::CDevPlayControl *CD1*, CtxDevSound, DPLOCAL);
39 // -----------------------------------------------------------------------------
40 // Symbian 2nd phase constructor
41 // -----------------------------------------------------------------------------
43 void CDevPlayControl::ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aAdaptationObserver)
45 DP_CONTEXT(CDevPlayControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
47 CDevAudioControl::ConstructL(aDevAudio, aAdaptationObserver);
51 // -----------------------------------------------------------------------------
52 // Symbian constructor
53 // -----------------------------------------------------------------------------
55 CDevPlayControl* CDevPlayControl::NewL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
57 DP_STATIC_CONTEXT(CDevPlayControl::NewL *CD0*, CtxDevSound, DPLOCAL);
59 CDevPlayControl* self = new (ELeave) CDevPlayControl();
60 CleanupStack::PushL(self);
61 self->ConstructL(aDevAudio, aDevSoundObserver);
62 CleanupStack::Pop(self);
63 DP0_RET(self, "0x%x");
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 CDevPlayControl::~CDevPlayControl()
72 DP_CONTEXT(CDevPlayControl::~CDevPlayControl *CD1*, CtxDevSound, DPLOCAL);
77 // -----------------------------------------------------------------------------
78 // CDevPlayControl::Initialize
79 // -----------------------------------------------------------------------------
81 TInt CDevPlayControl::Initialize(TUid aFormat)
83 DP_CONTEXT(CDevPlayControl::Initialize *CD1*, CtxDevSound, DPLOCAL);
85 ASSERT(iDevAudio->iAudioSource && iDevAudio->iAudioCodec && iGainControl);
88 err = iDevAudio->iAudioStream->AddSource(iDevAudio->iAudioSource);
91 err = iDevAudio->iAudioStream->AddSink(iDevAudio->iAudioSink);
94 err = iDevAudio->iAudioStream->AddAudioCodec(iDevAudio->iAudioCodec);
98 if(err == KErrNone && iDevAudio->iActiveState == EDevSoundAdaptorCreated_Uninitialised)
100 // Register for notifications
101 // Note KErrAlreadyExist is not acceptable as an error, since it shouldn't occurr
102 // It's mandatory unregister any observer when no more events are required
103 // Otherwise the notifications will be sent to all previously registered observer
104 // when reinitialising for different mode
105 err = iDevAudio->iAudioStream->RegisterAudioStreamObserver(*this);
108 err = iGainControl->RegisterAudioGainControlObserver(*this);
111 err = CacheAudioCodecIf();
114 __ASSERT_ALWAYS (iAudioCodecIf,CDevAudioControl::Panic(EAudioCodecIsNull));
115 err = iAudioCodecIf->RegisterAudioCodecObserver(*this);
118 err = iAudioCodecIf->SetFormat(aFormat);
121 // Needed to get ProcessingUnitError notification
122 err = iDevAudio->iAudioCodec->RegisterProcessingUnitObserver(*this);
131 DP0(DLINFO,"Error while registering observers");
132 iDevAudio->iAudioStream->UnregisterAudioStreamObserver(*this);
133 iGainControl->UnregisterAudioGainControlObserver(*this);
136 iAudioCodecIf->UnregisterAudioCodecObserver(*this);
137 iAudioCodecIf = NULL; // Not owned here: convenience pointer, so no need to delete
143 err = iDevAudio->iAudioStream->Initialize();
146 err = iDevAudio->CommitAudioContext();
149 iDevAudio->iActiveState = EDevSoundAdaptorInitialising;
157 // -----------------------------------------------------------------------------
158 // CDevPlayControl::ProcessInit
159 // -----------------------------------------------------------------------------
161 TInt CDevPlayControl::ProcessInit()
163 DP_CONTEXT(CDevPlayControl::ProcessInit *CD1*, CtxDevSound, DPLOCAL);
165 PRF(PRF_ID, PRF_START, PRF_TIME, AA_DS_PlayInit, "");
166 PRF(PRF_ID, PRF_START, PRF_LOAD, AA_DS_Play, "");
169 switch (iDevAudio->iActiveState)
171 case EDevSoundAdaptorInitialised_Initialised:
173 TAny* interface(NULL);
174 // get correct BufferSource interface of DevAudio::iAudioSource
175 interface = iDevAudio->iAudioSource->Interface(KUidMmfBufferSource);
177 if (interface == NULL)
179 DP0_RET(KErrNotSupported, "Incorrect source type! %d");
183 iBufferSource = static_cast<MMMFBufferSource*>(interface);
184 iBufferSource->SetDataSupplier(*this);
185 if (iDevAudio->IsPrioritySet())
187 TAudioTypeSettings priority;
188 iDevAudio->GetPrioritySettings(priority);
189 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)
230 err = iDevAudio->RequestGainAndBalance(this);
233 err = iDevAudio->iAudioStream->Activate();
237 err = iDevAudio->CommitAudioContext();
241 iDevAudio->iActiveState = EDevSoundAdaptorActivating;
245 case EDevSoundAdaptorActive_Active:
246 // Deliberate fall through - set err=KErrNotReady for PlayInit when already active
254 iDevAudio->iStop = EFalse;
260 // -----------------------------------------------------------------------------
261 // CDevPlayControl::ProcessData
262 // -----------------------------------------------------------------------------
264 void CDevPlayControl::ProcessData()
266 DP_CONTEXT(CDevPlayControl::ProcessData *CD1*, CtxDevSound, DPLOCAL);
270 if(!iDevAudio->iStop)
272 err = iBufferSource->BufferFilled(iBuffer);
273 if ( err != KErrNone)
275 iErrorCondition = err;
283 // -----------------------------------------------------------------------------
284 // From class MAudioDataSupplier
285 // CDevPlayControl::BufferToBeFilled
286 // -----------------------------------------------------------------------------
288 void CDevPlayControl::BufferToBeFilled(MMMFBufferSource* aSource, CMMFBuffer* aBuffer)
290 DP_CONTEXT(CDevPlayControl::BufferToBeFilled *CD1*, CtxDevSound, DPLOCAL);
292 PRF(PRF_ID, PRF_STOP, PRF_TIME, AA_DS_PlayInit, "");
293 __ASSERT_ALWAYS(iBufferSource==aSource, CDevAudioControl::Panic(EBufferMismatch));
294 iAdaptationObserver->BufferToBeFilled(aBuffer);
299 // -----------------------------------------------------------------------------
300 // From class MAudioDataSupplier
301 // CDevPlayControl::DiscardBuffers
302 // -----------------------------------------------------------------------------
304 void CDevPlayControl::DiscardBuffers(MMMFBufferSource* /*aSource*/)
306 DP_CONTEXT(CDevPlayControl::DiscardBuffers *CD1*, CtxDevSound, DPLOCAL);
312 // -----------------------------------------------------------------------------
313 // CDevPlayControl::ProcessingFinished
314 // -----------------------------------------------------------------------------
316 void CDevPlayControl::ProcessingFinished(MAudioStream& /*aStream*/)
318 DP_CONTEXT(CDevPlayControl::ProcessingFinished *CD1*, CtxDevSound, DPLOCAL);
320 PRF(PRF_ID, PRF_STOP, PRF_LOAD, AA_DS_Play, "");
322 if(iCallbackFromAdaptor == KCallbackNone)
324 iCallbackFromAdaptor = KCallbackProcessingFinished;
325 iAdaptationObserver->CallbackFromAdaptorReceived(iCallbackFromAdaptor, KErrNone);
329 // Multiple callbacks from adaptor
330 DP0(DLERR, "Multiple callbacks from adaptor");
336 // -----------------------------------------------------------------------------
337 // CDevPlayControl::ProcessingFinishedReceived
338 // -----------------------------------------------------------------------------
340 TInt CDevPlayControl::ProcessingFinishedReceived(TBool& aAyncOperation)
342 DP_CONTEXT(CDevPlayControl::ProcessingFinishedReceived *CD1*, CtxDevSound, DPLOCAL);
344 aAyncOperation = EFalse;
345 TInt err = iDevAudio->iAudioStream->Stop();
346 if ( err == KErrNone)
348 err = iDevAudio->CommitAudioContext();
351 iDevAudio->iActiveState = EDevSoundAdaptorStopping;
352 aAyncOperation = ETrue;
355 // Something was wrong, clear the flag.
358 iCallbackFromAdaptor = KCallbackNone;
363 // -----------------------------------------------------------------------------
364 // CDevPlayControl::BufferErrorEvent
365 // -----------------------------------------------------------------------------
367 void CDevPlayControl::BufferErrorEvent()
369 ProcessingUnitError(NULL,KErrNoMemory);
373 // -----------------------------------------------------------------------------
374 // CDevPlayControl::FinishWithError
375 // -----------------------------------------------------------------------------
377 void CDevPlayControl::FinishWithError(TInt aError)
379 iAdaptationObserver->PlayError(aError);
382 // ---------------------------------------------------------------------------
383 // CDevPlayControl::ProcessingError
384 // ---------------------------------------------------------------------------
385 TInt CDevPlayControl::ProcessingError(TBool& aAyncOperation)
387 TInt err = ProcessingFinishedReceived(aAyncOperation);