os/mm/devsound/a3fdevsound/src/devsoundadaptor/cdevplaycontrol.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/devsound/a3fdevsound/src/devsoundadaptor/cdevplaycontrol.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,392 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +
    1.20 +
    1.21 +#include "cdevplaycontrol.h"
    1.22 +
    1.23 +#include <a3f/maudiocontext.h>
    1.24 +#include <a3f/mbuffersource.h>
    1.25 +#include <a3f/maudiocodec.h>
    1.26 +#include <a3f/maudiogaincontrol.h>
    1.27 +#include <a3f/audioprocessingunittypeuids.h>
    1.28 +
    1.29 +
    1.30 +// ---------------------------------------------------------------------------
    1.31 +// Default constructor
    1.32 +// ---------------------------------------------------------------------------
    1.33 +//
    1.34 +CDevPlayControl::CDevPlayControl()
    1.35 +	{
    1.36 +	TRACE_CREATE();
    1.37 +	DP_CONTEXT(CDevPlayControl::CDevPlayControl *CD1*, CtxDevSound, DPLOCAL);
    1.38 +	DP_IN();
    1.39 +	DP_OUT();
    1.40 +	}
    1.41 +
    1.42 +// -----------------------------------------------------------------------------
    1.43 +// Symbian 2nd phase constructor
    1.44 +// -----------------------------------------------------------------------------
    1.45 +//
    1.46 +void CDevPlayControl::ConstructL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aAdaptationObserver)
    1.47 +	{
    1.48 +	DP_CONTEXT(CDevPlayControl::ConstructL *CD1*, CtxDevSound, DPLOCAL);
    1.49 +	DP_IN();
    1.50 +	CDevAudioControl::ConstructL(aDevAudio, aAdaptationObserver);
    1.51 +	DP_OUT();
    1.52 +	}
    1.53 +
    1.54 +// -----------------------------------------------------------------------------
    1.55 +// Symbian constructor
    1.56 +// -----------------------------------------------------------------------------
    1.57 +//
    1.58 +CDevPlayControl* CDevPlayControl::NewL(CDevAudio* aDevAudio, MDevSoundAdaptationObserver& aDevSoundObserver)
    1.59 +	{
    1.60 +	DP_STATIC_CONTEXT(CDevPlayControl::NewL *CD0*, CtxDevSound, DPLOCAL);
    1.61 +	DP_IN();
    1.62 +	CDevPlayControl* self = new (ELeave) CDevPlayControl();
    1.63 +	CleanupStack::PushL(self);
    1.64 +	self->ConstructL(aDevAudio, aDevSoundObserver);
    1.65 +	CleanupStack::Pop(self);
    1.66 +	DP0_RET(self, "0x%x");
    1.67 +	}
    1.68 +
    1.69 +// ---------------------------------------------------------------------------
    1.70 +// Destructor
    1.71 +// ---------------------------------------------------------------------------
    1.72 +//
    1.73 +CDevPlayControl::~CDevPlayControl()
    1.74 +	{
    1.75 +	DP_CONTEXT(CDevPlayControl::~CDevPlayControl *CD1*, CtxDevSound, DPLOCAL);
    1.76 +	DP_IN();
    1.77 +	DP_OUT();
    1.78 +	}
    1.79 +
    1.80 +// -----------------------------------------------------------------------------
    1.81 +// CDevPlayControl::Initialize
    1.82 +// -----------------------------------------------------------------------------
    1.83 +//
    1.84 +TInt CDevPlayControl::Initialize(TUid aFormat)
    1.85 +	{
    1.86 +	DP_CONTEXT(CDevPlayControl::Initialize *CD1*, CtxDevSound, DPLOCAL);
    1.87 +	DP_IN();
    1.88 +	ASSERT(iDevAudio->iAudioSource && iDevAudio->iAudioCodec && iGainControl);
    1.89 +	TInt err(KErrNone);
    1.90 +
    1.91 +	err = iDevAudio->iAudioStream->AddSource(iDevAudio->iAudioSource);
    1.92 +	if (err == KErrNone)
    1.93 +		{
    1.94 +		err = iDevAudio->iAudioStream->AddSink(iDevAudio->iAudioSink);
    1.95 +		if (err == KErrNone)
    1.96 +			{
    1.97 +			err = iDevAudio->iAudioStream->AddAudioCodec(iDevAudio->iAudioCodec);
    1.98 +			}
    1.99 +		}
   1.100 +	
   1.101 +	if(err == KErrNone && iDevAudio->iActiveState == EDevSoundAdaptorCreated_Uninitialised)
   1.102 +		{
   1.103 +		// Register for notifications
   1.104 +		// Note KErrAlreadyExist is not acceptable as an error, since it shouldn't occurr 
   1.105 +		// It's mandatory unregister any observer when no more events are required
   1.106 +		// Otherwise the notifications will be sent to all previously registered observer
   1.107 +		// when reinitialising for different mode
   1.108 +		err = iDevAudio->iAudioStream->RegisterAudioStreamObserver(*this);
   1.109 +		if (err == KErrNone)
   1.110 +			{
   1.111 +			err = iGainControl->RegisterAudioGainControlObserver(*this);
   1.112 +			if (err == KErrNone)
   1.113 +				{
   1.114 +				err = CacheAudioCodecIf();
   1.115 +				if (err == KErrNone)
   1.116 +					{
   1.117 +					__ASSERT_ALWAYS (iAudioCodecIf,CDevAudioControl::Panic(EAudioCodecIsNull));
   1.118 +					err = iAudioCodecIf->RegisterAudioCodecObserver(*this);
   1.119 +					if(err == KErrNone)
   1.120 +						{
   1.121 +						err = iAudioCodecIf->SetFormat(aFormat);
   1.122 +						if (err == KErrNone)
   1.123 +							{
   1.124 +							// Needed to get ProcessingUnitError notification 
   1.125 +							err = iDevAudio->iAudioCodec->RegisterProcessingUnitObserver(*this);
   1.126 +							}
   1.127 +						}
   1.128 +					}
   1.129 +				}
   1.130 +			}
   1.131 +
   1.132 +		if (err != KErrNone)
   1.133 +			{
   1.134 +			DP0(DLINFO,"Error while registering observers");
   1.135 +			iDevAudio->iAudioStream->UnregisterAudioStreamObserver(*this);
   1.136 +			iGainControl->UnregisterAudioGainControlObserver(*this);
   1.137 +			if(iAudioCodecIf)
   1.138 +				{
   1.139 +				iAudioCodecIf->UnregisterAudioCodecObserver(*this);
   1.140 +				iAudioCodecIf = NULL; // Not owned here: convenience pointer, so no need to delete
   1.141 +				}
   1.142 +			}
   1.143 +
   1.144 +		if (err == KErrNone)
   1.145 +			{
   1.146 +			err = iDevAudio->iAudioStream->Initialize();
   1.147 +			if (err == KErrNone)
   1.148 +				{
   1.149 +				err = iDevAudio->CommitAudioContext();
   1.150 +				if (err == KErrNone)
   1.151 +					{
   1.152 +					iDevAudio->iActiveState = EDevSoundAdaptorInitialising;
   1.153 +					}
   1.154 +				}
   1.155 +			}
   1.156 +		}
   1.157 +	DP0_RET(err,"%d");
   1.158 +	}
   1.159 +
   1.160 +// -----------------------------------------------------------------------------
   1.161 +// CDevPlayControl::ProcessInit
   1.162 +// -----------------------------------------------------------------------------
   1.163 +//
   1.164 +TInt CDevPlayControl::ProcessInit()
   1.165 +	{
   1.166 +	DP_CONTEXT(CDevPlayControl::ProcessInit *CD1*, CtxDevSound, DPLOCAL);
   1.167 +	DP_IN();
   1.168 +	PRF(PRF_ID, PRF_START, PRF_TIME, AA_DS_PlayInit, "");
   1.169 +	PRF(PRF_ID, PRF_START, PRF_LOAD, AA_DS_Play, "");
   1.170 +	TInt err(KErrNone);
   1.171 +	
   1.172 +	switch (iDevAudio->iActiveState)
   1.173 +		{
   1.174 +		case EDevSoundAdaptorInitialised_Initialised:
   1.175 +			{
   1.176 +			TAny* interface(NULL);
   1.177 +			// get correct BufferSource interface of DevAudio::iAudioSource
   1.178 +			interface = iDevAudio->iAudioSource->Interface(KUidMmfBufferSource);
   1.179 +			
   1.180 +			if (interface == NULL)
   1.181 +				{
   1.182 +				DP0_RET(KErrNotSupported, "Incorrect source type! %d");
   1.183 +				}
   1.184 +			else
   1.185 +				{
   1.186 +				iBufferSource = static_cast<MMMFBufferSource*>(interface);
   1.187 +				iBufferSource->SetDataSupplier(*this);
   1.188 +				if (iDevAudio->IsPrioritySet())
   1.189 +					{
   1.190 +					TAudioTypeSettings priority;
   1.191 +					iDevAudio->GetPrioritySettings(priority);
   1.192 +					err = iDevAudio->iAudioStream->SetAudioType(priority);
   1.193 +					}				
   1.194 +				}
   1.195 +			if ( err == KErrNone)
   1.196 +				{
   1.197 +				err = iDevAudio->iAudioStream->Load();
   1.198 +				}
   1.199 +			if ( err == KErrNone)
   1.200 +				{
   1.201 +				err = iDevAudio->CommitAudioContext();
   1.202 +				}
   1.203 +			if(err == KErrNone)
   1.204 +				{
   1.205 +				iDevAudio->iActiveState = EDevSoundAdaptorLoading;
   1.206 +				}
   1.207 +			}
   1.208 +			break;
   1.209 +
   1.210 +		case EDevSoundAdaptorGoingActive:
   1.211 +			{
   1.212 +			//If following condition is false, then we are here because of a
   1.213 +			//pre-emption clash in last Commit cycle started from
   1.214 +			//CDevCommonControl::ContextEventUpdateWithStateEventNoError.
   1.215 +			if(iDevAudio->iPreviousState != EDevSoundAdaptorActivating)
   1.216 +				{
   1.217 +				break;
   1.218 +				}
   1.219 +			//Fall through as required
   1.220 +			}
   1.221 +		case EDevSoundAdaptorPaused_Primed:
   1.222 +		case EDevSoundAdaptorInitialised_Idle:
   1.223 +			{
   1.224 +			//If following condition is true, then we are here because of a
   1.225 +			//pre-emption clash in last Commit cycle started from
   1.226 +			//CDevCommonControl::ContextEventUpdateWithStateEventAndError.
   1.227 +			if(iDevAudio->iPreviousState == EDevSoundAdaptorUnloading)
   1.228 +				{
   1.229 +				err = Unload();
   1.230 +				break;
   1.231 +				}
   1.232 +
   1.233 +			err = iDevAudio->RequestGainAndBalance(this);
   1.234 +			if (err==KErrNone)
   1.235 +				{
   1.236 +				err = iDevAudio->iAudioStream->Activate();
   1.237 +				}
   1.238 +			if (err == KErrNone)
   1.239 +				{
   1.240 +				err = iDevAudio->CommitAudioContext();
   1.241 +				}
   1.242 +			if (err == KErrNone)
   1.243 +				{
   1.244 +				iDevAudio->iActiveState = EDevSoundAdaptorActivating;
   1.245 +				}
   1.246 +			break;
   1.247 +			}
   1.248 +		case EDevSoundAdaptorActive_Active:
   1.249 +			// Deliberate fall through - set err=KErrNotReady for PlayInit when already active
   1.250 +		default:
   1.251 +			err = KErrNotReady;
   1.252 +			break;
   1.253 +		}
   1.254 +
   1.255 +	if(err == KErrNone)
   1.256 +		{
   1.257 +		iDevAudio->iStop = EFalse;
   1.258 +		}
   1.259 +	
   1.260 +	DP0_RET(err,"%d");
   1.261 +	}
   1.262 +
   1.263 +// -----------------------------------------------------------------------------
   1.264 +// CDevPlayControl::ProcessData
   1.265 +// -----------------------------------------------------------------------------
   1.266 +//
   1.267 +void CDevPlayControl::ProcessData()
   1.268 +	{
   1.269 +	DP_CONTEXT(CDevPlayControl::ProcessData *CD1*, CtxDevSound, DPLOCAL);
   1.270 +	DP_IN();
   1.271 +	TInt err(KErrNone);
   1.272 +	
   1.273 +	if(!iDevAudio->iStop)
   1.274 +		{
   1.275 +		err = iBufferSource->BufferFilled(iBuffer);
   1.276 +		if ( err != KErrNone)
   1.277 +			{
   1.278 +			iErrorCondition = err;
   1.279 +			Stop();
   1.280 +			}
   1.281 +		}
   1.282 +
   1.283 +	DP_OUT();
   1.284 +	}
   1.285 +
   1.286 +// -----------------------------------------------------------------------------
   1.287 +// From class MAudioDataSupplier
   1.288 +// CDevPlayControl::BufferToBeFilled
   1.289 +// -----------------------------------------------------------------------------
   1.290 +//
   1.291 +void CDevPlayControl::BufferToBeFilled(MMMFBufferSource* aSource, CMMFBuffer* aBuffer)
   1.292 +	{
   1.293 +	DP_CONTEXT(CDevPlayControl::BufferToBeFilled *CD1*, CtxDevSound, DPLOCAL);
   1.294 +	DP_IN();
   1.295 +	PRF(PRF_ID, PRF_STOP, PRF_TIME, AA_DS_PlayInit, "");
   1.296 +	__ASSERT_ALWAYS(iBufferSource==aSource, CDevAudioControl::Panic(EBufferMismatch));
   1.297 +	iAdaptationObserver->BufferToBeFilled(aBuffer);
   1.298 +	iBuffer = aBuffer;
   1.299 +	DP_OUT();
   1.300 +	}
   1.301 +
   1.302 +// -----------------------------------------------------------------------------
   1.303 +// From class MAudioDataSupplier
   1.304 +// CDevPlayControl::DiscardBuffers
   1.305 +// -----------------------------------------------------------------------------
   1.306 +//
   1.307 +void CDevPlayControl::DiscardBuffers(MMMFBufferSource* /*aSource*/)
   1.308 +	{
   1.309 +	DP_CONTEXT(CDevPlayControl::DiscardBuffers *CD1*, CtxDevSound, DPLOCAL);
   1.310 +	DP_IN();
   1.311 +
   1.312 +	DP_OUT();
   1.313 +	}
   1.314 +
   1.315 +// -----------------------------------------------------------------------------
   1.316 +// CDevPlayControl::ProcessingFinished
   1.317 +// -----------------------------------------------------------------------------
   1.318 +//
   1.319 +void CDevPlayControl::ProcessingFinished(MAudioStream& /*aStream*/)
   1.320 +	{
   1.321 +	DP_CONTEXT(CDevPlayControl::ProcessingFinished *CD1*, CtxDevSound, DPLOCAL);
   1.322 +	DP_IN();
   1.323 +	PRF(PRF_ID, PRF_STOP, PRF_LOAD, AA_DS_Play, "");
   1.324 +	
   1.325 +	if(iCallbackFromAdaptor == KCallbackNone)
   1.326 +		{
   1.327 +		iCallbackFromAdaptor = KCallbackProcessingFinished;
   1.328 +		iAdaptationObserver->CallbackFromAdaptorReceived(iCallbackFromAdaptor, KErrNone);
   1.329 +		}
   1.330 +	else
   1.331 +		{
   1.332 +		// Multiple callbacks from adaptor
   1.333 +		DP0(DLERR, "Multiple callbacks from adaptor");
   1.334 +		}
   1.335 +	DP_OUT();
   1.336 +	}
   1.337 +
   1.338 +
   1.339 +// -----------------------------------------------------------------------------
   1.340 +// CDevPlayControl::ProcessingFinishedReceived
   1.341 +// -----------------------------------------------------------------------------
   1.342 +//
   1.343 +TInt CDevPlayControl::ProcessingFinishedReceived(TBool& aAyncOperation)
   1.344 +	{
   1.345 +	DP_CONTEXT(CDevPlayControl::ProcessingFinishedReceived *CD1*, CtxDevSound, DPLOCAL);
   1.346 +	DP_IN();
   1.347 +	aAyncOperation = EFalse;
   1.348 +	TInt err = iDevAudio->iAudioStream->Stop();
   1.349 +	if ( err == KErrNone)
   1.350 +		{
   1.351 +		err = iDevAudio->CommitAudioContext();
   1.352 +		if(err == KErrNone)
   1.353 +			{
   1.354 +			iDevAudio->iActiveState = EDevSoundAdaptorStopping;
   1.355 +			aAyncOperation = ETrue;
   1.356 +			}
   1.357 +		}
   1.358 +	// Something was wrong, clear the flag.
   1.359 +	if (err != KErrNone)
   1.360 +		{
   1.361 +		iCallbackFromAdaptor = KCallbackNone;
   1.362 +		}
   1.363 +	DP0_RET(err,"%d");
   1.364 +	}
   1.365 +
   1.366 +// -----------------------------------------------------------------------------
   1.367 +// CDevPlayControl::BufferErrorEvent
   1.368 +// -----------------------------------------------------------------------------
   1.369 +//
   1.370 +void CDevPlayControl::BufferErrorEvent()
   1.371 +	{
   1.372 +	ProcessingUnitError(NULL,KErrNoMemory);
   1.373 +	}
   1.374 +
   1.375 +
   1.376 +// -----------------------------------------------------------------------------
   1.377 +// CDevPlayControl::FinishWithError
   1.378 +// -----------------------------------------------------------------------------
   1.379 +//
   1.380 +void CDevPlayControl::FinishWithError(TInt aError)
   1.381 +    {
   1.382 +    iAdaptationObserver->PlayError(aError);
   1.383 +    }
   1.384 +
   1.385 +// ---------------------------------------------------------------------------
   1.386 +// CDevPlayControl::ProcessingError
   1.387 +// ---------------------------------------------------------------------------
   1.388 +TInt CDevPlayControl::ProcessingError(TBool& aAyncOperation)
   1.389 +    {
   1.390 +    TInt err = ProcessingFinishedReceived(aAyncOperation);
   1.391 +    return err;
   1.392 +    }
   1.393 +    
   1.394 +// End of file
   1.395 +