1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmhais/a3facl/src/audiocontext/audiocontext.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,559 @@
1.4 +//audiocontext.cpp
1.5 +
1.6 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.7 +// All rights reserved.
1.8 +// This component and the accompanying materials are made available
1.9 +// under the terms of "Eclipse Public License v1.0"
1.10 +// which accompanies this distribution, and is available
1.11 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.12 +//
1.13 +// Initial Contributors:
1.14 +// Nokia Corporation - initial contribution.
1.15 +//
1.16 +// Contributors:
1.17 +//
1.18 +// Description:
1.19 +//
1.20 +
1.21 +
1.22 +
1.23 +#include <a3f/maudiostream.h>
1.24 +#include <a3f/maudiocodec.h>
1.25 +#include <a3f/maudiogaincontrol.h>
1.26 +#include <a3f/a3fbase.h>
1.27 +
1.28 +#include "audiocontext.h"
1.29 +#include "audioprocessingunit.h"
1.30 +#include "logicalaudiostream.h"
1.31 +#include "audiostreammanager.h"
1.32 +
1.33 +
1.34 +// ---------------------------------------------------------------------------
1.35 +// Default constructor
1.36 +// ---------------------------------------------------------------------------
1.37 +//
1.38 +CAudioContext::CAudioContext()
1.39 + {
1.40 + TRACE_CREATE();
1.41 + DP_CONTEXT(CAudioContext::CAudioContext *CD1*, CtxDevSound, DPLOCAL);
1.42 + DP_IN();
1.43 + iInCommit = EFalse;
1.44 + DP_OUT();
1.45 + }
1.46 +
1.47 +// ---------------------------------------------------------------------------
1.48 +// Symbian 2nd phase constructor
1.49 +// ---------------------------------------------------------------------------
1.50 +void CAudioContext::ConstructL()
1.51 + {
1.52 + DP_CONTEXT(CAudioContext::ConstructL *CD1*, CtxDevSound, DPLOCAL);
1.53 + DP_IN();
1.54 +
1.55 + // Setup Multimedia Resource Control
1.56 + User::LeaveIfError(iMMRC.Open(*this));
1.57 + User::LeaveIfError(iCriticalSection.CreateLocal());
1.58 + DP_OUT();
1.59 + }
1.60 +
1.61 +// ---------------------------------------------------------------------------
1.62 +// Symbian constructor
1.63 +// ---------------------------------------------------------------------------
1.64 +EXPORT_C CAudioContext* CAudioContext::NewL()
1.65 + {
1.66 + DP_STATIC_CONTEXT(CAudioContext::NewL *CD0*, CtxDevSound, DPLOCAL);
1.67 + DP_IN();
1.68 + CAudioContext* self = new(ELeave)CAudioContext();
1.69 + CleanupStack::PushL(self);
1.70 + self->ConstructL();
1.71 + CleanupStack::Pop(self);
1.72 + DP0_RET(self, "0x%x");
1.73 + }
1.74 +
1.75 +// ---------------------------------------------------------------------------
1.76 +// Destructor
1.77 +// ---------------------------------------------------------------------------
1.78 +//
1.79 +EXPORT_C CAudioContext::~CAudioContext()
1.80 + {
1.81 + DP_CONTEXT(CAudioContext::~CAudioContext *CD1*, CtxDevSound, DPLOCAL);
1.82 + DP_IN();
1.83 + iMMRC.Close();
1.84 + iAudioContextObservers.Close();
1.85 + iAudioProcessingUnits.Close();
1.86 + if (iActualChain)
1.87 + {
1.88 + iActualChain->Release();
1.89 + };
1.90 + iCriticalSection.Close();
1.91 + DP_OUT();
1.92 + }
1.93 +
1.94 +// ---------------------------------------------------------------------------
1.95 +// From class MAudioContext
1.96 +// CAudioContext::SetClientSettings
1.97 +// ---------------------------------------------------------------------------
1.98 +//
1.99 +TInt CAudioContext::SetClientSettings(const TClientContextSettings& aSettings)
1.100 + {
1.101 + DP_CONTEXT(CAudioContext::SetClientSettings *CD1*, CtxDevSound, DPLOCAL);
1.102 + DP_IN();
1.103 + TInt err(KErrNone);
1.104 + iClientSettings = aSettings;
1.105 + if( iContextId.Id() == 0 )
1.106 + {
1.107 + TUint64 id = iMMRC.LogOn(iClientSettings.iProcessId);
1.108 + TAudioContextId temp(id);
1.109 + iContextId = temp;
1.110 + }
1.111 + else
1.112 + {
1.113 + err = KErrAlreadyExists;
1.114 + }
1.115 + DP0_RET(err, "%d");
1.116 + }
1.117 +
1.118 +
1.119 +// ---------------------------------------------------------------------------
1.120 +// From class MAudioContext
1.121 +// CAudioContext::GetContextId
1.122 +// ---------------------------------------------------------------------------
1.123 +//
1.124 +TAudioContextId CAudioContext::ContextId() const
1.125 + {
1.126 + DP_CONTEXT(CAudioContext::ContextId *CD1*, CtxDevSound, DPLOCAL);
1.127 + DP_IN();
1.128 + DP_OUT();
1.129 + return iContextId;
1.130 + }
1.131 +
1.132 +// ---------------------------------------------------------------------------
1.133 +// From class MAudioContext
1.134 +// CAudioContext::Commit
1.135 +// ---------------------------------------------------------------------------
1.136 +//
1.137 +TInt CAudioContext::Commit()
1.138 + {
1.139 + DP_CONTEXT(CAudioContext::Commit *CD1*, CtxDevSound, DPLOCAL);
1.140 + DP_IN();
1.141 + TInt err(KErrNone);
1.142 + iInCommit = ETrue;
1.143 +
1.144 + if (iClientSettings.iProcessId.Id() == 0)
1.145 + {
1.146 + DP0_RET(KErrNotReady, "%d");
1.147 + }
1.148 +
1.149 + err = iMMRC.SendResourceRequest(iActualChain, iDesiredChain, this);
1.150 + DP0_RET(err, "%d");
1.151 + }
1.152 +
1.153 +// ---------------------------------------------------------------------------
1.154 +// From class MAudioContext
1.155 +// CAudioContext::CreateAudioStream
1.156 +// ---------------------------------------------------------------------------
1.157 +//
1.158 +TInt CAudioContext::CreateAudioStream(MAudioStream *&aStream)
1.159 + {
1.160 + DP_CONTEXT(CAudioContext::CreateAudioStream *CD1*, CtxDevSound, DPLOCAL);
1.161 + DP_IN();
1.162 + TInt err(KErrNone);
1.163 +
1.164 + CAudioStreamManager* manager(NULL);
1.165 +
1.166 + // Create desired logical chain
1.167 + TRAP(err, manager = CAudioStreamManager::NewL(KUidAudioStream));
1.168 + if(err == KErrNone)
1.169 + {
1.170 + aStream = static_cast<MAudioStream*>(manager);
1.171 +
1.172 + // Use this as desired chain (this is the one that the client modify)
1.173 + CLogicalAudioStream* logicalAudioStream = static_cast<CLogicalAudioStream*>(aStream);
1.174 + iDesiredChain = static_cast<MLogicalChain*>(logicalAudioStream);
1.175 + logicalAudioStream->SetParentContext(*this);
1.176 + if (iActualChain)
1.177 + {
1.178 + iActualChain->Release();
1.179 + iActualChain=NULL;
1.180 + }
1.181 + TRAP(err, iActualChain = iDesiredChain->CloneL());
1.182 + }
1.183 + DP0_RET(err, "%d");
1.184 + }
1.185 +
1.186 +// ---------------------------------------------------------------------------
1.187 +// From class MAudioContext
1.188 +// CAudioContext::DeleteAudioStream
1.189 +// ---------------------------------------------------------------------------
1.190 +//
1.191 +void CAudioContext::DeleteAudioStream(MAudioStream *&aStream)
1.192 + {
1.193 + DP_CONTEXT(CAudioContext::DeleteAudioStream *CD1*, CtxDevSound, DPLOCAL);
1.194 + DP_IN();
1.195 + CAudioStreamManager* manager = static_cast<CAudioStreamManager*>(aStream);
1.196 + delete manager;
1.197 + DP_OUT();
1.198 + }
1.199 +
1.200 +// ---------------------------------------------------------------------------
1.201 +// From class MAudioContext
1.202 +// CAudioContext::CreateAudioProcessingUnit
1.203 +// ---------------------------------------------------------------------------
1.204 +//
1.205 +TInt CAudioContext::CreateAudioProcessingUnit(TUid aTypeId, MAudioProcessingUnit *&aProcessingUnit)
1.206 + {
1.207 + DP_CONTEXT(CAudioContext::CreateAudioProcessingUnit *CD1*, CtxDevSound, DPLOCAL);
1.208 + DP_IN();
1.209 + TInt err(KErrNone);
1.210 + TAudioComponentId unitInstance = GetAndSetInstanceID();
1.211 +
1.212 + CAudioProcessingUnit* pUnit=NULL;
1.213 + TComponentParameters cParameters;
1.214 + cParameters.iTypeUid = aTypeId;
1.215 + cParameters.iInstanceId = unitInstance;
1.216 + cParameters.iContextId = iContextId;
1.217 + cParameters.iSettingsObserver = this;
1.218 +
1.219 + TRAP(err, pUnit = CAudioProcessingUnit::NewL(cParameters));
1.220 + if (err==KErrNone)
1.221 + {
1.222 + aProcessingUnit = static_cast<MAudioProcessingUnit*>(pUnit);
1.223 + err = iAudioProcessingUnits.Append(aProcessingUnit);
1.224 + if(err!=KErrNone)
1.225 + {
1.226 + delete pUnit;
1.227 + aProcessingUnit = NULL;
1.228 + }
1.229 + }
1.230 + DP0_RET(err, "%d");
1.231 + }
1.232 +
1.233 +// ---------------------------------------------------------------------------
1.234 +// From class MAudioContext
1.235 +// CAudioContext::DeleteAudioProcessingUnit
1.236 +// ---------------------------------------------------------------------------
1.237 +//
1.238 +void CAudioContext::DeleteAudioProcessingUnit(
1.239 + MAudioProcessingUnit *&aProcessingUnit)
1.240 + {
1.241 + DP_CONTEXT(CAudioContext::DeleteAudioProcessingUnit *CD1*, CtxDevSound, DPLOCAL);
1.242 + DP_IN();
1.243 + DP_OUT();
1.244 + CAudioProcessingUnit* cUnit = static_cast<CAudioProcessingUnit*>(aProcessingUnit);
1.245 + TAudioComponentId param = aProcessingUnit->InstanceId();
1.246 + TUint count= iAudioProcessingUnits.Count();
1.247 +
1.248 + for ( TUint i(0); i < count; i++ )
1.249 + {
1.250 + // find and delete component
1.251 + if( iAudioProcessingUnits[i]->InstanceId() == param)
1.252 + {
1.253 + iAudioProcessingUnits.Remove(i);
1.254 + delete cUnit;
1.255 + break;
1.256 + }
1.257 + }
1.258 + }
1.259 +
1.260 +// ---------------------------------------------------------------------------
1.261 +// From class MAudioContext
1.262 +// CAudioContext::RegisterAudioContextObserver
1.263 +// ---------------------------------------------------------------------------
1.264 +//
1.265 +TInt CAudioContext::RegisterAudioContextObserver(
1.266 + MAudioContextObserver& aObserver)
1.267 + {
1.268 + DP_CONTEXT(CAudioContext::RegisterAudioContextObserver *CD1*, CtxDevSound, DPLOCAL);
1.269 + DP_IN();
1.270 + TInt err = iAudioContextObservers.Find(&aObserver);
1.271 + if( err != KErrNotFound )
1.272 + {
1.273 + err = KErrAlreadyExists;
1.274 + }
1.275 + else
1.276 + {
1.277 + err = iAudioContextObservers.Append(&aObserver);
1.278 + }
1.279 + DP0_RET(err, "%d");
1.280 + }
1.281 +
1.282 +// ---------------------------------------------------------------------------
1.283 +// From class MAudioContext
1.284 +// CAudioContext::UnregisterAudioContextObserver
1.285 +// ---------------------------------------------------------------------------
1.286 +//
1.287 +void CAudioContext::UnregisterAudioContextObserver(
1.288 + MAudioContextObserver& aObserver)
1.289 + {
1.290 + DP_CONTEXT(CAudioContext::UnregisterAudioContextObserver *CD1*, CtxDevSound, DPLOCAL);
1.291 + DP_IN();
1.292 +
1.293 + TInt idxOrErr = iAudioContextObservers.Find(&aObserver);
1.294 + if( idxOrErr != KErrNotFound )
1.295 + {
1.296 + iAudioContextObservers.Remove(idxOrErr);
1.297 + }
1.298 + DP_OUT();
1.299 + }
1.300 +
1.301 +// ---------------------------------------------------------------------------
1.302 +// From class MAudioContext
1.303 +// CAudioContext::Interface
1.304 +// ---------------------------------------------------------------------------
1.305 +//
1.306 +TAny* CAudioContext::Interface(TUid aType)
1.307 + {
1.308 + DP_CONTEXT(CAudioContext::Interface *CD1*, CtxDevSound, DPLOCAL);
1.309 + DP_IN();
1.310 + TAny* interface = NULL;
1.311 + if(aType == KUIdAudioResourceNotification)
1.312 + {
1.313 + #ifndef SYMBIAN_DISABLE_ARN
1.314 + interface = static_cast<MA3FDevSoundAutoPauseResume*>(this);
1.315 + #else
1.316 + interface = NULL;
1.317 + #endif
1.318 + }
1.319 + else
1.320 + {
1.321 + interface = NULL;
1.322 + }
1.323 + DP_OUT();
1.324 + return interface;
1.325 + }
1.326 +
1.327 +
1.328 +// ---------------------------------------------------------------------------
1.329 +// From class MAudioContext
1.330 +// CAudioContext::Reset
1.331 +// ---------------------------------------------------------------------------
1.332 +//
1.333 +TInt CAudioContext::Reset()
1.334 + {
1.335 + DP_CONTEXT(CAudioContext::Reset *CD1*, CtxDevSound, DPLOCAL);
1.336 + DP_IN();
1.337 + TInt err = KErrNone;
1.338 + if (iInCommit)
1.339 + {
1.340 + err = KErrNotReady;
1.341 + }
1.342 + iDesiredChain->ResetMessage();
1.343 + iDesiredChain->CopySettings(*iActualChain);
1.344 + DP0_RET(err, "%d");
1.345 + }
1.346 +
1.347 +// ---------------------------------------------------------------------------
1.348 +// From class MMultimediaResourceControlObserver
1.349 +// CAudioContext::ReceiveResourceResponse
1.350 +// ---------------------------------------------------------------------------
1.351 +//
1.352 +void CAudioContext::ReceiveResourceResponse(MLogicalChain* aAppliedChain, TInt aError)
1.353 + {
1.354 + DP_CONTEXT(CAudioContext::ReceiveResourceResponse *CD1*, CtxDevSound, DPLOCAL);
1.355 + DP_IN();
1.356 +
1.357 + iCriticalSection.Wait();
1.358 + iMsgVersion++;
1.359 + iMMRC.ResetMessages();
1.360 + iCriticalSection.Signal();
1.361 +
1.362 + if ( aError == KErrNone || iPreempted )
1.363 + {
1.364 + if ( iPreempted )
1.365 + {
1.366 + iPreempted = EFalse;
1.367 + }
1.368 + // Desired chain was commited succesfully
1.369 + TInt err;
1.370 + MLogicalChain* lc = NULL;
1.371 + TRAP(err, lc = aAppliedChain->CloneL());
1.372 + DP1(DLERR,"ECloneLeftWhenReceivingResourceResponse %d", err);
1.373 + __ASSERT_DEBUG(err == KErrNone, Panic(ECloneLeftWhenReceivingResourceResponse));
1.374 + if ( iActualChain )
1.375 + {
1.376 + iActualChain->Release();
1.377 + }
1.378 + iActualChain = lc;
1.379 + }
1.380 + else
1.381 + {
1.382 + iDesiredChain->CopySettings(*iActualChain);
1.383 + }
1.384 + iDesiredChain->ResetMessage();
1.385 + ContextEventSignal(KUidA3FContextUpdateComplete, aError);
1.386 + iInCommit = EFalse;
1.387 + DP_OUT();
1.388 + }
1.389 +
1.390 +// ---------------------------------------------------------------------------
1.391 +// From class MMultimediaResourceControlObserver
1.392 +// CAudioContext::ReceiveResourceUpdate
1.393 +// ---------------------------------------------------------------------------
1.394 +//
1.395 +void CAudioContext::ReceiveResourceUpdate(MLogicalChain* /*aMessage*/, TInt aError)
1.396 + {
1.397 + DP_CONTEXT(CAudioContext::ReceiveResourceUpdate *CD1*, CtxDevSound, DPLOCAL);
1.398 + DP_IN();
1.399 + ContextEventSignal(KUidA3FContextCommitUpdate, aError);
1.400 + DP_OUT();
1.401 + }
1.402 +
1.403 +
1.404 +// ---------------------------------------------------------------------------
1.405 +// From class MMultimediaResourceControlObserver
1.406 +// CAudioContext::ReceivePreemptionUpdate
1.407 +// ---------------------------------------------------------------------------
1.408 +//
1.409 +void CAudioContext::ReceivePreemptionUpdate(MLogicalChain* /*aMessage*/, TInt aError)
1.410 + {
1.411 + DP_CONTEXT(CAudioContext::ReceivePreemptionUpdate *CD1*, CtxDevSound, DPLOCAL);
1.412 + DP_IN();
1.413 + iPreempted = ETrue;
1.414 + if(iInCommit)
1.415 + {
1.416 + // Send only one callback
1.417 + ContextEventSignal(KUidA3FContextPreEmptedCommit, aError);
1.418 + }
1.419 + else
1.420 + {
1.421 + // Send two callbacks
1.422 + ContextEventSignal(KUidA3FContextPreEmption, aError);
1.423 + ContextEventSignal(KUidA3FContextPreEmptionUpdate, aError);
1.424 + }
1.425 + DP_OUT();
1.426 + }
1.427 +
1.428 +
1.429 +// ---------------------------------------------------------------------------
1.430 +// From class MLogicalSettingObserver
1.431 +// CAudioContext::ReceiveComponentSettingsChange
1.432 +// ---------------------------------------------------------------------------
1.433 +void CAudioContext::ReceiveComponentSettingsChange(TUid /*aId*/, TMMRCMessageType aMessageType)
1.434 + {
1.435 + iDesiredChain->SetMessageType(aMessageType);
1.436 + }
1.437 +
1.438 +// ---------------------------------------------------------------------------
1.439 +// Internals
1.440 +// CAudioContext::GetAndSetInstanceID
1.441 +// ---------------------------------------------------------------------------
1.442 +TAudioComponentId CAudioContext::GetAndSetInstanceID()
1.443 + {
1.444 + DP_CONTEXT(CAudioContext::GetAndSetInstanceID *CD1*, CtxDevSound, DPLOCAL);
1.445 + DP_IN();
1.446 + TUint id(1); // start ID count from 1
1.447 + TAudioComponentId idComponent(1);
1.448 + TUint count = iAudioProcessingUnits.Count();
1.449 + if ( count < KMaxTUint )
1.450 + {
1.451 + id = ++count;
1.452 + }
1.453 + idComponent = id;
1.454 + DP_OUT();
1.455 + return idComponent;
1.456 + }
1.457 +
1.458 +// ---------------------------------------------------------------------------
1.459 +// Internals
1.460 +// CAudioContext::ContextEventSignal
1.461 +// ---------------------------------------------------------------------------
1.462 +void CAudioContext::ContextEventSignal(TUid aEvent, TInt aError)
1.463 + {
1.464 + DP_CONTEXT(CAudioContext::ContextEventSignal *CD1*, CtxDevSound, DPLOCAL);
1.465 + DP_IN();
1.466 + TUint count = iAudioContextObservers.Count();
1.467 + for ( TUint idx(0); idx < count; idx++ )
1.468 + {
1.469 + iAudioContextObservers[idx]->ContextEvent(aEvent, aError);
1.470 + }
1.471 + DP_OUT();
1.472 + }
1.473 +
1.474 +// ---------------------------------------------------------------------------
1.475 +// CAudioContext::GetLogicalChain
1.476 +// ---------------------------------------------------------------------------
1.477 +EXPORT_C MLogicalChain* CAudioContext::GetLogicalChain(TInt aIndex)
1.478 + {
1.479 + DP_CONTEXT(CAudioContext::GetLogicalChain *CD1*, CtxDevSound, DPLOCAL);
1.480 + DP_IN();
1.481 + MLogicalChain* logChain = NULL;
1.482 + if (aIndex == 0)
1.483 + {
1.484 + logChain = iDesiredChain;
1.485 + }
1.486 + else
1.487 + {
1.488 + logChain = iActualChain;
1.489 + }
1.490 + DP_OUT();
1.491 + return logChain;
1.492 + }
1.493 +
1.494 +
1.495 +// ---------------------------------------------------------------------------
1.496 +// CAudioContext::RegisterAsClient
1.497 +// ---------------------------------------------------------------------------
1.498 +TInt CAudioContext::RegisterAsClient(TUid aEventType, const TDesC8& aNotificationRegistrationData, MA3FDevSoundAutoPauseResumeObserver* aObserverPtr)
1.499 + {
1.500 + DP_CONTEXT(CAudioContext::RegisterAsClient *CD1*, CtxDevSound, DPLOCAL);
1.501 + DP_IN();
1.502 + TInt err(KErrNone);
1.503 + iObserverPtr = aObserverPtr;
1.504 + err = iMMRC.RegisterAsClient(aEventType, aNotificationRegistrationData);
1.505 + DP0_RET(err, "%d");
1.506 + }
1.507 +
1.508 +// ---------------------------------------------------------------------------
1.509 +// CAudioContext::CancelRegisterAsClient
1.510 +// ---------------------------------------------------------------------------
1.511 +TInt CAudioContext::CancelRegisterAsClient(TUid aEventType)
1.512 + {
1.513 + DP_CONTEXT(CAudioContext::CancelRegisterAsClient *CD1*, CtxDevSound, DPLOCAL);
1.514 + DP_IN();
1.515 + TInt err(KErrNone);
1.516 + err = iMMRC.CancelRegisterAsClient(aEventType);
1.517 + DP0_RET(err, "%d");
1.518 + }
1.519 +
1.520 +// ---------------------------------------------------------------------------
1.521 +// CAudioContext::WillResumePlay
1.522 +// ---------------------------------------------------------------------------
1.523 +TInt CAudioContext::WillResumePlay()
1.524 + {
1.525 + DP_CONTEXT(CAudioContext::WillResumePlay *CD1*, CtxDevSound, DPLOCAL);
1.526 + DP_IN();
1.527 + TInt err(KErrNone);
1.528 + err = iMMRC.WillResumePlay();
1.529 + DP0_RET(err, "%d");
1.530 + }
1.531 +
1.532 +// ---------------------------------------------------------------------------
1.533 +// From class MMultimediaResourceControlObserver
1.534 +// CAudioContext::CanResume
1.535 +// ---------------------------------------------------------------------------
1.536 +//
1.537 +void CAudioContext::CanResume()
1.538 + {
1.539 + DP_CONTEXT(CAudioContext::CanResume *CD1*, CtxDevSound, DPLOCAL);
1.540 + DP_IN();
1.541 + if(iObserverPtr)
1.542 + {
1.543 + iObserverPtr->NotifyResume();
1.544 + }
1.545 + DP_OUT();
1.546 + }
1.547 +
1.548 +void CAudioContext::Panic(TAudioContextPanicCodes aCode)
1.549 + {
1.550 + User::Panic(KAudioContextPanicCategory, aCode);
1.551 + }
1.552 +
1.553 +EXPORT_C TInt CAudioContext::MsgVersion()
1.554 + {
1.555 + return iMsgVersion;
1.556 + }
1.557 +
1.558 +EXPORT_C RCriticalSection& CAudioContext::CriticalSection()
1.559 + {
1.560 + return iCriticalSection;
1.561 + }
1.562 +// End of file