Update contrib.
3 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
4 // All rights reserved.
5 // This component and the accompanying materials are made available
6 // under the terms of "Eclipse Public License v1.0"
7 // which accompanies this distribution, and is available
8 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
10 // Initial Contributors:
11 // Nokia Corporation - initial contribution.
16 // Portions Copyright Nokia-Symbian * Nokia Core OS *
21 #include <mmf/common/mmfbase.hrh>
23 #include "mmfaudioserver.h"
24 #include "mmfaudioserverdecs.h"
25 #include "mmfdevsoundsession.h"
26 #include "mmfaudioserversession.h"
27 #include "mmfdevsoundserver.h"
28 #include "mmfdevsoundserverstart.h"
29 #include "mmfaudioserverfactory.h"
30 #include "devsoundsvrthreadpriorityconsts.h"
34 #define SYMBIAN_DEBPRN0(str) RDebug::Print(str, this)
36 #define SYMBIAN_DEBPRN0(str)
40 // Time delay that the audio server wait before shutdown itself when is detected that the last DevSoundSession is closed
41 const TInt KAudioServerShutDownDelay = 50000000; //50 sec
43 // ======== LOCAL FUNCTIONS ========
45 void Panic(TInt aPanicCode)
47 _LIT(KAudioServerPanicCategory, "mmfaudioserver");
48 User::Panic(KAudioServerPanicCategory, aPanicCode);
51 // ============================ MEMBER FUNCTIONS ===============================
53 // -----------------------------------------------------------------------------
54 // CMMFAudioServer::NewL
55 // Two-phased constructor.
56 // -----------------------------------------------------------------------------
58 CMMFAudioServer* CMMFAudioServer::NewL()
60 CMMFAudioServer* self = new(ELeave) CMMFAudioServer();
61 CleanupStack::PushL(self);
63 CleanupStack::Pop(self);
67 // -----------------------------------------------------------------------------
68 // CMMFAudioServer::CMMFAudioServer
69 // C++ default constructor can NOT contain any code, that
71 // -----------------------------------------------------------------------------
73 CMMFAudioServer::CMMFAudioServer()
74 : CMmfIpcServer(EPriorityStandard)
78 // -----------------------------------------------------------------------------
79 // CMMFAudioServer::ConstructL
80 // Symbian 2nd phase constructor can leave.
81 // -----------------------------------------------------------------------------
83 void CMMFAudioServer::ConstructL()
85 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::ConstructL - enter"));
87 SetPinClientDescriptors(ETrue);
88 // Call base class to Start server
89 StartL(KAudioServerName);
91 iFourCCConvertor = CFourCCConvertor::NewL();
92 iDelayAudioServerShutDown = CDelayAudioServerShutDown::NewL();
94 iFactory = CMMFAudioServerFactory::NewL();
95 iFactory->StartL(*this);
97 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::ConstructL - exit"));
100 // -----------------------------------------------------------------------------
101 // CMMFAudioServer::~CMMFAudioServer
103 // -----------------------------------------------------------------------------
105 CMMFAudioServer::~CMMFAudioServer()
107 if (iDelayAudioServerShutDown)
109 iDelayAudioServerShutDown->Cancel();
110 delete iDelayAudioServerShutDown;
111 iDelayAudioServerShutDown = NULL;
114 for(TInt i=(iDevSoundServList.Count()-1); i >=0 ; i--)
116 CStartAndMonitorDevSoundThread* devSoundMonitorThread = iDevSoundServList[i];
117 iDevSoundServList.Remove(i);
118 delete devSoundMonitorThread;
120 iDevSoundServList.Close();
121 delete iFourCCConvertor;
124 iFactory->Stop(*this);
129 // -----------------------------------------------------------------------------
130 // CMMFAudioServer::NewSessionL
131 // Creates a new session to handle requests by the client and returns it to the
132 // client server framework.
133 // -----------------------------------------------------------------------------
135 CMmfIpcSession* CMMFAudioServer::NewSessionL(const TVersion& aVersion) const
137 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::NewSessionL - enter"));
139 TVersion v(KMMFAudioServerVersion,
140 KMMFAudioServerMinorVersionNumber,
141 KMMFAudioServerBuildVersionNumber);
142 if(!User::QueryVersionSupported(v, aVersion))
144 User::Leave(KErrNotSupported);
147 for(TInt i=(iDevSoundServList.Count()-1); i >=0 ; i--)
149 CStartAndMonitorDevSoundThread* devSoundMonitorThread = iDevSoundServList[i];
151 if(!devSoundMonitorThread->IsActive())
153 iDevSoundServList.Remove(i);
154 delete devSoundMonitorThread;
156 iDevSoundServList.Compress();
159 RMMFDevSoundServerProxy devSoundSessionHandle;
160 User::LeaveIfError(StartDevSoundServer(devSoundSessionHandle));
161 CMMFAudioServerSession* audioServerSession = CMMFAudioServerSession::NewL(devSoundSessionHandle);
162 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::NewSessionL - session created"));
163 return audioServerSession;
166 // -----------------------------------------------------------------------------
167 // CMMFAudioServer::IncrementSessionId
168 // Increment the session Id.
169 // -----------------------------------------------------------------------------
171 void CMMFAudioServer::IncrementSessionId()
173 iAudioServerSessionId++;
176 // -----------------------------------------------------------------------------
177 // CMMFAudioServer::DecrementSessionId
178 // Decrement the session Id.
179 // -----------------------------------------------------------------------------
181 void CMMFAudioServer::DecrementSessionId()
183 iAudioServerSessionId--;
186 // -----------------------------------------------------------------------------
187 // CMMFAudioServer::IncrementDevSoundCount
188 // Increment the DevSound server counter. If there is atleast one DevSound
189 // server, it will cancel its shutdown timer.
190 // -----------------------------------------------------------------------------
192 void CMMFAudioServer::IncrementDevSoundCount()
195 //in the case we started the shutdown due to no more DevSound
198 ASSERT(iDelayAudioServerShutDown);
199 if (iDelayAudioServerShutDown)
201 iDelayAudioServerShutDown->Cancel();
206 // -----------------------------------------------------------------------------
207 // CMMFAudioServer::DecrementDevSoundCount
208 // Decrement the DevSound server counter. Once the number of DevSound server's
209 // instances becomes zero, Audio Server will start its shutdown routine.
210 // -----------------------------------------------------------------------------
212 void CMMFAudioServer::DecrementDevSoundCount()
215 if (iDevSoundCount == 0)
217 ASSERT(iDelayAudioServerShutDown);
218 if (iDelayAudioServerShutDown)
220 iDelayAudioServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KAudioServerShutDownDelay));
225 // -----------------------------------------------------------------------------
226 // CMMFAudioServer::SendEventToClient
227 // Sends Event to DevSound client.
228 // -----------------------------------------------------------------------------
230 void CMMFAudioServer::SendEventToClient(TInt aSessionToAlert, TInt /*aSessionToBeLaunched*/)
232 // For the session requested, send event to client
233 iSessionIter.SetToFirst();
234 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
237 if (session->AudioServerSessionId() == aSessionToAlert)
242 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
246 // -----------------------------------------------------------------------------
247 // CMMFAudioServer::LaunchRequest
248 // Launches the DevSound servers waiting for Audio Policy.
249 // -----------------------------------------------------------------------------
251 void CMMFAudioServer::LaunchRequest(TInt aSessionId)
253 iSessionIter.SetToFirst();
254 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
258 if (session->AudioServerSessionId() == aSessionId)
262 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
266 // -----------------------------------------------------------------------------
267 // CMMFAudioServer::StartDevSoundServer
268 // Starts a new DevSound server instance.
269 // -----------------------------------------------------------------------------
271 TInt CMMFAudioServer::StartDevSoundServer(RMMFDevSoundServerProxy& aDevSoundSessionHandle) const
273 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::StartDevSoundServer - enter"));
275 RMessage2 message(Message());
276 CStartAndMonitorDevSoundThread* monitorDevSound = NULL;
278 TRAPD(err, monitorDevSound = CStartAndMonitorDevSoundThread::NewL(const_cast<CMMFAudioServer*>(this)));
281 delete monitorDevSound;
285 err = iDevSoundServList.Append(monitorDevSound);
288 delete monitorDevSound;
292 SYMBIAN_DEBPRN0(_L("CMMFAudioServer[0x%x]::StartDevSoundServer - exit"));
293 return monitorDevSound->StartDevSoundServer(message, aDevSoundSessionHandle);
297 // -----------------------------------------------------------------------------
298 // CMMFAudioServer::CDelayAudioServerShutDown::NewL
299 // Two-phased constructor.
300 // -----------------------------------------------------------------------------
302 CMMFAudioServer::CDelayAudioServerShutDown*
303 CMMFAudioServer::CDelayAudioServerShutDown::NewL()
305 CDelayAudioServerShutDown* self = new(ELeave) CDelayAudioServerShutDown;
306 CleanupStack::PushL(self);
308 CleanupStack::Pop(self);
312 // -----------------------------------------------------------------------------
314 // -----------------------------------------------------------------------------
316 CMMFAudioServer::CDelayAudioServerShutDown::CDelayAudioServerShutDown()
321 // -----------------------------------------------------------------------------
322 // CMMFAudioServer::CDelayAudioServerShutDown::ConstructL
323 // -----------------------------------------------------------------------------
325 void CMMFAudioServer::CDelayAudioServerShutDown::ConstructL()
327 User::LeaveIfError(iShutDownTimer.CreateLocal());
328 CActiveScheduler::Add(this);
331 // -----------------------------------------------------------------------------
332 // CMMFAudioServer::CDelayAudioServerShutDown::~CDelayAudioServerShutDown
333 // -----------------------------------------------------------------------------
335 CMMFAudioServer::CDelayAudioServerShutDown::~CDelayAudioServerShutDown()
338 iShutDownTimer.Close();
341 // -----------------------------------------------------------------------------
342 // CMMFAudioServer::CDelayAudioServerShutDown::SetDelay
343 // Request a timeout after aDelay
344 // -----------------------------------------------------------------------------
346 void CMMFAudioServer::CDelayAudioServerShutDown::SetDelay(
347 TTimeIntervalMicroSeconds32 aDelay)
349 __ASSERT_ALWAYS(!IsActive(), Panic(EMMFAudioServerIsActive));
350 iShutDownTimer.After(iStatus, aDelay);
354 // -----------------------------------------------------------------------------
355 // CMMFAudioServer::CDelayAudioServerShutDown::RunL
356 // Called by Active object framework when timer times out.
357 // -----------------------------------------------------------------------------
359 void CMMFAudioServer::CDelayAudioServerShutDown::RunL()
361 CActiveScheduler::Stop();
364 // -----------------------------------------------------------------------------
365 // CMMFAudioServer::CDelayAudioServerShutDown::DoCancel
366 // Called by the Active object framework when user cancels active object.
367 // -----------------------------------------------------------------------------
369 void CMMFAudioServer::CDelayAudioServerShutDown::DoCancel()
371 iShutDownTimer.Cancel();
374 // -----------------------------------------------------------------------------
375 // CStartAndMonitorDevSoundThread::NewL
376 // -----------------------------------------------------------------------------
378 CStartAndMonitorDevSoundThread* CStartAndMonitorDevSoundThread::NewL(
379 CMMFAudioServer* aAudioServer)
381 CStartAndMonitorDevSoundThread* self = new(ELeave) CStartAndMonitorDevSoundThread(aAudioServer);
382 CleanupStack::PushL(self);
384 CleanupStack::Pop(self);
388 // -----------------------------------------------------------------------------
389 // CStartAndMonitorDevSoundThread::ConstructL
390 // -----------------------------------------------------------------------------
392 void CStartAndMonitorDevSoundThread::ConstructL()
394 CActiveScheduler::Add(this);
397 // -----------------------------------------------------------------------------
398 // CStartAndMonitorDevSoundThread::StartDevSoundServer
399 // Starts a new DevSound server.
400 // -----------------------------------------------------------------------------
402 TInt CStartAndMonitorDevSoundThread::StartDevSoundServer(
404 RMMFDevSoundServerProxy& aDevSoundSessionHandle)
406 SYMBIAN_DEBPRN0(_L("CStartAndMonitorDevSoundThread[0x%x]::StartDevSoundServer - enter"));
408 RThread clientThread;
411 // Open handle to client thread
412 err = aMessage.Client(clientThread);
415 clientThread.Close();
418 // Open a process-relative handle to the process which owns clientThread.
419 RProcess clientProcess;
420 err = clientThread.Process(clientProcess);
424 // Close client thread handle and return error
425 clientThread.Close();
429 // Get the client process id
430 TProcessId clientProcessId(clientProcess.Id());
432 // Close client thread and client process handles
433 clientThread.Close();
434 clientProcess.Close();
436 RServer2 devSoundServerHandle;
437 devSoundServerHandle.SetReturnedHandle(0);
439 //Pass to DevSoundServer the clien't process ID
440 TDevSoundServerStart start(iAudioServer,
442 devSoundServerHandle);
444 TThreadFunction serverFunc=CMMFDevSoundServer::StartThread;
446 // To deal with the unique thread (+semaphore!) naming in EPOC,
447 // and that we may be trying to restart a server that has just
448 // exited we attempt to create a unique thread name for the server.
449 #ifdef SYMBIAN_USE_SEPARATE_HEAPS
450 err = iServer.Create(KNullDesC,serverFunc,KDevSoundServerStackSize*2,
451 KDevSoundServerInitHeapSize,
452 KDevSoundServerMaxHeapSize,
456 err = iServer.Create(KNullDesC,
458 (KDevSoundServerStackSize*2),
462 #endif // SYMBIAN_USE_SEPARATE_HEAPS
467 iServer.SetPriority(KDevsoundSvrPriority);
469 // Synchronise with the server
470 TRequestStatus reqStatus;
471 iServer.Rendezvous(reqStatus);
473 if (reqStatus!=KRequestPending)
479 // Start the test harness
481 // Server will call the reciprocal static synchronise call
484 // note devSoundServerHandle really "owned" by client thread - just copied here
486 User::WaitForRequest(reqStatus); // wait for start or death
487 if(reqStatus.Int() != KErrNone)
489 // close our handle to the thread. If we reach here, the thread should have
490 // closed anyway as continue Rendezvous is always KErrNone()
492 return reqStatus.Int();
495 err = aDevSoundSessionHandle.Open(devSoundServerHandle);
499 // close our handle to the thread. If we reach here, this means that NewSessionL() failed
500 // on server, and that action itself will asynchronously stop the thread
505 aDevSoundSessionHandle.ShareProtected();
506 iServer.Logon(iStatus);
509 iAudioServer->IncrementDevSoundCount();
511 SYMBIAN_DEBPRN0(_L("CStartAndMonitorDevSoundThread[0x%x]::StartDevSoundServer - exit"));
515 // -----------------------------------------------------------------------------
516 // CStartAndMonitorDevSoundThread::CStartAndMonitorDevSoundThread
518 // -----------------------------------------------------------------------------
520 CStartAndMonitorDevSoundThread::CStartAndMonitorDevSoundThread(
521 CMMFAudioServer* aAudioServer)
522 : CActive(EPriorityStandard),iAudioServer(aAudioServer)
528 CStartAndMonitorDevSoundThread::~CStartAndMonitorDevSoundThread()
533 // -----------------------------------------------------------------------------
534 // CStartAndMonitorDevSoundThread::RunL
535 // Called by Active Object framework when DevSound server is destroyed.
536 // -----------------------------------------------------------------------------
538 void CStartAndMonitorDevSoundThread::RunL()
541 iAudioServer->DecrementDevSoundCount();
544 // -----------------------------------------------------------------------------
545 // CStartAndMonitorDevSoundThread::DoCancel
546 // Called by Active Object framework when client cancels active object.
547 // -----------------------------------------------------------------------------
549 void CStartAndMonitorDevSoundThread::DoCancel()
551 iServer.LogonCancel(iStatus);
555 // -----------------------------------------------------------------------------
556 // RMMFDevSoundServerProxy::Open
557 // Creates new audio server session for the devsound server.
558 // -----------------------------------------------------------------------------
560 TInt RMMFDevSoundServerProxy::Open(RServer2& aDevSoundServerHandle)
562 SYMBIAN_DEBPRN0(_L("RMMFDevSoundServerProxy[0x%x]::Open - enter"));
563 TInt err = CreateSession(aDevSoundServerHandle, TVersion(KMMFDevSoundServerVersion,
564 KMMFDevSoundServerMinorVersionNumber,
565 KMMFDevSoundServerBuildVersionNumber),
566 -1, EIpcSession_GlobalSharable);
568 SYMBIAN_DEBPRN0(_L("RMMFDevSoundServerProxy[0x%x]::Open - exit"));